Ruby 4.1.0dev (2026-10-18 revision 57509f1d218ddd43fc49c5af88fe0729726549cf)
parse.y (57509f1d218ddd43fc49c5af88fe0729726549cf)
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%{
13
14#if !YYPURE
15# error needs pure parser
16#endif
17#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20
21/* For Ripper */
22#ifdef RUBY_EXTCONF_H
23# include RUBY_EXTCONF_H
24#endif
25
26#include "ruby/internal/config.h"
27#include "internal/thread.h"
28
29#include <errno.h>
30
31#ifdef UNIVERSAL_PARSER
32
33#include "internal/ruby_parser.h"
34#include "parser_node.h"
35#include "universal_parser.c"
36
37#ifdef RIPPER
38#define STATIC_ID2SYM p->config->static_id2sym
39#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
40#endif
41
42#else
43
44#include "internal.h"
45#include "internal/array.h"
46#include "internal/compile.h"
47#include "internal/compilers.h"
48#include "internal/complex.h"
49#include "internal/encoding.h"
50#include "internal/error.h"
51#include "internal/gc.h"
52#include "internal/hash.h"
53#include "internal/io.h"
54#include "internal/numeric.h"
55#include "internal/parse.h"
56#include "internal/rational.h"
57#include "internal/re.h"
58#include "internal/ruby_parser.h"
59#include "internal/symbol.h"
60#include "internal/thread.h"
61#include "node.h"
62#include "parser_node.h"
63#include "probes.h"
64#include "regenc.h"
65#include "ruby/encoding.h"
66#include "ruby/regex.h"
67#include "ruby/ruby.h"
68#include "ruby/st.h"
69#include "ruby/util.h"
70#include "ruby/ractor.h"
71#include "symbol.h"
72
73#ifndef RIPPER
74static VALUE
75syntax_error_new(void)
76{
77 return rb_class_new_instance(0, 0, rb_eSyntaxError);
78}
79#endif
80
81static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
82
83#define compile_callback rb_suppress_tracing
84#endif /* !UNIVERSAL_PARSER */
85
86#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
87#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
88
89static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
90
91#ifndef RIPPER
92static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
93#endif
94
95static int
96node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
97{
98 return (n1->minus != n2->minus ||
99 n1->base != n2->base ||
100 strcmp(n1->val, n2->val));
101}
102
103static int
104node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
105{
106 return (n1->minus != n2->minus ||
107 strcmp(n1->val, n2->val));
108}
109
110static int
111node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
112{
113 return (n1->minus != n2->minus ||
114 n1->base != n2->base ||
115 n1->seen_point != n2->seen_point ||
116 strcmp(n1->val, n2->val));
117}
118
119static int
120node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
121{
122 return (n1->minus != n2->minus ||
123 n1->base != n2->base ||
124 n1->seen_point != n2->seen_point ||
125 n1->type != n2->type ||
126 strcmp(n1->val, n2->val));
127}
128
129static int
130rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
131{
132 return (n1->options != n2->options ||
133 rb_parser_string_hash_cmp(n1->string, n2->string));
134}
135
136static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
137static st_index_t rb_char_p_hash(const char *c);
138
139static int
140literal_cmp(st_data_t val, st_data_t lit)
141{
142 if (val == lit) return 0;
143
144 NODE *node_val = RNODE(val);
145 NODE *node_lit = RNODE(lit);
146 enum node_type type_val = nd_type(node_val);
147 enum node_type type_lit = nd_type(node_lit);
148
149 if (type_val != type_lit) {
150 return -1;
151 }
152
153 switch (type_lit) {
154 case NODE_INTEGER:
155 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
156 case NODE_FLOAT:
157 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
158 case NODE_RATIONAL:
159 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
160 case NODE_IMAGINARY:
161 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
162 case NODE_STR:
163 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
164 case NODE_SYM:
165 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
166 case NODE_REGX:
167 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
168 case NODE_LINE:
169 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
170 case NODE_FILE:
171 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
172 case NODE_ENCODING:
173 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
174 default:
175#ifdef UNIVERSAL_PARSER
176 abort();
177#else
178 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
179#endif
180 }
181}
182
183static st_index_t
184literal_hash(st_data_t a)
185{
186 NODE *node = (NODE *)a;
187 enum node_type type = nd_type(node);
188
189 switch (type) {
190 case NODE_INTEGER:
191 return rb_char_p_hash(RNODE_INTEGER(node)->val);
192 case NODE_FLOAT:
193 return rb_char_p_hash(RNODE_FLOAT(node)->val);
194 case NODE_RATIONAL:
195 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
196 case NODE_IMAGINARY:
197 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
198 case NODE_STR:
199 return rb_parser_str_hash(RNODE_STR(node)->string);
200 case NODE_SYM:
201 return rb_parser_str_hash(RNODE_SYM(node)->string);
202 case NODE_REGX:
203 return rb_parser_str_hash(RNODE_REGX(node)->string);
204 case NODE_LINE:
205 return (st_index_t)node->nd_loc.beg_pos.lineno;
206 case NODE_FILE:
207 return rb_parser_str_hash(RNODE_FILE(node)->path);
208 case NODE_ENCODING:
209 return (st_index_t)RNODE_ENCODING(node)->enc;
210 default:
211#ifdef UNIVERSAL_PARSER
212 abort();
213#else
214 rb_bug("unexpected node: %s", ruby_node_name(type));
215#endif
216 }
217}
218
219static inline int
220parse_isascii(int c)
221{
222 return '\0' <= c && c <= '\x7f';
223}
224
225#undef ISASCII
226#define ISASCII parse_isascii
227
228static inline int
229parse_isspace(int c)
230{
231 return c == ' ' || ('\t' <= c && c <= '\r');
232}
233
234#undef ISSPACE
235#define ISSPACE parse_isspace
236
237static inline int
238parse_iscntrl(int c)
239{
240 return ('\0' <= c && c < ' ') || c == '\x7f';
241}
242
243#undef ISCNTRL
244#define ISCNTRL(c) parse_iscntrl(c)
245
246static inline int
247parse_isupper(int c)
248{
249 return 'A' <= c && c <= 'Z';
250}
251
252static inline int
253parse_islower(int c)
254{
255 return 'a' <= c && c <= 'z';
256}
257
258static inline int
259parse_isalpha(int c)
260{
261 return parse_isupper(c) || parse_islower(c);
262}
263
264#undef ISALPHA
265#define ISALPHA(c) parse_isalpha(c)
266
267static inline int
268parse_isdigit(int c)
269{
270 return '0' <= c && c <= '9';
271}
272
273#undef ISDIGIT
274#define ISDIGIT(c) parse_isdigit(c)
275
276static inline int
277parse_isalnum(int c)
278{
279 return ISALPHA(c) || ISDIGIT(c);
280}
281
282#undef ISALNUM
283#define ISALNUM(c) parse_isalnum(c)
284
285static inline int
286parse_isxdigit(int c)
287{
288 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
289}
290
291#undef ISXDIGIT
292#define ISXDIGIT(c) parse_isxdigit(c)
293
294#include "parser_st.h"
295
296#undef STRCASECMP
297#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
298
299#undef STRNCASECMP
300#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
301
302#ifdef RIPPER
303#include "ripper_init.h"
304#endif
305
306enum rescue_context {
307 before_rescue,
308 after_rescue,
309 after_else,
310 after_ensure,
311};
312
313struct lex_context {
314 unsigned int in_defined: 1;
315 unsigned int in_kwarg: 1;
316 unsigned int in_argdef: 1;
317 unsigned int in_def: 1;
318 unsigned int in_class: 1;
319 unsigned int has_trailing_semicolon: 1;
320 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
321 BITFIELD(enum rescue_context, in_rescue, 2);
322 unsigned int cant_return: 1;
323 unsigned int in_alt_pattern: 1;
324 unsigned int capture_in_pattern: 1;
325};
326
327typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
328
329#if defined(__GNUC__) && !defined(__clang__)
330// Suppress "parameter passing for argument of type 'struct
331// lex_context' changed" notes. `struct lex_context` is file scope,
332// and has no ABI compatibility issue.
333RBIMPL_WARNING_PUSH()
334RBIMPL_WARNING_IGNORED(-Wpsabi)
335RBIMPL_WARNING_POP()
336// Not sure why effective even after popped.
337#endif
338
339#include "parse.h"
340
341#define NO_LEX_CTXT (struct lex_context){0}
342
343#ifndef WARN_PAST_SCOPE
344# define WARN_PAST_SCOPE 0
345#endif
346
347#define TAB_WIDTH 8
348
349#define yydebug (p->debug) /* disable the global variable definition */
350
351#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
352#define YY_LOCATION_PRINT(File, loc, p) \
353 rb_parser_printf(p, "%d.%d-%d.%d", \
354 (loc).beg_pos.lineno, (loc).beg_pos.column,\
355 (loc).end_pos.lineno, (loc).end_pos.column)
356#define YYLLOC_DEFAULT(Current, Rhs, N) \
357 do \
358 if (N) \
359 { \
360 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
361 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
362 } \
363 else \
364 { \
365 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
366 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
367 } \
368 while (0)
369#define YY_(Msgid) \
370 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
371 "nesting too deep" : (Msgid))
372
373#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
374 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
375#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
376 rb_parser_set_location_of_delayed_token(p, &(Current))
377#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
378 rb_parser_set_location_of_heredoc_end(p, &(Current))
379#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
380 rb_parser_set_location_of_dummy_end(p, &(Current))
381#define RUBY_SET_YYLLOC_OF_NONE(Current) \
382 rb_parser_set_location_of_none(p, &(Current))
383#define RUBY_SET_YYLLOC(Current) \
384 rb_parser_set_location(p, &(Current))
385#define RUBY_INIT_YYLLOC() \
386 { \
387 {p->ruby_sourceline, parser_column(p->lex.ptok - p->lex.pbeg)}, \
388 {p->ruby_sourceline, parser_column(p->lex.pcur - p->lex.pbeg)}, \
389 }
390
391/* Saturate instead of wrapping to a column before the line. */
392static inline int
393parser_column(rb_len_t pos)
394{
395#if SIZEOF_INT < SIZEOF_RB_LEN_T
396 if (pos > INT_MAX) return INT_MAX;
397#endif
398 return (int)pos;
399}
400
401#define IS_lex_state_for(x, ls) ((x) & (ls))
402#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
403#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
404#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
405
406# define SET_LEX_STATE(ls) \
407 parser_set_lex_state(p, ls, __LINE__)
408static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
409
410typedef VALUE stack_type;
411
412static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
413
414# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
415# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
416# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
417# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
418# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
419
420/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
421 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
422#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
423#define COND_POP() BITSTACK_POP(cond_stack)
424#define COND_P() BITSTACK_SET_P(cond_stack)
425#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
426
427/* A flag to identify keyword_do_block; "do" keyword after command_call.
428 Example: `foo 1, 2 do`. */
429#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
430#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
431#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
432#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
433
434struct vtable {
435 ID *tbl;
436 int pos;
437 int capa;
438 struct vtable *prev;
439};
440
441struct local_vars {
442 struct vtable *args;
443 struct vtable *vars;
444 struct vtable *used;
445# if WARN_PAST_SCOPE
446 struct vtable *past;
447# endif
448 struct local_vars *prev;
449 struct {
450 NODE *outer, *inner, *current;
451 } numparam;
452 NODE *it;
453};
454
455typedef struct rb_locations_lambda_body_t {
456 NODE *node;
457 YYLTYPE opening_loc;
458 YYLTYPE closing_loc;
459} rb_locations_lambda_body_t;
460
461enum {
462 ORDINAL_PARAM = -1,
463 NO_PARAM = 0,
464 NUMPARAM_MAX = 9,
465};
466
467#define DVARS_INHERIT ((void*)1)
468#define DVARS_TOPSCOPE NULL
469#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
470
471typedef struct token_info {
472 const char *token;
473 rb_code_position_t beg;
474 int indent;
475 int nonspc;
476 struct token_info *next;
477} token_info;
478
479typedef struct end_expect_token_locations {
480 const rb_code_position_t *pos;
481 struct end_expect_token_locations *prev;
482} end_expect_token_locations_t;
483
484typedef struct parser_string_buffer_elem {
485 struct parser_string_buffer_elem *next;
486 long len; /* Total length of allocated buf */
487 long used; /* Current usage of buf */
488 rb_parser_string_t *buf[FLEX_ARY_LEN];
489} parser_string_buffer_elem_t;
490
491typedef struct parser_string_buffer {
492 parser_string_buffer_elem_t *head;
493 parser_string_buffer_elem_t *last;
494} parser_string_buffer_t;
495
496#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
497
498/*
499 Structure of Lexer Buffer:
500
501 lex.pbeg lex.ptok lex.pcur lex.pend
502 | | | |
503 |------------+------------+------------|
504 |<---------->|
505 token
506*/
507struct parser_params {
508 YYSTYPE *lval;
509 YYLTYPE *yylloc;
510
511 struct {
512 rb_strterm_t *strterm;
513 rb_parser_lex_gets_func *gets;
514 rb_parser_input_data input;
515 parser_string_buffer_t string_buffer;
516 rb_parser_string_t *lastline;
517 rb_parser_string_t *nextline;
518 const char *pbeg;
519 const char *pcur;
520 const char *pend;
521 const char *ptok;
522 enum lex_state_e state;
523 /* track the nest level of any parens "()[]{}" */
524 int paren_nest;
525 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
526 int lpar_beg;
527 /* track the nest level of only braces "{}" */
528 int brace_nest;
529 } lex;
530 /* Cache the tail of the and/or chain most recently built by logop(), so a
531 * left-associative chain does not rescan its right branch on every
532 * operator. head is the chain's top node, tail the node whose nd_2nd is the
533 * next insertion point. */
534 struct {
535 NODE *head;
536 NODE *tail;
537 } logop;
538 stack_type cond_stack;
539 stack_type cmdarg_stack;
540 int tokidx;
541 int toksiz;
542 int heredoc_end;
543 int heredoc_indent;
544 int heredoc_line_indent;
545 char *tokenbuf;
546 struct local_vars *lvtbl;
547 st_table *pvtbl;
548 st_table *pktbl;
549 int line_count;
550 int ruby_sourceline; /* current line no. */
551 const char *ruby_sourcefile; /* current source file */
552 VALUE ruby_sourcefile_string;
553 rb_encoding *enc;
554 token_info *token_info;
555 st_table *case_labels;
556 rb_node_exits_t *exits;
557
558 VALUE debug_buffer;
559 VALUE debug_output;
560
561 struct {
562 rb_parser_string_t *token;
563 int beg_line;
564 int beg_col;
565 int end_line;
566 int end_col;
567 } delayed;
568
569 rb_ast_t *ast;
570 int node_id;
571
572 st_table *warn_duplicate_keys_table;
573
574 int max_numparam;
575 ID it_id;
576
577 struct lex_context ctxt;
578
579 NODE *eval_tree_begin;
580 NODE *eval_tree;
581 const struct rb_iseq_struct *parent_iseq;
582
583#ifdef UNIVERSAL_PARSER
584 const rb_parser_config_t *config;
585#endif
586 /* compile_option */
587 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
588
589 unsigned int command_start:1;
590 unsigned int eofp: 1;
591 unsigned int ruby__end__seen: 1;
592 unsigned int debug: 1;
593 unsigned int has_shebang: 1;
594 unsigned int token_seen: 1;
595 unsigned int token_info_enabled: 1;
596# if WARN_PAST_SCOPE
597 unsigned int past_scope_enabled: 1;
598# endif
599 unsigned int error_p: 1;
600 unsigned int cr_seen: 1;
601
602 /* Streaming hash state of the source bytes read so far. */
603 rb_source_hash_state_t source_hash;
604
605#ifndef RIPPER
606 /* Ruby core only */
607
608 unsigned int do_print: 1;
609 unsigned int do_loop: 1;
610 unsigned int do_chomp: 1;
611 unsigned int do_split: 1;
612 unsigned int error_tolerant: 1;
613 unsigned int keep_tokens: 1;
614
615 VALUE error_buffer;
616 rb_parser_ary_t *debug_lines;
617 /*
618 * Store specific keyword locations to generate dummy end token.
619 * Refer to the tail of list element.
620 */
621 end_expect_token_locations_t *end_expect_token_locations;
622 /* id for terms */
623 int token_id;
624 /* Array for term tokens */
625 rb_parser_ary_t *tokens;
626#else
627 /* Ripper only */
628
629 VALUE value;
630 VALUE result;
631 VALUE parsing_thread;
632 VALUE s_value; /* Token VALUE */
633 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
634 VALUE s_value_stack;
635#endif
636};
637
638#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
639#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
640#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
641static int
642numparam_id_p(struct parser_params *p, ID id)
643{
644 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
645 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
646 return idx > 0 && idx <= NUMPARAM_MAX;
647}
648static void numparam_name(struct parser_params *p, ID id);
649
650#ifdef RIPPER
651static void
652after_shift(struct parser_params *p)
653{
654 if (p->debug) {
655 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
656 }
657 rb_ary_push(p->s_value_stack, p->s_value);
658 p->s_value = Qnil;
659}
660
661static void
662before_reduce(int len, struct parser_params *p)
663{
664 // Initialize $$ with $1.
665 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
666}
667
668static void
669after_reduce(int len, struct parser_params *p)
670{
671 for (int i = 0; i < len; i++) {
672 VALUE tos = rb_ary_pop(p->s_value_stack);
673 if (p->debug) {
674 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
675 }
676 }
677 if (p->debug) {
678 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
679 }
680 rb_ary_push(p->s_value_stack, p->s_lvalue);
681 p->s_lvalue = Qnil;
682}
683
684static void
685after_shift_error_token(struct parser_params *p)
686{
687 if (p->debug) {
688 rb_parser_printf(p, "after-shift-error-token:\n");
689 }
690 rb_ary_push(p->s_value_stack, Qnil);
691}
692
693static void
694after_pop_stack(int len, struct parser_params *p)
695{
696 for (int i = 0; i < len; i++) {
697 VALUE tos = rb_ary_pop(p->s_value_stack);
698 if (p->debug) {
699 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
700 }
701 }
702}
703#else
704static void
705after_shift(struct parser_params *p)
706{
707}
708
709static void
710before_reduce(int len, struct parser_params *p)
711{
712}
713
714static void
715after_reduce(int len, struct parser_params *p)
716{
717}
718
719static void
720after_shift_error_token(struct parser_params *p)
721{
722}
723
724static void
725after_pop_stack(int len, struct parser_params *p)
726{
727}
728#endif
729
730#define intern_cstr(n,l,en) rb_intern3(n,l,en)
731
732#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
733
734#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
735#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
736#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
737#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
738#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
739#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
740
741#ifndef RIPPER
742static inline int
743char_at_end(struct parser_params *p, VALUE str, int when_empty)
744{
745 rb_len_t len = RSTRING_LEN(str);
746 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
747}
748#endif
749
750static void
751pop_pvtbl(struct parser_params *p, st_table *tbl)
752{
753 st_free_table(p->pvtbl);
754 p->pvtbl = tbl;
755}
756
757static void
758pop_pktbl(struct parser_params *p, st_table *tbl)
759{
760 if (p->pktbl) st_free_table(p->pktbl);
761 p->pktbl = tbl;
762}
763
764#define STRING_BUF_DEFAULT_LEN 16
765
766static void
767string_buffer_init(struct parser_params *p)
768{
769 parser_string_buffer_t *buf = &p->lex.string_buffer;
770 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
771
772 buf->head = buf->last = xmalloc(size);
773 buf->head->len = STRING_BUF_DEFAULT_LEN;
774 buf->head->used = 0;
775 buf->head->next = NULL;
776}
777
778static void
779string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
780{
781 parser_string_buffer_t *buf = &p->lex.string_buffer;
782
783 if (buf->head->used >= buf->head->len) {
784 parser_string_buffer_elem_t *elem;
785 long n = buf->head->len * 2;
786 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
787
788 elem = xmalloc(size);
789 elem->len = n;
790 elem->used = 0;
791 elem->next = NULL;
792 buf->last->next = elem;
793 buf->last = elem;
794 }
795 buf->last->buf[buf->last->used++] = str;
796}
797
798static void
799string_buffer_free(struct parser_params *p)
800{
801 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
802
803 while (elem) {
804 parser_string_buffer_elem_t *next_elem = elem->next;
805
806 for (long i = 0; i < elem->used; i++) {
807 rb_parser_string_free(p, elem->buf[i]);
808 }
809
810 xfree(elem);
811 elem = next_elem;
812 }
813}
814
815#ifndef RIPPER
816static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
817
818static void
819debug_end_expect_token_locations(struct parser_params *p, const char *name)
820{
821 if(p->debug) {
822 VALUE mesg = rb_sprintf("%s: [", name);
823 int i = 0;
824 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
825 if (i > 0)
826 rb_str_cat_cstr(mesg, ", ");
827 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
828 i++;
829 }
830 rb_str_cat_cstr(mesg, "]\n");
831 flush_debug_buffer(p, p->debug_output, mesg);
832 }
833}
834
835static void
836push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
837{
838 if(!p->error_tolerant) return;
839
840 end_expect_token_locations_t *locations;
841 locations = ALLOC(end_expect_token_locations_t);
842 locations->pos = pos;
843 locations->prev = p->end_expect_token_locations;
844 p->end_expect_token_locations = locations;
845
846 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
847}
848
849static void
850pop_end_expect_token_locations(struct parser_params *p)
851{
852 if(!p->end_expect_token_locations) return;
853
854 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
855 ruby_xfree_sized(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
856 p->end_expect_token_locations = locations;
857
858 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
859}
860
861static end_expect_token_locations_t *
862peek_end_expect_token_locations(struct parser_params *p)
863{
864 return p->end_expect_token_locations;
865}
866
867static const char *
868parser_token2char(struct parser_params *p, enum yytokentype tok)
869{
870 switch ((int) tok) {
871#define TOKEN2CHAR(tok) case tok: return (#tok);
872#define TOKEN2CHAR2(tok, name) case tok: return (name);
873 TOKEN2CHAR2(' ', "word_sep");
874 TOKEN2CHAR2('!', "!")
875 TOKEN2CHAR2('%', "%");
876 TOKEN2CHAR2('&', "&");
877 TOKEN2CHAR2('*', "*");
878 TOKEN2CHAR2('+', "+");
879 TOKEN2CHAR2('-', "-");
880 TOKEN2CHAR2('/', "/");
881 TOKEN2CHAR2('<', "<");
882 TOKEN2CHAR2('=', "=");
883 TOKEN2CHAR2('>', ">");
884 TOKEN2CHAR2('?', "?");
885 TOKEN2CHAR2('^', "^");
886 TOKEN2CHAR2('|', "|");
887 TOKEN2CHAR2('~', "~");
888 TOKEN2CHAR2(':', ":");
889 TOKEN2CHAR2(',', ",");
890 TOKEN2CHAR2('.', ".");
891 TOKEN2CHAR2(';', ";");
892 TOKEN2CHAR2('`', "`");
893 TOKEN2CHAR2('\n', "nl");
894 TOKEN2CHAR2('{', "\"{\"");
895 TOKEN2CHAR2('}', "\"}\"");
896 TOKEN2CHAR2('[', "\"[\"");
897 TOKEN2CHAR2(']', "\"]\"");
898 TOKEN2CHAR2('(', "\"(\"");
899 TOKEN2CHAR2(')', "\")\"");
900 TOKEN2CHAR2('\\', "backslash");
901 TOKEN2CHAR(keyword_class);
902 TOKEN2CHAR(keyword_module);
903 TOKEN2CHAR(keyword_def);
904 TOKEN2CHAR(keyword_undef);
905 TOKEN2CHAR(keyword_begin);
906 TOKEN2CHAR(keyword_rescue);
907 TOKEN2CHAR(keyword_ensure);
908 TOKEN2CHAR(keyword_end);
909 TOKEN2CHAR(keyword_if);
910 TOKEN2CHAR(keyword_unless);
911 TOKEN2CHAR(keyword_then);
912 TOKEN2CHAR(keyword_elsif);
913 TOKEN2CHAR(keyword_else);
914 TOKEN2CHAR(keyword_case);
915 TOKEN2CHAR(keyword_when);
916 TOKEN2CHAR(keyword_while);
917 TOKEN2CHAR(keyword_until);
918 TOKEN2CHAR(keyword_for);
919 TOKEN2CHAR(keyword_break);
920 TOKEN2CHAR(keyword_next);
921 TOKEN2CHAR(keyword_redo);
922 TOKEN2CHAR(keyword_retry);
923 TOKEN2CHAR(keyword_in);
924 TOKEN2CHAR(keyword_do);
925 TOKEN2CHAR(keyword_do_cond);
926 TOKEN2CHAR(keyword_do_block);
927 TOKEN2CHAR(keyword_do_LAMBDA);
928 TOKEN2CHAR(keyword_return);
929 TOKEN2CHAR(keyword_yield);
930 TOKEN2CHAR(keyword_super);
931 TOKEN2CHAR(keyword_self);
932 TOKEN2CHAR(keyword_nil);
933 TOKEN2CHAR(keyword_true);
934 TOKEN2CHAR(keyword_false);
935 TOKEN2CHAR(keyword_and);
936 TOKEN2CHAR(keyword_or);
937 TOKEN2CHAR(keyword_not);
938 TOKEN2CHAR(modifier_if);
939 TOKEN2CHAR(modifier_unless);
940 TOKEN2CHAR(modifier_while);
941 TOKEN2CHAR(modifier_until);
942 TOKEN2CHAR(modifier_rescue);
943 TOKEN2CHAR(keyword_alias);
944 TOKEN2CHAR(keyword_defined);
945 TOKEN2CHAR(keyword_BEGIN);
946 TOKEN2CHAR(keyword_END);
947 TOKEN2CHAR(keyword__LINE__);
948 TOKEN2CHAR(keyword__FILE__);
949 TOKEN2CHAR(keyword__ENCODING__);
950 TOKEN2CHAR(tIDENTIFIER);
951 TOKEN2CHAR(tFID);
952 TOKEN2CHAR(tGVAR);
953 TOKEN2CHAR(tIVAR);
954 TOKEN2CHAR(tCONSTANT);
955 TOKEN2CHAR(tCVAR);
956 TOKEN2CHAR(tLABEL);
957 TOKEN2CHAR(tINTEGER);
958 TOKEN2CHAR(tFLOAT);
959 TOKEN2CHAR(tRATIONAL);
960 TOKEN2CHAR(tIMAGINARY);
961 TOKEN2CHAR(tCHAR);
962 TOKEN2CHAR(tNTH_REF);
963 TOKEN2CHAR(tBACK_REF);
964 TOKEN2CHAR(tSTRING_CONTENT);
965 TOKEN2CHAR(tREGEXP_END);
966 TOKEN2CHAR(tDUMNY_END);
967 TOKEN2CHAR(tSP);
968 TOKEN2CHAR(tUPLUS);
969 TOKEN2CHAR(tUMINUS);
970 TOKEN2CHAR(tPOW);
971 TOKEN2CHAR(tCMP);
972 TOKEN2CHAR(tEQ);
973 TOKEN2CHAR(tEQQ);
974 TOKEN2CHAR(tNEQ);
975 TOKEN2CHAR(tGEQ);
976 TOKEN2CHAR(tLEQ);
977 TOKEN2CHAR(tANDOP);
978 TOKEN2CHAR(tOROP);
979 TOKEN2CHAR(tMATCH);
980 TOKEN2CHAR(tNMATCH);
981 TOKEN2CHAR(tDOT2);
982 TOKEN2CHAR(tDOT3);
983 TOKEN2CHAR(tBDOT2);
984 TOKEN2CHAR(tBDOT3);
985 TOKEN2CHAR(tAREF);
986 TOKEN2CHAR(tASET);
987 TOKEN2CHAR(tLSHFT);
988 TOKEN2CHAR(tRSHFT);
989 TOKEN2CHAR(tANDDOT);
990 TOKEN2CHAR(tCOLON2);
991 TOKEN2CHAR(tCOLON3);
992 TOKEN2CHAR(tOP_ASGN);
993 TOKEN2CHAR(tASSOC);
994 TOKEN2CHAR(tLPAREN);
995 TOKEN2CHAR(tLPAREN_ARG);
996 TOKEN2CHAR(tLBRACK);
997 TOKEN2CHAR(tLBRACE);
998 TOKEN2CHAR(tLBRACE_ARG);
999 TOKEN2CHAR(tSTAR);
1000 TOKEN2CHAR(tDSTAR);
1001 TOKEN2CHAR(tAMPER);
1002 TOKEN2CHAR(tLAMBDA);
1003 TOKEN2CHAR(tSYMBEG);
1004 TOKEN2CHAR(tSTRING_BEG);
1005 TOKEN2CHAR(tXSTRING_BEG);
1006 TOKEN2CHAR(tREGEXP_BEG);
1007 TOKEN2CHAR(tWORDS_BEG);
1008 TOKEN2CHAR(tQWORDS_BEG);
1009 TOKEN2CHAR(tSYMBOLS_BEG);
1010 TOKEN2CHAR(tQSYMBOLS_BEG);
1011 TOKEN2CHAR(tSTRING_END);
1012 TOKEN2CHAR(tSTRING_DEND);
1013 TOKEN2CHAR(tSTRING_DBEG);
1014 TOKEN2CHAR(tSTRING_DVAR);
1015 TOKEN2CHAR(tLAMBEG);
1016 TOKEN2CHAR(tLABEL_END);
1017 TOKEN2CHAR(tIGNORED_NL);
1018 TOKEN2CHAR(tCOMMENT);
1019 TOKEN2CHAR(tEMBDOC_BEG);
1020 TOKEN2CHAR(tEMBDOC);
1021 TOKEN2CHAR(tEMBDOC_END);
1022 TOKEN2CHAR(tHEREDOC_BEG);
1023 TOKEN2CHAR(tHEREDOC_END);
1024 TOKEN2CHAR(k__END__);
1025 TOKEN2CHAR(tLOWEST);
1026 TOKEN2CHAR(tUMINUS_NUM);
1027 TOKEN2CHAR(tLAST_TOKEN);
1028#undef TOKEN2CHAR
1029#undef TOKEN2CHAR2
1030 }
1031
1032 rb_bug("parser_token2id: unknown token %d", tok);
1033
1034 UNREACHABLE_RETURN(0);
1035}
1036#else
1037static void
1038push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1039{
1040}
1041
1042static void
1043pop_end_expect_token_locations(struct parser_params *p)
1044{
1045}
1046#endif
1047
1048RBIMPL_ATTR_NONNULL((1, 2, 3))
1049static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1050RBIMPL_ATTR_NONNULL((1, 2))
1051static int parser_yyerror0(struct parser_params*, const char*);
1052#define yyerror0(msg) parser_yyerror0(p, (msg))
1053#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1054#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1055#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1056#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1057#define lex_eol_p(p) lex_eol_n_p(p, 0)
1058#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1059#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1060#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1061
1062static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1063static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1064static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1065static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1066static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1067
1068#ifdef RIPPER
1069#define compile_for_eval (0)
1070#else
1071#define compile_for_eval (p->parent_iseq != 0)
1072#endif
1073
1074#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1075
1076#define CALL_Q_P(q) ((q) == tANDDOT)
1077#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1078
1079#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1080
1081static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1082
1083static inline void
1084rb_discard_node(struct parser_params *p, NODE *n)
1085{
1086 rb_ast_delete_node(p->ast, n);
1087}
1088
1089static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc);
1090static rb_node_scope_t *rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc);
1091static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1092static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc);
1093static 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);
1094static 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);
1095static 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);
1096static 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);
1097static 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);
1098static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc);
1099static 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);
1100static 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);
1101static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1102static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *for_keyword_loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *do_keyword_loc, const YYLTYPE *end_keyword_loc);
1103static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1104static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1105static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1106static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1107static 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);
1108static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1109static 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);
1110static 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);
1111static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1112static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1113static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1114static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1115static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1116static 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);
1117static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1118static 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);
1119static 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);
1120static 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);
1121static 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);
1122static 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);
1123static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1124static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1125static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1126static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1127static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1128static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc);
1129static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1130static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1131static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1132static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1133static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1134static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1135static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc);
1136static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1137static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1138static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1139static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1140static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1141static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1142static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1143static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1144static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1145static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1146static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1147static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1148static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1149static 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);
1150static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1151static 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);
1152static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1153static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1154static 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);
1155static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1156static rb_node_regx_t *rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc);
1157static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1158static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1159static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1160static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1161static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1162static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1163static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1164static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1165static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1166static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1167static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1168static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1169static 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);
1170static 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);
1171static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1172static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc);
1173static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc);
1174static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *operator_loc, const YYLTYPE *end_keyword_loc);
1175static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
1176static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
1177static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1178static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1179static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1180static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1181static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1182static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1183static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1184static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1185static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1186static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1187static 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);
1188static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1189static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1190static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1191static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1192static 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);
1193static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1194static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1195static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1196
1197#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1198#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1199#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1200#define NEW_IF(c,t,e,loc,ik_loc,tk_loc,ek_loc) (NODE *)rb_node_if_new(p,c,t,e,loc,ik_loc,tk_loc,ek_loc)
1201#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)
1202#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1203#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1204#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1205#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)
1206#define NEW_IN(c,t,e,loc,ik_loc,tk_loc,o_loc) (NODE *)rb_node_in_new(p,c,t,e,loc,ik_loc,tk_loc,o_loc)
1207#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)
1208#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)
1209#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1210#define NEW_FOR(i,b,loc,f_loc,i_loc,d_loc,e_loc) (NODE *)rb_node_for_new(p,i,b,loc,f_loc,i_loc,d_loc,e_loc)
1211#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1212#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1213#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1214#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1215#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1216#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1217#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1218#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1219#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1220#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1221#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1222#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1223#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1224#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1225#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1226#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)
1227#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)
1228#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1229#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1230#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1231#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1232#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1233#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1234#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1235#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1236#define NEW_SUPER(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_super_new(p,a,loc,k_loc,l_loc,r_loc)
1237#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1238#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1239#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1240#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1241#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1242#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1243#define NEW_YIELD(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_yield_new(p,a,loc,k_loc,l_loc,r_loc)
1244#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1245#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1246#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1247#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1248#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1249#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1250#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1251#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1252#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1253#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1254#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1255#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1256#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1257#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1258#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1259#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1260#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1261#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1262#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1263#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1264#define NEW_REGX(str,opts,loc,o_loc,ct_loc,c_loc) (NODE *)rb_node_regx_new(p,str,opts,loc,o_loc,ct_loc,c_loc)
1265#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1266#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1267#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1268#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1269#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1270#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1271#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1272#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1273#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1274#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1275#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1276#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1277#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1278#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1279#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1280#define NEW_CLASS(n,b,s,loc,ck_loc,io_loc,ek_loc) (NODE *)rb_node_class_new(p,n,b,s,loc,ck_loc,io_loc,ek_loc)
1281#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1282#define NEW_SCLASS(r,b,loc,ck_loc,op_loc,ek_loc) (NODE *)rb_node_sclass_new(p,r,b,loc,ck_loc,op_loc,ek_loc)
1283#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1284#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1285#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1286#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1287#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1288#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1289#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1290#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1291#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1292#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1293#define NEW_POSTEXE(b,loc,k_loc,o_loc,c_loc) (NODE *)rb_node_postexe_new(p,b,loc,k_loc,o_loc,c_loc)
1294#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1295#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1296#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1297#define NEW_LAMBDA(a,b,loc,op_loc,o_loc,c_loc) (NODE *)rb_node_lambda_new(p,a,b,loc,op_loc,o_loc,c_loc)
1298#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1299#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1300#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1301#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1302#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1303#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1304#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1305
1306enum internal_node_type {
1307 NODE_INTERNAL_ONLY = NODE_LAST,
1308 NODE_DEF_TEMP,
1309 NODE_EXITS,
1310 NODE_INTERNAL_LAST
1311};
1312
1313static const char *
1314parser_node_name(int node)
1315{
1316 switch (node) {
1317 case NODE_DEF_TEMP:
1318 return "NODE_DEF_TEMP";
1319 case NODE_EXITS:
1320 return "NODE_EXITS";
1321 default:
1322 return ruby_node_name(node);
1323 }
1324}
1325
1326/* This node is parse.y internal */
1327struct RNode_DEF_TEMP {
1328 NODE node;
1329
1330 /* for NODE_DEFN/NODE_DEFS */
1331
1332 struct RNode *nd_def;
1333 ID nd_mid;
1334
1335 struct {
1336 int max_numparam;
1337 NODE *numparam_save;
1338 struct lex_context ctxt;
1339 } save;
1340};
1341
1342#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1343
1344static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1345static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1346static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1347static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1348static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1349
1350#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1351#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1352#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1353#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1354
1355/* Make a new internal node, which should not be appeared in the
1356 * result AST and does not have node_id and location. */
1357static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1358#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1359
1360static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1361
1362static int
1363parser_get_node_id(struct parser_params *p)
1364{
1365 int node_id = p->node_id;
1366 p->node_id++;
1367 return node_id;
1368}
1369
1370static void
1371anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1372{
1373 if (id == tANDDOT) {
1374 yyerror1(loc, "&. inside multiple assignment destination");
1375 }
1376}
1377
1378static inline void
1379set_line_body(NODE *body, int line)
1380{
1381 if (!body) return;
1382 switch (nd_type(body)) {
1383 case NODE_RESCUE:
1384 case NODE_ENSURE:
1385 nd_set_line(body, line);
1386 }
1387}
1388
1389static void
1390set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1391{
1392 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1393 nd_set_line(node, beg->end_pos.lineno);
1394}
1395
1396static NODE *
1397last_expr_node(NODE *expr)
1398{
1399 while (expr) {
1400 if (nd_type_p(expr, NODE_BLOCK)) {
1401 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1402 }
1403 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1404 expr = RNODE_BEGIN(expr)->nd_body;
1405 }
1406 else {
1407 break;
1408 }
1409 }
1410 return expr;
1411}
1412
1413#ifndef RIPPER
1414#define yyparse ruby_yyparse
1415#endif
1416
1417static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1418static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1419static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1420static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1421static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1422static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1423
1424static NODE *newline_node(NODE*);
1425static void fixpos(NODE*,NODE*);
1426
1427static int value_expr(struct parser_params*,NODE*);
1428static void void_expr(struct parser_params*,NODE*);
1429static NODE *remove_begin(NODE*);
1430static NODE *void_stmts(struct parser_params*,NODE*);
1431static void reduce_nodes(struct parser_params*,NODE**);
1432static void block_dup_check(struct parser_params*,NODE*,NODE*);
1433
1434static NODE *block_append(struct parser_params*,NODE*,NODE*);
1435static NODE *list_append(struct parser_params*,NODE*,NODE*);
1436static NODE *list_concat(NODE*,NODE*);
1437static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1438static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1439static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1440static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1441static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1442static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1443static NODE *str2dstr(struct parser_params*,NODE*);
1444static NODE *evstr2dstr(struct parser_params*,NODE*);
1445static NODE *splat_array(NODE*);
1446static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1447
1448static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1449static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1450static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1451static 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);
1452static 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;}
1453static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1454
1455static bool args_info_empty_p(struct rb_args_info *args);
1456static 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*);
1457static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1458#define new_empty_args_tail(p, loc) new_args_tail(p, 0, 0, 0, loc)
1459static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1460static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1461static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1462static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1463static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1464static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1465
1466static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1467static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1468
1469static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1470static void no_blockarg(struct parser_params*,NODE*);
1471static NODE *ret_args(struct parser_params*,NODE*);
1472static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1473static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1474
1475static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1476static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1477
1478static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1479static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1480
1481static VALUE rb_backref_error(struct parser_params*,NODE*);
1482static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1483
1484static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1485static 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);
1486static 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);
1487static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1488static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1489
1490static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1491
1492static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1493static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1494
1495static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1496static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1497
1498static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1499
1500static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1501
1502#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1503
1504static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1505
1506static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1507
1508static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1509
1510static rb_ast_id_table_t *local_tbl(struct parser_params*);
1511
1512static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1513static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1514
1515static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1516static NODE *heredoc_dedent(struct parser_params*,NODE*);
1517
1518static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1519
1520static rb_locations_lambda_body_t* new_locations_lambda_body(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1521
1522#ifdef RIPPER
1523#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1524#define set_value(val) (p->s_lvalue = val)
1525static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1526static int id_is_var(struct parser_params *p, ID id);
1527#endif
1528
1529RUBY_SYMBOL_EXPORT_BEGIN
1530VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1531int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1532enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1533VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1534void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1535PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1536YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1537YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1538YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1539YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1540YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1541YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1542void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1543RUBY_SYMBOL_EXPORT_END
1544
1545static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1546static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1547static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1548static VALUE formal_argument_error(struct parser_params*, ID);
1549static ID shadowing_lvar(struct parser_params*,ID);
1550static void new_bv(struct parser_params*,ID);
1551
1552static void local_push(struct parser_params*,int);
1553static void local_pop(struct parser_params*);
1554static void local_var(struct parser_params*, ID);
1555static void arg_var(struct parser_params*, ID);
1556static int local_id(struct parser_params *p, ID id);
1557static int local_id_ref(struct parser_params*, ID, ID **);
1558#define internal_id rb_parser_internal_id
1559ID internal_id(struct parser_params*);
1560static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1561static int check_forwarding_args(struct parser_params*);
1562static void add_forwarding_args(struct parser_params *p);
1563static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1564
1565static const struct vtable *dyna_push(struct parser_params *);
1566static void dyna_pop(struct parser_params*, const struct vtable *);
1567static int dyna_in_block(struct parser_params*);
1568#define dyna_var(p, id) local_var(p, id)
1569static int dvar_defined(struct parser_params*, ID);
1570#define dvar_defined_ref rb_parser_dvar_defined_ref
1571int dvar_defined_ref(struct parser_params*, ID, ID**);
1572static int dvar_curr(struct parser_params*,ID);
1573
1574static int lvar_defined(struct parser_params*, ID);
1575
1576static NODE *numparam_push(struct parser_params *p);
1577static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1578
1579#define METHOD_NOT '!'
1580
1581#define idFWD_REST '*'
1582#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1583#define idFWD_BLOCK '&'
1584#define idFWD_ALL idDot3
1585#define arg_FWD_BLOCK idFWD_BLOCK
1586
1587#define RE_ONIG_OPTION_IGNORECASE 1
1588#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1589#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1590#define RE_OPTION_ONCE (1<<16)
1591#define RE_OPTION_ENCODING_SHIFT 8
1592#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1593#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1594#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1595#define RE_OPTION_MASK 0xff
1596#define RE_OPTION_ARG_ENCODING_NONE 32
1597
1598#define CHECK_LITERAL_WHEN (st_table *)1
1599#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1600
1601#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1602RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1603
1604#define TOKEN2ID(tok) ( \
1605 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1606 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1607 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1608 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1609 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1610 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1611 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1612
1613/****** Ripper *******/
1614
1615#ifdef RIPPER
1616
1617#include "eventids1.h"
1618#include "eventids2.h"
1619
1620extern const struct ripper_parser_ids ripper_parser_ids;
1621
1622static VALUE ripper_dispatch0(struct parser_params*,ID);
1623static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1624static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1625static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1626static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1627static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1628static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1629void ripper_error(struct parser_params *p);
1630
1631#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1632#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1633#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1634#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1635#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1636#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1637#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1638
1639#define yyparse ripper_yyparse
1640
1641static VALUE
1642aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1643{
1644 if (!NIL_P(pre_arg)) {
1645 if (!NIL_P(pre_args)) {
1646 rb_ary_unshift(pre_args, pre_arg);
1647 }
1648 else {
1649 pre_args = rb_ary_new_from_args(1, pre_arg);
1650 }
1651 }
1652 return pre_args;
1653}
1654
1655#define ID2VAL(id) STATIC_ID2SYM(id)
1656#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1657#endif /* RIPPER */
1658
1659#define KWD2EID(t, v) keyword_##t
1660
1661static NODE *
1662new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1663{
1664 body = remove_begin(body);
1665 reduce_nodes(p, &body);
1666 NODE *n = NEW_SCOPE(args, body, parent, loc);
1667 nd_set_line(n, loc->end_pos.lineno);
1668 set_line_body(body, loc->beg_pos.lineno);
1669 return n;
1670}
1671
1672static NODE *
1673rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1674 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1675{
1676 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1677 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1678 loc.beg_pos = arg_loc->beg_pos;
1679 return NEW_RESCUE(arg, rescue, 0, &loc);
1680}
1681
1682static NODE *add_block_exit(struct parser_params *p, NODE *node);
1683static rb_node_exits_t *init_block_exit(struct parser_params *p);
1684static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1685static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1686static void clear_block_exit(struct parser_params *p, bool error);
1687
1688static void
1689next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1690{
1691 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1692}
1693
1694static void
1695restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1696{
1697 /* See: def_name action */
1698 struct lex_context ctxt = temp->save.ctxt;
1699 p->ctxt.in_def = ctxt.in_def;
1700 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1701 p->ctxt.in_rescue = ctxt.in_rescue;
1702 p->max_numparam = temp->save.max_numparam;
1703 numparam_pop(p, temp->save.numparam_save);
1704 clear_block_exit(p, true);
1705}
1706
1707static void
1708endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1709{
1710 if (is_attrset_id(mid)) {
1711 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1712 }
1713 token_info_drop(p, "def", loc->beg_pos);
1714}
1715
1716#define debug_token_line(p, name, line) do { \
1717 if (p->debug) { \
1718 const char *const pcur = p->lex.pcur; \
1719 const char *const ptok = p->lex.ptok; \
1720 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1721 line, p->ruby_sourceline, \
1722 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1723 } \
1724 } while (0)
1725
1726#define begin_definition(k, loc_beg, loc_end) \
1727 do { \
1728 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1729 /* singleton class */ \
1730 p->ctxt.cant_return = !p->ctxt.in_def; \
1731 p->ctxt.in_def = 0; \
1732 } \
1733 else if (p->ctxt.in_def) { \
1734 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1735 yyerror1(&loc, k " definition in method body"); \
1736 } \
1737 else { \
1738 p->ctxt.cant_return = 1; \
1739 } \
1740 local_push(p, 0); \
1741 } while (0)
1742
1743#ifndef RIPPER
1744# define ifndef_ripper(x) (x)
1745# define ifdef_ripper(r,x) (x)
1746#else
1747# define ifndef_ripper(x)
1748# define ifdef_ripper(r,x) (r)
1749#endif
1750
1751# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1752# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1753# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1754# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1755# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1756# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1757# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1758# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1759# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1760# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1761# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1762# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1763# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1764# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1765# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1766# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1767# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1768# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1769# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1770# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1771#ifdef RIPPER
1772extern const ID id_warn, id_warning, id_gets, id_assoc;
1773# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1774# define WARN_S_L(s,l) STR_NEW(s,l)
1775# define WARN_S(s) STR_NEW2(s)
1776# define WARN_I(i) INT2NUM(i)
1777# define WARN_ID(i) rb_id2str(i)
1778# define PRIsWARN PRIsVALUE
1779# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1780# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1781# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1782# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1783# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1784# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1785# define compile_error ripper_compile_error
1786#else
1787# define WARN_S_L(s,l) s
1788# define WARN_S(s) s
1789# define WARN_I(i) i
1790# define WARN_ID(i) rb_id2name(i)
1791# define PRIsWARN PRIsVALUE
1792# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1793# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1794# define WARN_CALL rb_compile_warn
1795# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1796# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1797# define WARNING_CALL rb_compile_warning
1798PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1799# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1800#endif
1801
1802#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1803
1804static NODE *
1805add_block_exit(struct parser_params *p, NODE *node)
1806{
1807 if (!node) {
1808 compile_error(p, "unexpected null node");
1809 return 0;
1810 }
1811 switch (nd_type(node)) {
1812 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1813 default:
1814 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1815 return node;
1816 }
1817 if (!p->ctxt.in_defined) {
1818 rb_node_exits_t *exits = p->exits;
1819 if (exits) {
1820 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1821 exits->nd_stts = node;
1822 }
1823 }
1824 return node;
1825}
1826
1827static rb_node_exits_t *
1828init_block_exit(struct parser_params *p)
1829{
1830 rb_node_exits_t *old = p->exits;
1831 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1832 exits->nd_chain = 0;
1833 exits->nd_stts = RNODE(exits);
1834 p->exits = exits;
1835 return old;
1836}
1837
1838static rb_node_exits_t *
1839allow_block_exit(struct parser_params *p)
1840{
1841 rb_node_exits_t *exits = p->exits;
1842 p->exits = 0;
1843 return exits;
1844}
1845
1846static void
1847restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1848{
1849 p->exits = exits;
1850}
1851
1852static void
1853clear_block_exit(struct parser_params *p, bool error)
1854{
1855 rb_node_exits_t *exits = p->exits;
1856 if (!exits) return;
1857 if (error) {
1858 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1859 switch (nd_type(e)) {
1860 case NODE_BREAK:
1861 yyerror1(&e->nd_loc, "Invalid break");
1862 break;
1863 case NODE_NEXT:
1864 yyerror1(&e->nd_loc, "Invalid next");
1865 break;
1866 case NODE_REDO:
1867 yyerror1(&e->nd_loc, "Invalid redo");
1868 break;
1869 default:
1870 yyerror1(&e->nd_loc, "unexpected node");
1871 goto end_checks; /* no nd_chain */
1872 }
1873 }
1874 end_checks:;
1875 }
1876 exits->nd_stts = RNODE(exits);
1877 exits->nd_chain = 0;
1878}
1879
1880#define WARN_EOL(tok) \
1881 (looking_at_eol_p(p) ? \
1882 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1883 (void)0)
1884static int looking_at_eol_p(struct parser_params *p);
1885
1886static NODE *
1887get_nd_value(struct parser_params *p, NODE *node)
1888{
1889 switch (nd_type(node)) {
1890 case NODE_GASGN:
1891 return RNODE_GASGN(node)->nd_value;
1892 case NODE_IASGN:
1893 return RNODE_IASGN(node)->nd_value;
1894 case NODE_LASGN:
1895 return RNODE_LASGN(node)->nd_value;
1896 case NODE_DASGN:
1897 return RNODE_DASGN(node)->nd_value;
1898 case NODE_MASGN:
1899 return RNODE_MASGN(node)->nd_value;
1900 case NODE_CVASGN:
1901 return RNODE_CVASGN(node)->nd_value;
1902 case NODE_CDECL:
1903 return RNODE_CDECL(node)->nd_value;
1904 default:
1905 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1906 return 0;
1907 }
1908}
1909
1910static void
1911set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1912{
1913 switch (nd_type(node)) {
1914 case NODE_CDECL:
1915 RNODE_CDECL(node)->nd_value = rhs;
1916 break;
1917 case NODE_GASGN:
1918 RNODE_GASGN(node)->nd_value = rhs;
1919 break;
1920 case NODE_IASGN:
1921 RNODE_IASGN(node)->nd_value = rhs;
1922 break;
1923 case NODE_LASGN:
1924 RNODE_LASGN(node)->nd_value = rhs;
1925 break;
1926 case NODE_DASGN:
1927 RNODE_DASGN(node)->nd_value = rhs;
1928 break;
1929 case NODE_MASGN:
1930 RNODE_MASGN(node)->nd_value = rhs;
1931 break;
1932 case NODE_CVASGN:
1933 RNODE_CVASGN(node)->nd_value = rhs;
1934 break;
1935 default:
1936 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1937 break;
1938 }
1939}
1940
1941static ID
1942get_nd_vid(struct parser_params *p, NODE *node)
1943{
1944 switch (nd_type(node)) {
1945 case NODE_CDECL:
1946 return RNODE_CDECL(node)->nd_vid;
1947 case NODE_GASGN:
1948 return RNODE_GASGN(node)->nd_vid;
1949 case NODE_IASGN:
1950 return RNODE_IASGN(node)->nd_vid;
1951 case NODE_LASGN:
1952 return RNODE_LASGN(node)->nd_vid;
1953 case NODE_DASGN:
1954 return RNODE_DASGN(node)->nd_vid;
1955 case NODE_CVASGN:
1956 return RNODE_CVASGN(node)->nd_vid;
1957 default:
1958 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1959 return 0;
1960 }
1961}
1962
1963static NODE *
1964get_nd_args(struct parser_params *p, NODE *node)
1965{
1966 switch (nd_type(node)) {
1967 case NODE_CALL:
1968 return RNODE_CALL(node)->nd_args;
1969 case NODE_OPCALL:
1970 return RNODE_OPCALL(node)->nd_args;
1971 case NODE_FCALL:
1972 return RNODE_FCALL(node)->nd_args;
1973 case NODE_QCALL:
1974 return RNODE_QCALL(node)->nd_args;
1975 case NODE_SUPER:
1976 return RNODE_SUPER(node)->nd_args;
1977 case NODE_VCALL:
1978 case NODE_ZSUPER:
1979 case NODE_YIELD:
1980 case NODE_RETURN:
1981 case NODE_BREAK:
1982 case NODE_NEXT:
1983 return 0;
1984 default:
1985 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1986 return 0;
1987 }
1988}
1989
1990static st_index_t
1991djb2(const uint8_t *str, size_t len)
1992{
1993 st_index_t hash = 5381;
1994
1995 for (size_t i = 0; i < len; i++) {
1996 hash = ((hash << 5) + hash) + str[i];
1997 }
1998
1999 return hash;
2000}
2001
2002static st_index_t
2003parser_memhash(const void *ptr, rb_len_t len)
2004{
2005 return djb2(ptr, len);
2006}
2007
2008#define PARSER_STRING_PTR(str) (str->ptr)
2009#define PARSER_STRING_LEN(str) (str->len)
2010#define PARSER_STRING_END(str) (&str->ptr[str->len])
2011#define STRING_SIZE(str) ((size_t)str->len + 1)
2012#define STRING_TERM_LEN(str) (1)
2013#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
2014#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
2015 REALLOC_N(str->ptr, char, (size_t)total + termlen); \
2016 str->len = total; \
2017} while (0)
2018#define STRING_SET_LEN(str, n) do { \
2019 (str)->len = (n); \
2020} while (0)
2021#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2022 ((ptrvar) = str->ptr, \
2023 (lenvar) = str->len)
2024
2025static inline int
2026parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2027{
2028 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2029}
2030
2031static rb_parser_string_t *
2032rb_parser_string_new(rb_parser_t *p, const char *ptr, rb_len_t len)
2033{
2034 rb_parser_string_t *str;
2035
2036 if (len < 0) {
2037 rb_bug("negative string size (or size too big): %"PRIdLEN, len);
2038 }
2039
2040 str = xcalloc(1, sizeof(rb_parser_string_t));
2041 str->ptr = xcalloc(len + 1, sizeof(char));
2042
2043 if (ptr) {
2044 memcpy(PARSER_STRING_PTR(str), ptr, len);
2045 }
2046 STRING_SET_LEN(str, len);
2047 STRING_TERM_FILL(str);
2048 return str;
2049}
2050
2051static rb_parser_string_t *
2052rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, rb_len_t len, rb_encoding *enc)
2053{
2054 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2055 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2056 str->enc = enc;
2057 return str;
2058}
2059
2060#ifndef RIPPER
2061rb_parser_string_t *
2062rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2063{
2064 /* Type check */
2065 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2066 RB_GC_GUARD(str);
2067 return ret;
2068}
2069
2070void
2071rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2072{
2073 if (!str) return;
2074 xfree(PARSER_STRING_PTR(str));
2075 xfree(str);
2076}
2077#endif
2078
2079static st_index_t
2080rb_parser_str_hash(rb_parser_string_t *str)
2081{
2082 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2083}
2084
2085static st_index_t
2086rb_char_p_hash(const char *c)
2087{
2088 return parser_memhash((const void *)c, strlen(c));
2089}
2090
2091static size_t
2092rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2093{
2094 return PARSER_STRING_LEN(str);
2095}
2096
2097#ifndef RIPPER
2098static char *
2099rb_parser_string_end(rb_parser_string_t *str)
2100{
2101 return &str->ptr[str->len];
2102}
2103#endif
2104
2105static void
2106rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2107{
2108 str->enc = enc;
2109}
2110
2111static rb_encoding *
2112rb_parser_str_get_encoding(rb_parser_string_t *str)
2113{
2114 return str->enc;
2115}
2116
2117#ifndef RIPPER
2118static bool
2119PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2120{
2121 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2122}
2123#endif
2124
2125static int
2126PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2127{
2128 return str->coderange;
2129}
2130
2131static void
2132PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2133{
2134 str->coderange = coderange;
2135}
2136
2137static void
2138PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2139{
2140 rb_parser_string_set_encoding(str, enc);
2141 PARSER_ENC_CODERANGE_SET(str, cr);
2142}
2143
2144static void
2145PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2146{
2147 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2148}
2149
2150static bool
2151PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2152{
2153 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2154}
2155
2156static bool
2157PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2158{
2159 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2160}
2161
2162static const char *
2163rb_parser_search_nonascii(const char *p, const char *e)
2164{
2165 const char *s = p;
2166
2167 for (; s < e; s++) {
2168 if (*s & 0x80) return s;
2169 }
2170
2171 return NULL;
2172}
2173
2174static int
2175rb_parser_coderange_scan(struct parser_params *p, const char *ptr, rb_len_t len, rb_encoding *enc)
2176{
2177 const char *e = ptr + len;
2178
2179 if (enc == rb_ascii8bit_encoding()) {
2180 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2181 ptr = rb_parser_search_nonascii(ptr, e);
2182 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2183 }
2184
2185 /* parser string encoding is always asciicompat */
2186 ptr = rb_parser_search_nonascii(ptr, e);
2187 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2188 for (;;) {
2189 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2190 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2191 ptr += MBCLEN_CHARFOUND_LEN(ret);
2192 if (ptr == e) break;
2193 ptr = rb_parser_search_nonascii(ptr, e);
2194 if (!ptr) break;
2195 }
2196
2197 return RB_PARSER_ENC_CODERANGE_VALID;
2198}
2199
2200static int
2201rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2202{
2203 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2204}
2205
2206static int
2207rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2208{
2209 int cr = PARSER_ENC_CODERANGE(str);
2210
2211 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2212 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2213 PARSER_ENC_CODERANGE_SET(str, cr);
2214 }
2215
2216 return cr;
2217}
2218
2219static rb_parser_string_t *
2220rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2221{
2222 if (rb_parser_str_get_encoding(str) == enc)
2223 return str;
2224 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2225 PARSER_ENC_CODERANGE_CLEAR(str);
2226 }
2227 rb_parser_string_set_encoding(str, enc);
2228 return str;
2229}
2230
2231static bool
2232rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2233{
2234 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2235}
2236
2237static rb_encoding *
2238rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2239{
2240 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2241 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2242
2243 if (enc1 == NULL || enc2 == NULL)
2244 return 0;
2245
2246 if (enc1 == enc2) {
2247 return enc1;
2248 }
2249
2250 if (PARSER_STRING_LEN(str2) == 0)
2251 return enc1;
2252 if (PARSER_STRING_LEN(str1) == 0)
2253 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2254
2255 int cr1, cr2;
2256
2257 cr1 = rb_parser_enc_str_coderange(p, str1);
2258 cr2 = rb_parser_enc_str_coderange(p, str2);
2259
2260 if (cr1 != cr2) {
2261 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2262 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2263 }
2264
2265 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2266 return enc1;
2267 }
2268
2269 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2270 return enc2;
2271 }
2272
2273 return 0;
2274}
2275
2276static void
2277rb_parser_str_modify(rb_parser_string_t *str)
2278{
2279 PARSER_ENC_CODERANGE_CLEAR(str);
2280}
2281
2282static void
2283rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, rb_len_t len)
2284{
2285 rb_len_t capa;
2286 const int termlen = STRING_TERM_LEN(str);
2287
2288 if (len > (capa = (rb_len_t)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2289 rb_bug("probable buffer overflow: %"PRIdLEN" for %"PRIdLEN, len, capa);
2290 }
2291
2292 int cr = PARSER_ENC_CODERANGE(str);
2293 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2294 /* Leave unknown. */
2295 }
2296 else if (len > PARSER_STRING_LEN(str)) {
2297 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2298 }
2299 else if (len < PARSER_STRING_LEN(str)) {
2300 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2301 /* ASCII-only string is keeping after truncated. Valid
2302 * and broken may be invalid or valid, leave unknown. */
2303 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2304 }
2305 }
2306
2307 STRING_SET_LEN(str, len);
2308 STRING_TERM_FILL(str);
2309}
2310
2311static rb_parser_string_t *
2312rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len)
2313{
2314 rb_parser_str_modify(str);
2315 if (len == 0) return 0;
2316
2317 rb_len_t total, olen, off = -1;
2318 char *sptr;
2319 const int termlen = STRING_TERM_LEN(str);
2320
2321 PARSER_STRING_GETMEM(str, sptr, olen);
2322 if (ptr >= sptr && ptr <= sptr + olen) {
2323 off = ptr - sptr;
2324 }
2325
2326 if (olen > RB_LEN_MAX - len) {
2327 compile_error(p, "string sizes too big");
2328 return 0;
2329 }
2330 total = olen + len;
2331 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2332 sptr = PARSER_STRING_PTR(str);
2333 if (off != -1) {
2334 ptr = sptr + off;
2335 }
2336 memcpy(sptr + olen, ptr, len);
2337 STRING_SET_LEN(str, total);
2338 STRING_TERM_FILL(str);
2339
2340 return str;
2341}
2342
2343#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2344#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2345
2346static rb_parser_string_t *
2347rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len,
2348 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2349{
2350 int str_cr, res_cr;
2351 rb_encoding *str_enc, *res_enc;
2352
2353 str_enc = rb_parser_str_get_encoding(str);
2354 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2355
2356 if (str_enc == ptr_enc) {
2357 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2358 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2359 }
2360 }
2361 else {
2362 /* parser string encoding is always asciicompat */
2363 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2364 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2365 }
2366 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2367 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2368 str_cr = rb_parser_enc_str_coderange(p, str);
2369 }
2370 }
2371 }
2372 if (ptr_cr_ret)
2373 *ptr_cr_ret = ptr_cr;
2374
2375 if (str_enc != ptr_enc &&
2376 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2377 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2378 goto incompatible;
2379 }
2380
2381 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2382 res_enc = str_enc;
2383 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2384 }
2385 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2386 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2387 res_enc = str_enc;
2388 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2389 }
2390 else {
2391 res_enc = ptr_enc;
2392 res_cr = ptr_cr;
2393 }
2394 }
2395 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2396 res_enc = str_enc;
2397 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2398 res_cr = str_cr;
2399 else
2400 res_cr = ptr_cr;
2401 }
2402 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2403 res_enc = str_enc;
2404 res_cr = str_cr;
2405 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2406 }
2407
2408 if (len < 0) {
2409 compile_error(p, "negative string size (or size too big)");
2410 }
2411 parser_str_cat(str, ptr, len);
2412 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2413 return str;
2414
2415 incompatible:
2416 compile_error(p, "incompatible character encodings: %s and %s",
2417 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2418 UNREACHABLE_RETURN(0);
2419
2420}
2421
2422static rb_parser_string_t *
2423rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len,
2424 rb_encoding *ptr_enc)
2425{
2426 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2427}
2428
2429static rb_parser_string_t *
2430rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2431{
2432 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2433
2434 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2435 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2436
2437 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2438
2439 return str;
2440}
2441
2442static rb_parser_string_t *
2443rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, rb_len_t len)
2444{
2445 if (len < 0) {
2446 rb_bug("negative string size (or size too big)");
2447 }
2448
2449 rb_len_t slen = PARSER_STRING_LEN(str);
2450
2451 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2452 PARSER_ENC_CODERANGE_CLEAR(str);
2453 }
2454
2455 {
2456 rb_len_t capa;
2457 const int termlen = STRING_TERM_LEN(str);
2458
2459 if ((capa = slen) < len) {
2460 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2461 }
2462 else if (len == slen) return str;
2463 STRING_SET_LEN(str, len);
2464 STRING_TERM_FILL(str);
2465 }
2466 return str;
2467}
2468
2469# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2470 ((ptrvar) = str->ptr, \
2471 (lenvar) = str->len, \
2472 (encvar) = str->enc)
2473
2474static int
2475rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2476{
2477 rb_len_t len1, len2;
2478 const char *ptr1, *ptr2;
2479 rb_encoding *enc1, *enc2;
2480
2481 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2482 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2483
2484 return (len1 != len2 ||
2485 enc1 != enc2 ||
2486 memcmp(ptr1, ptr2, len1) != 0);
2487}
2488
2489static void
2490rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2491{
2492 long i;
2493 if (ary->capa < len) {
2494 ary->capa = len;
2495 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2496 for (i = ary->len; i < len; i++) {
2497 ary->data[i] = 0;
2498 }
2499 }
2500}
2501
2502/*
2503 * Do not call this directly.
2504 * Use rb_parser_ary_new_capa_for_XXX() instead.
2505 */
2506static rb_parser_ary_t *
2507parser_ary_new_capa(rb_parser_t *p, long len)
2508{
2509 if (len < 0) {
2510 rb_bug("negative array size (or size too big): %ld", len);
2511 }
2512 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2513 ary->data_type = 0;
2514 ary->len = 0;
2515 ary->capa = len;
2516 if (0 < len) {
2517 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2518 }
2519 else {
2520 ary->data = NULL;
2521 }
2522 return ary;
2523}
2524
2525#ifndef RIPPER
2526static rb_parser_ary_t *
2527rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2528{
2529 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2530 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2531 return ary;
2532}
2533
2534static rb_parser_ary_t *
2535rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2536{
2537 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2538 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2539 return ary;
2540}
2541#endif
2542
2543static rb_parser_ary_t *
2544rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2545{
2546 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2547 ary->data_type = PARSER_ARY_DATA_NODE;
2548 return ary;
2549}
2550
2551/*
2552 * Do not call this directly.
2553 * Use rb_parser_ary_push_XXX() instead.
2554 */
2555static rb_parser_ary_t *
2556parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2557{
2558 if (ary->len == ary->capa) {
2559 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2560 }
2561 ary->data[ary->len++] = val;
2562 return ary;
2563}
2564
2565#ifndef RIPPER
2566static rb_parser_ary_t *
2567rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2568{
2569 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2570 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2571 }
2572 return parser_ary_push(p, ary, val);
2573}
2574
2575static rb_parser_ary_t *
2576rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2577{
2578 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2579 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2580 }
2581 return parser_ary_push(p, ary, val);
2582}
2583#endif
2584
2585static rb_parser_ary_t *
2586rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2587{
2588 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2589 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2590 }
2591 return parser_ary_push(p, ary, val);
2592}
2593
2594#ifndef RIPPER
2595static void
2596rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2597{
2598 if (!token) return;
2599 rb_parser_string_free(p, token->str);
2600 xfree(token);
2601}
2602
2603static void
2604rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2605{
2606# define foreach_ary(ptr) \
2607 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2608 ptr < end_ary_data; ptr++)
2609 switch (ary->data_type) {
2610 case PARSER_ARY_DATA_AST_TOKEN:
2611 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2612 break;
2613 case PARSER_ARY_DATA_SCRIPT_LINE:
2614 foreach_ary(data) {rb_parser_string_free(p, *data);}
2615 break;
2616 case PARSER_ARY_DATA_NODE:
2617 /* Do nothing because nodes are freed when rb_ast_t is freed */
2618 break;
2619 default:
2620 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2621 break;
2622 }
2623# undef foreach_ary
2624 xfree(ary->data);
2625 xfree(ary);
2626}
2627
2628#endif /* !RIPPER */
2629%}
2630
2631%expect 0
2632%define api.pure
2633%define parse.error verbose
2634%printer {
2635 if ((NODE *)$$ == (NODE *)-1) {
2636 rb_parser_printf(p, "NODE_SPECIAL");
2637 }
2638 else if ($$) {
2639 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2640 }
2641} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2642 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2643%printer {
2644 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2645} <id>
2646%printer {
2647 switch (nd_type(RNODE($$))) {
2648 case NODE_INTEGER:
2649 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2650 break;
2651 case NODE_FLOAT:
2652 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2653 break;
2654 case NODE_RATIONAL:
2655 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2656 break;
2657 case NODE_IMAGINARY:
2658 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2659 break;
2660 default:
2661 break;
2662 }
2663} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2664%printer {
2665 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2666} tNTH_REF
2667%printer {
2668 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2669} tBACK_REF
2670
2671%destructor {
2672 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2673} <labels>
2674
2675%lex-param {struct parser_params *p}
2676%parse-param {struct parser_params *p}
2677%initial-action
2678{
2679 RUBY_SET_YYLLOC_OF_NONE(@$);
2680};
2681%after-shift after_shift
2682%before-reduce before_reduce
2683%after-reduce after_reduce
2684%after-shift-error-token after_shift_error_token
2685%after-pop-stack after_pop_stack
2686
2687%union {
2688 NODE *node;
2689 rb_node_fcall_t *node_fcall;
2690 rb_node_args_t *node_args;
2691 rb_node_args_aux_t *node_args_aux;
2692 rb_node_opt_arg_t *node_opt_arg;
2693 rb_node_kw_arg_t *node_kw_arg;
2694 rb_node_block_pass_t *node_block_pass;
2695 rb_node_masgn_t *node_masgn;
2696 rb_node_def_temp_t *node_def_temp;
2697 rb_node_exits_t *node_exits;
2698 struct rb_locations_lambda_body_t *locations_lambda_body;
2699 ID id;
2700 int num;
2701 st_table *tbl;
2702 st_table *labels;
2703 const struct vtable *vars;
2704 struct rb_strterm_struct *strterm;
2705 struct lex_context ctxt;
2706 enum lex_state_e state;
2707}
2708
2709%token <id>
2710 keyword_class "'class'"
2711 keyword_module "'module'"
2712 keyword_def "'def'"
2713 keyword_undef "'undef'"
2714 keyword_begin "'begin'"
2715 keyword_rescue "'rescue'"
2716 keyword_ensure "'ensure'"
2717 keyword_end "'end'"
2718 keyword_if "'if'"
2719 keyword_unless "'unless'"
2720 keyword_then "'then'"
2721 keyword_elsif "'elsif'"
2722 keyword_else "'else'"
2723 keyword_case "'case'"
2724 keyword_when "'when'"
2725 keyword_while "'while'"
2726 keyword_until "'until'"
2727 keyword_for "'for'"
2728 keyword_break "'break'"
2729 keyword_next "'next'"
2730 keyword_redo "'redo'"
2731 keyword_retry "'retry'"
2732 keyword_in "'in'"
2733 keyword_do "'do'"
2734 keyword_do_cond "'do' for condition"
2735 keyword_do_block "'do' for block"
2736 keyword_do_LAMBDA "'do' for lambda"
2737 keyword_return "'return'"
2738 keyword_yield "'yield'"
2739 keyword_super "'super'"
2740 keyword_self "'self'"
2741 keyword_nil "'nil'"
2742 keyword_true "'true'"
2743 keyword_false "'false'"
2744 keyword_and "'and'"
2745 keyword_or "'or'"
2746 keyword_not "'not'"
2747 modifier_if "'if' modifier"
2748 modifier_unless "'unless' modifier"
2749 modifier_while "'while' modifier"
2750 modifier_until "'until' modifier"
2751 modifier_rescue "'rescue' modifier"
2752 keyword_alias "'alias'"
2753 keyword_defined "'defined?'"
2754 keyword_BEGIN "'BEGIN'"
2755 keyword_END "'END'"
2756 keyword__LINE__ "'__LINE__'"
2757 keyword__FILE__ "'__FILE__'"
2758 keyword__ENCODING__ "'__ENCODING__'"
2759
2760%token <id> tIDENTIFIER "local variable or method"
2761%token <id> tFID "method"
2762%token <id> tGVAR "global variable"
2763%token <id> tIVAR "instance variable"
2764%token <id> tCONSTANT "constant"
2765%token <id> tCVAR "class variable"
2766%token <id> tLABEL "label"
2767%token <node> tINTEGER "integer literal"
2768%token <node> tFLOAT "float literal"
2769%token <node> tRATIONAL "rational literal"
2770%token <node> tIMAGINARY "imaginary literal"
2771%token <node> tCHAR "char literal"
2772%token <node> tNTH_REF "numbered reference"
2773%token <node> tBACK_REF "back reference"
2774%token <node> tSTRING_CONTENT "literal content"
2775%token <num> tREGEXP_END
2776%token <num> tDUMNY_END "dummy end"
2777
2778%type <node> singleton singleton_expr strings string string1 xstring regexp
2779%type <node> string_contents xstring_contents regexp_contents string_content
2780%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2781%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2782%type <node_def_temp> defn_head defs_head k_def
2783%type <node_exits> block_open k_while k_until k_for allow_exits
2784%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2785%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2786%type <node> command command_call command_call_value method_call
2787%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2788%type <node_fcall> fcall
2789%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2790%type <node> args arg_splat call_args opt_call_args
2791%type <node> paren_args opt_paren_args
2792%type <node_args> args_tail block_args_tail
2793%type <node> command_args aref_args
2794%type <node_block_pass> opt_block_arg block_arg
2795%type <node> var_ref var_lhs
2796%type <node> command_rhs arg_rhs
2797%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2798%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args f_empty_arg
2799%type <node_args_aux> f_arg f_arg_item
2800%type <node> f_marg f_rest_marg
2801%type <node_masgn> f_margs
2802%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2803%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2804%type <id> do bv_decls opt_bv_decl bvar
2805%type <node> lambda brace_body do_body
2806%type <locations_lambda_body> lambda_body
2807%type <node_args> f_larglist f_largs largs_tail
2808%type <node> brace_block cmd_brace_block do_block lhs none fitem
2809%type <node> mlhs_head mlhs_item mlhs_node
2810%type <node_masgn> mlhs mlhs_basic mlhs_inner
2811%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2812%type <node> p_expr p_as p_alt p_expr_basic p_find
2813%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2814%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2815%type <node> p_kwargs p_kwarg p_kw
2816%type <id> keyword_variable user_variable sym operation2 operation3
2817%type <id> cname fname op f_rest_arg f_block_arg opt_comma f_norm_arg f_bad_arg
2818%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2819%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2820%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2821%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2822%type <ctxt> p_in_kwarg
2823%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2824%type <num> max_numparam
2825%type <node> numparam
2826%type <id> it_id
2827%token END_OF_INPUT 0 "end-of-input"
2828%token <id> '.'
2829
2830/* escaped chars, should be ignored otherwise */
2831%token <id> '\\' "backslash"
2832%token tSP "escaped space"
2833%token <id> '\t' "escaped horizontal tab"
2834%token <id> '\f' "escaped form feed"
2835%token <id> '\r' "escaped carriage return"
2836%token <id> '\13' "escaped vertical tab"
2837%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2838%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2839%token tPOW RUBY_TOKEN(POW) "**"
2840%token tCMP RUBY_TOKEN(CMP) "<=>"
2841%token tEQ RUBY_TOKEN(EQ) "=="
2842%token tEQQ RUBY_TOKEN(EQQ) "==="
2843%token tNEQ RUBY_TOKEN(NEQ) "!="
2844%token tGEQ RUBY_TOKEN(GEQ) ">="
2845%token tLEQ RUBY_TOKEN(LEQ) "<="
2846%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2847%token tOROP RUBY_TOKEN(OROP) "||"
2848%token tMATCH RUBY_TOKEN(MATCH) "=~"
2849%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2850%token tDOT2 RUBY_TOKEN(DOT2) ".."
2851%token tDOT3 RUBY_TOKEN(DOT3) "..."
2852%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2853%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2854%token tAREF RUBY_TOKEN(AREF) "[]"
2855%token tASET RUBY_TOKEN(ASET) "[]="
2856%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2857%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2858%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2859%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2860%token tCOLON3 ":: at EXPR_BEG"
2861%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2862%token tASSOC "=>"
2863%token tLPAREN "("
2864%token tLPAREN_ARG "( arg"
2865%token tLBRACK "["
2866%token tLBRACE "{"
2867%token tLBRACE_ARG "{ arg"
2868%token tSTAR "*"
2869%token tDSTAR "**arg"
2870%token tAMPER "&"
2871%token <num> tLAMBDA "->"
2872%token tSYMBEG "symbol literal"
2873%token tSTRING_BEG "string literal"
2874%token tXSTRING_BEG "backtick literal"
2875%token tREGEXP_BEG "regexp literal"
2876%token tWORDS_BEG "word list"
2877%token tQWORDS_BEG "verbatim word list"
2878%token tSYMBOLS_BEG "symbol list"
2879%token tQSYMBOLS_BEG "verbatim symbol list"
2880%token tSTRING_END "terminator"
2881%token tSTRING_DEND "'}'"
2882%token <state> tSTRING_DBEG "'#{'"
2883%token tSTRING_DVAR tLAMBEG tLABEL_END
2884
2885%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2886%token tHEREDOC_BEG tHEREDOC_END k__END__
2887
2888/*
2889 * precedence table
2890 */
2891
2892%nonassoc tLOWEST
2893%nonassoc tLBRACE_ARG
2894
2895%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2896%left keyword_or keyword_and
2897%right keyword_not
2898%nonassoc keyword_defined
2899%right '=' tOP_ASGN
2900%left modifier_rescue
2901%right '?' ':'
2902%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2903%left tOROP
2904%left tANDOP
2905%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2906%left '>' tGEQ '<' tLEQ
2907%left '|' '^'
2908%left '&'
2909%left tLSHFT tRSHFT
2910%left '+' '-'
2911%left '*' '/' '%'
2912%right tUMINUS_NUM tUMINUS
2913%right tPOW
2914%right '!' '~' tUPLUS
2915
2916%token tLAST_TOKEN
2917
2918/*
2919 * inlining rules
2920 */
2921%rule %inline ident_or_const
2922 : tIDENTIFIER
2923 | tCONSTANT
2924 ;
2925
2926%rule %inline user_or_keyword_variable
2927 : user_variable
2928 | keyword_variable
2929 ;
2930
2931/*
2932 * parameterizing rules
2933 */
2934%rule asgn(rhs) <node>
2935 : lhs '=' lex_ctxt rhs
2936 {
2937 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2938 /*% ripper: assign!($:1, $:4) %*/
2939 }
2940 ;
2941
2942%rule args_tail_basic(value, trailing) <node_args>
2943 : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing)
2944 {
2945 $$ = new_args_tail(p, $1, $3, $4, &@3);
2946 /*% ripper: [$:1, $:3, $:4] %*/
2947 }
2948 | f_kwarg(value) opt_f_block_arg(trailing)
2949 {
2950 $$ = new_args_tail(p, $1, 0, $2, &@1);
2951 /*% ripper: [$:1, Qnil, $:2] %*/
2952 }
2953 | f_any_kwrest opt_f_block_arg(trailing)
2954 {
2955 $$ = new_args_tail(p, 0, $1, $2, &@1);
2956 /*% ripper: [Qnil, $:1, $:2] %*/
2957 }
2958 | f_block_arg
2959 {
2960 $$ = new_args_tail(p, 0, 0, $1, &@1);
2961 /*% ripper: [Qnil, Qnil, $:1] %*/
2962 }
2963 ;
2964
2965%rule opt_f_block_arg(trailing) <id>
2966 : ',' f_block_arg
2967 {
2968 $$ = $2;
2969 /*% ripper: $:2 %*/
2970 }
2971 | trailing
2972 ;
2973
2974%rule def_endless_method(bodystmt) <node>
2975 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2976 {
2977 endless_method_name(p, $head->nd_mid, &@head);
2978 restore_defun(p, $head);
2979 ($$ = $head->nd_def)->nd_loc = @$;
2980 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2981 RNODE_DEFN($$)->nd_defn = $bodystmt;
2982 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2983 /*% ripper: def!($:head, $:args, $:$) %*/
2984 local_pop(p);
2985 }
2986 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2987 {
2988 endless_method_name(p, $head->nd_mid, &@head);
2989 restore_defun(p, $head);
2990 ($$ = $head->nd_def)->nd_loc = @$;
2991 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2992 RNODE_DEFS($$)->nd_defn = $bodystmt;
2993 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2994 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2995 local_pop(p);
2996 }
2997 ;
2998
2999%rule compstmt(stmts) <node>
3000 : stmts terms?
3001 {
3002 void_stmts(p, $$ = $stmts);
3003 }
3004 ;
3005
3006%rule f_opt(value) <node_opt_arg>
3007 : f_arg_asgn f_eq value
3008 {
3009 p->ctxt.in_argdef = 1;
3010 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
3011 /*% ripper: [$:$, $:3] %*/
3012 }
3013 ;
3014
3015%rule f_opt_arg(value) <node_opt_arg>
3016 : f_opt(value)
3017 {
3018 $$ = $f_opt;
3019 /*% ripper: rb_ary_new3(1, $:1) %*/
3020 }
3021 | f_opt_arg(value) ',' f_opt(value)
3022 {
3023 $$ = opt_arg_append($f_opt_arg, $f_opt);
3024 /*% ripper: rb_ary_push($:1, $:3) %*/
3025 }
3026 ;
3027
3028%rule f_kw(value) <node_kw_arg>
3029 : f_label value
3030 {
3031 p->ctxt.in_argdef = 1;
3032 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3033 /*% ripper: [$:$, $:value] %*/
3034 }
3035 | f_label
3036 {
3037 p->ctxt.in_argdef = 1;
3038 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3039 /*% ripper: [$:$, 0] %*/
3040 }
3041 ;
3042
3043%rule f_kwarg(value) <node_kw_arg>
3044 : f_kw(value)
3045 {
3046 $$ = $f_kw;
3047 /*% ripper: rb_ary_new3(1, $:1) %*/
3048 }
3049 | f_kwarg(value) ',' f_kw(value)
3050 {
3051 $$ = kwd_append($f_kwarg, $f_kw);
3052 /*% ripper: rb_ary_push($:1, $:3) %*/
3053 }
3054 ;
3055
3056%rule mlhs_items(item) <node>
3057 : item
3058 {
3059 $$ = NEW_LIST($1, &@$);
3060 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3061 }
3062 | mlhs_items(item) ',' item
3063 {
3064 $$ = list_append(p, $1, $3);
3065 /*% ripper: mlhs_add!($:1, $:3) %*/
3066 }
3067 ;
3068
3069%rule op_asgn(rhs) <node>
3070 : var_lhs tOP_ASGN lex_ctxt rhs
3071 {
3072 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3073 /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
3074 }
3075 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3076 {
3077 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3078 /*% ripper: opassign!(aref_field!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/
3079 }
3080 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3081 {
3082 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3083 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3084 }
3085 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3086 {
3087 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3088 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3089 }
3090 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3091 {
3092 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3093 /*% ripper: opassign!(field!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3094 }
3095 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3096 {
3097 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3098 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3099 /*% ripper: opassign!(const_path_field!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3100 }
3101 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3102 {
3103 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3104 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3105 /*% ripper: opassign!(top_const_field!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3106 }
3107 | backref tOP_ASGN lex_ctxt rhs
3108 {
3109 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3110 $$ = NEW_ERROR(&@$);
3111 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:backref), $:tOP_ASGN, $:rhs)) %*/
3112 }
3113 ;
3114
3115%rule opt_args_tail(tail, trailing) <node_args>
3116 : ',' tail
3117 {
3118 $$ = $tail;
3119 /*% ripper: $:tail %*/
3120 }
3121 | trailing
3122 {
3123 $$ = new_empty_args_tail(p, &@$);
3124 /*% ripper: [Qnil, Qnil, Qnil] %*/
3125 }
3126 ;
3127
3128%rule range_expr(range) <node>
3129 : range tDOT2 range
3130 {
3131 value_expr(p, $1);
3132 value_expr(p, $3);
3133 $$ = NEW_DOT2($1, $3, &@$, &@2);
3134 /*% ripper: dot2!($:1, $:3) %*/
3135 }
3136 | range tDOT3 range
3137 {
3138 value_expr(p, $1);
3139 value_expr(p, $3);
3140 $$ = NEW_DOT3($1, $3, &@$, &@2);
3141 /*% ripper: dot3!($:1, $:3) %*/
3142 }
3143 | range tDOT2
3144 {
3145 value_expr(p, $1);
3146 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3147 /*% ripper: dot2!($:1, Qnil) %*/
3148 }
3149 | range tDOT3
3150 {
3151 value_expr(p, $1);
3152 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3153 /*% ripper: dot3!($:1, Qnil) %*/
3154 }
3155 | tBDOT2 range
3156 {
3157 value_expr(p, $2);
3158 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3159 /*% ripper: dot2!(Qnil, $:2) %*/
3160 }
3161 | tBDOT3 range
3162 {
3163 value_expr(p, $2);
3164 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3165 /*% ripper: dot3!(Qnil, $:2) %*/
3166 }
3167 ;
3168
3169%rule value_expr(value) <node>
3170 : value
3171 {
3172 value_expr(p, $1);
3173 $$ = $1;
3174 }
3175 ;
3176
3177%rule words(begin, word_list) <node>
3178 : begin ' '+ word_list tSTRING_END
3179 {
3180 $$ = make_list($word_list, &@$);
3181 /*% ripper: array!($:word_list) %*/
3182 }
3183 ;
3184
3185%%
3186program : {
3187 SET_LEX_STATE(EXPR_BEG);
3188 local_push(p, ifndef_ripper(1)+0);
3189 /* jumps are possible in the top-level loop. */
3190 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3191 }
3192 compstmt(top_stmts)
3193 {
3194 if ($2 && !compile_for_eval) {
3195 NODE *node = $2;
3196 /* last expression should not be void */
3197 if (nd_type_p(node, NODE_BLOCK)) {
3198 while (RNODE_BLOCK(node)->nd_next) {
3199 node = RNODE_BLOCK(node)->nd_next;
3200 }
3201 node = RNODE_BLOCK(node)->nd_head;
3202 }
3203 node = remove_begin(node);
3204 void_expr(p, node);
3205 }
3206 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3207 /*% ripper[final]: program!($:2) %*/
3208 local_pop(p);
3209 }
3210 ;
3211
3212top_stmts : none
3213 {
3214 $$ = NEW_BEGIN(0, &@$);
3215 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3216 }
3217 | top_stmt
3218 {
3219 $$ = newline_node($1);
3220 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3221 }
3222 | top_stmts terms top_stmt
3223 {
3224 $$ = block_append(p, $1, newline_node($3));
3225 /*% ripper: stmts_add!($:1, $:3) %*/
3226 }
3227 ;
3228
3229top_stmt : stmt
3230 {
3231 clear_block_exit(p, true);
3232 $$ = $1;
3233 }
3234 | keyword_BEGIN begin_block
3235 {
3236 $$ = $2;
3237 /*% ripper: $:2 %*/
3238 }
3239 ;
3240
3241block_open : '{' {$$ = init_block_exit(p);};
3242
3243begin_block : block_open compstmt(top_stmts) '}'
3244 {
3245 restore_block_exit(p, $block_open);
3246 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3247 NEW_BEGIN($compstmt, &@$));
3248 $$ = NEW_BEGIN(0, &@$);
3249 /*% ripper: BEGIN!($:compstmt) %*/
3250 }
3251 ;
3252
3253bodystmt : compstmt(stmts)[body]
3254 lex_ctxt[ctxt]
3255 opt_rescue
3256 k_else
3257 {
3258 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3259 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3260 }
3261 compstmt(stmts)[elsebody]
3262 {
3263 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3264 }
3265 opt_ensure
3266 {
3267 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3268 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3269 }
3270 | compstmt(stmts)[body]
3271 lex_ctxt[ctxt]
3272 opt_rescue
3273 {
3274 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3275 }
3276 opt_ensure
3277 {
3278 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3279 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3280 }
3281 ;
3282
3283stmts : none
3284 {
3285 $$ = NEW_BEGIN(0, &@$);
3286 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3287 }
3288 | stmt_or_begin
3289 {
3290 $$ = newline_node($1);
3291 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3292 }
3293 | stmts terms stmt_or_begin
3294 {
3295 $$ = block_append(p, $1, newline_node($3));
3296 /*% ripper: stmts_add!($:1, $:3) %*/
3297 }
3298 ;
3299
3300stmt_or_begin : stmt
3301 | keyword_BEGIN
3302 {
3303 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3304 }
3305 begin_block
3306 {
3307 $$ = $3;
3308 }
3309 ;
3310
3311allow_exits : {$$ = allow_block_exit(p);};
3312
3313k_END : keyword_END lex_ctxt
3314 {
3315 if (p->ctxt.in_def) {
3316 rb_warn0("END in method; use at_exit");
3317 }
3318 $$ = $2;
3319 p->ctxt.in_rescue = before_rescue;
3320 /*% ripper: $:2 %*/
3321 };
3322
3323stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3324 {
3325 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3326 /*% ripper: alias!($:new, $:old) %*/
3327 }
3328 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3329 {
3330 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3331 /*% ripper: var_alias!($:new, $:old) %*/
3332 }
3333 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
3334 {
3335 char buf[2];
3336 buf[0] = '$';
3337 buf[1] = (char)RNODE_BACK_REF($old)->nd_nth;
3338 $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw);
3339 /*% ripper: var_alias!($:new, $:old) %*/
3340 }
3341 | keyword_alias tGVAR tNTH_REF[nth]
3342 {
3343 static const char mesg[] = "can't make alias for the number variables";
3344 /*%%%*/
3345 yyerror1(&@nth, mesg);
3346 /*% %*/
3347 $$ = NEW_ERROR(&@$);
3348 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3349 }
3350 | keyword_undef[kw] undef_list[list]
3351 {
3352 nd_set_first_loc($list, @kw.beg_pos);
3353 RNODE_UNDEF($list)->keyword_loc = @kw;
3354 $$ = $list;
3355 /*% ripper: undef!($:list) %*/
3356 }
3357 | stmt[body] modifier_if[mod] expr_value[cond]
3358 {
3359 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3360 fixpos($$, $cond);
3361 /*% ripper: if_mod!($:cond, $:body) %*/
3362 }
3363 | stmt[body] modifier_unless[mod] expr_value[cond]
3364 {
3365 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3366 fixpos($$, $cond);
3367 /*% ripper: unless_mod!($:cond, $:body) %*/
3368 }
3369 | stmt[body] modifier_while[mod] expr_value[cond_expr]
3370 {
3371 clear_block_exit(p, false);
3372 if ($body && nd_type_p($body, NODE_BEGIN)) {
3373 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3374 }
3375 else {
3376 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3377 }
3378 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3379 }
3380 | stmt[body] modifier_until[mod] expr_value[cond_expr]
3381 {
3382 clear_block_exit(p, 0);
3383 if ($body && nd_type_p($body, NODE_BEGIN)) {
3384 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3385 }
3386 else {
3387 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3388 }
3389 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3390 }
3391 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3392 {
3393 p->ctxt.in_rescue = $ctxt.in_rescue;
3394 NODE *resq;
3395 YYLTYPE loc = code_loc_gen(&@mod, &@resbody);
3396 resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3397 $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$);
3398 /*% ripper: rescue_mod!($:body, $:resbody) %*/
3399 }
3400 | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace]
3401 {
3402 clear_block_exit(p, true);
3403 restore_block_exit(p, $block_open);
3404 p->ctxt = $k_end;
3405 {
3406 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$);
3407 $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace);
3408 RNODE_SCOPE(scope)->nd_parent = $$;
3409 }
3410 /*% ripper: END!($:body) %*/
3411 }
3412 | command_asgn
3413 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3414 {
3415 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3416 /*% ripper: massign!($:lhs, $:rhs) %*/
3417 }
3418 | asgn(mrhs)
3419 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3420 after_rescue[after_rescue] stmt[resbody]
3421 {
3422 p->ctxt.in_rescue = $after_rescue.in_rescue;
3423 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3424 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3425 loc.beg_pos = @mrhs_arg.beg_pos;
3426 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3427 $$ = node_assign(p, (NODE *)$lhs, $mrhs_arg, $lex_ctxt, &@$);
3428 /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/
3429 }
3430 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3431 {
3432 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3433 /*% ripper: massign!($:lhs, $:rhs) %*/
3434 }
3435 | expr
3436 | error
3437 {
3438 (void)yynerrs;
3439 $$ = NEW_ERROR(&@$);
3440 }
3441 ;
3442
3443command_asgn : asgn(command_rhs)
3444 | op_asgn(command_rhs)
3445 | def_endless_method(endless_command)
3446 ;
3447
3448endless_command : command
3449 | endless_command modifier_rescue after_rescue arg
3450 {
3451 p->ctxt.in_rescue = $3.in_rescue;
3452 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3453 /*% ripper: rescue_mod!($:1, $:4) %*/
3454 }
3455 | keyword_not '\n'? endless_command
3456 {
3457 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3458 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3459 }
3460 ;
3461
3462command_rhs : command_call_value %prec tOP_ASGN
3463 | command_call_value modifier_rescue after_rescue stmt
3464 {
3465 p->ctxt.in_rescue = $3.in_rescue;
3466 YYLTYPE loc = code_loc_gen(&@2, &@4);
3467 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3468 /*% ripper: rescue_mod!($:1, $:4) %*/
3469 }
3470 | command_asgn
3471 ;
3472
3473expr : command_call
3474 | expr[left] keyword_and[op] expr[right]
3475 {
3476 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3477 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3478 }
3479 | expr[left] keyword_or[op] expr[right]
3480 {
3481 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3482 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3483 }
3484 | keyword_not[not] '\n'? expr[arg]
3485 {
3486 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3487 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3488 }
3489 | '!'[not] command_call[arg]
3490 {
3491 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3492 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3493 }
3494 | arg tASSOC[assoc]
3495 {
3496 value_expr(p, $arg);
3497 }
3498 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3499 p_top_expr_body[body]
3500 {
3501 pop_pktbl(p, $p_pktbl);
3502 pop_pvtbl(p, $p_pvtbl);
3503 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3504 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3505 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3506 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@assoc), &@$, &NULL_LOC, &NULL_LOC);
3507 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3508 }
3509 | arg keyword_in
3510 {
3511 value_expr(p, $arg);
3512 }
3513 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3514 p_top_expr_body[body]
3515 {
3516 pop_pktbl(p, $p_pktbl);
3517 pop_pvtbl(p, $p_pvtbl);
3518 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3519 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3520 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3521 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3522 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3523 }
3524 | arg %prec tLBRACE_ARG
3525 ;
3526
3527def_name : fname
3528 {
3529 numparam_name(p, $fname);
3530 local_push(p, 0);
3531 p->ctxt.in_def = 1;
3532 p->ctxt.in_rescue = before_rescue;
3533 p->ctxt.cant_return = 0;
3534 $$ = $fname;
3535 }
3536 ;
3537
3538defn_head : k_def def_name
3539 {
3540 $$ = def_head_save(p, $k_def);
3541 $$->nd_mid = $def_name;
3542 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3543 /*% ripper: $:def_name %*/
3544 }
3545 ;
3546
3547defs_head : k_def singleton dot_or_colon
3548 {
3549 SET_LEX_STATE(EXPR_FNAME);
3550 }
3551 def_name
3552 {
3553 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3554 $$ = def_head_save(p, $k_def);
3555 $$->nd_mid = $def_name;
3556 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3557 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3558 }
3559 ;
3560
3561expr_value : value_expr(expr)
3562 | error
3563 {
3564 $$ = NEW_ERROR(&@$);
3565 }
3566 ;
3567
3568expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3569 {
3570 $$ = $2;
3571 /*% ripper: $:2 %*/
3572 }
3573 ;
3574
3575command_call : command
3576 | block_command
3577 ;
3578
3579command_call_value : value_expr(command_call)
3580 ;
3581
3582block_command : block_call
3583 | block_call call_op2 operation2 command_args
3584 {
3585 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3586 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3587 }
3588 ;
3589
3590cmd_brace_block : tLBRACE_ARG brace_body '}'
3591 {
3592 $$ = $2;
3593 set_embraced_location($$, &@1, &@3);
3594 /*% ripper: $:2 %*/
3595 }
3596 ;
3597
3598fcall : operation
3599 {
3600 $$ = NEW_FCALL($1, 0, &@$);
3601 /*% ripper: $:1 %*/
3602 }
3603 ;
3604
3605command : fcall command_args %prec tLOWEST
3606 {
3607 $1->nd_args = $2;
3608 nd_set_last_loc($1, @2.end_pos);
3609 $$ = (NODE *)$1;
3610 /*% ripper: command!($:1, $:2) %*/
3611 }
3612 | fcall command_args cmd_brace_block
3613 {
3614 block_dup_check(p, $2, $3);
3615 $1->nd_args = $2;
3616 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3617 fixpos($$, RNODE($1));
3618 nd_set_last_loc($1, @2.end_pos);
3619 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3620 }
3621 | primary_value call_op operation2 command_args %prec tLOWEST
3622 {
3623 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3624 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3625 }
3626 | primary_value call_op operation2 command_args cmd_brace_block
3627 {
3628 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3629 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3630 }
3631 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3632 {
3633 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3634 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3635 }
3636 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3637 {
3638 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3639 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3640 }
3641 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3642 {
3643 set_embraced_location($5, &@4, &@6);
3644 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3645 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3646 }
3647 | keyword_super command_args
3648 {
3649 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3650 fixpos($$, $2);
3651 /*% ripper: super!($:2) %*/
3652 }
3653 | k_yield command_args
3654 {
3655 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3656 fixpos($$, $2);
3657 /*% ripper: yield!($:2) %*/
3658 }
3659 | k_return_arg call_args
3660 {
3661 CMDARG_POP();
3662 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3663 /*% ripper: return!($:2) %*/
3664 }
3665 | k_break_arg call_args
3666 {
3667 CMDARG_POP();
3668 NODE *args = 0;
3669 args = ret_args(p, $2);
3670 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3671 /*% ripper: break!($:2) %*/
3672 }
3673 | k_next_arg call_args
3674 {
3675 CMDARG_POP();
3676 NODE *args = 0;
3677 args = ret_args(p, $2);
3678 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3679 /*% ripper: next!($:2) %*/
3680 }
3681 ;
3682
3683mlhs : mlhs_basic
3684 | tLPAREN mlhs_inner rparen
3685 {
3686 $$ = $2;
3687 /*% ripper: mlhs_paren!($:2) %*/
3688 }
3689 ;
3690
3691mlhs_inner : mlhs_basic
3692 | tLPAREN mlhs_inner rparen
3693 {
3694 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3695 /*% ripper: mlhs_paren!($:2) %*/
3696 }
3697 ;
3698
3699mlhs_basic : mlhs_head
3700 {
3701 $$ = NEW_MASGN($1, 0, &@$);
3702 /*% ripper: $:1 %*/
3703 }
3704 | mlhs_head mlhs_item
3705 {
3706 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3707 /*% ripper: mlhs_add!($:1, $:2) %*/
3708 }
3709 | mlhs_head tSTAR mlhs_node
3710 {
3711 $$ = NEW_MASGN($1, $3, &@$);
3712 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3713 }
3714 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3715 {
3716 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3717 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3718 }
3719 | mlhs_head tSTAR
3720 {
3721 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3722 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3723 }
3724 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3725 {
3726 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3727 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3728 }
3729 | tSTAR mlhs_node
3730 {
3731 $$ = NEW_MASGN(0, $2, &@$);
3732 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3733 }
3734 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3735 {
3736 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3737 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3738 }
3739 | tSTAR
3740 {
3741 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3742 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3743 }
3744 | tSTAR ',' mlhs_items(mlhs_item)
3745 {
3746 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3747 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3748 }
3749 ;
3750
3751mlhs_item : mlhs_node
3752 | tLPAREN mlhs_inner rparen
3753 {
3754 $$ = (NODE *)$2;
3755 /*% ripper: mlhs_paren!($:2) %*/
3756 }
3757 ;
3758
3759mlhs_head : mlhs_item ','
3760 {
3761 $$ = NEW_LIST($1, &@1);
3762 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3763 }
3764 | mlhs_head mlhs_item ','
3765 {
3766 $$ = list_append(p, $1, $2);
3767 /*% ripper: mlhs_add!($:1, $:2) %*/
3768 }
3769 ;
3770
3771
3772mlhs_node : user_or_keyword_variable
3773 {
3774 /*% ripper: var_field!($:1) %*/
3775 $$ = assignable(p, $1, 0, &@$);
3776 }
3777 | primary_value '[' opt_call_args rbracket
3778 {
3779 $$ = aryset(p, $1, $3, &@$);
3780 /*% ripper: aref_field!($:1, $:3) %*/
3781 }
3782 | primary_value call_op ident_or_const
3783 {
3784 anddot_multiple_assignment_check(p, &@2, $2);
3785 $$ = attrset(p, $1, $2, $3, &@$);
3786 /*% ripper: field!($:1, $:2, $:3) %*/
3787 }
3788 | primary_value tCOLON2 tIDENTIFIER
3789 {
3790 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3791 /*% ripper: const_path_field!($:1, $:3) %*/
3792 }
3793 | primary_value tCOLON2 tCONSTANT
3794 {
3795 /*% ripper: const_path_field!($:1, $:3) %*/
3796 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3797 }
3798 | tCOLON3 tCONSTANT
3799 {
3800 /*% ripper: top_const_field!($:2) %*/
3801 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3802 }
3803 | backref
3804 {
3805 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3806 $$ = NEW_ERROR(&@$);
3807 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3808 }
3809 ;
3810
3811lhs : user_or_keyword_variable
3812 {
3813 /*% ripper: var_field!($:1) %*/
3814 $$ = assignable(p, $1, 0, &@$);
3815 }
3816 | primary_value '[' opt_call_args rbracket
3817 {
3818 $$ = aryset(p, $1, $3, &@$);
3819 /*% ripper: aref_field!($:1, $:3) %*/
3820 }
3821 | primary_value call_op ident_or_const
3822 {
3823 $$ = attrset(p, $1, $2, $3, &@$);
3824 /*% ripper: field!($:1, $:2, $:3) %*/
3825 }
3826 | primary_value tCOLON2 tIDENTIFIER
3827 {
3828 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3829 /*% ripper: field!($:1, $:2, $:3) %*/
3830 }
3831 | primary_value tCOLON2 tCONSTANT
3832 {
3833 /*% ripper: const_path_field!($:1, $:3) %*/
3834 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3835 }
3836 | tCOLON3 tCONSTANT
3837 {
3838 /*% ripper: top_const_field!($:2) %*/
3839 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3840 }
3841 | backref
3842 {
3843 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3844 $$ = NEW_ERROR(&@$);
3845 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3846 }
3847 ;
3848
3849cname : tIDENTIFIER
3850 {
3851 static const char mesg[] = "class/module name must be CONSTANT";
3852 /*%%%*/
3853 yyerror1(&@1, mesg);
3854 /*% %*/
3855 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3856 }
3857 | tCONSTANT
3858 ;
3859
3860cpath : tCOLON3 cname
3861 {
3862 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3863 /*% ripper: top_const_ref!($:2) %*/
3864 }
3865 | cname
3866 {
3867 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3868 /*% ripper: const_ref!($:1) %*/
3869 }
3870 | primary_value tCOLON2 cname
3871 {
3872 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3873 /*% ripper: const_path_ref!($:1, $:3) %*/
3874 }
3875 ;
3876
3877fname : operation
3878 | op
3879 {
3880 SET_LEX_STATE(EXPR_ENDFN);
3881 $$ = $1;
3882 }
3883 | reswords
3884 ;
3885
3886fitem : fname
3887 {
3888 $$ = NEW_SYM(rb_id2str($1), &@$);
3889 /*% ripper: symbol_literal!($:1) %*/
3890 }
3891 | symbol
3892 ;
3893
3894undef_list : fitem
3895 {
3896 $$ = NEW_UNDEF($1, &@$);
3897 /*% ripper: rb_ary_new3(1, $:1) %*/
3898 }
3899 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3900 {
3901 nd_set_last_loc($1, @4.end_pos);
3902 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3903 /*% ripper: rb_ary_push($:1, $:4) %*/
3904 }
3905 ;
3906
3907op : '|' { $$ = '|'; }
3908 | '^' { $$ = '^'; }
3909 | '&' { $$ = '&'; }
3910 | tCMP { $$ = tCMP; }
3911 | tEQ { $$ = tEQ; }
3912 | tEQQ { $$ = tEQQ; }
3913 | tMATCH { $$ = tMATCH; }
3914 | tNMATCH { $$ = tNMATCH; }
3915 | '>' { $$ = '>'; }
3916 | tGEQ { $$ = tGEQ; }
3917 | '<' { $$ = '<'; }
3918 | tLEQ { $$ = tLEQ; }
3919 | tNEQ { $$ = tNEQ; }
3920 | tLSHFT { $$ = tLSHFT; }
3921 | tRSHFT { $$ = tRSHFT; }
3922 | '+' { $$ = '+'; }
3923 | '-' { $$ = '-'; }
3924 | '*' { $$ = '*'; }
3925 | tSTAR { $$ = '*'; }
3926 | '/' { $$ = '/'; }
3927 | '%' { $$ = '%'; }
3928 | tPOW { $$ = tPOW; }
3929 | tDSTAR { $$ = tDSTAR; }
3930 | '!' { $$ = '!'; }
3931 | '~' { $$ = '~'; }
3932 | tUPLUS { $$ = tUPLUS; }
3933 | tUMINUS { $$ = tUMINUS; }
3934 | tAREF { $$ = tAREF; }
3935 | tASET { $$ = tASET; }
3936 | '`' { $$ = '`'; }
3937 ;
3938
3939reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3940 | keyword_BEGIN | keyword_END
3941 | keyword_alias | keyword_and | keyword_begin
3942 | keyword_break | keyword_case | keyword_class | keyword_def
3943 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3944 | keyword_end | keyword_ensure | keyword_false
3945 | keyword_for | keyword_in | keyword_module | keyword_next
3946 | keyword_nil | keyword_not | keyword_or | keyword_redo
3947 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3948 | keyword_super | keyword_then | keyword_true | keyword_undef
3949 | keyword_when | keyword_yield | keyword_if | keyword_unless
3950 | keyword_while | keyword_until
3951 ;
3952
3953arg : asgn(arg_rhs)
3954 | op_asgn(arg_rhs)
3955 | range_expr(arg)
3956 | arg '+' arg
3957 {
3958 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3959 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3960 }
3961 | arg '-' arg
3962 {
3963 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3964 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3965 }
3966 | arg '*' arg
3967 {
3968 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3969 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3970 }
3971 | arg '/' arg
3972 {
3973 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3974 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3975 }
3976 | arg '%' arg
3977 {
3978 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3979 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3980 }
3981 | arg tPOW arg
3982 {
3983 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3984 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3985 }
3986 | tUMINUS_NUM simple_numeric tPOW arg
3987 {
3988 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3989 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3990 }
3991 | tUPLUS arg
3992 {
3993 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3994 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3995 }
3996 | tUMINUS arg
3997 {
3998 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3999 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
4000 }
4001 | arg '|' arg
4002 {
4003 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
4004 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
4005 }
4006 | arg '^' arg
4007 {
4008 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
4009 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
4010 }
4011 | arg '&' arg
4012 {
4013 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
4014 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
4015 }
4016 | arg tCMP arg
4017 {
4018 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
4019 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
4020 }
4021 | rel_expr %prec tCMP
4022 | arg tEQ arg
4023 {
4024 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4025 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4026 }
4027 | arg tEQQ arg
4028 {
4029 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4030 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4031 }
4032 | arg tNEQ arg
4033 {
4034 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4035 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4036 }
4037 | arg tMATCH arg
4038 {
4039 $$ = match_op(p, $1, $3, &@2, &@$);
4040 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4041 }
4042 | arg tNMATCH arg
4043 {
4044 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4045 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4046 }
4047 | '!' arg
4048 {
4049 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4050 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4051 }
4052 | '~' arg
4053 {
4054 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4055 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4056 }
4057 | arg tLSHFT arg
4058 {
4059 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4060 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4061 }
4062 | arg tRSHFT arg
4063 {
4064 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4065 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4066 }
4067 | arg tANDOP arg
4068 {
4069 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4070 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4071 }
4072 | arg tOROP arg
4073 {
4074 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4075 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4076 }
4077 | keyword_defined '\n'? begin_defined arg
4078 {
4079 p->ctxt.in_defined = $3.in_defined;
4080 $$ = new_defined(p, $4, &@$, &@1);
4081 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4082 /*% ripper: defined!($:4) %*/
4083 }
4084 | def_endless_method(endless_arg)
4085 | ternary
4086 | primary
4087 ;
4088
4089ternary : arg '?' arg '\n'? ':' arg
4090 {
4091 value_expr(p, $1);
4092 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4093 fixpos($$, $1);
4094 /*% ripper: ifop!($:1, $:3, $:6) %*/
4095 }
4096 ;
4097
4098endless_arg : arg %prec modifier_rescue
4099 | endless_arg modifier_rescue after_rescue arg
4100 {
4101 p->ctxt.in_rescue = $3.in_rescue;
4102 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4103 /*% ripper: rescue_mod!($:1, $:4) %*/
4104 }
4105 | keyword_not '\n'? endless_arg
4106 {
4107 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4108 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4109 }
4110 ;
4111
4112relop : '>' {$$ = '>';}
4113 | '<' {$$ = '<';}
4114 | tGEQ {$$ = idGE;}
4115 | tLEQ {$$ = idLE;}
4116 ;
4117
4118rel_expr : arg relop arg %prec '>'
4119 {
4120 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4121 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4122 }
4123 | rel_expr relop arg %prec '>'
4124 {
4125 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4126 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4127 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4128 }
4129 ;
4130
4131lex_ctxt : none
4132 {
4133 $$ = p->ctxt;
4134 }
4135 ;
4136
4137begin_defined : lex_ctxt
4138 {
4139 p->ctxt.in_defined = 1;
4140 $$ = $1;
4141 }
4142 ;
4143
4144after_rescue : lex_ctxt
4145 {
4146 p->ctxt.in_rescue = after_rescue;
4147 $$ = $1;
4148 }
4149 ;
4150
4151arg_value : value_expr(arg)
4152 ;
4153
4154aref_args : none
4155 | args trailer
4156 | args ',' assocs trailer
4157 {
4158 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4159 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4160 }
4161 | assocs trailer
4162 {
4163 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4164 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4165 }
4166 ;
4167
4168arg_rhs : arg %prec tOP_ASGN
4169 {
4170 value_expr(p, $1);
4171 $$ = $1;
4172 }
4173 | arg modifier_rescue after_rescue arg
4174 {
4175 p->ctxt.in_rescue = $3.in_rescue;
4176 value_expr(p, $1);
4177 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4178 /*% ripper: rescue_mod!($:1, $:4) %*/
4179 }
4180 ;
4181
4182paren_args : '(' opt_call_args rparen
4183 {
4184 $$ = $2;
4185 /*% ripper: arg_paren!($:2) %*/
4186 }
4187 | '(' args ',' args_forward rparen
4188 {
4189 if (!check_forwarding_args(p)) {
4190 $$ = 0;
4191 }
4192 else {
4193 $$ = new_args_forward_call(p, $2, &@4, &@$);
4194 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4195 }
4196 }
4197 | '(' args_forward rparen
4198 {
4199 if (!check_forwarding_args(p)) {
4200 $$ = 0;
4201 }
4202 else {
4203 $$ = new_args_forward_call(p, 0, &@2, &@$);
4204 /*% ripper: arg_paren!($:2) %*/
4205 }
4206 }
4207 ;
4208
4209opt_paren_args : none
4210 | paren_args
4211 {
4212 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4213 }
4214 ;
4215
4216opt_call_args : none
4217 | call_args
4218 | args ','
4219 | args ',' assocs ','
4220 {
4221 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4222 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4223 }
4224 | assocs ','
4225 {
4226 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4227 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4228 }
4229 ;
4230
4231call_args : value_expr(command)
4232 {
4233 $$ = NEW_LIST($1, &@$);
4234 /*% ripper: args_add!(args_new!, $:1) %*/
4235 }
4236 | def_endless_method(endless_command)
4237 {
4238 $$ = NEW_LIST($1, &@$);
4239 /*% ripper: args_add!(args_new!, $:1) %*/
4240 }
4241 | args opt_block_arg
4242 {
4243 $$ = arg_blk_pass($1, $2);
4244 /*% ripper: args_add_block!($:1, $:2) %*/
4245 }
4246 | assocs opt_block_arg
4247 {
4248 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4249 $$ = arg_blk_pass($$, $2);
4250 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4251 }
4252 | args ',' assocs opt_block_arg
4253 {
4254 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4255 $$ = arg_blk_pass($$, $4);
4256 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4257 }
4258 | block_arg
4259 /*% ripper: args_add_block!(args_new!, $:1) %*/
4260 ;
4261
4262command_args : {
4263 /* If call_args starts with a open paren '(' or '[',
4264 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4265 * but the push must be done after CMDARG_PUSH(1).
4266 * So this code makes them consistent by first cancelling
4267 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4268 * and finally redoing CMDARG_PUSH(0).
4269 */
4270 int lookahead = 0;
4271 switch (yychar) {
4272 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4273 lookahead = 1;
4274 }
4275 if (lookahead) CMDARG_POP();
4276 CMDARG_PUSH(1);
4277 if (lookahead) CMDARG_PUSH(0);
4278 }
4279 call_args
4280 {
4281 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4282 * but the push must be done after CMDARG_POP() in the parser.
4283 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4284 * CMDARG_POP() to pop 1 pushed by command_args,
4285 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4286 */
4287 int lookahead = 0;
4288 switch (yychar) {
4289 case tLBRACE_ARG:
4290 lookahead = 1;
4291 }
4292 if (lookahead) CMDARG_POP();
4293 CMDARG_POP();
4294 if (lookahead) CMDARG_PUSH(0);
4295 $$ = $2;
4296 /*% ripper: $:2 %*/
4297 }
4298 ;
4299
4300block_arg : tAMPER arg_value
4301 {
4302 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4303 /*% ripper: $:2 %*/
4304 }
4305 | tAMPER
4306 {
4307 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4308 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4309 /*% ripper: Qnil %*/
4310 }
4311 ;
4312
4313opt_block_arg : ',' block_arg
4314 {
4315 $$ = $2;
4316 /*% ripper: $:2 %*/
4317 }
4318 | none
4319 {
4320 $$ = 0;
4321 /*% ripper: Qfalse %*/
4322 }
4323 ;
4324
4325/* value */
4326args : arg_value
4327 {
4328 $$ = NEW_LIST($arg_value, &@$);
4329 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4330 }
4331 | arg_splat
4332 {
4333 $$ = $arg_splat;
4334 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4335 }
4336 | args[non_last_args] ',' arg_value
4337 {
4338 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4339 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4340 }
4341 | args[non_last_args] ',' arg_splat
4342 {
4343 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4344 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4345 }
4346 ;
4347
4348/* value */
4349arg_splat : tSTAR arg_value
4350 {
4351 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4352 /*% ripper: $:arg_value %*/
4353 }
4354 | tSTAR /* none */
4355 {
4356 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4357 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4358 /*% ripper: Qnil %*/
4359 }
4360 ;
4361
4362/* value */
4363mrhs_arg : mrhs
4364 | arg_value
4365 ;
4366
4367/* value */
4368mrhs : args ',' arg_value
4369 {
4370 $$ = last_arg_append(p, $args, $arg_value, &@$);
4371 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4372 }
4373 | args ',' tSTAR arg_value
4374 {
4375 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4376 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4377 }
4378 | tSTAR arg_value
4379 {
4380 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4381 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4382 }
4383 ;
4384
4385%rule %inline inline_primary
4386 : literal
4387 | strings
4388 | xstring
4389 | regexp
4390 | words
4391 | qwords
4392 | symbols
4393 | qsymbols
4394 ;
4395
4396primary : inline_primary
4397 | var_ref
4398 | backref
4399 | tFID[fid]
4400 {
4401 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4402 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
4403 }
4404 | k_begin[kw]
4405 {
4406 CMDARG_PUSH(0);
4407 }
4408 bodystmt[body]
4409 k_end[k_end]
4410 {
4411 CMDARG_POP();
4412 set_line_body($body, @kw.end_pos.lineno);
4413 $$ = NEW_BEGIN($body, &@$);
4414 nd_set_line($$, @kw.end_pos.lineno);
4415 /*% ripper: begin!($:body) %*/
4416 }
4417 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4418 {
4419 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4420 $$ = $body;
4421 /*% ripper: paren!($:body) %*/
4422 }
4423 | tLPAREN compstmt(stmts)[body] ')'
4424 {
4425 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4426 $$ = NEW_BLOCK($body, &@$);
4427 /*% ripper: paren!($:body) %*/
4428 }
4429 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4430 {
4431 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4432 /*% ripper: const_path_ref!($:recv, $:name) %*/
4433 }
4434 | tCOLON3[top] tCONSTANT[name]
4435 {
4436 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4437 /*% ripper: top_const_ref!($:name) %*/
4438 }
4439 | tLBRACK aref_args[args] ']'
4440 {
4441 $$ = make_list($args, &@$);
4442 /*% ripper: array!($:args) %*/
4443 }
4444 | tLBRACE assoc_list[list] '}'
4445 {
4446 $$ = new_hash(p, $list, &@$);
4447 RNODE_HASH($$)->nd_brace = TRUE;
4448 /*% ripper: hash!($:list) %*/
4449 }
4450 | k_return[kw]
4451 {
4452 $$ = NEW_RETURN(0, &@$, &@kw);
4453 /*% ripper: return0! %*/
4454 }
4455 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4456 {
4457 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4458 /*% ripper: yield!(paren!($:args)) %*/
4459 }
4460 | k_yield[kw] '('[lpar] rparen[rpar]
4461 {
4462 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4463 /*% ripper: yield!(paren!(args_new!)) %*/
4464 }
4465 | k_yield[kw]
4466 {
4467 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4468 /*% ripper: yield0! %*/
4469 }
4470 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
4471 {
4472 p->ctxt.in_defined = $ctxt.in_defined;
4473 $$ = new_defined(p, $arg, &@$, &@kw);
4474 p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon;
4475 /*% ripper: defined!($:arg) %*/
4476 }
4477 | keyword_not[kw] '(' expr[arg] rparen
4478 {
4479 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4480 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4481 }
4482 | keyword_not[kw] '('[lpar] rparen
4483 {
4484 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4485 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4486 }
4487 | fcall[call] brace_block[block]
4488 {
4489 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4490 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4491 }
4492 | method_call
4493 | method_call[call] brace_block[block]
4494 {
4495 block_dup_check(p, get_nd_args(p, $call), $block);
4496 $$ = method_add_block(p, $call, $block, &@$);
4497 /*% ripper: method_add_block!($:call, $:block) %*/
4498 }
4499 | lambda
4500 | k_if[kw] expr_value[cond] then[then]
4501 compstmt(stmts)[body]
4502 if_tail[tail]
4503 k_end[k_end]
4504 {
4505 if ($tail && nd_type_p($tail, NODE_IF))
4506 RNODE_IF($tail)->end_keyword_loc = @k_end;
4507
4508 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4509 fixpos($$, $cond);
4510 /*% ripper: if!($:cond, $:body, $:tail) %*/
4511 }
4512 | k_unless[kw] expr_value[cond] then[then]
4513 compstmt(stmts)[body]
4514 opt_else[tail]
4515 k_end[k_end]
4516 {
4517 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4518 fixpos($$, $cond);
4519 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4520 }
4521 | k_while[kw] expr_value_do[cond]
4522 compstmt(stmts)[body]
4523 k_end[k_end]
4524 {
4525 restore_block_exit(p, $kw);
4526 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4527 fixpos($$, $cond);
4528 /*% ripper: while!($:cond, $:body) %*/
4529 }
4530 | k_until[kw] expr_value_do[cond]
4531 compstmt(stmts)[body]
4532 k_end[k_end]
4533 {
4534 restore_block_exit(p, $kw);
4535 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4536 fixpos($$, $cond);
4537 /*% ripper: until!($:cond, $:body) %*/
4538 }
4539 | k_case[k_case] expr_value[expr] terms?
4540 {
4541 $$ = p->case_labels;
4542 p->case_labels = CHECK_LITERAL_WHEN;
4543 }[labels]<labels>
4544 case_body[body]
4545 k_end[k_end]
4546 {
4547 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4548 p->case_labels = $labels;
4549 $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end);
4550 fixpos($$, $expr);
4551 /*% ripper: case!($:expr, $:body) %*/
4552 }
4553 | k_case[k_case] terms?
4554 {
4555 $$ = p->case_labels;
4556 p->case_labels = 0;
4557 }[labels]<labels>
4558 case_body[body]
4559 k_end[k_end]
4560 {
4561 if (p->case_labels) st_free_table(p->case_labels);
4562 p->case_labels = $labels;
4563 $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end);
4564 /*% ripper: case!(Qnil, $:body) %*/
4565 }
4566 | k_case[k_case] expr_value[expr] terms?
4567 p_case_body[body]
4568 k_end[k_end]
4569 {
4570 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4571 /*% ripper: case!($:expr, $:body) %*/
4572 }
4573 | k_for[k_for] for_var[for_var] keyword_in[keyword_in]
4574 {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();}
4575 compstmt(stmts)[compstmt]
4576 k_end[k_end]
4577 {
4578 restore_block_exit(p, $k_for);
4579 /*
4580 * for a, b, c in e
4581 * #=>
4582 * e.each{|*x| a, b, c = x}
4583 *
4584 * for a in e
4585 * #=>
4586 * e.each{|x| a, = x}
4587 */
4588 ID id = internal_id(p);
4589 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4590 rb_node_args_t *args;
4591 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4592 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4593 tbl->ids[0] = id; /* internal id */
4594
4595 switch (nd_type($for_var)) {
4596 case NODE_LASGN:
4597 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4598 set_nd_value(p, $for_var, internal_var);
4599 id = 0;
4600 m->nd_plen = 1;
4601 m->nd_next = $for_var;
4602 break;
4603 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4604 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4605 break;
4606 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4607 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($for_var, &@for_var), 0, &@for_var), internal_var, NO_LEX_CTXT, &@for_var);
4608 }
4609 /* {|*internal_id| <m> = internal_id; ... } */
4610 args = new_args(p, m, 0, id, 0, new_empty_args_tail(p, &@for_var), &@for_var);
4611 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4612 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4613 $$ = NEW_FOR($expr_value, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4614 RNODE_SCOPE(scope)->nd_parent = $$;
4615 fixpos($$, $for_var);
4616 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4617 }
4618 | k_class cpath superclass
4619 {
4620 begin_definition("class", &@k_class, &@cpath);
4621 }
4622 bodystmt
4623 k_end
4624 {
4625 YYLTYPE inheritance_operator_loc = NULL_LOC;
4626 if ($superclass) {
4627 inheritance_operator_loc = @superclass;
4628 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4629 }
4630 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4631 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4632 set_line_body($bodystmt, @superclass.end_pos.lineno);
4633 nd_set_line($$, @superclass.end_pos.lineno);
4634 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4635 local_pop(p);
4636 p->ctxt.in_class = $k_class.in_class;
4637 p->ctxt.cant_return = $k_class.cant_return;
4638 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4639 }
4640 | k_class tLSHFT expr_value
4641 {
4642 begin_definition("", &@k_class, &@tLSHFT);
4643 }
4644 term
4645 bodystmt
4646 k_end
4647 {
4648 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4649 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4650 set_line_body($bodystmt, nd_line($expr_value));
4651 fixpos($$, $expr_value);
4652 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4653 local_pop(p);
4654 p->ctxt.in_def = $k_class.in_def;
4655 p->ctxt.in_class = $k_class.in_class;
4656 p->ctxt.cant_return = $k_class.cant_return;
4657 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4658 }
4659 | k_module cpath
4660 {
4661 begin_definition("module", &@k_module, &@cpath);
4662 }
4663 bodystmt
4664 k_end
4665 {
4666 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4667 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4668 set_line_body($bodystmt, @cpath.end_pos.lineno);
4669 nd_set_line($$, @cpath.end_pos.lineno);
4670 /*% ripper: module!($:cpath, $:bodystmt) %*/
4671 local_pop(p);
4672 p->ctxt.in_class = $k_module.in_class;
4673 p->ctxt.cant_return = $k_module.cant_return;
4674 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4675 }
4676 | defn_head[head]
4677 f_arglist[args]
4678 {
4679 push_end_expect_token_locations(p, &@head.beg_pos);
4680 }
4681 bodystmt
4682 k_end
4683 {
4684 restore_defun(p, $head);
4685 ($$ = $head->nd_def)->nd_loc = @$;
4686 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4687 RNODE_DEFN($$)->nd_defn = $bodystmt;
4688 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4689 local_pop(p);
4690 }
4691 | defs_head[head]
4692 f_arglist[args]
4693 {
4694 push_end_expect_token_locations(p, &@head.beg_pos);
4695 }
4696 bodystmt
4697 k_end
4698 {
4699 restore_defun(p, $head);
4700 ($$ = $head->nd_def)->nd_loc = @$;
4701 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4702 RNODE_DEFS($$)->nd_defn = $bodystmt;
4703 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4704 local_pop(p);
4705 }
4706 | keyword_break[kw]
4707 {
4708 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4709 /*% ripper: break!(args_new!) %*/
4710 }
4711 | keyword_next[kw]
4712 {
4713 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4714 /*% ripper: next!(args_new!) %*/
4715 }
4716 | keyword_redo[kw]
4717 {
4718 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4719 /*% ripper: redo! %*/
4720 }
4721 | keyword_retry[kw]
4722 {
4723 if (!p->ctxt.in_defined) {
4724 switch (p->ctxt.in_rescue) {
4725 case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break;
4726 case after_rescue: /* ok */ break;
4727 case after_else: yyerror1(&@kw, "Invalid retry after else"); break;
4728 case after_ensure: yyerror1(&@kw, "Invalid retry after ensure"); break;
4729 }
4730 }
4731 $$ = NEW_RETRY(&@$);
4732 /*% ripper: retry! %*/
4733 }
4734 ;
4735
4736primary_value : value_expr(primary)
4737 ;
4738
4739k_begin : keyword_begin
4740 {
4741 token_info_push(p, "begin", &@$);
4742 push_end_expect_token_locations(p, &@1.beg_pos);
4743 }
4744 ;
4745
4746k_if : keyword_if
4747 {
4748 WARN_EOL("if");
4749 token_info_push(p, "if", &@$);
4750 if (p->token_info && p->token_info->nonspc &&
4751 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4752 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4753 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4754 beg += rb_strlen_lit("else");
4755 while (beg < tok && ISSPACE(*beg)) beg++;
4756 if (beg == tok) {
4757 p->token_info->nonspc = 0;
4758 }
4759 }
4760 push_end_expect_token_locations(p, &@1.beg_pos);
4761 }
4762 ;
4763
4764k_unless : keyword_unless
4765 {
4766 token_info_push(p, "unless", &@$);
4767 push_end_expect_token_locations(p, &@1.beg_pos);
4768 }
4769 ;
4770
4771k_while : keyword_while[kw] allow_exits
4772 {
4773 $$ = $allow_exits;
4774 token_info_push(p, "while", &@$);
4775 push_end_expect_token_locations(p, &@kw.beg_pos);
4776 }
4777 ;
4778
4779k_until : keyword_until[kw] allow_exits
4780 {
4781 $$ = $allow_exits;
4782 token_info_push(p, "until", &@$);
4783 push_end_expect_token_locations(p, &@kw.beg_pos);
4784 }
4785 ;
4786
4787k_case : keyword_case
4788 {
4789 token_info_push(p, "case", &@$);
4790 push_end_expect_token_locations(p, &@1.beg_pos);
4791 }
4792 ;
4793
4794k_for : keyword_for[kw] allow_exits
4795 {
4796 $$ = $allow_exits;
4797 token_info_push(p, "for", &@$);
4798 push_end_expect_token_locations(p, &@kw.beg_pos);
4799 }
4800 ;
4801
4802k_class : keyword_class
4803 {
4804 token_info_push(p, "class", &@$);
4805 $$ = p->ctxt;
4806 p->ctxt.in_rescue = before_rescue;
4807 push_end_expect_token_locations(p, &@1.beg_pos);
4808 }
4809 ;
4810
4811k_module : keyword_module
4812 {
4813 token_info_push(p, "module", &@$);
4814 $$ = p->ctxt;
4815 p->ctxt.in_rescue = before_rescue;
4816 push_end_expect_token_locations(p, &@1.beg_pos);
4817 }
4818 ;
4819
4820k_def : keyword_def
4821 {
4822 token_info_push(p, "def", &@$);
4823 $$ = NEW_DEF_TEMP(&@$);
4824 p->ctxt.in_argdef = 1;
4825 }
4826 ;
4827
4828k_do : keyword_do
4829 {
4830 token_info_push(p, "do", &@$);
4831 push_end_expect_token_locations(p, &@1.beg_pos);
4832 }
4833 ;
4834
4835k_do_block : keyword_do_block
4836 {
4837 token_info_push(p, "do", &@$);
4838 push_end_expect_token_locations(p, &@1.beg_pos);
4839 }
4840 ;
4841
4842k_rescue : keyword_rescue
4843 {
4844 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4845 $$ = p->ctxt;
4846 p->ctxt.in_rescue = after_rescue;
4847 }
4848 ;
4849
4850k_ensure : keyword_ensure
4851 {
4852 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4853 $$ = p->ctxt;
4854 }
4855 ;
4856
4857k_when : keyword_when
4858 {
4859 token_info_warn(p, "when", p->token_info, 0, &@$);
4860 }
4861 ;
4862
4863k_else : keyword_else
4864 {
4865 token_info *ptinfo_beg = p->token_info;
4866 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4867 token_info_warn(p, "else", p->token_info, same, &@$);
4868 if (same) {
4869 token_info e;
4870 e.next = ptinfo_beg->next;
4871 e.token = "else";
4872 token_info_setup(&e, p->lex.pbeg, &@$);
4873 if (!e.nonspc) *ptinfo_beg = e;
4874 }
4875 }
4876 ;
4877
4878k_elsif : keyword_elsif
4879 {
4880 WARN_EOL("elsif");
4881 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4882 }
4883 ;
4884
4885k_end : keyword_end
4886 {
4887 token_info_pop(p, "end", &@$);
4888 pop_end_expect_token_locations(p);
4889 }
4890 | tDUMNY_END
4891 {
4892 compile_error(p, "syntax error, unexpected end-of-input");
4893 }
4894 ;
4895
4896k_return : keyword_return
4897 {
4898 if (p->ctxt.cant_return && !dyna_in_block(p))
4899 yyerror1(&@1, "Invalid return in class/module body");
4900 }
4901 ;
4902
4903k_return_arg : k_return
4904 {
4905 CMDARG_PUSH(0);
4906 }
4907 ;
4908
4909k_break_arg : keyword_break
4910 {
4911 CMDARG_PUSH(0);
4912 }
4913 ;
4914
4915k_next_arg : keyword_next
4916 {
4917 CMDARG_PUSH(0);
4918 }
4919 ;
4920
4921k_yield : keyword_yield
4922 {
4923 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4924 yyerror1(&@1, "Invalid yield");
4925 }
4926 ;
4927
4928then : term
4929 | keyword_then
4930 | term keyword_then
4931 ;
4932
4933do : term
4934 | keyword_do_cond { $$ = keyword_do_cond; }
4935 ;
4936
4937if_tail : opt_else
4938 | k_elsif expr_value then
4939 compstmt(stmts)
4940 if_tail
4941 {
4942 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4943 fixpos($$, $2);
4944 /*% ripper: elsif!($:2, $:4, $:5) %*/
4945 }
4946 ;
4947
4948opt_else : none
4949 | k_else compstmt(stmts)
4950 {
4951 $$ = $2;
4952 /*% ripper: else!($:2) %*/
4953 }
4954 ;
4955
4956for_var : lhs
4957 | mlhs
4958 ;
4959
4960f_marg : f_norm_arg
4961 {
4962 $$ = assignable(p, $1, 0, &@$);
4963 mark_lvar_used(p, $$);
4964 }
4965 | tLPAREN f_margs rparen
4966 {
4967 $$ = (NODE *)$2;
4968 /*% ripper: mlhs_paren!($:2) %*/
4969 }
4970 ;
4971
4972
4973f_margs : mlhs_items(f_marg)
4974 {
4975 $$ = NEW_MASGN($1, 0, &@$);
4976 /*% ripper: $:1 %*/
4977 }
4978 | mlhs_items(f_marg) ',' f_rest_marg
4979 {
4980 $$ = NEW_MASGN($1, $3, &@$);
4981 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4982 }
4983 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4984 {
4985 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4986 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4987 }
4988 | f_rest_marg
4989 {
4990 $$ = NEW_MASGN(0, $1, &@$);
4991 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4992 }
4993 | f_rest_marg ',' mlhs_items(f_marg)
4994 {
4995 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4996 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4997 }
4998 ;
4999
5000f_rest_marg : tSTAR f_norm_arg
5001 {
5002 /*% ripper: $:2 %*/
5003 $$ = assignable(p, $2, 0, &@$);
5004 mark_lvar_used(p, $$);
5005 }
5006 | tSTAR
5007 {
5008 $$ = NODE_SPECIAL_NO_NAME_REST;
5009 /*% ripper: Qnil %*/
5010 }
5011 ;
5012
5013f_any_kwrest : f_kwrest
5014 | f_no_kwarg
5015 {
5016 $$ = idNil;
5017 /*% ripper: ID2VAL(idNil) %*/
5018 }
5019 ;
5020
5021f_eq : {p->ctxt.in_argdef = 0;} '=';
5022
5023block_args_tail : args_tail_basic(primary_value, none)
5024 ;
5025
5026excessed_comma : ','
5027 {
5028 /* magic number for rest_id in iseq_set_arguments() */
5029 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5030 /*% ripper: excessed_comma! %*/
5031 }
5032 ;
5033
5034block_param : args-list(primary_value, opt_args_tail(block_args_tail, none))
5035 | f_arg[pre] excessed_comma
5036 {
5037 $$ = new_empty_args_tail(p, &@excessed_comma);
5038 $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$);
5039 /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/
5040 }
5041 | f_arg[pre] opt_args_tail(block_args_tail, none)[tail]
5042 {
5043 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5044 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5045 }
5046 | tail-only-args(block_args_tail)
5047 ;
5048
5049opt_block_param_def : none
5050 | block_param_def
5051 {
5052 p->command_start = TRUE;
5053 }
5054 ;
5055
5056block_param_def : '|' opt_block_param opt_bv_decl '|'
5057 {
5058 p->max_numparam = ORDINAL_PARAM;
5059 p->ctxt.in_argdef = 0;
5060 $$ = $2;
5061 /*% ripper: block_var!($:2, $:3) %*/
5062 }
5063 ;
5064
5065opt_block_param : /* none */
5066 {
5067 $$ = 0;
5068 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5069 }
5070 | block_param
5071 ;
5072
5073opt_bv_decl : '\n'?
5074 {
5075 $$ = 0;
5076 /*% ripper: Qfalse %*/
5077 }
5078 | '\n'? ';' bv_decls '\n'?
5079 {
5080 $$ = 0;
5081 /*% ripper: $:3 %*/
5082 }
5083 ;
5084
5085bv_decls : bvar
5086 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5087 | bv_decls ',' bvar
5088 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5089 ;
5090
5091bvar : tIDENTIFIER
5092 {
5093 new_bv(p, $1);
5094 /*% ripper: $:1 %*/
5095 }
5096 | f_bad_arg
5097 ;
5098
5099max_numparam : {
5100 $$ = p->max_numparam;
5101 p->max_numparam = 0;
5102 }
5103 ;
5104
5105numparam : {
5106 $$ = numparam_push(p);
5107 }
5108 ;
5109
5110it_id : {
5111 $$ = p->it_id;
5112 p->it_id = 0;
5113 }
5114 ;
5115
5116lambda : tLAMBDA[lpar]
5117 {
5118 token_info_push(p, "->", &@lpar);
5119 $$ = dyna_push(p);
5120 }[dyna]<vars>
5121 max_numparam numparam it_id allow_exits
5122 f_larglist[args]
5123 {
5124 CMDARG_PUSH(0);
5125 }
5126 lambda_body[body]
5127 {
5128 int max_numparam = p->max_numparam;
5129 ID it_id = p->it_id;
5130 p->lex.lpar_beg = $lpar;
5131 p->max_numparam = $max_numparam;
5132 p->it_id = $it_id;
5133 restore_block_exit(p, $allow_exits);
5134 CMDARG_POP();
5135 $args = args_with_numbered(p, $args, max_numparam, it_id);
5136 {
5137 YYLTYPE loc = code_loc_gen(&@lpar, &@body);
5138 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5139 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5140 nd_set_line($$, @args.end_pos.lineno);
5141 nd_set_first_loc($$, @lpar.beg_pos);
5142 xfree($body);
5143 }
5144 /*% ripper: lambda!($:args, $:body) %*/
5145 numparam_pop(p, $numparam);
5146 dyna_pop(p, $dyna);
5147 }
5148 ;
5149
5150f_larglist : '(' f_largs[args] opt_bv_decl ')'
5151 {
5152 p->ctxt.in_argdef = 0;
5153 $$ = $args;
5154 p->max_numparam = ORDINAL_PARAM;
5155 /*% ripper: paren!($:args) %*/
5156 }
5157 | f_largs[args]
5158 {
5159 p->ctxt.in_argdef = 0;
5160 if (!args_info_empty_p(&$args->nd_ainfo))
5161 p->max_numparam = ORDINAL_PARAM;
5162 $$ = $args;
5163 }
5164 ;
5165
5166lambda_body : tLAMBEG compstmt(stmts) '}'
5167 {
5168 token_info_pop(p, "}", &@3);
5169 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5170 /*% ripper: $:2 %*/
5171 }
5172 | keyword_do_LAMBDA
5173 {
5174 push_end_expect_token_locations(p, &@1.beg_pos);
5175 }
5176 bodystmt k_end
5177 {
5178 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5179 /*% ripper: $:3 %*/
5180 }
5181 ;
5182
5183do_block : k_do_block do_body k_end
5184 {
5185 $$ = $2;
5186 set_embraced_location($$, &@1, &@3);
5187 /*% ripper: $:2 %*/
5188 }
5189 ;
5190
5191block_call : command do_block
5192 {
5193 $$ = command_add_block(p, $1, $2, &@$);
5194 fixpos($$, $1);
5195 /*% ripper: method_add_block!($:1, $:2) %*/
5196 }
5197 | block_call call_op2 operation2 opt_paren_args
5198 {
5199 bool has_args = $4 != 0;
5200 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5201 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5202 /*% ripper: call!($:1, $:2, $:3) %*/
5203 if (has_args) {
5204 /*% ripper: method_add_arg!($:$, $:4) %*/
5205 }
5206 }
5207 | block_call call_op2 operation2 opt_paren_args brace_block
5208 {
5209 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5210 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5211 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5212 }
5213 | block_call call_op2 operation2 command_args do_block
5214 {
5215 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5216 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5217 }
5218 | block_call call_op2 paren_args
5219 {
5220 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5221 nd_set_line($$, @2.end_pos.lineno);
5222 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5223 }
5224 ;
5225
5226method_call : fcall paren_args
5227 {
5228 $1->nd_args = $2;
5229 $$ = (NODE *)$1;
5230 nd_set_last_loc($1, @2.end_pos);
5231 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5232 }
5233 | primary_value call_op operation2 opt_paren_args
5234 {
5235 bool has_args = $4 != 0;
5236 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5237 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5238 nd_set_line($$, @3.end_pos.lineno);
5239 /*% ripper: call!($:1, $:2, $:3) %*/
5240 if (has_args) {
5241 /*% ripper: method_add_arg!($:$, $:4) %*/
5242 }
5243 }
5244 | primary_value tCOLON2 operation2 paren_args
5245 {
5246 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5247 nd_set_line($$, @3.end_pos.lineno);
5248 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5249 }
5250 | primary_value tCOLON2 operation3
5251 {
5252 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5253 /*% ripper: call!($:1, $:2, $:3) %*/
5254 }
5255 | primary_value call_op2 paren_args
5256 {
5257 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5258 nd_set_line($$, @2.end_pos.lineno);
5259 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5260 }
5261 | keyword_super paren_args
5262 {
5263 rb_code_location_t lparen_loc = @2;
5264 rb_code_location_t rparen_loc = @2;
5265 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5266 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5267
5268 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5269 /*% ripper: super!($:2) %*/
5270 }
5271 | keyword_super
5272 {
5273 $$ = NEW_ZSUPER(&@$);
5274 /*% ripper: zsuper! %*/
5275 }
5276 | primary_value '[' opt_call_args rbracket
5277 {
5278 $$ = NEW_CALL($1, tAREF, $3, &@$);
5279 fixpos($$, $1);
5280 /*% ripper: aref!($:1, $:3) %*/
5281 }
5282 ;
5283
5284brace_block : '{' brace_body '}'
5285 {
5286 $$ = $2;
5287 set_embraced_location($$, &@1, &@3);
5288 /*% ripper: $:2 %*/
5289 }
5290 | k_do do_body k_end
5291 {
5292 $$ = $2;
5293 set_embraced_location($$, &@1, &@3);
5294 /*% ripper: $:2 %*/
5295 }
5296 ;
5297
5298brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5299 max_numparam numparam it_id allow_exits
5300 opt_block_param_def[args] compstmt(stmts)
5301 {
5302 int max_numparam = p->max_numparam;
5303 ID it_id = p->it_id;
5304 p->max_numparam = $max_numparam;
5305 p->it_id = $it_id;
5306 $args = args_with_numbered(p, $args, max_numparam, it_id);
5307 $$ = NEW_ITER($args, $compstmt, &@$);
5308 /*% ripper: brace_block!($:args, $:compstmt) %*/
5309 restore_block_exit(p, $allow_exits);
5310 numparam_pop(p, $numparam);
5311 dyna_pop(p, $dyna);
5312 }
5313 ;
5314
5315do_body : {
5316 $$ = dyna_push(p);
5317 CMDARG_PUSH(0);
5318 }[dyna]<vars>
5319 max_numparam numparam it_id allow_exits
5320 opt_block_param_def[args] bodystmt
5321 {
5322 int max_numparam = p->max_numparam;
5323 ID it_id = p->it_id;
5324 p->max_numparam = $max_numparam;
5325 p->it_id = $it_id;
5326 $args = args_with_numbered(p, $args, max_numparam, it_id);
5327 $$ = NEW_ITER($args, $bodystmt, &@$);
5328 /*% ripper: do_block!($:args, $:bodystmt) %*/
5329 CMDARG_POP();
5330 restore_block_exit(p, $allow_exits);
5331 numparam_pop(p, $numparam);
5332 dyna_pop(p, $dyna);
5333 }
5334 ;
5335
5336case_args : arg_value
5337 {
5338 check_literal_when(p, $arg_value, &@arg_value);
5339 $$ = NEW_LIST($arg_value, &@$);
5340 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5341 }
5342 | tSTAR arg_value
5343 {
5344 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5345 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5346 }
5347 | case_args[non_last_args] ',' arg_value
5348 {
5349 check_literal_when(p, $arg_value, &@arg_value);
5350 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5351 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5352 }
5353 | case_args[non_last_args] ',' tSTAR arg_value
5354 {
5355 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5356 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5357 }
5358 ;
5359
5360case_body : k_when case_args then
5361 compstmt(stmts)
5362 cases
5363 {
5364 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5365 fixpos($$, $2);
5366 /*% ripper: when!($:2, $:4, $:5) %*/
5367 }
5368 ;
5369
5370cases : opt_else
5371 | case_body
5372 ;
5373
5374p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5375p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5376
5377p_in_kwarg : {
5378 $$ = p->ctxt;
5379 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5380 p->command_start = FALSE;
5381 p->ctxt.in_kwarg = 1;
5382 p->ctxt.in_alt_pattern = 0;
5383 p->ctxt.capture_in_pattern = 0;
5384 }
5385 ;
5386
5387p_case_body : keyword_in
5388 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5389 p_top_expr[expr] then
5390 {
5391 pop_pktbl(p, $p_pktbl);
5392 pop_pvtbl(p, $p_pvtbl);
5393 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5394 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5395 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5396 }
5397 compstmt(stmts)
5398 p_cases[cases]
5399 {
5400 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5401 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5402 }
5403 ;
5404
5405p_cases : opt_else
5406 | p_case_body
5407 ;
5408
5409p_top_expr : p_top_expr_body
5410 | p_top_expr_body modifier_if expr_value
5411 {
5412 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5413 fixpos($$, $3);
5414 /*% ripper: if_mod!($:3, $:1) %*/
5415 }
5416 | p_top_expr_body modifier_unless expr_value
5417 {
5418 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5419 fixpos($$, $3);
5420 /*% ripper: unless_mod!($:3, $:1) %*/
5421 }
5422 ;
5423
5424p_top_expr_body : p_expr
5425 | p_expr ','
5426 {
5427 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5428 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5429 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5430 }
5431 | p_expr ',' p_args
5432 {
5433 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5434 nd_set_first_loc($$, @1.beg_pos);
5435 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5436 }
5437 | p_find
5438 {
5439 $$ = new_find_pattern(p, 0, $1, &@$);
5440 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5441 }
5442 | p_args_tail
5443 {
5444 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5445 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5446 }
5447 | p_kwargs
5448 {
5449 $$ = new_hash_pattern(p, 0, $1, &@$);
5450 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5451 }
5452 ;
5453
5454p_expr : p_as
5455 ;
5456
5457p_as : p_expr tASSOC p_variable
5458 {
5459 NODE *n = NEW_LIST($1, &@$);
5460 n = list_append(p, n, $3);
5461 $$ = new_hash(p, n, &@$);
5462 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5463 }
5464 | p_alt
5465 ;
5466
5467p_alt : p_alt[left] '|'[alt]
5468 {
5469 p->ctxt.in_alt_pattern = 1;
5470 }
5471 p_expr_basic[right]
5472 {
5473 if (p->ctxt.capture_in_pattern) {
5474 yyerror1(&@alt, "alternative pattern after variable capture");
5475 }
5476 p->ctxt.in_alt_pattern = 0;
5477 $$ = NEW_OR($left, $right, &@$, &@alt);
5478 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5479 }
5480 | p_expr_basic
5481 ;
5482
5483p_lparen : '(' p_pktbl
5484 {
5485 $$ = $2;
5486 /*% ripper: $:2 %*/
5487 }
5488 ;
5489
5490p_lbracket : '[' p_pktbl
5491 {
5492 $$ = $2;
5493 /*% ripper: $:2 %*/
5494 }
5495 ;
5496
5497p_expr_basic : p_value
5498 | p_variable
5499 | p_const p_lparen[p_pktbl] p_args rparen
5500 {
5501 pop_pktbl(p, $p_pktbl);
5502 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5503 nd_set_first_loc($$, @p_const.beg_pos);
5504 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5505 }
5506 | p_const p_lparen[p_pktbl] p_find rparen
5507 {
5508 pop_pktbl(p, $p_pktbl);
5509 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5510 nd_set_first_loc($$, @p_const.beg_pos);
5511 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5512 }
5513 | p_const p_lparen[p_pktbl] p_kwargs rparen
5514 {
5515 pop_pktbl(p, $p_pktbl);
5516 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5517 nd_set_first_loc($$, @p_const.beg_pos);
5518 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5519 }
5520 | p_const '(' rparen
5521 {
5522 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5523 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5524 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5525 }
5526 | p_const p_lbracket[p_pktbl] p_args rbracket
5527 {
5528 pop_pktbl(p, $p_pktbl);
5529 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5530 nd_set_first_loc($$, @p_const.beg_pos);
5531 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5532 }
5533 | p_const p_lbracket[p_pktbl] p_find rbracket
5534 {
5535 pop_pktbl(p, $p_pktbl);
5536 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5537 nd_set_first_loc($$, @p_const.beg_pos);
5538 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5539 }
5540 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5541 {
5542 pop_pktbl(p, $p_pktbl);
5543 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5544 nd_set_first_loc($$, @p_const.beg_pos);
5545 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5546 }
5547 | p_const '[' rbracket
5548 {
5549 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5550 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5551 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5552 }
5553 | tLBRACK p_args rbracket
5554 {
5555 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5556 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5557 }
5558 | tLBRACK p_find rbracket
5559 {
5560 $$ = new_find_pattern(p, 0, $p_find, &@$);
5561 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5562 }
5563 | tLBRACK rbracket
5564 {
5565 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5566 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5567 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5568 }
5569 | tLBRACE p_pktbl lex_ctxt[ctxt]
5570 {
5571 p->ctxt.in_kwarg = 0;
5572 }
5573 p_kwargs rbrace
5574 {
5575 pop_pktbl(p, $p_pktbl);
5576 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5577 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5578 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5579 }
5580 | tLBRACE rbrace
5581 {
5582 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5583 $$ = new_hash_pattern(p, 0, $$, &@$);
5584 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5585 }
5586 | tLPAREN p_pktbl p_expr rparen
5587 {
5588 pop_pktbl(p, $p_pktbl);
5589 $$ = $p_expr;
5590 /*% ripper: $:p_expr %*/
5591 }
5592 ;
5593
5594p_args : p_expr
5595 {
5596 NODE *pre_args = NEW_LIST($1, &@$);
5597 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5598 /*% ripper: [[$:1], Qnil, Qnil] %*/
5599 }
5600 | p_args_head
5601 {
5602 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5603 /*% ripper: [$:1, Qnil, Qnil] %*/
5604 }
5605 | p_args_head p_arg
5606 {
5607 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5608 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5609 }
5610 | p_args_head p_rest
5611 {
5612 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5613 /*% ripper: [$:1, $:2, Qnil] %*/
5614 }
5615 | p_args_head p_rest ',' p_args_post
5616 {
5617 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5618 /*% ripper: [$:1, $:2, $:4] %*/
5619 }
5620 | p_args_tail
5621 ;
5622
5623p_args_head : p_arg ','
5624 | p_args_head p_arg ','
5625 {
5626 $$ = list_concat($1, $2);
5627 /*% ripper: rb_ary_concat($:1, $:2) %*/
5628 }
5629 ;
5630
5631p_args_tail : p_rest
5632 {
5633 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5634 /*% ripper: [Qnil, $:1, Qnil] %*/
5635 }
5636 | p_rest ',' p_args_post
5637 {
5638 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5639 /*% ripper: [Qnil, $:1, $:3] %*/
5640 }
5641 ;
5642
5643p_find : p_rest ',' p_args_post ',' p_rest
5644 {
5645 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5646 /*% ripper: [$:1, $:3, $:5] %*/
5647 }
5648 ;
5649
5650
5651p_rest : tSTAR tIDENTIFIER
5652 {
5653 error_duplicate_pattern_variable(p, $2, &@2);
5654 /*% ripper: var_field!($:2) %*/
5655 $$ = assignable(p, $2, 0, &@$);
5656 }
5657 | tSTAR
5658 {
5659 $$ = 0;
5660 /*% ripper: var_field!(Qnil) %*/
5661 }
5662 ;
5663
5664p_args_post : p_arg
5665 | p_args_post ',' p_arg
5666 {
5667 $$ = list_concat($1, $3);
5668 /*% ripper: rb_ary_concat($:1, $:3) %*/
5669 }
5670 ;
5671
5672p_arg : p_expr
5673 {
5674 $$ = NEW_LIST($1, &@$);
5675 /*% ripper: [$:1] %*/
5676 }
5677 ;
5678
5679p_kwargs : p_kwarg ',' p_any_kwrest
5680 {
5681 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5682 /*% ripper: [$:1, $:3] %*/
5683 }
5684 | p_kwarg
5685 {
5686 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5687 /*% ripper: [$:1, Qnil] %*/
5688 }
5689 | p_kwarg ','
5690 {
5691 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5692 /*% ripper: [$:1, Qnil] %*/
5693 }
5694 | p_any_kwrest
5695 {
5696 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5697 /*% ripper: [[], $:1] %*/
5698 }
5699 ;
5700
5701p_kwarg : p_kw
5702 /*% ripper[brace]: [$:1] %*/
5703 | p_kwarg ',' p_kw
5704 {
5705 $$ = list_concat($1, $3);
5706 /*% ripper: rb_ary_push($:1, $:3) %*/
5707 }
5708 ;
5709
5710p_kw : p_kw_label p_expr
5711 {
5712 error_duplicate_pattern_key(p, $1, &@1);
5713 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5714 /*% ripper: [$:1, $:2] %*/
5715 }
5716 | p_kw_label
5717 {
5718 error_duplicate_pattern_key(p, $1, &@1);
5719 if ($1 && !is_local_id($1)) {
5720 yyerror1(&@1, "key must be valid as local variables");
5721 }
5722 error_duplicate_pattern_variable(p, $1, &@1);
5723 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5724 /*% ripper: [$:1, Qnil] %*/
5725 }
5726 ;
5727
5728p_kw_label : tLABEL
5729 | tSTRING_BEG string_contents tLABEL_END
5730 {
5731 YYLTYPE loc = code_loc_gen(&@1, &@3);
5732 if (!$2 || nd_type_p($2, NODE_STR)) {
5733 NODE *node = dsym_node(p, $2, &loc);
5734 $$ = rb_sym2id(rb_node_sym_string_val(node));
5735 }
5736 else {
5737 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5738 $$ = rb_intern_str(STR_NEW0());
5739 }
5740 /*% ripper: $:2 %*/
5741 }
5742 ;
5743
5744p_kwrest : kwrest_mark tIDENTIFIER
5745 {
5746 $$ = $2;
5747 /*% ripper: var_field!($:2) %*/
5748 }
5749 | kwrest_mark
5750 {
5751 $$ = 0;
5752 /*% ripper: Qnil %*/
5753 }
5754 ;
5755
5756p_kwnorest : kwrest_mark keyword_nil
5757 {
5758 $$ = 0;
5759 }
5760 ;
5761
5762p_any_kwrest : p_kwrest
5763 | p_kwnorest
5764 {
5765 $$ = idNil;
5766 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5767 }
5768 ;
5769
5770p_value : p_primitive
5771 | range_expr(p_primitive)
5772 | p_var_ref
5773 | p_expr_ref
5774 | p_const
5775 ;
5776
5777p_primitive : inline_primary
5778 | keyword_variable
5779 {
5780 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5781 /*% ripper: var_ref!($:1) %*/
5782 }
5783 | lambda
5784 ;
5785
5786p_variable : tIDENTIFIER
5787 {
5788 error_duplicate_pattern_variable(p, $1, &@1);
5789 /*% ripper: var_field!($:1) %*/
5790 $$ = assignable(p, $1, 0, &@$);
5791 }
5792 ;
5793
5794p_var_ref : '^' tIDENTIFIER
5795 {
5796 NODE *n = gettable(p, $2, &@$);
5797 if (!n) {
5798 n = NEW_ERROR(&@$);
5799 }
5800 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5801 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5802 }
5803 $$ = n;
5804 /*% ripper: var_ref!($:2) %*/
5805 }
5806 | '^' nonlocal_var
5807 {
5808 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5809 /*% ripper: var_ref!($:2) %*/
5810 }
5811 ;
5812
5813p_expr_ref : '^' tLPAREN expr_value rparen
5814 {
5815 $$ = NEW_BLOCK($3, &@$);
5816 /*% ripper: begin!($:3) %*/
5817 }
5818 ;
5819
5820p_const : tCOLON3 cname
5821 {
5822 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5823 /*% ripper: top_const_ref!($:2) %*/
5824 }
5825 | p_const tCOLON2 cname
5826 {
5827 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5828 /*% ripper: const_path_ref!($:1, $:3) %*/
5829 }
5830 | tCONSTANT
5831 {
5832 $$ = gettable(p, $1, &@$);
5833 /*% ripper: var_ref!($:1) %*/
5834 }
5835 ;
5836
5837opt_rescue : k_rescue exc_list exc_var then
5838 compstmt(stmts)
5839 opt_rescue
5840 {
5841 NODE *err = $3;
5842 if ($3) {
5843 err = NEW_ERRINFO(&@3);
5844 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5845 }
5846 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5847 if ($2) {
5848 fixpos($$, $2);
5849 }
5850 else if ($3) {
5851 fixpos($$, $3);
5852 }
5853 else {
5854 fixpos($$, $5);
5855 }
5856 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5857 }
5858 | none
5859 ;
5860
5861exc_list : arg_value
5862 {
5863 $$ = NEW_LIST($1, &@$);
5864 /*% ripper: rb_ary_new3(1, $:1) %*/
5865 }
5866 | mrhs
5867 {
5868 if (!($$ = splat_array($1))) $$ = $1;
5869 }
5870 | none
5871 ;
5872
5873exc_var : tASSOC lhs
5874 {
5875 $$ = $2;
5876 /*% ripper: $:2 %*/
5877 }
5878 | none
5879 ;
5880
5881opt_ensure : k_ensure stmts terms?
5882 {
5883 p->ctxt.in_rescue = $1.in_rescue;
5884 $$ = $2;
5885 void_expr(p, void_stmts(p, $$));
5886 /*% ripper: ensure!($:2) %*/
5887 }
5888 | none
5889 ;
5890
5891literal : numeric
5892 | symbol
5893 ;
5894
5895strings : string
5896 {
5897 if (!$1) {
5898 $$ = NEW_STR(STRING_NEW0(), &@$);
5899 }
5900 else {
5901 $$ = evstr2dstr(p, $1);
5902 }
5903 /*% ripper: $:1 %*/
5904 }
5905 ;
5906
5907string : tCHAR
5908 | string1
5909 | string string1
5910 {
5911 $$ = literal_concat(p, $1, $2, &@$);
5912 /*% ripper: string_concat!($:1, $:2) %*/
5913 }
5914 ;
5915
5916string1 : tSTRING_BEG string_contents tSTRING_END
5917 {
5918 $$ = heredoc_dedent(p, $2);
5919 if ($$) nd_set_loc($$, &@$);
5920 /*% ripper: $:2 %*/
5921 if (p->heredoc_indent > 0) {
5922 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5923 p->heredoc_indent = 0;
5924 }
5925 /*% ripper: string_literal!($:$) %*/
5926 }
5927 ;
5928
5929xstring : tXSTRING_BEG xstring_contents tSTRING_END
5930 {
5931 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5932 /*% ripper: $:2 %*/
5933 if (p->heredoc_indent > 0) {
5934 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5935 p->heredoc_indent = 0;
5936 }
5937 /*% ripper: xstring_literal!($:$) %*/
5938 }
5939 ;
5940
5941regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5942 {
5943 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5944 /*% ripper: regexp_literal!($:2, $:3) %*/
5945 }
5946 ;
5947
5948words : words(tWORDS_BEG, word_list)
5949 ;
5950
5951word_list : /* none */
5952 {
5953 $$ = 0;
5954 /*% ripper: words_new! %*/
5955 }
5956 | word_list word ' '+
5957 {
5958 $$ = list_append(p, $1, evstr2dstr(p, $2));
5959 /*% ripper: words_add!($:1, $:2) %*/
5960 }
5961 ;
5962
5963word : string_content
5964 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5965 | word string_content
5966 {
5967 $$ = literal_concat(p, $1, $2, &@$);
5968 /*% ripper: word_add!($:1, $:2) %*/
5969 }
5970 ;
5971
5972symbols : words(tSYMBOLS_BEG, symbol_list)
5973 ;
5974
5975symbol_list : /* none */
5976 {
5977 $$ = 0;
5978 /*% ripper: symbols_new! %*/
5979 }
5980 | symbol_list word ' '+
5981 {
5982 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5983 /*% ripper: symbols_add!($:1, $:2) %*/
5984 }
5985 ;
5986
5987qwords : words(tQWORDS_BEG, qword_list)
5988 ;
5989
5990qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5991 ;
5992
5993qword_list : /* none */
5994 {
5995 $$ = 0;
5996 /*% ripper: qwords_new! %*/
5997 }
5998 | qword_list tSTRING_CONTENT ' '+
5999 {
6000 $$ = list_append(p, $1, $2);
6001 /*% ripper: qwords_add!($:1, $:2) %*/
6002 }
6003 ;
6004
6005qsym_list : /* none */
6006 {
6007 $$ = 0;
6008 /*% ripper: qsymbols_new! %*/
6009 }
6010 | qsym_list tSTRING_CONTENT ' '+
6011 {
6012 $$ = symbol_append(p, $1, $2);
6013 /*% ripper: qsymbols_add!($:1, $:2) %*/
6014 }
6015 ;
6016
6017string_contents : /* none */
6018 {
6019 $$ = 0;
6020 /*% ripper: string_content! %*/
6021 }
6022 | string_contents string_content
6023 {
6024 $$ = literal_concat(p, $1, $2, &@$);
6025 /*% ripper: string_add!($:1, $:2) %*/
6026 }
6027 ;
6028
6029xstring_contents: /* none */
6030 {
6031 $$ = 0;
6032 /*% ripper: xstring_new! %*/
6033 }
6034 | xstring_contents string_content
6035 {
6036 $$ = literal_concat(p, $1, $2, &@$);
6037 /*% ripper: xstring_add!($:1, $:2) %*/
6038 }
6039 ;
6040
6041regexp_contents : /* none */
6042 {
6043 $$ = 0;
6044 /*% ripper: regexp_new! %*/
6045 }
6046 | regexp_contents string_content
6047 {
6048 NODE *head = $1, *tail = $2;
6049 if (!head) {
6050 $$ = tail;
6051 }
6052 else if (!tail) {
6053 $$ = head;
6054 }
6055 else {
6056 switch (nd_type(head)) {
6057 case NODE_STR:
6058 head = str2dstr(p, head);
6059 break;
6060 case NODE_DSTR:
6061 break;
6062 default:
6063 head = list_append(p, NEW_DSTR(0, &@$), head);
6064 break;
6065 }
6066 $$ = list_append(p, head, tail);
6067 }
6068 /*% ripper: regexp_add!($:1, $:2) %*/
6069 }
6070 ;
6071
6072string_content : tSTRING_CONTENT[content]
6073 /*% ripper[brace]: $:content %*/
6074 | tSTRING_DVAR[state]
6075 {
6076 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6077 $$ = p->lex.strterm;
6078 p->lex.strterm = 0;
6079 SET_LEX_STATE(EXPR_BEG);
6080 }[strterm]<strterm>
6081 string_dvar[dvar]
6082 {
6083 p->lex.strterm = $strterm;
6084 $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC);
6085 nd_set_line($$, @dvar.end_pos.lineno);
6086 /*% ripper: string_dvar!($:dvar) %*/
6087 }
6088 | tSTRING_DBEG[state]
6089 {
6090 CMDARG_PUSH(0);
6091 COND_PUSH(0);
6092 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6093 $$ = p->lex.strterm;
6094 p->lex.strterm = 0;
6095 SET_LEX_STATE(EXPR_BEG);
6096 }[term]<strterm>
6097 {
6098 $$ = p->lex.brace_nest;
6099 p->lex.brace_nest = 0;
6100 }[brace]<num>
6101 {
6102 $$ = p->lex.lpar_beg;
6103 p->lex.lpar_beg = -1;
6104 }[lpar]<num>
6105 {
6106 $$ = p->heredoc_indent;
6107 p->heredoc_indent = 0;
6108 }[indent]<num>
6109 compstmt(stmts) string_dend
6110 {
6111 COND_POP();
6112 CMDARG_POP();
6113 p->lex.strterm = $term;
6114 SET_LEX_STATE($state);
6115 p->lex.brace_nest = $brace;
6116 p->lex.lpar_beg = $lpar;
6117 p->heredoc_indent = $indent;
6118 p->heredoc_line_indent = -1;
6119 if ($compstmt) nd_unset_fl_newline($compstmt);
6120 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6121 /*% ripper: string_embexpr!($:compstmt) %*/
6122 }
6123 ;
6124
6125string_dend : tSTRING_DEND
6126 | END_OF_INPUT
6127 ;
6128
6129string_dvar : nonlocal_var
6130 {
6131 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6132 /*% ripper: var_ref!($:1) %*/
6133 }
6134 | backref
6135 ;
6136
6137symbol : ssym
6138 | dsym
6139 ;
6140
6141ssym : tSYMBEG sym
6142 {
6143 SET_LEX_STATE(EXPR_END);
6144 VALUE str = rb_id2str($2);
6145 /*
6146 * TODO:
6147 * set_yylval_noname sets invalid id to yylval.
6148 * This branch can be removed once yylval is changed to
6149 * hold lexed string.
6150 */
6151 if (!str) str = STR_NEW0();
6152 $$ = NEW_SYM(str, &@$);
6153 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6154 }
6155 ;
6156
6157sym : fname
6158 | nonlocal_var
6159 ;
6160
6161dsym : tSYMBEG string_contents tSTRING_END
6162 {
6163 SET_LEX_STATE(EXPR_END);
6164 $$ = dsym_node(p, $2, &@$);
6165 /*% ripper: dyna_symbol!($:2) %*/
6166 }
6167 ;
6168
6169numeric : simple_numeric
6170 | tUMINUS_NUM simple_numeric %prec tLOWEST
6171 {
6172 $$ = $2;
6173 negate_lit(p, $$, &@$);
6174 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6175 }
6176 ;
6177
6178simple_numeric : tINTEGER
6179 | tFLOAT
6180 | tRATIONAL
6181 | tIMAGINARY
6182 ;
6183
6184nonlocal_var : tIVAR
6185 | tGVAR
6186 | tCVAR
6187 ;
6188
6189user_variable : ident_or_const
6190 | nonlocal_var
6191 ;
6192
6193keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6194 | keyword_self {$$ = KWD2EID(self, $1);}
6195 | keyword_true {$$ = KWD2EID(true, $1);}
6196 | keyword_false {$$ = KWD2EID(false, $1);}
6197 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6198 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6199 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6200 ;
6201
6202var_ref : user_variable
6203 {
6204 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6205 if (ifdef_ripper(id_is_var(p, $1), false)) {
6206 /*% ripper: var_ref!($:1) %*/
6207 }
6208 else {
6209 /*% ripper: vcall!($:1) %*/
6210 }
6211 }
6212 | keyword_variable
6213 {
6214 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6215 /*% ripper: var_ref!($:1) %*/
6216 }
6217 ;
6218
6219var_lhs : user_or_keyword_variable
6220 {
6221 /*% ripper: var_field!($:1) %*/
6222 $$ = assignable(p, $1, 0, &@$);
6223 }
6224 ;
6225
6226backref : tNTH_REF
6227 | tBACK_REF
6228 ;
6229
6230superclass : '<'
6231 {
6232 SET_LEX_STATE(EXPR_BEG);
6233 p->command_start = TRUE;
6234 }
6235 expr_value term
6236 {
6237 $$ = $3;
6238 /*% ripper: $:3 %*/
6239 }
6240 | none
6241 ;
6242
6243f_opt_paren_args: f_paren_args
6244 | f_empty_arg
6245 {
6246 p->ctxt.in_argdef = 0;
6247 }
6248 ;
6249
6250f_empty_arg : /* none */
6251 {
6252 $$ = new_empty_args_tail(p, &@$);
6253 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6254 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6255 }
6256 ;
6257
6258f_paren_args : '(' f_args rparen
6259 {
6260 $$ = $2;
6261 /*% ripper: paren!($:2) %*/
6262 SET_LEX_STATE(EXPR_BEG);
6263 p->command_start = TRUE;
6264 p->ctxt.in_argdef = 0;
6265 }
6266 ;
6267
6268f_arglist : f_paren_args
6269 | {
6270 $$ = p->ctxt;
6271 p->ctxt.in_kwarg = 1;
6272 p->ctxt.in_argdef = 1;
6273 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6274 }<ctxt>
6275 f_args term
6276 {
6277 p->ctxt.in_kwarg = $1.in_kwarg;
6278 p->ctxt.in_argdef = 0;
6279 $$ = $2;
6280 SET_LEX_STATE(EXPR_BEG);
6281 p->command_start = TRUE;
6282 /*% ripper: $:2 %*/
6283 }
6284 ;
6285
6286args_tail : args_tail_basic(arg_value, opt_comma)
6287 | args_forward
6288 {
6289 add_forwarding_args(p);
6290 $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward);
6291 $$->nd_ainfo.forwarding = 1;
6292 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6293 }
6294 ;
6295
6296largs_tail : args_tail_basic(arg_value, none)
6297 | args_forward
6298 {
6299 yyerror1(&@args_forward, "unexpected ... in lambda argument");
6300 $$ = new_args_tail(p, 0, 0, 0, &@args_forward);
6301 $$->nd_ainfo.forwarding = 1;
6302 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6303 }
6304 ;
6305
6306%rule args-list(value, tail) <node_args>
6307 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6308 {
6309 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6310 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6311 }
6312 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6313 {
6314 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6315 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6316 }
6317 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6318 {
6319 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6320 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6321 }
6322 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6323 {
6324 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6325 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6326 }
6327 | f_arg[pre] ',' f_rest_arg[rest] tail
6328 {
6329 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6330 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6331 }
6332 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6333 {
6334 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6335 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6336 }
6337 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6338 {
6339 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6340 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6341 }
6342 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6343 {
6344 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6345 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6346 }
6347 | f_opt_arg(value)[opt] tail
6348 {
6349 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6350 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6351 }
6352 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6353 {
6354 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6355 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6356 }
6357 | f_rest_arg[rest] tail
6358 {
6359 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6360 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6361 }
6362 | f_rest_arg[rest] ',' f_arg[post] tail
6363 {
6364 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6365 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6366 }
6367 ;
6368
6369%rule tail-only-args(tail) <node_args>
6370 : tail
6371 {
6372 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6373 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6374 }
6375 ;
6376
6377%rule f_args-list(tail, trailing) <node_args>
6378 : args-list(arg_value, opt_args_tail(tail, trailing))
6379 | f_arg[pre] opt_args_tail(tail, trailing)[tail]
6380 {
6381 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6382 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6383 }
6384 | tail-only-args(tail)
6385 | f_empty_arg
6386 ;
6387
6388f_args : f_args-list(args_tail, opt_comma)
6389 ;
6390
6391f_largs : f_args-list(largs_tail, none)
6392 ;
6393
6394args_forward : tBDOT3
6395 {
6396 $$ = idFWD_KWREST;
6397 /*% ripper: args_forward! %*/
6398 }
6399 ;
6400
6401f_bad_arg : tCONSTANT
6402 {
6403 static const char mesg[] = "formal argument cannot be a constant";
6404 /*%%%*/
6405 yyerror1(&@1, mesg);
6406 /*% %*/
6407 $$ = 0;
6408 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6409 }
6410 | tIVAR
6411 {
6412 static const char mesg[] = "formal argument cannot be an instance variable";
6413 /*%%%*/
6414 yyerror1(&@1, mesg);
6415 /*% %*/
6416 $$ = 0;
6417 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6418 }
6419 | tGVAR
6420 {
6421 static const char mesg[] = "formal argument cannot be a global variable";
6422 /*%%%*/
6423 yyerror1(&@1, mesg);
6424 /*% %*/
6425 $$ = 0;
6426 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6427 }
6428 | tCVAR
6429 {
6430 static const char mesg[] = "formal argument cannot be a class variable";
6431 /*%%%*/
6432 yyerror1(&@1, mesg);
6433 /*% %*/
6434 $$ = 0;
6435 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6436 }
6437 ;
6438
6439f_norm_arg : f_bad_arg
6440 | tIDENTIFIER
6441 {
6442 VALUE e = formal_argument_error(p, $$ = $1);
6443 if (e) {
6444 /*% ripper[error]: param_error!(?e, $:1) %*/
6445 }
6446 p->max_numparam = ORDINAL_PARAM;
6447 }
6448 ;
6449
6450f_arg_asgn : f_norm_arg
6451 {
6452 arg_var(p, $1);
6453 $$ = $1;
6454 }
6455 ;
6456
6457f_arg_item : f_arg_asgn
6458 {
6459 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6460 /*% ripper: $:1 %*/
6461 }
6462 | tLPAREN f_margs rparen
6463 {
6464 ID tid = internal_id(p);
6465 YYLTYPE loc;
6466 loc.beg_pos = @2.beg_pos;
6467 loc.end_pos = @2.beg_pos;
6468 arg_var(p, tid);
6469 if (dyna_in_block(p)) {
6470 $2->nd_value = NEW_DVAR(tid, &loc);
6471 }
6472 else {
6473 $2->nd_value = NEW_LVAR(tid, &loc);
6474 }
6475 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6476 $$->nd_next = (NODE *)$2;
6477 /*% ripper: mlhs_paren!($:2) %*/
6478 }
6479 ;
6480
6481f_arg : f_arg_item
6482 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6483 | f_arg ',' f_arg_item
6484 {
6485 $$ = $1;
6486 $$->nd_plen++;
6487 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6488 rb_discard_node(p, (NODE *)$3);
6489 /*% ripper: rb_ary_push($:1, $:3) %*/
6490 }
6491 ;
6492
6493
6494f_label : tLABEL
6495 {
6496 VALUE e = formal_argument_error(p, $$ = $1);
6497 if (e) {
6498 $$ = 0;
6499 /*% ripper[error]: param_error!(?e, $:1) %*/
6500 }
6501 /*
6502 * Workaround for Prism::ParseTest#test_filepath for
6503 * "unparser/corpus/literal/def.txt"
6504 *
6505 * See the discussion on https://github.com/ruby/ruby/pull/9923
6506 */
6507 arg_var(p, ifdef_ripper(0, $1));
6508 /*% ripper: $:1 %*/
6509 p->max_numparam = ORDINAL_PARAM;
6510 p->ctxt.in_argdef = 0;
6511 }
6512 ;
6513
6514kwrest_mark : tPOW
6515 | tDSTAR
6516 ;
6517
6518f_no_kwarg : p_kwnorest
6519 {
6520 /*% ripper: nokw_param!(Qnil) %*/
6521 }
6522 ;
6523
6524f_kwrest : kwrest_mark tIDENTIFIER
6525 {
6526 arg_var(p, shadowing_lvar(p, $2));
6527 $$ = $2;
6528 /*% ripper: kwrest_param!($:2) %*/
6529 }
6530 | kwrest_mark
6531 {
6532 arg_var(p, idFWD_KWREST);
6533 $$ = idFWD_KWREST;
6534 /*% ripper: kwrest_param!(Qnil) %*/
6535 }
6536 ;
6537
6538restarg_mark : '*'
6539 | tSTAR
6540 ;
6541
6542f_rest_arg : restarg_mark tIDENTIFIER
6543 {
6544 arg_var(p, shadowing_lvar(p, $2));
6545 $$ = $2;
6546 /*% ripper: rest_param!($:2) %*/
6547 }
6548 | restarg_mark
6549 {
6550 arg_var(p, idFWD_REST);
6551 $$ = idFWD_REST;
6552 /*% ripper: rest_param!(Qnil) %*/
6553 }
6554 ;
6555
6556blkarg_mark : '&'
6557 | tAMPER
6558 ;
6559
6560f_block_arg : blkarg_mark tIDENTIFIER
6561 {
6562 arg_var(p, shadowing_lvar(p, $2));
6563 $$ = $2;
6564 /*% ripper: blockarg!($:2) %*/
6565 }
6566 | blkarg_mark keyword_nil
6567 {
6568 $$ = idNil;
6569 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6570 }
6571 | blkarg_mark
6572 {
6573 arg_var(p, idFWD_BLOCK);
6574 $$ = idFWD_BLOCK;
6575 /*% ripper: blockarg!(Qnil) %*/
6576 }
6577 ;
6578
6579opt_comma : ','?
6580 {
6581 $$ = 0;
6582 /*% ripper: Qnil %*/
6583 }
6584 ;
6585
6586
6587singleton : value_expr(singleton_expr)
6588 {
6589 NODE *expr = last_expr_node($1);
6590 switch (nd_type(expr)) {
6591 case NODE_STR:
6592 case NODE_DSTR:
6593 case NODE_XSTR:
6594 case NODE_DXSTR:
6595 case NODE_REGX:
6596 case NODE_DREGX:
6597 case NODE_SYM:
6598 case NODE_LINE:
6599 case NODE_FILE:
6600 case NODE_ENCODING:
6601 case NODE_INTEGER:
6602 case NODE_FLOAT:
6603 case NODE_RATIONAL:
6604 case NODE_IMAGINARY:
6605 case NODE_DSYM:
6606 case NODE_LIST:
6607 case NODE_ZLIST:
6608 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6609 break;
6610 default:
6611 break;
6612 }
6613 $$ = $1;
6614 }
6615 ;
6616
6617singleton_expr : var_ref
6618 | '('
6619 {
6620 SET_LEX_STATE(EXPR_BEG);
6621 p->ctxt.in_argdef = 0;
6622 }
6623 expr rparen
6624 {
6625 p->ctxt.in_argdef = 1;
6626 $$ = $3;
6627 /*% ripper: paren!($:3) %*/
6628 }
6629 ;
6630
6631assoc_list : none
6632 | assocs trailer
6633 {
6634 $$ = $1;
6635 /*% ripper: assoclist_from_args!($:1) %*/
6636 }
6637 ;
6638
6639assocs : assoc
6640 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6641 | assocs ',' assoc
6642 {
6643 NODE *assocs = $1;
6644 NODE *tail = $3;
6645 if (!assocs) {
6646 assocs = tail;
6647 }
6648 else if (tail) {
6649 NODE *n = RNODE_LIST(tail)->nd_next;
6650 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6651 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6652 /* DSTAR */
6653 tail = RNODE_HASH(n)->nd_head;
6654 }
6655 if (tail) {
6656 assocs = list_concat(assocs, tail);
6657 }
6658 }
6659 $$ = assocs;
6660 /*% ripper: rb_ary_push($:1, $:3) %*/
6661 }
6662 ;
6663
6664assoc : arg_value tASSOC arg_value
6665 {
6666 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6667 /*% ripper: assoc_new!($:1, $:3) %*/
6668 }
6669 | tLABEL arg_value
6670 {
6671 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6672 /*% ripper: assoc_new!($:1, $:2) %*/
6673 }
6674 | tLABEL
6675 {
6676 NODE *val = gettable(p, $1, &@$);
6677 if (!val) val = NEW_ERROR(&@$);
6678 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6679 /*% ripper: assoc_new!($:1, Qnil) %*/
6680 }
6681 | tSTRING_BEG string_contents tLABEL_END arg_value
6682 {
6683 YYLTYPE loc = code_loc_gen(&@1, &@3);
6684 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6685 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6686 }
6687 | tDSTAR arg_value
6688 {
6689 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6690 /*% ripper: assoc_splat!($:2) %*/
6691 }
6692 | tDSTAR
6693 {
6694 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6695 $$ = list_append(p, NEW_LIST(0, &@$),
6696 NEW_LVAR(idFWD_KWREST, &@$));
6697 /*% ripper: assoc_splat!(Qnil) %*/
6698 }
6699 ;
6700
6701%rule %inline operation : ident_or_const
6702 | tFID
6703 ;
6704
6705operation2 : operation
6706 | op
6707 ;
6708
6709operation3 : tIDENTIFIER
6710 | tFID
6711 | op
6712 ;
6713
6714dot_or_colon : '.'
6715 | tCOLON2
6716 ;
6717
6718call_op : '.'
6719 | tANDDOT
6720 ;
6721
6722call_op2 : call_op
6723 | tCOLON2
6724 ;
6725
6726rparen : '\n'? ')'
6727 ;
6728
6729rbracket : '\n'? ']'
6730 ;
6731
6732rbrace : '\n'? '}'
6733 ;
6734
6735trailer : '\n'?
6736 | ','
6737 ;
6738
6739term : ';'
6740 {
6741 yyerrok;
6742 token_flush(p);
6743 if (p->ctxt.in_defined) {
6744 p->ctxt.has_trailing_semicolon = 1;
6745 }
6746 }
6747 | '\n'
6748 {
6749 @$.end_pos = @$.beg_pos;
6750 token_flush(p);
6751 }
6752 ;
6753
6754terms : term
6755 | terms ';' {yyerrok;}
6756 ;
6757
6758none : /* none */
6759 {
6760 $$ = 0;
6761 /*% ripper: Qnil %*/
6762 }
6763 ;
6764%%
6765# undef p
6766# undef yylex
6767# undef yylval
6768# define yylval (*p->lval)
6769
6770static int regx_options(struct parser_params*);
6771static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6772static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6773static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6774static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6775
6776#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6777
6778# define set_yylval_node(x) { \
6779 YYLTYPE _cur_loc; \
6780 rb_parser_set_location(p, &_cur_loc); \
6781 yylval.node = (x); \
6782 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6783}
6784# define set_yylval_str(x) \
6785do { \
6786 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6787 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6788} while(0)
6789# define set_yylval_num(x) { \
6790 yylval.num = (x); \
6791 set_parser_s_value(x); \
6792}
6793# define set_yylval_id(x) (yylval.id = (x))
6794# define set_yylval_name(x) { \
6795 (yylval.id = (x)); \
6796 set_parser_s_value(ID2SYM(x)); \
6797}
6798# define yylval_id() (yylval.id)
6799
6800#define set_yylval_noname() set_yylval_id(keyword_nil)
6801#define has_delayed_token(p) (p->delayed.token != NULL)
6802
6803#ifndef RIPPER
6804#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6805#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6806
6807static bool
6808parser_has_token(struct parser_params *p)
6809{
6810 const char *const pcur = p->lex.pcur;
6811 const char *const ptok = p->lex.ptok;
6812 if (p->keep_tokens && (pcur < ptok)) {
6813 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6814 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6815 }
6816 return pcur > ptok;
6817}
6818
6819static const char *
6820escaped_char(int c)
6821{
6822 switch (c) {
6823 case '"': return "\\\"";
6824 case '\\': return "\\\\";
6825 case '\0': return "\\0";
6826 case '\n': return "\\n";
6827 case '\r': return "\\r";
6828 case '\t': return "\\t";
6829 case '\f': return "\\f";
6830 case '\013': return "\\v";
6831 case '\010': return "\\b";
6832 case '\007': return "\\a";
6833 case '\033': return "\\e";
6834 case '\x7f': return "\\c?";
6835 }
6836 return NULL;
6837}
6838
6839static rb_parser_string_t *
6840rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6841{
6842 rb_encoding *enc = p->enc;
6843 const char *ptr = str->ptr;
6844 const char *pend = ptr + str->len;
6845 const char *prev = ptr;
6846 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6847 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6848
6849 while (ptr < pend) {
6850 unsigned int c;
6851 const char *cc;
6852 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6853 if (!MBCLEN_CHARFOUND_P(n)) {
6854 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6855 n = rb_enc_mbminlen(enc);
6856 if (pend < ptr + n)
6857 n = (int)(pend - ptr);
6858 while (n--) {
6859 c = ((unsigned char)*ptr >> 4) & 0x0f;
6860 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6861 c = *ptr & 0x0f;
6862 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6863 parser_str_cat(result, charbuf, 4);
6864 prev = ++ptr;
6865 }
6866 continue;
6867 }
6868 n = MBCLEN_CHARFOUND_LEN(n);
6869 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6870 ptr += n;
6871 cc = escaped_char(c);
6872 if (cc) {
6873 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6874 parser_str_cat_cstr(result, cc);
6875 prev = ptr;
6876 }
6877 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6878 }
6879 else {
6880 if (ptr - n > prev) {
6881 parser_str_cat(result, prev, ptr - n - prev);
6882 prev = ptr - n;
6883 }
6884 parser_str_cat(result, prev, ptr - prev);
6885 prev = ptr;
6886 }
6887 }
6888 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6889
6890 return result;
6891}
6892
6893static void
6894parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6895{
6896 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6897 token->id = p->token_id;
6898 token->type_name = parser_token2char(p, t);
6899 token->str = str;
6900 token->loc.beg_pos = p->yylloc->beg_pos;
6901 token->loc.end_pos = p->yylloc->end_pos;
6902 rb_parser_ary_push_ast_token(p, p->tokens, token);
6903 p->token_id++;
6904
6905 if (p->debug) {
6906 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6907 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6908 line, token->id, token->type_name, str_escaped->ptr,
6909 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6910 token->loc.end_pos.lineno, token->loc.end_pos.column);
6911 rb_parser_string_free(p, str_escaped);
6912 }
6913}
6914
6915static void
6916parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6917{
6918 debug_token_line(p, "parser_dispatch_scan_event", line);
6919
6920 if (!parser_has_token(p)) return;
6921
6922 RUBY_SET_YYLLOC(*p->yylloc);
6923
6924 if (p->keep_tokens) {
6925 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6926 parser_append_tokens(p, str, t, line);
6927 }
6928
6929 token_flush(p);
6930}
6931
6932#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6933static void
6934parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6935{
6936 debug_token_line(p, "parser_dispatch_delayed_token", line);
6937
6938 if (!has_delayed_token(p)) return;
6939
6940 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6941
6942 if (p->keep_tokens) {
6943 /* p->delayed.token is freed by rb_parser_tokens_free */
6944 parser_append_tokens(p, p->delayed.token, t, line);
6945 }
6946 else {
6947 rb_parser_string_free(p, p->delayed.token);
6948 }
6949
6950 p->delayed.token = NULL;
6951}
6952#else
6953#define literal_flush(p, ptr) ((void)(ptr))
6954
6955static int
6956ripper_has_scan_event(struct parser_params *p)
6957{
6958 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6959 return p->lex.pcur > p->lex.ptok;
6960}
6961
6962static VALUE
6963ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6964{
6965 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6966 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6967 RUBY_SET_YYLLOC(*p->yylloc);
6968 token_flush(p);
6969 return rval;
6970}
6971
6972static void
6973ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6974{
6975 if (!ripper_has_scan_event(p)) return;
6976
6977 set_parser_s_value(ripper_scan_event_val(p, t));
6978}
6979#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6980
6981static void
6982ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6983{
6984 /* save and adjust the location to delayed token for callbacks */
6985 int saved_line = p->ruby_sourceline;
6986 const char *saved_tokp = p->lex.ptok;
6987 VALUE s_value, str;
6988
6989 if (!has_delayed_token(p)) return;
6990 p->ruby_sourceline = p->delayed.beg_line;
6991 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6992 str = rb_str_new_mutable_parser_string(p->delayed.token);
6993 rb_parser_string_free(p, p->delayed.token);
6994 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6995 set_parser_s_value(s_value);
6996 p->delayed.token = NULL;
6997 p->ruby_sourceline = saved_line;
6998 p->lex.ptok = saved_tokp;
6999}
7000#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7001#endif /* RIPPER */
7002
7003static inline int
7004is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7005{
7006 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7007}
7008
7009static inline bool
7010peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
7011{
7012 const char *ptr = p->lex.pcur + at;
7013 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
7014 if (memcmp(ptr, str, len)) return false;
7015 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
7016 switch (ptr[len]) {
7017 case '!': case '?': return false;
7018 }
7019 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
7020}
7021
7022static inline int
7023parser_is_identchar(struct parser_params *p)
7024{
7025 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7026}
7027
7028static inline int
7029parser_isascii(struct parser_params *p)
7030{
7031 return ISASCII(*(p->lex.pcur-1));
7032}
7033
7034static void
7035token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7036{
7037 int column = 1, nonspc = 0, i;
7038 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7039 if (*ptr == '\t') {
7040 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7041 }
7042 column++;
7043 if (*ptr != ' ' && *ptr != '\t') {
7044 nonspc = 1;
7045 }
7046 }
7047
7048 ptinfo->beg = loc->beg_pos;
7049 ptinfo->indent = column;
7050 ptinfo->nonspc = nonspc;
7051}
7052
7053static void
7054token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7055{
7056 token_info *ptinfo;
7057
7058 if (!p->token_info_enabled) return;
7059 ptinfo = ALLOC(token_info);
7060 ptinfo->token = token;
7061 ptinfo->next = p->token_info;
7062 token_info_setup(ptinfo, p->lex.pbeg, loc);
7063
7064 p->token_info = ptinfo;
7065}
7066
7067static void
7068token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7069{
7070 token_info *ptinfo_beg = p->token_info;
7071
7072 if (!ptinfo_beg) return;
7073
7074 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7075 token_info_warn(p, token, ptinfo_beg, 1, loc);
7076
7077 p->token_info = ptinfo_beg->next;
7078 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7079}
7080
7081static void
7082token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7083{
7084 token_info *ptinfo_beg = p->token_info;
7085
7086 if (!ptinfo_beg) return;
7087 p->token_info = ptinfo_beg->next;
7088
7089 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7090 ptinfo_beg->beg.column != beg_pos.column ||
7091 strcmp(ptinfo_beg->token, token)) {
7092 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7093 beg_pos.lineno, beg_pos.column, token,
7094 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7095 ptinfo_beg->token);
7096 }
7097
7098 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7099}
7100
7101static void
7102token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7103{
7104 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7105 if (!p->token_info_enabled) return;
7106 if (!ptinfo_beg) return;
7107 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7108 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7109 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7110 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7111 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7112 rb_warn3L(ptinfo_end->beg.lineno,
7113 "mismatched indentations at '%s' with '%s' at %d",
7114 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7115}
7116
7117static int
7118parser_precise_mbclen(struct parser_params *p, const char *ptr)
7119{
7120 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7121 if (!MBCLEN_CHARFOUND_P(len)) {
7122 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7123 return -1;
7124 }
7125 return len;
7126}
7127
7128#ifndef RIPPER
7129static inline void
7130parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7131{
7132 rb_parser_string_t *str;
7133 int lineno = p->ruby_sourceline;
7134 if (!yylloc) {
7135 return;
7136 }
7137 else if (yylloc->beg_pos.lineno == lineno) {
7138 str = p->lex.lastline;
7139 }
7140 else {
7141 return;
7142 }
7143 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7144}
7145
7146static int
7147parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7148{
7149#if 0
7150 YYLTYPE current;
7151
7152 if (!yylloc) {
7153 yylloc = RUBY_SET_YYLLOC(current);
7154 }
7155 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7156 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7157 yylloc = 0;
7158 }
7159#endif
7160 parser_compile_error(p, yylloc, "%s", msg);
7161 parser_show_error_line(p, yylloc);
7162 return 0;
7163}
7164
7165static int
7166parser_yyerror0(struct parser_params *p, const char *msg)
7167{
7168 YYLTYPE current;
7169 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7170}
7171
7172void
7173ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7174{
7175 VALUE mesg;
7176 const int max_line_margin = 30;
7177 const char *ptr, *ptr_end, *pt, *pb;
7178 const char *pre = "", *post = "", *pend;
7179 const char *code = "", *caret = "";
7180 const char *lim;
7181 const char *const pbeg = PARSER_STRING_PTR(str);
7182 char *buf;
7183 long len;
7184 int i;
7185
7186 if (!yylloc) return;
7187 pend = rb_parser_string_end(str);
7188 if (pend > pbeg && pend[-1] == '\n') {
7189 if (--pend > pbeg && pend[-1] == '\r') --pend;
7190 }
7191
7192 pt = pend;
7193 if (lineno == yylloc->end_pos.lineno &&
7194 (pend - pbeg) > yylloc->end_pos.column) {
7195 pt = pbeg + yylloc->end_pos.column;
7196 }
7197
7198 ptr = ptr_end = pt;
7199 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7200 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7201
7202 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7203 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7204
7205 len = ptr_end - ptr;
7206 if (len > 4) {
7207 if (ptr > pbeg) {
7208 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7209 if (ptr > pbeg) pre = "...";
7210 }
7211 if (ptr_end < pend) {
7212 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7213 if (ptr_end < pend) post = "...";
7214 }
7215 }
7216 pb = pbeg;
7217 if (lineno == yylloc->beg_pos.lineno) {
7218 pb += yylloc->beg_pos.column;
7219 if (pb > pt) pb = pt;
7220 }
7221 if (pb < ptr) pb = ptr;
7222 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7223 return;
7224 }
7225 if (RTEST(errbuf)) {
7226 mesg = rb_attr_get(errbuf, idMesg);
7227 if (char_at_end(p, mesg, '\n') != '\n')
7228 rb_str_cat_cstr(mesg, "\n");
7229 }
7230 else {
7231 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7232 }
7233 if (!errbuf && rb_stderr_tty_p()) {
7234#define CSI_BEGIN "\033["
7235#define CSI_SGR "m"
7236 rb_str_catf(mesg,
7237 CSI_BEGIN""CSI_SGR"%s" /* pre */
7238 CSI_BEGIN"1"CSI_SGR"%.*s"
7239 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7240 CSI_BEGIN";1"CSI_SGR"%.*s"
7241 CSI_BEGIN""CSI_SGR"%s" /* post */
7242 "\n",
7243 pre,
7244 (int)(pb - ptr), ptr,
7245 (int)(pt - pb), pb,
7246 (int)(ptr_end - pt), pt,
7247 post);
7248 }
7249 else {
7250 char *p2;
7251
7252 len = ptr_end - ptr;
7253 lim = pt < pend ? pt : pend;
7254 i = (int)(lim - ptr);
7255 buf = ALLOCA_N(char, i+2);
7256 code = ptr;
7257 caret = p2 = buf;
7258 if (ptr <= pb) {
7259 while (ptr < pb) {
7260 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7261 }
7262 *p2++ = '^';
7263 ptr++;
7264 }
7265 if (lim > ptr) {
7266 memset(p2, '~', (lim - ptr));
7267 p2 += (lim - ptr);
7268 }
7269 *p2 = '\0';
7270 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7271 pre, (int)len, code, post,
7272 pre, caret);
7273 }
7274 if (!errbuf) rb_write_error_str(mesg);
7275}
7276#else
7277
7278static int
7279parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7280{
7281 const char *pcur = 0, *ptok = 0;
7282 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7283 p->ruby_sourceline == yylloc->end_pos.lineno) {
7284 pcur = p->lex.pcur;
7285 ptok = p->lex.ptok;
7286 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7287 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7288 }
7289 parser_yyerror0(p, msg);
7290 if (pcur) {
7291 p->lex.ptok = ptok;
7292 p->lex.pcur = pcur;
7293 }
7294 return 0;
7295}
7296
7297static int
7298parser_yyerror0(struct parser_params *p, const char *msg)
7299{
7300 dispatch1(parse_error, STR_NEW2(msg));
7301 ripper_error(p);
7302 return 0;
7303}
7304
7305static inline void
7306parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7307{
7308}
7309#endif /* !RIPPER */
7310
7311static int
7312vtable_size(const struct vtable *tbl)
7313{
7314 if (!DVARS_TERMINAL_P(tbl)) {
7315 return tbl->pos;
7316 }
7317 else {
7318 return 0;
7319 }
7320}
7321
7322static struct vtable *
7323vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7324{
7325 struct vtable *tbl = ALLOC(struct vtable);
7326 tbl->pos = 0;
7327 tbl->capa = 8;
7328 tbl->tbl = ALLOC_N(ID, tbl->capa);
7329 tbl->prev = prev;
7330#ifndef RIPPER
7331 if (p->debug) {
7332 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7333 }
7334#endif
7335 return tbl;
7336}
7337#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7338
7339static void
7340vtable_free_gen(struct parser_params *p, int line, const char *name,
7341 struct vtable *tbl)
7342{
7343#ifndef RIPPER
7344 if (p->debug) {
7345 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7346 }
7347#endif
7348 if (!DVARS_TERMINAL_P(tbl)) {
7349 if (tbl->tbl) {
7350 ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID));
7351 }
7352 ruby_xfree_sized(tbl, sizeof(*tbl));
7353 }
7354}
7355#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7356
7357static void
7358vtable_add_gen(struct parser_params *p, int line, const char *name,
7359 struct vtable *tbl, ID id)
7360{
7361#ifndef RIPPER
7362 if (p->debug) {
7363 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7364 line, name, (void *)tbl, rb_id2name(id));
7365 }
7366#endif
7367 if (DVARS_TERMINAL_P(tbl)) {
7368 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7369 return;
7370 }
7371 if (tbl->pos == tbl->capa) {
7372 tbl->capa = tbl->capa * 2;
7373 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7374 }
7375 tbl->tbl[tbl->pos++] = id;
7376}
7377#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7378
7379static void
7380vtable_pop_gen(struct parser_params *p, int line, const char *name,
7381 struct vtable *tbl, int n)
7382{
7383 if (p->debug) {
7384 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7385 line, name, (void *)tbl, n);
7386 }
7387 if (tbl->pos < n) {
7388 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7389 return;
7390 }
7391 tbl->pos -= n;
7392}
7393#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7394
7395static int
7396vtable_included(const struct vtable * tbl, ID id)
7397{
7398 int i;
7399
7400 if (!DVARS_TERMINAL_P(tbl)) {
7401 for (i = 0; i < tbl->pos; i++) {
7402 if (tbl->tbl[i] == id) {
7403 return i+1;
7404 }
7405 }
7406 }
7407 return 0;
7408}
7409
7410static void parser_prepare(struct parser_params *p);
7411
7412static int
7413e_option_supplied(struct parser_params *p)
7414{
7415 return strcmp(p->ruby_sourcefile, "-e") == 0;
7416}
7417
7418#ifndef RIPPER
7419static NODE *parser_append_options(struct parser_params *p, NODE *node);
7420
7421static VALUE
7422yycompile0(VALUE arg)
7423{
7424 int n;
7425 NODE *tree;
7426 struct parser_params *p = (struct parser_params *)arg;
7427 int cov = FALSE;
7428
7429 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7430 cov = TRUE;
7431 }
7432
7433 if (p->debug_lines) {
7434 p->ast->body.script_lines = p->debug_lines;
7435 }
7436
7437 parser_prepare(p);
7438#define RUBY_DTRACE_PARSE_HOOK(name) \
7439 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7440 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7441 }
7442 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7443 n = yyparse(p);
7444 RUBY_DTRACE_PARSE_HOOK(END);
7445
7446 p->debug_lines = 0;
7447
7448 xfree(p->lex.strterm);
7449 p->lex.strterm = 0;
7450 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7451 if (n || p->error_p) {
7452 VALUE mesg = p->error_buffer;
7453 if (!mesg) {
7454 mesg = syntax_error_new();
7455 }
7456 if (!p->error_tolerant) {
7457 rb_set_errinfo(mesg);
7458 return FALSE;
7459 }
7460 }
7461 tree = p->eval_tree;
7462 if (!tree) {
7463 tree = NEW_NIL(&NULL_LOC);
7464 }
7465 else {
7466 rb_parser_ary_t *tokens = p->tokens;
7467 NODE *prelude;
7468 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7469 prelude = block_append(p, p->eval_tree_begin, body);
7470 RNODE_SCOPE(tree)->nd_body = prelude;
7471 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7472 p->ast->body.coverage_enabled = cov;
7473 if (p->keep_tokens) {
7474 p->ast->node_buffer->tokens = tokens;
7475 p->tokens = NULL;
7476 }
7477 }
7478 p->ast->body.root = tree;
7479 p->ast->body.line_count = p->line_count;
7480 return TRUE;
7481}
7482
7483static rb_ast_t *
7484yycompile(struct parser_params *p, VALUE fname, int line)
7485{
7486 rb_ast_t *ast;
7487 if (NIL_P(fname)) {
7488 p->ruby_sourcefile_string = Qnil;
7489 p->ruby_sourcefile = "(none)";
7490 }
7491 else {
7492 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7493 p->ruby_sourcefile = StringValueCStr(fname);
7494 }
7495 p->ruby_sourceline = line - 1;
7496
7497 p->lvtbl = NULL;
7498
7499 p->ast = ast = rb_ast_new();
7500 compile_callback(yycompile0, (VALUE)p);
7501 ast->body.source_hash = rb_source_hash_finalize(&p->source_hash);
7502 ast->body.has_source_hash = 1;
7503 p->ast = 0;
7504
7505 while (p->lvtbl) {
7506 local_pop(p);
7507 }
7508
7509 return ast;
7510}
7511#endif /* !RIPPER */
7512
7513static rb_encoding *
7514must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7515{
7516 rb_encoding *enc = rb_parser_str_get_encoding(s);
7517 if (!rb_enc_asciicompat(enc)) {
7518 rb_raise(rb_eArgError, "invalid source encoding");
7519 }
7520 return enc;
7521}
7522
7523static rb_parser_string_t *
7524lex_getline(struct parser_params *p)
7525{
7526 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7527 if (!line) return 0;
7528 p->line_count++;
7529 rb_source_hash_update(&p->source_hash, (const uint8_t *)line->ptr, (size_t)line->len);
7530 string_buffer_append(p, line);
7531 must_be_ascii_compatible(p, line);
7532 return line;
7533}
7534
7535#ifndef RIPPER
7536rb_ast_t*
7537rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7538{
7539 p->lex.gets = gets;
7540 p->lex.input = input;
7541 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7542
7543 return yycompile(p, fname, line);
7544}
7545#endif /* !RIPPER */
7546
7547#define STR_FUNC_ESCAPE 0x01
7548#define STR_FUNC_EXPAND 0x02
7549#define STR_FUNC_REGEXP 0x04
7550#define STR_FUNC_QWORDS 0x08
7551#define STR_FUNC_SYMBOL 0x10
7552#define STR_FUNC_INDENT 0x20
7553#define STR_FUNC_LABEL 0x40
7554#define STR_FUNC_LIST 0x4000
7555#define STR_FUNC_TERM 0x8000
7556
7557enum string_type {
7558 str_label = STR_FUNC_LABEL,
7559 str_squote = (0),
7560 str_dquote = (STR_FUNC_EXPAND),
7561 str_xquote = (STR_FUNC_EXPAND),
7562 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7563 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7564 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7565 str_ssym = (STR_FUNC_SYMBOL),
7566 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7567};
7568
7569static rb_parser_string_t *
7570parser_str_new(struct parser_params *p, const char *ptr, rb_len_t len, rb_encoding *enc, int func, rb_encoding *enc0)
7571{
7572 rb_parser_string_t *pstr;
7573
7574 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7575
7576 if (!(func & STR_FUNC_REGEXP)) {
7577 if (rb_parser_is_ascii_string(p, pstr)) {
7578 }
7579 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7580 /* everything is valid in ASCII-8BIT */
7581 enc = rb_ascii8bit_encoding();
7582 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7583 }
7584 }
7585
7586 return pstr;
7587}
7588
7589static int
7590strterm_is_heredoc(rb_strterm_t *strterm)
7591{
7592 return strterm->heredoc;
7593}
7594
7595static rb_strterm_t *
7596new_strterm(struct parser_params *p, int func, int term, int paren)
7597{
7598 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7599 strterm->u.literal.func = func;
7600 strterm->u.literal.term = term;
7601 strterm->u.literal.paren = paren;
7602 return strterm;
7603}
7604
7605static rb_strterm_t *
7606new_heredoc(struct parser_params *p)
7607{
7608 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7609 strterm->heredoc = true;
7610 return strterm;
7611}
7612
7613#define peek(p,c) peek_n(p, (c), 0)
7614#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7615#define peekc(p) peekc_n(p, 0)
7616#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7617
7618#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7619static void
7620parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7621{
7622 debug_token_line(p, "add_delayed_token", line);
7623
7624 if (tok < end) {
7625 if (has_delayed_token(p)) {
7626 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7627 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7628 int end_col = (next_line ? 0 : p->delayed.end_col);
7629 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7630 dispatch_delayed_token(p, tSTRING_CONTENT);
7631 }
7632 }
7633 if (!has_delayed_token(p)) {
7634 p->delayed.token = rb_parser_string_new(p, 0, 0);
7635 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7636 p->delayed.beg_line = p->ruby_sourceline;
7637 p->delayed.beg_col = rb_len2int(tok - p->lex.pbeg);
7638 }
7639 parser_str_cat(p->delayed.token, tok, end - tok);
7640 p->delayed.end_line = p->ruby_sourceline;
7641 p->delayed.end_col = rb_len2int(end - p->lex.pbeg);
7642 p->lex.ptok = end;
7643 }
7644}
7645
7646static void
7647set_lastline(struct parser_params *p, rb_parser_string_t *str)
7648{
7649 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7650 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7651 p->lex.lastline = str;
7652}
7653
7654static int
7655nextline(struct parser_params *p, int set_encoding)
7656{
7657 rb_parser_string_t *str = p->lex.nextline;
7658 p->lex.nextline = 0;
7659 if (!str) {
7660 if (p->eofp)
7661 return -1;
7662
7663 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7664 goto end_of_input;
7665 }
7666
7667 if (!p->lex.input || !(str = lex_getline(p))) {
7668 end_of_input:
7669 p->eofp = 1;
7670 lex_goto_eol(p);
7671 return -1;
7672 }
7673#ifndef RIPPER
7674 if (p->debug_lines) {
7675 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7676 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7677 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7678 }
7679#endif
7680 p->cr_seen = FALSE;
7681 }
7682 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7683 /* after here-document without terminator */
7684 goto end_of_input;
7685 }
7686 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7687 if (p->heredoc_end > 0) {
7688 p->ruby_sourceline = p->heredoc_end;
7689 p->heredoc_end = 0;
7690 }
7691 p->ruby_sourceline++;
7692 set_lastline(p, str);
7693 token_flush(p);
7694 return 0;
7695}
7696
7697static int
7698parser_cr(struct parser_params *p, int c)
7699{
7700 if (peek(p, '\n')) {
7701 p->lex.pcur++;
7702 c = '\n';
7703 }
7704 return c;
7705}
7706
7707static inline int
7708nextc0(struct parser_params *p, int set_encoding)
7709{
7710 int c;
7711
7712 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7713 if (nextline(p, set_encoding)) return -1;
7714 }
7715 c = (unsigned char)*p->lex.pcur++;
7716 if (UNLIKELY(c == '\r')) {
7717 c = parser_cr(p, c);
7718 }
7719
7720 return c;
7721}
7722#define nextc(p) nextc0(p, TRUE)
7723
7724static void
7725pushback(struct parser_params *p, int c)
7726{
7727 if (c == -1) return;
7728 p->eofp = 0;
7729 p->lex.pcur--;
7730 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7731 p->lex.pcur--;
7732 }
7733}
7734
7735#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7736
7737#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7738#define tok(p) (p)->tokenbuf
7739#define toklen(p) (p)->tokidx
7740
7741static int
7742looking_at_eol_p(struct parser_params *p)
7743{
7744 const char *ptr = p->lex.pcur;
7745 while (!lex_eol_ptr_p(p, ptr)) {
7746 int c = (unsigned char)*ptr++;
7747 int eol = (c == '\n' || c == '#');
7748 if (eol || !ISSPACE(c)) {
7749 return eol;
7750 }
7751 }
7752 return TRUE;
7753}
7754
7755static char*
7756newtok(struct parser_params *p)
7757{
7758 p->tokidx = 0;
7759 if (!p->tokenbuf) {
7760 p->toksiz = 60;
7761 p->tokenbuf = ALLOC_N(char, 60);
7762 }
7763 if (p->toksiz > 4096) {
7764 p->toksiz = 60;
7765 REALLOC_N(p->tokenbuf, char, 60);
7766 }
7767 return p->tokenbuf;
7768}
7769
7770static char *
7771tokspace(struct parser_params *p, int n)
7772{
7773 p->tokidx += n;
7774
7775 if (p->tokidx >= p->toksiz) {
7776 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7777 REALLOC_N(p->tokenbuf, char, p->toksiz);
7778 }
7779 return &p->tokenbuf[p->tokidx-n];
7780}
7781
7782static void
7783tokadd(struct parser_params *p, int c)
7784{
7785 p->tokenbuf[p->tokidx++] = (char)c;
7786 if (p->tokidx >= p->toksiz) {
7787 p->toksiz *= 2;
7788 REALLOC_N(p->tokenbuf, char, p->toksiz);
7789 }
7790}
7791
7792static int
7793tok_hex(struct parser_params *p, size_t *numlen)
7794{
7795 int c;
7796
7797 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7798 if (!*numlen) {
7799 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7800 yyerror0("invalid hex escape");
7801 dispatch_scan_event(p, tSTRING_CONTENT);
7802 return 0;
7803 }
7804 p->lex.pcur += *numlen;
7805 return c;
7806}
7807
7808#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7809
7810static int
7811escaped_control_code(int c)
7812{
7813 int c2 = 0;
7814 switch (c) {
7815 case ' ':
7816 c2 = 's';
7817 break;
7818 case '\n':
7819 c2 = 'n';
7820 break;
7821 case '\t':
7822 c2 = 't';
7823 break;
7824 case '\v':
7825 c2 = 'v';
7826 break;
7827 case '\r':
7828 c2 = 'r';
7829 break;
7830 case '\f':
7831 c2 = 'f';
7832 break;
7833 }
7834 return c2;
7835}
7836
7837#define WARN_SPACE_CHAR(c, prefix) \
7838 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7839
7840static int
7841tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7842 int regexp_literal, const char *begin)
7843{
7844 const int wide = !begin;
7845 size_t numlen;
7846 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7847
7848 p->lex.pcur += numlen;
7849 if (p->lex.strterm == NULL ||
7850 strterm_is_heredoc(p->lex.strterm) ||
7851 (p->lex.strterm->u.literal.func != str_regexp)) {
7852 if (!begin) begin = p->lex.pcur;
7853 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7854 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7855 yyerror0("invalid Unicode escape");
7856 dispatch_scan_event(p, tSTRING_CONTENT);
7857 return wide && numlen > 0;
7858 }
7859 if (codepoint > 0x10ffff) {
7860 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7861 yyerror0("invalid Unicode codepoint (too large)");
7862 dispatch_scan_event(p, tSTRING_CONTENT);
7863 return wide;
7864 }
7865 if ((codepoint & 0xfffff800) == 0xd800) {
7866 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7867 yyerror0("invalid Unicode codepoint");
7868 dispatch_scan_event(p, tSTRING_CONTENT);
7869 return wide;
7870 }
7871 }
7872 if (regexp_literal) {
7873 tokcopy(p, (int)numlen);
7874 }
7875 else if (codepoint >= 0x80) {
7876 rb_encoding *utf8 = rb_utf8_encoding();
7877 if (*encp && utf8 != *encp) {
7878 YYLTYPE loc = RUBY_INIT_YYLLOC();
7879 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7880 parser_show_error_line(p, &loc);
7881 return wide;
7882 }
7883 *encp = utf8;
7884 tokaddmbc(p, codepoint, *encp);
7885 }
7886 else {
7887 tokadd(p, codepoint);
7888 }
7889 return TRUE;
7890}
7891
7892static int tokadd_mbchar(struct parser_params *p, int c);
7893
7894static int
7895tokskip_mbchar(struct parser_params *p)
7896{
7897 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7898 if (len > 0) {
7899 p->lex.pcur += len - 1;
7900 }
7901 return len;
7902}
7903
7904/* return value is for ?\u3042 */
7905static void
7906tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7907 int term, int symbol_literal, int regexp_literal)
7908{
7909 /*
7910 * If `term` is not -1, then we allow multiple codepoints in \u{}
7911 * upto `term` byte, otherwise we're parsing a character literal.
7912 * And then add the codepoints to the current token.
7913 */
7914 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7915
7916 const int open_brace = '{', close_brace = '}';
7917
7918 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7919
7920 if (peek(p, open_brace)) { /* handle \u{...} form */
7921 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7922 /*
7923 * Skip parsing validation code and copy bytes as-is until term or
7924 * closing brace, in order to correctly handle extended regexps where
7925 * invalid unicode escapes are allowed in comments. The regexp parser
7926 * does its own validation and will catch any issues.
7927 */
7928 tokadd(p, open_brace);
7929 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7930 int c = peekc(p);
7931 if (c == close_brace) {
7932 tokadd(p, c);
7933 ++p->lex.pcur;
7934 break;
7935 }
7936 else if (c == term) {
7937 break;
7938 }
7939 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7940 tokadd(p, c);
7941 c = *++p->lex.pcur;
7942 }
7943 tokadd_mbchar(p, c);
7944 }
7945 }
7946 else {
7947 const char *second = NULL;
7948 int c, last = nextc(p);
7949 if (lex_eol_p(p)) goto unterminated;
7950 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7951 while (c != close_brace) {
7952 if (c == term) goto unterminated;
7953 if (second == multiple_codepoints)
7954 second = p->lex.pcur;
7955 if (regexp_literal) tokadd(p, last);
7956 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7957 break;
7958 }
7959 while (ISSPACE(c = peekc(p))) {
7960 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7961 last = c;
7962 }
7963 if (term == -1 && !second)
7964 second = multiple_codepoints;
7965 }
7966
7967 if (c != close_brace) {
7968 unterminated:
7969 flush_string_content(p, rb_utf8_encoding(), 0);
7970 yyerror0("unterminated Unicode escape");
7971 dispatch_scan_event(p, tSTRING_CONTENT);
7972 return;
7973 }
7974 if (second && second != multiple_codepoints) {
7975 const char *pcur = p->lex.pcur;
7976 p->lex.pcur = second;
7977 dispatch_scan_event(p, tSTRING_CONTENT);
7978 token_flush(p);
7979 p->lex.pcur = pcur;
7980 yyerror0(multiple_codepoints);
7981 token_flush(p);
7982 }
7983
7984 if (regexp_literal) tokadd(p, close_brace);
7985 nextc(p);
7986 }
7987 }
7988 else { /* handle \uxxxx form */
7989 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7990 token_flush(p);
7991 return;
7992 }
7993 }
7994}
7995
7996#define ESCAPE_CONTROL 1
7997#define ESCAPE_META 2
7998
7999static int
8000read_escape(struct parser_params *p, int flags, const char *begin)
8001{
8002 int c;
8003 size_t numlen;
8004
8005 switch (c = nextc(p)) {
8006 case '\\': /* Backslash */
8007 return c;
8008
8009 case 'n': /* newline */
8010 return '\n';
8011
8012 case 't': /* horizontal tab */
8013 return '\t';
8014
8015 case 'r': /* carriage-return */
8016 return '\r';
8017
8018 case 'f': /* form-feed */
8019 return '\f';
8020
8021 case 'v': /* vertical tab */
8022 return '\13';
8023
8024 case 'a': /* alarm(bell) */
8025 return '\007';
8026
8027 case 'e': /* escape */
8028 return 033;
8029
8030 case '0': case '1': case '2': case '3': /* octal constant */
8031 case '4': case '5': case '6': case '7':
8032 pushback(p, c);
8033 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8034 p->lex.pcur += numlen;
8035 return c;
8036
8037 case 'x': /* hex constant */
8038 c = tok_hex(p, &numlen);
8039 if (numlen == 0) return 0;
8040 return c;
8041
8042 case 'b': /* backspace */
8043 return '\010';
8044
8045 case 's': /* space */
8046 return ' ';
8047
8048 case 'M':
8049 if (flags & ESCAPE_META) goto eof;
8050 if ((c = nextc(p)) != '-') {
8051 goto eof;
8052 }
8053 if ((c = nextc(p)) == '\\') {
8054 switch (peekc(p)) {
8055 case 'u': case 'U':
8056 nextc(p);
8057 goto eof;
8058 }
8059 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8060 }
8061 else if (c == -1) goto eof;
8062 else if (!ISASCII(c)) {
8063 tokskip_mbchar(p);
8064 goto eof;
8065 }
8066 else {
8067 int c2 = escaped_control_code(c);
8068 if (c2) {
8069 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8070 WARN_SPACE_CHAR(c2, "\\M-");
8071 }
8072 else {
8073 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8074 }
8075 }
8076 else if (ISCNTRL(c)) goto eof;
8077 return ((c & 0xff) | 0x80);
8078 }
8079
8080 case 'C':
8081 if ((c = nextc(p)) != '-') {
8082 goto eof;
8083 }
8084 case 'c':
8085 if (flags & ESCAPE_CONTROL) goto eof;
8086 if ((c = nextc(p))== '\\') {
8087 switch (peekc(p)) {
8088 case 'u': case 'U':
8089 nextc(p);
8090 goto eof;
8091 }
8092 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8093 }
8094 else if (c == '?')
8095 return 0177;
8096 else if (c == -1) goto eof;
8097 else if (!ISASCII(c)) {
8098 tokskip_mbchar(p);
8099 goto eof;
8100 }
8101 else {
8102 int c2 = escaped_control_code(c);
8103 if (c2) {
8104 if (ISCNTRL(c)) {
8105 if (flags & ESCAPE_META) {
8106 WARN_SPACE_CHAR(c2, "\\M-");
8107 }
8108 else {
8109 WARN_SPACE_CHAR(c2, "");
8110 }
8111 }
8112 else {
8113 if (flags & ESCAPE_META) {
8114 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8115 }
8116 else {
8117 WARN_SPACE_CHAR(c2, "\\C-");
8118 }
8119 }
8120 }
8121 else if (ISCNTRL(c)) goto eof;
8122 }
8123 return c & 0x9f;
8124
8125 eof:
8126 case -1:
8127 flush_string_content(p, p->enc, p->lex.pcur - begin);
8128 yyerror0("Invalid escape character syntax");
8129 dispatch_scan_event(p, tSTRING_CONTENT);
8130 return '\0';
8131
8132 default:
8133 if (!ISASCII(c)) {
8134 tokskip_mbchar(p);
8135 goto eof;
8136 }
8137 return c;
8138 }
8139}
8140
8141static void
8142tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8143{
8144 int len = rb_enc_codelen(c, enc);
8145 rb_enc_mbcput(c, tokspace(p, len), enc);
8146}
8147
8148static int
8149tokadd_escape(struct parser_params *p)
8150{
8151 int c;
8152 size_t numlen;
8153 const char *begin = p->lex.pcur;
8154
8155 switch (c = nextc(p)) {
8156 case '\n':
8157 return 0; /* just ignore */
8158
8159 case '0': case '1': case '2': case '3': /* octal constant */
8160 case '4': case '5': case '6': case '7':
8161 {
8162 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8163 if (numlen == 0) goto eof;
8164 p->lex.pcur += numlen;
8165 tokcopy(p, (int)numlen + 1);
8166 }
8167 return 0;
8168
8169 case 'x': /* hex constant */
8170 {
8171 tok_hex(p, &numlen);
8172 if (numlen == 0) return -1;
8173 tokcopy(p, (int)numlen + 2);
8174 }
8175 return 0;
8176
8177 eof:
8178 case -1:
8179 flush_string_content(p, p->enc, p->lex.pcur - begin);
8180 yyerror0("Invalid escape character syntax");
8181 token_flush(p);
8182 return -1;
8183
8184 default:
8185 tokadd(p, '\\');
8186 tokadd(p, c);
8187 }
8188 return 0;
8189}
8190
8191static int
8192char_to_option(int c)
8193{
8194 int val;
8195
8196 switch (c) {
8197 case 'i':
8198 val = RE_ONIG_OPTION_IGNORECASE;
8199 break;
8200 case 'x':
8201 val = RE_ONIG_OPTION_EXTEND;
8202 break;
8203 case 'm':
8204 val = RE_ONIG_OPTION_MULTILINE;
8205 break;
8206 default:
8207 val = 0;
8208 break;
8209 }
8210 return val;
8211}
8212
8213#define ARG_ENCODING_FIXED 16
8214#define ARG_ENCODING_NONE 32
8215#define ENC_ASCII8BIT 1
8216#define ENC_EUC_JP 2
8217#define ENC_Windows_31J 3
8218#define ENC_UTF8 4
8219
8220static int
8221char_to_option_kcode(int c, int *option, int *kcode)
8222{
8223 *option = 0;
8224
8225 switch (c) {
8226 case 'n':
8227 *kcode = ENC_ASCII8BIT;
8228 return (*option = ARG_ENCODING_NONE);
8229 case 'e':
8230 *kcode = ENC_EUC_JP;
8231 break;
8232 case 's':
8233 *kcode = ENC_Windows_31J;
8234 break;
8235 case 'u':
8236 *kcode = ENC_UTF8;
8237 break;
8238 default:
8239 *kcode = -1;
8240 return (*option = char_to_option(c));
8241 }
8242 *option = ARG_ENCODING_FIXED;
8243 return 1;
8244}
8245
8246static int
8247regx_options(struct parser_params *p)
8248{
8249 int kcode = 0;
8250 int kopt = 0;
8251 int options = 0;
8252 int c, opt, kc;
8253
8254 newtok(p);
8255 while (c = nextc(p), ISALPHA(c)) {
8256 if (c == 'o') {
8257 options |= RE_OPTION_ONCE;
8258 }
8259 else if (char_to_option_kcode(c, &opt, &kc)) {
8260 if (kc >= 0) {
8261 if (kc != ENC_ASCII8BIT) kcode = c;
8262 kopt = opt;
8263 }
8264 else {
8265 options |= opt;
8266 }
8267 }
8268 else {
8269 tokadd(p, c);
8270 }
8271 }
8272 options |= kopt;
8273 pushback(p, c);
8274 if (toklen(p)) {
8275 YYLTYPE loc = RUBY_INIT_YYLLOC();
8276 tokfix(p);
8277 compile_error(p, "unknown regexp option%s - %*s",
8278 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8279 parser_show_error_line(p, &loc);
8280 }
8281 return options | RE_OPTION_ENCODING(kcode);
8282}
8283
8284static int
8285tokadd_mbchar(struct parser_params *p, int c)
8286{
8287 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8288 if (len < 0) return -1;
8289 tokadd(p, c);
8290 p->lex.pcur += --len;
8291 if (len > 0) tokcopy(p, len);
8292 return c;
8293}
8294
8295static inline int
8296simple_re_meta(int c)
8297{
8298 switch (c) {
8299 case '$': case '*': case '+': case '.':
8300 case '?': case '^': case '|':
8301 case ')': case ']': case '}': case '>':
8302 return TRUE;
8303 default:
8304 return FALSE;
8305 }
8306}
8307
8308static int
8309parser_update_heredoc_indent(struct parser_params *p, int c)
8310{
8311 if (p->heredoc_line_indent == -1) {
8312 if (c == '\n') p->heredoc_line_indent = 0;
8313 }
8314 else {
8315 if (c == ' ') {
8316 p->heredoc_line_indent++;
8317 return TRUE;
8318 }
8319 else if (c == '\t') {
8320 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8321 p->heredoc_line_indent = w * TAB_WIDTH;
8322 return TRUE;
8323 }
8324 else if (c != '\n') {
8325 if (p->heredoc_indent > p->heredoc_line_indent) {
8326 p->heredoc_indent = p->heredoc_line_indent;
8327 }
8328 p->heredoc_line_indent = -1;
8329 }
8330 else {
8331 /* Whitespace only line has no indentation */
8332 p->heredoc_line_indent = 0;
8333 }
8334 }
8335 return FALSE;
8336}
8337
8338static void
8339parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8340{
8341 YYLTYPE loc = RUBY_INIT_YYLLOC();
8342 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8343 compile_error(p, "%s mixed within %s source", n1, n2);
8344 parser_show_error_line(p, &loc);
8345}
8346
8347static void
8348parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8349{
8350 const char *pos = p->lex.pcur;
8351 p->lex.pcur = beg;
8352 parser_mixed_error(p, enc1, enc2);
8353 p->lex.pcur = pos;
8354}
8355
8356static inline char
8357nibble_char_upper(unsigned int c)
8358{
8359 c &= 0xf;
8360 return c + (c < 10 ? '0' : 'A' - 10);
8361}
8362
8363static int
8364tokadd_string(struct parser_params *p,
8365 int func, int term, int paren, long *nest,
8366 rb_encoding **encp, rb_encoding **enc)
8367{
8368 int c;
8369 bool erred = false;
8370#ifdef RIPPER
8371 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8372 int top_of_line = FALSE;
8373#endif
8374
8375#define mixed_error(enc1, enc2) \
8376 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8377#define mixed_escape(beg, enc1, enc2) \
8378 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8379
8380 while ((c = nextc(p)) != -1) {
8381 if (p->heredoc_indent > 0) {
8382 parser_update_heredoc_indent(p, c);
8383 }
8384#ifdef RIPPER
8385 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8386 pushback(p, c);
8387 break;
8388 }
8389#endif
8390
8391 if (paren && c == paren) {
8392 ++*nest;
8393 }
8394 else if (c == term) {
8395 if (!nest || !*nest) {
8396 pushback(p, c);
8397 break;
8398 }
8399 --*nest;
8400 }
8401 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8402 unsigned char c2 = *p->lex.pcur;
8403 if (c2 == '$' || c2 == '@' || c2 == '{') {
8404 pushback(p, c);
8405 break;
8406 }
8407 }
8408 else if (c == '\\') {
8409 c = nextc(p);
8410 switch (c) {
8411 case '\n':
8412 if (func & STR_FUNC_QWORDS) break;
8413 if (func & STR_FUNC_EXPAND) {
8414 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8415 continue;
8416 if (c == term) {
8417 c = '\\';
8418 goto terminate;
8419 }
8420 }
8421 tokadd(p, '\\');
8422 break;
8423
8424 case '\\':
8425 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8426 break;
8427
8428 case 'u':
8429 if ((func & STR_FUNC_EXPAND) == 0) {
8430 tokadd(p, '\\');
8431 break;
8432 }
8433 tokadd_utf8(p, enc, term,
8434 func & STR_FUNC_SYMBOL,
8435 func & STR_FUNC_REGEXP);
8436 continue;
8437
8438 default:
8439 if (c == -1) return -1;
8440 if (!ISASCII(c)) {
8441 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8442 goto non_ascii;
8443 }
8444 if (func & STR_FUNC_REGEXP) {
8445 switch (c) {
8446 case 'c':
8447 case 'C':
8448 case 'M': {
8449 pushback(p, c);
8450 c = read_escape(p, 0, p->lex.pcur - 1);
8451
8452 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8453 *t++ = '\\';
8454 *t++ = 'x';
8455 *t++ = nibble_char_upper(c >> 4);
8456 *t++ = nibble_char_upper(c);
8457 continue;
8458 }
8459 }
8460
8461 if (c == term && !simple_re_meta(c)) {
8462 tokadd(p, c);
8463 continue;
8464 }
8465 pushback(p, c);
8466 if ((c = tokadd_escape(p)) < 0)
8467 return -1;
8468 if (*enc && *enc != *encp) {
8469 mixed_escape(p->lex.ptok+2, *enc, *encp);
8470 }
8471 continue;
8472 }
8473 else if (func & STR_FUNC_EXPAND) {
8474 pushback(p, c);
8475 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8476 c = read_escape(p, 0, p->lex.pcur - 1);
8477 }
8478 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8479 /* ignore backslashed spaces in %w */
8480 }
8481 else if (c != term && !(paren && c == paren)) {
8482 tokadd(p, '\\');
8483 pushback(p, c);
8484 continue;
8485 }
8486 }
8487 }
8488 else if (!parser_isascii(p)) {
8489 non_ascii:
8490 if (!*enc) {
8491 *enc = *encp;
8492 }
8493 else if (*enc != *encp) {
8494 mixed_error(*enc, *encp);
8495 continue;
8496 }
8497 if (tokadd_mbchar(p, c) == -1) return -1;
8498 continue;
8499 }
8500 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8501 pushback(p, c);
8502 break;
8503 }
8504 if (c & 0x80) {
8505 if (!*enc) {
8506 *enc = *encp;
8507 }
8508 else if (*enc != *encp) {
8509 mixed_error(*enc, *encp);
8510 continue;
8511 }
8512 }
8513 tokadd(p, c);
8514#ifdef RIPPER
8515 top_of_line = (c == '\n');
8516#endif
8517 }
8518 terminate:
8519 if (*enc) *encp = *enc;
8520 return c;
8521}
8522
8523#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8524
8525static void
8526flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8527{
8528 p->lex.pcur -= back;
8529 if (has_delayed_token(p)) {
8530 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8531 if (len > 0) {
8532 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8533 p->delayed.end_line = p->ruby_sourceline;
8534 p->delayed.end_col = rb_len2int(p->lex.pcur - p->lex.pbeg);
8535 }
8536 dispatch_delayed_token(p, tSTRING_CONTENT);
8537 p->lex.ptok = p->lex.pcur;
8538 }
8539 dispatch_scan_event(p, tSTRING_CONTENT);
8540 p->lex.pcur += back;
8541}
8542
8543/* this can be shared with ripper, since it's independent from struct
8544 * parser_params. */
8545#ifndef RIPPER
8546#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8547#define SPECIAL_PUNCT(idx) ( \
8548 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8549 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8550 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8551 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8552 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8553 BIT('0', idx))
8554const uint_least32_t ruby_global_name_punct_bits[] = {
8555 SPECIAL_PUNCT(0),
8556 SPECIAL_PUNCT(1),
8557 SPECIAL_PUNCT(2),
8558};
8559#undef BIT
8560#undef SPECIAL_PUNCT
8561#endif
8562
8563static enum yytokentype
8564parser_peek_variable_name(struct parser_params *p)
8565{
8566 int c;
8567 const char *ptr = p->lex.pcur;
8568
8569 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8570 c = *ptr++;
8571 switch (c) {
8572 case '$':
8573 if ((c = *ptr) == '-') {
8574 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8575 c = *ptr;
8576 }
8577 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8578 return tSTRING_DVAR;
8579 }
8580 break;
8581 case '@':
8582 if ((c = *ptr) == '@') {
8583 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8584 c = *ptr;
8585 }
8586 break;
8587 case '{':
8588 p->lex.pcur = ptr;
8589 p->command_start = TRUE;
8590 yylval.state = p->lex.state;
8591 return tSTRING_DBEG;
8592 default:
8593 return 0;
8594 }
8595 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8596 return tSTRING_DVAR;
8597 return 0;
8598}
8599
8600#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8601#define IS_END() IS_lex_state(EXPR_END_ANY)
8602#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8603#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8604#define IS_LABEL_POSSIBLE() (\
8605 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8606 IS_ARG())
8607#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8608#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8609
8610static inline enum yytokentype
8611parser_string_term(struct parser_params *p, int func)
8612{
8613 xfree(p->lex.strterm);
8614 p->lex.strterm = 0;
8615 if (func & STR_FUNC_REGEXP) {
8616 set_yylval_num(regx_options(p));
8617 dispatch_scan_event(p, tREGEXP_END);
8618 SET_LEX_STATE(EXPR_END);
8619 return tREGEXP_END;
8620 }
8621 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8622 nextc(p);
8623 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8624 return tLABEL_END;
8625 }
8626 SET_LEX_STATE(EXPR_END);
8627 return tSTRING_END;
8628}
8629
8630static enum yytokentype
8631parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8632{
8633 int func = quote->func;
8634 int term = quote->term;
8635 int paren = quote->paren;
8636 int c, space = 0;
8637 rb_encoding *enc = p->enc;
8638 rb_encoding *base_enc = 0;
8639 rb_parser_string_t *lit;
8640
8641 if (func & STR_FUNC_TERM) {
8642 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8643 SET_LEX_STATE(EXPR_END);
8644 xfree(p->lex.strterm);
8645 p->lex.strterm = 0;
8646 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8647 }
8648 c = nextc(p);
8649 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8650 while (c != '\n' && ISSPACE(c = nextc(p)));
8651 space = 1;
8652 }
8653 if (func & STR_FUNC_LIST) {
8654 quote->func &= ~STR_FUNC_LIST;
8655 space = 1;
8656 }
8657 if (c == term && !quote->nest) {
8658 if (func & STR_FUNC_QWORDS) {
8659 quote->func |= STR_FUNC_TERM;
8660 pushback(p, c); /* dispatch the term at tSTRING_END */
8661 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8662 return ' ';
8663 }
8664 return parser_string_term(p, func);
8665 }
8666 if (space) {
8667 if (!ISSPACE(c)) pushback(p, c);
8668 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8669 return ' ';
8670 }
8671 newtok(p);
8672 if ((func & STR_FUNC_EXPAND) && c == '#') {
8673 enum yytokentype t = parser_peek_variable_name(p);
8674 if (t) return t;
8675 tokadd(p, '#');
8676 c = nextc(p);
8677 }
8678 pushback(p, c);
8679 if (tokadd_string(p, func, term, paren, &quote->nest,
8680 &enc, &base_enc) == -1) {
8681 if (p->eofp) {
8682#ifndef RIPPER
8683# define unterminated_literal(mesg) yyerror0(mesg)
8684#else
8685# define unterminated_literal(mesg) compile_error(p, mesg)
8686#endif
8687 literal_flush(p, p->lex.pcur);
8688 if (func & STR_FUNC_QWORDS) {
8689 /* no content to add, bailing out here */
8690 unterminated_literal("unterminated list meets end of file");
8691 xfree(p->lex.strterm);
8692 p->lex.strterm = 0;
8693 return tSTRING_END;
8694 }
8695 if (func & STR_FUNC_REGEXP) {
8696 unterminated_literal("unterminated regexp meets end of file");
8697 }
8698 else {
8699 unterminated_literal("unterminated string meets end of file");
8700 }
8701 quote->func |= STR_FUNC_TERM;
8702 }
8703 }
8704
8705 tokfix(p);
8706 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8707 set_yylval_str(lit);
8708 flush_string_content(p, enc, 0);
8709
8710 return tSTRING_CONTENT;
8711}
8712
8713static enum yytokentype
8714heredoc_identifier(struct parser_params *p)
8715{
8716 /*
8717 * term_len is length of `<<"END"` except `END`,
8718 * in this case term_len is 4 (<, <, " and ").
8719 */
8720 rb_len_t len, offset = p->lex.pcur - p->lex.pbeg;
8721 int c = nextc(p), term, func = 0, quote = 0;
8722 enum yytokentype token = tSTRING_BEG;
8723 int indent = 0;
8724
8725 if (c == '-') {
8726 c = nextc(p);
8727 func = STR_FUNC_INDENT;
8728 offset++;
8729 }
8730 else if (c == '~') {
8731 c = nextc(p);
8732 func = STR_FUNC_INDENT;
8733 offset++;
8734 indent = INT_MAX;
8735 }
8736 switch (c) {
8737 case '\'':
8738 func |= str_squote; goto quoted;
8739 case '"':
8740 func |= str_dquote; goto quoted;
8741 case '`':
8742 token = tXSTRING_BEG;
8743 func |= str_xquote; goto quoted;
8744
8745 quoted:
8746 quote++;
8747 offset++;
8748 term = c;
8749 len = 0;
8750 while ((c = nextc(p)) != term) {
8751 if (c == -1 || c == '\r' || c == '\n') {
8752 yyerror0("unterminated here document identifier");
8753 return -1;
8754 }
8755 }
8756 break;
8757
8758 default:
8759 if (!parser_is_identchar(p)) {
8760 pushback(p, c);
8761 if (func & STR_FUNC_INDENT) {
8762 pushback(p, indent > 0 ? '~' : '-');
8763 }
8764 return 0;
8765 }
8766 func |= str_dquote;
8767 do {
8768 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8769 if (n < 0) return 0;
8770 p->lex.pcur += --n;
8771 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8772 pushback(p, c);
8773 break;
8774 }
8775
8776 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8777 if ((rb_ulen_t)len >= HERETERM_LENGTH_MAX)
8778 yyerror0("too long here document identifier");
8779 dispatch_scan_event(p, tHEREDOC_BEG);
8780 lex_goto_eol(p);
8781
8782 p->lex.strterm = new_heredoc(p);
8783 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8784 here->offset = offset;
8785 here->sourceline = p->ruby_sourceline;
8786 here->length = (unsigned)len;
8787 here->quote = quote;
8788 here->func = func;
8789 here->lastline = p->lex.lastline;
8790
8791 token_flush(p);
8792 p->heredoc_indent = indent;
8793 p->heredoc_line_indent = 0;
8794 return token;
8795}
8796
8797static void
8798heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8799{
8800 rb_parser_string_t *line;
8801 rb_strterm_t *term = p->lex.strterm;
8802
8803 p->lex.strterm = 0;
8804 line = here->lastline;
8805 p->lex.lastline = line;
8806 p->lex.pbeg = PARSER_STRING_PTR(line);
8807 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8808 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8809 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8810 p->heredoc_end = p->ruby_sourceline;
8811 p->ruby_sourceline = (int)here->sourceline;
8812 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8813 p->eofp = 0;
8814 xfree(term);
8815}
8816
8817static int
8818dedent_string_column(const char *str, rb_len_t len, int width)
8819{
8820 int i, col = 0;
8821
8822 for (i = 0; i < len && col < width; i++) {
8823 if (str[i] == ' ') {
8824 col++;
8825 }
8826 else if (str[i] == '\t') {
8827 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8828 if (n > width) break;
8829 col = n;
8830 }
8831 else {
8832 break;
8833 }
8834 }
8835
8836 return i;
8837}
8838
8839static int
8840dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8841{
8842 char *str;
8843 rb_len_t len;
8844 int i;
8845
8846 len = PARSER_STRING_LEN(string);
8847 str = PARSER_STRING_PTR(string);
8848
8849 i = dedent_string_column(str, len, width);
8850 if (!i) return 0;
8851
8852 rb_parser_str_modify(string);
8853 str = PARSER_STRING_PTR(string);
8854 if (PARSER_STRING_LEN(string) != len)
8855 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8856 MEMMOVE(str, str + i, char, len - i);
8857 rb_parser_str_set_len(p, string, len - i);
8858 return i;
8859}
8860
8861static NODE *
8862heredoc_dedent(struct parser_params *p, NODE *root)
8863{
8864 NODE *node, *str_node, *prev_node;
8865 int indent = p->heredoc_indent;
8866 rb_parser_string_t *prev_lit = 0;
8867
8868 if (indent <= 0) return root;
8869 if (!root) return root;
8870
8871 prev_node = node = str_node = root;
8872 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8873
8874 while (str_node) {
8875 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8876 if (nd_fl_newline(str_node)) {
8877 dedent_string(p, lit, indent);
8878 }
8879 if (!prev_lit) {
8880 prev_lit = lit;
8881 }
8882 else if (!literal_concat0(p, prev_lit, lit)) {
8883 return 0;
8884 }
8885 else {
8886 NODE *end = RNODE_LIST(node)->as.nd_end;
8887 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8888 if (!node) {
8889 if (nd_type_p(prev_node, NODE_DSTR))
8890 nd_set_type(prev_node, NODE_STR);
8891 break;
8892 }
8893 RNODE_LIST(node)->as.nd_end = end;
8894 goto next_str;
8895 }
8896
8897 str_node = 0;
8898 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8899 next_str:
8900 if (!nd_type_p(node, NODE_LIST)) break;
8901 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8902 enum node_type type = nd_type(str_node);
8903 if (type == NODE_STR || type == NODE_DSTR) break;
8904 prev_lit = 0;
8905 str_node = 0;
8906 }
8907 }
8908 }
8909 return root;
8910}
8911
8912static int
8913whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8914{
8915 const char *beg = p->lex.pbeg;
8916 const char *ptr = p->lex.pend;
8917
8918 if (ptr - beg < len) return FALSE;
8919 if (ptr > beg && ptr[-1] == '\n') {
8920 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8921 if (ptr - beg < len) return FALSE;
8922 }
8923 if (strncmp(eos, ptr -= len, len)) return FALSE;
8924 if (indent) {
8925 while (beg < ptr && ISSPACE(*beg)) beg++;
8926 }
8927 return beg == ptr;
8928}
8929
8930static int
8931word_match_p(struct parser_params *p, const char *word, long len)
8932{
8933 if (strncmp(p->lex.pcur, word, len)) return 0;
8934 if (lex_eol_n_p(p, len)) return 1;
8935 int c = (unsigned char)p->lex.pcur[len];
8936 if (ISSPACE(c)) return 1;
8937 switch (c) {
8938 case '\0': case '\004': case '\032': return 1;
8939 }
8940 return 0;
8941}
8942
8943#define NUM_SUFFIX_R (1<<0)
8944#define NUM_SUFFIX_I (1<<1)
8945#define NUM_SUFFIX_ALL 3
8946
8947static int
8948number_literal_suffix(struct parser_params *p, int mask)
8949{
8950 int c, result = 0;
8951 const char *lastp = p->lex.pcur;
8952
8953 while ((c = nextc(p)) != -1) {
8954 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8955 result |= (mask & NUM_SUFFIX_I);
8956 mask &= ~NUM_SUFFIX_I;
8957 /* r after i, rational of complex is disallowed */
8958 mask &= ~NUM_SUFFIX_R;
8959 continue;
8960 }
8961 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8962 result |= (mask & NUM_SUFFIX_R);
8963 mask &= ~NUM_SUFFIX_R;
8964 continue;
8965 }
8966 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8967 p->lex.pcur = lastp;
8968 return 0;
8969 }
8970 pushback(p, c);
8971 break;
8972 }
8973 return result;
8974}
8975
8976static enum yytokentype
8977set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8978{
8979 enum rb_numeric_type numeric_type = integer_literal;
8980
8981 if (type == tFLOAT) {
8982 numeric_type = float_literal;
8983 }
8984
8985 if (suffix & NUM_SUFFIX_R) {
8986 type = tRATIONAL;
8987 numeric_type = rational_literal;
8988 }
8989 if (suffix & NUM_SUFFIX_I) {
8990 type = tIMAGINARY;
8991 }
8992
8993 switch (type) {
8994 case tINTEGER:
8995 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8996 break;
8997 case tFLOAT:
8998 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8999 break;
9000 case tRATIONAL:
9001 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9002 break;
9003 case tIMAGINARY:
9004 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9005 (void)numeric_type; /* for ripper */
9006 break;
9007 default:
9008 rb_bug("unexpected token: %d", type);
9009 }
9010 SET_LEX_STATE(EXPR_END);
9011 return type;
9012}
9013
9014#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9015static void
9016parser_dispatch_heredoc_end(struct parser_params *p, int line)
9017{
9018 if (has_delayed_token(p))
9019 dispatch_delayed_token(p, tSTRING_CONTENT);
9020
9021#ifdef RIPPER
9022 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9023 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9024#else
9025 if (p->keep_tokens) {
9026 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9027 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9028 parser_append_tokens(p, str, tHEREDOC_END, line);
9029 }
9030#endif
9031
9032 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9033 lex_goto_eol(p);
9034 token_flush(p);
9035}
9036
9037static enum yytokentype
9038here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9039{
9040 int c, func, indent = 0;
9041 const char *eos, *ptr, *ptr_end;
9042 long len;
9043 rb_parser_string_t *str = 0;
9044 rb_encoding *enc = p->enc;
9045 rb_encoding *base_enc = 0;
9046 int bol;
9047#ifdef RIPPER
9048 VALUE s_value;
9049#endif
9050
9051 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9052 len = here->length;
9053 indent = (func = here->func) & STR_FUNC_INDENT;
9054
9055 if ((c = nextc(p)) == -1) {
9056 error:
9057#ifdef RIPPER
9058 if (!has_delayed_token(p)) {
9059 dispatch_scan_event(p, tSTRING_CONTENT);
9060 }
9061 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9062 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9063 if (!(func & STR_FUNC_REGEXP)) {
9064 int cr = ENC_CODERANGE_UNKNOWN;
9065 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9066 if (cr != ENC_CODERANGE_7BIT &&
9067 rb_is_usascii_enc(p->enc) &&
9068 enc != rb_utf8_encoding()) {
9069 enc = rb_ascii8bit_encoding();
9070 }
9071 }
9072 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9073 }
9074 dispatch_delayed_token(p, tSTRING_CONTENT);
9075 }
9076 else {
9077 dispatch_delayed_token(p, tSTRING_CONTENT);
9078 dispatch_scan_event(p, tSTRING_CONTENT);
9079 }
9080 lex_goto_eol(p);
9081#endif
9082 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9083 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9084 (int)len, eos);
9085 token_flush(p);
9086 SET_LEX_STATE(EXPR_END);
9087 return tSTRING_END;
9088 }
9089 bol = was_bol(p);
9090 if (!bol) {
9091 /* not beginning of line, cannot be the terminator */
9092 }
9093 else if (p->heredoc_line_indent == -1) {
9094 /* `heredoc_line_indent == -1` means
9095 * - "after an interpolation in the same line", or
9096 * - "in a continuing line"
9097 */
9098 p->heredoc_line_indent = 0;
9099 }
9100 else if (whole_match_p(p, eos, len, indent)) {
9101 dispatch_heredoc_end(p);
9102 restore:
9103 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9104 token_flush(p);
9105 SET_LEX_STATE(EXPR_END);
9106 return tSTRING_END;
9107 }
9108
9109 if (!(func & STR_FUNC_EXPAND)) {
9110 do {
9111 ptr = PARSER_STRING_PTR(p->lex.lastline);
9112 ptr_end = p->lex.pend;
9113 if (ptr_end > ptr) {
9114 switch (ptr_end[-1]) {
9115 case '\n':
9116 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9117 ptr_end++;
9118 break;
9119 }
9120 case '\r':
9121 --ptr_end;
9122 }
9123 }
9124
9125 if (p->heredoc_indent > 0) {
9126 long i = 0;
9127 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9128 i++;
9129 p->heredoc_line_indent = 0;
9130 }
9131
9132 if (str)
9133 parser_str_cat(str, ptr, ptr_end - ptr);
9134 else
9135 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9136 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9137 lex_goto_eol(p);
9138 if (p->heredoc_indent > 0) {
9139 goto flush_str;
9140 }
9141 if (nextc(p) == -1) {
9142 if (str) {
9143 rb_parser_string_free(p, str);
9144 str = 0;
9145 }
9146 goto error;
9147 }
9148 } while (!whole_match_p(p, eos, len, indent));
9149 }
9150 else {
9151 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9152 newtok(p);
9153 if (c == '#') {
9154 enum yytokentype t = parser_peek_variable_name(p);
9155 if (p->heredoc_line_indent != -1) {
9156 if (p->heredoc_indent > p->heredoc_line_indent) {
9157 p->heredoc_indent = p->heredoc_line_indent;
9158 }
9159 p->heredoc_line_indent = -1;
9160 }
9161 if (t) return t;
9162 tokadd(p, '#');
9163 c = nextc(p);
9164 }
9165 do {
9166 pushback(p, c);
9167 enc = p->enc;
9168 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9169 if (p->eofp) goto error;
9170 goto restore;
9171 }
9172 if (c != '\n') {
9173 if (c == '\\') p->heredoc_line_indent = -1;
9174 flush:
9175 str = STR_NEW3(tok(p), toklen(p), enc, func);
9176 flush_str:
9177 set_yylval_str(str);
9178#ifndef RIPPER
9179 if (bol) nd_set_fl_newline(yylval.node);
9180#endif
9181 flush_string_content(p, enc, 0);
9182 return tSTRING_CONTENT;
9183 }
9184 tokadd(p, nextc(p));
9185 if (p->heredoc_indent > 0) {
9186 lex_goto_eol(p);
9187 goto flush;
9188 }
9189 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9190 if ((c = nextc(p)) == -1) goto error;
9191 } while (!whole_match_p(p, eos, len, indent));
9192 str = STR_NEW3(tok(p), toklen(p), enc, func);
9193 }
9194 dispatch_heredoc_end(p);
9195 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9196 token_flush(p);
9197 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9198#ifdef RIPPER
9199 /* Preserve s_value for set_yylval_str */
9200 s_value = p->s_value;
9201#endif
9202 set_yylval_str(str);
9203#ifdef RIPPER
9204 set_parser_s_value(s_value);
9205#endif
9206
9207#ifndef RIPPER
9208 if (bol) nd_set_fl_newline(yylval.node);
9209#endif
9210 return tSTRING_CONTENT;
9211}
9212
9213#include "lex.c"
9214
9215static int
9216arg_ambiguous(struct parser_params *p, char c)
9217{
9218#ifndef RIPPER
9219 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9220#else
9221 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9222#endif
9223 return TRUE;
9224}
9225
9226/* returns true value if formal argument error;
9227 * Qtrue, or error message if ripper */
9228static VALUE
9229formal_argument_error(struct parser_params *p, ID id)
9230{
9231 switch (id_type(id)) {
9232 case ID_LOCAL:
9233 break;
9234#ifndef RIPPER
9235# define ERR(mesg) (yyerror0(mesg), Qtrue)
9236#else
9237# define ERR(mesg) WARN_S(mesg)
9238#endif
9239 case ID_CONST:
9240 return ERR("formal argument cannot be a constant");
9241 case ID_INSTANCE:
9242 return ERR("formal argument cannot be an instance variable");
9243 case ID_GLOBAL:
9244 return ERR("formal argument cannot be a global variable");
9245 case ID_CLASS:
9246 return ERR("formal argument cannot be a class variable");
9247 default:
9248 return ERR("formal argument must be local variable");
9249#undef ERR
9250 }
9251 shadowing_lvar(p, id);
9252
9253 return Qfalse;
9254}
9255
9256static int
9257lvar_defined(struct parser_params *p, ID id)
9258{
9259 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9260}
9261
9262/* emacsen -*- hack */
9263static long
9264parser_encode_length(struct parser_params *p, const char *name, long len)
9265{
9266 long nlen;
9267
9268 if (len > 5 && name[nlen = len - 5] == '-') {
9269 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9270 return nlen;
9271 }
9272 if (len > 4 && name[nlen = len - 4] == '-') {
9273 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9274 return nlen;
9275 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9276 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9277 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9278 return nlen;
9279 }
9280 return len;
9281}
9282
9283static void
9284parser_set_encode(struct parser_params *p, const char *name)
9285{
9286 rb_encoding *enc;
9287 VALUE excargs[3];
9288 int idx = 0;
9289
9290 const char *wrong = 0;
9291 switch (*name) {
9292 case 'e': case 'E': wrong = "external"; break;
9293 case 'i': case 'I': wrong = "internal"; break;
9294 case 'f': case 'F': wrong = "filesystem"; break;
9295 case 'l': case 'L': wrong = "locale"; break;
9296 }
9297 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9298 idx = rb_enc_find_index(name);
9299 if (idx < 0) {
9300 unknown:
9301 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9302 error:
9303 excargs[0] = rb_eArgError;
9304 excargs[2] = rb_make_backtrace();
9305 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9306 VALUE exc = rb_make_exception(3, excargs);
9307 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9308
9309 rb_ast_free(p->ast);
9310 p->ast = NULL;
9311
9312 rb_exc_raise(exc);
9313 }
9314 enc = rb_enc_from_index(idx);
9315 if (!rb_enc_asciicompat(enc)) {
9316 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9317 goto error;
9318 }
9319 p->enc = enc;
9320#ifndef RIPPER
9321 if (p->debug_lines) {
9322 long i;
9323 for (i = 0; i < p->debug_lines->len; i++) {
9324 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9325 }
9326 }
9327#endif
9328}
9329
9330static bool
9331comment_at_top(struct parser_params *p)
9332{
9333 if (p->token_seen) return false;
9334 return (p->line_count == (p->has_shebang ? 2 : 1));
9335}
9336
9337typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9338typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9339
9340static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9341
9342static void
9343magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9344{
9345 if (!comment_at_top(p)) {
9346 return;
9347 }
9348 parser_set_encode(p, val);
9349}
9350
9351static int
9352parser_get_bool(struct parser_params *p, const char *name, const char *val)
9353{
9354 switch (*val) {
9355 case 't': case 'T':
9356 if (STRCASECMP(val, "true") == 0) {
9357 return TRUE;
9358 }
9359 break;
9360 case 'f': case 'F':
9361 if (STRCASECMP(val, "false") == 0) {
9362 return FALSE;
9363 }
9364 break;
9365 }
9366 return parser_invalid_pragma_value(p, name, val);
9367}
9368
9369static int
9370parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9371{
9372 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9373 return -1;
9374}
9375
9376static void
9377parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9378{
9379 int b = parser_get_bool(p, name, val);
9380 if (b >= 0) p->token_info_enabled = b;
9381}
9382
9383static void
9384parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9385{
9386 int b;
9387
9388 if (p->token_seen) {
9389 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9390 return;
9391 }
9392
9393 b = parser_get_bool(p, name, val);
9394 if (b < 0) return;
9395
9396 p->frozen_string_literal = b;
9397}
9398
9399static void
9400parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9401{
9402 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9403 if (*s == ' ' || *s == '\t') continue;
9404 if (*s == '#') break;
9405 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9406 return;
9407 }
9408
9409 switch (*val) {
9410 case 'n': case 'N':
9411 if (STRCASECMP(val, "none") == 0) {
9412 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9413 return;
9414 }
9415 break;
9416 case 'l': case 'L':
9417 if (STRCASECMP(val, "literal") == 0) {
9418 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9419 return;
9420 }
9421 break;
9422 case 'e': case 'E':
9423 if (STRCASECMP(val, "experimental_copy") == 0) {
9424 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9425 return;
9426 }
9427 if (STRCASECMP(val, "experimental_everything") == 0) {
9428 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9429 return;
9430 }
9431 break;
9432 }
9433 parser_invalid_pragma_value(p, name, val);
9434}
9435
9436# if WARN_PAST_SCOPE
9437static void
9438parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9439{
9440 int b = parser_get_bool(p, name, val);
9441 if (b >= 0) p->past_scope_enabled = b;
9442}
9443# endif
9444
9445struct magic_comment {
9446 const char *name;
9447 rb_magic_comment_setter_t func;
9448 rb_magic_comment_length_t length;
9449};
9450
9451static const struct magic_comment magic_comments[] = {
9452 {"coding", magic_comment_encoding, parser_encode_length},
9453 {"encoding", magic_comment_encoding, parser_encode_length},
9454 {"frozen_string_literal", parser_set_frozen_string_literal},
9455 {"shareable_constant_value", parser_set_shareable_constant_value},
9456 {"warn_indent", parser_set_token_info},
9457# if WARN_PAST_SCOPE
9458 {"warn_past_scope", parser_set_past_scope},
9459# endif
9460};
9461
9462static const char *
9463magic_comment_marker(const char *str, long len)
9464{
9465 long i = 2;
9466
9467 while (i < len) {
9468 switch (str[i]) {
9469 case '-':
9470 if (str[i-1] == '*' && str[i-2] == '-') {
9471 return str + i + 1;
9472 }
9473 i += 2;
9474 break;
9475 case '*':
9476 if (i + 1 >= len) return 0;
9477 if (str[i+1] != '-') {
9478 i += 4;
9479 }
9480 else if (str[i-1] != '-') {
9481 i += 2;
9482 }
9483 else {
9484 return str + i + 2;
9485 }
9486 break;
9487 default:
9488 i += 3;
9489 break;
9490 }
9491 }
9492 return 0;
9493}
9494
9495static int
9496parser_magic_comment(struct parser_params *p, const char *str, long len)
9497{
9498 int indicator = 0;
9499 VALUE name = 0, val = 0;
9500 const char *beg, *end, *vbeg, *vend;
9501#define str_copy(_s, _p, _n) ((_s) \
9502 ? (void)(rb_str_resize((_s), (_n)), \
9503 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9504 : (void)((_s) = STR_NEW((_p), (_n))))
9505
9506 if (len <= 7) return FALSE;
9507 if (!!(beg = magic_comment_marker(str, len))) {
9508 if (!(end = magic_comment_marker(beg, str + len - beg)))
9509 return FALSE;
9510 indicator = TRUE;
9511 str = beg;
9512 len = end - beg - 3;
9513 }
9514
9515 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9516 while (len > 0) {
9517 const struct magic_comment *mc = magic_comments;
9518 char *s;
9519 int i;
9520 long n = 0;
9521
9522 for (; len > 0 && *str; str++, --len) {
9523 switch (*str) {
9524 case '\'': case '"': case ':': case ';':
9525 continue;
9526 }
9527 if (!ISSPACE(*str)) break;
9528 }
9529 for (beg = str; len > 0; str++, --len) {
9530 switch (*str) {
9531 case '\'': case '"': case ':': case ';':
9532 break;
9533 default:
9534 if (ISSPACE(*str)) break;
9535 continue;
9536 }
9537 break;
9538 }
9539 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9540 if (!len) break;
9541 if (*str != ':') {
9542 if (!indicator) return FALSE;
9543 continue;
9544 }
9545
9546 do str++; while (--len > 0 && ISSPACE(*str));
9547 if (!len) break;
9548 const char *tok_beg = str;
9549 if (*str == '"') {
9550 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9551 if (*str == '\\') {
9552 --len;
9553 ++str;
9554 }
9555 }
9556 vend = str;
9557 if (len) {
9558 --len;
9559 ++str;
9560 }
9561 }
9562 else {
9563 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9564 vend = str;
9565 }
9566 const char *tok_end = str;
9567 if (indicator) {
9568 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9569 }
9570 else {
9571 while (len > 0 && (ISSPACE(*str))) --len, str++;
9572 if (len) return FALSE;
9573 }
9574
9575 n = end - beg;
9576 str_copy(name, beg, n);
9577 s = RSTRING_PTR(name);
9578 for (i = 0; i < n; ++i) {
9579 if (s[i] == '-') s[i] = '_';
9580 }
9581 do {
9582 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9583 n = vend - vbeg;
9584 if (mc->length) {
9585 n = (*mc->length)(p, vbeg, n);
9586 }
9587 str_copy(val, vbeg, n);
9588 p->lex.ptok = tok_beg;
9589 p->lex.pcur = tok_end;
9590 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9591 break;
9592 }
9593 } while (++mc < magic_comments + numberof(magic_comments));
9594#ifdef RIPPER
9595 str_copy(val, vbeg, vend - vbeg);
9596 dispatch2(magic_comment, name, val);
9597#endif
9598 }
9599
9600 return TRUE;
9601}
9602
9603static void
9604set_file_encoding(struct parser_params *p, const char *str, const char *send)
9605{
9606 int sep = 0;
9607 const char *beg = str;
9608 VALUE s;
9609
9610 for (;;) {
9611 if (send - str <= 6) return;
9612 switch (str[6]) {
9613 case 'C': case 'c': str += 6; continue;
9614 case 'O': case 'o': str += 5; continue;
9615 case 'D': case 'd': str += 4; continue;
9616 case 'I': case 'i': str += 3; continue;
9617 case 'N': case 'n': str += 2; continue;
9618 case 'G': case 'g': str += 1; continue;
9619 case '=': case ':':
9620 sep = 1;
9621 str += 6;
9622 break;
9623 default:
9624 str += 6;
9625 if (ISSPACE(*str)) break;
9626 continue;
9627 }
9628 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9629 sep = 0;
9630 }
9631 for (;;) {
9632 do {
9633 if (++str >= send) return;
9634 } while (ISSPACE(*str));
9635 if (sep) break;
9636 if (*str != '=' && *str != ':') return;
9637 sep = 1;
9638 str++;
9639 }
9640 beg = str;
9641 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9642 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9643 p->lex.ptok = beg;
9644 p->lex.pcur = str;
9645 parser_set_encode(p, RSTRING_PTR(s));
9646 rb_str_resize(s, 0);
9647}
9648
9649static void
9650parser_prepare(struct parser_params *p)
9651{
9652 int c = nextc0(p, FALSE);
9653 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9654 switch (c) {
9655 case '#':
9656 if (peek(p, '!')) p->has_shebang = 1;
9657 break;
9658 case 0xef: /* UTF-8 BOM marker */
9659 if (!lex_eol_n_p(p, 2) &&
9660 (unsigned char)p->lex.pcur[0] == 0xbb &&
9661 (unsigned char)p->lex.pcur[1] == 0xbf) {
9662 p->enc = rb_utf8_encoding();
9663 p->lex.pcur += 2;
9664#ifndef RIPPER
9665 if (p->debug_lines) {
9666 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9667 }
9668#endif
9669 p->lex.pbeg = p->lex.pcur;
9670 token_flush(p);
9671 return;
9672 }
9673 break;
9674 case -1: /* end of script. */
9675 return;
9676 }
9677 pushback(p, c);
9678 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9679}
9680
9681#ifndef RIPPER
9682#define ambiguous_operator(tok, op, syn) ( \
9683 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9684 rb_warning0("even though it seems like "syn""))
9685#else
9686#define ambiguous_operator(tok, op, syn) \
9687 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9688#endif
9689#define warn_balanced(tok, op, syn) ((void) \
9690 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9691 space_seen && !ISSPACE(c) && \
9692 (ambiguous_operator(tok, op, syn), 0)), \
9693 (enum yytokentype)(tok))
9694
9695static enum yytokentype
9696no_digits(struct parser_params *p)
9697{
9698 yyerror0("numeric literal without digits");
9699 if (peek(p, '_')) nextc(p);
9700 /* dummy 0, for tUMINUS_NUM at numeric */
9701 return set_number_literal(p, tINTEGER, 0, 10, 0);
9702}
9703
9704static enum yytokentype
9705parse_numeric(struct parser_params *p, int c)
9706{
9707 int is_float, seen_point, seen_e, nondigit;
9708 int suffix;
9709
9710 is_float = seen_point = seen_e = nondigit = 0;
9711 SET_LEX_STATE(EXPR_END);
9712 newtok(p);
9713 if (c == '-' || c == '+') {
9714 tokadd(p, c);
9715 c = nextc(p);
9716 }
9717 if (c == '0') {
9718 int start = toklen(p);
9719 c = nextc(p);
9720 if (c == 'x' || c == 'X') {
9721 /* hexadecimal */
9722 c = nextc(p);
9723 if (c != -1 && ISXDIGIT(c)) {
9724 do {
9725 if (c == '_') {
9726 if (nondigit) break;
9727 nondigit = c;
9728 continue;
9729 }
9730 if (!ISXDIGIT(c)) break;
9731 nondigit = 0;
9732 tokadd(p, c);
9733 } while ((c = nextc(p)) != -1);
9734 }
9735 pushback(p, c);
9736 tokfix(p);
9737 if (toklen(p) == start) {
9738 return no_digits(p);
9739 }
9740 else if (nondigit) goto trailing_uc;
9741 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9742 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9743 }
9744 if (c == 'b' || c == 'B') {
9745 /* binary */
9746 c = nextc(p);
9747 if (c == '0' || c == '1') {
9748 do {
9749 if (c == '_') {
9750 if (nondigit) break;
9751 nondigit = c;
9752 continue;
9753 }
9754 if (c != '0' && c != '1') break;
9755 nondigit = 0;
9756 tokadd(p, c);
9757 } while ((c = nextc(p)) != -1);
9758 }
9759 pushback(p, c);
9760 tokfix(p);
9761 if (toklen(p) == start) {
9762 return no_digits(p);
9763 }
9764 else if (nondigit) goto trailing_uc;
9765 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9766 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9767 }
9768 if (c == 'd' || c == 'D') {
9769 /* decimal */
9770 c = nextc(p);
9771 if (c != -1 && ISDIGIT(c)) {
9772 do {
9773 if (c == '_') {
9774 if (nondigit) break;
9775 nondigit = c;
9776 continue;
9777 }
9778 if (!ISDIGIT(c)) break;
9779 nondigit = 0;
9780 tokadd(p, c);
9781 } while ((c = nextc(p)) != -1);
9782 }
9783 pushback(p, c);
9784 tokfix(p);
9785 if (toklen(p) == start) {
9786 return no_digits(p);
9787 }
9788 else if (nondigit) goto trailing_uc;
9789 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9790 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9791 }
9792 if (c == '_') {
9793 /* 0_0 */
9794 goto octal_number;
9795 }
9796 if (c == 'o' || c == 'O') {
9797 /* prefixed octal */
9798 c = nextc(p);
9799 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9800 tokfix(p);
9801 return no_digits(p);
9802 }
9803 }
9804 if (c >= '0' && c <= '7') {
9805 /* octal */
9806 octal_number:
9807 do {
9808 if (c == '_') {
9809 if (nondigit) break;
9810 nondigit = c;
9811 continue;
9812 }
9813 if (c < '0' || c > '9') break;
9814 if (c > '7') goto invalid_octal;
9815 nondigit = 0;
9816 tokadd(p, c);
9817 } while ((c = nextc(p)) != -1);
9818 if (toklen(p) > start) {
9819 pushback(p, c);
9820 tokfix(p);
9821 if (nondigit) goto trailing_uc;
9822 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9823 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9824 }
9825 if (nondigit) {
9826 pushback(p, c);
9827 goto trailing_uc;
9828 }
9829 }
9830 if (c > '7' && c <= '9') {
9831 invalid_octal:
9832 yyerror0("Invalid octal digit");
9833 }
9834 else if (c == '.' || c == 'e' || c == 'E') {
9835 tokadd(p, '0');
9836 }
9837 else {
9838 pushback(p, c);
9839 tokfix(p);
9840 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9841 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9842 }
9843 }
9844
9845 for (;;) {
9846 switch (c) {
9847 case '0': case '1': case '2': case '3': case '4':
9848 case '5': case '6': case '7': case '8': case '9':
9849 nondigit = 0;
9850 tokadd(p, c);
9851 break;
9852
9853 case '.':
9854 if (nondigit) goto trailing_uc;
9855 if (seen_point || seen_e) {
9856 goto decode_num;
9857 }
9858 else {
9859 int c0 = nextc(p);
9860 if (c0 == -1 || !ISDIGIT(c0)) {
9861 pushback(p, c0);
9862 goto decode_num;
9863 }
9864 c = c0;
9865 }
9866 seen_point = toklen(p);
9867 tokadd(p, '.');
9868 tokadd(p, c);
9869 is_float++;
9870 nondigit = 0;
9871 break;
9872
9873 case 'e':
9874 case 'E':
9875 if (nondigit) {
9876 pushback(p, c);
9877 c = nondigit;
9878 goto decode_num;
9879 }
9880 if (seen_e) {
9881 goto decode_num;
9882 }
9883 nondigit = c;
9884 c = nextc(p);
9885 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9886 pushback(p, c);
9887 c = nondigit;
9888 nondigit = 0;
9889 goto decode_num;
9890 }
9891 tokadd(p, nondigit);
9892 seen_e++;
9893 is_float++;
9894 tokadd(p, c);
9895 nondigit = (c == '-' || c == '+') ? c : 0;
9896 break;
9897
9898 case '_': /* `_' in number just ignored */
9899 if (nondigit) goto decode_num;
9900 nondigit = c;
9901 break;
9902
9903 default:
9904 goto decode_num;
9905 }
9906 c = nextc(p);
9907 }
9908
9909 decode_num:
9910 pushback(p, c);
9911 if (nondigit) {
9912 trailing_uc:
9913 literal_flush(p, p->lex.pcur - 1);
9914 YYLTYPE loc = RUBY_INIT_YYLLOC();
9915 compile_error(p, "trailing '%c' in number", nondigit);
9916 parser_show_error_line(p, &loc);
9917 }
9918 tokfix(p);
9919 if (is_float) {
9920 enum yytokentype type = tFLOAT;
9921
9922 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9923 if (suffix & NUM_SUFFIX_R) {
9924 type = tRATIONAL;
9925 }
9926 else {
9927 errno = 0;
9928 strtod(tok(p), 0);
9929 if (errno == ERANGE) {
9930 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9931 errno = 0;
9932 }
9933 }
9934 return set_number_literal(p, type, suffix, 0, seen_point);
9935 }
9936 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9937 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9938}
9939
9940static enum yytokentype
9941parse_qmark(struct parser_params *p, int space_seen)
9942{
9943 rb_encoding *enc;
9944 register int c;
9945 rb_parser_string_t *lit;
9946 const char *start = p->lex.pcur;
9947
9948 if (IS_END()) {
9949 SET_LEX_STATE(EXPR_VALUE);
9950 return '?';
9951 }
9952 c = nextc(p);
9953 if (c == -1) {
9954 compile_error(p, "incomplete character syntax");
9955 return 0;
9956 }
9957 if (rb_enc_isspace(c, p->enc)) {
9958 if (!IS_ARG()) {
9959 int c2 = escaped_control_code(c);
9960 if (c2) {
9961 WARN_SPACE_CHAR(c2, "?");
9962 }
9963 }
9964 ternary:
9965 pushback(p, c);
9966 SET_LEX_STATE(EXPR_VALUE);
9967 return '?';
9968 }
9969 newtok(p);
9970 enc = p->enc;
9971 int w = parser_precise_mbclen(p, start);
9972 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9973 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9974 if (space_seen) {
9975 const char *ptr = start;
9976 do {
9977 int n = parser_precise_mbclen(p, ptr);
9978 if (n < 0) return -1;
9979 ptr += n;
9980 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9981 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9982 " a conditional operator, put a space after '?'",
9983 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9984 }
9985 goto ternary;
9986 }
9987 else if (c == '\\') {
9988 if (peek(p, 'u')) {
9989 nextc(p);
9990 enc = rb_utf8_encoding();
9991 tokadd_utf8(p, &enc, -1, 0, 0);
9992 }
9993 else if (!ISASCII(c = peekc(p)) && c != -1) {
9994 nextc(p);
9995 if (tokadd_mbchar(p, c) == -1) return 0;
9996 }
9997 else {
9998 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9999 tokadd(p, c);
10000 }
10001 }
10002 else {
10003 if (tokadd_mbchar(p, c) == -1) return 0;
10004 }
10005 tokfix(p);
10006 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10007 set_yylval_str(lit);
10008 SET_LEX_STATE(EXPR_END);
10009 return tCHAR;
10010}
10011
10012static enum yytokentype
10013parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10014{
10015 register int c;
10016 const char *ptok = p->lex.pcur;
10017
10018 if (IS_BEG()) {
10019 int term;
10020 int paren;
10021
10022 c = nextc(p);
10023 quotation:
10024 if (c == -1) goto unterminated;
10025 if (!ISALNUM(c)) {
10026 term = c;
10027 if (!ISASCII(c)) goto unknown;
10028 c = 'Q';
10029 }
10030 else {
10031 term = nextc(p);
10032 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10033 unknown:
10034 pushback(p, term);
10035 c = parser_precise_mbclen(p, p->lex.pcur);
10036 if (c < 0) return 0;
10037 p->lex.pcur += c;
10038 yyerror0("unknown type of %string");
10039 return 0;
10040 }
10041 }
10042 if (term == -1) {
10043 unterminated:
10044 compile_error(p, "unterminated quoted string meets end of file");
10045 return 0;
10046 }
10047 paren = term;
10048 if (term == '(') term = ')';
10049 else if (term == '[') term = ']';
10050 else if (term == '{') term = '}';
10051 else if (term == '<') term = '>';
10052 else paren = 0;
10053
10054 p->lex.ptok = ptok-1;
10055 switch (c) {
10056 case 'Q':
10057 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10058 return tSTRING_BEG;
10059
10060 case 'q':
10061 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10062 return tSTRING_BEG;
10063
10064 case 'W':
10065 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10066 return tWORDS_BEG;
10067
10068 case 'w':
10069 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10070 return tQWORDS_BEG;
10071
10072 case 'I':
10073 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10074 return tSYMBOLS_BEG;
10075
10076 case 'i':
10077 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10078 return tQSYMBOLS_BEG;
10079
10080 case 'x':
10081 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10082 return tXSTRING_BEG;
10083
10084 case 'r':
10085 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10086 return tREGEXP_BEG;
10087
10088 case 's':
10089 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10090 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10091 return tSYMBEG;
10092
10093 default:
10094 yyerror0("unknown type of %string");
10095 return 0;
10096 }
10097 }
10098 if ((c = nextc(p)) == '=') {
10099 set_yylval_id('%');
10100 SET_LEX_STATE(EXPR_BEG);
10101 return tOP_ASGN;
10102 }
10103 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10104 goto quotation;
10105 }
10106 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10107 pushback(p, c);
10108 return warn_balanced('%', "%%", "string literal");
10109}
10110
10111static int
10112tokadd_ident(struct parser_params *p, int c)
10113{
10114 do {
10115 if (tokadd_mbchar(p, c) == -1) return -1;
10116 c = nextc(p);
10117 } while (parser_is_identchar(p));
10118 pushback(p, c);
10119 return 0;
10120}
10121
10122static ID
10123tokenize_ident(struct parser_params *p)
10124{
10125 ID ident = TOK_INTERN();
10126
10127 set_yylval_name(ident);
10128
10129 return ident;
10130}
10131
10132static int
10133parse_numvar(struct parser_params *p)
10134{
10135 size_t len;
10136 int overflow;
10137 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10138 const unsigned long nth_ref_max =
10139 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10140 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10141 * turned into a Fixnum, in compile.c */
10142
10143 if (overflow || n > nth_ref_max) {
10144 /* compile_error()? */
10145 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10146 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10147 }
10148 else {
10149 return (int)n;
10150 }
10151}
10152
10153static enum yytokentype
10154parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10155{
10156 const char *ptr = p->lex.pcur;
10157 register int c;
10158
10159 SET_LEX_STATE(EXPR_END);
10160 p->lex.ptok = ptr - 1; /* from '$' */
10161 newtok(p);
10162 c = nextc(p);
10163 switch (c) {
10164 case '_': /* $_: last read line string */
10165 c = nextc(p);
10166 if (parser_is_identchar(p)) {
10167 tokadd(p, '$');
10168 tokadd(p, '_');
10169 break;
10170 }
10171 pushback(p, c);
10172 c = '_';
10173 /* fall through */
10174 case '~': /* $~: match-data */
10175 case '*': /* $*: argv */
10176 case '$': /* $$: pid */
10177 case '?': /* $?: last status */
10178 case '!': /* $!: error string */
10179 case '@': /* $@: error position */
10180 case '/': /* $/: input record separator */
10181 case '\\': /* $\: output record separator */
10182 case ';': /* $;: field separator */
10183 case ',': /* $,: output field separator */
10184 case '.': /* $.: last read line number */
10185 case '=': /* $=: ignorecase */
10186 case ':': /* $:: load path */
10187 case '<': /* $<: default input handle */
10188 case '>': /* $>: default output handle */
10189 case '\"': /* $": already loaded files */
10190 tokadd(p, '$');
10191 tokadd(p, c);
10192 goto gvar;
10193
10194 case '-':
10195 tokadd(p, '$');
10196 tokadd(p, c);
10197 c = nextc(p);
10198 if (parser_is_identchar(p)) {
10199 if (tokadd_mbchar(p, c) == -1) return 0;
10200 }
10201 else {
10202 pushback(p, c);
10203 pushback(p, '-');
10204 return '$';
10205 }
10206 gvar:
10207 tokenize_ident(p);
10208 return tGVAR;
10209
10210 case '&': /* $&: last match */
10211 case '`': /* $`: string before last match */
10212 case '\'': /* $': string after last match */
10213 case '+': /* $+: string matches last paren. */
10214 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10215 tokadd(p, '$');
10216 tokadd(p, c);
10217 goto gvar;
10218 }
10219 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10220 return tBACK_REF;
10221
10222 case '1': case '2': case '3':
10223 case '4': case '5': case '6':
10224 case '7': case '8': case '9':
10225 tokadd(p, '$');
10226 do {
10227 tokadd(p, c);
10228 c = nextc(p);
10229 } while (c != -1 && ISDIGIT(c));
10230 pushback(p, c);
10231 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10232 tokfix(p);
10233 c = parse_numvar(p);
10234 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10235 return tNTH_REF;
10236
10237 default:
10238 if (!parser_is_identchar(p)) {
10239 YYLTYPE loc = RUBY_INIT_YYLLOC();
10240 if (c == -1 || ISSPACE(c)) {
10241 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10242 }
10243 else {
10244 pushback(p, c);
10245 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10246 }
10247 parser_show_error_line(p, &loc);
10248 set_yylval_noname();
10249 return tGVAR;
10250 }
10251 /* fall through */
10252 case '0':
10253 tokadd(p, '$');
10254 }
10255
10256 if (tokadd_ident(p, c)) return 0;
10257 SET_LEX_STATE(EXPR_END);
10258 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10259 tokenize_ident(p);
10260 }
10261 else {
10262 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10263 set_yylval_noname();
10264 }
10265 return tGVAR;
10266}
10267
10268static bool
10269parser_numbered_param(struct parser_params *p, int n)
10270{
10271 if (n < 0) return false;
10272
10273 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10274 return false;
10275 }
10276 if (p->max_numparam == ORDINAL_PARAM) {
10277 compile_error(p, "ordinary parameter is defined");
10278 return false;
10279 }
10280 struct vtable *args = p->lvtbl->args;
10281 if (p->max_numparam < n) {
10282 p->max_numparam = n;
10283 }
10284 while (n > args->pos) {
10285 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10286 }
10287 return true;
10288}
10289
10290static enum yytokentype
10291parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10292{
10293 const char *ptr = p->lex.pcur;
10294 enum yytokentype result = tIVAR;
10295 register int c = nextc(p);
10296 YYLTYPE loc;
10297
10298 p->lex.ptok = ptr - 1; /* from '@' */
10299 newtok(p);
10300 tokadd(p, '@');
10301 if (c == '@') {
10302 result = tCVAR;
10303 tokadd(p, '@');
10304 c = nextc(p);
10305 }
10306 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10307 if (c == -1 || !parser_is_identchar(p)) {
10308 pushback(p, c);
10309 RUBY_SET_YYLLOC(loc);
10310 if (result == tIVAR) {
10311 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10312 }
10313 else {
10314 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10315 }
10316 parser_show_error_line(p, &loc);
10317 set_yylval_noname();
10318 SET_LEX_STATE(EXPR_END);
10319 return result;
10320 }
10321 else if (ISDIGIT(c)) {
10322 pushback(p, c);
10323 RUBY_SET_YYLLOC(loc);
10324 if (result == tIVAR) {
10325 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10326 }
10327 else {
10328 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10329 }
10330 parser_show_error_line(p, &loc);
10331 set_yylval_noname();
10332 SET_LEX_STATE(EXPR_END);
10333 return result;
10334 }
10335
10336 if (tokadd_ident(p, c)) return 0;
10337 tokenize_ident(p);
10338 return result;
10339}
10340
10341static enum yytokentype
10342parse_ident(struct parser_params *p, int c, int cmd_state)
10343{
10344 enum yytokentype result;
10345 bool is_ascii = true;
10346 const enum lex_state_e last_state = p->lex.state;
10347 ID ident;
10348 int enforce_keyword_end = 0;
10349
10350 do {
10351 if (!ISASCII(c)) is_ascii = false;
10352 if (tokadd_mbchar(p, c) == -1) return 0;
10353 c = nextc(p);
10354 } while (parser_is_identchar(p));
10355 if ((c == '!' || c == '?') && !peek(p, '=')) {
10356 result = tFID;
10357 tokadd(p, c);
10358 }
10359 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10360 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10361 result = tIDENTIFIER;
10362 tokadd(p, c);
10363 }
10364 else {
10365 result = tCONSTANT; /* assume provisionally */
10366 pushback(p, c);
10367 }
10368 tokfix(p);
10369
10370 if (IS_LABEL_POSSIBLE()) {
10371 if (IS_LABEL_SUFFIX(0)) {
10372 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10373 nextc(p);
10374 tokenize_ident(p);
10375 return tLABEL;
10376 }
10377 }
10378
10379#ifndef RIPPER
10380 if (peek_end_expect_token_locations(p)) {
10381 const rb_code_position_t *end_pos;
10382 int lineno, column;
10383 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
10384
10385 end_pos = peek_end_expect_token_locations(p)->pos;
10386 lineno = end_pos->lineno;
10387 column = end_pos->column;
10388
10389 if (p->debug) {
10390 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10391 p->ruby_sourceline, beg_pos, lineno, column);
10392 }
10393
10394 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10395 const struct kwtable *kw;
10396
10397 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10398 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10399 enforce_keyword_end = 1;
10400 }
10401 }
10402 }
10403#endif
10404
10405 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10406 const struct kwtable *kw;
10407
10408 /* See if it is a reserved word. */
10409 kw = rb_reserved_word(tok(p), toklen(p));
10410 if (kw) {
10411 enum lex_state_e state = p->lex.state;
10412 if (IS_lex_state_for(state, EXPR_FNAME)) {
10413 SET_LEX_STATE(EXPR_ENDFN);
10414 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10415 return kw->id[0];
10416 }
10417 SET_LEX_STATE(kw->state);
10418 if (IS_lex_state(EXPR_BEG)) {
10419 p->command_start = TRUE;
10420 }
10421 if (kw->id[0] == keyword_do) {
10422 if (lambda_beginning_p()) {
10423 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10424 return keyword_do_LAMBDA;
10425 }
10426 if (COND_P()) return keyword_do_cond;
10427 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10428 return keyword_do_block;
10429 return keyword_do;
10430 }
10431 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10432 return kw->id[0];
10433 else {
10434 if (kw->id[0] != kw->id[1])
10435 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10436 return kw->id[1];
10437 }
10438 }
10439 }
10440
10441 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10442 if (cmd_state) {
10443 SET_LEX_STATE(EXPR_CMDARG);
10444 }
10445 else {
10446 SET_LEX_STATE(EXPR_ARG);
10447 }
10448 }
10449 else if (p->lex.state == EXPR_FNAME) {
10450 SET_LEX_STATE(EXPR_ENDFN);
10451 }
10452 else {
10453 SET_LEX_STATE(EXPR_END);
10454 }
10455
10456 ident = tokenize_ident(p);
10457 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10458 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10459 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10460 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10461 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10462 }
10463 return result;
10464}
10465
10466static void
10467warn_cr(struct parser_params *p)
10468{
10469 if (!p->cr_seen) {
10470 p->cr_seen = TRUE;
10471 /* carried over with p->lex.nextline for nextc() */
10472 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10473 }
10474}
10475
10476static enum yytokentype
10477parser_yylex(struct parser_params *p)
10478{
10479 register int c;
10480 int space_seen = 0;
10481 int cmd_state;
10482 int label;
10483 enum lex_state_e last_state;
10484 int fallthru = FALSE;
10485 int token_seen = p->token_seen;
10486
10487 if (p->lex.strterm) {
10488 if (strterm_is_heredoc(p->lex.strterm)) {
10489 token_flush(p);
10490 return here_document(p, &p->lex.strterm->u.heredoc);
10491 }
10492 else {
10493 token_flush(p);
10494 return parse_string(p, &p->lex.strterm->u.literal);
10495 }
10496 }
10497 cmd_state = p->command_start;
10498 p->command_start = FALSE;
10499 p->token_seen = TRUE;
10500#ifndef RIPPER
10501 token_flush(p);
10502#endif
10503 retry:
10504 last_state = p->lex.state;
10505 switch (c = nextc(p)) {
10506 case '\0': /* NUL */
10507 case '\004': /* ^D */
10508 case '\032': /* ^Z */
10509 case -1: /* end of script. */
10510 p->eofp = 1;
10511#ifndef RIPPER
10512 if (p->end_expect_token_locations) {
10513 pop_end_expect_token_locations(p);
10514 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10515 return tDUMNY_END;
10516 }
10517#endif
10518 /* Set location for end-of-input because dispatch_scan_event is not called. */
10519 RUBY_SET_YYLLOC(*p->yylloc);
10520 return END_OF_INPUT;
10521
10522 /* white spaces */
10523 case '\r':
10524 warn_cr(p);
10525 /* fall through */
10526 case ' ': case '\t': case '\f':
10527 case '\13': /* '\v' */
10528 space_seen = 1;
10529 while ((c = nextc(p))) {
10530 switch (c) {
10531 case '\r':
10532 warn_cr(p);
10533 /* fall through */
10534 case ' ': case '\t': case '\f':
10535 case '\13': /* '\v' */
10536 break;
10537 default:
10538 goto outofloop;
10539 }
10540 }
10541 outofloop:
10542 pushback(p, c);
10543 dispatch_scan_event(p, tSP);
10544#ifndef RIPPER
10545 token_flush(p);
10546#endif
10547 goto retry;
10548
10549 case '#': /* it's a comment */
10550 p->token_seen = token_seen;
10551 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10552 /* no magic_comment in shebang line */
10553 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10554 if (comment_at_top(p)) {
10555 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10556 }
10557 }
10558 p->lex.pcur = pcur, p->lex.ptok = ptok;
10559 lex_goto_eol(p);
10560 dispatch_scan_event(p, tCOMMENT);
10561 fallthru = TRUE;
10562 /* fall through */
10563 case '\n':
10564 p->token_seen = token_seen;
10565 rb_parser_string_t *prevline = p->lex.lastline;
10566 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10567 !IS_lex_state(EXPR_LABELED));
10568 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10569 if (!fallthru) {
10570 dispatch_scan_event(p, tIGNORED_NL);
10571 }
10572 fallthru = FALSE;
10573 if (!c && p->ctxt.in_kwarg) {
10574 goto normal_newline;
10575 }
10576 goto retry;
10577 }
10578 while (1) {
10579 switch (c = nextc(p)) {
10580 case ' ': case '\t': case '\f': case '\r':
10581 case '\13': /* '\v' */
10582 space_seen = 1;
10583 break;
10584 case '#':
10585 pushback(p, c);
10586 if (space_seen) {
10587 dispatch_scan_event(p, tSP);
10588 token_flush(p);
10589 }
10590 goto retry;
10591 case 'a':
10592 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10593 goto bol;
10594 case 'o':
10595 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10596 goto bol;
10597 case '|':
10598 if (peek(p, '|')) goto leading_logical;
10599 goto bol;
10600 case '&':
10601 if (peek(p, '&')) {
10602 leading_logical:
10603 pushback(p, c);
10604 dispatch_delayed_token(p, tIGNORED_NL);
10605 cmd_state = FALSE;
10606 goto retry;
10607 }
10608 /* fall through */
10609 case '.': {
10610 dispatch_delayed_token(p, tIGNORED_NL);
10611 if (peek(p, '.') == (c == '&')) {
10612 pushback(p, c);
10613 dispatch_scan_event(p, tSP);
10614 goto retry;
10615 }
10616 }
10617 bol:
10618 default:
10619 p->ruby_sourceline--;
10620 p->lex.nextline = p->lex.lastline;
10621 set_lastline(p, prevline);
10622 case -1: /* EOF no decrement*/
10623 if (c == -1 && space_seen) {
10624 dispatch_scan_event(p, tSP);
10625 }
10626 lex_goto_eol(p);
10627 if (c != -1) {
10628 token_flush(p);
10629 RUBY_SET_YYLLOC(*p->yylloc);
10630 }
10631 goto normal_newline;
10632 }
10633 }
10634 normal_newline:
10635 p->command_start = TRUE;
10636 SET_LEX_STATE(EXPR_BEG);
10637 return '\n';
10638
10639 case '*':
10640 if ((c = nextc(p)) == '*') {
10641 if ((c = nextc(p)) == '=') {
10642 set_yylval_id(idPow);
10643 SET_LEX_STATE(EXPR_BEG);
10644 return tOP_ASGN;
10645 }
10646 pushback(p, c);
10647 if (IS_SPCARG(c)) {
10648 rb_warning0("'**' interpreted as argument prefix");
10649 c = tDSTAR;
10650 }
10651 else if (IS_BEG()) {
10652 c = tDSTAR;
10653 }
10654 else {
10655 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10656 }
10657 }
10658 else {
10659 if (c == '=') {
10660 set_yylval_id('*');
10661 SET_LEX_STATE(EXPR_BEG);
10662 return tOP_ASGN;
10663 }
10664 pushback(p, c);
10665 if (IS_SPCARG(c)) {
10666 rb_warning0("'*' interpreted as argument prefix");
10667 c = tSTAR;
10668 }
10669 else if (IS_BEG()) {
10670 c = tSTAR;
10671 }
10672 else {
10673 c = warn_balanced('*', "*", "argument prefix");
10674 }
10675 }
10676 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10677 return c;
10678
10679 case '!':
10680 c = nextc(p);
10681 if (IS_AFTER_OPERATOR()) {
10682 SET_LEX_STATE(EXPR_ARG);
10683 if (c == '@') {
10684 return '!';
10685 }
10686 }
10687 else {
10688 SET_LEX_STATE(EXPR_BEG);
10689 }
10690 if (c == '=') {
10691 return tNEQ;
10692 }
10693 if (c == '~') {
10694 return tNMATCH;
10695 }
10696 pushback(p, c);
10697 return '!';
10698
10699 case '=':
10700 if (was_bol(p)) {
10701 /* skip embedded rd document */
10702 if (word_match_p(p, "begin", 5)) {
10703 int first_p = TRUE;
10704
10705 lex_goto_eol(p);
10706 dispatch_scan_event(p, tEMBDOC_BEG);
10707 for (;;) {
10708 lex_goto_eol(p);
10709 if (!first_p) {
10710 dispatch_scan_event(p, tEMBDOC);
10711 }
10712 first_p = FALSE;
10713 c = nextc(p);
10714 if (c == -1) {
10715 compile_error(p, "embedded document meets end of file");
10716 return END_OF_INPUT;
10717 }
10718 if (c == '=' && word_match_p(p, "end", 3)) {
10719 break;
10720 }
10721 pushback(p, c);
10722 }
10723 lex_goto_eol(p);
10724 dispatch_scan_event(p, tEMBDOC_END);
10725 goto retry;
10726 }
10727 }
10728
10729 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10730 if ((c = nextc(p)) == '=') {
10731 if ((c = nextc(p)) == '=') {
10732 return tEQQ;
10733 }
10734 pushback(p, c);
10735 return tEQ;
10736 }
10737 if (c == '~') {
10738 return tMATCH;
10739 }
10740 else if (c == '>') {
10741 return tASSOC;
10742 }
10743 pushback(p, c);
10744 return '=';
10745
10746 case '<':
10747 c = nextc(p);
10748 if (c == '<' &&
10749 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10750 !IS_END() &&
10751 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10752 enum yytokentype token = heredoc_identifier(p);
10753 if (token) return token < 0 ? 0 : token;
10754 }
10755 if (IS_AFTER_OPERATOR()) {
10756 SET_LEX_STATE(EXPR_ARG);
10757 }
10758 else {
10759 if (IS_lex_state(EXPR_CLASS))
10760 p->command_start = TRUE;
10761 SET_LEX_STATE(EXPR_BEG);
10762 }
10763 if (c == '=') {
10764 if ((c = nextc(p)) == '>') {
10765 return tCMP;
10766 }
10767 pushback(p, c);
10768 return tLEQ;
10769 }
10770 if (c == '<') {
10771 if ((c = nextc(p)) == '=') {
10772 set_yylval_id(idLTLT);
10773 SET_LEX_STATE(EXPR_BEG);
10774 return tOP_ASGN;
10775 }
10776 pushback(p, c);
10777 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10778 }
10779 pushback(p, c);
10780 return '<';
10781
10782 case '>':
10783 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10784 if ((c = nextc(p)) == '=') {
10785 return tGEQ;
10786 }
10787 if (c == '>') {
10788 if ((c = nextc(p)) == '=') {
10789 set_yylval_id(idGTGT);
10790 SET_LEX_STATE(EXPR_BEG);
10791 return tOP_ASGN;
10792 }
10793 pushback(p, c);
10794 return tRSHFT;
10795 }
10796 pushback(p, c);
10797 return '>';
10798
10799 case '"':
10800 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10801 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10802 p->lex.ptok = p->lex.pcur-1;
10803 return tSTRING_BEG;
10804
10805 case '`':
10806 if (IS_lex_state(EXPR_FNAME)) {
10807 SET_LEX_STATE(EXPR_ENDFN);
10808 return c;
10809 }
10810 if (IS_lex_state(EXPR_DOT)) {
10811 if (cmd_state)
10812 SET_LEX_STATE(EXPR_CMDARG);
10813 else
10814 SET_LEX_STATE(EXPR_ARG);
10815 return c;
10816 }
10817 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10818 return tXSTRING_BEG;
10819
10820 case '\'':
10821 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10822 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10823 p->lex.ptok = p->lex.pcur-1;
10824 return tSTRING_BEG;
10825
10826 case '?':
10827 return parse_qmark(p, space_seen);
10828
10829 case '&':
10830 if ((c = nextc(p)) == '&') {
10831 SET_LEX_STATE(EXPR_BEG);
10832 if ((c = nextc(p)) == '=') {
10833 set_yylval_id(idANDOP);
10834 SET_LEX_STATE(EXPR_BEG);
10835 return tOP_ASGN;
10836 }
10837 pushback(p, c);
10838 return tANDOP;
10839 }
10840 else if (c == '=') {
10841 set_yylval_id('&');
10842 SET_LEX_STATE(EXPR_BEG);
10843 return tOP_ASGN;
10844 }
10845 else if (c == '.') {
10846 set_yylval_id(idANDDOT);
10847 SET_LEX_STATE(EXPR_DOT);
10848 return tANDDOT;
10849 }
10850 pushback(p, c);
10851 if (IS_SPCARG(c)) {
10852 if ((c != ':') ||
10853 (c = peekc_n(p, 1)) == -1 ||
10854 !(c == '\'' || c == '"' ||
10855 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10856 rb_warning0("'&' interpreted as argument prefix");
10857 }
10858 c = tAMPER;
10859 }
10860 else if (IS_BEG()) {
10861 c = tAMPER;
10862 }
10863 else {
10864 c = warn_balanced('&', "&", "argument prefix");
10865 }
10866 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10867 return c;
10868
10869 case '|':
10870 if ((c = nextc(p)) == '|') {
10871 SET_LEX_STATE(EXPR_BEG);
10872 if ((c = nextc(p)) == '=') {
10873 set_yylval_id(idOROP);
10874 SET_LEX_STATE(EXPR_BEG);
10875 return tOP_ASGN;
10876 }
10877 pushback(p, c);
10878 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10879 c = '|';
10880 pushback(p, '|');
10881 return c;
10882 }
10883 return tOROP;
10884 }
10885 if (c == '=') {
10886 set_yylval_id('|');
10887 SET_LEX_STATE(EXPR_BEG);
10888 return tOP_ASGN;
10889 }
10890 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10891 pushback(p, c);
10892 return '|';
10893
10894 case '+':
10895 c = nextc(p);
10896 if (IS_AFTER_OPERATOR()) {
10897 SET_LEX_STATE(EXPR_ARG);
10898 if (c == '@') {
10899 return tUPLUS;
10900 }
10901 pushback(p, c);
10902 return '+';
10903 }
10904 if (c == '=') {
10905 set_yylval_id('+');
10906 SET_LEX_STATE(EXPR_BEG);
10907 return tOP_ASGN;
10908 }
10909 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10910 SET_LEX_STATE(EXPR_BEG);
10911 pushback(p, c);
10912 if (c != -1 && ISDIGIT(c)) {
10913 return parse_numeric(p, '+');
10914 }
10915 return tUPLUS;
10916 }
10917 SET_LEX_STATE(EXPR_BEG);
10918 pushback(p, c);
10919 return warn_balanced('+', "+", "unary operator");
10920
10921 case '-':
10922 c = nextc(p);
10923 if (IS_AFTER_OPERATOR()) {
10924 SET_LEX_STATE(EXPR_ARG);
10925 if (c == '@') {
10926 return tUMINUS;
10927 }
10928 pushback(p, c);
10929 return '-';
10930 }
10931 if (c == '=') {
10932 set_yylval_id('-');
10933 SET_LEX_STATE(EXPR_BEG);
10934 return tOP_ASGN;
10935 }
10936 if (c == '>') {
10937 SET_LEX_STATE(EXPR_ENDFN);
10938 yylval.num = p->lex.lpar_beg;
10939 p->lex.lpar_beg = p->lex.paren_nest;
10940 return tLAMBDA;
10941 }
10942 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10943 SET_LEX_STATE(EXPR_BEG);
10944 pushback(p, c);
10945 if (c != -1 && ISDIGIT(c)) {
10946 return tUMINUS_NUM;
10947 }
10948 return tUMINUS;
10949 }
10950 SET_LEX_STATE(EXPR_BEG);
10951 pushback(p, c);
10952 return warn_balanced('-', "-", "unary operator");
10953
10954 case '.': {
10955 int is_beg = IS_BEG();
10956 SET_LEX_STATE(EXPR_BEG);
10957 if ((c = nextc(p)) == '.') {
10958 if ((c = nextc(p)) == '.') {
10959 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10960 SET_LEX_STATE(EXPR_ENDARG);
10961 return tBDOT3;
10962 }
10963 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10964 rb_warn0("... at EOL, should be parenthesized?");
10965 }
10966 return is_beg ? tBDOT3 : tDOT3;
10967 }
10968 pushback(p, c);
10969 return is_beg ? tBDOT2 : tDOT2;
10970 }
10971 pushback(p, c);
10972 if (c != -1 && ISDIGIT(c)) {
10973 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10974 parse_numeric(p, '.');
10975 if (ISDIGIT(prev)) {
10976 yyerror0("unexpected fraction part after numeric literal");
10977 }
10978 else {
10979 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10980 }
10981 SET_LEX_STATE(EXPR_END);
10982 p->lex.ptok = p->lex.pcur;
10983 goto retry;
10984 }
10985 set_yylval_id('.');
10986 SET_LEX_STATE(EXPR_DOT);
10987 return '.';
10988 }
10989
10990 case '0': case '1': case '2': case '3': case '4':
10991 case '5': case '6': case '7': case '8': case '9':
10992 return parse_numeric(p, c);
10993
10994 case ')':
10995 COND_POP();
10996 CMDARG_POP();
10997 SET_LEX_STATE(EXPR_ENDFN);
10998 p->lex.paren_nest--;
10999 return c;
11000
11001 case ']':
11002 COND_POP();
11003 CMDARG_POP();
11004 SET_LEX_STATE(EXPR_END);
11005 p->lex.paren_nest--;
11006 return c;
11007
11008 case '}':
11009 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11010 if (!p->lex.brace_nest--) return tSTRING_DEND;
11011 COND_POP();
11012 CMDARG_POP();
11013 SET_LEX_STATE(EXPR_END);
11014 p->lex.paren_nest--;
11015 return c;
11016
11017 case ':':
11018 c = nextc(p);
11019 if (c == ':') {
11020 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11021 SET_LEX_STATE(EXPR_BEG);
11022 return tCOLON3;
11023 }
11024 set_yylval_id(idCOLON2);
11025 SET_LEX_STATE(EXPR_DOT);
11026 return tCOLON2;
11027 }
11028 if (IS_END() || ISSPACE(c) || c == '#') {
11029 pushback(p, c);
11030 c = warn_balanced(':', ":", "symbol literal");
11031 SET_LEX_STATE(EXPR_BEG);
11032 return c;
11033 }
11034 switch (c) {
11035 case '\'':
11036 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11037 break;
11038 case '"':
11039 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11040 break;
11041 default:
11042 pushback(p, c);
11043 break;
11044 }
11045 SET_LEX_STATE(EXPR_FNAME);
11046 return tSYMBEG;
11047
11048 case '/':
11049 if (IS_BEG()) {
11050 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11051 return tREGEXP_BEG;
11052 }
11053 if ((c = nextc(p)) == '=') {
11054 set_yylval_id('/');
11055 SET_LEX_STATE(EXPR_BEG);
11056 return tOP_ASGN;
11057 }
11058 pushback(p, c);
11059 if (IS_SPCARG(c)) {
11060 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11061 return tREGEXP_BEG;
11062 }
11063 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11064 return warn_balanced('/', "/", "regexp literal");
11065
11066 case '^':
11067 if ((c = nextc(p)) == '=') {
11068 set_yylval_id('^');
11069 SET_LEX_STATE(EXPR_BEG);
11070 return tOP_ASGN;
11071 }
11072 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11073 pushback(p, c);
11074 return '^';
11075
11076 case ';':
11077 SET_LEX_STATE(EXPR_BEG);
11078 p->command_start = TRUE;
11079 return ';';
11080
11081 case ',':
11082 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11083 return ',';
11084
11085 case '~':
11086 if (IS_AFTER_OPERATOR()) {
11087 if ((c = nextc(p)) != '@') {
11088 pushback(p, c);
11089 }
11090 SET_LEX_STATE(EXPR_ARG);
11091 }
11092 else {
11093 SET_LEX_STATE(EXPR_BEG);
11094 }
11095 return '~';
11096
11097 case '(':
11098 if (IS_BEG()) {
11099 c = tLPAREN;
11100 }
11101 else if (!space_seen) {
11102 /* foo( ... ) => method call, no ambiguity */
11103 }
11104 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11105 c = tLPAREN_ARG;
11106 }
11107 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11108 rb_warning0("parentheses after method name is interpreted as "
11109 "an argument list, not a decomposed argument");
11110 }
11111 p->lex.paren_nest++;
11112 COND_PUSH(0);
11113 CMDARG_PUSH(0);
11114 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11115 return c;
11116
11117 case '[':
11118 p->lex.paren_nest++;
11119 if (IS_AFTER_OPERATOR()) {
11120 if ((c = nextc(p)) == ']') {
11121 p->lex.paren_nest--;
11122 SET_LEX_STATE(EXPR_ARG);
11123 if ((c = nextc(p)) == '=') {
11124 return tASET;
11125 }
11126 pushback(p, c);
11127 return tAREF;
11128 }
11129 pushback(p, c);
11130 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11131 return '[';
11132 }
11133 else if (IS_BEG()) {
11134 c = tLBRACK;
11135 }
11136 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11137 c = tLBRACK;
11138 }
11139 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11140 COND_PUSH(0);
11141 CMDARG_PUSH(0);
11142 return c;
11143
11144 case '{':
11145 ++p->lex.brace_nest;
11146 if (lambda_beginning_p())
11147 c = tLAMBEG;
11148 else if (IS_lex_state(EXPR_LABELED))
11149 c = tLBRACE; /* hash */
11150 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11151 c = '{'; /* block (primary) */
11152 else if (IS_lex_state(EXPR_ENDARG))
11153 c = tLBRACE_ARG; /* block (expr) */
11154 else
11155 c = tLBRACE; /* hash */
11156 if (c != tLBRACE) {
11157 p->command_start = TRUE;
11158 SET_LEX_STATE(EXPR_BEG);
11159 }
11160 else {
11161 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11162 }
11163 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11164 COND_PUSH(0);
11165 CMDARG_PUSH(0);
11166 return c;
11167
11168 case '\\':
11169 c = nextc(p);
11170 if (c == '\n') {
11171 space_seen = 1;
11172 dispatch_scan_event(p, tSP);
11173 goto retry; /* skip \\n */
11174 }
11175 if (c == ' ') return tSP;
11176 if (ISSPACE(c)) return c;
11177 pushback(p, c);
11178 return '\\';
11179
11180 case '%':
11181 return parse_percent(p, space_seen, last_state);
11182
11183 case '$':
11184 return parse_gvar(p, last_state);
11185
11186 case '@':
11187 return parse_atmark(p, last_state);
11188
11189 case '_':
11190 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11191 p->ruby__end__seen = 1;
11192 p->eofp = 1;
11193#ifdef RIPPER
11194 lex_goto_eol(p);
11195 dispatch_scan_event(p, k__END__);
11196#endif
11197 return END_OF_INPUT;
11198 }
11199 newtok(p);
11200 break;
11201
11202 default:
11203 if (!parser_is_identchar(p)) {
11204 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11205 token_flush(p);
11206 goto retry;
11207 }
11208
11209 newtok(p);
11210 break;
11211 }
11212
11213 return parse_ident(p, c, cmd_state);
11214}
11215
11216static enum yytokentype
11217yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11218{
11219 enum yytokentype t;
11220
11221 p->lval = lval;
11222 lval->node = 0;
11223 p->yylloc = yylloc;
11224
11225 t = parser_yylex(p);
11226
11227 if (has_delayed_token(p))
11228 dispatch_delayed_token(p, t);
11229 else if (t != END_OF_INPUT)
11230 dispatch_scan_event(p, t);
11231
11232 return t;
11233}
11234
11235#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11236
11237static NODE*
11238node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11239{
11240 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11241
11242 rb_node_init(n, type);
11243 return n;
11244}
11245
11246static NODE *
11247nd_set_loc(NODE *nd, const YYLTYPE *loc)
11248{
11249 nd->nd_loc = *loc;
11250 nd_set_line(nd, loc->beg_pos.lineno);
11251 return nd;
11252}
11253
11254static NODE*
11255node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11256{
11257 NODE *n = node_new_internal(p, type, size, alignment);
11258
11259 nd_set_loc(n, loc);
11260 nd_set_node_id(n, parser_get_node_id(p));
11261 return n;
11262}
11263
11264#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11265
11266static rb_node_scope_t *
11267rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11268{
11269 rb_ast_id_table_t *nd_tbl;
11270 nd_tbl = local_tbl(p);
11271 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11272 n->nd_tbl = nd_tbl;
11273 n->nd_body = nd_body;
11274 n->nd_parent = nd_parent;
11275 n->nd_args = nd_args;
11276
11277 return n;
11278}
11279
11280static rb_node_scope_t *
11281rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11282{
11283 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11284 n->nd_tbl = nd_tbl;
11285 n->nd_body = nd_body;
11286 n->nd_parent = nd_parent;
11287 n->nd_args = nd_args;
11288
11289 return n;
11290}
11291
11292static rb_node_defn_t *
11293rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11294{
11295 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11296 n->nd_mid = nd_mid;
11297 n->nd_defn = nd_defn;
11298
11299 return n;
11300}
11301
11302static rb_node_defs_t *
11303rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11304{
11305 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11306 n->nd_recv = nd_recv;
11307 n->nd_mid = nd_mid;
11308 n->nd_defn = nd_defn;
11309
11310 return n;
11311}
11312
11313static rb_node_block_t *
11314rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11315{
11316 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11317 n->nd_head = nd_head;
11318 n->nd_end = (NODE *)n;
11319 n->nd_next = 0;
11320
11321 return n;
11322}
11323
11324static rb_node_for_t *
11325rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *for_keyword_loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *do_keyword_loc, const YYLTYPE *end_keyword_loc)
11326{
11327 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11328 n->nd_body = nd_body;
11329 n->nd_iter = nd_iter;
11330 n->for_keyword_loc = *for_keyword_loc;
11331 n->in_keyword_loc = *in_keyword_loc;
11332 n->do_keyword_loc = *do_keyword_loc;
11333 n->end_keyword_loc = *end_keyword_loc;
11334
11335 return n;
11336}
11337
11338static rb_node_for_masgn_t *
11339rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11340{
11341 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11342 n->nd_var = nd_var;
11343
11344 return n;
11345}
11346
11347static rb_node_retry_t *
11348rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11349{
11350 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11351
11352 return n;
11353}
11354
11355static rb_node_begin_t *
11356rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11357{
11358 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11359 n->nd_body = nd_body;
11360
11361 return n;
11362}
11363
11364static rb_node_rescue_t *
11365rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11366{
11367 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11368 n->nd_head = nd_head;
11369 n->nd_resq = nd_resq;
11370 n->nd_else = nd_else;
11371
11372 return n;
11373}
11374
11375static rb_node_resbody_t *
11376rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11377{
11378 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11379 n->nd_args = nd_args;
11380 n->nd_exc_var = nd_exc_var;
11381 n->nd_body = nd_body;
11382 n->nd_next = nd_next;
11383
11384 return n;
11385}
11386
11387static rb_node_ensure_t *
11388rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11389{
11390 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11391 n->nd_head = nd_head;
11392 n->nd_ensr = nd_ensr;
11393
11394 return n;
11395}
11396
11397static rb_node_and_t *
11398rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11399{
11400 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11401 n->nd_1st = nd_1st;
11402 n->nd_2nd = nd_2nd;
11403 n->operator_loc = *operator_loc;
11404
11405 return n;
11406}
11407
11408static rb_node_or_t *
11409rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11410{
11411 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11412 n->nd_1st = nd_1st;
11413 n->nd_2nd = nd_2nd;
11414 n->operator_loc = *operator_loc;
11415
11416 return n;
11417}
11418
11419static rb_node_return_t *
11420rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11421{
11422 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11423 n->nd_stts = nd_stts;
11424 n->keyword_loc = *keyword_loc;
11425 return n;
11426}
11427
11428static rb_node_yield_t *
11429rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11430{
11431 if (nd_head) no_blockarg(p, nd_head);
11432
11433 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11434 n->nd_head = nd_head;
11435 n->keyword_loc = *keyword_loc;
11436 n->lparen_loc = *lparen_loc;
11437 n->rparen_loc = *rparen_loc;
11438
11439 return n;
11440}
11441
11442static rb_node_if_t *
11443rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc)
11444{
11445 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11446 n->nd_cond = nd_cond;
11447 n->nd_body = nd_body;
11448 n->nd_else = nd_else;
11449 n->if_keyword_loc = *if_keyword_loc;
11450 n->then_keyword_loc = *then_keyword_loc;
11451 n->end_keyword_loc = *end_keyword_loc;
11452
11453 return n;
11454}
11455
11456static rb_node_unless_t *
11457rb_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)
11458{
11459 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11460 n->nd_cond = nd_cond;
11461 n->nd_body = nd_body;
11462 n->nd_else = nd_else;
11463 n->keyword_loc = *keyword_loc;
11464 n->then_keyword_loc = *then_keyword_loc;
11465 n->end_keyword_loc = *end_keyword_loc;
11466
11467 return n;
11468}
11469
11470static rb_node_class_t *
11471rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc)
11472{
11473 /* Keep the order of node creation */
11474 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11475 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11476 RNODE_SCOPE(scope)->nd_parent = &n->node;
11477 n->nd_cpath = nd_cpath;
11478 n->nd_body = scope;
11479 n->nd_super = nd_super;
11480 n->class_keyword_loc = *class_keyword_loc;
11481 n->inheritance_operator_loc = *inheritance_operator_loc;
11482 n->end_keyword_loc = *end_keyword_loc;
11483
11484 return n;
11485}
11486
11487static rb_node_sclass_t *
11488rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *operator_loc, const YYLTYPE *end_keyword_loc)
11489{
11490 /* Keep the order of node creation */
11491 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11492 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11493 RNODE_SCOPE(scope)->nd_parent = &n->node;
11494 n->nd_recv = nd_recv;
11495 n->nd_body = scope;
11496 n->class_keyword_loc = *class_keyword_loc;
11497 n->operator_loc = *operator_loc;
11498 n->end_keyword_loc = *end_keyword_loc;
11499
11500 return n;
11501}
11502
11503static rb_node_module_t *
11504rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc)
11505{
11506 /* Keep the order of node creation */
11507 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11508 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11509 RNODE_SCOPE(scope)->nd_parent = &n->node;
11510 n->nd_cpath = nd_cpath;
11511 n->nd_body = scope;
11512 n->module_keyword_loc = *module_keyword_loc;
11513 n->end_keyword_loc = *end_keyword_loc;
11514
11515 return n;
11516}
11517
11518static rb_node_iter_t *
11519rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11520{
11521 /* Keep the order of node creation */
11522 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11523 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11524 RNODE_SCOPE(scope)->nd_parent = &n->node;
11525 n->nd_body = scope;
11526 n->nd_iter = 0;
11527
11528 return n;
11529}
11530
11531static rb_node_lambda_t *
11532rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
11533{
11534 /* Keep the order of node creation */
11535 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11536 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11537 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11538 RNODE_SCOPE(scope)->nd_parent = &n->node;
11539 n->nd_body = scope;
11540 n->operator_loc = *operator_loc;
11541 n->opening_loc = *opening_loc;
11542 n->closing_loc = *closing_loc;
11543
11544 return n;
11545}
11546
11547static rb_node_case_t *
11548rb_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)
11549{
11550 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11551 n->nd_head = nd_head;
11552 n->nd_body = nd_body;
11553 n->case_keyword_loc = *case_keyword_loc;
11554 n->end_keyword_loc = *end_keyword_loc;
11555
11556 return n;
11557}
11558
11559static rb_node_case2_t *
11560rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11561{
11562 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11563 n->nd_head = 0;
11564 n->nd_body = nd_body;
11565 n->case_keyword_loc = *case_keyword_loc;
11566 n->end_keyword_loc = *end_keyword_loc;
11567
11568 return n;
11569}
11570
11571static rb_node_case3_t *
11572rb_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)
11573{
11574 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11575 n->nd_head = nd_head;
11576 n->nd_body = nd_body;
11577 n->case_keyword_loc = *case_keyword_loc;
11578 n->end_keyword_loc = *end_keyword_loc;
11579
11580 return n;
11581}
11582
11583static rb_node_when_t *
11584rb_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)
11585{
11586 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11587 n->nd_head = nd_head;
11588 n->nd_body = nd_body;
11589 n->nd_next = nd_next;
11590 n->keyword_loc = *keyword_loc;
11591 n->then_keyword_loc = *then_keyword_loc;
11592
11593 return n;
11594}
11595
11596static rb_node_in_t *
11597rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc)
11598{
11599 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11600 n->nd_head = nd_head;
11601 n->nd_body = nd_body;
11602 n->nd_next = nd_next;
11603 n->in_keyword_loc = *in_keyword_loc;
11604 n->then_keyword_loc = *then_keyword_loc;
11605 n->operator_loc = *operator_loc;
11606
11607 return n;
11608}
11609
11610static rb_node_while_t *
11611rb_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)
11612{
11613 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11614 n->nd_cond = nd_cond;
11615 n->nd_body = nd_body;
11616 n->nd_state = nd_state;
11617 n->keyword_loc = *keyword_loc;
11618 n->closing_loc = *closing_loc;
11619
11620 return n;
11621}
11622
11623static rb_node_until_t *
11624rb_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)
11625{
11626 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11627 n->nd_cond = nd_cond;
11628 n->nd_body = nd_body;
11629 n->nd_state = nd_state;
11630 n->keyword_loc = *keyword_loc;
11631 n->closing_loc = *closing_loc;
11632
11633 return n;
11634}
11635
11636static rb_node_colon2_t *
11637rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11638{
11639 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11640 n->nd_head = nd_head;
11641 n->nd_mid = nd_mid;
11642 n->delimiter_loc = *delimiter_loc;
11643 n->name_loc = *name_loc;
11644
11645 return n;
11646}
11647
11648static rb_node_colon3_t *
11649rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11650{
11651 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11652 n->nd_mid = nd_mid;
11653 n->delimiter_loc = *delimiter_loc;
11654 n->name_loc = *name_loc;
11655
11656 return n;
11657}
11658
11659static rb_node_dot2_t *
11660rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11661{
11662 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11663 n->nd_beg = nd_beg;
11664 n->nd_end = nd_end;
11665 n->operator_loc = *operator_loc;
11666
11667 return n;
11668}
11669
11670static rb_node_dot3_t *
11671rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11672{
11673 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11674 n->nd_beg = nd_beg;
11675 n->nd_end = nd_end;
11676 n->operator_loc = *operator_loc;
11677
11678 return n;
11679}
11680
11681static rb_node_self_t *
11682rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11683{
11684 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11685 n->nd_state = 1;
11686
11687 return n;
11688}
11689
11690static rb_node_nil_t *
11691rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11692{
11693 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11694
11695 return n;
11696}
11697
11698static rb_node_true_t *
11699rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11700{
11701 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11702
11703 return n;
11704}
11705
11706static rb_node_false_t *
11707rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11708{
11709 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11710
11711 return n;
11712}
11713
11714static rb_node_super_t *
11715rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11716 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11717{
11718 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11719 n->nd_args = nd_args;
11720 n->keyword_loc = *keyword_loc;
11721 n->lparen_loc = *lparen_loc;
11722 n->rparen_loc = *rparen_loc;
11723
11724 return n;
11725}
11726
11727static rb_node_zsuper_t *
11728rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11729{
11730 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11731
11732 return n;
11733}
11734
11735static rb_node_match2_t *
11736rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11737{
11738 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11739 n->nd_recv = nd_recv;
11740 n->nd_value = nd_value;
11741 n->nd_args = 0;
11742
11743 return n;
11744}
11745
11746static rb_node_match3_t *
11747rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11748{
11749 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11750 n->nd_recv = nd_recv;
11751 n->nd_value = nd_value;
11752
11753 return n;
11754}
11755
11756static rb_node_list_t *
11757rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11758{
11759 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11760}
11761
11762static rb_node_list_t *
11763rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11764{
11765 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11766 n->nd_head = nd_head;
11767 n->as.nd_alen = nd_alen;
11768 n->nd_next = nd_next;
11769
11770 return n;
11771}
11772
11773static rb_node_zlist_t *
11774rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11775{
11776 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11777
11778 return n;
11779}
11780
11781static rb_node_hash_t *
11782rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11783{
11784 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11785 n->nd_head = nd_head;
11786 n->nd_brace = 0;
11787
11788 return n;
11789}
11790
11791static rb_node_masgn_t *
11792rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11793{
11794 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11795 n->nd_head = nd_head;
11796 n->nd_value = 0;
11797 n->nd_args = nd_args;
11798
11799 return n;
11800}
11801
11802static rb_node_gasgn_t *
11803rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11804{
11805 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11806 n->nd_vid = nd_vid;
11807 n->nd_value = nd_value;
11808
11809 return n;
11810}
11811
11812static rb_node_lasgn_t *
11813rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11814{
11815 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11816 n->nd_vid = nd_vid;
11817 n->nd_value = nd_value;
11818
11819 return n;
11820}
11821
11822static rb_node_dasgn_t *
11823rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11824{
11825 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11826 n->nd_vid = nd_vid;
11827 n->nd_value = nd_value;
11828
11829 return n;
11830}
11831
11832static rb_node_iasgn_t *
11833rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11834{
11835 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11836 n->nd_vid = nd_vid;
11837 n->nd_value = nd_value;
11838
11839 return n;
11840}
11841
11842static rb_node_cvasgn_t *
11843rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11844{
11845 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11846 n->nd_vid = nd_vid;
11847 n->nd_value = nd_value;
11848
11849 return n;
11850}
11851
11852static rb_node_op_asgn1_t *
11853rb_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)
11854{
11855 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11856 n->nd_recv = nd_recv;
11857 n->nd_mid = nd_mid;
11858 n->nd_index = index;
11859 n->nd_rvalue = rvalue;
11860 n->call_operator_loc = *call_operator_loc;
11861 n->opening_loc = *opening_loc;
11862 n->closing_loc = *closing_loc;
11863 n->binary_operator_loc = *binary_operator_loc;
11864
11865 return n;
11866}
11867
11868static rb_node_op_asgn2_t *
11869rb_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)
11870{
11871 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11872 n->nd_recv = nd_recv;
11873 n->nd_value = nd_value;
11874 n->nd_vid = nd_vid;
11875 n->nd_mid = nd_mid;
11876 n->nd_aid = nd_aid;
11877 n->call_operator_loc = *call_operator_loc;
11878 n->message_loc = *message_loc;
11879 n->binary_operator_loc = *binary_operator_loc;
11880
11881 return n;
11882}
11883
11884static rb_node_op_asgn_or_t *
11885rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11886{
11887 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11888 n->nd_head = nd_head;
11889 n->nd_value = nd_value;
11890
11891 return n;
11892}
11893
11894static rb_node_op_asgn_and_t *
11895rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11896{
11897 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11898 n->nd_head = nd_head;
11899 n->nd_value = nd_value;
11900
11901 return n;
11902}
11903
11904static rb_node_gvar_t *
11905rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11906{
11907 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11908 n->nd_vid = nd_vid;
11909
11910 return n;
11911}
11912
11913static rb_node_lvar_t *
11914rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11915{
11916 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11917 n->nd_vid = nd_vid;
11918
11919 return n;
11920}
11921
11922static rb_node_dvar_t *
11923rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11924{
11925 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11926 n->nd_vid = nd_vid;
11927
11928 return n;
11929}
11930
11931static rb_node_ivar_t *
11932rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11933{
11934 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11935 n->nd_vid = nd_vid;
11936
11937 return n;
11938}
11939
11940static rb_node_const_t *
11941rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11942{
11943 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11944 n->nd_vid = nd_vid;
11945
11946 return n;
11947}
11948
11949static rb_node_cvar_t *
11950rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11951{
11952 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11953 n->nd_vid = nd_vid;
11954
11955 return n;
11956}
11957
11958static rb_node_nth_ref_t *
11959rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11960{
11961 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11962 n->nd_nth = nd_nth;
11963
11964 return n;
11965}
11966
11967static rb_node_back_ref_t *
11968rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11969{
11970 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11971 n->nd_nth = nd_nth;
11972
11973 return n;
11974}
11975
11976static rb_node_integer_t *
11977rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11978{
11979 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11980 n->val = val;
11981 n->minus = FALSE;
11982 n->base = base;
11983
11984 return n;
11985}
11986
11987static rb_node_float_t *
11988rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11989{
11990 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11991 n->val = val;
11992 n->minus = FALSE;
11993
11994 return n;
11995}
11996
11997static rb_node_rational_t *
11998rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11999{
12000 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12001 n->val = val;
12002 n->minus = FALSE;
12003 n->base = base;
12004 n->seen_point = seen_point;
12005
12006 return n;
12007}
12008
12009static rb_node_imaginary_t *
12010rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12011{
12012 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12013 n->val = val;
12014 n->minus = FALSE;
12015 n->base = base;
12016 n->seen_point = seen_point;
12017 n->type = numeric_type;
12018
12019 return n;
12020}
12021
12022static rb_node_str_t *
12023rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12024{
12025 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12026 n->string = string;
12027
12028 return n;
12029}
12030
12031/* TODO; Use union for NODE_DSTR2 */
12032static rb_node_dstr_t *
12033rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12034{
12035 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12036 n->string = string;
12037 n->as.nd_alen = nd_alen;
12038 n->nd_next = (rb_node_list_t *)nd_next;
12039
12040 return n;
12041}
12042
12043static rb_node_dstr_t *
12044rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12045{
12046 return rb_node_dstr_new0(p, string, 1, 0, loc);
12047}
12048
12049static rb_node_xstr_t *
12050rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12051{
12052 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12053 n->string = string;
12054
12055 return n;
12056}
12057
12058static rb_node_dxstr_t *
12059rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12060{
12061 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12062 n->string = string;
12063 n->as.nd_alen = nd_alen;
12064 n->nd_next = (rb_node_list_t *)nd_next;
12065
12066 return n;
12067}
12068
12069static rb_node_sym_t *
12070rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12071{
12072 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12073 n->string = rb_str_to_parser_string(p, str);
12074
12075 return n;
12076}
12077
12078static rb_node_dsym_t *
12079rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12080{
12081 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12082 n->string = string;
12083 n->as.nd_alen = nd_alen;
12084 n->nd_next = (rb_node_list_t *)nd_next;
12085
12086 return n;
12087}
12088
12089static rb_node_evstr_t *
12090rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12091{
12092 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12093 n->nd_body = nd_body;
12094 n->opening_loc = *opening_loc;
12095 n->closing_loc = *closing_loc;
12096
12097 return n;
12098}
12099
12100static rb_node_regx_t *
12101rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12102{
12103 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12104 n->string = string;
12105 n->options = options & RE_OPTION_MASK;
12106 n->opening_loc = *opening_loc;
12107 n->content_loc = *content_loc;
12108 n->closing_loc = *closing_loc;
12109
12110 return n;
12111}
12112
12113static rb_node_call_t *
12114rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12115{
12116 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12117 n->nd_recv = nd_recv;
12118 n->nd_mid = nd_mid;
12119 n->nd_args = nd_args;
12120
12121 return n;
12122}
12123
12124static rb_node_opcall_t *
12125rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12126{
12127 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12128 n->nd_recv = nd_recv;
12129 n->nd_mid = nd_mid;
12130 n->nd_args = nd_args;
12131
12132 return n;
12133}
12134
12135static rb_node_fcall_t *
12136rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12137{
12138 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12139 n->nd_mid = nd_mid;
12140 n->nd_args = nd_args;
12141
12142 return n;
12143}
12144
12145static rb_node_qcall_t *
12146rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12147{
12148 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12149 n->nd_recv = nd_recv;
12150 n->nd_mid = nd_mid;
12151 n->nd_args = nd_args;
12152
12153 return n;
12154}
12155
12156static rb_node_vcall_t *
12157rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12158{
12159 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12160 n->nd_mid = nd_mid;
12161
12162 return n;
12163}
12164
12165static rb_node_once_t *
12166rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12167{
12168 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12169 n->nd_body = nd_body;
12170
12171 return n;
12172}
12173
12174static rb_node_args_t *
12175rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12176{
12177 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12178 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12179
12180 return n;
12181}
12182
12183static rb_node_args_aux_t *
12184rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12185{
12186 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12187 n->nd_pid = nd_pid;
12188 n->nd_plen = nd_plen;
12189 n->nd_next = 0;
12190
12191 return n;
12192}
12193
12194static rb_node_opt_arg_t *
12195rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12196{
12197 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12198 n->nd_body = nd_body;
12199 n->nd_next = 0;
12200
12201 return n;
12202}
12203
12204static rb_node_kw_arg_t *
12205rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12206{
12207 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12208 n->nd_body = nd_body;
12209 n->nd_next = 0;
12210
12211 return n;
12212}
12213
12214static rb_node_postarg_t *
12215rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12216{
12217 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12218 n->nd_1st = nd_1st;
12219 n->nd_2nd = nd_2nd;
12220
12221 return n;
12222}
12223
12224static rb_node_argscat_t *
12225rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12226{
12227 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12228 n->nd_head = nd_head;
12229 n->nd_body = nd_body;
12230
12231 return n;
12232}
12233
12234static rb_node_argspush_t *
12235rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12236{
12237 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12238 n->nd_head = nd_head;
12239 n->nd_body = nd_body;
12240
12241 return n;
12242}
12243
12244static rb_node_splat_t *
12245rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12246{
12247 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12248 n->nd_head = nd_head;
12249 n->operator_loc = *operator_loc;
12250
12251 return n;
12252}
12253
12254static rb_node_block_pass_t *
12255rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12256{
12257 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12258 n->forwarding = 0;
12259 n->nd_head = 0;
12260 n->nd_body = nd_body;
12261 n->operator_loc = *operator_loc;
12262
12263 return n;
12264}
12265
12266static rb_node_alias_t *
12267rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12268{
12269 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12270 n->nd_1st = nd_1st;
12271 n->nd_2nd = nd_2nd;
12272 n->keyword_loc = *keyword_loc;
12273
12274 return n;
12275}
12276
12277static rb_node_valias_t *
12278rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12279{
12280 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12281 n->nd_alias = nd_alias;
12282 n->nd_orig = nd_orig;
12283 n->keyword_loc = *keyword_loc;
12284
12285 return n;
12286}
12287
12288static rb_node_undef_t *
12289rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12290{
12291 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12292 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12293 n->keyword_loc = NULL_LOC;
12294 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12295
12296 return n;
12297}
12298
12299static rb_node_errinfo_t *
12300rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12301{
12302 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12303
12304 return n;
12305}
12306
12307static rb_node_defined_t *
12308rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12309{
12310 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12311 n->nd_head = nd_head;
12312 n->keyword_loc = *keyword_loc;
12313
12314 return n;
12315}
12316
12317static rb_node_postexe_t *
12318rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12319{
12320 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12321 n->nd_body = nd_body;
12322 n->keyword_loc = *keyword_loc;
12323 n->opening_loc = *opening_loc;
12324 n->closing_loc = *closing_loc;
12325
12326 return n;
12327}
12328
12329static rb_node_attrasgn_t *
12330rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12331{
12332 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12333 n->nd_recv = nd_recv;
12334 n->nd_mid = nd_mid;
12335 n->nd_args = nd_args;
12336
12337 return n;
12338}
12339
12340static rb_node_aryptn_t *
12341rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12342{
12343 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12344 n->nd_pconst = 0;
12345 n->pre_args = pre_args;
12346 n->rest_arg = rest_arg;
12347 n->post_args = post_args;
12348
12349 return n;
12350}
12351
12352static rb_node_hshptn_t *
12353rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12354{
12355 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12356 n->nd_pconst = nd_pconst;
12357 n->nd_pkwargs = nd_pkwargs;
12358 n->nd_pkwrestarg = nd_pkwrestarg;
12359
12360 return n;
12361}
12362
12363static rb_node_fndptn_t *
12364rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12365{
12366 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12367 n->nd_pconst = 0;
12368 n->pre_rest_arg = pre_rest_arg;
12369 n->args = args;
12370 n->post_rest_arg = post_rest_arg;
12371
12372 return n;
12373}
12374
12375static rb_node_line_t *
12376rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12377{
12378 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12379
12380 return n;
12381}
12382
12383static rb_node_file_t *
12384rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12385{
12386 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12387 n->path = rb_str_to_parser_string(p, str);
12388
12389 return n;
12390}
12391
12392static rb_node_encoding_t *
12393rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12394{
12395 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12396 n->enc = p->enc;
12397
12398 return n;
12399}
12400
12401static rb_node_cdecl_t *
12402rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12403{
12404 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12405 n->nd_vid = nd_vid;
12406 n->nd_value = nd_value;
12407 n->nd_else = nd_else;
12408 n->shareability = shareability;
12409
12410 return n;
12411}
12412
12413static rb_node_op_cdecl_t *
12414rb_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)
12415{
12416 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12417 n->nd_head = nd_head;
12418 n->nd_value = nd_value;
12419 n->nd_aid = nd_aid;
12420 n->shareability = shareability;
12421
12422 return n;
12423}
12424
12425static rb_node_error_t *
12426rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12427{
12428 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12429
12430 return n;
12431}
12432
12433static rb_node_break_t *
12434rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12435{
12436 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12437 n->nd_stts = nd_stts;
12438 n->nd_chain = 0;
12439 n->keyword_loc = *keyword_loc;
12440
12441 return n;
12442}
12443
12444static rb_node_next_t *
12445rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12446{
12447 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12448 n->nd_stts = nd_stts;
12449 n->nd_chain = 0;
12450 n->keyword_loc = *keyword_loc;
12451
12452 return n;
12453}
12454
12455static rb_node_redo_t *
12456rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12457{
12458 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12459 n->nd_chain = 0;
12460 n->keyword_loc = *keyword_loc;
12461
12462 return n;
12463}
12464
12465static rb_node_def_temp_t *
12466rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12467{
12468 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12469 n->save.numparam_save = 0;
12470 n->save.max_numparam = 0;
12471 n->save.ctxt = p->ctxt;
12472 n->nd_def = 0;
12473 n->nd_mid = 0;
12474
12475 return n;
12476}
12477
12478static rb_node_def_temp_t *
12479def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12480{
12481 n->save.numparam_save = numparam_push(p);
12482 n->save.max_numparam = p->max_numparam;
12483 return n;
12484}
12485
12486#ifndef RIPPER
12487static enum node_type
12488nodetype(NODE *node) /* for debug */
12489{
12490 return (enum node_type)nd_type(node);
12491}
12492
12493static int
12494nodeline(NODE *node)
12495{
12496 return nd_line(node);
12497}
12498#endif
12499
12500static NODE*
12501newline_node(NODE *node)
12502{
12503 if (node) {
12504 node = remove_begin(node);
12505 nd_set_fl_newline(node);
12506 }
12507 return node;
12508}
12509
12510static void
12511fixpos(NODE *node, NODE *orig)
12512{
12513 if (!node) return;
12514 if (!orig) return;
12515 nd_set_line(node, nd_line(orig));
12516}
12517
12518static NODE*
12519block_append(struct parser_params *p, NODE *head, NODE *tail)
12520{
12521 NODE *end, *h = head, *nd;
12522
12523 if (tail == 0) return head;
12524
12525 if (h == 0) return tail;
12526 switch (nd_type(h)) {
12527 default:
12528 h = end = NEW_BLOCK(head, &head->nd_loc);
12529 head = end;
12530 break;
12531 case NODE_BLOCK:
12532 end = RNODE_BLOCK(h)->nd_end;
12533 break;
12534 }
12535
12536 nd = RNODE_BLOCK(end)->nd_head;
12537 switch (nd_type(nd)) {
12538 case NODE_RETURN:
12539 case NODE_BREAK:
12540 case NODE_NEXT:
12541 case NODE_REDO:
12542 case NODE_RETRY:
12543 rb_warning0L(nd_line(tail), "statement not reached");
12544 break;
12545
12546 default:
12547 break;
12548 }
12549
12550 if (!nd_type_p(tail, NODE_BLOCK)) {
12551 tail = NEW_BLOCK(tail, &tail->nd_loc);
12552 }
12553 RNODE_BLOCK(end)->nd_next = tail;
12554 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12555 nd_set_last_loc(head, nd_last_loc(tail));
12556 return head;
12557}
12558
12559/* append item to the list */
12560static NODE*
12561list_append(struct parser_params *p, NODE *list, NODE *item)
12562{
12563 NODE *last;
12564
12565 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12566 if (RNODE_LIST(list)->nd_next) {
12567 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12568 }
12569 else {
12570 last = list;
12571 }
12572
12573 RNODE_LIST(list)->as.nd_alen += 1;
12574 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12575 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12576
12577 nd_set_last_loc(list, nd_last_loc(item));
12578
12579 return list;
12580}
12581
12582/* concat two lists */
12583static NODE*
12584list_concat(NODE *head, NODE *tail)
12585{
12586 NODE *last;
12587
12588 if (RNODE_LIST(head)->nd_next) {
12589 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12590 }
12591 else {
12592 last = head;
12593 }
12594
12595 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12596 RNODE_LIST(last)->nd_next = tail;
12597 if (RNODE_LIST(tail)->nd_next) {
12598 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12599 }
12600 else {
12601 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12602 }
12603
12604 nd_set_last_loc(head, nd_last_loc(tail));
12605
12606 return head;
12607}
12608
12609static int
12610literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12611{
12612 if (!tail) return 1;
12613 if (!rb_parser_enc_compatible(p, head, tail)) {
12614 compile_error(p, "string literal encodings differ (%s / %s)",
12615 rb_enc_name(rb_parser_str_get_encoding(head)),
12616 rb_enc_name(rb_parser_str_get_encoding(tail)));
12617 rb_parser_str_resize(p, head, 0);
12618 rb_parser_str_resize(p, tail, 0);
12619 return 0;
12620 }
12621 rb_parser_str_buf_append(p, head, tail);
12622 return 1;
12623}
12624
12625static rb_parser_string_t *
12626string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12627{
12628 if (htype != NODE_DSTR) return NULL;
12629 if (RNODE_DSTR(head)->nd_next) {
12630 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12631 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12632 }
12633 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12634 ASSUME(lit);
12635 return lit;
12636}
12637
12638#ifndef RIPPER
12639static rb_parser_string_t *
12640rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12641{
12642 rb_parser_string_t *copy;
12643 if (!orig) return NULL;
12644 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12645 copy->coderange = orig->coderange;
12646 copy->enc = orig->enc;
12647 return copy;
12648}
12649#endif
12650
12651/* concat two string literals */
12652static NODE *
12653literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12654{
12655 enum node_type htype;
12656 rb_parser_string_t *lit;
12657
12658 if (!head) return tail;
12659 if (!tail) return head;
12660
12661 htype = nd_type(head);
12662 if (htype == NODE_EVSTR) {
12663 head = new_dstr(p, head, loc);
12664 htype = NODE_DSTR;
12665 }
12666 if (p->heredoc_indent > 0) {
12667 switch (htype) {
12668 case NODE_STR:
12669 head = str2dstr(p, head);
12670 case NODE_DSTR:
12671 return list_append(p, head, tail);
12672 default:
12673 break;
12674 }
12675 }
12676 switch (nd_type(tail)) {
12677 case NODE_STR:
12678 if ((lit = string_literal_head(p, htype, head)) != false) {
12679 htype = NODE_STR;
12680 }
12681 else {
12682 lit = RNODE_DSTR(head)->string;
12683 }
12684 if (htype == NODE_STR) {
12685 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12686 error:
12687 rb_discard_node(p, head);
12688 rb_discard_node(p, tail);
12689 return 0;
12690 }
12691 rb_discard_node(p, tail);
12692 }
12693 else {
12694 list_append(p, head, tail);
12695 }
12696 break;
12697
12698 case NODE_DSTR:
12699 if (htype == NODE_STR) {
12700 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12701 goto error;
12702 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12703 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12704 RNODE_STR(head)->string = NULL;
12705 rb_discard_node(p, head);
12706 head = tail;
12707 }
12708 else if (!RNODE_DSTR(tail)->string) {
12709 append:
12710 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12711 if (!RNODE_DSTR(head)->nd_next) {
12712 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12713 }
12714 else if (RNODE_DSTR(tail)->nd_next) {
12715 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12716 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12717 }
12718 rb_discard_node(p, tail);
12719 }
12720 else if ((lit = string_literal_head(p, htype, head)) != false) {
12721 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12722 goto error;
12723 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12724 RNODE_DSTR(tail)->string = 0;
12725 goto append;
12726 }
12727 else {
12728 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));
12729 RNODE_DSTR(tail)->string = 0;
12730 }
12731 break;
12732
12733 case NODE_EVSTR:
12734 if (htype == NODE_STR) {
12735 head = str2dstr(p, head);
12736 RNODE_DSTR(head)->as.nd_alen = 1;
12737 }
12738 list_append(p, head, tail);
12739 break;
12740 }
12741 return head;
12742}
12743
12744static void
12745nd_copy_flag(NODE *new_node, NODE *old_node)
12746{
12747 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12748 nd_set_line(new_node, nd_line(old_node));
12749 new_node->nd_loc = old_node->nd_loc;
12750 new_node->node_id = old_node->node_id;
12751}
12752
12753static NODE *
12754str2dstr(struct parser_params *p, NODE *node)
12755{
12756 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12757 nd_copy_flag(new_node, node);
12758 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12759 RNODE_DSTR(new_node)->as.nd_alen = 0;
12760 RNODE_DSTR(new_node)->nd_next = 0;
12761 RNODE_STR(node)->string = 0;
12762
12763 return new_node;
12764}
12765
12766static NODE *
12767str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12768{
12769 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12770 nd_copy_flag(new_node, node);
12771 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12772 RNODE_REGX(new_node)->options = options;
12773 nd_set_loc(new_node, loc);
12774 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12775 RNODE_REGX(new_node)->content_loc = *content_loc;
12776 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12777 RNODE_STR(node)->string = 0;
12778
12779 return new_node;
12780}
12781
12782static NODE *
12783evstr2dstr(struct parser_params *p, NODE *node)
12784{
12785 if (nd_type_p(node, NODE_EVSTR)) {
12786 node = new_dstr(p, node, &node->nd_loc);
12787 }
12788 return node;
12789}
12790
12791static NODE *
12792new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12793{
12794 NODE *head = node;
12795
12796 if (node) {
12797 switch (nd_type(node)) {
12798 case NODE_STR:
12799 return str2dstr(p, node);
12800 case NODE_DSTR:
12801 break;
12802 case NODE_EVSTR:
12803 return node;
12804 }
12805 }
12806 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12807}
12808
12809static NODE *
12810new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12811{
12812 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12813 return list_append(p, dstr, node);
12814}
12815
12816static NODE *
12817call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12818 const YYLTYPE *op_loc, const YYLTYPE *loc)
12819{
12820 NODE *expr;
12821 value_expr(p, recv);
12822 value_expr(p, arg1);
12823 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12824 nd_set_line(expr, op_loc->beg_pos.lineno);
12825 return expr;
12826}
12827
12828static NODE *
12829call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12830{
12831 NODE *opcall;
12832 value_expr(p, recv);
12833 opcall = NEW_OPCALL(recv, id, 0, loc);
12834 nd_set_line(opcall, op_loc->beg_pos.lineno);
12835 return opcall;
12836}
12837
12838static NODE *
12839new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12840{
12841 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12842 nd_set_line(qcall, op_loc->beg_pos.lineno);
12843 return qcall;
12844}
12845
12846static NODE*
12847new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12848{
12849 NODE *ret;
12850 if (block) block_dup_check(p, args, block);
12851 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12852 if (block) ret = method_add_block(p, ret, block, loc);
12853 fixpos(ret, recv);
12854 return ret;
12855}
12856
12857static rb_locations_lambda_body_t*
12858new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12859{
12860 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12861 body->node = node;
12862 body->opening_loc = *opening_loc;
12863 body->closing_loc = *closing_loc;
12864 return body;
12865}
12866
12867static NODE *
12868command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12869{
12870 NODE **body;
12871 enum node_type type = nd_type(m);
12872 switch (type) {
12873 case NODE_YIELD:
12874 compile_error(p, "block given to yield");
12875 return m;
12876 case NODE_RETURN:
12877 body = &RNODE_RETURN(m)->nd_stts;
12878 break;
12879 case NODE_BREAK:
12880 case NODE_NEXT:
12881 body = &RNODE_EXITS(m)->nd_stts;
12882 break;
12883 case NODE_REDO: /* `redo` has no argument */
12884 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12885 return m;
12886 case NODE_RETRY: /* `retry` has no argument */
12887 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12888 return m;
12889 default:
12890 block_dup_check(p, get_nd_args(p, m), b);
12891 return method_add_block(p, m, b, loc);
12892 }
12893 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12894 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12895 *body = command_add_block(p, *body, b, &b_loc);
12896 m->nd_loc.end_pos = loc->end_pos;
12897 return m;
12898}
12899
12900#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12901
12902static NODE*
12903last_expr_once_body(NODE *node)
12904{
12905 if (!node) return 0;
12906 return nd_once_body(node);
12907}
12908
12909static NODE*
12910match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12911{
12912 NODE *n;
12913 int line = op_loc->beg_pos.lineno;
12914
12915 value_expr(p, node1);
12916 value_expr(p, node2);
12917
12918 if ((n = last_expr_once_body(node1)) != 0) {
12919 switch (nd_type(n)) {
12920 case NODE_DREGX:
12921 {
12922 NODE *match = NEW_MATCH2(node1, node2, loc);
12923 nd_set_line(match, line);
12924 return match;
12925 }
12926
12927 case NODE_REGX:
12928 {
12929 const VALUE lit = rb_node_regx_string_val(n);
12930 if (!NIL_P(lit)) {
12931 NODE *match = NEW_MATCH2(node1, node2, loc);
12932 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12933 nd_set_line(match, line);
12934 return match;
12935 }
12936 }
12937 }
12938 }
12939
12940 if ((n = last_expr_once_body(node2)) != 0) {
12941 NODE *match3;
12942
12943 switch (nd_type(n)) {
12944 case NODE_DREGX:
12945 match3 = NEW_MATCH3(node2, node1, loc);
12946 return match3;
12947 }
12948 }
12949
12950 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12951 nd_set_line(n, line);
12952 return n;
12953}
12954
12955# if WARN_PAST_SCOPE
12956static int
12957past_dvar_p(struct parser_params *p, ID id)
12958{
12959 struct vtable *past = p->lvtbl->past;
12960 while (past) {
12961 if (vtable_included(past, id)) return 1;
12962 past = past->prev;
12963 }
12964 return 0;
12965}
12966# endif
12967
12968static int
12969numparam_nested_p(struct parser_params *p)
12970{
12971 struct local_vars *local = p->lvtbl;
12972 NODE *outer = local->numparam.outer;
12973 NODE *inner = local->numparam.inner;
12974 if (outer || inner) {
12975 NODE *used = outer ? outer : inner;
12976 compile_error(p, "numbered parameter is already used in %s block\n"
12977 "%s:%d: numbered parameter is already used here",
12978 outer ? "outer" : "inner",
12979 p->ruby_sourcefile, nd_line(used));
12980 parser_show_error_line(p, &used->nd_loc);
12981 return 1;
12982 }
12983 return 0;
12984}
12985
12986static int
12987numparam_used_p(struct parser_params *p)
12988{
12989 NODE *numparam = p->lvtbl->numparam.current;
12990 if (numparam) {
12991 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12992 "%s:%d: numbered parameter is already used here",
12993 p->ruby_sourcefile, nd_line(numparam));
12994 parser_show_error_line(p, &numparam->nd_loc);
12995 return 1;
12996 }
12997 return 0;
12998}
12999
13000static int
13001it_used_p(struct parser_params *p)
13002{
13003 NODE *it = p->lvtbl->it;
13004 if (it) {
13005 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
13006 "%s:%d: 'it' is already used here",
13007 p->ruby_sourcefile, nd_line(it));
13008 parser_show_error_line(p, &it->nd_loc);
13009 return 1;
13010 }
13011 return 0;
13012}
13013
13014static NODE*
13015gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13016{
13017 ID *vidp = NULL;
13018 NODE *node;
13019 switch (id) {
13020 case keyword_self:
13021 return NEW_SELF(loc);
13022 case keyword_nil:
13023 return NEW_NIL(loc);
13024 case keyword_true:
13025 return NEW_TRUE(loc);
13026 case keyword_false:
13027 return NEW_FALSE(loc);
13028 case keyword__FILE__:
13029 {
13030 VALUE file = p->ruby_sourcefile_string;
13031 if (NIL_P(file))
13032 file = rb_str_new(0, 0);
13033 node = NEW_FILE(file, loc);
13034 }
13035 return node;
13036 case keyword__LINE__:
13037 return NEW_LINE(loc);
13038 case keyword__ENCODING__:
13039 return NEW_ENCODING(loc);
13040
13041 }
13042 switch (id_type(id)) {
13043 case ID_LOCAL:
13044 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13045 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13046 if (vidp) *vidp |= LVAR_USED;
13047 node = NEW_DVAR(id, loc);
13048 return node;
13049 }
13050 if (local_id_ref(p, id, &vidp)) {
13051 if (vidp) *vidp |= LVAR_USED;
13052 node = NEW_LVAR(id, loc);
13053 return node;
13054 }
13055 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13056 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13057 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13058 node = NEW_DVAR(id, loc);
13059 struct local_vars *local = p->lvtbl;
13060 if (!local->numparam.current) local->numparam.current = node;
13061 return node;
13062 }
13063# if WARN_PAST_SCOPE
13064 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13065 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13066 }
13067# endif
13068 /* method call without arguments */
13069 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13070 if (numparam_used_p(p)) return 0;
13071 if (p->max_numparam == ORDINAL_PARAM) {
13072 compile_error(p, "ordinary parameter is defined");
13073 return 0;
13074 }
13075 if (!p->it_id) {
13076 p->it_id = idItImplicit;
13077 vtable_add(p->lvtbl->args, p->it_id);
13078 }
13079 NODE *node = NEW_DVAR(p->it_id, loc);
13080 if (!p->lvtbl->it) p->lvtbl->it = node;
13081 return node;
13082 }
13083 return NEW_VCALL(id, loc);
13084 case ID_GLOBAL:
13085 return NEW_GVAR(id, loc);
13086 case ID_INSTANCE:
13087 return NEW_IVAR(id, loc);
13088 case ID_CONST:
13089 return NEW_CONST(id, loc);
13090 case ID_CLASS:
13091 return NEW_CVAR(id, loc);
13092 }
13093 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13094 return 0;
13095}
13096
13097static rb_node_opt_arg_t *
13098opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13099{
13100 rb_node_opt_arg_t *opts = opt_list;
13101 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13102
13103 while (opts->nd_next) {
13104 opts = opts->nd_next;
13105 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13106 }
13107 opts->nd_next = opt;
13108
13109 return opt_list;
13110}
13111
13112static rb_node_kw_arg_t *
13113kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13114{
13115 if (kwlist) {
13116 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13117 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13118 }
13119 return kwlist;
13120}
13121
13122static NODE *
13123new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13124{
13125 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13126 p->ctxt.has_trailing_semicolon = 0;
13127
13128 NODE *n = expr;
13129 while (n) {
13130 if (nd_type_p(n, NODE_BEGIN)) {
13131 n = RNODE_BEGIN(n)->nd_body;
13132 }
13133 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13134 n = RNODE_BLOCK(n)->nd_head;
13135 }
13136 else {
13137 break;
13138 }
13139 }
13140
13141 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13142 NODE *block = NEW_BLOCK(expr, loc);
13143 return NEW_DEFINED(block, loc, keyword_loc);
13144 }
13145
13146 return NEW_DEFINED(n, loc, keyword_loc);
13147}
13148
13149static NODE*
13150str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13151{
13152 VALUE lit;
13153 rb_parser_string_t *str = RNODE_STR(node)->string;
13154 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13155 yyerror1(loc, "invalid symbol");
13156 lit = STR_NEW0();
13157 }
13158 else {
13159 lit = rb_str_new_parser_string(str);
13160 }
13161 return NEW_SYM(lit, loc);
13162}
13163
13164static NODE*
13165symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13166{
13167 enum node_type type = nd_type(symbol);
13168 switch (type) {
13169 case NODE_DSTR:
13170 nd_set_type(symbol, NODE_DSYM);
13171 break;
13172 case NODE_STR:
13173 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13174 break;
13175 default:
13176 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13177 }
13178 return list_append(p, symbols, symbol);
13179}
13180
13181static void
13182dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13183{
13184 if (dreg->string) {
13185 reg_fragment_setenc(p, dreg->string, options);
13186 }
13187 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13188 NODE *frag = list->nd_head;
13189 if (nd_type_p(frag, NODE_STR)) {
13190 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13191 }
13192 else if (nd_type_p(frag, NODE_DSTR)) {
13193 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13194 }
13195 }
13196}
13197
13198static NODE *
13199new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13200{
13201 if (!node) {
13202 /* Check string is valid regex */
13203 rb_parser_string_t *str = STRING_NEW0();
13204 reg_compile(p, str, options);
13205 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13206 return node;
13207 }
13208 switch (nd_type(node)) {
13209 case NODE_STR:
13210 {
13211 /* Check string is valid regex */
13212 reg_compile(p, RNODE_STR(node)->string, options);
13213 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13214 }
13215 break;
13216 default:
13217 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13218 /* fall through */
13219 case NODE_DSTR:
13220 nd_set_type(node, NODE_DREGX);
13221 nd_set_loc(node, loc);
13222 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13223 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13224 if (dreg->nd_next) {
13225 dregex_fragment_setenc(p, dreg, options);
13226 }
13227 if (options & RE_OPTION_ONCE) {
13228 node = NEW_ONCE(node, loc);
13229 }
13230 break;
13231 }
13232 return node;
13233}
13234
13235static rb_node_kw_arg_t *
13236new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13237{
13238 if (!k) return 0;
13239 return NEW_KW_ARG((k), loc);
13240}
13241
13242static NODE *
13243new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13244{
13245 if (!node) {
13246 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13247 return xstr;
13248 }
13249 switch (nd_type(node)) {
13250 case NODE_STR:
13251 nd_set_type(node, NODE_XSTR);
13252 nd_set_loc(node, loc);
13253 break;
13254 case NODE_DSTR:
13255 nd_set_type(node, NODE_DXSTR);
13256 nd_set_loc(node, loc);
13257 break;
13258 default:
13259 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13260 break;
13261 }
13262 return node;
13263}
13264
13265static const
13266struct st_hash_type literal_type = {
13267 literal_cmp,
13268 literal_hash,
13269};
13270
13271static int nd_type_st_key_enable_p(NODE *node);
13272
13273static void
13274check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13275{
13276 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13277 if (!arg || !p->case_labels) return;
13278 if (!nd_type_st_key_enable_p(arg)) return;
13279
13280 if (p->case_labels == CHECK_LITERAL_WHEN) {
13281 p->case_labels = st_init_table(&literal_type);
13282 }
13283 else {
13284 st_data_t line;
13285 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13286 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13287 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13288 return;
13289 }
13290 }
13291 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13292}
13293
13294#ifdef RIPPER
13295static int
13296id_is_var(struct parser_params *p, ID id)
13297{
13298 if (is_notop_id(id)) {
13299 switch (id & ID_SCOPE_MASK) {
13300 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13301 return 1;
13302 case ID_LOCAL:
13303 if (dyna_in_block(p)) {
13304 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13305 }
13306 if (local_id(p, id)) return 1;
13307 /* method call without arguments */
13308 return 0;
13309 }
13310 }
13311 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13312 return 0;
13313}
13314#endif
13315
13316static inline enum lex_state_e
13317parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13318{
13319 if (p->debug) {
13320 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13321 }
13322 return p->lex.state = ls;
13323}
13324
13325#ifndef RIPPER
13326static void
13327flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13328{
13329 VALUE mesg = p->debug_buffer;
13330
13331 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13332 p->debug_buffer = Qnil;
13333 rb_io_puts(1, &mesg, out);
13334 }
13335 if (!NIL_P(str) && RSTRING_LEN(str)) {
13336 rb_io_write(p->debug_output, str);
13337 }
13338}
13339
13340static const char rb_parser_lex_state_names[][8] = {
13341 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13342 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13343 "LABEL", "LABELED","FITEM",
13344};
13345
13346static VALUE
13347append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13348{
13349 int i, sep = 0;
13350 unsigned int mask = 1;
13351 static const char none[] = "NONE";
13352
13353 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13354 if ((unsigned)state & mask) {
13355 if (sep) {
13356 rb_str_cat(buf, "|", 1);
13357 }
13358 sep = 1;
13359 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13360 }
13361 }
13362 if (!sep) {
13363 rb_str_cat(buf, none, sizeof(none)-1);
13364 }
13365 return buf;
13366}
13367
13368enum lex_state_e
13369rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13370 enum lex_state_e to, int line)
13371{
13372 VALUE mesg;
13373 mesg = rb_str_new_cstr("lex_state: ");
13374 append_lex_state_name(p, from, mesg);
13375 rb_str_cat_cstr(mesg, " -> ");
13376 append_lex_state_name(p, to, mesg);
13377 rb_str_catf(mesg, " at line %d\n", line);
13378 flush_debug_buffer(p, p->debug_output, mesg);
13379 return to;
13380}
13381
13382VALUE
13383rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13384{
13385 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13386}
13387
13388static void
13389append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13390{
13391 if (stack == 0) {
13392 rb_str_cat_cstr(mesg, "0");
13393 }
13394 else {
13395 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13396 for (; mask && !(stack & mask); mask >>= 1) continue;
13397 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13398 }
13399}
13400
13401void
13402rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13403 const char *name, int line)
13404{
13405 VALUE mesg = rb_sprintf("%s: ", name);
13406 append_bitstack_value(p, stack, mesg);
13407 rb_str_catf(mesg, " at line %d\n", line);
13408 flush_debug_buffer(p, p->debug_output, mesg);
13409}
13410
13411void
13412rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13413{
13414 va_list ap;
13415 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13416
13417 va_start(ap, fmt);
13418 rb_str_vcatf(mesg, fmt, ap);
13419 va_end(ap);
13420 yyerror0(RSTRING_PTR(mesg));
13421 RB_GC_GUARD(mesg);
13422
13423 mesg = rb_str_new(0, 0);
13424 append_lex_state_name(p, p->lex.state, mesg);
13425 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13426 rb_str_resize(mesg, 0);
13427 append_bitstack_value(p, p->cond_stack, mesg);
13428 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13429 rb_str_resize(mesg, 0);
13430 append_bitstack_value(p, p->cmdarg_stack, mesg);
13431 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13432 if (p->debug_output == rb_ractor_stdout())
13433 p->debug_output = rb_ractor_stderr();
13434 p->debug = TRUE;
13435}
13436
13437static YYLTYPE *
13438rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13439{
13440 yylloc->beg_pos.lineno = sourceline;
13441 yylloc->beg_pos.column = beg_pos;
13442 yylloc->end_pos.lineno = sourceline;
13443 yylloc->end_pos.column = end_pos;
13444 return yylloc;
13445}
13446
13447YYLTYPE *
13448rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13449{
13450 int sourceline = here->sourceline;
13451 int beg_pos = parser_column(here->offset - here->quote
13452 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT)));
13453 int end_pos = parser_column(here->offset + here->length + here->quote);
13454
13455 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13456}
13457
13458YYLTYPE *
13459rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13460{
13461 yylloc->beg_pos.lineno = p->delayed.beg_line;
13462 yylloc->beg_pos.column = p->delayed.beg_col;
13463 yylloc->end_pos.lineno = p->delayed.end_line;
13464 yylloc->end_pos.column = p->delayed.end_col;
13465
13466 return yylloc;
13467}
13468
13469YYLTYPE *
13470rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13471{
13472 int sourceline = p->ruby_sourceline;
13473 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13474 int end_pos = parser_column(p->lex.pend - p->lex.pbeg);
13475 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13476}
13477
13478YYLTYPE *
13479rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13480{
13481 yylloc->end_pos = yylloc->beg_pos;
13482
13483 return yylloc;
13484}
13485
13486YYLTYPE *
13487rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13488{
13489 int sourceline = p->ruby_sourceline;
13490 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13491 int end_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13492 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13493}
13494
13495YYLTYPE *
13496rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13497{
13498 int sourceline = p->ruby_sourceline;
13499 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13500 int end_pos = parser_column(p->lex.pcur - p->lex.pbeg);
13501 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13502}
13503#endif /* !RIPPER */
13504
13505static int
13506assignable0(struct parser_params *p, ID id, const char **err)
13507{
13508 if (!id) return -1;
13509 switch (id) {
13510 case keyword_self:
13511 *err = "Can't change the value of self";
13512 return -1;
13513 case keyword_nil:
13514 *err = "Can't assign to nil";
13515 return -1;
13516 case keyword_true:
13517 *err = "Can't assign to true";
13518 return -1;
13519 case keyword_false:
13520 *err = "Can't assign to false";
13521 return -1;
13522 case keyword__FILE__:
13523 *err = "Can't assign to __FILE__";
13524 return -1;
13525 case keyword__LINE__:
13526 *err = "Can't assign to __LINE__";
13527 return -1;
13528 case keyword__ENCODING__:
13529 *err = "Can't assign to __ENCODING__";
13530 return -1;
13531 }
13532 switch (id_type(id)) {
13533 case ID_LOCAL:
13534 if (dyna_in_block(p)) {
13535 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13536 compile_error(p, "Can't assign to numbered parameter _%d",
13537 NUMPARAM_ID_TO_IDX(id));
13538 return -1;
13539 }
13540 if (dvar_curr(p, id)) return NODE_DASGN;
13541 if (dvar_defined(p, id)) return NODE_DASGN;
13542 if (local_id(p, id)) return NODE_LASGN;
13543 dyna_var(p, id);
13544 return NODE_DASGN;
13545 }
13546 else {
13547 if (!local_id(p, id)) local_var(p, id);
13548 return NODE_LASGN;
13549 }
13550 break;
13551 case ID_GLOBAL: return NODE_GASGN;
13552 case ID_INSTANCE: return NODE_IASGN;
13553 case ID_CONST:
13554 if (!p->ctxt.in_def) return NODE_CDECL;
13555 *err = "dynamic constant assignment";
13556 return -1;
13557 case ID_CLASS: return NODE_CVASGN;
13558 default:
13559 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13560 }
13561 return -1;
13562}
13563
13564static NODE*
13565assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13566{
13567 const char *err = 0;
13568 int node_type = assignable0(p, id, &err);
13569 switch (node_type) {
13570 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13571 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13572 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13573 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13574 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13575 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13576 }
13577/* TODO: FIXME */
13578#ifndef RIPPER
13579 if (err) yyerror1(loc, err);
13580#else
13581 if (err) set_value(assign_error(p, err, p->s_lvalue));
13582#endif
13583 return NEW_ERROR(loc);
13584}
13585
13586static int
13587is_private_local_id(struct parser_params *p, ID name)
13588{
13589 VALUE s;
13590 if (name == idUScore) return 1;
13591 if (!is_local_id(name)) return 0;
13592 s = rb_id2str(name);
13593 if (!s) return 0;
13594 return RSTRING_PTR(s)[0] == '_';
13595}
13596
13597static int
13598shadowing_lvar_0(struct parser_params *p, ID name)
13599{
13600 if (dyna_in_block(p)) {
13601 if (dvar_curr(p, name)) {
13602 if (is_private_local_id(p, name)) return 1;
13603 yyerror0("duplicated argument name");
13604 }
13605 else if (dvar_defined(p, name) || local_id(p, name)) {
13606 vtable_add(p->lvtbl->vars, name);
13607 if (p->lvtbl->used) {
13608 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13609 }
13610 return 0;
13611 }
13612 }
13613 else {
13614 if (local_id(p, name)) {
13615 if (is_private_local_id(p, name)) return 1;
13616 yyerror0("duplicated argument name");
13617 }
13618 }
13619 return 1;
13620}
13621
13622static ID
13623shadowing_lvar(struct parser_params *p, ID name)
13624{
13625 shadowing_lvar_0(p, name);
13626 return name;
13627}
13628
13629static void
13630new_bv(struct parser_params *p, ID name)
13631{
13632 if (!name) return;
13633 if (!is_local_id(name)) {
13634 compile_error(p, "invalid local variable - %"PRIsVALUE,
13635 rb_id2str(name));
13636 return;
13637 }
13638 if (!shadowing_lvar_0(p, name)) return;
13639 dyna_var(p, name);
13640 ID *vidp = 0;
13641 if (dvar_defined_ref(p, name, &vidp)) {
13642 if (vidp) *vidp |= LVAR_USED;
13643 }
13644}
13645
13646static void
13647aryset_check(struct parser_params *p, NODE *args)
13648{
13649 NODE *block = 0, *kwds = 0;
13650 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13651 block = RNODE_BLOCK_PASS(args)->nd_body;
13652 args = RNODE_BLOCK_PASS(args)->nd_head;
13653 }
13654 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13655 args = RNODE_ARGSCAT(args)->nd_body;
13656 }
13657 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13658 kwds = RNODE_ARGSPUSH(args)->nd_body;
13659 }
13660 else {
13661 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13662 next = RNODE_LIST(next)->nd_next) {
13663 kwds = RNODE_LIST(next)->nd_head;
13664 }
13665 }
13666 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13667 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13668 }
13669 if (block) {
13670 yyerror1(&block->nd_loc, "block arg given in index assignment");
13671 }
13672}
13673
13674static NODE *
13675aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13676{
13677 aryset_check(p, idx);
13678 return NEW_ATTRASGN(recv, tASET, idx, loc);
13679}
13680
13681static void
13682block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13683{
13684 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13685 compile_error(p, "both block arg and actual block given");
13686 }
13687}
13688
13689static NODE *
13690attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13691{
13692 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13693 return NEW_ATTRASGN(recv, id, 0, loc);
13694}
13695
13696static VALUE
13697rb_backref_error(struct parser_params *p, NODE *node)
13698{
13699#ifndef RIPPER
13700# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13701#else
13702# define ERR(...) rb_sprintf(__VA_ARGS__)
13703#endif
13704 switch (nd_type(node)) {
13705 case NODE_NTH_REF:
13706 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13707 case NODE_BACK_REF:
13708 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13709 }
13710#undef ERR
13711 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13712}
13713
13714static NODE *
13715arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13716{
13717 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13718 switch (nd_type(node1)) {
13719 case NODE_LIST:
13720 return list_append(p, node1, node2);
13721 case NODE_BLOCK_PASS:
13722 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13723 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13724 return node1;
13725 case NODE_ARGSPUSH:
13726 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13727 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13728 nd_set_type(node1, NODE_ARGSCAT);
13729 return node1;
13730 case NODE_ARGSCAT:
13731 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13732 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13733 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13734 return node1;
13735 }
13736 return NEW_ARGSPUSH(node1, node2, loc);
13737}
13738
13739static NODE *
13740arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13741{
13742 if (!node2) return node1;
13743 switch (nd_type(node1)) {
13744 case NODE_BLOCK_PASS:
13745 if (RNODE_BLOCK_PASS(node1)->nd_head)
13746 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13747 else
13748 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13749 return node1;
13750 case NODE_ARGSPUSH:
13751 if (!nd_type_p(node2, NODE_LIST)) break;
13752 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13753 nd_set_type(node1, NODE_ARGSCAT);
13754 return node1;
13755 case NODE_ARGSCAT:
13756 if (!nd_type_p(node2, NODE_LIST) ||
13757 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13758 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13759 return node1;
13760 }
13761 return NEW_ARGSCAT(node1, node2, loc);
13762}
13763
13764static NODE *
13765last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13766{
13767 NODE *n1;
13768 if ((n1 = splat_array(args)) != 0) {
13769 return list_append(p, n1, last_arg);
13770 }
13771 return arg_append(p, args, last_arg, loc);
13772}
13773
13774static NODE *
13775rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13776{
13777 NODE *n1;
13778 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13779 return list_concat(n1, rest_arg);
13780 }
13781 return arg_concat(p, args, rest_arg, loc);
13782}
13783
13784static NODE *
13785splat_array(NODE* node)
13786{
13787 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13788 if (nd_type_p(node, NODE_LIST)) return node;
13789 return 0;
13790}
13791
13792static void
13793mark_lvar_used(struct parser_params *p, NODE *rhs)
13794{
13795 ID *vidp = NULL;
13796 if (!rhs) return;
13797 switch (nd_type(rhs)) {
13798 case NODE_LASGN:
13799 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13800 if (vidp) *vidp |= LVAR_USED;
13801 }
13802 break;
13803 case NODE_DASGN:
13804 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13805 if (vidp) *vidp |= LVAR_USED;
13806 }
13807 break;
13808#if 0
13809 case NODE_MASGN:
13810 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13811 mark_lvar_used(p, rhs->nd_head);
13812 }
13813 break;
13814#endif
13815 }
13816}
13817
13818static int is_static_content(NODE *node);
13819
13820static NODE *
13821node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13822{
13823 if (!lhs) return 0;
13824
13825 switch (nd_type(lhs)) {
13826 case NODE_CDECL:
13827 case NODE_GASGN:
13828 case NODE_IASGN:
13829 case NODE_LASGN:
13830 case NODE_DASGN:
13831 case NODE_MASGN:
13832 case NODE_CVASGN:
13833 set_nd_value(p, lhs, rhs);
13834 nd_set_loc(lhs, loc);
13835 break;
13836
13837 case NODE_ATTRASGN:
13838 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13839 nd_set_loc(lhs, loc);
13840 break;
13841
13842 default:
13843 /* should not happen */
13844 break;
13845 }
13846
13847 return lhs;
13848}
13849
13850static NODE *
13851value_expr_check(struct parser_params *p, NODE *node)
13852{
13853 NODE *void_node = 0, *vn;
13854
13855 if (!node) {
13856 rb_warning0("empty expression");
13857 }
13858 while (node) {
13859 switch (nd_type(node)) {
13860 case NODE_ENSURE:
13861 vn = RNODE_ENSURE(node)->nd_head;
13862 node = RNODE_ENSURE(node)->nd_ensr;
13863 /* nd_ensr should not be NULL, check it out next */
13864 if (vn && (vn = value_expr_check(p, vn))) {
13865 goto found;
13866 }
13867 break;
13868
13869 case NODE_RESCUE:
13870 /* void only if all children are void */
13871 vn = RNODE_RESCUE(node)->nd_head;
13872 if (!vn || !(vn = value_expr_check(p, vn))) {
13873 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13874 }
13875 if (!void_node) void_node = vn;
13876 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13877 if (!nd_type_p(r, NODE_RESBODY)) {
13878 compile_error(p, "unexpected node");
13879 return NULL;
13880 }
13881 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13882 return NULL;
13883 }
13884 if (!void_node) void_node = vn;
13885 }
13886 node = RNODE_RESCUE(node)->nd_else;
13887 if (!node) return void_node;
13888 break;
13889
13890 case NODE_RETURN:
13891 case NODE_BREAK:
13892 case NODE_NEXT:
13893 case NODE_REDO:
13894 case NODE_RETRY:
13895 goto found;
13896
13897 case NODE_CASE:
13898 case NODE_CASE2:
13899 for (node = RNODE_CASE(node)->nd_body;
13900 node && nd_type_p(node, NODE_WHEN);
13901 node = RNODE_WHEN(node)->nd_next) {
13902 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13903 return NULL;
13904 }
13905 if (!void_node) void_node = vn;
13906 }
13907 break;
13908
13909 case NODE_CASE3:
13910 {
13911 NODE *in = RNODE_CASE3(node)->nd_body;
13912 if (!in || !nd_type_p(in, NODE_IN)) {
13913 compile_error(p, "unexpected node");
13914 return NULL;
13915 }
13916 if (!RNODE_IN(in)->nd_body) {
13917 /* single line pattern matching with "=>" operator */
13918 goto found;
13919 }
13920 do {
13921 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13922 if (!vn) return NULL;
13923 if (!void_node) void_node = vn;
13924 in = RNODE_IN(in)->nd_next;
13925 } while (in && nd_type_p(in, NODE_IN));
13926 node = in; /* else */
13927 }
13928 break;
13929
13930 case NODE_BLOCK:
13931 while (RNODE_BLOCK(node)->nd_next) {
13932 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13933 if (vn) return vn;
13934 node = RNODE_BLOCK(node)->nd_next;
13935 }
13936 node = RNODE_BLOCK(node)->nd_head;
13937 break;
13938
13939 case NODE_BEGIN:
13940 node = RNODE_BEGIN(node)->nd_body;
13941 break;
13942
13943 case NODE_IF:
13944 case NODE_UNLESS:
13945 if (!RNODE_IF(node)->nd_body) {
13946 return NULL;
13947 }
13948 else if (!RNODE_IF(node)->nd_else) {
13949 return NULL;
13950 }
13951 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13952 if (!vn) return NULL;
13953 if (!void_node) void_node = vn;
13954 node = RNODE_IF(node)->nd_else;
13955 break;
13956
13957 case NODE_AND:
13958 case NODE_OR:
13959 /* The left operand was already checked for a value when logop()
13960 * built this node, so stop here instead of re-reporting the same
13961 * void value once per operator in a chain. */
13962 return NULL;
13963
13964 case NODE_LASGN:
13965 case NODE_DASGN:
13966 case NODE_MASGN:
13967 mark_lvar_used(p, node);
13968 return NULL;
13969
13970 default:
13971 return NULL;
13972 }
13973 }
13974
13975 return NULL;
13976
13977 found:
13978 /* return the first found node */
13979 return void_node ? void_node : node;
13980}
13981
13982static int
13983value_expr(struct parser_params *p, NODE *node)
13984{
13985 NODE *void_node = value_expr_check(p, node);
13986 if (void_node) {
13987 yyerror1(&void_node->nd_loc, "void value expression");
13988 /* or "control never reach"? */
13989 return FALSE;
13990 }
13991 return TRUE;
13992}
13993
13994static void
13995void_expr(struct parser_params *p, NODE *node)
13996{
13997 const char *useless = 0;
13998
13999 if (!RTEST(ruby_verbose)) return;
14000
14001 if (!node || !(node = nd_once_body(node))) return;
14002 switch (nd_type(node)) {
14003 case NODE_OPCALL:
14004 switch (RNODE_OPCALL(node)->nd_mid) {
14005 case '+':
14006 case '-':
14007 case '*':
14008 case '/':
14009 case '%':
14010 case tPOW:
14011 case tUPLUS:
14012 case tUMINUS:
14013 case '|':
14014 case '^':
14015 case '&':
14016 case tCMP:
14017 case '>':
14018 case tGEQ:
14019 case '<':
14020 case tLEQ:
14021 case tEQ:
14022 case tNEQ:
14023 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
14024 break;
14025 }
14026 break;
14027
14028 case NODE_LVAR:
14029 case NODE_DVAR:
14030 case NODE_GVAR:
14031 case NODE_IVAR:
14032 case NODE_CVAR:
14033 case NODE_NTH_REF:
14034 case NODE_BACK_REF:
14035 useless = "a variable";
14036 break;
14037 case NODE_CONST:
14038 useless = "a constant";
14039 break;
14040 case NODE_SYM:
14041 case NODE_LINE:
14042 case NODE_FILE:
14043 case NODE_ENCODING:
14044 case NODE_INTEGER:
14045 case NODE_FLOAT:
14046 case NODE_RATIONAL:
14047 case NODE_IMAGINARY:
14048 case NODE_STR:
14049 case NODE_DSTR:
14050 case NODE_REGX:
14051 case NODE_DREGX:
14052 useless = "a literal";
14053 break;
14054 case NODE_COLON2:
14055 case NODE_COLON3:
14056 useless = "::";
14057 break;
14058 case NODE_DOT2:
14059 useless = "..";
14060 break;
14061 case NODE_DOT3:
14062 useless = "...";
14063 break;
14064 case NODE_SELF:
14065 useless = "self";
14066 break;
14067 case NODE_NIL:
14068 useless = "nil";
14069 break;
14070 case NODE_TRUE:
14071 useless = "true";
14072 break;
14073 case NODE_FALSE:
14074 useless = "false";
14075 break;
14076 case NODE_DEFINED:
14077 useless = "defined?";
14078 break;
14079 }
14080
14081 if (useless) {
14082 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14083 }
14084}
14085
14086/* warns useless use of block and returns the last statement node */
14087static NODE *
14088void_stmts(struct parser_params *p, NODE *node)
14089{
14090 NODE *const n = node;
14091 if (!RTEST(ruby_verbose)) return n;
14092 if (!node) return n;
14093 if (!nd_type_p(node, NODE_BLOCK)) return n;
14094
14095 while (RNODE_BLOCK(node)->nd_next) {
14096 void_expr(p, RNODE_BLOCK(node)->nd_head);
14097 node = RNODE_BLOCK(node)->nd_next;
14098 }
14099 return RNODE_BLOCK(node)->nd_head;
14100}
14101
14102static NODE *
14103remove_begin(NODE *node)
14104{
14105 NODE **n = &node, *n1 = node;
14106 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14107 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14108 }
14109 return node;
14110}
14111
14112static void
14113reduce_nodes(struct parser_params *p, NODE **body)
14114{
14115 NODE *node = *body;
14116
14117 if (!node) {
14118 *body = NEW_NIL(&NULL_LOC);
14119 return;
14120 }
14121#define subnodes(type, n1, n2) \
14122 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14123 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14124 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14125
14126 while (node) {
14127 int newline = (int)nd_fl_newline(node);
14128 switch (nd_type(node)) {
14129 case NODE_NIL:
14130 // Keep an explicit nil in a method's tail (value) position when it
14131 // is on its own line, so it still emits a :line event and records
14132 // line coverage for that line. [Bug #22302]
14133 if (newline) return;
14134 end:
14135 *body = 0;
14136 return;
14137 case NODE_BEGIN:
14138 *body = node = RNODE_BEGIN(node)->nd_body;
14139 if (newline && node) nd_set_fl_newline(node);
14140 continue;
14141 case NODE_BLOCK:
14142 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14143 break;
14144 case NODE_IF:
14145 case NODE_UNLESS:
14146 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14147 return;
14148 case NODE_CASE:
14149 body = &RNODE_CASE(node)->nd_body;
14150 break;
14151 case NODE_WHEN:
14152 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14153 break;
14154 case NODE_ENSURE:
14155 body = &RNODE_ENSURE(node)->nd_head;
14156 break;
14157 case NODE_RESCUE:
14158 newline = 0; // RESBODY should not be a NEWLINE
14159 if (RNODE_RESCUE(node)->nd_else) {
14160 body = &RNODE_RESCUE(node)->nd_resq;
14161 break;
14162 }
14163 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14164 break;
14165 default:
14166 return;
14167 }
14168 node = *body;
14169 if (newline && node) nd_set_fl_newline(node);
14170 }
14171
14172#undef subnodes
14173}
14174
14175static int
14176is_static_content(NODE *node)
14177{
14178 if (!node) return 1;
14179 switch (nd_type(node)) {
14180 case NODE_HASH:
14181 if (!(node = RNODE_HASH(node)->nd_head)) break;
14182 case NODE_LIST:
14183 do {
14184 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14185 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14186 case NODE_SYM:
14187 case NODE_REGX:
14188 case NODE_LINE:
14189 case NODE_FILE:
14190 case NODE_ENCODING:
14191 case NODE_INTEGER:
14192 case NODE_FLOAT:
14193 case NODE_RATIONAL:
14194 case NODE_IMAGINARY:
14195 case NODE_STR:
14196 case NODE_NIL:
14197 case NODE_TRUE:
14198 case NODE_FALSE:
14199 case NODE_ZLIST:
14200 break;
14201 default:
14202 return 0;
14203 }
14204 return 1;
14205}
14206
14207static int
14208assign_in_cond(struct parser_params *p, NODE *node)
14209{
14210 switch (nd_type(node)) {
14211 case NODE_MASGN:
14212 case NODE_LASGN:
14213 case NODE_DASGN:
14214 case NODE_GASGN:
14215 case NODE_IASGN:
14216 case NODE_CVASGN:
14217 case NODE_CDECL:
14218 break;
14219
14220 default:
14221 return 0;
14222 }
14223
14224 if (!get_nd_value(p, node)) return 1;
14225 if (is_static_content(get_nd_value(p, node))) {
14226 /* reports always */
14227 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14228 }
14229 return 1;
14230}
14231
14232enum cond_type {
14233 COND_IN_OP,
14234 COND_IN_COND,
14235 COND_IN_FF
14236};
14237
14238#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14239 switch (t) { \
14240 case COND_IN_OP: break; \
14241 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14242 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14243 } \
14244} while (0)
14245
14246static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14247
14248static NODE*
14249range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14250{
14251 enum node_type type;
14252
14253 if (node == 0) return 0;
14254
14255 type = nd_type(node);
14256 value_expr(p, node);
14257 if (type == NODE_INTEGER) {
14258 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14259 ID lineno = rb_intern("$.");
14260 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14261 }
14262 return cond0(p, node, COND_IN_FF, loc, true);
14263}
14264
14265static NODE*
14266cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14267{
14268 if (node == 0) return 0;
14269 if (!(node = nd_once_body(node))) return 0;
14270 assign_in_cond(p, node);
14271
14272 switch (nd_type(node)) {
14273 case NODE_BEGIN:
14274 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14275 break;
14276
14277 case NODE_DSTR:
14278 case NODE_EVSTR:
14279 case NODE_STR:
14280 case NODE_FILE:
14281 SWITCH_BY_COND_TYPE(type, warn, "string ");
14282 break;
14283
14284 case NODE_REGX:
14285 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14286 nd_set_type(node, NODE_MATCH);
14287 break;
14288
14289 case NODE_DREGX:
14290 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14291
14292 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14293
14294 case NODE_BLOCK:
14295 {
14296 NODE *end = RNODE_BLOCK(node)->nd_end;
14297 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14298 if (top) top = node == end;
14299 *expr = cond0(p, *expr, type, loc, top);
14300 }
14301 break;
14302
14303 case NODE_AND:
14304 case NODE_OR:
14305 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14306 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14307 break;
14308
14309 case NODE_DOT2:
14310 case NODE_DOT3:
14311 if (!top) break;
14312 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14313 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14314 switch (nd_type(node)) {
14315 case NODE_DOT2:
14316 nd_set_type(node,NODE_FLIP2);
14317 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14318 (void)flip2;
14319 break;
14320 case NODE_DOT3:
14321 nd_set_type(node, NODE_FLIP3);
14322 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14323 (void)flip3;
14324 break;
14325 }
14326 break;
14327
14328 case NODE_SYM:
14329 case NODE_DSYM:
14330 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14331 break;
14332
14333 case NODE_LINE:
14334 case NODE_ENCODING:
14335 case NODE_INTEGER:
14336 case NODE_FLOAT:
14337 case NODE_RATIONAL:
14338 case NODE_IMAGINARY:
14339 SWITCH_BY_COND_TYPE(type, warning, "");
14340 break;
14341
14342 default:
14343 break;
14344 }
14345 return node;
14346}
14347
14348static NODE*
14349cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14350{
14351 if (node == 0) return 0;
14352 return cond0(p, node, COND_IN_COND, loc, true);
14353}
14354
14355static NODE*
14356method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14357{
14358 if (node == 0) return 0;
14359 return cond0(p, node, COND_IN_OP, loc, true);
14360}
14361
14362static NODE*
14363new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14364{
14365 YYLTYPE loc = {*pos, *pos};
14366 return NEW_NIL(&loc);
14367}
14368
14369static NODE*
14370new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc)
14371{
14372 if (!cc) return right;
14373 cc = cond0(p, cc, COND_IN_COND, loc, true);
14374 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14375}
14376
14377static NODE*
14378new_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)
14379{
14380 if (!cc) return right;
14381 cc = cond0(p, cc, COND_IN_COND, loc, true);
14382 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14383}
14384
14385#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))
14386
14387/* A cached logop tail is usable only if it is still a node of the chain's type
14388 * whose nd_2nd is the insertion point, i.e. not itself another node of that
14389 * type. This rejects a stale cache left over from an earlier parse. */
14390static int
14391logop_valid_tail(NODE *tail, enum node_type type)
14392{
14393 NODE *second;
14394 return tail && nd_type_p(tail, type) &&
14395 ((second = RNODE_AND(tail)->nd_2nd) == 0 || !nd_type_p(second, type));
14396}
14397
14398static NODE*
14399logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14400 const YYLTYPE *op_loc, const YYLTYPE *loc)
14401{
14402 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14403 NODE *op;
14404 /* Snapshot the cache from the previous logop() call before overwriting it. */
14405 NODE *prev_head = p->logop.head;
14406 NODE *prev_tail = p->logop.tail;
14407 NODE *node, *second;
14408 value_expr(p, left);
14409 if (left && nd_type_p(left, type)) {
14410 /* The insertion point is the far end of left's right branch.
14411 * Reusing the cached tail keeps a chain such as `a && a && ... && a`
14412 * linear instead of rescanning the whole branch on every operator. The
14413 * cache is validated (its nd_2nd is not another node of the same type)
14414 * so a stale entry falls back to the scan. */
14415 if (left == prev_head && logop_valid_tail(prev_tail, type)) {
14416 node = prev_tail;
14417 }
14418 else {
14419 node = left;
14420 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14421 node = second;
14422 }
14423 }
14424 second = RNODE_AND(node)->nd_2nd;
14425 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14426 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14427 left->nd_loc.end_pos = loc->end_pos;
14428 p->logop.head = left;
14429 p->logop.tail = RNODE_AND(node)->nd_2nd;
14430 return left;
14431 }
14432 op = NEW_AND_OR(type, left, right, loc, op_loc);
14433 nd_set_line(op, op_loc->beg_pos.lineno);
14434 /* Record where the next operator will extend op, mirroring the scan the
14435 * chained branch above would perform: if right is itself a chain of the
14436 * same type (from parentheses), its far end; otherwise op itself. */
14437 p->logop.head = op;
14438 if (right == prev_head && logop_valid_tail(prev_tail, type)) {
14439 p->logop.tail = prev_tail;
14440 }
14441 else if (right && nd_type_p(right, type)) {
14442 node = right;
14443 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14444 node = second;
14445 }
14446 p->logop.tail = node;
14447 }
14448 else {
14449 p->logop.tail = op;
14450 }
14451 return op;
14452}
14453
14454#undef NEW_AND_OR
14455
14456static void
14457no_blockarg(struct parser_params *p, NODE *node)
14458{
14459 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14460 compile_error(p, "block argument should not be given");
14461 }
14462}
14463
14464static NODE *
14465ret_args(struct parser_params *p, NODE *node)
14466{
14467 if (node) {
14468 no_blockarg(p, node);
14469 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14470 node = RNODE_LIST(node)->nd_head;
14471 }
14472 }
14473 return node;
14474}
14475
14476static NODE*
14477negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14478{
14479 switch (nd_type(node)) {
14480 case NODE_INTEGER:
14481 RNODE_INTEGER(node)->minus = TRUE;
14482 break;
14483 case NODE_FLOAT:
14484 RNODE_FLOAT(node)->minus = TRUE;
14485 break;
14486 case NODE_RATIONAL:
14487 RNODE_RATIONAL(node)->minus = TRUE;
14488 break;
14489 case NODE_IMAGINARY:
14490 RNODE_IMAGINARY(node)->minus = TRUE;
14491 break;
14492 }
14493 node->nd_loc = *loc;
14494 return node;
14495}
14496
14497static NODE *
14498arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14499{
14500 if (node2) {
14501 if (!node1) return (NODE *)node2;
14502 node2->nd_head = node1;
14503 nd_set_first_lineno(node2, nd_first_lineno(node1));
14504 nd_set_first_column(node2, nd_first_column(node1));
14505 return (NODE *)node2;
14506 }
14507 return node1;
14508}
14509
14510static bool
14511args_info_empty_p(struct rb_args_info *args)
14512{
14513 if (args->pre_args_num) return false;
14514 if (args->post_args_num) return false;
14515 if (args->rest_arg) return false;
14516 if (args->opt_args) return false;
14517 if (args->block_arg) return false;
14518 if (args->kw_args) return false;
14519 if (args->kw_rest_arg) return false;
14520 return true;
14521}
14522
14523static rb_node_args_t *
14524new_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)
14525{
14526 struct rb_args_info *args = &tail->nd_ainfo;
14527
14528 if (args->forwarding) {
14529 if (rest_arg) {
14530 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14531 return tail;
14532 }
14533 rest_arg = idFWD_REST;
14534 }
14535
14536 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14537 args->pre_init = pre_args ? pre_args->nd_next : 0;
14538
14539 args->post_args_num = post_args ? post_args->nd_plen : 0;
14540 args->post_init = post_args ? post_args->nd_next : 0;
14541 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14542
14543 args->rest_arg = rest_arg;
14544
14545 args->opt_args = opt_args;
14546
14547 nd_set_loc(RNODE(tail), loc);
14548
14549 return tail;
14550}
14551
14552static rb_node_args_t *
14553new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14554{
14555 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14556 struct rb_args_info *args = &node->nd_ainfo;
14557 if (p->error_p) return node;
14558
14559 if (block == idNil) {
14560 block = 0;
14561 args->no_blockarg = TRUE;
14562 }
14563 args->block_arg = block;
14564 args->kw_args = kw_args;
14565
14566 if (kw_args) {
14567 /*
14568 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14569 * variable order: k1, kr1, k2, &b, internal_id, krest
14570 * #=> <reorder>
14571 * variable order: kr1, k1, k2, internal_id, krest, &b
14572 */
14573 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14574 struct vtable *vtargs = p->lvtbl->args;
14575 rb_node_kw_arg_t *kwn = kw_args;
14576
14577 if (block) block = vtargs->tbl[vtargs->pos-1];
14578 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14579 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14580 while (kwn) {
14581 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14582 --kw_vars;
14583 --required_kw_vars;
14584 kwn = kwn->nd_next;
14585 }
14586
14587 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14588 ID vid = get_nd_vid(p, kwn->nd_body);
14589 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14590 *required_kw_vars++ = vid;
14591 }
14592 else {
14593 *kw_vars++ = vid;
14594 }
14595 }
14596
14597 arg_var(p, kw_bits);
14598 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14599 if (block) arg_var(p, block);
14600
14601 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14602 }
14603 else if (kw_rest_arg == idNil) {
14604 args->no_kwarg = 1;
14605 }
14606 else if (kw_rest_arg) {
14607 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14608 }
14609
14610 return node;
14611}
14612
14613static rb_node_args_t *
14614args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14615{
14616 if (max_numparam > NO_PARAM || it_id) {
14617 if (!args) {
14618 YYLTYPE loc = RUBY_INIT_YYLLOC();
14619 args = new_empty_args_tail(p, 0);
14620 nd_set_loc(RNODE(args), &loc);
14621 }
14622 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14623 }
14624 return args;
14625}
14626
14627static NODE*
14628new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14629{
14630 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14631
14632 if (pre_arg) {
14633 NODE *pre_args = NEW_LIST(pre_arg, loc);
14634 if (RNODE_ARYPTN(aryptn)->pre_args) {
14635 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14636 }
14637 else {
14638 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14639 }
14640 }
14641 return aryptn;
14642}
14643
14644static NODE*
14645new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14646{
14647 if (has_rest) {
14648 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14649 }
14650 else {
14651 rest_arg = NULL;
14652 }
14653 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14654
14655 return node;
14656}
14657
14658static NODE*
14659new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14660{
14661 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14662
14663 return fndptn;
14664}
14665
14666static NODE*
14667new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14668{
14669 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14670 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14671 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14672
14673 return node;
14674}
14675
14676static NODE*
14677new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14678{
14679 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14680 return hshptn;
14681}
14682
14683static NODE*
14684new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14685{
14686 NODE *node, *kw_rest_arg_node;
14687
14688 if (kw_rest_arg == idNil) {
14689 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14690 }
14691 else if (kw_rest_arg) {
14692 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14693 }
14694 else {
14695 kw_rest_arg_node = NULL;
14696 }
14697
14698 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14699
14700 return node;
14701}
14702
14703static NODE*
14704dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14705{
14706 if (!node) {
14707 return NEW_SYM(STR_NEW0(), loc);
14708 }
14709
14710 switch (nd_type(node)) {
14711 case NODE_DSTR:
14712 nd_set_type(node, NODE_DSYM);
14713 nd_set_loc(node, loc);
14714 break;
14715 case NODE_STR:
14716 node = str_to_sym_node(p, node, loc);
14717 break;
14718 default:
14719 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14720 break;
14721 }
14722 return node;
14723}
14724
14725static int
14726nd_type_st_key_enable_p(NODE *node)
14727{
14728 switch (nd_type(node)) {
14729 case NODE_INTEGER:
14730 case NODE_FLOAT:
14731 case NODE_RATIONAL:
14732 case NODE_IMAGINARY:
14733 case NODE_STR:
14734 case NODE_SYM:
14735 case NODE_REGX:
14736 case NODE_LINE:
14737 case NODE_FILE:
14738 case NODE_ENCODING:
14739 return true;
14740 default:
14741 return false;
14742 }
14743}
14744
14745static VALUE
14746nd_value(struct parser_params *p, NODE *node)
14747{
14748 switch (nd_type(node)) {
14749 case NODE_STR:
14750 return rb_node_str_string_val(node);
14751 case NODE_INTEGER:
14752 return rb_node_integer_literal_val(node);
14753 case NODE_FLOAT:
14754 return rb_node_float_literal_val(node);
14755 case NODE_RATIONAL:
14756 return rb_node_rational_literal_val(node);
14757 case NODE_IMAGINARY:
14758 return rb_node_imaginary_literal_val(node);
14759 case NODE_SYM:
14760 return rb_node_sym_string_val(node);
14761 case NODE_REGX:
14762 return rb_node_regx_string_val(node);
14763 case NODE_LINE:
14764 return rb_node_line_lineno_val(node);
14765 case NODE_ENCODING:
14766 return rb_node_encoding_val(node);
14767 case NODE_FILE:
14768 return rb_node_file_path_val(node);
14769 default:
14770 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14771 UNREACHABLE_RETURN(0);
14772 }
14773}
14774
14775static void
14776warn_duplicate_keys(struct parser_params *p, NODE *hash)
14777{
14778 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14779 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14780 while (hash && RNODE_LIST(hash)->nd_next) {
14781 NODE *head = RNODE_LIST(hash)->nd_head;
14782 NODE *value = RNODE_LIST(hash)->nd_next;
14783 NODE *next = RNODE_LIST(value)->nd_next;
14784 st_data_t key;
14785 st_data_t data;
14786
14787 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14788 if (!head) {
14789 head = value;
14790 }
14791
14792 if (nd_type_st_key_enable_p(head)) {
14793 key = (st_data_t)head;
14794
14795 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14796 rb_warn2L(nd_line((NODE *)data),
14797 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14798 nd_value(p, head), WARN_I(nd_line(head)));
14799 }
14800 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14801 }
14802 hash = next;
14803 }
14804 st_free_table(p->warn_duplicate_keys_table);
14805 p->warn_duplicate_keys_table = NULL;
14806}
14807
14808static NODE *
14809new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14810{
14811 if (hash) warn_duplicate_keys(p, hash);
14812 return NEW_HASH(hash, loc);
14813}
14814
14815static void
14816error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14817{
14818 if (is_private_local_id(p, id)) {
14819 return;
14820 }
14821 if (st_is_member(p->pvtbl, id)) {
14822 yyerror1(loc, "duplicated variable name");
14823 }
14824 else if (p->ctxt.in_alt_pattern && id) {
14825 yyerror1(loc, "variable capture in alternative pattern");
14826 }
14827 else {
14828 p->ctxt.capture_in_pattern = 1;
14829 st_insert(p->pvtbl, (st_data_t)id, 0);
14830 }
14831}
14832
14833static void
14834error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14835{
14836 if (!p->pktbl) {
14837 p->pktbl = st_init_numtable();
14838 }
14839 else if (st_is_member(p->pktbl, key)) {
14840 yyerror1(loc, "duplicated key name");
14841 return;
14842 }
14843 st_insert(p->pktbl, (st_data_t)key, 0);
14844}
14845
14846static NODE *
14847new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14848{
14849 return NEW_HASH(hash, loc);
14850}
14851
14852static NODE *
14853new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14854{
14855 NODE *asgn;
14856
14857 if (lhs) {
14858 ID vid = get_nd_vid(p, lhs);
14859 YYLTYPE lhs_loc = lhs->nd_loc;
14860 /* A constant read can raise NameError. Prism has no separate read node
14861 * for `Const op= value` and reports the whole expression, so give the
14862 * NODE_CONST the location of the whole operator assignment as well,
14863 * for consistent Thread::Backtrace::Location#source_range between both
14864 * parsers. */
14865 const YYLTYPE *read_loc = nd_type_p(lhs, NODE_CDECL) ? loc : &lhs_loc;
14866 if (op == tOROP) {
14867 set_nd_value(p, lhs, rhs);
14868 nd_set_loc(lhs, loc);
14869 asgn = NEW_OP_ASGN_OR(gettable(p, vid, read_loc), lhs, loc);
14870 }
14871 else if (op == tANDOP) {
14872 set_nd_value(p, lhs, rhs);
14873 nd_set_loc(lhs, loc);
14874 asgn = NEW_OP_ASGN_AND(gettable(p, vid, read_loc), lhs, loc);
14875 }
14876 else {
14877 asgn = lhs;
14878 rhs = NEW_CALL(gettable(p, vid, read_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14879 set_nd_value(p, asgn, rhs);
14880 nd_set_loc(asgn, loc);
14881 }
14882 }
14883 else {
14884 asgn = NEW_ERROR(loc);
14885 }
14886 return asgn;
14887}
14888
14889static NODE *
14890new_ary_op_assign(struct parser_params *p, NODE *ary,
14891 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14892 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14893{
14894 NODE *asgn;
14895
14896 aryset_check(p, args);
14897 args = make_list(args, args_loc);
14898 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14899 fixpos(asgn, ary);
14900 return asgn;
14901}
14902
14903static NODE *
14904new_attr_op_assign(struct parser_params *p, NODE *lhs,
14905 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14906 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14907{
14908 NODE *asgn;
14909
14910 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14911 fixpos(asgn, lhs);
14912 return asgn;
14913}
14914
14915static NODE *
14916new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14917{
14918 NODE *asgn;
14919
14920 if (lhs) {
14921 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14922 }
14923 else {
14924 asgn = NEW_ERROR(loc);
14925 }
14926 fixpos(asgn, lhs);
14927 return asgn;
14928}
14929
14930static NODE *
14931const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14932{
14933 if (p->ctxt.in_def) {
14934#ifndef RIPPER
14935 yyerror1(loc, "dynamic constant assignment");
14936#else
14937 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14938#endif
14939 }
14940 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14941}
14942
14943#ifdef RIPPER
14944static VALUE
14945assign_error(struct parser_params *p, const char *mesg, VALUE a)
14946{
14947 a = dispatch2(assign_error, ERR_MESG(), a);
14948 ripper_error(p);
14949 return a;
14950}
14951#endif
14952
14953static NODE *
14954new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14955{
14956 NODE *result = head;
14957 if (rescue) {
14958 NODE *tmp = rescue_else ? rescue_else : rescue;
14959 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14960
14961 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14962 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14963 }
14964 if (ensure) {
14965 result = NEW_ENSURE(result, ensure, loc);
14966 }
14967 fixpos(result, head);
14968 return result;
14969}
14970
14971static void
14972warn_unused_var(struct parser_params *p, struct local_vars *local)
14973{
14974 int cnt;
14975
14976 if (!local->used) return;
14977 cnt = local->used->pos;
14978 if (cnt != local->vars->pos) {
14979 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14980 }
14981#ifndef RIPPER
14982 ID *v = local->vars->tbl;
14983 ID *u = local->used->tbl;
14984 for (int i = 0; i < cnt; ++i) {
14985 if (!v[i] || (u[i] & LVAR_USED)) continue;
14986 if (is_private_local_id(p, v[i])) continue;
14987 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14988 }
14989#endif
14990}
14991
14992static void
14993local_push(struct parser_params *p, int toplevel_scope)
14994{
14995 struct local_vars *local;
14996 int inherits_dvars = toplevel_scope && compile_for_eval;
14997 int warn_unused_vars = RTEST(ruby_verbose);
14998
14999 local = ALLOC(struct local_vars);
15000 local->prev = p->lvtbl;
15001 local->args = vtable_alloc(0);
15002 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
15003#ifndef RIPPER
15004 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
15005 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
15006#endif
15007 local->numparam.outer = 0;
15008 local->numparam.inner = 0;
15009 local->numparam.current = 0;
15010 local->it = 0;
15011 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
15012
15013# if WARN_PAST_SCOPE
15014 local->past = 0;
15015# endif
15016 CMDARG_PUSH(0);
15017 COND_PUSH(0);
15018 p->lvtbl = local;
15019}
15020
15021static void
15022vtable_chain_free(struct parser_params *p, struct vtable *table)
15023{
15024 while (!DVARS_TERMINAL_P(table)) {
15025 struct vtable *cur_table = table;
15026 table = cur_table->prev;
15027 vtable_free(cur_table);
15028 }
15029}
15030
15031static void
15032local_free(struct parser_params *p, struct local_vars *local)
15033{
15034 vtable_chain_free(p, local->used);
15035
15036# if WARN_PAST_SCOPE
15037 vtable_chain_free(p, local->past);
15038# endif
15039
15040 vtable_chain_free(p, local->args);
15041 vtable_chain_free(p, local->vars);
15042
15043 ruby_xfree_sized(local, sizeof(struct local_vars));
15044}
15045
15046static void
15047local_pop(struct parser_params *p)
15048{
15049 struct local_vars *local = p->lvtbl->prev;
15050 if (p->lvtbl->used) {
15051 warn_unused_var(p, p->lvtbl);
15052 }
15053
15054 local_free(p, p->lvtbl);
15055 p->lvtbl = local;
15056
15057 CMDARG_POP();
15058 COND_POP();
15059}
15060
15061static rb_ast_id_table_t *
15062local_tbl(struct parser_params *p)
15063{
15064 int cnt_args = vtable_size(p->lvtbl->args);
15065 int cnt_vars = vtable_size(p->lvtbl->vars);
15066 int cnt = cnt_args + cnt_vars;
15067 int i, j;
15068 rb_ast_id_table_t *tbl;
15069
15070 if (cnt <= 0) return 0;
15071 tbl = rb_ast_new_local_table(p->ast, cnt);
15072 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
15073 /* remove IDs duplicated to warn shadowing */
15074 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15075 ID id = p->lvtbl->vars->tbl[i];
15076 if (!vtable_included(p->lvtbl->args, id)) {
15077 tbl->ids[j++] = id;
15078 }
15079 }
15080 if (j < cnt) {
15081 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15082 }
15083
15084 return tbl;
15085}
15086
15087static void
15088numparam_name(struct parser_params *p, ID id)
15089{
15090 if (!NUMPARAM_ID_P(id)) return;
15091 compile_error(p, "_%d is reserved for numbered parameter",
15092 NUMPARAM_ID_TO_IDX(id));
15093}
15094
15095static void
15096arg_var(struct parser_params *p, ID id)
15097{
15098 numparam_name(p, id);
15099 vtable_add(p->lvtbl->args, id);
15100}
15101
15102static void
15103local_var(struct parser_params *p, ID id)
15104{
15105 numparam_name(p, id);
15106 vtable_add(p->lvtbl->vars, id);
15107 if (p->lvtbl->used) {
15108 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15109 }
15110}
15111
15112#ifndef RIPPER
15113int
15114rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15115{
15116 return rb_local_defined(id, iseq);
15117}
15118#endif
15119
15120static int
15121local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15122{
15123 struct vtable *vars, *args, *used;
15124
15125 vars = p->lvtbl->vars;
15126 args = p->lvtbl->args;
15127 used = p->lvtbl->used;
15128
15129 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15130 vars = vars->prev;
15131 args = args->prev;
15132 if (used) used = used->prev;
15133 }
15134
15135 if (vars && vars->prev == DVARS_INHERIT) {
15136 return rb_parser_local_defined(p, id, p->parent_iseq);
15137 }
15138 else if (vtable_included(args, id)) {
15139 return 1;
15140 }
15141 else {
15142 int i = vtable_included(vars, id);
15143 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15144 return i != 0;
15145 }
15146}
15147
15148static int
15149local_id(struct parser_params *p, ID id)
15150{
15151 return local_id_ref(p, id, NULL);
15152}
15153
15154static int
15155check_forwarding_args(struct parser_params *p)
15156{
15157 if (local_id(p, idFWD_ALL)) return TRUE;
15158 compile_error(p, "unexpected ...");
15159 return FALSE;
15160}
15161
15162static void
15163add_forwarding_args(struct parser_params *p)
15164{
15165 arg_var(p, idFWD_REST);
15166 arg_var(p, idFWD_KWREST);
15167 arg_var(p, idFWD_BLOCK);
15168 arg_var(p, idFWD_ALL);
15169}
15170
15171static void
15172forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15173{
15174 bool conflict = false;
15175
15176 struct vtable *vars, *args;
15177
15178 vars = p->lvtbl->vars;
15179 args = p->lvtbl->args;
15180
15181 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15182 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15183 vars = vars->prev;
15184 args = args->prev;
15185 }
15186
15187 bool found = false;
15188 if (vars && vars->prev == DVARS_INHERIT && !found) {
15189 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15190 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15191 }
15192 else {
15193 found = (vtable_included(args, arg) &&
15194 !(all && vtable_included(args, all)));
15195 }
15196
15197 if (!found) {
15198 compile_error(p, "no anonymous %s parameter", var);
15199 }
15200 else if (conflict) {
15201 compile_error(p, "anonymous %s parameter is also used within block", var);
15202 }
15203}
15204
15205static NODE *
15206new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15207{
15208 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15209 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15210 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15211 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15212 block->forwarding = TRUE;
15213 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15214 return arg_blk_pass(args, block);
15215}
15216
15217static NODE *
15218numparam_push(struct parser_params *p)
15219{
15220 struct local_vars *local = p->lvtbl;
15221 NODE *inner = local->numparam.inner;
15222 if (!local->numparam.outer) {
15223 local->numparam.outer = local->numparam.current;
15224 }
15225 local->numparam.inner = 0;
15226 local->numparam.current = 0;
15227 local->it = 0;
15228 return inner;
15229}
15230
15231static void
15232numparam_pop(struct parser_params *p, NODE *prev_inner)
15233{
15234 struct local_vars *local = p->lvtbl;
15235 if (prev_inner) {
15236 /* prefer first one */
15237 local->numparam.inner = prev_inner;
15238 }
15239 else if (local->numparam.current) {
15240 /* current and inner are exclusive */
15241 local->numparam.inner = local->numparam.current;
15242 }
15243 if (p->max_numparam > NO_PARAM) {
15244 /* current and outer are exclusive */
15245 local->numparam.current = local->numparam.outer;
15246 local->numparam.outer = 0;
15247 }
15248 else {
15249 /* no numbered parameter */
15250 local->numparam.current = 0;
15251 }
15252 local->it = 0;
15253}
15254
15255static const struct vtable *
15256dyna_push(struct parser_params *p)
15257{
15258 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15259 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15260 if (p->lvtbl->used) {
15261 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15262 }
15263 return p->lvtbl->args;
15264}
15265
15266static void
15267dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15268{
15269 struct vtable *tmp = *vtblp;
15270 *vtblp = tmp->prev;
15271# if WARN_PAST_SCOPE
15272 if (p->past_scope_enabled) {
15273 tmp->prev = p->lvtbl->past;
15274 p->lvtbl->past = tmp;
15275 return;
15276 }
15277# endif
15278 vtable_free(tmp);
15279}
15280
15281static void
15282dyna_pop_1(struct parser_params *p)
15283{
15284 struct vtable *tmp;
15285
15286 if ((tmp = p->lvtbl->used) != 0) {
15287 warn_unused_var(p, p->lvtbl);
15288 p->lvtbl->used = p->lvtbl->used->prev;
15289 vtable_free(tmp);
15290 }
15291 dyna_pop_vtable(p, &p->lvtbl->args);
15292 dyna_pop_vtable(p, &p->lvtbl->vars);
15293}
15294
15295static void
15296dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15297{
15298 while (p->lvtbl->args != lvargs) {
15299 dyna_pop_1(p);
15300 if (!p->lvtbl->args) {
15301 struct local_vars *local = p->lvtbl->prev;
15302 ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl));
15303 p->lvtbl = local;
15304 }
15305 }
15306 dyna_pop_1(p);
15307}
15308
15309static int
15310dyna_in_block(struct parser_params *p)
15311{
15312 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15313}
15314
15315#ifndef RIPPER
15316int
15317dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15318{
15319 struct vtable *vars, *args, *used;
15320 int i;
15321
15322 args = p->lvtbl->args;
15323 vars = p->lvtbl->vars;
15324 used = p->lvtbl->used;
15325
15326 while (!DVARS_TERMINAL_P(vars)) {
15327 if (vtable_included(args, id)) {
15328 return 1;
15329 }
15330 if ((i = vtable_included(vars, id)) != 0) {
15331 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15332 return 1;
15333 }
15334 args = args->prev;
15335 vars = vars->prev;
15336 if (!vidrefp) used = 0;
15337 if (used) used = used->prev;
15338 }
15339
15340 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15341 return rb_dvar_defined(id, p->parent_iseq);
15342 }
15343
15344 return 0;
15345}
15346#endif
15347
15348static int
15349dvar_defined(struct parser_params *p, ID id)
15350{
15351 return dvar_defined_ref(p, id, NULL);
15352}
15353
15354static int
15355dvar_curr(struct parser_params *p, ID id)
15356{
15357 return (vtable_included(p->lvtbl->args, id) ||
15358 vtable_included(p->lvtbl->vars, id));
15359}
15360
15361static void
15362reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15363{
15364 compile_error(p,
15365 "regexp encoding option '%c' differs from source encoding '%s'",
15366 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15367}
15368
15369#ifndef RIPPER
15370static rb_encoding *
15371find_enc(struct parser_params* p, const char *name)
15372{
15373 int idx = rb_enc_find_index(name);
15374 if (idx < 0) {
15375 rb_bug("unknown encoding name: %s", name);
15376 }
15377
15378 return rb_enc_from_index(idx);
15379}
15380
15381static rb_encoding *
15382kcode_to_enc(struct parser_params* p, int kcode)
15383{
15384 rb_encoding *enc;
15385
15386 switch (kcode) {
15387 case ENC_ASCII8BIT:
15388 enc = rb_ascii8bit_encoding();
15389 break;
15390 case ENC_EUC_JP:
15391 enc = find_enc(p, "EUC-JP");
15392 break;
15393 case ENC_Windows_31J:
15394 enc = find_enc(p, "Windows-31J");
15395 break;
15396 case ENC_UTF8:
15397 enc = rb_utf8_encoding();
15398 break;
15399 default:
15400 enc = NULL;
15401 break;
15402 }
15403
15404 return enc;
15405}
15406
15407int
15408rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15409{
15410 int c = RE_OPTION_ENCODING_IDX(options);
15411
15412 if (c) {
15413 int opt, idx;
15414 rb_encoding *enc;
15415
15416 char_to_option_kcode(c, &opt, &idx);
15417 enc = kcode_to_enc(p, idx);
15418 if (enc != rb_parser_str_get_encoding(str) &&
15419 !rb_parser_is_ascii_string(p, str)) {
15420 goto error;
15421 }
15422 rb_parser_string_set_encoding(str, enc);
15423 }
15424 else if (RE_OPTION_ENCODING_NONE(options)) {
15425 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15426 !rb_parser_is_ascii_string(p, str)) {
15427 c = 'n';
15428 goto error;
15429 }
15430 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15431 }
15432 else if (rb_is_usascii_enc(p->enc)) {
15433 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15434 }
15435 return 0;
15436
15437 error:
15438 return c;
15439}
15440#endif
15441
15442static void
15443reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15444{
15445 int c = rb_reg_fragment_setenc(p, str, options);
15446 if (c) reg_fragment_enc_error(p, str, c);
15447}
15448
15449#ifndef UNIVERSAL_PARSER
15450typedef struct {
15451 struct parser_params* parser;
15452 rb_encoding *enc;
15453 NODE *succ_block;
15454 const YYLTYPE *loc;
15455 rb_parser_assignable_func assignable;
15456} reg_named_capture_assign_t;
15457
15458static int
15459reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15460 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15461{
15462 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15463 struct parser_params* p = arg->parser;
15464 rb_encoding *enc = arg->enc;
15465 long len = name_end - name;
15466 const char *s = (const char *)name;
15467
15468 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15469}
15470
15471static NODE *
15472reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15473{
15474 reg_named_capture_assign_t arg;
15475
15476 arg.parser = p;
15477 arg.enc = rb_enc_get(regexp);
15478 arg.succ_block = 0;
15479 arg.loc = loc;
15480 arg.assignable = assignable;
15481 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15482
15483 if (!arg.succ_block) return 0;
15484 return RNODE_BLOCK(arg.succ_block)->nd_next;
15485}
15486#endif
15487
15488#ifndef RIPPER
15489NODE *
15490rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15491{
15492 return assignable(p, id, val, loc);
15493}
15494
15495int
15496rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15497 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15498{
15499 ID var;
15500 NODE *node, *succ;
15501
15502 if (!len) return ST_CONTINUE;
15503 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15504 return ST_CONTINUE;
15505
15506 var = intern_cstr(s, len, enc);
15507 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15508 if (!lvar_defined(p, var)) return ST_CONTINUE;
15509 }
15510 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15511 succ = *succ_block;
15512 if (!succ) succ = NEW_ERROR(loc);
15513 succ = block_append(p, succ, node);
15514 *succ_block = succ;
15515 return ST_CONTINUE;
15516}
15517#endif
15518
15519static VALUE
15520parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15521{
15522 VALUE str2;
15523 reg_fragment_setenc(p, str, options);
15524 str2 = rb_str_new_parser_string(str);
15525 return rb_parser_reg_compile(p, str2, options);
15526}
15527
15528#ifndef RIPPER
15529VALUE
15530rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15531{
15532 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15533}
15534#endif
15535
15536static VALUE
15537reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15538{
15539 VALUE re;
15540 VALUE err;
15541
15542 err = rb_errinfo();
15543 re = parser_reg_compile(p, str, options);
15544 if (NIL_P(re)) {
15545 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15546 rb_set_errinfo(err);
15547 compile_error(p, "%"PRIsVALUE, m);
15548 return Qnil;
15549 }
15550 return re;
15551}
15552
15553#ifndef RIPPER
15554void
15555rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15556{
15557 p->do_print = print;
15558 p->do_loop = loop;
15559 p->do_chomp = chomp;
15560 p->do_split = split;
15561}
15562
15563static NODE *
15564parser_append_options(struct parser_params *p, NODE *node)
15565{
15566 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15567 const YYLTYPE *const LOC = &default_location;
15568
15569 if (p->do_print) {
15570 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15571 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15572 LOC);
15573 node = block_append(p, node, print);
15574 }
15575
15576 if (p->do_loop) {
15577 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15578
15579 if (p->do_split) {
15580 ID ifs = rb_intern("$;");
15581 ID fields = rb_intern("$F");
15582 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15583 NODE *split = NEW_GASGN(fields,
15584 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15585 rb_intern("split"), args, LOC),
15586 LOC);
15587 node = block_append(p, split, node);
15588 }
15589 if (p->do_chomp) {
15590 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15591 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15592 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15593 }
15594
15595 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15596 }
15597
15598 return node;
15599}
15600
15601void
15602rb_init_parse(void)
15603{
15604 /* just to suppress unused-function warnings */
15605 (void)nodetype;
15606 (void)nodeline;
15607}
15608
15609ID
15610internal_id(struct parser_params *p)
15611{
15612 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15613}
15614#endif /* !RIPPER */
15615
15616static void
15617parser_initialize(struct parser_params *p)
15618{
15619 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15620 p->command_start = TRUE;
15621 p->ruby_sourcefile_string = Qnil;
15622 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15623 string_buffer_init(p);
15624 p->node_id = 0;
15625 p->delayed.token = NULL;
15626 p->frozen_string_literal = -1; /* not specified */
15627 rb_source_hash_init(&p->source_hash);
15628#ifndef RIPPER
15629 p->error_buffer = Qfalse;
15630 p->end_expect_token_locations = NULL;
15631 p->token_id = 0;
15632 p->tokens = NULL;
15633#else
15634 p->result = Qnil;
15635 p->parsing_thread = Qnil;
15636 p->s_value = Qnil;
15637 p->s_lvalue = Qnil;
15638 p->s_value_stack = rb_ary_new();
15639#endif
15640 p->debug_buffer = Qnil;
15641 p->debug_output = rb_ractor_stdout();
15642 p->enc = rb_utf8_encoding();
15643 p->exits = 0;
15644}
15645
15646#ifdef RIPPER
15647#define rb_ruby_parser_mark ripper_parser_mark
15648#define rb_ruby_parser_free ripper_parser_free
15649#define rb_ruby_parser_memsize ripper_parser_memsize
15650#endif
15651
15652void
15653rb_ruby_parser_mark(void *ptr)
15654{
15655 struct parser_params *p = (struct parser_params*)ptr;
15656
15657 rb_gc_mark(p->ruby_sourcefile_string);
15658#ifndef RIPPER
15659 rb_gc_mark(p->error_buffer);
15660#else
15661 rb_gc_mark(p->value);
15662 rb_gc_mark(p->result);
15663 rb_gc_mark(p->parsing_thread);
15664 rb_gc_mark(p->s_value);
15665 rb_gc_mark(p->s_lvalue);
15666 rb_gc_mark(p->s_value_stack);
15667#endif
15668 rb_gc_mark(p->debug_buffer);
15669 rb_gc_mark(p->debug_output);
15670}
15671
15672void
15673rb_ruby_parser_free(void *ptr)
15674{
15675 struct parser_params *p = (struct parser_params*)ptr;
15676 struct local_vars *local, *prev;
15677
15678 if (p->ast) {
15679 rb_ast_free(p->ast);
15680 }
15681
15682 if (p->warn_duplicate_keys_table) {
15683 st_free_table(p->warn_duplicate_keys_table);
15684 }
15685
15686#ifndef RIPPER
15687 if (p->tokens) {
15688 rb_parser_ary_free(p, p->tokens);
15689 }
15690#endif
15691
15692 if (p->tokenbuf) {
15693 ruby_xfree_sized(p->tokenbuf, p->toksiz);
15694 }
15695
15696 for (local = p->lvtbl; local; local = prev) {
15697 prev = local->prev;
15698 local_free(p, local);
15699 }
15700
15701 {
15702 token_info *ptinfo;
15703 while ((ptinfo = p->token_info) != 0) {
15704 p->token_info = ptinfo->next;
15705 xfree(ptinfo);
15706 }
15707 }
15708 string_buffer_free(p);
15709
15710 if (p->pvtbl) {
15711 st_free_table(p->pvtbl);
15712 }
15713
15714 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15715 st_free_table(p->case_labels);
15716 }
15717
15718 xfree(p->lex.strterm);
15719 p->lex.strterm = 0;
15720
15721 xfree(ptr);
15722}
15723
15724size_t
15725rb_ruby_parser_memsize(const void *ptr)
15726{
15727 struct parser_params *p = (struct parser_params*)ptr;
15728 struct local_vars *local;
15729 size_t size = sizeof(*p);
15730
15731 size += p->toksiz;
15732 for (local = p->lvtbl; local; local = local->prev) {
15733 size += sizeof(*local);
15734 if (local->vars) size += local->vars->capa * sizeof(ID);
15735 }
15736 return size;
15737}
15738
15739#ifndef RIPPER
15740#undef rb_reserved_word
15741
15742const struct kwtable *
15743rb_reserved_word(const char *str, unsigned int len)
15744{
15745 return reserved_word(str, len);
15746}
15747
15748#ifdef UNIVERSAL_PARSER
15749rb_parser_t *
15750rb_ruby_parser_allocate(const rb_parser_config_t *config)
15751{
15752 /* parser_initialize expects fields to be set to 0 */
15753 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15754 p->config = config;
15755 return p;
15756}
15757
15758rb_parser_t *
15759rb_ruby_parser_new(const rb_parser_config_t *config)
15760{
15761 /* parser_initialize expects fields to be set to 0 */
15762 rb_parser_t *p = rb_ruby_parser_allocate(config);
15763 parser_initialize(p);
15764 return p;
15765}
15766#else
15767rb_parser_t *
15768rb_ruby_parser_allocate(void)
15769{
15770 /* parser_initialize expects fields to be set to 0 */
15771 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15772 return p;
15773}
15774
15775rb_parser_t *
15776rb_ruby_parser_new(void)
15777{
15778 /* parser_initialize expects fields to be set to 0 */
15779 rb_parser_t *p = rb_ruby_parser_allocate();
15780 parser_initialize(p);
15781 return p;
15782}
15783#endif
15784
15785rb_parser_t *
15786rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15787{
15788 p->error_buffer = main ? Qfalse : Qnil;
15789 p->parent_iseq = base;
15790 return p;
15791}
15792
15793void
15794rb_ruby_parser_set_script_lines(rb_parser_t *p)
15795{
15796 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15797}
15798
15799void
15800rb_ruby_parser_error_tolerant(rb_parser_t *p)
15801{
15802 p->error_tolerant = 1;
15803}
15804
15805void
15806rb_ruby_parser_keep_tokens(rb_parser_t *p)
15807{
15808 p->keep_tokens = 1;
15809 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15810}
15811
15812rb_encoding *
15813rb_ruby_parser_encoding(rb_parser_t *p)
15814{
15815 return p->enc;
15816}
15817
15818int
15819rb_ruby_parser_end_seen_p(rb_parser_t *p)
15820{
15821 return p->ruby__end__seen;
15822}
15823
15824int
15825rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15826{
15827 p->debug = flag;
15828 return flag;
15829}
15830#endif /* !RIPPER */
15831
15832#ifdef RIPPER
15833int
15834rb_ruby_parser_get_yydebug(rb_parser_t *p)
15835{
15836 return p->debug;
15837}
15838
15839void
15840rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15841{
15842 p->value = value;
15843}
15844
15845int
15846rb_ruby_parser_error_p(rb_parser_t *p)
15847{
15848 return p->error_p;
15849}
15850
15851VALUE
15852rb_ruby_parser_debug_output(rb_parser_t *p)
15853{
15854 return p->debug_output;
15855}
15856
15857void
15858rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15859{
15860 p->debug_output = output;
15861}
15862
15863VALUE
15864rb_ruby_parser_parsing_thread(rb_parser_t *p)
15865{
15866 return p->parsing_thread;
15867}
15868
15869void
15870rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15871{
15872 p->parsing_thread = parsing_thread;
15873}
15874
15875void
15876rb_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)
15877{
15878 p->lex.gets = gets;
15879 p->lex.input = input;
15880 p->eofp = 0;
15881 p->ruby_sourcefile_string = sourcefile_string;
15882 p->ruby_sourcefile = sourcefile;
15883 p->ruby_sourceline = sourceline;
15884}
15885
15886VALUE
15887rb_ruby_parser_result(rb_parser_t *p)
15888{
15889 return p->result;
15890}
15891
15892rb_encoding *
15893rb_ruby_parser_enc(rb_parser_t *p)
15894{
15895 return p->enc;
15896}
15897
15898VALUE
15899rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15900{
15901 return p->ruby_sourcefile_string;
15902}
15903
15904int
15905rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15906{
15907 return p->ruby_sourceline;
15908}
15909
15910int
15911rb_ruby_parser_lex_state(rb_parser_t *p)
15912{
15913 return p->lex.state;
15914}
15915
15916void
15917rb_ruby_ripper_parse0(rb_parser_t *p)
15918{
15919 parser_prepare(p);
15920 p->ast = rb_ast_new();
15921 ripper_yyparse((void*)p);
15922 rb_ast_free(p->ast);
15923 p->ast = 0;
15924 p->eval_tree = 0;
15925 p->eval_tree_begin = 0;
15926}
15927
15928int
15929rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15930{
15931 return dedent_string(p, string, width);
15932}
15933
15934int
15935rb_ruby_ripper_initialized_p(rb_parser_t *p)
15936{
15937 return p->lex.input != 0;
15938}
15939
15940void
15941rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15942{
15943 parser_initialize(p);
15944}
15945
15946long
15947rb_ruby_ripper_column(rb_parser_t *p)
15948{
15949 return p->lex.ptok - p->lex.pbeg;
15950}
15951
15952long
15953rb_ruby_ripper_token_len(rb_parser_t *p)
15954{
15955 return p->lex.pcur - p->lex.ptok;
15956}
15957
15958rb_parser_string_t *
15959rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15960{
15961 return p->lex.lastline;
15962}
15963
15964VALUE
15965rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15966{
15967 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15968}
15969
15970#ifdef UNIVERSAL_PARSER
15971rb_parser_t *
15972rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15973{
15974 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15975 p->config = config;
15976 return p;
15977}
15978#endif
15979
15980struct parser_params*
15981rb_ruby_ripper_parser_allocate(void)
15982{
15983 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15984}
15985#endif /* RIPPER */
15986
15987#ifndef RIPPER
15988void
15989rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15990{
15991 va_list ap;
15992 VALUE mesg = p->debug_buffer;
15993
15994 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15995 va_start(ap, fmt);
15996 rb_str_vcatf(mesg, fmt, ap);
15997 va_end(ap);
15998 if (char_at_end(p, mesg, 0) == '\n') {
15999 rb_io_write(p->debug_output, mesg);
16000 p->debug_buffer = Qnil;
16001 }
16002}
16003
16004static void
16005parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16006{
16007 va_list ap;
16008 int lineno, column;
16009
16010 if (loc) {
16011 lineno = loc->end_pos.lineno;
16012 column = loc->end_pos.column;
16013 }
16014 else {
16015 lineno = p->ruby_sourceline;
16016 column = rb_len2int(p->lex.pcur - p->lex.pbeg);
16017 }
16018
16019 rb_io_flush(p->debug_output);
16020 p->error_p = 1;
16021 va_start(ap, fmt);
16022 p->error_buffer =
16023 rb_syntax_error_append(p->error_buffer,
16024 p->ruby_sourcefile_string,
16025 lineno, column,
16026 p->enc, fmt, ap);
16027 va_end(ap);
16028}
16029
16030static size_t
16031count_char(const char *str, int c)
16032{
16033 int n = 0;
16034 while (str[n] == c) ++n;
16035 return n;
16036}
16037
16038/*
16039 * strip enclosing double-quotes, same as the default yytnamerr except
16040 * for that single-quotes matching back-quotes do not stop stripping.
16041 *
16042 * "\"`class' keyword\"" => "`class' keyword"
16043 */
16044size_t
16045rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16046{
16047 if (*yystr == '"') {
16048 size_t yyn = 0, bquote = 0;
16049 const char *yyp = yystr;
16050
16051 while (*++yyp) {
16052 switch (*yyp) {
16053 case '\'':
16054 if (!bquote) {
16055 bquote = count_char(yyp+1, '\'') + 1;
16056 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16057 yyn += bquote;
16058 yyp += bquote - 1;
16059 break;
16060 }
16061 else {
16062 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16063 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16064 yyn += bquote;
16065 yyp += bquote - 1;
16066 bquote = 0;
16067 break;
16068 }
16069 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16070 if (yyres) memcpy(yyres + yyn, yyp, 3);
16071 yyn += 3;
16072 yyp += 2;
16073 break;
16074 }
16075 goto do_not_strip_quotes;
16076 }
16077
16078 case ',':
16079 goto do_not_strip_quotes;
16080
16081 case '\\':
16082 if (*++yyp != '\\')
16083 goto do_not_strip_quotes;
16084 /* Fall through. */
16085 default:
16086 if (yyres)
16087 yyres[yyn] = *yyp;
16088 yyn++;
16089 break;
16090
16091 case '"':
16092 case '\0':
16093 if (yyres)
16094 yyres[yyn] = '\0';
16095 return yyn;
16096 }
16097 }
16098 do_not_strip_quotes: ;
16099 }
16100
16101 if (!yyres) return strlen(yystr);
16102
16103 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16104}
16105#endif
16106
16107#ifdef RIPPER
16108#define validate(x) (void)(x)
16109
16110static VALUE
16111ripper_dispatch0(struct parser_params *p, ID mid)
16112{
16113 return rb_funcall(p->value, mid, 0);
16114}
16115
16116static VALUE
16117ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16118{
16119 validate(a);
16120 return rb_funcall(p->value, mid, 1, a);
16121}
16122
16123static VALUE
16124ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16125{
16126 validate(a);
16127 validate(b);
16128 return rb_funcall(p->value, mid, 2, a, b);
16129}
16130
16131static VALUE
16132ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16133{
16134 validate(a);
16135 validate(b);
16136 validate(c);
16137 return rb_funcall(p->value, mid, 3, a, b, c);
16138}
16139
16140static VALUE
16141ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16142{
16143 validate(a);
16144 validate(b);
16145 validate(c);
16146 validate(d);
16147 return rb_funcall(p->value, mid, 4, a, b, c, d);
16148}
16149
16150static VALUE
16151ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16152{
16153 validate(a);
16154 validate(b);
16155 validate(c);
16156 validate(d);
16157 validate(e);
16158 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16159}
16160
16161static VALUE
16162ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16163{
16164 validate(a);
16165 validate(b);
16166 validate(c);
16167 validate(d);
16168 validate(e);
16169 validate(f);
16170 validate(g);
16171 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16172}
16173
16174void
16175ripper_error(struct parser_params *p)
16176{
16177 p->error_p = TRUE;
16178}
16179
16180VALUE
16181ripper_value(struct parser_params *p)
16182{
16183 (void)yystpcpy; /* may not used in newer bison */
16184
16185 return p->value;
16186}
16187
16188#endif /* RIPPER */
16189/*
16190 * Local variables:
16191 * mode: c
16192 * c-file-style: "ruby"
16193 * End:
16194 */