Ruby 4.1.0dev (2026-10-13 revision f81c21213293e584529f9b5ce7a5602103c11457)
parse.y (f81c21213293e584529f9b5ce7a5602103c11457)
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, (int)(p->lex.ptok - p->lex.pbeg)}, \
388 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
389 }
390
391#define IS_lex_state_for(x, ls) ((x) & (ls))
392#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
393#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
394#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
395
396# define SET_LEX_STATE(ls) \
397 parser_set_lex_state(p, ls, __LINE__)
398static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
399
400typedef VALUE stack_type;
401
402static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
403
404# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
405# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
406# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
407# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
408# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
409
410/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
411 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
412#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
413#define COND_POP() BITSTACK_POP(cond_stack)
414#define COND_P() BITSTACK_SET_P(cond_stack)
415#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
416
417/* A flag to identify keyword_do_block; "do" keyword after command_call.
418 Example: `foo 1, 2 do`. */
419#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
420#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
421#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
422#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
423
424struct vtable {
425 ID *tbl;
426 int pos;
427 int capa;
428 struct vtable *prev;
429};
430
431struct local_vars {
432 struct vtable *args;
433 struct vtable *vars;
434 struct vtable *used;
435# if WARN_PAST_SCOPE
436 struct vtable *past;
437# endif
438 struct local_vars *prev;
439 struct {
440 NODE *outer, *inner, *current;
441 } numparam;
442 NODE *it;
443};
444
445typedef struct rb_locations_lambda_body_t {
446 NODE *node;
447 YYLTYPE opening_loc;
448 YYLTYPE closing_loc;
449} rb_locations_lambda_body_t;
450
451enum {
452 ORDINAL_PARAM = -1,
453 NO_PARAM = 0,
454 NUMPARAM_MAX = 9,
455};
456
457#define DVARS_INHERIT ((void*)1)
458#define DVARS_TOPSCOPE NULL
459#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
460
461typedef struct token_info {
462 const char *token;
463 rb_code_position_t beg;
464 int indent;
465 int nonspc;
466 struct token_info *next;
467} token_info;
468
469typedef struct end_expect_token_locations {
470 const rb_code_position_t *pos;
471 struct end_expect_token_locations *prev;
472} end_expect_token_locations_t;
473
474typedef struct parser_string_buffer_elem {
475 struct parser_string_buffer_elem *next;
476 long len; /* Total length of allocated buf */
477 long used; /* Current usage of buf */
478 rb_parser_string_t *buf[FLEX_ARY_LEN];
479} parser_string_buffer_elem_t;
480
481typedef struct parser_string_buffer {
482 parser_string_buffer_elem_t *head;
483 parser_string_buffer_elem_t *last;
484} parser_string_buffer_t;
485
486#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
487
488/*
489 Structure of Lexer Buffer:
490
491 lex.pbeg lex.ptok lex.pcur lex.pend
492 | | | |
493 |------------+------------+------------|
494 |<---------->|
495 token
496*/
497struct parser_params {
498 YYSTYPE *lval;
499 YYLTYPE *yylloc;
500
501 struct {
502 rb_strterm_t *strterm;
503 rb_parser_lex_gets_func *gets;
504 rb_parser_input_data input;
505 parser_string_buffer_t string_buffer;
506 rb_parser_string_t *lastline;
507 rb_parser_string_t *nextline;
508 const char *pbeg;
509 const char *pcur;
510 const char *pend;
511 const char *ptok;
512 enum lex_state_e state;
513 /* track the nest level of any parens "()[]{}" */
514 int paren_nest;
515 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
516 int lpar_beg;
517 /* track the nest level of only braces "{}" */
518 int brace_nest;
519 } lex;
520 /* Cache the tail of the and/or chain most recently built by logop(), so a
521 * left-associative chain does not rescan its right branch on every
522 * operator. head is the chain's top node, tail the node whose nd_2nd is the
523 * next insertion point. */
524 struct {
525 NODE *head;
526 NODE *tail;
527 } logop;
528 stack_type cond_stack;
529 stack_type cmdarg_stack;
530 int tokidx;
531 int toksiz;
532 int heredoc_end;
533 int heredoc_indent;
534 int heredoc_line_indent;
535 char *tokenbuf;
536 struct local_vars *lvtbl;
537 st_table *pvtbl;
538 st_table *pktbl;
539 int line_count;
540 int ruby_sourceline; /* current line no. */
541 const char *ruby_sourcefile; /* current source file */
542 VALUE ruby_sourcefile_string;
543 rb_encoding *enc;
544 token_info *token_info;
545 st_table *case_labels;
546 rb_node_exits_t *exits;
547
548 VALUE debug_buffer;
549 VALUE debug_output;
550
551 struct {
552 rb_parser_string_t *token;
553 int beg_line;
554 int beg_col;
555 int end_line;
556 int end_col;
557 } delayed;
558
559 rb_ast_t *ast;
560 int node_id;
561
562 st_table *warn_duplicate_keys_table;
563
564 int max_numparam;
565 ID it_id;
566
567 struct lex_context ctxt;
568
569 NODE *eval_tree_begin;
570 NODE *eval_tree;
571 const struct rb_iseq_struct *parent_iseq;
572
573#ifdef UNIVERSAL_PARSER
574 const rb_parser_config_t *config;
575#endif
576 /* compile_option */
577 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
578
579 unsigned int command_start:1;
580 unsigned int eofp: 1;
581 unsigned int ruby__end__seen: 1;
582 unsigned int debug: 1;
583 unsigned int has_shebang: 1;
584 unsigned int token_seen: 1;
585 unsigned int token_info_enabled: 1;
586# if WARN_PAST_SCOPE
587 unsigned int past_scope_enabled: 1;
588# endif
589 unsigned int error_p: 1;
590 unsigned int cr_seen: 1;
591
592 /* Streaming hash state of the source bytes read so far. */
593 rb_source_hash_state_t source_hash;
594
595#ifndef RIPPER
596 /* Ruby core only */
597
598 unsigned int do_print: 1;
599 unsigned int do_loop: 1;
600 unsigned int do_chomp: 1;
601 unsigned int do_split: 1;
602 unsigned int error_tolerant: 1;
603 unsigned int keep_tokens: 1;
604
605 VALUE error_buffer;
606 rb_parser_ary_t *debug_lines;
607 /*
608 * Store specific keyword locations to generate dummy end token.
609 * Refer to the tail of list element.
610 */
611 end_expect_token_locations_t *end_expect_token_locations;
612 /* id for terms */
613 int token_id;
614 /* Array for term tokens */
615 rb_parser_ary_t *tokens;
616#else
617 /* Ripper only */
618
619 VALUE value;
620 VALUE result;
621 VALUE parsing_thread;
622 VALUE s_value; /* Token VALUE */
623 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
624 VALUE s_value_stack;
625#endif
626};
627
628#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
629#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
630#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
631static int
632numparam_id_p(struct parser_params *p, ID id)
633{
634 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
635 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
636 return idx > 0 && idx <= NUMPARAM_MAX;
637}
638static void numparam_name(struct parser_params *p, ID id);
639
640#ifdef RIPPER
641static void
642after_shift(struct parser_params *p)
643{
644 if (p->debug) {
645 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
646 }
647 rb_ary_push(p->s_value_stack, p->s_value);
648 p->s_value = Qnil;
649}
650
651static void
652before_reduce(int len, struct parser_params *p)
653{
654 // Initialize $$ with $1.
655 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
656}
657
658static void
659after_reduce(int len, struct parser_params *p)
660{
661 for (int i = 0; i < len; i++) {
662 VALUE tos = rb_ary_pop(p->s_value_stack);
663 if (p->debug) {
664 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
665 }
666 }
667 if (p->debug) {
668 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
669 }
670 rb_ary_push(p->s_value_stack, p->s_lvalue);
671 p->s_lvalue = Qnil;
672}
673
674static void
675after_shift_error_token(struct parser_params *p)
676{
677 if (p->debug) {
678 rb_parser_printf(p, "after-shift-error-token:\n");
679 }
680 rb_ary_push(p->s_value_stack, Qnil);
681}
682
683static void
684after_pop_stack(int len, struct parser_params *p)
685{
686 for (int i = 0; i < len; i++) {
687 VALUE tos = rb_ary_pop(p->s_value_stack);
688 if (p->debug) {
689 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
690 }
691 }
692}
693#else
694static void
695after_shift(struct parser_params *p)
696{
697}
698
699static void
700before_reduce(int len, struct parser_params *p)
701{
702}
703
704static void
705after_reduce(int len, struct parser_params *p)
706{
707}
708
709static void
710after_shift_error_token(struct parser_params *p)
711{
712}
713
714static void
715after_pop_stack(int len, struct parser_params *p)
716{
717}
718#endif
719
720#define intern_cstr(n,l,en) rb_intern3(n,l,en)
721
722#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
723
724#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
725#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
726#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
727#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
728#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
729#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
730
731#ifndef RIPPER
732static inline int
733char_at_end(struct parser_params *p, VALUE str, int when_empty)
734{
735 rb_len_t len = RSTRING_LEN(str);
736 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
737}
738#endif
739
740static void
741pop_pvtbl(struct parser_params *p, st_table *tbl)
742{
743 st_free_table(p->pvtbl);
744 p->pvtbl = tbl;
745}
746
747static void
748pop_pktbl(struct parser_params *p, st_table *tbl)
749{
750 if (p->pktbl) st_free_table(p->pktbl);
751 p->pktbl = tbl;
752}
753
754#define STRING_BUF_DEFAULT_LEN 16
755
756static void
757string_buffer_init(struct parser_params *p)
758{
759 parser_string_buffer_t *buf = &p->lex.string_buffer;
760 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
761
762 buf->head = buf->last = xmalloc(size);
763 buf->head->len = STRING_BUF_DEFAULT_LEN;
764 buf->head->used = 0;
765 buf->head->next = NULL;
766}
767
768static void
769string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
770{
771 parser_string_buffer_t *buf = &p->lex.string_buffer;
772
773 if (buf->head->used >= buf->head->len) {
774 parser_string_buffer_elem_t *elem;
775 long n = buf->head->len * 2;
776 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
777
778 elem = xmalloc(size);
779 elem->len = n;
780 elem->used = 0;
781 elem->next = NULL;
782 buf->last->next = elem;
783 buf->last = elem;
784 }
785 buf->last->buf[buf->last->used++] = str;
786}
787
788static void
789string_buffer_free(struct parser_params *p)
790{
791 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
792
793 while (elem) {
794 parser_string_buffer_elem_t *next_elem = elem->next;
795
796 for (long i = 0; i < elem->used; i++) {
797 rb_parser_string_free(p, elem->buf[i]);
798 }
799
800 xfree(elem);
801 elem = next_elem;
802 }
803}
804
805#ifndef RIPPER
806static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
807
808static void
809debug_end_expect_token_locations(struct parser_params *p, const char *name)
810{
811 if(p->debug) {
812 VALUE mesg = rb_sprintf("%s: [", name);
813 int i = 0;
814 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
815 if (i > 0)
816 rb_str_cat_cstr(mesg, ", ");
817 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
818 i++;
819 }
820 rb_str_cat_cstr(mesg, "]\n");
821 flush_debug_buffer(p, p->debug_output, mesg);
822 }
823}
824
825static void
826push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
827{
828 if(!p->error_tolerant) return;
829
830 end_expect_token_locations_t *locations;
831 locations = ALLOC(end_expect_token_locations_t);
832 locations->pos = pos;
833 locations->prev = p->end_expect_token_locations;
834 p->end_expect_token_locations = locations;
835
836 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
837}
838
839static void
840pop_end_expect_token_locations(struct parser_params *p)
841{
842 if(!p->end_expect_token_locations) return;
843
844 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
845 ruby_xfree_sized(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
846 p->end_expect_token_locations = locations;
847
848 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
849}
850
851static end_expect_token_locations_t *
852peek_end_expect_token_locations(struct parser_params *p)
853{
854 return p->end_expect_token_locations;
855}
856
857static const char *
858parser_token2char(struct parser_params *p, enum yytokentype tok)
859{
860 switch ((int) tok) {
861#define TOKEN2CHAR(tok) case tok: return (#tok);
862#define TOKEN2CHAR2(tok, name) case tok: return (name);
863 TOKEN2CHAR2(' ', "word_sep");
864 TOKEN2CHAR2('!', "!")
865 TOKEN2CHAR2('%', "%");
866 TOKEN2CHAR2('&', "&");
867 TOKEN2CHAR2('*', "*");
868 TOKEN2CHAR2('+', "+");
869 TOKEN2CHAR2('-', "-");
870 TOKEN2CHAR2('/', "/");
871 TOKEN2CHAR2('<', "<");
872 TOKEN2CHAR2('=', "=");
873 TOKEN2CHAR2('>', ">");
874 TOKEN2CHAR2('?', "?");
875 TOKEN2CHAR2('^', "^");
876 TOKEN2CHAR2('|', "|");
877 TOKEN2CHAR2('~', "~");
878 TOKEN2CHAR2(':', ":");
879 TOKEN2CHAR2(',', ",");
880 TOKEN2CHAR2('.', ".");
881 TOKEN2CHAR2(';', ";");
882 TOKEN2CHAR2('`', "`");
883 TOKEN2CHAR2('\n', "nl");
884 TOKEN2CHAR2('{', "\"{\"");
885 TOKEN2CHAR2('}', "\"}\"");
886 TOKEN2CHAR2('[', "\"[\"");
887 TOKEN2CHAR2(']', "\"]\"");
888 TOKEN2CHAR2('(', "\"(\"");
889 TOKEN2CHAR2(')', "\")\"");
890 TOKEN2CHAR2('\\', "backslash");
891 TOKEN2CHAR(keyword_class);
892 TOKEN2CHAR(keyword_module);
893 TOKEN2CHAR(keyword_def);
894 TOKEN2CHAR(keyword_undef);
895 TOKEN2CHAR(keyword_begin);
896 TOKEN2CHAR(keyword_rescue);
897 TOKEN2CHAR(keyword_ensure);
898 TOKEN2CHAR(keyword_end);
899 TOKEN2CHAR(keyword_if);
900 TOKEN2CHAR(keyword_unless);
901 TOKEN2CHAR(keyword_then);
902 TOKEN2CHAR(keyword_elsif);
903 TOKEN2CHAR(keyword_else);
904 TOKEN2CHAR(keyword_case);
905 TOKEN2CHAR(keyword_when);
906 TOKEN2CHAR(keyword_while);
907 TOKEN2CHAR(keyword_until);
908 TOKEN2CHAR(keyword_for);
909 TOKEN2CHAR(keyword_break);
910 TOKEN2CHAR(keyword_next);
911 TOKEN2CHAR(keyword_redo);
912 TOKEN2CHAR(keyword_retry);
913 TOKEN2CHAR(keyword_in);
914 TOKEN2CHAR(keyword_do);
915 TOKEN2CHAR(keyword_do_cond);
916 TOKEN2CHAR(keyword_do_block);
917 TOKEN2CHAR(keyword_do_LAMBDA);
918 TOKEN2CHAR(keyword_return);
919 TOKEN2CHAR(keyword_yield);
920 TOKEN2CHAR(keyword_super);
921 TOKEN2CHAR(keyword_self);
922 TOKEN2CHAR(keyword_nil);
923 TOKEN2CHAR(keyword_true);
924 TOKEN2CHAR(keyword_false);
925 TOKEN2CHAR(keyword_and);
926 TOKEN2CHAR(keyword_or);
927 TOKEN2CHAR(keyword_not);
928 TOKEN2CHAR(modifier_if);
929 TOKEN2CHAR(modifier_unless);
930 TOKEN2CHAR(modifier_while);
931 TOKEN2CHAR(modifier_until);
932 TOKEN2CHAR(modifier_rescue);
933 TOKEN2CHAR(keyword_alias);
934 TOKEN2CHAR(keyword_defined);
935 TOKEN2CHAR(keyword_BEGIN);
936 TOKEN2CHAR(keyword_END);
937 TOKEN2CHAR(keyword__LINE__);
938 TOKEN2CHAR(keyword__FILE__);
939 TOKEN2CHAR(keyword__ENCODING__);
940 TOKEN2CHAR(tIDENTIFIER);
941 TOKEN2CHAR(tFID);
942 TOKEN2CHAR(tGVAR);
943 TOKEN2CHAR(tIVAR);
944 TOKEN2CHAR(tCONSTANT);
945 TOKEN2CHAR(tCVAR);
946 TOKEN2CHAR(tLABEL);
947 TOKEN2CHAR(tINTEGER);
948 TOKEN2CHAR(tFLOAT);
949 TOKEN2CHAR(tRATIONAL);
950 TOKEN2CHAR(tIMAGINARY);
951 TOKEN2CHAR(tCHAR);
952 TOKEN2CHAR(tNTH_REF);
953 TOKEN2CHAR(tBACK_REF);
954 TOKEN2CHAR(tSTRING_CONTENT);
955 TOKEN2CHAR(tREGEXP_END);
956 TOKEN2CHAR(tDUMNY_END);
957 TOKEN2CHAR(tSP);
958 TOKEN2CHAR(tUPLUS);
959 TOKEN2CHAR(tUMINUS);
960 TOKEN2CHAR(tPOW);
961 TOKEN2CHAR(tCMP);
962 TOKEN2CHAR(tEQ);
963 TOKEN2CHAR(tEQQ);
964 TOKEN2CHAR(tNEQ);
965 TOKEN2CHAR(tGEQ);
966 TOKEN2CHAR(tLEQ);
967 TOKEN2CHAR(tANDOP);
968 TOKEN2CHAR(tOROP);
969 TOKEN2CHAR(tMATCH);
970 TOKEN2CHAR(tNMATCH);
971 TOKEN2CHAR(tDOT2);
972 TOKEN2CHAR(tDOT3);
973 TOKEN2CHAR(tBDOT2);
974 TOKEN2CHAR(tBDOT3);
975 TOKEN2CHAR(tAREF);
976 TOKEN2CHAR(tASET);
977 TOKEN2CHAR(tLSHFT);
978 TOKEN2CHAR(tRSHFT);
979 TOKEN2CHAR(tANDDOT);
980 TOKEN2CHAR(tCOLON2);
981 TOKEN2CHAR(tCOLON3);
982 TOKEN2CHAR(tOP_ASGN);
983 TOKEN2CHAR(tASSOC);
984 TOKEN2CHAR(tLPAREN);
985 TOKEN2CHAR(tLPAREN_ARG);
986 TOKEN2CHAR(tLBRACK);
987 TOKEN2CHAR(tLBRACE);
988 TOKEN2CHAR(tLBRACE_ARG);
989 TOKEN2CHAR(tSTAR);
990 TOKEN2CHAR(tDSTAR);
991 TOKEN2CHAR(tAMPER);
992 TOKEN2CHAR(tLAMBDA);
993 TOKEN2CHAR(tSYMBEG);
994 TOKEN2CHAR(tSTRING_BEG);
995 TOKEN2CHAR(tXSTRING_BEG);
996 TOKEN2CHAR(tREGEXP_BEG);
997 TOKEN2CHAR(tWORDS_BEG);
998 TOKEN2CHAR(tQWORDS_BEG);
999 TOKEN2CHAR(tSYMBOLS_BEG);
1000 TOKEN2CHAR(tQSYMBOLS_BEG);
1001 TOKEN2CHAR(tSTRING_END);
1002 TOKEN2CHAR(tSTRING_DEND);
1003 TOKEN2CHAR(tSTRING_DBEG);
1004 TOKEN2CHAR(tSTRING_DVAR);
1005 TOKEN2CHAR(tLAMBEG);
1006 TOKEN2CHAR(tLABEL_END);
1007 TOKEN2CHAR(tIGNORED_NL);
1008 TOKEN2CHAR(tCOMMENT);
1009 TOKEN2CHAR(tEMBDOC_BEG);
1010 TOKEN2CHAR(tEMBDOC);
1011 TOKEN2CHAR(tEMBDOC_END);
1012 TOKEN2CHAR(tHEREDOC_BEG);
1013 TOKEN2CHAR(tHEREDOC_END);
1014 TOKEN2CHAR(k__END__);
1015 TOKEN2CHAR(tLOWEST);
1016 TOKEN2CHAR(tUMINUS_NUM);
1017 TOKEN2CHAR(tLAST_TOKEN);
1018#undef TOKEN2CHAR
1019#undef TOKEN2CHAR2
1020 }
1021
1022 rb_bug("parser_token2id: unknown token %d", tok);
1023
1024 UNREACHABLE_RETURN(0);
1025}
1026#else
1027static void
1028push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1029{
1030}
1031
1032static void
1033pop_end_expect_token_locations(struct parser_params *p)
1034{
1035}
1036#endif
1037
1038RBIMPL_ATTR_NONNULL((1, 2, 3))
1039static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1040RBIMPL_ATTR_NONNULL((1, 2))
1041static int parser_yyerror0(struct parser_params*, const char*);
1042#define yyerror0(msg) parser_yyerror0(p, (msg))
1043#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1044#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1045#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1046#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1047#define lex_eol_p(p) lex_eol_n_p(p, 0)
1048#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1049#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1050#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1051
1052static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1053static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1054static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1055static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1056static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1057
1058#ifdef RIPPER
1059#define compile_for_eval (0)
1060#else
1061#define compile_for_eval (p->parent_iseq != 0)
1062#endif
1063
1064#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1065
1066#define CALL_Q_P(q) ((q) == tANDDOT)
1067#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1068
1069#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1070
1071static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1072
1073static inline void
1074rb_discard_node(struct parser_params *p, NODE *n)
1075{
1076 rb_ast_delete_node(p->ast, n);
1077}
1078
1079static 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);
1080static 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);
1081static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1082static 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);
1083static 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);
1084static 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);
1085static 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);
1086static 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);
1087static 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);
1088static 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);
1089static 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);
1090static 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);
1091static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1092static 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);
1093static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1094static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1095static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1096static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1097static 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);
1098static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1099static 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);
1100static 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);
1101static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1102static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1103static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1104static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1105static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1106static 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);
1107static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1108static 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);
1109static 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);
1110static 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);
1111static 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);
1112static 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);
1113static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1114static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1115static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1116static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1117static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1118static 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);
1119static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1120static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1121static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1122static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1123static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1124static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1125static 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);
1126static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1127static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1128static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1129static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1130static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1131static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1132static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1133static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1134static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1135static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1136static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1137static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1138static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1139static 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);
1140static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1141static 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);
1142static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1143static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1144static 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);
1145static 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);
1146static 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);
1147static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1148static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1149static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1150static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1151static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1152static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1153static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1154static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1155static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1156static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1157static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1158static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1159static 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);
1160static 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);
1161static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1162static 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);
1163static 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);
1164static 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);
1165static 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);
1166static 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);
1167static 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);
1168static 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);
1169static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1170static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1171static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1172static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1173static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1174static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1175static 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);
1176static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1177static 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);
1178static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1179static 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);
1180static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1181static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1182static 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);
1183static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1184static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1185static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1186
1187#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1188#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1189#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1190#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)
1191#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)
1192#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1193#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1194#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1195#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)
1196#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)
1197#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)
1198#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)
1199#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1200#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)
1201#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1202#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1203#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1204#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1205#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1206#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1207#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1208#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1209#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1210#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1211#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1212#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1213#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1214#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1215#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1216#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)
1217#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)
1218#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1219#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1220#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1221#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1222#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1223#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1224#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1225#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1226#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)
1227#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1228#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1229#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1230#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1231#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1232#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1233#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)
1234#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1235#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1236#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1237#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1238#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1239#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1240#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1241#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1242#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1243#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1244#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1245#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1246#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1247#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1248#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1249#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1250#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1251#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1252#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1253#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1254#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)
1255#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1256#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1257#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1258#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1259#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1260#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1261#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1262#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1263#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1264#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1265#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1266#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1267#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1268#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1269#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1270#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)
1271#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1272#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)
1273#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1274#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1275#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1276#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1277#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1278#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1279#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1280#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1281#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1282#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1283#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)
1284#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1285#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1286#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1287#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)
1288#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1289#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1290#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1291#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1292#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1293#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1294#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1295
1296enum internal_node_type {
1297 NODE_INTERNAL_ONLY = NODE_LAST,
1298 NODE_DEF_TEMP,
1299 NODE_EXITS,
1300 NODE_INTERNAL_LAST
1301};
1302
1303static const char *
1304parser_node_name(int node)
1305{
1306 switch (node) {
1307 case NODE_DEF_TEMP:
1308 return "NODE_DEF_TEMP";
1309 case NODE_EXITS:
1310 return "NODE_EXITS";
1311 default:
1312 return ruby_node_name(node);
1313 }
1314}
1315
1316/* This node is parse.y internal */
1317struct RNode_DEF_TEMP {
1318 NODE node;
1319
1320 /* for NODE_DEFN/NODE_DEFS */
1321
1322 struct RNode *nd_def;
1323 ID nd_mid;
1324
1325 struct {
1326 int max_numparam;
1327 NODE *numparam_save;
1328 struct lex_context ctxt;
1329 } save;
1330};
1331
1332#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1333
1334static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1335static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1336static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1337static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1338static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1339
1340#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1341#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1342#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1343#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1344
1345/* Make a new internal node, which should not be appeared in the
1346 * result AST and does not have node_id and location. */
1347static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1348#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1349
1350static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1351
1352static int
1353parser_get_node_id(struct parser_params *p)
1354{
1355 int node_id = p->node_id;
1356 p->node_id++;
1357 return node_id;
1358}
1359
1360static void
1361anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1362{
1363 if (id == tANDDOT) {
1364 yyerror1(loc, "&. inside multiple assignment destination");
1365 }
1366}
1367
1368static inline void
1369set_line_body(NODE *body, int line)
1370{
1371 if (!body) return;
1372 switch (nd_type(body)) {
1373 case NODE_RESCUE:
1374 case NODE_ENSURE:
1375 nd_set_line(body, line);
1376 }
1377}
1378
1379static void
1380set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1381{
1382 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1383 nd_set_line(node, beg->end_pos.lineno);
1384}
1385
1386static NODE *
1387last_expr_node(NODE *expr)
1388{
1389 while (expr) {
1390 if (nd_type_p(expr, NODE_BLOCK)) {
1391 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1392 }
1393 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1394 expr = RNODE_BEGIN(expr)->nd_body;
1395 }
1396 else {
1397 break;
1398 }
1399 }
1400 return expr;
1401}
1402
1403#ifndef RIPPER
1404#define yyparse ruby_yyparse
1405#endif
1406
1407static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1408static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1409static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1410static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1411static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1412static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1413
1414static NODE *newline_node(NODE*);
1415static void fixpos(NODE*,NODE*);
1416
1417static int value_expr(struct parser_params*,NODE*);
1418static void void_expr(struct parser_params*,NODE*);
1419static NODE *remove_begin(NODE*);
1420static NODE *void_stmts(struct parser_params*,NODE*);
1421static void reduce_nodes(struct parser_params*,NODE**);
1422static void block_dup_check(struct parser_params*,NODE*,NODE*);
1423
1424static NODE *block_append(struct parser_params*,NODE*,NODE*);
1425static NODE *list_append(struct parser_params*,NODE*,NODE*);
1426static NODE *list_concat(NODE*,NODE*);
1427static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1428static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1429static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1430static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1431static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1432static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1433static NODE *str2dstr(struct parser_params*,NODE*);
1434static NODE *evstr2dstr(struct parser_params*,NODE*);
1435static NODE *splat_array(NODE*);
1436static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1437
1438static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1439static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1440static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1441static 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);
1442static 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;}
1443static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1444
1445static bool args_info_empty_p(struct rb_args_info *args);
1446static 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*);
1447static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1448#define new_empty_args_tail(p, loc) new_args_tail(p, 0, 0, 0, loc)
1449static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1450static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1451static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1452static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1453static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1454static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1455
1456static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1457static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1458
1459static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1460static void no_blockarg(struct parser_params*,NODE*);
1461static NODE *ret_args(struct parser_params*,NODE*);
1462static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1463static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1464
1465static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1466static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1467
1468static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1469static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1470
1471static VALUE rb_backref_error(struct parser_params*,NODE*);
1472static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1473
1474static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1475static 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);
1476static 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);
1477static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1478static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1479
1480static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1481
1482static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1483static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1484
1485static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1486static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1487
1488static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1489
1490static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1491
1492#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1493
1494static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1495
1496static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1497
1498static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1499
1500static rb_ast_id_table_t *local_tbl(struct parser_params*);
1501
1502static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1503static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1504
1505static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1506static NODE *heredoc_dedent(struct parser_params*,NODE*);
1507
1508static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1509
1510static 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);
1511
1512#ifdef RIPPER
1513#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1514#define set_value(val) (p->s_lvalue = val)
1515static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1516static int id_is_var(struct parser_params *p, ID id);
1517#endif
1518
1519RUBY_SYMBOL_EXPORT_BEGIN
1520VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1521int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1522enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1523VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1524void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1525PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1526YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1527YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1528YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1529YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1530YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1531YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1532void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1533RUBY_SYMBOL_EXPORT_END
1534
1535static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1536static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1537static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1538static VALUE formal_argument_error(struct parser_params*, ID);
1539static ID shadowing_lvar(struct parser_params*,ID);
1540static void new_bv(struct parser_params*,ID);
1541
1542static void local_push(struct parser_params*,int);
1543static void local_pop(struct parser_params*);
1544static void local_var(struct parser_params*, ID);
1545static void arg_var(struct parser_params*, ID);
1546static int local_id(struct parser_params *p, ID id);
1547static int local_id_ref(struct parser_params*, ID, ID **);
1548#define internal_id rb_parser_internal_id
1549ID internal_id(struct parser_params*);
1550static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1551static int check_forwarding_args(struct parser_params*);
1552static void add_forwarding_args(struct parser_params *p);
1553static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1554
1555static const struct vtable *dyna_push(struct parser_params *);
1556static void dyna_pop(struct parser_params*, const struct vtable *);
1557static int dyna_in_block(struct parser_params*);
1558#define dyna_var(p, id) local_var(p, id)
1559static int dvar_defined(struct parser_params*, ID);
1560#define dvar_defined_ref rb_parser_dvar_defined_ref
1561int dvar_defined_ref(struct parser_params*, ID, ID**);
1562static int dvar_curr(struct parser_params*,ID);
1563
1564static int lvar_defined(struct parser_params*, ID);
1565
1566static NODE *numparam_push(struct parser_params *p);
1567static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1568
1569#define METHOD_NOT '!'
1570
1571#define idFWD_REST '*'
1572#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1573#define idFWD_BLOCK '&'
1574#define idFWD_ALL idDot3
1575#define arg_FWD_BLOCK idFWD_BLOCK
1576
1577#define RE_ONIG_OPTION_IGNORECASE 1
1578#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1579#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1580#define RE_OPTION_ONCE (1<<16)
1581#define RE_OPTION_ENCODING_SHIFT 8
1582#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1583#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1584#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1585#define RE_OPTION_MASK 0xff
1586#define RE_OPTION_ARG_ENCODING_NONE 32
1587
1588#define CHECK_LITERAL_WHEN (st_table *)1
1589#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1590
1591#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1592RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1593
1594#define TOKEN2ID(tok) ( \
1595 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1596 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1597 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1598 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1599 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1600 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1601 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1602
1603/****** Ripper *******/
1604
1605#ifdef RIPPER
1606
1607#include "eventids1.h"
1608#include "eventids2.h"
1609
1610extern const struct ripper_parser_ids ripper_parser_ids;
1611
1612static VALUE ripper_dispatch0(struct parser_params*,ID);
1613static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1614static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1615static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1616static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1617static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1618static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1619void ripper_error(struct parser_params *p);
1620
1621#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1622#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1623#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1624#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1625#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1626#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1627#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1628
1629#define yyparse ripper_yyparse
1630
1631static VALUE
1632aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1633{
1634 if (!NIL_P(pre_arg)) {
1635 if (!NIL_P(pre_args)) {
1636 rb_ary_unshift(pre_args, pre_arg);
1637 }
1638 else {
1639 pre_args = rb_ary_new_from_args(1, pre_arg);
1640 }
1641 }
1642 return pre_args;
1643}
1644
1645#define ID2VAL(id) STATIC_ID2SYM(id)
1646#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1647#endif /* RIPPER */
1648
1649#define KWD2EID(t, v) keyword_##t
1650
1651static NODE *
1652new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1653{
1654 body = remove_begin(body);
1655 reduce_nodes(p, &body);
1656 NODE *n = NEW_SCOPE(args, body, parent, loc);
1657 nd_set_line(n, loc->end_pos.lineno);
1658 set_line_body(body, loc->beg_pos.lineno);
1659 return n;
1660}
1661
1662static NODE *
1663rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1664 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1665{
1666 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1667 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1668 loc.beg_pos = arg_loc->beg_pos;
1669 return NEW_RESCUE(arg, rescue, 0, &loc);
1670}
1671
1672static NODE *add_block_exit(struct parser_params *p, NODE *node);
1673static rb_node_exits_t *init_block_exit(struct parser_params *p);
1674static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1675static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1676static void clear_block_exit(struct parser_params *p, bool error);
1677
1678static void
1679next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1680{
1681 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1682}
1683
1684static void
1685restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1686{
1687 /* See: def_name action */
1688 struct lex_context ctxt = temp->save.ctxt;
1689 p->ctxt.in_def = ctxt.in_def;
1690 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1691 p->ctxt.in_rescue = ctxt.in_rescue;
1692 p->max_numparam = temp->save.max_numparam;
1693 numparam_pop(p, temp->save.numparam_save);
1694 clear_block_exit(p, true);
1695}
1696
1697static void
1698endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1699{
1700 if (is_attrset_id(mid)) {
1701 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1702 }
1703 token_info_drop(p, "def", loc->beg_pos);
1704}
1705
1706#define debug_token_line(p, name, line) do { \
1707 if (p->debug) { \
1708 const char *const pcur = p->lex.pcur; \
1709 const char *const ptok = p->lex.ptok; \
1710 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1711 line, p->ruby_sourceline, \
1712 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1713 } \
1714 } while (0)
1715
1716#define begin_definition(k, loc_beg, loc_end) \
1717 do { \
1718 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1719 /* singleton class */ \
1720 p->ctxt.cant_return = !p->ctxt.in_def; \
1721 p->ctxt.in_def = 0; \
1722 } \
1723 else if (p->ctxt.in_def) { \
1724 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1725 yyerror1(&loc, k " definition in method body"); \
1726 } \
1727 else { \
1728 p->ctxt.cant_return = 1; \
1729 } \
1730 local_push(p, 0); \
1731 } while (0)
1732
1733#ifndef RIPPER
1734# define ifndef_ripper(x) (x)
1735# define ifdef_ripper(r,x) (x)
1736#else
1737# define ifndef_ripper(x)
1738# define ifdef_ripper(r,x) (r)
1739#endif
1740
1741# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1742# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1743# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1744# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1745# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1746# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1747# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1748# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1749# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1750# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1751# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1752# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1753# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1754# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1755# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1756# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1757# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1758# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1759# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1760# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1761#ifdef RIPPER
1762extern const ID id_warn, id_warning, id_gets, id_assoc;
1763# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1764# define WARN_S_L(s,l) STR_NEW(s,l)
1765# define WARN_S(s) STR_NEW2(s)
1766# define WARN_I(i) INT2NUM(i)
1767# define WARN_ID(i) rb_id2str(i)
1768# define PRIsWARN PRIsVALUE
1769# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1770# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1771# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1772# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1773# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1774# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1775# define compile_error ripper_compile_error
1776#else
1777# define WARN_S_L(s,l) s
1778# define WARN_S(s) s
1779# define WARN_I(i) i
1780# define WARN_ID(i) rb_id2name(i)
1781# define PRIsWARN PRIsVALUE
1782# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1783# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1784# define WARN_CALL rb_compile_warn
1785# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1786# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1787# define WARNING_CALL rb_compile_warning
1788PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1789# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1790#endif
1791
1792#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1793
1794static NODE *
1795add_block_exit(struct parser_params *p, NODE *node)
1796{
1797 if (!node) {
1798 compile_error(p, "unexpected null node");
1799 return 0;
1800 }
1801 switch (nd_type(node)) {
1802 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1803 default:
1804 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1805 return node;
1806 }
1807 if (!p->ctxt.in_defined) {
1808 rb_node_exits_t *exits = p->exits;
1809 if (exits) {
1810 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1811 exits->nd_stts = node;
1812 }
1813 }
1814 return node;
1815}
1816
1817static rb_node_exits_t *
1818init_block_exit(struct parser_params *p)
1819{
1820 rb_node_exits_t *old = p->exits;
1821 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1822 exits->nd_chain = 0;
1823 exits->nd_stts = RNODE(exits);
1824 p->exits = exits;
1825 return old;
1826}
1827
1828static rb_node_exits_t *
1829allow_block_exit(struct parser_params *p)
1830{
1831 rb_node_exits_t *exits = p->exits;
1832 p->exits = 0;
1833 return exits;
1834}
1835
1836static void
1837restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1838{
1839 p->exits = exits;
1840}
1841
1842static void
1843clear_block_exit(struct parser_params *p, bool error)
1844{
1845 rb_node_exits_t *exits = p->exits;
1846 if (!exits) return;
1847 if (error) {
1848 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1849 switch (nd_type(e)) {
1850 case NODE_BREAK:
1851 yyerror1(&e->nd_loc, "Invalid break");
1852 break;
1853 case NODE_NEXT:
1854 yyerror1(&e->nd_loc, "Invalid next");
1855 break;
1856 case NODE_REDO:
1857 yyerror1(&e->nd_loc, "Invalid redo");
1858 break;
1859 default:
1860 yyerror1(&e->nd_loc, "unexpected node");
1861 goto end_checks; /* no nd_chain */
1862 }
1863 }
1864 end_checks:;
1865 }
1866 exits->nd_stts = RNODE(exits);
1867 exits->nd_chain = 0;
1868}
1869
1870#define WARN_EOL(tok) \
1871 (looking_at_eol_p(p) ? \
1872 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1873 (void)0)
1874static int looking_at_eol_p(struct parser_params *p);
1875
1876static NODE *
1877get_nd_value(struct parser_params *p, NODE *node)
1878{
1879 switch (nd_type(node)) {
1880 case NODE_GASGN:
1881 return RNODE_GASGN(node)->nd_value;
1882 case NODE_IASGN:
1883 return RNODE_IASGN(node)->nd_value;
1884 case NODE_LASGN:
1885 return RNODE_LASGN(node)->nd_value;
1886 case NODE_DASGN:
1887 return RNODE_DASGN(node)->nd_value;
1888 case NODE_MASGN:
1889 return RNODE_MASGN(node)->nd_value;
1890 case NODE_CVASGN:
1891 return RNODE_CVASGN(node)->nd_value;
1892 case NODE_CDECL:
1893 return RNODE_CDECL(node)->nd_value;
1894 default:
1895 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1896 return 0;
1897 }
1898}
1899
1900static void
1901set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1902{
1903 switch (nd_type(node)) {
1904 case NODE_CDECL:
1905 RNODE_CDECL(node)->nd_value = rhs;
1906 break;
1907 case NODE_GASGN:
1908 RNODE_GASGN(node)->nd_value = rhs;
1909 break;
1910 case NODE_IASGN:
1911 RNODE_IASGN(node)->nd_value = rhs;
1912 break;
1913 case NODE_LASGN:
1914 RNODE_LASGN(node)->nd_value = rhs;
1915 break;
1916 case NODE_DASGN:
1917 RNODE_DASGN(node)->nd_value = rhs;
1918 break;
1919 case NODE_MASGN:
1920 RNODE_MASGN(node)->nd_value = rhs;
1921 break;
1922 case NODE_CVASGN:
1923 RNODE_CVASGN(node)->nd_value = rhs;
1924 break;
1925 default:
1926 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1927 break;
1928 }
1929}
1930
1931static ID
1932get_nd_vid(struct parser_params *p, NODE *node)
1933{
1934 switch (nd_type(node)) {
1935 case NODE_CDECL:
1936 return RNODE_CDECL(node)->nd_vid;
1937 case NODE_GASGN:
1938 return RNODE_GASGN(node)->nd_vid;
1939 case NODE_IASGN:
1940 return RNODE_IASGN(node)->nd_vid;
1941 case NODE_LASGN:
1942 return RNODE_LASGN(node)->nd_vid;
1943 case NODE_DASGN:
1944 return RNODE_DASGN(node)->nd_vid;
1945 case NODE_CVASGN:
1946 return RNODE_CVASGN(node)->nd_vid;
1947 default:
1948 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1949 return 0;
1950 }
1951}
1952
1953static NODE *
1954get_nd_args(struct parser_params *p, NODE *node)
1955{
1956 switch (nd_type(node)) {
1957 case NODE_CALL:
1958 return RNODE_CALL(node)->nd_args;
1959 case NODE_OPCALL:
1960 return RNODE_OPCALL(node)->nd_args;
1961 case NODE_FCALL:
1962 return RNODE_FCALL(node)->nd_args;
1963 case NODE_QCALL:
1964 return RNODE_QCALL(node)->nd_args;
1965 case NODE_SUPER:
1966 return RNODE_SUPER(node)->nd_args;
1967 case NODE_VCALL:
1968 case NODE_ZSUPER:
1969 case NODE_YIELD:
1970 case NODE_RETURN:
1971 case NODE_BREAK:
1972 case NODE_NEXT:
1973 return 0;
1974 default:
1975 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1976 return 0;
1977 }
1978}
1979
1980static st_index_t
1981djb2(const uint8_t *str, size_t len)
1982{
1983 st_index_t hash = 5381;
1984
1985 for (size_t i = 0; i < len; i++) {
1986 hash = ((hash << 5) + hash) + str[i];
1987 }
1988
1989 return hash;
1990}
1991
1992static st_index_t
1993parser_memhash(const void *ptr, long len)
1994{
1995 return djb2(ptr, len);
1996}
1997
1998#define PARSER_STRING_PTR(str) (str->ptr)
1999#define PARSER_STRING_LEN(str) (str->len)
2000#define PARSER_STRING_END(str) (&str->ptr[str->len])
2001#define STRING_SIZE(str) ((size_t)str->len + 1)
2002#define STRING_TERM_LEN(str) (1)
2003#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
2004#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
2005 REALLOC_N(str->ptr, char, (size_t)total + termlen); \
2006 str->len = total; \
2007} while (0)
2008#define STRING_SET_LEN(str, n) do { \
2009 (str)->len = (n); \
2010} while (0)
2011#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2012 ((ptrvar) = str->ptr, \
2013 (lenvar) = str->len)
2014
2015static inline int
2016parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2017{
2018 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2019}
2020
2021static rb_parser_string_t *
2022rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2023{
2024 rb_parser_string_t *str;
2025
2026 if (len < 0) {
2027 rb_bug("negative string size (or size too big): %ld", len);
2028 }
2029
2030 str = xcalloc(1, sizeof(rb_parser_string_t));
2031 str->ptr = xcalloc(len + 1, sizeof(char));
2032
2033 if (ptr) {
2034 memcpy(PARSER_STRING_PTR(str), ptr, len);
2035 }
2036 STRING_SET_LEN(str, len);
2037 STRING_TERM_FILL(str);
2038 return str;
2039}
2040
2041static rb_parser_string_t *
2042rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2043{
2044 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2045 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2046 str->enc = enc;
2047 return str;
2048}
2049
2050#ifndef RIPPER
2051rb_parser_string_t *
2052rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2053{
2054#if SIZEOF_RB_LEN_T > SIZEOF_LONG
2055 /* rb_parser_string_t holds its length as a long. */
2056 if (RSTRING_LEN(str) > LONG_MAX) {
2057 rb_raise(rb_eArgError, "string size too big");
2058 }
2059#endif
2060 /* Type check */
2061 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), (long)RSTRING_LEN(str), rb_enc_get(str));
2062 RB_GC_GUARD(str);
2063 return ret;
2064}
2065
2066void
2067rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2068{
2069 if (!str) return;
2070 xfree(PARSER_STRING_PTR(str));
2071 xfree(str);
2072}
2073#endif
2074
2075static st_index_t
2076rb_parser_str_hash(rb_parser_string_t *str)
2077{
2078 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2079}
2080
2081static st_index_t
2082rb_char_p_hash(const char *c)
2083{
2084 return parser_memhash((const void *)c, strlen(c));
2085}
2086
2087static size_t
2088rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2089{
2090 return PARSER_STRING_LEN(str);
2091}
2092
2093#ifndef RIPPER
2094static char *
2095rb_parser_string_end(rb_parser_string_t *str)
2096{
2097 return &str->ptr[str->len];
2098}
2099#endif
2100
2101static void
2102rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2103{
2104 str->enc = enc;
2105}
2106
2107static rb_encoding *
2108rb_parser_str_get_encoding(rb_parser_string_t *str)
2109{
2110 return str->enc;
2111}
2112
2113#ifndef RIPPER
2114static bool
2115PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2116{
2117 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2118}
2119#endif
2120
2121static int
2122PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2123{
2124 return str->coderange;
2125}
2126
2127static void
2128PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2129{
2130 str->coderange = coderange;
2131}
2132
2133static void
2134PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2135{
2136 rb_parser_string_set_encoding(str, enc);
2137 PARSER_ENC_CODERANGE_SET(str, cr);
2138}
2139
2140static void
2141PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2142{
2143 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2144}
2145
2146static bool
2147PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2148{
2149 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2150}
2151
2152static bool
2153PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2154{
2155 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2156}
2157
2158static const char *
2159rb_parser_search_nonascii(const char *p, const char *e)
2160{
2161 const char *s = p;
2162
2163 for (; s < e; s++) {
2164 if (*s & 0x80) return s;
2165 }
2166
2167 return NULL;
2168}
2169
2170static int
2171rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2172{
2173 const char *e = ptr + len;
2174
2175 if (enc == rb_ascii8bit_encoding()) {
2176 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2177 ptr = rb_parser_search_nonascii(ptr, e);
2178 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2179 }
2180
2181 /* parser string encoding is always asciicompat */
2182 ptr = rb_parser_search_nonascii(ptr, e);
2183 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2184 for (;;) {
2185 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2186 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2187 ptr += MBCLEN_CHARFOUND_LEN(ret);
2188 if (ptr == e) break;
2189 ptr = rb_parser_search_nonascii(ptr, e);
2190 if (!ptr) break;
2191 }
2192
2193 return RB_PARSER_ENC_CODERANGE_VALID;
2194}
2195
2196static int
2197rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2198{
2199 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2200}
2201
2202static int
2203rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2204{
2205 int cr = PARSER_ENC_CODERANGE(str);
2206
2207 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2208 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2209 PARSER_ENC_CODERANGE_SET(str, cr);
2210 }
2211
2212 return cr;
2213}
2214
2215static rb_parser_string_t *
2216rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2217{
2218 if (rb_parser_str_get_encoding(str) == enc)
2219 return str;
2220 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2221 PARSER_ENC_CODERANGE_CLEAR(str);
2222 }
2223 rb_parser_string_set_encoding(str, enc);
2224 return str;
2225}
2226
2227static bool
2228rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2229{
2230 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2231}
2232
2233static rb_encoding *
2234rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2235{
2236 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2237 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2238
2239 if (enc1 == NULL || enc2 == NULL)
2240 return 0;
2241
2242 if (enc1 == enc2) {
2243 return enc1;
2244 }
2245
2246 if (PARSER_STRING_LEN(str2) == 0)
2247 return enc1;
2248 if (PARSER_STRING_LEN(str1) == 0)
2249 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2250
2251 int cr1, cr2;
2252
2253 cr1 = rb_parser_enc_str_coderange(p, str1);
2254 cr2 = rb_parser_enc_str_coderange(p, str2);
2255
2256 if (cr1 != cr2) {
2257 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2258 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2259 }
2260
2261 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2262 return enc1;
2263 }
2264
2265 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2266 return enc2;
2267 }
2268
2269 return 0;
2270}
2271
2272static void
2273rb_parser_str_modify(rb_parser_string_t *str)
2274{
2275 PARSER_ENC_CODERANGE_CLEAR(str);
2276}
2277
2278static void
2279rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2280{
2281 long capa;
2282 const int termlen = STRING_TERM_LEN(str);
2283
2284 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2285 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2286 }
2287
2288 int cr = PARSER_ENC_CODERANGE(str);
2289 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2290 /* Leave unknown. */
2291 }
2292 else if (len > PARSER_STRING_LEN(str)) {
2293 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2294 }
2295 else if (len < PARSER_STRING_LEN(str)) {
2296 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2297 /* ASCII-only string is keeping after truncated. Valid
2298 * and broken may be invalid or valid, leave unknown. */
2299 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2300 }
2301 }
2302
2303 STRING_SET_LEN(str, len);
2304 STRING_TERM_FILL(str);
2305}
2306
2307static rb_parser_string_t *
2308rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2309{
2310 rb_parser_str_modify(str);
2311 if (len == 0) return 0;
2312
2313 long total, olen, off = -1;
2314 char *sptr;
2315 const int termlen = STRING_TERM_LEN(str);
2316
2317 PARSER_STRING_GETMEM(str, sptr, olen);
2318 if (ptr >= sptr && ptr <= sptr + olen) {
2319 off = ptr - sptr;
2320 }
2321
2322 if (olen > LONG_MAX - len) {
2323 compile_error(p, "string sizes too big");
2324 return 0;
2325 }
2326 total = olen + len;
2327 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2328 sptr = PARSER_STRING_PTR(str);
2329 if (off != -1) {
2330 ptr = sptr + off;
2331 }
2332 memcpy(sptr + olen, ptr, len);
2333 STRING_SET_LEN(str, total);
2334 STRING_TERM_FILL(str);
2335
2336 return str;
2337}
2338
2339#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2340#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2341
2342static rb_parser_string_t *
2343rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2344 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2345{
2346 int str_cr, res_cr;
2347 rb_encoding *str_enc, *res_enc;
2348
2349 str_enc = rb_parser_str_get_encoding(str);
2350 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2351
2352 if (str_enc == ptr_enc) {
2353 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2354 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2355 }
2356 }
2357 else {
2358 /* parser string encoding is always asciicompat */
2359 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2360 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2361 }
2362 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2363 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2364 str_cr = rb_parser_enc_str_coderange(p, str);
2365 }
2366 }
2367 }
2368 if (ptr_cr_ret)
2369 *ptr_cr_ret = ptr_cr;
2370
2371 if (str_enc != ptr_enc &&
2372 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2373 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2374 goto incompatible;
2375 }
2376
2377 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2378 res_enc = str_enc;
2379 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2380 }
2381 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2382 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2383 res_enc = str_enc;
2384 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2385 }
2386 else {
2387 res_enc = ptr_enc;
2388 res_cr = ptr_cr;
2389 }
2390 }
2391 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2392 res_enc = str_enc;
2393 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2394 res_cr = str_cr;
2395 else
2396 res_cr = ptr_cr;
2397 }
2398 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2399 res_enc = str_enc;
2400 res_cr = str_cr;
2401 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2402 }
2403
2404 if (len < 0) {
2405 compile_error(p, "negative string size (or size too big)");
2406 }
2407 parser_str_cat(str, ptr, len);
2408 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2409 return str;
2410
2411 incompatible:
2412 compile_error(p, "incompatible character encodings: %s and %s",
2413 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2414 UNREACHABLE_RETURN(0);
2415
2416}
2417
2418static rb_parser_string_t *
2419rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2420 rb_encoding *ptr_enc)
2421{
2422 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2423}
2424
2425static rb_parser_string_t *
2426rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2427{
2428 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2429
2430 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2431 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2432
2433 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2434
2435 return str;
2436}
2437
2438static rb_parser_string_t *
2439rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2440{
2441 if (len < 0) {
2442 rb_bug("negative string size (or size too big)");
2443 }
2444
2445 long slen = PARSER_STRING_LEN(str);
2446
2447 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2448 PARSER_ENC_CODERANGE_CLEAR(str);
2449 }
2450
2451 {
2452 long capa;
2453 const int termlen = STRING_TERM_LEN(str);
2454
2455 if ((capa = slen) < len) {
2456 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2457 }
2458 else if (len == slen) return str;
2459 STRING_SET_LEN(str, len);
2460 STRING_TERM_FILL(str);
2461 }
2462 return str;
2463}
2464
2465# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2466 ((ptrvar) = str->ptr, \
2467 (lenvar) = str->len, \
2468 (encvar) = str->enc)
2469
2470static int
2471rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2472{
2473 long len1, len2;
2474 const char *ptr1, *ptr2;
2475 rb_encoding *enc1, *enc2;
2476
2477 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2478 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2479
2480 return (len1 != len2 ||
2481 enc1 != enc2 ||
2482 memcmp(ptr1, ptr2, len1) != 0);
2483}
2484
2485static void
2486rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2487{
2488 long i;
2489 if (ary->capa < len) {
2490 ary->capa = len;
2491 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2492 for (i = ary->len; i < len; i++) {
2493 ary->data[i] = 0;
2494 }
2495 }
2496}
2497
2498/*
2499 * Do not call this directly.
2500 * Use rb_parser_ary_new_capa_for_XXX() instead.
2501 */
2502static rb_parser_ary_t *
2503parser_ary_new_capa(rb_parser_t *p, long len)
2504{
2505 if (len < 0) {
2506 rb_bug("negative array size (or size too big): %ld", len);
2507 }
2508 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2509 ary->data_type = 0;
2510 ary->len = 0;
2511 ary->capa = len;
2512 if (0 < len) {
2513 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2514 }
2515 else {
2516 ary->data = NULL;
2517 }
2518 return ary;
2519}
2520
2521#ifndef RIPPER
2522static rb_parser_ary_t *
2523rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2524{
2525 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2526 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2527 return ary;
2528}
2529
2530static rb_parser_ary_t *
2531rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2532{
2533 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2534 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2535 return ary;
2536}
2537#endif
2538
2539static rb_parser_ary_t *
2540rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2541{
2542 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2543 ary->data_type = PARSER_ARY_DATA_NODE;
2544 return ary;
2545}
2546
2547/*
2548 * Do not call this directly.
2549 * Use rb_parser_ary_push_XXX() instead.
2550 */
2551static rb_parser_ary_t *
2552parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2553{
2554 if (ary->len == ary->capa) {
2555 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2556 }
2557 ary->data[ary->len++] = val;
2558 return ary;
2559}
2560
2561#ifndef RIPPER
2562static rb_parser_ary_t *
2563rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2564{
2565 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2566 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2567 }
2568 return parser_ary_push(p, ary, val);
2569}
2570
2571static rb_parser_ary_t *
2572rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2573{
2574 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2575 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2576 }
2577 return parser_ary_push(p, ary, val);
2578}
2579#endif
2580
2581static rb_parser_ary_t *
2582rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2583{
2584 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2585 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2586 }
2587 return parser_ary_push(p, ary, val);
2588}
2589
2590#ifndef RIPPER
2591static void
2592rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2593{
2594 if (!token) return;
2595 rb_parser_string_free(p, token->str);
2596 xfree(token);
2597}
2598
2599static void
2600rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2601{
2602# define foreach_ary(ptr) \
2603 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2604 ptr < end_ary_data; ptr++)
2605 switch (ary->data_type) {
2606 case PARSER_ARY_DATA_AST_TOKEN:
2607 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2608 break;
2609 case PARSER_ARY_DATA_SCRIPT_LINE:
2610 foreach_ary(data) {rb_parser_string_free(p, *data);}
2611 break;
2612 case PARSER_ARY_DATA_NODE:
2613 /* Do nothing because nodes are freed when rb_ast_t is freed */
2614 break;
2615 default:
2616 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2617 break;
2618 }
2619# undef foreach_ary
2620 xfree(ary->data);
2621 xfree(ary);
2622}
2623
2624#endif /* !RIPPER */
2625%}
2626
2627%expect 0
2628%define api.pure
2629%define parse.error verbose
2630%printer {
2631 if ((NODE *)$$ == (NODE *)-1) {
2632 rb_parser_printf(p, "NODE_SPECIAL");
2633 }
2634 else if ($$) {
2635 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2636 }
2637} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2638 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2639%printer {
2640 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2641} <id>
2642%printer {
2643 switch (nd_type(RNODE($$))) {
2644 case NODE_INTEGER:
2645 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2646 break;
2647 case NODE_FLOAT:
2648 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2649 break;
2650 case NODE_RATIONAL:
2651 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2652 break;
2653 case NODE_IMAGINARY:
2654 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2655 break;
2656 default:
2657 break;
2658 }
2659} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2660%printer {
2661 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2662} tNTH_REF
2663%printer {
2664 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2665} tBACK_REF
2666
2667%destructor {
2668 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2669} <labels>
2670
2671%lex-param {struct parser_params *p}
2672%parse-param {struct parser_params *p}
2673%initial-action
2674{
2675 RUBY_SET_YYLLOC_OF_NONE(@$);
2676};
2677%after-shift after_shift
2678%before-reduce before_reduce
2679%after-reduce after_reduce
2680%after-shift-error-token after_shift_error_token
2681%after-pop-stack after_pop_stack
2682
2683%union {
2684 NODE *node;
2685 rb_node_fcall_t *node_fcall;
2686 rb_node_args_t *node_args;
2687 rb_node_args_aux_t *node_args_aux;
2688 rb_node_opt_arg_t *node_opt_arg;
2689 rb_node_kw_arg_t *node_kw_arg;
2690 rb_node_block_pass_t *node_block_pass;
2691 rb_node_masgn_t *node_masgn;
2692 rb_node_def_temp_t *node_def_temp;
2693 rb_node_exits_t *node_exits;
2694 struct rb_locations_lambda_body_t *locations_lambda_body;
2695 ID id;
2696 int num;
2697 st_table *tbl;
2698 st_table *labels;
2699 const struct vtable *vars;
2700 struct rb_strterm_struct *strterm;
2701 struct lex_context ctxt;
2702 enum lex_state_e state;
2703}
2704
2705%token <id>
2706 keyword_class "'class'"
2707 keyword_module "'module'"
2708 keyword_def "'def'"
2709 keyword_undef "'undef'"
2710 keyword_begin "'begin'"
2711 keyword_rescue "'rescue'"
2712 keyword_ensure "'ensure'"
2713 keyword_end "'end'"
2714 keyword_if "'if'"
2715 keyword_unless "'unless'"
2716 keyword_then "'then'"
2717 keyword_elsif "'elsif'"
2718 keyword_else "'else'"
2719 keyword_case "'case'"
2720 keyword_when "'when'"
2721 keyword_while "'while'"
2722 keyword_until "'until'"
2723 keyword_for "'for'"
2724 keyword_break "'break'"
2725 keyword_next "'next'"
2726 keyword_redo "'redo'"
2727 keyword_retry "'retry'"
2728 keyword_in "'in'"
2729 keyword_do "'do'"
2730 keyword_do_cond "'do' for condition"
2731 keyword_do_block "'do' for block"
2732 keyword_do_LAMBDA "'do' for lambda"
2733 keyword_return "'return'"
2734 keyword_yield "'yield'"
2735 keyword_super "'super'"
2736 keyword_self "'self'"
2737 keyword_nil "'nil'"
2738 keyword_true "'true'"
2739 keyword_false "'false'"
2740 keyword_and "'and'"
2741 keyword_or "'or'"
2742 keyword_not "'not'"
2743 modifier_if "'if' modifier"
2744 modifier_unless "'unless' modifier"
2745 modifier_while "'while' modifier"
2746 modifier_until "'until' modifier"
2747 modifier_rescue "'rescue' modifier"
2748 keyword_alias "'alias'"
2749 keyword_defined "'defined?'"
2750 keyword_BEGIN "'BEGIN'"
2751 keyword_END "'END'"
2752 keyword__LINE__ "'__LINE__'"
2753 keyword__FILE__ "'__FILE__'"
2754 keyword__ENCODING__ "'__ENCODING__'"
2755
2756%token <id> tIDENTIFIER "local variable or method"
2757%token <id> tFID "method"
2758%token <id> tGVAR "global variable"
2759%token <id> tIVAR "instance variable"
2760%token <id> tCONSTANT "constant"
2761%token <id> tCVAR "class variable"
2762%token <id> tLABEL "label"
2763%token <node> tINTEGER "integer literal"
2764%token <node> tFLOAT "float literal"
2765%token <node> tRATIONAL "rational literal"
2766%token <node> tIMAGINARY "imaginary literal"
2767%token <node> tCHAR "char literal"
2768%token <node> tNTH_REF "numbered reference"
2769%token <node> tBACK_REF "back reference"
2770%token <node> tSTRING_CONTENT "literal content"
2771%token <num> tREGEXP_END
2772%token <num> tDUMNY_END "dummy end"
2773
2774%type <node> singleton singleton_expr strings string string1 xstring regexp
2775%type <node> string_contents xstring_contents regexp_contents string_content
2776%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2777%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2778%type <node_def_temp> defn_head defs_head k_def
2779%type <node_exits> block_open k_while k_until k_for allow_exits
2780%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2781%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2782%type <node> command command_call command_call_value method_call
2783%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2784%type <node_fcall> fcall
2785%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2786%type <node> args arg_splat call_args opt_call_args
2787%type <node> paren_args opt_paren_args
2788%type <node_args> args_tail block_args_tail
2789%type <node> command_args aref_args
2790%type <node_block_pass> opt_block_arg block_arg
2791%type <node> var_ref var_lhs
2792%type <node> command_rhs arg_rhs
2793%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2794%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args f_empty_arg
2795%type <node_args_aux> f_arg f_arg_item
2796%type <node> f_marg f_rest_marg
2797%type <node_masgn> f_margs
2798%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2799%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2800%type <id> do bv_decls opt_bv_decl bvar
2801%type <node> lambda brace_body do_body
2802%type <locations_lambda_body> lambda_body
2803%type <node_args> f_larglist f_largs largs_tail
2804%type <node> brace_block cmd_brace_block do_block lhs none fitem
2805%type <node> mlhs_head mlhs_item mlhs_node
2806%type <node_masgn> mlhs mlhs_basic mlhs_inner
2807%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2808%type <node> p_expr p_as p_alt p_expr_basic p_find
2809%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2810%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2811%type <node> p_kwargs p_kwarg p_kw
2812%type <id> keyword_variable user_variable sym operation2 operation3
2813%type <id> cname fname op f_rest_arg f_block_arg opt_comma f_norm_arg f_bad_arg
2814%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2815%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2816%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2817%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2818%type <ctxt> p_in_kwarg
2819%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2820%type <num> max_numparam
2821%type <node> numparam
2822%type <id> it_id
2823%token END_OF_INPUT 0 "end-of-input"
2824%token <id> '.'
2825
2826/* escaped chars, should be ignored otherwise */
2827%token <id> '\\' "backslash"
2828%token tSP "escaped space"
2829%token <id> '\t' "escaped horizontal tab"
2830%token <id> '\f' "escaped form feed"
2831%token <id> '\r' "escaped carriage return"
2832%token <id> '\13' "escaped vertical tab"
2833%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2834%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2835%token tPOW RUBY_TOKEN(POW) "**"
2836%token tCMP RUBY_TOKEN(CMP) "<=>"
2837%token tEQ RUBY_TOKEN(EQ) "=="
2838%token tEQQ RUBY_TOKEN(EQQ) "==="
2839%token tNEQ RUBY_TOKEN(NEQ) "!="
2840%token tGEQ RUBY_TOKEN(GEQ) ">="
2841%token tLEQ RUBY_TOKEN(LEQ) "<="
2842%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2843%token tOROP RUBY_TOKEN(OROP) "||"
2844%token tMATCH RUBY_TOKEN(MATCH) "=~"
2845%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2846%token tDOT2 RUBY_TOKEN(DOT2) ".."
2847%token tDOT3 RUBY_TOKEN(DOT3) "..."
2848%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2849%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2850%token tAREF RUBY_TOKEN(AREF) "[]"
2851%token tASET RUBY_TOKEN(ASET) "[]="
2852%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2853%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2854%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2855%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2856%token tCOLON3 ":: at EXPR_BEG"
2857%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2858%token tASSOC "=>"
2859%token tLPAREN "("
2860%token tLPAREN_ARG "( arg"
2861%token tLBRACK "["
2862%token tLBRACE "{"
2863%token tLBRACE_ARG "{ arg"
2864%token tSTAR "*"
2865%token tDSTAR "**arg"
2866%token tAMPER "&"
2867%token <num> tLAMBDA "->"
2868%token tSYMBEG "symbol literal"
2869%token tSTRING_BEG "string literal"
2870%token tXSTRING_BEG "backtick literal"
2871%token tREGEXP_BEG "regexp literal"
2872%token tWORDS_BEG "word list"
2873%token tQWORDS_BEG "verbatim word list"
2874%token tSYMBOLS_BEG "symbol list"
2875%token tQSYMBOLS_BEG "verbatim symbol list"
2876%token tSTRING_END "terminator"
2877%token tSTRING_DEND "'}'"
2878%token <state> tSTRING_DBEG "'#{'"
2879%token tSTRING_DVAR tLAMBEG tLABEL_END
2880
2881%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2882%token tHEREDOC_BEG tHEREDOC_END k__END__
2883
2884/*
2885 * precedence table
2886 */
2887
2888%nonassoc tLOWEST
2889%nonassoc tLBRACE_ARG
2890
2891%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2892%left keyword_or keyword_and
2893%right keyword_not
2894%nonassoc keyword_defined
2895%right '=' tOP_ASGN
2896%left modifier_rescue
2897%right '?' ':'
2898%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2899%left tOROP
2900%left tANDOP
2901%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2902%left '>' tGEQ '<' tLEQ
2903%left '|' '^'
2904%left '&'
2905%left tLSHFT tRSHFT
2906%left '+' '-'
2907%left '*' '/' '%'
2908%right tUMINUS_NUM tUMINUS
2909%right tPOW
2910%right '!' '~' tUPLUS
2911
2912%token tLAST_TOKEN
2913
2914/*
2915 * inlining rules
2916 */
2917%rule %inline ident_or_const
2918 : tIDENTIFIER
2919 | tCONSTANT
2920 ;
2921
2922%rule %inline user_or_keyword_variable
2923 : user_variable
2924 | keyword_variable
2925 ;
2926
2927/*
2928 * parameterizing rules
2929 */
2930%rule asgn(rhs) <node>
2931 : lhs '=' lex_ctxt rhs
2932 {
2933 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2934 /*% ripper: assign!($:1, $:4) %*/
2935 }
2936 ;
2937
2938%rule args_tail_basic(value, trailing) <node_args>
2939 : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing)
2940 {
2941 $$ = new_args_tail(p, $1, $3, $4, &@3);
2942 /*% ripper: [$:1, $:3, $:4] %*/
2943 }
2944 | f_kwarg(value) opt_f_block_arg(trailing)
2945 {
2946 $$ = new_args_tail(p, $1, 0, $2, &@1);
2947 /*% ripper: [$:1, Qnil, $:2] %*/
2948 }
2949 | f_any_kwrest opt_f_block_arg(trailing)
2950 {
2951 $$ = new_args_tail(p, 0, $1, $2, &@1);
2952 /*% ripper: [Qnil, $:1, $:2] %*/
2953 }
2954 | f_block_arg
2955 {
2956 $$ = new_args_tail(p, 0, 0, $1, &@1);
2957 /*% ripper: [Qnil, Qnil, $:1] %*/
2958 }
2959 ;
2960
2961%rule opt_f_block_arg(trailing) <id>
2962 : ',' f_block_arg
2963 {
2964 $$ = $2;
2965 /*% ripper: $:2 %*/
2966 }
2967 | trailing
2968 ;
2969
2970%rule def_endless_method(bodystmt) <node>
2971 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2972 {
2973 endless_method_name(p, $head->nd_mid, &@head);
2974 restore_defun(p, $head);
2975 ($$ = $head->nd_def)->nd_loc = @$;
2976 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2977 RNODE_DEFN($$)->nd_defn = $bodystmt;
2978 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2979 /*% ripper: def!($:head, $:args, $:$) %*/
2980 local_pop(p);
2981 }
2982 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2983 {
2984 endless_method_name(p, $head->nd_mid, &@head);
2985 restore_defun(p, $head);
2986 ($$ = $head->nd_def)->nd_loc = @$;
2987 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2988 RNODE_DEFS($$)->nd_defn = $bodystmt;
2989 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2990 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2991 local_pop(p);
2992 }
2993 ;
2994
2995%rule compstmt(stmts) <node>
2996 : stmts terms?
2997 {
2998 void_stmts(p, $$ = $stmts);
2999 }
3000 ;
3001
3002%rule f_opt(value) <node_opt_arg>
3003 : f_arg_asgn f_eq value
3004 {
3005 p->ctxt.in_argdef = 1;
3006 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
3007 /*% ripper: [$:$, $:3] %*/
3008 }
3009 ;
3010
3011%rule f_opt_arg(value) <node_opt_arg>
3012 : f_opt(value)
3013 {
3014 $$ = $f_opt;
3015 /*% ripper: rb_ary_new3(1, $:1) %*/
3016 }
3017 | f_opt_arg(value) ',' f_opt(value)
3018 {
3019 $$ = opt_arg_append($f_opt_arg, $f_opt);
3020 /*% ripper: rb_ary_push($:1, $:3) %*/
3021 }
3022 ;
3023
3024%rule f_kw(value) <node_kw_arg>
3025 : f_label value
3026 {
3027 p->ctxt.in_argdef = 1;
3028 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3029 /*% ripper: [$:$, $:value] %*/
3030 }
3031 | f_label
3032 {
3033 p->ctxt.in_argdef = 1;
3034 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3035 /*% ripper: [$:$, 0] %*/
3036 }
3037 ;
3038
3039%rule f_kwarg(value) <node_kw_arg>
3040 : f_kw(value)
3041 {
3042 $$ = $f_kw;
3043 /*% ripper: rb_ary_new3(1, $:1) %*/
3044 }
3045 | f_kwarg(value) ',' f_kw(value)
3046 {
3047 $$ = kwd_append($f_kwarg, $f_kw);
3048 /*% ripper: rb_ary_push($:1, $:3) %*/
3049 }
3050 ;
3051
3052%rule mlhs_items(item) <node>
3053 : item
3054 {
3055 $$ = NEW_LIST($1, &@$);
3056 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3057 }
3058 | mlhs_items(item) ',' item
3059 {
3060 $$ = list_append(p, $1, $3);
3061 /*% ripper: mlhs_add!($:1, $:3) %*/
3062 }
3063 ;
3064
3065%rule op_asgn(rhs) <node>
3066 : var_lhs tOP_ASGN lex_ctxt rhs
3067 {
3068 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3069 /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
3070 }
3071 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3072 {
3073 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3074 /*% ripper: opassign!(aref_field!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/
3075 }
3076 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3077 {
3078 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3079 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3080 }
3081 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3082 {
3083 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3084 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3085 }
3086 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3087 {
3088 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3089 /*% ripper: opassign!(field!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3090 }
3091 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3092 {
3093 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3094 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3095 /*% ripper: opassign!(const_path_field!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3096 }
3097 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3098 {
3099 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3100 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3101 /*% ripper: opassign!(top_const_field!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3102 }
3103 | backref tOP_ASGN lex_ctxt rhs
3104 {
3105 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3106 $$ = NEW_ERROR(&@$);
3107 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:backref), $:tOP_ASGN, $:rhs)) %*/
3108 }
3109 ;
3110
3111%rule opt_args_tail(tail, trailing) <node_args>
3112 : ',' tail
3113 {
3114 $$ = $tail;
3115 /*% ripper: $:tail %*/
3116 }
3117 | trailing
3118 {
3119 $$ = new_empty_args_tail(p, &@$);
3120 /*% ripper: [Qnil, Qnil, Qnil] %*/
3121 }
3122 ;
3123
3124%rule range_expr(range) <node>
3125 : range tDOT2 range
3126 {
3127 value_expr(p, $1);
3128 value_expr(p, $3);
3129 $$ = NEW_DOT2($1, $3, &@$, &@2);
3130 /*% ripper: dot2!($:1, $:3) %*/
3131 }
3132 | range tDOT3 range
3133 {
3134 value_expr(p, $1);
3135 value_expr(p, $3);
3136 $$ = NEW_DOT3($1, $3, &@$, &@2);
3137 /*% ripper: dot3!($:1, $:3) %*/
3138 }
3139 | range tDOT2
3140 {
3141 value_expr(p, $1);
3142 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3143 /*% ripper: dot2!($:1, Qnil) %*/
3144 }
3145 | range tDOT3
3146 {
3147 value_expr(p, $1);
3148 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3149 /*% ripper: dot3!($:1, Qnil) %*/
3150 }
3151 | tBDOT2 range
3152 {
3153 value_expr(p, $2);
3154 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3155 /*% ripper: dot2!(Qnil, $:2) %*/
3156 }
3157 | tBDOT3 range
3158 {
3159 value_expr(p, $2);
3160 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3161 /*% ripper: dot3!(Qnil, $:2) %*/
3162 }
3163 ;
3164
3165%rule value_expr(value) <node>
3166 : value
3167 {
3168 value_expr(p, $1);
3169 $$ = $1;
3170 }
3171 ;
3172
3173%rule words(begin, word_list) <node>
3174 : begin ' '+ word_list tSTRING_END
3175 {
3176 $$ = make_list($word_list, &@$);
3177 /*% ripper: array!($:word_list) %*/
3178 }
3179 ;
3180
3181%%
3182program : {
3183 SET_LEX_STATE(EXPR_BEG);
3184 local_push(p, ifndef_ripper(1)+0);
3185 /* jumps are possible in the top-level loop. */
3186 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3187 }
3188 compstmt(top_stmts)
3189 {
3190 if ($2 && !compile_for_eval) {
3191 NODE *node = $2;
3192 /* last expression should not be void */
3193 if (nd_type_p(node, NODE_BLOCK)) {
3194 while (RNODE_BLOCK(node)->nd_next) {
3195 node = RNODE_BLOCK(node)->nd_next;
3196 }
3197 node = RNODE_BLOCK(node)->nd_head;
3198 }
3199 node = remove_begin(node);
3200 void_expr(p, node);
3201 }
3202 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3203 /*% ripper[final]: program!($:2) %*/
3204 local_pop(p);
3205 }
3206 ;
3207
3208top_stmts : none
3209 {
3210 $$ = NEW_BEGIN(0, &@$);
3211 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3212 }
3213 | top_stmt
3214 {
3215 $$ = newline_node($1);
3216 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3217 }
3218 | top_stmts terms top_stmt
3219 {
3220 $$ = block_append(p, $1, newline_node($3));
3221 /*% ripper: stmts_add!($:1, $:3) %*/
3222 }
3223 ;
3224
3225top_stmt : stmt
3226 {
3227 clear_block_exit(p, true);
3228 $$ = $1;
3229 }
3230 | keyword_BEGIN begin_block
3231 {
3232 $$ = $2;
3233 /*% ripper: $:2 %*/
3234 }
3235 ;
3236
3237block_open : '{' {$$ = init_block_exit(p);};
3238
3239begin_block : block_open compstmt(top_stmts) '}'
3240 {
3241 restore_block_exit(p, $block_open);
3242 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3243 NEW_BEGIN($compstmt, &@$));
3244 $$ = NEW_BEGIN(0, &@$);
3245 /*% ripper: BEGIN!($:compstmt) %*/
3246 }
3247 ;
3248
3249bodystmt : compstmt(stmts)[body]
3250 lex_ctxt[ctxt]
3251 opt_rescue
3252 k_else
3253 {
3254 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3255 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3256 }
3257 compstmt(stmts)[elsebody]
3258 {
3259 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3260 }
3261 opt_ensure
3262 {
3263 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3264 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3265 }
3266 | compstmt(stmts)[body]
3267 lex_ctxt[ctxt]
3268 opt_rescue
3269 {
3270 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3271 }
3272 opt_ensure
3273 {
3274 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3275 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3276 }
3277 ;
3278
3279stmts : none
3280 {
3281 $$ = NEW_BEGIN(0, &@$);
3282 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3283 }
3284 | stmt_or_begin
3285 {
3286 $$ = newline_node($1);
3287 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3288 }
3289 | stmts terms stmt_or_begin
3290 {
3291 $$ = block_append(p, $1, newline_node($3));
3292 /*% ripper: stmts_add!($:1, $:3) %*/
3293 }
3294 ;
3295
3296stmt_or_begin : stmt
3297 | keyword_BEGIN
3298 {
3299 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3300 }
3301 begin_block
3302 {
3303 $$ = $3;
3304 }
3305 ;
3306
3307allow_exits : {$$ = allow_block_exit(p);};
3308
3309k_END : keyword_END lex_ctxt
3310 {
3311 if (p->ctxt.in_def) {
3312 rb_warn0("END in method; use at_exit");
3313 }
3314 $$ = $2;
3315 p->ctxt.in_rescue = before_rescue;
3316 /*% ripper: $:2 %*/
3317 };
3318
3319stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3320 {
3321 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3322 /*% ripper: alias!($:new, $:old) %*/
3323 }
3324 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3325 {
3326 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3327 /*% ripper: var_alias!($:new, $:old) %*/
3328 }
3329 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
3330 {
3331 char buf[2];
3332 buf[0] = '$';
3333 buf[1] = (char)RNODE_BACK_REF($old)->nd_nth;
3334 $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw);
3335 /*% ripper: var_alias!($:new, $:old) %*/
3336 }
3337 | keyword_alias tGVAR tNTH_REF[nth]
3338 {
3339 static const char mesg[] = "can't make alias for the number variables";
3340 /*%%%*/
3341 yyerror1(&@nth, mesg);
3342 /*% %*/
3343 $$ = NEW_ERROR(&@$);
3344 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3345 }
3346 | keyword_undef[kw] undef_list[list]
3347 {
3348 nd_set_first_loc($list, @kw.beg_pos);
3349 RNODE_UNDEF($list)->keyword_loc = @kw;
3350 $$ = $list;
3351 /*% ripper: undef!($:list) %*/
3352 }
3353 | stmt[body] modifier_if[mod] expr_value[cond]
3354 {
3355 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3356 fixpos($$, $cond);
3357 /*% ripper: if_mod!($:cond, $:body) %*/
3358 }
3359 | stmt[body] modifier_unless[mod] expr_value[cond]
3360 {
3361 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3362 fixpos($$, $cond);
3363 /*% ripper: unless_mod!($:cond, $:body) %*/
3364 }
3365 | stmt[body] modifier_while[mod] expr_value[cond_expr]
3366 {
3367 clear_block_exit(p, false);
3368 if ($body && nd_type_p($body, NODE_BEGIN)) {
3369 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3370 }
3371 else {
3372 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3373 }
3374 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3375 }
3376 | stmt[body] modifier_until[mod] expr_value[cond_expr]
3377 {
3378 clear_block_exit(p, 0);
3379 if ($body && nd_type_p($body, NODE_BEGIN)) {
3380 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3381 }
3382 else {
3383 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3384 }
3385 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3386 }
3387 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3388 {
3389 p->ctxt.in_rescue = $ctxt.in_rescue;
3390 NODE *resq;
3391 YYLTYPE loc = code_loc_gen(&@mod, &@resbody);
3392 resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3393 $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$);
3394 /*% ripper: rescue_mod!($:body, $:resbody) %*/
3395 }
3396 | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace]
3397 {
3398 clear_block_exit(p, true);
3399 restore_block_exit(p, $block_open);
3400 p->ctxt = $k_end;
3401 {
3402 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$);
3403 $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace);
3404 RNODE_SCOPE(scope)->nd_parent = $$;
3405 }
3406 /*% ripper: END!($:body) %*/
3407 }
3408 | command_asgn
3409 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3410 {
3411 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3412 /*% ripper: massign!($:lhs, $:rhs) %*/
3413 }
3414 | asgn(mrhs)
3415 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3416 after_rescue[after_rescue] stmt[resbody]
3417 {
3418 p->ctxt.in_rescue = $after_rescue.in_rescue;
3419 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3420 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3421 loc.beg_pos = @mrhs_arg.beg_pos;
3422 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3423 $$ = node_assign(p, (NODE *)$lhs, $mrhs_arg, $lex_ctxt, &@$);
3424 /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/
3425 }
3426 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3427 {
3428 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3429 /*% ripper: massign!($:lhs, $:rhs) %*/
3430 }
3431 | expr
3432 | error
3433 {
3434 (void)yynerrs;
3435 $$ = NEW_ERROR(&@$);
3436 }
3437 ;
3438
3439command_asgn : asgn(command_rhs)
3440 | op_asgn(command_rhs)
3441 | def_endless_method(endless_command)
3442 ;
3443
3444endless_command : command
3445 | endless_command modifier_rescue after_rescue arg
3446 {
3447 p->ctxt.in_rescue = $3.in_rescue;
3448 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3449 /*% ripper: rescue_mod!($:1, $:4) %*/
3450 }
3451 | keyword_not '\n'? endless_command
3452 {
3453 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3454 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3455 }
3456 ;
3457
3458command_rhs : command_call_value %prec tOP_ASGN
3459 | command_call_value modifier_rescue after_rescue stmt
3460 {
3461 p->ctxt.in_rescue = $3.in_rescue;
3462 YYLTYPE loc = code_loc_gen(&@2, &@4);
3463 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3464 /*% ripper: rescue_mod!($:1, $:4) %*/
3465 }
3466 | command_asgn
3467 ;
3468
3469expr : command_call
3470 | expr[left] keyword_and[op] expr[right]
3471 {
3472 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3473 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3474 }
3475 | expr[left] keyword_or[op] expr[right]
3476 {
3477 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3478 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3479 }
3480 | keyword_not[not] '\n'? expr[arg]
3481 {
3482 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3483 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3484 }
3485 | '!'[not] command_call[arg]
3486 {
3487 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3488 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3489 }
3490 | arg tASSOC[assoc]
3491 {
3492 value_expr(p, $arg);
3493 }
3494 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3495 p_top_expr_body[body]
3496 {
3497 pop_pktbl(p, $p_pktbl);
3498 pop_pvtbl(p, $p_pvtbl);
3499 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3500 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3501 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3502 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@assoc), &@$, &NULL_LOC, &NULL_LOC);
3503 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3504 }
3505 | arg keyword_in
3506 {
3507 value_expr(p, $arg);
3508 }
3509 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3510 p_top_expr_body[body]
3511 {
3512 pop_pktbl(p, $p_pktbl);
3513 pop_pvtbl(p, $p_pvtbl);
3514 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3515 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3516 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3517 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3518 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3519 }
3520 | arg %prec tLBRACE_ARG
3521 ;
3522
3523def_name : fname
3524 {
3525 numparam_name(p, $fname);
3526 local_push(p, 0);
3527 p->ctxt.in_def = 1;
3528 p->ctxt.in_rescue = before_rescue;
3529 p->ctxt.cant_return = 0;
3530 $$ = $fname;
3531 }
3532 ;
3533
3534defn_head : k_def def_name
3535 {
3536 $$ = def_head_save(p, $k_def);
3537 $$->nd_mid = $def_name;
3538 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3539 /*% ripper: $:def_name %*/
3540 }
3541 ;
3542
3543defs_head : k_def singleton dot_or_colon
3544 {
3545 SET_LEX_STATE(EXPR_FNAME);
3546 }
3547 def_name
3548 {
3549 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3550 $$ = def_head_save(p, $k_def);
3551 $$->nd_mid = $def_name;
3552 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3553 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3554 }
3555 ;
3556
3557expr_value : value_expr(expr)
3558 | error
3559 {
3560 $$ = NEW_ERROR(&@$);
3561 }
3562 ;
3563
3564expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3565 {
3566 $$ = $2;
3567 /*% ripper: $:2 %*/
3568 }
3569 ;
3570
3571command_call : command
3572 | block_command
3573 ;
3574
3575command_call_value : value_expr(command_call)
3576 ;
3577
3578block_command : block_call
3579 | block_call call_op2 operation2 command_args
3580 {
3581 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3582 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3583 }
3584 ;
3585
3586cmd_brace_block : tLBRACE_ARG brace_body '}'
3587 {
3588 $$ = $2;
3589 set_embraced_location($$, &@1, &@3);
3590 /*% ripper: $:2 %*/
3591 }
3592 ;
3593
3594fcall : operation
3595 {
3596 $$ = NEW_FCALL($1, 0, &@$);
3597 /*% ripper: $:1 %*/
3598 }
3599 ;
3600
3601command : fcall command_args %prec tLOWEST
3602 {
3603 $1->nd_args = $2;
3604 nd_set_last_loc($1, @2.end_pos);
3605 $$ = (NODE *)$1;
3606 /*% ripper: command!($:1, $:2) %*/
3607 }
3608 | fcall command_args cmd_brace_block
3609 {
3610 block_dup_check(p, $2, $3);
3611 $1->nd_args = $2;
3612 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3613 fixpos($$, RNODE($1));
3614 nd_set_last_loc($1, @2.end_pos);
3615 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3616 }
3617 | primary_value call_op operation2 command_args %prec tLOWEST
3618 {
3619 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3620 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3621 }
3622 | primary_value call_op operation2 command_args cmd_brace_block
3623 {
3624 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3625 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3626 }
3627 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3628 {
3629 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3630 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3631 }
3632 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3633 {
3634 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3635 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3636 }
3637 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3638 {
3639 set_embraced_location($5, &@4, &@6);
3640 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3641 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3642 }
3643 | keyword_super command_args
3644 {
3645 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3646 fixpos($$, $2);
3647 /*% ripper: super!($:2) %*/
3648 }
3649 | k_yield command_args
3650 {
3651 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3652 fixpos($$, $2);
3653 /*% ripper: yield!($:2) %*/
3654 }
3655 | k_return call_args
3656 {
3657 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3658 /*% ripper: return!($:2) %*/
3659 }
3660 | keyword_break call_args
3661 {
3662 NODE *args = 0;
3663 args = ret_args(p, $2);
3664 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3665 /*% ripper: break!($:2) %*/
3666 }
3667 | keyword_next call_args
3668 {
3669 NODE *args = 0;
3670 args = ret_args(p, $2);
3671 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3672 /*% ripper: next!($:2) %*/
3673 }
3674 ;
3675
3676mlhs : mlhs_basic
3677 | tLPAREN mlhs_inner rparen
3678 {
3679 $$ = $2;
3680 /*% ripper: mlhs_paren!($:2) %*/
3681 }
3682 ;
3683
3684mlhs_inner : mlhs_basic
3685 | tLPAREN mlhs_inner rparen
3686 {
3687 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3688 /*% ripper: mlhs_paren!($:2) %*/
3689 }
3690 ;
3691
3692mlhs_basic : mlhs_head
3693 {
3694 $$ = NEW_MASGN($1, 0, &@$);
3695 /*% ripper: $:1 %*/
3696 }
3697 | mlhs_head mlhs_item
3698 {
3699 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3700 /*% ripper: mlhs_add!($:1, $:2) %*/
3701 }
3702 | mlhs_head tSTAR mlhs_node
3703 {
3704 $$ = NEW_MASGN($1, $3, &@$);
3705 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3706 }
3707 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3708 {
3709 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3710 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3711 }
3712 | mlhs_head tSTAR
3713 {
3714 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3715 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3716 }
3717 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3718 {
3719 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3720 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3721 }
3722 | tSTAR mlhs_node
3723 {
3724 $$ = NEW_MASGN(0, $2, &@$);
3725 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3726 }
3727 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3728 {
3729 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3730 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3731 }
3732 | tSTAR
3733 {
3734 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3735 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3736 }
3737 | tSTAR ',' mlhs_items(mlhs_item)
3738 {
3739 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3740 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3741 }
3742 ;
3743
3744mlhs_item : mlhs_node
3745 | tLPAREN mlhs_inner rparen
3746 {
3747 $$ = (NODE *)$2;
3748 /*% ripper: mlhs_paren!($:2) %*/
3749 }
3750 ;
3751
3752mlhs_head : mlhs_item ','
3753 {
3754 $$ = NEW_LIST($1, &@1);
3755 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3756 }
3757 | mlhs_head mlhs_item ','
3758 {
3759 $$ = list_append(p, $1, $2);
3760 /*% ripper: mlhs_add!($:1, $:2) %*/
3761 }
3762 ;
3763
3764
3765mlhs_node : user_or_keyword_variable
3766 {
3767 /*% ripper: var_field!($:1) %*/
3768 $$ = assignable(p, $1, 0, &@$);
3769 }
3770 | primary_value '[' opt_call_args rbracket
3771 {
3772 $$ = aryset(p, $1, $3, &@$);
3773 /*% ripper: aref_field!($:1, $:3) %*/
3774 }
3775 | primary_value call_op ident_or_const
3776 {
3777 anddot_multiple_assignment_check(p, &@2, $2);
3778 $$ = attrset(p, $1, $2, $3, &@$);
3779 /*% ripper: field!($:1, $:2, $:3) %*/
3780 }
3781 | primary_value tCOLON2 tIDENTIFIER
3782 {
3783 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3784 /*% ripper: const_path_field!($:1, $:3) %*/
3785 }
3786 | primary_value tCOLON2 tCONSTANT
3787 {
3788 /*% ripper: const_path_field!($:1, $:3) %*/
3789 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3790 }
3791 | tCOLON3 tCONSTANT
3792 {
3793 /*% ripper: top_const_field!($:2) %*/
3794 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3795 }
3796 | backref
3797 {
3798 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3799 $$ = NEW_ERROR(&@$);
3800 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3801 }
3802 ;
3803
3804lhs : user_or_keyword_variable
3805 {
3806 /*% ripper: var_field!($:1) %*/
3807 $$ = assignable(p, $1, 0, &@$);
3808 }
3809 | primary_value '[' opt_call_args rbracket
3810 {
3811 $$ = aryset(p, $1, $3, &@$);
3812 /*% ripper: aref_field!($:1, $:3) %*/
3813 }
3814 | primary_value call_op ident_or_const
3815 {
3816 $$ = attrset(p, $1, $2, $3, &@$);
3817 /*% ripper: field!($:1, $:2, $:3) %*/
3818 }
3819 | primary_value tCOLON2 tIDENTIFIER
3820 {
3821 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3822 /*% ripper: field!($:1, $:2, $:3) %*/
3823 }
3824 | primary_value tCOLON2 tCONSTANT
3825 {
3826 /*% ripper: const_path_field!($:1, $:3) %*/
3827 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3828 }
3829 | tCOLON3 tCONSTANT
3830 {
3831 /*% ripper: top_const_field!($:2) %*/
3832 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3833 }
3834 | backref
3835 {
3836 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3837 $$ = NEW_ERROR(&@$);
3838 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3839 }
3840 ;
3841
3842cname : tIDENTIFIER
3843 {
3844 static const char mesg[] = "class/module name must be CONSTANT";
3845 /*%%%*/
3846 yyerror1(&@1, mesg);
3847 /*% %*/
3848 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3849 }
3850 | tCONSTANT
3851 ;
3852
3853cpath : tCOLON3 cname
3854 {
3855 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3856 /*% ripper: top_const_ref!($:2) %*/
3857 }
3858 | cname
3859 {
3860 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3861 /*% ripper: const_ref!($:1) %*/
3862 }
3863 | primary_value tCOLON2 cname
3864 {
3865 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3866 /*% ripper: const_path_ref!($:1, $:3) %*/
3867 }
3868 ;
3869
3870fname : operation
3871 | op
3872 {
3873 SET_LEX_STATE(EXPR_ENDFN);
3874 $$ = $1;
3875 }
3876 | reswords
3877 ;
3878
3879fitem : fname
3880 {
3881 $$ = NEW_SYM(rb_id2str($1), &@$);
3882 /*% ripper: symbol_literal!($:1) %*/
3883 }
3884 | symbol
3885 ;
3886
3887undef_list : fitem
3888 {
3889 $$ = NEW_UNDEF($1, &@$);
3890 /*% ripper: rb_ary_new3(1, $:1) %*/
3891 }
3892 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3893 {
3894 nd_set_last_loc($1, @4.end_pos);
3895 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3896 /*% ripper: rb_ary_push($:1, $:4) %*/
3897 }
3898 ;
3899
3900op : '|' { $$ = '|'; }
3901 | '^' { $$ = '^'; }
3902 | '&' { $$ = '&'; }
3903 | tCMP { $$ = tCMP; }
3904 | tEQ { $$ = tEQ; }
3905 | tEQQ { $$ = tEQQ; }
3906 | tMATCH { $$ = tMATCH; }
3907 | tNMATCH { $$ = tNMATCH; }
3908 | '>' { $$ = '>'; }
3909 | tGEQ { $$ = tGEQ; }
3910 | '<' { $$ = '<'; }
3911 | tLEQ { $$ = tLEQ; }
3912 | tNEQ { $$ = tNEQ; }
3913 | tLSHFT { $$ = tLSHFT; }
3914 | tRSHFT { $$ = tRSHFT; }
3915 | '+' { $$ = '+'; }
3916 | '-' { $$ = '-'; }
3917 | '*' { $$ = '*'; }
3918 | tSTAR { $$ = '*'; }
3919 | '/' { $$ = '/'; }
3920 | '%' { $$ = '%'; }
3921 | tPOW { $$ = tPOW; }
3922 | tDSTAR { $$ = tDSTAR; }
3923 | '!' { $$ = '!'; }
3924 | '~' { $$ = '~'; }
3925 | tUPLUS { $$ = tUPLUS; }
3926 | tUMINUS { $$ = tUMINUS; }
3927 | tAREF { $$ = tAREF; }
3928 | tASET { $$ = tASET; }
3929 | '`' { $$ = '`'; }
3930 ;
3931
3932reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3933 | keyword_BEGIN | keyword_END
3934 | keyword_alias | keyword_and | keyword_begin
3935 | keyword_break | keyword_case | keyword_class | keyword_def
3936 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3937 | keyword_end | keyword_ensure | keyword_false
3938 | keyword_for | keyword_in | keyword_module | keyword_next
3939 | keyword_nil | keyword_not | keyword_or | keyword_redo
3940 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3941 | keyword_super | keyword_then | keyword_true | keyword_undef
3942 | keyword_when | keyword_yield | keyword_if | keyword_unless
3943 | keyword_while | keyword_until
3944 ;
3945
3946arg : asgn(arg_rhs)
3947 | op_asgn(arg_rhs)
3948 | range_expr(arg)
3949 | arg '+' arg
3950 {
3951 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3952 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3953 }
3954 | arg '-' arg
3955 {
3956 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3957 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3958 }
3959 | arg '*' arg
3960 {
3961 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3962 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3963 }
3964 | arg '/' arg
3965 {
3966 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3967 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3968 }
3969 | arg '%' arg
3970 {
3971 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3972 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3973 }
3974 | arg tPOW arg
3975 {
3976 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3977 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3978 }
3979 | tUMINUS_NUM simple_numeric tPOW arg
3980 {
3981 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3982 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3983 }
3984 | tUPLUS arg
3985 {
3986 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3987 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3988 }
3989 | tUMINUS arg
3990 {
3991 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3992 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3993 }
3994 | arg '|' arg
3995 {
3996 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3997 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3998 }
3999 | arg '^' arg
4000 {
4001 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
4002 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
4003 }
4004 | arg '&' arg
4005 {
4006 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
4007 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
4008 }
4009 | arg tCMP arg
4010 {
4011 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
4012 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
4013 }
4014 | rel_expr %prec tCMP
4015 | arg tEQ arg
4016 {
4017 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4018 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4019 }
4020 | arg tEQQ arg
4021 {
4022 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4023 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4024 }
4025 | arg tNEQ arg
4026 {
4027 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4028 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4029 }
4030 | arg tMATCH arg
4031 {
4032 $$ = match_op(p, $1, $3, &@2, &@$);
4033 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4034 }
4035 | arg tNMATCH arg
4036 {
4037 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4038 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4039 }
4040 | '!' arg
4041 {
4042 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4043 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4044 }
4045 | '~' arg
4046 {
4047 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4048 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4049 }
4050 | arg tLSHFT arg
4051 {
4052 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4053 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4054 }
4055 | arg tRSHFT arg
4056 {
4057 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4058 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4059 }
4060 | arg tANDOP arg
4061 {
4062 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4063 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4064 }
4065 | arg tOROP arg
4066 {
4067 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4068 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4069 }
4070 | keyword_defined '\n'? begin_defined arg
4071 {
4072 p->ctxt.in_defined = $3.in_defined;
4073 $$ = new_defined(p, $4, &@$, &@1);
4074 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4075 /*% ripper: defined!($:4) %*/
4076 }
4077 | def_endless_method(endless_arg)
4078 | ternary
4079 | primary
4080 ;
4081
4082ternary : arg '?' arg '\n'? ':' arg
4083 {
4084 value_expr(p, $1);
4085 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4086 fixpos($$, $1);
4087 /*% ripper: ifop!($:1, $:3, $:6) %*/
4088 }
4089 ;
4090
4091endless_arg : arg %prec modifier_rescue
4092 | endless_arg modifier_rescue after_rescue arg
4093 {
4094 p->ctxt.in_rescue = $3.in_rescue;
4095 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4096 /*% ripper: rescue_mod!($:1, $:4) %*/
4097 }
4098 | keyword_not '\n'? endless_arg
4099 {
4100 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4101 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4102 }
4103 ;
4104
4105relop : '>' {$$ = '>';}
4106 | '<' {$$ = '<';}
4107 | tGEQ {$$ = idGE;}
4108 | tLEQ {$$ = idLE;}
4109 ;
4110
4111rel_expr : arg relop arg %prec '>'
4112 {
4113 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4114 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4115 }
4116 | rel_expr relop arg %prec '>'
4117 {
4118 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4119 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4120 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4121 }
4122 ;
4123
4124lex_ctxt : none
4125 {
4126 $$ = p->ctxt;
4127 }
4128 ;
4129
4130begin_defined : lex_ctxt
4131 {
4132 p->ctxt.in_defined = 1;
4133 $$ = $1;
4134 }
4135 ;
4136
4137after_rescue : lex_ctxt
4138 {
4139 p->ctxt.in_rescue = after_rescue;
4140 $$ = $1;
4141 }
4142 ;
4143
4144arg_value : value_expr(arg)
4145 ;
4146
4147aref_args : none
4148 | args trailer
4149 | args ',' assocs trailer
4150 {
4151 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4152 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4153 }
4154 | assocs trailer
4155 {
4156 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4157 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4158 }
4159 ;
4160
4161arg_rhs : arg %prec tOP_ASGN
4162 {
4163 value_expr(p, $1);
4164 $$ = $1;
4165 }
4166 | arg modifier_rescue after_rescue arg
4167 {
4168 p->ctxt.in_rescue = $3.in_rescue;
4169 value_expr(p, $1);
4170 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4171 /*% ripper: rescue_mod!($:1, $:4) %*/
4172 }
4173 ;
4174
4175paren_args : '(' opt_call_args rparen
4176 {
4177 $$ = $2;
4178 /*% ripper: arg_paren!($:2) %*/
4179 }
4180 | '(' args ',' args_forward rparen
4181 {
4182 if (!check_forwarding_args(p)) {
4183 $$ = 0;
4184 }
4185 else {
4186 $$ = new_args_forward_call(p, $2, &@4, &@$);
4187 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4188 }
4189 }
4190 | '(' args_forward rparen
4191 {
4192 if (!check_forwarding_args(p)) {
4193 $$ = 0;
4194 }
4195 else {
4196 $$ = new_args_forward_call(p, 0, &@2, &@$);
4197 /*% ripper: arg_paren!($:2) %*/
4198 }
4199 }
4200 ;
4201
4202opt_paren_args : none
4203 | paren_args
4204 {
4205 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4206 }
4207 ;
4208
4209opt_call_args : none
4210 | call_args
4211 | args ','
4212 | args ',' assocs ','
4213 {
4214 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4215 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4216 }
4217 | assocs ','
4218 {
4219 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4220 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4221 }
4222 ;
4223
4224call_args : value_expr(command)
4225 {
4226 $$ = NEW_LIST($1, &@$);
4227 /*% ripper: args_add!(args_new!, $:1) %*/
4228 }
4229 | def_endless_method(endless_command)
4230 {
4231 $$ = NEW_LIST($1, &@$);
4232 /*% ripper: args_add!(args_new!, $:1) %*/
4233 }
4234 | args opt_block_arg
4235 {
4236 $$ = arg_blk_pass($1, $2);
4237 /*% ripper: args_add_block!($:1, $:2) %*/
4238 }
4239 | assocs opt_block_arg
4240 {
4241 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4242 $$ = arg_blk_pass($$, $2);
4243 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4244 }
4245 | args ',' assocs opt_block_arg
4246 {
4247 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4248 $$ = arg_blk_pass($$, $4);
4249 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4250 }
4251 | block_arg
4252 /*% ripper: args_add_block!(args_new!, $:1) %*/
4253 ;
4254
4255command_args : {
4256 /* If call_args starts with a open paren '(' or '[',
4257 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4258 * but the push must be done after CMDARG_PUSH(1).
4259 * So this code makes them consistent by first cancelling
4260 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4261 * and finally redoing CMDARG_PUSH(0).
4262 */
4263 int lookahead = 0;
4264 switch (yychar) {
4265 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4266 lookahead = 1;
4267 }
4268 if (lookahead) CMDARG_POP();
4269 CMDARG_PUSH(1);
4270 if (lookahead) CMDARG_PUSH(0);
4271 }
4272 call_args
4273 {
4274 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4275 * but the push must be done after CMDARG_POP() in the parser.
4276 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4277 * CMDARG_POP() to pop 1 pushed by command_args,
4278 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4279 */
4280 int lookahead = 0;
4281 switch (yychar) {
4282 case tLBRACE_ARG:
4283 lookahead = 1;
4284 }
4285 if (lookahead) CMDARG_POP();
4286 CMDARG_POP();
4287 if (lookahead) CMDARG_PUSH(0);
4288 $$ = $2;
4289 /*% ripper: $:2 %*/
4290 }
4291 ;
4292
4293block_arg : tAMPER arg_value
4294 {
4295 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4296 /*% ripper: $:2 %*/
4297 }
4298 | tAMPER
4299 {
4300 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4301 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4302 /*% ripper: Qnil %*/
4303 }
4304 ;
4305
4306opt_block_arg : ',' block_arg
4307 {
4308 $$ = $2;
4309 /*% ripper: $:2 %*/
4310 }
4311 | none
4312 {
4313 $$ = 0;
4314 /*% ripper: Qfalse %*/
4315 }
4316 ;
4317
4318/* value */
4319args : arg_value
4320 {
4321 $$ = NEW_LIST($arg_value, &@$);
4322 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4323 }
4324 | arg_splat
4325 {
4326 $$ = $arg_splat;
4327 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4328 }
4329 | args[non_last_args] ',' arg_value
4330 {
4331 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4332 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4333 }
4334 | args[non_last_args] ',' arg_splat
4335 {
4336 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4337 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4338 }
4339 ;
4340
4341/* value */
4342arg_splat : tSTAR arg_value
4343 {
4344 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4345 /*% ripper: $:arg_value %*/
4346 }
4347 | tSTAR /* none */
4348 {
4349 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4350 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4351 /*% ripper: Qnil %*/
4352 }
4353 ;
4354
4355/* value */
4356mrhs_arg : mrhs
4357 | arg_value
4358 ;
4359
4360/* value */
4361mrhs : args ',' arg_value
4362 {
4363 $$ = last_arg_append(p, $args, $arg_value, &@$);
4364 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4365 }
4366 | args ',' tSTAR arg_value
4367 {
4368 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4369 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4370 }
4371 | tSTAR arg_value
4372 {
4373 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4374 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4375 }
4376 ;
4377
4378%rule %inline inline_primary
4379 : literal
4380 | strings
4381 | xstring
4382 | regexp
4383 | words
4384 | qwords
4385 | symbols
4386 | qsymbols
4387 ;
4388
4389primary : inline_primary
4390 | var_ref
4391 | backref
4392 | tFID[fid]
4393 {
4394 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4395 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
4396 }
4397 | k_begin[kw]
4398 {
4399 CMDARG_PUSH(0);
4400 }
4401 bodystmt[body]
4402 k_end[k_end]
4403 {
4404 CMDARG_POP();
4405 set_line_body($body, @kw.end_pos.lineno);
4406 $$ = NEW_BEGIN($body, &@$);
4407 nd_set_line($$, @kw.end_pos.lineno);
4408 /*% ripper: begin!($:body) %*/
4409 }
4410 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4411 {
4412 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4413 $$ = $body;
4414 /*% ripper: paren!($:body) %*/
4415 }
4416 | tLPAREN compstmt(stmts)[body] ')'
4417 {
4418 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4419 $$ = NEW_BLOCK($body, &@$);
4420 /*% ripper: paren!($:body) %*/
4421 }
4422 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4423 {
4424 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4425 /*% ripper: const_path_ref!($:recv, $:name) %*/
4426 }
4427 | tCOLON3[top] tCONSTANT[name]
4428 {
4429 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4430 /*% ripper: top_const_ref!($:name) %*/
4431 }
4432 | tLBRACK aref_args[args] ']'
4433 {
4434 $$ = make_list($args, &@$);
4435 /*% ripper: array!($:args) %*/
4436 }
4437 | tLBRACE assoc_list[list] '}'
4438 {
4439 $$ = new_hash(p, $list, &@$);
4440 RNODE_HASH($$)->nd_brace = TRUE;
4441 /*% ripper: hash!($:list) %*/
4442 }
4443 | k_return[kw]
4444 {
4445 $$ = NEW_RETURN(0, &@$, &@kw);
4446 /*% ripper: return0! %*/
4447 }
4448 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4449 {
4450 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4451 /*% ripper: yield!(paren!($:args)) %*/
4452 }
4453 | k_yield[kw] '('[lpar] rparen[rpar]
4454 {
4455 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4456 /*% ripper: yield!(paren!(args_new!)) %*/
4457 }
4458 | k_yield[kw]
4459 {
4460 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4461 /*% ripper: yield0! %*/
4462 }
4463 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
4464 {
4465 p->ctxt.in_defined = $ctxt.in_defined;
4466 $$ = new_defined(p, $arg, &@$, &@kw);
4467 p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon;
4468 /*% ripper: defined!($:arg) %*/
4469 }
4470 | keyword_not[kw] '(' expr[arg] rparen
4471 {
4472 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4473 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4474 }
4475 | keyword_not[kw] '('[lpar] rparen
4476 {
4477 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4478 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4479 }
4480 | fcall[call] brace_block[block]
4481 {
4482 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4483 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4484 }
4485 | method_call
4486 | method_call[call] brace_block[block]
4487 {
4488 block_dup_check(p, get_nd_args(p, $call), $block);
4489 $$ = method_add_block(p, $call, $block, &@$);
4490 /*% ripper: method_add_block!($:call, $:block) %*/
4491 }
4492 | lambda
4493 | k_if[kw] expr_value[cond] then[then]
4494 compstmt(stmts)[body]
4495 if_tail[tail]
4496 k_end[k_end]
4497 {
4498 if ($tail && nd_type_p($tail, NODE_IF))
4499 RNODE_IF($tail)->end_keyword_loc = @k_end;
4500
4501 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4502 fixpos($$, $cond);
4503 /*% ripper: if!($:cond, $:body, $:tail) %*/
4504 }
4505 | k_unless[kw] expr_value[cond] then[then]
4506 compstmt(stmts)[body]
4507 opt_else[tail]
4508 k_end[k_end]
4509 {
4510 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4511 fixpos($$, $cond);
4512 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4513 }
4514 | k_while[kw] expr_value_do[cond]
4515 compstmt(stmts)[body]
4516 k_end[k_end]
4517 {
4518 restore_block_exit(p, $kw);
4519 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4520 fixpos($$, $cond);
4521 /*% ripper: while!($:cond, $:body) %*/
4522 }
4523 | k_until[kw] expr_value_do[cond]
4524 compstmt(stmts)[body]
4525 k_end[k_end]
4526 {
4527 restore_block_exit(p, $kw);
4528 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4529 fixpos($$, $cond);
4530 /*% ripper: until!($:cond, $:body) %*/
4531 }
4532 | k_case[k_case] expr_value[expr] terms?
4533 {
4534 $$ = p->case_labels;
4535 p->case_labels = CHECK_LITERAL_WHEN;
4536 }[labels]<labels>
4537 case_body[body]
4538 k_end[k_end]
4539 {
4540 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4541 p->case_labels = $labels;
4542 $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end);
4543 fixpos($$, $expr);
4544 /*% ripper: case!($:expr, $:body) %*/
4545 }
4546 | k_case[k_case] terms?
4547 {
4548 $$ = p->case_labels;
4549 p->case_labels = 0;
4550 }[labels]<labels>
4551 case_body[body]
4552 k_end[k_end]
4553 {
4554 if (p->case_labels) st_free_table(p->case_labels);
4555 p->case_labels = $labels;
4556 $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end);
4557 /*% ripper: case!(Qnil, $:body) %*/
4558 }
4559 | k_case[k_case] expr_value[expr] terms?
4560 p_case_body[body]
4561 k_end[k_end]
4562 {
4563 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4564 /*% ripper: case!($:expr, $:body) %*/
4565 }
4566 | k_for[k_for] for_var[for_var] keyword_in[keyword_in]
4567 {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();}
4568 compstmt(stmts)[compstmt]
4569 k_end[k_end]
4570 {
4571 restore_block_exit(p, $k_for);
4572 /*
4573 * for a, b, c in e
4574 * #=>
4575 * e.each{|*x| a, b, c = x}
4576 *
4577 * for a in e
4578 * #=>
4579 * e.each{|x| a, = x}
4580 */
4581 ID id = internal_id(p);
4582 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4583 rb_node_args_t *args;
4584 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4585 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4586 tbl->ids[0] = id; /* internal id */
4587
4588 switch (nd_type($for_var)) {
4589 case NODE_LASGN:
4590 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4591 set_nd_value(p, $for_var, internal_var);
4592 id = 0;
4593 m->nd_plen = 1;
4594 m->nd_next = $for_var;
4595 break;
4596 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4597 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4598 break;
4599 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4600 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);
4601 }
4602 /* {|*internal_id| <m> = internal_id; ... } */
4603 args = new_args(p, m, 0, id, 0, new_empty_args_tail(p, &@for_var), &@for_var);
4604 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4605 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4606 $$ = NEW_FOR($expr_value, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4607 RNODE_SCOPE(scope)->nd_parent = $$;
4608 fixpos($$, $for_var);
4609 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4610 }
4611 | k_class cpath superclass
4612 {
4613 begin_definition("class", &@k_class, &@cpath);
4614 }
4615 bodystmt
4616 k_end
4617 {
4618 YYLTYPE inheritance_operator_loc = NULL_LOC;
4619 if ($superclass) {
4620 inheritance_operator_loc = @superclass;
4621 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4622 }
4623 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4624 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4625 set_line_body($bodystmt, @superclass.end_pos.lineno);
4626 nd_set_line($$, @superclass.end_pos.lineno);
4627 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4628 local_pop(p);
4629 p->ctxt.in_class = $k_class.in_class;
4630 p->ctxt.cant_return = $k_class.cant_return;
4631 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4632 }
4633 | k_class tLSHFT expr_value
4634 {
4635 begin_definition("", &@k_class, &@tLSHFT);
4636 }
4637 term
4638 bodystmt
4639 k_end
4640 {
4641 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4642 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4643 set_line_body($bodystmt, nd_line($expr_value));
4644 fixpos($$, $expr_value);
4645 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4646 local_pop(p);
4647 p->ctxt.in_def = $k_class.in_def;
4648 p->ctxt.in_class = $k_class.in_class;
4649 p->ctxt.cant_return = $k_class.cant_return;
4650 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4651 }
4652 | k_module cpath
4653 {
4654 begin_definition("module", &@k_module, &@cpath);
4655 }
4656 bodystmt
4657 k_end
4658 {
4659 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4660 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4661 set_line_body($bodystmt, @cpath.end_pos.lineno);
4662 nd_set_line($$, @cpath.end_pos.lineno);
4663 /*% ripper: module!($:cpath, $:bodystmt) %*/
4664 local_pop(p);
4665 p->ctxt.in_class = $k_module.in_class;
4666 p->ctxt.cant_return = $k_module.cant_return;
4667 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4668 }
4669 | defn_head[head]
4670 f_arglist[args]
4671 {
4672 push_end_expect_token_locations(p, &@head.beg_pos);
4673 }
4674 bodystmt
4675 k_end
4676 {
4677 restore_defun(p, $head);
4678 ($$ = $head->nd_def)->nd_loc = @$;
4679 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4680 RNODE_DEFN($$)->nd_defn = $bodystmt;
4681 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4682 local_pop(p);
4683 }
4684 | defs_head[head]
4685 f_arglist[args]
4686 {
4687 push_end_expect_token_locations(p, &@head.beg_pos);
4688 }
4689 bodystmt
4690 k_end
4691 {
4692 restore_defun(p, $head);
4693 ($$ = $head->nd_def)->nd_loc = @$;
4694 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4695 RNODE_DEFS($$)->nd_defn = $bodystmt;
4696 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4697 local_pop(p);
4698 }
4699 | keyword_break[kw]
4700 {
4701 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4702 /*% ripper: break!(args_new!) %*/
4703 }
4704 | keyword_next[kw]
4705 {
4706 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4707 /*% ripper: next!(args_new!) %*/
4708 }
4709 | keyword_redo[kw]
4710 {
4711 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4712 /*% ripper: redo! %*/
4713 }
4714 | keyword_retry[kw]
4715 {
4716 if (!p->ctxt.in_defined) {
4717 switch (p->ctxt.in_rescue) {
4718 case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break;
4719 case after_rescue: /* ok */ break;
4720 case after_else: yyerror1(&@kw, "Invalid retry after else"); break;
4721 case after_ensure: yyerror1(&@kw, "Invalid retry after ensure"); break;
4722 }
4723 }
4724 $$ = NEW_RETRY(&@$);
4725 /*% ripper: retry! %*/
4726 }
4727 ;
4728
4729primary_value : value_expr(primary)
4730 ;
4731
4732k_begin : keyword_begin
4733 {
4734 token_info_push(p, "begin", &@$);
4735 push_end_expect_token_locations(p, &@1.beg_pos);
4736 }
4737 ;
4738
4739k_if : keyword_if
4740 {
4741 WARN_EOL("if");
4742 token_info_push(p, "if", &@$);
4743 if (p->token_info && p->token_info->nonspc &&
4744 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4745 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4746 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4747 beg += rb_strlen_lit("else");
4748 while (beg < tok && ISSPACE(*beg)) beg++;
4749 if (beg == tok) {
4750 p->token_info->nonspc = 0;
4751 }
4752 }
4753 push_end_expect_token_locations(p, &@1.beg_pos);
4754 }
4755 ;
4756
4757k_unless : keyword_unless
4758 {
4759 token_info_push(p, "unless", &@$);
4760 push_end_expect_token_locations(p, &@1.beg_pos);
4761 }
4762 ;
4763
4764k_while : keyword_while[kw] allow_exits
4765 {
4766 $$ = $allow_exits;
4767 token_info_push(p, "while", &@$);
4768 push_end_expect_token_locations(p, &@kw.beg_pos);
4769 }
4770 ;
4771
4772k_until : keyword_until[kw] allow_exits
4773 {
4774 $$ = $allow_exits;
4775 token_info_push(p, "until", &@$);
4776 push_end_expect_token_locations(p, &@kw.beg_pos);
4777 }
4778 ;
4779
4780k_case : keyword_case
4781 {
4782 token_info_push(p, "case", &@$);
4783 push_end_expect_token_locations(p, &@1.beg_pos);
4784 }
4785 ;
4786
4787k_for : keyword_for[kw] allow_exits
4788 {
4789 $$ = $allow_exits;
4790 token_info_push(p, "for", &@$);
4791 push_end_expect_token_locations(p, &@kw.beg_pos);
4792 }
4793 ;
4794
4795k_class : keyword_class
4796 {
4797 token_info_push(p, "class", &@$);
4798 $$ = p->ctxt;
4799 p->ctxt.in_rescue = before_rescue;
4800 push_end_expect_token_locations(p, &@1.beg_pos);
4801 }
4802 ;
4803
4804k_module : keyword_module
4805 {
4806 token_info_push(p, "module", &@$);
4807 $$ = p->ctxt;
4808 p->ctxt.in_rescue = before_rescue;
4809 push_end_expect_token_locations(p, &@1.beg_pos);
4810 }
4811 ;
4812
4813k_def : keyword_def
4814 {
4815 token_info_push(p, "def", &@$);
4816 $$ = NEW_DEF_TEMP(&@$);
4817 p->ctxt.in_argdef = 1;
4818 }
4819 ;
4820
4821k_do : keyword_do
4822 {
4823 token_info_push(p, "do", &@$);
4824 push_end_expect_token_locations(p, &@1.beg_pos);
4825 }
4826 ;
4827
4828k_do_block : keyword_do_block
4829 {
4830 token_info_push(p, "do", &@$);
4831 push_end_expect_token_locations(p, &@1.beg_pos);
4832 }
4833 ;
4834
4835k_rescue : keyword_rescue
4836 {
4837 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4838 $$ = p->ctxt;
4839 p->ctxt.in_rescue = after_rescue;
4840 }
4841 ;
4842
4843k_ensure : keyword_ensure
4844 {
4845 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4846 $$ = p->ctxt;
4847 }
4848 ;
4849
4850k_when : keyword_when
4851 {
4852 token_info_warn(p, "when", p->token_info, 0, &@$);
4853 }
4854 ;
4855
4856k_else : keyword_else
4857 {
4858 token_info *ptinfo_beg = p->token_info;
4859 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4860 token_info_warn(p, "else", p->token_info, same, &@$);
4861 if (same) {
4862 token_info e;
4863 e.next = ptinfo_beg->next;
4864 e.token = "else";
4865 token_info_setup(&e, p->lex.pbeg, &@$);
4866 if (!e.nonspc) *ptinfo_beg = e;
4867 }
4868 }
4869 ;
4870
4871k_elsif : keyword_elsif
4872 {
4873 WARN_EOL("elsif");
4874 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4875 }
4876 ;
4877
4878k_end : keyword_end
4879 {
4880 token_info_pop(p, "end", &@$);
4881 pop_end_expect_token_locations(p);
4882 }
4883 | tDUMNY_END
4884 {
4885 compile_error(p, "syntax error, unexpected end-of-input");
4886 }
4887 ;
4888
4889k_return : keyword_return
4890 {
4891 if (p->ctxt.cant_return && !dyna_in_block(p))
4892 yyerror1(&@1, "Invalid return in class/module body");
4893 }
4894 ;
4895
4896k_yield : keyword_yield
4897 {
4898 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4899 yyerror1(&@1, "Invalid yield");
4900 }
4901 ;
4902
4903then : term
4904 | keyword_then
4905 | term keyword_then
4906 ;
4907
4908do : term
4909 | keyword_do_cond { $$ = keyword_do_cond; }
4910 ;
4911
4912if_tail : opt_else
4913 | k_elsif expr_value then
4914 compstmt(stmts)
4915 if_tail
4916 {
4917 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4918 fixpos($$, $2);
4919 /*% ripper: elsif!($:2, $:4, $:5) %*/
4920 }
4921 ;
4922
4923opt_else : none
4924 | k_else compstmt(stmts)
4925 {
4926 $$ = $2;
4927 /*% ripper: else!($:2) %*/
4928 }
4929 ;
4930
4931for_var : lhs
4932 | mlhs
4933 ;
4934
4935f_marg : f_norm_arg
4936 {
4937 $$ = assignable(p, $1, 0, &@$);
4938 mark_lvar_used(p, $$);
4939 }
4940 | tLPAREN f_margs rparen
4941 {
4942 $$ = (NODE *)$2;
4943 /*% ripper: mlhs_paren!($:2) %*/
4944 }
4945 ;
4946
4947
4948f_margs : mlhs_items(f_marg)
4949 {
4950 $$ = NEW_MASGN($1, 0, &@$);
4951 /*% ripper: $:1 %*/
4952 }
4953 | mlhs_items(f_marg) ',' f_rest_marg
4954 {
4955 $$ = NEW_MASGN($1, $3, &@$);
4956 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4957 }
4958 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4959 {
4960 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4961 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4962 }
4963 | f_rest_marg
4964 {
4965 $$ = NEW_MASGN(0, $1, &@$);
4966 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4967 }
4968 | f_rest_marg ',' mlhs_items(f_marg)
4969 {
4970 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4971 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4972 }
4973 ;
4974
4975f_rest_marg : tSTAR f_norm_arg
4976 {
4977 /*% ripper: $:2 %*/
4978 $$ = assignable(p, $2, 0, &@$);
4979 mark_lvar_used(p, $$);
4980 }
4981 | tSTAR
4982 {
4983 $$ = NODE_SPECIAL_NO_NAME_REST;
4984 /*% ripper: Qnil %*/
4985 }
4986 ;
4987
4988f_any_kwrest : f_kwrest
4989 | f_no_kwarg
4990 {
4991 $$ = idNil;
4992 /*% ripper: ID2VAL(idNil) %*/
4993 }
4994 ;
4995
4996f_eq : {p->ctxt.in_argdef = 0;} '=';
4997
4998block_args_tail : args_tail_basic(primary_value, none)
4999 ;
5000
5001excessed_comma : ','
5002 {
5003 /* magic number for rest_id in iseq_set_arguments() */
5004 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5005 /*% ripper: excessed_comma! %*/
5006 }
5007 ;
5008
5009block_param : args-list(primary_value, opt_args_tail(block_args_tail, none))
5010 | f_arg[pre] excessed_comma
5011 {
5012 $$ = new_empty_args_tail(p, &@excessed_comma);
5013 $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$);
5014 /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/
5015 }
5016 | f_arg[pre] opt_args_tail(block_args_tail, none)[tail]
5017 {
5018 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5019 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5020 }
5021 | tail-only-args(block_args_tail)
5022 ;
5023
5024opt_block_param_def : none
5025 | block_param_def
5026 {
5027 p->command_start = TRUE;
5028 }
5029 ;
5030
5031block_param_def : '|' opt_block_param opt_bv_decl '|'
5032 {
5033 p->max_numparam = ORDINAL_PARAM;
5034 p->ctxt.in_argdef = 0;
5035 $$ = $2;
5036 /*% ripper: block_var!($:2, $:3) %*/
5037 }
5038 ;
5039
5040opt_block_param : /* none */
5041 {
5042 $$ = 0;
5043 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5044 }
5045 | block_param
5046 ;
5047
5048opt_bv_decl : '\n'?
5049 {
5050 $$ = 0;
5051 /*% ripper: Qfalse %*/
5052 }
5053 | '\n'? ';' bv_decls '\n'?
5054 {
5055 $$ = 0;
5056 /*% ripper: $:3 %*/
5057 }
5058 ;
5059
5060bv_decls : bvar
5061 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5062 | bv_decls ',' bvar
5063 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5064 ;
5065
5066bvar : tIDENTIFIER
5067 {
5068 new_bv(p, $1);
5069 /*% ripper: $:1 %*/
5070 }
5071 | f_bad_arg
5072 ;
5073
5074max_numparam : {
5075 $$ = p->max_numparam;
5076 p->max_numparam = 0;
5077 }
5078 ;
5079
5080numparam : {
5081 $$ = numparam_push(p);
5082 }
5083 ;
5084
5085it_id : {
5086 $$ = p->it_id;
5087 p->it_id = 0;
5088 }
5089 ;
5090
5091lambda : tLAMBDA[lpar]
5092 {
5093 token_info_push(p, "->", &@lpar);
5094 $$ = dyna_push(p);
5095 }[dyna]<vars>
5096 max_numparam numparam it_id allow_exits
5097 f_larglist[args]
5098 {
5099 CMDARG_PUSH(0);
5100 }
5101 lambda_body[body]
5102 {
5103 int max_numparam = p->max_numparam;
5104 ID it_id = p->it_id;
5105 p->lex.lpar_beg = $lpar;
5106 p->max_numparam = $max_numparam;
5107 p->it_id = $it_id;
5108 restore_block_exit(p, $allow_exits);
5109 CMDARG_POP();
5110 $args = args_with_numbered(p, $args, max_numparam, it_id);
5111 {
5112 YYLTYPE loc = code_loc_gen(&@lpar, &@body);
5113 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5114 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5115 nd_set_line($$, @args.end_pos.lineno);
5116 nd_set_first_loc($$, @lpar.beg_pos);
5117 xfree($body);
5118 }
5119 /*% ripper: lambda!($:args, $:body) %*/
5120 numparam_pop(p, $numparam);
5121 dyna_pop(p, $dyna);
5122 }
5123 ;
5124
5125f_larglist : '(' f_largs[args] opt_bv_decl ')'
5126 {
5127 p->ctxt.in_argdef = 0;
5128 $$ = $args;
5129 p->max_numparam = ORDINAL_PARAM;
5130 /*% ripper: paren!($:args) %*/
5131 }
5132 | f_largs[args]
5133 {
5134 p->ctxt.in_argdef = 0;
5135 if (!args_info_empty_p(&$args->nd_ainfo))
5136 p->max_numparam = ORDINAL_PARAM;
5137 $$ = $args;
5138 }
5139 ;
5140
5141lambda_body : tLAMBEG compstmt(stmts) '}'
5142 {
5143 token_info_pop(p, "}", &@3);
5144 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5145 /*% ripper: $:2 %*/
5146 }
5147 | keyword_do_LAMBDA
5148 {
5149 push_end_expect_token_locations(p, &@1.beg_pos);
5150 }
5151 bodystmt k_end
5152 {
5153 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5154 /*% ripper: $:3 %*/
5155 }
5156 ;
5157
5158do_block : k_do_block do_body k_end
5159 {
5160 $$ = $2;
5161 set_embraced_location($$, &@1, &@3);
5162 /*% ripper: $:2 %*/
5163 }
5164 ;
5165
5166block_call : command do_block
5167 {
5168 $$ = command_add_block(p, $1, $2, &@$);
5169 fixpos($$, $1);
5170 /*% ripper: method_add_block!($:1, $:2) %*/
5171 }
5172 | block_call call_op2 operation2 opt_paren_args
5173 {
5174 bool has_args = $4 != 0;
5175 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5176 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5177 /*% ripper: call!($:1, $:2, $:3) %*/
5178 if (has_args) {
5179 /*% ripper: method_add_arg!($:$, $:4) %*/
5180 }
5181 }
5182 | block_call call_op2 operation2 opt_paren_args brace_block
5183 {
5184 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5185 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5186 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5187 }
5188 | block_call call_op2 operation2 command_args do_block
5189 {
5190 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5191 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5192 }
5193 | block_call call_op2 paren_args
5194 {
5195 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5196 nd_set_line($$, @2.end_pos.lineno);
5197 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5198 }
5199 ;
5200
5201method_call : fcall paren_args
5202 {
5203 $1->nd_args = $2;
5204 $$ = (NODE *)$1;
5205 nd_set_last_loc($1, @2.end_pos);
5206 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5207 }
5208 | primary_value call_op operation2 opt_paren_args
5209 {
5210 bool has_args = $4 != 0;
5211 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5212 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5213 nd_set_line($$, @3.end_pos.lineno);
5214 /*% ripper: call!($:1, $:2, $:3) %*/
5215 if (has_args) {
5216 /*% ripper: method_add_arg!($:$, $:4) %*/
5217 }
5218 }
5219 | primary_value tCOLON2 operation2 paren_args
5220 {
5221 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5222 nd_set_line($$, @3.end_pos.lineno);
5223 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5224 }
5225 | primary_value tCOLON2 operation3
5226 {
5227 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5228 /*% ripper: call!($:1, $:2, $:3) %*/
5229 }
5230 | primary_value call_op2 paren_args
5231 {
5232 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5233 nd_set_line($$, @2.end_pos.lineno);
5234 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5235 }
5236 | keyword_super paren_args
5237 {
5238 rb_code_location_t lparen_loc = @2;
5239 rb_code_location_t rparen_loc = @2;
5240 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5241 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5242
5243 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5244 /*% ripper: super!($:2) %*/
5245 }
5246 | keyword_super
5247 {
5248 $$ = NEW_ZSUPER(&@$);
5249 /*% ripper: zsuper! %*/
5250 }
5251 | primary_value '[' opt_call_args rbracket
5252 {
5253 $$ = NEW_CALL($1, tAREF, $3, &@$);
5254 fixpos($$, $1);
5255 /*% ripper: aref!($:1, $:3) %*/
5256 }
5257 ;
5258
5259brace_block : '{' brace_body '}'
5260 {
5261 $$ = $2;
5262 set_embraced_location($$, &@1, &@3);
5263 /*% ripper: $:2 %*/
5264 }
5265 | k_do do_body k_end
5266 {
5267 $$ = $2;
5268 set_embraced_location($$, &@1, &@3);
5269 /*% ripper: $:2 %*/
5270 }
5271 ;
5272
5273brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5274 max_numparam numparam it_id allow_exits
5275 opt_block_param_def[args] compstmt(stmts)
5276 {
5277 int max_numparam = p->max_numparam;
5278 ID it_id = p->it_id;
5279 p->max_numparam = $max_numparam;
5280 p->it_id = $it_id;
5281 $args = args_with_numbered(p, $args, max_numparam, it_id);
5282 $$ = NEW_ITER($args, $compstmt, &@$);
5283 /*% ripper: brace_block!($:args, $:compstmt) %*/
5284 restore_block_exit(p, $allow_exits);
5285 numparam_pop(p, $numparam);
5286 dyna_pop(p, $dyna);
5287 }
5288 ;
5289
5290do_body : {
5291 $$ = dyna_push(p);
5292 CMDARG_PUSH(0);
5293 }[dyna]<vars>
5294 max_numparam numparam it_id allow_exits
5295 opt_block_param_def[args] bodystmt
5296 {
5297 int max_numparam = p->max_numparam;
5298 ID it_id = p->it_id;
5299 p->max_numparam = $max_numparam;
5300 p->it_id = $it_id;
5301 $args = args_with_numbered(p, $args, max_numparam, it_id);
5302 $$ = NEW_ITER($args, $bodystmt, &@$);
5303 /*% ripper: do_block!($:args, $:bodystmt) %*/
5304 CMDARG_POP();
5305 restore_block_exit(p, $allow_exits);
5306 numparam_pop(p, $numparam);
5307 dyna_pop(p, $dyna);
5308 }
5309 ;
5310
5311case_args : arg_value
5312 {
5313 check_literal_when(p, $arg_value, &@arg_value);
5314 $$ = NEW_LIST($arg_value, &@$);
5315 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5316 }
5317 | tSTAR arg_value
5318 {
5319 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5320 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5321 }
5322 | case_args[non_last_args] ',' arg_value
5323 {
5324 check_literal_when(p, $arg_value, &@arg_value);
5325 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5326 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5327 }
5328 | case_args[non_last_args] ',' tSTAR arg_value
5329 {
5330 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5331 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5332 }
5333 ;
5334
5335case_body : k_when case_args then
5336 compstmt(stmts)
5337 cases
5338 {
5339 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5340 fixpos($$, $2);
5341 /*% ripper: when!($:2, $:4, $:5) %*/
5342 }
5343 ;
5344
5345cases : opt_else
5346 | case_body
5347 ;
5348
5349p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5350p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5351
5352p_in_kwarg : {
5353 $$ = p->ctxt;
5354 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5355 p->command_start = FALSE;
5356 p->ctxt.in_kwarg = 1;
5357 p->ctxt.in_alt_pattern = 0;
5358 p->ctxt.capture_in_pattern = 0;
5359 }
5360 ;
5361
5362p_case_body : keyword_in
5363 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5364 p_top_expr[expr] then
5365 {
5366 pop_pktbl(p, $p_pktbl);
5367 pop_pvtbl(p, $p_pvtbl);
5368 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5369 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5370 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5371 }
5372 compstmt(stmts)
5373 p_cases[cases]
5374 {
5375 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5376 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5377 }
5378 ;
5379
5380p_cases : opt_else
5381 | p_case_body
5382 ;
5383
5384p_top_expr : p_top_expr_body
5385 | p_top_expr_body modifier_if expr_value
5386 {
5387 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5388 fixpos($$, $3);
5389 /*% ripper: if_mod!($:3, $:1) %*/
5390 }
5391 | p_top_expr_body modifier_unless expr_value
5392 {
5393 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5394 fixpos($$, $3);
5395 /*% ripper: unless_mod!($:3, $:1) %*/
5396 }
5397 ;
5398
5399p_top_expr_body : p_expr
5400 | p_expr ','
5401 {
5402 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5403 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5404 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5405 }
5406 | p_expr ',' p_args
5407 {
5408 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5409 nd_set_first_loc($$, @1.beg_pos);
5410 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5411 }
5412 | p_find
5413 {
5414 $$ = new_find_pattern(p, 0, $1, &@$);
5415 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5416 }
5417 | p_args_tail
5418 {
5419 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5420 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5421 }
5422 | p_kwargs
5423 {
5424 $$ = new_hash_pattern(p, 0, $1, &@$);
5425 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5426 }
5427 ;
5428
5429p_expr : p_as
5430 ;
5431
5432p_as : p_expr tASSOC p_variable
5433 {
5434 NODE *n = NEW_LIST($1, &@$);
5435 n = list_append(p, n, $3);
5436 $$ = new_hash(p, n, &@$);
5437 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5438 }
5439 | p_alt
5440 ;
5441
5442p_alt : p_alt[left] '|'[alt]
5443 {
5444 p->ctxt.in_alt_pattern = 1;
5445 }
5446 p_expr_basic[right]
5447 {
5448 if (p->ctxt.capture_in_pattern) {
5449 yyerror1(&@alt, "alternative pattern after variable capture");
5450 }
5451 p->ctxt.in_alt_pattern = 0;
5452 $$ = NEW_OR($left, $right, &@$, &@alt);
5453 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5454 }
5455 | p_expr_basic
5456 ;
5457
5458p_lparen : '(' p_pktbl
5459 {
5460 $$ = $2;
5461 /*% ripper: $:2 %*/
5462 }
5463 ;
5464
5465p_lbracket : '[' p_pktbl
5466 {
5467 $$ = $2;
5468 /*% ripper: $:2 %*/
5469 }
5470 ;
5471
5472p_expr_basic : p_value
5473 | p_variable
5474 | p_const p_lparen[p_pktbl] p_args rparen
5475 {
5476 pop_pktbl(p, $p_pktbl);
5477 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5478 nd_set_first_loc($$, @p_const.beg_pos);
5479 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5480 }
5481 | p_const p_lparen[p_pktbl] p_find rparen
5482 {
5483 pop_pktbl(p, $p_pktbl);
5484 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5485 nd_set_first_loc($$, @p_const.beg_pos);
5486 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5487 }
5488 | p_const p_lparen[p_pktbl] p_kwargs rparen
5489 {
5490 pop_pktbl(p, $p_pktbl);
5491 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5492 nd_set_first_loc($$, @p_const.beg_pos);
5493 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5494 }
5495 | p_const '(' rparen
5496 {
5497 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5498 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5499 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5500 }
5501 | p_const p_lbracket[p_pktbl] p_args rbracket
5502 {
5503 pop_pktbl(p, $p_pktbl);
5504 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5505 nd_set_first_loc($$, @p_const.beg_pos);
5506 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5507 }
5508 | p_const p_lbracket[p_pktbl] p_find rbracket
5509 {
5510 pop_pktbl(p, $p_pktbl);
5511 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5512 nd_set_first_loc($$, @p_const.beg_pos);
5513 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5514 }
5515 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5516 {
5517 pop_pktbl(p, $p_pktbl);
5518 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5519 nd_set_first_loc($$, @p_const.beg_pos);
5520 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5521 }
5522 | p_const '[' rbracket
5523 {
5524 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5525 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5526 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5527 }
5528 | tLBRACK p_args rbracket
5529 {
5530 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5531 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5532 }
5533 | tLBRACK p_find rbracket
5534 {
5535 $$ = new_find_pattern(p, 0, $p_find, &@$);
5536 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5537 }
5538 | tLBRACK rbracket
5539 {
5540 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5541 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5542 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5543 }
5544 | tLBRACE p_pktbl lex_ctxt[ctxt]
5545 {
5546 p->ctxt.in_kwarg = 0;
5547 }
5548 p_kwargs rbrace
5549 {
5550 pop_pktbl(p, $p_pktbl);
5551 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5552 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5553 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5554 }
5555 | tLBRACE rbrace
5556 {
5557 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5558 $$ = new_hash_pattern(p, 0, $$, &@$);
5559 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5560 }
5561 | tLPAREN p_pktbl p_expr rparen
5562 {
5563 pop_pktbl(p, $p_pktbl);
5564 $$ = $p_expr;
5565 /*% ripper: $:p_expr %*/
5566 }
5567 ;
5568
5569p_args : p_expr
5570 {
5571 NODE *pre_args = NEW_LIST($1, &@$);
5572 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5573 /*% ripper: [[$:1], Qnil, Qnil] %*/
5574 }
5575 | p_args_head
5576 {
5577 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5578 /*% ripper: [$:1, Qnil, Qnil] %*/
5579 }
5580 | p_args_head p_arg
5581 {
5582 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5583 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5584 }
5585 | p_args_head p_rest
5586 {
5587 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5588 /*% ripper: [$:1, $:2, Qnil] %*/
5589 }
5590 | p_args_head p_rest ',' p_args_post
5591 {
5592 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5593 /*% ripper: [$:1, $:2, $:4] %*/
5594 }
5595 | p_args_tail
5596 ;
5597
5598p_args_head : p_arg ','
5599 | p_args_head p_arg ','
5600 {
5601 $$ = list_concat($1, $2);
5602 /*% ripper: rb_ary_concat($:1, $:2) %*/
5603 }
5604 ;
5605
5606p_args_tail : p_rest
5607 {
5608 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5609 /*% ripper: [Qnil, $:1, Qnil] %*/
5610 }
5611 | p_rest ',' p_args_post
5612 {
5613 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5614 /*% ripper: [Qnil, $:1, $:3] %*/
5615 }
5616 ;
5617
5618p_find : p_rest ',' p_args_post ',' p_rest
5619 {
5620 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5621 /*% ripper: [$:1, $:3, $:5] %*/
5622 }
5623 ;
5624
5625
5626p_rest : tSTAR tIDENTIFIER
5627 {
5628 error_duplicate_pattern_variable(p, $2, &@2);
5629 /*% ripper: var_field!($:2) %*/
5630 $$ = assignable(p, $2, 0, &@$);
5631 }
5632 | tSTAR
5633 {
5634 $$ = 0;
5635 /*% ripper: var_field!(Qnil) %*/
5636 }
5637 ;
5638
5639p_args_post : p_arg
5640 | p_args_post ',' p_arg
5641 {
5642 $$ = list_concat($1, $3);
5643 /*% ripper: rb_ary_concat($:1, $:3) %*/
5644 }
5645 ;
5646
5647p_arg : p_expr
5648 {
5649 $$ = NEW_LIST($1, &@$);
5650 /*% ripper: [$:1] %*/
5651 }
5652 ;
5653
5654p_kwargs : p_kwarg ',' p_any_kwrest
5655 {
5656 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5657 /*% ripper: [$:1, $:3] %*/
5658 }
5659 | p_kwarg
5660 {
5661 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5662 /*% ripper: [$:1, Qnil] %*/
5663 }
5664 | p_kwarg ','
5665 {
5666 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5667 /*% ripper: [$:1, Qnil] %*/
5668 }
5669 | p_any_kwrest
5670 {
5671 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5672 /*% ripper: [[], $:1] %*/
5673 }
5674 ;
5675
5676p_kwarg : p_kw
5677 /*% ripper[brace]: [$:1] %*/
5678 | p_kwarg ',' p_kw
5679 {
5680 $$ = list_concat($1, $3);
5681 /*% ripper: rb_ary_push($:1, $:3) %*/
5682 }
5683 ;
5684
5685p_kw : p_kw_label p_expr
5686 {
5687 error_duplicate_pattern_key(p, $1, &@1);
5688 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5689 /*% ripper: [$:1, $:2] %*/
5690 }
5691 | p_kw_label
5692 {
5693 error_duplicate_pattern_key(p, $1, &@1);
5694 if ($1 && !is_local_id($1)) {
5695 yyerror1(&@1, "key must be valid as local variables");
5696 }
5697 error_duplicate_pattern_variable(p, $1, &@1);
5698 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5699 /*% ripper: [$:1, Qnil] %*/
5700 }
5701 ;
5702
5703p_kw_label : tLABEL
5704 | tSTRING_BEG string_contents tLABEL_END
5705 {
5706 YYLTYPE loc = code_loc_gen(&@1, &@3);
5707 if (!$2 || nd_type_p($2, NODE_STR)) {
5708 NODE *node = dsym_node(p, $2, &loc);
5709 $$ = rb_sym2id(rb_node_sym_string_val(node));
5710 }
5711 else {
5712 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5713 $$ = rb_intern_str(STR_NEW0());
5714 }
5715 /*% ripper: $:2 %*/
5716 }
5717 ;
5718
5719p_kwrest : kwrest_mark tIDENTIFIER
5720 {
5721 $$ = $2;
5722 /*% ripper: var_field!($:2) %*/
5723 }
5724 | kwrest_mark
5725 {
5726 $$ = 0;
5727 /*% ripper: Qnil %*/
5728 }
5729 ;
5730
5731p_kwnorest : kwrest_mark keyword_nil
5732 {
5733 $$ = 0;
5734 }
5735 ;
5736
5737p_any_kwrest : p_kwrest
5738 | p_kwnorest
5739 {
5740 $$ = idNil;
5741 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5742 }
5743 ;
5744
5745p_value : p_primitive
5746 | range_expr(p_primitive)
5747 | p_var_ref
5748 | p_expr_ref
5749 | p_const
5750 ;
5751
5752p_primitive : inline_primary
5753 | keyword_variable
5754 {
5755 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5756 /*% ripper: var_ref!($:1) %*/
5757 }
5758 | lambda
5759 ;
5760
5761p_variable : tIDENTIFIER
5762 {
5763 error_duplicate_pattern_variable(p, $1, &@1);
5764 /*% ripper: var_field!($:1) %*/
5765 $$ = assignable(p, $1, 0, &@$);
5766 }
5767 ;
5768
5769p_var_ref : '^' tIDENTIFIER
5770 {
5771 NODE *n = gettable(p, $2, &@$);
5772 if (!n) {
5773 n = NEW_ERROR(&@$);
5774 }
5775 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5776 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5777 }
5778 $$ = n;
5779 /*% ripper: var_ref!($:2) %*/
5780 }
5781 | '^' nonlocal_var
5782 {
5783 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5784 /*% ripper: var_ref!($:2) %*/
5785 }
5786 ;
5787
5788p_expr_ref : '^' tLPAREN expr_value rparen
5789 {
5790 $$ = NEW_BLOCK($3, &@$);
5791 /*% ripper: begin!($:3) %*/
5792 }
5793 ;
5794
5795p_const : tCOLON3 cname
5796 {
5797 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5798 /*% ripper: top_const_ref!($:2) %*/
5799 }
5800 | p_const tCOLON2 cname
5801 {
5802 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5803 /*% ripper: const_path_ref!($:1, $:3) %*/
5804 }
5805 | tCONSTANT
5806 {
5807 $$ = gettable(p, $1, &@$);
5808 /*% ripper: var_ref!($:1) %*/
5809 }
5810 ;
5811
5812opt_rescue : k_rescue exc_list exc_var then
5813 compstmt(stmts)
5814 opt_rescue
5815 {
5816 NODE *err = $3;
5817 if ($3) {
5818 err = NEW_ERRINFO(&@3);
5819 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5820 }
5821 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5822 if ($2) {
5823 fixpos($$, $2);
5824 }
5825 else if ($3) {
5826 fixpos($$, $3);
5827 }
5828 else {
5829 fixpos($$, $5);
5830 }
5831 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5832 }
5833 | none
5834 ;
5835
5836exc_list : arg_value
5837 {
5838 $$ = NEW_LIST($1, &@$);
5839 /*% ripper: rb_ary_new3(1, $:1) %*/
5840 }
5841 | mrhs
5842 {
5843 if (!($$ = splat_array($1))) $$ = $1;
5844 }
5845 | none
5846 ;
5847
5848exc_var : tASSOC lhs
5849 {
5850 $$ = $2;
5851 /*% ripper: $:2 %*/
5852 }
5853 | none
5854 ;
5855
5856opt_ensure : k_ensure stmts terms?
5857 {
5858 p->ctxt.in_rescue = $1.in_rescue;
5859 $$ = $2;
5860 void_expr(p, void_stmts(p, $$));
5861 /*% ripper: ensure!($:2) %*/
5862 }
5863 | none
5864 ;
5865
5866literal : numeric
5867 | symbol
5868 ;
5869
5870strings : string
5871 {
5872 if (!$1) {
5873 $$ = NEW_STR(STRING_NEW0(), &@$);
5874 }
5875 else {
5876 $$ = evstr2dstr(p, $1);
5877 }
5878 /*% ripper: $:1 %*/
5879 }
5880 ;
5881
5882string : tCHAR
5883 | string1
5884 | string string1
5885 {
5886 $$ = literal_concat(p, $1, $2, &@$);
5887 /*% ripper: string_concat!($:1, $:2) %*/
5888 }
5889 ;
5890
5891string1 : tSTRING_BEG string_contents tSTRING_END
5892 {
5893 $$ = heredoc_dedent(p, $2);
5894 if ($$) nd_set_loc($$, &@$);
5895 /*% ripper: $:2 %*/
5896 if (p->heredoc_indent > 0) {
5897 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5898 p->heredoc_indent = 0;
5899 }
5900 /*% ripper: string_literal!($:$) %*/
5901 }
5902 ;
5903
5904xstring : tXSTRING_BEG xstring_contents tSTRING_END
5905 {
5906 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5907 /*% ripper: $:2 %*/
5908 if (p->heredoc_indent > 0) {
5909 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5910 p->heredoc_indent = 0;
5911 }
5912 /*% ripper: xstring_literal!($:$) %*/
5913 }
5914 ;
5915
5916regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5917 {
5918 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5919 /*% ripper: regexp_literal!($:2, $:3) %*/
5920 }
5921 ;
5922
5923words : words(tWORDS_BEG, word_list)
5924 ;
5925
5926word_list : /* none */
5927 {
5928 $$ = 0;
5929 /*% ripper: words_new! %*/
5930 }
5931 | word_list word ' '+
5932 {
5933 $$ = list_append(p, $1, evstr2dstr(p, $2));
5934 /*% ripper: words_add!($:1, $:2) %*/
5935 }
5936 ;
5937
5938word : string_content
5939 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5940 | word string_content
5941 {
5942 $$ = literal_concat(p, $1, $2, &@$);
5943 /*% ripper: word_add!($:1, $:2) %*/
5944 }
5945 ;
5946
5947symbols : words(tSYMBOLS_BEG, symbol_list)
5948 ;
5949
5950symbol_list : /* none */
5951 {
5952 $$ = 0;
5953 /*% ripper: symbols_new! %*/
5954 }
5955 | symbol_list word ' '+
5956 {
5957 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5958 /*% ripper: symbols_add!($:1, $:2) %*/
5959 }
5960 ;
5961
5962qwords : words(tQWORDS_BEG, qword_list)
5963 ;
5964
5965qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5966 ;
5967
5968qword_list : /* none */
5969 {
5970 $$ = 0;
5971 /*% ripper: qwords_new! %*/
5972 }
5973 | qword_list tSTRING_CONTENT ' '+
5974 {
5975 $$ = list_append(p, $1, $2);
5976 /*% ripper: qwords_add!($:1, $:2) %*/
5977 }
5978 ;
5979
5980qsym_list : /* none */
5981 {
5982 $$ = 0;
5983 /*% ripper: qsymbols_new! %*/
5984 }
5985 | qsym_list tSTRING_CONTENT ' '+
5986 {
5987 $$ = symbol_append(p, $1, $2);
5988 /*% ripper: qsymbols_add!($:1, $:2) %*/
5989 }
5990 ;
5991
5992string_contents : /* none */
5993 {
5994 $$ = 0;
5995 /*% ripper: string_content! %*/
5996 }
5997 | string_contents string_content
5998 {
5999 $$ = literal_concat(p, $1, $2, &@$);
6000 /*% ripper: string_add!($:1, $:2) %*/
6001 }
6002 ;
6003
6004xstring_contents: /* none */
6005 {
6006 $$ = 0;
6007 /*% ripper: xstring_new! %*/
6008 }
6009 | xstring_contents string_content
6010 {
6011 $$ = literal_concat(p, $1, $2, &@$);
6012 /*% ripper: xstring_add!($:1, $:2) %*/
6013 }
6014 ;
6015
6016regexp_contents : /* none */
6017 {
6018 $$ = 0;
6019 /*% ripper: regexp_new! %*/
6020 }
6021 | regexp_contents string_content
6022 {
6023 NODE *head = $1, *tail = $2;
6024 if (!head) {
6025 $$ = tail;
6026 }
6027 else if (!tail) {
6028 $$ = head;
6029 }
6030 else {
6031 switch (nd_type(head)) {
6032 case NODE_STR:
6033 head = str2dstr(p, head);
6034 break;
6035 case NODE_DSTR:
6036 break;
6037 default:
6038 head = list_append(p, NEW_DSTR(0, &@$), head);
6039 break;
6040 }
6041 $$ = list_append(p, head, tail);
6042 }
6043 /*% ripper: regexp_add!($:1, $:2) %*/
6044 }
6045 ;
6046
6047string_content : tSTRING_CONTENT[content]
6048 /*% ripper[brace]: $:content %*/
6049 | tSTRING_DVAR[state]
6050 {
6051 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6052 $$ = p->lex.strterm;
6053 p->lex.strterm = 0;
6054 SET_LEX_STATE(EXPR_BEG);
6055 }[strterm]<strterm>
6056 string_dvar[dvar]
6057 {
6058 p->lex.strterm = $strterm;
6059 $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC);
6060 nd_set_line($$, @dvar.end_pos.lineno);
6061 /*% ripper: string_dvar!($:dvar) %*/
6062 }
6063 | tSTRING_DBEG[state]
6064 {
6065 CMDARG_PUSH(0);
6066 COND_PUSH(0);
6067 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6068 $$ = p->lex.strterm;
6069 p->lex.strterm = 0;
6070 SET_LEX_STATE(EXPR_BEG);
6071 }[term]<strterm>
6072 {
6073 $$ = p->lex.brace_nest;
6074 p->lex.brace_nest = 0;
6075 }[brace]<num>
6076 {
6077 $$ = p->lex.lpar_beg;
6078 p->lex.lpar_beg = -1;
6079 }[lpar]<num>
6080 {
6081 $$ = p->heredoc_indent;
6082 p->heredoc_indent = 0;
6083 }[indent]<num>
6084 compstmt(stmts) string_dend
6085 {
6086 COND_POP();
6087 CMDARG_POP();
6088 p->lex.strterm = $term;
6089 SET_LEX_STATE($state);
6090 p->lex.brace_nest = $brace;
6091 p->lex.lpar_beg = $lpar;
6092 p->heredoc_indent = $indent;
6093 p->heredoc_line_indent = -1;
6094 if ($compstmt) nd_unset_fl_newline($compstmt);
6095 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6096 /*% ripper: string_embexpr!($:compstmt) %*/
6097 }
6098 ;
6099
6100string_dend : tSTRING_DEND
6101 | END_OF_INPUT
6102 ;
6103
6104string_dvar : nonlocal_var
6105 {
6106 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6107 /*% ripper: var_ref!($:1) %*/
6108 }
6109 | backref
6110 ;
6111
6112symbol : ssym
6113 | dsym
6114 ;
6115
6116ssym : tSYMBEG sym
6117 {
6118 SET_LEX_STATE(EXPR_END);
6119 VALUE str = rb_id2str($2);
6120 /*
6121 * TODO:
6122 * set_yylval_noname sets invalid id to yylval.
6123 * This branch can be removed once yylval is changed to
6124 * hold lexed string.
6125 */
6126 if (!str) str = STR_NEW0();
6127 $$ = NEW_SYM(str, &@$);
6128 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6129 }
6130 ;
6131
6132sym : fname
6133 | nonlocal_var
6134 ;
6135
6136dsym : tSYMBEG string_contents tSTRING_END
6137 {
6138 SET_LEX_STATE(EXPR_END);
6139 $$ = dsym_node(p, $2, &@$);
6140 /*% ripper: dyna_symbol!($:2) %*/
6141 }
6142 ;
6143
6144numeric : simple_numeric
6145 | tUMINUS_NUM simple_numeric %prec tLOWEST
6146 {
6147 $$ = $2;
6148 negate_lit(p, $$, &@$);
6149 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6150 }
6151 ;
6152
6153simple_numeric : tINTEGER
6154 | tFLOAT
6155 | tRATIONAL
6156 | tIMAGINARY
6157 ;
6158
6159nonlocal_var : tIVAR
6160 | tGVAR
6161 | tCVAR
6162 ;
6163
6164user_variable : ident_or_const
6165 | nonlocal_var
6166 ;
6167
6168keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6169 | keyword_self {$$ = KWD2EID(self, $1);}
6170 | keyword_true {$$ = KWD2EID(true, $1);}
6171 | keyword_false {$$ = KWD2EID(false, $1);}
6172 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6173 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6174 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6175 ;
6176
6177var_ref : user_variable
6178 {
6179 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6180 if (ifdef_ripper(id_is_var(p, $1), false)) {
6181 /*% ripper: var_ref!($:1) %*/
6182 }
6183 else {
6184 /*% ripper: vcall!($:1) %*/
6185 }
6186 }
6187 | keyword_variable
6188 {
6189 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6190 /*% ripper: var_ref!($:1) %*/
6191 }
6192 ;
6193
6194var_lhs : user_or_keyword_variable
6195 {
6196 /*% ripper: var_field!($:1) %*/
6197 $$ = assignable(p, $1, 0, &@$);
6198 }
6199 ;
6200
6201backref : tNTH_REF
6202 | tBACK_REF
6203 ;
6204
6205superclass : '<'
6206 {
6207 SET_LEX_STATE(EXPR_BEG);
6208 p->command_start = TRUE;
6209 }
6210 expr_value term
6211 {
6212 $$ = $3;
6213 /*% ripper: $:3 %*/
6214 }
6215 | none
6216 ;
6217
6218f_opt_paren_args: f_paren_args
6219 | f_empty_arg
6220 {
6221 p->ctxt.in_argdef = 0;
6222 }
6223 ;
6224
6225f_empty_arg : /* none */
6226 {
6227 $$ = new_empty_args_tail(p, &@$);
6228 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6229 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6230 }
6231 ;
6232
6233f_paren_args : '(' f_args rparen
6234 {
6235 $$ = $2;
6236 /*% ripper: paren!($:2) %*/
6237 SET_LEX_STATE(EXPR_BEG);
6238 p->command_start = TRUE;
6239 p->ctxt.in_argdef = 0;
6240 }
6241 ;
6242
6243f_arglist : f_paren_args
6244 | {
6245 $$ = p->ctxt;
6246 p->ctxt.in_kwarg = 1;
6247 p->ctxt.in_argdef = 1;
6248 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6249 }<ctxt>
6250 f_args term
6251 {
6252 p->ctxt.in_kwarg = $1.in_kwarg;
6253 p->ctxt.in_argdef = 0;
6254 $$ = $2;
6255 SET_LEX_STATE(EXPR_BEG);
6256 p->command_start = TRUE;
6257 /*% ripper: $:2 %*/
6258 }
6259 ;
6260
6261args_tail : args_tail_basic(arg_value, opt_comma)
6262 | args_forward
6263 {
6264 add_forwarding_args(p);
6265 $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward);
6266 $$->nd_ainfo.forwarding = 1;
6267 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6268 }
6269 ;
6270
6271largs_tail : args_tail_basic(arg_value, none)
6272 | args_forward
6273 {
6274 yyerror1(&@args_forward, "unexpected ... in lambda argument");
6275 $$ = new_args_tail(p, 0, 0, 0, &@args_forward);
6276 $$->nd_ainfo.forwarding = 1;
6277 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6278 }
6279 ;
6280
6281%rule args-list(value, tail) <node_args>
6282 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6283 {
6284 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6285 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6286 }
6287 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6288 {
6289 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6290 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6291 }
6292 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6293 {
6294 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6295 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6296 }
6297 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6298 {
6299 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6300 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6301 }
6302 | f_arg[pre] ',' f_rest_arg[rest] tail
6303 {
6304 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6305 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6306 }
6307 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6308 {
6309 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6310 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6311 }
6312 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6313 {
6314 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6315 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6316 }
6317 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6318 {
6319 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6320 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6321 }
6322 | f_opt_arg(value)[opt] tail
6323 {
6324 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6325 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6326 }
6327 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6328 {
6329 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6330 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6331 }
6332 | f_rest_arg[rest] tail
6333 {
6334 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6335 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6336 }
6337 | f_rest_arg[rest] ',' f_arg[post] tail
6338 {
6339 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6340 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6341 }
6342 ;
6343
6344%rule tail-only-args(tail) <node_args>
6345 : tail
6346 {
6347 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6348 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6349 }
6350 ;
6351
6352%rule f_args-list(tail, trailing) <node_args>
6353 : args-list(arg_value, opt_args_tail(tail, trailing))
6354 | f_arg[pre] opt_args_tail(tail, trailing)[tail]
6355 {
6356 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6357 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6358 }
6359 | tail-only-args(tail)
6360 | f_empty_arg
6361 ;
6362
6363f_args : f_args-list(args_tail, opt_comma)
6364 ;
6365
6366f_largs : f_args-list(largs_tail, none)
6367 ;
6368
6369args_forward : tBDOT3
6370 {
6371 $$ = idFWD_KWREST;
6372 /*% ripper: args_forward! %*/
6373 }
6374 ;
6375
6376f_bad_arg : tCONSTANT
6377 {
6378 static const char mesg[] = "formal argument cannot be a constant";
6379 /*%%%*/
6380 yyerror1(&@1, mesg);
6381 /*% %*/
6382 $$ = 0;
6383 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6384 }
6385 | tIVAR
6386 {
6387 static const char mesg[] = "formal argument cannot be an instance variable";
6388 /*%%%*/
6389 yyerror1(&@1, mesg);
6390 /*% %*/
6391 $$ = 0;
6392 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6393 }
6394 | tGVAR
6395 {
6396 static const char mesg[] = "formal argument cannot be a global variable";
6397 /*%%%*/
6398 yyerror1(&@1, mesg);
6399 /*% %*/
6400 $$ = 0;
6401 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6402 }
6403 | tCVAR
6404 {
6405 static const char mesg[] = "formal argument cannot be a class variable";
6406 /*%%%*/
6407 yyerror1(&@1, mesg);
6408 /*% %*/
6409 $$ = 0;
6410 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6411 }
6412 ;
6413
6414f_norm_arg : f_bad_arg
6415 | tIDENTIFIER
6416 {
6417 VALUE e = formal_argument_error(p, $$ = $1);
6418 if (e) {
6419 /*% ripper[error]: param_error!(?e, $:1) %*/
6420 }
6421 p->max_numparam = ORDINAL_PARAM;
6422 }
6423 ;
6424
6425f_arg_asgn : f_norm_arg
6426 {
6427 arg_var(p, $1);
6428 $$ = $1;
6429 }
6430 ;
6431
6432f_arg_item : f_arg_asgn
6433 {
6434 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6435 /*% ripper: $:1 %*/
6436 }
6437 | tLPAREN f_margs rparen
6438 {
6439 ID tid = internal_id(p);
6440 YYLTYPE loc;
6441 loc.beg_pos = @2.beg_pos;
6442 loc.end_pos = @2.beg_pos;
6443 arg_var(p, tid);
6444 if (dyna_in_block(p)) {
6445 $2->nd_value = NEW_DVAR(tid, &loc);
6446 }
6447 else {
6448 $2->nd_value = NEW_LVAR(tid, &loc);
6449 }
6450 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6451 $$->nd_next = (NODE *)$2;
6452 /*% ripper: mlhs_paren!($:2) %*/
6453 }
6454 ;
6455
6456f_arg : f_arg_item
6457 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6458 | f_arg ',' f_arg_item
6459 {
6460 $$ = $1;
6461 $$->nd_plen++;
6462 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6463 rb_discard_node(p, (NODE *)$3);
6464 /*% ripper: rb_ary_push($:1, $:3) %*/
6465 }
6466 ;
6467
6468
6469f_label : tLABEL
6470 {
6471 VALUE e = formal_argument_error(p, $$ = $1);
6472 if (e) {
6473 $$ = 0;
6474 /*% ripper[error]: param_error!(?e, $:1) %*/
6475 }
6476 /*
6477 * Workaround for Prism::ParseTest#test_filepath for
6478 * "unparser/corpus/literal/def.txt"
6479 *
6480 * See the discussion on https://github.com/ruby/ruby/pull/9923
6481 */
6482 arg_var(p, ifdef_ripper(0, $1));
6483 /*% ripper: $:1 %*/
6484 p->max_numparam = ORDINAL_PARAM;
6485 p->ctxt.in_argdef = 0;
6486 }
6487 ;
6488
6489kwrest_mark : tPOW
6490 | tDSTAR
6491 ;
6492
6493f_no_kwarg : p_kwnorest
6494 {
6495 /*% ripper: nokw_param!(Qnil) %*/
6496 }
6497 ;
6498
6499f_kwrest : kwrest_mark tIDENTIFIER
6500 {
6501 arg_var(p, shadowing_lvar(p, $2));
6502 $$ = $2;
6503 /*% ripper: kwrest_param!($:2) %*/
6504 }
6505 | kwrest_mark
6506 {
6507 arg_var(p, idFWD_KWREST);
6508 $$ = idFWD_KWREST;
6509 /*% ripper: kwrest_param!(Qnil) %*/
6510 }
6511 ;
6512
6513restarg_mark : '*'
6514 | tSTAR
6515 ;
6516
6517f_rest_arg : restarg_mark tIDENTIFIER
6518 {
6519 arg_var(p, shadowing_lvar(p, $2));
6520 $$ = $2;
6521 /*% ripper: rest_param!($:2) %*/
6522 }
6523 | restarg_mark
6524 {
6525 arg_var(p, idFWD_REST);
6526 $$ = idFWD_REST;
6527 /*% ripper: rest_param!(Qnil) %*/
6528 }
6529 ;
6530
6531blkarg_mark : '&'
6532 | tAMPER
6533 ;
6534
6535f_block_arg : blkarg_mark tIDENTIFIER
6536 {
6537 arg_var(p, shadowing_lvar(p, $2));
6538 $$ = $2;
6539 /*% ripper: blockarg!($:2) %*/
6540 }
6541 | blkarg_mark keyword_nil
6542 {
6543 $$ = idNil;
6544 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6545 }
6546 | blkarg_mark
6547 {
6548 arg_var(p, idFWD_BLOCK);
6549 $$ = idFWD_BLOCK;
6550 /*% ripper: blockarg!(Qnil) %*/
6551 }
6552 ;
6553
6554opt_comma : ','?
6555 {
6556 $$ = 0;
6557 /*% ripper: Qnil %*/
6558 }
6559 ;
6560
6561
6562singleton : value_expr(singleton_expr)
6563 {
6564 NODE *expr = last_expr_node($1);
6565 switch (nd_type(expr)) {
6566 case NODE_STR:
6567 case NODE_DSTR:
6568 case NODE_XSTR:
6569 case NODE_DXSTR:
6570 case NODE_REGX:
6571 case NODE_DREGX:
6572 case NODE_SYM:
6573 case NODE_LINE:
6574 case NODE_FILE:
6575 case NODE_ENCODING:
6576 case NODE_INTEGER:
6577 case NODE_FLOAT:
6578 case NODE_RATIONAL:
6579 case NODE_IMAGINARY:
6580 case NODE_DSYM:
6581 case NODE_LIST:
6582 case NODE_ZLIST:
6583 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6584 break;
6585 default:
6586 break;
6587 }
6588 $$ = $1;
6589 }
6590 ;
6591
6592singleton_expr : var_ref
6593 | '('
6594 {
6595 SET_LEX_STATE(EXPR_BEG);
6596 p->ctxt.in_argdef = 0;
6597 }
6598 expr rparen
6599 {
6600 p->ctxt.in_argdef = 1;
6601 $$ = $3;
6602 /*% ripper: paren!($:3) %*/
6603 }
6604 ;
6605
6606assoc_list : none
6607 | assocs trailer
6608 {
6609 $$ = $1;
6610 /*% ripper: assoclist_from_args!($:1) %*/
6611 }
6612 ;
6613
6614assocs : assoc
6615 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6616 | assocs ',' assoc
6617 {
6618 NODE *assocs = $1;
6619 NODE *tail = $3;
6620 if (!assocs) {
6621 assocs = tail;
6622 }
6623 else if (tail) {
6624 NODE *n = RNODE_LIST(tail)->nd_next;
6625 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6626 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6627 /* DSTAR */
6628 tail = RNODE_HASH(n)->nd_head;
6629 }
6630 if (tail) {
6631 assocs = list_concat(assocs, tail);
6632 }
6633 }
6634 $$ = assocs;
6635 /*% ripper: rb_ary_push($:1, $:3) %*/
6636 }
6637 ;
6638
6639assoc : arg_value tASSOC arg_value
6640 {
6641 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6642 /*% ripper: assoc_new!($:1, $:3) %*/
6643 }
6644 | tLABEL arg_value
6645 {
6646 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6647 /*% ripper: assoc_new!($:1, $:2) %*/
6648 }
6649 | tLABEL
6650 {
6651 NODE *val = gettable(p, $1, &@$);
6652 if (!val) val = NEW_ERROR(&@$);
6653 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6654 /*% ripper: assoc_new!($:1, Qnil) %*/
6655 }
6656 | tSTRING_BEG string_contents tLABEL_END arg_value
6657 {
6658 YYLTYPE loc = code_loc_gen(&@1, &@3);
6659 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6660 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6661 }
6662 | tDSTAR arg_value
6663 {
6664 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6665 /*% ripper: assoc_splat!($:2) %*/
6666 }
6667 | tDSTAR
6668 {
6669 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6670 $$ = list_append(p, NEW_LIST(0, &@$),
6671 NEW_LVAR(idFWD_KWREST, &@$));
6672 /*% ripper: assoc_splat!(Qnil) %*/
6673 }
6674 ;
6675
6676%rule %inline operation : ident_or_const
6677 | tFID
6678 ;
6679
6680operation2 : operation
6681 | op
6682 ;
6683
6684operation3 : tIDENTIFIER
6685 | tFID
6686 | op
6687 ;
6688
6689dot_or_colon : '.'
6690 | tCOLON2
6691 ;
6692
6693call_op : '.'
6694 | tANDDOT
6695 ;
6696
6697call_op2 : call_op
6698 | tCOLON2
6699 ;
6700
6701rparen : '\n'? ')'
6702 ;
6703
6704rbracket : '\n'? ']'
6705 ;
6706
6707rbrace : '\n'? '}'
6708 ;
6709
6710trailer : '\n'?
6711 | ','
6712 ;
6713
6714term : ';'
6715 {
6716 yyerrok;
6717 token_flush(p);
6718 if (p->ctxt.in_defined) {
6719 p->ctxt.has_trailing_semicolon = 1;
6720 }
6721 }
6722 | '\n'
6723 {
6724 @$.end_pos = @$.beg_pos;
6725 token_flush(p);
6726 }
6727 ;
6728
6729terms : term
6730 | terms ';' {yyerrok;}
6731 ;
6732
6733none : /* none */
6734 {
6735 $$ = 0;
6736 /*% ripper: Qnil %*/
6737 }
6738 ;
6739%%
6740# undef p
6741# undef yylex
6742# undef yylval
6743# define yylval (*p->lval)
6744
6745static int regx_options(struct parser_params*);
6746static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6747static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6748static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6749static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6750
6751#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6752
6753# define set_yylval_node(x) { \
6754 YYLTYPE _cur_loc; \
6755 rb_parser_set_location(p, &_cur_loc); \
6756 yylval.node = (x); \
6757 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6758}
6759# define set_yylval_str(x) \
6760do { \
6761 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6762 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6763} while(0)
6764# define set_yylval_num(x) { \
6765 yylval.num = (x); \
6766 set_parser_s_value(x); \
6767}
6768# define set_yylval_id(x) (yylval.id = (x))
6769# define set_yylval_name(x) { \
6770 (yylval.id = (x)); \
6771 set_parser_s_value(ID2SYM(x)); \
6772}
6773# define yylval_id() (yylval.id)
6774
6775#define set_yylval_noname() set_yylval_id(keyword_nil)
6776#define has_delayed_token(p) (p->delayed.token != NULL)
6777
6778#ifndef RIPPER
6779#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6780#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6781
6782static bool
6783parser_has_token(struct parser_params *p)
6784{
6785 const char *const pcur = p->lex.pcur;
6786 const char *const ptok = p->lex.ptok;
6787 if (p->keep_tokens && (pcur < ptok)) {
6788 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6789 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6790 }
6791 return pcur > ptok;
6792}
6793
6794static const char *
6795escaped_char(int c)
6796{
6797 switch (c) {
6798 case '"': return "\\\"";
6799 case '\\': return "\\\\";
6800 case '\0': return "\\0";
6801 case '\n': return "\\n";
6802 case '\r': return "\\r";
6803 case '\t': return "\\t";
6804 case '\f': return "\\f";
6805 case '\013': return "\\v";
6806 case '\010': return "\\b";
6807 case '\007': return "\\a";
6808 case '\033': return "\\e";
6809 case '\x7f': return "\\c?";
6810 }
6811 return NULL;
6812}
6813
6814static rb_parser_string_t *
6815rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6816{
6817 rb_encoding *enc = p->enc;
6818 const char *ptr = str->ptr;
6819 const char *pend = ptr + str->len;
6820 const char *prev = ptr;
6821 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6822 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6823
6824 while (ptr < pend) {
6825 unsigned int c;
6826 const char *cc;
6827 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6828 if (!MBCLEN_CHARFOUND_P(n)) {
6829 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6830 n = rb_enc_mbminlen(enc);
6831 if (pend < ptr + n)
6832 n = (int)(pend - ptr);
6833 while (n--) {
6834 c = ((unsigned char)*ptr >> 4) & 0x0f;
6835 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6836 c = *ptr & 0x0f;
6837 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6838 parser_str_cat(result, charbuf, 4);
6839 prev = ++ptr;
6840 }
6841 continue;
6842 }
6843 n = MBCLEN_CHARFOUND_LEN(n);
6844 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6845 ptr += n;
6846 cc = escaped_char(c);
6847 if (cc) {
6848 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6849 parser_str_cat_cstr(result, cc);
6850 prev = ptr;
6851 }
6852 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6853 }
6854 else {
6855 if (ptr - n > prev) {
6856 parser_str_cat(result, prev, ptr - n - prev);
6857 prev = ptr - n;
6858 }
6859 parser_str_cat(result, prev, ptr - prev);
6860 prev = ptr;
6861 }
6862 }
6863 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6864
6865 return result;
6866}
6867
6868static void
6869parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6870{
6871 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6872 token->id = p->token_id;
6873 token->type_name = parser_token2char(p, t);
6874 token->str = str;
6875 token->loc.beg_pos = p->yylloc->beg_pos;
6876 token->loc.end_pos = p->yylloc->end_pos;
6877 rb_parser_ary_push_ast_token(p, p->tokens, token);
6878 p->token_id++;
6879
6880 if (p->debug) {
6881 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6882 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6883 line, token->id, token->type_name, str_escaped->ptr,
6884 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6885 token->loc.end_pos.lineno, token->loc.end_pos.column);
6886 rb_parser_string_free(p, str_escaped);
6887 }
6888}
6889
6890static void
6891parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6892{
6893 debug_token_line(p, "parser_dispatch_scan_event", line);
6894
6895 if (!parser_has_token(p)) return;
6896
6897 RUBY_SET_YYLLOC(*p->yylloc);
6898
6899 if (p->keep_tokens) {
6900 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6901 parser_append_tokens(p, str, t, line);
6902 }
6903
6904 token_flush(p);
6905}
6906
6907#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6908static void
6909parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6910{
6911 debug_token_line(p, "parser_dispatch_delayed_token", line);
6912
6913 if (!has_delayed_token(p)) return;
6914
6915 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6916
6917 if (p->keep_tokens) {
6918 /* p->delayed.token is freed by rb_parser_tokens_free */
6919 parser_append_tokens(p, p->delayed.token, t, line);
6920 }
6921 else {
6922 rb_parser_string_free(p, p->delayed.token);
6923 }
6924
6925 p->delayed.token = NULL;
6926}
6927#else
6928#define literal_flush(p, ptr) ((void)(ptr))
6929
6930static int
6931ripper_has_scan_event(struct parser_params *p)
6932{
6933 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6934 return p->lex.pcur > p->lex.ptok;
6935}
6936
6937static VALUE
6938ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6939{
6940 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6941 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6942 RUBY_SET_YYLLOC(*p->yylloc);
6943 token_flush(p);
6944 return rval;
6945}
6946
6947static void
6948ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6949{
6950 if (!ripper_has_scan_event(p)) return;
6951
6952 set_parser_s_value(ripper_scan_event_val(p, t));
6953}
6954#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6955
6956static void
6957ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6958{
6959 /* save and adjust the location to delayed token for callbacks */
6960 int saved_line = p->ruby_sourceline;
6961 const char *saved_tokp = p->lex.ptok;
6962 VALUE s_value, str;
6963
6964 if (!has_delayed_token(p)) return;
6965 p->ruby_sourceline = p->delayed.beg_line;
6966 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6967 str = rb_str_new_mutable_parser_string(p->delayed.token);
6968 rb_parser_string_free(p, p->delayed.token);
6969 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6970 set_parser_s_value(s_value);
6971 p->delayed.token = NULL;
6972 p->ruby_sourceline = saved_line;
6973 p->lex.ptok = saved_tokp;
6974}
6975#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6976#endif /* RIPPER */
6977
6978static inline int
6979is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6980{
6981 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6982}
6983
6984static inline bool
6985peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
6986{
6987 const char *ptr = p->lex.pcur + at;
6988 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
6989 if (memcmp(ptr, str, len)) return false;
6990 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
6991 switch (ptr[len]) {
6992 case '!': case '?': return false;
6993 }
6994 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
6995}
6996
6997static inline int
6998parser_is_identchar(struct parser_params *p)
6999{
7000 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7001}
7002
7003static inline int
7004parser_isascii(struct parser_params *p)
7005{
7006 return ISASCII(*(p->lex.pcur-1));
7007}
7008
7009static void
7010token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7011{
7012 int column = 1, nonspc = 0, i;
7013 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7014 if (*ptr == '\t') {
7015 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7016 }
7017 column++;
7018 if (*ptr != ' ' && *ptr != '\t') {
7019 nonspc = 1;
7020 }
7021 }
7022
7023 ptinfo->beg = loc->beg_pos;
7024 ptinfo->indent = column;
7025 ptinfo->nonspc = nonspc;
7026}
7027
7028static void
7029token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7030{
7031 token_info *ptinfo;
7032
7033 if (!p->token_info_enabled) return;
7034 ptinfo = ALLOC(token_info);
7035 ptinfo->token = token;
7036 ptinfo->next = p->token_info;
7037 token_info_setup(ptinfo, p->lex.pbeg, loc);
7038
7039 p->token_info = ptinfo;
7040}
7041
7042static void
7043token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7044{
7045 token_info *ptinfo_beg = p->token_info;
7046
7047 if (!ptinfo_beg) return;
7048
7049 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7050 token_info_warn(p, token, ptinfo_beg, 1, loc);
7051
7052 p->token_info = ptinfo_beg->next;
7053 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7054}
7055
7056static void
7057token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7058{
7059 token_info *ptinfo_beg = p->token_info;
7060
7061 if (!ptinfo_beg) return;
7062 p->token_info = ptinfo_beg->next;
7063
7064 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7065 ptinfo_beg->beg.column != beg_pos.column ||
7066 strcmp(ptinfo_beg->token, token)) {
7067 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7068 beg_pos.lineno, beg_pos.column, token,
7069 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7070 ptinfo_beg->token);
7071 }
7072
7073 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7074}
7075
7076static void
7077token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7078{
7079 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7080 if (!p->token_info_enabled) return;
7081 if (!ptinfo_beg) return;
7082 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7083 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7084 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7085 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7086 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7087 rb_warn3L(ptinfo_end->beg.lineno,
7088 "mismatched indentations at '%s' with '%s' at %d",
7089 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7090}
7091
7092static int
7093parser_precise_mbclen(struct parser_params *p, const char *ptr)
7094{
7095 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7096 if (!MBCLEN_CHARFOUND_P(len)) {
7097 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7098 return -1;
7099 }
7100 return len;
7101}
7102
7103#ifndef RIPPER
7104static inline void
7105parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7106{
7107 rb_parser_string_t *str;
7108 int lineno = p->ruby_sourceline;
7109 if (!yylloc) {
7110 return;
7111 }
7112 else if (yylloc->beg_pos.lineno == lineno) {
7113 str = p->lex.lastline;
7114 }
7115 else {
7116 return;
7117 }
7118 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7119}
7120
7121static int
7122parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7123{
7124#if 0
7125 YYLTYPE current;
7126
7127 if (!yylloc) {
7128 yylloc = RUBY_SET_YYLLOC(current);
7129 }
7130 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7131 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7132 yylloc = 0;
7133 }
7134#endif
7135 parser_compile_error(p, yylloc, "%s", msg);
7136 parser_show_error_line(p, yylloc);
7137 return 0;
7138}
7139
7140static int
7141parser_yyerror0(struct parser_params *p, const char *msg)
7142{
7143 YYLTYPE current;
7144 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7145}
7146
7147void
7148ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7149{
7150 VALUE mesg;
7151 const int max_line_margin = 30;
7152 const char *ptr, *ptr_end, *pt, *pb;
7153 const char *pre = "", *post = "", *pend;
7154 const char *code = "", *caret = "";
7155 const char *lim;
7156 const char *const pbeg = PARSER_STRING_PTR(str);
7157 char *buf;
7158 long len;
7159 int i;
7160
7161 if (!yylloc) return;
7162 pend = rb_parser_string_end(str);
7163 if (pend > pbeg && pend[-1] == '\n') {
7164 if (--pend > pbeg && pend[-1] == '\r') --pend;
7165 }
7166
7167 pt = pend;
7168 if (lineno == yylloc->end_pos.lineno &&
7169 (pend - pbeg) > yylloc->end_pos.column) {
7170 pt = pbeg + yylloc->end_pos.column;
7171 }
7172
7173 ptr = ptr_end = pt;
7174 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7175 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7176
7177 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7178 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7179
7180 len = ptr_end - ptr;
7181 if (len > 4) {
7182 if (ptr > pbeg) {
7183 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7184 if (ptr > pbeg) pre = "...";
7185 }
7186 if (ptr_end < pend) {
7187 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7188 if (ptr_end < pend) post = "...";
7189 }
7190 }
7191 pb = pbeg;
7192 if (lineno == yylloc->beg_pos.lineno) {
7193 pb += yylloc->beg_pos.column;
7194 if (pb > pt) pb = pt;
7195 }
7196 if (pb < ptr) pb = ptr;
7197 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7198 return;
7199 }
7200 if (RTEST(errbuf)) {
7201 mesg = rb_attr_get(errbuf, idMesg);
7202 if (char_at_end(p, mesg, '\n') != '\n')
7203 rb_str_cat_cstr(mesg, "\n");
7204 }
7205 else {
7206 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7207 }
7208 if (!errbuf && rb_stderr_tty_p()) {
7209#define CSI_BEGIN "\033["
7210#define CSI_SGR "m"
7211 rb_str_catf(mesg,
7212 CSI_BEGIN""CSI_SGR"%s" /* pre */
7213 CSI_BEGIN"1"CSI_SGR"%.*s"
7214 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7215 CSI_BEGIN";1"CSI_SGR"%.*s"
7216 CSI_BEGIN""CSI_SGR"%s" /* post */
7217 "\n",
7218 pre,
7219 (int)(pb - ptr), ptr,
7220 (int)(pt - pb), pb,
7221 (int)(ptr_end - pt), pt,
7222 post);
7223 }
7224 else {
7225 char *p2;
7226
7227 len = ptr_end - ptr;
7228 lim = pt < pend ? pt : pend;
7229 i = (int)(lim - ptr);
7230 buf = ALLOCA_N(char, i+2);
7231 code = ptr;
7232 caret = p2 = buf;
7233 if (ptr <= pb) {
7234 while (ptr < pb) {
7235 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7236 }
7237 *p2++ = '^';
7238 ptr++;
7239 }
7240 if (lim > ptr) {
7241 memset(p2, '~', (lim - ptr));
7242 p2 += (lim - ptr);
7243 }
7244 *p2 = '\0';
7245 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7246 pre, (int)len, code, post,
7247 pre, caret);
7248 }
7249 if (!errbuf) rb_write_error_str(mesg);
7250}
7251#else
7252
7253static int
7254parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7255{
7256 const char *pcur = 0, *ptok = 0;
7257 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7258 p->ruby_sourceline == yylloc->end_pos.lineno) {
7259 pcur = p->lex.pcur;
7260 ptok = p->lex.ptok;
7261 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7262 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7263 }
7264 parser_yyerror0(p, msg);
7265 if (pcur) {
7266 p->lex.ptok = ptok;
7267 p->lex.pcur = pcur;
7268 }
7269 return 0;
7270}
7271
7272static int
7273parser_yyerror0(struct parser_params *p, const char *msg)
7274{
7275 dispatch1(parse_error, STR_NEW2(msg));
7276 ripper_error(p);
7277 return 0;
7278}
7279
7280static inline void
7281parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7282{
7283}
7284#endif /* !RIPPER */
7285
7286static int
7287vtable_size(const struct vtable *tbl)
7288{
7289 if (!DVARS_TERMINAL_P(tbl)) {
7290 return tbl->pos;
7291 }
7292 else {
7293 return 0;
7294 }
7295}
7296
7297static struct vtable *
7298vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7299{
7300 struct vtable *tbl = ALLOC(struct vtable);
7301 tbl->pos = 0;
7302 tbl->capa = 8;
7303 tbl->tbl = ALLOC_N(ID, tbl->capa);
7304 tbl->prev = prev;
7305#ifndef RIPPER
7306 if (p->debug) {
7307 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7308 }
7309#endif
7310 return tbl;
7311}
7312#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7313
7314static void
7315vtable_free_gen(struct parser_params *p, int line, const char *name,
7316 struct vtable *tbl)
7317{
7318#ifndef RIPPER
7319 if (p->debug) {
7320 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7321 }
7322#endif
7323 if (!DVARS_TERMINAL_P(tbl)) {
7324 if (tbl->tbl) {
7325 ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID));
7326 }
7327 ruby_xfree_sized(tbl, sizeof(*tbl));
7328 }
7329}
7330#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7331
7332static void
7333vtable_add_gen(struct parser_params *p, int line, const char *name,
7334 struct vtable *tbl, ID id)
7335{
7336#ifndef RIPPER
7337 if (p->debug) {
7338 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7339 line, name, (void *)tbl, rb_id2name(id));
7340 }
7341#endif
7342 if (DVARS_TERMINAL_P(tbl)) {
7343 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7344 return;
7345 }
7346 if (tbl->pos == tbl->capa) {
7347 tbl->capa = tbl->capa * 2;
7348 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7349 }
7350 tbl->tbl[tbl->pos++] = id;
7351}
7352#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7353
7354static void
7355vtable_pop_gen(struct parser_params *p, int line, const char *name,
7356 struct vtable *tbl, int n)
7357{
7358 if (p->debug) {
7359 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7360 line, name, (void *)tbl, n);
7361 }
7362 if (tbl->pos < n) {
7363 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7364 return;
7365 }
7366 tbl->pos -= n;
7367}
7368#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7369
7370static int
7371vtable_included(const struct vtable * tbl, ID id)
7372{
7373 int i;
7374
7375 if (!DVARS_TERMINAL_P(tbl)) {
7376 for (i = 0; i < tbl->pos; i++) {
7377 if (tbl->tbl[i] == id) {
7378 return i+1;
7379 }
7380 }
7381 }
7382 return 0;
7383}
7384
7385static void parser_prepare(struct parser_params *p);
7386
7387static int
7388e_option_supplied(struct parser_params *p)
7389{
7390 return strcmp(p->ruby_sourcefile, "-e") == 0;
7391}
7392
7393#ifndef RIPPER
7394static NODE *parser_append_options(struct parser_params *p, NODE *node);
7395
7396static VALUE
7397yycompile0(VALUE arg)
7398{
7399 int n;
7400 NODE *tree;
7401 struct parser_params *p = (struct parser_params *)arg;
7402 int cov = FALSE;
7403
7404 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7405 cov = TRUE;
7406 }
7407
7408 if (p->debug_lines) {
7409 p->ast->body.script_lines = p->debug_lines;
7410 }
7411
7412 parser_prepare(p);
7413#define RUBY_DTRACE_PARSE_HOOK(name) \
7414 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7415 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7416 }
7417 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7418 n = yyparse(p);
7419 RUBY_DTRACE_PARSE_HOOK(END);
7420
7421 p->debug_lines = 0;
7422
7423 xfree(p->lex.strterm);
7424 p->lex.strterm = 0;
7425 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7426 if (n || p->error_p) {
7427 VALUE mesg = p->error_buffer;
7428 if (!mesg) {
7429 mesg = syntax_error_new();
7430 }
7431 if (!p->error_tolerant) {
7432 rb_set_errinfo(mesg);
7433 return FALSE;
7434 }
7435 }
7436 tree = p->eval_tree;
7437 if (!tree) {
7438 tree = NEW_NIL(&NULL_LOC);
7439 }
7440 else {
7441 rb_parser_ary_t *tokens = p->tokens;
7442 NODE *prelude;
7443 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7444 prelude = block_append(p, p->eval_tree_begin, body);
7445 RNODE_SCOPE(tree)->nd_body = prelude;
7446 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7447 p->ast->body.coverage_enabled = cov;
7448 if (p->keep_tokens) {
7449 p->ast->node_buffer->tokens = tokens;
7450 p->tokens = NULL;
7451 }
7452 }
7453 p->ast->body.root = tree;
7454 p->ast->body.line_count = p->line_count;
7455 return TRUE;
7456}
7457
7458static rb_ast_t *
7459yycompile(struct parser_params *p, VALUE fname, int line)
7460{
7461 rb_ast_t *ast;
7462 if (NIL_P(fname)) {
7463 p->ruby_sourcefile_string = Qnil;
7464 p->ruby_sourcefile = "(none)";
7465 }
7466 else {
7467 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7468 p->ruby_sourcefile = StringValueCStr(fname);
7469 }
7470 p->ruby_sourceline = line - 1;
7471
7472 p->lvtbl = NULL;
7473
7474 p->ast = ast = rb_ast_new();
7475 compile_callback(yycompile0, (VALUE)p);
7476 ast->body.source_hash = rb_source_hash_finalize(&p->source_hash);
7477 ast->body.has_source_hash = 1;
7478 p->ast = 0;
7479
7480 while (p->lvtbl) {
7481 local_pop(p);
7482 }
7483
7484 return ast;
7485}
7486#endif /* !RIPPER */
7487
7488static rb_encoding *
7489must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7490{
7491 rb_encoding *enc = rb_parser_str_get_encoding(s);
7492 if (!rb_enc_asciicompat(enc)) {
7493 rb_raise(rb_eArgError, "invalid source encoding");
7494 }
7495 return enc;
7496}
7497
7498static rb_parser_string_t *
7499lex_getline(struct parser_params *p)
7500{
7501 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7502 if (!line) return 0;
7503 p->line_count++;
7504 rb_source_hash_update(&p->source_hash, (const uint8_t *)line->ptr, (size_t)line->len);
7505 string_buffer_append(p, line);
7506 must_be_ascii_compatible(p, line);
7507 return line;
7508}
7509
7510#ifndef RIPPER
7511rb_ast_t*
7512rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7513{
7514 p->lex.gets = gets;
7515 p->lex.input = input;
7516 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7517
7518 return yycompile(p, fname, line);
7519}
7520#endif /* !RIPPER */
7521
7522#define STR_FUNC_ESCAPE 0x01
7523#define STR_FUNC_EXPAND 0x02
7524#define STR_FUNC_REGEXP 0x04
7525#define STR_FUNC_QWORDS 0x08
7526#define STR_FUNC_SYMBOL 0x10
7527#define STR_FUNC_INDENT 0x20
7528#define STR_FUNC_LABEL 0x40
7529#define STR_FUNC_LIST 0x4000
7530#define STR_FUNC_TERM 0x8000
7531
7532enum string_type {
7533 str_label = STR_FUNC_LABEL,
7534 str_squote = (0),
7535 str_dquote = (STR_FUNC_EXPAND),
7536 str_xquote = (STR_FUNC_EXPAND),
7537 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7538 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7539 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7540 str_ssym = (STR_FUNC_SYMBOL),
7541 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7542};
7543
7544static rb_parser_string_t *
7545parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7546{
7547 rb_parser_string_t *pstr;
7548
7549 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7550
7551 if (!(func & STR_FUNC_REGEXP)) {
7552 if (rb_parser_is_ascii_string(p, pstr)) {
7553 }
7554 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7555 /* everything is valid in ASCII-8BIT */
7556 enc = rb_ascii8bit_encoding();
7557 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7558 }
7559 }
7560
7561 return pstr;
7562}
7563
7564static int
7565strterm_is_heredoc(rb_strterm_t *strterm)
7566{
7567 return strterm->heredoc;
7568}
7569
7570static rb_strterm_t *
7571new_strterm(struct parser_params *p, int func, int term, int paren)
7572{
7573 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7574 strterm->u.literal.func = func;
7575 strterm->u.literal.term = term;
7576 strterm->u.literal.paren = paren;
7577 return strterm;
7578}
7579
7580static rb_strterm_t *
7581new_heredoc(struct parser_params *p)
7582{
7583 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7584 strterm->heredoc = true;
7585 return strterm;
7586}
7587
7588#define peek(p,c) peek_n(p, (c), 0)
7589#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7590#define peekc(p) peekc_n(p, 0)
7591#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7592
7593#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7594static void
7595parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7596{
7597 debug_token_line(p, "add_delayed_token", line);
7598
7599 if (tok < end) {
7600 if (has_delayed_token(p)) {
7601 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7602 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7603 int end_col = (next_line ? 0 : p->delayed.end_col);
7604 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7605 dispatch_delayed_token(p, tSTRING_CONTENT);
7606 }
7607 }
7608 if (!has_delayed_token(p)) {
7609 p->delayed.token = rb_parser_string_new(p, 0, 0);
7610 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7611 p->delayed.beg_line = p->ruby_sourceline;
7612 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7613 }
7614 parser_str_cat(p->delayed.token, tok, end - tok);
7615 p->delayed.end_line = p->ruby_sourceline;
7616 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7617 p->lex.ptok = end;
7618 }
7619}
7620
7621static void
7622set_lastline(struct parser_params *p, rb_parser_string_t *str)
7623{
7624 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7625 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7626 p->lex.lastline = str;
7627}
7628
7629static int
7630nextline(struct parser_params *p, int set_encoding)
7631{
7632 rb_parser_string_t *str = p->lex.nextline;
7633 p->lex.nextline = 0;
7634 if (!str) {
7635 if (p->eofp)
7636 return -1;
7637
7638 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7639 goto end_of_input;
7640 }
7641
7642 if (!p->lex.input || !(str = lex_getline(p))) {
7643 end_of_input:
7644 p->eofp = 1;
7645 lex_goto_eol(p);
7646 return -1;
7647 }
7648#ifndef RIPPER
7649 if (p->debug_lines) {
7650 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7651 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7652 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7653 }
7654#endif
7655 p->cr_seen = FALSE;
7656 }
7657 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7658 /* after here-document without terminator */
7659 goto end_of_input;
7660 }
7661 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7662 if (p->heredoc_end > 0) {
7663 p->ruby_sourceline = p->heredoc_end;
7664 p->heredoc_end = 0;
7665 }
7666 p->ruby_sourceline++;
7667 set_lastline(p, str);
7668 token_flush(p);
7669 return 0;
7670}
7671
7672static int
7673parser_cr(struct parser_params *p, int c)
7674{
7675 if (peek(p, '\n')) {
7676 p->lex.pcur++;
7677 c = '\n';
7678 }
7679 return c;
7680}
7681
7682static inline int
7683nextc0(struct parser_params *p, int set_encoding)
7684{
7685 int c;
7686
7687 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7688 if (nextline(p, set_encoding)) return -1;
7689 }
7690 c = (unsigned char)*p->lex.pcur++;
7691 if (UNLIKELY(c == '\r')) {
7692 c = parser_cr(p, c);
7693 }
7694
7695 return c;
7696}
7697#define nextc(p) nextc0(p, TRUE)
7698
7699static void
7700pushback(struct parser_params *p, int c)
7701{
7702 if (c == -1) return;
7703 p->eofp = 0;
7704 p->lex.pcur--;
7705 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7706 p->lex.pcur--;
7707 }
7708}
7709
7710#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7711
7712#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7713#define tok(p) (p)->tokenbuf
7714#define toklen(p) (p)->tokidx
7715
7716static int
7717looking_at_eol_p(struct parser_params *p)
7718{
7719 const char *ptr = p->lex.pcur;
7720 while (!lex_eol_ptr_p(p, ptr)) {
7721 int c = (unsigned char)*ptr++;
7722 int eol = (c == '\n' || c == '#');
7723 if (eol || !ISSPACE(c)) {
7724 return eol;
7725 }
7726 }
7727 return TRUE;
7728}
7729
7730static char*
7731newtok(struct parser_params *p)
7732{
7733 p->tokidx = 0;
7734 if (!p->tokenbuf) {
7735 p->toksiz = 60;
7736 p->tokenbuf = ALLOC_N(char, 60);
7737 }
7738 if (p->toksiz > 4096) {
7739 p->toksiz = 60;
7740 REALLOC_N(p->tokenbuf, char, 60);
7741 }
7742 return p->tokenbuf;
7743}
7744
7745static char *
7746tokspace(struct parser_params *p, int n)
7747{
7748 p->tokidx += n;
7749
7750 if (p->tokidx >= p->toksiz) {
7751 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7752 REALLOC_N(p->tokenbuf, char, p->toksiz);
7753 }
7754 return &p->tokenbuf[p->tokidx-n];
7755}
7756
7757static void
7758tokadd(struct parser_params *p, int c)
7759{
7760 p->tokenbuf[p->tokidx++] = (char)c;
7761 if (p->tokidx >= p->toksiz) {
7762 p->toksiz *= 2;
7763 REALLOC_N(p->tokenbuf, char, p->toksiz);
7764 }
7765}
7766
7767static int
7768tok_hex(struct parser_params *p, size_t *numlen)
7769{
7770 int c;
7771
7772 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7773 if (!*numlen) {
7774 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7775 yyerror0("invalid hex escape");
7776 dispatch_scan_event(p, tSTRING_CONTENT);
7777 return 0;
7778 }
7779 p->lex.pcur += *numlen;
7780 return c;
7781}
7782
7783#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7784
7785static int
7786escaped_control_code(int c)
7787{
7788 int c2 = 0;
7789 switch (c) {
7790 case ' ':
7791 c2 = 's';
7792 break;
7793 case '\n':
7794 c2 = 'n';
7795 break;
7796 case '\t':
7797 c2 = 't';
7798 break;
7799 case '\v':
7800 c2 = 'v';
7801 break;
7802 case '\r':
7803 c2 = 'r';
7804 break;
7805 case '\f':
7806 c2 = 'f';
7807 break;
7808 }
7809 return c2;
7810}
7811
7812#define WARN_SPACE_CHAR(c, prefix) \
7813 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7814
7815static int
7816tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7817 int regexp_literal, const char *begin)
7818{
7819 const int wide = !begin;
7820 size_t numlen;
7821 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7822
7823 p->lex.pcur += numlen;
7824 if (p->lex.strterm == NULL ||
7825 strterm_is_heredoc(p->lex.strterm) ||
7826 (p->lex.strterm->u.literal.func != str_regexp)) {
7827 if (!begin) begin = p->lex.pcur;
7828 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7829 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7830 yyerror0("invalid Unicode escape");
7831 dispatch_scan_event(p, tSTRING_CONTENT);
7832 return wide && numlen > 0;
7833 }
7834 if (codepoint > 0x10ffff) {
7835 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7836 yyerror0("invalid Unicode codepoint (too large)");
7837 dispatch_scan_event(p, tSTRING_CONTENT);
7838 return wide;
7839 }
7840 if ((codepoint & 0xfffff800) == 0xd800) {
7841 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7842 yyerror0("invalid Unicode codepoint");
7843 dispatch_scan_event(p, tSTRING_CONTENT);
7844 return wide;
7845 }
7846 }
7847 if (regexp_literal) {
7848 tokcopy(p, (int)numlen);
7849 }
7850 else if (codepoint >= 0x80) {
7851 rb_encoding *utf8 = rb_utf8_encoding();
7852 if (*encp && utf8 != *encp) {
7853 YYLTYPE loc = RUBY_INIT_YYLLOC();
7854 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7855 parser_show_error_line(p, &loc);
7856 return wide;
7857 }
7858 *encp = utf8;
7859 tokaddmbc(p, codepoint, *encp);
7860 }
7861 else {
7862 tokadd(p, codepoint);
7863 }
7864 return TRUE;
7865}
7866
7867static int tokadd_mbchar(struct parser_params *p, int c);
7868
7869static int
7870tokskip_mbchar(struct parser_params *p)
7871{
7872 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7873 if (len > 0) {
7874 p->lex.pcur += len - 1;
7875 }
7876 return len;
7877}
7878
7879/* return value is for ?\u3042 */
7880static void
7881tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7882 int term, int symbol_literal, int regexp_literal)
7883{
7884 /*
7885 * If `term` is not -1, then we allow multiple codepoints in \u{}
7886 * upto `term` byte, otherwise we're parsing a character literal.
7887 * And then add the codepoints to the current token.
7888 */
7889 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7890
7891 const int open_brace = '{', close_brace = '}';
7892
7893 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7894
7895 if (peek(p, open_brace)) { /* handle \u{...} form */
7896 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7897 /*
7898 * Skip parsing validation code and copy bytes as-is until term or
7899 * closing brace, in order to correctly handle extended regexps where
7900 * invalid unicode escapes are allowed in comments. The regexp parser
7901 * does its own validation and will catch any issues.
7902 */
7903 tokadd(p, open_brace);
7904 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7905 int c = peekc(p);
7906 if (c == close_brace) {
7907 tokadd(p, c);
7908 ++p->lex.pcur;
7909 break;
7910 }
7911 else if (c == term) {
7912 break;
7913 }
7914 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7915 tokadd(p, c);
7916 c = *++p->lex.pcur;
7917 }
7918 tokadd_mbchar(p, c);
7919 }
7920 }
7921 else {
7922 const char *second = NULL;
7923 int c, last = nextc(p);
7924 if (lex_eol_p(p)) goto unterminated;
7925 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7926 while (c != close_brace) {
7927 if (c == term) goto unterminated;
7928 if (second == multiple_codepoints)
7929 second = p->lex.pcur;
7930 if (regexp_literal) tokadd(p, last);
7931 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7932 break;
7933 }
7934 while (ISSPACE(c = peekc(p))) {
7935 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7936 last = c;
7937 }
7938 if (term == -1 && !second)
7939 second = multiple_codepoints;
7940 }
7941
7942 if (c != close_brace) {
7943 unterminated:
7944 flush_string_content(p, rb_utf8_encoding(), 0);
7945 yyerror0("unterminated Unicode escape");
7946 dispatch_scan_event(p, tSTRING_CONTENT);
7947 return;
7948 }
7949 if (second && second != multiple_codepoints) {
7950 const char *pcur = p->lex.pcur;
7951 p->lex.pcur = second;
7952 dispatch_scan_event(p, tSTRING_CONTENT);
7953 token_flush(p);
7954 p->lex.pcur = pcur;
7955 yyerror0(multiple_codepoints);
7956 token_flush(p);
7957 }
7958
7959 if (regexp_literal) tokadd(p, close_brace);
7960 nextc(p);
7961 }
7962 }
7963 else { /* handle \uxxxx form */
7964 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7965 token_flush(p);
7966 return;
7967 }
7968 }
7969}
7970
7971#define ESCAPE_CONTROL 1
7972#define ESCAPE_META 2
7973
7974static int
7975read_escape(struct parser_params *p, int flags, const char *begin)
7976{
7977 int c;
7978 size_t numlen;
7979
7980 switch (c = nextc(p)) {
7981 case '\\': /* Backslash */
7982 return c;
7983
7984 case 'n': /* newline */
7985 return '\n';
7986
7987 case 't': /* horizontal tab */
7988 return '\t';
7989
7990 case 'r': /* carriage-return */
7991 return '\r';
7992
7993 case 'f': /* form-feed */
7994 return '\f';
7995
7996 case 'v': /* vertical tab */
7997 return '\13';
7998
7999 case 'a': /* alarm(bell) */
8000 return '\007';
8001
8002 case 'e': /* escape */
8003 return 033;
8004
8005 case '0': case '1': case '2': case '3': /* octal constant */
8006 case '4': case '5': case '6': case '7':
8007 pushback(p, c);
8008 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8009 p->lex.pcur += numlen;
8010 return c;
8011
8012 case 'x': /* hex constant */
8013 c = tok_hex(p, &numlen);
8014 if (numlen == 0) return 0;
8015 return c;
8016
8017 case 'b': /* backspace */
8018 return '\010';
8019
8020 case 's': /* space */
8021 return ' ';
8022
8023 case 'M':
8024 if (flags & ESCAPE_META) goto eof;
8025 if ((c = nextc(p)) != '-') {
8026 goto eof;
8027 }
8028 if ((c = nextc(p)) == '\\') {
8029 switch (peekc(p)) {
8030 case 'u': case 'U':
8031 nextc(p);
8032 goto eof;
8033 }
8034 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8035 }
8036 else if (c == -1) goto eof;
8037 else if (!ISASCII(c)) {
8038 tokskip_mbchar(p);
8039 goto eof;
8040 }
8041 else {
8042 int c2 = escaped_control_code(c);
8043 if (c2) {
8044 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8045 WARN_SPACE_CHAR(c2, "\\M-");
8046 }
8047 else {
8048 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8049 }
8050 }
8051 else if (ISCNTRL(c)) goto eof;
8052 return ((c & 0xff) | 0x80);
8053 }
8054
8055 case 'C':
8056 if ((c = nextc(p)) != '-') {
8057 goto eof;
8058 }
8059 case 'c':
8060 if (flags & ESCAPE_CONTROL) goto eof;
8061 if ((c = nextc(p))== '\\') {
8062 switch (peekc(p)) {
8063 case 'u': case 'U':
8064 nextc(p);
8065 goto eof;
8066 }
8067 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8068 }
8069 else if (c == '?')
8070 return 0177;
8071 else if (c == -1) goto eof;
8072 else if (!ISASCII(c)) {
8073 tokskip_mbchar(p);
8074 goto eof;
8075 }
8076 else {
8077 int c2 = escaped_control_code(c);
8078 if (c2) {
8079 if (ISCNTRL(c)) {
8080 if (flags & ESCAPE_META) {
8081 WARN_SPACE_CHAR(c2, "\\M-");
8082 }
8083 else {
8084 WARN_SPACE_CHAR(c2, "");
8085 }
8086 }
8087 else {
8088 if (flags & ESCAPE_META) {
8089 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8090 }
8091 else {
8092 WARN_SPACE_CHAR(c2, "\\C-");
8093 }
8094 }
8095 }
8096 else if (ISCNTRL(c)) goto eof;
8097 }
8098 return c & 0x9f;
8099
8100 eof:
8101 case -1:
8102 flush_string_content(p, p->enc, p->lex.pcur - begin);
8103 yyerror0("Invalid escape character syntax");
8104 dispatch_scan_event(p, tSTRING_CONTENT);
8105 return '\0';
8106
8107 default:
8108 if (!ISASCII(c)) {
8109 tokskip_mbchar(p);
8110 goto eof;
8111 }
8112 return c;
8113 }
8114}
8115
8116static void
8117tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8118{
8119 int len = rb_enc_codelen(c, enc);
8120 rb_enc_mbcput(c, tokspace(p, len), enc);
8121}
8122
8123static int
8124tokadd_escape(struct parser_params *p)
8125{
8126 int c;
8127 size_t numlen;
8128 const char *begin = p->lex.pcur;
8129
8130 switch (c = nextc(p)) {
8131 case '\n':
8132 return 0; /* just ignore */
8133
8134 case '0': case '1': case '2': case '3': /* octal constant */
8135 case '4': case '5': case '6': case '7':
8136 {
8137 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8138 if (numlen == 0) goto eof;
8139 p->lex.pcur += numlen;
8140 tokcopy(p, (int)numlen + 1);
8141 }
8142 return 0;
8143
8144 case 'x': /* hex constant */
8145 {
8146 tok_hex(p, &numlen);
8147 if (numlen == 0) return -1;
8148 tokcopy(p, (int)numlen + 2);
8149 }
8150 return 0;
8151
8152 eof:
8153 case -1:
8154 flush_string_content(p, p->enc, p->lex.pcur - begin);
8155 yyerror0("Invalid escape character syntax");
8156 token_flush(p);
8157 return -1;
8158
8159 default:
8160 tokadd(p, '\\');
8161 tokadd(p, c);
8162 }
8163 return 0;
8164}
8165
8166static int
8167char_to_option(int c)
8168{
8169 int val;
8170
8171 switch (c) {
8172 case 'i':
8173 val = RE_ONIG_OPTION_IGNORECASE;
8174 break;
8175 case 'x':
8176 val = RE_ONIG_OPTION_EXTEND;
8177 break;
8178 case 'm':
8179 val = RE_ONIG_OPTION_MULTILINE;
8180 break;
8181 default:
8182 val = 0;
8183 break;
8184 }
8185 return val;
8186}
8187
8188#define ARG_ENCODING_FIXED 16
8189#define ARG_ENCODING_NONE 32
8190#define ENC_ASCII8BIT 1
8191#define ENC_EUC_JP 2
8192#define ENC_Windows_31J 3
8193#define ENC_UTF8 4
8194
8195static int
8196char_to_option_kcode(int c, int *option, int *kcode)
8197{
8198 *option = 0;
8199
8200 switch (c) {
8201 case 'n':
8202 *kcode = ENC_ASCII8BIT;
8203 return (*option = ARG_ENCODING_NONE);
8204 case 'e':
8205 *kcode = ENC_EUC_JP;
8206 break;
8207 case 's':
8208 *kcode = ENC_Windows_31J;
8209 break;
8210 case 'u':
8211 *kcode = ENC_UTF8;
8212 break;
8213 default:
8214 *kcode = -1;
8215 return (*option = char_to_option(c));
8216 }
8217 *option = ARG_ENCODING_FIXED;
8218 return 1;
8219}
8220
8221static int
8222regx_options(struct parser_params *p)
8223{
8224 int kcode = 0;
8225 int kopt = 0;
8226 int options = 0;
8227 int c, opt, kc;
8228
8229 newtok(p);
8230 while (c = nextc(p), ISALPHA(c)) {
8231 if (c == 'o') {
8232 options |= RE_OPTION_ONCE;
8233 }
8234 else if (char_to_option_kcode(c, &opt, &kc)) {
8235 if (kc >= 0) {
8236 if (kc != ENC_ASCII8BIT) kcode = c;
8237 kopt = opt;
8238 }
8239 else {
8240 options |= opt;
8241 }
8242 }
8243 else {
8244 tokadd(p, c);
8245 }
8246 }
8247 options |= kopt;
8248 pushback(p, c);
8249 if (toklen(p)) {
8250 YYLTYPE loc = RUBY_INIT_YYLLOC();
8251 tokfix(p);
8252 compile_error(p, "unknown regexp option%s - %*s",
8253 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8254 parser_show_error_line(p, &loc);
8255 }
8256 return options | RE_OPTION_ENCODING(kcode);
8257}
8258
8259static int
8260tokadd_mbchar(struct parser_params *p, int c)
8261{
8262 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8263 if (len < 0) return -1;
8264 tokadd(p, c);
8265 p->lex.pcur += --len;
8266 if (len > 0) tokcopy(p, len);
8267 return c;
8268}
8269
8270static inline int
8271simple_re_meta(int c)
8272{
8273 switch (c) {
8274 case '$': case '*': case '+': case '.':
8275 case '?': case '^': case '|':
8276 case ')': case ']': case '}': case '>':
8277 return TRUE;
8278 default:
8279 return FALSE;
8280 }
8281}
8282
8283static int
8284parser_update_heredoc_indent(struct parser_params *p, int c)
8285{
8286 if (p->heredoc_line_indent == -1) {
8287 if (c == '\n') p->heredoc_line_indent = 0;
8288 }
8289 else {
8290 if (c == ' ') {
8291 p->heredoc_line_indent++;
8292 return TRUE;
8293 }
8294 else if (c == '\t') {
8295 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8296 p->heredoc_line_indent = w * TAB_WIDTH;
8297 return TRUE;
8298 }
8299 else if (c != '\n') {
8300 if (p->heredoc_indent > p->heredoc_line_indent) {
8301 p->heredoc_indent = p->heredoc_line_indent;
8302 }
8303 p->heredoc_line_indent = -1;
8304 }
8305 else {
8306 /* Whitespace only line has no indentation */
8307 p->heredoc_line_indent = 0;
8308 }
8309 }
8310 return FALSE;
8311}
8312
8313static void
8314parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8315{
8316 YYLTYPE loc = RUBY_INIT_YYLLOC();
8317 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8318 compile_error(p, "%s mixed within %s source", n1, n2);
8319 parser_show_error_line(p, &loc);
8320}
8321
8322static void
8323parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8324{
8325 const char *pos = p->lex.pcur;
8326 p->lex.pcur = beg;
8327 parser_mixed_error(p, enc1, enc2);
8328 p->lex.pcur = pos;
8329}
8330
8331static inline char
8332nibble_char_upper(unsigned int c)
8333{
8334 c &= 0xf;
8335 return c + (c < 10 ? '0' : 'A' - 10);
8336}
8337
8338static int
8339tokadd_string(struct parser_params *p,
8340 int func, int term, int paren, long *nest,
8341 rb_encoding **encp, rb_encoding **enc)
8342{
8343 int c;
8344 bool erred = false;
8345#ifdef RIPPER
8346 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8347 int top_of_line = FALSE;
8348#endif
8349
8350#define mixed_error(enc1, enc2) \
8351 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8352#define mixed_escape(beg, enc1, enc2) \
8353 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8354
8355 while ((c = nextc(p)) != -1) {
8356 if (p->heredoc_indent > 0) {
8357 parser_update_heredoc_indent(p, c);
8358 }
8359#ifdef RIPPER
8360 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8361 pushback(p, c);
8362 break;
8363 }
8364#endif
8365
8366 if (paren && c == paren) {
8367 ++*nest;
8368 }
8369 else if (c == term) {
8370 if (!nest || !*nest) {
8371 pushback(p, c);
8372 break;
8373 }
8374 --*nest;
8375 }
8376 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8377 unsigned char c2 = *p->lex.pcur;
8378 if (c2 == '$' || c2 == '@' || c2 == '{') {
8379 pushback(p, c);
8380 break;
8381 }
8382 }
8383 else if (c == '\\') {
8384 c = nextc(p);
8385 switch (c) {
8386 case '\n':
8387 if (func & STR_FUNC_QWORDS) break;
8388 if (func & STR_FUNC_EXPAND) {
8389 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8390 continue;
8391 if (c == term) {
8392 c = '\\';
8393 goto terminate;
8394 }
8395 }
8396 tokadd(p, '\\');
8397 break;
8398
8399 case '\\':
8400 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8401 break;
8402
8403 case 'u':
8404 if ((func & STR_FUNC_EXPAND) == 0) {
8405 tokadd(p, '\\');
8406 break;
8407 }
8408 tokadd_utf8(p, enc, term,
8409 func & STR_FUNC_SYMBOL,
8410 func & STR_FUNC_REGEXP);
8411 continue;
8412
8413 default:
8414 if (c == -1) return -1;
8415 if (!ISASCII(c)) {
8416 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8417 goto non_ascii;
8418 }
8419 if (func & STR_FUNC_REGEXP) {
8420 switch (c) {
8421 case 'c':
8422 case 'C':
8423 case 'M': {
8424 pushback(p, c);
8425 c = read_escape(p, 0, p->lex.pcur - 1);
8426
8427 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8428 *t++ = '\\';
8429 *t++ = 'x';
8430 *t++ = nibble_char_upper(c >> 4);
8431 *t++ = nibble_char_upper(c);
8432 continue;
8433 }
8434 }
8435
8436 if (c == term && !simple_re_meta(c)) {
8437 tokadd(p, c);
8438 continue;
8439 }
8440 pushback(p, c);
8441 if ((c = tokadd_escape(p)) < 0)
8442 return -1;
8443 if (*enc && *enc != *encp) {
8444 mixed_escape(p->lex.ptok+2, *enc, *encp);
8445 }
8446 continue;
8447 }
8448 else if (func & STR_FUNC_EXPAND) {
8449 pushback(p, c);
8450 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8451 c = read_escape(p, 0, p->lex.pcur - 1);
8452 }
8453 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8454 /* ignore backslashed spaces in %w */
8455 }
8456 else if (c != term && !(paren && c == paren)) {
8457 tokadd(p, '\\');
8458 pushback(p, c);
8459 continue;
8460 }
8461 }
8462 }
8463 else if (!parser_isascii(p)) {
8464 non_ascii:
8465 if (!*enc) {
8466 *enc = *encp;
8467 }
8468 else if (*enc != *encp) {
8469 mixed_error(*enc, *encp);
8470 continue;
8471 }
8472 if (tokadd_mbchar(p, c) == -1) return -1;
8473 continue;
8474 }
8475 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8476 pushback(p, c);
8477 break;
8478 }
8479 if (c & 0x80) {
8480 if (!*enc) {
8481 *enc = *encp;
8482 }
8483 else if (*enc != *encp) {
8484 mixed_error(*enc, *encp);
8485 continue;
8486 }
8487 }
8488 tokadd(p, c);
8489#ifdef RIPPER
8490 top_of_line = (c == '\n');
8491#endif
8492 }
8493 terminate:
8494 if (*enc) *encp = *enc;
8495 return c;
8496}
8497
8498#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8499
8500static void
8501flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8502{
8503 p->lex.pcur -= back;
8504 if (has_delayed_token(p)) {
8505 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8506 if (len > 0) {
8507 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8508 p->delayed.end_line = p->ruby_sourceline;
8509 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8510 }
8511 dispatch_delayed_token(p, tSTRING_CONTENT);
8512 p->lex.ptok = p->lex.pcur;
8513 }
8514 dispatch_scan_event(p, tSTRING_CONTENT);
8515 p->lex.pcur += back;
8516}
8517
8518/* this can be shared with ripper, since it's independent from struct
8519 * parser_params. */
8520#ifndef RIPPER
8521#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8522#define SPECIAL_PUNCT(idx) ( \
8523 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8524 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8525 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8526 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8527 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8528 BIT('0', idx))
8529const uint_least32_t ruby_global_name_punct_bits[] = {
8530 SPECIAL_PUNCT(0),
8531 SPECIAL_PUNCT(1),
8532 SPECIAL_PUNCT(2),
8533};
8534#undef BIT
8535#undef SPECIAL_PUNCT
8536#endif
8537
8538static enum yytokentype
8539parser_peek_variable_name(struct parser_params *p)
8540{
8541 int c;
8542 const char *ptr = p->lex.pcur;
8543
8544 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8545 c = *ptr++;
8546 switch (c) {
8547 case '$':
8548 if ((c = *ptr) == '-') {
8549 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8550 c = *ptr;
8551 }
8552 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8553 return tSTRING_DVAR;
8554 }
8555 break;
8556 case '@':
8557 if ((c = *ptr) == '@') {
8558 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8559 c = *ptr;
8560 }
8561 break;
8562 case '{':
8563 p->lex.pcur = ptr;
8564 p->command_start = TRUE;
8565 yylval.state = p->lex.state;
8566 return tSTRING_DBEG;
8567 default:
8568 return 0;
8569 }
8570 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8571 return tSTRING_DVAR;
8572 return 0;
8573}
8574
8575#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8576#define IS_END() IS_lex_state(EXPR_END_ANY)
8577#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8578#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8579#define IS_LABEL_POSSIBLE() (\
8580 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8581 IS_ARG())
8582#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8583#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8584
8585static inline enum yytokentype
8586parser_string_term(struct parser_params *p, int func)
8587{
8588 xfree(p->lex.strterm);
8589 p->lex.strterm = 0;
8590 if (func & STR_FUNC_REGEXP) {
8591 set_yylval_num(regx_options(p));
8592 dispatch_scan_event(p, tREGEXP_END);
8593 SET_LEX_STATE(EXPR_END);
8594 return tREGEXP_END;
8595 }
8596 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8597 nextc(p);
8598 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8599 return tLABEL_END;
8600 }
8601 SET_LEX_STATE(EXPR_END);
8602 return tSTRING_END;
8603}
8604
8605static enum yytokentype
8606parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8607{
8608 int func = quote->func;
8609 int term = quote->term;
8610 int paren = quote->paren;
8611 int c, space = 0;
8612 rb_encoding *enc = p->enc;
8613 rb_encoding *base_enc = 0;
8614 rb_parser_string_t *lit;
8615
8616 if (func & STR_FUNC_TERM) {
8617 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8618 SET_LEX_STATE(EXPR_END);
8619 xfree(p->lex.strterm);
8620 p->lex.strterm = 0;
8621 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8622 }
8623 c = nextc(p);
8624 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8625 while (c != '\n' && ISSPACE(c = nextc(p)));
8626 space = 1;
8627 }
8628 if (func & STR_FUNC_LIST) {
8629 quote->func &= ~STR_FUNC_LIST;
8630 space = 1;
8631 }
8632 if (c == term && !quote->nest) {
8633 if (func & STR_FUNC_QWORDS) {
8634 quote->func |= STR_FUNC_TERM;
8635 pushback(p, c); /* dispatch the term at tSTRING_END */
8636 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8637 return ' ';
8638 }
8639 return parser_string_term(p, func);
8640 }
8641 if (space) {
8642 if (!ISSPACE(c)) pushback(p, c);
8643 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8644 return ' ';
8645 }
8646 newtok(p);
8647 if ((func & STR_FUNC_EXPAND) && c == '#') {
8648 enum yytokentype t = parser_peek_variable_name(p);
8649 if (t) return t;
8650 tokadd(p, '#');
8651 c = nextc(p);
8652 }
8653 pushback(p, c);
8654 if (tokadd_string(p, func, term, paren, &quote->nest,
8655 &enc, &base_enc) == -1) {
8656 if (p->eofp) {
8657#ifndef RIPPER
8658# define unterminated_literal(mesg) yyerror0(mesg)
8659#else
8660# define unterminated_literal(mesg) compile_error(p, mesg)
8661#endif
8662 literal_flush(p, p->lex.pcur);
8663 if (func & STR_FUNC_QWORDS) {
8664 /* no content to add, bailing out here */
8665 unterminated_literal("unterminated list meets end of file");
8666 xfree(p->lex.strterm);
8667 p->lex.strterm = 0;
8668 return tSTRING_END;
8669 }
8670 if (func & STR_FUNC_REGEXP) {
8671 unterminated_literal("unterminated regexp meets end of file");
8672 }
8673 else {
8674 unterminated_literal("unterminated string meets end of file");
8675 }
8676 quote->func |= STR_FUNC_TERM;
8677 }
8678 }
8679
8680 tokfix(p);
8681 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8682 set_yylval_str(lit);
8683 flush_string_content(p, enc, 0);
8684
8685 return tSTRING_CONTENT;
8686}
8687
8688static enum yytokentype
8689heredoc_identifier(struct parser_params *p)
8690{
8691 /*
8692 * term_len is length of `<<"END"` except `END`,
8693 * in this case term_len is 4 (<, <, " and ").
8694 */
8695 long len, offset = p->lex.pcur - p->lex.pbeg;
8696 int c = nextc(p), term, func = 0, quote = 0;
8697 enum yytokentype token = tSTRING_BEG;
8698 int indent = 0;
8699
8700 if (c == '-') {
8701 c = nextc(p);
8702 func = STR_FUNC_INDENT;
8703 offset++;
8704 }
8705 else if (c == '~') {
8706 c = nextc(p);
8707 func = STR_FUNC_INDENT;
8708 offset++;
8709 indent = INT_MAX;
8710 }
8711 switch (c) {
8712 case '\'':
8713 func |= str_squote; goto quoted;
8714 case '"':
8715 func |= str_dquote; goto quoted;
8716 case '`':
8717 token = tXSTRING_BEG;
8718 func |= str_xquote; goto quoted;
8719
8720 quoted:
8721 quote++;
8722 offset++;
8723 term = c;
8724 len = 0;
8725 while ((c = nextc(p)) != term) {
8726 if (c == -1 || c == '\r' || c == '\n') {
8727 yyerror0("unterminated here document identifier");
8728 return -1;
8729 }
8730 }
8731 break;
8732
8733 default:
8734 if (!parser_is_identchar(p)) {
8735 pushback(p, c);
8736 if (func & STR_FUNC_INDENT) {
8737 pushback(p, indent > 0 ? '~' : '-');
8738 }
8739 return 0;
8740 }
8741 func |= str_dquote;
8742 do {
8743 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8744 if (n < 0) return 0;
8745 p->lex.pcur += --n;
8746 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8747 pushback(p, c);
8748 break;
8749 }
8750
8751 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8752 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8753 yyerror0("too long here document identifier");
8754 dispatch_scan_event(p, tHEREDOC_BEG);
8755 lex_goto_eol(p);
8756
8757 p->lex.strterm = new_heredoc(p);
8758 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8759 here->offset = offset;
8760 here->sourceline = p->ruby_sourceline;
8761 here->length = (unsigned)len;
8762 here->quote = quote;
8763 here->func = func;
8764 here->lastline = p->lex.lastline;
8765
8766 token_flush(p);
8767 p->heredoc_indent = indent;
8768 p->heredoc_line_indent = 0;
8769 return token;
8770}
8771
8772static void
8773heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8774{
8775 rb_parser_string_t *line;
8776 rb_strterm_t *term = p->lex.strterm;
8777
8778 p->lex.strterm = 0;
8779 line = here->lastline;
8780 p->lex.lastline = line;
8781 p->lex.pbeg = PARSER_STRING_PTR(line);
8782 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8783 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8784 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8785 p->heredoc_end = p->ruby_sourceline;
8786 p->ruby_sourceline = (int)here->sourceline;
8787 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8788 p->eofp = 0;
8789 xfree(term);
8790}
8791
8792static int
8793dedent_string_column(const char *str, long len, int width)
8794{
8795 int i, col = 0;
8796
8797 for (i = 0; i < len && col < width; i++) {
8798 if (str[i] == ' ') {
8799 col++;
8800 }
8801 else if (str[i] == '\t') {
8802 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8803 if (n > width) break;
8804 col = n;
8805 }
8806 else {
8807 break;
8808 }
8809 }
8810
8811 return i;
8812}
8813
8814static int
8815dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8816{
8817 char *str;
8818 long len;
8819 int i;
8820
8821 len = PARSER_STRING_LEN(string);
8822 str = PARSER_STRING_PTR(string);
8823
8824 i = dedent_string_column(str, len, width);
8825 if (!i) return 0;
8826
8827 rb_parser_str_modify(string);
8828 str = PARSER_STRING_PTR(string);
8829 if (PARSER_STRING_LEN(string) != len)
8830 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8831 MEMMOVE(str, str + i, char, len - i);
8832 rb_parser_str_set_len(p, string, len - i);
8833 return i;
8834}
8835
8836static NODE *
8837heredoc_dedent(struct parser_params *p, NODE *root)
8838{
8839 NODE *node, *str_node, *prev_node;
8840 int indent = p->heredoc_indent;
8841 rb_parser_string_t *prev_lit = 0;
8842
8843 if (indent <= 0) return root;
8844 if (!root) return root;
8845
8846 prev_node = node = str_node = root;
8847 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8848
8849 while (str_node) {
8850 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8851 if (nd_fl_newline(str_node)) {
8852 dedent_string(p, lit, indent);
8853 }
8854 if (!prev_lit) {
8855 prev_lit = lit;
8856 }
8857 else if (!literal_concat0(p, prev_lit, lit)) {
8858 return 0;
8859 }
8860 else {
8861 NODE *end = RNODE_LIST(node)->as.nd_end;
8862 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8863 if (!node) {
8864 if (nd_type_p(prev_node, NODE_DSTR))
8865 nd_set_type(prev_node, NODE_STR);
8866 break;
8867 }
8868 RNODE_LIST(node)->as.nd_end = end;
8869 goto next_str;
8870 }
8871
8872 str_node = 0;
8873 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8874 next_str:
8875 if (!nd_type_p(node, NODE_LIST)) break;
8876 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8877 enum node_type type = nd_type(str_node);
8878 if (type == NODE_STR || type == NODE_DSTR) break;
8879 prev_lit = 0;
8880 str_node = 0;
8881 }
8882 }
8883 }
8884 return root;
8885}
8886
8887static int
8888whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8889{
8890 const char *beg = p->lex.pbeg;
8891 const char *ptr = p->lex.pend;
8892
8893 if (ptr - beg < len) return FALSE;
8894 if (ptr > beg && ptr[-1] == '\n') {
8895 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8896 if (ptr - beg < len) return FALSE;
8897 }
8898 if (strncmp(eos, ptr -= len, len)) return FALSE;
8899 if (indent) {
8900 while (beg < ptr && ISSPACE(*beg)) beg++;
8901 }
8902 return beg == ptr;
8903}
8904
8905static int
8906word_match_p(struct parser_params *p, const char *word, long len)
8907{
8908 if (strncmp(p->lex.pcur, word, len)) return 0;
8909 if (lex_eol_n_p(p, len)) return 1;
8910 int c = (unsigned char)p->lex.pcur[len];
8911 if (ISSPACE(c)) return 1;
8912 switch (c) {
8913 case '\0': case '\004': case '\032': return 1;
8914 }
8915 return 0;
8916}
8917
8918#define NUM_SUFFIX_R (1<<0)
8919#define NUM_SUFFIX_I (1<<1)
8920#define NUM_SUFFIX_ALL 3
8921
8922static int
8923number_literal_suffix(struct parser_params *p, int mask)
8924{
8925 int c, result = 0;
8926 const char *lastp = p->lex.pcur;
8927
8928 while ((c = nextc(p)) != -1) {
8929 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8930 result |= (mask & NUM_SUFFIX_I);
8931 mask &= ~NUM_SUFFIX_I;
8932 /* r after i, rational of complex is disallowed */
8933 mask &= ~NUM_SUFFIX_R;
8934 continue;
8935 }
8936 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8937 result |= (mask & NUM_SUFFIX_R);
8938 mask &= ~NUM_SUFFIX_R;
8939 continue;
8940 }
8941 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8942 p->lex.pcur = lastp;
8943 return 0;
8944 }
8945 pushback(p, c);
8946 break;
8947 }
8948 return result;
8949}
8950
8951static enum yytokentype
8952set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8953{
8954 enum rb_numeric_type numeric_type = integer_literal;
8955
8956 if (type == tFLOAT) {
8957 numeric_type = float_literal;
8958 }
8959
8960 if (suffix & NUM_SUFFIX_R) {
8961 type = tRATIONAL;
8962 numeric_type = rational_literal;
8963 }
8964 if (suffix & NUM_SUFFIX_I) {
8965 type = tIMAGINARY;
8966 }
8967
8968 switch (type) {
8969 case tINTEGER:
8970 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8971 break;
8972 case tFLOAT:
8973 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8974 break;
8975 case tRATIONAL:
8976 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8977 break;
8978 case tIMAGINARY:
8979 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8980 (void)numeric_type; /* for ripper */
8981 break;
8982 default:
8983 rb_bug("unexpected token: %d", type);
8984 }
8985 SET_LEX_STATE(EXPR_END);
8986 return type;
8987}
8988
8989#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8990static void
8991parser_dispatch_heredoc_end(struct parser_params *p, int line)
8992{
8993 if (has_delayed_token(p))
8994 dispatch_delayed_token(p, tSTRING_CONTENT);
8995
8996#ifdef RIPPER
8997 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8998 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8999#else
9000 if (p->keep_tokens) {
9001 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9002 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9003 parser_append_tokens(p, str, tHEREDOC_END, line);
9004 }
9005#endif
9006
9007 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9008 lex_goto_eol(p);
9009 token_flush(p);
9010}
9011
9012static enum yytokentype
9013here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9014{
9015 int c, func, indent = 0;
9016 const char *eos, *ptr, *ptr_end;
9017 long len;
9018 rb_parser_string_t *str = 0;
9019 rb_encoding *enc = p->enc;
9020 rb_encoding *base_enc = 0;
9021 int bol;
9022#ifdef RIPPER
9023 VALUE s_value;
9024#endif
9025
9026 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9027 len = here->length;
9028 indent = (func = here->func) & STR_FUNC_INDENT;
9029
9030 if ((c = nextc(p)) == -1) {
9031 error:
9032#ifdef RIPPER
9033 if (!has_delayed_token(p)) {
9034 dispatch_scan_event(p, tSTRING_CONTENT);
9035 }
9036 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9037 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9038 if (!(func & STR_FUNC_REGEXP)) {
9039 int cr = ENC_CODERANGE_UNKNOWN;
9040 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9041 if (cr != ENC_CODERANGE_7BIT &&
9042 rb_is_usascii_enc(p->enc) &&
9043 enc != rb_utf8_encoding()) {
9044 enc = rb_ascii8bit_encoding();
9045 }
9046 }
9047 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9048 }
9049 dispatch_delayed_token(p, tSTRING_CONTENT);
9050 }
9051 else {
9052 dispatch_delayed_token(p, tSTRING_CONTENT);
9053 dispatch_scan_event(p, tSTRING_CONTENT);
9054 }
9055 lex_goto_eol(p);
9056#endif
9057 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9058 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9059 (int)len, eos);
9060 token_flush(p);
9061 SET_LEX_STATE(EXPR_END);
9062 return tSTRING_END;
9063 }
9064 bol = was_bol(p);
9065 if (!bol) {
9066 /* not beginning of line, cannot be the terminator */
9067 }
9068 else if (p->heredoc_line_indent == -1) {
9069 /* `heredoc_line_indent == -1` means
9070 * - "after an interpolation in the same line", or
9071 * - "in a continuing line"
9072 */
9073 p->heredoc_line_indent = 0;
9074 }
9075 else if (whole_match_p(p, eos, len, indent)) {
9076 dispatch_heredoc_end(p);
9077 restore:
9078 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9079 token_flush(p);
9080 SET_LEX_STATE(EXPR_END);
9081 return tSTRING_END;
9082 }
9083
9084 if (!(func & STR_FUNC_EXPAND)) {
9085 do {
9086 ptr = PARSER_STRING_PTR(p->lex.lastline);
9087 ptr_end = p->lex.pend;
9088 if (ptr_end > ptr) {
9089 switch (ptr_end[-1]) {
9090 case '\n':
9091 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9092 ptr_end++;
9093 break;
9094 }
9095 case '\r':
9096 --ptr_end;
9097 }
9098 }
9099
9100 if (p->heredoc_indent > 0) {
9101 long i = 0;
9102 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9103 i++;
9104 p->heredoc_line_indent = 0;
9105 }
9106
9107 if (str)
9108 parser_str_cat(str, ptr, ptr_end - ptr);
9109 else
9110 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9111 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9112 lex_goto_eol(p);
9113 if (p->heredoc_indent > 0) {
9114 goto flush_str;
9115 }
9116 if (nextc(p) == -1) {
9117 if (str) {
9118 rb_parser_string_free(p, str);
9119 str = 0;
9120 }
9121 goto error;
9122 }
9123 } while (!whole_match_p(p, eos, len, indent));
9124 }
9125 else {
9126 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9127 newtok(p);
9128 if (c == '#') {
9129 enum yytokentype t = parser_peek_variable_name(p);
9130 if (p->heredoc_line_indent != -1) {
9131 if (p->heredoc_indent > p->heredoc_line_indent) {
9132 p->heredoc_indent = p->heredoc_line_indent;
9133 }
9134 p->heredoc_line_indent = -1;
9135 }
9136 if (t) return t;
9137 tokadd(p, '#');
9138 c = nextc(p);
9139 }
9140 do {
9141 pushback(p, c);
9142 enc = p->enc;
9143 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9144 if (p->eofp) goto error;
9145 goto restore;
9146 }
9147 if (c != '\n') {
9148 if (c == '\\') p->heredoc_line_indent = -1;
9149 flush:
9150 str = STR_NEW3(tok(p), toklen(p), enc, func);
9151 flush_str:
9152 set_yylval_str(str);
9153#ifndef RIPPER
9154 if (bol) nd_set_fl_newline(yylval.node);
9155#endif
9156 flush_string_content(p, enc, 0);
9157 return tSTRING_CONTENT;
9158 }
9159 tokadd(p, nextc(p));
9160 if (p->heredoc_indent > 0) {
9161 lex_goto_eol(p);
9162 goto flush;
9163 }
9164 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9165 if ((c = nextc(p)) == -1) goto error;
9166 } while (!whole_match_p(p, eos, len, indent));
9167 str = STR_NEW3(tok(p), toklen(p), enc, func);
9168 }
9169 dispatch_heredoc_end(p);
9170 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9171 token_flush(p);
9172 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9173#ifdef RIPPER
9174 /* Preserve s_value for set_yylval_str */
9175 s_value = p->s_value;
9176#endif
9177 set_yylval_str(str);
9178#ifdef RIPPER
9179 set_parser_s_value(s_value);
9180#endif
9181
9182#ifndef RIPPER
9183 if (bol) nd_set_fl_newline(yylval.node);
9184#endif
9185 return tSTRING_CONTENT;
9186}
9187
9188#include "lex.c"
9189
9190static int
9191arg_ambiguous(struct parser_params *p, char c)
9192{
9193#ifndef RIPPER
9194 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9195#else
9196 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9197#endif
9198 return TRUE;
9199}
9200
9201/* returns true value if formal argument error;
9202 * Qtrue, or error message if ripper */
9203static VALUE
9204formal_argument_error(struct parser_params *p, ID id)
9205{
9206 switch (id_type(id)) {
9207 case ID_LOCAL:
9208 break;
9209#ifndef RIPPER
9210# define ERR(mesg) (yyerror0(mesg), Qtrue)
9211#else
9212# define ERR(mesg) WARN_S(mesg)
9213#endif
9214 case ID_CONST:
9215 return ERR("formal argument cannot be a constant");
9216 case ID_INSTANCE:
9217 return ERR("formal argument cannot be an instance variable");
9218 case ID_GLOBAL:
9219 return ERR("formal argument cannot be a global variable");
9220 case ID_CLASS:
9221 return ERR("formal argument cannot be a class variable");
9222 default:
9223 return ERR("formal argument must be local variable");
9224#undef ERR
9225 }
9226 shadowing_lvar(p, id);
9227
9228 return Qfalse;
9229}
9230
9231static int
9232lvar_defined(struct parser_params *p, ID id)
9233{
9234 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9235}
9236
9237/* emacsen -*- hack */
9238static long
9239parser_encode_length(struct parser_params *p, const char *name, long len)
9240{
9241 long nlen;
9242
9243 if (len > 5 && name[nlen = len - 5] == '-') {
9244 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9245 return nlen;
9246 }
9247 if (len > 4 && name[nlen = len - 4] == '-') {
9248 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9249 return nlen;
9250 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9251 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9252 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9253 return nlen;
9254 }
9255 return len;
9256}
9257
9258static void
9259parser_set_encode(struct parser_params *p, const char *name)
9260{
9261 rb_encoding *enc;
9262 VALUE excargs[3];
9263 int idx = 0;
9264
9265 const char *wrong = 0;
9266 switch (*name) {
9267 case 'e': case 'E': wrong = "external"; break;
9268 case 'i': case 'I': wrong = "internal"; break;
9269 case 'f': case 'F': wrong = "filesystem"; break;
9270 case 'l': case 'L': wrong = "locale"; break;
9271 }
9272 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9273 idx = rb_enc_find_index(name);
9274 if (idx < 0) {
9275 unknown:
9276 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9277 error:
9278 excargs[0] = rb_eArgError;
9279 excargs[2] = rb_make_backtrace();
9280 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9281 VALUE exc = rb_make_exception(3, excargs);
9282 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9283
9284 rb_ast_free(p->ast);
9285 p->ast = NULL;
9286
9287 rb_exc_raise(exc);
9288 }
9289 enc = rb_enc_from_index(idx);
9290 if (!rb_enc_asciicompat(enc)) {
9291 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9292 goto error;
9293 }
9294 p->enc = enc;
9295#ifndef RIPPER
9296 if (p->debug_lines) {
9297 long i;
9298 for (i = 0; i < p->debug_lines->len; i++) {
9299 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9300 }
9301 }
9302#endif
9303}
9304
9305static bool
9306comment_at_top(struct parser_params *p)
9307{
9308 if (p->token_seen) return false;
9309 return (p->line_count == (p->has_shebang ? 2 : 1));
9310}
9311
9312typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9313typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9314
9315static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9316
9317static void
9318magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9319{
9320 if (!comment_at_top(p)) {
9321 return;
9322 }
9323 parser_set_encode(p, val);
9324}
9325
9326static int
9327parser_get_bool(struct parser_params *p, const char *name, const char *val)
9328{
9329 switch (*val) {
9330 case 't': case 'T':
9331 if (STRCASECMP(val, "true") == 0) {
9332 return TRUE;
9333 }
9334 break;
9335 case 'f': case 'F':
9336 if (STRCASECMP(val, "false") == 0) {
9337 return FALSE;
9338 }
9339 break;
9340 }
9341 return parser_invalid_pragma_value(p, name, val);
9342}
9343
9344static int
9345parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9346{
9347 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9348 return -1;
9349}
9350
9351static void
9352parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9353{
9354 int b = parser_get_bool(p, name, val);
9355 if (b >= 0) p->token_info_enabled = b;
9356}
9357
9358static void
9359parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9360{
9361 int b;
9362
9363 if (p->token_seen) {
9364 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9365 return;
9366 }
9367
9368 b = parser_get_bool(p, name, val);
9369 if (b < 0) return;
9370
9371 p->frozen_string_literal = b;
9372}
9373
9374static void
9375parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9376{
9377 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9378 if (*s == ' ' || *s == '\t') continue;
9379 if (*s == '#') break;
9380 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9381 return;
9382 }
9383
9384 switch (*val) {
9385 case 'n': case 'N':
9386 if (STRCASECMP(val, "none") == 0) {
9387 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9388 return;
9389 }
9390 break;
9391 case 'l': case 'L':
9392 if (STRCASECMP(val, "literal") == 0) {
9393 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9394 return;
9395 }
9396 break;
9397 case 'e': case 'E':
9398 if (STRCASECMP(val, "experimental_copy") == 0) {
9399 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9400 return;
9401 }
9402 if (STRCASECMP(val, "experimental_everything") == 0) {
9403 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9404 return;
9405 }
9406 break;
9407 }
9408 parser_invalid_pragma_value(p, name, val);
9409}
9410
9411# if WARN_PAST_SCOPE
9412static void
9413parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9414{
9415 int b = parser_get_bool(p, name, val);
9416 if (b >= 0) p->past_scope_enabled = b;
9417}
9418# endif
9419
9420struct magic_comment {
9421 const char *name;
9422 rb_magic_comment_setter_t func;
9423 rb_magic_comment_length_t length;
9424};
9425
9426static const struct magic_comment magic_comments[] = {
9427 {"coding", magic_comment_encoding, parser_encode_length},
9428 {"encoding", magic_comment_encoding, parser_encode_length},
9429 {"frozen_string_literal", parser_set_frozen_string_literal},
9430 {"shareable_constant_value", parser_set_shareable_constant_value},
9431 {"warn_indent", parser_set_token_info},
9432# if WARN_PAST_SCOPE
9433 {"warn_past_scope", parser_set_past_scope},
9434# endif
9435};
9436
9437static const char *
9438magic_comment_marker(const char *str, long len)
9439{
9440 long i = 2;
9441
9442 while (i < len) {
9443 switch (str[i]) {
9444 case '-':
9445 if (str[i-1] == '*' && str[i-2] == '-') {
9446 return str + i + 1;
9447 }
9448 i += 2;
9449 break;
9450 case '*':
9451 if (i + 1 >= len) return 0;
9452 if (str[i+1] != '-') {
9453 i += 4;
9454 }
9455 else if (str[i-1] != '-') {
9456 i += 2;
9457 }
9458 else {
9459 return str + i + 2;
9460 }
9461 break;
9462 default:
9463 i += 3;
9464 break;
9465 }
9466 }
9467 return 0;
9468}
9469
9470static int
9471parser_magic_comment(struct parser_params *p, const char *str, long len)
9472{
9473 int indicator = 0;
9474 VALUE name = 0, val = 0;
9475 const char *beg, *end, *vbeg, *vend;
9476#define str_copy(_s, _p, _n) ((_s) \
9477 ? (void)(rb_str_resize((_s), (_n)), \
9478 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9479 : (void)((_s) = STR_NEW((_p), (_n))))
9480
9481 if (len <= 7) return FALSE;
9482 if (!!(beg = magic_comment_marker(str, len))) {
9483 if (!(end = magic_comment_marker(beg, str + len - beg)))
9484 return FALSE;
9485 indicator = TRUE;
9486 str = beg;
9487 len = end - beg - 3;
9488 }
9489
9490 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9491 while (len > 0) {
9492 const struct magic_comment *mc = magic_comments;
9493 char *s;
9494 int i;
9495 long n = 0;
9496
9497 for (; len > 0 && *str; str++, --len) {
9498 switch (*str) {
9499 case '\'': case '"': case ':': case ';':
9500 continue;
9501 }
9502 if (!ISSPACE(*str)) break;
9503 }
9504 for (beg = str; len > 0; str++, --len) {
9505 switch (*str) {
9506 case '\'': case '"': case ':': case ';':
9507 break;
9508 default:
9509 if (ISSPACE(*str)) break;
9510 continue;
9511 }
9512 break;
9513 }
9514 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9515 if (!len) break;
9516 if (*str != ':') {
9517 if (!indicator) return FALSE;
9518 continue;
9519 }
9520
9521 do str++; while (--len > 0 && ISSPACE(*str));
9522 if (!len) break;
9523 const char *tok_beg = str;
9524 if (*str == '"') {
9525 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9526 if (*str == '\\') {
9527 --len;
9528 ++str;
9529 }
9530 }
9531 vend = str;
9532 if (len) {
9533 --len;
9534 ++str;
9535 }
9536 }
9537 else {
9538 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9539 vend = str;
9540 }
9541 const char *tok_end = str;
9542 if (indicator) {
9543 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9544 }
9545 else {
9546 while (len > 0 && (ISSPACE(*str))) --len, str++;
9547 if (len) return FALSE;
9548 }
9549
9550 n = end - beg;
9551 str_copy(name, beg, n);
9552 s = RSTRING_PTR(name);
9553 for (i = 0; i < n; ++i) {
9554 if (s[i] == '-') s[i] = '_';
9555 }
9556 do {
9557 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9558 n = vend - vbeg;
9559 if (mc->length) {
9560 n = (*mc->length)(p, vbeg, n);
9561 }
9562 str_copy(val, vbeg, n);
9563 p->lex.ptok = tok_beg;
9564 p->lex.pcur = tok_end;
9565 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9566 break;
9567 }
9568 } while (++mc < magic_comments + numberof(magic_comments));
9569#ifdef RIPPER
9570 str_copy(val, vbeg, vend - vbeg);
9571 dispatch2(magic_comment, name, val);
9572#endif
9573 }
9574
9575 return TRUE;
9576}
9577
9578static void
9579set_file_encoding(struct parser_params *p, const char *str, const char *send)
9580{
9581 int sep = 0;
9582 const char *beg = str;
9583 VALUE s;
9584
9585 for (;;) {
9586 if (send - str <= 6) return;
9587 switch (str[6]) {
9588 case 'C': case 'c': str += 6; continue;
9589 case 'O': case 'o': str += 5; continue;
9590 case 'D': case 'd': str += 4; continue;
9591 case 'I': case 'i': str += 3; continue;
9592 case 'N': case 'n': str += 2; continue;
9593 case 'G': case 'g': str += 1; continue;
9594 case '=': case ':':
9595 sep = 1;
9596 str += 6;
9597 break;
9598 default:
9599 str += 6;
9600 if (ISSPACE(*str)) break;
9601 continue;
9602 }
9603 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9604 sep = 0;
9605 }
9606 for (;;) {
9607 do {
9608 if (++str >= send) return;
9609 } while (ISSPACE(*str));
9610 if (sep) break;
9611 if (*str != '=' && *str != ':') return;
9612 sep = 1;
9613 str++;
9614 }
9615 beg = str;
9616 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9617 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9618 p->lex.ptok = beg;
9619 p->lex.pcur = str;
9620 parser_set_encode(p, RSTRING_PTR(s));
9621 rb_str_resize(s, 0);
9622}
9623
9624static void
9625parser_prepare(struct parser_params *p)
9626{
9627 int c = nextc0(p, FALSE);
9628 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9629 switch (c) {
9630 case '#':
9631 if (peek(p, '!')) p->has_shebang = 1;
9632 break;
9633 case 0xef: /* UTF-8 BOM marker */
9634 if (!lex_eol_n_p(p, 2) &&
9635 (unsigned char)p->lex.pcur[0] == 0xbb &&
9636 (unsigned char)p->lex.pcur[1] == 0xbf) {
9637 p->enc = rb_utf8_encoding();
9638 p->lex.pcur += 2;
9639#ifndef RIPPER
9640 if (p->debug_lines) {
9641 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9642 }
9643#endif
9644 p->lex.pbeg = p->lex.pcur;
9645 token_flush(p);
9646 return;
9647 }
9648 break;
9649 case -1: /* end of script. */
9650 return;
9651 }
9652 pushback(p, c);
9653 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9654}
9655
9656#ifndef RIPPER
9657#define ambiguous_operator(tok, op, syn) ( \
9658 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9659 rb_warning0("even though it seems like "syn""))
9660#else
9661#define ambiguous_operator(tok, op, syn) \
9662 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9663#endif
9664#define warn_balanced(tok, op, syn) ((void) \
9665 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9666 space_seen && !ISSPACE(c) && \
9667 (ambiguous_operator(tok, op, syn), 0)), \
9668 (enum yytokentype)(tok))
9669
9670static enum yytokentype
9671no_digits(struct parser_params *p)
9672{
9673 yyerror0("numeric literal without digits");
9674 if (peek(p, '_')) nextc(p);
9675 /* dummy 0, for tUMINUS_NUM at numeric */
9676 return set_number_literal(p, tINTEGER, 0, 10, 0);
9677}
9678
9679static enum yytokentype
9680parse_numeric(struct parser_params *p, int c)
9681{
9682 int is_float, seen_point, seen_e, nondigit;
9683 int suffix;
9684
9685 is_float = seen_point = seen_e = nondigit = 0;
9686 SET_LEX_STATE(EXPR_END);
9687 newtok(p);
9688 if (c == '-' || c == '+') {
9689 tokadd(p, c);
9690 c = nextc(p);
9691 }
9692 if (c == '0') {
9693 int start = toklen(p);
9694 c = nextc(p);
9695 if (c == 'x' || c == 'X') {
9696 /* hexadecimal */
9697 c = nextc(p);
9698 if (c != -1 && ISXDIGIT(c)) {
9699 do {
9700 if (c == '_') {
9701 if (nondigit) break;
9702 nondigit = c;
9703 continue;
9704 }
9705 if (!ISXDIGIT(c)) break;
9706 nondigit = 0;
9707 tokadd(p, c);
9708 } while ((c = nextc(p)) != -1);
9709 }
9710 pushback(p, c);
9711 tokfix(p);
9712 if (toklen(p) == start) {
9713 return no_digits(p);
9714 }
9715 else if (nondigit) goto trailing_uc;
9716 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9717 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9718 }
9719 if (c == 'b' || c == 'B') {
9720 /* binary */
9721 c = nextc(p);
9722 if (c == '0' || c == '1') {
9723 do {
9724 if (c == '_') {
9725 if (nondigit) break;
9726 nondigit = c;
9727 continue;
9728 }
9729 if (c != '0' && c != '1') break;
9730 nondigit = 0;
9731 tokadd(p, c);
9732 } while ((c = nextc(p)) != -1);
9733 }
9734 pushback(p, c);
9735 tokfix(p);
9736 if (toklen(p) == start) {
9737 return no_digits(p);
9738 }
9739 else if (nondigit) goto trailing_uc;
9740 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9741 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9742 }
9743 if (c == 'd' || c == 'D') {
9744 /* decimal */
9745 c = nextc(p);
9746 if (c != -1 && ISDIGIT(c)) {
9747 do {
9748 if (c == '_') {
9749 if (nondigit) break;
9750 nondigit = c;
9751 continue;
9752 }
9753 if (!ISDIGIT(c)) break;
9754 nondigit = 0;
9755 tokadd(p, c);
9756 } while ((c = nextc(p)) != -1);
9757 }
9758 pushback(p, c);
9759 tokfix(p);
9760 if (toklen(p) == start) {
9761 return no_digits(p);
9762 }
9763 else if (nondigit) goto trailing_uc;
9764 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9765 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9766 }
9767 if (c == '_') {
9768 /* 0_0 */
9769 goto octal_number;
9770 }
9771 if (c == 'o' || c == 'O') {
9772 /* prefixed octal */
9773 c = nextc(p);
9774 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9775 tokfix(p);
9776 return no_digits(p);
9777 }
9778 }
9779 if (c >= '0' && c <= '7') {
9780 /* octal */
9781 octal_number:
9782 do {
9783 if (c == '_') {
9784 if (nondigit) break;
9785 nondigit = c;
9786 continue;
9787 }
9788 if (c < '0' || c > '9') break;
9789 if (c > '7') goto invalid_octal;
9790 nondigit = 0;
9791 tokadd(p, c);
9792 } while ((c = nextc(p)) != -1);
9793 if (toklen(p) > start) {
9794 pushback(p, c);
9795 tokfix(p);
9796 if (nondigit) goto trailing_uc;
9797 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9798 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9799 }
9800 if (nondigit) {
9801 pushback(p, c);
9802 goto trailing_uc;
9803 }
9804 }
9805 if (c > '7' && c <= '9') {
9806 invalid_octal:
9807 yyerror0("Invalid octal digit");
9808 }
9809 else if (c == '.' || c == 'e' || c == 'E') {
9810 tokadd(p, '0');
9811 }
9812 else {
9813 pushback(p, c);
9814 tokfix(p);
9815 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9816 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9817 }
9818 }
9819
9820 for (;;) {
9821 switch (c) {
9822 case '0': case '1': case '2': case '3': case '4':
9823 case '5': case '6': case '7': case '8': case '9':
9824 nondigit = 0;
9825 tokadd(p, c);
9826 break;
9827
9828 case '.':
9829 if (nondigit) goto trailing_uc;
9830 if (seen_point || seen_e) {
9831 goto decode_num;
9832 }
9833 else {
9834 int c0 = nextc(p);
9835 if (c0 == -1 || !ISDIGIT(c0)) {
9836 pushback(p, c0);
9837 goto decode_num;
9838 }
9839 c = c0;
9840 }
9841 seen_point = toklen(p);
9842 tokadd(p, '.');
9843 tokadd(p, c);
9844 is_float++;
9845 nondigit = 0;
9846 break;
9847
9848 case 'e':
9849 case 'E':
9850 if (nondigit) {
9851 pushback(p, c);
9852 c = nondigit;
9853 goto decode_num;
9854 }
9855 if (seen_e) {
9856 goto decode_num;
9857 }
9858 nondigit = c;
9859 c = nextc(p);
9860 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9861 pushback(p, c);
9862 c = nondigit;
9863 nondigit = 0;
9864 goto decode_num;
9865 }
9866 tokadd(p, nondigit);
9867 seen_e++;
9868 is_float++;
9869 tokadd(p, c);
9870 nondigit = (c == '-' || c == '+') ? c : 0;
9871 break;
9872
9873 case '_': /* `_' in number just ignored */
9874 if (nondigit) goto decode_num;
9875 nondigit = c;
9876 break;
9877
9878 default:
9879 goto decode_num;
9880 }
9881 c = nextc(p);
9882 }
9883
9884 decode_num:
9885 pushback(p, c);
9886 if (nondigit) {
9887 trailing_uc:
9888 literal_flush(p, p->lex.pcur - 1);
9889 YYLTYPE loc = RUBY_INIT_YYLLOC();
9890 compile_error(p, "trailing '%c' in number", nondigit);
9891 parser_show_error_line(p, &loc);
9892 }
9893 tokfix(p);
9894 if (is_float) {
9895 enum yytokentype type = tFLOAT;
9896
9897 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9898 if (suffix & NUM_SUFFIX_R) {
9899 type = tRATIONAL;
9900 }
9901 else {
9902 errno = 0;
9903 strtod(tok(p), 0);
9904 if (errno == ERANGE) {
9905 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9906 errno = 0;
9907 }
9908 }
9909 return set_number_literal(p, type, suffix, 0, seen_point);
9910 }
9911 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9912 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9913}
9914
9915static enum yytokentype
9916parse_qmark(struct parser_params *p, int space_seen)
9917{
9918 rb_encoding *enc;
9919 register int c;
9920 rb_parser_string_t *lit;
9921 const char *start = p->lex.pcur;
9922
9923 if (IS_END()) {
9924 SET_LEX_STATE(EXPR_VALUE);
9925 return '?';
9926 }
9927 c = nextc(p);
9928 if (c == -1) {
9929 compile_error(p, "incomplete character syntax");
9930 return 0;
9931 }
9932 if (rb_enc_isspace(c, p->enc)) {
9933 if (!IS_ARG()) {
9934 int c2 = escaped_control_code(c);
9935 if (c2) {
9936 WARN_SPACE_CHAR(c2, "?");
9937 }
9938 }
9939 ternary:
9940 pushback(p, c);
9941 SET_LEX_STATE(EXPR_VALUE);
9942 return '?';
9943 }
9944 newtok(p);
9945 enc = p->enc;
9946 int w = parser_precise_mbclen(p, start);
9947 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9948 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9949 if (space_seen) {
9950 const char *ptr = start;
9951 do {
9952 int n = parser_precise_mbclen(p, ptr);
9953 if (n < 0) return -1;
9954 ptr += n;
9955 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9956 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9957 " a conditional operator, put a space after '?'",
9958 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9959 }
9960 goto ternary;
9961 }
9962 else if (c == '\\') {
9963 if (peek(p, 'u')) {
9964 nextc(p);
9965 enc = rb_utf8_encoding();
9966 tokadd_utf8(p, &enc, -1, 0, 0);
9967 }
9968 else if (!ISASCII(c = peekc(p)) && c != -1) {
9969 nextc(p);
9970 if (tokadd_mbchar(p, c) == -1) return 0;
9971 }
9972 else {
9973 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9974 tokadd(p, c);
9975 }
9976 }
9977 else {
9978 if (tokadd_mbchar(p, c) == -1) return 0;
9979 }
9980 tokfix(p);
9981 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9982 set_yylval_str(lit);
9983 SET_LEX_STATE(EXPR_END);
9984 return tCHAR;
9985}
9986
9987static enum yytokentype
9988parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9989{
9990 register int c;
9991 const char *ptok = p->lex.pcur;
9992
9993 if (IS_BEG()) {
9994 int term;
9995 int paren;
9996
9997 c = nextc(p);
9998 quotation:
9999 if (c == -1) goto unterminated;
10000 if (!ISALNUM(c)) {
10001 term = c;
10002 if (!ISASCII(c)) goto unknown;
10003 c = 'Q';
10004 }
10005 else {
10006 term = nextc(p);
10007 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10008 unknown:
10009 pushback(p, term);
10010 c = parser_precise_mbclen(p, p->lex.pcur);
10011 if (c < 0) return 0;
10012 p->lex.pcur += c;
10013 yyerror0("unknown type of %string");
10014 return 0;
10015 }
10016 }
10017 if (term == -1) {
10018 unterminated:
10019 compile_error(p, "unterminated quoted string meets end of file");
10020 return 0;
10021 }
10022 paren = term;
10023 if (term == '(') term = ')';
10024 else if (term == '[') term = ']';
10025 else if (term == '{') term = '}';
10026 else if (term == '<') term = '>';
10027 else paren = 0;
10028
10029 p->lex.ptok = ptok-1;
10030 switch (c) {
10031 case 'Q':
10032 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10033 return tSTRING_BEG;
10034
10035 case 'q':
10036 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10037 return tSTRING_BEG;
10038
10039 case 'W':
10040 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10041 return tWORDS_BEG;
10042
10043 case 'w':
10044 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10045 return tQWORDS_BEG;
10046
10047 case 'I':
10048 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10049 return tSYMBOLS_BEG;
10050
10051 case 'i':
10052 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10053 return tQSYMBOLS_BEG;
10054
10055 case 'x':
10056 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10057 return tXSTRING_BEG;
10058
10059 case 'r':
10060 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10061 return tREGEXP_BEG;
10062
10063 case 's':
10064 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10065 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10066 return tSYMBEG;
10067
10068 default:
10069 yyerror0("unknown type of %string");
10070 return 0;
10071 }
10072 }
10073 if ((c = nextc(p)) == '=') {
10074 set_yylval_id('%');
10075 SET_LEX_STATE(EXPR_BEG);
10076 return tOP_ASGN;
10077 }
10078 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10079 goto quotation;
10080 }
10081 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10082 pushback(p, c);
10083 return warn_balanced('%', "%%", "string literal");
10084}
10085
10086static int
10087tokadd_ident(struct parser_params *p, int c)
10088{
10089 do {
10090 if (tokadd_mbchar(p, c) == -1) return -1;
10091 c = nextc(p);
10092 } while (parser_is_identchar(p));
10093 pushback(p, c);
10094 return 0;
10095}
10096
10097static ID
10098tokenize_ident(struct parser_params *p)
10099{
10100 ID ident = TOK_INTERN();
10101
10102 set_yylval_name(ident);
10103
10104 return ident;
10105}
10106
10107static int
10108parse_numvar(struct parser_params *p)
10109{
10110 size_t len;
10111 int overflow;
10112 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10113 const unsigned long nth_ref_max =
10114 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10115 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10116 * turned into a Fixnum, in compile.c */
10117
10118 if (overflow || n > nth_ref_max) {
10119 /* compile_error()? */
10120 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10121 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10122 }
10123 else {
10124 return (int)n;
10125 }
10126}
10127
10128static enum yytokentype
10129parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10130{
10131 const char *ptr = p->lex.pcur;
10132 register int c;
10133
10134 SET_LEX_STATE(EXPR_END);
10135 p->lex.ptok = ptr - 1; /* from '$' */
10136 newtok(p);
10137 c = nextc(p);
10138 switch (c) {
10139 case '_': /* $_: last read line string */
10140 c = nextc(p);
10141 if (parser_is_identchar(p)) {
10142 tokadd(p, '$');
10143 tokadd(p, '_');
10144 break;
10145 }
10146 pushback(p, c);
10147 c = '_';
10148 /* fall through */
10149 case '~': /* $~: match-data */
10150 case '*': /* $*: argv */
10151 case '$': /* $$: pid */
10152 case '?': /* $?: last status */
10153 case '!': /* $!: error string */
10154 case '@': /* $@: error position */
10155 case '/': /* $/: input record separator */
10156 case '\\': /* $\: output record separator */
10157 case ';': /* $;: field separator */
10158 case ',': /* $,: output field separator */
10159 case '.': /* $.: last read line number */
10160 case '=': /* $=: ignorecase */
10161 case ':': /* $:: load path */
10162 case '<': /* $<: default input handle */
10163 case '>': /* $>: default output handle */
10164 case '\"': /* $": already loaded files */
10165 tokadd(p, '$');
10166 tokadd(p, c);
10167 goto gvar;
10168
10169 case '-':
10170 tokadd(p, '$');
10171 tokadd(p, c);
10172 c = nextc(p);
10173 if (parser_is_identchar(p)) {
10174 if (tokadd_mbchar(p, c) == -1) return 0;
10175 }
10176 else {
10177 pushback(p, c);
10178 pushback(p, '-');
10179 return '$';
10180 }
10181 gvar:
10182 tokenize_ident(p);
10183 return tGVAR;
10184
10185 case '&': /* $&: last match */
10186 case '`': /* $`: string before last match */
10187 case '\'': /* $': string after last match */
10188 case '+': /* $+: string matches last paren. */
10189 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10190 tokadd(p, '$');
10191 tokadd(p, c);
10192 goto gvar;
10193 }
10194 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10195 return tBACK_REF;
10196
10197 case '1': case '2': case '3':
10198 case '4': case '5': case '6':
10199 case '7': case '8': case '9':
10200 tokadd(p, '$');
10201 do {
10202 tokadd(p, c);
10203 c = nextc(p);
10204 } while (c != -1 && ISDIGIT(c));
10205 pushback(p, c);
10206 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10207 tokfix(p);
10208 c = parse_numvar(p);
10209 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10210 return tNTH_REF;
10211
10212 default:
10213 if (!parser_is_identchar(p)) {
10214 YYLTYPE loc = RUBY_INIT_YYLLOC();
10215 if (c == -1 || ISSPACE(c)) {
10216 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10217 }
10218 else {
10219 pushback(p, c);
10220 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10221 }
10222 parser_show_error_line(p, &loc);
10223 set_yylval_noname();
10224 return tGVAR;
10225 }
10226 /* fall through */
10227 case '0':
10228 tokadd(p, '$');
10229 }
10230
10231 if (tokadd_ident(p, c)) return 0;
10232 SET_LEX_STATE(EXPR_END);
10233 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10234 tokenize_ident(p);
10235 }
10236 else {
10237 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10238 set_yylval_noname();
10239 }
10240 return tGVAR;
10241}
10242
10243static bool
10244parser_numbered_param(struct parser_params *p, int n)
10245{
10246 if (n < 0) return false;
10247
10248 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10249 return false;
10250 }
10251 if (p->max_numparam == ORDINAL_PARAM) {
10252 compile_error(p, "ordinary parameter is defined");
10253 return false;
10254 }
10255 struct vtable *args = p->lvtbl->args;
10256 if (p->max_numparam < n) {
10257 p->max_numparam = n;
10258 }
10259 while (n > args->pos) {
10260 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10261 }
10262 return true;
10263}
10264
10265static enum yytokentype
10266parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10267{
10268 const char *ptr = p->lex.pcur;
10269 enum yytokentype result = tIVAR;
10270 register int c = nextc(p);
10271 YYLTYPE loc;
10272
10273 p->lex.ptok = ptr - 1; /* from '@' */
10274 newtok(p);
10275 tokadd(p, '@');
10276 if (c == '@') {
10277 result = tCVAR;
10278 tokadd(p, '@');
10279 c = nextc(p);
10280 }
10281 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10282 if (c == -1 || !parser_is_identchar(p)) {
10283 pushback(p, c);
10284 RUBY_SET_YYLLOC(loc);
10285 if (result == tIVAR) {
10286 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10287 }
10288 else {
10289 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10290 }
10291 parser_show_error_line(p, &loc);
10292 set_yylval_noname();
10293 SET_LEX_STATE(EXPR_END);
10294 return result;
10295 }
10296 else if (ISDIGIT(c)) {
10297 pushback(p, c);
10298 RUBY_SET_YYLLOC(loc);
10299 if (result == tIVAR) {
10300 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10301 }
10302 else {
10303 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10304 }
10305 parser_show_error_line(p, &loc);
10306 set_yylval_noname();
10307 SET_LEX_STATE(EXPR_END);
10308 return result;
10309 }
10310
10311 if (tokadd_ident(p, c)) return 0;
10312 tokenize_ident(p);
10313 return result;
10314}
10315
10316static enum yytokentype
10317parse_ident(struct parser_params *p, int c, int cmd_state)
10318{
10319 enum yytokentype result;
10320 bool is_ascii = true;
10321 const enum lex_state_e last_state = p->lex.state;
10322 ID ident;
10323 int enforce_keyword_end = 0;
10324
10325 do {
10326 if (!ISASCII(c)) is_ascii = false;
10327 if (tokadd_mbchar(p, c) == -1) return 0;
10328 c = nextc(p);
10329 } while (parser_is_identchar(p));
10330 if ((c == '!' || c == '?') && !peek(p, '=')) {
10331 result = tFID;
10332 tokadd(p, c);
10333 }
10334 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10335 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10336 result = tIDENTIFIER;
10337 tokadd(p, c);
10338 }
10339 else {
10340 result = tCONSTANT; /* assume provisionally */
10341 pushback(p, c);
10342 }
10343 tokfix(p);
10344
10345 if (IS_LABEL_POSSIBLE()) {
10346 if (IS_LABEL_SUFFIX(0)) {
10347 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10348 nextc(p);
10349 tokenize_ident(p);
10350 return tLABEL;
10351 }
10352 }
10353
10354#ifndef RIPPER
10355 if (peek_end_expect_token_locations(p)) {
10356 const rb_code_position_t *end_pos;
10357 int lineno, column;
10358 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10359
10360 end_pos = peek_end_expect_token_locations(p)->pos;
10361 lineno = end_pos->lineno;
10362 column = end_pos->column;
10363
10364 if (p->debug) {
10365 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10366 p->ruby_sourceline, beg_pos, lineno, column);
10367 }
10368
10369 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10370 const struct kwtable *kw;
10371
10372 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10373 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10374 enforce_keyword_end = 1;
10375 }
10376 }
10377 }
10378#endif
10379
10380 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10381 const struct kwtable *kw;
10382
10383 /* See if it is a reserved word. */
10384 kw = rb_reserved_word(tok(p), toklen(p));
10385 if (kw) {
10386 enum lex_state_e state = p->lex.state;
10387 if (IS_lex_state_for(state, EXPR_FNAME)) {
10388 SET_LEX_STATE(EXPR_ENDFN);
10389 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10390 return kw->id[0];
10391 }
10392 SET_LEX_STATE(kw->state);
10393 if (IS_lex_state(EXPR_BEG)) {
10394 p->command_start = TRUE;
10395 }
10396 if (kw->id[0] == keyword_do) {
10397 if (lambda_beginning_p()) {
10398 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10399 return keyword_do_LAMBDA;
10400 }
10401 if (COND_P()) return keyword_do_cond;
10402 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10403 return keyword_do_block;
10404 return keyword_do;
10405 }
10406 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10407 return kw->id[0];
10408 else {
10409 if (kw->id[0] != kw->id[1])
10410 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10411 return kw->id[1];
10412 }
10413 }
10414 }
10415
10416 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10417 if (cmd_state) {
10418 SET_LEX_STATE(EXPR_CMDARG);
10419 }
10420 else {
10421 SET_LEX_STATE(EXPR_ARG);
10422 }
10423 }
10424 else if (p->lex.state == EXPR_FNAME) {
10425 SET_LEX_STATE(EXPR_ENDFN);
10426 }
10427 else {
10428 SET_LEX_STATE(EXPR_END);
10429 }
10430
10431 ident = tokenize_ident(p);
10432 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10433 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10434 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10435 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10436 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10437 }
10438 return result;
10439}
10440
10441static void
10442warn_cr(struct parser_params *p)
10443{
10444 if (!p->cr_seen) {
10445 p->cr_seen = TRUE;
10446 /* carried over with p->lex.nextline for nextc() */
10447 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10448 }
10449}
10450
10451static enum yytokentype
10452parser_yylex(struct parser_params *p)
10453{
10454 register int c;
10455 int space_seen = 0;
10456 int cmd_state;
10457 int label;
10458 enum lex_state_e last_state;
10459 int fallthru = FALSE;
10460 int token_seen = p->token_seen;
10461
10462 if (p->lex.strterm) {
10463 if (strterm_is_heredoc(p->lex.strterm)) {
10464 token_flush(p);
10465 return here_document(p, &p->lex.strterm->u.heredoc);
10466 }
10467 else {
10468 token_flush(p);
10469 return parse_string(p, &p->lex.strterm->u.literal);
10470 }
10471 }
10472 cmd_state = p->command_start;
10473 p->command_start = FALSE;
10474 p->token_seen = TRUE;
10475#ifndef RIPPER
10476 token_flush(p);
10477#endif
10478 retry:
10479 last_state = p->lex.state;
10480 switch (c = nextc(p)) {
10481 case '\0': /* NUL */
10482 case '\004': /* ^D */
10483 case '\032': /* ^Z */
10484 case -1: /* end of script. */
10485 p->eofp = 1;
10486#ifndef RIPPER
10487 if (p->end_expect_token_locations) {
10488 pop_end_expect_token_locations(p);
10489 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10490 return tDUMNY_END;
10491 }
10492#endif
10493 /* Set location for end-of-input because dispatch_scan_event is not called. */
10494 RUBY_SET_YYLLOC(*p->yylloc);
10495 return END_OF_INPUT;
10496
10497 /* white spaces */
10498 case '\r':
10499 warn_cr(p);
10500 /* fall through */
10501 case ' ': case '\t': case '\f':
10502 case '\13': /* '\v' */
10503 space_seen = 1;
10504 while ((c = nextc(p))) {
10505 switch (c) {
10506 case '\r':
10507 warn_cr(p);
10508 /* fall through */
10509 case ' ': case '\t': case '\f':
10510 case '\13': /* '\v' */
10511 break;
10512 default:
10513 goto outofloop;
10514 }
10515 }
10516 outofloop:
10517 pushback(p, c);
10518 dispatch_scan_event(p, tSP);
10519#ifndef RIPPER
10520 token_flush(p);
10521#endif
10522 goto retry;
10523
10524 case '#': /* it's a comment */
10525 p->token_seen = token_seen;
10526 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10527 /* no magic_comment in shebang line */
10528 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10529 if (comment_at_top(p)) {
10530 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10531 }
10532 }
10533 p->lex.pcur = pcur, p->lex.ptok = ptok;
10534 lex_goto_eol(p);
10535 dispatch_scan_event(p, tCOMMENT);
10536 fallthru = TRUE;
10537 /* fall through */
10538 case '\n':
10539 p->token_seen = token_seen;
10540 rb_parser_string_t *prevline = p->lex.lastline;
10541 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10542 !IS_lex_state(EXPR_LABELED));
10543 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10544 if (!fallthru) {
10545 dispatch_scan_event(p, tIGNORED_NL);
10546 }
10547 fallthru = FALSE;
10548 if (!c && p->ctxt.in_kwarg) {
10549 goto normal_newline;
10550 }
10551 goto retry;
10552 }
10553 while (1) {
10554 switch (c = nextc(p)) {
10555 case ' ': case '\t': case '\f': case '\r':
10556 case '\13': /* '\v' */
10557 space_seen = 1;
10558 break;
10559 case '#':
10560 pushback(p, c);
10561 if (space_seen) {
10562 dispatch_scan_event(p, tSP);
10563 token_flush(p);
10564 }
10565 goto retry;
10566 case 'a':
10567 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10568 goto bol;
10569 case 'o':
10570 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10571 goto bol;
10572 case '|':
10573 if (peek(p, '|')) goto leading_logical;
10574 goto bol;
10575 case '&':
10576 if (peek(p, '&')) {
10577 leading_logical:
10578 pushback(p, c);
10579 dispatch_delayed_token(p, tIGNORED_NL);
10580 cmd_state = FALSE;
10581 goto retry;
10582 }
10583 /* fall through */
10584 case '.': {
10585 dispatch_delayed_token(p, tIGNORED_NL);
10586 if (peek(p, '.') == (c == '&')) {
10587 pushback(p, c);
10588 dispatch_scan_event(p, tSP);
10589 goto retry;
10590 }
10591 }
10592 bol:
10593 default:
10594 p->ruby_sourceline--;
10595 p->lex.nextline = p->lex.lastline;
10596 set_lastline(p, prevline);
10597 case -1: /* EOF no decrement*/
10598 if (c == -1 && space_seen) {
10599 dispatch_scan_event(p, tSP);
10600 }
10601 lex_goto_eol(p);
10602 if (c != -1) {
10603 token_flush(p);
10604 RUBY_SET_YYLLOC(*p->yylloc);
10605 }
10606 goto normal_newline;
10607 }
10608 }
10609 normal_newline:
10610 p->command_start = TRUE;
10611 SET_LEX_STATE(EXPR_BEG);
10612 return '\n';
10613
10614 case '*':
10615 if ((c = nextc(p)) == '*') {
10616 if ((c = nextc(p)) == '=') {
10617 set_yylval_id(idPow);
10618 SET_LEX_STATE(EXPR_BEG);
10619 return tOP_ASGN;
10620 }
10621 pushback(p, c);
10622 if (IS_SPCARG(c)) {
10623 rb_warning0("'**' interpreted as argument prefix");
10624 c = tDSTAR;
10625 }
10626 else if (IS_BEG()) {
10627 c = tDSTAR;
10628 }
10629 else {
10630 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10631 }
10632 }
10633 else {
10634 if (c == '=') {
10635 set_yylval_id('*');
10636 SET_LEX_STATE(EXPR_BEG);
10637 return tOP_ASGN;
10638 }
10639 pushback(p, c);
10640 if (IS_SPCARG(c)) {
10641 rb_warning0("'*' interpreted as argument prefix");
10642 c = tSTAR;
10643 }
10644 else if (IS_BEG()) {
10645 c = tSTAR;
10646 }
10647 else {
10648 c = warn_balanced('*', "*", "argument prefix");
10649 }
10650 }
10651 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10652 return c;
10653
10654 case '!':
10655 c = nextc(p);
10656 if (IS_AFTER_OPERATOR()) {
10657 SET_LEX_STATE(EXPR_ARG);
10658 if (c == '@') {
10659 return '!';
10660 }
10661 }
10662 else {
10663 SET_LEX_STATE(EXPR_BEG);
10664 }
10665 if (c == '=') {
10666 return tNEQ;
10667 }
10668 if (c == '~') {
10669 return tNMATCH;
10670 }
10671 pushback(p, c);
10672 return '!';
10673
10674 case '=':
10675 if (was_bol(p)) {
10676 /* skip embedded rd document */
10677 if (word_match_p(p, "begin", 5)) {
10678 int first_p = TRUE;
10679
10680 lex_goto_eol(p);
10681 dispatch_scan_event(p, tEMBDOC_BEG);
10682 for (;;) {
10683 lex_goto_eol(p);
10684 if (!first_p) {
10685 dispatch_scan_event(p, tEMBDOC);
10686 }
10687 first_p = FALSE;
10688 c = nextc(p);
10689 if (c == -1) {
10690 compile_error(p, "embedded document meets end of file");
10691 return END_OF_INPUT;
10692 }
10693 if (c == '=' && word_match_p(p, "end", 3)) {
10694 break;
10695 }
10696 pushback(p, c);
10697 }
10698 lex_goto_eol(p);
10699 dispatch_scan_event(p, tEMBDOC_END);
10700 goto retry;
10701 }
10702 }
10703
10704 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10705 if ((c = nextc(p)) == '=') {
10706 if ((c = nextc(p)) == '=') {
10707 return tEQQ;
10708 }
10709 pushback(p, c);
10710 return tEQ;
10711 }
10712 if (c == '~') {
10713 return tMATCH;
10714 }
10715 else if (c == '>') {
10716 return tASSOC;
10717 }
10718 pushback(p, c);
10719 return '=';
10720
10721 case '<':
10722 c = nextc(p);
10723 if (c == '<' &&
10724 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10725 !IS_END() &&
10726 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10727 enum yytokentype token = heredoc_identifier(p);
10728 if (token) return token < 0 ? 0 : token;
10729 }
10730 if (IS_AFTER_OPERATOR()) {
10731 SET_LEX_STATE(EXPR_ARG);
10732 }
10733 else {
10734 if (IS_lex_state(EXPR_CLASS))
10735 p->command_start = TRUE;
10736 SET_LEX_STATE(EXPR_BEG);
10737 }
10738 if (c == '=') {
10739 if ((c = nextc(p)) == '>') {
10740 return tCMP;
10741 }
10742 pushback(p, c);
10743 return tLEQ;
10744 }
10745 if (c == '<') {
10746 if ((c = nextc(p)) == '=') {
10747 set_yylval_id(idLTLT);
10748 SET_LEX_STATE(EXPR_BEG);
10749 return tOP_ASGN;
10750 }
10751 pushback(p, c);
10752 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10753 }
10754 pushback(p, c);
10755 return '<';
10756
10757 case '>':
10758 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10759 if ((c = nextc(p)) == '=') {
10760 return tGEQ;
10761 }
10762 if (c == '>') {
10763 if ((c = nextc(p)) == '=') {
10764 set_yylval_id(idGTGT);
10765 SET_LEX_STATE(EXPR_BEG);
10766 return tOP_ASGN;
10767 }
10768 pushback(p, c);
10769 return tRSHFT;
10770 }
10771 pushback(p, c);
10772 return '>';
10773
10774 case '"':
10775 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10776 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10777 p->lex.ptok = p->lex.pcur-1;
10778 return tSTRING_BEG;
10779
10780 case '`':
10781 if (IS_lex_state(EXPR_FNAME)) {
10782 SET_LEX_STATE(EXPR_ENDFN);
10783 return c;
10784 }
10785 if (IS_lex_state(EXPR_DOT)) {
10786 if (cmd_state)
10787 SET_LEX_STATE(EXPR_CMDARG);
10788 else
10789 SET_LEX_STATE(EXPR_ARG);
10790 return c;
10791 }
10792 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10793 return tXSTRING_BEG;
10794
10795 case '\'':
10796 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10797 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10798 p->lex.ptok = p->lex.pcur-1;
10799 return tSTRING_BEG;
10800
10801 case '?':
10802 return parse_qmark(p, space_seen);
10803
10804 case '&':
10805 if ((c = nextc(p)) == '&') {
10806 SET_LEX_STATE(EXPR_BEG);
10807 if ((c = nextc(p)) == '=') {
10808 set_yylval_id(idANDOP);
10809 SET_LEX_STATE(EXPR_BEG);
10810 return tOP_ASGN;
10811 }
10812 pushback(p, c);
10813 return tANDOP;
10814 }
10815 else if (c == '=') {
10816 set_yylval_id('&');
10817 SET_LEX_STATE(EXPR_BEG);
10818 return tOP_ASGN;
10819 }
10820 else if (c == '.') {
10821 set_yylval_id(idANDDOT);
10822 SET_LEX_STATE(EXPR_DOT);
10823 return tANDDOT;
10824 }
10825 pushback(p, c);
10826 if (IS_SPCARG(c)) {
10827 if ((c != ':') ||
10828 (c = peekc_n(p, 1)) == -1 ||
10829 !(c == '\'' || c == '"' ||
10830 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10831 rb_warning0("'&' interpreted as argument prefix");
10832 }
10833 c = tAMPER;
10834 }
10835 else if (IS_BEG()) {
10836 c = tAMPER;
10837 }
10838 else {
10839 c = warn_balanced('&', "&", "argument prefix");
10840 }
10841 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10842 return c;
10843
10844 case '|':
10845 if ((c = nextc(p)) == '|') {
10846 SET_LEX_STATE(EXPR_BEG);
10847 if ((c = nextc(p)) == '=') {
10848 set_yylval_id(idOROP);
10849 SET_LEX_STATE(EXPR_BEG);
10850 return tOP_ASGN;
10851 }
10852 pushback(p, c);
10853 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10854 c = '|';
10855 pushback(p, '|');
10856 return c;
10857 }
10858 return tOROP;
10859 }
10860 if (c == '=') {
10861 set_yylval_id('|');
10862 SET_LEX_STATE(EXPR_BEG);
10863 return tOP_ASGN;
10864 }
10865 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10866 pushback(p, c);
10867 return '|';
10868
10869 case '+':
10870 c = nextc(p);
10871 if (IS_AFTER_OPERATOR()) {
10872 SET_LEX_STATE(EXPR_ARG);
10873 if (c == '@') {
10874 return tUPLUS;
10875 }
10876 pushback(p, c);
10877 return '+';
10878 }
10879 if (c == '=') {
10880 set_yylval_id('+');
10881 SET_LEX_STATE(EXPR_BEG);
10882 return tOP_ASGN;
10883 }
10884 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10885 SET_LEX_STATE(EXPR_BEG);
10886 pushback(p, c);
10887 if (c != -1 && ISDIGIT(c)) {
10888 return parse_numeric(p, '+');
10889 }
10890 return tUPLUS;
10891 }
10892 SET_LEX_STATE(EXPR_BEG);
10893 pushback(p, c);
10894 return warn_balanced('+', "+", "unary operator");
10895
10896 case '-':
10897 c = nextc(p);
10898 if (IS_AFTER_OPERATOR()) {
10899 SET_LEX_STATE(EXPR_ARG);
10900 if (c == '@') {
10901 return tUMINUS;
10902 }
10903 pushback(p, c);
10904 return '-';
10905 }
10906 if (c == '=') {
10907 set_yylval_id('-');
10908 SET_LEX_STATE(EXPR_BEG);
10909 return tOP_ASGN;
10910 }
10911 if (c == '>') {
10912 SET_LEX_STATE(EXPR_ENDFN);
10913 yylval.num = p->lex.lpar_beg;
10914 p->lex.lpar_beg = p->lex.paren_nest;
10915 return tLAMBDA;
10916 }
10917 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10918 SET_LEX_STATE(EXPR_BEG);
10919 pushback(p, c);
10920 if (c != -1 && ISDIGIT(c)) {
10921 return tUMINUS_NUM;
10922 }
10923 return tUMINUS;
10924 }
10925 SET_LEX_STATE(EXPR_BEG);
10926 pushback(p, c);
10927 return warn_balanced('-', "-", "unary operator");
10928
10929 case '.': {
10930 int is_beg = IS_BEG();
10931 SET_LEX_STATE(EXPR_BEG);
10932 if ((c = nextc(p)) == '.') {
10933 if ((c = nextc(p)) == '.') {
10934 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10935 SET_LEX_STATE(EXPR_ENDARG);
10936 return tBDOT3;
10937 }
10938 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10939 rb_warn0("... at EOL, should be parenthesized?");
10940 }
10941 return is_beg ? tBDOT3 : tDOT3;
10942 }
10943 pushback(p, c);
10944 return is_beg ? tBDOT2 : tDOT2;
10945 }
10946 pushback(p, c);
10947 if (c != -1 && ISDIGIT(c)) {
10948 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10949 parse_numeric(p, '.');
10950 if (ISDIGIT(prev)) {
10951 yyerror0("unexpected fraction part after numeric literal");
10952 }
10953 else {
10954 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10955 }
10956 SET_LEX_STATE(EXPR_END);
10957 p->lex.ptok = p->lex.pcur;
10958 goto retry;
10959 }
10960 set_yylval_id('.');
10961 SET_LEX_STATE(EXPR_DOT);
10962 return '.';
10963 }
10964
10965 case '0': case '1': case '2': case '3': case '4':
10966 case '5': case '6': case '7': case '8': case '9':
10967 return parse_numeric(p, c);
10968
10969 case ')':
10970 COND_POP();
10971 CMDARG_POP();
10972 SET_LEX_STATE(EXPR_ENDFN);
10973 p->lex.paren_nest--;
10974 return c;
10975
10976 case ']':
10977 COND_POP();
10978 CMDARG_POP();
10979 SET_LEX_STATE(EXPR_END);
10980 p->lex.paren_nest--;
10981 return c;
10982
10983 case '}':
10984 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10985 if (!p->lex.brace_nest--) return tSTRING_DEND;
10986 COND_POP();
10987 CMDARG_POP();
10988 SET_LEX_STATE(EXPR_END);
10989 p->lex.paren_nest--;
10990 return c;
10991
10992 case ':':
10993 c = nextc(p);
10994 if (c == ':') {
10995 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10996 SET_LEX_STATE(EXPR_BEG);
10997 return tCOLON3;
10998 }
10999 set_yylval_id(idCOLON2);
11000 SET_LEX_STATE(EXPR_DOT);
11001 return tCOLON2;
11002 }
11003 if (IS_END() || ISSPACE(c) || c == '#') {
11004 pushback(p, c);
11005 c = warn_balanced(':', ":", "symbol literal");
11006 SET_LEX_STATE(EXPR_BEG);
11007 return c;
11008 }
11009 switch (c) {
11010 case '\'':
11011 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11012 break;
11013 case '"':
11014 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11015 break;
11016 default:
11017 pushback(p, c);
11018 break;
11019 }
11020 SET_LEX_STATE(EXPR_FNAME);
11021 return tSYMBEG;
11022
11023 case '/':
11024 if (IS_BEG()) {
11025 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11026 return tREGEXP_BEG;
11027 }
11028 if ((c = nextc(p)) == '=') {
11029 set_yylval_id('/');
11030 SET_LEX_STATE(EXPR_BEG);
11031 return tOP_ASGN;
11032 }
11033 pushback(p, c);
11034 if (IS_SPCARG(c)) {
11035 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11036 return tREGEXP_BEG;
11037 }
11038 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11039 return warn_balanced('/', "/", "regexp literal");
11040
11041 case '^':
11042 if ((c = nextc(p)) == '=') {
11043 set_yylval_id('^');
11044 SET_LEX_STATE(EXPR_BEG);
11045 return tOP_ASGN;
11046 }
11047 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11048 pushback(p, c);
11049 return '^';
11050
11051 case ';':
11052 SET_LEX_STATE(EXPR_BEG);
11053 p->command_start = TRUE;
11054 return ';';
11055
11056 case ',':
11057 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11058 return ',';
11059
11060 case '~':
11061 if (IS_AFTER_OPERATOR()) {
11062 if ((c = nextc(p)) != '@') {
11063 pushback(p, c);
11064 }
11065 SET_LEX_STATE(EXPR_ARG);
11066 }
11067 else {
11068 SET_LEX_STATE(EXPR_BEG);
11069 }
11070 return '~';
11071
11072 case '(':
11073 if (IS_BEG()) {
11074 c = tLPAREN;
11075 }
11076 else if (!space_seen) {
11077 /* foo( ... ) => method call, no ambiguity */
11078 }
11079 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11080 c = tLPAREN_ARG;
11081 }
11082 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11083 rb_warning0("parentheses after method name is interpreted as "
11084 "an argument list, not a decomposed argument");
11085 }
11086 p->lex.paren_nest++;
11087 COND_PUSH(0);
11088 CMDARG_PUSH(0);
11089 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11090 return c;
11091
11092 case '[':
11093 p->lex.paren_nest++;
11094 if (IS_AFTER_OPERATOR()) {
11095 if ((c = nextc(p)) == ']') {
11096 p->lex.paren_nest--;
11097 SET_LEX_STATE(EXPR_ARG);
11098 if ((c = nextc(p)) == '=') {
11099 return tASET;
11100 }
11101 pushback(p, c);
11102 return tAREF;
11103 }
11104 pushback(p, c);
11105 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11106 return '[';
11107 }
11108 else if (IS_BEG()) {
11109 c = tLBRACK;
11110 }
11111 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11112 c = tLBRACK;
11113 }
11114 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11115 COND_PUSH(0);
11116 CMDARG_PUSH(0);
11117 return c;
11118
11119 case '{':
11120 ++p->lex.brace_nest;
11121 if (lambda_beginning_p())
11122 c = tLAMBEG;
11123 else if (IS_lex_state(EXPR_LABELED))
11124 c = tLBRACE; /* hash */
11125 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11126 c = '{'; /* block (primary) */
11127 else if (IS_lex_state(EXPR_ENDARG))
11128 c = tLBRACE_ARG; /* block (expr) */
11129 else
11130 c = tLBRACE; /* hash */
11131 if (c != tLBRACE) {
11132 p->command_start = TRUE;
11133 SET_LEX_STATE(EXPR_BEG);
11134 }
11135 else {
11136 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11137 }
11138 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11139 COND_PUSH(0);
11140 CMDARG_PUSH(0);
11141 return c;
11142
11143 case '\\':
11144 c = nextc(p);
11145 if (c == '\n') {
11146 space_seen = 1;
11147 dispatch_scan_event(p, tSP);
11148 goto retry; /* skip \\n */
11149 }
11150 if (c == ' ') return tSP;
11151 if (ISSPACE(c)) return c;
11152 pushback(p, c);
11153 return '\\';
11154
11155 case '%':
11156 return parse_percent(p, space_seen, last_state);
11157
11158 case '$':
11159 return parse_gvar(p, last_state);
11160
11161 case '@':
11162 return parse_atmark(p, last_state);
11163
11164 case '_':
11165 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11166 p->ruby__end__seen = 1;
11167 p->eofp = 1;
11168#ifdef RIPPER
11169 lex_goto_eol(p);
11170 dispatch_scan_event(p, k__END__);
11171#endif
11172 return END_OF_INPUT;
11173 }
11174 newtok(p);
11175 break;
11176
11177 default:
11178 if (!parser_is_identchar(p)) {
11179 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11180 token_flush(p);
11181 goto retry;
11182 }
11183
11184 newtok(p);
11185 break;
11186 }
11187
11188 return parse_ident(p, c, cmd_state);
11189}
11190
11191static enum yytokentype
11192yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11193{
11194 enum yytokentype t;
11195
11196 p->lval = lval;
11197 lval->node = 0;
11198 p->yylloc = yylloc;
11199
11200 t = parser_yylex(p);
11201
11202 if (has_delayed_token(p))
11203 dispatch_delayed_token(p, t);
11204 else if (t != END_OF_INPUT)
11205 dispatch_scan_event(p, t);
11206
11207 return t;
11208}
11209
11210#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11211
11212static NODE*
11213node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11214{
11215 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11216
11217 rb_node_init(n, type);
11218 return n;
11219}
11220
11221static NODE *
11222nd_set_loc(NODE *nd, const YYLTYPE *loc)
11223{
11224 nd->nd_loc = *loc;
11225 nd_set_line(nd, loc->beg_pos.lineno);
11226 return nd;
11227}
11228
11229static NODE*
11230node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11231{
11232 NODE *n = node_new_internal(p, type, size, alignment);
11233
11234 nd_set_loc(n, loc);
11235 nd_set_node_id(n, parser_get_node_id(p));
11236 return n;
11237}
11238
11239#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11240
11241static rb_node_scope_t *
11242rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11243{
11244 rb_ast_id_table_t *nd_tbl;
11245 nd_tbl = local_tbl(p);
11246 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11247 n->nd_tbl = nd_tbl;
11248 n->nd_body = nd_body;
11249 n->nd_parent = nd_parent;
11250 n->nd_args = nd_args;
11251
11252 return n;
11253}
11254
11255static rb_node_scope_t *
11256rb_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)
11257{
11258 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11259 n->nd_tbl = nd_tbl;
11260 n->nd_body = nd_body;
11261 n->nd_parent = nd_parent;
11262 n->nd_args = nd_args;
11263
11264 return n;
11265}
11266
11267static rb_node_defn_t *
11268rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11269{
11270 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11271 n->nd_mid = nd_mid;
11272 n->nd_defn = nd_defn;
11273
11274 return n;
11275}
11276
11277static rb_node_defs_t *
11278rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11279{
11280 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11281 n->nd_recv = nd_recv;
11282 n->nd_mid = nd_mid;
11283 n->nd_defn = nd_defn;
11284
11285 return n;
11286}
11287
11288static rb_node_block_t *
11289rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11290{
11291 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11292 n->nd_head = nd_head;
11293 n->nd_end = (NODE *)n;
11294 n->nd_next = 0;
11295
11296 return n;
11297}
11298
11299static rb_node_for_t *
11300rb_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)
11301{
11302 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11303 n->nd_body = nd_body;
11304 n->nd_iter = nd_iter;
11305 n->for_keyword_loc = *for_keyword_loc;
11306 n->in_keyword_loc = *in_keyword_loc;
11307 n->do_keyword_loc = *do_keyword_loc;
11308 n->end_keyword_loc = *end_keyword_loc;
11309
11310 return n;
11311}
11312
11313static rb_node_for_masgn_t *
11314rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11315{
11316 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11317 n->nd_var = nd_var;
11318
11319 return n;
11320}
11321
11322static rb_node_retry_t *
11323rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11324{
11325 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11326
11327 return n;
11328}
11329
11330static rb_node_begin_t *
11331rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11332{
11333 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11334 n->nd_body = nd_body;
11335
11336 return n;
11337}
11338
11339static rb_node_rescue_t *
11340rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11341{
11342 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11343 n->nd_head = nd_head;
11344 n->nd_resq = nd_resq;
11345 n->nd_else = nd_else;
11346
11347 return n;
11348}
11349
11350static rb_node_resbody_t *
11351rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11352{
11353 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11354 n->nd_args = nd_args;
11355 n->nd_exc_var = nd_exc_var;
11356 n->nd_body = nd_body;
11357 n->nd_next = nd_next;
11358
11359 return n;
11360}
11361
11362static rb_node_ensure_t *
11363rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11364{
11365 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11366 n->nd_head = nd_head;
11367 n->nd_ensr = nd_ensr;
11368
11369 return n;
11370}
11371
11372static rb_node_and_t *
11373rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11374{
11375 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11376 n->nd_1st = nd_1st;
11377 n->nd_2nd = nd_2nd;
11378 n->operator_loc = *operator_loc;
11379
11380 return n;
11381}
11382
11383static rb_node_or_t *
11384rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11385{
11386 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11387 n->nd_1st = nd_1st;
11388 n->nd_2nd = nd_2nd;
11389 n->operator_loc = *operator_loc;
11390
11391 return n;
11392}
11393
11394static rb_node_return_t *
11395rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11396{
11397 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11398 n->nd_stts = nd_stts;
11399 n->keyword_loc = *keyword_loc;
11400 return n;
11401}
11402
11403static rb_node_yield_t *
11404rb_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)
11405{
11406 if (nd_head) no_blockarg(p, nd_head);
11407
11408 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11409 n->nd_head = nd_head;
11410 n->keyword_loc = *keyword_loc;
11411 n->lparen_loc = *lparen_loc;
11412 n->rparen_loc = *rparen_loc;
11413
11414 return n;
11415}
11416
11417static rb_node_if_t *
11418rb_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)
11419{
11420 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11421 n->nd_cond = nd_cond;
11422 n->nd_body = nd_body;
11423 n->nd_else = nd_else;
11424 n->if_keyword_loc = *if_keyword_loc;
11425 n->then_keyword_loc = *then_keyword_loc;
11426 n->end_keyword_loc = *end_keyword_loc;
11427
11428 return n;
11429}
11430
11431static rb_node_unless_t *
11432rb_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)
11433{
11434 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11435 n->nd_cond = nd_cond;
11436 n->nd_body = nd_body;
11437 n->nd_else = nd_else;
11438 n->keyword_loc = *keyword_loc;
11439 n->then_keyword_loc = *then_keyword_loc;
11440 n->end_keyword_loc = *end_keyword_loc;
11441
11442 return n;
11443}
11444
11445static rb_node_class_t *
11446rb_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)
11447{
11448 /* Keep the order of node creation */
11449 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11450 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11451 RNODE_SCOPE(scope)->nd_parent = &n->node;
11452 n->nd_cpath = nd_cpath;
11453 n->nd_body = scope;
11454 n->nd_super = nd_super;
11455 n->class_keyword_loc = *class_keyword_loc;
11456 n->inheritance_operator_loc = *inheritance_operator_loc;
11457 n->end_keyword_loc = *end_keyword_loc;
11458
11459 return n;
11460}
11461
11462static rb_node_sclass_t *
11463rb_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)
11464{
11465 /* Keep the order of node creation */
11466 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11467 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11468 RNODE_SCOPE(scope)->nd_parent = &n->node;
11469 n->nd_recv = nd_recv;
11470 n->nd_body = scope;
11471 n->class_keyword_loc = *class_keyword_loc;
11472 n->operator_loc = *operator_loc;
11473 n->end_keyword_loc = *end_keyword_loc;
11474
11475 return n;
11476}
11477
11478static rb_node_module_t *
11479rb_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)
11480{
11481 /* Keep the order of node creation */
11482 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11483 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11484 RNODE_SCOPE(scope)->nd_parent = &n->node;
11485 n->nd_cpath = nd_cpath;
11486 n->nd_body = scope;
11487 n->module_keyword_loc = *module_keyword_loc;
11488 n->end_keyword_loc = *end_keyword_loc;
11489
11490 return n;
11491}
11492
11493static rb_node_iter_t *
11494rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11495{
11496 /* Keep the order of node creation */
11497 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11498 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11499 RNODE_SCOPE(scope)->nd_parent = &n->node;
11500 n->nd_body = scope;
11501 n->nd_iter = 0;
11502
11503 return n;
11504}
11505
11506static rb_node_lambda_t *
11507rb_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)
11508{
11509 /* Keep the order of node creation */
11510 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11511 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11512 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11513 RNODE_SCOPE(scope)->nd_parent = &n->node;
11514 n->nd_body = scope;
11515 n->operator_loc = *operator_loc;
11516 n->opening_loc = *opening_loc;
11517 n->closing_loc = *closing_loc;
11518
11519 return n;
11520}
11521
11522static rb_node_case_t *
11523rb_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)
11524{
11525 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11526 n->nd_head = nd_head;
11527 n->nd_body = nd_body;
11528 n->case_keyword_loc = *case_keyword_loc;
11529 n->end_keyword_loc = *end_keyword_loc;
11530
11531 return n;
11532}
11533
11534static rb_node_case2_t *
11535rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11536{
11537 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11538 n->nd_head = 0;
11539 n->nd_body = nd_body;
11540 n->case_keyword_loc = *case_keyword_loc;
11541 n->end_keyword_loc = *end_keyword_loc;
11542
11543 return n;
11544}
11545
11546static rb_node_case3_t *
11547rb_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)
11548{
11549 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11550 n->nd_head = nd_head;
11551 n->nd_body = nd_body;
11552 n->case_keyword_loc = *case_keyword_loc;
11553 n->end_keyword_loc = *end_keyword_loc;
11554
11555 return n;
11556}
11557
11558static rb_node_when_t *
11559rb_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)
11560{
11561 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11562 n->nd_head = nd_head;
11563 n->nd_body = nd_body;
11564 n->nd_next = nd_next;
11565 n->keyword_loc = *keyword_loc;
11566 n->then_keyword_loc = *then_keyword_loc;
11567
11568 return n;
11569}
11570
11571static rb_node_in_t *
11572rb_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)
11573{
11574 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11575 n->nd_head = nd_head;
11576 n->nd_body = nd_body;
11577 n->nd_next = nd_next;
11578 n->in_keyword_loc = *in_keyword_loc;
11579 n->then_keyword_loc = *then_keyword_loc;
11580 n->operator_loc = *operator_loc;
11581
11582 return n;
11583}
11584
11585static rb_node_while_t *
11586rb_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)
11587{
11588 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11589 n->nd_cond = nd_cond;
11590 n->nd_body = nd_body;
11591 n->nd_state = nd_state;
11592 n->keyword_loc = *keyword_loc;
11593 n->closing_loc = *closing_loc;
11594
11595 return n;
11596}
11597
11598static rb_node_until_t *
11599rb_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)
11600{
11601 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11602 n->nd_cond = nd_cond;
11603 n->nd_body = nd_body;
11604 n->nd_state = nd_state;
11605 n->keyword_loc = *keyword_loc;
11606 n->closing_loc = *closing_loc;
11607
11608 return n;
11609}
11610
11611static rb_node_colon2_t *
11612rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11613{
11614 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11615 n->nd_head = nd_head;
11616 n->nd_mid = nd_mid;
11617 n->delimiter_loc = *delimiter_loc;
11618 n->name_loc = *name_loc;
11619
11620 return n;
11621}
11622
11623static rb_node_colon3_t *
11624rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11625{
11626 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11627 n->nd_mid = nd_mid;
11628 n->delimiter_loc = *delimiter_loc;
11629 n->name_loc = *name_loc;
11630
11631 return n;
11632}
11633
11634static rb_node_dot2_t *
11635rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11636{
11637 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11638 n->nd_beg = nd_beg;
11639 n->nd_end = nd_end;
11640 n->operator_loc = *operator_loc;
11641
11642 return n;
11643}
11644
11645static rb_node_dot3_t *
11646rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11647{
11648 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11649 n->nd_beg = nd_beg;
11650 n->nd_end = nd_end;
11651 n->operator_loc = *operator_loc;
11652
11653 return n;
11654}
11655
11656static rb_node_self_t *
11657rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11658{
11659 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11660 n->nd_state = 1;
11661
11662 return n;
11663}
11664
11665static rb_node_nil_t *
11666rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11667{
11668 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11669
11670 return n;
11671}
11672
11673static rb_node_true_t *
11674rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11675{
11676 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11677
11678 return n;
11679}
11680
11681static rb_node_false_t *
11682rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11683{
11684 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11685
11686 return n;
11687}
11688
11689static rb_node_super_t *
11690rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11691 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11692{
11693 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11694 n->nd_args = nd_args;
11695 n->keyword_loc = *keyword_loc;
11696 n->lparen_loc = *lparen_loc;
11697 n->rparen_loc = *rparen_loc;
11698
11699 return n;
11700}
11701
11702static rb_node_zsuper_t *
11703rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11704{
11705 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11706
11707 return n;
11708}
11709
11710static rb_node_match2_t *
11711rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11712{
11713 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11714 n->nd_recv = nd_recv;
11715 n->nd_value = nd_value;
11716 n->nd_args = 0;
11717
11718 return n;
11719}
11720
11721static rb_node_match3_t *
11722rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11723{
11724 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11725 n->nd_recv = nd_recv;
11726 n->nd_value = nd_value;
11727
11728 return n;
11729}
11730
11731static rb_node_list_t *
11732rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11733{
11734 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11735}
11736
11737static rb_node_list_t *
11738rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11739{
11740 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11741 n->nd_head = nd_head;
11742 n->as.nd_alen = nd_alen;
11743 n->nd_next = nd_next;
11744
11745 return n;
11746}
11747
11748static rb_node_zlist_t *
11749rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11750{
11751 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11752
11753 return n;
11754}
11755
11756static rb_node_hash_t *
11757rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11758{
11759 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11760 n->nd_head = nd_head;
11761 n->nd_brace = 0;
11762
11763 return n;
11764}
11765
11766static rb_node_masgn_t *
11767rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11768{
11769 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11770 n->nd_head = nd_head;
11771 n->nd_value = 0;
11772 n->nd_args = nd_args;
11773
11774 return n;
11775}
11776
11777static rb_node_gasgn_t *
11778rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11779{
11780 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11781 n->nd_vid = nd_vid;
11782 n->nd_value = nd_value;
11783
11784 return n;
11785}
11786
11787static rb_node_lasgn_t *
11788rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11789{
11790 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11791 n->nd_vid = nd_vid;
11792 n->nd_value = nd_value;
11793
11794 return n;
11795}
11796
11797static rb_node_dasgn_t *
11798rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11799{
11800 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11801 n->nd_vid = nd_vid;
11802 n->nd_value = nd_value;
11803
11804 return n;
11805}
11806
11807static rb_node_iasgn_t *
11808rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11809{
11810 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11811 n->nd_vid = nd_vid;
11812 n->nd_value = nd_value;
11813
11814 return n;
11815}
11816
11817static rb_node_cvasgn_t *
11818rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11819{
11820 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11821 n->nd_vid = nd_vid;
11822 n->nd_value = nd_value;
11823
11824 return n;
11825}
11826
11827static rb_node_op_asgn1_t *
11828rb_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)
11829{
11830 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11831 n->nd_recv = nd_recv;
11832 n->nd_mid = nd_mid;
11833 n->nd_index = index;
11834 n->nd_rvalue = rvalue;
11835 n->call_operator_loc = *call_operator_loc;
11836 n->opening_loc = *opening_loc;
11837 n->closing_loc = *closing_loc;
11838 n->binary_operator_loc = *binary_operator_loc;
11839
11840 return n;
11841}
11842
11843static rb_node_op_asgn2_t *
11844rb_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)
11845{
11846 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11847 n->nd_recv = nd_recv;
11848 n->nd_value = nd_value;
11849 n->nd_vid = nd_vid;
11850 n->nd_mid = nd_mid;
11851 n->nd_aid = nd_aid;
11852 n->call_operator_loc = *call_operator_loc;
11853 n->message_loc = *message_loc;
11854 n->binary_operator_loc = *binary_operator_loc;
11855
11856 return n;
11857}
11858
11859static rb_node_op_asgn_or_t *
11860rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11861{
11862 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11863 n->nd_head = nd_head;
11864 n->nd_value = nd_value;
11865
11866 return n;
11867}
11868
11869static rb_node_op_asgn_and_t *
11870rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11871{
11872 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11873 n->nd_head = nd_head;
11874 n->nd_value = nd_value;
11875
11876 return n;
11877}
11878
11879static rb_node_gvar_t *
11880rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11881{
11882 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11883 n->nd_vid = nd_vid;
11884
11885 return n;
11886}
11887
11888static rb_node_lvar_t *
11889rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11890{
11891 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11892 n->nd_vid = nd_vid;
11893
11894 return n;
11895}
11896
11897static rb_node_dvar_t *
11898rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11899{
11900 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11901 n->nd_vid = nd_vid;
11902
11903 return n;
11904}
11905
11906static rb_node_ivar_t *
11907rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11908{
11909 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11910 n->nd_vid = nd_vid;
11911
11912 return n;
11913}
11914
11915static rb_node_const_t *
11916rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11917{
11918 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11919 n->nd_vid = nd_vid;
11920
11921 return n;
11922}
11923
11924static rb_node_cvar_t *
11925rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11926{
11927 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11928 n->nd_vid = nd_vid;
11929
11930 return n;
11931}
11932
11933static rb_node_nth_ref_t *
11934rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11935{
11936 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11937 n->nd_nth = nd_nth;
11938
11939 return n;
11940}
11941
11942static rb_node_back_ref_t *
11943rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11944{
11945 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11946 n->nd_nth = nd_nth;
11947
11948 return n;
11949}
11950
11951static rb_node_integer_t *
11952rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11953{
11954 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11955 n->val = val;
11956 n->minus = FALSE;
11957 n->base = base;
11958
11959 return n;
11960}
11961
11962static rb_node_float_t *
11963rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11964{
11965 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11966 n->val = val;
11967 n->minus = FALSE;
11968
11969 return n;
11970}
11971
11972static rb_node_rational_t *
11973rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11974{
11975 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11976 n->val = val;
11977 n->minus = FALSE;
11978 n->base = base;
11979 n->seen_point = seen_point;
11980
11981 return n;
11982}
11983
11984static rb_node_imaginary_t *
11985rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11986{
11987 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11988 n->val = val;
11989 n->minus = FALSE;
11990 n->base = base;
11991 n->seen_point = seen_point;
11992 n->type = numeric_type;
11993
11994 return n;
11995}
11996
11997static rb_node_str_t *
11998rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11999{
12000 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12001 n->string = string;
12002
12003 return n;
12004}
12005
12006/* TODO; Use union for NODE_DSTR2 */
12007static rb_node_dstr_t *
12008rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12009{
12010 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12011 n->string = string;
12012 n->as.nd_alen = nd_alen;
12013 n->nd_next = (rb_node_list_t *)nd_next;
12014
12015 return n;
12016}
12017
12018static rb_node_dstr_t *
12019rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12020{
12021 return rb_node_dstr_new0(p, string, 1, 0, loc);
12022}
12023
12024static rb_node_xstr_t *
12025rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12026{
12027 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12028 n->string = string;
12029
12030 return n;
12031}
12032
12033static rb_node_dxstr_t *
12034rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12035{
12036 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12037 n->string = string;
12038 n->as.nd_alen = nd_alen;
12039 n->nd_next = (rb_node_list_t *)nd_next;
12040
12041 return n;
12042}
12043
12044static rb_node_sym_t *
12045rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12046{
12047 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12048 n->string = rb_str_to_parser_string(p, str);
12049
12050 return n;
12051}
12052
12053static rb_node_dsym_t *
12054rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12055{
12056 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12057 n->string = string;
12058 n->as.nd_alen = nd_alen;
12059 n->nd_next = (rb_node_list_t *)nd_next;
12060
12061 return n;
12062}
12063
12064static rb_node_evstr_t *
12065rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12066{
12067 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12068 n->nd_body = nd_body;
12069 n->opening_loc = *opening_loc;
12070 n->closing_loc = *closing_loc;
12071
12072 return n;
12073}
12074
12075static rb_node_regx_t *
12076rb_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)
12077{
12078 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12079 n->string = string;
12080 n->options = options & RE_OPTION_MASK;
12081 n->opening_loc = *opening_loc;
12082 n->content_loc = *content_loc;
12083 n->closing_loc = *closing_loc;
12084
12085 return n;
12086}
12087
12088static rb_node_call_t *
12089rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12090{
12091 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12092 n->nd_recv = nd_recv;
12093 n->nd_mid = nd_mid;
12094 n->nd_args = nd_args;
12095
12096 return n;
12097}
12098
12099static rb_node_opcall_t *
12100rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12101{
12102 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12103 n->nd_recv = nd_recv;
12104 n->nd_mid = nd_mid;
12105 n->nd_args = nd_args;
12106
12107 return n;
12108}
12109
12110static rb_node_fcall_t *
12111rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12112{
12113 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12114 n->nd_mid = nd_mid;
12115 n->nd_args = nd_args;
12116
12117 return n;
12118}
12119
12120static rb_node_qcall_t *
12121rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12122{
12123 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12124 n->nd_recv = nd_recv;
12125 n->nd_mid = nd_mid;
12126 n->nd_args = nd_args;
12127
12128 return n;
12129}
12130
12131static rb_node_vcall_t *
12132rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12133{
12134 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12135 n->nd_mid = nd_mid;
12136
12137 return n;
12138}
12139
12140static rb_node_once_t *
12141rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12142{
12143 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12144 n->nd_body = nd_body;
12145
12146 return n;
12147}
12148
12149static rb_node_args_t *
12150rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12151{
12152 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12153 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12154
12155 return n;
12156}
12157
12158static rb_node_args_aux_t *
12159rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12160{
12161 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12162 n->nd_pid = nd_pid;
12163 n->nd_plen = nd_plen;
12164 n->nd_next = 0;
12165
12166 return n;
12167}
12168
12169static rb_node_opt_arg_t *
12170rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12171{
12172 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12173 n->nd_body = nd_body;
12174 n->nd_next = 0;
12175
12176 return n;
12177}
12178
12179static rb_node_kw_arg_t *
12180rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12181{
12182 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12183 n->nd_body = nd_body;
12184 n->nd_next = 0;
12185
12186 return n;
12187}
12188
12189static rb_node_postarg_t *
12190rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12191{
12192 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12193 n->nd_1st = nd_1st;
12194 n->nd_2nd = nd_2nd;
12195
12196 return n;
12197}
12198
12199static rb_node_argscat_t *
12200rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12201{
12202 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12203 n->nd_head = nd_head;
12204 n->nd_body = nd_body;
12205
12206 return n;
12207}
12208
12209static rb_node_argspush_t *
12210rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12211{
12212 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12213 n->nd_head = nd_head;
12214 n->nd_body = nd_body;
12215
12216 return n;
12217}
12218
12219static rb_node_splat_t *
12220rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12221{
12222 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12223 n->nd_head = nd_head;
12224 n->operator_loc = *operator_loc;
12225
12226 return n;
12227}
12228
12229static rb_node_block_pass_t *
12230rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12231{
12232 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12233 n->forwarding = 0;
12234 n->nd_head = 0;
12235 n->nd_body = nd_body;
12236 n->operator_loc = *operator_loc;
12237
12238 return n;
12239}
12240
12241static rb_node_alias_t *
12242rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12243{
12244 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12245 n->nd_1st = nd_1st;
12246 n->nd_2nd = nd_2nd;
12247 n->keyword_loc = *keyword_loc;
12248
12249 return n;
12250}
12251
12252static rb_node_valias_t *
12253rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12254{
12255 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12256 n->nd_alias = nd_alias;
12257 n->nd_orig = nd_orig;
12258 n->keyword_loc = *keyword_loc;
12259
12260 return n;
12261}
12262
12263static rb_node_undef_t *
12264rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12265{
12266 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12267 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12268 n->keyword_loc = NULL_LOC;
12269 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12270
12271 return n;
12272}
12273
12274static rb_node_errinfo_t *
12275rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12276{
12277 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12278
12279 return n;
12280}
12281
12282static rb_node_defined_t *
12283rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12284{
12285 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12286 n->nd_head = nd_head;
12287 n->keyword_loc = *keyword_loc;
12288
12289 return n;
12290}
12291
12292static rb_node_postexe_t *
12293rb_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)
12294{
12295 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12296 n->nd_body = nd_body;
12297 n->keyword_loc = *keyword_loc;
12298 n->opening_loc = *opening_loc;
12299 n->closing_loc = *closing_loc;
12300
12301 return n;
12302}
12303
12304static rb_node_attrasgn_t *
12305rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12306{
12307 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12308 n->nd_recv = nd_recv;
12309 n->nd_mid = nd_mid;
12310 n->nd_args = nd_args;
12311
12312 return n;
12313}
12314
12315static rb_node_aryptn_t *
12316rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12317{
12318 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12319 n->nd_pconst = 0;
12320 n->pre_args = pre_args;
12321 n->rest_arg = rest_arg;
12322 n->post_args = post_args;
12323
12324 return n;
12325}
12326
12327static rb_node_hshptn_t *
12328rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12329{
12330 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12331 n->nd_pconst = nd_pconst;
12332 n->nd_pkwargs = nd_pkwargs;
12333 n->nd_pkwrestarg = nd_pkwrestarg;
12334
12335 return n;
12336}
12337
12338static rb_node_fndptn_t *
12339rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12340{
12341 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12342 n->nd_pconst = 0;
12343 n->pre_rest_arg = pre_rest_arg;
12344 n->args = args;
12345 n->post_rest_arg = post_rest_arg;
12346
12347 return n;
12348}
12349
12350static rb_node_line_t *
12351rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12352{
12353 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12354
12355 return n;
12356}
12357
12358static rb_node_file_t *
12359rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12360{
12361 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12362 n->path = rb_str_to_parser_string(p, str);
12363
12364 return n;
12365}
12366
12367static rb_node_encoding_t *
12368rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12369{
12370 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12371 n->enc = p->enc;
12372
12373 return n;
12374}
12375
12376static rb_node_cdecl_t *
12377rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12378{
12379 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12380 n->nd_vid = nd_vid;
12381 n->nd_value = nd_value;
12382 n->nd_else = nd_else;
12383 n->shareability = shareability;
12384
12385 return n;
12386}
12387
12388static rb_node_op_cdecl_t *
12389rb_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)
12390{
12391 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12392 n->nd_head = nd_head;
12393 n->nd_value = nd_value;
12394 n->nd_aid = nd_aid;
12395 n->shareability = shareability;
12396
12397 return n;
12398}
12399
12400static rb_node_error_t *
12401rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12402{
12403 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12404
12405 return n;
12406}
12407
12408static rb_node_break_t *
12409rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12410{
12411 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12412 n->nd_stts = nd_stts;
12413 n->nd_chain = 0;
12414 n->keyword_loc = *keyword_loc;
12415
12416 return n;
12417}
12418
12419static rb_node_next_t *
12420rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12421{
12422 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12423 n->nd_stts = nd_stts;
12424 n->nd_chain = 0;
12425 n->keyword_loc = *keyword_loc;
12426
12427 return n;
12428}
12429
12430static rb_node_redo_t *
12431rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12432{
12433 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12434 n->nd_chain = 0;
12435 n->keyword_loc = *keyword_loc;
12436
12437 return n;
12438}
12439
12440static rb_node_def_temp_t *
12441rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12442{
12443 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12444 n->save.numparam_save = 0;
12445 n->save.max_numparam = 0;
12446 n->save.ctxt = p->ctxt;
12447 n->nd_def = 0;
12448 n->nd_mid = 0;
12449
12450 return n;
12451}
12452
12453static rb_node_def_temp_t *
12454def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12455{
12456 n->save.numparam_save = numparam_push(p);
12457 n->save.max_numparam = p->max_numparam;
12458 return n;
12459}
12460
12461#ifndef RIPPER
12462static enum node_type
12463nodetype(NODE *node) /* for debug */
12464{
12465 return (enum node_type)nd_type(node);
12466}
12467
12468static int
12469nodeline(NODE *node)
12470{
12471 return nd_line(node);
12472}
12473#endif
12474
12475static NODE*
12476newline_node(NODE *node)
12477{
12478 if (node) {
12479 node = remove_begin(node);
12480 nd_set_fl_newline(node);
12481 }
12482 return node;
12483}
12484
12485static void
12486fixpos(NODE *node, NODE *orig)
12487{
12488 if (!node) return;
12489 if (!orig) return;
12490 nd_set_line(node, nd_line(orig));
12491}
12492
12493static NODE*
12494block_append(struct parser_params *p, NODE *head, NODE *tail)
12495{
12496 NODE *end, *h = head, *nd;
12497
12498 if (tail == 0) return head;
12499
12500 if (h == 0) return tail;
12501 switch (nd_type(h)) {
12502 default:
12503 h = end = NEW_BLOCK(head, &head->nd_loc);
12504 head = end;
12505 break;
12506 case NODE_BLOCK:
12507 end = RNODE_BLOCK(h)->nd_end;
12508 break;
12509 }
12510
12511 nd = RNODE_BLOCK(end)->nd_head;
12512 switch (nd_type(nd)) {
12513 case NODE_RETURN:
12514 case NODE_BREAK:
12515 case NODE_NEXT:
12516 case NODE_REDO:
12517 case NODE_RETRY:
12518 rb_warning0L(nd_line(tail), "statement not reached");
12519 break;
12520
12521 default:
12522 break;
12523 }
12524
12525 if (!nd_type_p(tail, NODE_BLOCK)) {
12526 tail = NEW_BLOCK(tail, &tail->nd_loc);
12527 }
12528 RNODE_BLOCK(end)->nd_next = tail;
12529 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12530 nd_set_last_loc(head, nd_last_loc(tail));
12531 return head;
12532}
12533
12534/* append item to the list */
12535static NODE*
12536list_append(struct parser_params *p, NODE *list, NODE *item)
12537{
12538 NODE *last;
12539
12540 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12541 if (RNODE_LIST(list)->nd_next) {
12542 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12543 }
12544 else {
12545 last = list;
12546 }
12547
12548 RNODE_LIST(list)->as.nd_alen += 1;
12549 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12550 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12551
12552 nd_set_last_loc(list, nd_last_loc(item));
12553
12554 return list;
12555}
12556
12557/* concat two lists */
12558static NODE*
12559list_concat(NODE *head, NODE *tail)
12560{
12561 NODE *last;
12562
12563 if (RNODE_LIST(head)->nd_next) {
12564 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12565 }
12566 else {
12567 last = head;
12568 }
12569
12570 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12571 RNODE_LIST(last)->nd_next = tail;
12572 if (RNODE_LIST(tail)->nd_next) {
12573 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12574 }
12575 else {
12576 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12577 }
12578
12579 nd_set_last_loc(head, nd_last_loc(tail));
12580
12581 return head;
12582}
12583
12584static int
12585literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12586{
12587 if (!tail) return 1;
12588 if (!rb_parser_enc_compatible(p, head, tail)) {
12589 compile_error(p, "string literal encodings differ (%s / %s)",
12590 rb_enc_name(rb_parser_str_get_encoding(head)),
12591 rb_enc_name(rb_parser_str_get_encoding(tail)));
12592 rb_parser_str_resize(p, head, 0);
12593 rb_parser_str_resize(p, tail, 0);
12594 return 0;
12595 }
12596 rb_parser_str_buf_append(p, head, tail);
12597 return 1;
12598}
12599
12600static rb_parser_string_t *
12601string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12602{
12603 if (htype != NODE_DSTR) return NULL;
12604 if (RNODE_DSTR(head)->nd_next) {
12605 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12606 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12607 }
12608 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12609 ASSUME(lit);
12610 return lit;
12611}
12612
12613#ifndef RIPPER
12614static rb_parser_string_t *
12615rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12616{
12617 rb_parser_string_t *copy;
12618 if (!orig) return NULL;
12619 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12620 copy->coderange = orig->coderange;
12621 copy->enc = orig->enc;
12622 return copy;
12623}
12624#endif
12625
12626/* concat two string literals */
12627static NODE *
12628literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12629{
12630 enum node_type htype;
12631 rb_parser_string_t *lit;
12632
12633 if (!head) return tail;
12634 if (!tail) return head;
12635
12636 htype = nd_type(head);
12637 if (htype == NODE_EVSTR) {
12638 head = new_dstr(p, head, loc);
12639 htype = NODE_DSTR;
12640 }
12641 if (p->heredoc_indent > 0) {
12642 switch (htype) {
12643 case NODE_STR:
12644 head = str2dstr(p, head);
12645 case NODE_DSTR:
12646 return list_append(p, head, tail);
12647 default:
12648 break;
12649 }
12650 }
12651 switch (nd_type(tail)) {
12652 case NODE_STR:
12653 if ((lit = string_literal_head(p, htype, head)) != false) {
12654 htype = NODE_STR;
12655 }
12656 else {
12657 lit = RNODE_DSTR(head)->string;
12658 }
12659 if (htype == NODE_STR) {
12660 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12661 error:
12662 rb_discard_node(p, head);
12663 rb_discard_node(p, tail);
12664 return 0;
12665 }
12666 rb_discard_node(p, tail);
12667 }
12668 else {
12669 list_append(p, head, tail);
12670 }
12671 break;
12672
12673 case NODE_DSTR:
12674 if (htype == NODE_STR) {
12675 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12676 goto error;
12677 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12678 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12679 RNODE_STR(head)->string = NULL;
12680 rb_discard_node(p, head);
12681 head = tail;
12682 }
12683 else if (!RNODE_DSTR(tail)->string) {
12684 append:
12685 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12686 if (!RNODE_DSTR(head)->nd_next) {
12687 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12688 }
12689 else if (RNODE_DSTR(tail)->nd_next) {
12690 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12691 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12692 }
12693 rb_discard_node(p, tail);
12694 }
12695 else if ((lit = string_literal_head(p, htype, head)) != false) {
12696 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12697 goto error;
12698 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12699 RNODE_DSTR(tail)->string = 0;
12700 goto append;
12701 }
12702 else {
12703 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));
12704 RNODE_DSTR(tail)->string = 0;
12705 }
12706 break;
12707
12708 case NODE_EVSTR:
12709 if (htype == NODE_STR) {
12710 head = str2dstr(p, head);
12711 RNODE_DSTR(head)->as.nd_alen = 1;
12712 }
12713 list_append(p, head, tail);
12714 break;
12715 }
12716 return head;
12717}
12718
12719static void
12720nd_copy_flag(NODE *new_node, NODE *old_node)
12721{
12722 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12723 nd_set_line(new_node, nd_line(old_node));
12724 new_node->nd_loc = old_node->nd_loc;
12725 new_node->node_id = old_node->node_id;
12726}
12727
12728static NODE *
12729str2dstr(struct parser_params *p, NODE *node)
12730{
12731 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12732 nd_copy_flag(new_node, node);
12733 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12734 RNODE_DSTR(new_node)->as.nd_alen = 0;
12735 RNODE_DSTR(new_node)->nd_next = 0;
12736 RNODE_STR(node)->string = 0;
12737
12738 return new_node;
12739}
12740
12741static NODE *
12742str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12743{
12744 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12745 nd_copy_flag(new_node, node);
12746 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12747 RNODE_REGX(new_node)->options = options;
12748 nd_set_loc(new_node, loc);
12749 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12750 RNODE_REGX(new_node)->content_loc = *content_loc;
12751 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12752 RNODE_STR(node)->string = 0;
12753
12754 return new_node;
12755}
12756
12757static NODE *
12758evstr2dstr(struct parser_params *p, NODE *node)
12759{
12760 if (nd_type_p(node, NODE_EVSTR)) {
12761 node = new_dstr(p, node, &node->nd_loc);
12762 }
12763 return node;
12764}
12765
12766static NODE *
12767new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12768{
12769 NODE *head = node;
12770
12771 if (node) {
12772 switch (nd_type(node)) {
12773 case NODE_STR:
12774 return str2dstr(p, node);
12775 case NODE_DSTR:
12776 break;
12777 case NODE_EVSTR:
12778 return node;
12779 }
12780 }
12781 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12782}
12783
12784static NODE *
12785new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12786{
12787 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12788 return list_append(p, dstr, node);
12789}
12790
12791static NODE *
12792call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12793 const YYLTYPE *op_loc, const YYLTYPE *loc)
12794{
12795 NODE *expr;
12796 value_expr(p, recv);
12797 value_expr(p, arg1);
12798 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12799 nd_set_line(expr, op_loc->beg_pos.lineno);
12800 return expr;
12801}
12802
12803static NODE *
12804call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12805{
12806 NODE *opcall;
12807 value_expr(p, recv);
12808 opcall = NEW_OPCALL(recv, id, 0, loc);
12809 nd_set_line(opcall, op_loc->beg_pos.lineno);
12810 return opcall;
12811}
12812
12813static NODE *
12814new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12815{
12816 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12817 nd_set_line(qcall, op_loc->beg_pos.lineno);
12818 return qcall;
12819}
12820
12821static NODE*
12822new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12823{
12824 NODE *ret;
12825 if (block) block_dup_check(p, args, block);
12826 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12827 if (block) ret = method_add_block(p, ret, block, loc);
12828 fixpos(ret, recv);
12829 return ret;
12830}
12831
12832static rb_locations_lambda_body_t*
12833new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12834{
12835 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12836 body->node = node;
12837 body->opening_loc = *opening_loc;
12838 body->closing_loc = *closing_loc;
12839 return body;
12840}
12841
12842static NODE *
12843command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12844{
12845 NODE **body;
12846 enum node_type type = nd_type(m);
12847 switch (type) {
12848 case NODE_YIELD:
12849 compile_error(p, "block given to yield");
12850 return m;
12851 case NODE_RETURN:
12852 body = &RNODE_RETURN(m)->nd_stts;
12853 break;
12854 case NODE_BREAK:
12855 case NODE_NEXT:
12856 body = &RNODE_EXITS(m)->nd_stts;
12857 break;
12858 case NODE_REDO: /* `redo` has no argument */
12859 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12860 return m;
12861 case NODE_RETRY: /* `retry` has no argument */
12862 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12863 return m;
12864 default:
12865 block_dup_check(p, get_nd_args(p, m), b);
12866 return method_add_block(p, m, b, loc);
12867 }
12868 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12869 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12870 *body = command_add_block(p, *body, b, &b_loc);
12871 m->nd_loc.end_pos = loc->end_pos;
12872 return m;
12873}
12874
12875#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12876
12877static NODE*
12878last_expr_once_body(NODE *node)
12879{
12880 if (!node) return 0;
12881 return nd_once_body(node);
12882}
12883
12884static NODE*
12885match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12886{
12887 NODE *n;
12888 int line = op_loc->beg_pos.lineno;
12889
12890 value_expr(p, node1);
12891 value_expr(p, node2);
12892
12893 if ((n = last_expr_once_body(node1)) != 0) {
12894 switch (nd_type(n)) {
12895 case NODE_DREGX:
12896 {
12897 NODE *match = NEW_MATCH2(node1, node2, loc);
12898 nd_set_line(match, line);
12899 return match;
12900 }
12901
12902 case NODE_REGX:
12903 {
12904 const VALUE lit = rb_node_regx_string_val(n);
12905 if (!NIL_P(lit)) {
12906 NODE *match = NEW_MATCH2(node1, node2, loc);
12907 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12908 nd_set_line(match, line);
12909 return match;
12910 }
12911 }
12912 }
12913 }
12914
12915 if ((n = last_expr_once_body(node2)) != 0) {
12916 NODE *match3;
12917
12918 switch (nd_type(n)) {
12919 case NODE_DREGX:
12920 match3 = NEW_MATCH3(node2, node1, loc);
12921 return match3;
12922 }
12923 }
12924
12925 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12926 nd_set_line(n, line);
12927 return n;
12928}
12929
12930# if WARN_PAST_SCOPE
12931static int
12932past_dvar_p(struct parser_params *p, ID id)
12933{
12934 struct vtable *past = p->lvtbl->past;
12935 while (past) {
12936 if (vtable_included(past, id)) return 1;
12937 past = past->prev;
12938 }
12939 return 0;
12940}
12941# endif
12942
12943static int
12944numparam_nested_p(struct parser_params *p)
12945{
12946 struct local_vars *local = p->lvtbl;
12947 NODE *outer = local->numparam.outer;
12948 NODE *inner = local->numparam.inner;
12949 if (outer || inner) {
12950 NODE *used = outer ? outer : inner;
12951 compile_error(p, "numbered parameter is already used in %s block\n"
12952 "%s:%d: numbered parameter is already used here",
12953 outer ? "outer" : "inner",
12954 p->ruby_sourcefile, nd_line(used));
12955 parser_show_error_line(p, &used->nd_loc);
12956 return 1;
12957 }
12958 return 0;
12959}
12960
12961static int
12962numparam_used_p(struct parser_params *p)
12963{
12964 NODE *numparam = p->lvtbl->numparam.current;
12965 if (numparam) {
12966 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12967 "%s:%d: numbered parameter is already used here",
12968 p->ruby_sourcefile, nd_line(numparam));
12969 parser_show_error_line(p, &numparam->nd_loc);
12970 return 1;
12971 }
12972 return 0;
12973}
12974
12975static int
12976it_used_p(struct parser_params *p)
12977{
12978 NODE *it = p->lvtbl->it;
12979 if (it) {
12980 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12981 "%s:%d: 'it' is already used here",
12982 p->ruby_sourcefile, nd_line(it));
12983 parser_show_error_line(p, &it->nd_loc);
12984 return 1;
12985 }
12986 return 0;
12987}
12988
12989static NODE*
12990gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12991{
12992 ID *vidp = NULL;
12993 NODE *node;
12994 switch (id) {
12995 case keyword_self:
12996 return NEW_SELF(loc);
12997 case keyword_nil:
12998 return NEW_NIL(loc);
12999 case keyword_true:
13000 return NEW_TRUE(loc);
13001 case keyword_false:
13002 return NEW_FALSE(loc);
13003 case keyword__FILE__:
13004 {
13005 VALUE file = p->ruby_sourcefile_string;
13006 if (NIL_P(file))
13007 file = rb_str_new(0, 0);
13008 node = NEW_FILE(file, loc);
13009 }
13010 return node;
13011 case keyword__LINE__:
13012 return NEW_LINE(loc);
13013 case keyword__ENCODING__:
13014 return NEW_ENCODING(loc);
13015
13016 }
13017 switch (id_type(id)) {
13018 case ID_LOCAL:
13019 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13020 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13021 if (vidp) *vidp |= LVAR_USED;
13022 node = NEW_DVAR(id, loc);
13023 return node;
13024 }
13025 if (local_id_ref(p, id, &vidp)) {
13026 if (vidp) *vidp |= LVAR_USED;
13027 node = NEW_LVAR(id, loc);
13028 return node;
13029 }
13030 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13031 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13032 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13033 node = NEW_DVAR(id, loc);
13034 struct local_vars *local = p->lvtbl;
13035 if (!local->numparam.current) local->numparam.current = node;
13036 return node;
13037 }
13038# if WARN_PAST_SCOPE
13039 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13040 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13041 }
13042# endif
13043 /* method call without arguments */
13044 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13045 if (numparam_used_p(p)) return 0;
13046 if (p->max_numparam == ORDINAL_PARAM) {
13047 compile_error(p, "ordinary parameter is defined");
13048 return 0;
13049 }
13050 if (!p->it_id) {
13051 p->it_id = idItImplicit;
13052 vtable_add(p->lvtbl->args, p->it_id);
13053 }
13054 NODE *node = NEW_DVAR(p->it_id, loc);
13055 if (!p->lvtbl->it) p->lvtbl->it = node;
13056 return node;
13057 }
13058 return NEW_VCALL(id, loc);
13059 case ID_GLOBAL:
13060 return NEW_GVAR(id, loc);
13061 case ID_INSTANCE:
13062 return NEW_IVAR(id, loc);
13063 case ID_CONST:
13064 return NEW_CONST(id, loc);
13065 case ID_CLASS:
13066 return NEW_CVAR(id, loc);
13067 }
13068 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13069 return 0;
13070}
13071
13072static rb_node_opt_arg_t *
13073opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13074{
13075 rb_node_opt_arg_t *opts = opt_list;
13076 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13077
13078 while (opts->nd_next) {
13079 opts = opts->nd_next;
13080 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13081 }
13082 opts->nd_next = opt;
13083
13084 return opt_list;
13085}
13086
13087static rb_node_kw_arg_t *
13088kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13089{
13090 if (kwlist) {
13091 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13092 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13093 }
13094 return kwlist;
13095}
13096
13097static NODE *
13098new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13099{
13100 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13101 p->ctxt.has_trailing_semicolon = 0;
13102
13103 NODE *n = expr;
13104 while (n) {
13105 if (nd_type_p(n, NODE_BEGIN)) {
13106 n = RNODE_BEGIN(n)->nd_body;
13107 }
13108 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13109 n = RNODE_BLOCK(n)->nd_head;
13110 }
13111 else {
13112 break;
13113 }
13114 }
13115
13116 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13117 NODE *block = NEW_BLOCK(expr, loc);
13118 return NEW_DEFINED(block, loc, keyword_loc);
13119 }
13120
13121 return NEW_DEFINED(n, loc, keyword_loc);
13122}
13123
13124static NODE*
13125str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13126{
13127 VALUE lit;
13128 rb_parser_string_t *str = RNODE_STR(node)->string;
13129 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13130 yyerror1(loc, "invalid symbol");
13131 lit = STR_NEW0();
13132 }
13133 else {
13134 lit = rb_str_new_parser_string(str);
13135 }
13136 return NEW_SYM(lit, loc);
13137}
13138
13139static NODE*
13140symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13141{
13142 enum node_type type = nd_type(symbol);
13143 switch (type) {
13144 case NODE_DSTR:
13145 nd_set_type(symbol, NODE_DSYM);
13146 break;
13147 case NODE_STR:
13148 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13149 break;
13150 default:
13151 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13152 }
13153 return list_append(p, symbols, symbol);
13154}
13155
13156static void
13157dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13158{
13159 if (dreg->string) {
13160 reg_fragment_setenc(p, dreg->string, options);
13161 }
13162 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13163 NODE *frag = list->nd_head;
13164 if (nd_type_p(frag, NODE_STR)) {
13165 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13166 }
13167 else if (nd_type_p(frag, NODE_DSTR)) {
13168 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13169 }
13170 }
13171}
13172
13173static NODE *
13174new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13175{
13176 if (!node) {
13177 /* Check string is valid regex */
13178 rb_parser_string_t *str = STRING_NEW0();
13179 reg_compile(p, str, options);
13180 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13181 return node;
13182 }
13183 switch (nd_type(node)) {
13184 case NODE_STR:
13185 {
13186 /* Check string is valid regex */
13187 reg_compile(p, RNODE_STR(node)->string, options);
13188 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13189 }
13190 break;
13191 default:
13192 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13193 /* fall through */
13194 case NODE_DSTR:
13195 nd_set_type(node, NODE_DREGX);
13196 nd_set_loc(node, loc);
13197 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13198 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13199 if (dreg->nd_next) {
13200 dregex_fragment_setenc(p, dreg, options);
13201 }
13202 if (options & RE_OPTION_ONCE) {
13203 node = NEW_ONCE(node, loc);
13204 }
13205 break;
13206 }
13207 return node;
13208}
13209
13210static rb_node_kw_arg_t *
13211new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13212{
13213 if (!k) return 0;
13214 return NEW_KW_ARG((k), loc);
13215}
13216
13217static NODE *
13218new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13219{
13220 if (!node) {
13221 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13222 return xstr;
13223 }
13224 switch (nd_type(node)) {
13225 case NODE_STR:
13226 nd_set_type(node, NODE_XSTR);
13227 nd_set_loc(node, loc);
13228 break;
13229 case NODE_DSTR:
13230 nd_set_type(node, NODE_DXSTR);
13231 nd_set_loc(node, loc);
13232 break;
13233 default:
13234 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13235 break;
13236 }
13237 return node;
13238}
13239
13240static const
13241struct st_hash_type literal_type = {
13242 literal_cmp,
13243 literal_hash,
13244};
13245
13246static int nd_type_st_key_enable_p(NODE *node);
13247
13248static void
13249check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13250{
13251 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13252 if (!arg || !p->case_labels) return;
13253 if (!nd_type_st_key_enable_p(arg)) return;
13254
13255 if (p->case_labels == CHECK_LITERAL_WHEN) {
13256 p->case_labels = st_init_table(&literal_type);
13257 }
13258 else {
13259 st_data_t line;
13260 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13261 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13262 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13263 return;
13264 }
13265 }
13266 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13267}
13268
13269#ifdef RIPPER
13270static int
13271id_is_var(struct parser_params *p, ID id)
13272{
13273 if (is_notop_id(id)) {
13274 switch (id & ID_SCOPE_MASK) {
13275 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13276 return 1;
13277 case ID_LOCAL:
13278 if (dyna_in_block(p)) {
13279 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13280 }
13281 if (local_id(p, id)) return 1;
13282 /* method call without arguments */
13283 return 0;
13284 }
13285 }
13286 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13287 return 0;
13288}
13289#endif
13290
13291static inline enum lex_state_e
13292parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13293{
13294 if (p->debug) {
13295 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13296 }
13297 return p->lex.state = ls;
13298}
13299
13300#ifndef RIPPER
13301static void
13302flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13303{
13304 VALUE mesg = p->debug_buffer;
13305
13306 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13307 p->debug_buffer = Qnil;
13308 rb_io_puts(1, &mesg, out);
13309 }
13310 if (!NIL_P(str) && RSTRING_LEN(str)) {
13311 rb_io_write(p->debug_output, str);
13312 }
13313}
13314
13315static const char rb_parser_lex_state_names[][8] = {
13316 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13317 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13318 "LABEL", "LABELED","FITEM",
13319};
13320
13321static VALUE
13322append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13323{
13324 int i, sep = 0;
13325 unsigned int mask = 1;
13326 static const char none[] = "NONE";
13327
13328 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13329 if ((unsigned)state & mask) {
13330 if (sep) {
13331 rb_str_cat(buf, "|", 1);
13332 }
13333 sep = 1;
13334 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13335 }
13336 }
13337 if (!sep) {
13338 rb_str_cat(buf, none, sizeof(none)-1);
13339 }
13340 return buf;
13341}
13342
13343enum lex_state_e
13344rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13345 enum lex_state_e to, int line)
13346{
13347 VALUE mesg;
13348 mesg = rb_str_new_cstr("lex_state: ");
13349 append_lex_state_name(p, from, mesg);
13350 rb_str_cat_cstr(mesg, " -> ");
13351 append_lex_state_name(p, to, mesg);
13352 rb_str_catf(mesg, " at line %d\n", line);
13353 flush_debug_buffer(p, p->debug_output, mesg);
13354 return to;
13355}
13356
13357VALUE
13358rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13359{
13360 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13361}
13362
13363static void
13364append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13365{
13366 if (stack == 0) {
13367 rb_str_cat_cstr(mesg, "0");
13368 }
13369 else {
13370 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13371 for (; mask && !(stack & mask); mask >>= 1) continue;
13372 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13373 }
13374}
13375
13376void
13377rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13378 const char *name, int line)
13379{
13380 VALUE mesg = rb_sprintf("%s: ", name);
13381 append_bitstack_value(p, stack, mesg);
13382 rb_str_catf(mesg, " at line %d\n", line);
13383 flush_debug_buffer(p, p->debug_output, mesg);
13384}
13385
13386void
13387rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13388{
13389 va_list ap;
13390 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13391
13392 va_start(ap, fmt);
13393 rb_str_vcatf(mesg, fmt, ap);
13394 va_end(ap);
13395 yyerror0(RSTRING_PTR(mesg));
13396 RB_GC_GUARD(mesg);
13397
13398 mesg = rb_str_new(0, 0);
13399 append_lex_state_name(p, p->lex.state, mesg);
13400 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13401 rb_str_resize(mesg, 0);
13402 append_bitstack_value(p, p->cond_stack, mesg);
13403 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13404 rb_str_resize(mesg, 0);
13405 append_bitstack_value(p, p->cmdarg_stack, mesg);
13406 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13407 if (p->debug_output == rb_ractor_stdout())
13408 p->debug_output = rb_ractor_stderr();
13409 p->debug = TRUE;
13410}
13411
13412static YYLTYPE *
13413rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13414{
13415 yylloc->beg_pos.lineno = sourceline;
13416 yylloc->beg_pos.column = beg_pos;
13417 yylloc->end_pos.lineno = sourceline;
13418 yylloc->end_pos.column = end_pos;
13419 return yylloc;
13420}
13421
13422YYLTYPE *
13423rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13424{
13425 int sourceline = here->sourceline;
13426 int beg_pos = (int)here->offset - here->quote
13427 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13428 int end_pos = (int)here->offset + here->length + here->quote;
13429
13430 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13431}
13432
13433YYLTYPE *
13434rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13435{
13436 yylloc->beg_pos.lineno = p->delayed.beg_line;
13437 yylloc->beg_pos.column = p->delayed.beg_col;
13438 yylloc->end_pos.lineno = p->delayed.end_line;
13439 yylloc->end_pos.column = p->delayed.end_col;
13440
13441 return yylloc;
13442}
13443
13444YYLTYPE *
13445rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13446{
13447 int sourceline = p->ruby_sourceline;
13448 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13449 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13450 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13451}
13452
13453YYLTYPE *
13454rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13455{
13456 yylloc->end_pos = yylloc->beg_pos;
13457
13458 return yylloc;
13459}
13460
13461YYLTYPE *
13462rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13463{
13464 int sourceline = p->ruby_sourceline;
13465 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13466 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13467 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13468}
13469
13470YYLTYPE *
13471rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13472{
13473 int sourceline = p->ruby_sourceline;
13474 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13475 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13476 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13477}
13478#endif /* !RIPPER */
13479
13480static int
13481assignable0(struct parser_params *p, ID id, const char **err)
13482{
13483 if (!id) return -1;
13484 switch (id) {
13485 case keyword_self:
13486 *err = "Can't change the value of self";
13487 return -1;
13488 case keyword_nil:
13489 *err = "Can't assign to nil";
13490 return -1;
13491 case keyword_true:
13492 *err = "Can't assign to true";
13493 return -1;
13494 case keyword_false:
13495 *err = "Can't assign to false";
13496 return -1;
13497 case keyword__FILE__:
13498 *err = "Can't assign to __FILE__";
13499 return -1;
13500 case keyword__LINE__:
13501 *err = "Can't assign to __LINE__";
13502 return -1;
13503 case keyword__ENCODING__:
13504 *err = "Can't assign to __ENCODING__";
13505 return -1;
13506 }
13507 switch (id_type(id)) {
13508 case ID_LOCAL:
13509 if (dyna_in_block(p)) {
13510 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13511 compile_error(p, "Can't assign to numbered parameter _%d",
13512 NUMPARAM_ID_TO_IDX(id));
13513 return -1;
13514 }
13515 if (dvar_curr(p, id)) return NODE_DASGN;
13516 if (dvar_defined(p, id)) return NODE_DASGN;
13517 if (local_id(p, id)) return NODE_LASGN;
13518 dyna_var(p, id);
13519 return NODE_DASGN;
13520 }
13521 else {
13522 if (!local_id(p, id)) local_var(p, id);
13523 return NODE_LASGN;
13524 }
13525 break;
13526 case ID_GLOBAL: return NODE_GASGN;
13527 case ID_INSTANCE: return NODE_IASGN;
13528 case ID_CONST:
13529 if (!p->ctxt.in_def) return NODE_CDECL;
13530 *err = "dynamic constant assignment";
13531 return -1;
13532 case ID_CLASS: return NODE_CVASGN;
13533 default:
13534 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13535 }
13536 return -1;
13537}
13538
13539static NODE*
13540assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13541{
13542 const char *err = 0;
13543 int node_type = assignable0(p, id, &err);
13544 switch (node_type) {
13545 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13546 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13547 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13548 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13549 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13550 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13551 }
13552/* TODO: FIXME */
13553#ifndef RIPPER
13554 if (err) yyerror1(loc, err);
13555#else
13556 if (err) set_value(assign_error(p, err, p->s_lvalue));
13557#endif
13558 return NEW_ERROR(loc);
13559}
13560
13561static int
13562is_private_local_id(struct parser_params *p, ID name)
13563{
13564 VALUE s;
13565 if (name == idUScore) return 1;
13566 if (!is_local_id(name)) return 0;
13567 s = rb_id2str(name);
13568 if (!s) return 0;
13569 return RSTRING_PTR(s)[0] == '_';
13570}
13571
13572static int
13573shadowing_lvar_0(struct parser_params *p, ID name)
13574{
13575 if (dyna_in_block(p)) {
13576 if (dvar_curr(p, name)) {
13577 if (is_private_local_id(p, name)) return 1;
13578 yyerror0("duplicated argument name");
13579 }
13580 else if (dvar_defined(p, name) || local_id(p, name)) {
13581 vtable_add(p->lvtbl->vars, name);
13582 if (p->lvtbl->used) {
13583 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13584 }
13585 return 0;
13586 }
13587 }
13588 else {
13589 if (local_id(p, name)) {
13590 if (is_private_local_id(p, name)) return 1;
13591 yyerror0("duplicated argument name");
13592 }
13593 }
13594 return 1;
13595}
13596
13597static ID
13598shadowing_lvar(struct parser_params *p, ID name)
13599{
13600 shadowing_lvar_0(p, name);
13601 return name;
13602}
13603
13604static void
13605new_bv(struct parser_params *p, ID name)
13606{
13607 if (!name) return;
13608 if (!is_local_id(name)) {
13609 compile_error(p, "invalid local variable - %"PRIsVALUE,
13610 rb_id2str(name));
13611 return;
13612 }
13613 if (!shadowing_lvar_0(p, name)) return;
13614 dyna_var(p, name);
13615 ID *vidp = 0;
13616 if (dvar_defined_ref(p, name, &vidp)) {
13617 if (vidp) *vidp |= LVAR_USED;
13618 }
13619}
13620
13621static void
13622aryset_check(struct parser_params *p, NODE *args)
13623{
13624 NODE *block = 0, *kwds = 0;
13625 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13626 block = RNODE_BLOCK_PASS(args)->nd_body;
13627 args = RNODE_BLOCK_PASS(args)->nd_head;
13628 }
13629 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13630 args = RNODE_ARGSCAT(args)->nd_body;
13631 }
13632 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13633 kwds = RNODE_ARGSPUSH(args)->nd_body;
13634 }
13635 else {
13636 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13637 next = RNODE_LIST(next)->nd_next) {
13638 kwds = RNODE_LIST(next)->nd_head;
13639 }
13640 }
13641 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13642 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13643 }
13644 if (block) {
13645 yyerror1(&block->nd_loc, "block arg given in index assignment");
13646 }
13647}
13648
13649static NODE *
13650aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13651{
13652 aryset_check(p, idx);
13653 return NEW_ATTRASGN(recv, tASET, idx, loc);
13654}
13655
13656static void
13657block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13658{
13659 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13660 compile_error(p, "both block arg and actual block given");
13661 }
13662}
13663
13664static NODE *
13665attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13666{
13667 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13668 return NEW_ATTRASGN(recv, id, 0, loc);
13669}
13670
13671static VALUE
13672rb_backref_error(struct parser_params *p, NODE *node)
13673{
13674#ifndef RIPPER
13675# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13676#else
13677# define ERR(...) rb_sprintf(__VA_ARGS__)
13678#endif
13679 switch (nd_type(node)) {
13680 case NODE_NTH_REF:
13681 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13682 case NODE_BACK_REF:
13683 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13684 }
13685#undef ERR
13686 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13687}
13688
13689static NODE *
13690arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13691{
13692 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13693 switch (nd_type(node1)) {
13694 case NODE_LIST:
13695 return list_append(p, node1, node2);
13696 case NODE_BLOCK_PASS:
13697 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13698 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13699 return node1;
13700 case NODE_ARGSPUSH:
13701 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13702 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13703 nd_set_type(node1, NODE_ARGSCAT);
13704 return node1;
13705 case NODE_ARGSCAT:
13706 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13707 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13708 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13709 return node1;
13710 }
13711 return NEW_ARGSPUSH(node1, node2, loc);
13712}
13713
13714static NODE *
13715arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13716{
13717 if (!node2) return node1;
13718 switch (nd_type(node1)) {
13719 case NODE_BLOCK_PASS:
13720 if (RNODE_BLOCK_PASS(node1)->nd_head)
13721 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13722 else
13723 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13724 return node1;
13725 case NODE_ARGSPUSH:
13726 if (!nd_type_p(node2, NODE_LIST)) break;
13727 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13728 nd_set_type(node1, NODE_ARGSCAT);
13729 return node1;
13730 case NODE_ARGSCAT:
13731 if (!nd_type_p(node2, NODE_LIST) ||
13732 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13733 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13734 return node1;
13735 }
13736 return NEW_ARGSCAT(node1, node2, loc);
13737}
13738
13739static NODE *
13740last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13741{
13742 NODE *n1;
13743 if ((n1 = splat_array(args)) != 0) {
13744 return list_append(p, n1, last_arg);
13745 }
13746 return arg_append(p, args, last_arg, loc);
13747}
13748
13749static NODE *
13750rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13751{
13752 NODE *n1;
13753 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13754 return list_concat(n1, rest_arg);
13755 }
13756 return arg_concat(p, args, rest_arg, loc);
13757}
13758
13759static NODE *
13760splat_array(NODE* node)
13761{
13762 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13763 if (nd_type_p(node, NODE_LIST)) return node;
13764 return 0;
13765}
13766
13767static void
13768mark_lvar_used(struct parser_params *p, NODE *rhs)
13769{
13770 ID *vidp = NULL;
13771 if (!rhs) return;
13772 switch (nd_type(rhs)) {
13773 case NODE_LASGN:
13774 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13775 if (vidp) *vidp |= LVAR_USED;
13776 }
13777 break;
13778 case NODE_DASGN:
13779 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13780 if (vidp) *vidp |= LVAR_USED;
13781 }
13782 break;
13783#if 0
13784 case NODE_MASGN:
13785 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13786 mark_lvar_used(p, rhs->nd_head);
13787 }
13788 break;
13789#endif
13790 }
13791}
13792
13793static int is_static_content(NODE *node);
13794
13795static NODE *
13796node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13797{
13798 if (!lhs) return 0;
13799
13800 switch (nd_type(lhs)) {
13801 case NODE_CDECL:
13802 case NODE_GASGN:
13803 case NODE_IASGN:
13804 case NODE_LASGN:
13805 case NODE_DASGN:
13806 case NODE_MASGN:
13807 case NODE_CVASGN:
13808 set_nd_value(p, lhs, rhs);
13809 nd_set_loc(lhs, loc);
13810 break;
13811
13812 case NODE_ATTRASGN:
13813 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13814 nd_set_loc(lhs, loc);
13815 break;
13816
13817 default:
13818 /* should not happen */
13819 break;
13820 }
13821
13822 return lhs;
13823}
13824
13825static NODE *
13826value_expr_check(struct parser_params *p, NODE *node)
13827{
13828 NODE *void_node = 0, *vn;
13829
13830 if (!node) {
13831 rb_warning0("empty expression");
13832 }
13833 while (node) {
13834 switch (nd_type(node)) {
13835 case NODE_ENSURE:
13836 vn = RNODE_ENSURE(node)->nd_head;
13837 node = RNODE_ENSURE(node)->nd_ensr;
13838 /* nd_ensr should not be NULL, check it out next */
13839 if (vn && (vn = value_expr_check(p, vn))) {
13840 goto found;
13841 }
13842 break;
13843
13844 case NODE_RESCUE:
13845 /* void only if all children are void */
13846 vn = RNODE_RESCUE(node)->nd_head;
13847 if (!vn || !(vn = value_expr_check(p, vn))) {
13848 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13849 }
13850 if (!void_node) void_node = vn;
13851 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13852 if (!nd_type_p(r, NODE_RESBODY)) {
13853 compile_error(p, "unexpected node");
13854 return NULL;
13855 }
13856 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13857 return NULL;
13858 }
13859 if (!void_node) void_node = vn;
13860 }
13861 node = RNODE_RESCUE(node)->nd_else;
13862 if (!node) return void_node;
13863 break;
13864
13865 case NODE_RETURN:
13866 case NODE_BREAK:
13867 case NODE_NEXT:
13868 case NODE_REDO:
13869 case NODE_RETRY:
13870 goto found;
13871
13872 case NODE_CASE:
13873 case NODE_CASE2:
13874 for (node = RNODE_CASE(node)->nd_body;
13875 node && nd_type_p(node, NODE_WHEN);
13876 node = RNODE_WHEN(node)->nd_next) {
13877 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13878 return NULL;
13879 }
13880 if (!void_node) void_node = vn;
13881 }
13882 break;
13883
13884 case NODE_CASE3:
13885 {
13886 NODE *in = RNODE_CASE3(node)->nd_body;
13887 if (!in || !nd_type_p(in, NODE_IN)) {
13888 compile_error(p, "unexpected node");
13889 return NULL;
13890 }
13891 if (!RNODE_IN(in)->nd_body) {
13892 /* single line pattern matching with "=>" operator */
13893 goto found;
13894 }
13895 do {
13896 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13897 if (!vn) return NULL;
13898 if (!void_node) void_node = vn;
13899 in = RNODE_IN(in)->nd_next;
13900 } while (in && nd_type_p(in, NODE_IN));
13901 node = in; /* else */
13902 }
13903 break;
13904
13905 case NODE_BLOCK:
13906 while (RNODE_BLOCK(node)->nd_next) {
13907 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13908 if (vn) return vn;
13909 node = RNODE_BLOCK(node)->nd_next;
13910 }
13911 node = RNODE_BLOCK(node)->nd_head;
13912 break;
13913
13914 case NODE_BEGIN:
13915 node = RNODE_BEGIN(node)->nd_body;
13916 break;
13917
13918 case NODE_IF:
13919 case NODE_UNLESS:
13920 if (!RNODE_IF(node)->nd_body) {
13921 return NULL;
13922 }
13923 else if (!RNODE_IF(node)->nd_else) {
13924 return NULL;
13925 }
13926 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13927 if (!vn) return NULL;
13928 if (!void_node) void_node = vn;
13929 node = RNODE_IF(node)->nd_else;
13930 break;
13931
13932 case NODE_AND:
13933 case NODE_OR:
13934 /* The left operand was already checked for a value when logop()
13935 * built this node, so stop here instead of re-reporting the same
13936 * void value once per operator in a chain. */
13937 return NULL;
13938
13939 case NODE_LASGN:
13940 case NODE_DASGN:
13941 case NODE_MASGN:
13942 mark_lvar_used(p, node);
13943 return NULL;
13944
13945 default:
13946 return NULL;
13947 }
13948 }
13949
13950 return NULL;
13951
13952 found:
13953 /* return the first found node */
13954 return void_node ? void_node : node;
13955}
13956
13957static int
13958value_expr(struct parser_params *p, NODE *node)
13959{
13960 NODE *void_node = value_expr_check(p, node);
13961 if (void_node) {
13962 yyerror1(&void_node->nd_loc, "void value expression");
13963 /* or "control never reach"? */
13964 return FALSE;
13965 }
13966 return TRUE;
13967}
13968
13969static void
13970void_expr(struct parser_params *p, NODE *node)
13971{
13972 const char *useless = 0;
13973
13974 if (!RTEST(ruby_verbose)) return;
13975
13976 if (!node || !(node = nd_once_body(node))) return;
13977 switch (nd_type(node)) {
13978 case NODE_OPCALL:
13979 switch (RNODE_OPCALL(node)->nd_mid) {
13980 case '+':
13981 case '-':
13982 case '*':
13983 case '/':
13984 case '%':
13985 case tPOW:
13986 case tUPLUS:
13987 case tUMINUS:
13988 case '|':
13989 case '^':
13990 case '&':
13991 case tCMP:
13992 case '>':
13993 case tGEQ:
13994 case '<':
13995 case tLEQ:
13996 case tEQ:
13997 case tNEQ:
13998 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13999 break;
14000 }
14001 break;
14002
14003 case NODE_LVAR:
14004 case NODE_DVAR:
14005 case NODE_GVAR:
14006 case NODE_IVAR:
14007 case NODE_CVAR:
14008 case NODE_NTH_REF:
14009 case NODE_BACK_REF:
14010 useless = "a variable";
14011 break;
14012 case NODE_CONST:
14013 useless = "a constant";
14014 break;
14015 case NODE_SYM:
14016 case NODE_LINE:
14017 case NODE_FILE:
14018 case NODE_ENCODING:
14019 case NODE_INTEGER:
14020 case NODE_FLOAT:
14021 case NODE_RATIONAL:
14022 case NODE_IMAGINARY:
14023 case NODE_STR:
14024 case NODE_DSTR:
14025 case NODE_REGX:
14026 case NODE_DREGX:
14027 useless = "a literal";
14028 break;
14029 case NODE_COLON2:
14030 case NODE_COLON3:
14031 useless = "::";
14032 break;
14033 case NODE_DOT2:
14034 useless = "..";
14035 break;
14036 case NODE_DOT3:
14037 useless = "...";
14038 break;
14039 case NODE_SELF:
14040 useless = "self";
14041 break;
14042 case NODE_NIL:
14043 useless = "nil";
14044 break;
14045 case NODE_TRUE:
14046 useless = "true";
14047 break;
14048 case NODE_FALSE:
14049 useless = "false";
14050 break;
14051 case NODE_DEFINED:
14052 useless = "defined?";
14053 break;
14054 }
14055
14056 if (useless) {
14057 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14058 }
14059}
14060
14061/* warns useless use of block and returns the last statement node */
14062static NODE *
14063void_stmts(struct parser_params *p, NODE *node)
14064{
14065 NODE *const n = node;
14066 if (!RTEST(ruby_verbose)) return n;
14067 if (!node) return n;
14068 if (!nd_type_p(node, NODE_BLOCK)) return n;
14069
14070 while (RNODE_BLOCK(node)->nd_next) {
14071 void_expr(p, RNODE_BLOCK(node)->nd_head);
14072 node = RNODE_BLOCK(node)->nd_next;
14073 }
14074 return RNODE_BLOCK(node)->nd_head;
14075}
14076
14077static NODE *
14078remove_begin(NODE *node)
14079{
14080 NODE **n = &node, *n1 = node;
14081 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14082 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14083 }
14084 return node;
14085}
14086
14087static void
14088reduce_nodes(struct parser_params *p, NODE **body)
14089{
14090 NODE *node = *body;
14091
14092 if (!node) {
14093 *body = NEW_NIL(&NULL_LOC);
14094 return;
14095 }
14096#define subnodes(type, n1, n2) \
14097 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14098 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14099 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14100
14101 while (node) {
14102 int newline = (int)nd_fl_newline(node);
14103 switch (nd_type(node)) {
14104 case NODE_NIL:
14105 // Keep an explicit nil in a method's tail (value) position when it
14106 // is on its own line, so it still emits a :line event and records
14107 // line coverage for that line. [Bug #22302]
14108 if (newline) return;
14109 end:
14110 *body = 0;
14111 return;
14112 case NODE_BEGIN:
14113 *body = node = RNODE_BEGIN(node)->nd_body;
14114 if (newline && node) nd_set_fl_newline(node);
14115 continue;
14116 case NODE_BLOCK:
14117 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14118 break;
14119 case NODE_IF:
14120 case NODE_UNLESS:
14121 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14122 return;
14123 case NODE_CASE:
14124 body = &RNODE_CASE(node)->nd_body;
14125 break;
14126 case NODE_WHEN:
14127 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14128 break;
14129 case NODE_ENSURE:
14130 body = &RNODE_ENSURE(node)->nd_head;
14131 break;
14132 case NODE_RESCUE:
14133 newline = 0; // RESBODY should not be a NEWLINE
14134 if (RNODE_RESCUE(node)->nd_else) {
14135 body = &RNODE_RESCUE(node)->nd_resq;
14136 break;
14137 }
14138 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14139 break;
14140 default:
14141 return;
14142 }
14143 node = *body;
14144 if (newline && node) nd_set_fl_newline(node);
14145 }
14146
14147#undef subnodes
14148}
14149
14150static int
14151is_static_content(NODE *node)
14152{
14153 if (!node) return 1;
14154 switch (nd_type(node)) {
14155 case NODE_HASH:
14156 if (!(node = RNODE_HASH(node)->nd_head)) break;
14157 case NODE_LIST:
14158 do {
14159 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14160 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14161 case NODE_SYM:
14162 case NODE_REGX:
14163 case NODE_LINE:
14164 case NODE_FILE:
14165 case NODE_ENCODING:
14166 case NODE_INTEGER:
14167 case NODE_FLOAT:
14168 case NODE_RATIONAL:
14169 case NODE_IMAGINARY:
14170 case NODE_STR:
14171 case NODE_NIL:
14172 case NODE_TRUE:
14173 case NODE_FALSE:
14174 case NODE_ZLIST:
14175 break;
14176 default:
14177 return 0;
14178 }
14179 return 1;
14180}
14181
14182static int
14183assign_in_cond(struct parser_params *p, NODE *node)
14184{
14185 switch (nd_type(node)) {
14186 case NODE_MASGN:
14187 case NODE_LASGN:
14188 case NODE_DASGN:
14189 case NODE_GASGN:
14190 case NODE_IASGN:
14191 case NODE_CVASGN:
14192 case NODE_CDECL:
14193 break;
14194
14195 default:
14196 return 0;
14197 }
14198
14199 if (!get_nd_value(p, node)) return 1;
14200 if (is_static_content(get_nd_value(p, node))) {
14201 /* reports always */
14202 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14203 }
14204 return 1;
14205}
14206
14207enum cond_type {
14208 COND_IN_OP,
14209 COND_IN_COND,
14210 COND_IN_FF
14211};
14212
14213#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14214 switch (t) { \
14215 case COND_IN_OP: break; \
14216 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14217 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14218 } \
14219} while (0)
14220
14221static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14222
14223static NODE*
14224range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14225{
14226 enum node_type type;
14227
14228 if (node == 0) return 0;
14229
14230 type = nd_type(node);
14231 value_expr(p, node);
14232 if (type == NODE_INTEGER) {
14233 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14234 ID lineno = rb_intern("$.");
14235 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14236 }
14237 return cond0(p, node, COND_IN_FF, loc, true);
14238}
14239
14240static NODE*
14241cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14242{
14243 if (node == 0) return 0;
14244 if (!(node = nd_once_body(node))) return 0;
14245 assign_in_cond(p, node);
14246
14247 switch (nd_type(node)) {
14248 case NODE_BEGIN:
14249 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14250 break;
14251
14252 case NODE_DSTR:
14253 case NODE_EVSTR:
14254 case NODE_STR:
14255 case NODE_FILE:
14256 SWITCH_BY_COND_TYPE(type, warn, "string ");
14257 break;
14258
14259 case NODE_REGX:
14260 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14261 nd_set_type(node, NODE_MATCH);
14262 break;
14263
14264 case NODE_DREGX:
14265 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14266
14267 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14268
14269 case NODE_BLOCK:
14270 {
14271 NODE *end = RNODE_BLOCK(node)->nd_end;
14272 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14273 if (top) top = node == end;
14274 *expr = cond0(p, *expr, type, loc, top);
14275 }
14276 break;
14277
14278 case NODE_AND:
14279 case NODE_OR:
14280 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14281 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14282 break;
14283
14284 case NODE_DOT2:
14285 case NODE_DOT3:
14286 if (!top) break;
14287 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14288 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14289 switch (nd_type(node)) {
14290 case NODE_DOT2:
14291 nd_set_type(node,NODE_FLIP2);
14292 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14293 (void)flip2;
14294 break;
14295 case NODE_DOT3:
14296 nd_set_type(node, NODE_FLIP3);
14297 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14298 (void)flip3;
14299 break;
14300 }
14301 break;
14302
14303 case NODE_SYM:
14304 case NODE_DSYM:
14305 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14306 break;
14307
14308 case NODE_LINE:
14309 case NODE_ENCODING:
14310 case NODE_INTEGER:
14311 case NODE_FLOAT:
14312 case NODE_RATIONAL:
14313 case NODE_IMAGINARY:
14314 SWITCH_BY_COND_TYPE(type, warning, "");
14315 break;
14316
14317 default:
14318 break;
14319 }
14320 return node;
14321}
14322
14323static NODE*
14324cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14325{
14326 if (node == 0) return 0;
14327 return cond0(p, node, COND_IN_COND, loc, true);
14328}
14329
14330static NODE*
14331method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14332{
14333 if (node == 0) return 0;
14334 return cond0(p, node, COND_IN_OP, loc, true);
14335}
14336
14337static NODE*
14338new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14339{
14340 YYLTYPE loc = {*pos, *pos};
14341 return NEW_NIL(&loc);
14342}
14343
14344static NODE*
14345new_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)
14346{
14347 if (!cc) return right;
14348 cc = cond0(p, cc, COND_IN_COND, loc, true);
14349 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14350}
14351
14352static NODE*
14353new_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)
14354{
14355 if (!cc) return right;
14356 cc = cond0(p, cc, COND_IN_COND, loc, true);
14357 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14358}
14359
14360#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))
14361
14362/* A cached logop tail is usable only if it is still a node of the chain's type
14363 * whose nd_2nd is the insertion point, i.e. not itself another node of that
14364 * type. This rejects a stale cache left over from an earlier parse. */
14365static int
14366logop_valid_tail(NODE *tail, enum node_type type)
14367{
14368 NODE *second;
14369 return tail && nd_type_p(tail, type) &&
14370 ((second = RNODE_AND(tail)->nd_2nd) == 0 || !nd_type_p(second, type));
14371}
14372
14373static NODE*
14374logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14375 const YYLTYPE *op_loc, const YYLTYPE *loc)
14376{
14377 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14378 NODE *op;
14379 /* Snapshot the cache from the previous logop() call before overwriting it. */
14380 NODE *prev_head = p->logop.head;
14381 NODE *prev_tail = p->logop.tail;
14382 NODE *node, *second;
14383 value_expr(p, left);
14384 if (left && nd_type_p(left, type)) {
14385 /* The insertion point is the far end of left's right branch.
14386 * Reusing the cached tail keeps a chain such as `a && a && ... && a`
14387 * linear instead of rescanning the whole branch on every operator. The
14388 * cache is validated (its nd_2nd is not another node of the same type)
14389 * so a stale entry falls back to the scan. */
14390 if (left == prev_head && logop_valid_tail(prev_tail, type)) {
14391 node = prev_tail;
14392 }
14393 else {
14394 node = left;
14395 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14396 node = second;
14397 }
14398 }
14399 second = RNODE_AND(node)->nd_2nd;
14400 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14401 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14402 left->nd_loc.end_pos = loc->end_pos;
14403 p->logop.head = left;
14404 p->logop.tail = RNODE_AND(node)->nd_2nd;
14405 return left;
14406 }
14407 op = NEW_AND_OR(type, left, right, loc, op_loc);
14408 nd_set_line(op, op_loc->beg_pos.lineno);
14409 /* Record where the next operator will extend op, mirroring the scan the
14410 * chained branch above would perform: if right is itself a chain of the
14411 * same type (from parentheses), its far end; otherwise op itself. */
14412 p->logop.head = op;
14413 if (right == prev_head && logop_valid_tail(prev_tail, type)) {
14414 p->logop.tail = prev_tail;
14415 }
14416 else if (right && nd_type_p(right, type)) {
14417 node = right;
14418 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14419 node = second;
14420 }
14421 p->logop.tail = node;
14422 }
14423 else {
14424 p->logop.tail = op;
14425 }
14426 return op;
14427}
14428
14429#undef NEW_AND_OR
14430
14431static void
14432no_blockarg(struct parser_params *p, NODE *node)
14433{
14434 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14435 compile_error(p, "block argument should not be given");
14436 }
14437}
14438
14439static NODE *
14440ret_args(struct parser_params *p, NODE *node)
14441{
14442 if (node) {
14443 no_blockarg(p, node);
14444 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14445 node = RNODE_LIST(node)->nd_head;
14446 }
14447 }
14448 return node;
14449}
14450
14451static NODE*
14452negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14453{
14454 switch (nd_type(node)) {
14455 case NODE_INTEGER:
14456 RNODE_INTEGER(node)->minus = TRUE;
14457 break;
14458 case NODE_FLOAT:
14459 RNODE_FLOAT(node)->minus = TRUE;
14460 break;
14461 case NODE_RATIONAL:
14462 RNODE_RATIONAL(node)->minus = TRUE;
14463 break;
14464 case NODE_IMAGINARY:
14465 RNODE_IMAGINARY(node)->minus = TRUE;
14466 break;
14467 }
14468 node->nd_loc = *loc;
14469 return node;
14470}
14471
14472static NODE *
14473arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14474{
14475 if (node2) {
14476 if (!node1) return (NODE *)node2;
14477 node2->nd_head = node1;
14478 nd_set_first_lineno(node2, nd_first_lineno(node1));
14479 nd_set_first_column(node2, nd_first_column(node1));
14480 return (NODE *)node2;
14481 }
14482 return node1;
14483}
14484
14485static bool
14486args_info_empty_p(struct rb_args_info *args)
14487{
14488 if (args->pre_args_num) return false;
14489 if (args->post_args_num) return false;
14490 if (args->rest_arg) return false;
14491 if (args->opt_args) return false;
14492 if (args->block_arg) return false;
14493 if (args->kw_args) return false;
14494 if (args->kw_rest_arg) return false;
14495 return true;
14496}
14497
14498static rb_node_args_t *
14499new_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)
14500{
14501 struct rb_args_info *args = &tail->nd_ainfo;
14502
14503 if (args->forwarding) {
14504 if (rest_arg) {
14505 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14506 return tail;
14507 }
14508 rest_arg = idFWD_REST;
14509 }
14510
14511 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14512 args->pre_init = pre_args ? pre_args->nd_next : 0;
14513
14514 args->post_args_num = post_args ? post_args->nd_plen : 0;
14515 args->post_init = post_args ? post_args->nd_next : 0;
14516 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14517
14518 args->rest_arg = rest_arg;
14519
14520 args->opt_args = opt_args;
14521
14522 nd_set_loc(RNODE(tail), loc);
14523
14524 return tail;
14525}
14526
14527static rb_node_args_t *
14528new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14529{
14530 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14531 struct rb_args_info *args = &node->nd_ainfo;
14532 if (p->error_p) return node;
14533
14534 if (block == idNil) {
14535 block = 0;
14536 args->no_blockarg = TRUE;
14537 }
14538 args->block_arg = block;
14539 args->kw_args = kw_args;
14540
14541 if (kw_args) {
14542 /*
14543 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14544 * variable order: k1, kr1, k2, &b, internal_id, krest
14545 * #=> <reorder>
14546 * variable order: kr1, k1, k2, internal_id, krest, &b
14547 */
14548 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14549 struct vtable *vtargs = p->lvtbl->args;
14550 rb_node_kw_arg_t *kwn = kw_args;
14551
14552 if (block) block = vtargs->tbl[vtargs->pos-1];
14553 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14554 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14555 while (kwn) {
14556 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14557 --kw_vars;
14558 --required_kw_vars;
14559 kwn = kwn->nd_next;
14560 }
14561
14562 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14563 ID vid = get_nd_vid(p, kwn->nd_body);
14564 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14565 *required_kw_vars++ = vid;
14566 }
14567 else {
14568 *kw_vars++ = vid;
14569 }
14570 }
14571
14572 arg_var(p, kw_bits);
14573 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14574 if (block) arg_var(p, block);
14575
14576 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14577 }
14578 else if (kw_rest_arg == idNil) {
14579 args->no_kwarg = 1;
14580 }
14581 else if (kw_rest_arg) {
14582 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14583 }
14584
14585 return node;
14586}
14587
14588static rb_node_args_t *
14589args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14590{
14591 if (max_numparam > NO_PARAM || it_id) {
14592 if (!args) {
14593 YYLTYPE loc = RUBY_INIT_YYLLOC();
14594 args = new_empty_args_tail(p, 0);
14595 nd_set_loc(RNODE(args), &loc);
14596 }
14597 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14598 }
14599 return args;
14600}
14601
14602static NODE*
14603new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14604{
14605 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14606
14607 if (pre_arg) {
14608 NODE *pre_args = NEW_LIST(pre_arg, loc);
14609 if (RNODE_ARYPTN(aryptn)->pre_args) {
14610 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14611 }
14612 else {
14613 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14614 }
14615 }
14616 return aryptn;
14617}
14618
14619static NODE*
14620new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14621{
14622 if (has_rest) {
14623 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14624 }
14625 else {
14626 rest_arg = NULL;
14627 }
14628 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14629
14630 return node;
14631}
14632
14633static NODE*
14634new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14635{
14636 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14637
14638 return fndptn;
14639}
14640
14641static NODE*
14642new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14643{
14644 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14645 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14646 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14647
14648 return node;
14649}
14650
14651static NODE*
14652new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14653{
14654 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14655 return hshptn;
14656}
14657
14658static NODE*
14659new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14660{
14661 NODE *node, *kw_rest_arg_node;
14662
14663 if (kw_rest_arg == idNil) {
14664 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14665 }
14666 else if (kw_rest_arg) {
14667 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14668 }
14669 else {
14670 kw_rest_arg_node = NULL;
14671 }
14672
14673 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14674
14675 return node;
14676}
14677
14678static NODE*
14679dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14680{
14681 if (!node) {
14682 return NEW_SYM(STR_NEW0(), loc);
14683 }
14684
14685 switch (nd_type(node)) {
14686 case NODE_DSTR:
14687 nd_set_type(node, NODE_DSYM);
14688 nd_set_loc(node, loc);
14689 break;
14690 case NODE_STR:
14691 node = str_to_sym_node(p, node, loc);
14692 break;
14693 default:
14694 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14695 break;
14696 }
14697 return node;
14698}
14699
14700static int
14701nd_type_st_key_enable_p(NODE *node)
14702{
14703 switch (nd_type(node)) {
14704 case NODE_INTEGER:
14705 case NODE_FLOAT:
14706 case NODE_RATIONAL:
14707 case NODE_IMAGINARY:
14708 case NODE_STR:
14709 case NODE_SYM:
14710 case NODE_REGX:
14711 case NODE_LINE:
14712 case NODE_FILE:
14713 case NODE_ENCODING:
14714 return true;
14715 default:
14716 return false;
14717 }
14718}
14719
14720static VALUE
14721nd_value(struct parser_params *p, NODE *node)
14722{
14723 switch (nd_type(node)) {
14724 case NODE_STR:
14725 return rb_node_str_string_val(node);
14726 case NODE_INTEGER:
14727 return rb_node_integer_literal_val(node);
14728 case NODE_FLOAT:
14729 return rb_node_float_literal_val(node);
14730 case NODE_RATIONAL:
14731 return rb_node_rational_literal_val(node);
14732 case NODE_IMAGINARY:
14733 return rb_node_imaginary_literal_val(node);
14734 case NODE_SYM:
14735 return rb_node_sym_string_val(node);
14736 case NODE_REGX:
14737 return rb_node_regx_string_val(node);
14738 case NODE_LINE:
14739 return rb_node_line_lineno_val(node);
14740 case NODE_ENCODING:
14741 return rb_node_encoding_val(node);
14742 case NODE_FILE:
14743 return rb_node_file_path_val(node);
14744 default:
14745 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14746 UNREACHABLE_RETURN(0);
14747 }
14748}
14749
14750static void
14751warn_duplicate_keys(struct parser_params *p, NODE *hash)
14752{
14753 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14754 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14755 while (hash && RNODE_LIST(hash)->nd_next) {
14756 NODE *head = RNODE_LIST(hash)->nd_head;
14757 NODE *value = RNODE_LIST(hash)->nd_next;
14758 NODE *next = RNODE_LIST(value)->nd_next;
14759 st_data_t key;
14760 st_data_t data;
14761
14762 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14763 if (!head) {
14764 head = value;
14765 }
14766
14767 if (nd_type_st_key_enable_p(head)) {
14768 key = (st_data_t)head;
14769
14770 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14771 rb_warn2L(nd_line((NODE *)data),
14772 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14773 nd_value(p, head), WARN_I(nd_line(head)));
14774 }
14775 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14776 }
14777 hash = next;
14778 }
14779 st_free_table(p->warn_duplicate_keys_table);
14780 p->warn_duplicate_keys_table = NULL;
14781}
14782
14783static NODE *
14784new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14785{
14786 if (hash) warn_duplicate_keys(p, hash);
14787 return NEW_HASH(hash, loc);
14788}
14789
14790static void
14791error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14792{
14793 if (is_private_local_id(p, id)) {
14794 return;
14795 }
14796 if (st_is_member(p->pvtbl, id)) {
14797 yyerror1(loc, "duplicated variable name");
14798 }
14799 else if (p->ctxt.in_alt_pattern && id) {
14800 yyerror1(loc, "variable capture in alternative pattern");
14801 }
14802 else {
14803 p->ctxt.capture_in_pattern = 1;
14804 st_insert(p->pvtbl, (st_data_t)id, 0);
14805 }
14806}
14807
14808static void
14809error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14810{
14811 if (!p->pktbl) {
14812 p->pktbl = st_init_numtable();
14813 }
14814 else if (st_is_member(p->pktbl, key)) {
14815 yyerror1(loc, "duplicated key name");
14816 return;
14817 }
14818 st_insert(p->pktbl, (st_data_t)key, 0);
14819}
14820
14821static NODE *
14822new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14823{
14824 return NEW_HASH(hash, loc);
14825}
14826
14827static NODE *
14828new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14829{
14830 NODE *asgn;
14831
14832 if (lhs) {
14833 ID vid = get_nd_vid(p, lhs);
14834 YYLTYPE lhs_loc = lhs->nd_loc;
14835 /* A constant read can raise NameError. Prism has no separate read node
14836 * for `Const op= value` and reports the whole expression, so give the
14837 * NODE_CONST the location of the whole operator assignment as well,
14838 * for consistent Thread::Backtrace::Location#source_range between both
14839 * parsers. */
14840 const YYLTYPE *read_loc = nd_type_p(lhs, NODE_CDECL) ? loc : &lhs_loc;
14841 if (op == tOROP) {
14842 set_nd_value(p, lhs, rhs);
14843 nd_set_loc(lhs, loc);
14844 asgn = NEW_OP_ASGN_OR(gettable(p, vid, read_loc), lhs, loc);
14845 }
14846 else if (op == tANDOP) {
14847 set_nd_value(p, lhs, rhs);
14848 nd_set_loc(lhs, loc);
14849 asgn = NEW_OP_ASGN_AND(gettable(p, vid, read_loc), lhs, loc);
14850 }
14851 else {
14852 asgn = lhs;
14853 rhs = NEW_CALL(gettable(p, vid, read_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14854 set_nd_value(p, asgn, rhs);
14855 nd_set_loc(asgn, loc);
14856 }
14857 }
14858 else {
14859 asgn = NEW_ERROR(loc);
14860 }
14861 return asgn;
14862}
14863
14864static NODE *
14865new_ary_op_assign(struct parser_params *p, NODE *ary,
14866 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14867 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14868{
14869 NODE *asgn;
14870
14871 aryset_check(p, args);
14872 args = make_list(args, args_loc);
14873 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14874 fixpos(asgn, ary);
14875 return asgn;
14876}
14877
14878static NODE *
14879new_attr_op_assign(struct parser_params *p, NODE *lhs,
14880 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14881 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14882{
14883 NODE *asgn;
14884
14885 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14886 fixpos(asgn, lhs);
14887 return asgn;
14888}
14889
14890static NODE *
14891new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14892{
14893 NODE *asgn;
14894
14895 if (lhs) {
14896 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14897 }
14898 else {
14899 asgn = NEW_ERROR(loc);
14900 }
14901 fixpos(asgn, lhs);
14902 return asgn;
14903}
14904
14905static NODE *
14906const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14907{
14908 if (p->ctxt.in_def) {
14909#ifndef RIPPER
14910 yyerror1(loc, "dynamic constant assignment");
14911#else
14912 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14913#endif
14914 }
14915 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14916}
14917
14918#ifdef RIPPER
14919static VALUE
14920assign_error(struct parser_params *p, const char *mesg, VALUE a)
14921{
14922 a = dispatch2(assign_error, ERR_MESG(), a);
14923 ripper_error(p);
14924 return a;
14925}
14926#endif
14927
14928static NODE *
14929new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14930{
14931 NODE *result = head;
14932 if (rescue) {
14933 NODE *tmp = rescue_else ? rescue_else : rescue;
14934 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14935
14936 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14937 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14938 }
14939 if (ensure) {
14940 result = NEW_ENSURE(result, ensure, loc);
14941 }
14942 fixpos(result, head);
14943 return result;
14944}
14945
14946static void
14947warn_unused_var(struct parser_params *p, struct local_vars *local)
14948{
14949 int cnt;
14950
14951 if (!local->used) return;
14952 cnt = local->used->pos;
14953 if (cnt != local->vars->pos) {
14954 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14955 }
14956#ifndef RIPPER
14957 ID *v = local->vars->tbl;
14958 ID *u = local->used->tbl;
14959 for (int i = 0; i < cnt; ++i) {
14960 if (!v[i] || (u[i] & LVAR_USED)) continue;
14961 if (is_private_local_id(p, v[i])) continue;
14962 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14963 }
14964#endif
14965}
14966
14967static void
14968local_push(struct parser_params *p, int toplevel_scope)
14969{
14970 struct local_vars *local;
14971 int inherits_dvars = toplevel_scope && compile_for_eval;
14972 int warn_unused_vars = RTEST(ruby_verbose);
14973
14974 local = ALLOC(struct local_vars);
14975 local->prev = p->lvtbl;
14976 local->args = vtable_alloc(0);
14977 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14978#ifndef RIPPER
14979 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14980 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14981#endif
14982 local->numparam.outer = 0;
14983 local->numparam.inner = 0;
14984 local->numparam.current = 0;
14985 local->it = 0;
14986 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14987
14988# if WARN_PAST_SCOPE
14989 local->past = 0;
14990# endif
14991 CMDARG_PUSH(0);
14992 COND_PUSH(0);
14993 p->lvtbl = local;
14994}
14995
14996static void
14997vtable_chain_free(struct parser_params *p, struct vtable *table)
14998{
14999 while (!DVARS_TERMINAL_P(table)) {
15000 struct vtable *cur_table = table;
15001 table = cur_table->prev;
15002 vtable_free(cur_table);
15003 }
15004}
15005
15006static void
15007local_free(struct parser_params *p, struct local_vars *local)
15008{
15009 vtable_chain_free(p, local->used);
15010
15011# if WARN_PAST_SCOPE
15012 vtable_chain_free(p, local->past);
15013# endif
15014
15015 vtable_chain_free(p, local->args);
15016 vtable_chain_free(p, local->vars);
15017
15018 ruby_xfree_sized(local, sizeof(struct local_vars));
15019}
15020
15021static void
15022local_pop(struct parser_params *p)
15023{
15024 struct local_vars *local = p->lvtbl->prev;
15025 if (p->lvtbl->used) {
15026 warn_unused_var(p, p->lvtbl);
15027 }
15028
15029 local_free(p, p->lvtbl);
15030 p->lvtbl = local;
15031
15032 CMDARG_POP();
15033 COND_POP();
15034}
15035
15036static rb_ast_id_table_t *
15037local_tbl(struct parser_params *p)
15038{
15039 int cnt_args = vtable_size(p->lvtbl->args);
15040 int cnt_vars = vtable_size(p->lvtbl->vars);
15041 int cnt = cnt_args + cnt_vars;
15042 int i, j;
15043 rb_ast_id_table_t *tbl;
15044
15045 if (cnt <= 0) return 0;
15046 tbl = rb_ast_new_local_table(p->ast, cnt);
15047 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
15048 /* remove IDs duplicated to warn shadowing */
15049 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15050 ID id = p->lvtbl->vars->tbl[i];
15051 if (!vtable_included(p->lvtbl->args, id)) {
15052 tbl->ids[j++] = id;
15053 }
15054 }
15055 if (j < cnt) {
15056 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15057 }
15058
15059 return tbl;
15060}
15061
15062static void
15063numparam_name(struct parser_params *p, ID id)
15064{
15065 if (!NUMPARAM_ID_P(id)) return;
15066 compile_error(p, "_%d is reserved for numbered parameter",
15067 NUMPARAM_ID_TO_IDX(id));
15068}
15069
15070static void
15071arg_var(struct parser_params *p, ID id)
15072{
15073 numparam_name(p, id);
15074 vtable_add(p->lvtbl->args, id);
15075}
15076
15077static void
15078local_var(struct parser_params *p, ID id)
15079{
15080 numparam_name(p, id);
15081 vtable_add(p->lvtbl->vars, id);
15082 if (p->lvtbl->used) {
15083 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15084 }
15085}
15086
15087#ifndef RIPPER
15088int
15089rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15090{
15091 return rb_local_defined(id, iseq);
15092}
15093#endif
15094
15095static int
15096local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15097{
15098 struct vtable *vars, *args, *used;
15099
15100 vars = p->lvtbl->vars;
15101 args = p->lvtbl->args;
15102 used = p->lvtbl->used;
15103
15104 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15105 vars = vars->prev;
15106 args = args->prev;
15107 if (used) used = used->prev;
15108 }
15109
15110 if (vars && vars->prev == DVARS_INHERIT) {
15111 return rb_parser_local_defined(p, id, p->parent_iseq);
15112 }
15113 else if (vtable_included(args, id)) {
15114 return 1;
15115 }
15116 else {
15117 int i = vtable_included(vars, id);
15118 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15119 return i != 0;
15120 }
15121}
15122
15123static int
15124local_id(struct parser_params *p, ID id)
15125{
15126 return local_id_ref(p, id, NULL);
15127}
15128
15129static int
15130check_forwarding_args(struct parser_params *p)
15131{
15132 if (local_id(p, idFWD_ALL)) return TRUE;
15133 compile_error(p, "unexpected ...");
15134 return FALSE;
15135}
15136
15137static void
15138add_forwarding_args(struct parser_params *p)
15139{
15140 arg_var(p, idFWD_REST);
15141 arg_var(p, idFWD_KWREST);
15142 arg_var(p, idFWD_BLOCK);
15143 arg_var(p, idFWD_ALL);
15144}
15145
15146static void
15147forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15148{
15149 bool conflict = false;
15150
15151 struct vtable *vars, *args;
15152
15153 vars = p->lvtbl->vars;
15154 args = p->lvtbl->args;
15155
15156 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15157 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15158 vars = vars->prev;
15159 args = args->prev;
15160 }
15161
15162 bool found = false;
15163 if (vars && vars->prev == DVARS_INHERIT && !found) {
15164 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15165 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15166 }
15167 else {
15168 found = (vtable_included(args, arg) &&
15169 !(all && vtable_included(args, all)));
15170 }
15171
15172 if (!found) {
15173 compile_error(p, "no anonymous %s parameter", var);
15174 }
15175 else if (conflict) {
15176 compile_error(p, "anonymous %s parameter is also used within block", var);
15177 }
15178}
15179
15180static NODE *
15181new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15182{
15183 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15184 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15185 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15186 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15187 block->forwarding = TRUE;
15188 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15189 return arg_blk_pass(args, block);
15190}
15191
15192static NODE *
15193numparam_push(struct parser_params *p)
15194{
15195 struct local_vars *local = p->lvtbl;
15196 NODE *inner = local->numparam.inner;
15197 if (!local->numparam.outer) {
15198 local->numparam.outer = local->numparam.current;
15199 }
15200 local->numparam.inner = 0;
15201 local->numparam.current = 0;
15202 local->it = 0;
15203 return inner;
15204}
15205
15206static void
15207numparam_pop(struct parser_params *p, NODE *prev_inner)
15208{
15209 struct local_vars *local = p->lvtbl;
15210 if (prev_inner) {
15211 /* prefer first one */
15212 local->numparam.inner = prev_inner;
15213 }
15214 else if (local->numparam.current) {
15215 /* current and inner are exclusive */
15216 local->numparam.inner = local->numparam.current;
15217 }
15218 if (p->max_numparam > NO_PARAM) {
15219 /* current and outer are exclusive */
15220 local->numparam.current = local->numparam.outer;
15221 local->numparam.outer = 0;
15222 }
15223 else {
15224 /* no numbered parameter */
15225 local->numparam.current = 0;
15226 }
15227 local->it = 0;
15228}
15229
15230static const struct vtable *
15231dyna_push(struct parser_params *p)
15232{
15233 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15234 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15235 if (p->lvtbl->used) {
15236 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15237 }
15238 return p->lvtbl->args;
15239}
15240
15241static void
15242dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15243{
15244 struct vtable *tmp = *vtblp;
15245 *vtblp = tmp->prev;
15246# if WARN_PAST_SCOPE
15247 if (p->past_scope_enabled) {
15248 tmp->prev = p->lvtbl->past;
15249 p->lvtbl->past = tmp;
15250 return;
15251 }
15252# endif
15253 vtable_free(tmp);
15254}
15255
15256static void
15257dyna_pop_1(struct parser_params *p)
15258{
15259 struct vtable *tmp;
15260
15261 if ((tmp = p->lvtbl->used) != 0) {
15262 warn_unused_var(p, p->lvtbl);
15263 p->lvtbl->used = p->lvtbl->used->prev;
15264 vtable_free(tmp);
15265 }
15266 dyna_pop_vtable(p, &p->lvtbl->args);
15267 dyna_pop_vtable(p, &p->lvtbl->vars);
15268}
15269
15270static void
15271dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15272{
15273 while (p->lvtbl->args != lvargs) {
15274 dyna_pop_1(p);
15275 if (!p->lvtbl->args) {
15276 struct local_vars *local = p->lvtbl->prev;
15277 ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl));
15278 p->lvtbl = local;
15279 }
15280 }
15281 dyna_pop_1(p);
15282}
15283
15284static int
15285dyna_in_block(struct parser_params *p)
15286{
15287 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15288}
15289
15290#ifndef RIPPER
15291int
15292dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15293{
15294 struct vtable *vars, *args, *used;
15295 int i;
15296
15297 args = p->lvtbl->args;
15298 vars = p->lvtbl->vars;
15299 used = p->lvtbl->used;
15300
15301 while (!DVARS_TERMINAL_P(vars)) {
15302 if (vtable_included(args, id)) {
15303 return 1;
15304 }
15305 if ((i = vtable_included(vars, id)) != 0) {
15306 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15307 return 1;
15308 }
15309 args = args->prev;
15310 vars = vars->prev;
15311 if (!vidrefp) used = 0;
15312 if (used) used = used->prev;
15313 }
15314
15315 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15316 return rb_dvar_defined(id, p->parent_iseq);
15317 }
15318
15319 return 0;
15320}
15321#endif
15322
15323static int
15324dvar_defined(struct parser_params *p, ID id)
15325{
15326 return dvar_defined_ref(p, id, NULL);
15327}
15328
15329static int
15330dvar_curr(struct parser_params *p, ID id)
15331{
15332 return (vtable_included(p->lvtbl->args, id) ||
15333 vtable_included(p->lvtbl->vars, id));
15334}
15335
15336static void
15337reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15338{
15339 compile_error(p,
15340 "regexp encoding option '%c' differs from source encoding '%s'",
15341 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15342}
15343
15344#ifndef RIPPER
15345static rb_encoding *
15346find_enc(struct parser_params* p, const char *name)
15347{
15348 int idx = rb_enc_find_index(name);
15349 if (idx < 0) {
15350 rb_bug("unknown encoding name: %s", name);
15351 }
15352
15353 return rb_enc_from_index(idx);
15354}
15355
15356static rb_encoding *
15357kcode_to_enc(struct parser_params* p, int kcode)
15358{
15359 rb_encoding *enc;
15360
15361 switch (kcode) {
15362 case ENC_ASCII8BIT:
15363 enc = rb_ascii8bit_encoding();
15364 break;
15365 case ENC_EUC_JP:
15366 enc = find_enc(p, "EUC-JP");
15367 break;
15368 case ENC_Windows_31J:
15369 enc = find_enc(p, "Windows-31J");
15370 break;
15371 case ENC_UTF8:
15372 enc = rb_utf8_encoding();
15373 break;
15374 default:
15375 enc = NULL;
15376 break;
15377 }
15378
15379 return enc;
15380}
15381
15382int
15383rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15384{
15385 int c = RE_OPTION_ENCODING_IDX(options);
15386
15387 if (c) {
15388 int opt, idx;
15389 rb_encoding *enc;
15390
15391 char_to_option_kcode(c, &opt, &idx);
15392 enc = kcode_to_enc(p, idx);
15393 if (enc != rb_parser_str_get_encoding(str) &&
15394 !rb_parser_is_ascii_string(p, str)) {
15395 goto error;
15396 }
15397 rb_parser_string_set_encoding(str, enc);
15398 }
15399 else if (RE_OPTION_ENCODING_NONE(options)) {
15400 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15401 !rb_parser_is_ascii_string(p, str)) {
15402 c = 'n';
15403 goto error;
15404 }
15405 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15406 }
15407 else if (rb_is_usascii_enc(p->enc)) {
15408 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15409 }
15410 return 0;
15411
15412 error:
15413 return c;
15414}
15415#endif
15416
15417static void
15418reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15419{
15420 int c = rb_reg_fragment_setenc(p, str, options);
15421 if (c) reg_fragment_enc_error(p, str, c);
15422}
15423
15424#ifndef UNIVERSAL_PARSER
15425typedef struct {
15426 struct parser_params* parser;
15427 rb_encoding *enc;
15428 NODE *succ_block;
15429 const YYLTYPE *loc;
15430 rb_parser_assignable_func assignable;
15431} reg_named_capture_assign_t;
15432
15433static int
15434reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15435 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15436{
15437 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15438 struct parser_params* p = arg->parser;
15439 rb_encoding *enc = arg->enc;
15440 long len = name_end - name;
15441 const char *s = (const char *)name;
15442
15443 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15444}
15445
15446static NODE *
15447reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15448{
15449 reg_named_capture_assign_t arg;
15450
15451 arg.parser = p;
15452 arg.enc = rb_enc_get(regexp);
15453 arg.succ_block = 0;
15454 arg.loc = loc;
15455 arg.assignable = assignable;
15456 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15457
15458 if (!arg.succ_block) return 0;
15459 return RNODE_BLOCK(arg.succ_block)->nd_next;
15460}
15461#endif
15462
15463#ifndef RIPPER
15464NODE *
15465rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15466{
15467 return assignable(p, id, val, loc);
15468}
15469
15470int
15471rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15472 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15473{
15474 ID var;
15475 NODE *node, *succ;
15476
15477 if (!len) return ST_CONTINUE;
15478 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15479 return ST_CONTINUE;
15480
15481 var = intern_cstr(s, len, enc);
15482 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15483 if (!lvar_defined(p, var)) return ST_CONTINUE;
15484 }
15485 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15486 succ = *succ_block;
15487 if (!succ) succ = NEW_ERROR(loc);
15488 succ = block_append(p, succ, node);
15489 *succ_block = succ;
15490 return ST_CONTINUE;
15491}
15492#endif
15493
15494static VALUE
15495parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15496{
15497 VALUE str2;
15498 reg_fragment_setenc(p, str, options);
15499 str2 = rb_str_new_parser_string(str);
15500 return rb_parser_reg_compile(p, str2, options);
15501}
15502
15503#ifndef RIPPER
15504VALUE
15505rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15506{
15507 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15508}
15509#endif
15510
15511static VALUE
15512reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15513{
15514 VALUE re;
15515 VALUE err;
15516
15517 err = rb_errinfo();
15518 re = parser_reg_compile(p, str, options);
15519 if (NIL_P(re)) {
15520 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15521 rb_set_errinfo(err);
15522 compile_error(p, "%"PRIsVALUE, m);
15523 return Qnil;
15524 }
15525 return re;
15526}
15527
15528#ifndef RIPPER
15529void
15530rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15531{
15532 p->do_print = print;
15533 p->do_loop = loop;
15534 p->do_chomp = chomp;
15535 p->do_split = split;
15536}
15537
15538static NODE *
15539parser_append_options(struct parser_params *p, NODE *node)
15540{
15541 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15542 const YYLTYPE *const LOC = &default_location;
15543
15544 if (p->do_print) {
15545 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15546 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15547 LOC);
15548 node = block_append(p, node, print);
15549 }
15550
15551 if (p->do_loop) {
15552 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15553
15554 if (p->do_split) {
15555 ID ifs = rb_intern("$;");
15556 ID fields = rb_intern("$F");
15557 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15558 NODE *split = NEW_GASGN(fields,
15559 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15560 rb_intern("split"), args, LOC),
15561 LOC);
15562 node = block_append(p, split, node);
15563 }
15564 if (p->do_chomp) {
15565 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15566 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15567 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15568 }
15569
15570 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15571 }
15572
15573 return node;
15574}
15575
15576void
15577rb_init_parse(void)
15578{
15579 /* just to suppress unused-function warnings */
15580 (void)nodetype;
15581 (void)nodeline;
15582}
15583
15584ID
15585internal_id(struct parser_params *p)
15586{
15587 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15588}
15589#endif /* !RIPPER */
15590
15591static void
15592parser_initialize(struct parser_params *p)
15593{
15594 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15595 p->command_start = TRUE;
15596 p->ruby_sourcefile_string = Qnil;
15597 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15598 string_buffer_init(p);
15599 p->node_id = 0;
15600 p->delayed.token = NULL;
15601 p->frozen_string_literal = -1; /* not specified */
15602 rb_source_hash_init(&p->source_hash);
15603#ifndef RIPPER
15604 p->error_buffer = Qfalse;
15605 p->end_expect_token_locations = NULL;
15606 p->token_id = 0;
15607 p->tokens = NULL;
15608#else
15609 p->result = Qnil;
15610 p->parsing_thread = Qnil;
15611 p->s_value = Qnil;
15612 p->s_lvalue = Qnil;
15613 p->s_value_stack = rb_ary_new();
15614#endif
15615 p->debug_buffer = Qnil;
15616 p->debug_output = rb_ractor_stdout();
15617 p->enc = rb_utf8_encoding();
15618 p->exits = 0;
15619}
15620
15621#ifdef RIPPER
15622#define rb_ruby_parser_mark ripper_parser_mark
15623#define rb_ruby_parser_free ripper_parser_free
15624#define rb_ruby_parser_memsize ripper_parser_memsize
15625#endif
15626
15627void
15628rb_ruby_parser_mark(void *ptr)
15629{
15630 struct parser_params *p = (struct parser_params*)ptr;
15631
15632 rb_gc_mark(p->ruby_sourcefile_string);
15633#ifndef RIPPER
15634 rb_gc_mark(p->error_buffer);
15635#else
15636 rb_gc_mark(p->value);
15637 rb_gc_mark(p->result);
15638 rb_gc_mark(p->parsing_thread);
15639 rb_gc_mark(p->s_value);
15640 rb_gc_mark(p->s_lvalue);
15641 rb_gc_mark(p->s_value_stack);
15642#endif
15643 rb_gc_mark(p->debug_buffer);
15644 rb_gc_mark(p->debug_output);
15645}
15646
15647void
15648rb_ruby_parser_free(void *ptr)
15649{
15650 struct parser_params *p = (struct parser_params*)ptr;
15651 struct local_vars *local, *prev;
15652
15653 if (p->ast) {
15654 rb_ast_free(p->ast);
15655 }
15656
15657 if (p->warn_duplicate_keys_table) {
15658 st_free_table(p->warn_duplicate_keys_table);
15659 }
15660
15661#ifndef RIPPER
15662 if (p->tokens) {
15663 rb_parser_ary_free(p, p->tokens);
15664 }
15665#endif
15666
15667 if (p->tokenbuf) {
15668 ruby_xfree_sized(p->tokenbuf, p->toksiz);
15669 }
15670
15671 for (local = p->lvtbl; local; local = prev) {
15672 prev = local->prev;
15673 local_free(p, local);
15674 }
15675
15676 {
15677 token_info *ptinfo;
15678 while ((ptinfo = p->token_info) != 0) {
15679 p->token_info = ptinfo->next;
15680 xfree(ptinfo);
15681 }
15682 }
15683 string_buffer_free(p);
15684
15685 if (p->pvtbl) {
15686 st_free_table(p->pvtbl);
15687 }
15688
15689 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15690 st_free_table(p->case_labels);
15691 }
15692
15693 xfree(p->lex.strterm);
15694 p->lex.strterm = 0;
15695
15696 xfree(ptr);
15697}
15698
15699size_t
15700rb_ruby_parser_memsize(const void *ptr)
15701{
15702 struct parser_params *p = (struct parser_params*)ptr;
15703 struct local_vars *local;
15704 size_t size = sizeof(*p);
15705
15706 size += p->toksiz;
15707 for (local = p->lvtbl; local; local = local->prev) {
15708 size += sizeof(*local);
15709 if (local->vars) size += local->vars->capa * sizeof(ID);
15710 }
15711 return size;
15712}
15713
15714#ifndef RIPPER
15715#undef rb_reserved_word
15716
15717const struct kwtable *
15718rb_reserved_word(const char *str, unsigned int len)
15719{
15720 return reserved_word(str, len);
15721}
15722
15723#ifdef UNIVERSAL_PARSER
15724rb_parser_t *
15725rb_ruby_parser_allocate(const rb_parser_config_t *config)
15726{
15727 /* parser_initialize expects fields to be set to 0 */
15728 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15729 p->config = config;
15730 return p;
15731}
15732
15733rb_parser_t *
15734rb_ruby_parser_new(const rb_parser_config_t *config)
15735{
15736 /* parser_initialize expects fields to be set to 0 */
15737 rb_parser_t *p = rb_ruby_parser_allocate(config);
15738 parser_initialize(p);
15739 return p;
15740}
15741#else
15742rb_parser_t *
15743rb_ruby_parser_allocate(void)
15744{
15745 /* parser_initialize expects fields to be set to 0 */
15746 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15747 return p;
15748}
15749
15750rb_parser_t *
15751rb_ruby_parser_new(void)
15752{
15753 /* parser_initialize expects fields to be set to 0 */
15754 rb_parser_t *p = rb_ruby_parser_allocate();
15755 parser_initialize(p);
15756 return p;
15757}
15758#endif
15759
15760rb_parser_t *
15761rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15762{
15763 p->error_buffer = main ? Qfalse : Qnil;
15764 p->parent_iseq = base;
15765 return p;
15766}
15767
15768void
15769rb_ruby_parser_set_script_lines(rb_parser_t *p)
15770{
15771 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15772}
15773
15774void
15775rb_ruby_parser_error_tolerant(rb_parser_t *p)
15776{
15777 p->error_tolerant = 1;
15778}
15779
15780void
15781rb_ruby_parser_keep_tokens(rb_parser_t *p)
15782{
15783 p->keep_tokens = 1;
15784 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15785}
15786
15787rb_encoding *
15788rb_ruby_parser_encoding(rb_parser_t *p)
15789{
15790 return p->enc;
15791}
15792
15793int
15794rb_ruby_parser_end_seen_p(rb_parser_t *p)
15795{
15796 return p->ruby__end__seen;
15797}
15798
15799int
15800rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15801{
15802 p->debug = flag;
15803 return flag;
15804}
15805#endif /* !RIPPER */
15806
15807#ifdef RIPPER
15808int
15809rb_ruby_parser_get_yydebug(rb_parser_t *p)
15810{
15811 return p->debug;
15812}
15813
15814void
15815rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15816{
15817 p->value = value;
15818}
15819
15820int
15821rb_ruby_parser_error_p(rb_parser_t *p)
15822{
15823 return p->error_p;
15824}
15825
15826VALUE
15827rb_ruby_parser_debug_output(rb_parser_t *p)
15828{
15829 return p->debug_output;
15830}
15831
15832void
15833rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15834{
15835 p->debug_output = output;
15836}
15837
15838VALUE
15839rb_ruby_parser_parsing_thread(rb_parser_t *p)
15840{
15841 return p->parsing_thread;
15842}
15843
15844void
15845rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15846{
15847 p->parsing_thread = parsing_thread;
15848}
15849
15850void
15851rb_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)
15852{
15853 p->lex.gets = gets;
15854 p->lex.input = input;
15855 p->eofp = 0;
15856 p->ruby_sourcefile_string = sourcefile_string;
15857 p->ruby_sourcefile = sourcefile;
15858 p->ruby_sourceline = sourceline;
15859}
15860
15861VALUE
15862rb_ruby_parser_result(rb_parser_t *p)
15863{
15864 return p->result;
15865}
15866
15867rb_encoding *
15868rb_ruby_parser_enc(rb_parser_t *p)
15869{
15870 return p->enc;
15871}
15872
15873VALUE
15874rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15875{
15876 return p->ruby_sourcefile_string;
15877}
15878
15879int
15880rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15881{
15882 return p->ruby_sourceline;
15883}
15884
15885int
15886rb_ruby_parser_lex_state(rb_parser_t *p)
15887{
15888 return p->lex.state;
15889}
15890
15891void
15892rb_ruby_ripper_parse0(rb_parser_t *p)
15893{
15894 parser_prepare(p);
15895 p->ast = rb_ast_new();
15896 ripper_yyparse((void*)p);
15897 rb_ast_free(p->ast);
15898 p->ast = 0;
15899 p->eval_tree = 0;
15900 p->eval_tree_begin = 0;
15901}
15902
15903int
15904rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15905{
15906 return dedent_string(p, string, width);
15907}
15908
15909int
15910rb_ruby_ripper_initialized_p(rb_parser_t *p)
15911{
15912 return p->lex.input != 0;
15913}
15914
15915void
15916rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15917{
15918 parser_initialize(p);
15919}
15920
15921long
15922rb_ruby_ripper_column(rb_parser_t *p)
15923{
15924 return p->lex.ptok - p->lex.pbeg;
15925}
15926
15927long
15928rb_ruby_ripper_token_len(rb_parser_t *p)
15929{
15930 return p->lex.pcur - p->lex.ptok;
15931}
15932
15933rb_parser_string_t *
15934rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15935{
15936 return p->lex.lastline;
15937}
15938
15939VALUE
15940rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15941{
15942 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15943}
15944
15945#ifdef UNIVERSAL_PARSER
15946rb_parser_t *
15947rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15948{
15949 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15950 p->config = config;
15951 return p;
15952}
15953#endif
15954
15955struct parser_params*
15956rb_ruby_ripper_parser_allocate(void)
15957{
15958 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15959}
15960#endif /* RIPPER */
15961
15962#ifndef RIPPER
15963void
15964rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15965{
15966 va_list ap;
15967 VALUE mesg = p->debug_buffer;
15968
15969 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15970 va_start(ap, fmt);
15971 rb_str_vcatf(mesg, fmt, ap);
15972 va_end(ap);
15973 if (char_at_end(p, mesg, 0) == '\n') {
15974 rb_io_write(p->debug_output, mesg);
15975 p->debug_buffer = Qnil;
15976 }
15977}
15978
15979static void
15980parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15981{
15982 va_list ap;
15983 int lineno, column;
15984
15985 if (loc) {
15986 lineno = loc->end_pos.lineno;
15987 column = loc->end_pos.column;
15988 }
15989 else {
15990 lineno = p->ruby_sourceline;
15991 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15992 }
15993
15994 rb_io_flush(p->debug_output);
15995 p->error_p = 1;
15996 va_start(ap, fmt);
15997 p->error_buffer =
15998 rb_syntax_error_append(p->error_buffer,
15999 p->ruby_sourcefile_string,
16000 lineno, column,
16001 p->enc, fmt, ap);
16002 va_end(ap);
16003}
16004
16005static size_t
16006count_char(const char *str, int c)
16007{
16008 int n = 0;
16009 while (str[n] == c) ++n;
16010 return n;
16011}
16012
16013/*
16014 * strip enclosing double-quotes, same as the default yytnamerr except
16015 * for that single-quotes matching back-quotes do not stop stripping.
16016 *
16017 * "\"`class' keyword\"" => "`class' keyword"
16018 */
16019size_t
16020rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16021{
16022 if (*yystr == '"') {
16023 size_t yyn = 0, bquote = 0;
16024 const char *yyp = yystr;
16025
16026 while (*++yyp) {
16027 switch (*yyp) {
16028 case '\'':
16029 if (!bquote) {
16030 bquote = count_char(yyp+1, '\'') + 1;
16031 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16032 yyn += bquote;
16033 yyp += bquote - 1;
16034 break;
16035 }
16036 else {
16037 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16038 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16039 yyn += bquote;
16040 yyp += bquote - 1;
16041 bquote = 0;
16042 break;
16043 }
16044 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16045 if (yyres) memcpy(yyres + yyn, yyp, 3);
16046 yyn += 3;
16047 yyp += 2;
16048 break;
16049 }
16050 goto do_not_strip_quotes;
16051 }
16052
16053 case ',':
16054 goto do_not_strip_quotes;
16055
16056 case '\\':
16057 if (*++yyp != '\\')
16058 goto do_not_strip_quotes;
16059 /* Fall through. */
16060 default:
16061 if (yyres)
16062 yyres[yyn] = *yyp;
16063 yyn++;
16064 break;
16065
16066 case '"':
16067 case '\0':
16068 if (yyres)
16069 yyres[yyn] = '\0';
16070 return yyn;
16071 }
16072 }
16073 do_not_strip_quotes: ;
16074 }
16075
16076 if (!yyres) return strlen(yystr);
16077
16078 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16079}
16080#endif
16081
16082#ifdef RIPPER
16083#define validate(x) (void)(x)
16084
16085static VALUE
16086ripper_dispatch0(struct parser_params *p, ID mid)
16087{
16088 return rb_funcall(p->value, mid, 0);
16089}
16090
16091static VALUE
16092ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16093{
16094 validate(a);
16095 return rb_funcall(p->value, mid, 1, a);
16096}
16097
16098static VALUE
16099ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16100{
16101 validate(a);
16102 validate(b);
16103 return rb_funcall(p->value, mid, 2, a, b);
16104}
16105
16106static VALUE
16107ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16108{
16109 validate(a);
16110 validate(b);
16111 validate(c);
16112 return rb_funcall(p->value, mid, 3, a, b, c);
16113}
16114
16115static VALUE
16116ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16117{
16118 validate(a);
16119 validate(b);
16120 validate(c);
16121 validate(d);
16122 return rb_funcall(p->value, mid, 4, a, b, c, d);
16123}
16124
16125static VALUE
16126ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16127{
16128 validate(a);
16129 validate(b);
16130 validate(c);
16131 validate(d);
16132 validate(e);
16133 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16134}
16135
16136static VALUE
16137ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16138{
16139 validate(a);
16140 validate(b);
16141 validate(c);
16142 validate(d);
16143 validate(e);
16144 validate(f);
16145 validate(g);
16146 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16147}
16148
16149void
16150ripper_error(struct parser_params *p)
16151{
16152 p->error_p = TRUE;
16153}
16154
16155VALUE
16156ripper_value(struct parser_params *p)
16157{
16158 (void)yystpcpy; /* may not used in newer bison */
16159
16160 return p->value;
16161}
16162
16163#endif /* RIPPER */
16164/*
16165 * Local variables:
16166 * mode: c
16167 * c-file-style: "ruby"
16168 * End:
16169 */