Ruby 4.1.0dev (2026-09-27 revision f6ff9e7d02e46360f8930b280a3dd921cccbda29)
parse.y (f6ff9e7d02e46360f8930b280a3dd921cccbda29)
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 long 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 /* Type check */
2055 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2056 RB_GC_GUARD(str);
2057 return ret;
2058}
2059
2060void
2061rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2062{
2063 if (!str) return;
2064 xfree(PARSER_STRING_PTR(str));
2065 xfree(str);
2066}
2067#endif
2068
2069static st_index_t
2070rb_parser_str_hash(rb_parser_string_t *str)
2071{
2072 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2073}
2074
2075static st_index_t
2076rb_char_p_hash(const char *c)
2077{
2078 return parser_memhash((const void *)c, strlen(c));
2079}
2080
2081static size_t
2082rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2083{
2084 return PARSER_STRING_LEN(str);
2085}
2086
2087#ifndef RIPPER
2088static char *
2089rb_parser_string_end(rb_parser_string_t *str)
2090{
2091 return &str->ptr[str->len];
2092}
2093#endif
2094
2095static void
2096rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2097{
2098 str->enc = enc;
2099}
2100
2101static rb_encoding *
2102rb_parser_str_get_encoding(rb_parser_string_t *str)
2103{
2104 return str->enc;
2105}
2106
2107#ifndef RIPPER
2108static bool
2109PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2110{
2111 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2112}
2113#endif
2114
2115static int
2116PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2117{
2118 return str->coderange;
2119}
2120
2121static void
2122PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2123{
2124 str->coderange = coderange;
2125}
2126
2127static void
2128PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2129{
2130 rb_parser_string_set_encoding(str, enc);
2131 PARSER_ENC_CODERANGE_SET(str, cr);
2132}
2133
2134static void
2135PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2136{
2137 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2138}
2139
2140static bool
2141PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2142{
2143 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2144}
2145
2146static bool
2147PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2148{
2149 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2150}
2151
2152static const char *
2153rb_parser_search_nonascii(const char *p, const char *e)
2154{
2155 const char *s = p;
2156
2157 for (; s < e; s++) {
2158 if (*s & 0x80) return s;
2159 }
2160
2161 return NULL;
2162}
2163
2164static int
2165rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2166{
2167 const char *e = ptr + len;
2168
2169 if (enc == rb_ascii8bit_encoding()) {
2170 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2171 ptr = rb_parser_search_nonascii(ptr, e);
2172 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2173 }
2174
2175 /* parser string encoding is always asciicompat */
2176 ptr = rb_parser_search_nonascii(ptr, e);
2177 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2178 for (;;) {
2179 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2180 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2181 ptr += MBCLEN_CHARFOUND_LEN(ret);
2182 if (ptr == e) break;
2183 ptr = rb_parser_search_nonascii(ptr, e);
2184 if (!ptr) break;
2185 }
2186
2187 return RB_PARSER_ENC_CODERANGE_VALID;
2188}
2189
2190static int
2191rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2192{
2193 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2194}
2195
2196static int
2197rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2198{
2199 int cr = PARSER_ENC_CODERANGE(str);
2200
2201 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2202 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2203 PARSER_ENC_CODERANGE_SET(str, cr);
2204 }
2205
2206 return cr;
2207}
2208
2209static rb_parser_string_t *
2210rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2211{
2212 if (rb_parser_str_get_encoding(str) == enc)
2213 return str;
2214 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2215 PARSER_ENC_CODERANGE_CLEAR(str);
2216 }
2217 rb_parser_string_set_encoding(str, enc);
2218 return str;
2219}
2220
2221static bool
2222rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2223{
2224 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2225}
2226
2227static rb_encoding *
2228rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2229{
2230 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2231 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2232
2233 if (enc1 == NULL || enc2 == NULL)
2234 return 0;
2235
2236 if (enc1 == enc2) {
2237 return enc1;
2238 }
2239
2240 if (PARSER_STRING_LEN(str2) == 0)
2241 return enc1;
2242 if (PARSER_STRING_LEN(str1) == 0)
2243 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2244
2245 int cr1, cr2;
2246
2247 cr1 = rb_parser_enc_str_coderange(p, str1);
2248 cr2 = rb_parser_enc_str_coderange(p, str2);
2249
2250 if (cr1 != cr2) {
2251 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2252 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2253 }
2254
2255 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2256 return enc1;
2257 }
2258
2259 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2260 return enc2;
2261 }
2262
2263 return 0;
2264}
2265
2266static void
2267rb_parser_str_modify(rb_parser_string_t *str)
2268{
2269 PARSER_ENC_CODERANGE_CLEAR(str);
2270}
2271
2272static void
2273rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2274{
2275 long capa;
2276 const int termlen = STRING_TERM_LEN(str);
2277
2278 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2279 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2280 }
2281
2282 int cr = PARSER_ENC_CODERANGE(str);
2283 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2284 /* Leave unknown. */
2285 }
2286 else if (len > PARSER_STRING_LEN(str)) {
2287 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2288 }
2289 else if (len < PARSER_STRING_LEN(str)) {
2290 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2291 /* ASCII-only string is keeping after truncated. Valid
2292 * and broken may be invalid or valid, leave unknown. */
2293 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2294 }
2295 }
2296
2297 STRING_SET_LEN(str, len);
2298 STRING_TERM_FILL(str);
2299}
2300
2301static rb_parser_string_t *
2302rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2303{
2304 rb_parser_str_modify(str);
2305 if (len == 0) return 0;
2306
2307 long total, olen, off = -1;
2308 char *sptr;
2309 const int termlen = STRING_TERM_LEN(str);
2310
2311 PARSER_STRING_GETMEM(str, sptr, olen);
2312 if (ptr >= sptr && ptr <= sptr + olen) {
2313 off = ptr - sptr;
2314 }
2315
2316 if (olen > LONG_MAX - len) {
2317 compile_error(p, "string sizes too big");
2318 return 0;
2319 }
2320 total = olen + len;
2321 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2322 sptr = PARSER_STRING_PTR(str);
2323 if (off != -1) {
2324 ptr = sptr + off;
2325 }
2326 memcpy(sptr + olen, ptr, len);
2327 STRING_SET_LEN(str, total);
2328 STRING_TERM_FILL(str);
2329
2330 return str;
2331}
2332
2333#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2334#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2335
2336static rb_parser_string_t *
2337rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2338 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2339{
2340 int str_cr, res_cr;
2341 rb_encoding *str_enc, *res_enc;
2342
2343 str_enc = rb_parser_str_get_encoding(str);
2344 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2345
2346 if (str_enc == ptr_enc) {
2347 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2348 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2349 }
2350 }
2351 else {
2352 /* parser string encoding is always asciicompat */
2353 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2354 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2355 }
2356 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2357 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2358 str_cr = rb_parser_enc_str_coderange(p, str);
2359 }
2360 }
2361 }
2362 if (ptr_cr_ret)
2363 *ptr_cr_ret = ptr_cr;
2364
2365 if (str_enc != ptr_enc &&
2366 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2367 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2368 goto incompatible;
2369 }
2370
2371 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2372 res_enc = str_enc;
2373 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2374 }
2375 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2376 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2377 res_enc = str_enc;
2378 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2379 }
2380 else {
2381 res_enc = ptr_enc;
2382 res_cr = ptr_cr;
2383 }
2384 }
2385 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2386 res_enc = str_enc;
2387 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2388 res_cr = str_cr;
2389 else
2390 res_cr = ptr_cr;
2391 }
2392 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2393 res_enc = str_enc;
2394 res_cr = str_cr;
2395 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2396 }
2397
2398 if (len < 0) {
2399 compile_error(p, "negative string size (or size too big)");
2400 }
2401 parser_str_cat(str, ptr, len);
2402 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2403 return str;
2404
2405 incompatible:
2406 compile_error(p, "incompatible character encodings: %s and %s",
2407 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2408 UNREACHABLE_RETURN(0);
2409
2410}
2411
2412static rb_parser_string_t *
2413rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2414 rb_encoding *ptr_enc)
2415{
2416 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2417}
2418
2419static rb_parser_string_t *
2420rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2421{
2422 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2423
2424 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2425 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2426
2427 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2428
2429 return str;
2430}
2431
2432static rb_parser_string_t *
2433rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2434{
2435 if (len < 0) {
2436 rb_bug("negative string size (or size too big)");
2437 }
2438
2439 long slen = PARSER_STRING_LEN(str);
2440
2441 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2442 PARSER_ENC_CODERANGE_CLEAR(str);
2443 }
2444
2445 {
2446 long capa;
2447 const int termlen = STRING_TERM_LEN(str);
2448
2449 if ((capa = slen) < len) {
2450 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2451 }
2452 else if (len == slen) return str;
2453 STRING_SET_LEN(str, len);
2454 STRING_TERM_FILL(str);
2455 }
2456 return str;
2457}
2458
2459# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2460 ((ptrvar) = str->ptr, \
2461 (lenvar) = str->len, \
2462 (encvar) = str->enc)
2463
2464static int
2465rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2466{
2467 long len1, len2;
2468 const char *ptr1, *ptr2;
2469 rb_encoding *enc1, *enc2;
2470
2471 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2472 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2473
2474 return (len1 != len2 ||
2475 enc1 != enc2 ||
2476 memcmp(ptr1, ptr2, len1) != 0);
2477}
2478
2479static void
2480rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2481{
2482 long i;
2483 if (ary->capa < len) {
2484 ary->capa = len;
2485 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2486 for (i = ary->len; i < len; i++) {
2487 ary->data[i] = 0;
2488 }
2489 }
2490}
2491
2492/*
2493 * Do not call this directly.
2494 * Use rb_parser_ary_new_capa_for_XXX() instead.
2495 */
2496static rb_parser_ary_t *
2497parser_ary_new_capa(rb_parser_t *p, long len)
2498{
2499 if (len < 0) {
2500 rb_bug("negative array size (or size too big): %ld", len);
2501 }
2502 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2503 ary->data_type = 0;
2504 ary->len = 0;
2505 ary->capa = len;
2506 if (0 < len) {
2507 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2508 }
2509 else {
2510 ary->data = NULL;
2511 }
2512 return ary;
2513}
2514
2515#ifndef RIPPER
2516static rb_parser_ary_t *
2517rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2518{
2519 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2520 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2521 return ary;
2522}
2523
2524static rb_parser_ary_t *
2525rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2526{
2527 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2528 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2529 return ary;
2530}
2531#endif
2532
2533static rb_parser_ary_t *
2534rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2535{
2536 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2537 ary->data_type = PARSER_ARY_DATA_NODE;
2538 return ary;
2539}
2540
2541/*
2542 * Do not call this directly.
2543 * Use rb_parser_ary_push_XXX() instead.
2544 */
2545static rb_parser_ary_t *
2546parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2547{
2548 if (ary->len == ary->capa) {
2549 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2550 }
2551 ary->data[ary->len++] = val;
2552 return ary;
2553}
2554
2555#ifndef RIPPER
2556static rb_parser_ary_t *
2557rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2558{
2559 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2560 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2561 }
2562 return parser_ary_push(p, ary, val);
2563}
2564
2565static rb_parser_ary_t *
2566rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2567{
2568 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2569 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2570 }
2571 return parser_ary_push(p, ary, val);
2572}
2573#endif
2574
2575static rb_parser_ary_t *
2576rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2577{
2578 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2579 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2580 }
2581 return parser_ary_push(p, ary, val);
2582}
2583
2584#ifndef RIPPER
2585static void
2586rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2587{
2588 if (!token) return;
2589 rb_parser_string_free(p, token->str);
2590 xfree(token);
2591}
2592
2593static void
2594rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2595{
2596# define foreach_ary(ptr) \
2597 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2598 ptr < end_ary_data; ptr++)
2599 switch (ary->data_type) {
2600 case PARSER_ARY_DATA_AST_TOKEN:
2601 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2602 break;
2603 case PARSER_ARY_DATA_SCRIPT_LINE:
2604 foreach_ary(data) {rb_parser_string_free(p, *data);}
2605 break;
2606 case PARSER_ARY_DATA_NODE:
2607 /* Do nothing because nodes are freed when rb_ast_t is freed */
2608 break;
2609 default:
2610 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2611 break;
2612 }
2613# undef foreach_ary
2614 xfree(ary->data);
2615 xfree(ary);
2616}
2617
2618#endif /* !RIPPER */
2619%}
2620
2621%expect 0
2622%define api.pure
2623%define parse.error verbose
2624%printer {
2625 if ((NODE *)$$ == (NODE *)-1) {
2626 rb_parser_printf(p, "NODE_SPECIAL");
2627 }
2628 else if ($$) {
2629 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2630 }
2631} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2632 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2633%printer {
2634 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2635} <id>
2636%printer {
2637 switch (nd_type(RNODE($$))) {
2638 case NODE_INTEGER:
2639 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2640 break;
2641 case NODE_FLOAT:
2642 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2643 break;
2644 case NODE_RATIONAL:
2645 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2646 break;
2647 case NODE_IMAGINARY:
2648 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2649 break;
2650 default:
2651 break;
2652 }
2653} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2654%printer {
2655 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2656} tNTH_REF
2657%printer {
2658 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2659} tBACK_REF
2660
2661%destructor {
2662 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2663} <labels>
2664
2665%lex-param {struct parser_params *p}
2666%parse-param {struct parser_params *p}
2667%initial-action
2668{
2669 RUBY_SET_YYLLOC_OF_NONE(@$);
2670};
2671%after-shift after_shift
2672%before-reduce before_reduce
2673%after-reduce after_reduce
2674%after-shift-error-token after_shift_error_token
2675%after-pop-stack after_pop_stack
2676
2677%union {
2678 NODE *node;
2679 rb_node_fcall_t *node_fcall;
2680 rb_node_args_t *node_args;
2681 rb_node_args_aux_t *node_args_aux;
2682 rb_node_opt_arg_t *node_opt_arg;
2683 rb_node_kw_arg_t *node_kw_arg;
2684 rb_node_block_pass_t *node_block_pass;
2685 rb_node_masgn_t *node_masgn;
2686 rb_node_def_temp_t *node_def_temp;
2687 rb_node_exits_t *node_exits;
2688 struct rb_locations_lambda_body_t *locations_lambda_body;
2689 ID id;
2690 int num;
2691 st_table *tbl;
2692 st_table *labels;
2693 const struct vtable *vars;
2694 struct rb_strterm_struct *strterm;
2695 struct lex_context ctxt;
2696 enum lex_state_e state;
2697}
2698
2699%token <id>
2700 keyword_class "'class'"
2701 keyword_module "'module'"
2702 keyword_def "'def'"
2703 keyword_undef "'undef'"
2704 keyword_begin "'begin'"
2705 keyword_rescue "'rescue'"
2706 keyword_ensure "'ensure'"
2707 keyword_end "'end'"
2708 keyword_if "'if'"
2709 keyword_unless "'unless'"
2710 keyword_then "'then'"
2711 keyword_elsif "'elsif'"
2712 keyword_else "'else'"
2713 keyword_case "'case'"
2714 keyword_when "'when'"
2715 keyword_while "'while'"
2716 keyword_until "'until'"
2717 keyword_for "'for'"
2718 keyword_break "'break'"
2719 keyword_next "'next'"
2720 keyword_redo "'redo'"
2721 keyword_retry "'retry'"
2722 keyword_in "'in'"
2723 keyword_do "'do'"
2724 keyword_do_cond "'do' for condition"
2725 keyword_do_block "'do' for block"
2726 keyword_do_LAMBDA "'do' for lambda"
2727 keyword_return "'return'"
2728 keyword_yield "'yield'"
2729 keyword_super "'super'"
2730 keyword_self "'self'"
2731 keyword_nil "'nil'"
2732 keyword_true "'true'"
2733 keyword_false "'false'"
2734 keyword_and "'and'"
2735 keyword_or "'or'"
2736 keyword_not "'not'"
2737 modifier_if "'if' modifier"
2738 modifier_unless "'unless' modifier"
2739 modifier_while "'while' modifier"
2740 modifier_until "'until' modifier"
2741 modifier_rescue "'rescue' modifier"
2742 keyword_alias "'alias'"
2743 keyword_defined "'defined?'"
2744 keyword_BEGIN "'BEGIN'"
2745 keyword_END "'END'"
2746 keyword__LINE__ "'__LINE__'"
2747 keyword__FILE__ "'__FILE__'"
2748 keyword__ENCODING__ "'__ENCODING__'"
2749
2750%token <id> tIDENTIFIER "local variable or method"
2751%token <id> tFID "method"
2752%token <id> tGVAR "global variable"
2753%token <id> tIVAR "instance variable"
2754%token <id> tCONSTANT "constant"
2755%token <id> tCVAR "class variable"
2756%token <id> tLABEL "label"
2757%token <node> tINTEGER "integer literal"
2758%token <node> tFLOAT "float literal"
2759%token <node> tRATIONAL "rational literal"
2760%token <node> tIMAGINARY "imaginary literal"
2761%token <node> tCHAR "char literal"
2762%token <node> tNTH_REF "numbered reference"
2763%token <node> tBACK_REF "back reference"
2764%token <node> tSTRING_CONTENT "literal content"
2765%token <num> tREGEXP_END
2766%token <num> tDUMNY_END "dummy end"
2767
2768%type <node> singleton singleton_expr strings string string1 xstring regexp
2769%type <node> string_contents xstring_contents regexp_contents string_content
2770%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2771%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2772%type <node_def_temp> defn_head defs_head k_def
2773%type <node_exits> block_open k_while k_until k_for allow_exits
2774%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2775%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2776%type <node> command command_call command_call_value method_call
2777%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2778%type <node_fcall> fcall
2779%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2780%type <node> args arg_splat call_args opt_call_args
2781%type <node> paren_args opt_paren_args
2782%type <node_args> args_tail block_args_tail
2783%type <node> command_args aref_args
2784%type <node_block_pass> opt_block_arg block_arg
2785%type <node> var_ref var_lhs
2786%type <node> command_rhs arg_rhs
2787%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2788%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args f_empty_arg
2789%type <node_args_aux> f_arg f_arg_item
2790%type <node> f_marg f_rest_marg
2791%type <node_masgn> f_margs
2792%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2793%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2794%type <id> do bv_decls opt_bv_decl bvar
2795%type <node> lambda brace_body do_body
2796%type <locations_lambda_body> lambda_body
2797%type <node_args> f_larglist f_largs largs_tail
2798%type <node> brace_block cmd_brace_block do_block lhs none fitem
2799%type <node> mlhs_head mlhs_item mlhs_node
2800%type <node_masgn> mlhs mlhs_basic mlhs_inner
2801%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2802%type <node> p_expr p_as p_alt p_expr_basic p_find
2803%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2804%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2805%type <node> p_kwargs p_kwarg p_kw
2806%type <id> keyword_variable user_variable sym operation2 operation3
2807%type <id> cname fname op f_rest_arg f_block_arg opt_comma f_norm_arg f_bad_arg
2808%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2809%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2810%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2811%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2812%type <ctxt> p_in_kwarg
2813%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2814%type <num> max_numparam
2815%type <node> numparam
2816%type <id> it_id
2817%token END_OF_INPUT 0 "end-of-input"
2818%token <id> '.'
2819
2820/* escaped chars, should be ignored otherwise */
2821%token <id> '\\' "backslash"
2822%token tSP "escaped space"
2823%token <id> '\t' "escaped horizontal tab"
2824%token <id> '\f' "escaped form feed"
2825%token <id> '\r' "escaped carriage return"
2826%token <id> '\13' "escaped vertical tab"
2827%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2828%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2829%token tPOW RUBY_TOKEN(POW) "**"
2830%token tCMP RUBY_TOKEN(CMP) "<=>"
2831%token tEQ RUBY_TOKEN(EQ) "=="
2832%token tEQQ RUBY_TOKEN(EQQ) "==="
2833%token tNEQ RUBY_TOKEN(NEQ) "!="
2834%token tGEQ RUBY_TOKEN(GEQ) ">="
2835%token tLEQ RUBY_TOKEN(LEQ) "<="
2836%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2837%token tOROP RUBY_TOKEN(OROP) "||"
2838%token tMATCH RUBY_TOKEN(MATCH) "=~"
2839%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2840%token tDOT2 RUBY_TOKEN(DOT2) ".."
2841%token tDOT3 RUBY_TOKEN(DOT3) "..."
2842%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2843%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2844%token tAREF RUBY_TOKEN(AREF) "[]"
2845%token tASET RUBY_TOKEN(ASET) "[]="
2846%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2847%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2848%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2849%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2850%token tCOLON3 ":: at EXPR_BEG"
2851%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2852%token tASSOC "=>"
2853%token tLPAREN "("
2854%token tLPAREN_ARG "( arg"
2855%token tLBRACK "["
2856%token tLBRACE "{"
2857%token tLBRACE_ARG "{ arg"
2858%token tSTAR "*"
2859%token tDSTAR "**arg"
2860%token tAMPER "&"
2861%token <num> tLAMBDA "->"
2862%token tSYMBEG "symbol literal"
2863%token tSTRING_BEG "string literal"
2864%token tXSTRING_BEG "backtick literal"
2865%token tREGEXP_BEG "regexp literal"
2866%token tWORDS_BEG "word list"
2867%token tQWORDS_BEG "verbatim word list"
2868%token tSYMBOLS_BEG "symbol list"
2869%token tQSYMBOLS_BEG "verbatim symbol list"
2870%token tSTRING_END "terminator"
2871%token tSTRING_DEND "'}'"
2872%token <state> tSTRING_DBEG "'#{'"
2873%token tSTRING_DVAR tLAMBEG tLABEL_END
2874
2875%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2876%token tHEREDOC_BEG tHEREDOC_END k__END__
2877
2878/*
2879 * precedence table
2880 */
2881
2882%nonassoc tLOWEST
2883%nonassoc tLBRACE_ARG
2884
2885%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2886%left keyword_or keyword_and
2887%right keyword_not
2888%nonassoc keyword_defined
2889%right '=' tOP_ASGN
2890%left modifier_rescue
2891%right '?' ':'
2892%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2893%left tOROP
2894%left tANDOP
2895%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2896%left '>' tGEQ '<' tLEQ
2897%left '|' '^'
2898%left '&'
2899%left tLSHFT tRSHFT
2900%left '+' '-'
2901%left '*' '/' '%'
2902%right tUMINUS_NUM tUMINUS
2903%right tPOW
2904%right '!' '~' tUPLUS
2905
2906%token tLAST_TOKEN
2907
2908/*
2909 * inlining rules
2910 */
2911%rule %inline ident_or_const
2912 : tIDENTIFIER
2913 | tCONSTANT
2914 ;
2915
2916%rule %inline user_or_keyword_variable
2917 : user_variable
2918 | keyword_variable
2919 ;
2920
2921/*
2922 * parameterizing rules
2923 */
2924%rule asgn(rhs) <node>
2925 : lhs '=' lex_ctxt rhs
2926 {
2927 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2928 /*% ripper: assign!($:1, $:4) %*/
2929 }
2930 ;
2931
2932%rule args_tail_basic(value, trailing) <node_args>
2933 : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing)
2934 {
2935 $$ = new_args_tail(p, $1, $3, $4, &@3);
2936 /*% ripper: [$:1, $:3, $:4] %*/
2937 }
2938 | f_kwarg(value) opt_f_block_arg(trailing)
2939 {
2940 $$ = new_args_tail(p, $1, 0, $2, &@1);
2941 /*% ripper: [$:1, Qnil, $:2] %*/
2942 }
2943 | f_any_kwrest opt_f_block_arg(trailing)
2944 {
2945 $$ = new_args_tail(p, 0, $1, $2, &@1);
2946 /*% ripper: [Qnil, $:1, $:2] %*/
2947 }
2948 | f_block_arg
2949 {
2950 $$ = new_args_tail(p, 0, 0, $1, &@1);
2951 /*% ripper: [Qnil, Qnil, $:1] %*/
2952 }
2953 ;
2954
2955%rule opt_f_block_arg(trailing) <id>
2956 : ',' f_block_arg
2957 {
2958 $$ = $2;
2959 /*% ripper: $:2 %*/
2960 }
2961 | trailing
2962 ;
2963
2964%rule def_endless_method(bodystmt) <node>
2965 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2966 {
2967 endless_method_name(p, $head->nd_mid, &@head);
2968 restore_defun(p, $head);
2969 ($$ = $head->nd_def)->nd_loc = @$;
2970 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2971 RNODE_DEFN($$)->nd_defn = $bodystmt;
2972 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2973 /*% ripper: def!($:head, $:args, $:$) %*/
2974 local_pop(p);
2975 }
2976 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2977 {
2978 endless_method_name(p, $head->nd_mid, &@head);
2979 restore_defun(p, $head);
2980 ($$ = $head->nd_def)->nd_loc = @$;
2981 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2982 RNODE_DEFS($$)->nd_defn = $bodystmt;
2983 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2984 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2985 local_pop(p);
2986 }
2987 ;
2988
2989%rule compstmt(stmts) <node>
2990 : stmts terms?
2991 {
2992 void_stmts(p, $$ = $stmts);
2993 }
2994 ;
2995
2996%rule f_opt(value) <node_opt_arg>
2997 : f_arg_asgn f_eq value
2998 {
2999 p->ctxt.in_argdef = 1;
3000 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
3001 /*% ripper: [$:$, $:3] %*/
3002 }
3003 ;
3004
3005%rule f_opt_arg(value) <node_opt_arg>
3006 : f_opt(value)
3007 {
3008 $$ = $f_opt;
3009 /*% ripper: rb_ary_new3(1, $:1) %*/
3010 }
3011 | f_opt_arg(value) ',' f_opt(value)
3012 {
3013 $$ = opt_arg_append($f_opt_arg, $f_opt);
3014 /*% ripper: rb_ary_push($:1, $:3) %*/
3015 }
3016 ;
3017
3018%rule f_kw(value) <node_kw_arg>
3019 : f_label value
3020 {
3021 p->ctxt.in_argdef = 1;
3022 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3023 /*% ripper: [$:$, $:value] %*/
3024 }
3025 | f_label
3026 {
3027 p->ctxt.in_argdef = 1;
3028 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3029 /*% ripper: [$:$, 0] %*/
3030 }
3031 ;
3032
3033%rule f_kwarg(value) <node_kw_arg>
3034 : f_kw(value)
3035 {
3036 $$ = $f_kw;
3037 /*% ripper: rb_ary_new3(1, $:1) %*/
3038 }
3039 | f_kwarg(value) ',' f_kw(value)
3040 {
3041 $$ = kwd_append($f_kwarg, $f_kw);
3042 /*% ripper: rb_ary_push($:1, $:3) %*/
3043 }
3044 ;
3045
3046%rule mlhs_items(item) <node>
3047 : item
3048 {
3049 $$ = NEW_LIST($1, &@$);
3050 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3051 }
3052 | mlhs_items(item) ',' item
3053 {
3054 $$ = list_append(p, $1, $3);
3055 /*% ripper: mlhs_add!($:1, $:3) %*/
3056 }
3057 ;
3058
3059%rule op_asgn(rhs) <node>
3060 : var_lhs tOP_ASGN lex_ctxt rhs
3061 {
3062 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3063 /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
3064 }
3065 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3066 {
3067 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3068 /*% ripper: opassign!(aref_field!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/
3069 }
3070 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3071 {
3072 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3073 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3074 }
3075 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3076 {
3077 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3078 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3079 }
3080 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3081 {
3082 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3083 /*% ripper: opassign!(field!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3084 }
3085 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3086 {
3087 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3088 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3089 /*% ripper: opassign!(const_path_field!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3090 }
3091 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3092 {
3093 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3094 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3095 /*% ripper: opassign!(top_const_field!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3096 }
3097 | backref tOP_ASGN lex_ctxt rhs
3098 {
3099 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3100 $$ = NEW_ERROR(&@$);
3101 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:backref), $:tOP_ASGN, $:rhs)) %*/
3102 }
3103 ;
3104
3105%rule opt_args_tail(tail, trailing) <node_args>
3106 : ',' tail
3107 {
3108 $$ = $tail;
3109 /*% ripper: $:tail %*/
3110 }
3111 | trailing
3112 {
3113 $$ = new_empty_args_tail(p, &@$);
3114 /*% ripper: [Qnil, Qnil, Qnil] %*/
3115 }
3116 ;
3117
3118%rule range_expr(range) <node>
3119 : range tDOT2 range
3120 {
3121 value_expr(p, $1);
3122 value_expr(p, $3);
3123 $$ = NEW_DOT2($1, $3, &@$, &@2);
3124 /*% ripper: dot2!($:1, $:3) %*/
3125 }
3126 | range tDOT3 range
3127 {
3128 value_expr(p, $1);
3129 value_expr(p, $3);
3130 $$ = NEW_DOT3($1, $3, &@$, &@2);
3131 /*% ripper: dot3!($:1, $:3) %*/
3132 }
3133 | range tDOT2
3134 {
3135 value_expr(p, $1);
3136 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3137 /*% ripper: dot2!($:1, Qnil) %*/
3138 }
3139 | range tDOT3
3140 {
3141 value_expr(p, $1);
3142 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3143 /*% ripper: dot3!($:1, Qnil) %*/
3144 }
3145 | tBDOT2 range
3146 {
3147 value_expr(p, $2);
3148 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3149 /*% ripper: dot2!(Qnil, $:2) %*/
3150 }
3151 | tBDOT3 range
3152 {
3153 value_expr(p, $2);
3154 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3155 /*% ripper: dot3!(Qnil, $:2) %*/
3156 }
3157 ;
3158
3159%rule value_expr(value) <node>
3160 : value
3161 {
3162 value_expr(p, $1);
3163 $$ = $1;
3164 }
3165 ;
3166
3167%rule words(begin, word_list) <node>
3168 : begin ' '+ word_list tSTRING_END
3169 {
3170 $$ = make_list($word_list, &@$);
3171 /*% ripper: array!($:word_list) %*/
3172 }
3173 ;
3174
3175%%
3176program : {
3177 SET_LEX_STATE(EXPR_BEG);
3178 local_push(p, ifndef_ripper(1)+0);
3179 /* jumps are possible in the top-level loop. */
3180 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3181 }
3182 compstmt(top_stmts)
3183 {
3184 if ($2 && !compile_for_eval) {
3185 NODE *node = $2;
3186 /* last expression should not be void */
3187 if (nd_type_p(node, NODE_BLOCK)) {
3188 while (RNODE_BLOCK(node)->nd_next) {
3189 node = RNODE_BLOCK(node)->nd_next;
3190 }
3191 node = RNODE_BLOCK(node)->nd_head;
3192 }
3193 node = remove_begin(node);
3194 void_expr(p, node);
3195 }
3196 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3197 /*% ripper[final]: program!($:2) %*/
3198 local_pop(p);
3199 }
3200 ;
3201
3202top_stmts : none
3203 {
3204 $$ = NEW_BEGIN(0, &@$);
3205 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3206 }
3207 | top_stmt
3208 {
3209 $$ = newline_node($1);
3210 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3211 }
3212 | top_stmts terms top_stmt
3213 {
3214 $$ = block_append(p, $1, newline_node($3));
3215 /*% ripper: stmts_add!($:1, $:3) %*/
3216 }
3217 ;
3218
3219top_stmt : stmt
3220 {
3221 clear_block_exit(p, true);
3222 $$ = $1;
3223 }
3224 | keyword_BEGIN begin_block
3225 {
3226 $$ = $2;
3227 /*% ripper: $:2 %*/
3228 }
3229 ;
3230
3231block_open : '{' {$$ = init_block_exit(p);};
3232
3233begin_block : block_open compstmt(top_stmts) '}'
3234 {
3235 restore_block_exit(p, $block_open);
3236 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3237 NEW_BEGIN($compstmt, &@$));
3238 $$ = NEW_BEGIN(0, &@$);
3239 /*% ripper: BEGIN!($:compstmt) %*/
3240 }
3241 ;
3242
3243bodystmt : compstmt(stmts)[body]
3244 lex_ctxt[ctxt]
3245 opt_rescue
3246 k_else
3247 {
3248 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3249 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3250 }
3251 compstmt(stmts)[elsebody]
3252 {
3253 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3254 }
3255 opt_ensure
3256 {
3257 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3258 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3259 }
3260 | compstmt(stmts)[body]
3261 lex_ctxt[ctxt]
3262 opt_rescue
3263 {
3264 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3265 }
3266 opt_ensure
3267 {
3268 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3269 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3270 }
3271 ;
3272
3273stmts : none
3274 {
3275 $$ = NEW_BEGIN(0, &@$);
3276 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3277 }
3278 | stmt_or_begin
3279 {
3280 $$ = newline_node($1);
3281 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3282 }
3283 | stmts terms stmt_or_begin
3284 {
3285 $$ = block_append(p, $1, newline_node($3));
3286 /*% ripper: stmts_add!($:1, $:3) %*/
3287 }
3288 ;
3289
3290stmt_or_begin : stmt
3291 | keyword_BEGIN
3292 {
3293 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3294 }
3295 begin_block
3296 {
3297 $$ = $3;
3298 }
3299 ;
3300
3301allow_exits : {$$ = allow_block_exit(p);};
3302
3303k_END : keyword_END lex_ctxt
3304 {
3305 if (p->ctxt.in_def) {
3306 rb_warn0("END in method; use at_exit");
3307 }
3308 $$ = $2;
3309 p->ctxt.in_rescue = before_rescue;
3310 /*% ripper: $:2 %*/
3311 };
3312
3313stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3314 {
3315 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3316 /*% ripper: alias!($:new, $:old) %*/
3317 }
3318 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3319 {
3320 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3321 /*% ripper: var_alias!($:new, $:old) %*/
3322 }
3323 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
3324 {
3325 char buf[2];
3326 buf[0] = '$';
3327 buf[1] = (char)RNODE_BACK_REF($old)->nd_nth;
3328 $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw);
3329 /*% ripper: var_alias!($:new, $:old) %*/
3330 }
3331 | keyword_alias tGVAR tNTH_REF[nth]
3332 {
3333 static const char mesg[] = "can't make alias for the number variables";
3334 /*%%%*/
3335 yyerror1(&@nth, mesg);
3336 /*% %*/
3337 $$ = NEW_ERROR(&@$);
3338 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3339 }
3340 | keyword_undef[kw] undef_list[list]
3341 {
3342 nd_set_first_loc($list, @kw.beg_pos);
3343 RNODE_UNDEF($list)->keyword_loc = @kw;
3344 $$ = $list;
3345 /*% ripper: undef!($:list) %*/
3346 }
3347 | stmt[body] modifier_if[mod] expr_value[cond]
3348 {
3349 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3350 fixpos($$, $cond);
3351 /*% ripper: if_mod!($:cond, $:body) %*/
3352 }
3353 | stmt[body] modifier_unless[mod] expr_value[cond]
3354 {
3355 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3356 fixpos($$, $cond);
3357 /*% ripper: unless_mod!($:cond, $:body) %*/
3358 }
3359 | stmt[body] modifier_while[mod] expr_value[cond_expr]
3360 {
3361 clear_block_exit(p, false);
3362 if ($body && nd_type_p($body, NODE_BEGIN)) {
3363 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3364 }
3365 else {
3366 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3367 }
3368 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3369 }
3370 | stmt[body] modifier_until[mod] expr_value[cond_expr]
3371 {
3372 clear_block_exit(p, 0);
3373 if ($body && nd_type_p($body, NODE_BEGIN)) {
3374 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3375 }
3376 else {
3377 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3378 }
3379 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3380 }
3381 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3382 {
3383 p->ctxt.in_rescue = $ctxt.in_rescue;
3384 NODE *resq;
3385 YYLTYPE loc = code_loc_gen(&@mod, &@resbody);
3386 resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3387 $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$);
3388 /*% ripper: rescue_mod!($:body, $:resbody) %*/
3389 }
3390 | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace]
3391 {
3392 clear_block_exit(p, true);
3393 restore_block_exit(p, $block_open);
3394 p->ctxt = $k_end;
3395 {
3396 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$);
3397 $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace);
3398 RNODE_SCOPE(scope)->nd_parent = $$;
3399 }
3400 /*% ripper: END!($:body) %*/
3401 }
3402 | command_asgn
3403 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3404 {
3405 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3406 /*% ripper: massign!($:lhs, $:rhs) %*/
3407 }
3408 | asgn(mrhs)
3409 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3410 after_rescue[after_rescue] stmt[resbody]
3411 {
3412 p->ctxt.in_rescue = $after_rescue.in_rescue;
3413 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3414 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3415 loc.beg_pos = @mrhs_arg.beg_pos;
3416 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3417 $$ = node_assign(p, (NODE *)$lhs, $mrhs_arg, $lex_ctxt, &@$);
3418 /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/
3419 }
3420 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3421 {
3422 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3423 /*% ripper: massign!($:lhs, $:rhs) %*/
3424 }
3425 | expr
3426 | error
3427 {
3428 (void)yynerrs;
3429 $$ = NEW_ERROR(&@$);
3430 }
3431 ;
3432
3433command_asgn : asgn(command_rhs)
3434 | op_asgn(command_rhs)
3435 | def_endless_method(endless_command)
3436 ;
3437
3438endless_command : command
3439 | endless_command modifier_rescue after_rescue arg
3440 {
3441 p->ctxt.in_rescue = $3.in_rescue;
3442 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3443 /*% ripper: rescue_mod!($:1, $:4) %*/
3444 }
3445 | keyword_not '\n'? endless_command
3446 {
3447 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3448 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3449 }
3450 ;
3451
3452command_rhs : command_call_value %prec tOP_ASGN
3453 | command_call_value modifier_rescue after_rescue stmt
3454 {
3455 p->ctxt.in_rescue = $3.in_rescue;
3456 YYLTYPE loc = code_loc_gen(&@2, &@4);
3457 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3458 /*% ripper: rescue_mod!($:1, $:4) %*/
3459 }
3460 | command_asgn
3461 ;
3462
3463expr : command_call
3464 | expr[left] keyword_and[op] expr[right]
3465 {
3466 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3467 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3468 }
3469 | expr[left] keyword_or[op] expr[right]
3470 {
3471 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3472 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3473 }
3474 | keyword_not[not] '\n'? expr[arg]
3475 {
3476 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3477 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3478 }
3479 | '!'[not] command_call[arg]
3480 {
3481 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3482 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3483 }
3484 | arg tASSOC[assoc]
3485 {
3486 value_expr(p, $arg);
3487 }
3488 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3489 p_top_expr_body[body]
3490 {
3491 pop_pktbl(p, $p_pktbl);
3492 pop_pvtbl(p, $p_pvtbl);
3493 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3494 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3495 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3496 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@assoc), &@$, &NULL_LOC, &NULL_LOC);
3497 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3498 }
3499 | arg keyword_in
3500 {
3501 value_expr(p, $arg);
3502 }
3503 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3504 p_top_expr_body[body]
3505 {
3506 pop_pktbl(p, $p_pktbl);
3507 pop_pvtbl(p, $p_pvtbl);
3508 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3509 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3510 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3511 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3512 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3513 }
3514 | arg %prec tLBRACE_ARG
3515 ;
3516
3517def_name : fname
3518 {
3519 numparam_name(p, $fname);
3520 local_push(p, 0);
3521 p->ctxt.in_def = 1;
3522 p->ctxt.in_rescue = before_rescue;
3523 p->ctxt.cant_return = 0;
3524 $$ = $fname;
3525 }
3526 ;
3527
3528defn_head : k_def def_name
3529 {
3530 $$ = def_head_save(p, $k_def);
3531 $$->nd_mid = $def_name;
3532 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3533 /*% ripper: $:def_name %*/
3534 }
3535 ;
3536
3537defs_head : k_def singleton dot_or_colon
3538 {
3539 SET_LEX_STATE(EXPR_FNAME);
3540 }
3541 def_name
3542 {
3543 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3544 $$ = def_head_save(p, $k_def);
3545 $$->nd_mid = $def_name;
3546 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3547 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3548 }
3549 ;
3550
3551expr_value : value_expr(expr)
3552 | error
3553 {
3554 $$ = NEW_ERROR(&@$);
3555 }
3556 ;
3557
3558expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3559 {
3560 $$ = $2;
3561 /*% ripper: $:2 %*/
3562 }
3563 ;
3564
3565command_call : command
3566 | block_command
3567 ;
3568
3569command_call_value : value_expr(command_call)
3570 ;
3571
3572block_command : block_call
3573 | block_call call_op2 operation2 command_args
3574 {
3575 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3576 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3577 }
3578 ;
3579
3580cmd_brace_block : tLBRACE_ARG brace_body '}'
3581 {
3582 $$ = $2;
3583 set_embraced_location($$, &@1, &@3);
3584 /*% ripper: $:2 %*/
3585 }
3586 ;
3587
3588fcall : operation
3589 {
3590 $$ = NEW_FCALL($1, 0, &@$);
3591 /*% ripper: $:1 %*/
3592 }
3593 ;
3594
3595command : fcall command_args %prec tLOWEST
3596 {
3597 $1->nd_args = $2;
3598 nd_set_last_loc($1, @2.end_pos);
3599 $$ = (NODE *)$1;
3600 /*% ripper: command!($:1, $:2) %*/
3601 }
3602 | fcall command_args cmd_brace_block
3603 {
3604 block_dup_check(p, $2, $3);
3605 $1->nd_args = $2;
3606 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3607 fixpos($$, RNODE($1));
3608 nd_set_last_loc($1, @2.end_pos);
3609 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3610 }
3611 | primary_value call_op operation2 command_args %prec tLOWEST
3612 {
3613 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3614 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3615 }
3616 | primary_value call_op operation2 command_args cmd_brace_block
3617 {
3618 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3619 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3620 }
3621 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3622 {
3623 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3624 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3625 }
3626 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3627 {
3628 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3629 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3630 }
3631 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3632 {
3633 set_embraced_location($5, &@4, &@6);
3634 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3635 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3636 }
3637 | keyword_super command_args
3638 {
3639 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3640 fixpos($$, $2);
3641 /*% ripper: super!($:2) %*/
3642 }
3643 | k_yield command_args
3644 {
3645 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3646 fixpos($$, $2);
3647 /*% ripper: yield!($:2) %*/
3648 }
3649 | k_return call_args
3650 {
3651 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3652 /*% ripper: return!($:2) %*/
3653 }
3654 | keyword_break call_args
3655 {
3656 NODE *args = 0;
3657 args = ret_args(p, $2);
3658 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3659 /*% ripper: break!($:2) %*/
3660 }
3661 | keyword_next call_args
3662 {
3663 NODE *args = 0;
3664 args = ret_args(p, $2);
3665 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3666 /*% ripper: next!($:2) %*/
3667 }
3668 ;
3669
3670mlhs : mlhs_basic
3671 | tLPAREN mlhs_inner rparen
3672 {
3673 $$ = $2;
3674 /*% ripper: mlhs_paren!($:2) %*/
3675 }
3676 ;
3677
3678mlhs_inner : mlhs_basic
3679 | tLPAREN mlhs_inner rparen
3680 {
3681 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3682 /*% ripper: mlhs_paren!($:2) %*/
3683 }
3684 ;
3685
3686mlhs_basic : mlhs_head
3687 {
3688 $$ = NEW_MASGN($1, 0, &@$);
3689 /*% ripper: $:1 %*/
3690 }
3691 | mlhs_head mlhs_item
3692 {
3693 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3694 /*% ripper: mlhs_add!($:1, $:2) %*/
3695 }
3696 | mlhs_head tSTAR mlhs_node
3697 {
3698 $$ = NEW_MASGN($1, $3, &@$);
3699 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3700 }
3701 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3702 {
3703 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3704 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3705 }
3706 | mlhs_head tSTAR
3707 {
3708 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3709 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3710 }
3711 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3712 {
3713 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3714 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3715 }
3716 | tSTAR mlhs_node
3717 {
3718 $$ = NEW_MASGN(0, $2, &@$);
3719 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3720 }
3721 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3722 {
3723 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3724 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3725 }
3726 | tSTAR
3727 {
3728 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3729 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3730 }
3731 | tSTAR ',' mlhs_items(mlhs_item)
3732 {
3733 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3734 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3735 }
3736 ;
3737
3738mlhs_item : mlhs_node
3739 | tLPAREN mlhs_inner rparen
3740 {
3741 $$ = (NODE *)$2;
3742 /*% ripper: mlhs_paren!($:2) %*/
3743 }
3744 ;
3745
3746mlhs_head : mlhs_item ','
3747 {
3748 $$ = NEW_LIST($1, &@1);
3749 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3750 }
3751 | mlhs_head mlhs_item ','
3752 {
3753 $$ = list_append(p, $1, $2);
3754 /*% ripper: mlhs_add!($:1, $:2) %*/
3755 }
3756 ;
3757
3758
3759mlhs_node : user_or_keyword_variable
3760 {
3761 /*% ripper: var_field!($:1) %*/
3762 $$ = assignable(p, $1, 0, &@$);
3763 }
3764 | primary_value '[' opt_call_args rbracket
3765 {
3766 $$ = aryset(p, $1, $3, &@$);
3767 /*% ripper: aref_field!($:1, $:3) %*/
3768 }
3769 | primary_value call_op ident_or_const
3770 {
3771 anddot_multiple_assignment_check(p, &@2, $2);
3772 $$ = attrset(p, $1, $2, $3, &@$);
3773 /*% ripper: field!($:1, $:2, $:3) %*/
3774 }
3775 | primary_value tCOLON2 tIDENTIFIER
3776 {
3777 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3778 /*% ripper: const_path_field!($:1, $:3) %*/
3779 }
3780 | primary_value tCOLON2 tCONSTANT
3781 {
3782 /*% ripper: const_path_field!($:1, $:3) %*/
3783 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3784 }
3785 | tCOLON3 tCONSTANT
3786 {
3787 /*% ripper: top_const_field!($:2) %*/
3788 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3789 }
3790 | backref
3791 {
3792 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3793 $$ = NEW_ERROR(&@$);
3794 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3795 }
3796 ;
3797
3798lhs : user_or_keyword_variable
3799 {
3800 /*% ripper: var_field!($:1) %*/
3801 $$ = assignable(p, $1, 0, &@$);
3802 }
3803 | primary_value '[' opt_call_args rbracket
3804 {
3805 $$ = aryset(p, $1, $3, &@$);
3806 /*% ripper: aref_field!($:1, $:3) %*/
3807 }
3808 | primary_value call_op ident_or_const
3809 {
3810 $$ = attrset(p, $1, $2, $3, &@$);
3811 /*% ripper: field!($:1, $:2, $:3) %*/
3812 }
3813 | primary_value tCOLON2 tIDENTIFIER
3814 {
3815 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3816 /*% ripper: field!($:1, $:2, $:3) %*/
3817 }
3818 | primary_value tCOLON2 tCONSTANT
3819 {
3820 /*% ripper: const_path_field!($:1, $:3) %*/
3821 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3822 }
3823 | tCOLON3 tCONSTANT
3824 {
3825 /*% ripper: top_const_field!($:2) %*/
3826 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3827 }
3828 | backref
3829 {
3830 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3831 $$ = NEW_ERROR(&@$);
3832 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3833 }
3834 ;
3835
3836cname : tIDENTIFIER
3837 {
3838 static const char mesg[] = "class/module name must be CONSTANT";
3839 /*%%%*/
3840 yyerror1(&@1, mesg);
3841 /*% %*/
3842 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3843 }
3844 | tCONSTANT
3845 ;
3846
3847cpath : tCOLON3 cname
3848 {
3849 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3850 /*% ripper: top_const_ref!($:2) %*/
3851 }
3852 | cname
3853 {
3854 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3855 /*% ripper: const_ref!($:1) %*/
3856 }
3857 | primary_value tCOLON2 cname
3858 {
3859 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3860 /*% ripper: const_path_ref!($:1, $:3) %*/
3861 }
3862 ;
3863
3864fname : operation
3865 | op
3866 {
3867 SET_LEX_STATE(EXPR_ENDFN);
3868 $$ = $1;
3869 }
3870 | reswords
3871 ;
3872
3873fitem : fname
3874 {
3875 $$ = NEW_SYM(rb_id2str($1), &@$);
3876 /*% ripper: symbol_literal!($:1) %*/
3877 }
3878 | symbol
3879 ;
3880
3881undef_list : fitem
3882 {
3883 $$ = NEW_UNDEF($1, &@$);
3884 /*% ripper: rb_ary_new3(1, $:1) %*/
3885 }
3886 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3887 {
3888 nd_set_last_loc($1, @4.end_pos);
3889 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3890 /*% ripper: rb_ary_push($:1, $:4) %*/
3891 }
3892 ;
3893
3894op : '|' { $$ = '|'; }
3895 | '^' { $$ = '^'; }
3896 | '&' { $$ = '&'; }
3897 | tCMP { $$ = tCMP; }
3898 | tEQ { $$ = tEQ; }
3899 | tEQQ { $$ = tEQQ; }
3900 | tMATCH { $$ = tMATCH; }
3901 | tNMATCH { $$ = tNMATCH; }
3902 | '>' { $$ = '>'; }
3903 | tGEQ { $$ = tGEQ; }
3904 | '<' { $$ = '<'; }
3905 | tLEQ { $$ = tLEQ; }
3906 | tNEQ { $$ = tNEQ; }
3907 | tLSHFT { $$ = tLSHFT; }
3908 | tRSHFT { $$ = tRSHFT; }
3909 | '+' { $$ = '+'; }
3910 | '-' { $$ = '-'; }
3911 | '*' { $$ = '*'; }
3912 | tSTAR { $$ = '*'; }
3913 | '/' { $$ = '/'; }
3914 | '%' { $$ = '%'; }
3915 | tPOW { $$ = tPOW; }
3916 | tDSTAR { $$ = tDSTAR; }
3917 | '!' { $$ = '!'; }
3918 | '~' { $$ = '~'; }
3919 | tUPLUS { $$ = tUPLUS; }
3920 | tUMINUS { $$ = tUMINUS; }
3921 | tAREF { $$ = tAREF; }
3922 | tASET { $$ = tASET; }
3923 | '`' { $$ = '`'; }
3924 ;
3925
3926reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3927 | keyword_BEGIN | keyword_END
3928 | keyword_alias | keyword_and | keyword_begin
3929 | keyword_break | keyword_case | keyword_class | keyword_def
3930 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3931 | keyword_end | keyword_ensure | keyword_false
3932 | keyword_for | keyword_in | keyword_module | keyword_next
3933 | keyword_nil | keyword_not | keyword_or | keyword_redo
3934 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3935 | keyword_super | keyword_then | keyword_true | keyword_undef
3936 | keyword_when | keyword_yield | keyword_if | keyword_unless
3937 | keyword_while | keyword_until
3938 ;
3939
3940arg : asgn(arg_rhs)
3941 | op_asgn(arg_rhs)
3942 | range_expr(arg)
3943 | arg '+' arg
3944 {
3945 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3946 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3947 }
3948 | arg '-' arg
3949 {
3950 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3951 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3952 }
3953 | arg '*' arg
3954 {
3955 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3956 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3957 }
3958 | arg '/' arg
3959 {
3960 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3961 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3962 }
3963 | arg '%' arg
3964 {
3965 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3966 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3967 }
3968 | arg tPOW arg
3969 {
3970 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3971 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3972 }
3973 | tUMINUS_NUM simple_numeric tPOW arg
3974 {
3975 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3976 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3977 }
3978 | tUPLUS arg
3979 {
3980 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3981 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3982 }
3983 | tUMINUS arg
3984 {
3985 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3986 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3987 }
3988 | arg '|' arg
3989 {
3990 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3991 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3992 }
3993 | arg '^' arg
3994 {
3995 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3996 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3997 }
3998 | arg '&' arg
3999 {
4000 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
4001 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
4002 }
4003 | arg tCMP arg
4004 {
4005 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
4006 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
4007 }
4008 | rel_expr %prec tCMP
4009 | arg tEQ arg
4010 {
4011 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4012 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4013 }
4014 | arg tEQQ arg
4015 {
4016 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4017 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4018 }
4019 | arg tNEQ arg
4020 {
4021 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4022 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4023 }
4024 | arg tMATCH arg
4025 {
4026 $$ = match_op(p, $1, $3, &@2, &@$);
4027 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4028 }
4029 | arg tNMATCH arg
4030 {
4031 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4032 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4033 }
4034 | '!' arg
4035 {
4036 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4037 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4038 }
4039 | '~' arg
4040 {
4041 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4042 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4043 }
4044 | arg tLSHFT arg
4045 {
4046 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4047 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4048 }
4049 | arg tRSHFT arg
4050 {
4051 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4052 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4053 }
4054 | arg tANDOP arg
4055 {
4056 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4057 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4058 }
4059 | arg tOROP arg
4060 {
4061 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4062 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4063 }
4064 | keyword_defined '\n'? begin_defined arg
4065 {
4066 p->ctxt.in_defined = $3.in_defined;
4067 $$ = new_defined(p, $4, &@$, &@1);
4068 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4069 /*% ripper: defined!($:4) %*/
4070 }
4071 | def_endless_method(endless_arg)
4072 | ternary
4073 | primary
4074 ;
4075
4076ternary : arg '?' arg '\n'? ':' arg
4077 {
4078 value_expr(p, $1);
4079 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4080 fixpos($$, $1);
4081 /*% ripper: ifop!($:1, $:3, $:6) %*/
4082 }
4083 ;
4084
4085endless_arg : arg %prec modifier_rescue
4086 | endless_arg modifier_rescue after_rescue arg
4087 {
4088 p->ctxt.in_rescue = $3.in_rescue;
4089 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4090 /*% ripper: rescue_mod!($:1, $:4) %*/
4091 }
4092 | keyword_not '\n'? endless_arg
4093 {
4094 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4095 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4096 }
4097 ;
4098
4099relop : '>' {$$ = '>';}
4100 | '<' {$$ = '<';}
4101 | tGEQ {$$ = idGE;}
4102 | tLEQ {$$ = idLE;}
4103 ;
4104
4105rel_expr : arg relop arg %prec '>'
4106 {
4107 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4108 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4109 }
4110 | rel_expr relop arg %prec '>'
4111 {
4112 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4113 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4114 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4115 }
4116 ;
4117
4118lex_ctxt : none
4119 {
4120 $$ = p->ctxt;
4121 }
4122 ;
4123
4124begin_defined : lex_ctxt
4125 {
4126 p->ctxt.in_defined = 1;
4127 $$ = $1;
4128 }
4129 ;
4130
4131after_rescue : lex_ctxt
4132 {
4133 p->ctxt.in_rescue = after_rescue;
4134 $$ = $1;
4135 }
4136 ;
4137
4138arg_value : value_expr(arg)
4139 ;
4140
4141aref_args : none
4142 | args trailer
4143 | args ',' assocs trailer
4144 {
4145 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4146 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4147 }
4148 | assocs trailer
4149 {
4150 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4151 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4152 }
4153 ;
4154
4155arg_rhs : arg %prec tOP_ASGN
4156 {
4157 value_expr(p, $1);
4158 $$ = $1;
4159 }
4160 | arg modifier_rescue after_rescue arg
4161 {
4162 p->ctxt.in_rescue = $3.in_rescue;
4163 value_expr(p, $1);
4164 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4165 /*% ripper: rescue_mod!($:1, $:4) %*/
4166 }
4167 ;
4168
4169paren_args : '(' opt_call_args rparen
4170 {
4171 $$ = $2;
4172 /*% ripper: arg_paren!($:2) %*/
4173 }
4174 | '(' args ',' args_forward rparen
4175 {
4176 if (!check_forwarding_args(p)) {
4177 $$ = 0;
4178 }
4179 else {
4180 $$ = new_args_forward_call(p, $2, &@4, &@$);
4181 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4182 }
4183 }
4184 | '(' args_forward rparen
4185 {
4186 if (!check_forwarding_args(p)) {
4187 $$ = 0;
4188 }
4189 else {
4190 $$ = new_args_forward_call(p, 0, &@2, &@$);
4191 /*% ripper: arg_paren!($:2) %*/
4192 }
4193 }
4194 ;
4195
4196opt_paren_args : none
4197 | paren_args
4198 {
4199 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4200 }
4201 ;
4202
4203opt_call_args : none
4204 | call_args
4205 | args ','
4206 | args ',' assocs ','
4207 {
4208 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4209 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4210 }
4211 | assocs ','
4212 {
4213 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4214 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4215 }
4216 ;
4217
4218call_args : value_expr(command)
4219 {
4220 $$ = NEW_LIST($1, &@$);
4221 /*% ripper: args_add!(args_new!, $:1) %*/
4222 }
4223 | def_endless_method(endless_command)
4224 {
4225 $$ = NEW_LIST($1, &@$);
4226 /*% ripper: args_add!(args_new!, $:1) %*/
4227 }
4228 | args opt_block_arg
4229 {
4230 $$ = arg_blk_pass($1, $2);
4231 /*% ripper: args_add_block!($:1, $:2) %*/
4232 }
4233 | assocs opt_block_arg
4234 {
4235 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4236 $$ = arg_blk_pass($$, $2);
4237 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4238 }
4239 | args ',' assocs opt_block_arg
4240 {
4241 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4242 $$ = arg_blk_pass($$, $4);
4243 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4244 }
4245 | block_arg
4246 /*% ripper: args_add_block!(args_new!, $:1) %*/
4247 ;
4248
4249command_args : {
4250 /* If call_args starts with a open paren '(' or '[',
4251 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4252 * but the push must be done after CMDARG_PUSH(1).
4253 * So this code makes them consistent by first cancelling
4254 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4255 * and finally redoing CMDARG_PUSH(0).
4256 */
4257 int lookahead = 0;
4258 switch (yychar) {
4259 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4260 lookahead = 1;
4261 }
4262 if (lookahead) CMDARG_POP();
4263 CMDARG_PUSH(1);
4264 if (lookahead) CMDARG_PUSH(0);
4265 }
4266 call_args
4267 {
4268 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4269 * but the push must be done after CMDARG_POP() in the parser.
4270 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4271 * CMDARG_POP() to pop 1 pushed by command_args,
4272 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4273 */
4274 int lookahead = 0;
4275 switch (yychar) {
4276 case tLBRACE_ARG:
4277 lookahead = 1;
4278 }
4279 if (lookahead) CMDARG_POP();
4280 CMDARG_POP();
4281 if (lookahead) CMDARG_PUSH(0);
4282 $$ = $2;
4283 /*% ripper: $:2 %*/
4284 }
4285 ;
4286
4287block_arg : tAMPER arg_value
4288 {
4289 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4290 /*% ripper: $:2 %*/
4291 }
4292 | tAMPER
4293 {
4294 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4295 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4296 /*% ripper: Qnil %*/
4297 }
4298 ;
4299
4300opt_block_arg : ',' block_arg
4301 {
4302 $$ = $2;
4303 /*% ripper: $:2 %*/
4304 }
4305 | none
4306 {
4307 $$ = 0;
4308 /*% ripper: Qfalse %*/
4309 }
4310 ;
4311
4312/* value */
4313args : arg_value
4314 {
4315 $$ = NEW_LIST($arg_value, &@$);
4316 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4317 }
4318 | arg_splat
4319 {
4320 $$ = $arg_splat;
4321 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4322 }
4323 | args[non_last_args] ',' arg_value
4324 {
4325 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4326 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4327 }
4328 | args[non_last_args] ',' arg_splat
4329 {
4330 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4331 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4332 }
4333 ;
4334
4335/* value */
4336arg_splat : tSTAR arg_value
4337 {
4338 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4339 /*% ripper: $:arg_value %*/
4340 }
4341 | tSTAR /* none */
4342 {
4343 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4344 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4345 /*% ripper: Qnil %*/
4346 }
4347 ;
4348
4349/* value */
4350mrhs_arg : mrhs
4351 | arg_value
4352 ;
4353
4354/* value */
4355mrhs : args ',' arg_value
4356 {
4357 $$ = last_arg_append(p, $args, $arg_value, &@$);
4358 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4359 }
4360 | args ',' tSTAR arg_value
4361 {
4362 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4363 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4364 }
4365 | tSTAR arg_value
4366 {
4367 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4368 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4369 }
4370 ;
4371
4372%rule %inline inline_primary
4373 : literal
4374 | strings
4375 | xstring
4376 | regexp
4377 | words
4378 | qwords
4379 | symbols
4380 | qsymbols
4381 ;
4382
4383primary : inline_primary
4384 | var_ref
4385 | backref
4386 | tFID[fid]
4387 {
4388 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4389 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
4390 }
4391 | k_begin[kw]
4392 {
4393 CMDARG_PUSH(0);
4394 }
4395 bodystmt[body]
4396 k_end[k_end]
4397 {
4398 CMDARG_POP();
4399 set_line_body($body, @kw.end_pos.lineno);
4400 $$ = NEW_BEGIN($body, &@$);
4401 nd_set_line($$, @kw.end_pos.lineno);
4402 /*% ripper: begin!($:body) %*/
4403 }
4404 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4405 {
4406 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4407 $$ = $body;
4408 /*% ripper: paren!($:body) %*/
4409 }
4410 | tLPAREN compstmt(stmts)[body] ')'
4411 {
4412 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4413 $$ = NEW_BLOCK($body, &@$);
4414 /*% ripper: paren!($:body) %*/
4415 }
4416 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4417 {
4418 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4419 /*% ripper: const_path_ref!($:recv, $:name) %*/
4420 }
4421 | tCOLON3[top] tCONSTANT[name]
4422 {
4423 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4424 /*% ripper: top_const_ref!($:name) %*/
4425 }
4426 | tLBRACK aref_args[args] ']'
4427 {
4428 $$ = make_list($args, &@$);
4429 /*% ripper: array!($:args) %*/
4430 }
4431 | tLBRACE assoc_list[list] '}'
4432 {
4433 $$ = new_hash(p, $list, &@$);
4434 RNODE_HASH($$)->nd_brace = TRUE;
4435 /*% ripper: hash!($:list) %*/
4436 }
4437 | k_return[kw]
4438 {
4439 $$ = NEW_RETURN(0, &@$, &@kw);
4440 /*% ripper: return0! %*/
4441 }
4442 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4443 {
4444 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4445 /*% ripper: yield!(paren!($:args)) %*/
4446 }
4447 | k_yield[kw] '('[lpar] rparen[rpar]
4448 {
4449 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4450 /*% ripper: yield!(paren!(args_new!)) %*/
4451 }
4452 | k_yield[kw]
4453 {
4454 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4455 /*% ripper: yield0! %*/
4456 }
4457 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
4458 {
4459 p->ctxt.in_defined = $ctxt.in_defined;
4460 $$ = new_defined(p, $arg, &@$, &@kw);
4461 p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon;
4462 /*% ripper: defined!($:arg) %*/
4463 }
4464 | keyword_not[kw] '(' expr[arg] rparen
4465 {
4466 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4467 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4468 }
4469 | keyword_not[kw] '('[lpar] rparen
4470 {
4471 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4472 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4473 }
4474 | fcall[call] brace_block[block]
4475 {
4476 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4477 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4478 }
4479 | method_call
4480 | method_call[call] brace_block[block]
4481 {
4482 block_dup_check(p, get_nd_args(p, $call), $block);
4483 $$ = method_add_block(p, $call, $block, &@$);
4484 /*% ripper: method_add_block!($:call, $:block) %*/
4485 }
4486 | lambda
4487 | k_if[kw] expr_value[cond] then[then]
4488 compstmt(stmts)[body]
4489 if_tail[tail]
4490 k_end[k_end]
4491 {
4492 if ($tail && nd_type_p($tail, NODE_IF))
4493 RNODE_IF($tail)->end_keyword_loc = @k_end;
4494
4495 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4496 fixpos($$, $cond);
4497 /*% ripper: if!($:cond, $:body, $:tail) %*/
4498 }
4499 | k_unless[kw] expr_value[cond] then[then]
4500 compstmt(stmts)[body]
4501 opt_else[tail]
4502 k_end[k_end]
4503 {
4504 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4505 fixpos($$, $cond);
4506 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4507 }
4508 | k_while[kw] expr_value_do[cond]
4509 compstmt(stmts)[body]
4510 k_end[k_end]
4511 {
4512 restore_block_exit(p, $kw);
4513 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4514 fixpos($$, $cond);
4515 /*% ripper: while!($:cond, $:body) %*/
4516 }
4517 | k_until[kw] expr_value_do[cond]
4518 compstmt(stmts)[body]
4519 k_end[k_end]
4520 {
4521 restore_block_exit(p, $kw);
4522 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4523 fixpos($$, $cond);
4524 /*% ripper: until!($:cond, $:body) %*/
4525 }
4526 | k_case[k_case] expr_value[expr] terms?
4527 {
4528 $$ = p->case_labels;
4529 p->case_labels = CHECK_LITERAL_WHEN;
4530 }[labels]<labels>
4531 case_body[body]
4532 k_end[k_end]
4533 {
4534 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4535 p->case_labels = $labels;
4536 $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end);
4537 fixpos($$, $expr);
4538 /*% ripper: case!($:expr, $:body) %*/
4539 }
4540 | k_case[k_case] terms?
4541 {
4542 $$ = p->case_labels;
4543 p->case_labels = 0;
4544 }[labels]<labels>
4545 case_body[body]
4546 k_end[k_end]
4547 {
4548 if (p->case_labels) st_free_table(p->case_labels);
4549 p->case_labels = $labels;
4550 $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end);
4551 /*% ripper: case!(Qnil, $:body) %*/
4552 }
4553 | k_case[k_case] expr_value[expr] terms?
4554 p_case_body[body]
4555 k_end[k_end]
4556 {
4557 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4558 /*% ripper: case!($:expr, $:body) %*/
4559 }
4560 | k_for[k_for] for_var[for_var] keyword_in[keyword_in]
4561 {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();}
4562 compstmt(stmts)[compstmt]
4563 k_end[k_end]
4564 {
4565 restore_block_exit(p, $k_for);
4566 /*
4567 * for a, b, c in e
4568 * #=>
4569 * e.each{|*x| a, b, c = x}
4570 *
4571 * for a in e
4572 * #=>
4573 * e.each{|x| a, = x}
4574 */
4575 ID id = internal_id(p);
4576 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4577 rb_node_args_t *args;
4578 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4579 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4580 tbl->ids[0] = id; /* internal id */
4581
4582 switch (nd_type($for_var)) {
4583 case NODE_LASGN:
4584 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4585 set_nd_value(p, $for_var, internal_var);
4586 id = 0;
4587 m->nd_plen = 1;
4588 m->nd_next = $for_var;
4589 break;
4590 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4591 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4592 break;
4593 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4594 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);
4595 }
4596 /* {|*internal_id| <m> = internal_id; ... } */
4597 args = new_args(p, m, 0, id, 0, new_empty_args_tail(p, &@for_var), &@for_var);
4598 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4599 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4600 $$ = NEW_FOR($expr_value, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4601 RNODE_SCOPE(scope)->nd_parent = $$;
4602 fixpos($$, $for_var);
4603 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4604 }
4605 | k_class cpath superclass
4606 {
4607 begin_definition("class", &@k_class, &@cpath);
4608 }
4609 bodystmt
4610 k_end
4611 {
4612 YYLTYPE inheritance_operator_loc = NULL_LOC;
4613 if ($superclass) {
4614 inheritance_operator_loc = @superclass;
4615 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4616 }
4617 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4618 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4619 set_line_body($bodystmt, @superclass.end_pos.lineno);
4620 nd_set_line($$, @superclass.end_pos.lineno);
4621 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4622 local_pop(p);
4623 p->ctxt.in_class = $k_class.in_class;
4624 p->ctxt.cant_return = $k_class.cant_return;
4625 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4626 }
4627 | k_class tLSHFT expr_value
4628 {
4629 begin_definition("", &@k_class, &@tLSHFT);
4630 }
4631 term
4632 bodystmt
4633 k_end
4634 {
4635 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4636 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4637 set_line_body($bodystmt, nd_line($expr_value));
4638 fixpos($$, $expr_value);
4639 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4640 local_pop(p);
4641 p->ctxt.in_def = $k_class.in_def;
4642 p->ctxt.in_class = $k_class.in_class;
4643 p->ctxt.cant_return = $k_class.cant_return;
4644 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4645 }
4646 | k_module cpath
4647 {
4648 begin_definition("module", &@k_module, &@cpath);
4649 }
4650 bodystmt
4651 k_end
4652 {
4653 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4654 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4655 set_line_body($bodystmt, @cpath.end_pos.lineno);
4656 nd_set_line($$, @cpath.end_pos.lineno);
4657 /*% ripper: module!($:cpath, $:bodystmt) %*/
4658 local_pop(p);
4659 p->ctxt.in_class = $k_module.in_class;
4660 p->ctxt.cant_return = $k_module.cant_return;
4661 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4662 }
4663 | defn_head[head]
4664 f_arglist[args]
4665 {
4666 push_end_expect_token_locations(p, &@head.beg_pos);
4667 }
4668 bodystmt
4669 k_end
4670 {
4671 restore_defun(p, $head);
4672 ($$ = $head->nd_def)->nd_loc = @$;
4673 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4674 RNODE_DEFN($$)->nd_defn = $bodystmt;
4675 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4676 local_pop(p);
4677 }
4678 | defs_head[head]
4679 f_arglist[args]
4680 {
4681 push_end_expect_token_locations(p, &@head.beg_pos);
4682 }
4683 bodystmt
4684 k_end
4685 {
4686 restore_defun(p, $head);
4687 ($$ = $head->nd_def)->nd_loc = @$;
4688 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4689 RNODE_DEFS($$)->nd_defn = $bodystmt;
4690 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4691 local_pop(p);
4692 }
4693 | keyword_break[kw]
4694 {
4695 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4696 /*% ripper: break!(args_new!) %*/
4697 }
4698 | keyword_next[kw]
4699 {
4700 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4701 /*% ripper: next!(args_new!) %*/
4702 }
4703 | keyword_redo[kw]
4704 {
4705 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4706 /*% ripper: redo! %*/
4707 }
4708 | keyword_retry[kw]
4709 {
4710 if (!p->ctxt.in_defined) {
4711 switch (p->ctxt.in_rescue) {
4712 case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break;
4713 case after_rescue: /* ok */ break;
4714 case after_else: yyerror1(&@kw, "Invalid retry after else"); break;
4715 case after_ensure: yyerror1(&@kw, "Invalid retry after ensure"); break;
4716 }
4717 }
4718 $$ = NEW_RETRY(&@$);
4719 /*% ripper: retry! %*/
4720 }
4721 ;
4722
4723primary_value : value_expr(primary)
4724 ;
4725
4726k_begin : keyword_begin
4727 {
4728 token_info_push(p, "begin", &@$);
4729 push_end_expect_token_locations(p, &@1.beg_pos);
4730 }
4731 ;
4732
4733k_if : keyword_if
4734 {
4735 WARN_EOL("if");
4736 token_info_push(p, "if", &@$);
4737 if (p->token_info && p->token_info->nonspc &&
4738 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4739 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4740 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4741 beg += rb_strlen_lit("else");
4742 while (beg < tok && ISSPACE(*beg)) beg++;
4743 if (beg == tok) {
4744 p->token_info->nonspc = 0;
4745 }
4746 }
4747 push_end_expect_token_locations(p, &@1.beg_pos);
4748 }
4749 ;
4750
4751k_unless : keyword_unless
4752 {
4753 token_info_push(p, "unless", &@$);
4754 push_end_expect_token_locations(p, &@1.beg_pos);
4755 }
4756 ;
4757
4758k_while : keyword_while[kw] allow_exits
4759 {
4760 $$ = $allow_exits;
4761 token_info_push(p, "while", &@$);
4762 push_end_expect_token_locations(p, &@kw.beg_pos);
4763 }
4764 ;
4765
4766k_until : keyword_until[kw] allow_exits
4767 {
4768 $$ = $allow_exits;
4769 token_info_push(p, "until", &@$);
4770 push_end_expect_token_locations(p, &@kw.beg_pos);
4771 }
4772 ;
4773
4774k_case : keyword_case
4775 {
4776 token_info_push(p, "case", &@$);
4777 push_end_expect_token_locations(p, &@1.beg_pos);
4778 }
4779 ;
4780
4781k_for : keyword_for[kw] allow_exits
4782 {
4783 $$ = $allow_exits;
4784 token_info_push(p, "for", &@$);
4785 push_end_expect_token_locations(p, &@kw.beg_pos);
4786 }
4787 ;
4788
4789k_class : keyword_class
4790 {
4791 token_info_push(p, "class", &@$);
4792 $$ = p->ctxt;
4793 p->ctxt.in_rescue = before_rescue;
4794 push_end_expect_token_locations(p, &@1.beg_pos);
4795 }
4796 ;
4797
4798k_module : keyword_module
4799 {
4800 token_info_push(p, "module", &@$);
4801 $$ = p->ctxt;
4802 p->ctxt.in_rescue = before_rescue;
4803 push_end_expect_token_locations(p, &@1.beg_pos);
4804 }
4805 ;
4806
4807k_def : keyword_def
4808 {
4809 token_info_push(p, "def", &@$);
4810 $$ = NEW_DEF_TEMP(&@$);
4811 p->ctxt.in_argdef = 1;
4812 }
4813 ;
4814
4815k_do : keyword_do
4816 {
4817 token_info_push(p, "do", &@$);
4818 push_end_expect_token_locations(p, &@1.beg_pos);
4819 }
4820 ;
4821
4822k_do_block : keyword_do_block
4823 {
4824 token_info_push(p, "do", &@$);
4825 push_end_expect_token_locations(p, &@1.beg_pos);
4826 }
4827 ;
4828
4829k_rescue : keyword_rescue
4830 {
4831 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4832 $$ = p->ctxt;
4833 p->ctxt.in_rescue = after_rescue;
4834 }
4835 ;
4836
4837k_ensure : keyword_ensure
4838 {
4839 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4840 $$ = p->ctxt;
4841 }
4842 ;
4843
4844k_when : keyword_when
4845 {
4846 token_info_warn(p, "when", p->token_info, 0, &@$);
4847 }
4848 ;
4849
4850k_else : keyword_else
4851 {
4852 token_info *ptinfo_beg = p->token_info;
4853 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4854 token_info_warn(p, "else", p->token_info, same, &@$);
4855 if (same) {
4856 token_info e;
4857 e.next = ptinfo_beg->next;
4858 e.token = "else";
4859 token_info_setup(&e, p->lex.pbeg, &@$);
4860 if (!e.nonspc) *ptinfo_beg = e;
4861 }
4862 }
4863 ;
4864
4865k_elsif : keyword_elsif
4866 {
4867 WARN_EOL("elsif");
4868 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4869 }
4870 ;
4871
4872k_end : keyword_end
4873 {
4874 token_info_pop(p, "end", &@$);
4875 pop_end_expect_token_locations(p);
4876 }
4877 | tDUMNY_END
4878 {
4879 compile_error(p, "syntax error, unexpected end-of-input");
4880 }
4881 ;
4882
4883k_return : keyword_return
4884 {
4885 if (p->ctxt.cant_return && !dyna_in_block(p))
4886 yyerror1(&@1, "Invalid return in class/module body");
4887 }
4888 ;
4889
4890k_yield : keyword_yield
4891 {
4892 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4893 yyerror1(&@1, "Invalid yield");
4894 }
4895 ;
4896
4897then : term
4898 | keyword_then
4899 | term keyword_then
4900 ;
4901
4902do : term
4903 | keyword_do_cond { $$ = keyword_do_cond; }
4904 ;
4905
4906if_tail : opt_else
4907 | k_elsif expr_value then
4908 compstmt(stmts)
4909 if_tail
4910 {
4911 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4912 fixpos($$, $2);
4913 /*% ripper: elsif!($:2, $:4, $:5) %*/
4914 }
4915 ;
4916
4917opt_else : none
4918 | k_else compstmt(stmts)
4919 {
4920 $$ = $2;
4921 /*% ripper: else!($:2) %*/
4922 }
4923 ;
4924
4925for_var : lhs
4926 | mlhs
4927 ;
4928
4929f_marg : f_norm_arg
4930 {
4931 $$ = assignable(p, $1, 0, &@$);
4932 mark_lvar_used(p, $$);
4933 }
4934 | tLPAREN f_margs rparen
4935 {
4936 $$ = (NODE *)$2;
4937 /*% ripper: mlhs_paren!($:2) %*/
4938 }
4939 ;
4940
4941
4942f_margs : mlhs_items(f_marg)
4943 {
4944 $$ = NEW_MASGN($1, 0, &@$);
4945 /*% ripper: $:1 %*/
4946 }
4947 | mlhs_items(f_marg) ',' f_rest_marg
4948 {
4949 $$ = NEW_MASGN($1, $3, &@$);
4950 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4951 }
4952 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4953 {
4954 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4955 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4956 }
4957 | f_rest_marg
4958 {
4959 $$ = NEW_MASGN(0, $1, &@$);
4960 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4961 }
4962 | f_rest_marg ',' mlhs_items(f_marg)
4963 {
4964 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4965 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4966 }
4967 ;
4968
4969f_rest_marg : tSTAR f_norm_arg
4970 {
4971 /*% ripper: $:2 %*/
4972 $$ = assignable(p, $2, 0, &@$);
4973 mark_lvar_used(p, $$);
4974 }
4975 | tSTAR
4976 {
4977 $$ = NODE_SPECIAL_NO_NAME_REST;
4978 /*% ripper: Qnil %*/
4979 }
4980 ;
4981
4982f_any_kwrest : f_kwrest
4983 | f_no_kwarg
4984 {
4985 $$ = idNil;
4986 /*% ripper: ID2VAL(idNil) %*/
4987 }
4988 ;
4989
4990f_eq : {p->ctxt.in_argdef = 0;} '=';
4991
4992block_args_tail : args_tail_basic(primary_value, none)
4993 ;
4994
4995excessed_comma : ','
4996 {
4997 /* magic number for rest_id in iseq_set_arguments() */
4998 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4999 /*% ripper: excessed_comma! %*/
5000 }
5001 ;
5002
5003block_param : args-list(primary_value, opt_args_tail(block_args_tail, none))
5004 | f_arg[pre] excessed_comma
5005 {
5006 $$ = new_empty_args_tail(p, &@excessed_comma);
5007 $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$);
5008 /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/
5009 }
5010 | f_arg[pre] opt_args_tail(block_args_tail, none)[tail]
5011 {
5012 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5013 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5014 }
5015 | tail-only-args(block_args_tail)
5016 ;
5017
5018opt_block_param_def : none
5019 | block_param_def
5020 {
5021 p->command_start = TRUE;
5022 }
5023 ;
5024
5025block_param_def : '|' opt_block_param opt_bv_decl '|'
5026 {
5027 p->max_numparam = ORDINAL_PARAM;
5028 p->ctxt.in_argdef = 0;
5029 $$ = $2;
5030 /*% ripper: block_var!($:2, $:3) %*/
5031 }
5032 ;
5033
5034opt_block_param : /* none */
5035 {
5036 $$ = 0;
5037 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5038 }
5039 | block_param
5040 ;
5041
5042opt_bv_decl : '\n'?
5043 {
5044 $$ = 0;
5045 /*% ripper: Qfalse %*/
5046 }
5047 | '\n'? ';' bv_decls '\n'?
5048 {
5049 $$ = 0;
5050 /*% ripper: $:3 %*/
5051 }
5052 ;
5053
5054bv_decls : bvar
5055 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5056 | bv_decls ',' bvar
5057 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5058 ;
5059
5060bvar : tIDENTIFIER
5061 {
5062 new_bv(p, $1);
5063 /*% ripper: $:1 %*/
5064 }
5065 | f_bad_arg
5066 ;
5067
5068max_numparam : {
5069 $$ = p->max_numparam;
5070 p->max_numparam = 0;
5071 }
5072 ;
5073
5074numparam : {
5075 $$ = numparam_push(p);
5076 }
5077 ;
5078
5079it_id : {
5080 $$ = p->it_id;
5081 p->it_id = 0;
5082 }
5083 ;
5084
5085lambda : tLAMBDA[lpar]
5086 {
5087 token_info_push(p, "->", &@lpar);
5088 $$ = dyna_push(p);
5089 }[dyna]<vars>
5090 max_numparam numparam it_id allow_exits
5091 f_larglist[args]
5092 {
5093 CMDARG_PUSH(0);
5094 }
5095 lambda_body[body]
5096 {
5097 int max_numparam = p->max_numparam;
5098 ID it_id = p->it_id;
5099 p->lex.lpar_beg = $lpar;
5100 p->max_numparam = $max_numparam;
5101 p->it_id = $it_id;
5102 restore_block_exit(p, $allow_exits);
5103 CMDARG_POP();
5104 $args = args_with_numbered(p, $args, max_numparam, it_id);
5105 {
5106 YYLTYPE loc = code_loc_gen(&@lpar, &@body);
5107 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5108 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5109 nd_set_line($$, @args.end_pos.lineno);
5110 nd_set_first_loc($$, @lpar.beg_pos);
5111 xfree($body);
5112 }
5113 /*% ripper: lambda!($:args, $:body) %*/
5114 numparam_pop(p, $numparam);
5115 dyna_pop(p, $dyna);
5116 }
5117 ;
5118
5119f_larglist : '(' f_largs[args] opt_bv_decl ')'
5120 {
5121 p->ctxt.in_argdef = 0;
5122 $$ = $args;
5123 p->max_numparam = ORDINAL_PARAM;
5124 /*% ripper: paren!($:args) %*/
5125 }
5126 | f_largs[args]
5127 {
5128 p->ctxt.in_argdef = 0;
5129 if (!args_info_empty_p(&$args->nd_ainfo))
5130 p->max_numparam = ORDINAL_PARAM;
5131 $$ = $args;
5132 }
5133 ;
5134
5135lambda_body : tLAMBEG compstmt(stmts) '}'
5136 {
5137 token_info_pop(p, "}", &@3);
5138 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5139 /*% ripper: $:2 %*/
5140 }
5141 | keyword_do_LAMBDA
5142 {
5143 push_end_expect_token_locations(p, &@1.beg_pos);
5144 }
5145 bodystmt k_end
5146 {
5147 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5148 /*% ripper: $:3 %*/
5149 }
5150 ;
5151
5152do_block : k_do_block do_body k_end
5153 {
5154 $$ = $2;
5155 set_embraced_location($$, &@1, &@3);
5156 /*% ripper: $:2 %*/
5157 }
5158 ;
5159
5160block_call : command do_block
5161 {
5162 $$ = command_add_block(p, $1, $2, &@$);
5163 fixpos($$, $1);
5164 /*% ripper: method_add_block!($:1, $:2) %*/
5165 }
5166 | block_call call_op2 operation2 opt_paren_args
5167 {
5168 bool has_args = $4 != 0;
5169 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5170 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5171 /*% ripper: call!($:1, $:2, $:3) %*/
5172 if (has_args) {
5173 /*% ripper: method_add_arg!($:$, $:4) %*/
5174 }
5175 }
5176 | block_call call_op2 operation2 opt_paren_args brace_block
5177 {
5178 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5179 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5180 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5181 }
5182 | block_call call_op2 operation2 command_args do_block
5183 {
5184 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5185 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5186 }
5187 | block_call call_op2 paren_args
5188 {
5189 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5190 nd_set_line($$, @2.end_pos.lineno);
5191 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5192 }
5193 ;
5194
5195method_call : fcall paren_args
5196 {
5197 $1->nd_args = $2;
5198 $$ = (NODE *)$1;
5199 nd_set_last_loc($1, @2.end_pos);
5200 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5201 }
5202 | primary_value call_op operation2 opt_paren_args
5203 {
5204 bool has_args = $4 != 0;
5205 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5206 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5207 nd_set_line($$, @3.end_pos.lineno);
5208 /*% ripper: call!($:1, $:2, $:3) %*/
5209 if (has_args) {
5210 /*% ripper: method_add_arg!($:$, $:4) %*/
5211 }
5212 }
5213 | primary_value tCOLON2 operation2 paren_args
5214 {
5215 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5216 nd_set_line($$, @3.end_pos.lineno);
5217 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5218 }
5219 | primary_value tCOLON2 operation3
5220 {
5221 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5222 /*% ripper: call!($:1, $:2, $:3) %*/
5223 }
5224 | primary_value call_op2 paren_args
5225 {
5226 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5227 nd_set_line($$, @2.end_pos.lineno);
5228 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5229 }
5230 | keyword_super paren_args
5231 {
5232 rb_code_location_t lparen_loc = @2;
5233 rb_code_location_t rparen_loc = @2;
5234 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5235 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5236
5237 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5238 /*% ripper: super!($:2) %*/
5239 }
5240 | keyword_super
5241 {
5242 $$ = NEW_ZSUPER(&@$);
5243 /*% ripper: zsuper! %*/
5244 }
5245 | primary_value '[' opt_call_args rbracket
5246 {
5247 $$ = NEW_CALL($1, tAREF, $3, &@$);
5248 fixpos($$, $1);
5249 /*% ripper: aref!($:1, $:3) %*/
5250 }
5251 ;
5252
5253brace_block : '{' brace_body '}'
5254 {
5255 $$ = $2;
5256 set_embraced_location($$, &@1, &@3);
5257 /*% ripper: $:2 %*/
5258 }
5259 | k_do do_body k_end
5260 {
5261 $$ = $2;
5262 set_embraced_location($$, &@1, &@3);
5263 /*% ripper: $:2 %*/
5264 }
5265 ;
5266
5267brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5268 max_numparam numparam it_id allow_exits
5269 opt_block_param_def[args] compstmt(stmts)
5270 {
5271 int max_numparam = p->max_numparam;
5272 ID it_id = p->it_id;
5273 p->max_numparam = $max_numparam;
5274 p->it_id = $it_id;
5275 $args = args_with_numbered(p, $args, max_numparam, it_id);
5276 $$ = NEW_ITER($args, $compstmt, &@$);
5277 /*% ripper: brace_block!($:args, $:compstmt) %*/
5278 restore_block_exit(p, $allow_exits);
5279 numparam_pop(p, $numparam);
5280 dyna_pop(p, $dyna);
5281 }
5282 ;
5283
5284do_body : {
5285 $$ = dyna_push(p);
5286 CMDARG_PUSH(0);
5287 }[dyna]<vars>
5288 max_numparam numparam it_id allow_exits
5289 opt_block_param_def[args] bodystmt
5290 {
5291 int max_numparam = p->max_numparam;
5292 ID it_id = p->it_id;
5293 p->max_numparam = $max_numparam;
5294 p->it_id = $it_id;
5295 $args = args_with_numbered(p, $args, max_numparam, it_id);
5296 $$ = NEW_ITER($args, $bodystmt, &@$);
5297 /*% ripper: do_block!($:args, $:bodystmt) %*/
5298 CMDARG_POP();
5299 restore_block_exit(p, $allow_exits);
5300 numparam_pop(p, $numparam);
5301 dyna_pop(p, $dyna);
5302 }
5303 ;
5304
5305case_args : arg_value
5306 {
5307 check_literal_when(p, $arg_value, &@arg_value);
5308 $$ = NEW_LIST($arg_value, &@$);
5309 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5310 }
5311 | tSTAR arg_value
5312 {
5313 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5314 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5315 }
5316 | case_args[non_last_args] ',' arg_value
5317 {
5318 check_literal_when(p, $arg_value, &@arg_value);
5319 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5320 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5321 }
5322 | case_args[non_last_args] ',' tSTAR arg_value
5323 {
5324 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5325 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5326 }
5327 ;
5328
5329case_body : k_when case_args then
5330 compstmt(stmts)
5331 cases
5332 {
5333 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5334 fixpos($$, $2);
5335 /*% ripper: when!($:2, $:4, $:5) %*/
5336 }
5337 ;
5338
5339cases : opt_else
5340 | case_body
5341 ;
5342
5343p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5344p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5345
5346p_in_kwarg : {
5347 $$ = p->ctxt;
5348 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5349 p->command_start = FALSE;
5350 p->ctxt.in_kwarg = 1;
5351 p->ctxt.in_alt_pattern = 0;
5352 p->ctxt.capture_in_pattern = 0;
5353 }
5354 ;
5355
5356p_case_body : keyword_in
5357 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5358 p_top_expr[expr] then
5359 {
5360 pop_pktbl(p, $p_pktbl);
5361 pop_pvtbl(p, $p_pvtbl);
5362 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5363 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5364 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5365 }
5366 compstmt(stmts)
5367 p_cases[cases]
5368 {
5369 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5370 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5371 }
5372 ;
5373
5374p_cases : opt_else
5375 | p_case_body
5376 ;
5377
5378p_top_expr : p_top_expr_body
5379 | p_top_expr_body modifier_if expr_value
5380 {
5381 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5382 fixpos($$, $3);
5383 /*% ripper: if_mod!($:3, $:1) %*/
5384 }
5385 | p_top_expr_body modifier_unless expr_value
5386 {
5387 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5388 fixpos($$, $3);
5389 /*% ripper: unless_mod!($:3, $:1) %*/
5390 }
5391 ;
5392
5393p_top_expr_body : p_expr
5394 | p_expr ','
5395 {
5396 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5397 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5398 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5399 }
5400 | p_expr ',' p_args
5401 {
5402 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5403 nd_set_first_loc($$, @1.beg_pos);
5404 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5405 }
5406 | p_find
5407 {
5408 $$ = new_find_pattern(p, 0, $1, &@$);
5409 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5410 }
5411 | p_args_tail
5412 {
5413 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5414 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5415 }
5416 | p_kwargs
5417 {
5418 $$ = new_hash_pattern(p, 0, $1, &@$);
5419 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5420 }
5421 ;
5422
5423p_expr : p_as
5424 ;
5425
5426p_as : p_expr tASSOC p_variable
5427 {
5428 NODE *n = NEW_LIST($1, &@$);
5429 n = list_append(p, n, $3);
5430 $$ = new_hash(p, n, &@$);
5431 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5432 }
5433 | p_alt
5434 ;
5435
5436p_alt : p_alt[left] '|'[alt]
5437 {
5438 p->ctxt.in_alt_pattern = 1;
5439 }
5440 p_expr_basic[right]
5441 {
5442 if (p->ctxt.capture_in_pattern) {
5443 yyerror1(&@alt, "alternative pattern after variable capture");
5444 }
5445 p->ctxt.in_alt_pattern = 0;
5446 $$ = NEW_OR($left, $right, &@$, &@alt);
5447 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5448 }
5449 | p_expr_basic
5450 ;
5451
5452p_lparen : '(' p_pktbl
5453 {
5454 $$ = $2;
5455 /*% ripper: $:2 %*/
5456 }
5457 ;
5458
5459p_lbracket : '[' p_pktbl
5460 {
5461 $$ = $2;
5462 /*% ripper: $:2 %*/
5463 }
5464 ;
5465
5466p_expr_basic : p_value
5467 | p_variable
5468 | p_const p_lparen[p_pktbl] p_args rparen
5469 {
5470 pop_pktbl(p, $p_pktbl);
5471 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5472 nd_set_first_loc($$, @p_const.beg_pos);
5473 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5474 }
5475 | p_const p_lparen[p_pktbl] p_find rparen
5476 {
5477 pop_pktbl(p, $p_pktbl);
5478 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5479 nd_set_first_loc($$, @p_const.beg_pos);
5480 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5481 }
5482 | p_const p_lparen[p_pktbl] p_kwargs rparen
5483 {
5484 pop_pktbl(p, $p_pktbl);
5485 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5486 nd_set_first_loc($$, @p_const.beg_pos);
5487 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5488 }
5489 | p_const '(' rparen
5490 {
5491 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5492 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5493 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5494 }
5495 | p_const p_lbracket[p_pktbl] p_args rbracket
5496 {
5497 pop_pktbl(p, $p_pktbl);
5498 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5499 nd_set_first_loc($$, @p_const.beg_pos);
5500 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5501 }
5502 | p_const p_lbracket[p_pktbl] p_find rbracket
5503 {
5504 pop_pktbl(p, $p_pktbl);
5505 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5506 nd_set_first_loc($$, @p_const.beg_pos);
5507 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5508 }
5509 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5510 {
5511 pop_pktbl(p, $p_pktbl);
5512 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5513 nd_set_first_loc($$, @p_const.beg_pos);
5514 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5515 }
5516 | p_const '[' rbracket
5517 {
5518 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5519 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5520 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5521 }
5522 | tLBRACK p_args rbracket
5523 {
5524 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5525 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5526 }
5527 | tLBRACK p_find rbracket
5528 {
5529 $$ = new_find_pattern(p, 0, $p_find, &@$);
5530 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5531 }
5532 | tLBRACK rbracket
5533 {
5534 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5535 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5536 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5537 }
5538 | tLBRACE p_pktbl lex_ctxt[ctxt]
5539 {
5540 p->ctxt.in_kwarg = 0;
5541 }
5542 p_kwargs rbrace
5543 {
5544 pop_pktbl(p, $p_pktbl);
5545 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5546 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5547 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5548 }
5549 | tLBRACE rbrace
5550 {
5551 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5552 $$ = new_hash_pattern(p, 0, $$, &@$);
5553 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5554 }
5555 | tLPAREN p_pktbl p_expr rparen
5556 {
5557 pop_pktbl(p, $p_pktbl);
5558 $$ = $p_expr;
5559 /*% ripper: $:p_expr %*/
5560 }
5561 ;
5562
5563p_args : p_expr
5564 {
5565 NODE *pre_args = NEW_LIST($1, &@$);
5566 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5567 /*% ripper: [[$:1], Qnil, Qnil] %*/
5568 }
5569 | p_args_head
5570 {
5571 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5572 /*% ripper: [$:1, Qnil, Qnil] %*/
5573 }
5574 | p_args_head p_arg
5575 {
5576 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5577 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5578 }
5579 | p_args_head p_rest
5580 {
5581 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5582 /*% ripper: [$:1, $:2, Qnil] %*/
5583 }
5584 | p_args_head p_rest ',' p_args_post
5585 {
5586 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5587 /*% ripper: [$:1, $:2, $:4] %*/
5588 }
5589 | p_args_tail
5590 ;
5591
5592p_args_head : p_arg ','
5593 | p_args_head p_arg ','
5594 {
5595 $$ = list_concat($1, $2);
5596 /*% ripper: rb_ary_concat($:1, $:2) %*/
5597 }
5598 ;
5599
5600p_args_tail : p_rest
5601 {
5602 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5603 /*% ripper: [Qnil, $:1, Qnil] %*/
5604 }
5605 | p_rest ',' p_args_post
5606 {
5607 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5608 /*% ripper: [Qnil, $:1, $:3] %*/
5609 }
5610 ;
5611
5612p_find : p_rest ',' p_args_post ',' p_rest
5613 {
5614 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5615 /*% ripper: [$:1, $:3, $:5] %*/
5616 }
5617 ;
5618
5619
5620p_rest : tSTAR tIDENTIFIER
5621 {
5622 error_duplicate_pattern_variable(p, $2, &@2);
5623 /*% ripper: var_field!($:2) %*/
5624 $$ = assignable(p, $2, 0, &@$);
5625 }
5626 | tSTAR
5627 {
5628 $$ = 0;
5629 /*% ripper: var_field!(Qnil) %*/
5630 }
5631 ;
5632
5633p_args_post : p_arg
5634 | p_args_post ',' p_arg
5635 {
5636 $$ = list_concat($1, $3);
5637 /*% ripper: rb_ary_concat($:1, $:3) %*/
5638 }
5639 ;
5640
5641p_arg : p_expr
5642 {
5643 $$ = NEW_LIST($1, &@$);
5644 /*% ripper: [$:1] %*/
5645 }
5646 ;
5647
5648p_kwargs : p_kwarg ',' p_any_kwrest
5649 {
5650 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5651 /*% ripper: [$:1, $:3] %*/
5652 }
5653 | p_kwarg
5654 {
5655 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5656 /*% ripper: [$:1, Qnil] %*/
5657 }
5658 | p_kwarg ','
5659 {
5660 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5661 /*% ripper: [$:1, Qnil] %*/
5662 }
5663 | p_any_kwrest
5664 {
5665 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5666 /*% ripper: [[], $:1] %*/
5667 }
5668 ;
5669
5670p_kwarg : p_kw
5671 /*% ripper[brace]: [$:1] %*/
5672 | p_kwarg ',' p_kw
5673 {
5674 $$ = list_concat($1, $3);
5675 /*% ripper: rb_ary_push($:1, $:3) %*/
5676 }
5677 ;
5678
5679p_kw : p_kw_label p_expr
5680 {
5681 error_duplicate_pattern_key(p, $1, &@1);
5682 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5683 /*% ripper: [$:1, $:2] %*/
5684 }
5685 | p_kw_label
5686 {
5687 error_duplicate_pattern_key(p, $1, &@1);
5688 if ($1 && !is_local_id($1)) {
5689 yyerror1(&@1, "key must be valid as local variables");
5690 }
5691 error_duplicate_pattern_variable(p, $1, &@1);
5692 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5693 /*% ripper: [$:1, Qnil] %*/
5694 }
5695 ;
5696
5697p_kw_label : tLABEL
5698 | tSTRING_BEG string_contents tLABEL_END
5699 {
5700 YYLTYPE loc = code_loc_gen(&@1, &@3);
5701 if (!$2 || nd_type_p($2, NODE_STR)) {
5702 NODE *node = dsym_node(p, $2, &loc);
5703 $$ = rb_sym2id(rb_node_sym_string_val(node));
5704 }
5705 else {
5706 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5707 $$ = rb_intern_str(STR_NEW0());
5708 }
5709 /*% ripper: $:2 %*/
5710 }
5711 ;
5712
5713p_kwrest : kwrest_mark tIDENTIFIER
5714 {
5715 $$ = $2;
5716 /*% ripper: var_field!($:2) %*/
5717 }
5718 | kwrest_mark
5719 {
5720 $$ = 0;
5721 /*% ripper: Qnil %*/
5722 }
5723 ;
5724
5725p_kwnorest : kwrest_mark keyword_nil
5726 {
5727 $$ = 0;
5728 }
5729 ;
5730
5731p_any_kwrest : p_kwrest
5732 | p_kwnorest
5733 {
5734 $$ = idNil;
5735 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5736 }
5737 ;
5738
5739p_value : p_primitive
5740 | range_expr(p_primitive)
5741 | p_var_ref
5742 | p_expr_ref
5743 | p_const
5744 ;
5745
5746p_primitive : inline_primary
5747 | keyword_variable
5748 {
5749 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5750 /*% ripper: var_ref!($:1) %*/
5751 }
5752 | lambda
5753 ;
5754
5755p_variable : tIDENTIFIER
5756 {
5757 error_duplicate_pattern_variable(p, $1, &@1);
5758 /*% ripper: var_field!($:1) %*/
5759 $$ = assignable(p, $1, 0, &@$);
5760 }
5761 ;
5762
5763p_var_ref : '^' tIDENTIFIER
5764 {
5765 NODE *n = gettable(p, $2, &@$);
5766 if (!n) {
5767 n = NEW_ERROR(&@$);
5768 }
5769 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5770 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5771 }
5772 $$ = n;
5773 /*% ripper: var_ref!($:2) %*/
5774 }
5775 | '^' nonlocal_var
5776 {
5777 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5778 /*% ripper: var_ref!($:2) %*/
5779 }
5780 ;
5781
5782p_expr_ref : '^' tLPAREN expr_value rparen
5783 {
5784 $$ = NEW_BLOCK($3, &@$);
5785 /*% ripper: begin!($:3) %*/
5786 }
5787 ;
5788
5789p_const : tCOLON3 cname
5790 {
5791 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5792 /*% ripper: top_const_ref!($:2) %*/
5793 }
5794 | p_const tCOLON2 cname
5795 {
5796 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5797 /*% ripper: const_path_ref!($:1, $:3) %*/
5798 }
5799 | tCONSTANT
5800 {
5801 $$ = gettable(p, $1, &@$);
5802 /*% ripper: var_ref!($:1) %*/
5803 }
5804 ;
5805
5806opt_rescue : k_rescue exc_list exc_var then
5807 compstmt(stmts)
5808 opt_rescue
5809 {
5810 NODE *err = $3;
5811 if ($3) {
5812 err = NEW_ERRINFO(&@3);
5813 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5814 }
5815 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5816 if ($2) {
5817 fixpos($$, $2);
5818 }
5819 else if ($3) {
5820 fixpos($$, $3);
5821 }
5822 else {
5823 fixpos($$, $5);
5824 }
5825 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5826 }
5827 | none
5828 ;
5829
5830exc_list : arg_value
5831 {
5832 $$ = NEW_LIST($1, &@$);
5833 /*% ripper: rb_ary_new3(1, $:1) %*/
5834 }
5835 | mrhs
5836 {
5837 if (!($$ = splat_array($1))) $$ = $1;
5838 }
5839 | none
5840 ;
5841
5842exc_var : tASSOC lhs
5843 {
5844 $$ = $2;
5845 /*% ripper: $:2 %*/
5846 }
5847 | none
5848 ;
5849
5850opt_ensure : k_ensure stmts terms?
5851 {
5852 p->ctxt.in_rescue = $1.in_rescue;
5853 $$ = $2;
5854 void_expr(p, void_stmts(p, $$));
5855 /*% ripper: ensure!($:2) %*/
5856 }
5857 | none
5858 ;
5859
5860literal : numeric
5861 | symbol
5862 ;
5863
5864strings : string
5865 {
5866 if (!$1) {
5867 $$ = NEW_STR(STRING_NEW0(), &@$);
5868 }
5869 else {
5870 $$ = evstr2dstr(p, $1);
5871 }
5872 /*% ripper: $:1 %*/
5873 }
5874 ;
5875
5876string : tCHAR
5877 | string1
5878 | string string1
5879 {
5880 $$ = literal_concat(p, $1, $2, &@$);
5881 /*% ripper: string_concat!($:1, $:2) %*/
5882 }
5883 ;
5884
5885string1 : tSTRING_BEG string_contents tSTRING_END
5886 {
5887 $$ = heredoc_dedent(p, $2);
5888 if ($$) nd_set_loc($$, &@$);
5889 /*% ripper: $:2 %*/
5890 if (p->heredoc_indent > 0) {
5891 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5892 p->heredoc_indent = 0;
5893 }
5894 /*% ripper: string_literal!($:$) %*/
5895 }
5896 ;
5897
5898xstring : tXSTRING_BEG xstring_contents tSTRING_END
5899 {
5900 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5901 /*% ripper: $:2 %*/
5902 if (p->heredoc_indent > 0) {
5903 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5904 p->heredoc_indent = 0;
5905 }
5906 /*% ripper: xstring_literal!($:$) %*/
5907 }
5908 ;
5909
5910regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5911 {
5912 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5913 /*% ripper: regexp_literal!($:2, $:3) %*/
5914 }
5915 ;
5916
5917words : words(tWORDS_BEG, word_list)
5918 ;
5919
5920word_list : /* none */
5921 {
5922 $$ = 0;
5923 /*% ripper: words_new! %*/
5924 }
5925 | word_list word ' '+
5926 {
5927 $$ = list_append(p, $1, evstr2dstr(p, $2));
5928 /*% ripper: words_add!($:1, $:2) %*/
5929 }
5930 ;
5931
5932word : string_content
5933 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5934 | word string_content
5935 {
5936 $$ = literal_concat(p, $1, $2, &@$);
5937 /*% ripper: word_add!($:1, $:2) %*/
5938 }
5939 ;
5940
5941symbols : words(tSYMBOLS_BEG, symbol_list)
5942 ;
5943
5944symbol_list : /* none */
5945 {
5946 $$ = 0;
5947 /*% ripper: symbols_new! %*/
5948 }
5949 | symbol_list word ' '+
5950 {
5951 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5952 /*% ripper: symbols_add!($:1, $:2) %*/
5953 }
5954 ;
5955
5956qwords : words(tQWORDS_BEG, qword_list)
5957 ;
5958
5959qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5960 ;
5961
5962qword_list : /* none */
5963 {
5964 $$ = 0;
5965 /*% ripper: qwords_new! %*/
5966 }
5967 | qword_list tSTRING_CONTENT ' '+
5968 {
5969 $$ = list_append(p, $1, $2);
5970 /*% ripper: qwords_add!($:1, $:2) %*/
5971 }
5972 ;
5973
5974qsym_list : /* none */
5975 {
5976 $$ = 0;
5977 /*% ripper: qsymbols_new! %*/
5978 }
5979 | qsym_list tSTRING_CONTENT ' '+
5980 {
5981 $$ = symbol_append(p, $1, $2);
5982 /*% ripper: qsymbols_add!($:1, $:2) %*/
5983 }
5984 ;
5985
5986string_contents : /* none */
5987 {
5988 $$ = 0;
5989 /*% ripper: string_content! %*/
5990 }
5991 | string_contents string_content
5992 {
5993 $$ = literal_concat(p, $1, $2, &@$);
5994 /*% ripper: string_add!($:1, $:2) %*/
5995 }
5996 ;
5997
5998xstring_contents: /* none */
5999 {
6000 $$ = 0;
6001 /*% ripper: xstring_new! %*/
6002 }
6003 | xstring_contents string_content
6004 {
6005 $$ = literal_concat(p, $1, $2, &@$);
6006 /*% ripper: xstring_add!($:1, $:2) %*/
6007 }
6008 ;
6009
6010regexp_contents : /* none */
6011 {
6012 $$ = 0;
6013 /*% ripper: regexp_new! %*/
6014 }
6015 | regexp_contents string_content
6016 {
6017 NODE *head = $1, *tail = $2;
6018 if (!head) {
6019 $$ = tail;
6020 }
6021 else if (!tail) {
6022 $$ = head;
6023 }
6024 else {
6025 switch (nd_type(head)) {
6026 case NODE_STR:
6027 head = str2dstr(p, head);
6028 break;
6029 case NODE_DSTR:
6030 break;
6031 default:
6032 head = list_append(p, NEW_DSTR(0, &@$), head);
6033 break;
6034 }
6035 $$ = list_append(p, head, tail);
6036 }
6037 /*% ripper: regexp_add!($:1, $:2) %*/
6038 }
6039 ;
6040
6041string_content : tSTRING_CONTENT[content]
6042 /*% ripper[brace]: $:content %*/
6043 | tSTRING_DVAR[state]
6044 {
6045 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6046 $$ = p->lex.strterm;
6047 p->lex.strterm = 0;
6048 SET_LEX_STATE(EXPR_BEG);
6049 }[strterm]<strterm>
6050 string_dvar[dvar]
6051 {
6052 p->lex.strterm = $strterm;
6053 $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC);
6054 nd_set_line($$, @dvar.end_pos.lineno);
6055 /*% ripper: string_dvar!($:dvar) %*/
6056 }
6057 | tSTRING_DBEG[state]
6058 {
6059 CMDARG_PUSH(0);
6060 COND_PUSH(0);
6061 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6062 $$ = p->lex.strterm;
6063 p->lex.strterm = 0;
6064 SET_LEX_STATE(EXPR_BEG);
6065 }[term]<strterm>
6066 {
6067 $$ = p->lex.brace_nest;
6068 p->lex.brace_nest = 0;
6069 }[brace]<num>
6070 {
6071 $$ = p->lex.lpar_beg;
6072 p->lex.lpar_beg = -1;
6073 }[lpar]<num>
6074 {
6075 $$ = p->heredoc_indent;
6076 p->heredoc_indent = 0;
6077 }[indent]<num>
6078 compstmt(stmts) string_dend
6079 {
6080 COND_POP();
6081 CMDARG_POP();
6082 p->lex.strterm = $term;
6083 SET_LEX_STATE($state);
6084 p->lex.brace_nest = $brace;
6085 p->lex.lpar_beg = $lpar;
6086 p->heredoc_indent = $indent;
6087 p->heredoc_line_indent = -1;
6088 if ($compstmt) nd_unset_fl_newline($compstmt);
6089 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6090 /*% ripper: string_embexpr!($:compstmt) %*/
6091 }
6092 ;
6093
6094string_dend : tSTRING_DEND
6095 | END_OF_INPUT
6096 ;
6097
6098string_dvar : nonlocal_var
6099 {
6100 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6101 /*% ripper: var_ref!($:1) %*/
6102 }
6103 | backref
6104 ;
6105
6106symbol : ssym
6107 | dsym
6108 ;
6109
6110ssym : tSYMBEG sym
6111 {
6112 SET_LEX_STATE(EXPR_END);
6113 VALUE str = rb_id2str($2);
6114 /*
6115 * TODO:
6116 * set_yylval_noname sets invalid id to yylval.
6117 * This branch can be removed once yylval is changed to
6118 * hold lexed string.
6119 */
6120 if (!str) str = STR_NEW0();
6121 $$ = NEW_SYM(str, &@$);
6122 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6123 }
6124 ;
6125
6126sym : fname
6127 | nonlocal_var
6128 ;
6129
6130dsym : tSYMBEG string_contents tSTRING_END
6131 {
6132 SET_LEX_STATE(EXPR_END);
6133 $$ = dsym_node(p, $2, &@$);
6134 /*% ripper: dyna_symbol!($:2) %*/
6135 }
6136 ;
6137
6138numeric : simple_numeric
6139 | tUMINUS_NUM simple_numeric %prec tLOWEST
6140 {
6141 $$ = $2;
6142 negate_lit(p, $$, &@$);
6143 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6144 }
6145 ;
6146
6147simple_numeric : tINTEGER
6148 | tFLOAT
6149 | tRATIONAL
6150 | tIMAGINARY
6151 ;
6152
6153nonlocal_var : tIVAR
6154 | tGVAR
6155 | tCVAR
6156 ;
6157
6158user_variable : ident_or_const
6159 | nonlocal_var
6160 ;
6161
6162keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6163 | keyword_self {$$ = KWD2EID(self, $1);}
6164 | keyword_true {$$ = KWD2EID(true, $1);}
6165 | keyword_false {$$ = KWD2EID(false, $1);}
6166 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6167 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6168 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6169 ;
6170
6171var_ref : user_variable
6172 {
6173 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6174 if (ifdef_ripper(id_is_var(p, $1), false)) {
6175 /*% ripper: var_ref!($:1) %*/
6176 }
6177 else {
6178 /*% ripper: vcall!($:1) %*/
6179 }
6180 }
6181 | keyword_variable
6182 {
6183 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6184 /*% ripper: var_ref!($:1) %*/
6185 }
6186 ;
6187
6188var_lhs : user_or_keyword_variable
6189 {
6190 /*% ripper: var_field!($:1) %*/
6191 $$ = assignable(p, $1, 0, &@$);
6192 }
6193 ;
6194
6195backref : tNTH_REF
6196 | tBACK_REF
6197 ;
6198
6199superclass : '<'
6200 {
6201 SET_LEX_STATE(EXPR_BEG);
6202 p->command_start = TRUE;
6203 }
6204 expr_value term
6205 {
6206 $$ = $3;
6207 /*% ripper: $:3 %*/
6208 }
6209 | none
6210 ;
6211
6212f_opt_paren_args: f_paren_args
6213 | f_empty_arg
6214 {
6215 p->ctxt.in_argdef = 0;
6216 }
6217 ;
6218
6219f_empty_arg : /* none */
6220 {
6221 $$ = new_empty_args_tail(p, &@$);
6222 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6223 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6224 }
6225 ;
6226
6227f_paren_args : '(' f_args rparen
6228 {
6229 $$ = $2;
6230 /*% ripper: paren!($:2) %*/
6231 SET_LEX_STATE(EXPR_BEG);
6232 p->command_start = TRUE;
6233 p->ctxt.in_argdef = 0;
6234 }
6235 ;
6236
6237f_arglist : f_paren_args
6238 | {
6239 $$ = p->ctxt;
6240 p->ctxt.in_kwarg = 1;
6241 p->ctxt.in_argdef = 1;
6242 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6243 }<ctxt>
6244 f_args term
6245 {
6246 p->ctxt.in_kwarg = $1.in_kwarg;
6247 p->ctxt.in_argdef = 0;
6248 $$ = $2;
6249 SET_LEX_STATE(EXPR_BEG);
6250 p->command_start = TRUE;
6251 /*% ripper: $:2 %*/
6252 }
6253 ;
6254
6255args_tail : args_tail_basic(arg_value, opt_comma)
6256 | args_forward
6257 {
6258 add_forwarding_args(p);
6259 $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward);
6260 $$->nd_ainfo.forwarding = 1;
6261 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6262 }
6263 ;
6264
6265largs_tail : args_tail_basic(arg_value, none)
6266 | args_forward
6267 {
6268 yyerror1(&@args_forward, "unexpected ... in lambda argument");
6269 $$ = new_args_tail(p, 0, 0, 0, &@args_forward);
6270 $$->nd_ainfo.forwarding = 1;
6271 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6272 }
6273 ;
6274
6275%rule args-list(value, tail) <node_args>
6276 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6277 {
6278 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6279 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6280 }
6281 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6282 {
6283 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6284 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6285 }
6286 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6287 {
6288 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6289 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6290 }
6291 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6292 {
6293 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6294 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6295 }
6296 | f_arg[pre] ',' f_rest_arg[rest] tail
6297 {
6298 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6299 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6300 }
6301 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6302 {
6303 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6304 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6305 }
6306 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6307 {
6308 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6309 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6310 }
6311 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6312 {
6313 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6314 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6315 }
6316 | f_opt_arg(value)[opt] tail
6317 {
6318 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6319 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6320 }
6321 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6322 {
6323 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6324 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6325 }
6326 | f_rest_arg[rest] tail
6327 {
6328 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6329 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6330 }
6331 | f_rest_arg[rest] ',' f_arg[post] tail
6332 {
6333 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6334 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6335 }
6336 ;
6337
6338%rule tail-only-args(tail) <node_args>
6339 : tail
6340 {
6341 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6342 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6343 }
6344 ;
6345
6346%rule f_args-list(tail, trailing) <node_args>
6347 : args-list(arg_value, opt_args_tail(tail, trailing))
6348 | f_arg[pre] opt_args_tail(tail, trailing)[tail]
6349 {
6350 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6351 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6352 }
6353 | tail-only-args(tail)
6354 | f_empty_arg
6355 ;
6356
6357f_args : f_args-list(args_tail, opt_comma)
6358 ;
6359
6360f_largs : f_args-list(largs_tail, none)
6361 ;
6362
6363args_forward : tBDOT3
6364 {
6365 $$ = idFWD_KWREST;
6366 /*% ripper: args_forward! %*/
6367 }
6368 ;
6369
6370f_bad_arg : tCONSTANT
6371 {
6372 static const char mesg[] = "formal argument cannot be a constant";
6373 /*%%%*/
6374 yyerror1(&@1, mesg);
6375 /*% %*/
6376 $$ = 0;
6377 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6378 }
6379 | tIVAR
6380 {
6381 static const char mesg[] = "formal argument cannot be an instance variable";
6382 /*%%%*/
6383 yyerror1(&@1, mesg);
6384 /*% %*/
6385 $$ = 0;
6386 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6387 }
6388 | tGVAR
6389 {
6390 static const char mesg[] = "formal argument cannot be a global variable";
6391 /*%%%*/
6392 yyerror1(&@1, mesg);
6393 /*% %*/
6394 $$ = 0;
6395 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6396 }
6397 | tCVAR
6398 {
6399 static const char mesg[] = "formal argument cannot be a class variable";
6400 /*%%%*/
6401 yyerror1(&@1, mesg);
6402 /*% %*/
6403 $$ = 0;
6404 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6405 }
6406 ;
6407
6408f_norm_arg : f_bad_arg
6409 | tIDENTIFIER
6410 {
6411 VALUE e = formal_argument_error(p, $$ = $1);
6412 if (e) {
6413 /*% ripper[error]: param_error!(?e, $:1) %*/
6414 }
6415 p->max_numparam = ORDINAL_PARAM;
6416 }
6417 ;
6418
6419f_arg_asgn : f_norm_arg
6420 {
6421 arg_var(p, $1);
6422 $$ = $1;
6423 }
6424 ;
6425
6426f_arg_item : f_arg_asgn
6427 {
6428 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6429 /*% ripper: $:1 %*/
6430 }
6431 | tLPAREN f_margs rparen
6432 {
6433 ID tid = internal_id(p);
6434 YYLTYPE loc;
6435 loc.beg_pos = @2.beg_pos;
6436 loc.end_pos = @2.beg_pos;
6437 arg_var(p, tid);
6438 if (dyna_in_block(p)) {
6439 $2->nd_value = NEW_DVAR(tid, &loc);
6440 }
6441 else {
6442 $2->nd_value = NEW_LVAR(tid, &loc);
6443 }
6444 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6445 $$->nd_next = (NODE *)$2;
6446 /*% ripper: mlhs_paren!($:2) %*/
6447 }
6448 ;
6449
6450f_arg : f_arg_item
6451 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6452 | f_arg ',' f_arg_item
6453 {
6454 $$ = $1;
6455 $$->nd_plen++;
6456 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6457 rb_discard_node(p, (NODE *)$3);
6458 /*% ripper: rb_ary_push($:1, $:3) %*/
6459 }
6460 ;
6461
6462
6463f_label : tLABEL
6464 {
6465 VALUE e = formal_argument_error(p, $$ = $1);
6466 if (e) {
6467 $$ = 0;
6468 /*% ripper[error]: param_error!(?e, $:1) %*/
6469 }
6470 /*
6471 * Workaround for Prism::ParseTest#test_filepath for
6472 * "unparser/corpus/literal/def.txt"
6473 *
6474 * See the discussion on https://github.com/ruby/ruby/pull/9923
6475 */
6476 arg_var(p, ifdef_ripper(0, $1));
6477 /*% ripper: $:1 %*/
6478 p->max_numparam = ORDINAL_PARAM;
6479 p->ctxt.in_argdef = 0;
6480 }
6481 ;
6482
6483kwrest_mark : tPOW
6484 | tDSTAR
6485 ;
6486
6487f_no_kwarg : p_kwnorest
6488 {
6489 /*% ripper: nokw_param!(Qnil) %*/
6490 }
6491 ;
6492
6493f_kwrest : kwrest_mark tIDENTIFIER
6494 {
6495 arg_var(p, shadowing_lvar(p, $2));
6496 $$ = $2;
6497 /*% ripper: kwrest_param!($:2) %*/
6498 }
6499 | kwrest_mark
6500 {
6501 arg_var(p, idFWD_KWREST);
6502 $$ = idFWD_KWREST;
6503 /*% ripper: kwrest_param!(Qnil) %*/
6504 }
6505 ;
6506
6507restarg_mark : '*'
6508 | tSTAR
6509 ;
6510
6511f_rest_arg : restarg_mark tIDENTIFIER
6512 {
6513 arg_var(p, shadowing_lvar(p, $2));
6514 $$ = $2;
6515 /*% ripper: rest_param!($:2) %*/
6516 }
6517 | restarg_mark
6518 {
6519 arg_var(p, idFWD_REST);
6520 $$ = idFWD_REST;
6521 /*% ripper: rest_param!(Qnil) %*/
6522 }
6523 ;
6524
6525blkarg_mark : '&'
6526 | tAMPER
6527 ;
6528
6529f_block_arg : blkarg_mark tIDENTIFIER
6530 {
6531 arg_var(p, shadowing_lvar(p, $2));
6532 $$ = $2;
6533 /*% ripper: blockarg!($:2) %*/
6534 }
6535 | blkarg_mark keyword_nil
6536 {
6537 $$ = idNil;
6538 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6539 }
6540 | blkarg_mark
6541 {
6542 arg_var(p, idFWD_BLOCK);
6543 $$ = idFWD_BLOCK;
6544 /*% ripper: blockarg!(Qnil) %*/
6545 }
6546 ;
6547
6548opt_comma : ','?
6549 {
6550 $$ = 0;
6551 /*% ripper: Qnil %*/
6552 }
6553 ;
6554
6555
6556singleton : value_expr(singleton_expr)
6557 {
6558 NODE *expr = last_expr_node($1);
6559 switch (nd_type(expr)) {
6560 case NODE_STR:
6561 case NODE_DSTR:
6562 case NODE_XSTR:
6563 case NODE_DXSTR:
6564 case NODE_REGX:
6565 case NODE_DREGX:
6566 case NODE_SYM:
6567 case NODE_LINE:
6568 case NODE_FILE:
6569 case NODE_ENCODING:
6570 case NODE_INTEGER:
6571 case NODE_FLOAT:
6572 case NODE_RATIONAL:
6573 case NODE_IMAGINARY:
6574 case NODE_DSYM:
6575 case NODE_LIST:
6576 case NODE_ZLIST:
6577 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6578 break;
6579 default:
6580 break;
6581 }
6582 $$ = $1;
6583 }
6584 ;
6585
6586singleton_expr : var_ref
6587 | '('
6588 {
6589 SET_LEX_STATE(EXPR_BEG);
6590 p->ctxt.in_argdef = 0;
6591 }
6592 expr rparen
6593 {
6594 p->ctxt.in_argdef = 1;
6595 $$ = $3;
6596 /*% ripper: paren!($:3) %*/
6597 }
6598 ;
6599
6600assoc_list : none
6601 | assocs trailer
6602 {
6603 $$ = $1;
6604 /*% ripper: assoclist_from_args!($:1) %*/
6605 }
6606 ;
6607
6608assocs : assoc
6609 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6610 | assocs ',' assoc
6611 {
6612 NODE *assocs = $1;
6613 NODE *tail = $3;
6614 if (!assocs) {
6615 assocs = tail;
6616 }
6617 else if (tail) {
6618 NODE *n = RNODE_LIST(tail)->nd_next;
6619 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6620 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6621 /* DSTAR */
6622 tail = RNODE_HASH(n)->nd_head;
6623 }
6624 if (tail) {
6625 assocs = list_concat(assocs, tail);
6626 }
6627 }
6628 $$ = assocs;
6629 /*% ripper: rb_ary_push($:1, $:3) %*/
6630 }
6631 ;
6632
6633assoc : arg_value tASSOC arg_value
6634 {
6635 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6636 /*% ripper: assoc_new!($:1, $:3) %*/
6637 }
6638 | tLABEL arg_value
6639 {
6640 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6641 /*% ripper: assoc_new!($:1, $:2) %*/
6642 }
6643 | tLABEL
6644 {
6645 NODE *val = gettable(p, $1, &@$);
6646 if (!val) val = NEW_ERROR(&@$);
6647 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6648 /*% ripper: assoc_new!($:1, Qnil) %*/
6649 }
6650 | tSTRING_BEG string_contents tLABEL_END arg_value
6651 {
6652 YYLTYPE loc = code_loc_gen(&@1, &@3);
6653 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6654 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6655 }
6656 | tDSTAR arg_value
6657 {
6658 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6659 /*% ripper: assoc_splat!($:2) %*/
6660 }
6661 | tDSTAR
6662 {
6663 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6664 $$ = list_append(p, NEW_LIST(0, &@$),
6665 NEW_LVAR(idFWD_KWREST, &@$));
6666 /*% ripper: assoc_splat!(Qnil) %*/
6667 }
6668 ;
6669
6670%rule %inline operation : ident_or_const
6671 | tFID
6672 ;
6673
6674operation2 : operation
6675 | op
6676 ;
6677
6678operation3 : tIDENTIFIER
6679 | tFID
6680 | op
6681 ;
6682
6683dot_or_colon : '.'
6684 | tCOLON2
6685 ;
6686
6687call_op : '.'
6688 | tANDDOT
6689 ;
6690
6691call_op2 : call_op
6692 | tCOLON2
6693 ;
6694
6695rparen : '\n'? ')'
6696 ;
6697
6698rbracket : '\n'? ']'
6699 ;
6700
6701rbrace : '\n'? '}'
6702 ;
6703
6704trailer : '\n'?
6705 | ','
6706 ;
6707
6708term : ';'
6709 {
6710 yyerrok;
6711 token_flush(p);
6712 if (p->ctxt.in_defined) {
6713 p->ctxt.has_trailing_semicolon = 1;
6714 }
6715 }
6716 | '\n'
6717 {
6718 @$.end_pos = @$.beg_pos;
6719 token_flush(p);
6720 }
6721 ;
6722
6723terms : term
6724 | terms ';' {yyerrok;}
6725 ;
6726
6727none : /* none */
6728 {
6729 $$ = 0;
6730 /*% ripper: Qnil %*/
6731 }
6732 ;
6733%%
6734# undef p
6735# undef yylex
6736# undef yylval
6737# define yylval (*p->lval)
6738
6739static int regx_options(struct parser_params*);
6740static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6741static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6742static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6743static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6744
6745#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6746
6747# define set_yylval_node(x) { \
6748 YYLTYPE _cur_loc; \
6749 rb_parser_set_location(p, &_cur_loc); \
6750 yylval.node = (x); \
6751 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6752}
6753# define set_yylval_str(x) \
6754do { \
6755 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6756 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6757} while(0)
6758# define set_yylval_num(x) { \
6759 yylval.num = (x); \
6760 set_parser_s_value(x); \
6761}
6762# define set_yylval_id(x) (yylval.id = (x))
6763# define set_yylval_name(x) { \
6764 (yylval.id = (x)); \
6765 set_parser_s_value(ID2SYM(x)); \
6766}
6767# define yylval_id() (yylval.id)
6768
6769#define set_yylval_noname() set_yylval_id(keyword_nil)
6770#define has_delayed_token(p) (p->delayed.token != NULL)
6771
6772#ifndef RIPPER
6773#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6774#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6775
6776static bool
6777parser_has_token(struct parser_params *p)
6778{
6779 const char *const pcur = p->lex.pcur;
6780 const char *const ptok = p->lex.ptok;
6781 if (p->keep_tokens && (pcur < ptok)) {
6782 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6783 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6784 }
6785 return pcur > ptok;
6786}
6787
6788static const char *
6789escaped_char(int c)
6790{
6791 switch (c) {
6792 case '"': return "\\\"";
6793 case '\\': return "\\\\";
6794 case '\0': return "\\0";
6795 case '\n': return "\\n";
6796 case '\r': return "\\r";
6797 case '\t': return "\\t";
6798 case '\f': return "\\f";
6799 case '\013': return "\\v";
6800 case '\010': return "\\b";
6801 case '\007': return "\\a";
6802 case '\033': return "\\e";
6803 case '\x7f': return "\\c?";
6804 }
6805 return NULL;
6806}
6807
6808static rb_parser_string_t *
6809rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6810{
6811 rb_encoding *enc = p->enc;
6812 const char *ptr = str->ptr;
6813 const char *pend = ptr + str->len;
6814 const char *prev = ptr;
6815 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6816 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6817
6818 while (ptr < pend) {
6819 unsigned int c;
6820 const char *cc;
6821 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6822 if (!MBCLEN_CHARFOUND_P(n)) {
6823 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6824 n = rb_enc_mbminlen(enc);
6825 if (pend < ptr + n)
6826 n = (int)(pend - ptr);
6827 while (n--) {
6828 c = ((unsigned char)*ptr >> 4) & 0x0f;
6829 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6830 c = *ptr & 0x0f;
6831 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6832 parser_str_cat(result, charbuf, 4);
6833 prev = ++ptr;
6834 }
6835 continue;
6836 }
6837 n = MBCLEN_CHARFOUND_LEN(n);
6838 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6839 ptr += n;
6840 cc = escaped_char(c);
6841 if (cc) {
6842 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6843 parser_str_cat_cstr(result, cc);
6844 prev = ptr;
6845 }
6846 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6847 }
6848 else {
6849 if (ptr - n > prev) {
6850 parser_str_cat(result, prev, ptr - n - prev);
6851 prev = ptr - n;
6852 }
6853 parser_str_cat(result, prev, ptr - prev);
6854 prev = ptr;
6855 }
6856 }
6857 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6858
6859 return result;
6860}
6861
6862static void
6863parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6864{
6865 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6866 token->id = p->token_id;
6867 token->type_name = parser_token2char(p, t);
6868 token->str = str;
6869 token->loc.beg_pos = p->yylloc->beg_pos;
6870 token->loc.end_pos = p->yylloc->end_pos;
6871 rb_parser_ary_push_ast_token(p, p->tokens, token);
6872 p->token_id++;
6873
6874 if (p->debug) {
6875 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6876 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6877 line, token->id, token->type_name, str_escaped->ptr,
6878 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6879 token->loc.end_pos.lineno, token->loc.end_pos.column);
6880 rb_parser_string_free(p, str_escaped);
6881 }
6882}
6883
6884static void
6885parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6886{
6887 debug_token_line(p, "parser_dispatch_scan_event", line);
6888
6889 if (!parser_has_token(p)) return;
6890
6891 RUBY_SET_YYLLOC(*p->yylloc);
6892
6893 if (p->keep_tokens) {
6894 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6895 parser_append_tokens(p, str, t, line);
6896 }
6897
6898 token_flush(p);
6899}
6900
6901#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6902static void
6903parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6904{
6905 debug_token_line(p, "parser_dispatch_delayed_token", line);
6906
6907 if (!has_delayed_token(p)) return;
6908
6909 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6910
6911 if (p->keep_tokens) {
6912 /* p->delayed.token is freed by rb_parser_tokens_free */
6913 parser_append_tokens(p, p->delayed.token, t, line);
6914 }
6915 else {
6916 rb_parser_string_free(p, p->delayed.token);
6917 }
6918
6919 p->delayed.token = NULL;
6920}
6921#else
6922#define literal_flush(p, ptr) ((void)(ptr))
6923
6924static int
6925ripper_has_scan_event(struct parser_params *p)
6926{
6927 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6928 return p->lex.pcur > p->lex.ptok;
6929}
6930
6931static VALUE
6932ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6933{
6934 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6935 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6936 RUBY_SET_YYLLOC(*p->yylloc);
6937 token_flush(p);
6938 return rval;
6939}
6940
6941static void
6942ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6943{
6944 if (!ripper_has_scan_event(p)) return;
6945
6946 set_parser_s_value(ripper_scan_event_val(p, t));
6947}
6948#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6949
6950static void
6951ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6952{
6953 /* save and adjust the location to delayed token for callbacks */
6954 int saved_line = p->ruby_sourceline;
6955 const char *saved_tokp = p->lex.ptok;
6956 VALUE s_value, str;
6957
6958 if (!has_delayed_token(p)) return;
6959 p->ruby_sourceline = p->delayed.beg_line;
6960 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6961 str = rb_str_new_mutable_parser_string(p->delayed.token);
6962 rb_parser_string_free(p, p->delayed.token);
6963 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6964 set_parser_s_value(s_value);
6965 p->delayed.token = NULL;
6966 p->ruby_sourceline = saved_line;
6967 p->lex.ptok = saved_tokp;
6968}
6969#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6970#endif /* RIPPER */
6971
6972static inline int
6973is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6974{
6975 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6976}
6977
6978static inline bool
6979peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
6980{
6981 const char *ptr = p->lex.pcur + at;
6982 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
6983 if (memcmp(ptr, str, len)) return false;
6984 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
6985 switch (ptr[len]) {
6986 case '!': case '?': return false;
6987 }
6988 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
6989}
6990
6991static inline int
6992parser_is_identchar(struct parser_params *p)
6993{
6994 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6995}
6996
6997static inline int
6998parser_isascii(struct parser_params *p)
6999{
7000 return ISASCII(*(p->lex.pcur-1));
7001}
7002
7003static void
7004token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7005{
7006 int column = 1, nonspc = 0, i;
7007 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7008 if (*ptr == '\t') {
7009 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7010 }
7011 column++;
7012 if (*ptr != ' ' && *ptr != '\t') {
7013 nonspc = 1;
7014 }
7015 }
7016
7017 ptinfo->beg = loc->beg_pos;
7018 ptinfo->indent = column;
7019 ptinfo->nonspc = nonspc;
7020}
7021
7022static void
7023token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7024{
7025 token_info *ptinfo;
7026
7027 if (!p->token_info_enabled) return;
7028 ptinfo = ALLOC(token_info);
7029 ptinfo->token = token;
7030 ptinfo->next = p->token_info;
7031 token_info_setup(ptinfo, p->lex.pbeg, loc);
7032
7033 p->token_info = ptinfo;
7034}
7035
7036static void
7037token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7038{
7039 token_info *ptinfo_beg = p->token_info;
7040
7041 if (!ptinfo_beg) return;
7042
7043 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7044 token_info_warn(p, token, ptinfo_beg, 1, loc);
7045
7046 p->token_info = ptinfo_beg->next;
7047 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7048}
7049
7050static void
7051token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7052{
7053 token_info *ptinfo_beg = p->token_info;
7054
7055 if (!ptinfo_beg) return;
7056 p->token_info = ptinfo_beg->next;
7057
7058 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7059 ptinfo_beg->beg.column != beg_pos.column ||
7060 strcmp(ptinfo_beg->token, token)) {
7061 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7062 beg_pos.lineno, beg_pos.column, token,
7063 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7064 ptinfo_beg->token);
7065 }
7066
7067 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7068}
7069
7070static void
7071token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7072{
7073 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7074 if (!p->token_info_enabled) return;
7075 if (!ptinfo_beg) return;
7076 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7077 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7078 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7079 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7080 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7081 rb_warn3L(ptinfo_end->beg.lineno,
7082 "mismatched indentations at '%s' with '%s' at %d",
7083 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7084}
7085
7086static int
7087parser_precise_mbclen(struct parser_params *p, const char *ptr)
7088{
7089 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7090 if (!MBCLEN_CHARFOUND_P(len)) {
7091 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7092 return -1;
7093 }
7094 return len;
7095}
7096
7097#ifndef RIPPER
7098static inline void
7099parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7100{
7101 rb_parser_string_t *str;
7102 int lineno = p->ruby_sourceline;
7103 if (!yylloc) {
7104 return;
7105 }
7106 else if (yylloc->beg_pos.lineno == lineno) {
7107 str = p->lex.lastline;
7108 }
7109 else {
7110 return;
7111 }
7112 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7113}
7114
7115static int
7116parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7117{
7118#if 0
7119 YYLTYPE current;
7120
7121 if (!yylloc) {
7122 yylloc = RUBY_SET_YYLLOC(current);
7123 }
7124 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7125 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7126 yylloc = 0;
7127 }
7128#endif
7129 parser_compile_error(p, yylloc, "%s", msg);
7130 parser_show_error_line(p, yylloc);
7131 return 0;
7132}
7133
7134static int
7135parser_yyerror0(struct parser_params *p, const char *msg)
7136{
7137 YYLTYPE current;
7138 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7139}
7140
7141void
7142ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7143{
7144 VALUE mesg;
7145 const int max_line_margin = 30;
7146 const char *ptr, *ptr_end, *pt, *pb;
7147 const char *pre = "", *post = "", *pend;
7148 const char *code = "", *caret = "";
7149 const char *lim;
7150 const char *const pbeg = PARSER_STRING_PTR(str);
7151 char *buf;
7152 long len;
7153 int i;
7154
7155 if (!yylloc) return;
7156 pend = rb_parser_string_end(str);
7157 if (pend > pbeg && pend[-1] == '\n') {
7158 if (--pend > pbeg && pend[-1] == '\r') --pend;
7159 }
7160
7161 pt = pend;
7162 if (lineno == yylloc->end_pos.lineno &&
7163 (pend - pbeg) > yylloc->end_pos.column) {
7164 pt = pbeg + yylloc->end_pos.column;
7165 }
7166
7167 ptr = ptr_end = pt;
7168 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7169 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7170
7171 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7172 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7173
7174 len = ptr_end - ptr;
7175 if (len > 4) {
7176 if (ptr > pbeg) {
7177 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7178 if (ptr > pbeg) pre = "...";
7179 }
7180 if (ptr_end < pend) {
7181 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7182 if (ptr_end < pend) post = "...";
7183 }
7184 }
7185 pb = pbeg;
7186 if (lineno == yylloc->beg_pos.lineno) {
7187 pb += yylloc->beg_pos.column;
7188 if (pb > pt) pb = pt;
7189 }
7190 if (pb < ptr) pb = ptr;
7191 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7192 return;
7193 }
7194 if (RTEST(errbuf)) {
7195 mesg = rb_attr_get(errbuf, idMesg);
7196 if (char_at_end(p, mesg, '\n') != '\n')
7197 rb_str_cat_cstr(mesg, "\n");
7198 }
7199 else {
7200 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7201 }
7202 if (!errbuf && rb_stderr_tty_p()) {
7203#define CSI_BEGIN "\033["
7204#define CSI_SGR "m"
7205 rb_str_catf(mesg,
7206 CSI_BEGIN""CSI_SGR"%s" /* pre */
7207 CSI_BEGIN"1"CSI_SGR"%.*s"
7208 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7209 CSI_BEGIN";1"CSI_SGR"%.*s"
7210 CSI_BEGIN""CSI_SGR"%s" /* post */
7211 "\n",
7212 pre,
7213 (int)(pb - ptr), ptr,
7214 (int)(pt - pb), pb,
7215 (int)(ptr_end - pt), pt,
7216 post);
7217 }
7218 else {
7219 char *p2;
7220
7221 len = ptr_end - ptr;
7222 lim = pt < pend ? pt : pend;
7223 i = (int)(lim - ptr);
7224 buf = ALLOCA_N(char, i+2);
7225 code = ptr;
7226 caret = p2 = buf;
7227 if (ptr <= pb) {
7228 while (ptr < pb) {
7229 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7230 }
7231 *p2++ = '^';
7232 ptr++;
7233 }
7234 if (lim > ptr) {
7235 memset(p2, '~', (lim - ptr));
7236 p2 += (lim - ptr);
7237 }
7238 *p2 = '\0';
7239 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7240 pre, (int)len, code, post,
7241 pre, caret);
7242 }
7243 if (!errbuf) rb_write_error_str(mesg);
7244}
7245#else
7246
7247static int
7248parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7249{
7250 const char *pcur = 0, *ptok = 0;
7251 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7252 p->ruby_sourceline == yylloc->end_pos.lineno) {
7253 pcur = p->lex.pcur;
7254 ptok = p->lex.ptok;
7255 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7256 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7257 }
7258 parser_yyerror0(p, msg);
7259 if (pcur) {
7260 p->lex.ptok = ptok;
7261 p->lex.pcur = pcur;
7262 }
7263 return 0;
7264}
7265
7266static int
7267parser_yyerror0(struct parser_params *p, const char *msg)
7268{
7269 dispatch1(parse_error, STR_NEW2(msg));
7270 ripper_error(p);
7271 return 0;
7272}
7273
7274static inline void
7275parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7276{
7277}
7278#endif /* !RIPPER */
7279
7280static int
7281vtable_size(const struct vtable *tbl)
7282{
7283 if (!DVARS_TERMINAL_P(tbl)) {
7284 return tbl->pos;
7285 }
7286 else {
7287 return 0;
7288 }
7289}
7290
7291static struct vtable *
7292vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7293{
7294 struct vtable *tbl = ALLOC(struct vtable);
7295 tbl->pos = 0;
7296 tbl->capa = 8;
7297 tbl->tbl = ALLOC_N(ID, tbl->capa);
7298 tbl->prev = prev;
7299#ifndef RIPPER
7300 if (p->debug) {
7301 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7302 }
7303#endif
7304 return tbl;
7305}
7306#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7307
7308static void
7309vtable_free_gen(struct parser_params *p, int line, const char *name,
7310 struct vtable *tbl)
7311{
7312#ifndef RIPPER
7313 if (p->debug) {
7314 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7315 }
7316#endif
7317 if (!DVARS_TERMINAL_P(tbl)) {
7318 if (tbl->tbl) {
7319 ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID));
7320 }
7321 ruby_xfree_sized(tbl, sizeof(*tbl));
7322 }
7323}
7324#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7325
7326static void
7327vtable_add_gen(struct parser_params *p, int line, const char *name,
7328 struct vtable *tbl, ID id)
7329{
7330#ifndef RIPPER
7331 if (p->debug) {
7332 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7333 line, name, (void *)tbl, rb_id2name(id));
7334 }
7335#endif
7336 if (DVARS_TERMINAL_P(tbl)) {
7337 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7338 return;
7339 }
7340 if (tbl->pos == tbl->capa) {
7341 tbl->capa = tbl->capa * 2;
7342 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7343 }
7344 tbl->tbl[tbl->pos++] = id;
7345}
7346#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7347
7348static void
7349vtable_pop_gen(struct parser_params *p, int line, const char *name,
7350 struct vtable *tbl, int n)
7351{
7352 if (p->debug) {
7353 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7354 line, name, (void *)tbl, n);
7355 }
7356 if (tbl->pos < n) {
7357 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7358 return;
7359 }
7360 tbl->pos -= n;
7361}
7362#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7363
7364static int
7365vtable_included(const struct vtable * tbl, ID id)
7366{
7367 int i;
7368
7369 if (!DVARS_TERMINAL_P(tbl)) {
7370 for (i = 0; i < tbl->pos; i++) {
7371 if (tbl->tbl[i] == id) {
7372 return i+1;
7373 }
7374 }
7375 }
7376 return 0;
7377}
7378
7379static void parser_prepare(struct parser_params *p);
7380
7381static int
7382e_option_supplied(struct parser_params *p)
7383{
7384 return strcmp(p->ruby_sourcefile, "-e") == 0;
7385}
7386
7387#ifndef RIPPER
7388static NODE *parser_append_options(struct parser_params *p, NODE *node);
7389
7390static VALUE
7391yycompile0(VALUE arg)
7392{
7393 int n;
7394 NODE *tree;
7395 struct parser_params *p = (struct parser_params *)arg;
7396 int cov = FALSE;
7397
7398 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7399 cov = TRUE;
7400 }
7401
7402 if (p->debug_lines) {
7403 p->ast->body.script_lines = p->debug_lines;
7404 }
7405
7406 parser_prepare(p);
7407#define RUBY_DTRACE_PARSE_HOOK(name) \
7408 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7409 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7410 }
7411 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7412 n = yyparse(p);
7413 RUBY_DTRACE_PARSE_HOOK(END);
7414
7415 p->debug_lines = 0;
7416
7417 xfree(p->lex.strterm);
7418 p->lex.strterm = 0;
7419 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7420 if (n || p->error_p) {
7421 VALUE mesg = p->error_buffer;
7422 if (!mesg) {
7423 mesg = syntax_error_new();
7424 }
7425 if (!p->error_tolerant) {
7426 rb_set_errinfo(mesg);
7427 return FALSE;
7428 }
7429 }
7430 tree = p->eval_tree;
7431 if (!tree) {
7432 tree = NEW_NIL(&NULL_LOC);
7433 }
7434 else {
7435 rb_parser_ary_t *tokens = p->tokens;
7436 NODE *prelude;
7437 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7438 prelude = block_append(p, p->eval_tree_begin, body);
7439 RNODE_SCOPE(tree)->nd_body = prelude;
7440 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7441 p->ast->body.coverage_enabled = cov;
7442 if (p->keep_tokens) {
7443 p->ast->node_buffer->tokens = tokens;
7444 p->tokens = NULL;
7445 }
7446 }
7447 p->ast->body.root = tree;
7448 p->ast->body.line_count = p->line_count;
7449 return TRUE;
7450}
7451
7452static rb_ast_t *
7453yycompile(struct parser_params *p, VALUE fname, int line)
7454{
7455 rb_ast_t *ast;
7456 if (NIL_P(fname)) {
7457 p->ruby_sourcefile_string = Qnil;
7458 p->ruby_sourcefile = "(none)";
7459 }
7460 else {
7461 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7462 p->ruby_sourcefile = StringValueCStr(fname);
7463 }
7464 p->ruby_sourceline = line - 1;
7465
7466 p->lvtbl = NULL;
7467
7468 p->ast = ast = rb_ast_new();
7469 compile_callback(yycompile0, (VALUE)p);
7470 ast->body.source_hash = rb_source_hash_finalize(&p->source_hash);
7471 ast->body.has_source_hash = 1;
7472 p->ast = 0;
7473
7474 while (p->lvtbl) {
7475 local_pop(p);
7476 }
7477
7478 return ast;
7479}
7480#endif /* !RIPPER */
7481
7482static rb_encoding *
7483must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7484{
7485 rb_encoding *enc = rb_parser_str_get_encoding(s);
7486 if (!rb_enc_asciicompat(enc)) {
7487 rb_raise(rb_eArgError, "invalid source encoding");
7488 }
7489 return enc;
7490}
7491
7492static rb_parser_string_t *
7493lex_getline(struct parser_params *p)
7494{
7495 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7496 if (!line) return 0;
7497 p->line_count++;
7498 rb_source_hash_update(&p->source_hash, (const uint8_t *)line->ptr, (size_t)line->len);
7499 string_buffer_append(p, line);
7500 must_be_ascii_compatible(p, line);
7501 return line;
7502}
7503
7504#ifndef RIPPER
7505rb_ast_t*
7506rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7507{
7508 p->lex.gets = gets;
7509 p->lex.input = input;
7510 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7511
7512 return yycompile(p, fname, line);
7513}
7514#endif /* !RIPPER */
7515
7516#define STR_FUNC_ESCAPE 0x01
7517#define STR_FUNC_EXPAND 0x02
7518#define STR_FUNC_REGEXP 0x04
7519#define STR_FUNC_QWORDS 0x08
7520#define STR_FUNC_SYMBOL 0x10
7521#define STR_FUNC_INDENT 0x20
7522#define STR_FUNC_LABEL 0x40
7523#define STR_FUNC_LIST 0x4000
7524#define STR_FUNC_TERM 0x8000
7525
7526enum string_type {
7527 str_label = STR_FUNC_LABEL,
7528 str_squote = (0),
7529 str_dquote = (STR_FUNC_EXPAND),
7530 str_xquote = (STR_FUNC_EXPAND),
7531 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7532 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7533 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7534 str_ssym = (STR_FUNC_SYMBOL),
7535 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7536};
7537
7538static rb_parser_string_t *
7539parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7540{
7541 rb_parser_string_t *pstr;
7542
7543 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7544
7545 if (!(func & STR_FUNC_REGEXP)) {
7546 if (rb_parser_is_ascii_string(p, pstr)) {
7547 }
7548 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7549 /* everything is valid in ASCII-8BIT */
7550 enc = rb_ascii8bit_encoding();
7551 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7552 }
7553 }
7554
7555 return pstr;
7556}
7557
7558static int
7559strterm_is_heredoc(rb_strterm_t *strterm)
7560{
7561 return strterm->heredoc;
7562}
7563
7564static rb_strterm_t *
7565new_strterm(struct parser_params *p, int func, int term, int paren)
7566{
7567 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7568 strterm->u.literal.func = func;
7569 strterm->u.literal.term = term;
7570 strterm->u.literal.paren = paren;
7571 return strterm;
7572}
7573
7574static rb_strterm_t *
7575new_heredoc(struct parser_params *p)
7576{
7577 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7578 strterm->heredoc = true;
7579 return strterm;
7580}
7581
7582#define peek(p,c) peek_n(p, (c), 0)
7583#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7584#define peekc(p) peekc_n(p, 0)
7585#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7586
7587#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7588static void
7589parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7590{
7591 debug_token_line(p, "add_delayed_token", line);
7592
7593 if (tok < end) {
7594 if (has_delayed_token(p)) {
7595 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7596 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7597 int end_col = (next_line ? 0 : p->delayed.end_col);
7598 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7599 dispatch_delayed_token(p, tSTRING_CONTENT);
7600 }
7601 }
7602 if (!has_delayed_token(p)) {
7603 p->delayed.token = rb_parser_string_new(p, 0, 0);
7604 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7605 p->delayed.beg_line = p->ruby_sourceline;
7606 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7607 }
7608 parser_str_cat(p->delayed.token, tok, end - tok);
7609 p->delayed.end_line = p->ruby_sourceline;
7610 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7611 p->lex.ptok = end;
7612 }
7613}
7614
7615static void
7616set_lastline(struct parser_params *p, rb_parser_string_t *str)
7617{
7618 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7619 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7620 p->lex.lastline = str;
7621}
7622
7623static int
7624nextline(struct parser_params *p, int set_encoding)
7625{
7626 rb_parser_string_t *str = p->lex.nextline;
7627 p->lex.nextline = 0;
7628 if (!str) {
7629 if (p->eofp)
7630 return -1;
7631
7632 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7633 goto end_of_input;
7634 }
7635
7636 if (!p->lex.input || !(str = lex_getline(p))) {
7637 end_of_input:
7638 p->eofp = 1;
7639 lex_goto_eol(p);
7640 return -1;
7641 }
7642#ifndef RIPPER
7643 if (p->debug_lines) {
7644 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7645 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7646 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7647 }
7648#endif
7649 p->cr_seen = FALSE;
7650 }
7651 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7652 /* after here-document without terminator */
7653 goto end_of_input;
7654 }
7655 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7656 if (p->heredoc_end > 0) {
7657 p->ruby_sourceline = p->heredoc_end;
7658 p->heredoc_end = 0;
7659 }
7660 p->ruby_sourceline++;
7661 set_lastline(p, str);
7662 token_flush(p);
7663 return 0;
7664}
7665
7666static int
7667parser_cr(struct parser_params *p, int c)
7668{
7669 if (peek(p, '\n')) {
7670 p->lex.pcur++;
7671 c = '\n';
7672 }
7673 return c;
7674}
7675
7676static inline int
7677nextc0(struct parser_params *p, int set_encoding)
7678{
7679 int c;
7680
7681 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7682 if (nextline(p, set_encoding)) return -1;
7683 }
7684 c = (unsigned char)*p->lex.pcur++;
7685 if (UNLIKELY(c == '\r')) {
7686 c = parser_cr(p, c);
7687 }
7688
7689 return c;
7690}
7691#define nextc(p) nextc0(p, TRUE)
7692
7693static void
7694pushback(struct parser_params *p, int c)
7695{
7696 if (c == -1) return;
7697 p->eofp = 0;
7698 p->lex.pcur--;
7699 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7700 p->lex.pcur--;
7701 }
7702}
7703
7704#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7705
7706#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7707#define tok(p) (p)->tokenbuf
7708#define toklen(p) (p)->tokidx
7709
7710static int
7711looking_at_eol_p(struct parser_params *p)
7712{
7713 const char *ptr = p->lex.pcur;
7714 while (!lex_eol_ptr_p(p, ptr)) {
7715 int c = (unsigned char)*ptr++;
7716 int eol = (c == '\n' || c == '#');
7717 if (eol || !ISSPACE(c)) {
7718 return eol;
7719 }
7720 }
7721 return TRUE;
7722}
7723
7724static char*
7725newtok(struct parser_params *p)
7726{
7727 p->tokidx = 0;
7728 if (!p->tokenbuf) {
7729 p->toksiz = 60;
7730 p->tokenbuf = ALLOC_N(char, 60);
7731 }
7732 if (p->toksiz > 4096) {
7733 p->toksiz = 60;
7734 REALLOC_N(p->tokenbuf, char, 60);
7735 }
7736 return p->tokenbuf;
7737}
7738
7739static char *
7740tokspace(struct parser_params *p, int n)
7741{
7742 p->tokidx += n;
7743
7744 if (p->tokidx >= p->toksiz) {
7745 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7746 REALLOC_N(p->tokenbuf, char, p->toksiz);
7747 }
7748 return &p->tokenbuf[p->tokidx-n];
7749}
7750
7751static void
7752tokadd(struct parser_params *p, int c)
7753{
7754 p->tokenbuf[p->tokidx++] = (char)c;
7755 if (p->tokidx >= p->toksiz) {
7756 p->toksiz *= 2;
7757 REALLOC_N(p->tokenbuf, char, p->toksiz);
7758 }
7759}
7760
7761static int
7762tok_hex(struct parser_params *p, size_t *numlen)
7763{
7764 int c;
7765
7766 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7767 if (!*numlen) {
7768 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7769 yyerror0("invalid hex escape");
7770 dispatch_scan_event(p, tSTRING_CONTENT);
7771 return 0;
7772 }
7773 p->lex.pcur += *numlen;
7774 return c;
7775}
7776
7777#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7778
7779static int
7780escaped_control_code(int c)
7781{
7782 int c2 = 0;
7783 switch (c) {
7784 case ' ':
7785 c2 = 's';
7786 break;
7787 case '\n':
7788 c2 = 'n';
7789 break;
7790 case '\t':
7791 c2 = 't';
7792 break;
7793 case '\v':
7794 c2 = 'v';
7795 break;
7796 case '\r':
7797 c2 = 'r';
7798 break;
7799 case '\f':
7800 c2 = 'f';
7801 break;
7802 }
7803 return c2;
7804}
7805
7806#define WARN_SPACE_CHAR(c, prefix) \
7807 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7808
7809static int
7810tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7811 int regexp_literal, const char *begin)
7812{
7813 const int wide = !begin;
7814 size_t numlen;
7815 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7816
7817 p->lex.pcur += numlen;
7818 if (p->lex.strterm == NULL ||
7819 strterm_is_heredoc(p->lex.strterm) ||
7820 (p->lex.strterm->u.literal.func != str_regexp)) {
7821 if (!begin) begin = p->lex.pcur;
7822 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7823 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7824 yyerror0("invalid Unicode escape");
7825 dispatch_scan_event(p, tSTRING_CONTENT);
7826 return wide && numlen > 0;
7827 }
7828 if (codepoint > 0x10ffff) {
7829 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7830 yyerror0("invalid Unicode codepoint (too large)");
7831 dispatch_scan_event(p, tSTRING_CONTENT);
7832 return wide;
7833 }
7834 if ((codepoint & 0xfffff800) == 0xd800) {
7835 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7836 yyerror0("invalid Unicode codepoint");
7837 dispatch_scan_event(p, tSTRING_CONTENT);
7838 return wide;
7839 }
7840 }
7841 if (regexp_literal) {
7842 tokcopy(p, (int)numlen);
7843 }
7844 else if (codepoint >= 0x80) {
7845 rb_encoding *utf8 = rb_utf8_encoding();
7846 if (*encp && utf8 != *encp) {
7847 YYLTYPE loc = RUBY_INIT_YYLLOC();
7848 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7849 parser_show_error_line(p, &loc);
7850 return wide;
7851 }
7852 *encp = utf8;
7853 tokaddmbc(p, codepoint, *encp);
7854 }
7855 else {
7856 tokadd(p, codepoint);
7857 }
7858 return TRUE;
7859}
7860
7861static int tokadd_mbchar(struct parser_params *p, int c);
7862
7863static int
7864tokskip_mbchar(struct parser_params *p)
7865{
7866 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7867 if (len > 0) {
7868 p->lex.pcur += len - 1;
7869 }
7870 return len;
7871}
7872
7873/* return value is for ?\u3042 */
7874static void
7875tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7876 int term, int symbol_literal, int regexp_literal)
7877{
7878 /*
7879 * If `term` is not -1, then we allow multiple codepoints in \u{}
7880 * upto `term` byte, otherwise we're parsing a character literal.
7881 * And then add the codepoints to the current token.
7882 */
7883 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7884
7885 const int open_brace = '{', close_brace = '}';
7886
7887 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7888
7889 if (peek(p, open_brace)) { /* handle \u{...} form */
7890 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7891 /*
7892 * Skip parsing validation code and copy bytes as-is until term or
7893 * closing brace, in order to correctly handle extended regexps where
7894 * invalid unicode escapes are allowed in comments. The regexp parser
7895 * does its own validation and will catch any issues.
7896 */
7897 tokadd(p, open_brace);
7898 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7899 int c = peekc(p);
7900 if (c == close_brace) {
7901 tokadd(p, c);
7902 ++p->lex.pcur;
7903 break;
7904 }
7905 else if (c == term) {
7906 break;
7907 }
7908 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7909 tokadd(p, c);
7910 c = *++p->lex.pcur;
7911 }
7912 tokadd_mbchar(p, c);
7913 }
7914 }
7915 else {
7916 const char *second = NULL;
7917 int c, last = nextc(p);
7918 if (lex_eol_p(p)) goto unterminated;
7919 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7920 while (c != close_brace) {
7921 if (c == term) goto unterminated;
7922 if (second == multiple_codepoints)
7923 second = p->lex.pcur;
7924 if (regexp_literal) tokadd(p, last);
7925 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7926 break;
7927 }
7928 while (ISSPACE(c = peekc(p))) {
7929 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7930 last = c;
7931 }
7932 if (term == -1 && !second)
7933 second = multiple_codepoints;
7934 }
7935
7936 if (c != close_brace) {
7937 unterminated:
7938 flush_string_content(p, rb_utf8_encoding(), 0);
7939 yyerror0("unterminated Unicode escape");
7940 dispatch_scan_event(p, tSTRING_CONTENT);
7941 return;
7942 }
7943 if (second && second != multiple_codepoints) {
7944 const char *pcur = p->lex.pcur;
7945 p->lex.pcur = second;
7946 dispatch_scan_event(p, tSTRING_CONTENT);
7947 token_flush(p);
7948 p->lex.pcur = pcur;
7949 yyerror0(multiple_codepoints);
7950 token_flush(p);
7951 }
7952
7953 if (regexp_literal) tokadd(p, close_brace);
7954 nextc(p);
7955 }
7956 }
7957 else { /* handle \uxxxx form */
7958 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7959 token_flush(p);
7960 return;
7961 }
7962 }
7963}
7964
7965#define ESCAPE_CONTROL 1
7966#define ESCAPE_META 2
7967
7968static int
7969read_escape(struct parser_params *p, int flags, const char *begin)
7970{
7971 int c;
7972 size_t numlen;
7973
7974 switch (c = nextc(p)) {
7975 case '\\': /* Backslash */
7976 return c;
7977
7978 case 'n': /* newline */
7979 return '\n';
7980
7981 case 't': /* horizontal tab */
7982 return '\t';
7983
7984 case 'r': /* carriage-return */
7985 return '\r';
7986
7987 case 'f': /* form-feed */
7988 return '\f';
7989
7990 case 'v': /* vertical tab */
7991 return '\13';
7992
7993 case 'a': /* alarm(bell) */
7994 return '\007';
7995
7996 case 'e': /* escape */
7997 return 033;
7998
7999 case '0': case '1': case '2': case '3': /* octal constant */
8000 case '4': case '5': case '6': case '7':
8001 pushback(p, c);
8002 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8003 p->lex.pcur += numlen;
8004 return c;
8005
8006 case 'x': /* hex constant */
8007 c = tok_hex(p, &numlen);
8008 if (numlen == 0) return 0;
8009 return c;
8010
8011 case 'b': /* backspace */
8012 return '\010';
8013
8014 case 's': /* space */
8015 return ' ';
8016
8017 case 'M':
8018 if (flags & ESCAPE_META) goto eof;
8019 if ((c = nextc(p)) != '-') {
8020 goto eof;
8021 }
8022 if ((c = nextc(p)) == '\\') {
8023 switch (peekc(p)) {
8024 case 'u': case 'U':
8025 nextc(p);
8026 goto eof;
8027 }
8028 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8029 }
8030 else if (c == -1) goto eof;
8031 else if (!ISASCII(c)) {
8032 tokskip_mbchar(p);
8033 goto eof;
8034 }
8035 else {
8036 int c2 = escaped_control_code(c);
8037 if (c2) {
8038 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8039 WARN_SPACE_CHAR(c2, "\\M-");
8040 }
8041 else {
8042 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8043 }
8044 }
8045 else if (ISCNTRL(c)) goto eof;
8046 return ((c & 0xff) | 0x80);
8047 }
8048
8049 case 'C':
8050 if ((c = nextc(p)) != '-') {
8051 goto eof;
8052 }
8053 case 'c':
8054 if (flags & ESCAPE_CONTROL) goto eof;
8055 if ((c = nextc(p))== '\\') {
8056 switch (peekc(p)) {
8057 case 'u': case 'U':
8058 nextc(p);
8059 goto eof;
8060 }
8061 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8062 }
8063 else if (c == '?')
8064 return 0177;
8065 else if (c == -1) goto eof;
8066 else if (!ISASCII(c)) {
8067 tokskip_mbchar(p);
8068 goto eof;
8069 }
8070 else {
8071 int c2 = escaped_control_code(c);
8072 if (c2) {
8073 if (ISCNTRL(c)) {
8074 if (flags & ESCAPE_META) {
8075 WARN_SPACE_CHAR(c2, "\\M-");
8076 }
8077 else {
8078 WARN_SPACE_CHAR(c2, "");
8079 }
8080 }
8081 else {
8082 if (flags & ESCAPE_META) {
8083 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8084 }
8085 else {
8086 WARN_SPACE_CHAR(c2, "\\C-");
8087 }
8088 }
8089 }
8090 else if (ISCNTRL(c)) goto eof;
8091 }
8092 return c & 0x9f;
8093
8094 eof:
8095 case -1:
8096 flush_string_content(p, p->enc, p->lex.pcur - begin);
8097 yyerror0("Invalid escape character syntax");
8098 dispatch_scan_event(p, tSTRING_CONTENT);
8099 return '\0';
8100
8101 default:
8102 if (!ISASCII(c)) {
8103 tokskip_mbchar(p);
8104 goto eof;
8105 }
8106 return c;
8107 }
8108}
8109
8110static void
8111tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8112{
8113 int len = rb_enc_codelen(c, enc);
8114 rb_enc_mbcput(c, tokspace(p, len), enc);
8115}
8116
8117static int
8118tokadd_escape(struct parser_params *p)
8119{
8120 int c;
8121 size_t numlen;
8122 const char *begin = p->lex.pcur;
8123
8124 switch (c = nextc(p)) {
8125 case '\n':
8126 return 0; /* just ignore */
8127
8128 case '0': case '1': case '2': case '3': /* octal constant */
8129 case '4': case '5': case '6': case '7':
8130 {
8131 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8132 if (numlen == 0) goto eof;
8133 p->lex.pcur += numlen;
8134 tokcopy(p, (int)numlen + 1);
8135 }
8136 return 0;
8137
8138 case 'x': /* hex constant */
8139 {
8140 tok_hex(p, &numlen);
8141 if (numlen == 0) return -1;
8142 tokcopy(p, (int)numlen + 2);
8143 }
8144 return 0;
8145
8146 eof:
8147 case -1:
8148 flush_string_content(p, p->enc, p->lex.pcur - begin);
8149 yyerror0("Invalid escape character syntax");
8150 token_flush(p);
8151 return -1;
8152
8153 default:
8154 tokadd(p, '\\');
8155 tokadd(p, c);
8156 }
8157 return 0;
8158}
8159
8160static int
8161char_to_option(int c)
8162{
8163 int val;
8164
8165 switch (c) {
8166 case 'i':
8167 val = RE_ONIG_OPTION_IGNORECASE;
8168 break;
8169 case 'x':
8170 val = RE_ONIG_OPTION_EXTEND;
8171 break;
8172 case 'm':
8173 val = RE_ONIG_OPTION_MULTILINE;
8174 break;
8175 default:
8176 val = 0;
8177 break;
8178 }
8179 return val;
8180}
8181
8182#define ARG_ENCODING_FIXED 16
8183#define ARG_ENCODING_NONE 32
8184#define ENC_ASCII8BIT 1
8185#define ENC_EUC_JP 2
8186#define ENC_Windows_31J 3
8187#define ENC_UTF8 4
8188
8189static int
8190char_to_option_kcode(int c, int *option, int *kcode)
8191{
8192 *option = 0;
8193
8194 switch (c) {
8195 case 'n':
8196 *kcode = ENC_ASCII8BIT;
8197 return (*option = ARG_ENCODING_NONE);
8198 case 'e':
8199 *kcode = ENC_EUC_JP;
8200 break;
8201 case 's':
8202 *kcode = ENC_Windows_31J;
8203 break;
8204 case 'u':
8205 *kcode = ENC_UTF8;
8206 break;
8207 default:
8208 *kcode = -1;
8209 return (*option = char_to_option(c));
8210 }
8211 *option = ARG_ENCODING_FIXED;
8212 return 1;
8213}
8214
8215static int
8216regx_options(struct parser_params *p)
8217{
8218 int kcode = 0;
8219 int kopt = 0;
8220 int options = 0;
8221 int c, opt, kc;
8222
8223 newtok(p);
8224 while (c = nextc(p), ISALPHA(c)) {
8225 if (c == 'o') {
8226 options |= RE_OPTION_ONCE;
8227 }
8228 else if (char_to_option_kcode(c, &opt, &kc)) {
8229 if (kc >= 0) {
8230 if (kc != ENC_ASCII8BIT) kcode = c;
8231 kopt = opt;
8232 }
8233 else {
8234 options |= opt;
8235 }
8236 }
8237 else {
8238 tokadd(p, c);
8239 }
8240 }
8241 options |= kopt;
8242 pushback(p, c);
8243 if (toklen(p)) {
8244 YYLTYPE loc = RUBY_INIT_YYLLOC();
8245 tokfix(p);
8246 compile_error(p, "unknown regexp option%s - %*s",
8247 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8248 parser_show_error_line(p, &loc);
8249 }
8250 return options | RE_OPTION_ENCODING(kcode);
8251}
8252
8253static int
8254tokadd_mbchar(struct parser_params *p, int c)
8255{
8256 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8257 if (len < 0) return -1;
8258 tokadd(p, c);
8259 p->lex.pcur += --len;
8260 if (len > 0) tokcopy(p, len);
8261 return c;
8262}
8263
8264static inline int
8265simple_re_meta(int c)
8266{
8267 switch (c) {
8268 case '$': case '*': case '+': case '.':
8269 case '?': case '^': case '|':
8270 case ')': case ']': case '}': case '>':
8271 return TRUE;
8272 default:
8273 return FALSE;
8274 }
8275}
8276
8277static int
8278parser_update_heredoc_indent(struct parser_params *p, int c)
8279{
8280 if (p->heredoc_line_indent == -1) {
8281 if (c == '\n') p->heredoc_line_indent = 0;
8282 }
8283 else {
8284 if (c == ' ') {
8285 p->heredoc_line_indent++;
8286 return TRUE;
8287 }
8288 else if (c == '\t') {
8289 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8290 p->heredoc_line_indent = w * TAB_WIDTH;
8291 return TRUE;
8292 }
8293 else if (c != '\n') {
8294 if (p->heredoc_indent > p->heredoc_line_indent) {
8295 p->heredoc_indent = p->heredoc_line_indent;
8296 }
8297 p->heredoc_line_indent = -1;
8298 }
8299 else {
8300 /* Whitespace only line has no indentation */
8301 p->heredoc_line_indent = 0;
8302 }
8303 }
8304 return FALSE;
8305}
8306
8307static void
8308parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8309{
8310 YYLTYPE loc = RUBY_INIT_YYLLOC();
8311 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8312 compile_error(p, "%s mixed within %s source", n1, n2);
8313 parser_show_error_line(p, &loc);
8314}
8315
8316static void
8317parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8318{
8319 const char *pos = p->lex.pcur;
8320 p->lex.pcur = beg;
8321 parser_mixed_error(p, enc1, enc2);
8322 p->lex.pcur = pos;
8323}
8324
8325static inline char
8326nibble_char_upper(unsigned int c)
8327{
8328 c &= 0xf;
8329 return c + (c < 10 ? '0' : 'A' - 10);
8330}
8331
8332static int
8333tokadd_string(struct parser_params *p,
8334 int func, int term, int paren, long *nest,
8335 rb_encoding **encp, rb_encoding **enc)
8336{
8337 int c;
8338 bool erred = false;
8339#ifdef RIPPER
8340 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8341 int top_of_line = FALSE;
8342#endif
8343
8344#define mixed_error(enc1, enc2) \
8345 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8346#define mixed_escape(beg, enc1, enc2) \
8347 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8348
8349 while ((c = nextc(p)) != -1) {
8350 if (p->heredoc_indent > 0) {
8351 parser_update_heredoc_indent(p, c);
8352 }
8353#ifdef RIPPER
8354 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8355 pushback(p, c);
8356 break;
8357 }
8358#endif
8359
8360 if (paren && c == paren) {
8361 ++*nest;
8362 }
8363 else if (c == term) {
8364 if (!nest || !*nest) {
8365 pushback(p, c);
8366 break;
8367 }
8368 --*nest;
8369 }
8370 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8371 unsigned char c2 = *p->lex.pcur;
8372 if (c2 == '$' || c2 == '@' || c2 == '{') {
8373 pushback(p, c);
8374 break;
8375 }
8376 }
8377 else if (c == '\\') {
8378 c = nextc(p);
8379 switch (c) {
8380 case '\n':
8381 if (func & STR_FUNC_QWORDS) break;
8382 if (func & STR_FUNC_EXPAND) {
8383 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8384 continue;
8385 if (c == term) {
8386 c = '\\';
8387 goto terminate;
8388 }
8389 }
8390 tokadd(p, '\\');
8391 break;
8392
8393 case '\\':
8394 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8395 break;
8396
8397 case 'u':
8398 if ((func & STR_FUNC_EXPAND) == 0) {
8399 tokadd(p, '\\');
8400 break;
8401 }
8402 tokadd_utf8(p, enc, term,
8403 func & STR_FUNC_SYMBOL,
8404 func & STR_FUNC_REGEXP);
8405 continue;
8406
8407 default:
8408 if (c == -1) return -1;
8409 if (!ISASCII(c)) {
8410 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8411 goto non_ascii;
8412 }
8413 if (func & STR_FUNC_REGEXP) {
8414 switch (c) {
8415 case 'c':
8416 case 'C':
8417 case 'M': {
8418 pushback(p, c);
8419 c = read_escape(p, 0, p->lex.pcur - 1);
8420
8421 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8422 *t++ = '\\';
8423 *t++ = 'x';
8424 *t++ = nibble_char_upper(c >> 4);
8425 *t++ = nibble_char_upper(c);
8426 continue;
8427 }
8428 }
8429
8430 if (c == term && !simple_re_meta(c)) {
8431 tokadd(p, c);
8432 continue;
8433 }
8434 pushback(p, c);
8435 if ((c = tokadd_escape(p)) < 0)
8436 return -1;
8437 if (*enc && *enc != *encp) {
8438 mixed_escape(p->lex.ptok+2, *enc, *encp);
8439 }
8440 continue;
8441 }
8442 else if (func & STR_FUNC_EXPAND) {
8443 pushback(p, c);
8444 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8445 c = read_escape(p, 0, p->lex.pcur - 1);
8446 }
8447 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8448 /* ignore backslashed spaces in %w */
8449 }
8450 else if (c != term && !(paren && c == paren)) {
8451 tokadd(p, '\\');
8452 pushback(p, c);
8453 continue;
8454 }
8455 }
8456 }
8457 else if (!parser_isascii(p)) {
8458 non_ascii:
8459 if (!*enc) {
8460 *enc = *encp;
8461 }
8462 else if (*enc != *encp) {
8463 mixed_error(*enc, *encp);
8464 continue;
8465 }
8466 if (tokadd_mbchar(p, c) == -1) return -1;
8467 continue;
8468 }
8469 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8470 pushback(p, c);
8471 break;
8472 }
8473 if (c & 0x80) {
8474 if (!*enc) {
8475 *enc = *encp;
8476 }
8477 else if (*enc != *encp) {
8478 mixed_error(*enc, *encp);
8479 continue;
8480 }
8481 }
8482 tokadd(p, c);
8483#ifdef RIPPER
8484 top_of_line = (c == '\n');
8485#endif
8486 }
8487 terminate:
8488 if (*enc) *encp = *enc;
8489 return c;
8490}
8491
8492#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8493
8494static void
8495flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8496{
8497 p->lex.pcur -= back;
8498 if (has_delayed_token(p)) {
8499 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8500 if (len > 0) {
8501 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8502 p->delayed.end_line = p->ruby_sourceline;
8503 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8504 }
8505 dispatch_delayed_token(p, tSTRING_CONTENT);
8506 p->lex.ptok = p->lex.pcur;
8507 }
8508 dispatch_scan_event(p, tSTRING_CONTENT);
8509 p->lex.pcur += back;
8510}
8511
8512/* this can be shared with ripper, since it's independent from struct
8513 * parser_params. */
8514#ifndef RIPPER
8515#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8516#define SPECIAL_PUNCT(idx) ( \
8517 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8518 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8519 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8520 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8521 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8522 BIT('0', idx))
8523const uint_least32_t ruby_global_name_punct_bits[] = {
8524 SPECIAL_PUNCT(0),
8525 SPECIAL_PUNCT(1),
8526 SPECIAL_PUNCT(2),
8527};
8528#undef BIT
8529#undef SPECIAL_PUNCT
8530#endif
8531
8532static enum yytokentype
8533parser_peek_variable_name(struct parser_params *p)
8534{
8535 int c;
8536 const char *ptr = p->lex.pcur;
8537
8538 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8539 c = *ptr++;
8540 switch (c) {
8541 case '$':
8542 if ((c = *ptr) == '-') {
8543 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8544 c = *ptr;
8545 }
8546 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8547 return tSTRING_DVAR;
8548 }
8549 break;
8550 case '@':
8551 if ((c = *ptr) == '@') {
8552 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8553 c = *ptr;
8554 }
8555 break;
8556 case '{':
8557 p->lex.pcur = ptr;
8558 p->command_start = TRUE;
8559 yylval.state = p->lex.state;
8560 return tSTRING_DBEG;
8561 default:
8562 return 0;
8563 }
8564 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8565 return tSTRING_DVAR;
8566 return 0;
8567}
8568
8569#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8570#define IS_END() IS_lex_state(EXPR_END_ANY)
8571#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8572#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8573#define IS_LABEL_POSSIBLE() (\
8574 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8575 IS_ARG())
8576#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8577#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8578
8579static inline enum yytokentype
8580parser_string_term(struct parser_params *p, int func)
8581{
8582 xfree(p->lex.strterm);
8583 p->lex.strterm = 0;
8584 if (func & STR_FUNC_REGEXP) {
8585 set_yylval_num(regx_options(p));
8586 dispatch_scan_event(p, tREGEXP_END);
8587 SET_LEX_STATE(EXPR_END);
8588 return tREGEXP_END;
8589 }
8590 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8591 nextc(p);
8592 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8593 return tLABEL_END;
8594 }
8595 SET_LEX_STATE(EXPR_END);
8596 return tSTRING_END;
8597}
8598
8599static enum yytokentype
8600parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8601{
8602 int func = quote->func;
8603 int term = quote->term;
8604 int paren = quote->paren;
8605 int c, space = 0;
8606 rb_encoding *enc = p->enc;
8607 rb_encoding *base_enc = 0;
8608 rb_parser_string_t *lit;
8609
8610 if (func & STR_FUNC_TERM) {
8611 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8612 SET_LEX_STATE(EXPR_END);
8613 xfree(p->lex.strterm);
8614 p->lex.strterm = 0;
8615 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8616 }
8617 c = nextc(p);
8618 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8619 while (c != '\n' && ISSPACE(c = nextc(p)));
8620 space = 1;
8621 }
8622 if (func & STR_FUNC_LIST) {
8623 quote->func &= ~STR_FUNC_LIST;
8624 space = 1;
8625 }
8626 if (c == term && !quote->nest) {
8627 if (func & STR_FUNC_QWORDS) {
8628 quote->func |= STR_FUNC_TERM;
8629 pushback(p, c); /* dispatch the term at tSTRING_END */
8630 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8631 return ' ';
8632 }
8633 return parser_string_term(p, func);
8634 }
8635 if (space) {
8636 if (!ISSPACE(c)) pushback(p, c);
8637 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8638 return ' ';
8639 }
8640 newtok(p);
8641 if ((func & STR_FUNC_EXPAND) && c == '#') {
8642 enum yytokentype t = parser_peek_variable_name(p);
8643 if (t) return t;
8644 tokadd(p, '#');
8645 c = nextc(p);
8646 }
8647 pushback(p, c);
8648 if (tokadd_string(p, func, term, paren, &quote->nest,
8649 &enc, &base_enc) == -1) {
8650 if (p->eofp) {
8651#ifndef RIPPER
8652# define unterminated_literal(mesg) yyerror0(mesg)
8653#else
8654# define unterminated_literal(mesg) compile_error(p, mesg)
8655#endif
8656 literal_flush(p, p->lex.pcur);
8657 if (func & STR_FUNC_QWORDS) {
8658 /* no content to add, bailing out here */
8659 unterminated_literal("unterminated list meets end of file");
8660 xfree(p->lex.strterm);
8661 p->lex.strterm = 0;
8662 return tSTRING_END;
8663 }
8664 if (func & STR_FUNC_REGEXP) {
8665 unterminated_literal("unterminated regexp meets end of file");
8666 }
8667 else {
8668 unterminated_literal("unterminated string meets end of file");
8669 }
8670 quote->func |= STR_FUNC_TERM;
8671 }
8672 }
8673
8674 tokfix(p);
8675 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8676 set_yylval_str(lit);
8677 flush_string_content(p, enc, 0);
8678
8679 return tSTRING_CONTENT;
8680}
8681
8682static enum yytokentype
8683heredoc_identifier(struct parser_params *p)
8684{
8685 /*
8686 * term_len is length of `<<"END"` except `END`,
8687 * in this case term_len is 4 (<, <, " and ").
8688 */
8689 long len, offset = p->lex.pcur - p->lex.pbeg;
8690 int c = nextc(p), term, func = 0, quote = 0;
8691 enum yytokentype token = tSTRING_BEG;
8692 int indent = 0;
8693
8694 if (c == '-') {
8695 c = nextc(p);
8696 func = STR_FUNC_INDENT;
8697 offset++;
8698 }
8699 else if (c == '~') {
8700 c = nextc(p);
8701 func = STR_FUNC_INDENT;
8702 offset++;
8703 indent = INT_MAX;
8704 }
8705 switch (c) {
8706 case '\'':
8707 func |= str_squote; goto quoted;
8708 case '"':
8709 func |= str_dquote; goto quoted;
8710 case '`':
8711 token = tXSTRING_BEG;
8712 func |= str_xquote; goto quoted;
8713
8714 quoted:
8715 quote++;
8716 offset++;
8717 term = c;
8718 len = 0;
8719 while ((c = nextc(p)) != term) {
8720 if (c == -1 || c == '\r' || c == '\n') {
8721 yyerror0("unterminated here document identifier");
8722 return -1;
8723 }
8724 }
8725 break;
8726
8727 default:
8728 if (!parser_is_identchar(p)) {
8729 pushback(p, c);
8730 if (func & STR_FUNC_INDENT) {
8731 pushback(p, indent > 0 ? '~' : '-');
8732 }
8733 return 0;
8734 }
8735 func |= str_dquote;
8736 do {
8737 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8738 if (n < 0) return 0;
8739 p->lex.pcur += --n;
8740 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8741 pushback(p, c);
8742 break;
8743 }
8744
8745 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8746 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8747 yyerror0("too long here document identifier");
8748 dispatch_scan_event(p, tHEREDOC_BEG);
8749 lex_goto_eol(p);
8750
8751 p->lex.strterm = new_heredoc(p);
8752 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8753 here->offset = offset;
8754 here->sourceline = p->ruby_sourceline;
8755 here->length = (unsigned)len;
8756 here->quote = quote;
8757 here->func = func;
8758 here->lastline = p->lex.lastline;
8759
8760 token_flush(p);
8761 p->heredoc_indent = indent;
8762 p->heredoc_line_indent = 0;
8763 return token;
8764}
8765
8766static void
8767heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8768{
8769 rb_parser_string_t *line;
8770 rb_strterm_t *term = p->lex.strterm;
8771
8772 p->lex.strterm = 0;
8773 line = here->lastline;
8774 p->lex.lastline = line;
8775 p->lex.pbeg = PARSER_STRING_PTR(line);
8776 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8777 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8778 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8779 p->heredoc_end = p->ruby_sourceline;
8780 p->ruby_sourceline = (int)here->sourceline;
8781 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8782 p->eofp = 0;
8783 xfree(term);
8784}
8785
8786static int
8787dedent_string_column(const char *str, long len, int width)
8788{
8789 int i, col = 0;
8790
8791 for (i = 0; i < len && col < width; i++) {
8792 if (str[i] == ' ') {
8793 col++;
8794 }
8795 else if (str[i] == '\t') {
8796 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8797 if (n > width) break;
8798 col = n;
8799 }
8800 else {
8801 break;
8802 }
8803 }
8804
8805 return i;
8806}
8807
8808static int
8809dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8810{
8811 char *str;
8812 long len;
8813 int i;
8814
8815 len = PARSER_STRING_LEN(string);
8816 str = PARSER_STRING_PTR(string);
8817
8818 i = dedent_string_column(str, len, width);
8819 if (!i) return 0;
8820
8821 rb_parser_str_modify(string);
8822 str = PARSER_STRING_PTR(string);
8823 if (PARSER_STRING_LEN(string) != len)
8824 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8825 MEMMOVE(str, str + i, char, len - i);
8826 rb_parser_str_set_len(p, string, len - i);
8827 return i;
8828}
8829
8830static NODE *
8831heredoc_dedent(struct parser_params *p, NODE *root)
8832{
8833 NODE *node, *str_node, *prev_node;
8834 int indent = p->heredoc_indent;
8835 rb_parser_string_t *prev_lit = 0;
8836
8837 if (indent <= 0) return root;
8838 if (!root) return root;
8839
8840 prev_node = node = str_node = root;
8841 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8842
8843 while (str_node) {
8844 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8845 if (nd_fl_newline(str_node)) {
8846 dedent_string(p, lit, indent);
8847 }
8848 if (!prev_lit) {
8849 prev_lit = lit;
8850 }
8851 else if (!literal_concat0(p, prev_lit, lit)) {
8852 return 0;
8853 }
8854 else {
8855 NODE *end = RNODE_LIST(node)->as.nd_end;
8856 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8857 if (!node) {
8858 if (nd_type_p(prev_node, NODE_DSTR))
8859 nd_set_type(prev_node, NODE_STR);
8860 break;
8861 }
8862 RNODE_LIST(node)->as.nd_end = end;
8863 goto next_str;
8864 }
8865
8866 str_node = 0;
8867 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8868 next_str:
8869 if (!nd_type_p(node, NODE_LIST)) break;
8870 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8871 enum node_type type = nd_type(str_node);
8872 if (type == NODE_STR || type == NODE_DSTR) break;
8873 prev_lit = 0;
8874 str_node = 0;
8875 }
8876 }
8877 }
8878 return root;
8879}
8880
8881static int
8882whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8883{
8884 const char *beg = p->lex.pbeg;
8885 const char *ptr = p->lex.pend;
8886
8887 if (ptr - beg < len) return FALSE;
8888 if (ptr > beg && ptr[-1] == '\n') {
8889 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8890 if (ptr - beg < len) return FALSE;
8891 }
8892 if (strncmp(eos, ptr -= len, len)) return FALSE;
8893 if (indent) {
8894 while (beg < ptr && ISSPACE(*beg)) beg++;
8895 }
8896 return beg == ptr;
8897}
8898
8899static int
8900word_match_p(struct parser_params *p, const char *word, long len)
8901{
8902 if (strncmp(p->lex.pcur, word, len)) return 0;
8903 if (lex_eol_n_p(p, len)) return 1;
8904 int c = (unsigned char)p->lex.pcur[len];
8905 if (ISSPACE(c)) return 1;
8906 switch (c) {
8907 case '\0': case '\004': case '\032': return 1;
8908 }
8909 return 0;
8910}
8911
8912#define NUM_SUFFIX_R (1<<0)
8913#define NUM_SUFFIX_I (1<<1)
8914#define NUM_SUFFIX_ALL 3
8915
8916static int
8917number_literal_suffix(struct parser_params *p, int mask)
8918{
8919 int c, result = 0;
8920 const char *lastp = p->lex.pcur;
8921
8922 while ((c = nextc(p)) != -1) {
8923 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8924 result |= (mask & NUM_SUFFIX_I);
8925 mask &= ~NUM_SUFFIX_I;
8926 /* r after i, rational of complex is disallowed */
8927 mask &= ~NUM_SUFFIX_R;
8928 continue;
8929 }
8930 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8931 result |= (mask & NUM_SUFFIX_R);
8932 mask &= ~NUM_SUFFIX_R;
8933 continue;
8934 }
8935 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8936 p->lex.pcur = lastp;
8937 return 0;
8938 }
8939 pushback(p, c);
8940 break;
8941 }
8942 return result;
8943}
8944
8945static enum yytokentype
8946set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8947{
8948 enum rb_numeric_type numeric_type = integer_literal;
8949
8950 if (type == tFLOAT) {
8951 numeric_type = float_literal;
8952 }
8953
8954 if (suffix & NUM_SUFFIX_R) {
8955 type = tRATIONAL;
8956 numeric_type = rational_literal;
8957 }
8958 if (suffix & NUM_SUFFIX_I) {
8959 type = tIMAGINARY;
8960 }
8961
8962 switch (type) {
8963 case tINTEGER:
8964 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8965 break;
8966 case tFLOAT:
8967 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8968 break;
8969 case tRATIONAL:
8970 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8971 break;
8972 case tIMAGINARY:
8973 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8974 (void)numeric_type; /* for ripper */
8975 break;
8976 default:
8977 rb_bug("unexpected token: %d", type);
8978 }
8979 SET_LEX_STATE(EXPR_END);
8980 return type;
8981}
8982
8983#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8984static void
8985parser_dispatch_heredoc_end(struct parser_params *p, int line)
8986{
8987 if (has_delayed_token(p))
8988 dispatch_delayed_token(p, tSTRING_CONTENT);
8989
8990#ifdef RIPPER
8991 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8992 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8993#else
8994 if (p->keep_tokens) {
8995 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
8996 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8997 parser_append_tokens(p, str, tHEREDOC_END, line);
8998 }
8999#endif
9000
9001 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9002 lex_goto_eol(p);
9003 token_flush(p);
9004}
9005
9006static enum yytokentype
9007here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9008{
9009 int c, func, indent = 0;
9010 const char *eos, *ptr, *ptr_end;
9011 long len;
9012 rb_parser_string_t *str = 0;
9013 rb_encoding *enc = p->enc;
9014 rb_encoding *base_enc = 0;
9015 int bol;
9016#ifdef RIPPER
9017 VALUE s_value;
9018#endif
9019
9020 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9021 len = here->length;
9022 indent = (func = here->func) & STR_FUNC_INDENT;
9023
9024 if ((c = nextc(p)) == -1) {
9025 error:
9026#ifdef RIPPER
9027 if (!has_delayed_token(p)) {
9028 dispatch_scan_event(p, tSTRING_CONTENT);
9029 }
9030 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9031 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9032 if (!(func & STR_FUNC_REGEXP)) {
9033 int cr = ENC_CODERANGE_UNKNOWN;
9034 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9035 if (cr != ENC_CODERANGE_7BIT &&
9036 rb_is_usascii_enc(p->enc) &&
9037 enc != rb_utf8_encoding()) {
9038 enc = rb_ascii8bit_encoding();
9039 }
9040 }
9041 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9042 }
9043 dispatch_delayed_token(p, tSTRING_CONTENT);
9044 }
9045 else {
9046 dispatch_delayed_token(p, tSTRING_CONTENT);
9047 dispatch_scan_event(p, tSTRING_CONTENT);
9048 }
9049 lex_goto_eol(p);
9050#endif
9051 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9052 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9053 (int)len, eos);
9054 token_flush(p);
9055 SET_LEX_STATE(EXPR_END);
9056 return tSTRING_END;
9057 }
9058 bol = was_bol(p);
9059 if (!bol) {
9060 /* not beginning of line, cannot be the terminator */
9061 }
9062 else if (p->heredoc_line_indent == -1) {
9063 /* `heredoc_line_indent == -1` means
9064 * - "after an interpolation in the same line", or
9065 * - "in a continuing line"
9066 */
9067 p->heredoc_line_indent = 0;
9068 }
9069 else if (whole_match_p(p, eos, len, indent)) {
9070 dispatch_heredoc_end(p);
9071 restore:
9072 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9073 token_flush(p);
9074 SET_LEX_STATE(EXPR_END);
9075 return tSTRING_END;
9076 }
9077
9078 if (!(func & STR_FUNC_EXPAND)) {
9079 do {
9080 ptr = PARSER_STRING_PTR(p->lex.lastline);
9081 ptr_end = p->lex.pend;
9082 if (ptr_end > ptr) {
9083 switch (ptr_end[-1]) {
9084 case '\n':
9085 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9086 ptr_end++;
9087 break;
9088 }
9089 case '\r':
9090 --ptr_end;
9091 }
9092 }
9093
9094 if (p->heredoc_indent > 0) {
9095 long i = 0;
9096 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9097 i++;
9098 p->heredoc_line_indent = 0;
9099 }
9100
9101 if (str)
9102 parser_str_cat(str, ptr, ptr_end - ptr);
9103 else
9104 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9105 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9106 lex_goto_eol(p);
9107 if (p->heredoc_indent > 0) {
9108 goto flush_str;
9109 }
9110 if (nextc(p) == -1) {
9111 if (str) {
9112 rb_parser_string_free(p, str);
9113 str = 0;
9114 }
9115 goto error;
9116 }
9117 } while (!whole_match_p(p, eos, len, indent));
9118 }
9119 else {
9120 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9121 newtok(p);
9122 if (c == '#') {
9123 enum yytokentype t = parser_peek_variable_name(p);
9124 if (p->heredoc_line_indent != -1) {
9125 if (p->heredoc_indent > p->heredoc_line_indent) {
9126 p->heredoc_indent = p->heredoc_line_indent;
9127 }
9128 p->heredoc_line_indent = -1;
9129 }
9130 if (t) return t;
9131 tokadd(p, '#');
9132 c = nextc(p);
9133 }
9134 do {
9135 pushback(p, c);
9136 enc = p->enc;
9137 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9138 if (p->eofp) goto error;
9139 goto restore;
9140 }
9141 if (c != '\n') {
9142 if (c == '\\') p->heredoc_line_indent = -1;
9143 flush:
9144 str = STR_NEW3(tok(p), toklen(p), enc, func);
9145 flush_str:
9146 set_yylval_str(str);
9147#ifndef RIPPER
9148 if (bol) nd_set_fl_newline(yylval.node);
9149#endif
9150 flush_string_content(p, enc, 0);
9151 return tSTRING_CONTENT;
9152 }
9153 tokadd(p, nextc(p));
9154 if (p->heredoc_indent > 0) {
9155 lex_goto_eol(p);
9156 goto flush;
9157 }
9158 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9159 if ((c = nextc(p)) == -1) goto error;
9160 } while (!whole_match_p(p, eos, len, indent));
9161 str = STR_NEW3(tok(p), toklen(p), enc, func);
9162 }
9163 dispatch_heredoc_end(p);
9164 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9165 token_flush(p);
9166 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9167#ifdef RIPPER
9168 /* Preserve s_value for set_yylval_str */
9169 s_value = p->s_value;
9170#endif
9171 set_yylval_str(str);
9172#ifdef RIPPER
9173 set_parser_s_value(s_value);
9174#endif
9175
9176#ifndef RIPPER
9177 if (bol) nd_set_fl_newline(yylval.node);
9178#endif
9179 return tSTRING_CONTENT;
9180}
9181
9182#include "lex.c"
9183
9184static int
9185arg_ambiguous(struct parser_params *p, char c)
9186{
9187#ifndef RIPPER
9188 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9189#else
9190 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9191#endif
9192 return TRUE;
9193}
9194
9195/* returns true value if formal argument error;
9196 * Qtrue, or error message if ripper */
9197static VALUE
9198formal_argument_error(struct parser_params *p, ID id)
9199{
9200 switch (id_type(id)) {
9201 case ID_LOCAL:
9202 break;
9203#ifndef RIPPER
9204# define ERR(mesg) (yyerror0(mesg), Qtrue)
9205#else
9206# define ERR(mesg) WARN_S(mesg)
9207#endif
9208 case ID_CONST:
9209 return ERR("formal argument cannot be a constant");
9210 case ID_INSTANCE:
9211 return ERR("formal argument cannot be an instance variable");
9212 case ID_GLOBAL:
9213 return ERR("formal argument cannot be a global variable");
9214 case ID_CLASS:
9215 return ERR("formal argument cannot be a class variable");
9216 default:
9217 return ERR("formal argument must be local variable");
9218#undef ERR
9219 }
9220 shadowing_lvar(p, id);
9221
9222 return Qfalse;
9223}
9224
9225static int
9226lvar_defined(struct parser_params *p, ID id)
9227{
9228 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9229}
9230
9231/* emacsen -*- hack */
9232static long
9233parser_encode_length(struct parser_params *p, const char *name, long len)
9234{
9235 long nlen;
9236
9237 if (len > 5 && name[nlen = len - 5] == '-') {
9238 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9239 return nlen;
9240 }
9241 if (len > 4 && name[nlen = len - 4] == '-') {
9242 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9243 return nlen;
9244 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9245 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9246 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9247 return nlen;
9248 }
9249 return len;
9250}
9251
9252static void
9253parser_set_encode(struct parser_params *p, const char *name)
9254{
9255 rb_encoding *enc;
9256 VALUE excargs[3];
9257 int idx = 0;
9258
9259 const char *wrong = 0;
9260 switch (*name) {
9261 case 'e': case 'E': wrong = "external"; break;
9262 case 'i': case 'I': wrong = "internal"; break;
9263 case 'f': case 'F': wrong = "filesystem"; break;
9264 case 'l': case 'L': wrong = "locale"; break;
9265 }
9266 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9267 idx = rb_enc_find_index(name);
9268 if (idx < 0) {
9269 unknown:
9270 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9271 error:
9272 excargs[0] = rb_eArgError;
9273 excargs[2] = rb_make_backtrace();
9274 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9275 VALUE exc = rb_make_exception(3, excargs);
9276 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9277
9278 rb_ast_free(p->ast);
9279 p->ast = NULL;
9280
9281 rb_exc_raise(exc);
9282 }
9283 enc = rb_enc_from_index(idx);
9284 if (!rb_enc_asciicompat(enc)) {
9285 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9286 goto error;
9287 }
9288 p->enc = enc;
9289#ifndef RIPPER
9290 if (p->debug_lines) {
9291 long i;
9292 for (i = 0; i < p->debug_lines->len; i++) {
9293 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9294 }
9295 }
9296#endif
9297}
9298
9299static bool
9300comment_at_top(struct parser_params *p)
9301{
9302 if (p->token_seen) return false;
9303 return (p->line_count == (p->has_shebang ? 2 : 1));
9304}
9305
9306typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9307typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9308
9309static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9310
9311static void
9312magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9313{
9314 if (!comment_at_top(p)) {
9315 return;
9316 }
9317 parser_set_encode(p, val);
9318}
9319
9320static int
9321parser_get_bool(struct parser_params *p, const char *name, const char *val)
9322{
9323 switch (*val) {
9324 case 't': case 'T':
9325 if (STRCASECMP(val, "true") == 0) {
9326 return TRUE;
9327 }
9328 break;
9329 case 'f': case 'F':
9330 if (STRCASECMP(val, "false") == 0) {
9331 return FALSE;
9332 }
9333 break;
9334 }
9335 return parser_invalid_pragma_value(p, name, val);
9336}
9337
9338static int
9339parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9340{
9341 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9342 return -1;
9343}
9344
9345static void
9346parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9347{
9348 int b = parser_get_bool(p, name, val);
9349 if (b >= 0) p->token_info_enabled = b;
9350}
9351
9352static void
9353parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9354{
9355 int b;
9356
9357 if (p->token_seen) {
9358 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9359 return;
9360 }
9361
9362 b = parser_get_bool(p, name, val);
9363 if (b < 0) return;
9364
9365 p->frozen_string_literal = b;
9366}
9367
9368static void
9369parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9370{
9371 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9372 if (*s == ' ' || *s == '\t') continue;
9373 if (*s == '#') break;
9374 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9375 return;
9376 }
9377
9378 switch (*val) {
9379 case 'n': case 'N':
9380 if (STRCASECMP(val, "none") == 0) {
9381 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9382 return;
9383 }
9384 break;
9385 case 'l': case 'L':
9386 if (STRCASECMP(val, "literal") == 0) {
9387 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9388 return;
9389 }
9390 break;
9391 case 'e': case 'E':
9392 if (STRCASECMP(val, "experimental_copy") == 0) {
9393 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9394 return;
9395 }
9396 if (STRCASECMP(val, "experimental_everything") == 0) {
9397 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9398 return;
9399 }
9400 break;
9401 }
9402 parser_invalid_pragma_value(p, name, val);
9403}
9404
9405# if WARN_PAST_SCOPE
9406static void
9407parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9408{
9409 int b = parser_get_bool(p, name, val);
9410 if (b >= 0) p->past_scope_enabled = b;
9411}
9412# endif
9413
9414struct magic_comment {
9415 const char *name;
9416 rb_magic_comment_setter_t func;
9417 rb_magic_comment_length_t length;
9418};
9419
9420static const struct magic_comment magic_comments[] = {
9421 {"coding", magic_comment_encoding, parser_encode_length},
9422 {"encoding", magic_comment_encoding, parser_encode_length},
9423 {"frozen_string_literal", parser_set_frozen_string_literal},
9424 {"shareable_constant_value", parser_set_shareable_constant_value},
9425 {"warn_indent", parser_set_token_info},
9426# if WARN_PAST_SCOPE
9427 {"warn_past_scope", parser_set_past_scope},
9428# endif
9429};
9430
9431static const char *
9432magic_comment_marker(const char *str, long len)
9433{
9434 long i = 2;
9435
9436 while (i < len) {
9437 switch (str[i]) {
9438 case '-':
9439 if (str[i-1] == '*' && str[i-2] == '-') {
9440 return str + i + 1;
9441 }
9442 i += 2;
9443 break;
9444 case '*':
9445 if (i + 1 >= len) return 0;
9446 if (str[i+1] != '-') {
9447 i += 4;
9448 }
9449 else if (str[i-1] != '-') {
9450 i += 2;
9451 }
9452 else {
9453 return str + i + 2;
9454 }
9455 break;
9456 default:
9457 i += 3;
9458 break;
9459 }
9460 }
9461 return 0;
9462}
9463
9464static int
9465parser_magic_comment(struct parser_params *p, const char *str, long len)
9466{
9467 int indicator = 0;
9468 VALUE name = 0, val = 0;
9469 const char *beg, *end, *vbeg, *vend;
9470#define str_copy(_s, _p, _n) ((_s) \
9471 ? (void)(rb_str_resize((_s), (_n)), \
9472 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9473 : (void)((_s) = STR_NEW((_p), (_n))))
9474
9475 if (len <= 7) return FALSE;
9476 if (!!(beg = magic_comment_marker(str, len))) {
9477 if (!(end = magic_comment_marker(beg, str + len - beg)))
9478 return FALSE;
9479 indicator = TRUE;
9480 str = beg;
9481 len = end - beg - 3;
9482 }
9483
9484 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9485 while (len > 0) {
9486 const struct magic_comment *mc = magic_comments;
9487 char *s;
9488 int i;
9489 long n = 0;
9490
9491 for (; len > 0 && *str; str++, --len) {
9492 switch (*str) {
9493 case '\'': case '"': case ':': case ';':
9494 continue;
9495 }
9496 if (!ISSPACE(*str)) break;
9497 }
9498 for (beg = str; len > 0; str++, --len) {
9499 switch (*str) {
9500 case '\'': case '"': case ':': case ';':
9501 break;
9502 default:
9503 if (ISSPACE(*str)) break;
9504 continue;
9505 }
9506 break;
9507 }
9508 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9509 if (!len) break;
9510 if (*str != ':') {
9511 if (!indicator) return FALSE;
9512 continue;
9513 }
9514
9515 do str++; while (--len > 0 && ISSPACE(*str));
9516 if (!len) break;
9517 const char *tok_beg = str;
9518 if (*str == '"') {
9519 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9520 if (*str == '\\') {
9521 --len;
9522 ++str;
9523 }
9524 }
9525 vend = str;
9526 if (len) {
9527 --len;
9528 ++str;
9529 }
9530 }
9531 else {
9532 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9533 vend = str;
9534 }
9535 const char *tok_end = str;
9536 if (indicator) {
9537 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9538 }
9539 else {
9540 while (len > 0 && (ISSPACE(*str))) --len, str++;
9541 if (len) return FALSE;
9542 }
9543
9544 n = end - beg;
9545 str_copy(name, beg, n);
9546 s = RSTRING_PTR(name);
9547 for (i = 0; i < n; ++i) {
9548 if (s[i] == '-') s[i] = '_';
9549 }
9550 do {
9551 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9552 n = vend - vbeg;
9553 if (mc->length) {
9554 n = (*mc->length)(p, vbeg, n);
9555 }
9556 str_copy(val, vbeg, n);
9557 p->lex.ptok = tok_beg;
9558 p->lex.pcur = tok_end;
9559 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9560 break;
9561 }
9562 } while (++mc < magic_comments + numberof(magic_comments));
9563#ifdef RIPPER
9564 str_copy(val, vbeg, vend - vbeg);
9565 dispatch2(magic_comment, name, val);
9566#endif
9567 }
9568
9569 return TRUE;
9570}
9571
9572static void
9573set_file_encoding(struct parser_params *p, const char *str, const char *send)
9574{
9575 int sep = 0;
9576 const char *beg = str;
9577 VALUE s;
9578
9579 for (;;) {
9580 if (send - str <= 6) return;
9581 switch (str[6]) {
9582 case 'C': case 'c': str += 6; continue;
9583 case 'O': case 'o': str += 5; continue;
9584 case 'D': case 'd': str += 4; continue;
9585 case 'I': case 'i': str += 3; continue;
9586 case 'N': case 'n': str += 2; continue;
9587 case 'G': case 'g': str += 1; continue;
9588 case '=': case ':':
9589 sep = 1;
9590 str += 6;
9591 break;
9592 default:
9593 str += 6;
9594 if (ISSPACE(*str)) break;
9595 continue;
9596 }
9597 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9598 sep = 0;
9599 }
9600 for (;;) {
9601 do {
9602 if (++str >= send) return;
9603 } while (ISSPACE(*str));
9604 if (sep) break;
9605 if (*str != '=' && *str != ':') return;
9606 sep = 1;
9607 str++;
9608 }
9609 beg = str;
9610 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9611 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9612 p->lex.ptok = beg;
9613 p->lex.pcur = str;
9614 parser_set_encode(p, RSTRING_PTR(s));
9615 rb_str_resize(s, 0);
9616}
9617
9618static void
9619parser_prepare(struct parser_params *p)
9620{
9621 int c = nextc0(p, FALSE);
9622 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9623 switch (c) {
9624 case '#':
9625 if (peek(p, '!')) p->has_shebang = 1;
9626 break;
9627 case 0xef: /* UTF-8 BOM marker */
9628 if (!lex_eol_n_p(p, 2) &&
9629 (unsigned char)p->lex.pcur[0] == 0xbb &&
9630 (unsigned char)p->lex.pcur[1] == 0xbf) {
9631 p->enc = rb_utf8_encoding();
9632 p->lex.pcur += 2;
9633#ifndef RIPPER
9634 if (p->debug_lines) {
9635 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9636 }
9637#endif
9638 p->lex.pbeg = p->lex.pcur;
9639 token_flush(p);
9640 return;
9641 }
9642 break;
9643 case -1: /* end of script. */
9644 return;
9645 }
9646 pushback(p, c);
9647 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9648}
9649
9650#ifndef RIPPER
9651#define ambiguous_operator(tok, op, syn) ( \
9652 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9653 rb_warning0("even though it seems like "syn""))
9654#else
9655#define ambiguous_operator(tok, op, syn) \
9656 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9657#endif
9658#define warn_balanced(tok, op, syn) ((void) \
9659 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9660 space_seen && !ISSPACE(c) && \
9661 (ambiguous_operator(tok, op, syn), 0)), \
9662 (enum yytokentype)(tok))
9663
9664static enum yytokentype
9665no_digits(struct parser_params *p)
9666{
9667 yyerror0("numeric literal without digits");
9668 if (peek(p, '_')) nextc(p);
9669 /* dummy 0, for tUMINUS_NUM at numeric */
9670 return set_number_literal(p, tINTEGER, 0, 10, 0);
9671}
9672
9673static enum yytokentype
9674parse_numeric(struct parser_params *p, int c)
9675{
9676 int is_float, seen_point, seen_e, nondigit;
9677 int suffix;
9678
9679 is_float = seen_point = seen_e = nondigit = 0;
9680 SET_LEX_STATE(EXPR_END);
9681 newtok(p);
9682 if (c == '-' || c == '+') {
9683 tokadd(p, c);
9684 c = nextc(p);
9685 }
9686 if (c == '0') {
9687 int start = toklen(p);
9688 c = nextc(p);
9689 if (c == 'x' || c == 'X') {
9690 /* hexadecimal */
9691 c = nextc(p);
9692 if (c != -1 && ISXDIGIT(c)) {
9693 do {
9694 if (c == '_') {
9695 if (nondigit) break;
9696 nondigit = c;
9697 continue;
9698 }
9699 if (!ISXDIGIT(c)) break;
9700 nondigit = 0;
9701 tokadd(p, c);
9702 } while ((c = nextc(p)) != -1);
9703 }
9704 pushback(p, c);
9705 tokfix(p);
9706 if (toklen(p) == start) {
9707 return no_digits(p);
9708 }
9709 else if (nondigit) goto trailing_uc;
9710 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9711 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9712 }
9713 if (c == 'b' || c == 'B') {
9714 /* binary */
9715 c = nextc(p);
9716 if (c == '0' || c == '1') {
9717 do {
9718 if (c == '_') {
9719 if (nondigit) break;
9720 nondigit = c;
9721 continue;
9722 }
9723 if (c != '0' && c != '1') break;
9724 nondigit = 0;
9725 tokadd(p, c);
9726 } while ((c = nextc(p)) != -1);
9727 }
9728 pushback(p, c);
9729 tokfix(p);
9730 if (toklen(p) == start) {
9731 return no_digits(p);
9732 }
9733 else if (nondigit) goto trailing_uc;
9734 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9735 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9736 }
9737 if (c == 'd' || c == 'D') {
9738 /* decimal */
9739 c = nextc(p);
9740 if (c != -1 && ISDIGIT(c)) {
9741 do {
9742 if (c == '_') {
9743 if (nondigit) break;
9744 nondigit = c;
9745 continue;
9746 }
9747 if (!ISDIGIT(c)) break;
9748 nondigit = 0;
9749 tokadd(p, c);
9750 } while ((c = nextc(p)) != -1);
9751 }
9752 pushback(p, c);
9753 tokfix(p);
9754 if (toklen(p) == start) {
9755 return no_digits(p);
9756 }
9757 else if (nondigit) goto trailing_uc;
9758 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9759 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9760 }
9761 if (c == '_') {
9762 /* 0_0 */
9763 goto octal_number;
9764 }
9765 if (c == 'o' || c == 'O') {
9766 /* prefixed octal */
9767 c = nextc(p);
9768 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9769 tokfix(p);
9770 return no_digits(p);
9771 }
9772 }
9773 if (c >= '0' && c <= '7') {
9774 /* octal */
9775 octal_number:
9776 do {
9777 if (c == '_') {
9778 if (nondigit) break;
9779 nondigit = c;
9780 continue;
9781 }
9782 if (c < '0' || c > '9') break;
9783 if (c > '7') goto invalid_octal;
9784 nondigit = 0;
9785 tokadd(p, c);
9786 } while ((c = nextc(p)) != -1);
9787 if (toklen(p) > start) {
9788 pushback(p, c);
9789 tokfix(p);
9790 if (nondigit) goto trailing_uc;
9791 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9792 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9793 }
9794 if (nondigit) {
9795 pushback(p, c);
9796 goto trailing_uc;
9797 }
9798 }
9799 if (c > '7' && c <= '9') {
9800 invalid_octal:
9801 yyerror0("Invalid octal digit");
9802 }
9803 else if (c == '.' || c == 'e' || c == 'E') {
9804 tokadd(p, '0');
9805 }
9806 else {
9807 pushback(p, c);
9808 tokfix(p);
9809 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9810 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9811 }
9812 }
9813
9814 for (;;) {
9815 switch (c) {
9816 case '0': case '1': case '2': case '3': case '4':
9817 case '5': case '6': case '7': case '8': case '9':
9818 nondigit = 0;
9819 tokadd(p, c);
9820 break;
9821
9822 case '.':
9823 if (nondigit) goto trailing_uc;
9824 if (seen_point || seen_e) {
9825 goto decode_num;
9826 }
9827 else {
9828 int c0 = nextc(p);
9829 if (c0 == -1 || !ISDIGIT(c0)) {
9830 pushback(p, c0);
9831 goto decode_num;
9832 }
9833 c = c0;
9834 }
9835 seen_point = toklen(p);
9836 tokadd(p, '.');
9837 tokadd(p, c);
9838 is_float++;
9839 nondigit = 0;
9840 break;
9841
9842 case 'e':
9843 case 'E':
9844 if (nondigit) {
9845 pushback(p, c);
9846 c = nondigit;
9847 goto decode_num;
9848 }
9849 if (seen_e) {
9850 goto decode_num;
9851 }
9852 nondigit = c;
9853 c = nextc(p);
9854 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9855 pushback(p, c);
9856 c = nondigit;
9857 nondigit = 0;
9858 goto decode_num;
9859 }
9860 tokadd(p, nondigit);
9861 seen_e++;
9862 is_float++;
9863 tokadd(p, c);
9864 nondigit = (c == '-' || c == '+') ? c : 0;
9865 break;
9866
9867 case '_': /* `_' in number just ignored */
9868 if (nondigit) goto decode_num;
9869 nondigit = c;
9870 break;
9871
9872 default:
9873 goto decode_num;
9874 }
9875 c = nextc(p);
9876 }
9877
9878 decode_num:
9879 pushback(p, c);
9880 if (nondigit) {
9881 trailing_uc:
9882 literal_flush(p, p->lex.pcur - 1);
9883 YYLTYPE loc = RUBY_INIT_YYLLOC();
9884 compile_error(p, "trailing '%c' in number", nondigit);
9885 parser_show_error_line(p, &loc);
9886 }
9887 tokfix(p);
9888 if (is_float) {
9889 enum yytokentype type = tFLOAT;
9890
9891 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9892 if (suffix & NUM_SUFFIX_R) {
9893 type = tRATIONAL;
9894 }
9895 else {
9896 errno = 0;
9897 strtod(tok(p), 0);
9898 if (errno == ERANGE) {
9899 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9900 errno = 0;
9901 }
9902 }
9903 return set_number_literal(p, type, suffix, 0, seen_point);
9904 }
9905 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9906 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9907}
9908
9909static enum yytokentype
9910parse_qmark(struct parser_params *p, int space_seen)
9911{
9912 rb_encoding *enc;
9913 register int c;
9914 rb_parser_string_t *lit;
9915 const char *start = p->lex.pcur;
9916
9917 if (IS_END()) {
9918 SET_LEX_STATE(EXPR_VALUE);
9919 return '?';
9920 }
9921 c = nextc(p);
9922 if (c == -1) {
9923 compile_error(p, "incomplete character syntax");
9924 return 0;
9925 }
9926 if (rb_enc_isspace(c, p->enc)) {
9927 if (!IS_ARG()) {
9928 int c2 = escaped_control_code(c);
9929 if (c2) {
9930 WARN_SPACE_CHAR(c2, "?");
9931 }
9932 }
9933 ternary:
9934 pushback(p, c);
9935 SET_LEX_STATE(EXPR_VALUE);
9936 return '?';
9937 }
9938 newtok(p);
9939 enc = p->enc;
9940 int w = parser_precise_mbclen(p, start);
9941 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9942 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9943 if (space_seen) {
9944 const char *ptr = start;
9945 do {
9946 int n = parser_precise_mbclen(p, ptr);
9947 if (n < 0) return -1;
9948 ptr += n;
9949 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9950 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9951 " a conditional operator, put a space after '?'",
9952 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9953 }
9954 goto ternary;
9955 }
9956 else if (c == '\\') {
9957 if (peek(p, 'u')) {
9958 nextc(p);
9959 enc = rb_utf8_encoding();
9960 tokadd_utf8(p, &enc, -1, 0, 0);
9961 }
9962 else if (!ISASCII(c = peekc(p)) && c != -1) {
9963 nextc(p);
9964 if (tokadd_mbchar(p, c) == -1) return 0;
9965 }
9966 else {
9967 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9968 tokadd(p, c);
9969 }
9970 }
9971 else {
9972 if (tokadd_mbchar(p, c) == -1) return 0;
9973 }
9974 tokfix(p);
9975 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9976 set_yylval_str(lit);
9977 SET_LEX_STATE(EXPR_END);
9978 return tCHAR;
9979}
9980
9981static enum yytokentype
9982parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9983{
9984 register int c;
9985 const char *ptok = p->lex.pcur;
9986
9987 if (IS_BEG()) {
9988 int term;
9989 int paren;
9990
9991 c = nextc(p);
9992 quotation:
9993 if (c == -1) goto unterminated;
9994 if (!ISALNUM(c)) {
9995 term = c;
9996 if (!ISASCII(c)) goto unknown;
9997 c = 'Q';
9998 }
9999 else {
10000 term = nextc(p);
10001 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10002 unknown:
10003 pushback(p, term);
10004 c = parser_precise_mbclen(p, p->lex.pcur);
10005 if (c < 0) return 0;
10006 p->lex.pcur += c;
10007 yyerror0("unknown type of %string");
10008 return 0;
10009 }
10010 }
10011 if (term == -1) {
10012 unterminated:
10013 compile_error(p, "unterminated quoted string meets end of file");
10014 return 0;
10015 }
10016 paren = term;
10017 if (term == '(') term = ')';
10018 else if (term == '[') term = ']';
10019 else if (term == '{') term = '}';
10020 else if (term == '<') term = '>';
10021 else paren = 0;
10022
10023 p->lex.ptok = ptok-1;
10024 switch (c) {
10025 case 'Q':
10026 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10027 return tSTRING_BEG;
10028
10029 case 'q':
10030 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10031 return tSTRING_BEG;
10032
10033 case 'W':
10034 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10035 return tWORDS_BEG;
10036
10037 case 'w':
10038 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10039 return tQWORDS_BEG;
10040
10041 case 'I':
10042 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10043 return tSYMBOLS_BEG;
10044
10045 case 'i':
10046 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10047 return tQSYMBOLS_BEG;
10048
10049 case 'x':
10050 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10051 return tXSTRING_BEG;
10052
10053 case 'r':
10054 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10055 return tREGEXP_BEG;
10056
10057 case 's':
10058 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10059 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10060 return tSYMBEG;
10061
10062 default:
10063 yyerror0("unknown type of %string");
10064 return 0;
10065 }
10066 }
10067 if ((c = nextc(p)) == '=') {
10068 set_yylval_id('%');
10069 SET_LEX_STATE(EXPR_BEG);
10070 return tOP_ASGN;
10071 }
10072 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10073 goto quotation;
10074 }
10075 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10076 pushback(p, c);
10077 return warn_balanced('%', "%%", "string literal");
10078}
10079
10080static int
10081tokadd_ident(struct parser_params *p, int c)
10082{
10083 do {
10084 if (tokadd_mbchar(p, c) == -1) return -1;
10085 c = nextc(p);
10086 } while (parser_is_identchar(p));
10087 pushback(p, c);
10088 return 0;
10089}
10090
10091static ID
10092tokenize_ident(struct parser_params *p)
10093{
10094 ID ident = TOK_INTERN();
10095
10096 set_yylval_name(ident);
10097
10098 return ident;
10099}
10100
10101static int
10102parse_numvar(struct parser_params *p)
10103{
10104 size_t len;
10105 int overflow;
10106 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10107 const unsigned long nth_ref_max =
10108 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10109 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10110 * turned into a Fixnum, in compile.c */
10111
10112 if (overflow || n > nth_ref_max) {
10113 /* compile_error()? */
10114 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10115 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10116 }
10117 else {
10118 return (int)n;
10119 }
10120}
10121
10122static enum yytokentype
10123parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10124{
10125 const char *ptr = p->lex.pcur;
10126 register int c;
10127
10128 SET_LEX_STATE(EXPR_END);
10129 p->lex.ptok = ptr - 1; /* from '$' */
10130 newtok(p);
10131 c = nextc(p);
10132 switch (c) {
10133 case '_': /* $_: last read line string */
10134 c = nextc(p);
10135 if (parser_is_identchar(p)) {
10136 tokadd(p, '$');
10137 tokadd(p, '_');
10138 break;
10139 }
10140 pushback(p, c);
10141 c = '_';
10142 /* fall through */
10143 case '~': /* $~: match-data */
10144 case '*': /* $*: argv */
10145 case '$': /* $$: pid */
10146 case '?': /* $?: last status */
10147 case '!': /* $!: error string */
10148 case '@': /* $@: error position */
10149 case '/': /* $/: input record separator */
10150 case '\\': /* $\: output record separator */
10151 case ';': /* $;: field separator */
10152 case ',': /* $,: output field separator */
10153 case '.': /* $.: last read line number */
10154 case '=': /* $=: ignorecase */
10155 case ':': /* $:: load path */
10156 case '<': /* $<: default input handle */
10157 case '>': /* $>: default output handle */
10158 case '\"': /* $": already loaded files */
10159 tokadd(p, '$');
10160 tokadd(p, c);
10161 goto gvar;
10162
10163 case '-':
10164 tokadd(p, '$');
10165 tokadd(p, c);
10166 c = nextc(p);
10167 if (parser_is_identchar(p)) {
10168 if (tokadd_mbchar(p, c) == -1) return 0;
10169 }
10170 else {
10171 pushback(p, c);
10172 pushback(p, '-');
10173 return '$';
10174 }
10175 gvar:
10176 tokenize_ident(p);
10177 return tGVAR;
10178
10179 case '&': /* $&: last match */
10180 case '`': /* $`: string before last match */
10181 case '\'': /* $': string after last match */
10182 case '+': /* $+: string matches last paren. */
10183 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10184 tokadd(p, '$');
10185 tokadd(p, c);
10186 goto gvar;
10187 }
10188 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10189 return tBACK_REF;
10190
10191 case '1': case '2': case '3':
10192 case '4': case '5': case '6':
10193 case '7': case '8': case '9':
10194 tokadd(p, '$');
10195 do {
10196 tokadd(p, c);
10197 c = nextc(p);
10198 } while (c != -1 && ISDIGIT(c));
10199 pushback(p, c);
10200 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10201 tokfix(p);
10202 c = parse_numvar(p);
10203 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10204 return tNTH_REF;
10205
10206 default:
10207 if (!parser_is_identchar(p)) {
10208 YYLTYPE loc = RUBY_INIT_YYLLOC();
10209 if (c == -1 || ISSPACE(c)) {
10210 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10211 }
10212 else {
10213 pushback(p, c);
10214 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10215 }
10216 parser_show_error_line(p, &loc);
10217 set_yylval_noname();
10218 return tGVAR;
10219 }
10220 /* fall through */
10221 case '0':
10222 tokadd(p, '$');
10223 }
10224
10225 if (tokadd_ident(p, c)) return 0;
10226 SET_LEX_STATE(EXPR_END);
10227 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10228 tokenize_ident(p);
10229 }
10230 else {
10231 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10232 set_yylval_noname();
10233 }
10234 return tGVAR;
10235}
10236
10237static bool
10238parser_numbered_param(struct parser_params *p, int n)
10239{
10240 if (n < 0) return false;
10241
10242 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10243 return false;
10244 }
10245 if (p->max_numparam == ORDINAL_PARAM) {
10246 compile_error(p, "ordinary parameter is defined");
10247 return false;
10248 }
10249 struct vtable *args = p->lvtbl->args;
10250 if (p->max_numparam < n) {
10251 p->max_numparam = n;
10252 }
10253 while (n > args->pos) {
10254 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10255 }
10256 return true;
10257}
10258
10259static enum yytokentype
10260parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10261{
10262 const char *ptr = p->lex.pcur;
10263 enum yytokentype result = tIVAR;
10264 register int c = nextc(p);
10265 YYLTYPE loc;
10266
10267 p->lex.ptok = ptr - 1; /* from '@' */
10268 newtok(p);
10269 tokadd(p, '@');
10270 if (c == '@') {
10271 result = tCVAR;
10272 tokadd(p, '@');
10273 c = nextc(p);
10274 }
10275 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10276 if (c == -1 || !parser_is_identchar(p)) {
10277 pushback(p, c);
10278 RUBY_SET_YYLLOC(loc);
10279 if (result == tIVAR) {
10280 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10281 }
10282 else {
10283 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10284 }
10285 parser_show_error_line(p, &loc);
10286 set_yylval_noname();
10287 SET_LEX_STATE(EXPR_END);
10288 return result;
10289 }
10290 else if (ISDIGIT(c)) {
10291 pushback(p, c);
10292 RUBY_SET_YYLLOC(loc);
10293 if (result == tIVAR) {
10294 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10295 }
10296 else {
10297 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10298 }
10299 parser_show_error_line(p, &loc);
10300 set_yylval_noname();
10301 SET_LEX_STATE(EXPR_END);
10302 return result;
10303 }
10304
10305 if (tokadd_ident(p, c)) return 0;
10306 tokenize_ident(p);
10307 return result;
10308}
10309
10310static enum yytokentype
10311parse_ident(struct parser_params *p, int c, int cmd_state)
10312{
10313 enum yytokentype result;
10314 bool is_ascii = true;
10315 const enum lex_state_e last_state = p->lex.state;
10316 ID ident;
10317 int enforce_keyword_end = 0;
10318
10319 do {
10320 if (!ISASCII(c)) is_ascii = false;
10321 if (tokadd_mbchar(p, c) == -1) return 0;
10322 c = nextc(p);
10323 } while (parser_is_identchar(p));
10324 if ((c == '!' || c == '?') && !peek(p, '=')) {
10325 result = tFID;
10326 tokadd(p, c);
10327 }
10328 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10329 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10330 result = tIDENTIFIER;
10331 tokadd(p, c);
10332 }
10333 else {
10334 result = tCONSTANT; /* assume provisionally */
10335 pushback(p, c);
10336 }
10337 tokfix(p);
10338
10339 if (IS_LABEL_POSSIBLE()) {
10340 if (IS_LABEL_SUFFIX(0)) {
10341 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10342 nextc(p);
10343 tokenize_ident(p);
10344 return tLABEL;
10345 }
10346 }
10347
10348#ifndef RIPPER
10349 if (peek_end_expect_token_locations(p)) {
10350 const rb_code_position_t *end_pos;
10351 int lineno, column;
10352 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10353
10354 end_pos = peek_end_expect_token_locations(p)->pos;
10355 lineno = end_pos->lineno;
10356 column = end_pos->column;
10357
10358 if (p->debug) {
10359 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10360 p->ruby_sourceline, beg_pos, lineno, column);
10361 }
10362
10363 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10364 const struct kwtable *kw;
10365
10366 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10367 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10368 enforce_keyword_end = 1;
10369 }
10370 }
10371 }
10372#endif
10373
10374 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10375 const struct kwtable *kw;
10376
10377 /* See if it is a reserved word. */
10378 kw = rb_reserved_word(tok(p), toklen(p));
10379 if (kw) {
10380 enum lex_state_e state = p->lex.state;
10381 if (IS_lex_state_for(state, EXPR_FNAME)) {
10382 SET_LEX_STATE(EXPR_ENDFN);
10383 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10384 return kw->id[0];
10385 }
10386 SET_LEX_STATE(kw->state);
10387 if (IS_lex_state(EXPR_BEG)) {
10388 p->command_start = TRUE;
10389 }
10390 if (kw->id[0] == keyword_do) {
10391 if (lambda_beginning_p()) {
10392 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10393 return keyword_do_LAMBDA;
10394 }
10395 if (COND_P()) return keyword_do_cond;
10396 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10397 return keyword_do_block;
10398 return keyword_do;
10399 }
10400 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10401 return kw->id[0];
10402 else {
10403 if (kw->id[0] != kw->id[1])
10404 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10405 return kw->id[1];
10406 }
10407 }
10408 }
10409
10410 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10411 if (cmd_state) {
10412 SET_LEX_STATE(EXPR_CMDARG);
10413 }
10414 else {
10415 SET_LEX_STATE(EXPR_ARG);
10416 }
10417 }
10418 else if (p->lex.state == EXPR_FNAME) {
10419 SET_LEX_STATE(EXPR_ENDFN);
10420 }
10421 else {
10422 SET_LEX_STATE(EXPR_END);
10423 }
10424
10425 ident = tokenize_ident(p);
10426 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10427 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10428 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10429 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10430 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10431 }
10432 return result;
10433}
10434
10435static void
10436warn_cr(struct parser_params *p)
10437{
10438 if (!p->cr_seen) {
10439 p->cr_seen = TRUE;
10440 /* carried over with p->lex.nextline for nextc() */
10441 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10442 }
10443}
10444
10445static enum yytokentype
10446parser_yylex(struct parser_params *p)
10447{
10448 register int c;
10449 int space_seen = 0;
10450 int cmd_state;
10451 int label;
10452 enum lex_state_e last_state;
10453 int fallthru = FALSE;
10454 int token_seen = p->token_seen;
10455
10456 if (p->lex.strterm) {
10457 if (strterm_is_heredoc(p->lex.strterm)) {
10458 token_flush(p);
10459 return here_document(p, &p->lex.strterm->u.heredoc);
10460 }
10461 else {
10462 token_flush(p);
10463 return parse_string(p, &p->lex.strterm->u.literal);
10464 }
10465 }
10466 cmd_state = p->command_start;
10467 p->command_start = FALSE;
10468 p->token_seen = TRUE;
10469#ifndef RIPPER
10470 token_flush(p);
10471#endif
10472 retry:
10473 last_state = p->lex.state;
10474 switch (c = nextc(p)) {
10475 case '\0': /* NUL */
10476 case '\004': /* ^D */
10477 case '\032': /* ^Z */
10478 case -1: /* end of script. */
10479 p->eofp = 1;
10480#ifndef RIPPER
10481 if (p->end_expect_token_locations) {
10482 pop_end_expect_token_locations(p);
10483 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10484 return tDUMNY_END;
10485 }
10486#endif
10487 /* Set location for end-of-input because dispatch_scan_event is not called. */
10488 RUBY_SET_YYLLOC(*p->yylloc);
10489 return END_OF_INPUT;
10490
10491 /* white spaces */
10492 case '\r':
10493 warn_cr(p);
10494 /* fall through */
10495 case ' ': case '\t': case '\f':
10496 case '\13': /* '\v' */
10497 space_seen = 1;
10498 while ((c = nextc(p))) {
10499 switch (c) {
10500 case '\r':
10501 warn_cr(p);
10502 /* fall through */
10503 case ' ': case '\t': case '\f':
10504 case '\13': /* '\v' */
10505 break;
10506 default:
10507 goto outofloop;
10508 }
10509 }
10510 outofloop:
10511 pushback(p, c);
10512 dispatch_scan_event(p, tSP);
10513#ifndef RIPPER
10514 token_flush(p);
10515#endif
10516 goto retry;
10517
10518 case '#': /* it's a comment */
10519 p->token_seen = token_seen;
10520 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10521 /* no magic_comment in shebang line */
10522 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10523 if (comment_at_top(p)) {
10524 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10525 }
10526 }
10527 p->lex.pcur = pcur, p->lex.ptok = ptok;
10528 lex_goto_eol(p);
10529 dispatch_scan_event(p, tCOMMENT);
10530 fallthru = TRUE;
10531 /* fall through */
10532 case '\n':
10533 p->token_seen = token_seen;
10534 rb_parser_string_t *prevline = p->lex.lastline;
10535 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10536 !IS_lex_state(EXPR_LABELED));
10537 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10538 if (!fallthru) {
10539 dispatch_scan_event(p, tIGNORED_NL);
10540 }
10541 fallthru = FALSE;
10542 if (!c && p->ctxt.in_kwarg) {
10543 goto normal_newline;
10544 }
10545 goto retry;
10546 }
10547 while (1) {
10548 switch (c = nextc(p)) {
10549 case ' ': case '\t': case '\f': case '\r':
10550 case '\13': /* '\v' */
10551 space_seen = 1;
10552 break;
10553 case '#':
10554 pushback(p, c);
10555 if (space_seen) {
10556 dispatch_scan_event(p, tSP);
10557 token_flush(p);
10558 }
10559 goto retry;
10560 case 'a':
10561 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10562 goto bol;
10563 case 'o':
10564 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10565 goto bol;
10566 case '|':
10567 if (peek(p, '|')) goto leading_logical;
10568 goto bol;
10569 case '&':
10570 if (peek(p, '&')) {
10571 leading_logical:
10572 pushback(p, c);
10573 dispatch_delayed_token(p, tIGNORED_NL);
10574 cmd_state = FALSE;
10575 goto retry;
10576 }
10577 /* fall through */
10578 case '.': {
10579 dispatch_delayed_token(p, tIGNORED_NL);
10580 if (peek(p, '.') == (c == '&')) {
10581 pushback(p, c);
10582 dispatch_scan_event(p, tSP);
10583 goto retry;
10584 }
10585 }
10586 bol:
10587 default:
10588 p->ruby_sourceline--;
10589 p->lex.nextline = p->lex.lastline;
10590 set_lastline(p, prevline);
10591 case -1: /* EOF no decrement*/
10592 if (c == -1 && space_seen) {
10593 dispatch_scan_event(p, tSP);
10594 }
10595 lex_goto_eol(p);
10596 if (c != -1) {
10597 token_flush(p);
10598 RUBY_SET_YYLLOC(*p->yylloc);
10599 }
10600 goto normal_newline;
10601 }
10602 }
10603 normal_newline:
10604 p->command_start = TRUE;
10605 SET_LEX_STATE(EXPR_BEG);
10606 return '\n';
10607
10608 case '*':
10609 if ((c = nextc(p)) == '*') {
10610 if ((c = nextc(p)) == '=') {
10611 set_yylval_id(idPow);
10612 SET_LEX_STATE(EXPR_BEG);
10613 return tOP_ASGN;
10614 }
10615 pushback(p, c);
10616 if (IS_SPCARG(c)) {
10617 rb_warning0("'**' interpreted as argument prefix");
10618 c = tDSTAR;
10619 }
10620 else if (IS_BEG()) {
10621 c = tDSTAR;
10622 }
10623 else {
10624 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10625 }
10626 }
10627 else {
10628 if (c == '=') {
10629 set_yylval_id('*');
10630 SET_LEX_STATE(EXPR_BEG);
10631 return tOP_ASGN;
10632 }
10633 pushback(p, c);
10634 if (IS_SPCARG(c)) {
10635 rb_warning0("'*' interpreted as argument prefix");
10636 c = tSTAR;
10637 }
10638 else if (IS_BEG()) {
10639 c = tSTAR;
10640 }
10641 else {
10642 c = warn_balanced('*', "*", "argument prefix");
10643 }
10644 }
10645 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10646 return c;
10647
10648 case '!':
10649 c = nextc(p);
10650 if (IS_AFTER_OPERATOR()) {
10651 SET_LEX_STATE(EXPR_ARG);
10652 if (c == '@') {
10653 return '!';
10654 }
10655 }
10656 else {
10657 SET_LEX_STATE(EXPR_BEG);
10658 }
10659 if (c == '=') {
10660 return tNEQ;
10661 }
10662 if (c == '~') {
10663 return tNMATCH;
10664 }
10665 pushback(p, c);
10666 return '!';
10667
10668 case '=':
10669 if (was_bol(p)) {
10670 /* skip embedded rd document */
10671 if (word_match_p(p, "begin", 5)) {
10672 int first_p = TRUE;
10673
10674 lex_goto_eol(p);
10675 dispatch_scan_event(p, tEMBDOC_BEG);
10676 for (;;) {
10677 lex_goto_eol(p);
10678 if (!first_p) {
10679 dispatch_scan_event(p, tEMBDOC);
10680 }
10681 first_p = FALSE;
10682 c = nextc(p);
10683 if (c == -1) {
10684 compile_error(p, "embedded document meets end of file");
10685 return END_OF_INPUT;
10686 }
10687 if (c == '=' && word_match_p(p, "end", 3)) {
10688 break;
10689 }
10690 pushback(p, c);
10691 }
10692 lex_goto_eol(p);
10693 dispatch_scan_event(p, tEMBDOC_END);
10694 goto retry;
10695 }
10696 }
10697
10698 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10699 if ((c = nextc(p)) == '=') {
10700 if ((c = nextc(p)) == '=') {
10701 return tEQQ;
10702 }
10703 pushback(p, c);
10704 return tEQ;
10705 }
10706 if (c == '~') {
10707 return tMATCH;
10708 }
10709 else if (c == '>') {
10710 return tASSOC;
10711 }
10712 pushback(p, c);
10713 return '=';
10714
10715 case '<':
10716 c = nextc(p);
10717 if (c == '<' &&
10718 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10719 !IS_END() &&
10720 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10721 enum yytokentype token = heredoc_identifier(p);
10722 if (token) return token < 0 ? 0 : token;
10723 }
10724 if (IS_AFTER_OPERATOR()) {
10725 SET_LEX_STATE(EXPR_ARG);
10726 }
10727 else {
10728 if (IS_lex_state(EXPR_CLASS))
10729 p->command_start = TRUE;
10730 SET_LEX_STATE(EXPR_BEG);
10731 }
10732 if (c == '=') {
10733 if ((c = nextc(p)) == '>') {
10734 return tCMP;
10735 }
10736 pushback(p, c);
10737 return tLEQ;
10738 }
10739 if (c == '<') {
10740 if ((c = nextc(p)) == '=') {
10741 set_yylval_id(idLTLT);
10742 SET_LEX_STATE(EXPR_BEG);
10743 return tOP_ASGN;
10744 }
10745 pushback(p, c);
10746 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10747 }
10748 pushback(p, c);
10749 return '<';
10750
10751 case '>':
10752 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10753 if ((c = nextc(p)) == '=') {
10754 return tGEQ;
10755 }
10756 if (c == '>') {
10757 if ((c = nextc(p)) == '=') {
10758 set_yylval_id(idGTGT);
10759 SET_LEX_STATE(EXPR_BEG);
10760 return tOP_ASGN;
10761 }
10762 pushback(p, c);
10763 return tRSHFT;
10764 }
10765 pushback(p, c);
10766 return '>';
10767
10768 case '"':
10769 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10770 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10771 p->lex.ptok = p->lex.pcur-1;
10772 return tSTRING_BEG;
10773
10774 case '`':
10775 if (IS_lex_state(EXPR_FNAME)) {
10776 SET_LEX_STATE(EXPR_ENDFN);
10777 return c;
10778 }
10779 if (IS_lex_state(EXPR_DOT)) {
10780 if (cmd_state)
10781 SET_LEX_STATE(EXPR_CMDARG);
10782 else
10783 SET_LEX_STATE(EXPR_ARG);
10784 return c;
10785 }
10786 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10787 return tXSTRING_BEG;
10788
10789 case '\'':
10790 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10791 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10792 p->lex.ptok = p->lex.pcur-1;
10793 return tSTRING_BEG;
10794
10795 case '?':
10796 return parse_qmark(p, space_seen);
10797
10798 case '&':
10799 if ((c = nextc(p)) == '&') {
10800 SET_LEX_STATE(EXPR_BEG);
10801 if ((c = nextc(p)) == '=') {
10802 set_yylval_id(idANDOP);
10803 SET_LEX_STATE(EXPR_BEG);
10804 return tOP_ASGN;
10805 }
10806 pushback(p, c);
10807 return tANDOP;
10808 }
10809 else if (c == '=') {
10810 set_yylval_id('&');
10811 SET_LEX_STATE(EXPR_BEG);
10812 return tOP_ASGN;
10813 }
10814 else if (c == '.') {
10815 set_yylval_id(idANDDOT);
10816 SET_LEX_STATE(EXPR_DOT);
10817 return tANDDOT;
10818 }
10819 pushback(p, c);
10820 if (IS_SPCARG(c)) {
10821 if ((c != ':') ||
10822 (c = peekc_n(p, 1)) == -1 ||
10823 !(c == '\'' || c == '"' ||
10824 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10825 rb_warning0("'&' interpreted as argument prefix");
10826 }
10827 c = tAMPER;
10828 }
10829 else if (IS_BEG()) {
10830 c = tAMPER;
10831 }
10832 else {
10833 c = warn_balanced('&', "&", "argument prefix");
10834 }
10835 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10836 return c;
10837
10838 case '|':
10839 if ((c = nextc(p)) == '|') {
10840 SET_LEX_STATE(EXPR_BEG);
10841 if ((c = nextc(p)) == '=') {
10842 set_yylval_id(idOROP);
10843 SET_LEX_STATE(EXPR_BEG);
10844 return tOP_ASGN;
10845 }
10846 pushback(p, c);
10847 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10848 c = '|';
10849 pushback(p, '|');
10850 return c;
10851 }
10852 return tOROP;
10853 }
10854 if (c == '=') {
10855 set_yylval_id('|');
10856 SET_LEX_STATE(EXPR_BEG);
10857 return tOP_ASGN;
10858 }
10859 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10860 pushback(p, c);
10861 return '|';
10862
10863 case '+':
10864 c = nextc(p);
10865 if (IS_AFTER_OPERATOR()) {
10866 SET_LEX_STATE(EXPR_ARG);
10867 if (c == '@') {
10868 return tUPLUS;
10869 }
10870 pushback(p, c);
10871 return '+';
10872 }
10873 if (c == '=') {
10874 set_yylval_id('+');
10875 SET_LEX_STATE(EXPR_BEG);
10876 return tOP_ASGN;
10877 }
10878 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10879 SET_LEX_STATE(EXPR_BEG);
10880 pushback(p, c);
10881 if (c != -1 && ISDIGIT(c)) {
10882 return parse_numeric(p, '+');
10883 }
10884 return tUPLUS;
10885 }
10886 SET_LEX_STATE(EXPR_BEG);
10887 pushback(p, c);
10888 return warn_balanced('+', "+", "unary operator");
10889
10890 case '-':
10891 c = nextc(p);
10892 if (IS_AFTER_OPERATOR()) {
10893 SET_LEX_STATE(EXPR_ARG);
10894 if (c == '@') {
10895 return tUMINUS;
10896 }
10897 pushback(p, c);
10898 return '-';
10899 }
10900 if (c == '=') {
10901 set_yylval_id('-');
10902 SET_LEX_STATE(EXPR_BEG);
10903 return tOP_ASGN;
10904 }
10905 if (c == '>') {
10906 SET_LEX_STATE(EXPR_ENDFN);
10907 yylval.num = p->lex.lpar_beg;
10908 p->lex.lpar_beg = p->lex.paren_nest;
10909 return tLAMBDA;
10910 }
10911 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10912 SET_LEX_STATE(EXPR_BEG);
10913 pushback(p, c);
10914 if (c != -1 && ISDIGIT(c)) {
10915 return tUMINUS_NUM;
10916 }
10917 return tUMINUS;
10918 }
10919 SET_LEX_STATE(EXPR_BEG);
10920 pushback(p, c);
10921 return warn_balanced('-', "-", "unary operator");
10922
10923 case '.': {
10924 int is_beg = IS_BEG();
10925 SET_LEX_STATE(EXPR_BEG);
10926 if ((c = nextc(p)) == '.') {
10927 if ((c = nextc(p)) == '.') {
10928 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10929 SET_LEX_STATE(EXPR_ENDARG);
10930 return tBDOT3;
10931 }
10932 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10933 rb_warn0("... at EOL, should be parenthesized?");
10934 }
10935 return is_beg ? tBDOT3 : tDOT3;
10936 }
10937 pushback(p, c);
10938 return is_beg ? tBDOT2 : tDOT2;
10939 }
10940 pushback(p, c);
10941 if (c != -1 && ISDIGIT(c)) {
10942 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10943 parse_numeric(p, '.');
10944 if (ISDIGIT(prev)) {
10945 yyerror0("unexpected fraction part after numeric literal");
10946 }
10947 else {
10948 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10949 }
10950 SET_LEX_STATE(EXPR_END);
10951 p->lex.ptok = p->lex.pcur;
10952 goto retry;
10953 }
10954 set_yylval_id('.');
10955 SET_LEX_STATE(EXPR_DOT);
10956 return '.';
10957 }
10958
10959 case '0': case '1': case '2': case '3': case '4':
10960 case '5': case '6': case '7': case '8': case '9':
10961 return parse_numeric(p, c);
10962
10963 case ')':
10964 COND_POP();
10965 CMDARG_POP();
10966 SET_LEX_STATE(EXPR_ENDFN);
10967 p->lex.paren_nest--;
10968 return c;
10969
10970 case ']':
10971 COND_POP();
10972 CMDARG_POP();
10973 SET_LEX_STATE(EXPR_END);
10974 p->lex.paren_nest--;
10975 return c;
10976
10977 case '}':
10978 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10979 if (!p->lex.brace_nest--) return tSTRING_DEND;
10980 COND_POP();
10981 CMDARG_POP();
10982 SET_LEX_STATE(EXPR_END);
10983 p->lex.paren_nest--;
10984 return c;
10985
10986 case ':':
10987 c = nextc(p);
10988 if (c == ':') {
10989 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10990 SET_LEX_STATE(EXPR_BEG);
10991 return tCOLON3;
10992 }
10993 set_yylval_id(idCOLON2);
10994 SET_LEX_STATE(EXPR_DOT);
10995 return tCOLON2;
10996 }
10997 if (IS_END() || ISSPACE(c) || c == '#') {
10998 pushback(p, c);
10999 c = warn_balanced(':', ":", "symbol literal");
11000 SET_LEX_STATE(EXPR_BEG);
11001 return c;
11002 }
11003 switch (c) {
11004 case '\'':
11005 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11006 break;
11007 case '"':
11008 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11009 break;
11010 default:
11011 pushback(p, c);
11012 break;
11013 }
11014 SET_LEX_STATE(EXPR_FNAME);
11015 return tSYMBEG;
11016
11017 case '/':
11018 if (IS_BEG()) {
11019 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11020 return tREGEXP_BEG;
11021 }
11022 if ((c = nextc(p)) == '=') {
11023 set_yylval_id('/');
11024 SET_LEX_STATE(EXPR_BEG);
11025 return tOP_ASGN;
11026 }
11027 pushback(p, c);
11028 if (IS_SPCARG(c)) {
11029 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11030 return tREGEXP_BEG;
11031 }
11032 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11033 return warn_balanced('/', "/", "regexp literal");
11034
11035 case '^':
11036 if ((c = nextc(p)) == '=') {
11037 set_yylval_id('^');
11038 SET_LEX_STATE(EXPR_BEG);
11039 return tOP_ASGN;
11040 }
11041 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11042 pushback(p, c);
11043 return '^';
11044
11045 case ';':
11046 SET_LEX_STATE(EXPR_BEG);
11047 p->command_start = TRUE;
11048 return ';';
11049
11050 case ',':
11051 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11052 return ',';
11053
11054 case '~':
11055 if (IS_AFTER_OPERATOR()) {
11056 if ((c = nextc(p)) != '@') {
11057 pushback(p, c);
11058 }
11059 SET_LEX_STATE(EXPR_ARG);
11060 }
11061 else {
11062 SET_LEX_STATE(EXPR_BEG);
11063 }
11064 return '~';
11065
11066 case '(':
11067 if (IS_BEG()) {
11068 c = tLPAREN;
11069 }
11070 else if (!space_seen) {
11071 /* foo( ... ) => method call, no ambiguity */
11072 }
11073 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11074 c = tLPAREN_ARG;
11075 }
11076 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11077 rb_warning0("parentheses after method name is interpreted as "
11078 "an argument list, not a decomposed argument");
11079 }
11080 p->lex.paren_nest++;
11081 COND_PUSH(0);
11082 CMDARG_PUSH(0);
11083 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11084 return c;
11085
11086 case '[':
11087 p->lex.paren_nest++;
11088 if (IS_AFTER_OPERATOR()) {
11089 if ((c = nextc(p)) == ']') {
11090 p->lex.paren_nest--;
11091 SET_LEX_STATE(EXPR_ARG);
11092 if ((c = nextc(p)) == '=') {
11093 return tASET;
11094 }
11095 pushback(p, c);
11096 return tAREF;
11097 }
11098 pushback(p, c);
11099 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11100 return '[';
11101 }
11102 else if (IS_BEG()) {
11103 c = tLBRACK;
11104 }
11105 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11106 c = tLBRACK;
11107 }
11108 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11109 COND_PUSH(0);
11110 CMDARG_PUSH(0);
11111 return c;
11112
11113 case '{':
11114 ++p->lex.brace_nest;
11115 if (lambda_beginning_p())
11116 c = tLAMBEG;
11117 else if (IS_lex_state(EXPR_LABELED))
11118 c = tLBRACE; /* hash */
11119 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11120 c = '{'; /* block (primary) */
11121 else if (IS_lex_state(EXPR_ENDARG))
11122 c = tLBRACE_ARG; /* block (expr) */
11123 else
11124 c = tLBRACE; /* hash */
11125 if (c != tLBRACE) {
11126 p->command_start = TRUE;
11127 SET_LEX_STATE(EXPR_BEG);
11128 }
11129 else {
11130 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11131 }
11132 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11133 COND_PUSH(0);
11134 CMDARG_PUSH(0);
11135 return c;
11136
11137 case '\\':
11138 c = nextc(p);
11139 if (c == '\n') {
11140 space_seen = 1;
11141 dispatch_scan_event(p, tSP);
11142 goto retry; /* skip \\n */
11143 }
11144 if (c == ' ') return tSP;
11145 if (ISSPACE(c)) return c;
11146 pushback(p, c);
11147 return '\\';
11148
11149 case '%':
11150 return parse_percent(p, space_seen, last_state);
11151
11152 case '$':
11153 return parse_gvar(p, last_state);
11154
11155 case '@':
11156 return parse_atmark(p, last_state);
11157
11158 case '_':
11159 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11160 p->ruby__end__seen = 1;
11161 p->eofp = 1;
11162#ifdef RIPPER
11163 lex_goto_eol(p);
11164 dispatch_scan_event(p, k__END__);
11165#endif
11166 return END_OF_INPUT;
11167 }
11168 newtok(p);
11169 break;
11170
11171 default:
11172 if (!parser_is_identchar(p)) {
11173 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11174 token_flush(p);
11175 goto retry;
11176 }
11177
11178 newtok(p);
11179 break;
11180 }
11181
11182 return parse_ident(p, c, cmd_state);
11183}
11184
11185static enum yytokentype
11186yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11187{
11188 enum yytokentype t;
11189
11190 p->lval = lval;
11191 lval->node = 0;
11192 p->yylloc = yylloc;
11193
11194 t = parser_yylex(p);
11195
11196 if (has_delayed_token(p))
11197 dispatch_delayed_token(p, t);
11198 else if (t != END_OF_INPUT)
11199 dispatch_scan_event(p, t);
11200
11201 return t;
11202}
11203
11204#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11205
11206static NODE*
11207node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11208{
11209 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11210
11211 rb_node_init(n, type);
11212 return n;
11213}
11214
11215static NODE *
11216nd_set_loc(NODE *nd, const YYLTYPE *loc)
11217{
11218 nd->nd_loc = *loc;
11219 nd_set_line(nd, loc->beg_pos.lineno);
11220 return nd;
11221}
11222
11223static NODE*
11224node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11225{
11226 NODE *n = node_new_internal(p, type, size, alignment);
11227
11228 nd_set_loc(n, loc);
11229 nd_set_node_id(n, parser_get_node_id(p));
11230 return n;
11231}
11232
11233#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11234
11235static rb_node_scope_t *
11236rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11237{
11238 rb_ast_id_table_t *nd_tbl;
11239 nd_tbl = local_tbl(p);
11240 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11241 n->nd_tbl = nd_tbl;
11242 n->nd_body = nd_body;
11243 n->nd_parent = nd_parent;
11244 n->nd_args = nd_args;
11245
11246 return n;
11247}
11248
11249static rb_node_scope_t *
11250rb_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)
11251{
11252 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11253 n->nd_tbl = nd_tbl;
11254 n->nd_body = nd_body;
11255 n->nd_parent = nd_parent;
11256 n->nd_args = nd_args;
11257
11258 return n;
11259}
11260
11261static rb_node_defn_t *
11262rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11263{
11264 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11265 n->nd_mid = nd_mid;
11266 n->nd_defn = nd_defn;
11267
11268 return n;
11269}
11270
11271static rb_node_defs_t *
11272rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11273{
11274 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11275 n->nd_recv = nd_recv;
11276 n->nd_mid = nd_mid;
11277 n->nd_defn = nd_defn;
11278
11279 return n;
11280}
11281
11282static rb_node_block_t *
11283rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11284{
11285 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11286 n->nd_head = nd_head;
11287 n->nd_end = (NODE *)n;
11288 n->nd_next = 0;
11289
11290 return n;
11291}
11292
11293static rb_node_for_t *
11294rb_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)
11295{
11296 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11297 n->nd_body = nd_body;
11298 n->nd_iter = nd_iter;
11299 n->for_keyword_loc = *for_keyword_loc;
11300 n->in_keyword_loc = *in_keyword_loc;
11301 n->do_keyword_loc = *do_keyword_loc;
11302 n->end_keyword_loc = *end_keyword_loc;
11303
11304 return n;
11305}
11306
11307static rb_node_for_masgn_t *
11308rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11309{
11310 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11311 n->nd_var = nd_var;
11312
11313 return n;
11314}
11315
11316static rb_node_retry_t *
11317rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11318{
11319 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11320
11321 return n;
11322}
11323
11324static rb_node_begin_t *
11325rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11326{
11327 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11328 n->nd_body = nd_body;
11329
11330 return n;
11331}
11332
11333static rb_node_rescue_t *
11334rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11335{
11336 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11337 n->nd_head = nd_head;
11338 n->nd_resq = nd_resq;
11339 n->nd_else = nd_else;
11340
11341 return n;
11342}
11343
11344static rb_node_resbody_t *
11345rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11346{
11347 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11348 n->nd_args = nd_args;
11349 n->nd_exc_var = nd_exc_var;
11350 n->nd_body = nd_body;
11351 n->nd_next = nd_next;
11352
11353 return n;
11354}
11355
11356static rb_node_ensure_t *
11357rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11358{
11359 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11360 n->nd_head = nd_head;
11361 n->nd_ensr = nd_ensr;
11362
11363 return n;
11364}
11365
11366static rb_node_and_t *
11367rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11368{
11369 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11370 n->nd_1st = nd_1st;
11371 n->nd_2nd = nd_2nd;
11372 n->operator_loc = *operator_loc;
11373
11374 return n;
11375}
11376
11377static rb_node_or_t *
11378rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11379{
11380 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11381 n->nd_1st = nd_1st;
11382 n->nd_2nd = nd_2nd;
11383 n->operator_loc = *operator_loc;
11384
11385 return n;
11386}
11387
11388static rb_node_return_t *
11389rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11390{
11391 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11392 n->nd_stts = nd_stts;
11393 n->keyword_loc = *keyword_loc;
11394 return n;
11395}
11396
11397static rb_node_yield_t *
11398rb_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)
11399{
11400 if (nd_head) no_blockarg(p, nd_head);
11401
11402 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11403 n->nd_head = nd_head;
11404 n->keyword_loc = *keyword_loc;
11405 n->lparen_loc = *lparen_loc;
11406 n->rparen_loc = *rparen_loc;
11407
11408 return n;
11409}
11410
11411static rb_node_if_t *
11412rb_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)
11413{
11414 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11415 n->nd_cond = nd_cond;
11416 n->nd_body = nd_body;
11417 n->nd_else = nd_else;
11418 n->if_keyword_loc = *if_keyword_loc;
11419 n->then_keyword_loc = *then_keyword_loc;
11420 n->end_keyword_loc = *end_keyword_loc;
11421
11422 return n;
11423}
11424
11425static rb_node_unless_t *
11426rb_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)
11427{
11428 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11429 n->nd_cond = nd_cond;
11430 n->nd_body = nd_body;
11431 n->nd_else = nd_else;
11432 n->keyword_loc = *keyword_loc;
11433 n->then_keyword_loc = *then_keyword_loc;
11434 n->end_keyword_loc = *end_keyword_loc;
11435
11436 return n;
11437}
11438
11439static rb_node_class_t *
11440rb_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)
11441{
11442 /* Keep the order of node creation */
11443 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11444 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11445 RNODE_SCOPE(scope)->nd_parent = &n->node;
11446 n->nd_cpath = nd_cpath;
11447 n->nd_body = scope;
11448 n->nd_super = nd_super;
11449 n->class_keyword_loc = *class_keyword_loc;
11450 n->inheritance_operator_loc = *inheritance_operator_loc;
11451 n->end_keyword_loc = *end_keyword_loc;
11452
11453 return n;
11454}
11455
11456static rb_node_sclass_t *
11457rb_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)
11458{
11459 /* Keep the order of node creation */
11460 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11461 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11462 RNODE_SCOPE(scope)->nd_parent = &n->node;
11463 n->nd_recv = nd_recv;
11464 n->nd_body = scope;
11465 n->class_keyword_loc = *class_keyword_loc;
11466 n->operator_loc = *operator_loc;
11467 n->end_keyword_loc = *end_keyword_loc;
11468
11469 return n;
11470}
11471
11472static rb_node_module_t *
11473rb_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)
11474{
11475 /* Keep the order of node creation */
11476 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11477 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11478 RNODE_SCOPE(scope)->nd_parent = &n->node;
11479 n->nd_cpath = nd_cpath;
11480 n->nd_body = scope;
11481 n->module_keyword_loc = *module_keyword_loc;
11482 n->end_keyword_loc = *end_keyword_loc;
11483
11484 return n;
11485}
11486
11487static rb_node_iter_t *
11488rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11489{
11490 /* Keep the order of node creation */
11491 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11492 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11493 RNODE_SCOPE(scope)->nd_parent = &n->node;
11494 n->nd_body = scope;
11495 n->nd_iter = 0;
11496
11497 return n;
11498}
11499
11500static rb_node_lambda_t *
11501rb_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)
11502{
11503 /* Keep the order of node creation */
11504 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11505 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11506 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11507 RNODE_SCOPE(scope)->nd_parent = &n->node;
11508 n->nd_body = scope;
11509 n->operator_loc = *operator_loc;
11510 n->opening_loc = *opening_loc;
11511 n->closing_loc = *closing_loc;
11512
11513 return n;
11514}
11515
11516static rb_node_case_t *
11517rb_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)
11518{
11519 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11520 n->nd_head = nd_head;
11521 n->nd_body = nd_body;
11522 n->case_keyword_loc = *case_keyword_loc;
11523 n->end_keyword_loc = *end_keyword_loc;
11524
11525 return n;
11526}
11527
11528static rb_node_case2_t *
11529rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11530{
11531 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11532 n->nd_head = 0;
11533 n->nd_body = nd_body;
11534 n->case_keyword_loc = *case_keyword_loc;
11535 n->end_keyword_loc = *end_keyword_loc;
11536
11537 return n;
11538}
11539
11540static rb_node_case3_t *
11541rb_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)
11542{
11543 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11544 n->nd_head = nd_head;
11545 n->nd_body = nd_body;
11546 n->case_keyword_loc = *case_keyword_loc;
11547 n->end_keyword_loc = *end_keyword_loc;
11548
11549 return n;
11550}
11551
11552static rb_node_when_t *
11553rb_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)
11554{
11555 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11556 n->nd_head = nd_head;
11557 n->nd_body = nd_body;
11558 n->nd_next = nd_next;
11559 n->keyword_loc = *keyword_loc;
11560 n->then_keyword_loc = *then_keyword_loc;
11561
11562 return n;
11563}
11564
11565static rb_node_in_t *
11566rb_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)
11567{
11568 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11569 n->nd_head = nd_head;
11570 n->nd_body = nd_body;
11571 n->nd_next = nd_next;
11572 n->in_keyword_loc = *in_keyword_loc;
11573 n->then_keyword_loc = *then_keyword_loc;
11574 n->operator_loc = *operator_loc;
11575
11576 return n;
11577}
11578
11579static rb_node_while_t *
11580rb_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)
11581{
11582 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11583 n->nd_cond = nd_cond;
11584 n->nd_body = nd_body;
11585 n->nd_state = nd_state;
11586 n->keyword_loc = *keyword_loc;
11587 n->closing_loc = *closing_loc;
11588
11589 return n;
11590}
11591
11592static rb_node_until_t *
11593rb_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)
11594{
11595 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11596 n->nd_cond = nd_cond;
11597 n->nd_body = nd_body;
11598 n->nd_state = nd_state;
11599 n->keyword_loc = *keyword_loc;
11600 n->closing_loc = *closing_loc;
11601
11602 return n;
11603}
11604
11605static rb_node_colon2_t *
11606rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11607{
11608 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11609 n->nd_head = nd_head;
11610 n->nd_mid = nd_mid;
11611 n->delimiter_loc = *delimiter_loc;
11612 n->name_loc = *name_loc;
11613
11614 return n;
11615}
11616
11617static rb_node_colon3_t *
11618rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11619{
11620 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11621 n->nd_mid = nd_mid;
11622 n->delimiter_loc = *delimiter_loc;
11623 n->name_loc = *name_loc;
11624
11625 return n;
11626}
11627
11628static rb_node_dot2_t *
11629rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11630{
11631 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11632 n->nd_beg = nd_beg;
11633 n->nd_end = nd_end;
11634 n->operator_loc = *operator_loc;
11635
11636 return n;
11637}
11638
11639static rb_node_dot3_t *
11640rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11641{
11642 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11643 n->nd_beg = nd_beg;
11644 n->nd_end = nd_end;
11645 n->operator_loc = *operator_loc;
11646
11647 return n;
11648}
11649
11650static rb_node_self_t *
11651rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11652{
11653 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11654 n->nd_state = 1;
11655
11656 return n;
11657}
11658
11659static rb_node_nil_t *
11660rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11661{
11662 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11663
11664 return n;
11665}
11666
11667static rb_node_true_t *
11668rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11669{
11670 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11671
11672 return n;
11673}
11674
11675static rb_node_false_t *
11676rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11677{
11678 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11679
11680 return n;
11681}
11682
11683static rb_node_super_t *
11684rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11685 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11686{
11687 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11688 n->nd_args = nd_args;
11689 n->keyword_loc = *keyword_loc;
11690 n->lparen_loc = *lparen_loc;
11691 n->rparen_loc = *rparen_loc;
11692
11693 return n;
11694}
11695
11696static rb_node_zsuper_t *
11697rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11698{
11699 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11700
11701 return n;
11702}
11703
11704static rb_node_match2_t *
11705rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11706{
11707 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11708 n->nd_recv = nd_recv;
11709 n->nd_value = nd_value;
11710 n->nd_args = 0;
11711
11712 return n;
11713}
11714
11715static rb_node_match3_t *
11716rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11717{
11718 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11719 n->nd_recv = nd_recv;
11720 n->nd_value = nd_value;
11721
11722 return n;
11723}
11724
11725static rb_node_list_t *
11726rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11727{
11728 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11729}
11730
11731static rb_node_list_t *
11732rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11733{
11734 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11735 n->nd_head = nd_head;
11736 n->as.nd_alen = nd_alen;
11737 n->nd_next = nd_next;
11738
11739 return n;
11740}
11741
11742static rb_node_zlist_t *
11743rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11744{
11745 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11746
11747 return n;
11748}
11749
11750static rb_node_hash_t *
11751rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11752{
11753 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11754 n->nd_head = nd_head;
11755 n->nd_brace = 0;
11756
11757 return n;
11758}
11759
11760static rb_node_masgn_t *
11761rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11762{
11763 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11764 n->nd_head = nd_head;
11765 n->nd_value = 0;
11766 n->nd_args = nd_args;
11767
11768 return n;
11769}
11770
11771static rb_node_gasgn_t *
11772rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11773{
11774 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11775 n->nd_vid = nd_vid;
11776 n->nd_value = nd_value;
11777
11778 return n;
11779}
11780
11781static rb_node_lasgn_t *
11782rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11783{
11784 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11785 n->nd_vid = nd_vid;
11786 n->nd_value = nd_value;
11787
11788 return n;
11789}
11790
11791static rb_node_dasgn_t *
11792rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11793{
11794 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11795 n->nd_vid = nd_vid;
11796 n->nd_value = nd_value;
11797
11798 return n;
11799}
11800
11801static rb_node_iasgn_t *
11802rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11803{
11804 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11805 n->nd_vid = nd_vid;
11806 n->nd_value = nd_value;
11807
11808 return n;
11809}
11810
11811static rb_node_cvasgn_t *
11812rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11813{
11814 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11815 n->nd_vid = nd_vid;
11816 n->nd_value = nd_value;
11817
11818 return n;
11819}
11820
11821static rb_node_op_asgn1_t *
11822rb_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)
11823{
11824 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11825 n->nd_recv = nd_recv;
11826 n->nd_mid = nd_mid;
11827 n->nd_index = index;
11828 n->nd_rvalue = rvalue;
11829 n->call_operator_loc = *call_operator_loc;
11830 n->opening_loc = *opening_loc;
11831 n->closing_loc = *closing_loc;
11832 n->binary_operator_loc = *binary_operator_loc;
11833
11834 return n;
11835}
11836
11837static rb_node_op_asgn2_t *
11838rb_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)
11839{
11840 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11841 n->nd_recv = nd_recv;
11842 n->nd_value = nd_value;
11843 n->nd_vid = nd_vid;
11844 n->nd_mid = nd_mid;
11845 n->nd_aid = nd_aid;
11846 n->call_operator_loc = *call_operator_loc;
11847 n->message_loc = *message_loc;
11848 n->binary_operator_loc = *binary_operator_loc;
11849
11850 return n;
11851}
11852
11853static rb_node_op_asgn_or_t *
11854rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11855{
11856 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11857 n->nd_head = nd_head;
11858 n->nd_value = nd_value;
11859
11860 return n;
11861}
11862
11863static rb_node_op_asgn_and_t *
11864rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11865{
11866 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11867 n->nd_head = nd_head;
11868 n->nd_value = nd_value;
11869
11870 return n;
11871}
11872
11873static rb_node_gvar_t *
11874rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11875{
11876 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11877 n->nd_vid = nd_vid;
11878
11879 return n;
11880}
11881
11882static rb_node_lvar_t *
11883rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11884{
11885 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11886 n->nd_vid = nd_vid;
11887
11888 return n;
11889}
11890
11891static rb_node_dvar_t *
11892rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11893{
11894 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11895 n->nd_vid = nd_vid;
11896
11897 return n;
11898}
11899
11900static rb_node_ivar_t *
11901rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11902{
11903 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11904 n->nd_vid = nd_vid;
11905
11906 return n;
11907}
11908
11909static rb_node_const_t *
11910rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11911{
11912 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11913 n->nd_vid = nd_vid;
11914
11915 return n;
11916}
11917
11918static rb_node_cvar_t *
11919rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11920{
11921 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11922 n->nd_vid = nd_vid;
11923
11924 return n;
11925}
11926
11927static rb_node_nth_ref_t *
11928rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11929{
11930 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11931 n->nd_nth = nd_nth;
11932
11933 return n;
11934}
11935
11936static rb_node_back_ref_t *
11937rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11938{
11939 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11940 n->nd_nth = nd_nth;
11941
11942 return n;
11943}
11944
11945static rb_node_integer_t *
11946rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11947{
11948 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11949 n->val = val;
11950 n->minus = FALSE;
11951 n->base = base;
11952
11953 return n;
11954}
11955
11956static rb_node_float_t *
11957rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11958{
11959 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11960 n->val = val;
11961 n->minus = FALSE;
11962
11963 return n;
11964}
11965
11966static rb_node_rational_t *
11967rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11968{
11969 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11970 n->val = val;
11971 n->minus = FALSE;
11972 n->base = base;
11973 n->seen_point = seen_point;
11974
11975 return n;
11976}
11977
11978static rb_node_imaginary_t *
11979rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11980{
11981 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11982 n->val = val;
11983 n->minus = FALSE;
11984 n->base = base;
11985 n->seen_point = seen_point;
11986 n->type = numeric_type;
11987
11988 return n;
11989}
11990
11991static rb_node_str_t *
11992rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11993{
11994 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11995 n->string = string;
11996
11997 return n;
11998}
11999
12000/* TODO; Use union for NODE_DSTR2 */
12001static rb_node_dstr_t *
12002rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12003{
12004 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12005 n->string = string;
12006 n->as.nd_alen = nd_alen;
12007 n->nd_next = (rb_node_list_t *)nd_next;
12008
12009 return n;
12010}
12011
12012static rb_node_dstr_t *
12013rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12014{
12015 return rb_node_dstr_new0(p, string, 1, 0, loc);
12016}
12017
12018static rb_node_xstr_t *
12019rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12020{
12021 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12022 n->string = string;
12023
12024 return n;
12025}
12026
12027static rb_node_dxstr_t *
12028rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12029{
12030 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12031 n->string = string;
12032 n->as.nd_alen = nd_alen;
12033 n->nd_next = (rb_node_list_t *)nd_next;
12034
12035 return n;
12036}
12037
12038static rb_node_sym_t *
12039rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12040{
12041 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12042 n->string = rb_str_to_parser_string(p, str);
12043
12044 return n;
12045}
12046
12047static rb_node_dsym_t *
12048rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12049{
12050 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12051 n->string = string;
12052 n->as.nd_alen = nd_alen;
12053 n->nd_next = (rb_node_list_t *)nd_next;
12054
12055 return n;
12056}
12057
12058static rb_node_evstr_t *
12059rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12060{
12061 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12062 n->nd_body = nd_body;
12063 n->opening_loc = *opening_loc;
12064 n->closing_loc = *closing_loc;
12065
12066 return n;
12067}
12068
12069static rb_node_regx_t *
12070rb_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)
12071{
12072 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12073 n->string = string;
12074 n->options = options & RE_OPTION_MASK;
12075 n->opening_loc = *opening_loc;
12076 n->content_loc = *content_loc;
12077 n->closing_loc = *closing_loc;
12078
12079 return n;
12080}
12081
12082static rb_node_call_t *
12083rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12084{
12085 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12086 n->nd_recv = nd_recv;
12087 n->nd_mid = nd_mid;
12088 n->nd_args = nd_args;
12089
12090 return n;
12091}
12092
12093static rb_node_opcall_t *
12094rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12095{
12096 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12097 n->nd_recv = nd_recv;
12098 n->nd_mid = nd_mid;
12099 n->nd_args = nd_args;
12100
12101 return n;
12102}
12103
12104static rb_node_fcall_t *
12105rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12106{
12107 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12108 n->nd_mid = nd_mid;
12109 n->nd_args = nd_args;
12110
12111 return n;
12112}
12113
12114static rb_node_qcall_t *
12115rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12116{
12117 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12118 n->nd_recv = nd_recv;
12119 n->nd_mid = nd_mid;
12120 n->nd_args = nd_args;
12121
12122 return n;
12123}
12124
12125static rb_node_vcall_t *
12126rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12127{
12128 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12129 n->nd_mid = nd_mid;
12130
12131 return n;
12132}
12133
12134static rb_node_once_t *
12135rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12136{
12137 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12138 n->nd_body = nd_body;
12139
12140 return n;
12141}
12142
12143static rb_node_args_t *
12144rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12145{
12146 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12147 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12148
12149 return n;
12150}
12151
12152static rb_node_args_aux_t *
12153rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12154{
12155 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12156 n->nd_pid = nd_pid;
12157 n->nd_plen = nd_plen;
12158 n->nd_next = 0;
12159
12160 return n;
12161}
12162
12163static rb_node_opt_arg_t *
12164rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12165{
12166 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12167 n->nd_body = nd_body;
12168 n->nd_next = 0;
12169
12170 return n;
12171}
12172
12173static rb_node_kw_arg_t *
12174rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12175{
12176 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12177 n->nd_body = nd_body;
12178 n->nd_next = 0;
12179
12180 return n;
12181}
12182
12183static rb_node_postarg_t *
12184rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12185{
12186 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12187 n->nd_1st = nd_1st;
12188 n->nd_2nd = nd_2nd;
12189
12190 return n;
12191}
12192
12193static rb_node_argscat_t *
12194rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12195{
12196 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12197 n->nd_head = nd_head;
12198 n->nd_body = nd_body;
12199
12200 return n;
12201}
12202
12203static rb_node_argspush_t *
12204rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12205{
12206 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12207 n->nd_head = nd_head;
12208 n->nd_body = nd_body;
12209
12210 return n;
12211}
12212
12213static rb_node_splat_t *
12214rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12215{
12216 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12217 n->nd_head = nd_head;
12218 n->operator_loc = *operator_loc;
12219
12220 return n;
12221}
12222
12223static rb_node_block_pass_t *
12224rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12225{
12226 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12227 n->forwarding = 0;
12228 n->nd_head = 0;
12229 n->nd_body = nd_body;
12230 n->operator_loc = *operator_loc;
12231
12232 return n;
12233}
12234
12235static rb_node_alias_t *
12236rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12237{
12238 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12239 n->nd_1st = nd_1st;
12240 n->nd_2nd = nd_2nd;
12241 n->keyword_loc = *keyword_loc;
12242
12243 return n;
12244}
12245
12246static rb_node_valias_t *
12247rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12248{
12249 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12250 n->nd_alias = nd_alias;
12251 n->nd_orig = nd_orig;
12252 n->keyword_loc = *keyword_loc;
12253
12254 return n;
12255}
12256
12257static rb_node_undef_t *
12258rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12259{
12260 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12261 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12262 n->keyword_loc = NULL_LOC;
12263 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12264
12265 return n;
12266}
12267
12268static rb_node_errinfo_t *
12269rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12270{
12271 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12272
12273 return n;
12274}
12275
12276static rb_node_defined_t *
12277rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12278{
12279 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12280 n->nd_head = nd_head;
12281 n->keyword_loc = *keyword_loc;
12282
12283 return n;
12284}
12285
12286static rb_node_postexe_t *
12287rb_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)
12288{
12289 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12290 n->nd_body = nd_body;
12291 n->keyword_loc = *keyword_loc;
12292 n->opening_loc = *opening_loc;
12293 n->closing_loc = *closing_loc;
12294
12295 return n;
12296}
12297
12298static rb_node_attrasgn_t *
12299rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12300{
12301 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12302 n->nd_recv = nd_recv;
12303 n->nd_mid = nd_mid;
12304 n->nd_args = nd_args;
12305
12306 return n;
12307}
12308
12309static rb_node_aryptn_t *
12310rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12311{
12312 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12313 n->nd_pconst = 0;
12314 n->pre_args = pre_args;
12315 n->rest_arg = rest_arg;
12316 n->post_args = post_args;
12317
12318 return n;
12319}
12320
12321static rb_node_hshptn_t *
12322rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12323{
12324 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12325 n->nd_pconst = nd_pconst;
12326 n->nd_pkwargs = nd_pkwargs;
12327 n->nd_pkwrestarg = nd_pkwrestarg;
12328
12329 return n;
12330}
12331
12332static rb_node_fndptn_t *
12333rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12334{
12335 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12336 n->nd_pconst = 0;
12337 n->pre_rest_arg = pre_rest_arg;
12338 n->args = args;
12339 n->post_rest_arg = post_rest_arg;
12340
12341 return n;
12342}
12343
12344static rb_node_line_t *
12345rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12346{
12347 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12348
12349 return n;
12350}
12351
12352static rb_node_file_t *
12353rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12354{
12355 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12356 n->path = rb_str_to_parser_string(p, str);
12357
12358 return n;
12359}
12360
12361static rb_node_encoding_t *
12362rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12363{
12364 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12365 n->enc = p->enc;
12366
12367 return n;
12368}
12369
12370static rb_node_cdecl_t *
12371rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12372{
12373 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12374 n->nd_vid = nd_vid;
12375 n->nd_value = nd_value;
12376 n->nd_else = nd_else;
12377 n->shareability = shareability;
12378
12379 return n;
12380}
12381
12382static rb_node_op_cdecl_t *
12383rb_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)
12384{
12385 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12386 n->nd_head = nd_head;
12387 n->nd_value = nd_value;
12388 n->nd_aid = nd_aid;
12389 n->shareability = shareability;
12390
12391 return n;
12392}
12393
12394static rb_node_error_t *
12395rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12396{
12397 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12398
12399 return n;
12400}
12401
12402static rb_node_break_t *
12403rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12404{
12405 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12406 n->nd_stts = nd_stts;
12407 n->nd_chain = 0;
12408 n->keyword_loc = *keyword_loc;
12409
12410 return n;
12411}
12412
12413static rb_node_next_t *
12414rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12415{
12416 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12417 n->nd_stts = nd_stts;
12418 n->nd_chain = 0;
12419 n->keyword_loc = *keyword_loc;
12420
12421 return n;
12422}
12423
12424static rb_node_redo_t *
12425rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12426{
12427 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12428 n->nd_chain = 0;
12429 n->keyword_loc = *keyword_loc;
12430
12431 return n;
12432}
12433
12434static rb_node_def_temp_t *
12435rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12436{
12437 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12438 n->save.numparam_save = 0;
12439 n->save.max_numparam = 0;
12440 n->save.ctxt = p->ctxt;
12441 n->nd_def = 0;
12442 n->nd_mid = 0;
12443
12444 return n;
12445}
12446
12447static rb_node_def_temp_t *
12448def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12449{
12450 n->save.numparam_save = numparam_push(p);
12451 n->save.max_numparam = p->max_numparam;
12452 return n;
12453}
12454
12455#ifndef RIPPER
12456static enum node_type
12457nodetype(NODE *node) /* for debug */
12458{
12459 return (enum node_type)nd_type(node);
12460}
12461
12462static int
12463nodeline(NODE *node)
12464{
12465 return nd_line(node);
12466}
12467#endif
12468
12469static NODE*
12470newline_node(NODE *node)
12471{
12472 if (node) {
12473 node = remove_begin(node);
12474 nd_set_fl_newline(node);
12475 }
12476 return node;
12477}
12478
12479static void
12480fixpos(NODE *node, NODE *orig)
12481{
12482 if (!node) return;
12483 if (!orig) return;
12484 nd_set_line(node, nd_line(orig));
12485}
12486
12487static NODE*
12488block_append(struct parser_params *p, NODE *head, NODE *tail)
12489{
12490 NODE *end, *h = head, *nd;
12491
12492 if (tail == 0) return head;
12493
12494 if (h == 0) return tail;
12495 switch (nd_type(h)) {
12496 default:
12497 h = end = NEW_BLOCK(head, &head->nd_loc);
12498 head = end;
12499 break;
12500 case NODE_BLOCK:
12501 end = RNODE_BLOCK(h)->nd_end;
12502 break;
12503 }
12504
12505 nd = RNODE_BLOCK(end)->nd_head;
12506 switch (nd_type(nd)) {
12507 case NODE_RETURN:
12508 case NODE_BREAK:
12509 case NODE_NEXT:
12510 case NODE_REDO:
12511 case NODE_RETRY:
12512 rb_warning0L(nd_line(tail), "statement not reached");
12513 break;
12514
12515 default:
12516 break;
12517 }
12518
12519 if (!nd_type_p(tail, NODE_BLOCK)) {
12520 tail = NEW_BLOCK(tail, &tail->nd_loc);
12521 }
12522 RNODE_BLOCK(end)->nd_next = tail;
12523 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12524 nd_set_last_loc(head, nd_last_loc(tail));
12525 return head;
12526}
12527
12528/* append item to the list */
12529static NODE*
12530list_append(struct parser_params *p, NODE *list, NODE *item)
12531{
12532 NODE *last;
12533
12534 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12535 if (RNODE_LIST(list)->nd_next) {
12536 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12537 }
12538 else {
12539 last = list;
12540 }
12541
12542 RNODE_LIST(list)->as.nd_alen += 1;
12543 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12544 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12545
12546 nd_set_last_loc(list, nd_last_loc(item));
12547
12548 return list;
12549}
12550
12551/* concat two lists */
12552static NODE*
12553list_concat(NODE *head, NODE *tail)
12554{
12555 NODE *last;
12556
12557 if (RNODE_LIST(head)->nd_next) {
12558 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12559 }
12560 else {
12561 last = head;
12562 }
12563
12564 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12565 RNODE_LIST(last)->nd_next = tail;
12566 if (RNODE_LIST(tail)->nd_next) {
12567 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12568 }
12569 else {
12570 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12571 }
12572
12573 nd_set_last_loc(head, nd_last_loc(tail));
12574
12575 return head;
12576}
12577
12578static int
12579literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12580{
12581 if (!tail) return 1;
12582 if (!rb_parser_enc_compatible(p, head, tail)) {
12583 compile_error(p, "string literal encodings differ (%s / %s)",
12584 rb_enc_name(rb_parser_str_get_encoding(head)),
12585 rb_enc_name(rb_parser_str_get_encoding(tail)));
12586 rb_parser_str_resize(p, head, 0);
12587 rb_parser_str_resize(p, tail, 0);
12588 return 0;
12589 }
12590 rb_parser_str_buf_append(p, head, tail);
12591 return 1;
12592}
12593
12594static rb_parser_string_t *
12595string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12596{
12597 if (htype != NODE_DSTR) return NULL;
12598 if (RNODE_DSTR(head)->nd_next) {
12599 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12600 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12601 }
12602 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12603 ASSUME(lit);
12604 return lit;
12605}
12606
12607#ifndef RIPPER
12608static rb_parser_string_t *
12609rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12610{
12611 rb_parser_string_t *copy;
12612 if (!orig) return NULL;
12613 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12614 copy->coderange = orig->coderange;
12615 copy->enc = orig->enc;
12616 return copy;
12617}
12618#endif
12619
12620/* concat two string literals */
12621static NODE *
12622literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12623{
12624 enum node_type htype;
12625 rb_parser_string_t *lit;
12626
12627 if (!head) return tail;
12628 if (!tail) return head;
12629
12630 htype = nd_type(head);
12631 if (htype == NODE_EVSTR) {
12632 head = new_dstr(p, head, loc);
12633 htype = NODE_DSTR;
12634 }
12635 if (p->heredoc_indent > 0) {
12636 switch (htype) {
12637 case NODE_STR:
12638 head = str2dstr(p, head);
12639 case NODE_DSTR:
12640 return list_append(p, head, tail);
12641 default:
12642 break;
12643 }
12644 }
12645 switch (nd_type(tail)) {
12646 case NODE_STR:
12647 if ((lit = string_literal_head(p, htype, head)) != false) {
12648 htype = NODE_STR;
12649 }
12650 else {
12651 lit = RNODE_DSTR(head)->string;
12652 }
12653 if (htype == NODE_STR) {
12654 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12655 error:
12656 rb_discard_node(p, head);
12657 rb_discard_node(p, tail);
12658 return 0;
12659 }
12660 rb_discard_node(p, tail);
12661 }
12662 else {
12663 list_append(p, head, tail);
12664 }
12665 break;
12666
12667 case NODE_DSTR:
12668 if (htype == NODE_STR) {
12669 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12670 goto error;
12671 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12672 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12673 RNODE_STR(head)->string = NULL;
12674 rb_discard_node(p, head);
12675 head = tail;
12676 }
12677 else if (!RNODE_DSTR(tail)->string) {
12678 append:
12679 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12680 if (!RNODE_DSTR(head)->nd_next) {
12681 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12682 }
12683 else if (RNODE_DSTR(tail)->nd_next) {
12684 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12685 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12686 }
12687 rb_discard_node(p, tail);
12688 }
12689 else if ((lit = string_literal_head(p, htype, head)) != false) {
12690 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12691 goto error;
12692 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12693 RNODE_DSTR(tail)->string = 0;
12694 goto append;
12695 }
12696 else {
12697 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));
12698 RNODE_DSTR(tail)->string = 0;
12699 }
12700 break;
12701
12702 case NODE_EVSTR:
12703 if (htype == NODE_STR) {
12704 head = str2dstr(p, head);
12705 RNODE_DSTR(head)->as.nd_alen = 1;
12706 }
12707 list_append(p, head, tail);
12708 break;
12709 }
12710 return head;
12711}
12712
12713static void
12714nd_copy_flag(NODE *new_node, NODE *old_node)
12715{
12716 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12717 nd_set_line(new_node, nd_line(old_node));
12718 new_node->nd_loc = old_node->nd_loc;
12719 new_node->node_id = old_node->node_id;
12720}
12721
12722static NODE *
12723str2dstr(struct parser_params *p, NODE *node)
12724{
12725 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12726 nd_copy_flag(new_node, node);
12727 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12728 RNODE_DSTR(new_node)->as.nd_alen = 0;
12729 RNODE_DSTR(new_node)->nd_next = 0;
12730 RNODE_STR(node)->string = 0;
12731
12732 return new_node;
12733}
12734
12735static NODE *
12736str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12737{
12738 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12739 nd_copy_flag(new_node, node);
12740 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12741 RNODE_REGX(new_node)->options = options;
12742 nd_set_loc(new_node, loc);
12743 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12744 RNODE_REGX(new_node)->content_loc = *content_loc;
12745 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12746 RNODE_STR(node)->string = 0;
12747
12748 return new_node;
12749}
12750
12751static NODE *
12752evstr2dstr(struct parser_params *p, NODE *node)
12753{
12754 if (nd_type_p(node, NODE_EVSTR)) {
12755 node = new_dstr(p, node, &node->nd_loc);
12756 }
12757 return node;
12758}
12759
12760static NODE *
12761new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12762{
12763 NODE *head = node;
12764
12765 if (node) {
12766 switch (nd_type(node)) {
12767 case NODE_STR:
12768 return str2dstr(p, node);
12769 case NODE_DSTR:
12770 break;
12771 case NODE_EVSTR:
12772 return node;
12773 }
12774 }
12775 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12776}
12777
12778static NODE *
12779new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12780{
12781 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12782 return list_append(p, dstr, node);
12783}
12784
12785static NODE *
12786call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12787 const YYLTYPE *op_loc, const YYLTYPE *loc)
12788{
12789 NODE *expr;
12790 value_expr(p, recv);
12791 value_expr(p, arg1);
12792 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12793 nd_set_line(expr, op_loc->beg_pos.lineno);
12794 return expr;
12795}
12796
12797static NODE *
12798call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12799{
12800 NODE *opcall;
12801 value_expr(p, recv);
12802 opcall = NEW_OPCALL(recv, id, 0, loc);
12803 nd_set_line(opcall, op_loc->beg_pos.lineno);
12804 return opcall;
12805}
12806
12807static NODE *
12808new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12809{
12810 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12811 nd_set_line(qcall, op_loc->beg_pos.lineno);
12812 return qcall;
12813}
12814
12815static NODE*
12816new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12817{
12818 NODE *ret;
12819 if (block) block_dup_check(p, args, block);
12820 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12821 if (block) ret = method_add_block(p, ret, block, loc);
12822 fixpos(ret, recv);
12823 return ret;
12824}
12825
12826static rb_locations_lambda_body_t*
12827new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12828{
12829 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12830 body->node = node;
12831 body->opening_loc = *opening_loc;
12832 body->closing_loc = *closing_loc;
12833 return body;
12834}
12835
12836static NODE *
12837command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12838{
12839 NODE **body;
12840 enum node_type type = nd_type(m);
12841 switch (type) {
12842 case NODE_YIELD:
12843 compile_error(p, "block given to yield");
12844 return m;
12845 case NODE_RETURN:
12846 body = &RNODE_RETURN(m)->nd_stts;
12847 break;
12848 case NODE_BREAK:
12849 case NODE_NEXT:
12850 body = &RNODE_EXITS(m)->nd_stts;
12851 break;
12852 case NODE_REDO: /* `redo` has no argument */
12853 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12854 return m;
12855 case NODE_RETRY: /* `retry` has no argument */
12856 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12857 return m;
12858 default:
12859 block_dup_check(p, get_nd_args(p, m), b);
12860 return method_add_block(p, m, b, loc);
12861 }
12862 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12863 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12864 *body = command_add_block(p, *body, b, &b_loc);
12865 m->nd_loc.end_pos = loc->end_pos;
12866 return m;
12867}
12868
12869#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12870
12871static NODE*
12872last_expr_once_body(NODE *node)
12873{
12874 if (!node) return 0;
12875 return nd_once_body(node);
12876}
12877
12878static NODE*
12879match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12880{
12881 NODE *n;
12882 int line = op_loc->beg_pos.lineno;
12883
12884 value_expr(p, node1);
12885 value_expr(p, node2);
12886
12887 if ((n = last_expr_once_body(node1)) != 0) {
12888 switch (nd_type(n)) {
12889 case NODE_DREGX:
12890 {
12891 NODE *match = NEW_MATCH2(node1, node2, loc);
12892 nd_set_line(match, line);
12893 return match;
12894 }
12895
12896 case NODE_REGX:
12897 {
12898 const VALUE lit = rb_node_regx_string_val(n);
12899 if (!NIL_P(lit)) {
12900 NODE *match = NEW_MATCH2(node1, node2, loc);
12901 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12902 nd_set_line(match, line);
12903 return match;
12904 }
12905 }
12906 }
12907 }
12908
12909 if ((n = last_expr_once_body(node2)) != 0) {
12910 NODE *match3;
12911
12912 switch (nd_type(n)) {
12913 case NODE_DREGX:
12914 match3 = NEW_MATCH3(node2, node1, loc);
12915 return match3;
12916 }
12917 }
12918
12919 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12920 nd_set_line(n, line);
12921 return n;
12922}
12923
12924# if WARN_PAST_SCOPE
12925static int
12926past_dvar_p(struct parser_params *p, ID id)
12927{
12928 struct vtable *past = p->lvtbl->past;
12929 while (past) {
12930 if (vtable_included(past, id)) return 1;
12931 past = past->prev;
12932 }
12933 return 0;
12934}
12935# endif
12936
12937static int
12938numparam_nested_p(struct parser_params *p)
12939{
12940 struct local_vars *local = p->lvtbl;
12941 NODE *outer = local->numparam.outer;
12942 NODE *inner = local->numparam.inner;
12943 if (outer || inner) {
12944 NODE *used = outer ? outer : inner;
12945 compile_error(p, "numbered parameter is already used in %s block\n"
12946 "%s:%d: numbered parameter is already used here",
12947 outer ? "outer" : "inner",
12948 p->ruby_sourcefile, nd_line(used));
12949 parser_show_error_line(p, &used->nd_loc);
12950 return 1;
12951 }
12952 return 0;
12953}
12954
12955static int
12956numparam_used_p(struct parser_params *p)
12957{
12958 NODE *numparam = p->lvtbl->numparam.current;
12959 if (numparam) {
12960 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12961 "%s:%d: numbered parameter is already used here",
12962 p->ruby_sourcefile, nd_line(numparam));
12963 parser_show_error_line(p, &numparam->nd_loc);
12964 return 1;
12965 }
12966 return 0;
12967}
12968
12969static int
12970it_used_p(struct parser_params *p)
12971{
12972 NODE *it = p->lvtbl->it;
12973 if (it) {
12974 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12975 "%s:%d: 'it' is already used here",
12976 p->ruby_sourcefile, nd_line(it));
12977 parser_show_error_line(p, &it->nd_loc);
12978 return 1;
12979 }
12980 return 0;
12981}
12982
12983static NODE*
12984gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12985{
12986 ID *vidp = NULL;
12987 NODE *node;
12988 switch (id) {
12989 case keyword_self:
12990 return NEW_SELF(loc);
12991 case keyword_nil:
12992 return NEW_NIL(loc);
12993 case keyword_true:
12994 return NEW_TRUE(loc);
12995 case keyword_false:
12996 return NEW_FALSE(loc);
12997 case keyword__FILE__:
12998 {
12999 VALUE file = p->ruby_sourcefile_string;
13000 if (NIL_P(file))
13001 file = rb_str_new(0, 0);
13002 node = NEW_FILE(file, loc);
13003 }
13004 return node;
13005 case keyword__LINE__:
13006 return NEW_LINE(loc);
13007 case keyword__ENCODING__:
13008 return NEW_ENCODING(loc);
13009
13010 }
13011 switch (id_type(id)) {
13012 case ID_LOCAL:
13013 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13014 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13015 if (vidp) *vidp |= LVAR_USED;
13016 node = NEW_DVAR(id, loc);
13017 return node;
13018 }
13019 if (local_id_ref(p, id, &vidp)) {
13020 if (vidp) *vidp |= LVAR_USED;
13021 node = NEW_LVAR(id, loc);
13022 return node;
13023 }
13024 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13025 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13026 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13027 node = NEW_DVAR(id, loc);
13028 struct local_vars *local = p->lvtbl;
13029 if (!local->numparam.current) local->numparam.current = node;
13030 return node;
13031 }
13032# if WARN_PAST_SCOPE
13033 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13034 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13035 }
13036# endif
13037 /* method call without arguments */
13038 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13039 if (numparam_used_p(p)) return 0;
13040 if (p->max_numparam == ORDINAL_PARAM) {
13041 compile_error(p, "ordinary parameter is defined");
13042 return 0;
13043 }
13044 if (!p->it_id) {
13045 p->it_id = idItImplicit;
13046 vtable_add(p->lvtbl->args, p->it_id);
13047 }
13048 NODE *node = NEW_DVAR(p->it_id, loc);
13049 if (!p->lvtbl->it) p->lvtbl->it = node;
13050 return node;
13051 }
13052 return NEW_VCALL(id, loc);
13053 case ID_GLOBAL:
13054 return NEW_GVAR(id, loc);
13055 case ID_INSTANCE:
13056 return NEW_IVAR(id, loc);
13057 case ID_CONST:
13058 return NEW_CONST(id, loc);
13059 case ID_CLASS:
13060 return NEW_CVAR(id, loc);
13061 }
13062 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13063 return 0;
13064}
13065
13066static rb_node_opt_arg_t *
13067opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13068{
13069 rb_node_opt_arg_t *opts = opt_list;
13070 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13071
13072 while (opts->nd_next) {
13073 opts = opts->nd_next;
13074 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13075 }
13076 opts->nd_next = opt;
13077
13078 return opt_list;
13079}
13080
13081static rb_node_kw_arg_t *
13082kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13083{
13084 if (kwlist) {
13085 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13086 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13087 }
13088 return kwlist;
13089}
13090
13091static NODE *
13092new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13093{
13094 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13095 p->ctxt.has_trailing_semicolon = 0;
13096
13097 NODE *n = expr;
13098 while (n) {
13099 if (nd_type_p(n, NODE_BEGIN)) {
13100 n = RNODE_BEGIN(n)->nd_body;
13101 }
13102 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13103 n = RNODE_BLOCK(n)->nd_head;
13104 }
13105 else {
13106 break;
13107 }
13108 }
13109
13110 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13111 NODE *block = NEW_BLOCK(expr, loc);
13112 return NEW_DEFINED(block, loc, keyword_loc);
13113 }
13114
13115 return NEW_DEFINED(n, loc, keyword_loc);
13116}
13117
13118static NODE*
13119str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13120{
13121 VALUE lit;
13122 rb_parser_string_t *str = RNODE_STR(node)->string;
13123 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13124 yyerror1(loc, "invalid symbol");
13125 lit = STR_NEW0();
13126 }
13127 else {
13128 lit = rb_str_new_parser_string(str);
13129 }
13130 return NEW_SYM(lit, loc);
13131}
13132
13133static NODE*
13134symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13135{
13136 enum node_type type = nd_type(symbol);
13137 switch (type) {
13138 case NODE_DSTR:
13139 nd_set_type(symbol, NODE_DSYM);
13140 break;
13141 case NODE_STR:
13142 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13143 break;
13144 default:
13145 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13146 }
13147 return list_append(p, symbols, symbol);
13148}
13149
13150static void
13151dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13152{
13153 if (dreg->string) {
13154 reg_fragment_setenc(p, dreg->string, options);
13155 }
13156 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13157 NODE *frag = list->nd_head;
13158 if (nd_type_p(frag, NODE_STR)) {
13159 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13160 }
13161 else if (nd_type_p(frag, NODE_DSTR)) {
13162 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13163 }
13164 }
13165}
13166
13167static NODE *
13168new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13169{
13170 if (!node) {
13171 /* Check string is valid regex */
13172 rb_parser_string_t *str = STRING_NEW0();
13173 reg_compile(p, str, options);
13174 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13175 return node;
13176 }
13177 switch (nd_type(node)) {
13178 case NODE_STR:
13179 {
13180 /* Check string is valid regex */
13181 reg_compile(p, RNODE_STR(node)->string, options);
13182 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13183 }
13184 break;
13185 default:
13186 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13187 /* fall through */
13188 case NODE_DSTR:
13189 nd_set_type(node, NODE_DREGX);
13190 nd_set_loc(node, loc);
13191 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13192 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13193 if (dreg->nd_next) {
13194 dregex_fragment_setenc(p, dreg, options);
13195 }
13196 if (options & RE_OPTION_ONCE) {
13197 node = NEW_ONCE(node, loc);
13198 }
13199 break;
13200 }
13201 return node;
13202}
13203
13204static rb_node_kw_arg_t *
13205new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13206{
13207 if (!k) return 0;
13208 return NEW_KW_ARG((k), loc);
13209}
13210
13211static NODE *
13212new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13213{
13214 if (!node) {
13215 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13216 return xstr;
13217 }
13218 switch (nd_type(node)) {
13219 case NODE_STR:
13220 nd_set_type(node, NODE_XSTR);
13221 nd_set_loc(node, loc);
13222 break;
13223 case NODE_DSTR:
13224 nd_set_type(node, NODE_DXSTR);
13225 nd_set_loc(node, loc);
13226 break;
13227 default:
13228 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13229 break;
13230 }
13231 return node;
13232}
13233
13234static const
13235struct st_hash_type literal_type = {
13236 literal_cmp,
13237 literal_hash,
13238};
13239
13240static int nd_type_st_key_enable_p(NODE *node);
13241
13242static void
13243check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13244{
13245 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13246 if (!arg || !p->case_labels) return;
13247 if (!nd_type_st_key_enable_p(arg)) return;
13248
13249 if (p->case_labels == CHECK_LITERAL_WHEN) {
13250 p->case_labels = st_init_table(&literal_type);
13251 }
13252 else {
13253 st_data_t line;
13254 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13255 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13256 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13257 return;
13258 }
13259 }
13260 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13261}
13262
13263#ifdef RIPPER
13264static int
13265id_is_var(struct parser_params *p, ID id)
13266{
13267 if (is_notop_id(id)) {
13268 switch (id & ID_SCOPE_MASK) {
13269 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13270 return 1;
13271 case ID_LOCAL:
13272 if (dyna_in_block(p)) {
13273 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13274 }
13275 if (local_id(p, id)) return 1;
13276 /* method call without arguments */
13277 return 0;
13278 }
13279 }
13280 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13281 return 0;
13282}
13283#endif
13284
13285static inline enum lex_state_e
13286parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13287{
13288 if (p->debug) {
13289 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13290 }
13291 return p->lex.state = ls;
13292}
13293
13294#ifndef RIPPER
13295static void
13296flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13297{
13298 VALUE mesg = p->debug_buffer;
13299
13300 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13301 p->debug_buffer = Qnil;
13302 rb_io_puts(1, &mesg, out);
13303 }
13304 if (!NIL_P(str) && RSTRING_LEN(str)) {
13305 rb_io_write(p->debug_output, str);
13306 }
13307}
13308
13309static const char rb_parser_lex_state_names[][8] = {
13310 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13311 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13312 "LABEL", "LABELED","FITEM",
13313};
13314
13315static VALUE
13316append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13317{
13318 int i, sep = 0;
13319 unsigned int mask = 1;
13320 static const char none[] = "NONE";
13321
13322 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13323 if ((unsigned)state & mask) {
13324 if (sep) {
13325 rb_str_cat(buf, "|", 1);
13326 }
13327 sep = 1;
13328 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13329 }
13330 }
13331 if (!sep) {
13332 rb_str_cat(buf, none, sizeof(none)-1);
13333 }
13334 return buf;
13335}
13336
13337enum lex_state_e
13338rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13339 enum lex_state_e to, int line)
13340{
13341 VALUE mesg;
13342 mesg = rb_str_new_cstr("lex_state: ");
13343 append_lex_state_name(p, from, mesg);
13344 rb_str_cat_cstr(mesg, " -> ");
13345 append_lex_state_name(p, to, mesg);
13346 rb_str_catf(mesg, " at line %d\n", line);
13347 flush_debug_buffer(p, p->debug_output, mesg);
13348 return to;
13349}
13350
13351VALUE
13352rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13353{
13354 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13355}
13356
13357static void
13358append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13359{
13360 if (stack == 0) {
13361 rb_str_cat_cstr(mesg, "0");
13362 }
13363 else {
13364 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13365 for (; mask && !(stack & mask); mask >>= 1) continue;
13366 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13367 }
13368}
13369
13370void
13371rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13372 const char *name, int line)
13373{
13374 VALUE mesg = rb_sprintf("%s: ", name);
13375 append_bitstack_value(p, stack, mesg);
13376 rb_str_catf(mesg, " at line %d\n", line);
13377 flush_debug_buffer(p, p->debug_output, mesg);
13378}
13379
13380void
13381rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13382{
13383 va_list ap;
13384 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13385
13386 va_start(ap, fmt);
13387 rb_str_vcatf(mesg, fmt, ap);
13388 va_end(ap);
13389 yyerror0(RSTRING_PTR(mesg));
13390 RB_GC_GUARD(mesg);
13391
13392 mesg = rb_str_new(0, 0);
13393 append_lex_state_name(p, p->lex.state, mesg);
13394 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13395 rb_str_resize(mesg, 0);
13396 append_bitstack_value(p, p->cond_stack, mesg);
13397 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13398 rb_str_resize(mesg, 0);
13399 append_bitstack_value(p, p->cmdarg_stack, mesg);
13400 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13401 if (p->debug_output == rb_ractor_stdout())
13402 p->debug_output = rb_ractor_stderr();
13403 p->debug = TRUE;
13404}
13405
13406static YYLTYPE *
13407rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13408{
13409 yylloc->beg_pos.lineno = sourceline;
13410 yylloc->beg_pos.column = beg_pos;
13411 yylloc->end_pos.lineno = sourceline;
13412 yylloc->end_pos.column = end_pos;
13413 return yylloc;
13414}
13415
13416YYLTYPE *
13417rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13418{
13419 int sourceline = here->sourceline;
13420 int beg_pos = (int)here->offset - here->quote
13421 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13422 int end_pos = (int)here->offset + here->length + here->quote;
13423
13424 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13425}
13426
13427YYLTYPE *
13428rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13429{
13430 yylloc->beg_pos.lineno = p->delayed.beg_line;
13431 yylloc->beg_pos.column = p->delayed.beg_col;
13432 yylloc->end_pos.lineno = p->delayed.end_line;
13433 yylloc->end_pos.column = p->delayed.end_col;
13434
13435 return yylloc;
13436}
13437
13438YYLTYPE *
13439rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13440{
13441 int sourceline = p->ruby_sourceline;
13442 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13443 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13444 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13445}
13446
13447YYLTYPE *
13448rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13449{
13450 yylloc->end_pos = yylloc->beg_pos;
13451
13452 return yylloc;
13453}
13454
13455YYLTYPE *
13456rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13457{
13458 int sourceline = p->ruby_sourceline;
13459 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13460 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13461 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13462}
13463
13464YYLTYPE *
13465rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13466{
13467 int sourceline = p->ruby_sourceline;
13468 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13469 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13470 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13471}
13472#endif /* !RIPPER */
13473
13474static int
13475assignable0(struct parser_params *p, ID id, const char **err)
13476{
13477 if (!id) return -1;
13478 switch (id) {
13479 case keyword_self:
13480 *err = "Can't change the value of self";
13481 return -1;
13482 case keyword_nil:
13483 *err = "Can't assign to nil";
13484 return -1;
13485 case keyword_true:
13486 *err = "Can't assign to true";
13487 return -1;
13488 case keyword_false:
13489 *err = "Can't assign to false";
13490 return -1;
13491 case keyword__FILE__:
13492 *err = "Can't assign to __FILE__";
13493 return -1;
13494 case keyword__LINE__:
13495 *err = "Can't assign to __LINE__";
13496 return -1;
13497 case keyword__ENCODING__:
13498 *err = "Can't assign to __ENCODING__";
13499 return -1;
13500 }
13501 switch (id_type(id)) {
13502 case ID_LOCAL:
13503 if (dyna_in_block(p)) {
13504 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13505 compile_error(p, "Can't assign to numbered parameter _%d",
13506 NUMPARAM_ID_TO_IDX(id));
13507 return -1;
13508 }
13509 if (dvar_curr(p, id)) return NODE_DASGN;
13510 if (dvar_defined(p, id)) return NODE_DASGN;
13511 if (local_id(p, id)) return NODE_LASGN;
13512 dyna_var(p, id);
13513 return NODE_DASGN;
13514 }
13515 else {
13516 if (!local_id(p, id)) local_var(p, id);
13517 return NODE_LASGN;
13518 }
13519 break;
13520 case ID_GLOBAL: return NODE_GASGN;
13521 case ID_INSTANCE: return NODE_IASGN;
13522 case ID_CONST:
13523 if (!p->ctxt.in_def) return NODE_CDECL;
13524 *err = "dynamic constant assignment";
13525 return -1;
13526 case ID_CLASS: return NODE_CVASGN;
13527 default:
13528 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13529 }
13530 return -1;
13531}
13532
13533static NODE*
13534assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13535{
13536 const char *err = 0;
13537 int node_type = assignable0(p, id, &err);
13538 switch (node_type) {
13539 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13540 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13541 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13542 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13543 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13544 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13545 }
13546/* TODO: FIXME */
13547#ifndef RIPPER
13548 if (err) yyerror1(loc, err);
13549#else
13550 if (err) set_value(assign_error(p, err, p->s_lvalue));
13551#endif
13552 return NEW_ERROR(loc);
13553}
13554
13555static int
13556is_private_local_id(struct parser_params *p, ID name)
13557{
13558 VALUE s;
13559 if (name == idUScore) return 1;
13560 if (!is_local_id(name)) return 0;
13561 s = rb_id2str(name);
13562 if (!s) return 0;
13563 return RSTRING_PTR(s)[0] == '_';
13564}
13565
13566static int
13567shadowing_lvar_0(struct parser_params *p, ID name)
13568{
13569 if (dyna_in_block(p)) {
13570 if (dvar_curr(p, name)) {
13571 if (is_private_local_id(p, name)) return 1;
13572 yyerror0("duplicated argument name");
13573 }
13574 else if (dvar_defined(p, name) || local_id(p, name)) {
13575 vtable_add(p->lvtbl->vars, name);
13576 if (p->lvtbl->used) {
13577 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13578 }
13579 return 0;
13580 }
13581 }
13582 else {
13583 if (local_id(p, name)) {
13584 if (is_private_local_id(p, name)) return 1;
13585 yyerror0("duplicated argument name");
13586 }
13587 }
13588 return 1;
13589}
13590
13591static ID
13592shadowing_lvar(struct parser_params *p, ID name)
13593{
13594 shadowing_lvar_0(p, name);
13595 return name;
13596}
13597
13598static void
13599new_bv(struct parser_params *p, ID name)
13600{
13601 if (!name) return;
13602 if (!is_local_id(name)) {
13603 compile_error(p, "invalid local variable - %"PRIsVALUE,
13604 rb_id2str(name));
13605 return;
13606 }
13607 if (!shadowing_lvar_0(p, name)) return;
13608 dyna_var(p, name);
13609 ID *vidp = 0;
13610 if (dvar_defined_ref(p, name, &vidp)) {
13611 if (vidp) *vidp |= LVAR_USED;
13612 }
13613}
13614
13615static void
13616aryset_check(struct parser_params *p, NODE *args)
13617{
13618 NODE *block = 0, *kwds = 0;
13619 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13620 block = RNODE_BLOCK_PASS(args)->nd_body;
13621 args = RNODE_BLOCK_PASS(args)->nd_head;
13622 }
13623 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13624 args = RNODE_ARGSCAT(args)->nd_body;
13625 }
13626 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13627 kwds = RNODE_ARGSPUSH(args)->nd_body;
13628 }
13629 else {
13630 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13631 next = RNODE_LIST(next)->nd_next) {
13632 kwds = RNODE_LIST(next)->nd_head;
13633 }
13634 }
13635 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13636 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13637 }
13638 if (block) {
13639 yyerror1(&block->nd_loc, "block arg given in index assignment");
13640 }
13641}
13642
13643static NODE *
13644aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13645{
13646 aryset_check(p, idx);
13647 return NEW_ATTRASGN(recv, tASET, idx, loc);
13648}
13649
13650static void
13651block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13652{
13653 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13654 compile_error(p, "both block arg and actual block given");
13655 }
13656}
13657
13658static NODE *
13659attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13660{
13661 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13662 return NEW_ATTRASGN(recv, id, 0, loc);
13663}
13664
13665static VALUE
13666rb_backref_error(struct parser_params *p, NODE *node)
13667{
13668#ifndef RIPPER
13669# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13670#else
13671# define ERR(...) rb_sprintf(__VA_ARGS__)
13672#endif
13673 switch (nd_type(node)) {
13674 case NODE_NTH_REF:
13675 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13676 case NODE_BACK_REF:
13677 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13678 }
13679#undef ERR
13680 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13681}
13682
13683static NODE *
13684arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13685{
13686 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13687 switch (nd_type(node1)) {
13688 case NODE_LIST:
13689 return list_append(p, node1, node2);
13690 case NODE_BLOCK_PASS:
13691 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13692 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13693 return node1;
13694 case NODE_ARGSPUSH:
13695 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13696 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13697 nd_set_type(node1, NODE_ARGSCAT);
13698 return node1;
13699 case NODE_ARGSCAT:
13700 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13701 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13702 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13703 return node1;
13704 }
13705 return NEW_ARGSPUSH(node1, node2, loc);
13706}
13707
13708static NODE *
13709arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13710{
13711 if (!node2) return node1;
13712 switch (nd_type(node1)) {
13713 case NODE_BLOCK_PASS:
13714 if (RNODE_BLOCK_PASS(node1)->nd_head)
13715 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13716 else
13717 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13718 return node1;
13719 case NODE_ARGSPUSH:
13720 if (!nd_type_p(node2, NODE_LIST)) break;
13721 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13722 nd_set_type(node1, NODE_ARGSCAT);
13723 return node1;
13724 case NODE_ARGSCAT:
13725 if (!nd_type_p(node2, NODE_LIST) ||
13726 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13727 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13728 return node1;
13729 }
13730 return NEW_ARGSCAT(node1, node2, loc);
13731}
13732
13733static NODE *
13734last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13735{
13736 NODE *n1;
13737 if ((n1 = splat_array(args)) != 0) {
13738 return list_append(p, n1, last_arg);
13739 }
13740 return arg_append(p, args, last_arg, loc);
13741}
13742
13743static NODE *
13744rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13745{
13746 NODE *n1;
13747 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13748 return list_concat(n1, rest_arg);
13749 }
13750 return arg_concat(p, args, rest_arg, loc);
13751}
13752
13753static NODE *
13754splat_array(NODE* node)
13755{
13756 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13757 if (nd_type_p(node, NODE_LIST)) return node;
13758 return 0;
13759}
13760
13761static void
13762mark_lvar_used(struct parser_params *p, NODE *rhs)
13763{
13764 ID *vidp = NULL;
13765 if (!rhs) return;
13766 switch (nd_type(rhs)) {
13767 case NODE_LASGN:
13768 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13769 if (vidp) *vidp |= LVAR_USED;
13770 }
13771 break;
13772 case NODE_DASGN:
13773 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13774 if (vidp) *vidp |= LVAR_USED;
13775 }
13776 break;
13777#if 0
13778 case NODE_MASGN:
13779 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13780 mark_lvar_used(p, rhs->nd_head);
13781 }
13782 break;
13783#endif
13784 }
13785}
13786
13787static int is_static_content(NODE *node);
13788
13789static NODE *
13790node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13791{
13792 if (!lhs) return 0;
13793
13794 switch (nd_type(lhs)) {
13795 case NODE_CDECL:
13796 case NODE_GASGN:
13797 case NODE_IASGN:
13798 case NODE_LASGN:
13799 case NODE_DASGN:
13800 case NODE_MASGN:
13801 case NODE_CVASGN:
13802 set_nd_value(p, lhs, rhs);
13803 nd_set_loc(lhs, loc);
13804 break;
13805
13806 case NODE_ATTRASGN:
13807 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13808 nd_set_loc(lhs, loc);
13809 break;
13810
13811 default:
13812 /* should not happen */
13813 break;
13814 }
13815
13816 return lhs;
13817}
13818
13819static NODE *
13820value_expr_check(struct parser_params *p, NODE *node)
13821{
13822 NODE *void_node = 0, *vn;
13823
13824 if (!node) {
13825 rb_warning0("empty expression");
13826 }
13827 while (node) {
13828 switch (nd_type(node)) {
13829 case NODE_ENSURE:
13830 vn = RNODE_ENSURE(node)->nd_head;
13831 node = RNODE_ENSURE(node)->nd_ensr;
13832 /* nd_ensr should not be NULL, check it out next */
13833 if (vn && (vn = value_expr_check(p, vn))) {
13834 goto found;
13835 }
13836 break;
13837
13838 case NODE_RESCUE:
13839 /* void only if all children are void */
13840 vn = RNODE_RESCUE(node)->nd_head;
13841 if (!vn || !(vn = value_expr_check(p, vn))) {
13842 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13843 }
13844 if (!void_node) void_node = vn;
13845 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13846 if (!nd_type_p(r, NODE_RESBODY)) {
13847 compile_error(p, "unexpected node");
13848 return NULL;
13849 }
13850 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13851 return NULL;
13852 }
13853 if (!void_node) void_node = vn;
13854 }
13855 node = RNODE_RESCUE(node)->nd_else;
13856 if (!node) return void_node;
13857 break;
13858
13859 case NODE_RETURN:
13860 case NODE_BREAK:
13861 case NODE_NEXT:
13862 case NODE_REDO:
13863 case NODE_RETRY:
13864 goto found;
13865
13866 case NODE_CASE:
13867 case NODE_CASE2:
13868 for (node = RNODE_CASE(node)->nd_body;
13869 node && nd_type_p(node, NODE_WHEN);
13870 node = RNODE_WHEN(node)->nd_next) {
13871 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13872 return NULL;
13873 }
13874 if (!void_node) void_node = vn;
13875 }
13876 break;
13877
13878 case NODE_CASE3:
13879 {
13880 NODE *in = RNODE_CASE3(node)->nd_body;
13881 if (!in || !nd_type_p(in, NODE_IN)) {
13882 compile_error(p, "unexpected node");
13883 return NULL;
13884 }
13885 if (!RNODE_IN(in)->nd_body) {
13886 /* single line pattern matching with "=>" operator */
13887 goto found;
13888 }
13889 do {
13890 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13891 if (!vn) return NULL;
13892 if (!void_node) void_node = vn;
13893 in = RNODE_IN(in)->nd_next;
13894 } while (in && nd_type_p(in, NODE_IN));
13895 node = in; /* else */
13896 }
13897 break;
13898
13899 case NODE_BLOCK:
13900 while (RNODE_BLOCK(node)->nd_next) {
13901 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13902 if (vn) return vn;
13903 node = RNODE_BLOCK(node)->nd_next;
13904 }
13905 node = RNODE_BLOCK(node)->nd_head;
13906 break;
13907
13908 case NODE_BEGIN:
13909 node = RNODE_BEGIN(node)->nd_body;
13910 break;
13911
13912 case NODE_IF:
13913 case NODE_UNLESS:
13914 if (!RNODE_IF(node)->nd_body) {
13915 return NULL;
13916 }
13917 else if (!RNODE_IF(node)->nd_else) {
13918 return NULL;
13919 }
13920 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13921 if (!vn) return NULL;
13922 if (!void_node) void_node = vn;
13923 node = RNODE_IF(node)->nd_else;
13924 break;
13925
13926 case NODE_AND:
13927 case NODE_OR:
13928 /* The left operand was already checked for a value when logop()
13929 * built this node, so stop here instead of re-reporting the same
13930 * void value once per operator in a chain. */
13931 return NULL;
13932
13933 case NODE_LASGN:
13934 case NODE_DASGN:
13935 case NODE_MASGN:
13936 mark_lvar_used(p, node);
13937 return NULL;
13938
13939 default:
13940 return NULL;
13941 }
13942 }
13943
13944 return NULL;
13945
13946 found:
13947 /* return the first found node */
13948 return void_node ? void_node : node;
13949}
13950
13951static int
13952value_expr(struct parser_params *p, NODE *node)
13953{
13954 NODE *void_node = value_expr_check(p, node);
13955 if (void_node) {
13956 yyerror1(&void_node->nd_loc, "void value expression");
13957 /* or "control never reach"? */
13958 return FALSE;
13959 }
13960 return TRUE;
13961}
13962
13963static void
13964void_expr(struct parser_params *p, NODE *node)
13965{
13966 const char *useless = 0;
13967
13968 if (!RTEST(ruby_verbose)) return;
13969
13970 if (!node || !(node = nd_once_body(node))) return;
13971 switch (nd_type(node)) {
13972 case NODE_OPCALL:
13973 switch (RNODE_OPCALL(node)->nd_mid) {
13974 case '+':
13975 case '-':
13976 case '*':
13977 case '/':
13978 case '%':
13979 case tPOW:
13980 case tUPLUS:
13981 case tUMINUS:
13982 case '|':
13983 case '^':
13984 case '&':
13985 case tCMP:
13986 case '>':
13987 case tGEQ:
13988 case '<':
13989 case tLEQ:
13990 case tEQ:
13991 case tNEQ:
13992 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13993 break;
13994 }
13995 break;
13996
13997 case NODE_LVAR:
13998 case NODE_DVAR:
13999 case NODE_GVAR:
14000 case NODE_IVAR:
14001 case NODE_CVAR:
14002 case NODE_NTH_REF:
14003 case NODE_BACK_REF:
14004 useless = "a variable";
14005 break;
14006 case NODE_CONST:
14007 useless = "a constant";
14008 break;
14009 case NODE_SYM:
14010 case NODE_LINE:
14011 case NODE_FILE:
14012 case NODE_ENCODING:
14013 case NODE_INTEGER:
14014 case NODE_FLOAT:
14015 case NODE_RATIONAL:
14016 case NODE_IMAGINARY:
14017 case NODE_STR:
14018 case NODE_DSTR:
14019 case NODE_REGX:
14020 case NODE_DREGX:
14021 useless = "a literal";
14022 break;
14023 case NODE_COLON2:
14024 case NODE_COLON3:
14025 useless = "::";
14026 break;
14027 case NODE_DOT2:
14028 useless = "..";
14029 break;
14030 case NODE_DOT3:
14031 useless = "...";
14032 break;
14033 case NODE_SELF:
14034 useless = "self";
14035 break;
14036 case NODE_NIL:
14037 useless = "nil";
14038 break;
14039 case NODE_TRUE:
14040 useless = "true";
14041 break;
14042 case NODE_FALSE:
14043 useless = "false";
14044 break;
14045 case NODE_DEFINED:
14046 useless = "defined?";
14047 break;
14048 }
14049
14050 if (useless) {
14051 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14052 }
14053}
14054
14055/* warns useless use of block and returns the last statement node */
14056static NODE *
14057void_stmts(struct parser_params *p, NODE *node)
14058{
14059 NODE *const n = node;
14060 if (!RTEST(ruby_verbose)) return n;
14061 if (!node) return n;
14062 if (!nd_type_p(node, NODE_BLOCK)) return n;
14063
14064 while (RNODE_BLOCK(node)->nd_next) {
14065 void_expr(p, RNODE_BLOCK(node)->nd_head);
14066 node = RNODE_BLOCK(node)->nd_next;
14067 }
14068 return RNODE_BLOCK(node)->nd_head;
14069}
14070
14071static NODE *
14072remove_begin(NODE *node)
14073{
14074 NODE **n = &node, *n1 = node;
14075 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14076 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14077 }
14078 return node;
14079}
14080
14081static void
14082reduce_nodes(struct parser_params *p, NODE **body)
14083{
14084 NODE *node = *body;
14085
14086 if (!node) {
14087 *body = NEW_NIL(&NULL_LOC);
14088 return;
14089 }
14090#define subnodes(type, n1, n2) \
14091 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14092 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14093 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14094
14095 while (node) {
14096 int newline = (int)nd_fl_newline(node);
14097 switch (nd_type(node)) {
14098 case NODE_NIL:
14099 // Keep an explicit nil in a method's tail (value) position when it
14100 // is on its own line, so it still emits a :line event and records
14101 // line coverage for that line. [Bug #22302]
14102 if (newline) return;
14103 end:
14104 *body = 0;
14105 return;
14106 case NODE_BEGIN:
14107 *body = node = RNODE_BEGIN(node)->nd_body;
14108 if (newline && node) nd_set_fl_newline(node);
14109 continue;
14110 case NODE_BLOCK:
14111 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14112 break;
14113 case NODE_IF:
14114 case NODE_UNLESS:
14115 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14116 return;
14117 case NODE_CASE:
14118 body = &RNODE_CASE(node)->nd_body;
14119 break;
14120 case NODE_WHEN:
14121 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14122 break;
14123 case NODE_ENSURE:
14124 body = &RNODE_ENSURE(node)->nd_head;
14125 break;
14126 case NODE_RESCUE:
14127 newline = 0; // RESBODY should not be a NEWLINE
14128 if (RNODE_RESCUE(node)->nd_else) {
14129 body = &RNODE_RESCUE(node)->nd_resq;
14130 break;
14131 }
14132 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14133 break;
14134 default:
14135 return;
14136 }
14137 node = *body;
14138 if (newline && node) nd_set_fl_newline(node);
14139 }
14140
14141#undef subnodes
14142}
14143
14144static int
14145is_static_content(NODE *node)
14146{
14147 if (!node) return 1;
14148 switch (nd_type(node)) {
14149 case NODE_HASH:
14150 if (!(node = RNODE_HASH(node)->nd_head)) break;
14151 case NODE_LIST:
14152 do {
14153 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14154 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14155 case NODE_SYM:
14156 case NODE_REGX:
14157 case NODE_LINE:
14158 case NODE_FILE:
14159 case NODE_ENCODING:
14160 case NODE_INTEGER:
14161 case NODE_FLOAT:
14162 case NODE_RATIONAL:
14163 case NODE_IMAGINARY:
14164 case NODE_STR:
14165 case NODE_NIL:
14166 case NODE_TRUE:
14167 case NODE_FALSE:
14168 case NODE_ZLIST:
14169 break;
14170 default:
14171 return 0;
14172 }
14173 return 1;
14174}
14175
14176static int
14177assign_in_cond(struct parser_params *p, NODE *node)
14178{
14179 switch (nd_type(node)) {
14180 case NODE_MASGN:
14181 case NODE_LASGN:
14182 case NODE_DASGN:
14183 case NODE_GASGN:
14184 case NODE_IASGN:
14185 case NODE_CVASGN:
14186 case NODE_CDECL:
14187 break;
14188
14189 default:
14190 return 0;
14191 }
14192
14193 if (!get_nd_value(p, node)) return 1;
14194 if (is_static_content(get_nd_value(p, node))) {
14195 /* reports always */
14196 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14197 }
14198 return 1;
14199}
14200
14201enum cond_type {
14202 COND_IN_OP,
14203 COND_IN_COND,
14204 COND_IN_FF
14205};
14206
14207#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14208 switch (t) { \
14209 case COND_IN_OP: break; \
14210 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14211 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14212 } \
14213} while (0)
14214
14215static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14216
14217static NODE*
14218range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14219{
14220 enum node_type type;
14221
14222 if (node == 0) return 0;
14223
14224 type = nd_type(node);
14225 value_expr(p, node);
14226 if (type == NODE_INTEGER) {
14227 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14228 ID lineno = rb_intern("$.");
14229 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14230 }
14231 return cond0(p, node, COND_IN_FF, loc, true);
14232}
14233
14234static NODE*
14235cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14236{
14237 if (node == 0) return 0;
14238 if (!(node = nd_once_body(node))) return 0;
14239 assign_in_cond(p, node);
14240
14241 switch (nd_type(node)) {
14242 case NODE_BEGIN:
14243 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14244 break;
14245
14246 case NODE_DSTR:
14247 case NODE_EVSTR:
14248 case NODE_STR:
14249 case NODE_FILE:
14250 SWITCH_BY_COND_TYPE(type, warn, "string ");
14251 break;
14252
14253 case NODE_REGX:
14254 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14255 nd_set_type(node, NODE_MATCH);
14256 break;
14257
14258 case NODE_DREGX:
14259 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14260
14261 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14262
14263 case NODE_BLOCK:
14264 {
14265 NODE *end = RNODE_BLOCK(node)->nd_end;
14266 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14267 if (top) top = node == end;
14268 *expr = cond0(p, *expr, type, loc, top);
14269 }
14270 break;
14271
14272 case NODE_AND:
14273 case NODE_OR:
14274 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14275 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14276 break;
14277
14278 case NODE_DOT2:
14279 case NODE_DOT3:
14280 if (!top) break;
14281 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14282 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14283 switch (nd_type(node)) {
14284 case NODE_DOT2:
14285 nd_set_type(node,NODE_FLIP2);
14286 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14287 (void)flip2;
14288 break;
14289 case NODE_DOT3:
14290 nd_set_type(node, NODE_FLIP3);
14291 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14292 (void)flip3;
14293 break;
14294 }
14295 break;
14296
14297 case NODE_SYM:
14298 case NODE_DSYM:
14299 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14300 break;
14301
14302 case NODE_LINE:
14303 case NODE_ENCODING:
14304 case NODE_INTEGER:
14305 case NODE_FLOAT:
14306 case NODE_RATIONAL:
14307 case NODE_IMAGINARY:
14308 SWITCH_BY_COND_TYPE(type, warning, "");
14309 break;
14310
14311 default:
14312 break;
14313 }
14314 return node;
14315}
14316
14317static NODE*
14318cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14319{
14320 if (node == 0) return 0;
14321 return cond0(p, node, COND_IN_COND, loc, true);
14322}
14323
14324static NODE*
14325method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14326{
14327 if (node == 0) return 0;
14328 return cond0(p, node, COND_IN_OP, loc, true);
14329}
14330
14331static NODE*
14332new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14333{
14334 YYLTYPE loc = {*pos, *pos};
14335 return NEW_NIL(&loc);
14336}
14337
14338static NODE*
14339new_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)
14340{
14341 if (!cc) return right;
14342 cc = cond0(p, cc, COND_IN_COND, loc, true);
14343 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14344}
14345
14346static NODE*
14347new_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)
14348{
14349 if (!cc) return right;
14350 cc = cond0(p, cc, COND_IN_COND, loc, true);
14351 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14352}
14353
14354#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))
14355
14356/* A cached logop tail is usable only if it is still a node of the chain's type
14357 * whose nd_2nd is the insertion point, i.e. not itself another node of that
14358 * type. This rejects a stale cache left over from an earlier parse. */
14359static int
14360logop_valid_tail(NODE *tail, enum node_type type)
14361{
14362 NODE *second;
14363 return tail && nd_type_p(tail, type) &&
14364 ((second = RNODE_AND(tail)->nd_2nd) == 0 || !nd_type_p(second, type));
14365}
14366
14367static NODE*
14368logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14369 const YYLTYPE *op_loc, const YYLTYPE *loc)
14370{
14371 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14372 NODE *op;
14373 /* Snapshot the cache from the previous logop() call before overwriting it. */
14374 NODE *prev_head = p->logop.head;
14375 NODE *prev_tail = p->logop.tail;
14376 NODE *node, *second;
14377 value_expr(p, left);
14378 if (left && nd_type_p(left, type)) {
14379 /* The insertion point is the far end of left's right branch.
14380 * Reusing the cached tail keeps a chain such as `a && a && ... && a`
14381 * linear instead of rescanning the whole branch on every operator. The
14382 * cache is validated (its nd_2nd is not another node of the same type)
14383 * so a stale entry falls back to the scan. */
14384 if (left == prev_head && logop_valid_tail(prev_tail, type)) {
14385 node = prev_tail;
14386 }
14387 else {
14388 node = left;
14389 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14390 node = second;
14391 }
14392 }
14393 second = RNODE_AND(node)->nd_2nd;
14394 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14395 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14396 left->nd_loc.end_pos = loc->end_pos;
14397 p->logop.head = left;
14398 p->logop.tail = RNODE_AND(node)->nd_2nd;
14399 return left;
14400 }
14401 op = NEW_AND_OR(type, left, right, loc, op_loc);
14402 nd_set_line(op, op_loc->beg_pos.lineno);
14403 /* Record where the next operator will extend op, mirroring the scan the
14404 * chained branch above would perform: if right is itself a chain of the
14405 * same type (from parentheses), its far end; otherwise op itself. */
14406 p->logop.head = op;
14407 if (right == prev_head && logop_valid_tail(prev_tail, type)) {
14408 p->logop.tail = prev_tail;
14409 }
14410 else if (right && nd_type_p(right, type)) {
14411 node = right;
14412 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14413 node = second;
14414 }
14415 p->logop.tail = node;
14416 }
14417 else {
14418 p->logop.tail = op;
14419 }
14420 return op;
14421}
14422
14423#undef NEW_AND_OR
14424
14425static void
14426no_blockarg(struct parser_params *p, NODE *node)
14427{
14428 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14429 compile_error(p, "block argument should not be given");
14430 }
14431}
14432
14433static NODE *
14434ret_args(struct parser_params *p, NODE *node)
14435{
14436 if (node) {
14437 no_blockarg(p, node);
14438 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14439 node = RNODE_LIST(node)->nd_head;
14440 }
14441 }
14442 return node;
14443}
14444
14445static NODE*
14446negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14447{
14448 switch (nd_type(node)) {
14449 case NODE_INTEGER:
14450 RNODE_INTEGER(node)->minus = TRUE;
14451 break;
14452 case NODE_FLOAT:
14453 RNODE_FLOAT(node)->minus = TRUE;
14454 break;
14455 case NODE_RATIONAL:
14456 RNODE_RATIONAL(node)->minus = TRUE;
14457 break;
14458 case NODE_IMAGINARY:
14459 RNODE_IMAGINARY(node)->minus = TRUE;
14460 break;
14461 }
14462 node->nd_loc = *loc;
14463 return node;
14464}
14465
14466static NODE *
14467arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14468{
14469 if (node2) {
14470 if (!node1) return (NODE *)node2;
14471 node2->nd_head = node1;
14472 nd_set_first_lineno(node2, nd_first_lineno(node1));
14473 nd_set_first_column(node2, nd_first_column(node1));
14474 return (NODE *)node2;
14475 }
14476 return node1;
14477}
14478
14479static bool
14480args_info_empty_p(struct rb_args_info *args)
14481{
14482 if (args->pre_args_num) return false;
14483 if (args->post_args_num) return false;
14484 if (args->rest_arg) return false;
14485 if (args->opt_args) return false;
14486 if (args->block_arg) return false;
14487 if (args->kw_args) return false;
14488 if (args->kw_rest_arg) return false;
14489 return true;
14490}
14491
14492static rb_node_args_t *
14493new_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)
14494{
14495 struct rb_args_info *args = &tail->nd_ainfo;
14496
14497 if (args->forwarding) {
14498 if (rest_arg) {
14499 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14500 return tail;
14501 }
14502 rest_arg = idFWD_REST;
14503 }
14504
14505 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14506 args->pre_init = pre_args ? pre_args->nd_next : 0;
14507
14508 args->post_args_num = post_args ? post_args->nd_plen : 0;
14509 args->post_init = post_args ? post_args->nd_next : 0;
14510 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14511
14512 args->rest_arg = rest_arg;
14513
14514 args->opt_args = opt_args;
14515
14516 nd_set_loc(RNODE(tail), loc);
14517
14518 return tail;
14519}
14520
14521static rb_node_args_t *
14522new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14523{
14524 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14525 struct rb_args_info *args = &node->nd_ainfo;
14526 if (p->error_p) return node;
14527
14528 if (block == idNil) {
14529 block = 0;
14530 args->no_blockarg = TRUE;
14531 }
14532 args->block_arg = block;
14533 args->kw_args = kw_args;
14534
14535 if (kw_args) {
14536 /*
14537 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14538 * variable order: k1, kr1, k2, &b, internal_id, krest
14539 * #=> <reorder>
14540 * variable order: kr1, k1, k2, internal_id, krest, &b
14541 */
14542 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14543 struct vtable *vtargs = p->lvtbl->args;
14544 rb_node_kw_arg_t *kwn = kw_args;
14545
14546 if (block) block = vtargs->tbl[vtargs->pos-1];
14547 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14548 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14549 while (kwn) {
14550 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14551 --kw_vars;
14552 --required_kw_vars;
14553 kwn = kwn->nd_next;
14554 }
14555
14556 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14557 ID vid = get_nd_vid(p, kwn->nd_body);
14558 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14559 *required_kw_vars++ = vid;
14560 }
14561 else {
14562 *kw_vars++ = vid;
14563 }
14564 }
14565
14566 arg_var(p, kw_bits);
14567 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14568 if (block) arg_var(p, block);
14569
14570 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14571 }
14572 else if (kw_rest_arg == idNil) {
14573 args->no_kwarg = 1;
14574 }
14575 else if (kw_rest_arg) {
14576 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14577 }
14578
14579 return node;
14580}
14581
14582static rb_node_args_t *
14583args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14584{
14585 if (max_numparam > NO_PARAM || it_id) {
14586 if (!args) {
14587 YYLTYPE loc = RUBY_INIT_YYLLOC();
14588 args = new_empty_args_tail(p, 0);
14589 nd_set_loc(RNODE(args), &loc);
14590 }
14591 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14592 }
14593 return args;
14594}
14595
14596static NODE*
14597new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14598{
14599 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14600
14601 if (pre_arg) {
14602 NODE *pre_args = NEW_LIST(pre_arg, loc);
14603 if (RNODE_ARYPTN(aryptn)->pre_args) {
14604 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14605 }
14606 else {
14607 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14608 }
14609 }
14610 return aryptn;
14611}
14612
14613static NODE*
14614new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14615{
14616 if (has_rest) {
14617 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14618 }
14619 else {
14620 rest_arg = NULL;
14621 }
14622 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14623
14624 return node;
14625}
14626
14627static NODE*
14628new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14629{
14630 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14631
14632 return fndptn;
14633}
14634
14635static NODE*
14636new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14637{
14638 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14639 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14640 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14641
14642 return node;
14643}
14644
14645static NODE*
14646new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14647{
14648 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14649 return hshptn;
14650}
14651
14652static NODE*
14653new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14654{
14655 NODE *node, *kw_rest_arg_node;
14656
14657 if (kw_rest_arg == idNil) {
14658 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14659 }
14660 else if (kw_rest_arg) {
14661 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14662 }
14663 else {
14664 kw_rest_arg_node = NULL;
14665 }
14666
14667 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14668
14669 return node;
14670}
14671
14672static NODE*
14673dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14674{
14675 if (!node) {
14676 return NEW_SYM(STR_NEW0(), loc);
14677 }
14678
14679 switch (nd_type(node)) {
14680 case NODE_DSTR:
14681 nd_set_type(node, NODE_DSYM);
14682 nd_set_loc(node, loc);
14683 break;
14684 case NODE_STR:
14685 node = str_to_sym_node(p, node, loc);
14686 break;
14687 default:
14688 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14689 break;
14690 }
14691 return node;
14692}
14693
14694static int
14695nd_type_st_key_enable_p(NODE *node)
14696{
14697 switch (nd_type(node)) {
14698 case NODE_INTEGER:
14699 case NODE_FLOAT:
14700 case NODE_RATIONAL:
14701 case NODE_IMAGINARY:
14702 case NODE_STR:
14703 case NODE_SYM:
14704 case NODE_REGX:
14705 case NODE_LINE:
14706 case NODE_FILE:
14707 case NODE_ENCODING:
14708 return true;
14709 default:
14710 return false;
14711 }
14712}
14713
14714static VALUE
14715nd_value(struct parser_params *p, NODE *node)
14716{
14717 switch (nd_type(node)) {
14718 case NODE_STR:
14719 return rb_node_str_string_val(node);
14720 case NODE_INTEGER:
14721 return rb_node_integer_literal_val(node);
14722 case NODE_FLOAT:
14723 return rb_node_float_literal_val(node);
14724 case NODE_RATIONAL:
14725 return rb_node_rational_literal_val(node);
14726 case NODE_IMAGINARY:
14727 return rb_node_imaginary_literal_val(node);
14728 case NODE_SYM:
14729 return rb_node_sym_string_val(node);
14730 case NODE_REGX:
14731 return rb_node_regx_string_val(node);
14732 case NODE_LINE:
14733 return rb_node_line_lineno_val(node);
14734 case NODE_ENCODING:
14735 return rb_node_encoding_val(node);
14736 case NODE_FILE:
14737 return rb_node_file_path_val(node);
14738 default:
14739 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14740 UNREACHABLE_RETURN(0);
14741 }
14742}
14743
14744static void
14745warn_duplicate_keys(struct parser_params *p, NODE *hash)
14746{
14747 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14748 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14749 while (hash && RNODE_LIST(hash)->nd_next) {
14750 NODE *head = RNODE_LIST(hash)->nd_head;
14751 NODE *value = RNODE_LIST(hash)->nd_next;
14752 NODE *next = RNODE_LIST(value)->nd_next;
14753 st_data_t key;
14754 st_data_t data;
14755
14756 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14757 if (!head) {
14758 head = value;
14759 }
14760
14761 if (nd_type_st_key_enable_p(head)) {
14762 key = (st_data_t)head;
14763
14764 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14765 rb_warn2L(nd_line((NODE *)data),
14766 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14767 nd_value(p, head), WARN_I(nd_line(head)));
14768 }
14769 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14770 }
14771 hash = next;
14772 }
14773 st_free_table(p->warn_duplicate_keys_table);
14774 p->warn_duplicate_keys_table = NULL;
14775}
14776
14777static NODE *
14778new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14779{
14780 if (hash) warn_duplicate_keys(p, hash);
14781 return NEW_HASH(hash, loc);
14782}
14783
14784static void
14785error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14786{
14787 if (is_private_local_id(p, id)) {
14788 return;
14789 }
14790 if (st_is_member(p->pvtbl, id)) {
14791 yyerror1(loc, "duplicated variable name");
14792 }
14793 else if (p->ctxt.in_alt_pattern && id) {
14794 yyerror1(loc, "variable capture in alternative pattern");
14795 }
14796 else {
14797 p->ctxt.capture_in_pattern = 1;
14798 st_insert(p->pvtbl, (st_data_t)id, 0);
14799 }
14800}
14801
14802static void
14803error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14804{
14805 if (!p->pktbl) {
14806 p->pktbl = st_init_numtable();
14807 }
14808 else if (st_is_member(p->pktbl, key)) {
14809 yyerror1(loc, "duplicated key name");
14810 return;
14811 }
14812 st_insert(p->pktbl, (st_data_t)key, 0);
14813}
14814
14815static NODE *
14816new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14817{
14818 return NEW_HASH(hash, loc);
14819}
14820
14821static NODE *
14822new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14823{
14824 NODE *asgn;
14825
14826 if (lhs) {
14827 ID vid = get_nd_vid(p, lhs);
14828 YYLTYPE lhs_loc = lhs->nd_loc;
14829 /* A constant read can raise NameError. Prism has no separate read node
14830 * for `Const op= value` and reports the whole expression, so give the
14831 * NODE_CONST the location of the whole operator assignment as well,
14832 * for consistent Thread::Backtrace::Location#source_range between both
14833 * parsers. */
14834 const YYLTYPE *read_loc = nd_type_p(lhs, NODE_CDECL) ? loc : &lhs_loc;
14835 if (op == tOROP) {
14836 set_nd_value(p, lhs, rhs);
14837 nd_set_loc(lhs, loc);
14838 asgn = NEW_OP_ASGN_OR(gettable(p, vid, read_loc), lhs, loc);
14839 }
14840 else if (op == tANDOP) {
14841 set_nd_value(p, lhs, rhs);
14842 nd_set_loc(lhs, loc);
14843 asgn = NEW_OP_ASGN_AND(gettable(p, vid, read_loc), lhs, loc);
14844 }
14845 else {
14846 asgn = lhs;
14847 rhs = NEW_CALL(gettable(p, vid, read_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14848 set_nd_value(p, asgn, rhs);
14849 nd_set_loc(asgn, loc);
14850 }
14851 }
14852 else {
14853 asgn = NEW_ERROR(loc);
14854 }
14855 return asgn;
14856}
14857
14858static NODE *
14859new_ary_op_assign(struct parser_params *p, NODE *ary,
14860 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14861 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14862{
14863 NODE *asgn;
14864
14865 aryset_check(p, args);
14866 args = make_list(args, args_loc);
14867 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14868 fixpos(asgn, ary);
14869 return asgn;
14870}
14871
14872static NODE *
14873new_attr_op_assign(struct parser_params *p, NODE *lhs,
14874 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14875 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14876{
14877 NODE *asgn;
14878
14879 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14880 fixpos(asgn, lhs);
14881 return asgn;
14882}
14883
14884static NODE *
14885new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14886{
14887 NODE *asgn;
14888
14889 if (lhs) {
14890 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14891 }
14892 else {
14893 asgn = NEW_ERROR(loc);
14894 }
14895 fixpos(asgn, lhs);
14896 return asgn;
14897}
14898
14899static NODE *
14900const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14901{
14902 if (p->ctxt.in_def) {
14903#ifndef RIPPER
14904 yyerror1(loc, "dynamic constant assignment");
14905#else
14906 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14907#endif
14908 }
14909 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14910}
14911
14912#ifdef RIPPER
14913static VALUE
14914assign_error(struct parser_params *p, const char *mesg, VALUE a)
14915{
14916 a = dispatch2(assign_error, ERR_MESG(), a);
14917 ripper_error(p);
14918 return a;
14919}
14920#endif
14921
14922static NODE *
14923new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14924{
14925 NODE *result = head;
14926 if (rescue) {
14927 NODE *tmp = rescue_else ? rescue_else : rescue;
14928 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14929
14930 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14931 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14932 }
14933 if (ensure) {
14934 result = NEW_ENSURE(result, ensure, loc);
14935 }
14936 fixpos(result, head);
14937 return result;
14938}
14939
14940static void
14941warn_unused_var(struct parser_params *p, struct local_vars *local)
14942{
14943 int cnt;
14944
14945 if (!local->used) return;
14946 cnt = local->used->pos;
14947 if (cnt != local->vars->pos) {
14948 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14949 }
14950#ifndef RIPPER
14951 ID *v = local->vars->tbl;
14952 ID *u = local->used->tbl;
14953 for (int i = 0; i < cnt; ++i) {
14954 if (!v[i] || (u[i] & LVAR_USED)) continue;
14955 if (is_private_local_id(p, v[i])) continue;
14956 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14957 }
14958#endif
14959}
14960
14961static void
14962local_push(struct parser_params *p, int toplevel_scope)
14963{
14964 struct local_vars *local;
14965 int inherits_dvars = toplevel_scope && compile_for_eval;
14966 int warn_unused_vars = RTEST(ruby_verbose);
14967
14968 local = ALLOC(struct local_vars);
14969 local->prev = p->lvtbl;
14970 local->args = vtable_alloc(0);
14971 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14972#ifndef RIPPER
14973 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14974 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14975#endif
14976 local->numparam.outer = 0;
14977 local->numparam.inner = 0;
14978 local->numparam.current = 0;
14979 local->it = 0;
14980 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14981
14982# if WARN_PAST_SCOPE
14983 local->past = 0;
14984# endif
14985 CMDARG_PUSH(0);
14986 COND_PUSH(0);
14987 p->lvtbl = local;
14988}
14989
14990static void
14991vtable_chain_free(struct parser_params *p, struct vtable *table)
14992{
14993 while (!DVARS_TERMINAL_P(table)) {
14994 struct vtable *cur_table = table;
14995 table = cur_table->prev;
14996 vtable_free(cur_table);
14997 }
14998}
14999
15000static void
15001local_free(struct parser_params *p, struct local_vars *local)
15002{
15003 vtable_chain_free(p, local->used);
15004
15005# if WARN_PAST_SCOPE
15006 vtable_chain_free(p, local->past);
15007# endif
15008
15009 vtable_chain_free(p, local->args);
15010 vtable_chain_free(p, local->vars);
15011
15012 ruby_xfree_sized(local, sizeof(struct local_vars));
15013}
15014
15015static void
15016local_pop(struct parser_params *p)
15017{
15018 struct local_vars *local = p->lvtbl->prev;
15019 if (p->lvtbl->used) {
15020 warn_unused_var(p, p->lvtbl);
15021 }
15022
15023 local_free(p, p->lvtbl);
15024 p->lvtbl = local;
15025
15026 CMDARG_POP();
15027 COND_POP();
15028}
15029
15030static rb_ast_id_table_t *
15031local_tbl(struct parser_params *p)
15032{
15033 int cnt_args = vtable_size(p->lvtbl->args);
15034 int cnt_vars = vtable_size(p->lvtbl->vars);
15035 int cnt = cnt_args + cnt_vars;
15036 int i, j;
15037 rb_ast_id_table_t *tbl;
15038
15039 if (cnt <= 0) return 0;
15040 tbl = rb_ast_new_local_table(p->ast, cnt);
15041 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
15042 /* remove IDs duplicated to warn shadowing */
15043 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15044 ID id = p->lvtbl->vars->tbl[i];
15045 if (!vtable_included(p->lvtbl->args, id)) {
15046 tbl->ids[j++] = id;
15047 }
15048 }
15049 if (j < cnt) {
15050 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15051 }
15052
15053 return tbl;
15054}
15055
15056static void
15057numparam_name(struct parser_params *p, ID id)
15058{
15059 if (!NUMPARAM_ID_P(id)) return;
15060 compile_error(p, "_%d is reserved for numbered parameter",
15061 NUMPARAM_ID_TO_IDX(id));
15062}
15063
15064static void
15065arg_var(struct parser_params *p, ID id)
15066{
15067 numparam_name(p, id);
15068 vtable_add(p->lvtbl->args, id);
15069}
15070
15071static void
15072local_var(struct parser_params *p, ID id)
15073{
15074 numparam_name(p, id);
15075 vtable_add(p->lvtbl->vars, id);
15076 if (p->lvtbl->used) {
15077 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15078 }
15079}
15080
15081#ifndef RIPPER
15082int
15083rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15084{
15085 return rb_local_defined(id, iseq);
15086}
15087#endif
15088
15089static int
15090local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15091{
15092 struct vtable *vars, *args, *used;
15093
15094 vars = p->lvtbl->vars;
15095 args = p->lvtbl->args;
15096 used = p->lvtbl->used;
15097
15098 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15099 vars = vars->prev;
15100 args = args->prev;
15101 if (used) used = used->prev;
15102 }
15103
15104 if (vars && vars->prev == DVARS_INHERIT) {
15105 return rb_parser_local_defined(p, id, p->parent_iseq);
15106 }
15107 else if (vtable_included(args, id)) {
15108 return 1;
15109 }
15110 else {
15111 int i = vtable_included(vars, id);
15112 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15113 return i != 0;
15114 }
15115}
15116
15117static int
15118local_id(struct parser_params *p, ID id)
15119{
15120 return local_id_ref(p, id, NULL);
15121}
15122
15123static int
15124check_forwarding_args(struct parser_params *p)
15125{
15126 if (local_id(p, idFWD_ALL)) return TRUE;
15127 compile_error(p, "unexpected ...");
15128 return FALSE;
15129}
15130
15131static void
15132add_forwarding_args(struct parser_params *p)
15133{
15134 arg_var(p, idFWD_REST);
15135 arg_var(p, idFWD_KWREST);
15136 arg_var(p, idFWD_BLOCK);
15137 arg_var(p, idFWD_ALL);
15138}
15139
15140static void
15141forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15142{
15143 bool conflict = false;
15144
15145 struct vtable *vars, *args;
15146
15147 vars = p->lvtbl->vars;
15148 args = p->lvtbl->args;
15149
15150 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15151 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15152 vars = vars->prev;
15153 args = args->prev;
15154 }
15155
15156 bool found = false;
15157 if (vars && vars->prev == DVARS_INHERIT && !found) {
15158 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15159 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15160 }
15161 else {
15162 found = (vtable_included(args, arg) &&
15163 !(all && vtable_included(args, all)));
15164 }
15165
15166 if (!found) {
15167 compile_error(p, "no anonymous %s parameter", var);
15168 }
15169 else if (conflict) {
15170 compile_error(p, "anonymous %s parameter is also used within block", var);
15171 }
15172}
15173
15174static NODE *
15175new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15176{
15177 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15178 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15179 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15180 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15181 block->forwarding = TRUE;
15182 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15183 return arg_blk_pass(args, block);
15184}
15185
15186static NODE *
15187numparam_push(struct parser_params *p)
15188{
15189 struct local_vars *local = p->lvtbl;
15190 NODE *inner = local->numparam.inner;
15191 if (!local->numparam.outer) {
15192 local->numparam.outer = local->numparam.current;
15193 }
15194 local->numparam.inner = 0;
15195 local->numparam.current = 0;
15196 local->it = 0;
15197 return inner;
15198}
15199
15200static void
15201numparam_pop(struct parser_params *p, NODE *prev_inner)
15202{
15203 struct local_vars *local = p->lvtbl;
15204 if (prev_inner) {
15205 /* prefer first one */
15206 local->numparam.inner = prev_inner;
15207 }
15208 else if (local->numparam.current) {
15209 /* current and inner are exclusive */
15210 local->numparam.inner = local->numparam.current;
15211 }
15212 if (p->max_numparam > NO_PARAM) {
15213 /* current and outer are exclusive */
15214 local->numparam.current = local->numparam.outer;
15215 local->numparam.outer = 0;
15216 }
15217 else {
15218 /* no numbered parameter */
15219 local->numparam.current = 0;
15220 }
15221 local->it = 0;
15222}
15223
15224static const struct vtable *
15225dyna_push(struct parser_params *p)
15226{
15227 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15228 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15229 if (p->lvtbl->used) {
15230 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15231 }
15232 return p->lvtbl->args;
15233}
15234
15235static void
15236dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15237{
15238 struct vtable *tmp = *vtblp;
15239 *vtblp = tmp->prev;
15240# if WARN_PAST_SCOPE
15241 if (p->past_scope_enabled) {
15242 tmp->prev = p->lvtbl->past;
15243 p->lvtbl->past = tmp;
15244 return;
15245 }
15246# endif
15247 vtable_free(tmp);
15248}
15249
15250static void
15251dyna_pop_1(struct parser_params *p)
15252{
15253 struct vtable *tmp;
15254
15255 if ((tmp = p->lvtbl->used) != 0) {
15256 warn_unused_var(p, p->lvtbl);
15257 p->lvtbl->used = p->lvtbl->used->prev;
15258 vtable_free(tmp);
15259 }
15260 dyna_pop_vtable(p, &p->lvtbl->args);
15261 dyna_pop_vtable(p, &p->lvtbl->vars);
15262}
15263
15264static void
15265dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15266{
15267 while (p->lvtbl->args != lvargs) {
15268 dyna_pop_1(p);
15269 if (!p->lvtbl->args) {
15270 struct local_vars *local = p->lvtbl->prev;
15271 ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl));
15272 p->lvtbl = local;
15273 }
15274 }
15275 dyna_pop_1(p);
15276}
15277
15278static int
15279dyna_in_block(struct parser_params *p)
15280{
15281 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15282}
15283
15284#ifndef RIPPER
15285int
15286dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15287{
15288 struct vtable *vars, *args, *used;
15289 int i;
15290
15291 args = p->lvtbl->args;
15292 vars = p->lvtbl->vars;
15293 used = p->lvtbl->used;
15294
15295 while (!DVARS_TERMINAL_P(vars)) {
15296 if (vtable_included(args, id)) {
15297 return 1;
15298 }
15299 if ((i = vtable_included(vars, id)) != 0) {
15300 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15301 return 1;
15302 }
15303 args = args->prev;
15304 vars = vars->prev;
15305 if (!vidrefp) used = 0;
15306 if (used) used = used->prev;
15307 }
15308
15309 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15310 return rb_dvar_defined(id, p->parent_iseq);
15311 }
15312
15313 return 0;
15314}
15315#endif
15316
15317static int
15318dvar_defined(struct parser_params *p, ID id)
15319{
15320 return dvar_defined_ref(p, id, NULL);
15321}
15322
15323static int
15324dvar_curr(struct parser_params *p, ID id)
15325{
15326 return (vtable_included(p->lvtbl->args, id) ||
15327 vtable_included(p->lvtbl->vars, id));
15328}
15329
15330static void
15331reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15332{
15333 compile_error(p,
15334 "regexp encoding option '%c' differs from source encoding '%s'",
15335 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15336}
15337
15338#ifndef RIPPER
15339static rb_encoding *
15340find_enc(struct parser_params* p, const char *name)
15341{
15342 int idx = rb_enc_find_index(name);
15343 if (idx < 0) {
15344 rb_bug("unknown encoding name: %s", name);
15345 }
15346
15347 return rb_enc_from_index(idx);
15348}
15349
15350static rb_encoding *
15351kcode_to_enc(struct parser_params* p, int kcode)
15352{
15353 rb_encoding *enc;
15354
15355 switch (kcode) {
15356 case ENC_ASCII8BIT:
15357 enc = rb_ascii8bit_encoding();
15358 break;
15359 case ENC_EUC_JP:
15360 enc = find_enc(p, "EUC-JP");
15361 break;
15362 case ENC_Windows_31J:
15363 enc = find_enc(p, "Windows-31J");
15364 break;
15365 case ENC_UTF8:
15366 enc = rb_utf8_encoding();
15367 break;
15368 default:
15369 enc = NULL;
15370 break;
15371 }
15372
15373 return enc;
15374}
15375
15376int
15377rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15378{
15379 int c = RE_OPTION_ENCODING_IDX(options);
15380
15381 if (c) {
15382 int opt, idx;
15383 rb_encoding *enc;
15384
15385 char_to_option_kcode(c, &opt, &idx);
15386 enc = kcode_to_enc(p, idx);
15387 if (enc != rb_parser_str_get_encoding(str) &&
15388 !rb_parser_is_ascii_string(p, str)) {
15389 goto error;
15390 }
15391 rb_parser_string_set_encoding(str, enc);
15392 }
15393 else if (RE_OPTION_ENCODING_NONE(options)) {
15394 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15395 !rb_parser_is_ascii_string(p, str)) {
15396 c = 'n';
15397 goto error;
15398 }
15399 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15400 }
15401 else if (rb_is_usascii_enc(p->enc)) {
15402 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15403 }
15404 return 0;
15405
15406 error:
15407 return c;
15408}
15409#endif
15410
15411static void
15412reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15413{
15414 int c = rb_reg_fragment_setenc(p, str, options);
15415 if (c) reg_fragment_enc_error(p, str, c);
15416}
15417
15418#ifndef UNIVERSAL_PARSER
15419typedef struct {
15420 struct parser_params* parser;
15421 rb_encoding *enc;
15422 NODE *succ_block;
15423 const YYLTYPE *loc;
15424 rb_parser_assignable_func assignable;
15425} reg_named_capture_assign_t;
15426
15427static int
15428reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15429 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15430{
15431 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15432 struct parser_params* p = arg->parser;
15433 rb_encoding *enc = arg->enc;
15434 long len = name_end - name;
15435 const char *s = (const char *)name;
15436
15437 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15438}
15439
15440static NODE *
15441reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15442{
15443 reg_named_capture_assign_t arg;
15444
15445 arg.parser = p;
15446 arg.enc = rb_enc_get(regexp);
15447 arg.succ_block = 0;
15448 arg.loc = loc;
15449 arg.assignable = assignable;
15450 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15451
15452 if (!arg.succ_block) return 0;
15453 return RNODE_BLOCK(arg.succ_block)->nd_next;
15454}
15455#endif
15456
15457#ifndef RIPPER
15458NODE *
15459rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15460{
15461 return assignable(p, id, val, loc);
15462}
15463
15464int
15465rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15466 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15467{
15468 ID var;
15469 NODE *node, *succ;
15470
15471 if (!len) return ST_CONTINUE;
15472 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15473 return ST_CONTINUE;
15474
15475 var = intern_cstr(s, len, enc);
15476 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15477 if (!lvar_defined(p, var)) return ST_CONTINUE;
15478 }
15479 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15480 succ = *succ_block;
15481 if (!succ) succ = NEW_ERROR(loc);
15482 succ = block_append(p, succ, node);
15483 *succ_block = succ;
15484 return ST_CONTINUE;
15485}
15486#endif
15487
15488static VALUE
15489parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15490{
15491 VALUE str2;
15492 reg_fragment_setenc(p, str, options);
15493 str2 = rb_str_new_parser_string(str);
15494 return rb_parser_reg_compile(p, str2, options);
15495}
15496
15497#ifndef RIPPER
15498VALUE
15499rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15500{
15501 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15502}
15503#endif
15504
15505static VALUE
15506reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15507{
15508 VALUE re;
15509 VALUE err;
15510
15511 err = rb_errinfo();
15512 re = parser_reg_compile(p, str, options);
15513 if (NIL_P(re)) {
15514 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15515 rb_set_errinfo(err);
15516 compile_error(p, "%"PRIsVALUE, m);
15517 return Qnil;
15518 }
15519 return re;
15520}
15521
15522#ifndef RIPPER
15523void
15524rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15525{
15526 p->do_print = print;
15527 p->do_loop = loop;
15528 p->do_chomp = chomp;
15529 p->do_split = split;
15530}
15531
15532static NODE *
15533parser_append_options(struct parser_params *p, NODE *node)
15534{
15535 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15536 const YYLTYPE *const LOC = &default_location;
15537
15538 if (p->do_print) {
15539 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15540 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15541 LOC);
15542 node = block_append(p, node, print);
15543 }
15544
15545 if (p->do_loop) {
15546 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15547
15548 if (p->do_split) {
15549 ID ifs = rb_intern("$;");
15550 ID fields = rb_intern("$F");
15551 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15552 NODE *split = NEW_GASGN(fields,
15553 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15554 rb_intern("split"), args, LOC),
15555 LOC);
15556 node = block_append(p, split, node);
15557 }
15558 if (p->do_chomp) {
15559 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15560 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15561 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15562 }
15563
15564 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15565 }
15566
15567 return node;
15568}
15569
15570void
15571rb_init_parse(void)
15572{
15573 /* just to suppress unused-function warnings */
15574 (void)nodetype;
15575 (void)nodeline;
15576}
15577
15578ID
15579internal_id(struct parser_params *p)
15580{
15581 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15582}
15583#endif /* !RIPPER */
15584
15585static void
15586parser_initialize(struct parser_params *p)
15587{
15588 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15589 p->command_start = TRUE;
15590 p->ruby_sourcefile_string = Qnil;
15591 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15592 string_buffer_init(p);
15593 p->node_id = 0;
15594 p->delayed.token = NULL;
15595 p->frozen_string_literal = -1; /* not specified */
15596 rb_source_hash_init(&p->source_hash);
15597#ifndef RIPPER
15598 p->error_buffer = Qfalse;
15599 p->end_expect_token_locations = NULL;
15600 p->token_id = 0;
15601 p->tokens = NULL;
15602#else
15603 p->result = Qnil;
15604 p->parsing_thread = Qnil;
15605 p->s_value = Qnil;
15606 p->s_lvalue = Qnil;
15607 p->s_value_stack = rb_ary_new();
15608#endif
15609 p->debug_buffer = Qnil;
15610 p->debug_output = rb_ractor_stdout();
15611 p->enc = rb_utf8_encoding();
15612 p->exits = 0;
15613}
15614
15615#ifdef RIPPER
15616#define rb_ruby_parser_mark ripper_parser_mark
15617#define rb_ruby_parser_free ripper_parser_free
15618#define rb_ruby_parser_memsize ripper_parser_memsize
15619#endif
15620
15621void
15622rb_ruby_parser_mark(void *ptr)
15623{
15624 struct parser_params *p = (struct parser_params*)ptr;
15625
15626 rb_gc_mark(p->ruby_sourcefile_string);
15627#ifndef RIPPER
15628 rb_gc_mark(p->error_buffer);
15629#else
15630 rb_gc_mark(p->value);
15631 rb_gc_mark(p->result);
15632 rb_gc_mark(p->parsing_thread);
15633 rb_gc_mark(p->s_value);
15634 rb_gc_mark(p->s_lvalue);
15635 rb_gc_mark(p->s_value_stack);
15636#endif
15637 rb_gc_mark(p->debug_buffer);
15638 rb_gc_mark(p->debug_output);
15639}
15640
15641void
15642rb_ruby_parser_free(void *ptr)
15643{
15644 struct parser_params *p = (struct parser_params*)ptr;
15645 struct local_vars *local, *prev;
15646
15647 if (p->ast) {
15648 rb_ast_free(p->ast);
15649 }
15650
15651 if (p->warn_duplicate_keys_table) {
15652 st_free_table(p->warn_duplicate_keys_table);
15653 }
15654
15655#ifndef RIPPER
15656 if (p->tokens) {
15657 rb_parser_ary_free(p, p->tokens);
15658 }
15659#endif
15660
15661 if (p->tokenbuf) {
15662 ruby_xfree_sized(p->tokenbuf, p->toksiz);
15663 }
15664
15665 for (local = p->lvtbl; local; local = prev) {
15666 prev = local->prev;
15667 local_free(p, local);
15668 }
15669
15670 {
15671 token_info *ptinfo;
15672 while ((ptinfo = p->token_info) != 0) {
15673 p->token_info = ptinfo->next;
15674 xfree(ptinfo);
15675 }
15676 }
15677 string_buffer_free(p);
15678
15679 if (p->pvtbl) {
15680 st_free_table(p->pvtbl);
15681 }
15682
15683 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15684 st_free_table(p->case_labels);
15685 }
15686
15687 xfree(p->lex.strterm);
15688 p->lex.strterm = 0;
15689
15690 xfree(ptr);
15691}
15692
15693size_t
15694rb_ruby_parser_memsize(const void *ptr)
15695{
15696 struct parser_params *p = (struct parser_params*)ptr;
15697 struct local_vars *local;
15698 size_t size = sizeof(*p);
15699
15700 size += p->toksiz;
15701 for (local = p->lvtbl; local; local = local->prev) {
15702 size += sizeof(*local);
15703 if (local->vars) size += local->vars->capa * sizeof(ID);
15704 }
15705 return size;
15706}
15707
15708#ifndef RIPPER
15709#undef rb_reserved_word
15710
15711const struct kwtable *
15712rb_reserved_word(const char *str, unsigned int len)
15713{
15714 return reserved_word(str, len);
15715}
15716
15717#ifdef UNIVERSAL_PARSER
15718rb_parser_t *
15719rb_ruby_parser_allocate(const rb_parser_config_t *config)
15720{
15721 /* parser_initialize expects fields to be set to 0 */
15722 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15723 p->config = config;
15724 return p;
15725}
15726
15727rb_parser_t *
15728rb_ruby_parser_new(const rb_parser_config_t *config)
15729{
15730 /* parser_initialize expects fields to be set to 0 */
15731 rb_parser_t *p = rb_ruby_parser_allocate(config);
15732 parser_initialize(p);
15733 return p;
15734}
15735#else
15736rb_parser_t *
15737rb_ruby_parser_allocate(void)
15738{
15739 /* parser_initialize expects fields to be set to 0 */
15740 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15741 return p;
15742}
15743
15744rb_parser_t *
15745rb_ruby_parser_new(void)
15746{
15747 /* parser_initialize expects fields to be set to 0 */
15748 rb_parser_t *p = rb_ruby_parser_allocate();
15749 parser_initialize(p);
15750 return p;
15751}
15752#endif
15753
15754rb_parser_t *
15755rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15756{
15757 p->error_buffer = main ? Qfalse : Qnil;
15758 p->parent_iseq = base;
15759 return p;
15760}
15761
15762void
15763rb_ruby_parser_set_script_lines(rb_parser_t *p)
15764{
15765 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15766}
15767
15768void
15769rb_ruby_parser_error_tolerant(rb_parser_t *p)
15770{
15771 p->error_tolerant = 1;
15772}
15773
15774void
15775rb_ruby_parser_keep_tokens(rb_parser_t *p)
15776{
15777 p->keep_tokens = 1;
15778 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15779}
15780
15781rb_encoding *
15782rb_ruby_parser_encoding(rb_parser_t *p)
15783{
15784 return p->enc;
15785}
15786
15787int
15788rb_ruby_parser_end_seen_p(rb_parser_t *p)
15789{
15790 return p->ruby__end__seen;
15791}
15792
15793int
15794rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15795{
15796 p->debug = flag;
15797 return flag;
15798}
15799#endif /* !RIPPER */
15800
15801#ifdef RIPPER
15802int
15803rb_ruby_parser_get_yydebug(rb_parser_t *p)
15804{
15805 return p->debug;
15806}
15807
15808void
15809rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15810{
15811 p->value = value;
15812}
15813
15814int
15815rb_ruby_parser_error_p(rb_parser_t *p)
15816{
15817 return p->error_p;
15818}
15819
15820VALUE
15821rb_ruby_parser_debug_output(rb_parser_t *p)
15822{
15823 return p->debug_output;
15824}
15825
15826void
15827rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15828{
15829 p->debug_output = output;
15830}
15831
15832VALUE
15833rb_ruby_parser_parsing_thread(rb_parser_t *p)
15834{
15835 return p->parsing_thread;
15836}
15837
15838void
15839rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15840{
15841 p->parsing_thread = parsing_thread;
15842}
15843
15844void
15845rb_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)
15846{
15847 p->lex.gets = gets;
15848 p->lex.input = input;
15849 p->eofp = 0;
15850 p->ruby_sourcefile_string = sourcefile_string;
15851 p->ruby_sourcefile = sourcefile;
15852 p->ruby_sourceline = sourceline;
15853}
15854
15855VALUE
15856rb_ruby_parser_result(rb_parser_t *p)
15857{
15858 return p->result;
15859}
15860
15861rb_encoding *
15862rb_ruby_parser_enc(rb_parser_t *p)
15863{
15864 return p->enc;
15865}
15866
15867VALUE
15868rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15869{
15870 return p->ruby_sourcefile_string;
15871}
15872
15873int
15874rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15875{
15876 return p->ruby_sourceline;
15877}
15878
15879int
15880rb_ruby_parser_lex_state(rb_parser_t *p)
15881{
15882 return p->lex.state;
15883}
15884
15885void
15886rb_ruby_ripper_parse0(rb_parser_t *p)
15887{
15888 parser_prepare(p);
15889 p->ast = rb_ast_new();
15890 ripper_yyparse((void*)p);
15891 rb_ast_free(p->ast);
15892 p->ast = 0;
15893 p->eval_tree = 0;
15894 p->eval_tree_begin = 0;
15895}
15896
15897int
15898rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15899{
15900 return dedent_string(p, string, width);
15901}
15902
15903int
15904rb_ruby_ripper_initialized_p(rb_parser_t *p)
15905{
15906 return p->lex.input != 0;
15907}
15908
15909void
15910rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15911{
15912 parser_initialize(p);
15913}
15914
15915long
15916rb_ruby_ripper_column(rb_parser_t *p)
15917{
15918 return p->lex.ptok - p->lex.pbeg;
15919}
15920
15921long
15922rb_ruby_ripper_token_len(rb_parser_t *p)
15923{
15924 return p->lex.pcur - p->lex.ptok;
15925}
15926
15927rb_parser_string_t *
15928rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15929{
15930 return p->lex.lastline;
15931}
15932
15933VALUE
15934rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15935{
15936 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15937}
15938
15939#ifdef UNIVERSAL_PARSER
15940rb_parser_t *
15941rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15942{
15943 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15944 p->config = config;
15945 return p;
15946}
15947#endif
15948
15949struct parser_params*
15950rb_ruby_ripper_parser_allocate(void)
15951{
15952 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15953}
15954#endif /* RIPPER */
15955
15956#ifndef RIPPER
15957void
15958rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15959{
15960 va_list ap;
15961 VALUE mesg = p->debug_buffer;
15962
15963 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15964 va_start(ap, fmt);
15965 rb_str_vcatf(mesg, fmt, ap);
15966 va_end(ap);
15967 if (char_at_end(p, mesg, 0) == '\n') {
15968 rb_io_write(p->debug_output, mesg);
15969 p->debug_buffer = Qnil;
15970 }
15971}
15972
15973static void
15974parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15975{
15976 va_list ap;
15977 int lineno, column;
15978
15979 if (loc) {
15980 lineno = loc->end_pos.lineno;
15981 column = loc->end_pos.column;
15982 }
15983 else {
15984 lineno = p->ruby_sourceline;
15985 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15986 }
15987
15988 rb_io_flush(p->debug_output);
15989 p->error_p = 1;
15990 va_start(ap, fmt);
15991 p->error_buffer =
15992 rb_syntax_error_append(p->error_buffer,
15993 p->ruby_sourcefile_string,
15994 lineno, column,
15995 p->enc, fmt, ap);
15996 va_end(ap);
15997}
15998
15999static size_t
16000count_char(const char *str, int c)
16001{
16002 int n = 0;
16003 while (str[n] == c) ++n;
16004 return n;
16005}
16006
16007/*
16008 * strip enclosing double-quotes, same as the default yytnamerr except
16009 * for that single-quotes matching back-quotes do not stop stripping.
16010 *
16011 * "\"`class' keyword\"" => "`class' keyword"
16012 */
16013size_t
16014rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16015{
16016 if (*yystr == '"') {
16017 size_t yyn = 0, bquote = 0;
16018 const char *yyp = yystr;
16019
16020 while (*++yyp) {
16021 switch (*yyp) {
16022 case '\'':
16023 if (!bquote) {
16024 bquote = count_char(yyp+1, '\'') + 1;
16025 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16026 yyn += bquote;
16027 yyp += bquote - 1;
16028 break;
16029 }
16030 else {
16031 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16032 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16033 yyn += bquote;
16034 yyp += bquote - 1;
16035 bquote = 0;
16036 break;
16037 }
16038 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16039 if (yyres) memcpy(yyres + yyn, yyp, 3);
16040 yyn += 3;
16041 yyp += 2;
16042 break;
16043 }
16044 goto do_not_strip_quotes;
16045 }
16046
16047 case ',':
16048 goto do_not_strip_quotes;
16049
16050 case '\\':
16051 if (*++yyp != '\\')
16052 goto do_not_strip_quotes;
16053 /* Fall through. */
16054 default:
16055 if (yyres)
16056 yyres[yyn] = *yyp;
16057 yyn++;
16058 break;
16059
16060 case '"':
16061 case '\0':
16062 if (yyres)
16063 yyres[yyn] = '\0';
16064 return yyn;
16065 }
16066 }
16067 do_not_strip_quotes: ;
16068 }
16069
16070 if (!yyres) return strlen(yystr);
16071
16072 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16073}
16074#endif
16075
16076#ifdef RIPPER
16077#define validate(x) (void)(x)
16078
16079static VALUE
16080ripper_dispatch0(struct parser_params *p, ID mid)
16081{
16082 return rb_funcall(p->value, mid, 0);
16083}
16084
16085static VALUE
16086ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16087{
16088 validate(a);
16089 return rb_funcall(p->value, mid, 1, a);
16090}
16091
16092static VALUE
16093ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16094{
16095 validate(a);
16096 validate(b);
16097 return rb_funcall(p->value, mid, 2, a, b);
16098}
16099
16100static VALUE
16101ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16102{
16103 validate(a);
16104 validate(b);
16105 validate(c);
16106 return rb_funcall(p->value, mid, 3, a, b, c);
16107}
16108
16109static VALUE
16110ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16111{
16112 validate(a);
16113 validate(b);
16114 validate(c);
16115 validate(d);
16116 return rb_funcall(p->value, mid, 4, a, b, c, d);
16117}
16118
16119static VALUE
16120ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16121{
16122 validate(a);
16123 validate(b);
16124 validate(c);
16125 validate(d);
16126 validate(e);
16127 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16128}
16129
16130static VALUE
16131ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16132{
16133 validate(a);
16134 validate(b);
16135 validate(c);
16136 validate(d);
16137 validate(e);
16138 validate(f);
16139 validate(g);
16140 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16141}
16142
16143void
16144ripper_error(struct parser_params *p)
16145{
16146 p->error_p = TRUE;
16147}
16148
16149VALUE
16150ripper_value(struct parser_params *p)
16151{
16152 (void)yystpcpy; /* may not used in newer bison */
16153
16154 return p->value;
16155}
16156
16157#endif /* RIPPER */
16158/*
16159 * Local variables:
16160 * mode: c
16161 * c-file-style: "ruby"
16162 * End:
16163 */