Ruby: parse.y Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

parse.y
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%{
13
14#if !YYPURE
15# error needs pure parser
16#endif
17#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20
21/* For Ripper */
22#ifdef RUBY_EXTCONF_H
23# include RUBY_EXTCONF_H
24#endif
25
26#include "ruby/internal/config.h"
27
28#include <errno.h>
29
30#ifdef UNIVERSAL_PARSER
31
32#include "internal/ruby_parser.h"
33#include "parser_node.h"
34#include "universal_parser.c"
35
36#ifdef RIPPER
37#define STATIC_ID2SYM p->config->static_id2sym
38#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
39#endif
40
41#else
42
43#include "internal.h"
44#include "internal/compile.h"
45#include "internal/compilers.h"
46#include "internal/complex.h"
47#include "internal/encoding.h"
48#include "internal/error.h"
49#include "internal/hash.h"
50#include "internal/io.h"
51#include "internal/numeric.h"
52#include "internal/parse.h"
53#include "internal/rational.h"
54#include "internal/re.h"
55#include "internal/ruby_parser.h"
56#include "internal/symbol.h"
57#include "internal/thread.h"
58#include "internal/variable.h"
59#include "node.h"
60#include "parser_node.h"
61#include "probes.h"
62#include "regenc.h"
63#include "ruby/encoding.h"
64#include "ruby/regex.h"
65#include "ruby/ruby.h"
66#include "ruby/st.h"
67#include "ruby/util.h"
68#include "ruby/ractor.h"
69#include "symbol.h"
70
71#ifndef RIPPER
72static VALUE
73syntax_error_new(void)
74{
75 return rb_class_new_instance(0, 0, rb_eSyntaxError);
76}
77#endif
78
79static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
80
81#define compile_callback rb_suppress_tracing
82#endif /* !UNIVERSAL_PARSER */
83
84#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
85#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
86
87static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
88
89#ifndef RIPPER
90static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
91#endif
92
93static int
94node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
95{
96 return (n1->minus != n2->minus ||
97 n1->base != n2->base ||
98 strcmp(n1->val, n2->val));
99}
100
101static int
102node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
103{
104 return (n1->minus != n2->minus ||
105 strcmp(n1->val, n2->val));
106}
107
108static int
109node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
110{
111 return (n1->minus != n2->minus ||
112 n1->base != n2->base ||
113 n1->seen_point != n2->seen_point ||
114 strcmp(n1->val, n2->val));
115}
116
117static int
118node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
119{
120 return (n1->minus != n2->minus ||
121 n1->base != n2->base ||
122 n1->seen_point != n2->seen_point ||
123 n1->type != n2->type ||
124 strcmp(n1->val, n2->val));
125}
126
127static int
128rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
129{
130 return (n1->options != n2->options ||
131 rb_parser_string_hash_cmp(n1->string, n2->string));
132}
133
134static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
135static st_index_t rb_char_p_hash(const char *c);
136
137static int
138literal_cmp(st_data_t val, st_data_t lit)
139{
140 if (val == lit) return 0;
141
142 NODE *node_val = RNODE(val);
143 NODE *node_lit = RNODE(lit);
144 enum node_type type_val = nd_type(node_val);
145 enum node_type type_lit = nd_type(node_lit);
146
147 if (type_val != type_lit) {
148 return -1;
149 }
150
151 switch (type_lit) {
152 case NODE_INTEGER:
153 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
154 case NODE_FLOAT:
155 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
156 case NODE_RATIONAL:
157 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
158 case NODE_IMAGINARY:
159 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
160 case NODE_STR:
161 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
162 case NODE_SYM:
163 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
164 case NODE_REGX:
165 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
166 case NODE_LINE:
167 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
168 case NODE_FILE:
169 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
170 case NODE_ENCODING:
171 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
172 default:
173#ifdef UNIVERSAL_PARSER
174 abort();
175#else
176 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
177#endif
178 }
179}
180
181static st_index_t
182literal_hash(st_data_t a)
183{
184 NODE *node = (NODE *)a;
185 enum node_type type = nd_type(node);
186
187 switch (type) {
188 case NODE_INTEGER:
189 return rb_char_p_hash(RNODE_INTEGER(node)->val);
190 case NODE_FLOAT:
191 return rb_char_p_hash(RNODE_FLOAT(node)->val);
192 case NODE_RATIONAL:
193 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
194 case NODE_IMAGINARY:
195 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
196 case NODE_STR:
197 return rb_parser_str_hash(RNODE_STR(node)->string);
198 case NODE_SYM:
199 return rb_parser_str_hash(RNODE_SYM(node)->string);
200 case NODE_REGX:
201 return rb_parser_str_hash(RNODE_REGX(node)->string);
202 case NODE_LINE:
203 return (st_index_t)node->nd_loc.beg_pos.lineno;
204 case NODE_FILE:
205 return rb_parser_str_hash(RNODE_FILE(node)->path);
206 case NODE_ENCODING:
207 return (st_index_t)RNODE_ENCODING(node)->enc;
208 default:
209#ifdef UNIVERSAL_PARSER
210 abort();
211#else
212 rb_bug("unexpected node: %s", ruby_node_name(type));
213#endif
214 }
215}
216
217static inline int
218parse_isascii(int c)
219{
220 return '\0' <= c && c <= '\x7f';
221}
222
223#undef ISASCII
224#define ISASCII parse_isascii
225
226static inline int
227parse_isspace(int c)
228{
229 return c == ' ' || ('\t' <= c && c <= '\r');
230}
231
232#undef ISSPACE
233#define ISSPACE parse_isspace
234
235static inline int
236parse_iscntrl(int c)
237{
238 return ('\0' <= c && c < ' ') || c == '\x7f';
239}
240
241#undef ISCNTRL
242#define ISCNTRL(c) parse_iscntrl(c)
243
244static inline int
245parse_isupper(int c)
246{
247 return 'A' <= c && c <= 'Z';
248}
249
250static inline int
251parse_islower(int c)
252{
253 return 'a' <= c && c <= 'z';
254}
255
256static inline int
257parse_isalpha(int c)
258{
259 return parse_isupper(c) || parse_islower(c);
260}
261
262#undef ISALPHA
263#define ISALPHA(c) parse_isalpha(c)
264
265static inline int
266parse_isdigit(int c)
267{
268 return '0' <= c && c <= '9';
269}
270
271#undef ISDIGIT
272#define ISDIGIT(c) parse_isdigit(c)
273
274static inline int
275parse_isalnum(int c)
276{
277 return ISALPHA(c) || ISDIGIT(c);
278}
279
280#undef ISALNUM
281#define ISALNUM(c) parse_isalnum(c)
282
283static inline int
284parse_isxdigit(int c)
285{
286 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
287}
288
289#undef ISXDIGIT
290#define ISXDIGIT(c) parse_isxdigit(c)
291
292#include "parser_st.h"
293
294#undef STRCASECMP
295#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
296
297#undef STRNCASECMP
298#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
299
300#ifdef RIPPER
301#include "ripper_init.h"
302#endif
303
304enum rescue_context {
305 before_rescue,
306 after_rescue,
307 after_else,
308 after_ensure,
309};
310
311struct lex_context {
312 unsigned int in_defined: 1;
313 unsigned int in_kwarg: 1;
314 unsigned int in_argdef: 1;
315 unsigned int in_def: 1;
316 unsigned int in_class: 1;
317 unsigned int has_trailing_semicolon: 1;
318 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
319 BITFIELD(enum rescue_context, in_rescue, 2);
320 unsigned int cant_return: 1;
321 unsigned int in_alt_pattern: 1;
322 unsigned int capture_in_pattern: 1;
323};
324
325typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
326
327#if defined(__GNUC__) && !defined(__clang__)
328// Suppress "parameter passing for argument of type 'struct
329// lex_context' changed" notes. `struct lex_context` is file scope,
330// and has no ABI compatibility issue.
331RBIMPL_WARNING_PUSH()
332RBIMPL_WARNING_IGNORED(-Wpsabi)
333RBIMPL_WARNING_POP()
334// Not sure why effective even after popped.
335#endif
336
337#include "parse.h"
338
339#define NO_LEX_CTXT (struct lex_context){0}
340
341#ifndef WARN_PAST_SCOPE
342# define WARN_PAST_SCOPE 0
343#endif
344
345#define TAB_WIDTH 8
346
347#define yydebug (p->debug) /* disable the global variable definition */
348
349#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
350#define YY_LOCATION_PRINT(File, loc, p) \
351 rb_parser_printf(p, "%d.%d-%d.%d", \
352 (loc).beg_pos.lineno, (loc).beg_pos.column,\
353 (loc).end_pos.lineno, (loc).end_pos.column)
354#define YYLLOC_DEFAULT(Current, Rhs, N) \
355 do \
356 if (N) \
357 { \
358 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
359 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
360 } \
361 else \
362 { \
363 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
364 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
365 } \
366 while (0)
367#define YY_(Msgid) \
368 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
369 "nesting too deep" : (Msgid))
370
371#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
372 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
373#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
374 rb_parser_set_location_of_delayed_token(p, &(Current))
375#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
376 rb_parser_set_location_of_heredoc_end(p, &(Current))
377#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
378 rb_parser_set_location_of_dummy_end(p, &(Current))
379#define RUBY_SET_YYLLOC_OF_NONE(Current) \
380 rb_parser_set_location_of_none(p, &(Current))
381#define RUBY_SET_YYLLOC(Current) \
382 rb_parser_set_location(p, &(Current))
383#define RUBY_INIT_YYLLOC() \
384 { \
385 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
386 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
387 }
388
389#define IS_lex_state_for(x, ls) ((x) & (ls))
390#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
391#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
392#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
393
394# define SET_LEX_STATE(ls) \
395 parser_set_lex_state(p, ls, __LINE__)
396static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
397
398typedef VALUE stack_type;
399
400static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
401
402# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
403# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
404# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
405# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
406# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
407
408/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
409 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
410#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
411#define COND_POP() BITSTACK_POP(cond_stack)
412#define COND_P() BITSTACK_SET_P(cond_stack)
413#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
414
415/* A flag to identify keyword_do_block; "do" keyword after command_call.
416 Example: `foo 1, 2 do`. */
417#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
418#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
419#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
420#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
421
422struct vtable {
423 ID *tbl;
424 int pos;
425 int capa;
426 struct vtable *prev;
427};
428
429struct local_vars {
430 struct vtable *args;
431 struct vtable *vars;
432 struct vtable *used;
433# if WARN_PAST_SCOPE
434 struct vtable *past;
435# endif
436 struct local_vars *prev;
437 struct {
438 NODE *outer, *inner, *current;
439 } numparam;
440 NODE *it;
441};
442
443typedef struct rb_locations_lambda_body_t {
444 NODE *node;
445 YYLTYPE opening_loc;
446 YYLTYPE closing_loc;
447} rb_locations_lambda_body_t;
448
449enum {
450 ORDINAL_PARAM = -1,
451 NO_PARAM = 0,
452 NUMPARAM_MAX = 9,
453};
454
455#define DVARS_INHERIT ((void*)1)
456#define DVARS_TOPSCOPE NULL
457#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
458
459typedef struct token_info {
460 const char *token;
461 rb_code_position_t beg;
462 int indent;
463 int nonspc;
464 struct token_info *next;
465} token_info;
466
467typedef struct end_expect_token_locations {
468 const rb_code_position_t *pos;
469 struct end_expect_token_locations *prev;
470} end_expect_token_locations_t;
471
472typedef struct parser_string_buffer_elem {
473 struct parser_string_buffer_elem *next;
474 long len; /* Total length of allocated buf */
475 long used; /* Current usage of buf */
476 rb_parser_string_t *buf[FLEX_ARY_LEN];
477} parser_string_buffer_elem_t;
478
479typedef struct parser_string_buffer {
480 parser_string_buffer_elem_t *head;
481 parser_string_buffer_elem_t *last;
482} parser_string_buffer_t;
483
484#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
485
486/*
487 Structure of Lexer Buffer:
488
489 lex.pbeg lex.ptok lex.pcur lex.pend
490 | | | |
491 |------------+------------+------------|
492 |<---------->|
493 token
494*/
495struct parser_params {
496 YYSTYPE *lval;
497 YYLTYPE *yylloc;
498
499 struct {
500 rb_strterm_t *strterm;
501 rb_parser_lex_gets_func *gets;
502 rb_parser_input_data input;
503 parser_string_buffer_t string_buffer;
504 rb_parser_string_t *lastline;
505 rb_parser_string_t *nextline;
506 const char *pbeg;
507 const char *pcur;
508 const char *pend;
509 const char *ptok;
510 enum lex_state_e state;
511 /* track the nest level of any parens "()[]{}" */
512 int paren_nest;
513 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
514 int lpar_beg;
515 /* track the nest level of only braces "{}" */
516 int brace_nest;
517 } lex;
518 stack_type cond_stack;
519 stack_type cmdarg_stack;
520 int tokidx;
521 int toksiz;
522 int heredoc_end;
523 int heredoc_indent;
524 int heredoc_line_indent;
525 char *tokenbuf;
526 struct local_vars *lvtbl;
527 st_table *pvtbl;
528 st_table *pktbl;
529 int line_count;
530 int ruby_sourceline; /* current line no. */
531 const char *ruby_sourcefile; /* current source file */
532 VALUE ruby_sourcefile_string;
533 rb_encoding *enc;
534 token_info *token_info;
535 st_table *case_labels;
536 rb_node_exits_t *exits;
537
538 VALUE debug_buffer;
539 VALUE debug_output;
540
541 struct {
542 rb_parser_string_t *token;
543 int beg_line;
544 int beg_col;
545 int end_line;
546 int end_col;
547 } delayed;
548
549 rb_ast_t *ast;
550 int node_id;
551
552 st_table *warn_duplicate_keys_table;
553
554 int max_numparam;
555 ID it_id;
556
557 struct lex_context ctxt;
558
559 NODE *eval_tree_begin;
560 NODE *eval_tree;
561 const struct rb_iseq_struct *parent_iseq;
562
563#ifdef UNIVERSAL_PARSER
564 const rb_parser_config_t *config;
565#endif
566 /* compile_option */
567 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
568
569 unsigned int command_start:1;
570 unsigned int eofp: 1;
571 unsigned int ruby__end__seen: 1;
572 unsigned int debug: 1;
573 unsigned int has_shebang: 1;
574 unsigned int token_seen: 1;
575 unsigned int token_info_enabled: 1;
576# if WARN_PAST_SCOPE
577 unsigned int past_scope_enabled: 1;
578# endif
579 unsigned int error_p: 1;
580 unsigned int cr_seen: 1;
581
582#ifndef RIPPER
583 /* Ruby core only */
584
585 unsigned int do_print: 1;
586 unsigned int do_loop: 1;
587 unsigned int do_chomp: 1;
588 unsigned int do_split: 1;
589 unsigned int error_tolerant: 1;
590 unsigned int keep_tokens: 1;
591
592 VALUE error_buffer;
593 rb_parser_ary_t *debug_lines;
594 /*
595 * Store specific keyword locations to generate dummy end token.
596 * Refer to the tail of list element.
597 */
598 end_expect_token_locations_t *end_expect_token_locations;
599 /* id for terms */
600 int token_id;
601 /* Array for term tokens */
602 rb_parser_ary_t *tokens;
603#else
604 /* Ripper only */
605
606 VALUE value;
607 VALUE result;
608 VALUE parsing_thread;
609 VALUE s_value; /* Token VALUE */
610 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
611 VALUE s_value_stack;
612#endif
613};
614
615#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
616#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
617#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
618static int
619numparam_id_p(struct parser_params *p, ID id)
620{
621 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
622 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
623 return idx > 0 && idx <= NUMPARAM_MAX;
624}
625static void numparam_name(struct parser_params *p, ID id);
626
627#ifdef RIPPER
628static void
629after_shift(struct parser_params *p)
630{
631 if (p->debug) {
632 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
633 }
634 rb_ary_push(p->s_value_stack, p->s_value);
635 p->s_value = Qnil;
636}
637
638static void
639before_reduce(int len, struct parser_params *p)
640{
641 // Initialize $$ with $1.
642 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
643}
644
645static void
646after_reduce(int len, struct parser_params *p)
647{
648 for (int i = 0; i < len; i++) {
649 VALUE tos = rb_ary_pop(p->s_value_stack);
650 if (p->debug) {
651 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
652 }
653 }
654 if (p->debug) {
655 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
656 }
657 rb_ary_push(p->s_value_stack, p->s_lvalue);
658 p->s_lvalue = Qnil;
659}
660
661static void
662after_shift_error_token(struct parser_params *p)
663{
664 if (p->debug) {
665 rb_parser_printf(p, "after-shift-error-token:\n");
666 }
667 rb_ary_push(p->s_value_stack, Qnil);
668}
669
670static void
671after_pop_stack(int len, struct parser_params *p)
672{
673 for (int i = 0; i < len; i++) {
674 VALUE tos = rb_ary_pop(p->s_value_stack);
675 if (p->debug) {
676 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
677 }
678 }
679}
680#else
681static void
682after_shift(struct parser_params *p)
683{
684}
685
686static void
687before_reduce(int len, struct parser_params *p)
688{
689}
690
691static void
692after_reduce(int len, struct parser_params *p)
693{
694}
695
696static void
697after_shift_error_token(struct parser_params *p)
698{
699}
700
701static void
702after_pop_stack(int len, struct parser_params *p)
703{
704}
705#endif
706
707#define intern_cstr(n,l,en) rb_intern3(n,l,en)
708
709#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
710
711#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
712#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
713#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
714#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
715#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
716#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
717
718#ifndef RIPPER
719static inline int
720char_at_end(struct parser_params *p, VALUE str, int when_empty)
721{
722 long len = RSTRING_LEN(str);
723 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
724}
725#endif
726
727static void
728pop_pvtbl(struct parser_params *p, st_table *tbl)
729{
730 st_free_table(p->pvtbl);
731 p->pvtbl = tbl;
732}
733
734static void
735pop_pktbl(struct parser_params *p, st_table *tbl)
736{
737 if (p->pktbl) st_free_table(p->pktbl);
738 p->pktbl = tbl;
739}
740
741#define STRING_BUF_DEFAULT_LEN 16
742
743static void
744string_buffer_init(struct parser_params *p)
745{
746 parser_string_buffer_t *buf = &p->lex.string_buffer;
747 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
748
749 buf->head = buf->last = xmalloc(size);
750 buf->head->len = STRING_BUF_DEFAULT_LEN;
751 buf->head->used = 0;
752 buf->head->next = NULL;
753}
754
755static void
756string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
757{
758 parser_string_buffer_t *buf = &p->lex.string_buffer;
759
760 if (buf->head->used >= buf->head->len) {
761 parser_string_buffer_elem_t *elem;
762 long n = buf->head->len * 2;
763 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
764
765 elem = xmalloc(size);
766 elem->len = n;
767 elem->used = 0;
768 elem->next = NULL;
769 buf->last->next = elem;
770 buf->last = elem;
771 }
772 buf->last->buf[buf->last->used++] = str;
773}
774
775static void
776string_buffer_free(struct parser_params *p)
777{
778 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
779
780 while (elem) {
781 parser_string_buffer_elem_t *next_elem = elem->next;
782
783 for (long i = 0; i < elem->used; i++) {
784 rb_parser_string_free(p, elem->buf[i]);
785 }
786
787 xfree(elem);
788 elem = next_elem;
789 }
790}
791
792#ifndef RIPPER
793static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
794
795static void
796debug_end_expect_token_locations(struct parser_params *p, const char *name)
797{
798 if(p->debug) {
799 VALUE mesg = rb_sprintf("%s: [", name);
800 int i = 0;
801 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
802 if (i > 0)
803 rb_str_cat_cstr(mesg, ", ");
804 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
805 i++;
806 }
807 rb_str_cat_cstr(mesg, "]\n");
808 flush_debug_buffer(p, p->debug_output, mesg);
809 }
810}
811
812static void
813push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
814{
815 if(!p->error_tolerant) return;
816
817 end_expect_token_locations_t *locations;
818 locations = ALLOC(end_expect_token_locations_t);
819 locations->pos = pos;
820 locations->prev = p->end_expect_token_locations;
821 p->end_expect_token_locations = locations;
822
823 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
824}
825
826static void
827pop_end_expect_token_locations(struct parser_params *p)
828{
829 if(!p->end_expect_token_locations) return;
830
831 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
832 ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
833 p->end_expect_token_locations = locations;
834
835 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
836}
837
838static end_expect_token_locations_t *
839peek_end_expect_token_locations(struct parser_params *p)
840{
841 return p->end_expect_token_locations;
842}
843
844static const char *
845parser_token2char(struct parser_params *p, enum yytokentype tok)
846{
847 switch ((int) tok) {
848#define TOKEN2CHAR(tok) case tok: return (#tok);
849#define TOKEN2CHAR2(tok, name) case tok: return (name);
850 TOKEN2CHAR2(' ', "word_sep");
851 TOKEN2CHAR2('!', "!")
852 TOKEN2CHAR2('%', "%");
853 TOKEN2CHAR2('&', "&");
854 TOKEN2CHAR2('*', "*");
855 TOKEN2CHAR2('+', "+");
856 TOKEN2CHAR2('-', "-");
857 TOKEN2CHAR2('/', "/");
858 TOKEN2CHAR2('<', "<");
859 TOKEN2CHAR2('=', "=");
860 TOKEN2CHAR2('>', ">");
861 TOKEN2CHAR2('?', "?");
862 TOKEN2CHAR2('^', "^");
863 TOKEN2CHAR2('|', "|");
864 TOKEN2CHAR2('~', "~");
865 TOKEN2CHAR2(':', ":");
866 TOKEN2CHAR2(',', ",");
867 TOKEN2CHAR2('.', ".");
868 TOKEN2CHAR2(';', ";");
869 TOKEN2CHAR2('`', "`");
870 TOKEN2CHAR2('\n', "nl");
871 TOKEN2CHAR2('{', "\"{\"");
872 TOKEN2CHAR2('}', "\"}\"");
873 TOKEN2CHAR2('[', "\"[\"");
874 TOKEN2CHAR2(']', "\"]\"");
875 TOKEN2CHAR2('(', "\"(\"");
876 TOKEN2CHAR2(')', "\")\"");
877 TOKEN2CHAR2('\\', "backslash");
878 TOKEN2CHAR(keyword_class);
879 TOKEN2CHAR(keyword_module);
880 TOKEN2CHAR(keyword_def);
881 TOKEN2CHAR(keyword_undef);
882 TOKEN2CHAR(keyword_begin);
883 TOKEN2CHAR(keyword_rescue);
884 TOKEN2CHAR(keyword_ensure);
885 TOKEN2CHAR(keyword_end);
886 TOKEN2CHAR(keyword_if);
887 TOKEN2CHAR(keyword_unless);
888 TOKEN2CHAR(keyword_then);
889 TOKEN2CHAR(keyword_elsif);
890 TOKEN2CHAR(keyword_else);
891 TOKEN2CHAR(keyword_case);
892 TOKEN2CHAR(keyword_when);
893 TOKEN2CHAR(keyword_while);
894 TOKEN2CHAR(keyword_until);
895 TOKEN2CHAR(keyword_for);
896 TOKEN2CHAR(keyword_break);
897 TOKEN2CHAR(keyword_next);
898 TOKEN2CHAR(keyword_redo);
899 TOKEN2CHAR(keyword_retry);
900 TOKEN2CHAR(keyword_in);
901 TOKEN2CHAR(keyword_do);
902 TOKEN2CHAR(keyword_do_cond);
903 TOKEN2CHAR(keyword_do_block);
904 TOKEN2CHAR(keyword_do_LAMBDA);
905 TOKEN2CHAR(keyword_return);
906 TOKEN2CHAR(keyword_yield);
907 TOKEN2CHAR(keyword_super);
908 TOKEN2CHAR(keyword_self);
909 TOKEN2CHAR(keyword_nil);
910 TOKEN2CHAR(keyword_true);
911 TOKEN2CHAR(keyword_false);
912 TOKEN2CHAR(keyword_and);
913 TOKEN2CHAR(keyword_or);
914 TOKEN2CHAR(keyword_not);
915 TOKEN2CHAR(modifier_if);
916 TOKEN2CHAR(modifier_unless);
917 TOKEN2CHAR(modifier_while);
918 TOKEN2CHAR(modifier_until);
919 TOKEN2CHAR(modifier_rescue);
920 TOKEN2CHAR(keyword_alias);
921 TOKEN2CHAR(keyword_defined);
922 TOKEN2CHAR(keyword_BEGIN);
923 TOKEN2CHAR(keyword_END);
924 TOKEN2CHAR(keyword__LINE__);
925 TOKEN2CHAR(keyword__FILE__);
926 TOKEN2CHAR(keyword__ENCODING__);
927 TOKEN2CHAR(tIDENTIFIER);
928 TOKEN2CHAR(tFID);
929 TOKEN2CHAR(tGVAR);
930 TOKEN2CHAR(tIVAR);
931 TOKEN2CHAR(tCONSTANT);
932 TOKEN2CHAR(tCVAR);
933 TOKEN2CHAR(tLABEL);
934 TOKEN2CHAR(tINTEGER);
935 TOKEN2CHAR(tFLOAT);
936 TOKEN2CHAR(tRATIONAL);
937 TOKEN2CHAR(tIMAGINARY);
938 TOKEN2CHAR(tCHAR);
939 TOKEN2CHAR(tNTH_REF);
940 TOKEN2CHAR(tBACK_REF);
941 TOKEN2CHAR(tSTRING_CONTENT);
942 TOKEN2CHAR(tREGEXP_END);
943 TOKEN2CHAR(tDUMNY_END);
944 TOKEN2CHAR(tSP);
945 TOKEN2CHAR(tUPLUS);
946 TOKEN2CHAR(tUMINUS);
947 TOKEN2CHAR(tPOW);
948 TOKEN2CHAR(tCMP);
949 TOKEN2CHAR(tEQ);
950 TOKEN2CHAR(tEQQ);
951 TOKEN2CHAR(tNEQ);
952 TOKEN2CHAR(tGEQ);
953 TOKEN2CHAR(tLEQ);
954 TOKEN2CHAR(tANDOP);
955 TOKEN2CHAR(tOROP);
956 TOKEN2CHAR(tMATCH);
957 TOKEN2CHAR(tNMATCH);
958 TOKEN2CHAR(tDOT2);
959 TOKEN2CHAR(tDOT3);
960 TOKEN2CHAR(tBDOT2);
961 TOKEN2CHAR(tBDOT3);
962 TOKEN2CHAR(tAREF);
963 TOKEN2CHAR(tASET);
964 TOKEN2CHAR(tLSHFT);
965 TOKEN2CHAR(tRSHFT);
966 TOKEN2CHAR(tANDDOT);
967 TOKEN2CHAR(tCOLON2);
968 TOKEN2CHAR(tCOLON3);
969 TOKEN2CHAR(tOP_ASGN);
970 TOKEN2CHAR(tASSOC);
971 TOKEN2CHAR(tLPAREN);
972 TOKEN2CHAR(tLPAREN_ARG);
973 TOKEN2CHAR(tLBRACK);
974 TOKEN2CHAR(tLBRACE);
975 TOKEN2CHAR(tLBRACE_ARG);
976 TOKEN2CHAR(tSTAR);
977 TOKEN2CHAR(tDSTAR);
978 TOKEN2CHAR(tAMPER);
979 TOKEN2CHAR(tLAMBDA);
980 TOKEN2CHAR(tSYMBEG);
981 TOKEN2CHAR(tSTRING_BEG);
982 TOKEN2CHAR(tXSTRING_BEG);
983 TOKEN2CHAR(tREGEXP_BEG);
984 TOKEN2CHAR(tWORDS_BEG);
985 TOKEN2CHAR(tQWORDS_BEG);
986 TOKEN2CHAR(tSYMBOLS_BEG);
987 TOKEN2CHAR(tQSYMBOLS_BEG);
988 TOKEN2CHAR(tSTRING_END);
989 TOKEN2CHAR(tSTRING_DEND);
990 TOKEN2CHAR(tSTRING_DBEG);
991 TOKEN2CHAR(tSTRING_DVAR);
992 TOKEN2CHAR(tLAMBEG);
993 TOKEN2CHAR(tLABEL_END);
994 TOKEN2CHAR(tIGNORED_NL);
995 TOKEN2CHAR(tCOMMENT);
996 TOKEN2CHAR(tEMBDOC_BEG);
997 TOKEN2CHAR(tEMBDOC);
998 TOKEN2CHAR(tEMBDOC_END);
999 TOKEN2CHAR(tHEREDOC_BEG);
1000 TOKEN2CHAR(tHEREDOC_END);
1001 TOKEN2CHAR(k__END__);
1002 TOKEN2CHAR(tLOWEST);
1003 TOKEN2CHAR(tUMINUS_NUM);
1004 TOKEN2CHAR(tLAST_TOKEN);
1005#undef TOKEN2CHAR
1006#undef TOKEN2CHAR2
1007 }
1008
1009 rb_bug("parser_token2id: unknown token %d", tok);
1010
1011 UNREACHABLE_RETURN(0);
1012}
1013#else
1014static void
1015push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1016{
1017}
1018
1019static void
1020pop_end_expect_token_locations(struct parser_params *p)
1021{
1022}
1023#endif
1024
1025RBIMPL_ATTR_NONNULL((1, 2, 3))
1026static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1027RBIMPL_ATTR_NONNULL((1, 2))
1028static int parser_yyerror0(struct parser_params*, const char*);
1029#define yyerror0(msg) parser_yyerror0(p, (msg))
1030#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1031#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1032#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1033#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1034#define lex_eol_p(p) lex_eol_n_p(p, 0)
1035#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1036#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1037#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1038
1039static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1040static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1041static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1042static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1043static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1044
1045#ifdef RIPPER
1046#define compile_for_eval (0)
1047#else
1048#define compile_for_eval (p->parent_iseq != 0)
1049#endif
1050
1051#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1052
1053#define CALL_Q_P(q) ((q) == tANDDOT)
1054#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1055
1056#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1057
1058static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1059
1060static inline void
1061rb_discard_node(struct parser_params *p, NODE *n)
1062{
1063 rb_ast_delete_node(p->ast, n);
1064}
1065
1066static 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);
1067static 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);
1068static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1069static 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);
1070static 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);
1071static 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);
1072static 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);
1073static 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);
1074static 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);
1075static 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);
1076static 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);
1077static 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);
1078static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1079static 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);
1080static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1081static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1082static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1083static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1084static 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);
1085static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1086static 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);
1087static 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);
1088static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1089static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1090static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1091static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1092static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1093static 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);
1094static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1095static 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);
1096static 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);
1097static 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);
1098static 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);
1099static 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);
1100static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1101static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1102static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1103static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1104static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1105static 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);
1106static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1107static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1108static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1109static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1110static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1111static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1112static 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);
1113static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1114static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1115static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1116static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1117static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1118static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1119static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1120static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1121static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1122static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1123static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1124static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1125static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1126static 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);
1127static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1128static 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);
1129static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1130static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1131static 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);
1132static 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);
1133static 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);
1134static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1135static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1136static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1137static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1138static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1139static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1140static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1141static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1142static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1143static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1144static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1145static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1146static 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);
1147static 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);
1148static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1149static 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);
1150static 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);
1151static 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);
1152static 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);
1153static 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);
1154static 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);
1155static 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);
1156static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1157static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1158static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1159static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1160static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1161static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1162static 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);
1163static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1164static 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);
1165static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1166static 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);
1167static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1168static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1169static 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);
1170static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1171static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1172static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1173
1174#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1175#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1176#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1177#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)
1178#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)
1179#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1180#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1181#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1182#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)
1183#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)
1184#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)
1185#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)
1186#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1187#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)
1188#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1189#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1190#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1191#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1192#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1193#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1194#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1195#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1196#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1197#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1198#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1199#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1200#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1201#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1202#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1203#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)
1204#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)
1205#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1206#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1207#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1208#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1209#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1210#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1211#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1212#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1213#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)
1214#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1215#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1216#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1217#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1218#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1219#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1220#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)
1221#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1222#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1223#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1224#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1225#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1226#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1227#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1228#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1229#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1230#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1231#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1232#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1233#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1234#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1235#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1236#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1237#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1238#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1239#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1240#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1241#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)
1242#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1243#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1244#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1245#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1246#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1247#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1248#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1249#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1250#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1251#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1252#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1253#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1254#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1255#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1256#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1257#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)
1258#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1259#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)
1260#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1261#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1262#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1263#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1264#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1265#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1266#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1267#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1268#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1269#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1270#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)
1271#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1272#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1273#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1274#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)
1275#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1276#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1277#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1278#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1279#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1280#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1281#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1282
1283enum internal_node_type {
1284 NODE_INTERNAL_ONLY = NODE_LAST,
1285 NODE_DEF_TEMP,
1286 NODE_EXITS,
1287 NODE_INTERNAL_LAST
1288};
1289
1290static const char *
1291parser_node_name(int node)
1292{
1293 switch (node) {
1294 case NODE_DEF_TEMP:
1295 return "NODE_DEF_TEMP";
1296 case NODE_EXITS:
1297 return "NODE_EXITS";
1298 default:
1299 return ruby_node_name(node);
1300 }
1301}
1302
1303/* This node is parse.y internal */
1304struct RNode_DEF_TEMP {
1305 NODE node;
1306
1307 /* for NODE_DEFN/NODE_DEFS */
1308
1309 struct RNode *nd_def;
1310 ID nd_mid;
1311
1312 struct {
1313 int max_numparam;
1314 NODE *numparam_save;
1315 struct lex_context ctxt;
1316 } save;
1317};
1318
1319#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1320
1321static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1322static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1323static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1324static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1325static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1326
1327#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1328#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1329#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1330#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1331
1332/* Make a new internal node, which should not be appeared in the
1333 * result AST and does not have node_id and location. */
1334static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1335#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1336
1337static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1338
1339static int
1340parser_get_node_id(struct parser_params *p)
1341{
1342 int node_id = p->node_id;
1343 p->node_id++;
1344 return node_id;
1345}
1346
1347static void
1348anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1349{
1350 if (id == tANDDOT) {
1351 yyerror1(loc, "&. inside multiple assignment destination");
1352 }
1353}
1354
1355static inline void
1356set_line_body(NODE *body, int line)
1357{
1358 if (!body) return;
1359 switch (nd_type(body)) {
1360 case NODE_RESCUE:
1361 case NODE_ENSURE:
1362 nd_set_line(body, line);
1363 }
1364}
1365
1366static void
1367set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1368{
1369 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1370 nd_set_line(node, beg->end_pos.lineno);
1371}
1372
1373static NODE *
1374last_expr_node(NODE *expr)
1375{
1376 while (expr) {
1377 if (nd_type_p(expr, NODE_BLOCK)) {
1378 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1379 }
1380 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1381 expr = RNODE_BEGIN(expr)->nd_body;
1382 }
1383 else {
1384 break;
1385 }
1386 }
1387 return expr;
1388}
1389
1390#ifndef RIPPER
1391#define yyparse ruby_yyparse
1392#endif
1393
1394static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1395static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1396static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1397static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1398static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1399static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1400
1401static NODE *newline_node(NODE*);
1402static void fixpos(NODE*,NODE*);
1403
1404static int value_expr(struct parser_params*,NODE*);
1405static void void_expr(struct parser_params*,NODE*);
1406static NODE *remove_begin(NODE*);
1407static NODE *void_stmts(struct parser_params*,NODE*);
1408static void reduce_nodes(struct parser_params*,NODE**);
1409static void block_dup_check(struct parser_params*,NODE*,NODE*);
1410
1411static NODE *block_append(struct parser_params*,NODE*,NODE*);
1412static NODE *list_append(struct parser_params*,NODE*,NODE*);
1413static NODE *list_concat(NODE*,NODE*);
1414static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1415static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1416static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1417static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1418static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1419static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1420static NODE *str2dstr(struct parser_params*,NODE*);
1421static NODE *evstr2dstr(struct parser_params*,NODE*);
1422static NODE *splat_array(NODE*);
1423static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1424
1425static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1426static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1427static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1428static 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);
1429static 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;}
1430static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1431
1432static bool args_info_empty_p(struct rb_args_info *args);
1433static 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*);
1434static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1435static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1436static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1437static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1438static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1439static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1440static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1441
1442static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1443static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1444
1445static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1446static void no_blockarg(struct parser_params*,NODE*);
1447static NODE *ret_args(struct parser_params*,NODE*);
1448static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1449static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1450
1451static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1452static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1453
1454static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1455static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1456
1457static VALUE rb_backref_error(struct parser_params*,NODE*);
1458static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1459
1460static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1461static 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);
1462static 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);
1463static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1464static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1465
1466static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1467
1468static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1469static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1470
1471static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1472static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1473
1474static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1475
1476static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1477
1478#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1479
1480static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1481
1482static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1483
1484static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1485
1486static rb_ast_id_table_t *local_tbl(struct parser_params*);
1487
1488static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1489static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1490
1491static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1492static NODE *heredoc_dedent(struct parser_params*,NODE*);
1493
1494static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1495
1496static 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);
1497
1498#ifdef RIPPER
1499#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1500#define set_value(val) (p->s_lvalue = val)
1501static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1502static int id_is_var(struct parser_params *p, ID id);
1503#endif
1504
1505RUBY_SYMBOL_EXPORT_BEGIN
1506VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1507int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1508enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1509VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1510void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1511PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1512YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1513YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1514YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1515YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1516YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1517YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1518void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1519RUBY_SYMBOL_EXPORT_END
1520
1521static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1522static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1523static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1524static VALUE formal_argument_error(struct parser_params*, ID);
1525static ID shadowing_lvar(struct parser_params*,ID);
1526static void new_bv(struct parser_params*,ID);
1527
1528static void local_push(struct parser_params*,int);
1529static void local_pop(struct parser_params*);
1530static void local_var(struct parser_params*, ID);
1531static void arg_var(struct parser_params*, ID);
1532static int local_id(struct parser_params *p, ID id);
1533static int local_id_ref(struct parser_params*, ID, ID **);
1534#define internal_id rb_parser_internal_id
1535ID internal_id(struct parser_params*);
1536static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1537static int check_forwarding_args(struct parser_params*);
1538static void add_forwarding_args(struct parser_params *p);
1539static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1540
1541static const struct vtable *dyna_push(struct parser_params *);
1542static void dyna_pop(struct parser_params*, const struct vtable *);
1543static int dyna_in_block(struct parser_params*);
1544#define dyna_var(p, id) local_var(p, id)
1545static int dvar_defined(struct parser_params*, ID);
1546#define dvar_defined_ref rb_parser_dvar_defined_ref
1547int dvar_defined_ref(struct parser_params*, ID, ID**);
1548static int dvar_curr(struct parser_params*,ID);
1549
1550static int lvar_defined(struct parser_params*, ID);
1551
1552static NODE *numparam_push(struct parser_params *p);
1553static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1554
1555#define METHOD_NOT '!'
1556
1557#define idFWD_REST '*'
1558#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1559#define idFWD_BLOCK '&'
1560#define idFWD_ALL idDot3
1561#define arg_FWD_BLOCK idFWD_BLOCK
1562
1563#define RE_ONIG_OPTION_IGNORECASE 1
1564#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1565#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1566#define RE_OPTION_ONCE (1<<16)
1567#define RE_OPTION_ENCODING_SHIFT 8
1568#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1569#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1570#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1571#define RE_OPTION_MASK 0xff
1572#define RE_OPTION_ARG_ENCODING_NONE 32
1573
1574#define CHECK_LITERAL_WHEN (st_table *)1
1575#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1576
1577#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1578RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1579
1580#define TOKEN2ID(tok) ( \
1581 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1582 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1583 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1584 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1585 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1586 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1587 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1588
1589/****** Ripper *******/
1590
1591#ifdef RIPPER
1592
1593#include "eventids1.h"
1594#include "eventids2.h"
1595
1596extern const struct ripper_parser_ids ripper_parser_ids;
1597
1598static VALUE ripper_dispatch0(struct parser_params*,ID);
1599static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1600static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1601static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1602static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1603static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1604static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1605void ripper_error(struct parser_params *p);
1606
1607#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1608#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1609#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1610#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1611#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1612#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1613#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1614
1615#define yyparse ripper_yyparse
1616
1617static VALUE
1618aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1619{
1620 if (!NIL_P(pre_arg)) {
1621 if (!NIL_P(pre_args)) {
1622 rb_ary_unshift(pre_args, pre_arg);
1623 }
1624 else {
1625 pre_args = rb_ary_new_from_args(1, pre_arg);
1626 }
1627 }
1628 return pre_args;
1629}
1630
1631#define ID2VAL(id) STATIC_ID2SYM(id)
1632#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1633#endif /* RIPPER */
1634
1635#define KWD2EID(t, v) keyword_##t
1636
1637static NODE *
1638new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1639{
1640 body = remove_begin(body);
1641 reduce_nodes(p, &body);
1642 NODE *n = NEW_SCOPE(args, body, parent, loc);
1643 nd_set_line(n, loc->end_pos.lineno);
1644 set_line_body(body, loc->beg_pos.lineno);
1645 return n;
1646}
1647
1648static NODE *
1649rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1650 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1651{
1652 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1653 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1654 loc.beg_pos = arg_loc->beg_pos;
1655 return NEW_RESCUE(arg, rescue, 0, &loc);
1656}
1657
1658static NODE *add_block_exit(struct parser_params *p, NODE *node);
1659static rb_node_exits_t *init_block_exit(struct parser_params *p);
1660static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1661static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1662static void clear_block_exit(struct parser_params *p, bool error);
1663
1664static void
1665next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1666{
1667 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1668}
1669
1670static void
1671restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1672{
1673 /* See: def_name action */
1674 struct lex_context ctxt = temp->save.ctxt;
1675 p->ctxt.in_def = ctxt.in_def;
1676 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1677 p->ctxt.in_rescue = ctxt.in_rescue;
1678 p->max_numparam = temp->save.max_numparam;
1679 numparam_pop(p, temp->save.numparam_save);
1680 clear_block_exit(p, true);
1681}
1682
1683static void
1684endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1685{
1686 if (is_attrset_id(mid)) {
1687 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1688 }
1689 token_info_drop(p, "def", loc->beg_pos);
1690}
1691
1692#define debug_token_line(p, name, line) do { \
1693 if (p->debug) { \
1694 const char *const pcur = p->lex.pcur; \
1695 const char *const ptok = p->lex.ptok; \
1696 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1697 line, p->ruby_sourceline, \
1698 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1699 } \
1700 } while (0)
1701
1702#define begin_definition(k, loc_beg, loc_end) \
1703 do { \
1704 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1705 /* singleton class */ \
1706 p->ctxt.cant_return = !p->ctxt.in_def; \
1707 p->ctxt.in_def = 0; \
1708 } \
1709 else if (p->ctxt.in_def) { \
1710 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1711 yyerror1(&loc, k " definition in method body"); \
1712 } \
1713 else { \
1714 p->ctxt.cant_return = 1; \
1715 } \
1716 local_push(p, 0); \
1717 } while (0)
1718
1719#ifndef RIPPER
1720# define ifndef_ripper(x) (x)
1721# define ifdef_ripper(r,x) (x)
1722#else
1723# define ifndef_ripper(x)
1724# define ifdef_ripper(r,x) (r)
1725#endif
1726
1727# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1728# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1729# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1730# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1731# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1732# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1733# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1734# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1735# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1736# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1737# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1738# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1739# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1740# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1741# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1742# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1743# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1744# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1745# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1746# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1747#ifdef RIPPER
1748extern const ID id_warn, id_warning, id_gets, id_assoc;
1749# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1750# define WARN_S_L(s,l) STR_NEW(s,l)
1751# define WARN_S(s) STR_NEW2(s)
1752# define WARN_I(i) INT2NUM(i)
1753# define WARN_ID(i) rb_id2str(i)
1754# define PRIsWARN PRIsVALUE
1755# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1756# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1757# ifdef HAVE_VA_ARGS_MACRO
1758# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1759# else
1760# define WARN_CALL rb_funcall
1761# endif
1762# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1763# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1764# ifdef HAVE_VA_ARGS_MACRO
1765# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1766# else
1767# define WARNING_CALL rb_funcall
1768# endif
1769# define compile_error ripper_compile_error
1770#else
1771# define WARN_S_L(s,l) s
1772# define WARN_S(s) s
1773# define WARN_I(i) i
1774# define WARN_ID(i) rb_id2name(i)
1775# define PRIsWARN PRIsVALUE
1776# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1777# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1778# define WARN_CALL rb_compile_warn
1779# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1780# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1781# define WARNING_CALL rb_compile_warning
1782PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1783# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1784#endif
1785
1786#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1787
1788static NODE *
1789add_block_exit(struct parser_params *p, NODE *node)
1790{
1791 if (!node) {
1792 compile_error(p, "unexpected null node");
1793 return 0;
1794 }
1795 switch (nd_type(node)) {
1796 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1797 default:
1798 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1799 return node;
1800 }
1801 if (!p->ctxt.in_defined) {
1802 rb_node_exits_t *exits = p->exits;
1803 if (exits) {
1804 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1805 exits->nd_stts = node;
1806 }
1807 }
1808 return node;
1809}
1810
1811static rb_node_exits_t *
1812init_block_exit(struct parser_params *p)
1813{
1814 rb_node_exits_t *old = p->exits;
1815 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1816 exits->nd_chain = 0;
1817 exits->nd_stts = RNODE(exits);
1818 p->exits = exits;
1819 return old;
1820}
1821
1822static rb_node_exits_t *
1823allow_block_exit(struct parser_params *p)
1824{
1825 rb_node_exits_t *exits = p->exits;
1826 p->exits = 0;
1827 return exits;
1828}
1829
1830static void
1831restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1832{
1833 p->exits = exits;
1834}
1835
1836static void
1837clear_block_exit(struct parser_params *p, bool error)
1838{
1839 rb_node_exits_t *exits = p->exits;
1840 if (!exits) return;
1841 if (error) {
1842 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1843 switch (nd_type(e)) {
1844 case NODE_BREAK:
1845 yyerror1(&e->nd_loc, "Invalid break");
1846 break;
1847 case NODE_NEXT:
1848 yyerror1(&e->nd_loc, "Invalid next");
1849 break;
1850 case NODE_REDO:
1851 yyerror1(&e->nd_loc, "Invalid redo");
1852 break;
1853 default:
1854 yyerror1(&e->nd_loc, "unexpected node");
1855 goto end_checks; /* no nd_chain */
1856 }
1857 }
1858 end_checks:;
1859 }
1860 exits->nd_stts = RNODE(exits);
1861 exits->nd_chain = 0;
1862}
1863
1864#define WARN_EOL(tok) \
1865 (looking_at_eol_p(p) ? \
1866 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1867 (void)0)
1868static int looking_at_eol_p(struct parser_params *p);
1869
1870static NODE *
1871get_nd_value(struct parser_params *p, NODE *node)
1872{
1873 switch (nd_type(node)) {
1874 case NODE_GASGN:
1875 return RNODE_GASGN(node)->nd_value;
1876 case NODE_IASGN:
1877 return RNODE_IASGN(node)->nd_value;
1878 case NODE_LASGN:
1879 return RNODE_LASGN(node)->nd_value;
1880 case NODE_DASGN:
1881 return RNODE_DASGN(node)->nd_value;
1882 case NODE_MASGN:
1883 return RNODE_MASGN(node)->nd_value;
1884 case NODE_CVASGN:
1885 return RNODE_CVASGN(node)->nd_value;
1886 case NODE_CDECL:
1887 return RNODE_CDECL(node)->nd_value;
1888 default:
1889 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1890 return 0;
1891 }
1892}
1893
1894static void
1895set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1896{
1897 switch (nd_type(node)) {
1898 case NODE_CDECL:
1899 RNODE_CDECL(node)->nd_value = rhs;
1900 break;
1901 case NODE_GASGN:
1902 RNODE_GASGN(node)->nd_value = rhs;
1903 break;
1904 case NODE_IASGN:
1905 RNODE_IASGN(node)->nd_value = rhs;
1906 break;
1907 case NODE_LASGN:
1908 RNODE_LASGN(node)->nd_value = rhs;
1909 break;
1910 case NODE_DASGN:
1911 RNODE_DASGN(node)->nd_value = rhs;
1912 break;
1913 case NODE_MASGN:
1914 RNODE_MASGN(node)->nd_value = rhs;
1915 break;
1916 case NODE_CVASGN:
1917 RNODE_CVASGN(node)->nd_value = rhs;
1918 break;
1919 default:
1920 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1921 break;
1922 }
1923}
1924
1925static ID
1926get_nd_vid(struct parser_params *p, NODE *node)
1927{
1928 switch (nd_type(node)) {
1929 case NODE_CDECL:
1930 return RNODE_CDECL(node)->nd_vid;
1931 case NODE_GASGN:
1932 return RNODE_GASGN(node)->nd_vid;
1933 case NODE_IASGN:
1934 return RNODE_IASGN(node)->nd_vid;
1935 case NODE_LASGN:
1936 return RNODE_LASGN(node)->nd_vid;
1937 case NODE_DASGN:
1938 return RNODE_DASGN(node)->nd_vid;
1939 case NODE_CVASGN:
1940 return RNODE_CVASGN(node)->nd_vid;
1941 default:
1942 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1943 return 0;
1944 }
1945}
1946
1947static NODE *
1948get_nd_args(struct parser_params *p, NODE *node)
1949{
1950 switch (nd_type(node)) {
1951 case NODE_CALL:
1952 return RNODE_CALL(node)->nd_args;
1953 case NODE_OPCALL:
1954 return RNODE_OPCALL(node)->nd_args;
1955 case NODE_FCALL:
1956 return RNODE_FCALL(node)->nd_args;
1957 case NODE_QCALL:
1958 return RNODE_QCALL(node)->nd_args;
1959 case NODE_SUPER:
1960 return RNODE_SUPER(node)->nd_args;
1961 case NODE_VCALL:
1962 case NODE_ZSUPER:
1963 case NODE_YIELD:
1964 case NODE_RETURN:
1965 case NODE_BREAK:
1966 case NODE_NEXT:
1967 return 0;
1968 default:
1969 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1970 return 0;
1971 }
1972}
1973
1974static st_index_t
1975djb2(const uint8_t *str, size_t len)
1976{
1977 st_index_t hash = 5381;
1978
1979 for (size_t i = 0; i < len; i++) {
1980 hash = ((hash << 5) + hash) + str[i];
1981 }
1982
1983 return hash;
1984}
1985
1986static st_index_t
1987parser_memhash(const void *ptr, long len)
1988{
1989 return djb2(ptr, len);
1990}
1991
1992#define PARSER_STRING_PTR(str) (str->ptr)
1993#define PARSER_STRING_LEN(str) (str->len)
1994#define PARSER_STRING_END(str) (&str->ptr[str->len])
1995#define STRING_SIZE(str) ((size_t)str->len + 1)
1996#define STRING_TERM_LEN(str) (1)
1997#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1998#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1999 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
2000 str->len = total; \
2001} while (0)
2002#define STRING_SET_LEN(str, n) do { \
2003 (str)->len = (n); \
2004} while (0)
2005#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2006 ((ptrvar) = str->ptr, \
2007 (lenvar) = str->len)
2008
2009static inline int
2010parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2011{
2012 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2013}
2014
2015static rb_parser_string_t *
2016rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2017{
2018 rb_parser_string_t *str;
2019
2020 if (len < 0) {
2021 rb_bug("negative string size (or size too big): %ld", len);
2022 }
2023
2024 str = xcalloc(1, sizeof(rb_parser_string_t));
2025 str->ptr = xcalloc(len + 1, sizeof(char));
2026
2027 if (ptr) {
2028 memcpy(PARSER_STRING_PTR(str), ptr, len);
2029 }
2030 STRING_SET_LEN(str, len);
2031 STRING_TERM_FILL(str);
2032 return str;
2033}
2034
2035static rb_parser_string_t *
2036rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2037{
2038 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2039 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2040 str->enc = enc;
2041 return str;
2042}
2043
2044#ifndef RIPPER
2045rb_parser_string_t *
2046rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2047{
2048 /* Type check */
2049 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2050 RB_GC_GUARD(str);
2051 return ret;
2052}
2053
2054void
2055rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2056{
2057 if (!str) return;
2058 xfree(PARSER_STRING_PTR(str));
2059 xfree(str);
2060}
2061#endif
2062
2063static st_index_t
2064rb_parser_str_hash(rb_parser_string_t *str)
2065{
2066 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2067}
2068
2069static st_index_t
2070rb_char_p_hash(const char *c)
2071{
2072 return parser_memhash((const void *)c, strlen(c));
2073}
2074
2075static size_t
2076rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2077{
2078 return PARSER_STRING_LEN(str);
2079}
2080
2081#ifndef RIPPER
2082static char *
2083rb_parser_string_end(rb_parser_string_t *str)
2084{
2085 return &str->ptr[str->len];
2086}
2087#endif
2088
2089static void
2090rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2091{
2092 str->enc = enc;
2093}
2094
2095static rb_encoding *
2096rb_parser_str_get_encoding(rb_parser_string_t *str)
2097{
2098 return str->enc;
2099}
2100
2101#ifndef RIPPER
2102static bool
2103PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2104{
2105 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2106}
2107#endif
2108
2109static int
2110PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2111{
2112 return str->coderange;
2113}
2114
2115static void
2116PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2117{
2118 str->coderange = coderange;
2119}
2120
2121static void
2122PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2123{
2124 rb_parser_string_set_encoding(str, enc);
2125 PARSER_ENC_CODERANGE_SET(str, cr);
2126}
2127
2128static void
2129PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2130{
2131 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2132}
2133
2134static bool
2135PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2136{
2137 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2138}
2139
2140static bool
2141PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2142{
2143 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2144}
2145
2146static const char *
2147rb_parser_search_nonascii(const char *p, const char *e)
2148{
2149 const char *s = p;
2150
2151 for (; s < e; s++) {
2152 if (*s & 0x80) return s;
2153 }
2154
2155 return NULL;
2156}
2157
2158static int
2159rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2160{
2161 const char *e = ptr + len;
2162
2163 if (enc == rb_ascii8bit_encoding()) {
2164 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2165 ptr = rb_parser_search_nonascii(ptr, e);
2166 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2167 }
2168
2169 /* parser string encoding is always asciicompat */
2170 ptr = rb_parser_search_nonascii(ptr, e);
2171 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2172 for (;;) {
2173 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2174 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2175 ptr += MBCLEN_CHARFOUND_LEN(ret);
2176 if (ptr == e) break;
2177 ptr = rb_parser_search_nonascii(ptr, e);
2178 if (!ptr) break;
2179 }
2180
2181 return RB_PARSER_ENC_CODERANGE_VALID;
2182}
2183
2184static int
2185rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2186{
2187 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2188}
2189
2190static int
2191rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2192{
2193 int cr = PARSER_ENC_CODERANGE(str);
2194
2195 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2196 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2197 PARSER_ENC_CODERANGE_SET(str, cr);
2198 }
2199
2200 return cr;
2201}
2202
2203static rb_parser_string_t *
2204rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2205{
2206 if (rb_parser_str_get_encoding(str) == enc)
2207 return str;
2208 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2209 PARSER_ENC_CODERANGE_CLEAR(str);
2210 }
2211 rb_parser_string_set_encoding(str, enc);
2212 return str;
2213}
2214
2215static bool
2216rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2217{
2218 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2219}
2220
2221static rb_encoding *
2222rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2223{
2224 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2225 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2226
2227 if (enc1 == NULL || enc2 == NULL)
2228 return 0;
2229
2230 if (enc1 == enc2) {
2231 return enc1;
2232 }
2233
2234 if (PARSER_STRING_LEN(str2) == 0)
2235 return enc1;
2236 if (PARSER_STRING_LEN(str1) == 0)
2237 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2238
2239 int cr1, cr2;
2240
2241 cr1 = rb_parser_enc_str_coderange(p, str1);
2242 cr2 = rb_parser_enc_str_coderange(p, str2);
2243
2244 if (cr1 != cr2) {
2245 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2246 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2247 }
2248
2249 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2250 return enc1;
2251 }
2252
2253 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2254 return enc2;
2255 }
2256
2257 return 0;
2258}
2259
2260static void
2261rb_parser_str_modify(rb_parser_string_t *str)
2262{
2263 PARSER_ENC_CODERANGE_CLEAR(str);
2264}
2265
2266static void
2267rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2268{
2269 long capa;
2270 const int termlen = STRING_TERM_LEN(str);
2271
2272 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2273 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2274 }
2275
2276 int cr = PARSER_ENC_CODERANGE(str);
2277 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2278 /* Leave unknown. */
2279 }
2280 else if (len > PARSER_STRING_LEN(str)) {
2281 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2282 }
2283 else if (len < PARSER_STRING_LEN(str)) {
2284 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2285 /* ASCII-only string is keeping after truncated. Valid
2286 * and broken may be invalid or valid, leave unknown. */
2287 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2288 }
2289 }
2290
2291 STRING_SET_LEN(str, len);
2292 STRING_TERM_FILL(str);
2293}
2294
2295static rb_parser_string_t *
2296rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2297{
2298 rb_parser_str_modify(str);
2299 if (len == 0) return 0;
2300
2301 long total, olen, off = -1;
2302 char *sptr;
2303 const int termlen = STRING_TERM_LEN(str);
2304
2305 PARSER_STRING_GETMEM(str, sptr, olen);
2306 if (ptr >= sptr && ptr <= sptr + olen) {
2307 off = ptr - sptr;
2308 }
2309
2310 if (olen > LONG_MAX - len) {
2311 compile_error(p, "string sizes too big");
2312 return 0;
2313 }
2314 total = olen + len;
2315 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2316 sptr = PARSER_STRING_PTR(str);
2317 if (off != -1) {
2318 ptr = sptr + off;
2319 }
2320 memcpy(sptr + olen, ptr, len);
2321 STRING_SET_LEN(str, total);
2322 STRING_TERM_FILL(str);
2323
2324 return str;
2325}
2326
2327#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2328#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2329
2330static rb_parser_string_t *
2331rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2332 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2333{
2334 int str_cr, res_cr;
2335 rb_encoding *str_enc, *res_enc;
2336
2337 str_enc = rb_parser_str_get_encoding(str);
2338 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2339
2340 if (str_enc == ptr_enc) {
2341 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2342 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2343 }
2344 }
2345 else {
2346 /* parser string encoding is always asciicompat */
2347 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2348 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2349 }
2350 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2351 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2352 str_cr = rb_parser_enc_str_coderange(p, str);
2353 }
2354 }
2355 }
2356 if (ptr_cr_ret)
2357 *ptr_cr_ret = ptr_cr;
2358
2359 if (str_enc != ptr_enc &&
2360 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2361 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2362 goto incompatible;
2363 }
2364
2365 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2366 res_enc = str_enc;
2367 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2368 }
2369 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2370 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2371 res_enc = str_enc;
2372 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2373 }
2374 else {
2375 res_enc = ptr_enc;
2376 res_cr = ptr_cr;
2377 }
2378 }
2379 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2380 res_enc = str_enc;
2381 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2382 res_cr = str_cr;
2383 else
2384 res_cr = ptr_cr;
2385 }
2386 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2387 res_enc = str_enc;
2388 res_cr = str_cr;
2389 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2390 }
2391
2392 if (len < 0) {
2393 compile_error(p, "negative string size (or size too big)");
2394 }
2395 parser_str_cat(str, ptr, len);
2396 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2397 return str;
2398
2399 incompatible:
2400 compile_error(p, "incompatible character encodings: %s and %s",
2401 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2402 UNREACHABLE_RETURN(0);
2403
2404}
2405
2406static rb_parser_string_t *
2407rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2408 rb_encoding *ptr_enc)
2409{
2410 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2411}
2412
2413static rb_parser_string_t *
2414rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2415{
2416 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2417
2418 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2419 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2420
2421 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2422
2423 return str;
2424}
2425
2426static rb_parser_string_t *
2427rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2428{
2429 if (len < 0) {
2430 rb_bug("negative string size (or size too big)");
2431 }
2432
2433 long slen = PARSER_STRING_LEN(str);
2434
2435 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2436 PARSER_ENC_CODERANGE_CLEAR(str);
2437 }
2438
2439 {
2440 long capa;
2441 const int termlen = STRING_TERM_LEN(str);
2442
2443 if ((capa = slen) < len) {
2444 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2445 }
2446 else if (len == slen) return str;
2447 STRING_SET_LEN(str, len);
2448 STRING_TERM_FILL(str);
2449 }
2450 return str;
2451}
2452
2453# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2454 ((ptrvar) = str->ptr, \
2455 (lenvar) = str->len, \
2456 (encvar) = str->enc)
2457
2458static int
2459rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2460{
2461 long len1, len2;
2462 const char *ptr1, *ptr2;
2463 rb_encoding *enc1, *enc2;
2464
2465 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2466 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2467
2468 return (len1 != len2 ||
2469 enc1 != enc2 ||
2470 memcmp(ptr1, ptr2, len1) != 0);
2471}
2472
2473static void
2474rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2475{
2476 long i;
2477 if (ary->capa < len) {
2478 ary->capa = len;
2479 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2480 for (i = ary->len; i < len; i++) {
2481 ary->data[i] = 0;
2482 }
2483 }
2484}
2485
2486/*
2487 * Do not call this directly.
2488 * Use rb_parser_ary_new_capa_for_XXX() instead.
2489 */
2490static rb_parser_ary_t *
2491parser_ary_new_capa(rb_parser_t *p, long len)
2492{
2493 if (len < 0) {
2494 rb_bug("negative array size (or size too big): %ld", len);
2495 }
2496 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2497 ary->data_type = 0;
2498 ary->len = 0;
2499 ary->capa = len;
2500 if (0 < len) {
2501 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2502 }
2503 else {
2504 ary->data = NULL;
2505 }
2506 return ary;
2507}
2508
2509#ifndef RIPPER
2510static rb_parser_ary_t *
2511rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2512{
2513 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2514 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2515 return ary;
2516}
2517
2518static rb_parser_ary_t *
2519rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2520{
2521 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2522 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2523 return ary;
2524}
2525#endif
2526
2527static rb_parser_ary_t *
2528rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2529{
2530 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2531 ary->data_type = PARSER_ARY_DATA_NODE;
2532 return ary;
2533}
2534
2535/*
2536 * Do not call this directly.
2537 * Use rb_parser_ary_push_XXX() instead.
2538 */
2539static rb_parser_ary_t *
2540parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2541{
2542 if (ary->len == ary->capa) {
2543 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2544 }
2545 ary->data[ary->len++] = val;
2546 return ary;
2547}
2548
2549#ifndef RIPPER
2550static rb_parser_ary_t *
2551rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2552{
2553 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2554 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2555 }
2556 return parser_ary_push(p, ary, val);
2557}
2558
2559static rb_parser_ary_t *
2560rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2561{
2562 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2563 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2564 }
2565 return parser_ary_push(p, ary, val);
2566}
2567#endif
2568
2569static rb_parser_ary_t *
2570rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2571{
2572 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2573 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2574 }
2575 return parser_ary_push(p, ary, val);
2576}
2577
2578#ifndef RIPPER
2579static void
2580rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2581{
2582 if (!token) return;
2583 rb_parser_string_free(p, token->str);
2584 xfree(token);
2585}
2586
2587static void
2588rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2589{
2590# define foreach_ary(ptr) \
2591 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2592 ptr < end_ary_data; ptr++)
2593 switch (ary->data_type) {
2594 case PARSER_ARY_DATA_AST_TOKEN:
2595 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2596 break;
2597 case PARSER_ARY_DATA_SCRIPT_LINE:
2598 foreach_ary(data) {rb_parser_string_free(p, *data);}
2599 break;
2600 case PARSER_ARY_DATA_NODE:
2601 /* Do nothing because nodes are freed when rb_ast_t is freed */
2602 break;
2603 default:
2604 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2605 break;
2606 }
2607# undef foreach_ary
2608 xfree(ary->data);
2609 xfree(ary);
2610}
2611
2612#endif /* !RIPPER */
2613%}
2614
2615%expect 0
2616%define api.pure
2617%define parse.error verbose
2618%printer {
2619 if ((NODE *)$$ == (NODE *)-1) {
2620 rb_parser_printf(p, "NODE_SPECIAL");
2621 }
2622 else if ($$) {
2623 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2624 }
2625} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2626 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2627%printer {
2628 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2629} <id>
2630%printer {
2631 switch (nd_type(RNODE($$))) {
2632 case NODE_INTEGER:
2633 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2634 break;
2635 case NODE_FLOAT:
2636 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2637 break;
2638 case NODE_RATIONAL:
2639 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2640 break;
2641 case NODE_IMAGINARY:
2642 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2643 break;
2644 default:
2645 break;
2646 }
2647} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2648%printer {
2649 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2650} tNTH_REF
2651%printer {
2652 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2653} tBACK_REF
2654
2655%destructor {
2656 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2657} <labels>
2658
2659%lex-param {struct parser_params *p}
2660%parse-param {struct parser_params *p}
2661%initial-action
2662{
2663 RUBY_SET_YYLLOC_OF_NONE(@$);
2664};
2665%after-shift after_shift
2666%before-reduce before_reduce
2667%after-reduce after_reduce
2668%after-shift-error-token after_shift_error_token
2669%after-pop-stack after_pop_stack
2670
2671%union {
2672 NODE *node;
2673 rb_node_fcall_t *node_fcall;
2674 rb_node_args_t *node_args;
2675 rb_node_args_aux_t *node_args_aux;
2676 rb_node_opt_arg_t *node_opt_arg;
2677 rb_node_kw_arg_t *node_kw_arg;
2678 rb_node_block_pass_t *node_block_pass;
2679 rb_node_masgn_t *node_masgn;
2680 rb_node_def_temp_t *node_def_temp;
2681 rb_node_exits_t *node_exits;
2682 struct rb_locations_lambda_body_t *locations_lambda_body;
2683 ID id;
2684 int num;
2685 st_table *tbl;
2686 st_table *labels;
2687 const struct vtable *vars;
2688 struct rb_strterm_struct *strterm;
2689 struct lex_context ctxt;
2690 enum lex_state_e state;
2691}
2692
2693%token <id>
2694 keyword_class "'class'"
2695 keyword_module "'module'"
2696 keyword_def "'def'"
2697 keyword_undef "'undef'"
2698 keyword_begin "'begin'"
2699 keyword_rescue "'rescue'"
2700 keyword_ensure "'ensure'"
2701 keyword_end "'end'"
2702 keyword_if "'if'"
2703 keyword_unless "'unless'"
2704 keyword_then "'then'"
2705 keyword_elsif "'elsif'"
2706 keyword_else "'else'"
2707 keyword_case "'case'"
2708 keyword_when "'when'"
2709 keyword_while "'while'"
2710 keyword_until "'until'"
2711 keyword_for "'for'"
2712 keyword_break "'break'"
2713 keyword_next "'next'"
2714 keyword_redo "'redo'"
2715 keyword_retry "'retry'"
2716 keyword_in "'in'"
2717 keyword_do "'do'"
2718 keyword_do_cond "'do' for condition"
2719 keyword_do_block "'do' for block"
2720 keyword_do_LAMBDA "'do' for lambda"
2721 keyword_return "'return'"
2722 keyword_yield "'yield'"
2723 keyword_super "'super'"
2724 keyword_self "'self'"
2725 keyword_nil "'nil'"
2726 keyword_true "'true'"
2727 keyword_false "'false'"
2728 keyword_and "'and'"
2729 keyword_or "'or'"
2730 keyword_not "'not'"
2731 modifier_if "'if' modifier"
2732 modifier_unless "'unless' modifier"
2733 modifier_while "'while' modifier"
2734 modifier_until "'until' modifier"
2735 modifier_rescue "'rescue' modifier"
2736 keyword_alias "'alias'"
2737 keyword_defined "'defined?'"
2738 keyword_BEGIN "'BEGIN'"
2739 keyword_END "'END'"
2740 keyword__LINE__ "'__LINE__'"
2741 keyword__FILE__ "'__FILE__'"
2742 keyword__ENCODING__ "'__ENCODING__'"
2743
2744%token <id> tIDENTIFIER "local variable or method"
2745%token <id> tFID "method"
2746%token <id> tGVAR "global variable"
2747%token <id> tIVAR "instance variable"
2748%token <id> tCONSTANT "constant"
2749%token <id> tCVAR "class variable"
2750%token <id> tLABEL "label"
2751%token <node> tINTEGER "integer literal"
2752%token <node> tFLOAT "float literal"
2753%token <node> tRATIONAL "rational literal"
2754%token <node> tIMAGINARY "imaginary literal"
2755%token <node> tCHAR "char literal"
2756%token <node> tNTH_REF "numbered reference"
2757%token <node> tBACK_REF "back reference"
2758%token <node> tSTRING_CONTENT "literal content"
2759%token <num> tREGEXP_END
2760%token <num> tDUMNY_END "dummy end"
2761
2762%type <node> singleton singleton_expr strings string string1 xstring regexp
2763%type <node> string_contents xstring_contents regexp_contents string_content
2764%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2765%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2766%type <node_def_temp> defn_head defs_head k_def
2767%type <node_exits> block_open k_while k_until k_for allow_exits
2768%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2769%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2770%type <node> command command_call command_call_value method_call
2771%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2772%type <node_fcall> fcall
2773%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2774%type <node> args arg_splat call_args opt_call_args
2775%type <node> paren_args opt_paren_args
2776%type <node_args> args_tail block_args_tail
2777%type <node> command_args aref_args
2778%type <node_block_pass> opt_block_arg block_arg
2779%type <node> var_ref var_lhs
2780%type <node> command_rhs arg_rhs
2781%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2782%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2783%type <node_args_aux> f_arg f_arg_item
2784%type <node> f_marg f_rest_marg
2785%type <node_masgn> f_margs
2786%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2787%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2788%type <id> do bv_decls opt_bv_decl bvar
2789%type <node> lambda brace_body do_body
2790%type <locations_lambda_body> lambda_body
2791%type <node_args> f_larglist
2792%type <node> brace_block cmd_brace_block do_block lhs none fitem
2793%type <node> mlhs_head mlhs_item mlhs_node
2794%type <node_masgn> mlhs mlhs_basic mlhs_inner
2795%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2796%type <node> p_expr p_as p_alt p_expr_basic p_find
2797%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2798%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2799%type <node> p_kwargs p_kwarg p_kw
2800%type <id> keyword_variable user_variable sym operation2 operation3
2801%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2802%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2803%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2804%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2805%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2806%type <ctxt> p_in_kwarg
2807%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2808%type <num> max_numparam
2809%type <node> numparam
2810%type <id> it_id
2811%token END_OF_INPUT 0 "end-of-input"
2812%token <id> '.'
2813
2814/* escaped chars, should be ignored otherwise */
2815%token <id> '\\' "backslash"
2816%token tSP "escaped space"
2817%token <id> '\t' "escaped horizontal tab"
2818%token <id> '\f' "escaped form feed"
2819%token <id> '\r' "escaped carriage return"
2820%token <id> '\13' "escaped vertical tab"
2821%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2822%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2823%token tPOW RUBY_TOKEN(POW) "**"
2824%token tCMP RUBY_TOKEN(CMP) "<=>"
2825%token tEQ RUBY_TOKEN(EQ) "=="
2826%token tEQQ RUBY_TOKEN(EQQ) "==="
2827%token tNEQ RUBY_TOKEN(NEQ) "!="
2828%token tGEQ RUBY_TOKEN(GEQ) ">="
2829%token tLEQ RUBY_TOKEN(LEQ) "<="
2830%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2831%token tOROP RUBY_TOKEN(OROP) "||"
2832%token tMATCH RUBY_TOKEN(MATCH) "=~"
2833%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2834%token tDOT2 RUBY_TOKEN(DOT2) ".."
2835%token tDOT3 RUBY_TOKEN(DOT3) "..."
2836%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2837%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2838%token tAREF RUBY_TOKEN(AREF) "[]"
2839%token tASET RUBY_TOKEN(ASET) "[]="
2840%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2841%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2842%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2843%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2844%token tCOLON3 ":: at EXPR_BEG"
2845%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2846%token tASSOC "=>"
2847%token tLPAREN "("
2848%token tLPAREN_ARG "( arg"
2849%token tLBRACK "["
2850%token tLBRACE "{"
2851%token tLBRACE_ARG "{ arg"
2852%token tSTAR "*"
2853%token tDSTAR "**arg"
2854%token tAMPER "&"
2855%token <num> tLAMBDA "->"
2856%token tSYMBEG "symbol literal"
2857%token tSTRING_BEG "string literal"
2858%token tXSTRING_BEG "backtick literal"
2859%token tREGEXP_BEG "regexp literal"
2860%token tWORDS_BEG "word list"
2861%token tQWORDS_BEG "verbatim word list"
2862%token tSYMBOLS_BEG "symbol list"
2863%token tQSYMBOLS_BEG "verbatim symbol list"
2864%token tSTRING_END "terminator"
2865%token tSTRING_DEND "'}'"
2866%token <state> tSTRING_DBEG "'#{'"
2867%token tSTRING_DVAR tLAMBEG tLABEL_END
2868
2869%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2870%token tHEREDOC_BEG tHEREDOC_END k__END__
2871
2872/*
2873 * precedence table
2874 */
2875
2876%nonassoc tLOWEST
2877%nonassoc tLBRACE_ARG
2878
2879%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2880%left keyword_or keyword_and
2881%right keyword_not
2882%nonassoc keyword_defined
2883%right '=' tOP_ASGN
2884%left modifier_rescue
2885%right '?' ':'
2886%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2887%left tOROP
2888%left tANDOP
2889%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2890%left '>' tGEQ '<' tLEQ
2891%left '|' '^'
2892%left '&'
2893%left tLSHFT tRSHFT
2894%left '+' '-'
2895%left '*' '/' '%'
2896%right tUMINUS_NUM tUMINUS
2897%right tPOW
2898%right '!' '~' tUPLUS
2899
2900%token tLAST_TOKEN
2901
2902/*
2903 * inlining rules
2904 */
2905%rule %inline ident_or_const
2906 : tIDENTIFIER
2907 | tCONSTANT
2908 ;
2909
2910%rule %inline user_or_keyword_variable
2911 : user_variable
2912 | keyword_variable
2913 ;
2914
2915/*
2916 * parameterizing rules
2917 */
2918%rule asgn(rhs) <node>
2919 : lhs '=' lex_ctxt rhs
2920 {
2921 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2922 /*% ripper: assign!($:1, $:4) %*/
2923 }
2924 ;
2925
2926%rule args_tail_basic(value) <node_args>
2927 : f_kwarg(value) ',' f_kwrest opt_f_block_arg
2928 {
2929 $$ = new_args_tail(p, $1, $3, $4, &@3);
2930 /*% ripper: [$:1, $:3, $:4] %*/
2931 }
2932 | f_kwarg(value) opt_f_block_arg
2933 {
2934 $$ = new_args_tail(p, $1, 0, $2, &@1);
2935 /*% ripper: [$:1, Qnil, $:2] %*/
2936 }
2937 | f_any_kwrest opt_f_block_arg
2938 {
2939 $$ = new_args_tail(p, 0, $1, $2, &@1);
2940 /*% ripper: [Qnil, $:1, $:2] %*/
2941 }
2942 | f_block_arg
2943 {
2944 $$ = new_args_tail(p, 0, 0, $1, &@1);
2945 /*% ripper: [Qnil, Qnil, $:1] %*/
2946 }
2947 ;
2948
2949%rule def_endless_method(bodystmt) <node>
2950 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2951 {
2952 endless_method_name(p, $head->nd_mid, &@head);
2953 restore_defun(p, $head);
2954 ($$ = $head->nd_def)->nd_loc = @$;
2955 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2956 RNODE_DEFN($$)->nd_defn = $bodystmt;
2957 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2958 /*% ripper: def!($:head, $:args, $:$) %*/
2959 local_pop(p);
2960 }
2961 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2962 {
2963 endless_method_name(p, $head->nd_mid, &@head);
2964 restore_defun(p, $head);
2965 ($$ = $head->nd_def)->nd_loc = @$;
2966 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2967 RNODE_DEFS($$)->nd_defn = $bodystmt;
2968 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2969 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2970 local_pop(p);
2971 }
2972 ;
2973
2974%rule compstmt(stmts) <node>
2975 : stmts terms?
2976 {
2977 void_stmts(p, $$ = $stmts);
2978 }
2979 ;
2980
2981%rule f_opt(value) <node_opt_arg>
2982 : f_arg_asgn f_eq value
2983 {
2984 p->ctxt.in_argdef = 1;
2985 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2986 /*% ripper: [$:$, $:3] %*/
2987 }
2988 ;
2989
2990%rule f_opt_arg(value) <node_opt_arg>
2991 : f_opt(value)
2992 {
2993 $$ = $f_opt;
2994 /*% ripper: rb_ary_new3(1, $:1) %*/
2995 }
2996 | f_opt_arg(value) ',' f_opt(value)
2997 {
2998 $$ = opt_arg_append($f_opt_arg, $f_opt);
2999 /*% ripper: rb_ary_push($:1, $:3) %*/
3000 }
3001 ;
3002
3003%rule f_kw(value) <node_kw_arg>
3004 : f_label value
3005 {
3006 p->ctxt.in_argdef = 1;
3007 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3008 /*% ripper: [$:$, $:value] %*/
3009 }
3010 | f_label
3011 {
3012 p->ctxt.in_argdef = 1;
3013 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3014 /*% ripper: [$:$, 0] %*/
3015 }
3016 ;
3017
3018%rule f_kwarg(value) <node_kw_arg>
3019 : f_kw(value)
3020 {
3021 $$ = $f_kw;
3022 /*% ripper: rb_ary_new3(1, $:1) %*/
3023 }
3024 | f_kwarg(value) ',' f_kw(value)
3025 {
3026 $$ = kwd_append($f_kwarg, $f_kw);
3027 /*% ripper: rb_ary_push($:1, $:3) %*/
3028 }
3029 ;
3030
3031%rule mlhs_items(item) <node>
3032 : item
3033 {
3034 $$ = NEW_LIST($1, &@$);
3035 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3036 }
3037 | mlhs_items(item) ',' item
3038 {
3039 $$ = list_append(p, $1, $3);
3040 /*% ripper: mlhs_add!($:1, $:3) %*/
3041 }
3042 ;
3043
3044%rule op_asgn(rhs) <node>
3045 : var_lhs tOP_ASGN lex_ctxt rhs
3046 {
3047 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3048 /*% ripper: opassign!($:1, $:2, $:4) %*/
3049 }
3050 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3051 {
3052 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3053 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3054 }
3055 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3056 {
3057 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3058 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3059 }
3060 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3061 {
3062 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3063 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3064 }
3065 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3066 {
3067 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3068 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3069 }
3070 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3071 {
3072 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3073 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3074 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3075 }
3076 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3077 {
3078 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3079 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3080 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3081 }
3082 | backref tOP_ASGN lex_ctxt rhs
3083 {
3084 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3085 $$ = NEW_ERROR(&@$);
3086 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3087 }
3088 ;
3089
3090%rule opt_args_tail(tail) <node_args>
3091 : ',' tail
3092 {
3093 $$ = $tail;
3094 /*% ripper: $:2 %*/
3095 }
3096 | /* none */
3097 {
3098 $$ = new_args_tail(p, 0, 0, 0, &@0);
3099 /*% ripper: [Qnil, Qnil, Qnil] %*/
3100 }
3101 ;
3102
3103%rule range_expr(range) <node>
3104 : range tDOT2 range
3105 {
3106 value_expr(p, $1);
3107 value_expr(p, $3);
3108 $$ = NEW_DOT2($1, $3, &@$, &@2);
3109 /*% ripper: dot2!($:1, $:3) %*/
3110 }
3111 | range tDOT3 range
3112 {
3113 value_expr(p, $1);
3114 value_expr(p, $3);
3115 $$ = NEW_DOT3($1, $3, &@$, &@2);
3116 /*% ripper: dot3!($:1, $:3) %*/
3117 }
3118 | range tDOT2
3119 {
3120 value_expr(p, $1);
3121 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3122 /*% ripper: dot2!($:1, Qnil) %*/
3123 }
3124 | range tDOT3
3125 {
3126 value_expr(p, $1);
3127 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3128 /*% ripper: dot3!($:1, Qnil) %*/
3129 }
3130 | tBDOT2 range
3131 {
3132 value_expr(p, $2);
3133 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3134 /*% ripper: dot2!(Qnil, $:2) %*/
3135 }
3136 | tBDOT3 range
3137 {
3138 value_expr(p, $2);
3139 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3140 /*% ripper: dot3!(Qnil, $:2) %*/
3141 }
3142 ;
3143
3144%rule value_expr(value) <node>
3145 : value
3146 {
3147 value_expr(p, $1);
3148 $$ = $1;
3149 }
3150 ;
3151
3152%rule words(begin, word_list) <node>
3153 : begin ' '+ word_list tSTRING_END
3154 {
3155 $$ = make_list($word_list, &@$);
3156 /*% ripper: array!($:3) %*/
3157 }
3158 ;
3159
3160%%
3161program : {
3162 SET_LEX_STATE(EXPR_BEG);
3163 local_push(p, ifndef_ripper(1)+0);
3164 /* jumps are possible in the top-level loop. */
3165 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3166 }
3167 compstmt(top_stmts)
3168 {
3169 if ($2 && !compile_for_eval) {
3170 NODE *node = $2;
3171 /* last expression should not be void */
3172 if (nd_type_p(node, NODE_BLOCK)) {
3173 while (RNODE_BLOCK(node)->nd_next) {
3174 node = RNODE_BLOCK(node)->nd_next;
3175 }
3176 node = RNODE_BLOCK(node)->nd_head;
3177 }
3178 node = remove_begin(node);
3179 void_expr(p, node);
3180 }
3181 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3182 /*% ripper[final]: program!($:2) %*/
3183 local_pop(p);
3184 }
3185 ;
3186
3187top_stmts : none
3188 {
3189 $$ = NEW_BEGIN(0, &@$);
3190 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3191 }
3192 | top_stmt
3193 {
3194 $$ = newline_node($1);
3195 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3196 }
3197 | top_stmts terms top_stmt
3198 {
3199 $$ = block_append(p, $1, newline_node($3));
3200 /*% ripper: stmts_add!($:1, $:3) %*/
3201 }
3202 ;
3203
3204top_stmt : stmt
3205 {
3206 clear_block_exit(p, true);
3207 $$ = $1;
3208 }
3209 | keyword_BEGIN begin_block
3210 {
3211 $$ = $2;
3212 /*% ripper: $:2 %*/
3213 }
3214 ;
3215
3216block_open : '{' {$$ = init_block_exit(p);};
3217
3218begin_block : block_open compstmt(top_stmts) '}'
3219 {
3220 restore_block_exit(p, $block_open);
3221 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3222 NEW_BEGIN($2, &@$));
3223 $$ = NEW_BEGIN(0, &@$);
3224 /*% ripper: BEGIN!($:2) %*/
3225 }
3226 ;
3227
3228bodystmt : compstmt(stmts)[body]
3229 lex_ctxt[ctxt]
3230 opt_rescue
3231 k_else
3232 {
3233 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3234 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3235 }
3236 compstmt(stmts)[elsebody]
3237 {
3238 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3239 }
3240 opt_ensure
3241 {
3242 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3243 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3244 }
3245 | compstmt(stmts)[body]
3246 lex_ctxt[ctxt]
3247 opt_rescue
3248 {
3249 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3250 }
3251 opt_ensure
3252 {
3253 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3254 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3255 }
3256 ;
3257
3258stmts : none
3259 {
3260 $$ = NEW_BEGIN(0, &@$);
3261 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3262 }
3263 | stmt_or_begin
3264 {
3265 $$ = newline_node($1);
3266 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3267 }
3268 | stmts terms stmt_or_begin
3269 {
3270 $$ = block_append(p, $1, newline_node($3));
3271 /*% ripper: stmts_add!($:1, $:3) %*/
3272 }
3273 ;
3274
3275stmt_or_begin : stmt
3276 | keyword_BEGIN
3277 {
3278 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3279 }
3280 begin_block
3281 {
3282 $$ = $3;
3283 }
3284 ;
3285
3286allow_exits : {$$ = allow_block_exit(p);};
3287
3288k_END : keyword_END lex_ctxt
3289 {
3290 $$ = $2;
3291 p->ctxt.in_rescue = before_rescue;
3292 /*% ripper: $:2 %*/
3293 };
3294
3295stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3296 {
3297 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3298 /*% ripper: alias!($:2, $:4) %*/
3299 }
3300 | keyword_alias tGVAR tGVAR
3301 {
3302 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3303 /*% ripper: var_alias!($:2, $:3) %*/
3304 }
3305 | keyword_alias tGVAR tBACK_REF
3306 {
3307 char buf[2];
3308 buf[0] = '$';
3309 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3310 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3311 /*% ripper: var_alias!($:2, $:3) %*/
3312 }
3313 | keyword_alias tGVAR tNTH_REF
3314 {
3315 static const char mesg[] = "can't make alias for the number variables";
3316 /*%%%*/
3317 yyerror1(&@3, mesg);
3318 /*% %*/
3319 $$ = NEW_ERROR(&@$);
3320 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3321 }
3322 | keyword_undef undef_list
3323 {
3324 nd_set_first_loc($2, @1.beg_pos);
3325 RNODE_UNDEF($2)->keyword_loc = @1;
3326 $$ = $2;
3327 /*% ripper: undef!($:2) %*/
3328 }
3329 | stmt modifier_if expr_value
3330 {
3331 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3332 fixpos($$, $3);
3333 /*% ripper: if_mod!($:3, $:1) %*/
3334 }
3335 | stmt modifier_unless expr_value
3336 {
3337 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3338 fixpos($$, $3);
3339 /*% ripper: unless_mod!($:3, $:1) %*/
3340 }
3341 | stmt modifier_while expr_value
3342 {
3343 clear_block_exit(p, false);
3344 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3345 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3346 }
3347 else {
3348 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3349 }
3350 /*% ripper: while_mod!($:3, $:1) %*/
3351 }
3352 | stmt modifier_until expr_value
3353 {
3354 clear_block_exit(p, false);
3355 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3356 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3357 }
3358 else {
3359 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3360 }
3361 /*% ripper: until_mod!($:3, $:1) %*/
3362 }
3363 | stmt modifier_rescue after_rescue stmt
3364 {
3365 p->ctxt.in_rescue = $3.in_rescue;
3366 NODE *resq;
3367 YYLTYPE loc = code_loc_gen(&@2, &@4);
3368 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3369 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3370 /*% ripper: rescue_mod!($:1, $:4) %*/
3371 }
3372 | k_END allow_exits '{' compstmt(stmts) '}'
3373 {
3374 if (p->ctxt.in_def) {
3375 rb_warn0("END in method; use at_exit");
3376 }
3377 restore_block_exit(p, $allow_exits);
3378 p->ctxt = $k_END;
3379 {
3380 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, NULL /* parent */, &@$);
3381 $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
3382 RNODE_SCOPE(scope)->nd_parent = $$;
3383 }
3384 /*% ripper: END!($:compstmt) %*/
3385 }
3386 | command_asgn
3387 | mlhs '=' lex_ctxt command_call_value
3388 {
3389 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3390 /*% ripper: massign!($:1, $:4) %*/
3391 }
3392 | asgn(mrhs)
3393 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3394 after_rescue stmt[resbody]
3395 {
3396 p->ctxt.in_rescue = $3.in_rescue;
3397 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3398 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3399 loc.beg_pos = @mrhs_arg.beg_pos;
3400 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3401 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3402 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3403 }
3404 | mlhs '=' lex_ctxt mrhs_arg
3405 {
3406 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3407 /*% ripper: massign!($:1, $:4) %*/
3408 }
3409 | expr
3410 | error
3411 {
3412 (void)yynerrs;
3413 $$ = NEW_ERROR(&@$);
3414 }
3415 ;
3416
3417command_asgn : asgn(command_rhs)
3418 | op_asgn(command_rhs)
3419 | def_endless_method(endless_command)
3420 ;
3421
3422endless_command : command
3423 | endless_command modifier_rescue after_rescue arg
3424 {
3425 p->ctxt.in_rescue = $3.in_rescue;
3426 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3427 /*% ripper: rescue_mod!($:1, $:4) %*/
3428 }
3429 | keyword_not '\n'? endless_command
3430 {
3431 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3432 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3433 }
3434 ;
3435
3436command_rhs : command_call_value %prec tOP_ASGN
3437 | command_call_value modifier_rescue after_rescue stmt
3438 {
3439 p->ctxt.in_rescue = $3.in_rescue;
3440 YYLTYPE loc = code_loc_gen(&@2, &@4);
3441 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3442 /*% ripper: rescue_mod!($:1, $:4) %*/
3443 }
3444 | command_asgn
3445 ;
3446
3447expr : command_call
3448 | expr keyword_and expr
3449 {
3450 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3451 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3452 }
3453 | expr keyword_or expr
3454 {
3455 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3456 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3457 }
3458 | keyword_not '\n'? expr
3459 {
3460 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3461 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3462 }
3463 | '!' command_call
3464 {
3465 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3466 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3467 }
3468 | arg tASSOC
3469 {
3470 value_expr(p, $arg);
3471 }
3472 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3473 p_top_expr_body[body]
3474 {
3475 pop_pktbl(p, $p_pktbl);
3476 pop_pvtbl(p, $p_pvtbl);
3477 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3478 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3479 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3480 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@2), &@$, &NULL_LOC, &NULL_LOC);
3481 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3482 }
3483 | arg keyword_in
3484 {
3485 value_expr(p, $arg);
3486 }
3487 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3488 p_top_expr_body[body]
3489 {
3490 pop_pktbl(p, $p_pktbl);
3491 pop_pvtbl(p, $p_pvtbl);
3492 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3493 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3494 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3495 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3496 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3497 }
3498 | arg %prec tLBRACE_ARG
3499 ;
3500
3501def_name : fname
3502 {
3503 numparam_name(p, $fname);
3504 local_push(p, 0);
3505 p->ctxt.in_def = 1;
3506 p->ctxt.in_rescue = before_rescue;
3507 p->ctxt.cant_return = 0;
3508 $$ = $fname;
3509 }
3510 ;
3511
3512defn_head : k_def def_name
3513 {
3514 $$ = def_head_save(p, $k_def);
3515 $$->nd_mid = $def_name;
3516 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3517 /*% ripper: $:def_name %*/
3518 }
3519 ;
3520
3521defs_head : k_def singleton dot_or_colon
3522 {
3523 SET_LEX_STATE(EXPR_FNAME);
3524 }
3525 def_name
3526 {
3527 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3528 $$ = def_head_save(p, $k_def);
3529 $$->nd_mid = $def_name;
3530 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3531 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3532 }
3533 ;
3534
3535expr_value : value_expr(expr)
3536 | error
3537 {
3538 $$ = NEW_ERROR(&@$);
3539 }
3540 ;
3541
3542expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3543 {
3544 $$ = $2;
3545 /*% ripper: $:2 %*/
3546 }
3547 ;
3548
3549command_call : command
3550 | block_command
3551 ;
3552
3553command_call_value : value_expr(command_call)
3554 ;
3555
3556block_command : block_call
3557 | block_call call_op2 operation2 command_args
3558 {
3559 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3560 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3561 }
3562 ;
3563
3564cmd_brace_block : tLBRACE_ARG brace_body '}'
3565 {
3566 $$ = $2;
3567 set_embraced_location($$, &@1, &@3);
3568 /*% ripper: $:2 %*/
3569 }
3570 ;
3571
3572fcall : operation
3573 {
3574 $$ = NEW_FCALL($1, 0, &@$);
3575 /*% ripper: $:1 %*/
3576 }
3577 ;
3578
3579command : fcall command_args %prec tLOWEST
3580 {
3581 $1->nd_args = $2;
3582 nd_set_last_loc($1, @2.end_pos);
3583 $$ = (NODE *)$1;
3584 /*% ripper: command!($:1, $:2) %*/
3585 }
3586 | fcall command_args cmd_brace_block
3587 {
3588 block_dup_check(p, $2, $3);
3589 $1->nd_args = $2;
3590 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3591 fixpos($$, RNODE($1));
3592 nd_set_last_loc($1, @2.end_pos);
3593 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3594 }
3595 | primary_value call_op operation2 command_args %prec tLOWEST
3596 {
3597 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3598 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3599 }
3600 | primary_value call_op operation2 command_args cmd_brace_block
3601 {
3602 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3603 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3604 }
3605 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3606 {
3607 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3608 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3609 }
3610 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3611 {
3612 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3613 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3614 }
3615 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3616 {
3617 set_embraced_location($5, &@4, &@6);
3618 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3619 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3620 }
3621 | keyword_super command_args
3622 {
3623 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3624 fixpos($$, $2);
3625 /*% ripper: super!($:2) %*/
3626 }
3627 | k_yield command_args
3628 {
3629 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3630 fixpos($$, $2);
3631 /*% ripper: yield!($:2) %*/
3632 }
3633 | k_return call_args
3634 {
3635 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3636 /*% ripper: return!($:2) %*/
3637 }
3638 | keyword_break call_args
3639 {
3640 NODE *args = 0;
3641 args = ret_args(p, $2);
3642 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3643 /*% ripper: break!($:2) %*/
3644 }
3645 | keyword_next call_args
3646 {
3647 NODE *args = 0;
3648 args = ret_args(p, $2);
3649 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3650 /*% ripper: next!($:2) %*/
3651 }
3652 ;
3653
3654mlhs : mlhs_basic
3655 | tLPAREN mlhs_inner rparen
3656 {
3657 $$ = $2;
3658 /*% ripper: mlhs_paren!($:2) %*/
3659 }
3660 ;
3661
3662mlhs_inner : mlhs_basic
3663 | tLPAREN mlhs_inner rparen
3664 {
3665 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3666 /*% ripper: mlhs_paren!($:2) %*/
3667 }
3668 ;
3669
3670mlhs_basic : mlhs_head
3671 {
3672 $$ = NEW_MASGN($1, 0, &@$);
3673 /*% ripper: $:1 %*/
3674 }
3675 | mlhs_head mlhs_item
3676 {
3677 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3678 /*% ripper: mlhs_add!($:1, $:2) %*/
3679 }
3680 | mlhs_head tSTAR mlhs_node
3681 {
3682 $$ = NEW_MASGN($1, $3, &@$);
3683 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3684 }
3685 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3686 {
3687 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3688 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3689 }
3690 | mlhs_head tSTAR
3691 {
3692 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3693 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3694 }
3695 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3696 {
3697 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3698 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3699 }
3700 | tSTAR mlhs_node
3701 {
3702 $$ = NEW_MASGN(0, $2, &@$);
3703 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3704 }
3705 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3706 {
3707 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3708 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3709 }
3710 | tSTAR
3711 {
3712 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3713 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3714 }
3715 | tSTAR ',' mlhs_items(mlhs_item)
3716 {
3717 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3718 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3719 }
3720 ;
3721
3722mlhs_item : mlhs_node
3723 | tLPAREN mlhs_inner rparen
3724 {
3725 $$ = (NODE *)$2;
3726 /*% ripper: mlhs_paren!($:2) %*/
3727 }
3728 ;
3729
3730mlhs_head : mlhs_item ','
3731 {
3732 $$ = NEW_LIST($1, &@1);
3733 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3734 }
3735 | mlhs_head mlhs_item ','
3736 {
3737 $$ = list_append(p, $1, $2);
3738 /*% ripper: mlhs_add!($:1, $:2) %*/
3739 }
3740 ;
3741
3742
3743mlhs_node : user_or_keyword_variable
3744 {
3745 /*% ripper: var_field!($:1) %*/
3746 $$ = assignable(p, $1, 0, &@$);
3747 }
3748 | primary_value '[' opt_call_args rbracket
3749 {
3750 $$ = aryset(p, $1, $3, &@$);
3751 /*% ripper: aref_field!($:1, $:3) %*/
3752 }
3753 | primary_value call_op ident_or_const
3754 {
3755 anddot_multiple_assignment_check(p, &@2, $2);
3756 $$ = attrset(p, $1, $2, $3, &@$);
3757 /*% ripper: field!($:1, $:2, $:3) %*/
3758 }
3759 | primary_value tCOLON2 tIDENTIFIER
3760 {
3761 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3762 /*% ripper: const_path_field!($:1, $:3) %*/
3763 }
3764 | primary_value tCOLON2 tCONSTANT
3765 {
3766 /*% ripper: const_path_field!($:1, $:3) %*/
3767 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3768 }
3769 | tCOLON3 tCONSTANT
3770 {
3771 /*% ripper: top_const_field!($:2) %*/
3772 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3773 }
3774 | backref
3775 {
3776 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3777 $$ = NEW_ERROR(&@$);
3778 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3779 }
3780 ;
3781
3782lhs : user_or_keyword_variable
3783 {
3784 /*% ripper: var_field!($:1) %*/
3785 $$ = assignable(p, $1, 0, &@$);
3786 }
3787 | primary_value '[' opt_call_args rbracket
3788 {
3789 $$ = aryset(p, $1, $3, &@$);
3790 /*% ripper: aref_field!($:1, $:3) %*/
3791 }
3792 | primary_value call_op ident_or_const
3793 {
3794 $$ = attrset(p, $1, $2, $3, &@$);
3795 /*% ripper: field!($:1, $:2, $:3) %*/
3796 }
3797 | primary_value tCOLON2 tIDENTIFIER
3798 {
3799 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3800 /*% ripper: field!($:1, $:2, $:3) %*/
3801 }
3802 | primary_value tCOLON2 tCONSTANT
3803 {
3804 /*% ripper: const_path_field!($:1, $:3) %*/
3805 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3806 }
3807 | tCOLON3 tCONSTANT
3808 {
3809 /*% ripper: top_const_field!($:2) %*/
3810 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3811 }
3812 | backref
3813 {
3814 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3815 $$ = NEW_ERROR(&@$);
3816 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3817 }
3818 ;
3819
3820cname : tIDENTIFIER
3821 {
3822 static const char mesg[] = "class/module name must be CONSTANT";
3823 /*%%%*/
3824 yyerror1(&@1, mesg);
3825 /*% %*/
3826 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3827 }
3828 | tCONSTANT
3829 ;
3830
3831cpath : tCOLON3 cname
3832 {
3833 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3834 /*% ripper: top_const_ref!($:2) %*/
3835 }
3836 | cname
3837 {
3838 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3839 /*% ripper: const_ref!($:1) %*/
3840 }
3841 | primary_value tCOLON2 cname
3842 {
3843 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3844 /*% ripper: const_path_ref!($:1, $:3) %*/
3845 }
3846 ;
3847
3848fname : operation
3849 | op
3850 {
3851 SET_LEX_STATE(EXPR_ENDFN);
3852 $$ = $1;
3853 }
3854 | reswords
3855 ;
3856
3857fitem : fname
3858 {
3859 $$ = NEW_SYM(rb_id2str($1), &@$);
3860 /*% ripper: symbol_literal!($:1) %*/
3861 }
3862 | symbol
3863 ;
3864
3865undef_list : fitem
3866 {
3867 $$ = NEW_UNDEF($1, &@$);
3868 /*% ripper: rb_ary_new3(1, $:1) %*/
3869 }
3870 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3871 {
3872 nd_set_last_loc($1, @4.end_pos);
3873 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3874 /*% ripper: rb_ary_push($:1, $:4) %*/
3875 }
3876 ;
3877
3878op : '|' { $$ = '|'; }
3879 | '^' { $$ = '^'; }
3880 | '&' { $$ = '&'; }
3881 | tCMP { $$ = tCMP; }
3882 | tEQ { $$ = tEQ; }
3883 | tEQQ { $$ = tEQQ; }
3884 | tMATCH { $$ = tMATCH; }
3885 | tNMATCH { $$ = tNMATCH; }
3886 | '>' { $$ = '>'; }
3887 | tGEQ { $$ = tGEQ; }
3888 | '<' { $$ = '<'; }
3889 | tLEQ { $$ = tLEQ; }
3890 | tNEQ { $$ = tNEQ; }
3891 | tLSHFT { $$ = tLSHFT; }
3892 | tRSHFT { $$ = tRSHFT; }
3893 | '+' { $$ = '+'; }
3894 | '-' { $$ = '-'; }
3895 | '*' { $$ = '*'; }
3896 | tSTAR { $$ = '*'; }
3897 | '/' { $$ = '/'; }
3898 | '%' { $$ = '%'; }
3899 | tPOW { $$ = tPOW; }
3900 | tDSTAR { $$ = tDSTAR; }
3901 | '!' { $$ = '!'; }
3902 | '~' { $$ = '~'; }
3903 | tUPLUS { $$ = tUPLUS; }
3904 | tUMINUS { $$ = tUMINUS; }
3905 | tAREF { $$ = tAREF; }
3906 | tASET { $$ = tASET; }
3907 | '`' { $$ = '`'; }
3908 ;
3909
3910reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3911 | keyword_BEGIN | keyword_END
3912 | keyword_alias | keyword_and | keyword_begin
3913 | keyword_break | keyword_case | keyword_class | keyword_def
3914 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3915 | keyword_end | keyword_ensure | keyword_false
3916 | keyword_for | keyword_in | keyword_module | keyword_next
3917 | keyword_nil | keyword_not | keyword_or | keyword_redo
3918 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3919 | keyword_super | keyword_then | keyword_true | keyword_undef
3920 | keyword_when | keyword_yield | keyword_if | keyword_unless
3921 | keyword_while | keyword_until
3922 ;
3923
3924arg : asgn(arg_rhs)
3925 | op_asgn(arg_rhs)
3926 | range_expr(arg)
3927 | arg '+' arg
3928 {
3929 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3930 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3931 }
3932 | arg '-' arg
3933 {
3934 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3935 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3936 }
3937 | arg '*' arg
3938 {
3939 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3940 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3941 }
3942 | arg '/' arg
3943 {
3944 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3945 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3946 }
3947 | arg '%' arg
3948 {
3949 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3950 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3951 }
3952 | arg tPOW arg
3953 {
3954 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3955 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3956 }
3957 | tUMINUS_NUM simple_numeric tPOW arg
3958 {
3959 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3960 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3961 }
3962 | tUPLUS arg
3963 {
3964 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3965 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3966 }
3967 | tUMINUS arg
3968 {
3969 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3970 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3971 }
3972 | arg '|' arg
3973 {
3974 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3975 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3976 }
3977 | arg '^' arg
3978 {
3979 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3980 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3981 }
3982 | arg '&' arg
3983 {
3984 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3985 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3986 }
3987 | arg tCMP arg
3988 {
3989 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3990 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3991 }
3992 | rel_expr %prec tCMP
3993 | arg tEQ arg
3994 {
3995 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3996 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3997 }
3998 | arg tEQQ arg
3999 {
4000 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4001 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4002 }
4003 | arg tNEQ arg
4004 {
4005 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4006 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4007 }
4008 | arg tMATCH arg
4009 {
4010 $$ = match_op(p, $1, $3, &@2, &@$);
4011 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4012 }
4013 | arg tNMATCH arg
4014 {
4015 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4016 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4017 }
4018 | '!' arg
4019 {
4020 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4021 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4022 }
4023 | '~' arg
4024 {
4025 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4026 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4027 }
4028 | arg tLSHFT arg
4029 {
4030 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4031 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4032 }
4033 | arg tRSHFT arg
4034 {
4035 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4036 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4037 }
4038 | arg tANDOP arg
4039 {
4040 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4041 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4042 }
4043 | arg tOROP arg
4044 {
4045 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4046 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4047 }
4048 | keyword_defined '\n'? begin_defined arg
4049 {
4050 p->ctxt.in_defined = $3.in_defined;
4051 $$ = new_defined(p, $4, &@$, &@1);
4052 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4053 /*% ripper: defined!($:4) %*/
4054 }
4055 | def_endless_method(endless_arg)
4056 | ternary
4057 | primary
4058 ;
4059
4060ternary : arg '?' arg '\n'? ':' arg
4061 {
4062 value_expr(p, $1);
4063 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4064 fixpos($$, $1);
4065 /*% ripper: ifop!($:1, $:3, $:6) %*/
4066 }
4067 ;
4068
4069endless_arg : arg %prec modifier_rescue
4070 | endless_arg modifier_rescue after_rescue arg
4071 {
4072 p->ctxt.in_rescue = $3.in_rescue;
4073 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4074 /*% ripper: rescue_mod!($:1, $:4) %*/
4075 }
4076 | keyword_not '\n'? endless_arg
4077 {
4078 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4079 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4080 }
4081 ;
4082
4083relop : '>' {$$ = '>';}
4084 | '<' {$$ = '<';}
4085 | tGEQ {$$ = idGE;}
4086 | tLEQ {$$ = idLE;}
4087 ;
4088
4089rel_expr : arg relop arg %prec '>'
4090 {
4091 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4092 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4093 }
4094 | rel_expr relop arg %prec '>'
4095 {
4096 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4097 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4098 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4099 }
4100 ;
4101
4102lex_ctxt : none
4103 {
4104 $$ = p->ctxt;
4105 }
4106 ;
4107
4108begin_defined : lex_ctxt
4109 {
4110 p->ctxt.in_defined = 1;
4111 $$ = $1;
4112 }
4113 ;
4114
4115after_rescue : lex_ctxt
4116 {
4117 p->ctxt.in_rescue = after_rescue;
4118 $$ = $1;
4119 }
4120 ;
4121
4122arg_value : value_expr(arg)
4123 ;
4124
4125aref_args : none
4126 | args trailer
4127 | args ',' assocs trailer
4128 {
4129 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4130 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4131 }
4132 | assocs trailer
4133 {
4134 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4135 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4136 }
4137 ;
4138
4139arg_rhs : arg %prec tOP_ASGN
4140 {
4141 value_expr(p, $1);
4142 $$ = $1;
4143 }
4144 | arg modifier_rescue after_rescue arg
4145 {
4146 p->ctxt.in_rescue = $3.in_rescue;
4147 value_expr(p, $1);
4148 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4149 /*% ripper: rescue_mod!($:1, $:4) %*/
4150 }
4151 ;
4152
4153paren_args : '(' opt_call_args rparen
4154 {
4155 $$ = $2;
4156 /*% ripper: arg_paren!($:2) %*/
4157 }
4158 | '(' args ',' args_forward rparen
4159 {
4160 if (!check_forwarding_args(p)) {
4161 $$ = 0;
4162 }
4163 else {
4164 $$ = new_args_forward_call(p, $2, &@4, &@$);
4165 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4166 }
4167 }
4168 | '(' args_forward rparen
4169 {
4170 if (!check_forwarding_args(p)) {
4171 $$ = 0;
4172 }
4173 else {
4174 $$ = new_args_forward_call(p, 0, &@2, &@$);
4175 /*% ripper: arg_paren!($:2) %*/
4176 }
4177 }
4178 ;
4179
4180opt_paren_args : none
4181 | paren_args
4182 {
4183 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4184 }
4185 ;
4186
4187opt_call_args : none
4188 | call_args
4189 | args ','
4190 | args ',' assocs ','
4191 {
4192 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4193 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4194 }
4195 | assocs ','
4196 {
4197 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4198 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4199 }
4200 ;
4201
4202call_args : value_expr(command)
4203 {
4204 $$ = NEW_LIST($1, &@$);
4205 /*% ripper: args_add!(args_new!, $:1) %*/
4206 }
4207 | def_endless_method(endless_command)
4208 {
4209 $$ = NEW_LIST($1, &@$);
4210 /*% ripper: args_add!(args_new!, $:1) %*/
4211 }
4212 | args opt_block_arg
4213 {
4214 $$ = arg_blk_pass($1, $2);
4215 /*% ripper: args_add_block!($:1, $:2) %*/
4216 }
4217 | assocs opt_block_arg
4218 {
4219 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4220 $$ = arg_blk_pass($$, $2);
4221 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4222 }
4223 | args ',' assocs opt_block_arg
4224 {
4225 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4226 $$ = arg_blk_pass($$, $4);
4227 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4228 }
4229 | block_arg
4230 /*% ripper: args_add_block!(args_new!, $:1) %*/
4231 ;
4232
4233command_args : {
4234 /* If call_args starts with a open paren '(' or '[',
4235 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4236 * but the push must be done after CMDARG_PUSH(1).
4237 * So this code makes them consistent by first cancelling
4238 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4239 * and finally redoing CMDARG_PUSH(0).
4240 */
4241 int lookahead = 0;
4242 switch (yychar) {
4243 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4244 lookahead = 1;
4245 }
4246 if (lookahead) CMDARG_POP();
4247 CMDARG_PUSH(1);
4248 if (lookahead) CMDARG_PUSH(0);
4249 }
4250 call_args
4251 {
4252 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4253 * but the push must be done after CMDARG_POP() in the parser.
4254 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4255 * CMDARG_POP() to pop 1 pushed by command_args,
4256 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4257 */
4258 int lookahead = 0;
4259 switch (yychar) {
4260 case tLBRACE_ARG:
4261 lookahead = 1;
4262 }
4263 if (lookahead) CMDARG_POP();
4264 CMDARG_POP();
4265 if (lookahead) CMDARG_PUSH(0);
4266 $$ = $2;
4267 /*% ripper: $:2 %*/
4268 }
4269 ;
4270
4271block_arg : tAMPER arg_value
4272 {
4273 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4274 /*% ripper: $:2 %*/
4275 }
4276 | tAMPER
4277 {
4278 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4279 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4280 /*% ripper: Qnil %*/
4281 }
4282 ;
4283
4284opt_block_arg : ',' block_arg
4285 {
4286 $$ = $2;
4287 /*% ripper: $:2 %*/
4288 }
4289 | none
4290 {
4291 $$ = 0;
4292 /*% ripper: Qfalse %*/
4293 }
4294 ;
4295
4296/* value */
4297args : arg_value
4298 {
4299 $$ = NEW_LIST($arg_value, &@$);
4300 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4301 }
4302 | arg_splat
4303 {
4304 $$ = $arg_splat;
4305 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4306 }
4307 | args[non_last_args] ',' arg_value
4308 {
4309 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4310 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4311 }
4312 | args[non_last_args] ',' arg_splat
4313 {
4314 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4315 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4316 }
4317 ;
4318
4319/* value */
4320arg_splat : tSTAR arg_value
4321 {
4322 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4323 /*% ripper: $:arg_value %*/
4324 }
4325 | tSTAR /* none */
4326 {
4327 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4328 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4329 /*% ripper: Qnil %*/
4330 }
4331 ;
4332
4333/* value */
4334mrhs_arg : mrhs
4335 | arg_value
4336 ;
4337
4338/* value */
4339mrhs : args ',' arg_value
4340 {
4341 $$ = last_arg_append(p, $args, $arg_value, &@$);
4342 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4343 }
4344 | args ',' tSTAR arg_value
4345 {
4346 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4347 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4348 }
4349 | tSTAR arg_value
4350 {
4351 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4352 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4353 }
4354 ;
4355
4356%rule %inline inline_primary
4357 : literal
4358 | strings
4359 | xstring
4360 | regexp
4361 | words
4362 | qwords
4363 | symbols
4364 | qsymbols
4365 ;
4366
4367primary : inline_primary
4368 | var_ref
4369 | backref
4370 | tFID
4371 {
4372 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4373 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4374 }
4375 | k_begin
4376 {
4377 CMDARG_PUSH(0);
4378 }
4379 bodystmt
4380 k_end
4381 {
4382 CMDARG_POP();
4383 set_line_body($3, @1.end_pos.lineno);
4384 $$ = NEW_BEGIN($3, &@$);
4385 nd_set_line($$, @1.end_pos.lineno);
4386 /*% ripper: begin!($:3) %*/
4387 }
4388 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4389 {
4390 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4391 $$ = $2;
4392 /*% ripper: paren!($:2) %*/
4393 }
4394 | tLPAREN compstmt(stmts) ')'
4395 {
4396 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4397 $$ = NEW_BLOCK($2, &@$);
4398 /*% ripper: paren!($:2) %*/
4399 }
4400 | primary_value tCOLON2 tCONSTANT
4401 {
4402 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
4403 /*% ripper: const_path_ref!($:1, $:3) %*/
4404 }
4405 | tCOLON3 tCONSTANT
4406 {
4407 $$ = NEW_COLON3($2, &@$, &@1, &@2);
4408 /*% ripper: top_const_ref!($:2) %*/
4409 }
4410 | tLBRACK aref_args ']'
4411 {
4412 $$ = make_list($2, &@$);
4413 /*% ripper: array!($:2) %*/
4414 }
4415 | tLBRACE assoc_list '}'
4416 {
4417 $$ = new_hash(p, $2, &@$);
4418 RNODE_HASH($$)->nd_brace = TRUE;
4419 /*% ripper: hash!($:2) %*/
4420 }
4421 | k_return
4422 {
4423 $$ = NEW_RETURN(0, &@$, &@1);
4424 /*% ripper: return0! %*/
4425 }
4426 | k_yield '(' call_args rparen
4427 {
4428 $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4);
4429 /*% ripper: yield!(paren!($:3)) %*/
4430 }
4431 | k_yield '(' rparen
4432 {
4433 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4434 /*% ripper: yield!(paren!(args_new!)) %*/
4435 }
4436 | k_yield
4437 {
4438 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4439 /*% ripper: yield0! %*/
4440 }
4441 | keyword_defined '\n'? '(' begin_defined expr rparen
4442 {
4443 p->ctxt.in_defined = $4.in_defined;
4444 $$ = new_defined(p, $5, &@$, &@1);
4445 p->ctxt.has_trailing_semicolon = $4.has_trailing_semicolon;
4446 /*% ripper: defined!($:5) %*/
4447 }
4448 | keyword_not '(' expr rparen
4449 {
4450 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4451 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4452 }
4453 | keyword_not '(' rparen
4454 {
4455 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@2), &@2), METHOD_NOT, &@1, &@$);
4456 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4457 }
4458 | fcall brace_block
4459 {
4460 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4461 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4462 }
4463 | method_call
4464 | method_call brace_block
4465 {
4466 block_dup_check(p, get_nd_args(p, $1), $2);
4467 $$ = method_add_block(p, $1, $2, &@$);
4468 /*% ripper: method_add_block!($:1, $:2) %*/
4469 }
4470 | lambda
4471 | k_if expr_value then
4472 compstmt(stmts)
4473 if_tail
4474 k_end
4475 {
4476 if ($5 && nd_type_p($5, NODE_IF))
4477 RNODE_IF($5)->end_keyword_loc = @6;
4478
4479 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4480 fixpos($$, $2);
4481 /*% ripper: if!($:2, $:4, $:5) %*/
4482 }
4483 | k_unless expr_value then
4484 compstmt(stmts)
4485 opt_else
4486 k_end
4487 {
4488 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4489 fixpos($$, $2);
4490 /*% ripper: unless!($:2, $:4, $:5) %*/
4491 }
4492 | k_while expr_value_do
4493 compstmt(stmts)
4494 k_end
4495 {
4496 restore_block_exit(p, $1);
4497 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4498 fixpos($$, $2);
4499 /*% ripper: while!($:2, $:3) %*/
4500 }
4501 | k_until expr_value_do
4502 compstmt(stmts)
4503 k_end
4504 {
4505 restore_block_exit(p, $1);
4506 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4507 fixpos($$, $2);
4508 /*% ripper: until!($:2, $:3) %*/
4509 }
4510 | k_case expr_value terms?
4511 {
4512 $$ = p->case_labels;
4513 p->case_labels = CHECK_LITERAL_WHEN;
4514 }<labels>
4515 case_body
4516 k_end
4517 {
4518 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4519 p->case_labels = $4;
4520 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4521 fixpos($$, $2);
4522 /*% ripper: case!($:2, $:5) %*/
4523 }
4524 | k_case terms?
4525 {
4526 $$ = p->case_labels;
4527 p->case_labels = 0;
4528 }<labels>
4529 case_body
4530 k_end
4531 {
4532 if (p->case_labels) st_free_table(p->case_labels);
4533 p->case_labels = $3;
4534 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4535 /*% ripper: case!(Qnil, $:4) %*/
4536 }
4537 | k_case expr_value terms?
4538 p_case_body
4539 k_end
4540 {
4541 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4542 /*% ripper: case!($:2, $:4) %*/
4543 }
4544 | k_for for_var keyword_in
4545 {COND_PUSH(1);} expr_value do {COND_POP();}
4546 compstmt(stmts)
4547 k_end
4548 {
4549 restore_block_exit(p, $k_for);
4550 /*
4551 * for a, b, c in e
4552 * #=>
4553 * e.each{|*x| a, b, c = x}
4554 *
4555 * for a in e
4556 * #=>
4557 * e.each{|x| a, = x}
4558 */
4559 ID id = internal_id(p);
4560 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4561 rb_node_args_t *args;
4562 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4563 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4564 tbl->ids[0] = id; /* internal id */
4565
4566 switch (nd_type($for_var)) {
4567 case NODE_LASGN:
4568 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4569 set_nd_value(p, $for_var, internal_var);
4570 id = 0;
4571 m->nd_plen = 1;
4572 m->nd_next = $for_var;
4573 break;
4574 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4575 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4576 break;
4577 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4578 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);
4579 }
4580 /* {|*internal_id| <m> = internal_id; ... } */
4581 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4582 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4583 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4584 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4585 RNODE_SCOPE(scope)->nd_parent = $$;
4586 fixpos($$, $for_var);
4587 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4588 }
4589 | k_class cpath superclass
4590 {
4591 begin_definition("class", &@k_class, &@cpath);
4592 }
4593 bodystmt
4594 k_end
4595 {
4596 YYLTYPE inheritance_operator_loc = NULL_LOC;
4597 if ($superclass) {
4598 inheritance_operator_loc = @superclass;
4599 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4600 }
4601 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4602 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4603 set_line_body($bodystmt, @superclass.end_pos.lineno);
4604 nd_set_line($$, @superclass.end_pos.lineno);
4605 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4606 local_pop(p);
4607 p->ctxt.in_class = $k_class.in_class;
4608 p->ctxt.cant_return = $k_class.cant_return;
4609 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4610 }
4611 | k_class tLSHFT expr_value
4612 {
4613 begin_definition("", &@k_class, &@tLSHFT);
4614 }
4615 term
4616 bodystmt
4617 k_end
4618 {
4619 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4620 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4621 set_line_body($bodystmt, nd_line($expr_value));
4622 fixpos($$, $expr_value);
4623 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4624 local_pop(p);
4625 p->ctxt.in_def = $k_class.in_def;
4626 p->ctxt.in_class = $k_class.in_class;
4627 p->ctxt.cant_return = $k_class.cant_return;
4628 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4629 }
4630 | k_module cpath
4631 {
4632 begin_definition("module", &@k_module, &@cpath);
4633 }
4634 bodystmt
4635 k_end
4636 {
4637 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4638 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4639 set_line_body($bodystmt, @cpath.end_pos.lineno);
4640 nd_set_line($$, @cpath.end_pos.lineno);
4641 /*% ripper: module!($:cpath, $:bodystmt) %*/
4642 local_pop(p);
4643 p->ctxt.in_class = $k_module.in_class;
4644 p->ctxt.cant_return = $k_module.cant_return;
4645 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4646 }
4647 | defn_head[head]
4648 f_arglist[args]
4649 {
4650 push_end_expect_token_locations(p, &@head.beg_pos);
4651 }
4652 bodystmt
4653 k_end
4654 {
4655 restore_defun(p, $head);
4656 ($$ = $head->nd_def)->nd_loc = @$;
4657 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4658 RNODE_DEFN($$)->nd_defn = $bodystmt;
4659 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4660 local_pop(p);
4661 }
4662 | defs_head[head]
4663 f_arglist[args]
4664 {
4665 push_end_expect_token_locations(p, &@head.beg_pos);
4666 }
4667 bodystmt
4668 k_end
4669 {
4670 restore_defun(p, $head);
4671 ($$ = $head->nd_def)->nd_loc = @$;
4672 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4673 RNODE_DEFS($$)->nd_defn = $bodystmt;
4674 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4675 local_pop(p);
4676 }
4677 | keyword_break
4678 {
4679 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4680 /*% ripper: break!(args_new!) %*/
4681 }
4682 | keyword_next
4683 {
4684 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4685 /*% ripper: next!(args_new!) %*/
4686 }
4687 | keyword_redo
4688 {
4689 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4690 /*% ripper: redo! %*/
4691 }
4692 | keyword_retry
4693 {
4694 if (!p->ctxt.in_defined) {
4695 switch (p->ctxt.in_rescue) {
4696 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4697 case after_rescue: /* ok */ break;
4698 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4699 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4700 }
4701 }
4702 $$ = NEW_RETRY(&@$);
4703 /*% ripper: retry! %*/
4704 }
4705 ;
4706
4707primary_value : value_expr(primary)
4708 ;
4709
4710k_begin : keyword_begin
4711 {
4712 token_info_push(p, "begin", &@$);
4713 push_end_expect_token_locations(p, &@1.beg_pos);
4714 }
4715 ;
4716
4717k_if : keyword_if
4718 {
4719 WARN_EOL("if");
4720 token_info_push(p, "if", &@$);
4721 if (p->token_info && p->token_info->nonspc &&
4722 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4723 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4724 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4725 beg += rb_strlen_lit("else");
4726 while (beg < tok && ISSPACE(*beg)) beg++;
4727 if (beg == tok) {
4728 p->token_info->nonspc = 0;
4729 }
4730 }
4731 push_end_expect_token_locations(p, &@1.beg_pos);
4732 }
4733 ;
4734
4735k_unless : keyword_unless
4736 {
4737 token_info_push(p, "unless", &@$);
4738 push_end_expect_token_locations(p, &@1.beg_pos);
4739 }
4740 ;
4741
4742k_while : keyword_while allow_exits
4743 {
4744 $$ = $allow_exits;
4745 token_info_push(p, "while", &@$);
4746 push_end_expect_token_locations(p, &@1.beg_pos);
4747 }
4748 ;
4749
4750k_until : keyword_until allow_exits
4751 {
4752 $$ = $allow_exits;
4753 token_info_push(p, "until", &@$);
4754 push_end_expect_token_locations(p, &@1.beg_pos);
4755 }
4756 ;
4757
4758k_case : keyword_case
4759 {
4760 token_info_push(p, "case", &@$);
4761 push_end_expect_token_locations(p, &@1.beg_pos);
4762 }
4763 ;
4764
4765k_for : keyword_for allow_exits
4766 {
4767 $$ = $allow_exits;
4768 token_info_push(p, "for", &@$);
4769 push_end_expect_token_locations(p, &@1.beg_pos);
4770 }
4771 ;
4772
4773k_class : keyword_class
4774 {
4775 token_info_push(p, "class", &@$);
4776 $$ = p->ctxt;
4777 p->ctxt.in_rescue = before_rescue;
4778 push_end_expect_token_locations(p, &@1.beg_pos);
4779 }
4780 ;
4781
4782k_module : keyword_module
4783 {
4784 token_info_push(p, "module", &@$);
4785 $$ = p->ctxt;
4786 p->ctxt.in_rescue = before_rescue;
4787 push_end_expect_token_locations(p, &@1.beg_pos);
4788 }
4789 ;
4790
4791k_def : keyword_def
4792 {
4793 token_info_push(p, "def", &@$);
4794 $$ = NEW_DEF_TEMP(&@$);
4795 p->ctxt.in_argdef = 1;
4796 }
4797 ;
4798
4799k_do : keyword_do
4800 {
4801 token_info_push(p, "do", &@$);
4802 push_end_expect_token_locations(p, &@1.beg_pos);
4803 }
4804 ;
4805
4806k_do_block : keyword_do_block
4807 {
4808 token_info_push(p, "do", &@$);
4809 push_end_expect_token_locations(p, &@1.beg_pos);
4810 }
4811 ;
4812
4813k_rescue : keyword_rescue
4814 {
4815 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4816 $$ = p->ctxt;
4817 p->ctxt.in_rescue = after_rescue;
4818 }
4819 ;
4820
4821k_ensure : keyword_ensure
4822 {
4823 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4824 $$ = p->ctxt;
4825 }
4826 ;
4827
4828k_when : keyword_when
4829 {
4830 token_info_warn(p, "when", p->token_info, 0, &@$);
4831 }
4832 ;
4833
4834k_else : keyword_else
4835 {
4836 token_info *ptinfo_beg = p->token_info;
4837 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4838 token_info_warn(p, "else", p->token_info, same, &@$);
4839 if (same) {
4840 token_info e;
4841 e.next = ptinfo_beg->next;
4842 e.token = "else";
4843 token_info_setup(&e, p->lex.pbeg, &@$);
4844 if (!e.nonspc) *ptinfo_beg = e;
4845 }
4846 }
4847 ;
4848
4849k_elsif : keyword_elsif
4850 {
4851 WARN_EOL("elsif");
4852 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4853 }
4854 ;
4855
4856k_end : keyword_end
4857 {
4858 token_info_pop(p, "end", &@$);
4859 pop_end_expect_token_locations(p);
4860 }
4861 | tDUMNY_END
4862 {
4863 compile_error(p, "syntax error, unexpected end-of-input");
4864 }
4865 ;
4866
4867k_return : keyword_return
4868 {
4869 if (p->ctxt.cant_return && !dyna_in_block(p))
4870 yyerror1(&@1, "Invalid return in class/module body");
4871 }
4872 ;
4873
4874k_yield : keyword_yield
4875 {
4876 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4877 yyerror1(&@1, "Invalid yield");
4878 }
4879 ;
4880
4881then : term
4882 | keyword_then
4883 | term keyword_then
4884 ;
4885
4886do : term
4887 | keyword_do_cond { $$ = keyword_do_cond; }
4888 ;
4889
4890if_tail : opt_else
4891 | k_elsif expr_value then
4892 compstmt(stmts)
4893 if_tail
4894 {
4895 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4896 fixpos($$, $2);
4897 /*% ripper: elsif!($:2, $:4, $:5) %*/
4898 }
4899 ;
4900
4901opt_else : none
4902 | k_else compstmt(stmts)
4903 {
4904 $$ = $2;
4905 /*% ripper: else!($:2) %*/
4906 }
4907 ;
4908
4909for_var : lhs
4910 | mlhs
4911 ;
4912
4913f_marg : f_norm_arg
4914 {
4915 $$ = assignable(p, $1, 0, &@$);
4916 mark_lvar_used(p, $$);
4917 }
4918 | tLPAREN f_margs rparen
4919 {
4920 $$ = (NODE *)$2;
4921 /*% ripper: mlhs_paren!($:2) %*/
4922 }
4923 ;
4924
4925
4926f_margs : mlhs_items(f_marg)
4927 {
4928 $$ = NEW_MASGN($1, 0, &@$);
4929 /*% ripper: $:1 %*/
4930 }
4931 | mlhs_items(f_marg) ',' f_rest_marg
4932 {
4933 $$ = NEW_MASGN($1, $3, &@$);
4934 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4935 }
4936 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4937 {
4938 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4939 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4940 }
4941 | f_rest_marg
4942 {
4943 $$ = NEW_MASGN(0, $1, &@$);
4944 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4945 }
4946 | f_rest_marg ',' mlhs_items(f_marg)
4947 {
4948 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4949 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4950 }
4951 ;
4952
4953f_rest_marg : tSTAR f_norm_arg
4954 {
4955 /*% ripper: $:2 %*/
4956 $$ = assignable(p, $2, 0, &@$);
4957 mark_lvar_used(p, $$);
4958 }
4959 | tSTAR
4960 {
4961 $$ = NODE_SPECIAL_NO_NAME_REST;
4962 /*% ripper: Qnil %*/
4963 }
4964 ;
4965
4966f_any_kwrest : f_kwrest
4967 | f_no_kwarg
4968 {
4969 $$ = idNil;
4970 /*% ripper: ID2VAL(idNil) %*/
4971 }
4972 ;
4973
4974f_eq : {p->ctxt.in_argdef = 0;} '=';
4975
4976block_args_tail : args_tail_basic(primary_value)
4977 ;
4978
4979excessed_comma : ','
4980 {
4981 /* magic number for rest_id in iseq_set_arguments() */
4982 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4983 /*% ripper: excessed_comma! %*/
4984 }
4985 ;
4986
4987block_param : f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
4988 {
4989 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
4990 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
4991 }
4992 | f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4993 {
4994 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4995 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
4996 }
4997 | f_arg ',' f_opt_arg(primary_value) opt_args_tail(block_args_tail)
4998 {
4999 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
5000 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
5001 }
5002 | f_arg ',' f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5003 {
5004 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5005 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5006 }
5007 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5008 {
5009 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5010 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5011 }
5012 | f_arg excessed_comma
5013 {
5014 $$ = new_args_tail(p, 0, 0, 0, &@2);
5015 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5016 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5017 }
5018 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5019 {
5020 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5021 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5022 }
5023 | f_arg opt_args_tail(block_args_tail)
5024 {
5025 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5026 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5027 }
5028 | f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5029 {
5030 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5031 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5032 }
5033 | f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5034 {
5035 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5036 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5037 }
5038 | f_opt_arg(primary_value) opt_args_tail(block_args_tail)
5039 {
5040 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5041 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5042 }
5043 | f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5044 {
5045 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5046 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5047 }
5048 | f_rest_arg opt_args_tail(block_args_tail)
5049 {
5050 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5051 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5052 }
5053 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5054 {
5055 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5056 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5057 }
5058 | block_args_tail
5059 {
5060 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5061 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5062 }
5063 ;
5064
5065opt_block_param_def : none
5066 | block_param_def
5067 {
5068 p->command_start = TRUE;
5069 }
5070 ;
5071
5072block_param_def : '|' opt_block_param opt_bv_decl '|'
5073 {
5074 p->max_numparam = ORDINAL_PARAM;
5075 p->ctxt.in_argdef = 0;
5076 $$ = $2;
5077 /*% ripper: block_var!($:2, $:3) %*/
5078 }
5079 ;
5080
5081opt_block_param : /* none */
5082 {
5083 $$ = 0;
5084 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5085 }
5086 | block_param
5087 ;
5088
5089opt_bv_decl : '\n'?
5090 {
5091 $$ = 0;
5092 /*% ripper: Qfalse %*/
5093 }
5094 | '\n'? ';' bv_decls '\n'?
5095 {
5096 $$ = 0;
5097 /*% ripper: $:3 %*/
5098 }
5099 ;
5100
5101bv_decls : bvar
5102 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5103 | bv_decls ',' bvar
5104 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5105 ;
5106
5107bvar : tIDENTIFIER
5108 {
5109 new_bv(p, $1);
5110 /*% ripper: $:1 %*/
5111 }
5112 | f_bad_arg
5113 ;
5114
5115max_numparam : {
5116 $$ = p->max_numparam;
5117 p->max_numparam = 0;
5118 }
5119 ;
5120
5121numparam : {
5122 $$ = numparam_push(p);
5123 }
5124 ;
5125
5126it_id : {
5127 $$ = p->it_id;
5128 p->it_id = 0;
5129 }
5130 ;
5131
5132lambda : tLAMBDA[lpar]
5133 {
5134 token_info_push(p, "->", &@1);
5135 $$ = dyna_push(p);
5136 }[dyna]<vars>
5137 max_numparam numparam it_id allow_exits
5138 f_larglist[args]
5139 {
5140 CMDARG_PUSH(0);
5141 }
5142 lambda_body[body]
5143 {
5144 int max_numparam = p->max_numparam;
5145 ID it_id = p->it_id;
5146 p->lex.lpar_beg = $lpar;
5147 p->max_numparam = $max_numparam;
5148 p->it_id = $it_id;
5149 restore_block_exit(p, $allow_exits);
5150 CMDARG_POP();
5151 $args = args_with_numbered(p, $args, max_numparam, it_id);
5152 {
5153 YYLTYPE loc = code_loc_gen(&@args, &@body);
5154 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5155 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5156 nd_set_line($$, @args.end_pos.lineno);
5157 nd_set_first_loc($$, @1.beg_pos);
5158 xfree($body);
5159 }
5160 /*% ripper: lambda!($:args, $:body) %*/
5161 numparam_pop(p, $numparam);
5162 dyna_pop(p, $dyna);
5163 }
5164 ;
5165
5166f_larglist : '(' f_args opt_bv_decl ')'
5167 {
5168 p->ctxt.in_argdef = 0;
5169 $$ = $f_args;
5170 p->max_numparam = ORDINAL_PARAM;
5171 /*% ripper: paren!($:2) %*/
5172 }
5173 | f_args
5174 {
5175 p->ctxt.in_argdef = 0;
5176 if (!args_info_empty_p(&$1->nd_ainfo))
5177 p->max_numparam = ORDINAL_PARAM;
5178 $$ = $f_args;
5179 }
5180 ;
5181
5182lambda_body : tLAMBEG compstmt(stmts) '}'
5183 {
5184 token_info_pop(p, "}", &@3);
5185 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5186 /*% ripper: $:2 %*/
5187 }
5188 | keyword_do_LAMBDA
5189 {
5190 push_end_expect_token_locations(p, &@1.beg_pos);
5191 }
5192 bodystmt k_end
5193 {
5194 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5195 /*% ripper: $:3 %*/
5196 }
5197 ;
5198
5199do_block : k_do_block do_body k_end
5200 {
5201 $$ = $2;
5202 set_embraced_location($$, &@1, &@3);
5203 /*% ripper: $:2 %*/
5204 }
5205 ;
5206
5207block_call : command do_block
5208 {
5209 $$ = command_add_block(p, $1, $2, &@$);
5210 fixpos($$, $1);
5211 /*% ripper: method_add_block!($:1, $:2) %*/
5212 }
5213 | block_call call_op2 operation2 opt_paren_args
5214 {
5215 bool has_args = $4 != 0;
5216 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5217 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5218 /*% ripper: call!($:1, $:2, $:3) %*/
5219 if (has_args) {
5220 /*% ripper: method_add_arg!($:$, $:4) %*/
5221 }
5222 }
5223 | block_call call_op2 operation2 opt_paren_args brace_block
5224 {
5225 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5226 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5227 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5228 }
5229 | block_call call_op2 operation2 command_args do_block
5230 {
5231 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5232 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5233 }
5234 | block_call call_op2 paren_args
5235 {
5236 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5237 nd_set_line($$, @2.end_pos.lineno);
5238 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5239 }
5240 ;
5241
5242method_call : fcall paren_args
5243 {
5244 $1->nd_args = $2;
5245 $$ = (NODE *)$1;
5246 nd_set_last_loc($1, @2.end_pos);
5247 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5248 }
5249 | primary_value call_op operation2 opt_paren_args
5250 {
5251 bool has_args = $4 != 0;
5252 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5253 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5254 nd_set_line($$, @3.end_pos.lineno);
5255 /*% ripper: call!($:1, $:2, $:3) %*/
5256 if (has_args) {
5257 /*% ripper: method_add_arg!($:$, $:4) %*/
5258 }
5259 }
5260 | primary_value tCOLON2 operation2 paren_args
5261 {
5262 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5263 nd_set_line($$, @3.end_pos.lineno);
5264 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5265 }
5266 | primary_value tCOLON2 operation3
5267 {
5268 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5269 /*% ripper: call!($:1, $:2, $:3) %*/
5270 }
5271 | primary_value call_op2 paren_args
5272 {
5273 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5274 nd_set_line($$, @2.end_pos.lineno);
5275 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5276 }
5277 | keyword_super paren_args
5278 {
5279 rb_code_location_t lparen_loc = @2;
5280 rb_code_location_t rparen_loc = @2;
5281 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5282 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5283
5284 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5285 /*% ripper: super!($:2) %*/
5286 }
5287 | keyword_super
5288 {
5289 $$ = NEW_ZSUPER(&@$);
5290 /*% ripper: zsuper! %*/
5291 }
5292 | primary_value '[' opt_call_args rbracket
5293 {
5294 $$ = NEW_CALL($1, tAREF, $3, &@$);
5295 fixpos($$, $1);
5296 /*% ripper: aref!($:1, $:3) %*/
5297 }
5298 ;
5299
5300brace_block : '{' brace_body '}'
5301 {
5302 $$ = $2;
5303 set_embraced_location($$, &@1, &@3);
5304 /*% ripper: $:2 %*/
5305 }
5306 | k_do do_body k_end
5307 {
5308 $$ = $2;
5309 set_embraced_location($$, &@1, &@3);
5310 /*% ripper: $:2 %*/
5311 }
5312 ;
5313
5314brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5315 max_numparam numparam it_id allow_exits
5316 opt_block_param_def[args] compstmt(stmts)
5317 {
5318 int max_numparam = p->max_numparam;
5319 ID it_id = p->it_id;
5320 p->max_numparam = $max_numparam;
5321 p->it_id = $it_id;
5322 $args = args_with_numbered(p, $args, max_numparam, it_id);
5323 $$ = NEW_ITER($args, $compstmt, &@$);
5324 /*% ripper: brace_block!($:args, $:compstmt) %*/
5325 restore_block_exit(p, $allow_exits);
5326 numparam_pop(p, $numparam);
5327 dyna_pop(p, $dyna);
5328 }
5329 ;
5330
5331do_body : {
5332 $$ = dyna_push(p);
5333 CMDARG_PUSH(0);
5334 }[dyna]<vars>
5335 max_numparam numparam it_id allow_exits
5336 opt_block_param_def[args] bodystmt
5337 {
5338 int max_numparam = p->max_numparam;
5339 ID it_id = p->it_id;
5340 p->max_numparam = $max_numparam;
5341 p->it_id = $it_id;
5342 $args = args_with_numbered(p, $args, max_numparam, it_id);
5343 $$ = NEW_ITER($args, $bodystmt, &@$);
5344 /*% ripper: do_block!($:args, $:bodystmt) %*/
5345 CMDARG_POP();
5346 restore_block_exit(p, $allow_exits);
5347 numparam_pop(p, $numparam);
5348 dyna_pop(p, $dyna);
5349 }
5350 ;
5351
5352case_args : arg_value
5353 {
5354 check_literal_when(p, $arg_value, &@arg_value);
5355 $$ = NEW_LIST($arg_value, &@$);
5356 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5357 }
5358 | tSTAR arg_value
5359 {
5360 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5361 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5362 }
5363 | case_args[non_last_args] ',' arg_value
5364 {
5365 check_literal_when(p, $arg_value, &@arg_value);
5366 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5367 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5368 }
5369 | case_args[non_last_args] ',' tSTAR arg_value
5370 {
5371 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5372 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5373 }
5374 ;
5375
5376case_body : k_when case_args then
5377 compstmt(stmts)
5378 cases
5379 {
5380 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5381 fixpos($$, $2);
5382 /*% ripper: when!($:2, $:4, $:5) %*/
5383 }
5384 ;
5385
5386cases : opt_else
5387 | case_body
5388 ;
5389
5390p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5391p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5392
5393p_in_kwarg : {
5394 $$ = p->ctxt;
5395 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5396 p->command_start = FALSE;
5397 p->ctxt.in_kwarg = 1;
5398 p->ctxt.in_alt_pattern = 0;
5399 p->ctxt.capture_in_pattern = 0;
5400 }
5401 ;
5402
5403p_case_body : keyword_in
5404 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5405 p_top_expr[expr] then
5406 {
5407 pop_pktbl(p, $p_pktbl);
5408 pop_pvtbl(p, $p_pvtbl);
5409 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5410 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5411 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5412 }
5413 compstmt(stmts)
5414 p_cases[cases]
5415 {
5416 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5417 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5418 }
5419 ;
5420
5421p_cases : opt_else
5422 | p_case_body
5423 ;
5424
5425p_top_expr : p_top_expr_body
5426 | p_top_expr_body modifier_if expr_value
5427 {
5428 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5429 fixpos($$, $3);
5430 /*% ripper: if_mod!($:3, $:1) %*/
5431 }
5432 | p_top_expr_body modifier_unless expr_value
5433 {
5434 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5435 fixpos($$, $3);
5436 /*% ripper: unless_mod!($:3, $:1) %*/
5437 }
5438 ;
5439
5440p_top_expr_body : p_expr
5441 | p_expr ','
5442 {
5443 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5444 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5445 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5446 }
5447 | p_expr ',' p_args
5448 {
5449 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5450 nd_set_first_loc($$, @1.beg_pos);
5451 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5452 }
5453 | p_find
5454 {
5455 $$ = new_find_pattern(p, 0, $1, &@$);
5456 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5457 }
5458 | p_args_tail
5459 {
5460 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5461 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5462 }
5463 | p_kwargs
5464 {
5465 $$ = new_hash_pattern(p, 0, $1, &@$);
5466 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5467 }
5468 ;
5469
5470p_expr : p_as
5471 ;
5472
5473p_as : p_expr tASSOC p_variable
5474 {
5475 NODE *n = NEW_LIST($1, &@$);
5476 n = list_append(p, n, $3);
5477 $$ = new_hash(p, n, &@$);
5478 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5479 }
5480 | p_alt
5481 ;
5482
5483p_alt : p_alt[left] '|'[alt]
5484 {
5485 p->ctxt.in_alt_pattern = 1;
5486 }
5487 p_expr_basic[right]
5488 {
5489 if (p->ctxt.capture_in_pattern) {
5490 yyerror1(&@alt, "alternative pattern after variable capture");
5491 }
5492 p->ctxt.in_alt_pattern = 0;
5493 $$ = NEW_OR($left, $right, &@$, &@alt);
5494 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5495 }
5496 | p_expr_basic
5497 ;
5498
5499p_lparen : '(' p_pktbl
5500 {
5501 $$ = $2;
5502 /*% ripper: $:2 %*/
5503 }
5504 ;
5505
5506p_lbracket : '[' p_pktbl
5507 {
5508 $$ = $2;
5509 /*% ripper: $:2 %*/
5510 }
5511 ;
5512
5513p_expr_basic : p_value
5514 | p_variable
5515 | p_const p_lparen[p_pktbl] p_args rparen
5516 {
5517 pop_pktbl(p, $p_pktbl);
5518 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5519 nd_set_first_loc($$, @p_const.beg_pos);
5520 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5521 }
5522 | p_const p_lparen[p_pktbl] p_find rparen
5523 {
5524 pop_pktbl(p, $p_pktbl);
5525 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5526 nd_set_first_loc($$, @p_const.beg_pos);
5527 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5528 }
5529 | p_const p_lparen[p_pktbl] p_kwargs rparen
5530 {
5531 pop_pktbl(p, $p_pktbl);
5532 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5533 nd_set_first_loc($$, @p_const.beg_pos);
5534 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5535 }
5536 | p_const '(' rparen
5537 {
5538 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5539 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5540 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5541 }
5542 | p_const p_lbracket[p_pktbl] p_args rbracket
5543 {
5544 pop_pktbl(p, $p_pktbl);
5545 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5546 nd_set_first_loc($$, @p_const.beg_pos);
5547 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5548 }
5549 | p_const p_lbracket[p_pktbl] p_find rbracket
5550 {
5551 pop_pktbl(p, $p_pktbl);
5552 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5553 nd_set_first_loc($$, @p_const.beg_pos);
5554 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5555 }
5556 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5557 {
5558 pop_pktbl(p, $p_pktbl);
5559 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5560 nd_set_first_loc($$, @p_const.beg_pos);
5561 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5562 }
5563 | p_const '[' rbracket
5564 {
5565 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5566 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5567 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5568 }
5569 | tLBRACK p_args rbracket
5570 {
5571 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5572 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5573 }
5574 | tLBRACK p_find rbracket
5575 {
5576 $$ = new_find_pattern(p, 0, $p_find, &@$);
5577 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5578 }
5579 | tLBRACK rbracket
5580 {
5581 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5582 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5583 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5584 }
5585 | tLBRACE p_pktbl lex_ctxt[ctxt]
5586 {
5587 p->ctxt.in_kwarg = 0;
5588 }
5589 p_kwargs rbrace
5590 {
5591 pop_pktbl(p, $p_pktbl);
5592 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5593 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5594 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5595 }
5596 | tLBRACE rbrace
5597 {
5598 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5599 $$ = new_hash_pattern(p, 0, $$, &@$);
5600 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5601 }
5602 | tLPAREN p_pktbl p_expr rparen
5603 {
5604 pop_pktbl(p, $p_pktbl);
5605 $$ = $p_expr;
5606 /*% ripper: $:p_expr %*/
5607 }
5608 ;
5609
5610p_args : p_expr
5611 {
5612 NODE *pre_args = NEW_LIST($1, &@$);
5613 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5614 /*% ripper: [[$:1], Qnil, Qnil] %*/
5615 }
5616 | p_args_head
5617 {
5618 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5619 /*% ripper: [$:1, Qnil, Qnil] %*/
5620 }
5621 | p_args_head p_arg
5622 {
5623 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5624 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5625 }
5626 | p_args_head p_rest
5627 {
5628 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5629 /*% ripper: [$:1, $:2, Qnil] %*/
5630 }
5631 | p_args_head p_rest ',' p_args_post
5632 {
5633 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5634 /*% ripper: [$:1, $:2, $:4] %*/
5635 }
5636 | p_args_tail
5637 ;
5638
5639p_args_head : p_arg ','
5640 | p_args_head p_arg ','
5641 {
5642 $$ = list_concat($1, $2);
5643 /*% ripper: rb_ary_concat($:1, $:2) %*/
5644 }
5645 ;
5646
5647p_args_tail : p_rest
5648 {
5649 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5650 /*% ripper: [Qnil, $:1, Qnil] %*/
5651 }
5652 | p_rest ',' p_args_post
5653 {
5654 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5655 /*% ripper: [Qnil, $:1, $:3] %*/
5656 }
5657 ;
5658
5659p_find : p_rest ',' p_args_post ',' p_rest
5660 {
5661 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5662 /*% ripper: [$:1, $:3, $:5] %*/
5663 }
5664 ;
5665
5666
5667p_rest : tSTAR tIDENTIFIER
5668 {
5669 error_duplicate_pattern_variable(p, $2, &@2);
5670 /*% ripper: var_field!($:2) %*/
5671 $$ = assignable(p, $2, 0, &@$);
5672 }
5673 | tSTAR
5674 {
5675 $$ = 0;
5676 /*% ripper: var_field!(Qnil) %*/
5677 }
5678 ;
5679
5680p_args_post : p_arg
5681 | p_args_post ',' p_arg
5682 {
5683 $$ = list_concat($1, $3);
5684 /*% ripper: rb_ary_concat($:1, $:3) %*/
5685 }
5686 ;
5687
5688p_arg : p_expr
5689 {
5690 $$ = NEW_LIST($1, &@$);
5691 /*% ripper: [$:1] %*/
5692 }
5693 ;
5694
5695p_kwargs : p_kwarg ',' p_any_kwrest
5696 {
5697 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5698 /*% ripper: [$:1, $:3] %*/
5699 }
5700 | p_kwarg
5701 {
5702 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5703 /*% ripper: [$:1, Qnil] %*/
5704 }
5705 | p_kwarg ','
5706 {
5707 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5708 /*% ripper: [$:1, Qnil] %*/
5709 }
5710 | p_any_kwrest
5711 {
5712 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5713 /*% ripper: [[], $:1] %*/
5714 }
5715 ;
5716
5717p_kwarg : p_kw
5718 /*% ripper[brace]: [$:1] %*/
5719 | p_kwarg ',' p_kw
5720 {
5721 $$ = list_concat($1, $3);
5722 /*% ripper: rb_ary_push($:1, $:3) %*/
5723 }
5724 ;
5725
5726p_kw : p_kw_label p_expr
5727 {
5728 error_duplicate_pattern_key(p, $1, &@1);
5729 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5730 /*% ripper: [$:1, $:2] %*/
5731 }
5732 | p_kw_label
5733 {
5734 error_duplicate_pattern_key(p, $1, &@1);
5735 if ($1 && !is_local_id($1)) {
5736 yyerror1(&@1, "key must be valid as local variables");
5737 }
5738 error_duplicate_pattern_variable(p, $1, &@1);
5739 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5740 /*% ripper: [$:1, Qnil] %*/
5741 }
5742 ;
5743
5744p_kw_label : tLABEL
5745 | tSTRING_BEG string_contents tLABEL_END
5746 {
5747 YYLTYPE loc = code_loc_gen(&@1, &@3);
5748 if (!$2 || nd_type_p($2, NODE_STR)) {
5749 NODE *node = dsym_node(p, $2, &loc);
5750 $$ = rb_sym2id(rb_node_sym_string_val(node));
5751 }
5752 else {
5753 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5754 $$ = rb_intern_str(STR_NEW0());
5755 }
5756 /*% ripper: $:2 %*/
5757 }
5758 ;
5759
5760p_kwrest : kwrest_mark tIDENTIFIER
5761 {
5762 $$ = $2;
5763 /*% ripper: var_field!($:2) %*/
5764 }
5765 | kwrest_mark
5766 {
5767 $$ = 0;
5768 /*% ripper: Qnil %*/
5769 }
5770 ;
5771
5772p_kwnorest : kwrest_mark keyword_nil
5773 {
5774 $$ = 0;
5775 }
5776 ;
5777
5778p_any_kwrest : p_kwrest
5779 | p_kwnorest
5780 {
5781 $$ = idNil;
5782 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5783 }
5784 ;
5785
5786p_value : p_primitive
5787 | range_expr(p_primitive)
5788 | p_var_ref
5789 | p_expr_ref
5790 | p_const
5791 ;
5792
5793p_primitive : inline_primary
5794 | keyword_variable
5795 {
5796 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5797 /*% ripper: var_ref!($:1) %*/
5798 }
5799 | lambda
5800 ;
5801
5802p_variable : tIDENTIFIER
5803 {
5804 error_duplicate_pattern_variable(p, $1, &@1);
5805 /*% ripper: var_field!($:1) %*/
5806 $$ = assignable(p, $1, 0, &@$);
5807 }
5808 ;
5809
5810p_var_ref : '^' tIDENTIFIER
5811 {
5812 NODE *n = gettable(p, $2, &@$);
5813 if (!n) {
5814 n = NEW_ERROR(&@$);
5815 }
5816 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5817 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5818 }
5819 $$ = n;
5820 /*% ripper: var_ref!($:2) %*/
5821 }
5822 | '^' nonlocal_var
5823 {
5824 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5825 /*% ripper: var_ref!($:2) %*/
5826 }
5827 ;
5828
5829p_expr_ref : '^' tLPAREN expr_value rparen
5830 {
5831 $$ = NEW_BLOCK($3, &@$);
5832 /*% ripper: begin!($:3) %*/
5833 }
5834 ;
5835
5836p_const : tCOLON3 cname
5837 {
5838 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5839 /*% ripper: top_const_ref!($:2) %*/
5840 }
5841 | p_const tCOLON2 cname
5842 {
5843 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5844 /*% ripper: const_path_ref!($:1, $:3) %*/
5845 }
5846 | tCONSTANT
5847 {
5848 $$ = gettable(p, $1, &@$);
5849 /*% ripper: var_ref!($:1) %*/
5850 }
5851 ;
5852
5853opt_rescue : k_rescue exc_list exc_var then
5854 compstmt(stmts)
5855 opt_rescue
5856 {
5857 NODE *err = $3;
5858 if ($3) {
5859 err = NEW_ERRINFO(&@3);
5860 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5861 }
5862 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5863 if ($2) {
5864 fixpos($$, $2);
5865 }
5866 else if ($3) {
5867 fixpos($$, $3);
5868 }
5869 else {
5870 fixpos($$, $5);
5871 }
5872 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5873 }
5874 | none
5875 ;
5876
5877exc_list : arg_value
5878 {
5879 $$ = NEW_LIST($1, &@$);
5880 /*% ripper: rb_ary_new3(1, $:1) %*/
5881 }
5882 | mrhs
5883 {
5884 if (!($$ = splat_array($1))) $$ = $1;
5885 }
5886 | none
5887 ;
5888
5889exc_var : tASSOC lhs
5890 {
5891 $$ = $2;
5892 /*% ripper: $:2 %*/
5893 }
5894 | none
5895 ;
5896
5897opt_ensure : k_ensure stmts terms?
5898 {
5899 p->ctxt.in_rescue = $1.in_rescue;
5900 $$ = $2;
5901 void_expr(p, void_stmts(p, $$));
5902 /*% ripper: ensure!($:2) %*/
5903 }
5904 | none
5905 ;
5906
5907literal : numeric
5908 | symbol
5909 ;
5910
5911strings : string
5912 {
5913 if (!$1) {
5914 $$ = NEW_STR(STRING_NEW0(), &@$);
5915 }
5916 else {
5917 $$ = evstr2dstr(p, $1);
5918 }
5919 /*% ripper: $:1 %*/
5920 }
5921 ;
5922
5923string : tCHAR
5924 | string1
5925 | string string1
5926 {
5927 $$ = literal_concat(p, $1, $2, &@$);
5928 /*% ripper: string_concat!($:1, $:2) %*/
5929 }
5930 ;
5931
5932string1 : tSTRING_BEG string_contents tSTRING_END
5933 {
5934 $$ = heredoc_dedent(p, $2);
5935 if ($$) nd_set_loc($$, &@$);
5936 /*% ripper: $:2 %*/
5937 if (p->heredoc_indent > 0) {
5938 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5939 p->heredoc_indent = 0;
5940 }
5941 /*% ripper: string_literal!($:$) %*/
5942 }
5943 ;
5944
5945xstring : tXSTRING_BEG xstring_contents tSTRING_END
5946 {
5947 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5948 /*% ripper: $:2 %*/
5949 if (p->heredoc_indent > 0) {
5950 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5951 p->heredoc_indent = 0;
5952 }
5953 /*% ripper: xstring_literal!($:$) %*/
5954 }
5955 ;
5956
5957regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5958 {
5959 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5960 /*% ripper: regexp_literal!($:2, $:3) %*/
5961 }
5962 ;
5963
5964words : words(tWORDS_BEG, word_list)
5965 ;
5966
5967word_list : /* none */
5968 {
5969 $$ = 0;
5970 /*% ripper: words_new! %*/
5971 }
5972 | word_list word ' '+
5973 {
5974 $$ = list_append(p, $1, evstr2dstr(p, $2));
5975 /*% ripper: words_add!($:1, $:2) %*/
5976 }
5977 ;
5978
5979word : string_content
5980 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5981 | word string_content
5982 {
5983 $$ = literal_concat(p, $1, $2, &@$);
5984 /*% ripper: word_add!($:1, $:2) %*/
5985 }
5986 ;
5987
5988symbols : words(tSYMBOLS_BEG, symbol_list)
5989 ;
5990
5991symbol_list : /* none */
5992 {
5993 $$ = 0;
5994 /*% ripper: symbols_new! %*/
5995 }
5996 | symbol_list word ' '+
5997 {
5998 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5999 /*% ripper: symbols_add!($:1, $:2) %*/
6000 }
6001 ;
6002
6003qwords : words(tQWORDS_BEG, qword_list)
6004 ;
6005
6006qsymbols : words(tQSYMBOLS_BEG, qsym_list)
6007 ;
6008
6009qword_list : /* none */
6010 {
6011 $$ = 0;
6012 /*% ripper: qwords_new! %*/
6013 }
6014 | qword_list tSTRING_CONTENT ' '+
6015 {
6016 $$ = list_append(p, $1, $2);
6017 /*% ripper: qwords_add!($:1, $:2) %*/
6018 }
6019 ;
6020
6021qsym_list : /* none */
6022 {
6023 $$ = 0;
6024 /*% ripper: qsymbols_new! %*/
6025 }
6026 | qsym_list tSTRING_CONTENT ' '+
6027 {
6028 $$ = symbol_append(p, $1, $2);
6029 /*% ripper: qsymbols_add!($:1, $:2) %*/
6030 }
6031 ;
6032
6033string_contents : /* none */
6034 {
6035 $$ = 0;
6036 /*% ripper: string_content! %*/
6037 }
6038 | string_contents string_content
6039 {
6040 $$ = literal_concat(p, $1, $2, &@$);
6041 /*% ripper: string_add!($:1, $:2) %*/
6042 }
6043 ;
6044
6045xstring_contents: /* none */
6046 {
6047 $$ = 0;
6048 /*% ripper: xstring_new! %*/
6049 }
6050 | xstring_contents string_content
6051 {
6052 $$ = literal_concat(p, $1, $2, &@$);
6053 /*% ripper: xstring_add!($:1, $:2) %*/
6054 }
6055 ;
6056
6057regexp_contents : /* none */
6058 {
6059 $$ = 0;
6060 /*% ripper: regexp_new! %*/
6061 }
6062 | regexp_contents string_content
6063 {
6064 NODE *head = $1, *tail = $2;
6065 if (!head) {
6066 $$ = tail;
6067 }
6068 else if (!tail) {
6069 $$ = head;
6070 }
6071 else {
6072 switch (nd_type(head)) {
6073 case NODE_STR:
6074 head = str2dstr(p, head);
6075 break;
6076 case NODE_DSTR:
6077 break;
6078 default:
6079 head = list_append(p, NEW_DSTR(0, &@$), head);
6080 break;
6081 }
6082 $$ = list_append(p, head, tail);
6083 }
6084 /*% ripper: regexp_add!($:1, $:2) %*/
6085 }
6086 ;
6087
6088string_content : tSTRING_CONTENT
6089 /*% ripper[brace]: $:1 %*/
6090 | tSTRING_DVAR
6091 {
6092 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6093 $$ = p->lex.strterm;
6094 p->lex.strterm = 0;
6095 SET_LEX_STATE(EXPR_BEG);
6096 }<strterm>
6097 string_dvar
6098 {
6099 p->lex.strterm = $2;
6100 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6101 nd_set_line($$, @3.end_pos.lineno);
6102 /*% ripper: string_dvar!($:3) %*/
6103 }
6104 | tSTRING_DBEG[state]
6105 {
6106 CMDARG_PUSH(0);
6107 COND_PUSH(0);
6108 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6109 $$ = p->lex.strterm;
6110 p->lex.strterm = 0;
6111 SET_LEX_STATE(EXPR_BEG);
6112 }[term]<strterm>
6113 {
6114 $$ = p->lex.brace_nest;
6115 p->lex.brace_nest = 0;
6116 }[brace]<num>
6117 {
6118 $$ = p->lex.lpar_beg;
6119 p->lex.lpar_beg = -1;
6120 }[lpar]<num>
6121 {
6122 $$ = p->heredoc_indent;
6123 p->heredoc_indent = 0;
6124 }[indent]<num>
6125 compstmt(stmts) string_dend
6126 {
6127 COND_POP();
6128 CMDARG_POP();
6129 p->lex.strterm = $term;
6130 SET_LEX_STATE($state);
6131 p->lex.brace_nest = $brace;
6132 p->lex.lpar_beg = $lpar;
6133 p->heredoc_indent = $indent;
6134 p->heredoc_line_indent = -1;
6135 if ($compstmt) nd_unset_fl_newline($compstmt);
6136 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6137 /*% ripper: string_embexpr!($:compstmt) %*/
6138 }
6139 ;
6140
6141string_dend : tSTRING_DEND
6142 | END_OF_INPUT
6143 ;
6144
6145string_dvar : nonlocal_var
6146 {
6147 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6148 /*% ripper: var_ref!($:1) %*/
6149 }
6150 | backref
6151 ;
6152
6153symbol : ssym
6154 | dsym
6155 ;
6156
6157ssym : tSYMBEG sym
6158 {
6159 SET_LEX_STATE(EXPR_END);
6160 VALUE str = rb_id2str($2);
6161 /*
6162 * TODO:
6163 * set_yylval_noname sets invalid id to yylval.
6164 * This branch can be removed once yylval is changed to
6165 * hold lexed string.
6166 */
6167 if (!str) str = STR_NEW0();
6168 $$ = NEW_SYM(str, &@$);
6169 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6170 }
6171 ;
6172
6173sym : fname
6174 | nonlocal_var
6175 ;
6176
6177dsym : tSYMBEG string_contents tSTRING_END
6178 {
6179 SET_LEX_STATE(EXPR_END);
6180 $$ = dsym_node(p, $2, &@$);
6181 /*% ripper: dyna_symbol!($:2) %*/
6182 }
6183 ;
6184
6185numeric : simple_numeric
6186 | tUMINUS_NUM simple_numeric %prec tLOWEST
6187 {
6188 $$ = $2;
6189 negate_lit(p, $$, &@$);
6190 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6191 }
6192 ;
6193
6194simple_numeric : tINTEGER
6195 | tFLOAT
6196 | tRATIONAL
6197 | tIMAGINARY
6198 ;
6199
6200nonlocal_var : tIVAR
6201 | tGVAR
6202 | tCVAR
6203 ;
6204
6205user_variable : ident_or_const
6206 | nonlocal_var
6207 ;
6208
6209keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6210 | keyword_self {$$ = KWD2EID(self, $1);}
6211 | keyword_true {$$ = KWD2EID(true, $1);}
6212 | keyword_false {$$ = KWD2EID(false, $1);}
6213 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6214 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6215 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6216 ;
6217
6218var_ref : user_variable
6219 {
6220 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6221 if (ifdef_ripper(id_is_var(p, $1), false)) {
6222 /*% ripper: var_ref!($:1) %*/
6223 }
6224 else {
6225 /*% ripper: vcall!($:1) %*/
6226 }
6227 }
6228 | keyword_variable
6229 {
6230 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6231 /*% ripper: var_ref!($:1) %*/
6232 }
6233 ;
6234
6235var_lhs : user_or_keyword_variable
6236 {
6237 /*% ripper: var_field!($:1) %*/
6238 $$ = assignable(p, $1, 0, &@$);
6239 }
6240 ;
6241
6242backref : tNTH_REF
6243 | tBACK_REF
6244 ;
6245
6246superclass : '<'
6247 {
6248 SET_LEX_STATE(EXPR_BEG);
6249 p->command_start = TRUE;
6250 }
6251 expr_value term
6252 {
6253 $$ = $3;
6254 /*% ripper: $:3 %*/
6255 }
6256 | none
6257 ;
6258
6259f_opt_paren_args: f_paren_args
6260 | none
6261 {
6262 p->ctxt.in_argdef = 0;
6263 $$ = new_args_tail(p, 0, 0, 0, &@0);
6264 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6265 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6266 }
6267 ;
6268
6269f_paren_args : '(' f_args rparen
6270 {
6271 $$ = $2;
6272 /*% ripper: paren!($:2) %*/
6273 SET_LEX_STATE(EXPR_BEG);
6274 p->command_start = TRUE;
6275 p->ctxt.in_argdef = 0;
6276 }
6277 ;
6278
6279f_arglist : f_paren_args
6280 | {
6281 $$ = p->ctxt;
6282 p->ctxt.in_kwarg = 1;
6283 p->ctxt.in_argdef = 1;
6284 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6285 }<ctxt>
6286 f_args term
6287 {
6288 p->ctxt.in_kwarg = $1.in_kwarg;
6289 p->ctxt.in_argdef = 0;
6290 $$ = $2;
6291 SET_LEX_STATE(EXPR_BEG);
6292 p->command_start = TRUE;
6293 /*% ripper: $:2 %*/
6294 }
6295 ;
6296
6297args_tail : args_tail_basic(arg_value)
6298 | args_forward
6299 {
6300 ID fwd = $args_forward;
6301 if (lambda_beginning_p() ||
6302 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6303 yyerror0("unexpected ... in lambda argument");
6304 fwd = 0;
6305 }
6306 else {
6307 add_forwarding_args(p);
6308 }
6309 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6310 $$->nd_ainfo.forwarding = 1;
6311 /*% ripper: [Qnil, $:1, Qnil] %*/
6312 }
6313 ;
6314
6315f_args : f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6316 {
6317 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6318 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6319 }
6320 | f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6321 {
6322 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6323 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6324 }
6325 | f_arg ',' f_opt_arg(arg_value) opt_args_tail(args_tail)
6326 {
6327 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6328 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6329 }
6330 | f_arg ',' f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6331 {
6332 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6333 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6334 }
6335 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6336 {
6337 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6338 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6339 }
6340 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6341 {
6342 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6343 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6344 }
6345 | f_arg opt_args_tail(args_tail)
6346 {
6347 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6348 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6349 }
6350 | f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6351 {
6352 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6353 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6354 }
6355 | f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6356 {
6357 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6358 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6359 }
6360 | f_opt_arg(arg_value) opt_args_tail(args_tail)
6361 {
6362 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6363 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6364 }
6365 | f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6366 {
6367 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6368 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6369 }
6370 | f_rest_arg opt_args_tail(args_tail)
6371 {
6372 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6373 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6374 }
6375 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6376 {
6377 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6378 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6379 }
6380 | args_tail
6381 {
6382 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6383 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6384 }
6385 | /* none */
6386 {
6387 $$ = new_args_tail(p, 0, 0, 0, &@0);
6388 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6389 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6390 }
6391 ;
6392
6393args_forward : tBDOT3
6394 {
6395 $$ = idFWD_KWREST;
6396 /*% ripper: args_forward! %*/
6397 }
6398 ;
6399
6400f_bad_arg : tCONSTANT
6401 {
6402 static const char mesg[] = "formal argument cannot be a constant";
6403 /*%%%*/
6404 yyerror1(&@1, mesg);
6405 /*% %*/
6406 $$ = 0;
6407 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6408 }
6409 | tIVAR
6410 {
6411 static const char mesg[] = "formal argument cannot be an instance variable";
6412 /*%%%*/
6413 yyerror1(&@1, mesg);
6414 /*% %*/
6415 $$ = 0;
6416 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6417 }
6418 | tGVAR
6419 {
6420 static const char mesg[] = "formal argument cannot be a global variable";
6421 /*%%%*/
6422 yyerror1(&@1, mesg);
6423 /*% %*/
6424 $$ = 0;
6425 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6426 }
6427 | tCVAR
6428 {
6429 static const char mesg[] = "formal argument cannot be a class variable";
6430 /*%%%*/
6431 yyerror1(&@1, mesg);
6432 /*% %*/
6433 $$ = 0;
6434 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6435 }
6436 ;
6437
6438f_norm_arg : f_bad_arg
6439 | tIDENTIFIER
6440 {
6441 VALUE e = formal_argument_error(p, $$ = $1);
6442 if (e) {
6443 /*% ripper[error]: param_error!(?e, $:1) %*/
6444 }
6445 p->max_numparam = ORDINAL_PARAM;
6446 }
6447 ;
6448
6449f_arg_asgn : f_norm_arg
6450 {
6451 arg_var(p, $1);
6452 $$ = $1;
6453 }
6454 ;
6455
6456f_arg_item : f_arg_asgn
6457 {
6458 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6459 /*% ripper: $:1 %*/
6460 }
6461 | tLPAREN f_margs rparen
6462 {
6463 ID tid = internal_id(p);
6464 YYLTYPE loc;
6465 loc.beg_pos = @2.beg_pos;
6466 loc.end_pos = @2.beg_pos;
6467 arg_var(p, tid);
6468 if (dyna_in_block(p)) {
6469 $2->nd_value = NEW_DVAR(tid, &loc);
6470 }
6471 else {
6472 $2->nd_value = NEW_LVAR(tid, &loc);
6473 }
6474 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6475 $$->nd_next = (NODE *)$2;
6476 /*% ripper: mlhs_paren!($:2) %*/
6477 }
6478 ;
6479
6480f_arg : f_arg_item
6481 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6482 | f_arg ',' f_arg_item
6483 {
6484 $$ = $1;
6485 $$->nd_plen++;
6486 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6487 rb_discard_node(p, (NODE *)$3);
6488 /*% ripper: rb_ary_push($:1, $:3) %*/
6489 }
6490 ;
6491
6492
6493f_label : tLABEL
6494 {
6495 VALUE e = formal_argument_error(p, $$ = $1);
6496 if (e) {
6497 $$ = 0;
6498 /*% ripper[error]: param_error!(?e, $:1) %*/
6499 }
6500 /*
6501 * Workaround for Prism::ParseTest#test_filepath for
6502 * "unparser/corpus/literal/def.txt"
6503 *
6504 * See the discussion on https://github.com/ruby/ruby/pull/9923
6505 */
6506 arg_var(p, ifdef_ripper(0, $1));
6507 /*% ripper: $:1 %*/
6508 p->max_numparam = ORDINAL_PARAM;
6509 p->ctxt.in_argdef = 0;
6510 }
6511 ;
6512
6513kwrest_mark : tPOW
6514 | tDSTAR
6515 ;
6516
6517f_no_kwarg : p_kwnorest
6518 {
6519 /*% ripper: nokw_param!(Qnil) %*/
6520 }
6521 ;
6522
6523f_kwrest : kwrest_mark tIDENTIFIER
6524 {
6525 arg_var(p, shadowing_lvar(p, $2));
6526 $$ = $2;
6527 /*% ripper: kwrest_param!($:2) %*/
6528 }
6529 | kwrest_mark
6530 {
6531 arg_var(p, idFWD_KWREST);
6532 $$ = idFWD_KWREST;
6533 /*% ripper: kwrest_param!(Qnil) %*/
6534 }
6535 ;
6536
6537restarg_mark : '*'
6538 | tSTAR
6539 ;
6540
6541f_rest_arg : restarg_mark tIDENTIFIER
6542 {
6543 arg_var(p, shadowing_lvar(p, $2));
6544 $$ = $2;
6545 /*% ripper: rest_param!($:2) %*/
6546 }
6547 | restarg_mark
6548 {
6549 arg_var(p, idFWD_REST);
6550 $$ = idFWD_REST;
6551 /*% ripper: rest_param!(Qnil) %*/
6552 }
6553 ;
6554
6555blkarg_mark : '&'
6556 | tAMPER
6557 ;
6558
6559f_block_arg : blkarg_mark tIDENTIFIER
6560 {
6561 arg_var(p, shadowing_lvar(p, $2));
6562 $$ = $2;
6563 /*% ripper: blockarg!($:2) %*/
6564 }
6565 | blkarg_mark
6566 {
6567 arg_var(p, idFWD_BLOCK);
6568 $$ = idFWD_BLOCK;
6569 /*% ripper: blockarg!(Qnil) %*/
6570 }
6571 ;
6572
6573opt_f_block_arg : ',' f_block_arg
6574 {
6575 $$ = $2;
6576 /*% ripper: $:2 %*/
6577 }
6578 | none
6579 ;
6580
6581
6582singleton : value_expr(singleton_expr)
6583 {
6584 NODE *expr = last_expr_node($1);
6585 switch (nd_type(expr)) {
6586 case NODE_STR:
6587 case NODE_DSTR:
6588 case NODE_XSTR:
6589 case NODE_DXSTR:
6590 case NODE_REGX:
6591 case NODE_DREGX:
6592 case NODE_SYM:
6593 case NODE_LINE:
6594 case NODE_FILE:
6595 case NODE_ENCODING:
6596 case NODE_INTEGER:
6597 case NODE_FLOAT:
6598 case NODE_RATIONAL:
6599 case NODE_IMAGINARY:
6600 case NODE_DSYM:
6601 case NODE_LIST:
6602 case NODE_ZLIST:
6603 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6604 break;
6605 default:
6606 break;
6607 }
6608 $$ = $1;
6609 }
6610 ;
6611
6612singleton_expr : var_ref
6613 | '('
6614 {
6615 SET_LEX_STATE(EXPR_BEG);
6616 p->ctxt.in_argdef = 0;
6617 }
6618 expr rparen
6619 {
6620 p->ctxt.in_argdef = 1;
6621 $$ = $3;
6622 /*% ripper: paren!($:3) %*/
6623 }
6624 ;
6625
6626assoc_list : none
6627 | assocs trailer
6628 {
6629 $$ = $1;
6630 /*% ripper: assoclist_from_args!($:1) %*/
6631 }
6632 ;
6633
6634assocs : assoc
6635 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6636 | assocs ',' assoc
6637 {
6638 NODE *assocs = $1;
6639 NODE *tail = $3;
6640 if (!assocs) {
6641 assocs = tail;
6642 }
6643 else if (tail) {
6644 if (RNODE_LIST(assocs)->nd_head) {
6645 NODE *n = RNODE_LIST(tail)->nd_next;
6646 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6647 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6648 /* DSTAR */
6649 tail = RNODE_HASH(n)->nd_head;
6650 }
6651 }
6652 if (tail) {
6653 assocs = list_concat(assocs, tail);
6654 }
6655 }
6656 $$ = assocs;
6657 /*% ripper: rb_ary_push($:1, $:3) %*/
6658 }
6659 ;
6660
6661assoc : arg_value tASSOC arg_value
6662 {
6663 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6664 /*% ripper: assoc_new!($:1, $:3) %*/
6665 }
6666 | tLABEL arg_value
6667 {
6668 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6669 /*% ripper: assoc_new!($:1, $:2) %*/
6670 }
6671 | tLABEL
6672 {
6673 NODE *val = gettable(p, $1, &@$);
6674 if (!val) val = NEW_ERROR(&@$);
6675 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6676 /*% ripper: assoc_new!($:1, Qnil) %*/
6677 }
6678 | tSTRING_BEG string_contents tLABEL_END arg_value
6679 {
6680 YYLTYPE loc = code_loc_gen(&@1, &@3);
6681 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6682 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6683 }
6684 | tDSTAR arg_value
6685 {
6686 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6687 /*% ripper: assoc_splat!($:2) %*/
6688 }
6689 | tDSTAR
6690 {
6691 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6692 $$ = list_append(p, NEW_LIST(0, &@$),
6693 NEW_LVAR(idFWD_KWREST, &@$));
6694 /*% ripper: assoc_splat!(Qnil) %*/
6695 }
6696 ;
6697
6698%rule %inline operation : ident_or_const
6699 | tFID
6700 ;
6701
6702operation2 : operation
6703 | op
6704 ;
6705
6706operation3 : tIDENTIFIER
6707 | tFID
6708 | op
6709 ;
6710
6711dot_or_colon : '.'
6712 | tCOLON2
6713 ;
6714
6715call_op : '.'
6716 | tANDDOT
6717 ;
6718
6719call_op2 : call_op
6720 | tCOLON2
6721 ;
6722
6723rparen : '\n'? ')'
6724 ;
6725
6726rbracket : '\n'? ']'
6727 ;
6728
6729rbrace : '\n'? '}'
6730 ;
6731
6732trailer : '\n'?
6733 | ','
6734 ;
6735
6736term : ';'
6737 {
6738 yyerrok;
6739 token_flush(p);
6740 if (p->ctxt.in_defined) {
6741 p->ctxt.has_trailing_semicolon = 1;
6742 }
6743 }
6744 | '\n'
6745 {
6746 @$.end_pos = @$.beg_pos;
6747 token_flush(p);
6748 }
6749 ;
6750
6751terms : term
6752 | terms ';' {yyerrok;}
6753 ;
6754
6755none : /* none */
6756 {
6757 $$ = 0;
6758 /*% ripper: Qnil %*/
6759 }
6760 ;
6761%%
6762# undef p
6763# undef yylex
6764# undef yylval
6765# define yylval (*p->lval)
6766
6767static int regx_options(struct parser_params*);
6768static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6769static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6770static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6771static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6772
6773#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6774
6775# define set_yylval_node(x) { \
6776 YYLTYPE _cur_loc; \
6777 rb_parser_set_location(p, &_cur_loc); \
6778 yylval.node = (x); \
6779 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6780}
6781# define set_yylval_str(x) \
6782do { \
6783 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6784 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6785} while(0)
6786# define set_yylval_num(x) { \
6787 yylval.num = (x); \
6788 set_parser_s_value(x); \
6789}
6790# define set_yylval_id(x) (yylval.id = (x))
6791# define set_yylval_name(x) { \
6792 (yylval.id = (x)); \
6793 set_parser_s_value(ID2SYM(x)); \
6794}
6795# define yylval_id() (yylval.id)
6796
6797#define set_yylval_noname() set_yylval_id(keyword_nil)
6798#define has_delayed_token(p) (p->delayed.token != NULL)
6799
6800#ifndef RIPPER
6801#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6802#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6803
6804static bool
6805parser_has_token(struct parser_params *p)
6806{
6807 const char *const pcur = p->lex.pcur;
6808 const char *const ptok = p->lex.ptok;
6809 if (p->keep_tokens && (pcur < ptok)) {
6810 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6811 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6812 }
6813 return pcur > ptok;
6814}
6815
6816static const char *
6817escaped_char(int c)
6818{
6819 switch (c) {
6820 case '"': return "\\\"";
6821 case '\\': return "\\\\";
6822 case '\0': return "\\0";
6823 case '\n': return "\\n";
6824 case '\r': return "\\r";
6825 case '\t': return "\\t";
6826 case '\f': return "\\f";
6827 case '\013': return "\\v";
6828 case '\010': return "\\b";
6829 case '\007': return "\\a";
6830 case '\033': return "\\e";
6831 case '\x7f': return "\\c?";
6832 }
6833 return NULL;
6834}
6835
6836static rb_parser_string_t *
6837rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6838{
6839 rb_encoding *enc = p->enc;
6840 const char *ptr = str->ptr;
6841 const char *pend = ptr + str->len;
6842 const char *prev = ptr;
6843 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6844 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6845
6846 while (ptr < pend) {
6847 unsigned int c;
6848 const char *cc;
6849 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6850 if (!MBCLEN_CHARFOUND_P(n)) {
6851 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6852 n = rb_enc_mbminlen(enc);
6853 if (pend < ptr + n)
6854 n = (int)(pend - ptr);
6855 while (n--) {
6856 c = *ptr & 0xf0 >> 4;
6857 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6858 c = *ptr & 0x0f;
6859 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6860 parser_str_cat(result, charbuf, 4);
6861 prev = ++ptr;
6862 }
6863 continue;
6864 }
6865 n = MBCLEN_CHARFOUND_LEN(n);
6866 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6867 ptr += n;
6868 cc = escaped_char(c);
6869 if (cc) {
6870 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6871 parser_str_cat_cstr(result, cc);
6872 prev = ptr;
6873 }
6874 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6875 }
6876 else {
6877 if (ptr - n > prev) {
6878 parser_str_cat(result, prev, ptr - n - prev);
6879 prev = ptr - n;
6880 }
6881 parser_str_cat(result, prev, ptr - prev);
6882 prev = ptr;
6883 }
6884 }
6885 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6886
6887 return result;
6888}
6889
6890static void
6891parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6892{
6893 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6894 token->id = p->token_id;
6895 token->type_name = parser_token2char(p, t);
6896 token->str = str;
6897 token->loc.beg_pos = p->yylloc->beg_pos;
6898 token->loc.end_pos = p->yylloc->end_pos;
6899 rb_parser_ary_push_ast_token(p, p->tokens, token);
6900 p->token_id++;
6901
6902 if (p->debug) {
6903 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6904 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6905 line, token->id, token->type_name, str_escaped->ptr,
6906 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6907 token->loc.end_pos.lineno, token->loc.end_pos.column);
6908 rb_parser_string_free(p, str_escaped);
6909 }
6910}
6911
6912static void
6913parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6914{
6915 debug_token_line(p, "parser_dispatch_scan_event", line);
6916
6917 if (!parser_has_token(p)) return;
6918
6919 RUBY_SET_YYLLOC(*p->yylloc);
6920
6921 if (p->keep_tokens) {
6922 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6923 parser_append_tokens(p, str, t, line);
6924 }
6925
6926 token_flush(p);
6927}
6928
6929#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6930static void
6931parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6932{
6933 debug_token_line(p, "parser_dispatch_delayed_token", line);
6934
6935 if (!has_delayed_token(p)) return;
6936
6937 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6938
6939 if (p->keep_tokens) {
6940 /* p->delayed.token is freed by rb_parser_tokens_free */
6941 parser_append_tokens(p, p->delayed.token, t, line);
6942 }
6943 else {
6944 rb_parser_string_free(p, p->delayed.token);
6945 }
6946
6947 p->delayed.token = NULL;
6948}
6949#else
6950#define literal_flush(p, ptr) ((void)(ptr))
6951
6952static int
6953ripper_has_scan_event(struct parser_params *p)
6954{
6955 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6956 return p->lex.pcur > p->lex.ptok;
6957}
6958
6959static VALUE
6960ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6961{
6962 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6963 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6964 RUBY_SET_YYLLOC(*p->yylloc);
6965 token_flush(p);
6966 return rval;
6967}
6968
6969static void
6970ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6971{
6972 if (!ripper_has_scan_event(p)) return;
6973
6974 set_parser_s_value(ripper_scan_event_val(p, t));
6975}
6976#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6977
6978static void
6979ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6980{
6981 /* save and adjust the location to delayed token for callbacks */
6982 int saved_line = p->ruby_sourceline;
6983 const char *saved_tokp = p->lex.ptok;
6984 VALUE s_value, str;
6985
6986 if (!has_delayed_token(p)) return;
6987 p->ruby_sourceline = p->delayed.beg_line;
6988 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6989 str = rb_str_new_mutable_parser_string(p->delayed.token);
6990 rb_parser_string_free(p, p->delayed.token);
6991 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6992 set_parser_s_value(s_value);
6993 p->delayed.token = NULL;
6994 p->ruby_sourceline = saved_line;
6995 p->lex.ptok = saved_tokp;
6996}
6997#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6998#endif /* RIPPER */
6999
7000static inline int
7001is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7002{
7003 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7004}
7005
7006static inline bool
7007peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
7008{
7009 const char *ptr = p->lex.pcur + at;
7010 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
7011 if (memcmp(ptr, str, len)) return false;
7012 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
7013 switch (ptr[len]) {
7014 case '!': case '?': return false;
7015 }
7016 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
7017}
7018
7019static inline int
7020parser_is_identchar(struct parser_params *p)
7021{
7022 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7023}
7024
7025static inline int
7026parser_isascii(struct parser_params *p)
7027{
7028 return ISASCII(*(p->lex.pcur-1));
7029}
7030
7031static void
7032token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7033{
7034 int column = 1, nonspc = 0, i;
7035 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7036 if (*ptr == '\t') {
7037 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7038 }
7039 column++;
7040 if (*ptr != ' ' && *ptr != '\t') {
7041 nonspc = 1;
7042 }
7043 }
7044
7045 ptinfo->beg = loc->beg_pos;
7046 ptinfo->indent = column;
7047 ptinfo->nonspc = nonspc;
7048}
7049
7050static void
7051token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7052{
7053 token_info *ptinfo;
7054
7055 if (!p->token_info_enabled) return;
7056 ptinfo = ALLOC(token_info);
7057 ptinfo->token = token;
7058 ptinfo->next = p->token_info;
7059 token_info_setup(ptinfo, p->lex.pbeg, loc);
7060
7061 p->token_info = ptinfo;
7062}
7063
7064static void
7065token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7066{
7067 token_info *ptinfo_beg = p->token_info;
7068
7069 if (!ptinfo_beg) return;
7070
7071 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7072 token_info_warn(p, token, ptinfo_beg, 1, loc);
7073
7074 p->token_info = ptinfo_beg->next;
7075 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7076}
7077
7078static void
7079token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7080{
7081 token_info *ptinfo_beg = p->token_info;
7082
7083 if (!ptinfo_beg) return;
7084 p->token_info = ptinfo_beg->next;
7085
7086 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7087 ptinfo_beg->beg.column != beg_pos.column ||
7088 strcmp(ptinfo_beg->token, token)) {
7089 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7090 beg_pos.lineno, beg_pos.column, token,
7091 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7092 ptinfo_beg->token);
7093 }
7094
7095 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7096}
7097
7098static void
7099token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7100{
7101 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7102 if (!p->token_info_enabled) return;
7103 if (!ptinfo_beg) return;
7104 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7105 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7106 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7107 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7108 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7109 rb_warn3L(ptinfo_end->beg.lineno,
7110 "mismatched indentations at '%s' with '%s' at %d",
7111 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7112}
7113
7114static int
7115parser_precise_mbclen(struct parser_params *p, const char *ptr)
7116{
7117 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7118 if (!MBCLEN_CHARFOUND_P(len)) {
7119 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7120 return -1;
7121 }
7122 return len;
7123}
7124
7125#ifndef RIPPER
7126static inline void
7127parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7128{
7129 rb_parser_string_t *str;
7130 int lineno = p->ruby_sourceline;
7131 if (!yylloc) {
7132 return;
7133 }
7134 else if (yylloc->beg_pos.lineno == lineno) {
7135 str = p->lex.lastline;
7136 }
7137 else {
7138 return;
7139 }
7140 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7141}
7142
7143static int
7144parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7145{
7146#if 0
7147 YYLTYPE current;
7148
7149 if (!yylloc) {
7150 yylloc = RUBY_SET_YYLLOC(current);
7151 }
7152 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7153 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7154 yylloc = 0;
7155 }
7156#endif
7157 parser_compile_error(p, yylloc, "%s", msg);
7158 parser_show_error_line(p, yylloc);
7159 return 0;
7160}
7161
7162static int
7163parser_yyerror0(struct parser_params *p, const char *msg)
7164{
7165 YYLTYPE current;
7166 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7167}
7168
7169void
7170ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7171{
7172 VALUE mesg;
7173 const int max_line_margin = 30;
7174 const char *ptr, *ptr_end, *pt, *pb;
7175 const char *pre = "", *post = "", *pend;
7176 const char *code = "", *caret = "";
7177 const char *lim;
7178 const char *const pbeg = PARSER_STRING_PTR(str);
7179 char *buf;
7180 long len;
7181 int i;
7182
7183 if (!yylloc) return;
7184 pend = rb_parser_string_end(str);
7185 if (pend > pbeg && pend[-1] == '\n') {
7186 if (--pend > pbeg && pend[-1] == '\r') --pend;
7187 }
7188
7189 pt = pend;
7190 if (lineno == yylloc->end_pos.lineno &&
7191 (pend - pbeg) > yylloc->end_pos.column) {
7192 pt = pbeg + yylloc->end_pos.column;
7193 }
7194
7195 ptr = ptr_end = pt;
7196 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7197 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7198
7199 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7200 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7201
7202 len = ptr_end - ptr;
7203 if (len > 4) {
7204 if (ptr > pbeg) {
7205 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7206 if (ptr > pbeg) pre = "...";
7207 }
7208 if (ptr_end < pend) {
7209 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7210 if (ptr_end < pend) post = "...";
7211 }
7212 }
7213 pb = pbeg;
7214 if (lineno == yylloc->beg_pos.lineno) {
7215 pb += yylloc->beg_pos.column;
7216 if (pb > pt) pb = pt;
7217 }
7218 if (pb < ptr) pb = ptr;
7219 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7220 return;
7221 }
7222 if (RTEST(errbuf)) {
7223 mesg = rb_attr_get(errbuf, idMesg);
7224 if (char_at_end(p, mesg, '\n') != '\n')
7225 rb_str_cat_cstr(mesg, "\n");
7226 }
7227 else {
7228 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7229 }
7230 if (!errbuf && rb_stderr_tty_p()) {
7231#define CSI_BEGIN "\033["
7232#define CSI_SGR "m"
7233 rb_str_catf(mesg,
7234 CSI_BEGIN""CSI_SGR"%s" /* pre */
7235 CSI_BEGIN"1"CSI_SGR"%.*s"
7236 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7237 CSI_BEGIN";1"CSI_SGR"%.*s"
7238 CSI_BEGIN""CSI_SGR"%s" /* post */
7239 "\n",
7240 pre,
7241 (int)(pb - ptr), ptr,
7242 (int)(pt - pb), pb,
7243 (int)(ptr_end - pt), pt,
7244 post);
7245 }
7246 else {
7247 char *p2;
7248
7249 len = ptr_end - ptr;
7250 lim = pt < pend ? pt : pend;
7251 i = (int)(lim - ptr);
7252 buf = ALLOCA_N(char, i+2);
7253 code = ptr;
7254 caret = p2 = buf;
7255 if (ptr <= pb) {
7256 while (ptr < pb) {
7257 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7258 }
7259 *p2++ = '^';
7260 ptr++;
7261 }
7262 if (lim > ptr) {
7263 memset(p2, '~', (lim - ptr));
7264 p2 += (lim - ptr);
7265 }
7266 *p2 = '\0';
7267 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7268 pre, (int)len, code, post,
7269 pre, caret);
7270 }
7271 if (!errbuf) rb_write_error_str(mesg);
7272}
7273#else
7274
7275static int
7276parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7277{
7278 const char *pcur = 0, *ptok = 0;
7279 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7280 p->ruby_sourceline == yylloc->end_pos.lineno) {
7281 pcur = p->lex.pcur;
7282 ptok = p->lex.ptok;
7283 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7284 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7285 }
7286 parser_yyerror0(p, msg);
7287 if (pcur) {
7288 p->lex.ptok = ptok;
7289 p->lex.pcur = pcur;
7290 }
7291 return 0;
7292}
7293
7294static int
7295parser_yyerror0(struct parser_params *p, const char *msg)
7296{
7297 dispatch1(parse_error, STR_NEW2(msg));
7298 ripper_error(p);
7299 return 0;
7300}
7301
7302static inline void
7303parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7304{
7305}
7306#endif /* !RIPPER */
7307
7308static int
7309vtable_size(const struct vtable *tbl)
7310{
7311 if (!DVARS_TERMINAL_P(tbl)) {
7312 return tbl->pos;
7313 }
7314 else {
7315 return 0;
7316 }
7317}
7318
7319static struct vtable *
7320vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7321{
7322 struct vtable *tbl = ALLOC(struct vtable);
7323 tbl->pos = 0;
7324 tbl->capa = 8;
7325 tbl->tbl = ALLOC_N(ID, tbl->capa);
7326 tbl->prev = prev;
7327#ifndef RIPPER
7328 if (p->debug) {
7329 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7330 }
7331#endif
7332 return tbl;
7333}
7334#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7335
7336static void
7337vtable_free_gen(struct parser_params *p, int line, const char *name,
7338 struct vtable *tbl)
7339{
7340#ifndef RIPPER
7341 if (p->debug) {
7342 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7343 }
7344#endif
7345 if (!DVARS_TERMINAL_P(tbl)) {
7346 if (tbl->tbl) {
7347 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7348 }
7349 ruby_sized_xfree(tbl, sizeof(*tbl));
7350 }
7351}
7352#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7353
7354static void
7355vtable_add_gen(struct parser_params *p, int line, const char *name,
7356 struct vtable *tbl, ID id)
7357{
7358#ifndef RIPPER
7359 if (p->debug) {
7360 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7361 line, name, (void *)tbl, rb_id2name(id));
7362 }
7363#endif
7364 if (DVARS_TERMINAL_P(tbl)) {
7365 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7366 return;
7367 }
7368 if (tbl->pos == tbl->capa) {
7369 tbl->capa = tbl->capa * 2;
7370 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7371 }
7372 tbl->tbl[tbl->pos++] = id;
7373}
7374#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7375
7376static void
7377vtable_pop_gen(struct parser_params *p, int line, const char *name,
7378 struct vtable *tbl, int n)
7379{
7380 if (p->debug) {
7381 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7382 line, name, (void *)tbl, n);
7383 }
7384 if (tbl->pos < n) {
7385 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7386 return;
7387 }
7388 tbl->pos -= n;
7389}
7390#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7391
7392static int
7393vtable_included(const struct vtable * tbl, ID id)
7394{
7395 int i;
7396
7397 if (!DVARS_TERMINAL_P(tbl)) {
7398 for (i = 0; i < tbl->pos; i++) {
7399 if (tbl->tbl[i] == id) {
7400 return i+1;
7401 }
7402 }
7403 }
7404 return 0;
7405}
7406
7407static void parser_prepare(struct parser_params *p);
7408
7409static int
7410e_option_supplied(struct parser_params *p)
7411{
7412 return strcmp(p->ruby_sourcefile, "-e") == 0;
7413}
7414
7415#ifndef RIPPER
7416static NODE *parser_append_options(struct parser_params *p, NODE *node);
7417
7418static VALUE
7419yycompile0(VALUE arg)
7420{
7421 int n;
7422 NODE *tree;
7423 struct parser_params *p = (struct parser_params *)arg;
7424 int cov = FALSE;
7425
7426 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7427 cov = TRUE;
7428 }
7429
7430 if (p->debug_lines) {
7431 p->ast->body.script_lines = p->debug_lines;
7432 }
7433
7434 parser_prepare(p);
7435#define RUBY_DTRACE_PARSE_HOOK(name) \
7436 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7437 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7438 }
7439 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7440 n = yyparse(p);
7441 RUBY_DTRACE_PARSE_HOOK(END);
7442
7443 p->debug_lines = 0;
7444
7445 xfree(p->lex.strterm);
7446 p->lex.strterm = 0;
7447 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7448 if (n || p->error_p) {
7449 VALUE mesg = p->error_buffer;
7450 if (!mesg) {
7451 mesg = syntax_error_new();
7452 }
7453 if (!p->error_tolerant) {
7454 rb_set_errinfo(mesg);
7455 return FALSE;
7456 }
7457 }
7458 tree = p->eval_tree;
7459 if (!tree) {
7460 tree = NEW_NIL(&NULL_LOC);
7461 }
7462 else {
7463 rb_parser_ary_t *tokens = p->tokens;
7464 NODE *prelude;
7465 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7466 prelude = block_append(p, p->eval_tree_begin, body);
7467 RNODE_SCOPE(tree)->nd_body = prelude;
7468 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7469 p->ast->body.coverage_enabled = cov;
7470 if (p->keep_tokens) {
7471 p->ast->node_buffer->tokens = tokens;
7472 p->tokens = NULL;
7473 }
7474 }
7475 p->ast->body.root = tree;
7476 p->ast->body.line_count = p->line_count;
7477 return TRUE;
7478}
7479
7480static rb_ast_t *
7481yycompile(struct parser_params *p, VALUE fname, int line)
7482{
7483 rb_ast_t *ast;
7484 if (NIL_P(fname)) {
7485 p->ruby_sourcefile_string = Qnil;
7486 p->ruby_sourcefile = "(none)";
7487 }
7488 else {
7489 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7490 p->ruby_sourcefile = StringValueCStr(fname);
7491 }
7492 p->ruby_sourceline = line - 1;
7493
7494 p->lvtbl = NULL;
7495
7496 p->ast = ast = rb_ast_new();
7497 compile_callback(yycompile0, (VALUE)p);
7498 p->ast = 0;
7499
7500 while (p->lvtbl) {
7501 local_pop(p);
7502 }
7503
7504 return ast;
7505}
7506#endif /* !RIPPER */
7507
7508static rb_encoding *
7509must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7510{
7511 rb_encoding *enc = rb_parser_str_get_encoding(s);
7512 if (!rb_enc_asciicompat(enc)) {
7513 rb_raise(rb_eArgError, "invalid source encoding");
7514 }
7515 return enc;
7516}
7517
7518static rb_parser_string_t *
7519lex_getline(struct parser_params *p)
7520{
7521 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7522 if (!line) return 0;
7523 p->line_count++;
7524 string_buffer_append(p, line);
7525 must_be_ascii_compatible(p, line);
7526 return line;
7527}
7528
7529#ifndef RIPPER
7530rb_ast_t*
7531rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7532{
7533 p->lex.gets = gets;
7534 p->lex.input = input;
7535 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7536
7537 return yycompile(p, fname, line);
7538}
7539#endif /* !RIPPER */
7540
7541#define STR_FUNC_ESCAPE 0x01
7542#define STR_FUNC_EXPAND 0x02
7543#define STR_FUNC_REGEXP 0x04
7544#define STR_FUNC_QWORDS 0x08
7545#define STR_FUNC_SYMBOL 0x10
7546#define STR_FUNC_INDENT 0x20
7547#define STR_FUNC_LABEL 0x40
7548#define STR_FUNC_LIST 0x4000
7549#define STR_FUNC_TERM 0x8000
7550
7551enum string_type {
7552 str_label = STR_FUNC_LABEL,
7553 str_squote = (0),
7554 str_dquote = (STR_FUNC_EXPAND),
7555 str_xquote = (STR_FUNC_EXPAND),
7556 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7557 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7558 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7559 str_ssym = (STR_FUNC_SYMBOL),
7560 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7561};
7562
7563static rb_parser_string_t *
7564parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7565{
7566 rb_parser_string_t *pstr;
7567
7568 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7569
7570 if (!(func & STR_FUNC_REGEXP)) {
7571 if (rb_parser_is_ascii_string(p, pstr)) {
7572 }
7573 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7574 /* everything is valid in ASCII-8BIT */
7575 enc = rb_ascii8bit_encoding();
7576 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7577 }
7578 }
7579
7580 return pstr;
7581}
7582
7583static int
7584strterm_is_heredoc(rb_strterm_t *strterm)
7585{
7586 return strterm->heredoc;
7587}
7588
7589static rb_strterm_t *
7590new_strterm(struct parser_params *p, int func, int term, int paren)
7591{
7592 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7593 strterm->u.literal.func = func;
7594 strterm->u.literal.term = term;
7595 strterm->u.literal.paren = paren;
7596 return strterm;
7597}
7598
7599static rb_strterm_t *
7600new_heredoc(struct parser_params *p)
7601{
7602 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7603 strterm->heredoc = true;
7604 return strterm;
7605}
7606
7607#define peek(p,c) peek_n(p, (c), 0)
7608#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7609#define peekc(p) peekc_n(p, 0)
7610#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7611
7612#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7613static void
7614parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7615{
7616 debug_token_line(p, "add_delayed_token", line);
7617
7618 if (tok < end) {
7619 if (has_delayed_token(p)) {
7620 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7621 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7622 int end_col = (next_line ? 0 : p->delayed.end_col);
7623 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7624 dispatch_delayed_token(p, tSTRING_CONTENT);
7625 }
7626 }
7627 if (!has_delayed_token(p)) {
7628 p->delayed.token = rb_parser_string_new(p, 0, 0);
7629 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7630 p->delayed.beg_line = p->ruby_sourceline;
7631 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7632 }
7633 parser_str_cat(p->delayed.token, tok, end - tok);
7634 p->delayed.end_line = p->ruby_sourceline;
7635 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7636 p->lex.ptok = end;
7637 }
7638}
7639
7640static void
7641set_lastline(struct parser_params *p, rb_parser_string_t *str)
7642{
7643 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7644 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7645 p->lex.lastline = str;
7646}
7647
7648static int
7649nextline(struct parser_params *p, int set_encoding)
7650{
7651 rb_parser_string_t *str = p->lex.nextline;
7652 p->lex.nextline = 0;
7653 if (!str) {
7654 if (p->eofp)
7655 return -1;
7656
7657 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7658 goto end_of_input;
7659 }
7660
7661 if (!p->lex.input || !(str = lex_getline(p))) {
7662 end_of_input:
7663 p->eofp = 1;
7664 lex_goto_eol(p);
7665 return -1;
7666 }
7667#ifndef RIPPER
7668 if (p->debug_lines) {
7669 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7670 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7671 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7672 }
7673#endif
7674 p->cr_seen = FALSE;
7675 }
7676 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7677 /* after here-document without terminator */
7678 goto end_of_input;
7679 }
7680 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7681 if (p->heredoc_end > 0) {
7682 p->ruby_sourceline = p->heredoc_end;
7683 p->heredoc_end = 0;
7684 }
7685 p->ruby_sourceline++;
7686 set_lastline(p, str);
7687 token_flush(p);
7688 return 0;
7689}
7690
7691static int
7692parser_cr(struct parser_params *p, int c)
7693{
7694 if (peek(p, '\n')) {
7695 p->lex.pcur++;
7696 c = '\n';
7697 }
7698 return c;
7699}
7700
7701static inline int
7702nextc0(struct parser_params *p, int set_encoding)
7703{
7704 int c;
7705
7706 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7707 if (nextline(p, set_encoding)) return -1;
7708 }
7709 c = (unsigned char)*p->lex.pcur++;
7710 if (UNLIKELY(c == '\r')) {
7711 c = parser_cr(p, c);
7712 }
7713
7714 return c;
7715}
7716#define nextc(p) nextc0(p, TRUE)
7717
7718static void
7719pushback(struct parser_params *p, int c)
7720{
7721 if (c == -1) return;
7722 p->eofp = 0;
7723 p->lex.pcur--;
7724 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7725 p->lex.pcur--;
7726 }
7727}
7728
7729#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7730
7731#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7732#define tok(p) (p)->tokenbuf
7733#define toklen(p) (p)->tokidx
7734
7735static int
7736looking_at_eol_p(struct parser_params *p)
7737{
7738 const char *ptr = p->lex.pcur;
7739 while (!lex_eol_ptr_p(p, ptr)) {
7740 int c = (unsigned char)*ptr++;
7741 int eol = (c == '\n' || c == '#');
7742 if (eol || !ISSPACE(c)) {
7743 return eol;
7744 }
7745 }
7746 return TRUE;
7747}
7748
7749static char*
7750newtok(struct parser_params *p)
7751{
7752 p->tokidx = 0;
7753 if (!p->tokenbuf) {
7754 p->toksiz = 60;
7755 p->tokenbuf = ALLOC_N(char, 60);
7756 }
7757 if (p->toksiz > 4096) {
7758 p->toksiz = 60;
7759 REALLOC_N(p->tokenbuf, char, 60);
7760 }
7761 return p->tokenbuf;
7762}
7763
7764static char *
7765tokspace(struct parser_params *p, int n)
7766{
7767 p->tokidx += n;
7768
7769 if (p->tokidx >= p->toksiz) {
7770 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7771 REALLOC_N(p->tokenbuf, char, p->toksiz);
7772 }
7773 return &p->tokenbuf[p->tokidx-n];
7774}
7775
7776static void
7777tokadd(struct parser_params *p, int c)
7778{
7779 p->tokenbuf[p->tokidx++] = (char)c;
7780 if (p->tokidx >= p->toksiz) {
7781 p->toksiz *= 2;
7782 REALLOC_N(p->tokenbuf, char, p->toksiz);
7783 }
7784}
7785
7786static int
7787tok_hex(struct parser_params *p, size_t *numlen)
7788{
7789 int c;
7790
7791 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7792 if (!*numlen) {
7793 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7794 yyerror0("invalid hex escape");
7795 dispatch_scan_event(p, tSTRING_CONTENT);
7796 return 0;
7797 }
7798 p->lex.pcur += *numlen;
7799 return c;
7800}
7801
7802#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7803
7804static int
7805escaped_control_code(int c)
7806{
7807 int c2 = 0;
7808 switch (c) {
7809 case ' ':
7810 c2 = 's';
7811 break;
7812 case '\n':
7813 c2 = 'n';
7814 break;
7815 case '\t':
7816 c2 = 't';
7817 break;
7818 case '\v':
7819 c2 = 'v';
7820 break;
7821 case '\r':
7822 c2 = 'r';
7823 break;
7824 case '\f':
7825 c2 = 'f';
7826 break;
7827 }
7828 return c2;
7829}
7830
7831#define WARN_SPACE_CHAR(c, prefix) \
7832 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7833
7834static int
7835tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7836 int regexp_literal, const char *begin)
7837{
7838 const int wide = !begin;
7839 size_t numlen;
7840 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7841
7842 p->lex.pcur += numlen;
7843 if (p->lex.strterm == NULL ||
7844 strterm_is_heredoc(p->lex.strterm) ||
7845 (p->lex.strterm->u.literal.func != str_regexp)) {
7846 if (!begin) begin = p->lex.pcur;
7847 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7848 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7849 yyerror0("invalid Unicode escape");
7850 dispatch_scan_event(p, tSTRING_CONTENT);
7851 return wide && numlen > 0;
7852 }
7853 if (codepoint > 0x10ffff) {
7854 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7855 yyerror0("invalid Unicode codepoint (too large)");
7856 dispatch_scan_event(p, tSTRING_CONTENT);
7857 return wide;
7858 }
7859 if ((codepoint & 0xfffff800) == 0xd800) {
7860 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7861 yyerror0("invalid Unicode codepoint");
7862 dispatch_scan_event(p, tSTRING_CONTENT);
7863 return wide;
7864 }
7865 }
7866 if (regexp_literal) {
7867 tokcopy(p, (int)numlen);
7868 }
7869 else if (codepoint >= 0x80) {
7870 rb_encoding *utf8 = rb_utf8_encoding();
7871 if (*encp && utf8 != *encp) {
7872 YYLTYPE loc = RUBY_INIT_YYLLOC();
7873 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7874 parser_show_error_line(p, &loc);
7875 return wide;
7876 }
7877 *encp = utf8;
7878 tokaddmbc(p, codepoint, *encp);
7879 }
7880 else {
7881 tokadd(p, codepoint);
7882 }
7883 return TRUE;
7884}
7885
7886static int tokadd_mbchar(struct parser_params *p, int c);
7887
7888static int
7889tokskip_mbchar(struct parser_params *p)
7890{
7891 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7892 if (len > 0) {
7893 p->lex.pcur += len - 1;
7894 }
7895 return len;
7896}
7897
7898/* return value is for ?\u3042 */
7899static void
7900tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7901 int term, int symbol_literal, int regexp_literal)
7902{
7903 /*
7904 * If `term` is not -1, then we allow multiple codepoints in \u{}
7905 * upto `term` byte, otherwise we're parsing a character literal.
7906 * And then add the codepoints to the current token.
7907 */
7908 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7909
7910 const int open_brace = '{', close_brace = '}';
7911
7912 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7913
7914 if (peek(p, open_brace)) { /* handle \u{...} form */
7915 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7916 /*
7917 * Skip parsing validation code and copy bytes as-is until term or
7918 * closing brace, in order to correctly handle extended regexps where
7919 * invalid unicode escapes are allowed in comments. The regexp parser
7920 * does its own validation and will catch any issues.
7921 */
7922 tokadd(p, open_brace);
7923 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7924 int c = peekc(p);
7925 if (c == close_brace) {
7926 tokadd(p, c);
7927 ++p->lex.pcur;
7928 break;
7929 }
7930 else if (c == term) {
7931 break;
7932 }
7933 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7934 tokadd(p, c);
7935 c = *++p->lex.pcur;
7936 }
7937 tokadd_mbchar(p, c);
7938 }
7939 }
7940 else {
7941 const char *second = NULL;
7942 int c, last = nextc(p);
7943 if (lex_eol_p(p)) goto unterminated;
7944 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7945 while (c != close_brace) {
7946 if (c == term) goto unterminated;
7947 if (second == multiple_codepoints)
7948 second = p->lex.pcur;
7949 if (regexp_literal) tokadd(p, last);
7950 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7951 break;
7952 }
7953 while (ISSPACE(c = peekc(p))) {
7954 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7955 last = c;
7956 }
7957 if (term == -1 && !second)
7958 second = multiple_codepoints;
7959 }
7960
7961 if (c != close_brace) {
7962 unterminated:
7963 flush_string_content(p, rb_utf8_encoding(), 0);
7964 yyerror0("unterminated Unicode escape");
7965 dispatch_scan_event(p, tSTRING_CONTENT);
7966 return;
7967 }
7968 if (second && second != multiple_codepoints) {
7969 const char *pcur = p->lex.pcur;
7970 p->lex.pcur = second;
7971 dispatch_scan_event(p, tSTRING_CONTENT);
7972 token_flush(p);
7973 p->lex.pcur = pcur;
7974 yyerror0(multiple_codepoints);
7975 token_flush(p);
7976 }
7977
7978 if (regexp_literal) tokadd(p, close_brace);
7979 nextc(p);
7980 }
7981 }
7982 else { /* handle \uxxxx form */
7983 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7984 token_flush(p);
7985 return;
7986 }
7987 }
7988}
7989
7990#define ESCAPE_CONTROL 1
7991#define ESCAPE_META 2
7992
7993static int
7994read_escape(struct parser_params *p, int flags, const char *begin)
7995{
7996 int c;
7997 size_t numlen;
7998
7999 switch (c = nextc(p)) {
8000 case '\\': /* Backslash */
8001 return c;
8002
8003 case 'n': /* newline */
8004 return '\n';
8005
8006 case 't': /* horizontal tab */
8007 return '\t';
8008
8009 case 'r': /* carriage-return */
8010 return '\r';
8011
8012 case 'f': /* form-feed */
8013 return '\f';
8014
8015 case 'v': /* vertical tab */
8016 return '\13';
8017
8018 case 'a': /* alarm(bell) */
8019 return '\007';
8020
8021 case 'e': /* escape */
8022 return 033;
8023
8024 case '0': case '1': case '2': case '3': /* octal constant */
8025 case '4': case '5': case '6': case '7':
8026 pushback(p, c);
8027 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8028 p->lex.pcur += numlen;
8029 return c;
8030
8031 case 'x': /* hex constant */
8032 c = tok_hex(p, &numlen);
8033 if (numlen == 0) return 0;
8034 return c;
8035
8036 case 'b': /* backspace */
8037 return '\010';
8038
8039 case 's': /* space */
8040 return ' ';
8041
8042 case 'M':
8043 if (flags & ESCAPE_META) goto eof;
8044 if ((c = nextc(p)) != '-') {
8045 goto eof;
8046 }
8047 if ((c = nextc(p)) == '\\') {
8048 switch (peekc(p)) {
8049 case 'u': case 'U':
8050 nextc(p);
8051 goto eof;
8052 }
8053 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8054 }
8055 else if (c == -1) goto eof;
8056 else if (!ISASCII(c)) {
8057 tokskip_mbchar(p);
8058 goto eof;
8059 }
8060 else {
8061 int c2 = escaped_control_code(c);
8062 if (c2) {
8063 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8064 WARN_SPACE_CHAR(c2, "\\M-");
8065 }
8066 else {
8067 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8068 }
8069 }
8070 else if (ISCNTRL(c)) goto eof;
8071 return ((c & 0xff) | 0x80);
8072 }
8073
8074 case 'C':
8075 if ((c = nextc(p)) != '-') {
8076 goto eof;
8077 }
8078 case 'c':
8079 if (flags & ESCAPE_CONTROL) goto eof;
8080 if ((c = nextc(p))== '\\') {
8081 switch (peekc(p)) {
8082 case 'u': case 'U':
8083 nextc(p);
8084 goto eof;
8085 }
8086 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8087 }
8088 else if (c == '?')
8089 return 0177;
8090 else if (c == -1) goto eof;
8091 else if (!ISASCII(c)) {
8092 tokskip_mbchar(p);
8093 goto eof;
8094 }
8095 else {
8096 int c2 = escaped_control_code(c);
8097 if (c2) {
8098 if (ISCNTRL(c)) {
8099 if (flags & ESCAPE_META) {
8100 WARN_SPACE_CHAR(c2, "\\M-");
8101 }
8102 else {
8103 WARN_SPACE_CHAR(c2, "");
8104 }
8105 }
8106 else {
8107 if (flags & ESCAPE_META) {
8108 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8109 }
8110 else {
8111 WARN_SPACE_CHAR(c2, "\\C-");
8112 }
8113 }
8114 }
8115 else if (ISCNTRL(c)) goto eof;
8116 }
8117 return c & 0x9f;
8118
8119 eof:
8120 case -1:
8121 flush_string_content(p, p->enc, p->lex.pcur - begin);
8122 yyerror0("Invalid escape character syntax");
8123 dispatch_scan_event(p, tSTRING_CONTENT);
8124 return '\0';
8125
8126 default:
8127 if (!ISASCII(c)) {
8128 tokskip_mbchar(p);
8129 goto eof;
8130 }
8131 return c;
8132 }
8133}
8134
8135static void
8136tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8137{
8138 int len = rb_enc_codelen(c, enc);
8139 rb_enc_mbcput(c, tokspace(p, len), enc);
8140}
8141
8142static int
8143tokadd_escape(struct parser_params *p)
8144{
8145 int c;
8146 size_t numlen;
8147 const char *begin = p->lex.pcur;
8148
8149 switch (c = nextc(p)) {
8150 case '\n':
8151 return 0; /* just ignore */
8152
8153 case '0': case '1': case '2': case '3': /* octal constant */
8154 case '4': case '5': case '6': case '7':
8155 {
8156 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8157 if (numlen == 0) goto eof;
8158 p->lex.pcur += numlen;
8159 tokcopy(p, (int)numlen + 1);
8160 }
8161 return 0;
8162
8163 case 'x': /* hex constant */
8164 {
8165 tok_hex(p, &numlen);
8166 if (numlen == 0) return -1;
8167 tokcopy(p, (int)numlen + 2);
8168 }
8169 return 0;
8170
8171 eof:
8172 case -1:
8173 flush_string_content(p, p->enc, p->lex.pcur - begin);
8174 yyerror0("Invalid escape character syntax");
8175 token_flush(p);
8176 return -1;
8177
8178 default:
8179 tokadd(p, '\\');
8180 tokadd(p, c);
8181 }
8182 return 0;
8183}
8184
8185static int
8186char_to_option(int c)
8187{
8188 int val;
8189
8190 switch (c) {
8191 case 'i':
8192 val = RE_ONIG_OPTION_IGNORECASE;
8193 break;
8194 case 'x':
8195 val = RE_ONIG_OPTION_EXTEND;
8196 break;
8197 case 'm':
8198 val = RE_ONIG_OPTION_MULTILINE;
8199 break;
8200 default:
8201 val = 0;
8202 break;
8203 }
8204 return val;
8205}
8206
8207#define ARG_ENCODING_FIXED 16
8208#define ARG_ENCODING_NONE 32
8209#define ENC_ASCII8BIT 1
8210#define ENC_EUC_JP 2
8211#define ENC_Windows_31J 3
8212#define ENC_UTF8 4
8213
8214static int
8215char_to_option_kcode(int c, int *option, int *kcode)
8216{
8217 *option = 0;
8218
8219 switch (c) {
8220 case 'n':
8221 *kcode = ENC_ASCII8BIT;
8222 return (*option = ARG_ENCODING_NONE);
8223 case 'e':
8224 *kcode = ENC_EUC_JP;
8225 break;
8226 case 's':
8227 *kcode = ENC_Windows_31J;
8228 break;
8229 case 'u':
8230 *kcode = ENC_UTF8;
8231 break;
8232 default:
8233 *kcode = -1;
8234 return (*option = char_to_option(c));
8235 }
8236 *option = ARG_ENCODING_FIXED;
8237 return 1;
8238}
8239
8240static int
8241regx_options(struct parser_params *p)
8242{
8243 int kcode = 0;
8244 int kopt = 0;
8245 int options = 0;
8246 int c, opt, kc;
8247
8248 newtok(p);
8249 while (c = nextc(p), ISALPHA(c)) {
8250 if (c == 'o') {
8251 options |= RE_OPTION_ONCE;
8252 }
8253 else if (char_to_option_kcode(c, &opt, &kc)) {
8254 if (kc >= 0) {
8255 if (kc != ENC_ASCII8BIT) kcode = c;
8256 kopt = opt;
8257 }
8258 else {
8259 options |= opt;
8260 }
8261 }
8262 else {
8263 tokadd(p, c);
8264 }
8265 }
8266 options |= kopt;
8267 pushback(p, c);
8268 if (toklen(p)) {
8269 YYLTYPE loc = RUBY_INIT_YYLLOC();
8270 tokfix(p);
8271 compile_error(p, "unknown regexp option%s - %*s",
8272 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8273 parser_show_error_line(p, &loc);
8274 }
8275 return options | RE_OPTION_ENCODING(kcode);
8276}
8277
8278static int
8279tokadd_mbchar(struct parser_params *p, int c)
8280{
8281 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8282 if (len < 0) return -1;
8283 tokadd(p, c);
8284 p->lex.pcur += --len;
8285 if (len > 0) tokcopy(p, len);
8286 return c;
8287}
8288
8289static inline int
8290simple_re_meta(int c)
8291{
8292 switch (c) {
8293 case '$': case '*': case '+': case '.':
8294 case '?': case '^': case '|':
8295 case ')': case ']': case '}': case '>':
8296 return TRUE;
8297 default:
8298 return FALSE;
8299 }
8300}
8301
8302static int
8303parser_update_heredoc_indent(struct parser_params *p, int c)
8304{
8305 if (p->heredoc_line_indent == -1) {
8306 if (c == '\n') p->heredoc_line_indent = 0;
8307 }
8308 else {
8309 if (c == ' ') {
8310 p->heredoc_line_indent++;
8311 return TRUE;
8312 }
8313 else if (c == '\t') {
8314 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8315 p->heredoc_line_indent = w * TAB_WIDTH;
8316 return TRUE;
8317 }
8318 else if (c != '\n') {
8319 if (p->heredoc_indent > p->heredoc_line_indent) {
8320 p->heredoc_indent = p->heredoc_line_indent;
8321 }
8322 p->heredoc_line_indent = -1;
8323 }
8324 else {
8325 /* Whitespace only line has no indentation */
8326 p->heredoc_line_indent = 0;
8327 }
8328 }
8329 return FALSE;
8330}
8331
8332static void
8333parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8334{
8335 YYLTYPE loc = RUBY_INIT_YYLLOC();
8336 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8337 compile_error(p, "%s mixed within %s source", n1, n2);
8338 parser_show_error_line(p, &loc);
8339}
8340
8341static void
8342parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8343{
8344 const char *pos = p->lex.pcur;
8345 p->lex.pcur = beg;
8346 parser_mixed_error(p, enc1, enc2);
8347 p->lex.pcur = pos;
8348}
8349
8350static inline char
8351nibble_char_upper(unsigned int c)
8352{
8353 c &= 0xf;
8354 return c + (c < 10 ? '0' : 'A' - 10);
8355}
8356
8357static int
8358tokadd_string(struct parser_params *p,
8359 int func, int term, int paren, long *nest,
8360 rb_encoding **encp, rb_encoding **enc)
8361{
8362 int c;
8363 bool erred = false;
8364#ifdef RIPPER
8365 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8366 int top_of_line = FALSE;
8367#endif
8368
8369#define mixed_error(enc1, enc2) \
8370 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8371#define mixed_escape(beg, enc1, enc2) \
8372 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8373
8374 while ((c = nextc(p)) != -1) {
8375 if (p->heredoc_indent > 0) {
8376 parser_update_heredoc_indent(p, c);
8377 }
8378#ifdef RIPPER
8379 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8380 pushback(p, c);
8381 break;
8382 }
8383#endif
8384
8385 if (paren && c == paren) {
8386 ++*nest;
8387 }
8388 else if (c == term) {
8389 if (!nest || !*nest) {
8390 pushback(p, c);
8391 break;
8392 }
8393 --*nest;
8394 }
8395 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8396 unsigned char c2 = *p->lex.pcur;
8397 if (c2 == '$' || c2 == '@' || c2 == '{') {
8398 pushback(p, c);
8399 break;
8400 }
8401 }
8402 else if (c == '\\') {
8403 c = nextc(p);
8404 switch (c) {
8405 case '\n':
8406 if (func & STR_FUNC_QWORDS) break;
8407 if (func & STR_FUNC_EXPAND) {
8408 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8409 continue;
8410 if (c == term) {
8411 c = '\\';
8412 goto terminate;
8413 }
8414 }
8415 tokadd(p, '\\');
8416 break;
8417
8418 case '\\':
8419 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8420 break;
8421
8422 case 'u':
8423 if ((func & STR_FUNC_EXPAND) == 0) {
8424 tokadd(p, '\\');
8425 break;
8426 }
8427 tokadd_utf8(p, enc, term,
8428 func & STR_FUNC_SYMBOL,
8429 func & STR_FUNC_REGEXP);
8430 continue;
8431
8432 default:
8433 if (c == -1) return -1;
8434 if (!ISASCII(c)) {
8435 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8436 goto non_ascii;
8437 }
8438 if (func & STR_FUNC_REGEXP) {
8439 switch (c) {
8440 case 'c':
8441 case 'C':
8442 case 'M': {
8443 pushback(p, c);
8444 c = read_escape(p, 0, p->lex.pcur - 1);
8445
8446 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8447 *t++ = '\\';
8448 *t++ = 'x';
8449 *t++ = nibble_char_upper(c >> 4);
8450 *t++ = nibble_char_upper(c);
8451 continue;
8452 }
8453 }
8454
8455 if (c == term && !simple_re_meta(c)) {
8456 tokadd(p, c);
8457 continue;
8458 }
8459 pushback(p, c);
8460 if ((c = tokadd_escape(p)) < 0)
8461 return -1;
8462 if (*enc && *enc != *encp) {
8463 mixed_escape(p->lex.ptok+2, *enc, *encp);
8464 }
8465 continue;
8466 }
8467 else if (func & STR_FUNC_EXPAND) {
8468 pushback(p, c);
8469 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8470 c = read_escape(p, 0, p->lex.pcur - 1);
8471 }
8472 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8473 /* ignore backslashed spaces in %w */
8474 }
8475 else if (c != term && !(paren && c == paren)) {
8476 tokadd(p, '\\');
8477 pushback(p, c);
8478 continue;
8479 }
8480 }
8481 }
8482 else if (!parser_isascii(p)) {
8483 non_ascii:
8484 if (!*enc) {
8485 *enc = *encp;
8486 }
8487 else if (*enc != *encp) {
8488 mixed_error(*enc, *encp);
8489 continue;
8490 }
8491 if (tokadd_mbchar(p, c) == -1) return -1;
8492 continue;
8493 }
8494 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8495 pushback(p, c);
8496 break;
8497 }
8498 if (c & 0x80) {
8499 if (!*enc) {
8500 *enc = *encp;
8501 }
8502 else if (*enc != *encp) {
8503 mixed_error(*enc, *encp);
8504 continue;
8505 }
8506 }
8507 tokadd(p, c);
8508#ifdef RIPPER
8509 top_of_line = (c == '\n');
8510#endif
8511 }
8512 terminate:
8513 if (*enc) *encp = *enc;
8514 return c;
8515}
8516
8517#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8518
8519static void
8520flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8521{
8522 p->lex.pcur -= back;
8523 if (has_delayed_token(p)) {
8524 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8525 if (len > 0) {
8526 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8527 p->delayed.end_line = p->ruby_sourceline;
8528 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8529 }
8530 dispatch_delayed_token(p, tSTRING_CONTENT);
8531 p->lex.ptok = p->lex.pcur;
8532 }
8533 dispatch_scan_event(p, tSTRING_CONTENT);
8534 p->lex.pcur += back;
8535}
8536
8537/* this can be shared with ripper, since it's independent from struct
8538 * parser_params. */
8539#ifndef RIPPER
8540#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8541#define SPECIAL_PUNCT(idx) ( \
8542 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8543 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8544 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8545 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8546 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8547 BIT('0', idx))
8548const uint_least32_t ruby_global_name_punct_bits[] = {
8549 SPECIAL_PUNCT(0),
8550 SPECIAL_PUNCT(1),
8551 SPECIAL_PUNCT(2),
8552};
8553#undef BIT
8554#undef SPECIAL_PUNCT
8555#endif
8556
8557static enum yytokentype
8558parser_peek_variable_name(struct parser_params *p)
8559{
8560 int c;
8561 const char *ptr = p->lex.pcur;
8562
8563 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8564 c = *ptr++;
8565 switch (c) {
8566 case '$':
8567 if ((c = *ptr) == '-') {
8568 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8569 c = *ptr;
8570 }
8571 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8572 return tSTRING_DVAR;
8573 }
8574 break;
8575 case '@':
8576 if ((c = *ptr) == '@') {
8577 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8578 c = *ptr;
8579 }
8580 break;
8581 case '{':
8582 p->lex.pcur = ptr;
8583 p->command_start = TRUE;
8584 yylval.state = p->lex.state;
8585 return tSTRING_DBEG;
8586 default:
8587 return 0;
8588 }
8589 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8590 return tSTRING_DVAR;
8591 return 0;
8592}
8593
8594#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8595#define IS_END() IS_lex_state(EXPR_END_ANY)
8596#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8597#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8598#define IS_LABEL_POSSIBLE() (\
8599 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8600 IS_ARG())
8601#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8602#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8603
8604static inline enum yytokentype
8605parser_string_term(struct parser_params *p, int func)
8606{
8607 xfree(p->lex.strterm);
8608 p->lex.strterm = 0;
8609 if (func & STR_FUNC_REGEXP) {
8610 set_yylval_num(regx_options(p));
8611 dispatch_scan_event(p, tREGEXP_END);
8612 SET_LEX_STATE(EXPR_END);
8613 return tREGEXP_END;
8614 }
8615 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8616 nextc(p);
8617 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8618 return tLABEL_END;
8619 }
8620 SET_LEX_STATE(EXPR_END);
8621 return tSTRING_END;
8622}
8623
8624static enum yytokentype
8625parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8626{
8627 int func = quote->func;
8628 int term = quote->term;
8629 int paren = quote->paren;
8630 int c, space = 0;
8631 rb_encoding *enc = p->enc;
8632 rb_encoding *base_enc = 0;
8633 rb_parser_string_t *lit;
8634
8635 if (func & STR_FUNC_TERM) {
8636 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8637 SET_LEX_STATE(EXPR_END);
8638 xfree(p->lex.strterm);
8639 p->lex.strterm = 0;
8640 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8641 }
8642 c = nextc(p);
8643 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8644 while (c != '\n' && ISSPACE(c = nextc(p)));
8645 space = 1;
8646 }
8647 if (func & STR_FUNC_LIST) {
8648 quote->func &= ~STR_FUNC_LIST;
8649 space = 1;
8650 }
8651 if (c == term && !quote->nest) {
8652 if (func & STR_FUNC_QWORDS) {
8653 quote->func |= STR_FUNC_TERM;
8654 pushback(p, c); /* dispatch the term at tSTRING_END */
8655 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8656 return ' ';
8657 }
8658 return parser_string_term(p, func);
8659 }
8660 if (space) {
8661 if (!ISSPACE(c)) pushback(p, c);
8662 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8663 return ' ';
8664 }
8665 newtok(p);
8666 if ((func & STR_FUNC_EXPAND) && c == '#') {
8667 enum yytokentype t = parser_peek_variable_name(p);
8668 if (t) return t;
8669 tokadd(p, '#');
8670 c = nextc(p);
8671 }
8672 pushback(p, c);
8673 if (tokadd_string(p, func, term, paren, &quote->nest,
8674 &enc, &base_enc) == -1) {
8675 if (p->eofp) {
8676#ifndef RIPPER
8677# define unterminated_literal(mesg) yyerror0(mesg)
8678#else
8679# define unterminated_literal(mesg) compile_error(p, mesg)
8680#endif
8681 literal_flush(p, p->lex.pcur);
8682 if (func & STR_FUNC_QWORDS) {
8683 /* no content to add, bailing out here */
8684 unterminated_literal("unterminated list meets end of file");
8685 xfree(p->lex.strterm);
8686 p->lex.strterm = 0;
8687 return tSTRING_END;
8688 }
8689 if (func & STR_FUNC_REGEXP) {
8690 unterminated_literal("unterminated regexp meets end of file");
8691 }
8692 else {
8693 unterminated_literal("unterminated string meets end of file");
8694 }
8695 quote->func |= STR_FUNC_TERM;
8696 }
8697 }
8698
8699 tokfix(p);
8700 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8701 set_yylval_str(lit);
8702 flush_string_content(p, enc, 0);
8703
8704 return tSTRING_CONTENT;
8705}
8706
8707static enum yytokentype
8708heredoc_identifier(struct parser_params *p)
8709{
8710 /*
8711 * term_len is length of `<<"END"` except `END`,
8712 * in this case term_len is 4 (<, <, " and ").
8713 */
8714 long len, offset = p->lex.pcur - p->lex.pbeg;
8715 int c = nextc(p), term, func = 0, quote = 0;
8716 enum yytokentype token = tSTRING_BEG;
8717 int indent = 0;
8718
8719 if (c == '-') {
8720 c = nextc(p);
8721 func = STR_FUNC_INDENT;
8722 offset++;
8723 }
8724 else if (c == '~') {
8725 c = nextc(p);
8726 func = STR_FUNC_INDENT;
8727 offset++;
8728 indent = INT_MAX;
8729 }
8730 switch (c) {
8731 case '\'':
8732 func |= str_squote; goto quoted;
8733 case '"':
8734 func |= str_dquote; goto quoted;
8735 case '`':
8736 token = tXSTRING_BEG;
8737 func |= str_xquote; goto quoted;
8738
8739 quoted:
8740 quote++;
8741 offset++;
8742 term = c;
8743 len = 0;
8744 while ((c = nextc(p)) != term) {
8745 if (c == -1 || c == '\r' || c == '\n') {
8746 yyerror0("unterminated here document identifier");
8747 return -1;
8748 }
8749 }
8750 break;
8751
8752 default:
8753 if (!parser_is_identchar(p)) {
8754 pushback(p, c);
8755 if (func & STR_FUNC_INDENT) {
8756 pushback(p, indent > 0 ? '~' : '-');
8757 }
8758 return 0;
8759 }
8760 func |= str_dquote;
8761 do {
8762 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8763 if (n < 0) return 0;
8764 p->lex.pcur += --n;
8765 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8766 pushback(p, c);
8767 break;
8768 }
8769
8770 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8771 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8772 yyerror0("too long here document identifier");
8773 dispatch_scan_event(p, tHEREDOC_BEG);
8774 lex_goto_eol(p);
8775
8776 p->lex.strterm = new_heredoc(p);
8777 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8778 here->offset = offset;
8779 here->sourceline = p->ruby_sourceline;
8780 here->length = (unsigned)len;
8781 here->quote = quote;
8782 here->func = func;
8783 here->lastline = p->lex.lastline;
8784
8785 token_flush(p);
8786 p->heredoc_indent = indent;
8787 p->heredoc_line_indent = 0;
8788 return token;
8789}
8790
8791static void
8792heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8793{
8794 rb_parser_string_t *line;
8795 rb_strterm_t *term = p->lex.strterm;
8796
8797 p->lex.strterm = 0;
8798 line = here->lastline;
8799 p->lex.lastline = line;
8800 p->lex.pbeg = PARSER_STRING_PTR(line);
8801 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8802 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8803 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8804 p->heredoc_end = p->ruby_sourceline;
8805 p->ruby_sourceline = (int)here->sourceline;
8806 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8807 p->eofp = 0;
8808 xfree(term);
8809}
8810
8811static int
8812dedent_string_column(const char *str, long len, int width)
8813{
8814 int i, col = 0;
8815
8816 for (i = 0; i < len && col < width; i++) {
8817 if (str[i] == ' ') {
8818 col++;
8819 }
8820 else if (str[i] == '\t') {
8821 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8822 if (n > width) break;
8823 col = n;
8824 }
8825 else {
8826 break;
8827 }
8828 }
8829
8830 return i;
8831}
8832
8833static int
8834dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8835{
8836 char *str;
8837 long len;
8838 int i;
8839
8840 len = PARSER_STRING_LEN(string);
8841 str = PARSER_STRING_PTR(string);
8842
8843 i = dedent_string_column(str, len, width);
8844 if (!i) return 0;
8845
8846 rb_parser_str_modify(string);
8847 str = PARSER_STRING_PTR(string);
8848 if (PARSER_STRING_LEN(string) != len)
8849 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8850 MEMMOVE(str, str + i, char, len - i);
8851 rb_parser_str_set_len(p, string, len - i);
8852 return i;
8853}
8854
8855static NODE *
8856heredoc_dedent(struct parser_params *p, NODE *root)
8857{
8858 NODE *node, *str_node, *prev_node;
8859 int indent = p->heredoc_indent;
8860 rb_parser_string_t *prev_lit = 0;
8861
8862 if (indent <= 0) return root;
8863 if (!root) return root;
8864
8865 prev_node = node = str_node = root;
8866 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8867
8868 while (str_node) {
8869 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8870 if (nd_fl_newline(str_node)) {
8871 dedent_string(p, lit, indent);
8872 }
8873 if (!prev_lit) {
8874 prev_lit = lit;
8875 }
8876 else if (!literal_concat0(p, prev_lit, lit)) {
8877 return 0;
8878 }
8879 else {
8880 NODE *end = RNODE_LIST(node)->as.nd_end;
8881 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8882 if (!node) {
8883 if (nd_type_p(prev_node, NODE_DSTR))
8884 nd_set_type(prev_node, NODE_STR);
8885 break;
8886 }
8887 RNODE_LIST(node)->as.nd_end = end;
8888 goto next_str;
8889 }
8890
8891 str_node = 0;
8892 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8893 next_str:
8894 if (!nd_type_p(node, NODE_LIST)) break;
8895 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8896 enum node_type type = nd_type(str_node);
8897 if (type == NODE_STR || type == NODE_DSTR) break;
8898 prev_lit = 0;
8899 str_node = 0;
8900 }
8901 }
8902 }
8903 return root;
8904}
8905
8906static int
8907whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8908{
8909 const char *beg = p->lex.pbeg;
8910 const char *ptr = p->lex.pend;
8911
8912 if (ptr - beg < len) return FALSE;
8913 if (ptr > beg && ptr[-1] == '\n') {
8914 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8915 if (ptr - beg < len) return FALSE;
8916 }
8917 if (strncmp(eos, ptr -= len, len)) return FALSE;
8918 if (indent) {
8919 while (beg < ptr && ISSPACE(*beg)) beg++;
8920 }
8921 return beg == ptr;
8922}
8923
8924static int
8925word_match_p(struct parser_params *p, const char *word, long len)
8926{
8927 if (strncmp(p->lex.pcur, word, len)) return 0;
8928 if (lex_eol_n_p(p, len)) return 1;
8929 int c = (unsigned char)p->lex.pcur[len];
8930 if (ISSPACE(c)) return 1;
8931 switch (c) {
8932 case '\0': case '\004': case '\032': return 1;
8933 }
8934 return 0;
8935}
8936
8937#define NUM_SUFFIX_R (1<<0)
8938#define NUM_SUFFIX_I (1<<1)
8939#define NUM_SUFFIX_ALL 3
8940
8941static int
8942number_literal_suffix(struct parser_params *p, int mask)
8943{
8944 int c, result = 0;
8945 const char *lastp = p->lex.pcur;
8946
8947 while ((c = nextc(p)) != -1) {
8948 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8949 result |= (mask & NUM_SUFFIX_I);
8950 mask &= ~NUM_SUFFIX_I;
8951 /* r after i, rational of complex is disallowed */
8952 mask &= ~NUM_SUFFIX_R;
8953 continue;
8954 }
8955 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8956 result |= (mask & NUM_SUFFIX_R);
8957 mask &= ~NUM_SUFFIX_R;
8958 continue;
8959 }
8960 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8961 p->lex.pcur = lastp;
8962 return 0;
8963 }
8964 pushback(p, c);
8965 break;
8966 }
8967 return result;
8968}
8969
8970static enum yytokentype
8971set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8972{
8973 enum rb_numeric_type numeric_type = integer_literal;
8974
8975 if (type == tFLOAT) {
8976 numeric_type = float_literal;
8977 }
8978
8979 if (suffix & NUM_SUFFIX_R) {
8980 type = tRATIONAL;
8981 numeric_type = rational_literal;
8982 }
8983 if (suffix & NUM_SUFFIX_I) {
8984 type = tIMAGINARY;
8985 }
8986
8987 switch (type) {
8988 case tINTEGER:
8989 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8990 break;
8991 case tFLOAT:
8992 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8993 break;
8994 case tRATIONAL:
8995 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8996 break;
8997 case tIMAGINARY:
8998 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8999 (void)numeric_type; /* for ripper */
9000 break;
9001 default:
9002 rb_bug("unexpected token: %d", type);
9003 }
9004 SET_LEX_STATE(EXPR_END);
9005 return type;
9006}
9007
9008#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9009static void
9010parser_dispatch_heredoc_end(struct parser_params *p, int line)
9011{
9012 if (has_delayed_token(p))
9013 dispatch_delayed_token(p, tSTRING_CONTENT);
9014
9015#ifdef RIPPER
9016 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9017 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9018#else
9019 if (p->keep_tokens) {
9020 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9021 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9022 parser_append_tokens(p, str, tHEREDOC_END, line);
9023 }
9024#endif
9025
9026 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9027 lex_goto_eol(p);
9028 token_flush(p);
9029}
9030
9031static enum yytokentype
9032here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9033{
9034 int c, func, indent = 0;
9035 const char *eos, *ptr, *ptr_end;
9036 long len;
9037 rb_parser_string_t *str = 0;
9038 rb_encoding *enc = p->enc;
9039 rb_encoding *base_enc = 0;
9040 int bol;
9041#ifdef RIPPER
9042 VALUE s_value;
9043#endif
9044
9045 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9046 len = here->length;
9047 indent = (func = here->func) & STR_FUNC_INDENT;
9048
9049 if ((c = nextc(p)) == -1) {
9050 error:
9051#ifdef RIPPER
9052 if (!has_delayed_token(p)) {
9053 dispatch_scan_event(p, tSTRING_CONTENT);
9054 }
9055 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9056 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9057 if (!(func & STR_FUNC_REGEXP)) {
9058 int cr = ENC_CODERANGE_UNKNOWN;
9059 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9060 if (cr != ENC_CODERANGE_7BIT &&
9061 rb_is_usascii_enc(p->enc) &&
9062 enc != rb_utf8_encoding()) {
9063 enc = rb_ascii8bit_encoding();
9064 }
9065 }
9066 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9067 }
9068 dispatch_delayed_token(p, tSTRING_CONTENT);
9069 }
9070 else {
9071 dispatch_delayed_token(p, tSTRING_CONTENT);
9072 dispatch_scan_event(p, tSTRING_CONTENT);
9073 }
9074 lex_goto_eol(p);
9075#endif
9076 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9077 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9078 (int)len, eos);
9079 token_flush(p);
9080 SET_LEX_STATE(EXPR_END);
9081 return tSTRING_END;
9082 }
9083 bol = was_bol(p);
9084 if (!bol) {
9085 /* not beginning of line, cannot be the terminator */
9086 }
9087 else if (p->heredoc_line_indent == -1) {
9088 /* `heredoc_line_indent == -1` means
9089 * - "after an interpolation in the same line", or
9090 * - "in a continuing line"
9091 */
9092 p->heredoc_line_indent = 0;
9093 }
9094 else if (whole_match_p(p, eos, len, indent)) {
9095 dispatch_heredoc_end(p);
9096 restore:
9097 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9098 token_flush(p);
9099 SET_LEX_STATE(EXPR_END);
9100 return tSTRING_END;
9101 }
9102
9103 if (!(func & STR_FUNC_EXPAND)) {
9104 do {
9105 ptr = PARSER_STRING_PTR(p->lex.lastline);
9106 ptr_end = p->lex.pend;
9107 if (ptr_end > ptr) {
9108 switch (ptr_end[-1]) {
9109 case '\n':
9110 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9111 ptr_end++;
9112 break;
9113 }
9114 case '\r':
9115 --ptr_end;
9116 }
9117 }
9118
9119 if (p->heredoc_indent > 0) {
9120 long i = 0;
9121 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9122 i++;
9123 p->heredoc_line_indent = 0;
9124 }
9125
9126 if (str)
9127 parser_str_cat(str, ptr, ptr_end - ptr);
9128 else
9129 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9130 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9131 lex_goto_eol(p);
9132 if (p->heredoc_indent > 0) {
9133 goto flush_str;
9134 }
9135 if (nextc(p) == -1) {
9136 if (str) {
9137 rb_parser_string_free(p, str);
9138 str = 0;
9139 }
9140 goto error;
9141 }
9142 } while (!whole_match_p(p, eos, len, indent));
9143 }
9144 else {
9145 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9146 newtok(p);
9147 if (c == '#') {
9148 enum yytokentype t = parser_peek_variable_name(p);
9149 if (p->heredoc_line_indent != -1) {
9150 if (p->heredoc_indent > p->heredoc_line_indent) {
9151 p->heredoc_indent = p->heredoc_line_indent;
9152 }
9153 p->heredoc_line_indent = -1;
9154 }
9155 if (t) return t;
9156 tokadd(p, '#');
9157 c = nextc(p);
9158 }
9159 do {
9160 pushback(p, c);
9161 enc = p->enc;
9162 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9163 if (p->eofp) goto error;
9164 goto restore;
9165 }
9166 if (c != '\n') {
9167 if (c == '\\') p->heredoc_line_indent = -1;
9168 flush:
9169 str = STR_NEW3(tok(p), toklen(p), enc, func);
9170 flush_str:
9171 set_yylval_str(str);
9172#ifndef RIPPER
9173 if (bol) nd_set_fl_newline(yylval.node);
9174#endif
9175 flush_string_content(p, enc, 0);
9176 return tSTRING_CONTENT;
9177 }
9178 tokadd(p, nextc(p));
9179 if (p->heredoc_indent > 0) {
9180 lex_goto_eol(p);
9181 goto flush;
9182 }
9183 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9184 if ((c = nextc(p)) == -1) goto error;
9185 } while (!whole_match_p(p, eos, len, indent));
9186 str = STR_NEW3(tok(p), toklen(p), enc, func);
9187 }
9188 dispatch_heredoc_end(p);
9189 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9190 token_flush(p);
9191 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9192#ifdef RIPPER
9193 /* Preserve s_value for set_yylval_str */
9194 s_value = p->s_value;
9195#endif
9196 set_yylval_str(str);
9197#ifdef RIPPER
9198 set_parser_s_value(s_value);
9199#endif
9200
9201#ifndef RIPPER
9202 if (bol) nd_set_fl_newline(yylval.node);
9203#endif
9204 return tSTRING_CONTENT;
9205}
9206
9207#include "lex.c"
9208
9209static int
9210arg_ambiguous(struct parser_params *p, char c)
9211{
9212#ifndef RIPPER
9213 if (c == '/') {
9214 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9215 }
9216 else {
9217 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9218 }
9219#else
9220 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9221#endif
9222 return TRUE;
9223}
9224
9225/* returns true value if formal argument error;
9226 * Qtrue, or error message if ripper */
9227static VALUE
9228formal_argument_error(struct parser_params *p, ID id)
9229{
9230 switch (id_type(id)) {
9231 case ID_LOCAL:
9232 break;
9233#ifndef RIPPER
9234# define ERR(mesg) (yyerror0(mesg), Qtrue)
9235#else
9236# define ERR(mesg) WARN_S(mesg)
9237#endif
9238 case ID_CONST:
9239 return ERR("formal argument cannot be a constant");
9240 case ID_INSTANCE:
9241 return ERR("formal argument cannot be an instance variable");
9242 case ID_GLOBAL:
9243 return ERR("formal argument cannot be a global variable");
9244 case ID_CLASS:
9245 return ERR("formal argument cannot be a class variable");
9246 default:
9247 return ERR("formal argument must be local variable");
9248#undef ERR
9249 }
9250 shadowing_lvar(p, id);
9251
9252 return Qfalse;
9253}
9254
9255static int
9256lvar_defined(struct parser_params *p, ID id)
9257{
9258 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9259}
9260
9261/* emacsen -*- hack */
9262static long
9263parser_encode_length(struct parser_params *p, const char *name, long len)
9264{
9265 long nlen;
9266
9267 if (len > 5 && name[nlen = len - 5] == '-') {
9268 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9269 return nlen;
9270 }
9271 if (len > 4 && name[nlen = len - 4] == '-') {
9272 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9273 return nlen;
9274 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9275 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9276 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9277 return nlen;
9278 }
9279 return len;
9280}
9281
9282static void
9283parser_set_encode(struct parser_params *p, const char *name)
9284{
9285 rb_encoding *enc;
9286 VALUE excargs[3];
9287 int idx = 0;
9288
9289 const char *wrong = 0;
9290 switch (*name) {
9291 case 'e': case 'E': wrong = "external"; break;
9292 case 'i': case 'I': wrong = "internal"; break;
9293 case 'f': case 'F': wrong = "filesystem"; break;
9294 case 'l': case 'L': wrong = "locale"; break;
9295 }
9296 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9297 idx = rb_enc_find_index(name);
9298 if (idx < 0) {
9299 unknown:
9300 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9301 error:
9302 excargs[0] = rb_eArgError;
9303 excargs[2] = rb_make_backtrace();
9304 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9305 VALUE exc = rb_make_exception(3, excargs);
9306 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9307
9308 rb_ast_free(p->ast);
9309 p->ast = NULL;
9310
9311 rb_exc_raise(exc);
9312 }
9313 enc = rb_enc_from_index(idx);
9314 if (!rb_enc_asciicompat(enc)) {
9315 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9316 goto error;
9317 }
9318 p->enc = enc;
9319#ifndef RIPPER
9320 if (p->debug_lines) {
9321 long i;
9322 for (i = 0; i < p->debug_lines->len; i++) {
9323 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9324 }
9325 }
9326#endif
9327}
9328
9329static bool
9330comment_at_top(struct parser_params *p)
9331{
9332 if (p->token_seen) return false;
9333 return (p->line_count == (p->has_shebang ? 2 : 1));
9334}
9335
9336typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9337typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9338
9339static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9340
9341static void
9342magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9343{
9344 if (!comment_at_top(p)) {
9345 return;
9346 }
9347 parser_set_encode(p, val);
9348}
9349
9350static int
9351parser_get_bool(struct parser_params *p, const char *name, const char *val)
9352{
9353 switch (*val) {
9354 case 't': case 'T':
9355 if (STRCASECMP(val, "true") == 0) {
9356 return TRUE;
9357 }
9358 break;
9359 case 'f': case 'F':
9360 if (STRCASECMP(val, "false") == 0) {
9361 return FALSE;
9362 }
9363 break;
9364 }
9365 return parser_invalid_pragma_value(p, name, val);
9366}
9367
9368static int
9369parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9370{
9371 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9372 return -1;
9373}
9374
9375static void
9376parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9377{
9378 int b = parser_get_bool(p, name, val);
9379 if (b >= 0) p->token_info_enabled = b;
9380}
9381
9382static void
9383parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9384{
9385 int b;
9386
9387 if (p->token_seen) {
9388 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9389 return;
9390 }
9391
9392 b = parser_get_bool(p, name, val);
9393 if (b < 0) return;
9394
9395 p->frozen_string_literal = b;
9396}
9397
9398static void
9399parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9400{
9401 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9402 if (*s == ' ' || *s == '\t') continue;
9403 if (*s == '#') break;
9404 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9405 return;
9406 }
9407
9408 switch (*val) {
9409 case 'n': case 'N':
9410 if (STRCASECMP(val, "none") == 0) {
9411 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9412 return;
9413 }
9414 break;
9415 case 'l': case 'L':
9416 if (STRCASECMP(val, "literal") == 0) {
9417 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9418 return;
9419 }
9420 break;
9421 case 'e': case 'E':
9422 if (STRCASECMP(val, "experimental_copy") == 0) {
9423 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9424 return;
9425 }
9426 if (STRCASECMP(val, "experimental_everything") == 0) {
9427 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9428 return;
9429 }
9430 break;
9431 }
9432 parser_invalid_pragma_value(p, name, val);
9433}
9434
9435# if WARN_PAST_SCOPE
9436static void
9437parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9438{
9439 int b = parser_get_bool(p, name, val);
9440 if (b >= 0) p->past_scope_enabled = b;
9441}
9442# endif
9443
9444struct magic_comment {
9445 const char *name;
9446 rb_magic_comment_setter_t func;
9447 rb_magic_comment_length_t length;
9448};
9449
9450static const struct magic_comment magic_comments[] = {
9451 {"coding", magic_comment_encoding, parser_encode_length},
9452 {"encoding", magic_comment_encoding, parser_encode_length},
9453 {"frozen_string_literal", parser_set_frozen_string_literal},
9454 {"shareable_constant_value", parser_set_shareable_constant_value},
9455 {"warn_indent", parser_set_token_info},
9456# if WARN_PAST_SCOPE
9457 {"warn_past_scope", parser_set_past_scope},
9458# endif
9459};
9460
9461static const char *
9462magic_comment_marker(const char *str, long len)
9463{
9464 long i = 2;
9465
9466 while (i < len) {
9467 switch (str[i]) {
9468 case '-':
9469 if (str[i-1] == '*' && str[i-2] == '-') {
9470 return str + i + 1;
9471 }
9472 i += 2;
9473 break;
9474 case '*':
9475 if (i + 1 >= len) return 0;
9476 if (str[i+1] != '-') {
9477 i += 4;
9478 }
9479 else if (str[i-1] != '-') {
9480 i += 2;
9481 }
9482 else {
9483 return str + i + 2;
9484 }
9485 break;
9486 default:
9487 i += 3;
9488 break;
9489 }
9490 }
9491 return 0;
9492}
9493
9494static int
9495parser_magic_comment(struct parser_params *p, const char *str, long len)
9496{
9497 int indicator = 0;
9498 VALUE name = 0, val = 0;
9499 const char *beg, *end, *vbeg, *vend;
9500#define str_copy(_s, _p, _n) ((_s) \
9501 ? (void)(rb_str_resize((_s), (_n)), \
9502 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9503 : (void)((_s) = STR_NEW((_p), (_n))))
9504
9505 if (len <= 7) return FALSE;
9506 if (!!(beg = magic_comment_marker(str, len))) {
9507 if (!(end = magic_comment_marker(beg, str + len - beg)))
9508 return FALSE;
9509 indicator = TRUE;
9510 str = beg;
9511 len = end - beg - 3;
9512 }
9513
9514 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9515 while (len > 0) {
9516 const struct magic_comment *mc = magic_comments;
9517 char *s;
9518 int i;
9519 long n = 0;
9520
9521 for (; len > 0 && *str; str++, --len) {
9522 switch (*str) {
9523 case '\'': case '"': case ':': case ';':
9524 continue;
9525 }
9526 if (!ISSPACE(*str)) break;
9527 }
9528 for (beg = str; len > 0; str++, --len) {
9529 switch (*str) {
9530 case '\'': case '"': case ':': case ';':
9531 break;
9532 default:
9533 if (ISSPACE(*str)) break;
9534 continue;
9535 }
9536 break;
9537 }
9538 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9539 if (!len) break;
9540 if (*str != ':') {
9541 if (!indicator) return FALSE;
9542 continue;
9543 }
9544
9545 do str++; while (--len > 0 && ISSPACE(*str));
9546 if (!len) break;
9547 const char *tok_beg = str;
9548 if (*str == '"') {
9549 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9550 if (*str == '\\') {
9551 --len;
9552 ++str;
9553 }
9554 }
9555 vend = str;
9556 if (len) {
9557 --len;
9558 ++str;
9559 }
9560 }
9561 else {
9562 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9563 vend = str;
9564 }
9565 const char *tok_end = str;
9566 if (indicator) {
9567 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9568 }
9569 else {
9570 while (len > 0 && (ISSPACE(*str))) --len, str++;
9571 if (len) return FALSE;
9572 }
9573
9574 n = end - beg;
9575 str_copy(name, beg, n);
9576 s = RSTRING_PTR(name);
9577 for (i = 0; i < n; ++i) {
9578 if (s[i] == '-') s[i] = '_';
9579 }
9580 do {
9581 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9582 n = vend - vbeg;
9583 if (mc->length) {
9584 n = (*mc->length)(p, vbeg, n);
9585 }
9586 str_copy(val, vbeg, n);
9587 p->lex.ptok = tok_beg;
9588 p->lex.pcur = tok_end;
9589 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9590 break;
9591 }
9592 } while (++mc < magic_comments + numberof(magic_comments));
9593#ifdef RIPPER
9594 str_copy(val, vbeg, vend - vbeg);
9595 dispatch2(magic_comment, name, val);
9596#endif
9597 }
9598
9599 return TRUE;
9600}
9601
9602static void
9603set_file_encoding(struct parser_params *p, const char *str, const char *send)
9604{
9605 int sep = 0;
9606 const char *beg = str;
9607 VALUE s;
9608
9609 for (;;) {
9610 if (send - str <= 6) return;
9611 switch (str[6]) {
9612 case 'C': case 'c': str += 6; continue;
9613 case 'O': case 'o': str += 5; continue;
9614 case 'D': case 'd': str += 4; continue;
9615 case 'I': case 'i': str += 3; continue;
9616 case 'N': case 'n': str += 2; continue;
9617 case 'G': case 'g': str += 1; continue;
9618 case '=': case ':':
9619 sep = 1;
9620 str += 6;
9621 break;
9622 default:
9623 str += 6;
9624 if (ISSPACE(*str)) break;
9625 continue;
9626 }
9627 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9628 sep = 0;
9629 }
9630 for (;;) {
9631 do {
9632 if (++str >= send) return;
9633 } while (ISSPACE(*str));
9634 if (sep) break;
9635 if (*str != '=' && *str != ':') return;
9636 sep = 1;
9637 str++;
9638 }
9639 beg = str;
9640 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9641 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9642 p->lex.ptok = beg;
9643 p->lex.pcur = str;
9644 parser_set_encode(p, RSTRING_PTR(s));
9645 rb_str_resize(s, 0);
9646}
9647
9648static void
9649parser_prepare(struct parser_params *p)
9650{
9651 int c = nextc0(p, FALSE);
9652 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9653 switch (c) {
9654 case '#':
9655 if (peek(p, '!')) p->has_shebang = 1;
9656 break;
9657 case 0xef: /* UTF-8 BOM marker */
9658 if (!lex_eol_n_p(p, 2) &&
9659 (unsigned char)p->lex.pcur[0] == 0xbb &&
9660 (unsigned char)p->lex.pcur[1] == 0xbf) {
9661 p->enc = rb_utf8_encoding();
9662 p->lex.pcur += 2;
9663#ifndef RIPPER
9664 if (p->debug_lines) {
9665 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9666 }
9667#endif
9668 p->lex.pbeg = p->lex.pcur;
9669 token_flush(p);
9670 return;
9671 }
9672 break;
9673 case -1: /* end of script. */
9674 return;
9675 }
9676 pushback(p, c);
9677 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9678}
9679
9680#ifndef RIPPER
9681#define ambiguous_operator(tok, op, syn) ( \
9682 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9683 rb_warning0("even though it seems like "syn""))
9684#else
9685#define ambiguous_operator(tok, op, syn) \
9686 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9687#endif
9688#define warn_balanced(tok, op, syn) ((void) \
9689 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9690 space_seen && !ISSPACE(c) && \
9691 (ambiguous_operator(tok, op, syn), 0)), \
9692 (enum yytokentype)(tok))
9693
9694static enum yytokentype
9695no_digits(struct parser_params *p)
9696{
9697 yyerror0("numeric literal without digits");
9698 if (peek(p, '_')) nextc(p);
9699 /* dummy 0, for tUMINUS_NUM at numeric */
9700 return set_number_literal(p, tINTEGER, 0, 10, 0);
9701}
9702
9703static enum yytokentype
9704parse_numeric(struct parser_params *p, int c)
9705{
9706 int is_float, seen_point, seen_e, nondigit;
9707 int suffix;
9708
9709 is_float = seen_point = seen_e = nondigit = 0;
9710 SET_LEX_STATE(EXPR_END);
9711 newtok(p);
9712 if (c == '-' || c == '+') {
9713 tokadd(p, c);
9714 c = nextc(p);
9715 }
9716 if (c == '0') {
9717 int start = toklen(p);
9718 c = nextc(p);
9719 if (c == 'x' || c == 'X') {
9720 /* hexadecimal */
9721 c = nextc(p);
9722 if (c != -1 && ISXDIGIT(c)) {
9723 do {
9724 if (c == '_') {
9725 if (nondigit) break;
9726 nondigit = c;
9727 continue;
9728 }
9729 if (!ISXDIGIT(c)) break;
9730 nondigit = 0;
9731 tokadd(p, c);
9732 } while ((c = nextc(p)) != -1);
9733 }
9734 pushback(p, c);
9735 tokfix(p);
9736 if (toklen(p) == start) {
9737 return no_digits(p);
9738 }
9739 else if (nondigit) goto trailing_uc;
9740 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9741 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9742 }
9743 if (c == 'b' || c == 'B') {
9744 /* binary */
9745 c = nextc(p);
9746 if (c == '0' || c == '1') {
9747 do {
9748 if (c == '_') {
9749 if (nondigit) break;
9750 nondigit = c;
9751 continue;
9752 }
9753 if (c != '0' && c != '1') break;
9754 nondigit = 0;
9755 tokadd(p, c);
9756 } while ((c = nextc(p)) != -1);
9757 }
9758 pushback(p, c);
9759 tokfix(p);
9760 if (toklen(p) == start) {
9761 return no_digits(p);
9762 }
9763 else if (nondigit) goto trailing_uc;
9764 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9765 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9766 }
9767 if (c == 'd' || c == 'D') {
9768 /* decimal */
9769 c = nextc(p);
9770 if (c != -1 && ISDIGIT(c)) {
9771 do {
9772 if (c == '_') {
9773 if (nondigit) break;
9774 nondigit = c;
9775 continue;
9776 }
9777 if (!ISDIGIT(c)) break;
9778 nondigit = 0;
9779 tokadd(p, c);
9780 } while ((c = nextc(p)) != -1);
9781 }
9782 pushback(p, c);
9783 tokfix(p);
9784 if (toklen(p) == start) {
9785 return no_digits(p);
9786 }
9787 else if (nondigit) goto trailing_uc;
9788 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9789 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9790 }
9791 if (c == '_') {
9792 /* 0_0 */
9793 goto octal_number;
9794 }
9795 if (c == 'o' || c == 'O') {
9796 /* prefixed octal */
9797 c = nextc(p);
9798 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9799 tokfix(p);
9800 return no_digits(p);
9801 }
9802 }
9803 if (c >= '0' && c <= '7') {
9804 /* octal */
9805 octal_number:
9806 do {
9807 if (c == '_') {
9808 if (nondigit) break;
9809 nondigit = c;
9810 continue;
9811 }
9812 if (c < '0' || c > '9') break;
9813 if (c > '7') goto invalid_octal;
9814 nondigit = 0;
9815 tokadd(p, c);
9816 } while ((c = nextc(p)) != -1);
9817 if (toklen(p) > start) {
9818 pushback(p, c);
9819 tokfix(p);
9820 if (nondigit) goto trailing_uc;
9821 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9822 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9823 }
9824 if (nondigit) {
9825 pushback(p, c);
9826 goto trailing_uc;
9827 }
9828 }
9829 if (c > '7' && c <= '9') {
9830 invalid_octal:
9831 yyerror0("Invalid octal digit");
9832 }
9833 else if (c == '.' || c == 'e' || c == 'E') {
9834 tokadd(p, '0');
9835 }
9836 else {
9837 pushback(p, c);
9838 tokfix(p);
9839 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9840 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9841 }
9842 }
9843
9844 for (;;) {
9845 switch (c) {
9846 case '0': case '1': case '2': case '3': case '4':
9847 case '5': case '6': case '7': case '8': case '9':
9848 nondigit = 0;
9849 tokadd(p, c);
9850 break;
9851
9852 case '.':
9853 if (nondigit) goto trailing_uc;
9854 if (seen_point || seen_e) {
9855 goto decode_num;
9856 }
9857 else {
9858 int c0 = nextc(p);
9859 if (c0 == -1 || !ISDIGIT(c0)) {
9860 pushback(p, c0);
9861 goto decode_num;
9862 }
9863 c = c0;
9864 }
9865 seen_point = toklen(p);
9866 tokadd(p, '.');
9867 tokadd(p, c);
9868 is_float++;
9869 nondigit = 0;
9870 break;
9871
9872 case 'e':
9873 case 'E':
9874 if (nondigit) {
9875 pushback(p, c);
9876 c = nondigit;
9877 goto decode_num;
9878 }
9879 if (seen_e) {
9880 goto decode_num;
9881 }
9882 nondigit = c;
9883 c = nextc(p);
9884 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9885 pushback(p, c);
9886 c = nondigit;
9887 nondigit = 0;
9888 goto decode_num;
9889 }
9890 tokadd(p, nondigit);
9891 seen_e++;
9892 is_float++;
9893 tokadd(p, c);
9894 nondigit = (c == '-' || c == '+') ? c : 0;
9895 break;
9896
9897 case '_': /* `_' in number just ignored */
9898 if (nondigit) goto decode_num;
9899 nondigit = c;
9900 break;
9901
9902 default:
9903 goto decode_num;
9904 }
9905 c = nextc(p);
9906 }
9907
9908 decode_num:
9909 pushback(p, c);
9910 if (nondigit) {
9911 trailing_uc:
9912 literal_flush(p, p->lex.pcur - 1);
9913 YYLTYPE loc = RUBY_INIT_YYLLOC();
9914 compile_error(p, "trailing '%c' in number", nondigit);
9915 parser_show_error_line(p, &loc);
9916 }
9917 tokfix(p);
9918 if (is_float) {
9919 enum yytokentype type = tFLOAT;
9920
9921 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9922 if (suffix & NUM_SUFFIX_R) {
9923 type = tRATIONAL;
9924 }
9925 else {
9926 strtod(tok(p), 0);
9927 if (errno == ERANGE) {
9928 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9929 errno = 0;
9930 }
9931 }
9932 return set_number_literal(p, type, suffix, 0, seen_point);
9933 }
9934 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9935 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9936}
9937
9938static enum yytokentype
9939parse_qmark(struct parser_params *p, int space_seen)
9940{
9941 rb_encoding *enc;
9942 register int c;
9943 rb_parser_string_t *lit;
9944 const char *start = p->lex.pcur;
9945
9946 if (IS_END()) {
9947 SET_LEX_STATE(EXPR_VALUE);
9948 return '?';
9949 }
9950 c = nextc(p);
9951 if (c == -1) {
9952 compile_error(p, "incomplete character syntax");
9953 return 0;
9954 }
9955 if (rb_enc_isspace(c, p->enc)) {
9956 if (!IS_ARG()) {
9957 int c2 = escaped_control_code(c);
9958 if (c2) {
9959 WARN_SPACE_CHAR(c2, "?");
9960 }
9961 }
9962 ternary:
9963 pushback(p, c);
9964 SET_LEX_STATE(EXPR_VALUE);
9965 return '?';
9966 }
9967 newtok(p);
9968 enc = p->enc;
9969 int w = parser_precise_mbclen(p, start);
9970 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9971 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9972 if (space_seen) {
9973 const char *ptr = start;
9974 do {
9975 int n = parser_precise_mbclen(p, ptr);
9976 if (n < 0) return -1;
9977 ptr += n;
9978 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9979 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9980 " a conditional operator, put a space after '?'",
9981 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9982 }
9983 goto ternary;
9984 }
9985 else if (c == '\\') {
9986 if (peek(p, 'u')) {
9987 nextc(p);
9988 enc = rb_utf8_encoding();
9989 tokadd_utf8(p, &enc, -1, 0, 0);
9990 }
9991 else if (!ISASCII(c = peekc(p)) && c != -1) {
9992 nextc(p);
9993 if (tokadd_mbchar(p, c) == -1) return 0;
9994 }
9995 else {
9996 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9997 tokadd(p, c);
9998 }
9999 }
10000 else {
10001 if (tokadd_mbchar(p, c) == -1) return 0;
10002 }
10003 tokfix(p);
10004 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10005 set_yylval_str(lit);
10006 SET_LEX_STATE(EXPR_END);
10007 return tCHAR;
10008}
10009
10010static enum yytokentype
10011parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10012{
10013 register int c;
10014 const char *ptok = p->lex.pcur;
10015
10016 if (IS_BEG()) {
10017 int term;
10018 int paren;
10019
10020 c = nextc(p);
10021 quotation:
10022 if (c == -1) goto unterminated;
10023 if (!ISALNUM(c)) {
10024 term = c;
10025 if (!ISASCII(c)) goto unknown;
10026 c = 'Q';
10027 }
10028 else {
10029 term = nextc(p);
10030 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10031 unknown:
10032 pushback(p, term);
10033 c = parser_precise_mbclen(p, p->lex.pcur);
10034 if (c < 0) return 0;
10035 p->lex.pcur += c;
10036 yyerror0("unknown type of %string");
10037 return 0;
10038 }
10039 }
10040 if (term == -1) {
10041 unterminated:
10042 compile_error(p, "unterminated quoted string meets end of file");
10043 return 0;
10044 }
10045 paren = term;
10046 if (term == '(') term = ')';
10047 else if (term == '[') term = ']';
10048 else if (term == '{') term = '}';
10049 else if (term == '<') term = '>';
10050 else paren = 0;
10051
10052 p->lex.ptok = ptok-1;
10053 switch (c) {
10054 case 'Q':
10055 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10056 return tSTRING_BEG;
10057
10058 case 'q':
10059 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10060 return tSTRING_BEG;
10061
10062 case 'W':
10063 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10064 return tWORDS_BEG;
10065
10066 case 'w':
10067 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10068 return tQWORDS_BEG;
10069
10070 case 'I':
10071 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10072 return tSYMBOLS_BEG;
10073
10074 case 'i':
10075 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10076 return tQSYMBOLS_BEG;
10077
10078 case 'x':
10079 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10080 return tXSTRING_BEG;
10081
10082 case 'r':
10083 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10084 return tREGEXP_BEG;
10085
10086 case 's':
10087 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10088 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10089 return tSYMBEG;
10090
10091 default:
10092 yyerror0("unknown type of %string");
10093 return 0;
10094 }
10095 }
10096 if ((c = nextc(p)) == '=') {
10097 set_yylval_id('%');
10098 SET_LEX_STATE(EXPR_BEG);
10099 return tOP_ASGN;
10100 }
10101 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10102 goto quotation;
10103 }
10104 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10105 pushback(p, c);
10106 return warn_balanced('%', "%%", "string literal");
10107}
10108
10109static int
10110tokadd_ident(struct parser_params *p, int c)
10111{
10112 do {
10113 if (tokadd_mbchar(p, c) == -1) return -1;
10114 c = nextc(p);
10115 } while (parser_is_identchar(p));
10116 pushback(p, c);
10117 return 0;
10118}
10119
10120static ID
10121tokenize_ident(struct parser_params *p)
10122{
10123 ID ident = TOK_INTERN();
10124
10125 set_yylval_name(ident);
10126
10127 return ident;
10128}
10129
10130static int
10131parse_numvar(struct parser_params *p)
10132{
10133 size_t len;
10134 int overflow;
10135 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10136 const unsigned long nth_ref_max =
10137 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10138 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10139 * turned into a Fixnum, in compile.c */
10140
10141 if (overflow || n > nth_ref_max) {
10142 /* compile_error()? */
10143 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10144 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10145 }
10146 else {
10147 return (int)n;
10148 }
10149}
10150
10151static enum yytokentype
10152parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10153{
10154 const char *ptr = p->lex.pcur;
10155 register int c;
10156
10157 SET_LEX_STATE(EXPR_END);
10158 p->lex.ptok = ptr - 1; /* from '$' */
10159 newtok(p);
10160 c = nextc(p);
10161 switch (c) {
10162 case '_': /* $_: last read line string */
10163 c = nextc(p);
10164 if (parser_is_identchar(p)) {
10165 tokadd(p, '$');
10166 tokadd(p, '_');
10167 break;
10168 }
10169 pushback(p, c);
10170 c = '_';
10171 /* fall through */
10172 case '~': /* $~: match-data */
10173 case '*': /* $*: argv */
10174 case '$': /* $$: pid */
10175 case '?': /* $?: last status */
10176 case '!': /* $!: error string */
10177 case '@': /* $@: error position */
10178 case '/': /* $/: input record separator */
10179 case '\\': /* $\: output record separator */
10180 case ';': /* $;: field separator */
10181 case ',': /* $,: output field separator */
10182 case '.': /* $.: last read line number */
10183 case '=': /* $=: ignorecase */
10184 case ':': /* $:: load path */
10185 case '<': /* $<: default input handle */
10186 case '>': /* $>: default output handle */
10187 case '\"': /* $": already loaded files */
10188 tokadd(p, '$');
10189 tokadd(p, c);
10190 goto gvar;
10191
10192 case '-':
10193 tokadd(p, '$');
10194 tokadd(p, c);
10195 c = nextc(p);
10196 if (parser_is_identchar(p)) {
10197 if (tokadd_mbchar(p, c) == -1) return 0;
10198 }
10199 else {
10200 pushback(p, c);
10201 pushback(p, '-');
10202 return '$';
10203 }
10204 gvar:
10205 tokenize_ident(p);
10206 return tGVAR;
10207
10208 case '&': /* $&: last match */
10209 case '`': /* $`: string before last match */
10210 case '\'': /* $': string after last match */
10211 case '+': /* $+: string matches last paren. */
10212 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10213 tokadd(p, '$');
10214 tokadd(p, c);
10215 goto gvar;
10216 }
10217 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10218 return tBACK_REF;
10219
10220 case '1': case '2': case '3':
10221 case '4': case '5': case '6':
10222 case '7': case '8': case '9':
10223 tokadd(p, '$');
10224 do {
10225 tokadd(p, c);
10226 c = nextc(p);
10227 } while (c != -1 && ISDIGIT(c));
10228 pushback(p, c);
10229 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10230 tokfix(p);
10231 c = parse_numvar(p);
10232 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10233 return tNTH_REF;
10234
10235 default:
10236 if (!parser_is_identchar(p)) {
10237 YYLTYPE loc = RUBY_INIT_YYLLOC();
10238 if (c == -1 || ISSPACE(c)) {
10239 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10240 }
10241 else {
10242 pushback(p, c);
10243 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10244 }
10245 parser_show_error_line(p, &loc);
10246 set_yylval_noname();
10247 return tGVAR;
10248 }
10249 /* fall through */
10250 case '0':
10251 tokadd(p, '$');
10252 }
10253
10254 if (tokadd_ident(p, c)) return 0;
10255 SET_LEX_STATE(EXPR_END);
10256 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10257 tokenize_ident(p);
10258 }
10259 else {
10260 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10261 set_yylval_noname();
10262 }
10263 return tGVAR;
10264}
10265
10266static bool
10267parser_numbered_param(struct parser_params *p, int n)
10268{
10269 if (n < 0) return false;
10270
10271 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10272 return false;
10273 }
10274 if (p->max_numparam == ORDINAL_PARAM) {
10275 compile_error(p, "ordinary parameter is defined");
10276 return false;
10277 }
10278 struct vtable *args = p->lvtbl->args;
10279 if (p->max_numparam < n) {
10280 p->max_numparam = n;
10281 }
10282 while (n > args->pos) {
10283 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10284 }
10285 return true;
10286}
10287
10288static enum yytokentype
10289parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10290{
10291 const char *ptr = p->lex.pcur;
10292 enum yytokentype result = tIVAR;
10293 register int c = nextc(p);
10294 YYLTYPE loc;
10295
10296 p->lex.ptok = ptr - 1; /* from '@' */
10297 newtok(p);
10298 tokadd(p, '@');
10299 if (c == '@') {
10300 result = tCVAR;
10301 tokadd(p, '@');
10302 c = nextc(p);
10303 }
10304 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10305 if (c == -1 || !parser_is_identchar(p)) {
10306 pushback(p, c);
10307 RUBY_SET_YYLLOC(loc);
10308 if (result == tIVAR) {
10309 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10310 }
10311 else {
10312 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10313 }
10314 parser_show_error_line(p, &loc);
10315 set_yylval_noname();
10316 SET_LEX_STATE(EXPR_END);
10317 return result;
10318 }
10319 else if (ISDIGIT(c)) {
10320 pushback(p, c);
10321 RUBY_SET_YYLLOC(loc);
10322 if (result == tIVAR) {
10323 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10324 }
10325 else {
10326 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10327 }
10328 parser_show_error_line(p, &loc);
10329 set_yylval_noname();
10330 SET_LEX_STATE(EXPR_END);
10331 return result;
10332 }
10333
10334 if (tokadd_ident(p, c)) return 0;
10335 tokenize_ident(p);
10336 return result;
10337}
10338
10339static enum yytokentype
10340parse_ident(struct parser_params *p, int c, int cmd_state)
10341{
10342 enum yytokentype result;
10343 bool is_ascii = true;
10344 const enum lex_state_e last_state = p->lex.state;
10345 ID ident;
10346 int enforce_keyword_end = 0;
10347
10348 do {
10349 if (!ISASCII(c)) is_ascii = false;
10350 if (tokadd_mbchar(p, c) == -1) return 0;
10351 c = nextc(p);
10352 } while (parser_is_identchar(p));
10353 if ((c == '!' || c == '?') && !peek(p, '=')) {
10354 result = tFID;
10355 tokadd(p, c);
10356 }
10357 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10358 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10359 result = tIDENTIFIER;
10360 tokadd(p, c);
10361 }
10362 else {
10363 result = tCONSTANT; /* assume provisionally */
10364 pushback(p, c);
10365 }
10366 tokfix(p);
10367
10368 if (IS_LABEL_POSSIBLE()) {
10369 if (IS_LABEL_SUFFIX(0)) {
10370 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10371 nextc(p);
10372 tokenize_ident(p);
10373 return tLABEL;
10374 }
10375 }
10376
10377#ifndef RIPPER
10378 if (peek_end_expect_token_locations(p)) {
10379 const rb_code_position_t *end_pos;
10380 int lineno, column;
10381 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10382
10383 end_pos = peek_end_expect_token_locations(p)->pos;
10384 lineno = end_pos->lineno;
10385 column = end_pos->column;
10386
10387 if (p->debug) {
10388 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10389 p->ruby_sourceline, beg_pos, lineno, column);
10390 }
10391
10392 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10393 const struct kwtable *kw;
10394
10395 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10396 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10397 enforce_keyword_end = 1;
10398 }
10399 }
10400 }
10401#endif
10402
10403 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10404 const struct kwtable *kw;
10405
10406 /* See if it is a reserved word. */
10407 kw = rb_reserved_word(tok(p), toklen(p));
10408 if (kw) {
10409 enum lex_state_e state = p->lex.state;
10410 if (IS_lex_state_for(state, EXPR_FNAME)) {
10411 SET_LEX_STATE(EXPR_ENDFN);
10412 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10413 return kw->id[0];
10414 }
10415 SET_LEX_STATE(kw->state);
10416 if (IS_lex_state(EXPR_BEG)) {
10417 p->command_start = TRUE;
10418 }
10419 if (kw->id[0] == keyword_do) {
10420 if (lambda_beginning_p()) {
10421 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10422 return keyword_do_LAMBDA;
10423 }
10424 if (COND_P()) return keyword_do_cond;
10425 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10426 return keyword_do_block;
10427 return keyword_do;
10428 }
10429 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10430 return kw->id[0];
10431 else {
10432 if (kw->id[0] != kw->id[1])
10433 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10434 return kw->id[1];
10435 }
10436 }
10437 }
10438
10439 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10440 if (cmd_state) {
10441 SET_LEX_STATE(EXPR_CMDARG);
10442 }
10443 else {
10444 SET_LEX_STATE(EXPR_ARG);
10445 }
10446 }
10447 else if (p->lex.state == EXPR_FNAME) {
10448 SET_LEX_STATE(EXPR_ENDFN);
10449 }
10450 else {
10451 SET_LEX_STATE(EXPR_END);
10452 }
10453
10454 ident = tokenize_ident(p);
10455 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10456 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10457 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10458 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10459 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10460 }
10461 return result;
10462}
10463
10464static void
10465warn_cr(struct parser_params *p)
10466{
10467 if (!p->cr_seen) {
10468 p->cr_seen = TRUE;
10469 /* carried over with p->lex.nextline for nextc() */
10470 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10471 }
10472}
10473
10474static enum yytokentype
10475parser_yylex(struct parser_params *p)
10476{
10477 register int c;
10478 int space_seen = 0;
10479 int cmd_state;
10480 int label;
10481 enum lex_state_e last_state;
10482 int fallthru = FALSE;
10483 int token_seen = p->token_seen;
10484
10485 if (p->lex.strterm) {
10486 if (strterm_is_heredoc(p->lex.strterm)) {
10487 token_flush(p);
10488 return here_document(p, &p->lex.strterm->u.heredoc);
10489 }
10490 else {
10491 token_flush(p);
10492 return parse_string(p, &p->lex.strterm->u.literal);
10493 }
10494 }
10495 cmd_state = p->command_start;
10496 p->command_start = FALSE;
10497 p->token_seen = TRUE;
10498#ifndef RIPPER
10499 token_flush(p);
10500#endif
10501 retry:
10502 last_state = p->lex.state;
10503 switch (c = nextc(p)) {
10504 case '\0': /* NUL */
10505 case '\004': /* ^D */
10506 case '\032': /* ^Z */
10507 case -1: /* end of script. */
10508 p->eofp = 1;
10509#ifndef RIPPER
10510 if (p->end_expect_token_locations) {
10511 pop_end_expect_token_locations(p);
10512 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10513 return tDUMNY_END;
10514 }
10515#endif
10516 /* Set location for end-of-input because dispatch_scan_event is not called. */
10517 RUBY_SET_YYLLOC(*p->yylloc);
10518 return END_OF_INPUT;
10519
10520 /* white spaces */
10521 case '\r':
10522 warn_cr(p);
10523 /* fall through */
10524 case ' ': case '\t': case '\f':
10525 case '\13': /* '\v' */
10526 space_seen = 1;
10527 while ((c = nextc(p))) {
10528 switch (c) {
10529 case '\r':
10530 warn_cr(p);
10531 /* fall through */
10532 case ' ': case '\t': case '\f':
10533 case '\13': /* '\v' */
10534 break;
10535 default:
10536 goto outofloop;
10537 }
10538 }
10539 outofloop:
10540 pushback(p, c);
10541 dispatch_scan_event(p, tSP);
10542#ifndef RIPPER
10543 token_flush(p);
10544#endif
10545 goto retry;
10546
10547 case '#': /* it's a comment */
10548 p->token_seen = token_seen;
10549 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10550 /* no magic_comment in shebang line */
10551 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10552 if (comment_at_top(p)) {
10553 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10554 }
10555 }
10556 p->lex.pcur = pcur, p->lex.ptok = ptok;
10557 lex_goto_eol(p);
10558 dispatch_scan_event(p, tCOMMENT);
10559 fallthru = TRUE;
10560 /* fall through */
10561 case '\n':
10562 p->token_seen = token_seen;
10563 rb_parser_string_t *prevline = p->lex.lastline;
10564 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10565 !IS_lex_state(EXPR_LABELED));
10566 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10567 if (!fallthru) {
10568 dispatch_scan_event(p, tIGNORED_NL);
10569 }
10570 fallthru = FALSE;
10571 if (!c && p->ctxt.in_kwarg) {
10572 goto normal_newline;
10573 }
10574 goto retry;
10575 }
10576 while (1) {
10577 switch (c = nextc(p)) {
10578 case ' ': case '\t': case '\f': case '\r':
10579 case '\13': /* '\v' */
10580 space_seen = 1;
10581 break;
10582 case '#':
10583 pushback(p, c);
10584 if (space_seen) {
10585 dispatch_scan_event(p, tSP);
10586 token_flush(p);
10587 }
10588 goto retry;
10589 case 'a':
10590 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10591 goto bol;
10592 case 'o':
10593 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10594 goto bol;
10595 case '|':
10596 if (peek(p, '|')) goto leading_logical;
10597 goto bol;
10598 case '&':
10599 if (peek(p, '&')) {
10600 leading_logical:
10601 pushback(p, c);
10602 dispatch_delayed_token(p, tIGNORED_NL);
10603 cmd_state = FALSE;
10604 goto retry;
10605 }
10606 /* fall through */
10607 case '.': {
10608 dispatch_delayed_token(p, tIGNORED_NL);
10609 if (peek(p, '.') == (c == '&')) {
10610 pushback(p, c);
10611 dispatch_scan_event(p, tSP);
10612 goto retry;
10613 }
10614 }
10615 bol:
10616 default:
10617 p->ruby_sourceline--;
10618 p->lex.nextline = p->lex.lastline;
10619 set_lastline(p, prevline);
10620 case -1: /* EOF no decrement*/
10621 if (c == -1 && space_seen) {
10622 dispatch_scan_event(p, tSP);
10623 }
10624 lex_goto_eol(p);
10625 if (c != -1) {
10626 token_flush(p);
10627 RUBY_SET_YYLLOC(*p->yylloc);
10628 }
10629 goto normal_newline;
10630 }
10631 }
10632 normal_newline:
10633 p->command_start = TRUE;
10634 SET_LEX_STATE(EXPR_BEG);
10635 return '\n';
10636
10637 case '*':
10638 if ((c = nextc(p)) == '*') {
10639 if ((c = nextc(p)) == '=') {
10640 set_yylval_id(idPow);
10641 SET_LEX_STATE(EXPR_BEG);
10642 return tOP_ASGN;
10643 }
10644 pushback(p, c);
10645 if (IS_SPCARG(c)) {
10646 rb_warning0("'**' interpreted as argument prefix");
10647 c = tDSTAR;
10648 }
10649 else if (IS_BEG()) {
10650 c = tDSTAR;
10651 }
10652 else {
10653 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10654 }
10655 }
10656 else {
10657 if (c == '=') {
10658 set_yylval_id('*');
10659 SET_LEX_STATE(EXPR_BEG);
10660 return tOP_ASGN;
10661 }
10662 pushback(p, c);
10663 if (IS_SPCARG(c)) {
10664 rb_warning0("'*' interpreted as argument prefix");
10665 c = tSTAR;
10666 }
10667 else if (IS_BEG()) {
10668 c = tSTAR;
10669 }
10670 else {
10671 c = warn_balanced('*', "*", "argument prefix");
10672 }
10673 }
10674 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10675 return c;
10676
10677 case '!':
10678 c = nextc(p);
10679 if (IS_AFTER_OPERATOR()) {
10680 SET_LEX_STATE(EXPR_ARG);
10681 if (c == '@') {
10682 return '!';
10683 }
10684 }
10685 else {
10686 SET_LEX_STATE(EXPR_BEG);
10687 }
10688 if (c == '=') {
10689 return tNEQ;
10690 }
10691 if (c == '~') {
10692 return tNMATCH;
10693 }
10694 pushback(p, c);
10695 return '!';
10696
10697 case '=':
10698 if (was_bol(p)) {
10699 /* skip embedded rd document */
10700 if (word_match_p(p, "begin", 5)) {
10701 int first_p = TRUE;
10702
10703 lex_goto_eol(p);
10704 dispatch_scan_event(p, tEMBDOC_BEG);
10705 for (;;) {
10706 lex_goto_eol(p);
10707 if (!first_p) {
10708 dispatch_scan_event(p, tEMBDOC);
10709 }
10710 first_p = FALSE;
10711 c = nextc(p);
10712 if (c == -1) {
10713 compile_error(p, "embedded document meets end of file");
10714 return END_OF_INPUT;
10715 }
10716 if (c == '=' && word_match_p(p, "end", 3)) {
10717 break;
10718 }
10719 pushback(p, c);
10720 }
10721 lex_goto_eol(p);
10722 dispatch_scan_event(p, tEMBDOC_END);
10723 goto retry;
10724 }
10725 }
10726
10727 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10728 if ((c = nextc(p)) == '=') {
10729 if ((c = nextc(p)) == '=') {
10730 return tEQQ;
10731 }
10732 pushback(p, c);
10733 return tEQ;
10734 }
10735 if (c == '~') {
10736 return tMATCH;
10737 }
10738 else if (c == '>') {
10739 return tASSOC;
10740 }
10741 pushback(p, c);
10742 return '=';
10743
10744 case '<':
10745 c = nextc(p);
10746 if (c == '<' &&
10747 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10748 !IS_END() &&
10749 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10750 enum yytokentype token = heredoc_identifier(p);
10751 if (token) return token < 0 ? 0 : token;
10752 }
10753 if (IS_AFTER_OPERATOR()) {
10754 SET_LEX_STATE(EXPR_ARG);
10755 }
10756 else {
10757 if (IS_lex_state(EXPR_CLASS))
10758 p->command_start = TRUE;
10759 SET_LEX_STATE(EXPR_BEG);
10760 }
10761 if (c == '=') {
10762 if ((c = nextc(p)) == '>') {
10763 return tCMP;
10764 }
10765 pushback(p, c);
10766 return tLEQ;
10767 }
10768 if (c == '<') {
10769 if ((c = nextc(p)) == '=') {
10770 set_yylval_id(idLTLT);
10771 SET_LEX_STATE(EXPR_BEG);
10772 return tOP_ASGN;
10773 }
10774 pushback(p, c);
10775 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10776 }
10777 pushback(p, c);
10778 return '<';
10779
10780 case '>':
10781 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10782 if ((c = nextc(p)) == '=') {
10783 return tGEQ;
10784 }
10785 if (c == '>') {
10786 if ((c = nextc(p)) == '=') {
10787 set_yylval_id(idGTGT);
10788 SET_LEX_STATE(EXPR_BEG);
10789 return tOP_ASGN;
10790 }
10791 pushback(p, c);
10792 return tRSHFT;
10793 }
10794 pushback(p, c);
10795 return '>';
10796
10797 case '"':
10798 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10799 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10800 p->lex.ptok = p->lex.pcur-1;
10801 return tSTRING_BEG;
10802
10803 case '`':
10804 if (IS_lex_state(EXPR_FNAME)) {
10805 SET_LEX_STATE(EXPR_ENDFN);
10806 return c;
10807 }
10808 if (IS_lex_state(EXPR_DOT)) {
10809 if (cmd_state)
10810 SET_LEX_STATE(EXPR_CMDARG);
10811 else
10812 SET_LEX_STATE(EXPR_ARG);
10813 return c;
10814 }
10815 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10816 return tXSTRING_BEG;
10817
10818 case '\'':
10819 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10820 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10821 p->lex.ptok = p->lex.pcur-1;
10822 return tSTRING_BEG;
10823
10824 case '?':
10825 return parse_qmark(p, space_seen);
10826
10827 case '&':
10828 if ((c = nextc(p)) == '&') {
10829 SET_LEX_STATE(EXPR_BEG);
10830 if ((c = nextc(p)) == '=') {
10831 set_yylval_id(idANDOP);
10832 SET_LEX_STATE(EXPR_BEG);
10833 return tOP_ASGN;
10834 }
10835 pushback(p, c);
10836 return tANDOP;
10837 }
10838 else if (c == '=') {
10839 set_yylval_id('&');
10840 SET_LEX_STATE(EXPR_BEG);
10841 return tOP_ASGN;
10842 }
10843 else if (c == '.') {
10844 set_yylval_id(idANDDOT);
10845 SET_LEX_STATE(EXPR_DOT);
10846 return tANDDOT;
10847 }
10848 pushback(p, c);
10849 if (IS_SPCARG(c)) {
10850 if ((c != ':') ||
10851 (c = peekc_n(p, 1)) == -1 ||
10852 !(c == '\'' || c == '"' ||
10853 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10854 rb_warning0("'&' interpreted as argument prefix");
10855 }
10856 c = tAMPER;
10857 }
10858 else if (IS_BEG()) {
10859 c = tAMPER;
10860 }
10861 else {
10862 c = warn_balanced('&', "&", "argument prefix");
10863 }
10864 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10865 return c;
10866
10867 case '|':
10868 if ((c = nextc(p)) == '|') {
10869 SET_LEX_STATE(EXPR_BEG);
10870 if ((c = nextc(p)) == '=') {
10871 set_yylval_id(idOROP);
10872 SET_LEX_STATE(EXPR_BEG);
10873 return tOP_ASGN;
10874 }
10875 pushback(p, c);
10876 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10877 c = '|';
10878 pushback(p, '|');
10879 return c;
10880 }
10881 return tOROP;
10882 }
10883 if (c == '=') {
10884 set_yylval_id('|');
10885 SET_LEX_STATE(EXPR_BEG);
10886 return tOP_ASGN;
10887 }
10888 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10889 pushback(p, c);
10890 return '|';
10891
10892 case '+':
10893 c = nextc(p);
10894 if (IS_AFTER_OPERATOR()) {
10895 SET_LEX_STATE(EXPR_ARG);
10896 if (c == '@') {
10897 return tUPLUS;
10898 }
10899 pushback(p, c);
10900 return '+';
10901 }
10902 if (c == '=') {
10903 set_yylval_id('+');
10904 SET_LEX_STATE(EXPR_BEG);
10905 return tOP_ASGN;
10906 }
10907 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10908 SET_LEX_STATE(EXPR_BEG);
10909 pushback(p, c);
10910 if (c != -1 && ISDIGIT(c)) {
10911 return parse_numeric(p, '+');
10912 }
10913 return tUPLUS;
10914 }
10915 SET_LEX_STATE(EXPR_BEG);
10916 pushback(p, c);
10917 return warn_balanced('+', "+", "unary operator");
10918
10919 case '-':
10920 c = nextc(p);
10921 if (IS_AFTER_OPERATOR()) {
10922 SET_LEX_STATE(EXPR_ARG);
10923 if (c == '@') {
10924 return tUMINUS;
10925 }
10926 pushback(p, c);
10927 return '-';
10928 }
10929 if (c == '=') {
10930 set_yylval_id('-');
10931 SET_LEX_STATE(EXPR_BEG);
10932 return tOP_ASGN;
10933 }
10934 if (c == '>') {
10935 SET_LEX_STATE(EXPR_ENDFN);
10936 yylval.num = p->lex.lpar_beg;
10937 p->lex.lpar_beg = p->lex.paren_nest;
10938 return tLAMBDA;
10939 }
10940 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10941 SET_LEX_STATE(EXPR_BEG);
10942 pushback(p, c);
10943 if (c != -1 && ISDIGIT(c)) {
10944 return tUMINUS_NUM;
10945 }
10946 return tUMINUS;
10947 }
10948 SET_LEX_STATE(EXPR_BEG);
10949 pushback(p, c);
10950 return warn_balanced('-', "-", "unary operator");
10951
10952 case '.': {
10953 int is_beg = IS_BEG();
10954 SET_LEX_STATE(EXPR_BEG);
10955 if ((c = nextc(p)) == '.') {
10956 if ((c = nextc(p)) == '.') {
10957 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10958 SET_LEX_STATE(EXPR_ENDARG);
10959 return tBDOT3;
10960 }
10961 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10962 rb_warn0("... at EOL, should be parenthesized?");
10963 }
10964 return is_beg ? tBDOT3 : tDOT3;
10965 }
10966 pushback(p, c);
10967 return is_beg ? tBDOT2 : tDOT2;
10968 }
10969 pushback(p, c);
10970 if (c != -1 && ISDIGIT(c)) {
10971 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10972 parse_numeric(p, '.');
10973 if (ISDIGIT(prev)) {
10974 yyerror0("unexpected fraction part after numeric literal");
10975 }
10976 else {
10977 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10978 }
10979 SET_LEX_STATE(EXPR_END);
10980 p->lex.ptok = p->lex.pcur;
10981 goto retry;
10982 }
10983 set_yylval_id('.');
10984 SET_LEX_STATE(EXPR_DOT);
10985 return '.';
10986 }
10987
10988 case '0': case '1': case '2': case '3': case '4':
10989 case '5': case '6': case '7': case '8': case '9':
10990 return parse_numeric(p, c);
10991
10992 case ')':
10993 COND_POP();
10994 CMDARG_POP();
10995 SET_LEX_STATE(EXPR_ENDFN);
10996 p->lex.paren_nest--;
10997 return c;
10998
10999 case ']':
11000 COND_POP();
11001 CMDARG_POP();
11002 SET_LEX_STATE(EXPR_END);
11003 p->lex.paren_nest--;
11004 return c;
11005
11006 case '}':
11007 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11008 if (!p->lex.brace_nest--) return tSTRING_DEND;
11009 COND_POP();
11010 CMDARG_POP();
11011 SET_LEX_STATE(EXPR_END);
11012 p->lex.paren_nest--;
11013 return c;
11014
11015 case ':':
11016 c = nextc(p);
11017 if (c == ':') {
11018 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11019 SET_LEX_STATE(EXPR_BEG);
11020 return tCOLON3;
11021 }
11022 set_yylval_id(idCOLON2);
11023 SET_LEX_STATE(EXPR_DOT);
11024 return tCOLON2;
11025 }
11026 if (IS_END() || ISSPACE(c) || c == '#') {
11027 pushback(p, c);
11028 c = warn_balanced(':', ":", "symbol literal");
11029 SET_LEX_STATE(EXPR_BEG);
11030 return c;
11031 }
11032 switch (c) {
11033 case '\'':
11034 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11035 break;
11036 case '"':
11037 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11038 break;
11039 default:
11040 pushback(p, c);
11041 break;
11042 }
11043 SET_LEX_STATE(EXPR_FNAME);
11044 return tSYMBEG;
11045
11046 case '/':
11047 if (IS_BEG()) {
11048 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11049 return tREGEXP_BEG;
11050 }
11051 if ((c = nextc(p)) == '=') {
11052 set_yylval_id('/');
11053 SET_LEX_STATE(EXPR_BEG);
11054 return tOP_ASGN;
11055 }
11056 pushback(p, c);
11057 if (IS_SPCARG(c)) {
11058 arg_ambiguous(p, '/');
11059 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11060 return tREGEXP_BEG;
11061 }
11062 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11063 return warn_balanced('/', "/", "regexp literal");
11064
11065 case '^':
11066 if ((c = nextc(p)) == '=') {
11067 set_yylval_id('^');
11068 SET_LEX_STATE(EXPR_BEG);
11069 return tOP_ASGN;
11070 }
11071 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11072 pushback(p, c);
11073 return '^';
11074
11075 case ';':
11076 SET_LEX_STATE(EXPR_BEG);
11077 p->command_start = TRUE;
11078 return ';';
11079
11080 case ',':
11081 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11082 return ',';
11083
11084 case '~':
11085 if (IS_AFTER_OPERATOR()) {
11086 if ((c = nextc(p)) != '@') {
11087 pushback(p, c);
11088 }
11089 SET_LEX_STATE(EXPR_ARG);
11090 }
11091 else {
11092 SET_LEX_STATE(EXPR_BEG);
11093 }
11094 return '~';
11095
11096 case '(':
11097 if (IS_BEG()) {
11098 c = tLPAREN;
11099 }
11100 else if (!space_seen) {
11101 /* foo( ... ) => method call, no ambiguity */
11102 }
11103 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11104 c = tLPAREN_ARG;
11105 }
11106 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11107 rb_warning0("parentheses after method name is interpreted as "
11108 "an argument list, not a decomposed argument");
11109 }
11110 p->lex.paren_nest++;
11111 COND_PUSH(0);
11112 CMDARG_PUSH(0);
11113 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11114 return c;
11115
11116 case '[':
11117 p->lex.paren_nest++;
11118 if (IS_AFTER_OPERATOR()) {
11119 if ((c = nextc(p)) == ']') {
11120 p->lex.paren_nest--;
11121 SET_LEX_STATE(EXPR_ARG);
11122 if ((c = nextc(p)) == '=') {
11123 return tASET;
11124 }
11125 pushback(p, c);
11126 return tAREF;
11127 }
11128 pushback(p, c);
11129 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11130 return '[';
11131 }
11132 else if (IS_BEG()) {
11133 c = tLBRACK;
11134 }
11135 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11136 c = tLBRACK;
11137 }
11138 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11139 COND_PUSH(0);
11140 CMDARG_PUSH(0);
11141 return c;
11142
11143 case '{':
11144 ++p->lex.brace_nest;
11145 if (lambda_beginning_p())
11146 c = tLAMBEG;
11147 else if (IS_lex_state(EXPR_LABELED))
11148 c = tLBRACE; /* hash */
11149 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11150 c = '{'; /* block (primary) */
11151 else if (IS_lex_state(EXPR_ENDARG))
11152 c = tLBRACE_ARG; /* block (expr) */
11153 else
11154 c = tLBRACE; /* hash */
11155 if (c != tLBRACE) {
11156 p->command_start = TRUE;
11157 SET_LEX_STATE(EXPR_BEG);
11158 }
11159 else {
11160 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11161 }
11162 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11163 COND_PUSH(0);
11164 CMDARG_PUSH(0);
11165 return c;
11166
11167 case '\\':
11168 c = nextc(p);
11169 if (c == '\n') {
11170 space_seen = 1;
11171 dispatch_scan_event(p, tSP);
11172 goto retry; /* skip \\n */
11173 }
11174 if (c == ' ') return tSP;
11175 if (ISSPACE(c)) return c;
11176 pushback(p, c);
11177 return '\\';
11178
11179 case '%':
11180 return parse_percent(p, space_seen, last_state);
11181
11182 case '$':
11183 return parse_gvar(p, last_state);
11184
11185 case '@':
11186 return parse_atmark(p, last_state);
11187
11188 case '_':
11189 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11190 p->ruby__end__seen = 1;
11191 p->eofp = 1;
11192#ifdef RIPPER
11193 lex_goto_eol(p);
11194 dispatch_scan_event(p, k__END__);
11195#endif
11196 return END_OF_INPUT;
11197 }
11198 newtok(p);
11199 break;
11200
11201 default:
11202 if (!parser_is_identchar(p)) {
11203 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11204 token_flush(p);
11205 goto retry;
11206 }
11207
11208 newtok(p);
11209 break;
11210 }
11211
11212 return parse_ident(p, c, cmd_state);
11213}
11214
11215static enum yytokentype
11216yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11217{
11218 enum yytokentype t;
11219
11220 p->lval = lval;
11221 lval->node = 0;
11222 p->yylloc = yylloc;
11223
11224 t = parser_yylex(p);
11225
11226 if (has_delayed_token(p))
11227 dispatch_delayed_token(p, t);
11228 else if (t != END_OF_INPUT)
11229 dispatch_scan_event(p, t);
11230
11231 return t;
11232}
11233
11234#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11235
11236static NODE*
11237node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11238{
11239 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11240
11241 rb_node_init(n, type);
11242 return n;
11243}
11244
11245static NODE *
11246nd_set_loc(NODE *nd, const YYLTYPE *loc)
11247{
11248 nd->nd_loc = *loc;
11249 nd_set_line(nd, loc->beg_pos.lineno);
11250 return nd;
11251}
11252
11253static NODE*
11254node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11255{
11256 NODE *n = node_new_internal(p, type, size, alignment);
11257
11258 nd_set_loc(n, loc);
11259 nd_set_node_id(n, parser_get_node_id(p));
11260 return n;
11261}
11262
11263#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11264
11265static rb_node_scope_t *
11266rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11267{
11268 rb_ast_id_table_t *nd_tbl;
11269 nd_tbl = local_tbl(p);
11270 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11271 n->nd_tbl = nd_tbl;
11272 n->nd_body = nd_body;
11273 n->nd_parent = nd_parent;
11274 n->nd_args = nd_args;
11275
11276 return n;
11277}
11278
11279static rb_node_scope_t *
11280rb_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)
11281{
11282 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11283 n->nd_tbl = nd_tbl;
11284 n->nd_body = nd_body;
11285 n->nd_parent = nd_parent;
11286 n->nd_args = nd_args;
11287
11288 return n;
11289}
11290
11291static rb_node_defn_t *
11292rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11293{
11294 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11295 n->nd_mid = nd_mid;
11296 n->nd_defn = nd_defn;
11297
11298 return n;
11299}
11300
11301static rb_node_defs_t *
11302rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11303{
11304 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11305 n->nd_recv = nd_recv;
11306 n->nd_mid = nd_mid;
11307 n->nd_defn = nd_defn;
11308
11309 return n;
11310}
11311
11312static rb_node_block_t *
11313rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11314{
11315 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11316 n->nd_head = nd_head;
11317 n->nd_end = (NODE *)n;
11318 n->nd_next = 0;
11319
11320 return n;
11321}
11322
11323static rb_node_for_t *
11324rb_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)
11325{
11326 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11327 n->nd_body = nd_body;
11328 n->nd_iter = nd_iter;
11329 n->for_keyword_loc = *for_keyword_loc;
11330 n->in_keyword_loc = *in_keyword_loc;
11331 n->do_keyword_loc = *do_keyword_loc;
11332 n->end_keyword_loc = *end_keyword_loc;
11333
11334 return n;
11335}
11336
11337static rb_node_for_masgn_t *
11338rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11339{
11340 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11341 n->nd_var = nd_var;
11342
11343 return n;
11344}
11345
11346static rb_node_retry_t *
11347rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11348{
11349 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11350
11351 return n;
11352}
11353
11354static rb_node_begin_t *
11355rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11356{
11357 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11358 n->nd_body = nd_body;
11359
11360 return n;
11361}
11362
11363static rb_node_rescue_t *
11364rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11365{
11366 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11367 n->nd_head = nd_head;
11368 n->nd_resq = nd_resq;
11369 n->nd_else = nd_else;
11370
11371 return n;
11372}
11373
11374static rb_node_resbody_t *
11375rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11376{
11377 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11378 n->nd_args = nd_args;
11379 n->nd_exc_var = nd_exc_var;
11380 n->nd_body = nd_body;
11381 n->nd_next = nd_next;
11382
11383 return n;
11384}
11385
11386static rb_node_ensure_t *
11387rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11388{
11389 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11390 n->nd_head = nd_head;
11391 n->nd_ensr = nd_ensr;
11392
11393 return n;
11394}
11395
11396static rb_node_and_t *
11397rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11398{
11399 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11400 n->nd_1st = nd_1st;
11401 n->nd_2nd = nd_2nd;
11402 n->operator_loc = *operator_loc;
11403
11404 return n;
11405}
11406
11407static rb_node_or_t *
11408rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11409{
11410 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11411 n->nd_1st = nd_1st;
11412 n->nd_2nd = nd_2nd;
11413 n->operator_loc = *operator_loc;
11414
11415 return n;
11416}
11417
11418static rb_node_return_t *
11419rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11420{
11421 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11422 n->nd_stts = nd_stts;
11423 n->keyword_loc = *keyword_loc;
11424 return n;
11425}
11426
11427static rb_node_yield_t *
11428rb_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)
11429{
11430 if (nd_head) no_blockarg(p, nd_head);
11431
11432 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11433 n->nd_head = nd_head;
11434 n->keyword_loc = *keyword_loc;
11435 n->lparen_loc = *lparen_loc;
11436 n->rparen_loc = *rparen_loc;
11437
11438 return n;
11439}
11440
11441static rb_node_if_t *
11442rb_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)
11443{
11444 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11445 n->nd_cond = nd_cond;
11446 n->nd_body = nd_body;
11447 n->nd_else = nd_else;
11448 n->if_keyword_loc = *if_keyword_loc;
11449 n->then_keyword_loc = *then_keyword_loc;
11450 n->end_keyword_loc = *end_keyword_loc;
11451
11452 return n;
11453}
11454
11455static rb_node_unless_t *
11456rb_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)
11457{
11458 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11459 n->nd_cond = nd_cond;
11460 n->nd_body = nd_body;
11461 n->nd_else = nd_else;
11462 n->keyword_loc = *keyword_loc;
11463 n->then_keyword_loc = *then_keyword_loc;
11464 n->end_keyword_loc = *end_keyword_loc;
11465
11466 return n;
11467}
11468
11469static rb_node_class_t *
11470rb_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)
11471{
11472 /* Keep the order of node creation */
11473 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11474 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11475 RNODE_SCOPE(scope)->nd_parent = &n->node;
11476 n->nd_cpath = nd_cpath;
11477 n->nd_body = scope;
11478 n->nd_super = nd_super;
11479 n->class_keyword_loc = *class_keyword_loc;
11480 n->inheritance_operator_loc = *inheritance_operator_loc;
11481 n->end_keyword_loc = *end_keyword_loc;
11482
11483 return n;
11484}
11485
11486static rb_node_sclass_t *
11487rb_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)
11488{
11489 /* Keep the order of node creation */
11490 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11491 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11492 RNODE_SCOPE(scope)->nd_parent = &n->node;
11493 n->nd_recv = nd_recv;
11494 n->nd_body = scope;
11495 n->class_keyword_loc = *class_keyword_loc;
11496 n->operator_loc = *operator_loc;
11497 n->end_keyword_loc = *end_keyword_loc;
11498
11499 return n;
11500}
11501
11502static rb_node_module_t *
11503rb_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)
11504{
11505 /* Keep the order of node creation */
11506 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11507 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11508 RNODE_SCOPE(scope)->nd_parent = &n->node;
11509 n->nd_cpath = nd_cpath;
11510 n->nd_body = scope;
11511 n->module_keyword_loc = *module_keyword_loc;
11512 n->end_keyword_loc = *end_keyword_loc;
11513
11514 return n;
11515}
11516
11517static rb_node_iter_t *
11518rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11519{
11520 /* Keep the order of node creation */
11521 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11522 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11523 RNODE_SCOPE(scope)->nd_parent = &n->node;
11524 n->nd_body = scope;
11525 n->nd_iter = 0;
11526
11527 return n;
11528}
11529
11530static rb_node_lambda_t *
11531rb_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)
11532{
11533 /* Keep the order of node creation */
11534 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11535 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11536 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11537 RNODE_SCOPE(scope)->nd_parent = &n->node;
11538 n->nd_body = scope;
11539 n->operator_loc = *operator_loc;
11540 n->opening_loc = *opening_loc;
11541 n->closing_loc = *closing_loc;
11542
11543 return n;
11544}
11545
11546static rb_node_case_t *
11547rb_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)
11548{
11549 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11550 n->nd_head = nd_head;
11551 n->nd_body = nd_body;
11552 n->case_keyword_loc = *case_keyword_loc;
11553 n->end_keyword_loc = *end_keyword_loc;
11554
11555 return n;
11556}
11557
11558static rb_node_case2_t *
11559rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11560{
11561 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11562 n->nd_head = 0;
11563 n->nd_body = nd_body;
11564 n->case_keyword_loc = *case_keyword_loc;
11565 n->end_keyword_loc = *end_keyword_loc;
11566
11567 return n;
11568}
11569
11570static rb_node_case3_t *
11571rb_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)
11572{
11573 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11574 n->nd_head = nd_head;
11575 n->nd_body = nd_body;
11576 n->case_keyword_loc = *case_keyword_loc;
11577 n->end_keyword_loc = *end_keyword_loc;
11578
11579 return n;
11580}
11581
11582static rb_node_when_t *
11583rb_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)
11584{
11585 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11586 n->nd_head = nd_head;
11587 n->nd_body = nd_body;
11588 n->nd_next = nd_next;
11589 n->keyword_loc = *keyword_loc;
11590 n->then_keyword_loc = *then_keyword_loc;
11591
11592 return n;
11593}
11594
11595static rb_node_in_t *
11596rb_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)
11597{
11598 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11599 n->nd_head = nd_head;
11600 n->nd_body = nd_body;
11601 n->nd_next = nd_next;
11602 n->in_keyword_loc = *in_keyword_loc;
11603 n->then_keyword_loc = *then_keyword_loc;
11604 n->operator_loc = *operator_loc;
11605
11606 return n;
11607}
11608
11609static rb_node_while_t *
11610rb_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)
11611{
11612 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11613 n->nd_cond = nd_cond;
11614 n->nd_body = nd_body;
11615 n->nd_state = nd_state;
11616 n->keyword_loc = *keyword_loc;
11617 n->closing_loc = *closing_loc;
11618
11619 return n;
11620}
11621
11622static rb_node_until_t *
11623rb_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)
11624{
11625 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11626 n->nd_cond = nd_cond;
11627 n->nd_body = nd_body;
11628 n->nd_state = nd_state;
11629 n->keyword_loc = *keyword_loc;
11630 n->closing_loc = *closing_loc;
11631
11632 return n;
11633}
11634
11635static rb_node_colon2_t *
11636rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11637{
11638 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11639 n->nd_head = nd_head;
11640 n->nd_mid = nd_mid;
11641 n->delimiter_loc = *delimiter_loc;
11642 n->name_loc = *name_loc;
11643
11644 return n;
11645}
11646
11647static rb_node_colon3_t *
11648rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11649{
11650 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11651 n->nd_mid = nd_mid;
11652 n->delimiter_loc = *delimiter_loc;
11653 n->name_loc = *name_loc;
11654
11655 return n;
11656}
11657
11658static rb_node_dot2_t *
11659rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11660{
11661 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11662 n->nd_beg = nd_beg;
11663 n->nd_end = nd_end;
11664 n->operator_loc = *operator_loc;
11665
11666 return n;
11667}
11668
11669static rb_node_dot3_t *
11670rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11671{
11672 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11673 n->nd_beg = nd_beg;
11674 n->nd_end = nd_end;
11675 n->operator_loc = *operator_loc;
11676
11677 return n;
11678}
11679
11680static rb_node_self_t *
11681rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11682{
11683 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11684 n->nd_state = 1;
11685
11686 return n;
11687}
11688
11689static rb_node_nil_t *
11690rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11691{
11692 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11693
11694 return n;
11695}
11696
11697static rb_node_true_t *
11698rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11699{
11700 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11701
11702 return n;
11703}
11704
11705static rb_node_false_t *
11706rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11707{
11708 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11709
11710 return n;
11711}
11712
11713static rb_node_super_t *
11714rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11715 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11716{
11717 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11718 n->nd_args = nd_args;
11719 n->keyword_loc = *keyword_loc;
11720 n->lparen_loc = *lparen_loc;
11721 n->rparen_loc = *rparen_loc;
11722
11723 return n;
11724}
11725
11726static rb_node_zsuper_t *
11727rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11728{
11729 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11730
11731 return n;
11732}
11733
11734static rb_node_match2_t *
11735rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11736{
11737 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11738 n->nd_recv = nd_recv;
11739 n->nd_value = nd_value;
11740 n->nd_args = 0;
11741
11742 return n;
11743}
11744
11745static rb_node_match3_t *
11746rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11747{
11748 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11749 n->nd_recv = nd_recv;
11750 n->nd_value = nd_value;
11751
11752 return n;
11753}
11754
11755/* TODO: Use union for NODE_LIST2 */
11756static rb_node_list_t *
11757rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11758{
11759 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11760 n->nd_head = nd_head;
11761 n->as.nd_alen = 1;
11762 n->nd_next = 0;
11763
11764 return n;
11765}
11766
11767static rb_node_list_t *
11768rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11769{
11770 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11771 n->nd_head = nd_head;
11772 n->as.nd_alen = nd_alen;
11773 n->nd_next = nd_next;
11774
11775 return n;
11776}
11777
11778static rb_node_zlist_t *
11779rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11780{
11781 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11782
11783 return n;
11784}
11785
11786static rb_node_hash_t *
11787rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11788{
11789 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11790 n->nd_head = nd_head;
11791 n->nd_brace = 0;
11792
11793 return n;
11794}
11795
11796static rb_node_masgn_t *
11797rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11798{
11799 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11800 n->nd_head = nd_head;
11801 n->nd_value = 0;
11802 n->nd_args = nd_args;
11803
11804 return n;
11805}
11806
11807static rb_node_gasgn_t *
11808rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11809{
11810 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11811 n->nd_vid = nd_vid;
11812 n->nd_value = nd_value;
11813
11814 return n;
11815}
11816
11817static rb_node_lasgn_t *
11818rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11819{
11820 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11821 n->nd_vid = nd_vid;
11822 n->nd_value = nd_value;
11823
11824 return n;
11825}
11826
11827static rb_node_dasgn_t *
11828rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11829{
11830 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11831 n->nd_vid = nd_vid;
11832 n->nd_value = nd_value;
11833
11834 return n;
11835}
11836
11837static rb_node_iasgn_t *
11838rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11839{
11840 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11841 n->nd_vid = nd_vid;
11842 n->nd_value = nd_value;
11843
11844 return n;
11845}
11846
11847static rb_node_cvasgn_t *
11848rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11849{
11850 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11851 n->nd_vid = nd_vid;
11852 n->nd_value = nd_value;
11853
11854 return n;
11855}
11856
11857static rb_node_op_asgn1_t *
11858rb_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)
11859{
11860 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11861 n->nd_recv = nd_recv;
11862 n->nd_mid = nd_mid;
11863 n->nd_index = index;
11864 n->nd_rvalue = rvalue;
11865 n->call_operator_loc = *call_operator_loc;
11866 n->opening_loc = *opening_loc;
11867 n->closing_loc = *closing_loc;
11868 n->binary_operator_loc = *binary_operator_loc;
11869
11870 return n;
11871}
11872
11873static rb_node_op_asgn2_t *
11874rb_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)
11875{
11876 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11877 n->nd_recv = nd_recv;
11878 n->nd_value = nd_value;
11879 n->nd_vid = nd_vid;
11880 n->nd_mid = nd_mid;
11881 n->nd_aid = nd_aid;
11882 n->call_operator_loc = *call_operator_loc;
11883 n->message_loc = *message_loc;
11884 n->binary_operator_loc = *binary_operator_loc;
11885
11886 return n;
11887}
11888
11889static rb_node_op_asgn_or_t *
11890rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11891{
11892 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11893 n->nd_head = nd_head;
11894 n->nd_value = nd_value;
11895
11896 return n;
11897}
11898
11899static rb_node_op_asgn_and_t *
11900rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11901{
11902 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11903 n->nd_head = nd_head;
11904 n->nd_value = nd_value;
11905
11906 return n;
11907}
11908
11909static rb_node_gvar_t *
11910rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11911{
11912 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11913 n->nd_vid = nd_vid;
11914
11915 return n;
11916}
11917
11918static rb_node_lvar_t *
11919rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11920{
11921 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11922 n->nd_vid = nd_vid;
11923
11924 return n;
11925}
11926
11927static rb_node_dvar_t *
11928rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11929{
11930 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11931 n->nd_vid = nd_vid;
11932
11933 return n;
11934}
11935
11936static rb_node_ivar_t *
11937rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11938{
11939 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11940 n->nd_vid = nd_vid;
11941
11942 return n;
11943}
11944
11945static rb_node_const_t *
11946rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11947{
11948 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11949 n->nd_vid = nd_vid;
11950
11951 return n;
11952}
11953
11954static rb_node_cvar_t *
11955rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11956{
11957 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11958 n->nd_vid = nd_vid;
11959
11960 return n;
11961}
11962
11963static rb_node_nth_ref_t *
11964rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11965{
11966 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11967 n->nd_nth = nd_nth;
11968
11969 return n;
11970}
11971
11972static rb_node_back_ref_t *
11973rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11974{
11975 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11976 n->nd_nth = nd_nth;
11977
11978 return n;
11979}
11980
11981static rb_node_integer_t *
11982rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11983{
11984 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11985 n->val = val;
11986 n->minus = FALSE;
11987 n->base = base;
11988
11989 return n;
11990}
11991
11992static rb_node_float_t *
11993rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11994{
11995 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11996 n->val = val;
11997 n->minus = FALSE;
11998
11999 return n;
12000}
12001
12002static rb_node_rational_t *
12003rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12004{
12005 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12006 n->val = val;
12007 n->minus = FALSE;
12008 n->base = base;
12009 n->seen_point = seen_point;
12010
12011 return n;
12012}
12013
12014static rb_node_imaginary_t *
12015rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12016{
12017 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12018 n->val = val;
12019 n->minus = FALSE;
12020 n->base = base;
12021 n->seen_point = seen_point;
12022 n->type = numeric_type;
12023
12024 return n;
12025}
12026
12027static rb_node_str_t *
12028rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12029{
12030 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12031 n->string = string;
12032
12033 return n;
12034}
12035
12036/* TODO; Use union for NODE_DSTR2 */
12037static rb_node_dstr_t *
12038rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12039{
12040 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12041 n->string = string;
12042 n->as.nd_alen = nd_alen;
12043 n->nd_next = (rb_node_list_t *)nd_next;
12044
12045 return n;
12046}
12047
12048static rb_node_dstr_t *
12049rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12050{
12051 return rb_node_dstr_new0(p, string, 1, 0, loc);
12052}
12053
12054static rb_node_xstr_t *
12055rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12056{
12057 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12058 n->string = string;
12059
12060 return n;
12061}
12062
12063static rb_node_dxstr_t *
12064rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12065{
12066 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12067 n->string = string;
12068 n->as.nd_alen = nd_alen;
12069 n->nd_next = (rb_node_list_t *)nd_next;
12070
12071 return n;
12072}
12073
12074static rb_node_sym_t *
12075rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12076{
12077 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12078 n->string = rb_str_to_parser_string(p, str);
12079
12080 return n;
12081}
12082
12083static rb_node_dsym_t *
12084rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12085{
12086 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12087 n->string = string;
12088 n->as.nd_alen = nd_alen;
12089 n->nd_next = (rb_node_list_t *)nd_next;
12090
12091 return n;
12092}
12093
12094static rb_node_evstr_t *
12095rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12096{
12097 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12098 n->nd_body = nd_body;
12099 n->opening_loc = *opening_loc;
12100 n->closing_loc = *closing_loc;
12101
12102 return n;
12103}
12104
12105static rb_node_regx_t *
12106rb_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)
12107{
12108 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12109 n->string = string;
12110 n->options = options & RE_OPTION_MASK;
12111 n->opening_loc = *opening_loc;
12112 n->content_loc = *content_loc;
12113 n->closing_loc = *closing_loc;
12114
12115 return n;
12116}
12117
12118static rb_node_call_t *
12119rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12120{
12121 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12122 n->nd_recv = nd_recv;
12123 n->nd_mid = nd_mid;
12124 n->nd_args = nd_args;
12125
12126 return n;
12127}
12128
12129static rb_node_opcall_t *
12130rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12131{
12132 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12133 n->nd_recv = nd_recv;
12134 n->nd_mid = nd_mid;
12135 n->nd_args = nd_args;
12136
12137 return n;
12138}
12139
12140static rb_node_fcall_t *
12141rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12142{
12143 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12144 n->nd_mid = nd_mid;
12145 n->nd_args = nd_args;
12146
12147 return n;
12148}
12149
12150static rb_node_qcall_t *
12151rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12152{
12153 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12154 n->nd_recv = nd_recv;
12155 n->nd_mid = nd_mid;
12156 n->nd_args = nd_args;
12157
12158 return n;
12159}
12160
12161static rb_node_vcall_t *
12162rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12163{
12164 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12165 n->nd_mid = nd_mid;
12166
12167 return n;
12168}
12169
12170static rb_node_once_t *
12171rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12172{
12173 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12174 n->nd_body = nd_body;
12175
12176 return n;
12177}
12178
12179static rb_node_args_t *
12180rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12181{
12182 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12183 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12184
12185 return n;
12186}
12187
12188static rb_node_args_aux_t *
12189rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12190{
12191 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12192 n->nd_pid = nd_pid;
12193 n->nd_plen = nd_plen;
12194 n->nd_next = 0;
12195
12196 return n;
12197}
12198
12199static rb_node_opt_arg_t *
12200rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12201{
12202 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12203 n->nd_body = nd_body;
12204 n->nd_next = 0;
12205
12206 return n;
12207}
12208
12209static rb_node_kw_arg_t *
12210rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12211{
12212 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12213 n->nd_body = nd_body;
12214 n->nd_next = 0;
12215
12216 return n;
12217}
12218
12219static rb_node_postarg_t *
12220rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12221{
12222 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12223 n->nd_1st = nd_1st;
12224 n->nd_2nd = nd_2nd;
12225
12226 return n;
12227}
12228
12229static rb_node_argscat_t *
12230rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12231{
12232 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12233 n->nd_head = nd_head;
12234 n->nd_body = nd_body;
12235
12236 return n;
12237}
12238
12239static rb_node_argspush_t *
12240rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12241{
12242 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12243 n->nd_head = nd_head;
12244 n->nd_body = nd_body;
12245
12246 return n;
12247}
12248
12249static rb_node_splat_t *
12250rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12251{
12252 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12253 n->nd_head = nd_head;
12254 n->operator_loc = *operator_loc;
12255
12256 return n;
12257}
12258
12259static rb_node_block_pass_t *
12260rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12261{
12262 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12263 n->forwarding = 0;
12264 n->nd_head = 0;
12265 n->nd_body = nd_body;
12266 n->operator_loc = *operator_loc;
12267
12268 return n;
12269}
12270
12271static rb_node_alias_t *
12272rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12273{
12274 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12275 n->nd_1st = nd_1st;
12276 n->nd_2nd = nd_2nd;
12277 n->keyword_loc = *keyword_loc;
12278
12279 return n;
12280}
12281
12282static rb_node_valias_t *
12283rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12284{
12285 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12286 n->nd_alias = nd_alias;
12287 n->nd_orig = nd_orig;
12288 n->keyword_loc = *keyword_loc;
12289
12290 return n;
12291}
12292
12293static rb_node_undef_t *
12294rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12295{
12296 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12297 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12298 n->keyword_loc = NULL_LOC;
12299 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12300
12301 return n;
12302}
12303
12304static rb_node_errinfo_t *
12305rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12306{
12307 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12308
12309 return n;
12310}
12311
12312static rb_node_defined_t *
12313rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12314{
12315 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12316 n->nd_head = nd_head;
12317 n->keyword_loc = *keyword_loc;
12318
12319 return n;
12320}
12321
12322static rb_node_postexe_t *
12323rb_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)
12324{
12325 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12326 n->nd_body = nd_body;
12327 n->keyword_loc = *keyword_loc;
12328 n->opening_loc = *opening_loc;
12329 n->closing_loc = *closing_loc;
12330
12331 return n;
12332}
12333
12334static rb_node_attrasgn_t *
12335rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12336{
12337 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12338 n->nd_recv = nd_recv;
12339 n->nd_mid = nd_mid;
12340 n->nd_args = nd_args;
12341
12342 return n;
12343}
12344
12345static rb_node_aryptn_t *
12346rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12347{
12348 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12349 n->nd_pconst = 0;
12350 n->pre_args = pre_args;
12351 n->rest_arg = rest_arg;
12352 n->post_args = post_args;
12353
12354 return n;
12355}
12356
12357static rb_node_hshptn_t *
12358rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12359{
12360 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12361 n->nd_pconst = nd_pconst;
12362 n->nd_pkwargs = nd_pkwargs;
12363 n->nd_pkwrestarg = nd_pkwrestarg;
12364
12365 return n;
12366}
12367
12368static rb_node_fndptn_t *
12369rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12370{
12371 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12372 n->nd_pconst = 0;
12373 n->pre_rest_arg = pre_rest_arg;
12374 n->args = args;
12375 n->post_rest_arg = post_rest_arg;
12376
12377 return n;
12378}
12379
12380static rb_node_line_t *
12381rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12382{
12383 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12384
12385 return n;
12386}
12387
12388static rb_node_file_t *
12389rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12390{
12391 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12392 n->path = rb_str_to_parser_string(p, str);
12393
12394 return n;
12395}
12396
12397static rb_node_encoding_t *
12398rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12399{
12400 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12401 n->enc = p->enc;
12402
12403 return n;
12404}
12405
12406static rb_node_cdecl_t *
12407rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12408{
12409 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12410 n->nd_vid = nd_vid;
12411 n->nd_value = nd_value;
12412 n->nd_else = nd_else;
12413 n->shareability = shareability;
12414
12415 return n;
12416}
12417
12418static rb_node_op_cdecl_t *
12419rb_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)
12420{
12421 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12422 n->nd_head = nd_head;
12423 n->nd_value = nd_value;
12424 n->nd_aid = nd_aid;
12425 n->shareability = shareability;
12426
12427 return n;
12428}
12429
12430static rb_node_error_t *
12431rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12432{
12433 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12434
12435 return n;
12436}
12437
12438static rb_node_break_t *
12439rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12440{
12441 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12442 n->nd_stts = nd_stts;
12443 n->nd_chain = 0;
12444 n->keyword_loc = *keyword_loc;
12445
12446 return n;
12447}
12448
12449static rb_node_next_t *
12450rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12451{
12452 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12453 n->nd_stts = nd_stts;
12454 n->nd_chain = 0;
12455 n->keyword_loc = *keyword_loc;
12456
12457 return n;
12458}
12459
12460static rb_node_redo_t *
12461rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12462{
12463 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12464 n->nd_chain = 0;
12465 n->keyword_loc = *keyword_loc;
12466
12467 return n;
12468}
12469
12470static rb_node_def_temp_t *
12471rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12472{
12473 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12474 n->save.numparam_save = 0;
12475 n->save.max_numparam = 0;
12476 n->save.ctxt = p->ctxt;
12477 n->nd_def = 0;
12478 n->nd_mid = 0;
12479
12480 return n;
12481}
12482
12483static rb_node_def_temp_t *
12484def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12485{
12486 n->save.numparam_save = numparam_push(p);
12487 n->save.max_numparam = p->max_numparam;
12488 return n;
12489}
12490
12491#ifndef RIPPER
12492static enum node_type
12493nodetype(NODE *node) /* for debug */
12494{
12495 return (enum node_type)nd_type(node);
12496}
12497
12498static int
12499nodeline(NODE *node)
12500{
12501 return nd_line(node);
12502}
12503#endif
12504
12505static NODE*
12506newline_node(NODE *node)
12507{
12508 if (node) {
12509 node = remove_begin(node);
12510 nd_set_fl_newline(node);
12511 }
12512 return node;
12513}
12514
12515static void
12516fixpos(NODE *node, NODE *orig)
12517{
12518 if (!node) return;
12519 if (!orig) return;
12520 nd_set_line(node, nd_line(orig));
12521}
12522
12523static NODE*
12524block_append(struct parser_params *p, NODE *head, NODE *tail)
12525{
12526 NODE *end, *h = head, *nd;
12527
12528 if (tail == 0) return head;
12529
12530 if (h == 0) return tail;
12531 switch (nd_type(h)) {
12532 default:
12533 h = end = NEW_BLOCK(head, &head->nd_loc);
12534 head = end;
12535 break;
12536 case NODE_BLOCK:
12537 end = RNODE_BLOCK(h)->nd_end;
12538 break;
12539 }
12540
12541 nd = RNODE_BLOCK(end)->nd_head;
12542 switch (nd_type(nd)) {
12543 case NODE_RETURN:
12544 case NODE_BREAK:
12545 case NODE_NEXT:
12546 case NODE_REDO:
12547 case NODE_RETRY:
12548 rb_warning0L(nd_line(tail), "statement not reached");
12549 break;
12550
12551 default:
12552 break;
12553 }
12554
12555 if (!nd_type_p(tail, NODE_BLOCK)) {
12556 tail = NEW_BLOCK(tail, &tail->nd_loc);
12557 }
12558 RNODE_BLOCK(end)->nd_next = tail;
12559 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12560 nd_set_last_loc(head, nd_last_loc(tail));
12561 return head;
12562}
12563
12564/* append item to the list */
12565static NODE*
12566list_append(struct parser_params *p, NODE *list, NODE *item)
12567{
12568 NODE *last;
12569
12570 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12571 if (RNODE_LIST(list)->nd_next) {
12572 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12573 }
12574 else {
12575 last = list;
12576 }
12577
12578 RNODE_LIST(list)->as.nd_alen += 1;
12579 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12580 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12581
12582 nd_set_last_loc(list, nd_last_loc(item));
12583
12584 return list;
12585}
12586
12587/* concat two lists */
12588static NODE*
12589list_concat(NODE *head, NODE *tail)
12590{
12591 NODE *last;
12592
12593 if (RNODE_LIST(head)->nd_next) {
12594 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12595 }
12596 else {
12597 last = head;
12598 }
12599
12600 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12601 RNODE_LIST(last)->nd_next = tail;
12602 if (RNODE_LIST(tail)->nd_next) {
12603 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12604 }
12605 else {
12606 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12607 }
12608
12609 nd_set_last_loc(head, nd_last_loc(tail));
12610
12611 return head;
12612}
12613
12614static int
12615literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12616{
12617 if (!tail) return 1;
12618 if (!rb_parser_enc_compatible(p, head, tail)) {
12619 compile_error(p, "string literal encodings differ (%s / %s)",
12620 rb_enc_name(rb_parser_str_get_encoding(head)),
12621 rb_enc_name(rb_parser_str_get_encoding(tail)));
12622 rb_parser_str_resize(p, head, 0);
12623 rb_parser_str_resize(p, tail, 0);
12624 return 0;
12625 }
12626 rb_parser_str_buf_append(p, head, tail);
12627 return 1;
12628}
12629
12630static rb_parser_string_t *
12631string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12632{
12633 if (htype != NODE_DSTR) return NULL;
12634 if (RNODE_DSTR(head)->nd_next) {
12635 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12636 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12637 }
12638 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12639 ASSUME(lit);
12640 return lit;
12641}
12642
12643#ifndef RIPPER
12644static rb_parser_string_t *
12645rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12646{
12647 rb_parser_string_t *copy;
12648 if (!orig) return NULL;
12649 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12650 copy->coderange = orig->coderange;
12651 copy->enc = orig->enc;
12652 return copy;
12653}
12654#endif
12655
12656/* concat two string literals */
12657static NODE *
12658literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12659{
12660 enum node_type htype;
12661 rb_parser_string_t *lit;
12662
12663 if (!head) return tail;
12664 if (!tail) return head;
12665
12666 htype = nd_type(head);
12667 if (htype == NODE_EVSTR) {
12668 head = new_dstr(p, head, loc);
12669 htype = NODE_DSTR;
12670 }
12671 if (p->heredoc_indent > 0) {
12672 switch (htype) {
12673 case NODE_STR:
12674 head = str2dstr(p, head);
12675 case NODE_DSTR:
12676 return list_append(p, head, tail);
12677 default:
12678 break;
12679 }
12680 }
12681 switch (nd_type(tail)) {
12682 case NODE_STR:
12683 if ((lit = string_literal_head(p, htype, head)) != false) {
12684 htype = NODE_STR;
12685 }
12686 else {
12687 lit = RNODE_DSTR(head)->string;
12688 }
12689 if (htype == NODE_STR) {
12690 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12691 error:
12692 rb_discard_node(p, head);
12693 rb_discard_node(p, tail);
12694 return 0;
12695 }
12696 rb_discard_node(p, tail);
12697 }
12698 else {
12699 list_append(p, head, tail);
12700 }
12701 break;
12702
12703 case NODE_DSTR:
12704 if (htype == NODE_STR) {
12705 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12706 goto error;
12707 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12708 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12709 RNODE_STR(head)->string = NULL;
12710 rb_discard_node(p, head);
12711 head = tail;
12712 }
12713 else if (!RNODE_DSTR(tail)->string) {
12714 append:
12715 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12716 if (!RNODE_DSTR(head)->nd_next) {
12717 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12718 }
12719 else if (RNODE_DSTR(tail)->nd_next) {
12720 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12721 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12722 }
12723 rb_discard_node(p, tail);
12724 }
12725 else if ((lit = string_literal_head(p, htype, head)) != false) {
12726 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12727 goto error;
12728 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12729 RNODE_DSTR(tail)->string = 0;
12730 goto append;
12731 }
12732 else {
12733 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));
12734 RNODE_DSTR(tail)->string = 0;
12735 }
12736 break;
12737
12738 case NODE_EVSTR:
12739 if (htype == NODE_STR) {
12740 head = str2dstr(p, head);
12741 RNODE_DSTR(head)->as.nd_alen = 1;
12742 }
12743 list_append(p, head, tail);
12744 break;
12745 }
12746 return head;
12747}
12748
12749static void
12750nd_copy_flag(NODE *new_node, NODE *old_node)
12751{
12752 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12753 nd_set_line(new_node, nd_line(old_node));
12754 new_node->nd_loc = old_node->nd_loc;
12755 new_node->node_id = old_node->node_id;
12756}
12757
12758static NODE *
12759str2dstr(struct parser_params *p, NODE *node)
12760{
12761 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12762 nd_copy_flag(new_node, node);
12763 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12764 RNODE_DSTR(new_node)->as.nd_alen = 0;
12765 RNODE_DSTR(new_node)->nd_next = 0;
12766 RNODE_STR(node)->string = 0;
12767
12768 return new_node;
12769}
12770
12771static NODE *
12772str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12773{
12774 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12775 nd_copy_flag(new_node, node);
12776 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12777 RNODE_REGX(new_node)->options = options;
12778 nd_set_loc(new_node, loc);
12779 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12780 RNODE_REGX(new_node)->content_loc = *content_loc;
12781 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12782 RNODE_STR(node)->string = 0;
12783
12784 return new_node;
12785}
12786
12787static NODE *
12788evstr2dstr(struct parser_params *p, NODE *node)
12789{
12790 if (nd_type_p(node, NODE_EVSTR)) {
12791 node = new_dstr(p, node, &node->nd_loc);
12792 }
12793 return node;
12794}
12795
12796static NODE *
12797new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12798{
12799 NODE *head = node;
12800
12801 if (node) {
12802 switch (nd_type(node)) {
12803 case NODE_STR:
12804 return str2dstr(p, node);
12805 case NODE_DSTR:
12806 break;
12807 case NODE_EVSTR:
12808 return node;
12809 }
12810 }
12811 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12812}
12813
12814static NODE *
12815new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12816{
12817 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12818 return list_append(p, dstr, node);
12819}
12820
12821static NODE *
12822call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12823 const YYLTYPE *op_loc, const YYLTYPE *loc)
12824{
12825 NODE *expr;
12826 value_expr(p, recv);
12827 value_expr(p, arg1);
12828 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12829 nd_set_line(expr, op_loc->beg_pos.lineno);
12830 return expr;
12831}
12832
12833static NODE *
12834call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12835{
12836 NODE *opcall;
12837 value_expr(p, recv);
12838 opcall = NEW_OPCALL(recv, id, 0, loc);
12839 nd_set_line(opcall, op_loc->beg_pos.lineno);
12840 return opcall;
12841}
12842
12843static NODE *
12844new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12845{
12846 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12847 nd_set_line(qcall, op_loc->beg_pos.lineno);
12848 return qcall;
12849}
12850
12851static NODE*
12852new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12853{
12854 NODE *ret;
12855 if (block) block_dup_check(p, args, block);
12856 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12857 if (block) ret = method_add_block(p, ret, block, loc);
12858 fixpos(ret, recv);
12859 return ret;
12860}
12861
12862static rb_locations_lambda_body_t*
12863new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12864{
12865 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12866 body->node = node;
12867 body->opening_loc = *opening_loc;
12868 body->closing_loc = *closing_loc;
12869 return body;
12870}
12871
12872static NODE *
12873command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12874{
12875 NODE **body;
12876 enum node_type type = nd_type(m);
12877 switch (type) {
12878 case NODE_YIELD:
12879 compile_error(p, "block given to yield");
12880 return m;
12881 case NODE_RETURN:
12882 body = &RNODE_RETURN(m)->nd_stts;
12883 break;
12884 case NODE_BREAK:
12885 case NODE_NEXT:
12886 body = &RNODE_EXITS(m)->nd_stts;
12887 break;
12888 case NODE_REDO: /* `redo` has no argument */
12889 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12890 return m;
12891 case NODE_RETRY: /* `retry` has no argument */
12892 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12893 return m;
12894 default:
12895 block_dup_check(p, get_nd_args(p, m), b);
12896 return method_add_block(p, m, b, loc);
12897 }
12898 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12899 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12900 *body = command_add_block(p, *body, b, &b_loc);
12901 m->nd_loc.end_pos = loc->end_pos;
12902 return m;
12903}
12904
12905#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12906
12907static NODE*
12908last_expr_once_body(NODE *node)
12909{
12910 if (!node) return 0;
12911 return nd_once_body(node);
12912}
12913
12914static NODE*
12915match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12916{
12917 NODE *n;
12918 int line = op_loc->beg_pos.lineno;
12919
12920 value_expr(p, node1);
12921 value_expr(p, node2);
12922
12923 if ((n = last_expr_once_body(node1)) != 0) {
12924 switch (nd_type(n)) {
12925 case NODE_DREGX:
12926 {
12927 NODE *match = NEW_MATCH2(node1, node2, loc);
12928 nd_set_line(match, line);
12929 return match;
12930 }
12931
12932 case NODE_REGX:
12933 {
12934 const VALUE lit = rb_node_regx_string_val(n);
12935 if (!NIL_P(lit)) {
12936 NODE *match = NEW_MATCH2(node1, node2, loc);
12937 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12938 nd_set_line(match, line);
12939 return match;
12940 }
12941 }
12942 }
12943 }
12944
12945 if ((n = last_expr_once_body(node2)) != 0) {
12946 NODE *match3;
12947
12948 switch (nd_type(n)) {
12949 case NODE_DREGX:
12950 match3 = NEW_MATCH3(node2, node1, loc);
12951 return match3;
12952 }
12953 }
12954
12955 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12956 nd_set_line(n, line);
12957 return n;
12958}
12959
12960# if WARN_PAST_SCOPE
12961static int
12962past_dvar_p(struct parser_params *p, ID id)
12963{
12964 struct vtable *past = p->lvtbl->past;
12965 while (past) {
12966 if (vtable_included(past, id)) return 1;
12967 past = past->prev;
12968 }
12969 return 0;
12970}
12971# endif
12972
12973static int
12974numparam_nested_p(struct parser_params *p)
12975{
12976 struct local_vars *local = p->lvtbl;
12977 NODE *outer = local->numparam.outer;
12978 NODE *inner = local->numparam.inner;
12979 if (outer || inner) {
12980 NODE *used = outer ? outer : inner;
12981 compile_error(p, "numbered parameter is already used in %s block\n"
12982 "%s:%d: numbered parameter is already used here",
12983 outer ? "outer" : "inner",
12984 p->ruby_sourcefile, nd_line(used));
12985 parser_show_error_line(p, &used->nd_loc);
12986 return 1;
12987 }
12988 return 0;
12989}
12990
12991static int
12992numparam_used_p(struct parser_params *p)
12993{
12994 NODE *numparam = p->lvtbl->numparam.current;
12995 if (numparam) {
12996 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12997 "%s:%d: numbered parameter is already used here",
12998 p->ruby_sourcefile, nd_line(numparam));
12999 parser_show_error_line(p, &numparam->nd_loc);
13000 return 1;
13001 }
13002 return 0;
13003}
13004
13005static int
13006it_used_p(struct parser_params *p)
13007{
13008 NODE *it = p->lvtbl->it;
13009 if (it) {
13010 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
13011 "%s:%d: 'it' is already used here",
13012 p->ruby_sourcefile, nd_line(it));
13013 parser_show_error_line(p, &it->nd_loc);
13014 return 1;
13015 }
13016 return 0;
13017}
13018
13019static NODE*
13020gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13021{
13022 ID *vidp = NULL;
13023 NODE *node;
13024 switch (id) {
13025 case keyword_self:
13026 return NEW_SELF(loc);
13027 case keyword_nil:
13028 return NEW_NIL(loc);
13029 case keyword_true:
13030 return NEW_TRUE(loc);
13031 case keyword_false:
13032 return NEW_FALSE(loc);
13033 case keyword__FILE__:
13034 {
13035 VALUE file = p->ruby_sourcefile_string;
13036 if (NIL_P(file))
13037 file = rb_str_new(0, 0);
13038 node = NEW_FILE(file, loc);
13039 }
13040 return node;
13041 case keyword__LINE__:
13042 return NEW_LINE(loc);
13043 case keyword__ENCODING__:
13044 return NEW_ENCODING(loc);
13045
13046 }
13047 switch (id_type(id)) {
13048 case ID_LOCAL:
13049 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13050 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13051 if (vidp) *vidp |= LVAR_USED;
13052 node = NEW_DVAR(id, loc);
13053 return node;
13054 }
13055 if (local_id_ref(p, id, &vidp)) {
13056 if (vidp) *vidp |= LVAR_USED;
13057 node = NEW_LVAR(id, loc);
13058 return node;
13059 }
13060 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13061 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13062 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13063 node = NEW_DVAR(id, loc);
13064 struct local_vars *local = p->lvtbl;
13065 if (!local->numparam.current) local->numparam.current = node;
13066 return node;
13067 }
13068# if WARN_PAST_SCOPE
13069 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13070 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13071 }
13072# endif
13073 /* method call without arguments */
13074 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13075 if (numparam_used_p(p)) return 0;
13076 if (p->max_numparam == ORDINAL_PARAM) {
13077 compile_error(p, "ordinary parameter is defined");
13078 return 0;
13079 }
13080 if (!p->it_id) {
13081 p->it_id = idItImplicit;
13082 vtable_add(p->lvtbl->args, p->it_id);
13083 }
13084 NODE *node = NEW_DVAR(p->it_id, loc);
13085 if (!p->lvtbl->it) p->lvtbl->it = node;
13086 return node;
13087 }
13088 return NEW_VCALL(id, loc);
13089 case ID_GLOBAL:
13090 return NEW_GVAR(id, loc);
13091 case ID_INSTANCE:
13092 return NEW_IVAR(id, loc);
13093 case ID_CONST:
13094 return NEW_CONST(id, loc);
13095 case ID_CLASS:
13096 return NEW_CVAR(id, loc);
13097 }
13098 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13099 return 0;
13100}
13101
13102static rb_node_opt_arg_t *
13103opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13104{
13105 rb_node_opt_arg_t *opts = opt_list;
13106 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13107
13108 while (opts->nd_next) {
13109 opts = opts->nd_next;
13110 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13111 }
13112 opts->nd_next = opt;
13113
13114 return opt_list;
13115}
13116
13117static rb_node_kw_arg_t *
13118kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13119{
13120 if (kwlist) {
13121 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13122 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13123 }
13124 return kwlist;
13125}
13126
13127static NODE *
13128new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13129{
13130 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13131 p->ctxt.has_trailing_semicolon = 0;
13132
13133 NODE *n = expr;
13134 while (n) {
13135 if (nd_type_p(n, NODE_BEGIN)) {
13136 n = RNODE_BEGIN(n)->nd_body;
13137 }
13138 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13139 n = RNODE_BLOCK(n)->nd_head;
13140 }
13141 else {
13142 break;
13143 }
13144 }
13145
13146 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13147 NODE *block = NEW_BLOCK(expr, loc);
13148 return NEW_DEFINED(block, loc, keyword_loc);
13149 }
13150
13151 return NEW_DEFINED(n, loc, keyword_loc);
13152}
13153
13154static NODE*
13155str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13156{
13157 VALUE lit;
13158 rb_parser_string_t *str = RNODE_STR(node)->string;
13159 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13160 yyerror1(loc, "invalid symbol");
13161 lit = STR_NEW0();
13162 }
13163 else {
13164 lit = rb_str_new_parser_string(str);
13165 }
13166 return NEW_SYM(lit, loc);
13167}
13168
13169static NODE*
13170symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13171{
13172 enum node_type type = nd_type(symbol);
13173 switch (type) {
13174 case NODE_DSTR:
13175 nd_set_type(symbol, NODE_DSYM);
13176 break;
13177 case NODE_STR:
13178 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13179 break;
13180 default:
13181 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13182 }
13183 return list_append(p, symbols, symbol);
13184}
13185
13186static void
13187dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13188{
13189 if (dreg->string) {
13190 reg_fragment_setenc(p, dreg->string, options);
13191 }
13192 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13193 NODE *frag = list->nd_head;
13194 if (nd_type_p(frag, NODE_STR)) {
13195 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13196 }
13197 else if (nd_type_p(frag, NODE_DSTR)) {
13198 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13199 }
13200 }
13201}
13202
13203static NODE *
13204new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13205{
13206 if (!node) {
13207 /* Check string is valid regex */
13208 rb_parser_string_t *str = STRING_NEW0();
13209 reg_compile(p, str, options);
13210 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13211 return node;
13212 }
13213 switch (nd_type(node)) {
13214 case NODE_STR:
13215 {
13216 /* Check string is valid regex */
13217 reg_compile(p, RNODE_STR(node)->string, options);
13218 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13219 }
13220 break;
13221 default:
13222 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13223 /* fall through */
13224 case NODE_DSTR:
13225 nd_set_type(node, NODE_DREGX);
13226 nd_set_loc(node, loc);
13227 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13228 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13229 if (dreg->nd_next) {
13230 dregex_fragment_setenc(p, dreg, options);
13231 }
13232 if (options & RE_OPTION_ONCE) {
13233 node = NEW_ONCE(node, loc);
13234 }
13235 break;
13236 }
13237 return node;
13238}
13239
13240static rb_node_kw_arg_t *
13241new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13242{
13243 if (!k) return 0;
13244 return NEW_KW_ARG((k), loc);
13245}
13246
13247static NODE *
13248new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13249{
13250 if (!node) {
13251 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13252 return xstr;
13253 }
13254 switch (nd_type(node)) {
13255 case NODE_STR:
13256 nd_set_type(node, NODE_XSTR);
13257 nd_set_loc(node, loc);
13258 break;
13259 case NODE_DSTR:
13260 nd_set_type(node, NODE_DXSTR);
13261 nd_set_loc(node, loc);
13262 break;
13263 default:
13264 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13265 break;
13266 }
13267 return node;
13268}
13269
13270static const
13271struct st_hash_type literal_type = {
13272 literal_cmp,
13273 literal_hash,
13274};
13275
13276static int nd_type_st_key_enable_p(NODE *node);
13277
13278static void
13279check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13280{
13281 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13282 if (!arg || !p->case_labels) return;
13283 if (!nd_type_st_key_enable_p(arg)) return;
13284
13285 if (p->case_labels == CHECK_LITERAL_WHEN) {
13286 p->case_labels = st_init_table(&literal_type);
13287 }
13288 else {
13289 st_data_t line;
13290 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13291 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13292 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13293 return;
13294 }
13295 }
13296 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13297}
13298
13299#ifdef RIPPER
13300static int
13301id_is_var(struct parser_params *p, ID id)
13302{
13303 if (is_notop_id(id)) {
13304 switch (id & ID_SCOPE_MASK) {
13305 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13306 return 1;
13307 case ID_LOCAL:
13308 if (dyna_in_block(p)) {
13309 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13310 }
13311 if (local_id(p, id)) return 1;
13312 /* method call without arguments */
13313 return 0;
13314 }
13315 }
13316 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13317 return 0;
13318}
13319#endif
13320
13321static inline enum lex_state_e
13322parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13323{
13324 if (p->debug) {
13325 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13326 }
13327 return p->lex.state = ls;
13328}
13329
13330#ifndef RIPPER
13331static void
13332flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13333{
13334 VALUE mesg = p->debug_buffer;
13335
13336 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13337 p->debug_buffer = Qnil;
13338 rb_io_puts(1, &mesg, out);
13339 }
13340 if (!NIL_P(str) && RSTRING_LEN(str)) {
13341 rb_io_write(p->debug_output, str);
13342 }
13343}
13344
13345static const char rb_parser_lex_state_names[][8] = {
13346 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13347 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13348 "LABEL", "LABELED","FITEM",
13349};
13350
13351static VALUE
13352append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13353{
13354 int i, sep = 0;
13355 unsigned int mask = 1;
13356 static const char none[] = "NONE";
13357
13358 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13359 if ((unsigned)state & mask) {
13360 if (sep) {
13361 rb_str_cat(buf, "|", 1);
13362 }
13363 sep = 1;
13364 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13365 }
13366 }
13367 if (!sep) {
13368 rb_str_cat(buf, none, sizeof(none)-1);
13369 }
13370 return buf;
13371}
13372
13373enum lex_state_e
13374rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13375 enum lex_state_e to, int line)
13376{
13377 VALUE mesg;
13378 mesg = rb_str_new_cstr("lex_state: ");
13379 append_lex_state_name(p, from, mesg);
13380 rb_str_cat_cstr(mesg, " -> ");
13381 append_lex_state_name(p, to, mesg);
13382 rb_str_catf(mesg, " at line %d\n", line);
13383 flush_debug_buffer(p, p->debug_output, mesg);
13384 return to;
13385}
13386
13387VALUE
13388rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13389{
13390 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13391}
13392
13393static void
13394append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13395{
13396 if (stack == 0) {
13397 rb_str_cat_cstr(mesg, "0");
13398 }
13399 else {
13400 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13401 for (; mask && !(stack & mask); mask >>= 1) continue;
13402 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13403 }
13404}
13405
13406void
13407rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13408 const char *name, int line)
13409{
13410 VALUE mesg = rb_sprintf("%s: ", name);
13411 append_bitstack_value(p, stack, mesg);
13412 rb_str_catf(mesg, " at line %d\n", line);
13413 flush_debug_buffer(p, p->debug_output, mesg);
13414}
13415
13416void
13417rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13418{
13419 va_list ap;
13420 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13421
13422 va_start(ap, fmt);
13423 rb_str_vcatf(mesg, fmt, ap);
13424 va_end(ap);
13425 yyerror0(RSTRING_PTR(mesg));
13426 RB_GC_GUARD(mesg);
13427
13428 mesg = rb_str_new(0, 0);
13429 append_lex_state_name(p, p->lex.state, mesg);
13430 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13431 rb_str_resize(mesg, 0);
13432 append_bitstack_value(p, p->cond_stack, mesg);
13433 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13434 rb_str_resize(mesg, 0);
13435 append_bitstack_value(p, p->cmdarg_stack, mesg);
13436 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13437 if (p->debug_output == rb_ractor_stdout())
13438 p->debug_output = rb_ractor_stderr();
13439 p->debug = TRUE;
13440}
13441
13442static YYLTYPE *
13443rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13444{
13445 yylloc->beg_pos.lineno = sourceline;
13446 yylloc->beg_pos.column = beg_pos;
13447 yylloc->end_pos.lineno = sourceline;
13448 yylloc->end_pos.column = end_pos;
13449 return yylloc;
13450}
13451
13452YYLTYPE *
13453rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13454{
13455 int sourceline = here->sourceline;
13456 int beg_pos = (int)here->offset - here->quote
13457 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13458 int end_pos = (int)here->offset + here->length + here->quote;
13459
13460 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13461}
13462
13463YYLTYPE *
13464rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13465{
13466 yylloc->beg_pos.lineno = p->delayed.beg_line;
13467 yylloc->beg_pos.column = p->delayed.beg_col;
13468 yylloc->end_pos.lineno = p->delayed.end_line;
13469 yylloc->end_pos.column = p->delayed.end_col;
13470
13471 return yylloc;
13472}
13473
13474YYLTYPE *
13475rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13476{
13477 int sourceline = p->ruby_sourceline;
13478 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13479 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13480 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13481}
13482
13483YYLTYPE *
13484rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13485{
13486 yylloc->end_pos = yylloc->beg_pos;
13487
13488 return yylloc;
13489}
13490
13491YYLTYPE *
13492rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13493{
13494 int sourceline = p->ruby_sourceline;
13495 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13496 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13497 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13498}
13499
13500YYLTYPE *
13501rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13502{
13503 int sourceline = p->ruby_sourceline;
13504 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13505 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13506 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13507}
13508#endif /* !RIPPER */
13509
13510static int
13511assignable0(struct parser_params *p, ID id, const char **err)
13512{
13513 if (!id) return -1;
13514 switch (id) {
13515 case keyword_self:
13516 *err = "Can't change the value of self";
13517 return -1;
13518 case keyword_nil:
13519 *err = "Can't assign to nil";
13520 return -1;
13521 case keyword_true:
13522 *err = "Can't assign to true";
13523 return -1;
13524 case keyword_false:
13525 *err = "Can't assign to false";
13526 return -1;
13527 case keyword__FILE__:
13528 *err = "Can't assign to __FILE__";
13529 return -1;
13530 case keyword__LINE__:
13531 *err = "Can't assign to __LINE__";
13532 return -1;
13533 case keyword__ENCODING__:
13534 *err = "Can't assign to __ENCODING__";
13535 return -1;
13536 }
13537 switch (id_type(id)) {
13538 case ID_LOCAL:
13539 if (dyna_in_block(p)) {
13540 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13541 compile_error(p, "Can't assign to numbered parameter _%d",
13542 NUMPARAM_ID_TO_IDX(id));
13543 return -1;
13544 }
13545 if (dvar_curr(p, id)) return NODE_DASGN;
13546 if (dvar_defined(p, id)) return NODE_DASGN;
13547 if (local_id(p, id)) return NODE_LASGN;
13548 dyna_var(p, id);
13549 return NODE_DASGN;
13550 }
13551 else {
13552 if (!local_id(p, id)) local_var(p, id);
13553 return NODE_LASGN;
13554 }
13555 break;
13556 case ID_GLOBAL: return NODE_GASGN;
13557 case ID_INSTANCE: return NODE_IASGN;
13558 case ID_CONST:
13559 if (!p->ctxt.in_def) return NODE_CDECL;
13560 *err = "dynamic constant assignment";
13561 return -1;
13562 case ID_CLASS: return NODE_CVASGN;
13563 default:
13564 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13565 }
13566 return -1;
13567}
13568
13569static NODE*
13570assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13571{
13572 const char *err = 0;
13573 int node_type = assignable0(p, id, &err);
13574 switch (node_type) {
13575 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13576 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13577 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13578 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13579 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13580 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13581 }
13582/* TODO: FIXME */
13583#ifndef RIPPER
13584 if (err) yyerror1(loc, err);
13585#else
13586 if (err) set_value(assign_error(p, err, p->s_lvalue));
13587#endif
13588 return NEW_ERROR(loc);
13589}
13590
13591static int
13592is_private_local_id(struct parser_params *p, ID name)
13593{
13594 VALUE s;
13595 if (name == idUScore) return 1;
13596 if (!is_local_id(name)) return 0;
13597 s = rb_id2str(name);
13598 if (!s) return 0;
13599 return RSTRING_PTR(s)[0] == '_';
13600}
13601
13602static int
13603shadowing_lvar_0(struct parser_params *p, ID name)
13604{
13605 if (dyna_in_block(p)) {
13606 if (dvar_curr(p, name)) {
13607 if (is_private_local_id(p, name)) return 1;
13608 yyerror0("duplicated argument name");
13609 }
13610 else if (dvar_defined(p, name) || local_id(p, name)) {
13611 vtable_add(p->lvtbl->vars, name);
13612 if (p->lvtbl->used) {
13613 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13614 }
13615 return 0;
13616 }
13617 }
13618 else {
13619 if (local_id(p, name)) {
13620 if (is_private_local_id(p, name)) return 1;
13621 yyerror0("duplicated argument name");
13622 }
13623 }
13624 return 1;
13625}
13626
13627static ID
13628shadowing_lvar(struct parser_params *p, ID name)
13629{
13630 shadowing_lvar_0(p, name);
13631 return name;
13632}
13633
13634static void
13635new_bv(struct parser_params *p, ID name)
13636{
13637 if (!name) return;
13638 if (!is_local_id(name)) {
13639 compile_error(p, "invalid local variable - %"PRIsVALUE,
13640 rb_id2str(name));
13641 return;
13642 }
13643 if (!shadowing_lvar_0(p, name)) return;
13644 dyna_var(p, name);
13645 ID *vidp = 0;
13646 if (dvar_defined_ref(p, name, &vidp)) {
13647 if (vidp) *vidp |= LVAR_USED;
13648 }
13649}
13650
13651static void
13652aryset_check(struct parser_params *p, NODE *args)
13653{
13654 NODE *block = 0, *kwds = 0;
13655 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13656 block = RNODE_BLOCK_PASS(args)->nd_body;
13657 args = RNODE_BLOCK_PASS(args)->nd_head;
13658 }
13659 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13660 args = RNODE_ARGSCAT(args)->nd_body;
13661 }
13662 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13663 kwds = RNODE_ARGSPUSH(args)->nd_body;
13664 }
13665 else {
13666 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13667 next = RNODE_LIST(next)->nd_next) {
13668 kwds = RNODE_LIST(next)->nd_head;
13669 }
13670 }
13671 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13672 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13673 }
13674 if (block) {
13675 yyerror1(&block->nd_loc, "block arg given in index assignment");
13676 }
13677}
13678
13679static NODE *
13680aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13681{
13682 aryset_check(p, idx);
13683 return NEW_ATTRASGN(recv, tASET, idx, loc);
13684}
13685
13686static void
13687block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13688{
13689 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13690 compile_error(p, "both block arg and actual block given");
13691 }
13692}
13693
13694static NODE *
13695attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13696{
13697 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13698 return NEW_ATTRASGN(recv, id, 0, loc);
13699}
13700
13701static VALUE
13702rb_backref_error(struct parser_params *p, NODE *node)
13703{
13704#ifndef RIPPER
13705# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13706#else
13707# define ERR(...) rb_sprintf(__VA_ARGS__)
13708#endif
13709 switch (nd_type(node)) {
13710 case NODE_NTH_REF:
13711 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13712 case NODE_BACK_REF:
13713 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13714 }
13715#undef ERR
13716 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13717}
13718
13719static NODE *
13720arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13721{
13722 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13723 switch (nd_type(node1)) {
13724 case NODE_LIST:
13725 return list_append(p, node1, node2);
13726 case NODE_BLOCK_PASS:
13727 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13728 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13729 return node1;
13730 case NODE_ARGSPUSH:
13731 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13732 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13733 nd_set_type(node1, NODE_ARGSCAT);
13734 return node1;
13735 case NODE_ARGSCAT:
13736 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13737 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13738 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13739 return node1;
13740 }
13741 return NEW_ARGSPUSH(node1, node2, loc);
13742}
13743
13744static NODE *
13745arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13746{
13747 if (!node2) return node1;
13748 switch (nd_type(node1)) {
13749 case NODE_BLOCK_PASS:
13750 if (RNODE_BLOCK_PASS(node1)->nd_head)
13751 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13752 else
13753 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13754 return node1;
13755 case NODE_ARGSPUSH:
13756 if (!nd_type_p(node2, NODE_LIST)) break;
13757 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13758 nd_set_type(node1, NODE_ARGSCAT);
13759 return node1;
13760 case NODE_ARGSCAT:
13761 if (!nd_type_p(node2, NODE_LIST) ||
13762 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13763 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13764 return node1;
13765 }
13766 return NEW_ARGSCAT(node1, node2, loc);
13767}
13768
13769static NODE *
13770last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13771{
13772 NODE *n1;
13773 if ((n1 = splat_array(args)) != 0) {
13774 return list_append(p, n1, last_arg);
13775 }
13776 return arg_append(p, args, last_arg, loc);
13777}
13778
13779static NODE *
13780rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13781{
13782 NODE *n1;
13783 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13784 return list_concat(n1, rest_arg);
13785 }
13786 return arg_concat(p, args, rest_arg, loc);
13787}
13788
13789static NODE *
13790splat_array(NODE* node)
13791{
13792 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13793 if (nd_type_p(node, NODE_LIST)) return node;
13794 return 0;
13795}
13796
13797static void
13798mark_lvar_used(struct parser_params *p, NODE *rhs)
13799{
13800 ID *vidp = NULL;
13801 if (!rhs) return;
13802 switch (nd_type(rhs)) {
13803 case NODE_LASGN:
13804 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13805 if (vidp) *vidp |= LVAR_USED;
13806 }
13807 break;
13808 case NODE_DASGN:
13809 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13810 if (vidp) *vidp |= LVAR_USED;
13811 }
13812 break;
13813#if 0
13814 case NODE_MASGN:
13815 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13816 mark_lvar_used(p, rhs->nd_head);
13817 }
13818 break;
13819#endif
13820 }
13821}
13822
13823static int is_static_content(NODE *node);
13824
13825static NODE *
13826node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13827{
13828 if (!lhs) return 0;
13829
13830 switch (nd_type(lhs)) {
13831 case NODE_CDECL:
13832 case NODE_GASGN:
13833 case NODE_IASGN:
13834 case NODE_LASGN:
13835 case NODE_DASGN:
13836 case NODE_MASGN:
13837 case NODE_CVASGN:
13838 set_nd_value(p, lhs, rhs);
13839 nd_set_loc(lhs, loc);
13840 break;
13841
13842 case NODE_ATTRASGN:
13843 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13844 nd_set_loc(lhs, loc);
13845 break;
13846
13847 default:
13848 /* should not happen */
13849 break;
13850 }
13851
13852 return lhs;
13853}
13854
13855static NODE *
13856value_expr_check(struct parser_params *p, NODE *node)
13857{
13858 NODE *void_node = 0, *vn;
13859
13860 if (!node) {
13861 rb_warning0("empty expression");
13862 }
13863 while (node) {
13864 switch (nd_type(node)) {
13865 case NODE_ENSURE:
13866 vn = RNODE_ENSURE(node)->nd_head;
13867 node = RNODE_ENSURE(node)->nd_ensr;
13868 /* nd_ensr should not be NULL, check it out next */
13869 if (vn && (vn = value_expr_check(p, vn))) {
13870 goto found;
13871 }
13872 break;
13873
13874 case NODE_RESCUE:
13875 /* void only if all children are void */
13876 vn = RNODE_RESCUE(node)->nd_head;
13877 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13878 if (!void_node) void_node = vn;
13879 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13880 if (!nd_type_p(r, NODE_RESBODY)) {
13881 compile_error(p, "unexpected node");
13882 return NULL;
13883 }
13884 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13885 void_node = 0;
13886 break;
13887 }
13888 if (!void_node) void_node = vn;
13889 }
13890 node = RNODE_RESCUE(node)->nd_else;
13891 if (!node) return void_node;
13892 break;
13893
13894 case NODE_RETURN:
13895 case NODE_BREAK:
13896 case NODE_NEXT:
13897 case NODE_REDO:
13898 case NODE_RETRY:
13899 goto found;
13900
13901 case NODE_CASE3:
13902 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13903 compile_error(p, "unexpected node");
13904 return NULL;
13905 }
13906 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13907 return NULL;
13908 }
13909 /* single line pattern matching with "=>" operator */
13910 goto found;
13911
13912 case NODE_BLOCK:
13913 while (RNODE_BLOCK(node)->nd_next) {
13914 node = RNODE_BLOCK(node)->nd_next;
13915 }
13916 node = RNODE_BLOCK(node)->nd_head;
13917 break;
13918
13919 case NODE_BEGIN:
13920 node = RNODE_BEGIN(node)->nd_body;
13921 break;
13922
13923 case NODE_IF:
13924 case NODE_UNLESS:
13925 if (!RNODE_IF(node)->nd_body) {
13926 return NULL;
13927 }
13928 else if (!RNODE_IF(node)->nd_else) {
13929 return NULL;
13930 }
13931 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13932 if (!vn) return NULL;
13933 if (!void_node) void_node = vn;
13934 node = RNODE_IF(node)->nd_else;
13935 break;
13936
13937 case NODE_AND:
13938 case NODE_OR:
13939 node = RNODE_AND(node)->nd_1st;
13940 break;
13941
13942 case NODE_LASGN:
13943 case NODE_DASGN:
13944 case NODE_MASGN:
13945 mark_lvar_used(p, node);
13946 return NULL;
13947
13948 default:
13949 return NULL;
13950 }
13951 }
13952
13953 return NULL;
13954
13955 found:
13956 /* return the first found node */
13957 return void_node ? void_node : node;
13958}
13959
13960static int
13961value_expr(struct parser_params *p, NODE *node)
13962{
13963 NODE *void_node = value_expr_check(p, node);
13964 if (void_node) {
13965 yyerror1(&void_node->nd_loc, "void value expression");
13966 /* or "control never reach"? */
13967 return FALSE;
13968 }
13969 return TRUE;
13970}
13971
13972static void
13973void_expr(struct parser_params *p, NODE *node)
13974{
13975 const char *useless = 0;
13976
13977 if (!RTEST(ruby_verbose)) return;
13978
13979 if (!node || !(node = nd_once_body(node))) return;
13980 switch (nd_type(node)) {
13981 case NODE_OPCALL:
13982 switch (RNODE_OPCALL(node)->nd_mid) {
13983 case '+':
13984 case '-':
13985 case '*':
13986 case '/':
13987 case '%':
13988 case tPOW:
13989 case tUPLUS:
13990 case tUMINUS:
13991 case '|':
13992 case '^':
13993 case '&':
13994 case tCMP:
13995 case '>':
13996 case tGEQ:
13997 case '<':
13998 case tLEQ:
13999 case tEQ:
14000 case tNEQ:
14001 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
14002 break;
14003 }
14004 break;
14005
14006 case NODE_LVAR:
14007 case NODE_DVAR:
14008 case NODE_GVAR:
14009 case NODE_IVAR:
14010 case NODE_CVAR:
14011 case NODE_NTH_REF:
14012 case NODE_BACK_REF:
14013 useless = "a variable";
14014 break;
14015 case NODE_CONST:
14016 useless = "a constant";
14017 break;
14018 case NODE_SYM:
14019 case NODE_LINE:
14020 case NODE_FILE:
14021 case NODE_ENCODING:
14022 case NODE_INTEGER:
14023 case NODE_FLOAT:
14024 case NODE_RATIONAL:
14025 case NODE_IMAGINARY:
14026 case NODE_STR:
14027 case NODE_DSTR:
14028 case NODE_REGX:
14029 case NODE_DREGX:
14030 useless = "a literal";
14031 break;
14032 case NODE_COLON2:
14033 case NODE_COLON3:
14034 useless = "::";
14035 break;
14036 case NODE_DOT2:
14037 useless = "..";
14038 break;
14039 case NODE_DOT3:
14040 useless = "...";
14041 break;
14042 case NODE_SELF:
14043 useless = "self";
14044 break;
14045 case NODE_NIL:
14046 useless = "nil";
14047 break;
14048 case NODE_TRUE:
14049 useless = "true";
14050 break;
14051 case NODE_FALSE:
14052 useless = "false";
14053 break;
14054 case NODE_DEFINED:
14055 useless = "defined?";
14056 break;
14057 }
14058
14059 if (useless) {
14060 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14061 }
14062}
14063
14064/* warns useless use of block and returns the last statement node */
14065static NODE *
14066void_stmts(struct parser_params *p, NODE *node)
14067{
14068 NODE *const n = node;
14069 if (!RTEST(ruby_verbose)) return n;
14070 if (!node) return n;
14071 if (!nd_type_p(node, NODE_BLOCK)) return n;
14072
14073 while (RNODE_BLOCK(node)->nd_next) {
14074 void_expr(p, RNODE_BLOCK(node)->nd_head);
14075 node = RNODE_BLOCK(node)->nd_next;
14076 }
14077 return RNODE_BLOCK(node)->nd_head;
14078}
14079
14080static NODE *
14081remove_begin(NODE *node)
14082{
14083 NODE **n = &node, *n1 = node;
14084 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14085 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14086 }
14087 return node;
14088}
14089
14090static void
14091reduce_nodes(struct parser_params *p, NODE **body)
14092{
14093 NODE *node = *body;
14094
14095 if (!node) {
14096 *body = NEW_NIL(&NULL_LOC);
14097 return;
14098 }
14099#define subnodes(type, n1, n2) \
14100 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14101 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14102 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14103
14104 while (node) {
14105 int newline = (int)nd_fl_newline(node);
14106 switch (nd_type(node)) {
14107 end:
14108 case NODE_NIL:
14109 *body = 0;
14110 return;
14111 case NODE_BEGIN:
14112 *body = node = RNODE_BEGIN(node)->nd_body;
14113 if (newline && node) nd_set_fl_newline(node);
14114 continue;
14115 case NODE_BLOCK:
14116 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14117 break;
14118 case NODE_IF:
14119 case NODE_UNLESS:
14120 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14121 return;
14122 case NODE_CASE:
14123 body = &RNODE_CASE(node)->nd_body;
14124 break;
14125 case NODE_WHEN:
14126 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14127 break;
14128 case NODE_ENSURE:
14129 body = &RNODE_ENSURE(node)->nd_head;
14130 break;
14131 case NODE_RESCUE:
14132 newline = 0; // RESBODY should not be a NEWLINE
14133 if (RNODE_RESCUE(node)->nd_else) {
14134 body = &RNODE_RESCUE(node)->nd_resq;
14135 break;
14136 }
14137 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14138 break;
14139 default:
14140 return;
14141 }
14142 node = *body;
14143 if (newline && node) nd_set_fl_newline(node);
14144 }
14145
14146#undef subnodes
14147}
14148
14149static int
14150is_static_content(NODE *node)
14151{
14152 if (!node) return 1;
14153 switch (nd_type(node)) {
14154 case NODE_HASH:
14155 if (!(node = RNODE_HASH(node)->nd_head)) break;
14156 case NODE_LIST:
14157 do {
14158 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14159 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14160 case NODE_SYM:
14161 case NODE_REGX:
14162 case NODE_LINE:
14163 case NODE_FILE:
14164 case NODE_ENCODING:
14165 case NODE_INTEGER:
14166 case NODE_FLOAT:
14167 case NODE_RATIONAL:
14168 case NODE_IMAGINARY:
14169 case NODE_STR:
14170 case NODE_NIL:
14171 case NODE_TRUE:
14172 case NODE_FALSE:
14173 case NODE_ZLIST:
14174 break;
14175 default:
14176 return 0;
14177 }
14178 return 1;
14179}
14180
14181static int
14182assign_in_cond(struct parser_params *p, NODE *node)
14183{
14184 switch (nd_type(node)) {
14185 case NODE_MASGN:
14186 case NODE_LASGN:
14187 case NODE_DASGN:
14188 case NODE_GASGN:
14189 case NODE_IASGN:
14190 case NODE_CVASGN:
14191 case NODE_CDECL:
14192 break;
14193
14194 default:
14195 return 0;
14196 }
14197
14198 if (!get_nd_value(p, node)) return 1;
14199 if (is_static_content(get_nd_value(p, node))) {
14200 /* reports always */
14201 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14202 }
14203 return 1;
14204}
14205
14206enum cond_type {
14207 COND_IN_OP,
14208 COND_IN_COND,
14209 COND_IN_FF
14210};
14211
14212#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14213 switch (t) { \
14214 case COND_IN_OP: break; \
14215 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14216 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14217 } \
14218} while (0)
14219
14220static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14221
14222static NODE*
14223range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14224{
14225 enum node_type type;
14226
14227 if (node == 0) return 0;
14228
14229 type = nd_type(node);
14230 value_expr(p, node);
14231 if (type == NODE_INTEGER) {
14232 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14233 ID lineno = rb_intern("$.");
14234 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14235 }
14236 return cond0(p, node, COND_IN_FF, loc, true);
14237}
14238
14239static NODE*
14240cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14241{
14242 if (node == 0) return 0;
14243 if (!(node = nd_once_body(node))) return 0;
14244 assign_in_cond(p, node);
14245
14246 switch (nd_type(node)) {
14247 case NODE_BEGIN:
14248 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14249 break;
14250
14251 case NODE_DSTR:
14252 case NODE_EVSTR:
14253 case NODE_STR:
14254 case NODE_FILE:
14255 SWITCH_BY_COND_TYPE(type, warn, "string ");
14256 break;
14257
14258 case NODE_REGX:
14259 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14260 nd_set_type(node, NODE_MATCH);
14261 break;
14262
14263 case NODE_DREGX:
14264 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14265
14266 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14267
14268 case NODE_BLOCK:
14269 {
14270 NODE *end = RNODE_BLOCK(node)->nd_end;
14271 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14272 if (top) top = node == end;
14273 *expr = cond0(p, *expr, type, loc, top);
14274 }
14275 break;
14276
14277 case NODE_AND:
14278 case NODE_OR:
14279 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14280 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14281 break;
14282
14283 case NODE_DOT2:
14284 case NODE_DOT3:
14285 if (!top) break;
14286 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14287 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14288 switch (nd_type(node)) {
14289 case NODE_DOT2:
14290 nd_set_type(node,NODE_FLIP2);
14291 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14292 (void)flip2;
14293 break;
14294 case NODE_DOT3:
14295 nd_set_type(node, NODE_FLIP3);
14296 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14297 (void)flip3;
14298 break;
14299 }
14300 break;
14301
14302 case NODE_SYM:
14303 case NODE_DSYM:
14304 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14305 break;
14306
14307 case NODE_LINE:
14308 case NODE_ENCODING:
14309 case NODE_INTEGER:
14310 case NODE_FLOAT:
14311 case NODE_RATIONAL:
14312 case NODE_IMAGINARY:
14313 SWITCH_BY_COND_TYPE(type, warning, "");
14314 break;
14315
14316 default:
14317 break;
14318 }
14319 return node;
14320}
14321
14322static NODE*
14323cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14324{
14325 if (node == 0) return 0;
14326 return cond0(p, node, COND_IN_COND, loc, true);
14327}
14328
14329static NODE*
14330method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14331{
14332 if (node == 0) return 0;
14333 return cond0(p, node, COND_IN_OP, loc, true);
14334}
14335
14336static NODE*
14337new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14338{
14339 YYLTYPE loc = {*pos, *pos};
14340 return NEW_NIL(&loc);
14341}
14342
14343static NODE*
14344new_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)
14345{
14346 if (!cc) return right;
14347 cc = cond0(p, cc, COND_IN_COND, loc, true);
14348 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14349}
14350
14351static NODE*
14352new_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)
14353{
14354 if (!cc) return right;
14355 cc = cond0(p, cc, COND_IN_COND, loc, true);
14356 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14357}
14358
14359#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))
14360
14361static NODE*
14362logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14363 const YYLTYPE *op_loc, const YYLTYPE *loc)
14364{
14365 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14366 NODE *op;
14367 value_expr(p, left);
14368 if (left && nd_type_p(left, type)) {
14369 NODE *node = left, *second;
14370 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14371 node = second;
14372 }
14373 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14374 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14375 left->nd_loc.end_pos = loc->end_pos;
14376 return left;
14377 }
14378 op = NEW_AND_OR(type, left, right, loc, op_loc);
14379 nd_set_line(op, op_loc->beg_pos.lineno);
14380 return op;
14381}
14382
14383#undef NEW_AND_OR
14384
14385static void
14386no_blockarg(struct parser_params *p, NODE *node)
14387{
14388 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14389 compile_error(p, "block argument should not be given");
14390 }
14391}
14392
14393static NODE *
14394ret_args(struct parser_params *p, NODE *node)
14395{
14396 if (node) {
14397 no_blockarg(p, node);
14398 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14399 node = RNODE_LIST(node)->nd_head;
14400 }
14401 }
14402 return node;
14403}
14404
14405static NODE*
14406negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14407{
14408 switch (nd_type(node)) {
14409 case NODE_INTEGER:
14410 RNODE_INTEGER(node)->minus = TRUE;
14411 break;
14412 case NODE_FLOAT:
14413 RNODE_FLOAT(node)->minus = TRUE;
14414 break;
14415 case NODE_RATIONAL:
14416 RNODE_RATIONAL(node)->minus = TRUE;
14417 break;
14418 case NODE_IMAGINARY:
14419 RNODE_IMAGINARY(node)->minus = TRUE;
14420 break;
14421 }
14422 node->nd_loc = *loc;
14423 return node;
14424}
14425
14426static NODE *
14427arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14428{
14429 if (node2) {
14430 if (!node1) return (NODE *)node2;
14431 node2->nd_head = node1;
14432 nd_set_first_lineno(node2, nd_first_lineno(node1));
14433 nd_set_first_column(node2, nd_first_column(node1));
14434 return (NODE *)node2;
14435 }
14436 return node1;
14437}
14438
14439static bool
14440args_info_empty_p(struct rb_args_info *args)
14441{
14442 if (args->pre_args_num) return false;
14443 if (args->post_args_num) return false;
14444 if (args->rest_arg) return false;
14445 if (args->opt_args) return false;
14446 if (args->block_arg) return false;
14447 if (args->kw_args) return false;
14448 if (args->kw_rest_arg) return false;
14449 return true;
14450}
14451
14452static rb_node_args_t *
14453new_args(struct parser_params *p, rb_node_args_aux_t *pre_args, rb_node_opt_arg_t *opt_args, ID rest_arg, rb_node_args_aux_t *post_args, rb_node_args_t *tail, const YYLTYPE *loc)
14454{
14455 struct rb_args_info *args = &tail->nd_ainfo;
14456
14457 if (args->forwarding) {
14458 if (rest_arg) {
14459 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14460 return tail;
14461 }
14462 rest_arg = idFWD_REST;
14463 }
14464
14465 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14466 args->pre_init = pre_args ? pre_args->nd_next : 0;
14467
14468 args->post_args_num = post_args ? post_args->nd_plen : 0;
14469 args->post_init = post_args ? post_args->nd_next : 0;
14470 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14471
14472 args->rest_arg = rest_arg;
14473
14474 args->opt_args = opt_args;
14475
14476 nd_set_loc(RNODE(tail), loc);
14477
14478 return tail;
14479}
14480
14481static rb_node_args_t *
14482new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14483{
14484 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14485 struct rb_args_info *args = &node->nd_ainfo;
14486 if (p->error_p) return node;
14487
14488 args->block_arg = block;
14489 args->kw_args = kw_args;
14490
14491 if (kw_args) {
14492 /*
14493 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14494 * variable order: k1, kr1, k2, &b, internal_id, krest
14495 * #=> <reorder>
14496 * variable order: kr1, k1, k2, internal_id, krest, &b
14497 */
14498 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14499 struct vtable *vtargs = p->lvtbl->args;
14500 rb_node_kw_arg_t *kwn = kw_args;
14501
14502 if (block) block = vtargs->tbl[vtargs->pos-1];
14503 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14504 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14505 while (kwn) {
14506 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14507 --kw_vars;
14508 --required_kw_vars;
14509 kwn = kwn->nd_next;
14510 }
14511
14512 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14513 ID vid = get_nd_vid(p, kwn->nd_body);
14514 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14515 *required_kw_vars++ = vid;
14516 }
14517 else {
14518 *kw_vars++ = vid;
14519 }
14520 }
14521
14522 arg_var(p, kw_bits);
14523 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14524 if (block) arg_var(p, block);
14525
14526 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14527 }
14528 else if (kw_rest_arg == idNil) {
14529 args->no_kwarg = 1;
14530 }
14531 else if (kw_rest_arg) {
14532 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14533 }
14534
14535 return node;
14536}
14537
14538static rb_node_args_t *
14539args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14540{
14541 if (max_numparam > NO_PARAM || it_id) {
14542 if (!args) {
14543 YYLTYPE loc = RUBY_INIT_YYLLOC();
14544 args = new_args_tail(p, 0, 0, 0, 0);
14545 nd_set_loc(RNODE(args), &loc);
14546 }
14547 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14548 }
14549 return args;
14550}
14551
14552static NODE*
14553new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14554{
14555 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14556
14557 if (pre_arg) {
14558 NODE *pre_args = NEW_LIST(pre_arg, loc);
14559 if (RNODE_ARYPTN(aryptn)->pre_args) {
14560 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14561 }
14562 else {
14563 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14564 }
14565 }
14566 return aryptn;
14567}
14568
14569static NODE*
14570new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14571{
14572 if (has_rest) {
14573 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14574 }
14575 else {
14576 rest_arg = NULL;
14577 }
14578 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14579
14580 return node;
14581}
14582
14583static NODE*
14584new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14585{
14586 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14587
14588 return fndptn;
14589}
14590
14591static NODE*
14592new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14593{
14594 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14595 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14596 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14597
14598 return node;
14599}
14600
14601static NODE*
14602new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14603{
14604 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14605 return hshptn;
14606}
14607
14608static NODE*
14609new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14610{
14611 NODE *node, *kw_rest_arg_node;
14612
14613 if (kw_rest_arg == idNil) {
14614 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14615 }
14616 else if (kw_rest_arg) {
14617 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14618 }
14619 else {
14620 kw_rest_arg_node = NULL;
14621 }
14622
14623 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14624
14625 return node;
14626}
14627
14628static NODE*
14629dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14630{
14631 if (!node) {
14632 return NEW_SYM(STR_NEW0(), loc);
14633 }
14634
14635 switch (nd_type(node)) {
14636 case NODE_DSTR:
14637 nd_set_type(node, NODE_DSYM);
14638 nd_set_loc(node, loc);
14639 break;
14640 case NODE_STR:
14641 node = str_to_sym_node(p, node, loc);
14642 break;
14643 default:
14644 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14645 break;
14646 }
14647 return node;
14648}
14649
14650static int
14651nd_type_st_key_enable_p(NODE *node)
14652{
14653 switch (nd_type(node)) {
14654 case NODE_INTEGER:
14655 case NODE_FLOAT:
14656 case NODE_RATIONAL:
14657 case NODE_IMAGINARY:
14658 case NODE_STR:
14659 case NODE_SYM:
14660 case NODE_REGX:
14661 case NODE_LINE:
14662 case NODE_FILE:
14663 case NODE_ENCODING:
14664 return true;
14665 default:
14666 return false;
14667 }
14668}
14669
14670static VALUE
14671nd_value(struct parser_params *p, NODE *node)
14672{
14673 switch (nd_type(node)) {
14674 case NODE_STR:
14675 return rb_node_str_string_val(node);
14676 case NODE_INTEGER:
14677 return rb_node_integer_literal_val(node);
14678 case NODE_FLOAT:
14679 return rb_node_float_literal_val(node);
14680 case NODE_RATIONAL:
14681 return rb_node_rational_literal_val(node);
14682 case NODE_IMAGINARY:
14683 return rb_node_imaginary_literal_val(node);
14684 case NODE_SYM:
14685 return rb_node_sym_string_val(node);
14686 case NODE_REGX:
14687 return rb_node_regx_string_val(node);
14688 case NODE_LINE:
14689 return rb_node_line_lineno_val(node);
14690 case NODE_ENCODING:
14691 return rb_node_encoding_val(node);
14692 case NODE_FILE:
14693 return rb_node_file_path_val(node);
14694 default:
14695 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14696 UNREACHABLE_RETURN(0);
14697 }
14698}
14699
14700static void
14701warn_duplicate_keys(struct parser_params *p, NODE *hash)
14702{
14703 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14704 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14705 while (hash && RNODE_LIST(hash)->nd_next) {
14706 NODE *head = RNODE_LIST(hash)->nd_head;
14707 NODE *value = RNODE_LIST(hash)->nd_next;
14708 NODE *next = RNODE_LIST(value)->nd_next;
14709 st_data_t key;
14710 st_data_t data;
14711
14712 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14713 if (!head) {
14714 head = value;
14715 }
14716
14717 if (nd_type_st_key_enable_p(head)) {
14718 key = (st_data_t)head;
14719
14720 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14721 rb_warn2L(nd_line((NODE *)data),
14722 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14723 nd_value(p, head), WARN_I(nd_line(head)));
14724 }
14725 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14726 }
14727 hash = next;
14728 }
14729 st_free_table(p->warn_duplicate_keys_table);
14730 p->warn_duplicate_keys_table = NULL;
14731}
14732
14733static NODE *
14734new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14735{
14736 if (hash) warn_duplicate_keys(p, hash);
14737 return NEW_HASH(hash, loc);
14738}
14739
14740static void
14741error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14742{
14743 if (is_private_local_id(p, id)) {
14744 return;
14745 }
14746 if (st_is_member(p->pvtbl, id)) {
14747 yyerror1(loc, "duplicated variable name");
14748 }
14749 else if (p->ctxt.in_alt_pattern && id) {
14750 yyerror1(loc, "variable capture in alternative pattern");
14751 }
14752 else {
14753 p->ctxt.capture_in_pattern = 1;
14754 st_insert(p->pvtbl, (st_data_t)id, 0);
14755 }
14756}
14757
14758static void
14759error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14760{
14761 if (!p->pktbl) {
14762 p->pktbl = st_init_numtable();
14763 }
14764 else if (st_is_member(p->pktbl, key)) {
14765 yyerror1(loc, "duplicated key name");
14766 return;
14767 }
14768 st_insert(p->pktbl, (st_data_t)key, 0);
14769}
14770
14771static NODE *
14772new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14773{
14774 return NEW_HASH(hash, loc);
14775}
14776
14777static NODE *
14778new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14779{
14780 NODE *asgn;
14781
14782 if (lhs) {
14783 ID vid = get_nd_vid(p, lhs);
14784 YYLTYPE lhs_loc = lhs->nd_loc;
14785 if (op == tOROP) {
14786 set_nd_value(p, lhs, rhs);
14787 nd_set_loc(lhs, loc);
14788 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14789 }
14790 else if (op == tANDOP) {
14791 set_nd_value(p, lhs, rhs);
14792 nd_set_loc(lhs, loc);
14793 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14794 }
14795 else {
14796 asgn = lhs;
14797 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14798 set_nd_value(p, asgn, rhs);
14799 nd_set_loc(asgn, loc);
14800 }
14801 }
14802 else {
14803 asgn = NEW_ERROR(loc);
14804 }
14805 return asgn;
14806}
14807
14808static NODE *
14809new_ary_op_assign(struct parser_params *p, NODE *ary,
14810 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14811 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14812{
14813 NODE *asgn;
14814
14815 aryset_check(p, args);
14816 args = make_list(args, args_loc);
14817 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14818 fixpos(asgn, ary);
14819 return asgn;
14820}
14821
14822static NODE *
14823new_attr_op_assign(struct parser_params *p, NODE *lhs,
14824 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14825 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14826{
14827 NODE *asgn;
14828
14829 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14830 fixpos(asgn, lhs);
14831 return asgn;
14832}
14833
14834static NODE *
14835new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14836{
14837 NODE *asgn;
14838
14839 if (lhs) {
14840 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14841 }
14842 else {
14843 asgn = NEW_ERROR(loc);
14844 }
14845 fixpos(asgn, lhs);
14846 return asgn;
14847}
14848
14849static NODE *
14850const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14851{
14852 if (p->ctxt.in_def) {
14853#ifndef RIPPER
14854 yyerror1(loc, "dynamic constant assignment");
14855#else
14856 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14857#endif
14858 }
14859 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14860}
14861
14862#ifdef RIPPER
14863static VALUE
14864assign_error(struct parser_params *p, const char *mesg, VALUE a)
14865{
14866 a = dispatch2(assign_error, ERR_MESG(), a);
14867 ripper_error(p);
14868 return a;
14869}
14870#endif
14871
14872static NODE *
14873new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14874{
14875 NODE *result = head;
14876 if (rescue) {
14877 NODE *tmp = rescue_else ? rescue_else : rescue;
14878 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14879
14880 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14881 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14882 }
14883 if (ensure) {
14884 result = NEW_ENSURE(result, ensure, loc);
14885 }
14886 fixpos(result, head);
14887 return result;
14888}
14889
14890static void
14891warn_unused_var(struct parser_params *p, struct local_vars *local)
14892{
14893 int cnt;
14894
14895 if (!local->used) return;
14896 cnt = local->used->pos;
14897 if (cnt != local->vars->pos) {
14898 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14899 }
14900#ifndef RIPPER
14901 ID *v = local->vars->tbl;
14902 ID *u = local->used->tbl;
14903 for (int i = 0; i < cnt; ++i) {
14904 if (!v[i] || (u[i] & LVAR_USED)) continue;
14905 if (is_private_local_id(p, v[i])) continue;
14906 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14907 }
14908#endif
14909}
14910
14911static void
14912local_push(struct parser_params *p, int toplevel_scope)
14913{
14914 struct local_vars *local;
14915 int inherits_dvars = toplevel_scope && compile_for_eval;
14916 int warn_unused_vars = RTEST(ruby_verbose);
14917
14918 local = ALLOC(struct local_vars);
14919 local->prev = p->lvtbl;
14920 local->args = vtable_alloc(0);
14921 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14922#ifndef RIPPER
14923 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14924 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14925#endif
14926 local->numparam.outer = 0;
14927 local->numparam.inner = 0;
14928 local->numparam.current = 0;
14929 local->it = 0;
14930 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14931
14932# if WARN_PAST_SCOPE
14933 local->past = 0;
14934# endif
14935 CMDARG_PUSH(0);
14936 COND_PUSH(0);
14937 p->lvtbl = local;
14938}
14939
14940static void
14941vtable_chain_free(struct parser_params *p, struct vtable *table)
14942{
14943 while (!DVARS_TERMINAL_P(table)) {
14944 struct vtable *cur_table = table;
14945 table = cur_table->prev;
14946 vtable_free(cur_table);
14947 }
14948}
14949
14950static void
14951local_free(struct parser_params *p, struct local_vars *local)
14952{
14953 vtable_chain_free(p, local->used);
14954
14955# if WARN_PAST_SCOPE
14956 vtable_chain_free(p, local->past);
14957# endif
14958
14959 vtable_chain_free(p, local->args);
14960 vtable_chain_free(p, local->vars);
14961
14962 ruby_sized_xfree(local, sizeof(struct local_vars));
14963}
14964
14965static void
14966local_pop(struct parser_params *p)
14967{
14968 struct local_vars *local = p->lvtbl->prev;
14969 if (p->lvtbl->used) {
14970 warn_unused_var(p, p->lvtbl);
14971 }
14972
14973 local_free(p, p->lvtbl);
14974 p->lvtbl = local;
14975
14976 CMDARG_POP();
14977 COND_POP();
14978}
14979
14980static rb_ast_id_table_t *
14981local_tbl(struct parser_params *p)
14982{
14983 int cnt_args = vtable_size(p->lvtbl->args);
14984 int cnt_vars = vtable_size(p->lvtbl->vars);
14985 int cnt = cnt_args + cnt_vars;
14986 int i, j;
14987 rb_ast_id_table_t *tbl;
14988
14989 if (cnt <= 0) return 0;
14990 tbl = rb_ast_new_local_table(p->ast, cnt);
14991 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14992 /* remove IDs duplicated to warn shadowing */
14993 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14994 ID id = p->lvtbl->vars->tbl[i];
14995 if (!vtable_included(p->lvtbl->args, id)) {
14996 tbl->ids[j++] = id;
14997 }
14998 }
14999 if (j < cnt) {
15000 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15001 }
15002
15003 return tbl;
15004}
15005
15006static void
15007numparam_name(struct parser_params *p, ID id)
15008{
15009 if (!NUMPARAM_ID_P(id)) return;
15010 compile_error(p, "_%d is reserved for numbered parameter",
15011 NUMPARAM_ID_TO_IDX(id));
15012}
15013
15014static void
15015arg_var(struct parser_params *p, ID id)
15016{
15017 numparam_name(p, id);
15018 vtable_add(p->lvtbl->args, id);
15019}
15020
15021static void
15022local_var(struct parser_params *p, ID id)
15023{
15024 numparam_name(p, id);
15025 vtable_add(p->lvtbl->vars, id);
15026 if (p->lvtbl->used) {
15027 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15028 }
15029}
15030
15031#ifndef RIPPER
15032int
15033rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15034{
15035 return rb_local_defined(id, iseq);
15036}
15037#endif
15038
15039static int
15040local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15041{
15042 struct vtable *vars, *args, *used;
15043
15044 vars = p->lvtbl->vars;
15045 args = p->lvtbl->args;
15046 used = p->lvtbl->used;
15047
15048 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15049 vars = vars->prev;
15050 args = args->prev;
15051 if (used) used = used->prev;
15052 }
15053
15054 if (vars && vars->prev == DVARS_INHERIT) {
15055 return rb_parser_local_defined(p, id, p->parent_iseq);
15056 }
15057 else if (vtable_included(args, id)) {
15058 return 1;
15059 }
15060 else {
15061 int i = vtable_included(vars, id);
15062 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15063 return i != 0;
15064 }
15065}
15066
15067static int
15068local_id(struct parser_params *p, ID id)
15069{
15070 return local_id_ref(p, id, NULL);
15071}
15072
15073static int
15074check_forwarding_args(struct parser_params *p)
15075{
15076 if (local_id(p, idFWD_ALL)) return TRUE;
15077 compile_error(p, "unexpected ...");
15078 return FALSE;
15079}
15080
15081static void
15082add_forwarding_args(struct parser_params *p)
15083{
15084 arg_var(p, idFWD_REST);
15085 arg_var(p, idFWD_KWREST);
15086 arg_var(p, idFWD_BLOCK);
15087 arg_var(p, idFWD_ALL);
15088}
15089
15090static void
15091forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15092{
15093 bool conflict = false;
15094
15095 struct vtable *vars, *args;
15096
15097 vars = p->lvtbl->vars;
15098 args = p->lvtbl->args;
15099
15100 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15101 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15102 vars = vars->prev;
15103 args = args->prev;
15104 }
15105
15106 bool found = false;
15107 if (vars && vars->prev == DVARS_INHERIT && !found) {
15108 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15109 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15110 }
15111 else {
15112 found = (vtable_included(args, arg) &&
15113 !(all && vtable_included(args, all)));
15114 }
15115
15116 if (!found) {
15117 compile_error(p, "no anonymous %s parameter", var);
15118 }
15119 else if (conflict) {
15120 compile_error(p, "anonymous %s parameter is also used within block", var);
15121 }
15122}
15123
15124static NODE *
15125new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15126{
15127 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15128 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15129 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15130 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15131 block->forwarding = TRUE;
15132 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15133 return arg_blk_pass(args, block);
15134}
15135
15136static NODE *
15137numparam_push(struct parser_params *p)
15138{
15139 struct local_vars *local = p->lvtbl;
15140 NODE *inner = local->numparam.inner;
15141 if (!local->numparam.outer) {
15142 local->numparam.outer = local->numparam.current;
15143 }
15144 local->numparam.inner = 0;
15145 local->numparam.current = 0;
15146 local->it = 0;
15147 return inner;
15148}
15149
15150static void
15151numparam_pop(struct parser_params *p, NODE *prev_inner)
15152{
15153 struct local_vars *local = p->lvtbl;
15154 if (prev_inner) {
15155 /* prefer first one */
15156 local->numparam.inner = prev_inner;
15157 }
15158 else if (local->numparam.current) {
15159 /* current and inner are exclusive */
15160 local->numparam.inner = local->numparam.current;
15161 }
15162 if (p->max_numparam > NO_PARAM) {
15163 /* current and outer are exclusive */
15164 local->numparam.current = local->numparam.outer;
15165 local->numparam.outer = 0;
15166 }
15167 else {
15168 /* no numbered parameter */
15169 local->numparam.current = 0;
15170 }
15171 local->it = 0;
15172}
15173
15174static const struct vtable *
15175dyna_push(struct parser_params *p)
15176{
15177 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15178 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15179 if (p->lvtbl->used) {
15180 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15181 }
15182 return p->lvtbl->args;
15183}
15184
15185static void
15186dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15187{
15188 struct vtable *tmp = *vtblp;
15189 *vtblp = tmp->prev;
15190# if WARN_PAST_SCOPE
15191 if (p->past_scope_enabled) {
15192 tmp->prev = p->lvtbl->past;
15193 p->lvtbl->past = tmp;
15194 return;
15195 }
15196# endif
15197 vtable_free(tmp);
15198}
15199
15200static void
15201dyna_pop_1(struct parser_params *p)
15202{
15203 struct vtable *tmp;
15204
15205 if ((tmp = p->lvtbl->used) != 0) {
15206 warn_unused_var(p, p->lvtbl);
15207 p->lvtbl->used = p->lvtbl->used->prev;
15208 vtable_free(tmp);
15209 }
15210 dyna_pop_vtable(p, &p->lvtbl->args);
15211 dyna_pop_vtable(p, &p->lvtbl->vars);
15212}
15213
15214static void
15215dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15216{
15217 while (p->lvtbl->args != lvargs) {
15218 dyna_pop_1(p);
15219 if (!p->lvtbl->args) {
15220 struct local_vars *local = p->lvtbl->prev;
15221 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15222 p->lvtbl = local;
15223 }
15224 }
15225 dyna_pop_1(p);
15226}
15227
15228static int
15229dyna_in_block(struct parser_params *p)
15230{
15231 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15232}
15233
15234#ifndef RIPPER
15235int
15236dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15237{
15238 struct vtable *vars, *args, *used;
15239 int i;
15240
15241 args = p->lvtbl->args;
15242 vars = p->lvtbl->vars;
15243 used = p->lvtbl->used;
15244
15245 while (!DVARS_TERMINAL_P(vars)) {
15246 if (vtable_included(args, id)) {
15247 return 1;
15248 }
15249 if ((i = vtable_included(vars, id)) != 0) {
15250 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15251 return 1;
15252 }
15253 args = args->prev;
15254 vars = vars->prev;
15255 if (!vidrefp) used = 0;
15256 if (used) used = used->prev;
15257 }
15258
15259 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15260 return rb_dvar_defined(id, p->parent_iseq);
15261 }
15262
15263 return 0;
15264}
15265#endif
15266
15267static int
15268dvar_defined(struct parser_params *p, ID id)
15269{
15270 return dvar_defined_ref(p, id, NULL);
15271}
15272
15273static int
15274dvar_curr(struct parser_params *p, ID id)
15275{
15276 return (vtable_included(p->lvtbl->args, id) ||
15277 vtable_included(p->lvtbl->vars, id));
15278}
15279
15280static void
15281reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15282{
15283 compile_error(p,
15284 "regexp encoding option '%c' differs from source encoding '%s'",
15285 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15286}
15287
15288#ifndef RIPPER
15289static rb_encoding *
15290find_enc(struct parser_params* p, const char *name)
15291{
15292 int idx = rb_enc_find_index(name);
15293 if (idx < 0) {
15294 rb_bug("unknown encoding name: %s", name);
15295 }
15296
15297 return rb_enc_from_index(idx);
15298}
15299
15300static rb_encoding *
15301kcode_to_enc(struct parser_params* p, int kcode)
15302{
15303 rb_encoding *enc;
15304
15305 switch (kcode) {
15306 case ENC_ASCII8BIT:
15307 enc = rb_ascii8bit_encoding();
15308 break;
15309 case ENC_EUC_JP:
15310 enc = find_enc(p, "EUC-JP");
15311 break;
15312 case ENC_Windows_31J:
15313 enc = find_enc(p, "Windows-31J");
15314 break;
15315 case ENC_UTF8:
15316 enc = rb_utf8_encoding();
15317 break;
15318 default:
15319 enc = NULL;
15320 break;
15321 }
15322
15323 return enc;
15324}
15325
15326int
15327rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15328{
15329 int c = RE_OPTION_ENCODING_IDX(options);
15330
15331 if (c) {
15332 int opt, idx;
15333 rb_encoding *enc;
15334
15335 char_to_option_kcode(c, &opt, &idx);
15336 enc = kcode_to_enc(p, idx);
15337 if (enc != rb_parser_str_get_encoding(str) &&
15338 !rb_parser_is_ascii_string(p, str)) {
15339 goto error;
15340 }
15341 rb_parser_string_set_encoding(str, enc);
15342 }
15343 else if (RE_OPTION_ENCODING_NONE(options)) {
15344 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15345 !rb_parser_is_ascii_string(p, str)) {
15346 c = 'n';
15347 goto error;
15348 }
15349 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15350 }
15351 else if (rb_is_usascii_enc(p->enc)) {
15352 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15353 }
15354 return 0;
15355
15356 error:
15357 return c;
15358}
15359#endif
15360
15361static void
15362reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15363{
15364 int c = rb_reg_fragment_setenc(p, str, options);
15365 if (c) reg_fragment_enc_error(p, str, c);
15366}
15367
15368#ifndef UNIVERSAL_PARSER
15369typedef struct {
15370 struct parser_params* parser;
15371 rb_encoding *enc;
15372 NODE *succ_block;
15373 const YYLTYPE *loc;
15374 rb_parser_assignable_func assignable;
15375} reg_named_capture_assign_t;
15376
15377static int
15378reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15379 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15380{
15381 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15382 struct parser_params* p = arg->parser;
15383 rb_encoding *enc = arg->enc;
15384 long len = name_end - name;
15385 const char *s = (const char *)name;
15386
15387 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15388}
15389
15390static NODE *
15391reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15392{
15393 reg_named_capture_assign_t arg;
15394
15395 arg.parser = p;
15396 arg.enc = rb_enc_get(regexp);
15397 arg.succ_block = 0;
15398 arg.loc = loc;
15399 arg.assignable = assignable;
15400 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15401
15402 if (!arg.succ_block) return 0;
15403 return RNODE_BLOCK(arg.succ_block)->nd_next;
15404}
15405#endif
15406
15407#ifndef RIPPER
15408NODE *
15409rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15410{
15411 return assignable(p, id, val, loc);
15412}
15413
15414int
15415rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15416 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15417{
15418 ID var;
15419 NODE *node, *succ;
15420
15421 if (!len) return ST_CONTINUE;
15422 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15423 return ST_CONTINUE;
15424
15425 var = intern_cstr(s, len, enc);
15426 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15427 if (!lvar_defined(p, var)) return ST_CONTINUE;
15428 }
15429 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15430 succ = *succ_block;
15431 if (!succ) succ = NEW_ERROR(loc);
15432 succ = block_append(p, succ, node);
15433 *succ_block = succ;
15434 return ST_CONTINUE;
15435}
15436#endif
15437
15438static VALUE
15439parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15440{
15441 VALUE str2;
15442 reg_fragment_setenc(p, str, options);
15443 str2 = rb_str_new_parser_string(str);
15444 return rb_parser_reg_compile(p, str2, options);
15445}
15446
15447#ifndef RIPPER
15448VALUE
15449rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15450{
15451 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15452}
15453#endif
15454
15455static VALUE
15456reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15457{
15458 VALUE re;
15459 VALUE err;
15460
15461 err = rb_errinfo();
15462 re = parser_reg_compile(p, str, options);
15463 if (NIL_P(re)) {
15464 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15465 rb_set_errinfo(err);
15466 compile_error(p, "%"PRIsVALUE, m);
15467 return Qnil;
15468 }
15469 return re;
15470}
15471
15472#ifndef RIPPER
15473void
15474rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15475{
15476 p->do_print = print;
15477 p->do_loop = loop;
15478 p->do_chomp = chomp;
15479 p->do_split = split;
15480}
15481
15482static NODE *
15483parser_append_options(struct parser_params *p, NODE *node)
15484{
15485 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15486 const YYLTYPE *const LOC = &default_location;
15487
15488 if (p->do_print) {
15489 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15490 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15491 LOC);
15492 node = block_append(p, node, print);
15493 }
15494
15495 if (p->do_loop) {
15496 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15497
15498 if (p->do_split) {
15499 ID ifs = rb_intern("$;");
15500 ID fields = rb_intern("$F");
15501 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15502 NODE *split = NEW_GASGN(fields,
15503 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15504 rb_intern("split"), args, LOC),
15505 LOC);
15506 node = block_append(p, split, node);
15507 }
15508 if (p->do_chomp) {
15509 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15510 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15511 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15512 }
15513
15514 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15515 }
15516
15517 return node;
15518}
15519
15520void
15521rb_init_parse(void)
15522{
15523 /* just to suppress unused-function warnings */
15524 (void)nodetype;
15525 (void)nodeline;
15526}
15527
15528ID
15529internal_id(struct parser_params *p)
15530{
15531 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15532}
15533#endif /* !RIPPER */
15534
15535static void
15536parser_initialize(struct parser_params *p)
15537{
15538 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15539 p->command_start = TRUE;
15540 p->ruby_sourcefile_string = Qnil;
15541 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15542 string_buffer_init(p);
15543 p->node_id = 0;
15544 p->delayed.token = NULL;
15545 p->frozen_string_literal = -1; /* not specified */
15546#ifndef RIPPER
15547 p->error_buffer = Qfalse;
15548 p->end_expect_token_locations = NULL;
15549 p->token_id = 0;
15550 p->tokens = NULL;
15551#else
15552 p->result = Qnil;
15553 p->parsing_thread = Qnil;
15554 p->s_value = Qnil;
15555 p->s_lvalue = Qnil;
15556 p->s_value_stack = rb_ary_new();
15557#endif
15558 p->debug_buffer = Qnil;
15559 p->debug_output = rb_ractor_stdout();
15560 p->enc = rb_utf8_encoding();
15561 p->exits = 0;
15562}
15563
15564#ifdef RIPPER
15565#define rb_ruby_parser_mark ripper_parser_mark
15566#define rb_ruby_parser_free ripper_parser_free
15567#define rb_ruby_parser_memsize ripper_parser_memsize
15568#endif
15569
15570void
15571rb_ruby_parser_mark(void *ptr)
15572{
15573 struct parser_params *p = (struct parser_params*)ptr;
15574
15575 rb_gc_mark(p->ruby_sourcefile_string);
15576#ifndef RIPPER
15577 rb_gc_mark(p->error_buffer);
15578#else
15579 rb_gc_mark(p->value);
15580 rb_gc_mark(p->result);
15581 rb_gc_mark(p->parsing_thread);
15582 rb_gc_mark(p->s_value);
15583 rb_gc_mark(p->s_lvalue);
15584 rb_gc_mark(p->s_value_stack);
15585#endif
15586 rb_gc_mark(p->debug_buffer);
15587 rb_gc_mark(p->debug_output);
15588}
15589
15590void
15591rb_ruby_parser_free(void *ptr)
15592{
15593 struct parser_params *p = (struct parser_params*)ptr;
15594 struct local_vars *local, *prev;
15595
15596 if (p->ast) {
15597 rb_ast_free(p->ast);
15598 }
15599
15600 if (p->warn_duplicate_keys_table) {
15601 st_free_table(p->warn_duplicate_keys_table);
15602 }
15603
15604#ifndef RIPPER
15605 if (p->tokens) {
15606 rb_parser_ary_free(p, p->tokens);
15607 }
15608#endif
15609
15610 if (p->tokenbuf) {
15611 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15612 }
15613
15614 for (local = p->lvtbl; local; local = prev) {
15615 prev = local->prev;
15616 local_free(p, local);
15617 }
15618
15619 {
15620 token_info *ptinfo;
15621 while ((ptinfo = p->token_info) != 0) {
15622 p->token_info = ptinfo->next;
15623 xfree(ptinfo);
15624 }
15625 }
15626 string_buffer_free(p);
15627
15628 if (p->pvtbl) {
15629 st_free_table(p->pvtbl);
15630 }
15631
15632 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15633 st_free_table(p->case_labels);
15634 }
15635
15636 xfree(p->lex.strterm);
15637 p->lex.strterm = 0;
15638
15639 xfree(ptr);
15640}
15641
15642size_t
15643rb_ruby_parser_memsize(const void *ptr)
15644{
15645 struct parser_params *p = (struct parser_params*)ptr;
15646 struct local_vars *local;
15647 size_t size = sizeof(*p);
15648
15649 size += p->toksiz;
15650 for (local = p->lvtbl; local; local = local->prev) {
15651 size += sizeof(*local);
15652 if (local->vars) size += local->vars->capa * sizeof(ID);
15653 }
15654 return size;
15655}
15656
15657#ifndef RIPPER
15658#undef rb_reserved_word
15659
15660const struct kwtable *
15661rb_reserved_word(const char *str, unsigned int len)
15662{
15663 return reserved_word(str, len);
15664}
15665
15666#ifdef UNIVERSAL_PARSER
15667rb_parser_t *
15668rb_ruby_parser_allocate(const rb_parser_config_t *config)
15669{
15670 /* parser_initialize expects fields to be set to 0 */
15671 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15672 p->config = config;
15673 return p;
15674}
15675
15676rb_parser_t *
15677rb_ruby_parser_new(const rb_parser_config_t *config)
15678{
15679 /* parser_initialize expects fields to be set to 0 */
15680 rb_parser_t *p = rb_ruby_parser_allocate(config);
15681 parser_initialize(p);
15682 return p;
15683}
15684#else
15685rb_parser_t *
15686rb_ruby_parser_allocate(void)
15687{
15688 /* parser_initialize expects fields to be set to 0 */
15689 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15690 return p;
15691}
15692
15693rb_parser_t *
15694rb_ruby_parser_new(void)
15695{
15696 /* parser_initialize expects fields to be set to 0 */
15697 rb_parser_t *p = rb_ruby_parser_allocate();
15698 parser_initialize(p);
15699 return p;
15700}
15701#endif
15702
15703rb_parser_t *
15704rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15705{
15706 p->error_buffer = main ? Qfalse : Qnil;
15707 p->parent_iseq = base;
15708 return p;
15709}
15710
15711void
15712rb_ruby_parser_set_script_lines(rb_parser_t *p)
15713{
15714 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15715}
15716
15717void
15718rb_ruby_parser_error_tolerant(rb_parser_t *p)
15719{
15720 p->error_tolerant = 1;
15721}
15722
15723void
15724rb_ruby_parser_keep_tokens(rb_parser_t *p)
15725{
15726 p->keep_tokens = 1;
15727 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15728}
15729
15730rb_encoding *
15731rb_ruby_parser_encoding(rb_parser_t *p)
15732{
15733 return p->enc;
15734}
15735
15736int
15737rb_ruby_parser_end_seen_p(rb_parser_t *p)
15738{
15739 return p->ruby__end__seen;
15740}
15741
15742int
15743rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15744{
15745 p->debug = flag;
15746 return flag;
15747}
15748#endif /* !RIPPER */
15749
15750#ifdef RIPPER
15751int
15752rb_ruby_parser_get_yydebug(rb_parser_t *p)
15753{
15754 return p->debug;
15755}
15756
15757void
15758rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15759{
15760 p->value = value;
15761}
15762
15763int
15764rb_ruby_parser_error_p(rb_parser_t *p)
15765{
15766 return p->error_p;
15767}
15768
15769VALUE
15770rb_ruby_parser_debug_output(rb_parser_t *p)
15771{
15772 return p->debug_output;
15773}
15774
15775void
15776rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15777{
15778 p->debug_output = output;
15779}
15780
15781VALUE
15782rb_ruby_parser_parsing_thread(rb_parser_t *p)
15783{
15784 return p->parsing_thread;
15785}
15786
15787void
15788rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15789{
15790 p->parsing_thread = parsing_thread;
15791}
15792
15793void
15794rb_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)
15795{
15796 p->lex.gets = gets;
15797 p->lex.input = input;
15798 p->eofp = 0;
15799 p->ruby_sourcefile_string = sourcefile_string;
15800 p->ruby_sourcefile = sourcefile;
15801 p->ruby_sourceline = sourceline;
15802}
15803
15804VALUE
15805rb_ruby_parser_result(rb_parser_t *p)
15806{
15807 return p->result;
15808}
15809
15810rb_encoding *
15811rb_ruby_parser_enc(rb_parser_t *p)
15812{
15813 return p->enc;
15814}
15815
15816VALUE
15817rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15818{
15819 return p->ruby_sourcefile_string;
15820}
15821
15822int
15823rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15824{
15825 return p->ruby_sourceline;
15826}
15827
15828int
15829rb_ruby_parser_lex_state(rb_parser_t *p)
15830{
15831 return p->lex.state;
15832}
15833
15834void
15835rb_ruby_ripper_parse0(rb_parser_t *p)
15836{
15837 parser_prepare(p);
15838 p->ast = rb_ast_new();
15839 ripper_yyparse((void*)p);
15840 rb_ast_free(p->ast);
15841 p->ast = 0;
15842 p->eval_tree = 0;
15843 p->eval_tree_begin = 0;
15844}
15845
15846int
15847rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15848{
15849 return dedent_string(p, string, width);
15850}
15851
15852int
15853rb_ruby_ripper_initialized_p(rb_parser_t *p)
15854{
15855 return p->lex.input != 0;
15856}
15857
15858void
15859rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15860{
15861 parser_initialize(p);
15862}
15863
15864long
15865rb_ruby_ripper_column(rb_parser_t *p)
15866{
15867 return p->lex.ptok - p->lex.pbeg;
15868}
15869
15870long
15871rb_ruby_ripper_token_len(rb_parser_t *p)
15872{
15873 return p->lex.pcur - p->lex.ptok;
15874}
15875
15876rb_parser_string_t *
15877rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15878{
15879 return p->lex.lastline;
15880}
15881
15882VALUE
15883rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15884{
15885 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15886}
15887
15888#ifdef UNIVERSAL_PARSER
15889rb_parser_t *
15890rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15891{
15892 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15893 p->config = config;
15894 return p;
15895}
15896#endif
15897
15898struct parser_params*
15899rb_ruby_ripper_parser_allocate(void)
15900{
15901 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15902}
15903#endif /* RIPPER */
15904
15905#ifndef RIPPER
15906void
15907rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15908{
15909 va_list ap;
15910 VALUE mesg = p->debug_buffer;
15911
15912 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15913 va_start(ap, fmt);
15914 rb_str_vcatf(mesg, fmt, ap);
15915 va_end(ap);
15916 if (char_at_end(p, mesg, 0) == '\n') {
15917 rb_io_write(p->debug_output, mesg);
15918 p->debug_buffer = Qnil;
15919 }
15920}
15921
15922static void
15923parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15924{
15925 va_list ap;
15926 int lineno, column;
15927
15928 if (loc) {
15929 lineno = loc->end_pos.lineno;
15930 column = loc->end_pos.column;
15931 }
15932 else {
15933 lineno = p->ruby_sourceline;
15934 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15935 }
15936
15937 rb_io_flush(p->debug_output);
15938 p->error_p = 1;
15939 va_start(ap, fmt);
15940 p->error_buffer =
15941 rb_syntax_error_append(p->error_buffer,
15942 p->ruby_sourcefile_string,
15943 lineno, column,
15944 p->enc, fmt, ap);
15945 va_end(ap);
15946}
15947
15948static size_t
15949count_char(const char *str, int c)
15950{
15951 int n = 0;
15952 while (str[n] == c) ++n;
15953 return n;
15954}
15955
15956/*
15957 * strip enclosing double-quotes, same as the default yytnamerr except
15958 * for that single-quotes matching back-quotes do not stop stripping.
15959 *
15960 * "\"`class' keyword\"" => "`class' keyword"
15961 */
15962size_t
15963rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15964{
15965 if (*yystr == '"') {
15966 size_t yyn = 0, bquote = 0;
15967 const char *yyp = yystr;
15968
15969 while (*++yyp) {
15970 switch (*yyp) {
15971 case '\'':
15972 if (!bquote) {
15973 bquote = count_char(yyp+1, '\'') + 1;
15974 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15975 yyn += bquote;
15976 yyp += bquote - 1;
15977 break;
15978 }
15979 else {
15980 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15981 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15982 yyn += bquote;
15983 yyp += bquote - 1;
15984 bquote = 0;
15985 break;
15986 }
15987 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15988 if (yyres) memcpy(yyres + yyn, yyp, 3);
15989 yyn += 3;
15990 yyp += 2;
15991 break;
15992 }
15993 goto do_not_strip_quotes;
15994 }
15995
15996 case ',':
15997 goto do_not_strip_quotes;
15998
15999 case '\\':
16000 if (*++yyp != '\\')
16001 goto do_not_strip_quotes;
16002 /* Fall through. */
16003 default:
16004 if (yyres)
16005 yyres[yyn] = *yyp;
16006 yyn++;
16007 break;
16008
16009 case '"':
16010 case '\0':
16011 if (yyres)
16012 yyres[yyn] = '\0';
16013 return yyn;
16014 }
16015 }
16016 do_not_strip_quotes: ;
16017 }
16018
16019 if (!yyres) return strlen(yystr);
16020
16021 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16022}
16023#endif
16024
16025#ifdef RIPPER
16026#define validate(x) (void)(x)
16027
16028static VALUE
16029ripper_dispatch0(struct parser_params *p, ID mid)
16030{
16031 return rb_funcall(p->value, mid, 0);
16032}
16033
16034static VALUE
16035ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16036{
16037 validate(a);
16038 return rb_funcall(p->value, mid, 1, a);
16039}
16040
16041static VALUE
16042ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16043{
16044 validate(a);
16045 validate(b);
16046 return rb_funcall(p->value, mid, 2, a, b);
16047}
16048
16049static VALUE
16050ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16051{
16052 validate(a);
16053 validate(b);
16054 validate(c);
16055 return rb_funcall(p->value, mid, 3, a, b, c);
16056}
16057
16058static VALUE
16059ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16060{
16061 validate(a);
16062 validate(b);
16063 validate(c);
16064 validate(d);
16065 return rb_funcall(p->value, mid, 4, a, b, c, d);
16066}
16067
16068static VALUE
16069ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16070{
16071 validate(a);
16072 validate(b);
16073 validate(c);
16074 validate(d);
16075 validate(e);
16076 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16077}
16078
16079static VALUE
16080ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16081{
16082 validate(a);
16083 validate(b);
16084 validate(c);
16085 validate(d);
16086 validate(e);
16087 validate(f);
16088 validate(g);
16089 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16090}
16091
16092void
16093ripper_error(struct parser_params *p)
16094{
16095 p->error_p = TRUE;
16096}
16097
16098VALUE
16099ripper_value(struct parser_params *p)
16100{
16101 (void)yystpcpy; /* may not used in newer bison */
16102
16103 return p->value;
16104}
16105
16106#endif /* RIPPER */
16107/*
16108 * Local variables:
16109 * mode: c
16110 * c-file-style: "ruby"
16111 * End:
16112 */