Ruby 4.1.0dev (2026-03-12 revision 235195f80b17243f348344bb698564386131068e)
parse.y (235195f80b17243f348344bb698564386131068e)
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;}
1430
1431static bool args_info_empty_p(struct rb_args_info *args);
1432static 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*);
1433static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1434#define new_empty_args_tail(p, loc) new_args_tail(p, 0, 0, 0, loc)
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*);
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 REALLOC_N(str->ptr, char, (size_t)total + termlen); \
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 block_args-opt_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 f_empty_arg
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 f_largs largs_tail
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!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
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!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/
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!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
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!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
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!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
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!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
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!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
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!($:backref), $:tOP_ASGN, $:rhs)) %*/
3087 }
3088 ;
3089
3090%rule opt_args_tail(tail) <node_args>
3091 : ',' tail
3092 {
3093 $$ = $tail;
3094 /*% ripper: $:tail %*/
3095 }
3096 | /* none */
3097 {
3098 $$ = new_empty_args_tail(p, &@$);
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!($:word_list) %*/
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($compstmt, &@$));
3223 $$ = NEW_BEGIN(0, &@$);
3224 /*% ripper: BEGIN!($:compstmt) %*/
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[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3296 {
3297 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3298 /*% ripper: alias!($:new, $:old) %*/
3299 }
3300 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3301 {
3302 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3303 /*% ripper: var_alias!($:new, $:old) %*/
3304 }
3305 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
3306 {
3307 char buf[2];
3308 buf[0] = '$';
3309 buf[1] = (char)RNODE_BACK_REF($old)->nd_nth;
3310 $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw);
3311 /*% ripper: var_alias!($:new, $:old) %*/
3312 }
3313 | keyword_alias tGVAR tNTH_REF[nth]
3314 {
3315 static const char mesg[] = "can't make alias for the number variables";
3316 /*%%%*/
3317 yyerror1(&@nth, mesg);
3318 /*% %*/
3319 $$ = NEW_ERROR(&@$);
3320 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3321 }
3322 | keyword_undef[kw] undef_list[list]
3323 {
3324 nd_set_first_loc($list, @kw.beg_pos);
3325 RNODE_UNDEF($list)->keyword_loc = @kw;
3326 $$ = $list;
3327 /*% ripper: undef!($:list) %*/
3328 }
3329 | stmt[body] modifier_if[mod] expr_value[cond]
3330 {
3331 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3332 fixpos($$, $cond);
3333 /*% ripper: if_mod!($:cond, $:body) %*/
3334 }
3335 | stmt[body] modifier_unless[mod] expr_value[cond]
3336 {
3337 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3338 fixpos($$, $cond);
3339 /*% ripper: unless_mod!($:cond, $:body) %*/
3340 }
3341 | stmt[body] modifier_while[mod] expr_value[cond_expr]
3342 {
3343 clear_block_exit(p, false);
3344 if ($body && nd_type_p($body, NODE_BEGIN)) {
3345 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3346 }
3347 else {
3348 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3349 }
3350 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3351 }
3352 | stmt[body] modifier_until[mod] expr_value[cond_expr]
3353 {
3354 clear_block_exit(p, false);
3355 if ($body && nd_type_p($body, NODE_BEGIN)) {
3356 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3357 }
3358 else {
3359 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3360 }
3361 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3362 }
3363 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3364 {
3365 p->ctxt.in_rescue = $ctxt.in_rescue;
3366 NODE *resq;
3367 YYLTYPE loc = code_loc_gen(&@mod, &@resbody);
3368 resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3369 $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$);
3370 /*% ripper: rescue_mod!($:body, $:resbody) %*/
3371 }
3372 | k_END[k_end] allow_exits[allow] '{'[lbrace] compstmt(stmts)[body] '}'[rbrace]
3373 {
3374 if (p->ctxt.in_def) {
3375 rb_warn0("END in method; use at_exit");
3376 }
3377 restore_block_exit(p, $allow);
3378 p->ctxt = $k_end;
3379 {
3380 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$);
3381 $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace);
3382 RNODE_SCOPE(scope)->nd_parent = $$;
3383 }
3384 /*% ripper: END!($:body) %*/
3385 }
3386 | command_asgn
3387 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3388 {
3389 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3390 /*% ripper: massign!($:lhs, $:rhs) %*/
3391 }
3392 | asgn(mrhs)
3393 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3394 after_rescue[after_rescue] stmt[resbody]
3395 {
3396 p->ctxt.in_rescue = $after_rescue.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 *)$lhs, $mrhs_arg, $lex_ctxt, &@$);
3402 /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/
3403 }
3404 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3405 {
3406 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3407 /*% ripper: massign!($:lhs, $:rhs) %*/
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[left] keyword_and[op] expr[right]
3449 {
3450 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3451 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3452 }
3453 | expr[left] keyword_or[op] expr[right]
3454 {
3455 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3456 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3457 }
3458 | keyword_not[not] '\n'? expr[arg]
3459 {
3460 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3461 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3462 }
3463 | '!'[not] command_call[arg]
3464 {
3465 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3466 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3467 }
3468 | arg tASSOC[assoc]
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, &@assoc), &@$, &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[fid]
4371 {
4372 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4373 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
4374 }
4375 | k_begin[kw]
4376 {
4377 CMDARG_PUSH(0);
4378 }
4379 bodystmt[body]
4380 k_end[k_end]
4381 {
4382 CMDARG_POP();
4383 set_line_body($body, @kw.end_pos.lineno);
4384 $$ = NEW_BEGIN($body, &@$);
4385 nd_set_line($$, @kw.end_pos.lineno);
4386 /*% ripper: begin!($:body) %*/
4387 }
4388 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4389 {
4390 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4391 $$ = $body;
4392 /*% ripper: paren!($:body) %*/
4393 }
4394 | tLPAREN compstmt(stmts)[body] ')'
4395 {
4396 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4397 $$ = NEW_BLOCK($body, &@$);
4398 /*% ripper: paren!($:body) %*/
4399 }
4400 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4401 {
4402 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4403 /*% ripper: const_path_ref!($:recv, $:name) %*/
4404 }
4405 | tCOLON3[top] tCONSTANT[name]
4406 {
4407 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4408 /*% ripper: top_const_ref!($:name) %*/
4409 }
4410 | tLBRACK aref_args[args] ']'
4411 {
4412 $$ = make_list($args, &@$);
4413 /*% ripper: array!($:args) %*/
4414 }
4415 | tLBRACE assoc_list[list] '}'
4416 {
4417 $$ = new_hash(p, $list, &@$);
4418 RNODE_HASH($$)->nd_brace = TRUE;
4419 /*% ripper: hash!($:list) %*/
4420 }
4421 | k_return[kw]
4422 {
4423 $$ = NEW_RETURN(0, &@$, &@kw);
4424 /*% ripper: return0! %*/
4425 }
4426 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4427 {
4428 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4429 /*% ripper: yield!(paren!($:args)) %*/
4430 }
4431 | k_yield[kw] '('[lpar] rparen[rpar]
4432 {
4433 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4434 /*% ripper: yield!(paren!(args_new!)) %*/
4435 }
4436 | k_yield[kw]
4437 {
4438 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4439 /*% ripper: yield0! %*/
4440 }
4441 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
4442 {
4443 p->ctxt.in_defined = $ctxt.in_defined;
4444 $$ = new_defined(p, $arg, &@$, &@kw);
4445 p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon;
4446 /*% ripper: defined!($:arg) %*/
4447 }
4448 | keyword_not[kw] '(' expr[arg] rparen
4449 {
4450 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4451 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4452 }
4453 | keyword_not[kw] '('[lpar] rparen
4454 {
4455 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4456 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4457 }
4458 | fcall[call] brace_block[block]
4459 {
4460 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4461 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4462 }
4463 | method_call
4464 | method_call[call] brace_block[block]
4465 {
4466 block_dup_check(p, get_nd_args(p, $call), $block);
4467 $$ = method_add_block(p, $call, $block, &@$);
4468 /*% ripper: method_add_block!($:call, $:block) %*/
4469 }
4470 | lambda
4471 | k_if[kw] expr_value[cond] then[then]
4472 compstmt(stmts)[body]
4473 if_tail[tail]
4474 k_end[k_end]
4475 {
4476 if ($tail && nd_type_p($tail, NODE_IF))
4477 RNODE_IF($tail)->end_keyword_loc = @k_end;
4478
4479 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4480 fixpos($$, $cond);
4481 /*% ripper: if!($:cond, $:body, $:tail) %*/
4482 }
4483 | k_unless[kw] expr_value[cond] then[then]
4484 compstmt(stmts)[body]
4485 opt_else[tail]
4486 k_end[k_end]
4487 {
4488 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4489 fixpos($$, $cond);
4490 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4491 }
4492 | k_while[kw] expr_value_do[cond]
4493 compstmt(stmts)[body]
4494 k_end[k_end]
4495 {
4496 restore_block_exit(p, $kw);
4497 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4498 fixpos($$, $cond);
4499 /*% ripper: while!($:cond, $:body) %*/
4500 }
4501 | k_until[kw] expr_value_do[cond]
4502 compstmt(stmts)[body]
4503 k_end[k_end]
4504 {
4505 restore_block_exit(p, $kw);
4506 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4507 fixpos($$, $cond);
4508 /*% ripper: until!($:cond, $:body) %*/
4509 }
4510 | k_case[k_case] expr_value[expr] terms?
4511 {
4512 $$ = p->case_labels;
4513 p->case_labels = CHECK_LITERAL_WHEN;
4514 }[labels]<labels>
4515 case_body[body]
4516 k_end[k_end]
4517 {
4518 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4519 p->case_labels = $labels;
4520 $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end);
4521 fixpos($$, $expr);
4522 /*% ripper: case!($:expr, $:body) %*/
4523 }
4524 | k_case[k_case] terms?
4525 {
4526 $$ = p->case_labels;
4527 p->case_labels = 0;
4528 }[labels]<labels>
4529 case_body[body]
4530 k_end[k_end]
4531 {
4532 if (p->case_labels) st_free_table(p->case_labels);
4533 p->case_labels = $labels;
4534 $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end);
4535 /*% ripper: case!(Qnil, $:body) %*/
4536 }
4537 | k_case[k_case] expr_value[expr] terms?
4538 p_case_body[body]
4539 k_end[k_end]
4540 {
4541 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4542 /*% ripper: case!($:expr, $:body) %*/
4543 }
4544 | k_for[k_for] for_var[for_var] keyword_in[keyword_in]
4545 {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();}
4546 compstmt(stmts)[compstmt]
4547 k_end[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_empty_args_tail(p, &@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($expr_value, 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[kw]
4678 {
4679 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4680 /*% ripper: break!(args_new!) %*/
4681 }
4682 | keyword_next[kw]
4683 {
4684 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4685 /*% ripper: next!(args_new!) %*/
4686 }
4687 | keyword_redo[kw]
4688 {
4689 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4690 /*% ripper: redo! %*/
4691 }
4692 | keyword_retry[kw]
4693 {
4694 if (!p->ctxt.in_defined) {
4695 switch (p->ctxt.in_rescue) {
4696 case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break;
4697 case after_rescue: /* ok */ break;
4698 case after_else: yyerror1(&@kw, "Invalid retry after else"); break;
4699 case after_ensure: yyerror1(&@kw, "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[kw] allow_exits
4743 {
4744 $$ = $allow_exits;
4745 token_info_push(p, "while", &@$);
4746 push_end_expect_token_locations(p, &@kw.beg_pos);
4747 }
4748 ;
4749
4750k_until : keyword_until[kw] allow_exits
4751 {
4752 $$ = $allow_exits;
4753 token_info_push(p, "until", &@$);
4754 push_end_expect_token_locations(p, &@kw.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[kw] allow_exits
4766 {
4767 $$ = $allow_exits;
4768 token_info_push(p, "for", &@$);
4769 push_end_expect_token_locations(p, &@kw.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
4979block_args-opt_tail : opt_args_tail(block_args_tail)
4980 ;
4981
4982excessed_comma : ','
4983 {
4984 /* magic number for rest_id in iseq_set_arguments() */
4985 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4986 /*% ripper: excessed_comma! %*/
4987 }
4988 ;
4989
4990block_param : args-list(primary_value, block_args-opt_tail)
4991 | f_arg[pre] excessed_comma
4992 {
4993 $$ = new_empty_args_tail(p, &@excessed_comma);
4994 $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$);
4995 /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/
4996 }
4997 | f_arg[pre] opt_args_tail(block_args_tail)[tail]
4998 {
4999 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5000 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5001 }
5002 | tail-only-args(block_args_tail)
5003 ;
5004
5005opt_block_param_def : none
5006 | block_param_def
5007 {
5008 p->command_start = TRUE;
5009 }
5010 ;
5011
5012block_param_def : '|' opt_block_param opt_bv_decl '|'
5013 {
5014 p->max_numparam = ORDINAL_PARAM;
5015 p->ctxt.in_argdef = 0;
5016 $$ = $2;
5017 /*% ripper: block_var!($:2, $:3) %*/
5018 }
5019 ;
5020
5021opt_block_param : /* none */
5022 {
5023 $$ = 0;
5024 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5025 }
5026 | block_param
5027 ;
5028
5029opt_bv_decl : '\n'?
5030 {
5031 $$ = 0;
5032 /*% ripper: Qfalse %*/
5033 }
5034 | '\n'? ';' bv_decls '\n'?
5035 {
5036 $$ = 0;
5037 /*% ripper: $:3 %*/
5038 }
5039 ;
5040
5041bv_decls : bvar
5042 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5043 | bv_decls ',' bvar
5044 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5045 ;
5046
5047bvar : tIDENTIFIER
5048 {
5049 new_bv(p, $1);
5050 /*% ripper: $:1 %*/
5051 }
5052 | f_bad_arg
5053 ;
5054
5055max_numparam : {
5056 $$ = p->max_numparam;
5057 p->max_numparam = 0;
5058 }
5059 ;
5060
5061numparam : {
5062 $$ = numparam_push(p);
5063 }
5064 ;
5065
5066it_id : {
5067 $$ = p->it_id;
5068 p->it_id = 0;
5069 }
5070 ;
5071
5072lambda : tLAMBDA[lpar]
5073 {
5074 token_info_push(p, "->", &@lpar);
5075 $$ = dyna_push(p);
5076 }[dyna]<vars>
5077 max_numparam numparam it_id allow_exits
5078 f_larglist[args]
5079 {
5080 CMDARG_PUSH(0);
5081 }
5082 lambda_body[body]
5083 {
5084 int max_numparam = p->max_numparam;
5085 ID it_id = p->it_id;
5086 p->lex.lpar_beg = $lpar;
5087 p->max_numparam = $max_numparam;
5088 p->it_id = $it_id;
5089 restore_block_exit(p, $allow_exits);
5090 CMDARG_POP();
5091 $args = args_with_numbered(p, $args, max_numparam, it_id);
5092 {
5093 YYLTYPE loc = code_loc_gen(&@lpar, &@body);
5094 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5095 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5096 nd_set_line($$, @args.end_pos.lineno);
5097 nd_set_first_loc($$, @lpar.beg_pos);
5098 xfree($body);
5099 }
5100 /*% ripper: lambda!($:args, $:body) %*/
5101 numparam_pop(p, $numparam);
5102 dyna_pop(p, $dyna);
5103 }
5104 ;
5105
5106f_larglist : '(' f_largs[args] opt_bv_decl ')'
5107 {
5108 p->ctxt.in_argdef = 0;
5109 $$ = $args;
5110 p->max_numparam = ORDINAL_PARAM;
5111 /*% ripper: paren!($:args) %*/
5112 }
5113 | f_largs[args]
5114 {
5115 p->ctxt.in_argdef = 0;
5116 if (!args_info_empty_p(&$args->nd_ainfo))
5117 p->max_numparam = ORDINAL_PARAM;
5118 $$ = $args;
5119 }
5120 ;
5121
5122lambda_body : tLAMBEG compstmt(stmts) '}'
5123 {
5124 token_info_pop(p, "}", &@3);
5125 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5126 /*% ripper: $:2 %*/
5127 }
5128 | keyword_do_LAMBDA
5129 {
5130 push_end_expect_token_locations(p, &@1.beg_pos);
5131 }
5132 bodystmt k_end
5133 {
5134 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5135 /*% ripper: $:3 %*/
5136 }
5137 ;
5138
5139do_block : k_do_block do_body k_end
5140 {
5141 $$ = $2;
5142 set_embraced_location($$, &@1, &@3);
5143 /*% ripper: $:2 %*/
5144 }
5145 ;
5146
5147block_call : command do_block
5148 {
5149 if (nd_type_p($1, NODE_YIELD)) {
5150 compile_error(p, "block given to yield");
5151 }
5152 else {
5153 block_dup_check(p, get_nd_args(p, $1), $2);
5154 }
5155 $$ = method_add_block(p, $1, $2, &@$);
5156 fixpos($$, $1);
5157 /*% ripper: method_add_block!($:1, $:2) %*/
5158 }
5159 | block_call call_op2 operation2 opt_paren_args
5160 {
5161 bool has_args = $4 != 0;
5162 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5163 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5164 /*% ripper: call!($:1, $:2, $:3) %*/
5165 if (has_args) {
5166 /*% ripper: method_add_arg!($:$, $:4) %*/
5167 }
5168 }
5169 | block_call call_op2 operation2 opt_paren_args brace_block
5170 {
5171 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5172 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5173 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5174 }
5175 | block_call call_op2 operation2 command_args do_block
5176 {
5177 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5178 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5179 }
5180 | block_call call_op2 paren_args
5181 {
5182 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5183 nd_set_line($$, @2.end_pos.lineno);
5184 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5185 }
5186 ;
5187
5188method_call : fcall paren_args
5189 {
5190 $1->nd_args = $2;
5191 $$ = (NODE *)$1;
5192 nd_set_last_loc($1, @2.end_pos);
5193 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5194 }
5195 | primary_value call_op operation2 opt_paren_args
5196 {
5197 bool has_args = $4 != 0;
5198 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5199 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5200 nd_set_line($$, @3.end_pos.lineno);
5201 /*% ripper: call!($:1, $:2, $:3) %*/
5202 if (has_args) {
5203 /*% ripper: method_add_arg!($:$, $:4) %*/
5204 }
5205 }
5206 | primary_value tCOLON2 operation2 paren_args
5207 {
5208 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5209 nd_set_line($$, @3.end_pos.lineno);
5210 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5211 }
5212 | primary_value tCOLON2 operation3
5213 {
5214 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5215 /*% ripper: call!($:1, $:2, $:3) %*/
5216 }
5217 | primary_value call_op2 paren_args
5218 {
5219 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5220 nd_set_line($$, @2.end_pos.lineno);
5221 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5222 }
5223 | keyword_super paren_args
5224 {
5225 rb_code_location_t lparen_loc = @2;
5226 rb_code_location_t rparen_loc = @2;
5227 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5228 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5229
5230 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5231 /*% ripper: super!($:2) %*/
5232 }
5233 | keyword_super
5234 {
5235 $$ = NEW_ZSUPER(&@$);
5236 /*% ripper: zsuper! %*/
5237 }
5238 | primary_value '[' opt_call_args rbracket
5239 {
5240 $$ = NEW_CALL($1, tAREF, $3, &@$);
5241 fixpos($$, $1);
5242 /*% ripper: aref!($:1, $:3) %*/
5243 }
5244 ;
5245
5246brace_block : '{' brace_body '}'
5247 {
5248 $$ = $2;
5249 set_embraced_location($$, &@1, &@3);
5250 /*% ripper: $:2 %*/
5251 }
5252 | k_do do_body k_end
5253 {
5254 $$ = $2;
5255 set_embraced_location($$, &@1, &@3);
5256 /*% ripper: $:2 %*/
5257 }
5258 ;
5259
5260brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5261 max_numparam numparam it_id allow_exits
5262 opt_block_param_def[args] compstmt(stmts)
5263 {
5264 int max_numparam = p->max_numparam;
5265 ID it_id = p->it_id;
5266 p->max_numparam = $max_numparam;
5267 p->it_id = $it_id;
5268 $args = args_with_numbered(p, $args, max_numparam, it_id);
5269 $$ = NEW_ITER($args, $compstmt, &@$);
5270 /*% ripper: brace_block!($:args, $:compstmt) %*/
5271 restore_block_exit(p, $allow_exits);
5272 numparam_pop(p, $numparam);
5273 dyna_pop(p, $dyna);
5274 }
5275 ;
5276
5277do_body : {
5278 $$ = dyna_push(p);
5279 CMDARG_PUSH(0);
5280 }[dyna]<vars>
5281 max_numparam numparam it_id allow_exits
5282 opt_block_param_def[args] bodystmt
5283 {
5284 int max_numparam = p->max_numparam;
5285 ID it_id = p->it_id;
5286 p->max_numparam = $max_numparam;
5287 p->it_id = $it_id;
5288 $args = args_with_numbered(p, $args, max_numparam, it_id);
5289 $$ = NEW_ITER($args, $bodystmt, &@$);
5290 /*% ripper: do_block!($:args, $:bodystmt) %*/
5291 CMDARG_POP();
5292 restore_block_exit(p, $allow_exits);
5293 numparam_pop(p, $numparam);
5294 dyna_pop(p, $dyna);
5295 }
5296 ;
5297
5298case_args : arg_value
5299 {
5300 check_literal_when(p, $arg_value, &@arg_value);
5301 $$ = NEW_LIST($arg_value, &@$);
5302 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5303 }
5304 | tSTAR arg_value
5305 {
5306 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5307 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5308 }
5309 | case_args[non_last_args] ',' arg_value
5310 {
5311 check_literal_when(p, $arg_value, &@arg_value);
5312 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5313 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5314 }
5315 | case_args[non_last_args] ',' tSTAR arg_value
5316 {
5317 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5318 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5319 }
5320 ;
5321
5322case_body : k_when case_args then
5323 compstmt(stmts)
5324 cases
5325 {
5326 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5327 fixpos($$, $2);
5328 /*% ripper: when!($:2, $:4, $:5) %*/
5329 }
5330 ;
5331
5332cases : opt_else
5333 | case_body
5334 ;
5335
5336p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5337p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5338
5339p_in_kwarg : {
5340 $$ = p->ctxt;
5341 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5342 p->command_start = FALSE;
5343 p->ctxt.in_kwarg = 1;
5344 p->ctxt.in_alt_pattern = 0;
5345 p->ctxt.capture_in_pattern = 0;
5346 }
5347 ;
5348
5349p_case_body : keyword_in
5350 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5351 p_top_expr[expr] then
5352 {
5353 pop_pktbl(p, $p_pktbl);
5354 pop_pvtbl(p, $p_pvtbl);
5355 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5356 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5357 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5358 }
5359 compstmt(stmts)
5360 p_cases[cases]
5361 {
5362 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5363 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5364 }
5365 ;
5366
5367p_cases : opt_else
5368 | p_case_body
5369 ;
5370
5371p_top_expr : p_top_expr_body
5372 | p_top_expr_body modifier_if expr_value
5373 {
5374 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5375 fixpos($$, $3);
5376 /*% ripper: if_mod!($:3, $:1) %*/
5377 }
5378 | p_top_expr_body modifier_unless expr_value
5379 {
5380 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5381 fixpos($$, $3);
5382 /*% ripper: unless_mod!($:3, $:1) %*/
5383 }
5384 ;
5385
5386p_top_expr_body : p_expr
5387 | p_expr ','
5388 {
5389 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5390 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5391 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5392 }
5393 | p_expr ',' p_args
5394 {
5395 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5396 nd_set_first_loc($$, @1.beg_pos);
5397 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5398 }
5399 | p_find
5400 {
5401 $$ = new_find_pattern(p, 0, $1, &@$);
5402 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5403 }
5404 | p_args_tail
5405 {
5406 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5407 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5408 }
5409 | p_kwargs
5410 {
5411 $$ = new_hash_pattern(p, 0, $1, &@$);
5412 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5413 }
5414 ;
5415
5416p_expr : p_as
5417 ;
5418
5419p_as : p_expr tASSOC p_variable
5420 {
5421 NODE *n = NEW_LIST($1, &@$);
5422 n = list_append(p, n, $3);
5423 $$ = new_hash(p, n, &@$);
5424 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5425 }
5426 | p_alt
5427 ;
5428
5429p_alt : p_alt[left] '|'[alt]
5430 {
5431 p->ctxt.in_alt_pattern = 1;
5432 }
5433 p_expr_basic[right]
5434 {
5435 if (p->ctxt.capture_in_pattern) {
5436 yyerror1(&@alt, "alternative pattern after variable capture");
5437 }
5438 p->ctxt.in_alt_pattern = 0;
5439 $$ = NEW_OR($left, $right, &@$, &@alt);
5440 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5441 }
5442 | p_expr_basic
5443 ;
5444
5445p_lparen : '(' p_pktbl
5446 {
5447 $$ = $2;
5448 /*% ripper: $:2 %*/
5449 }
5450 ;
5451
5452p_lbracket : '[' p_pktbl
5453 {
5454 $$ = $2;
5455 /*% ripper: $:2 %*/
5456 }
5457 ;
5458
5459p_expr_basic : p_value
5460 | p_variable
5461 | p_const p_lparen[p_pktbl] p_args rparen
5462 {
5463 pop_pktbl(p, $p_pktbl);
5464 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5465 nd_set_first_loc($$, @p_const.beg_pos);
5466 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5467 }
5468 | p_const p_lparen[p_pktbl] p_find rparen
5469 {
5470 pop_pktbl(p, $p_pktbl);
5471 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5472 nd_set_first_loc($$, @p_const.beg_pos);
5473 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5474 }
5475 | p_const p_lparen[p_pktbl] p_kwargs rparen
5476 {
5477 pop_pktbl(p, $p_pktbl);
5478 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5479 nd_set_first_loc($$, @p_const.beg_pos);
5480 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5481 }
5482 | p_const '(' rparen
5483 {
5484 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5485 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5486 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5487 }
5488 | p_const p_lbracket[p_pktbl] p_args rbracket
5489 {
5490 pop_pktbl(p, $p_pktbl);
5491 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5492 nd_set_first_loc($$, @p_const.beg_pos);
5493 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5494 }
5495 | p_const p_lbracket[p_pktbl] p_find rbracket
5496 {
5497 pop_pktbl(p, $p_pktbl);
5498 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5499 nd_set_first_loc($$, @p_const.beg_pos);
5500 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5501 }
5502 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5503 {
5504 pop_pktbl(p, $p_pktbl);
5505 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5506 nd_set_first_loc($$, @p_const.beg_pos);
5507 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5508 }
5509 | p_const '[' rbracket
5510 {
5511 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5512 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5513 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5514 }
5515 | tLBRACK p_args rbracket
5516 {
5517 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5518 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5519 }
5520 | tLBRACK p_find rbracket
5521 {
5522 $$ = new_find_pattern(p, 0, $p_find, &@$);
5523 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5524 }
5525 | tLBRACK rbracket
5526 {
5527 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5528 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5529 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5530 }
5531 | tLBRACE p_pktbl lex_ctxt[ctxt]
5532 {
5533 p->ctxt.in_kwarg = 0;
5534 }
5535 p_kwargs rbrace
5536 {
5537 pop_pktbl(p, $p_pktbl);
5538 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5539 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5540 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5541 }
5542 | tLBRACE rbrace
5543 {
5544 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5545 $$ = new_hash_pattern(p, 0, $$, &@$);
5546 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5547 }
5548 | tLPAREN p_pktbl p_expr rparen
5549 {
5550 pop_pktbl(p, $p_pktbl);
5551 $$ = $p_expr;
5552 /*% ripper: $:p_expr %*/
5553 }
5554 ;
5555
5556p_args : p_expr
5557 {
5558 NODE *pre_args = NEW_LIST($1, &@$);
5559 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5560 /*% ripper: [[$:1], Qnil, Qnil] %*/
5561 }
5562 | p_args_head
5563 {
5564 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5565 /*% ripper: [$:1, Qnil, Qnil] %*/
5566 }
5567 | p_args_head p_arg
5568 {
5569 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5570 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5571 }
5572 | p_args_head p_rest
5573 {
5574 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5575 /*% ripper: [$:1, $:2, Qnil] %*/
5576 }
5577 | p_args_head p_rest ',' p_args_post
5578 {
5579 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5580 /*% ripper: [$:1, $:2, $:4] %*/
5581 }
5582 | p_args_tail
5583 ;
5584
5585p_args_head : p_arg ','
5586 | p_args_head p_arg ','
5587 {
5588 $$ = list_concat($1, $2);
5589 /*% ripper: rb_ary_concat($:1, $:2) %*/
5590 }
5591 ;
5592
5593p_args_tail : p_rest
5594 {
5595 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5596 /*% ripper: [Qnil, $:1, Qnil] %*/
5597 }
5598 | p_rest ',' p_args_post
5599 {
5600 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5601 /*% ripper: [Qnil, $:1, $:3] %*/
5602 }
5603 ;
5604
5605p_find : p_rest ',' p_args_post ',' p_rest
5606 {
5607 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5608 /*% ripper: [$:1, $:3, $:5] %*/
5609 }
5610 ;
5611
5612
5613p_rest : tSTAR tIDENTIFIER
5614 {
5615 error_duplicate_pattern_variable(p, $2, &@2);
5616 /*% ripper: var_field!($:2) %*/
5617 $$ = assignable(p, $2, 0, &@$);
5618 }
5619 | tSTAR
5620 {
5621 $$ = 0;
5622 /*% ripper: var_field!(Qnil) %*/
5623 }
5624 ;
5625
5626p_args_post : p_arg
5627 | p_args_post ',' p_arg
5628 {
5629 $$ = list_concat($1, $3);
5630 /*% ripper: rb_ary_concat($:1, $:3) %*/
5631 }
5632 ;
5633
5634p_arg : p_expr
5635 {
5636 $$ = NEW_LIST($1, &@$);
5637 /*% ripper: [$:1] %*/
5638 }
5639 ;
5640
5641p_kwargs : p_kwarg ',' p_any_kwrest
5642 {
5643 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5644 /*% ripper: [$:1, $:3] %*/
5645 }
5646 | p_kwarg
5647 {
5648 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5649 /*% ripper: [$:1, Qnil] %*/
5650 }
5651 | p_kwarg ','
5652 {
5653 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5654 /*% ripper: [$:1, Qnil] %*/
5655 }
5656 | p_any_kwrest
5657 {
5658 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5659 /*% ripper: [[], $:1] %*/
5660 }
5661 ;
5662
5663p_kwarg : p_kw
5664 /*% ripper[brace]: [$:1] %*/
5665 | p_kwarg ',' p_kw
5666 {
5667 $$ = list_concat($1, $3);
5668 /*% ripper: rb_ary_push($:1, $:3) %*/
5669 }
5670 ;
5671
5672p_kw : p_kw_label p_expr
5673 {
5674 error_duplicate_pattern_key(p, $1, &@1);
5675 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5676 /*% ripper: [$:1, $:2] %*/
5677 }
5678 | p_kw_label
5679 {
5680 error_duplicate_pattern_key(p, $1, &@1);
5681 if ($1 && !is_local_id($1)) {
5682 yyerror1(&@1, "key must be valid as local variables");
5683 }
5684 error_duplicate_pattern_variable(p, $1, &@1);
5685 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5686 /*% ripper: [$:1, Qnil] %*/
5687 }
5688 ;
5689
5690p_kw_label : tLABEL
5691 | tSTRING_BEG string_contents tLABEL_END
5692 {
5693 YYLTYPE loc = code_loc_gen(&@1, &@3);
5694 if (!$2 || nd_type_p($2, NODE_STR)) {
5695 NODE *node = dsym_node(p, $2, &loc);
5696 $$ = rb_sym2id(rb_node_sym_string_val(node));
5697 }
5698 else {
5699 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5700 $$ = rb_intern_str(STR_NEW0());
5701 }
5702 /*% ripper: $:2 %*/
5703 }
5704 ;
5705
5706p_kwrest : kwrest_mark tIDENTIFIER
5707 {
5708 $$ = $2;
5709 /*% ripper: var_field!($:2) %*/
5710 }
5711 | kwrest_mark
5712 {
5713 $$ = 0;
5714 /*% ripper: Qnil %*/
5715 }
5716 ;
5717
5718p_kwnorest : kwrest_mark keyword_nil
5719 {
5720 $$ = 0;
5721 }
5722 ;
5723
5724p_any_kwrest : p_kwrest
5725 | p_kwnorest
5726 {
5727 $$ = idNil;
5728 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5729 }
5730 ;
5731
5732p_value : p_primitive
5733 | range_expr(p_primitive)
5734 | p_var_ref
5735 | p_expr_ref
5736 | p_const
5737 ;
5738
5739p_primitive : inline_primary
5740 | keyword_variable
5741 {
5742 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5743 /*% ripper: var_ref!($:1) %*/
5744 }
5745 | lambda
5746 ;
5747
5748p_variable : tIDENTIFIER
5749 {
5750 error_duplicate_pattern_variable(p, $1, &@1);
5751 /*% ripper: var_field!($:1) %*/
5752 $$ = assignable(p, $1, 0, &@$);
5753 }
5754 ;
5755
5756p_var_ref : '^' tIDENTIFIER
5757 {
5758 NODE *n = gettable(p, $2, &@$);
5759 if (!n) {
5760 n = NEW_ERROR(&@$);
5761 }
5762 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5763 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5764 }
5765 $$ = n;
5766 /*% ripper: var_ref!($:2) %*/
5767 }
5768 | '^' nonlocal_var
5769 {
5770 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5771 /*% ripper: var_ref!($:2) %*/
5772 }
5773 ;
5774
5775p_expr_ref : '^' tLPAREN expr_value rparen
5776 {
5777 $$ = NEW_BLOCK($3, &@$);
5778 /*% ripper: begin!($:3) %*/
5779 }
5780 ;
5781
5782p_const : tCOLON3 cname
5783 {
5784 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5785 /*% ripper: top_const_ref!($:2) %*/
5786 }
5787 | p_const tCOLON2 cname
5788 {
5789 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5790 /*% ripper: const_path_ref!($:1, $:3) %*/
5791 }
5792 | tCONSTANT
5793 {
5794 $$ = gettable(p, $1, &@$);
5795 /*% ripper: var_ref!($:1) %*/
5796 }
5797 ;
5798
5799opt_rescue : k_rescue exc_list exc_var then
5800 compstmt(stmts)
5801 opt_rescue
5802 {
5803 NODE *err = $3;
5804 if ($3) {
5805 err = NEW_ERRINFO(&@3);
5806 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5807 }
5808 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5809 if ($2) {
5810 fixpos($$, $2);
5811 }
5812 else if ($3) {
5813 fixpos($$, $3);
5814 }
5815 else {
5816 fixpos($$, $5);
5817 }
5818 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5819 }
5820 | none
5821 ;
5822
5823exc_list : arg_value
5824 {
5825 $$ = NEW_LIST($1, &@$);
5826 /*% ripper: rb_ary_new3(1, $:1) %*/
5827 }
5828 | mrhs
5829 {
5830 if (!($$ = splat_array($1))) $$ = $1;
5831 }
5832 | none
5833 ;
5834
5835exc_var : tASSOC lhs
5836 {
5837 $$ = $2;
5838 /*% ripper: $:2 %*/
5839 }
5840 | none
5841 ;
5842
5843opt_ensure : k_ensure stmts terms?
5844 {
5845 p->ctxt.in_rescue = $1.in_rescue;
5846 $$ = $2;
5847 void_expr(p, void_stmts(p, $$));
5848 /*% ripper: ensure!($:2) %*/
5849 }
5850 | none
5851 ;
5852
5853literal : numeric
5854 | symbol
5855 ;
5856
5857strings : string
5858 {
5859 if (!$1) {
5860 $$ = NEW_STR(STRING_NEW0(), &@$);
5861 }
5862 else {
5863 $$ = evstr2dstr(p, $1);
5864 }
5865 /*% ripper: $:1 %*/
5866 }
5867 ;
5868
5869string : tCHAR
5870 | string1
5871 | string string1
5872 {
5873 $$ = literal_concat(p, $1, $2, &@$);
5874 /*% ripper: string_concat!($:1, $:2) %*/
5875 }
5876 ;
5877
5878string1 : tSTRING_BEG string_contents tSTRING_END
5879 {
5880 $$ = heredoc_dedent(p, $2);
5881 if ($$) nd_set_loc($$, &@$);
5882 /*% ripper: $:2 %*/
5883 if (p->heredoc_indent > 0) {
5884 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5885 p->heredoc_indent = 0;
5886 }
5887 /*% ripper: string_literal!($:$) %*/
5888 }
5889 ;
5890
5891xstring : tXSTRING_BEG xstring_contents tSTRING_END
5892 {
5893 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5894 /*% ripper: $:2 %*/
5895 if (p->heredoc_indent > 0) {
5896 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5897 p->heredoc_indent = 0;
5898 }
5899 /*% ripper: xstring_literal!($:$) %*/
5900 }
5901 ;
5902
5903regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5904 {
5905 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5906 /*% ripper: regexp_literal!($:2, $:3) %*/
5907 }
5908 ;
5909
5910words : words(tWORDS_BEG, word_list)
5911 ;
5912
5913word_list : /* none */
5914 {
5915 $$ = 0;
5916 /*% ripper: words_new! %*/
5917 }
5918 | word_list word ' '+
5919 {
5920 $$ = list_append(p, $1, evstr2dstr(p, $2));
5921 /*% ripper: words_add!($:1, $:2) %*/
5922 }
5923 ;
5924
5925word : string_content
5926 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5927 | word string_content
5928 {
5929 $$ = literal_concat(p, $1, $2, &@$);
5930 /*% ripper: word_add!($:1, $:2) %*/
5931 }
5932 ;
5933
5934symbols : words(tSYMBOLS_BEG, symbol_list)
5935 ;
5936
5937symbol_list : /* none */
5938 {
5939 $$ = 0;
5940 /*% ripper: symbols_new! %*/
5941 }
5942 | symbol_list word ' '+
5943 {
5944 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5945 /*% ripper: symbols_add!($:1, $:2) %*/
5946 }
5947 ;
5948
5949qwords : words(tQWORDS_BEG, qword_list)
5950 ;
5951
5952qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5953 ;
5954
5955qword_list : /* none */
5956 {
5957 $$ = 0;
5958 /*% ripper: qwords_new! %*/
5959 }
5960 | qword_list tSTRING_CONTENT ' '+
5961 {
5962 $$ = list_append(p, $1, $2);
5963 /*% ripper: qwords_add!($:1, $:2) %*/
5964 }
5965 ;
5966
5967qsym_list : /* none */
5968 {
5969 $$ = 0;
5970 /*% ripper: qsymbols_new! %*/
5971 }
5972 | qsym_list tSTRING_CONTENT ' '+
5973 {
5974 $$ = symbol_append(p, $1, $2);
5975 /*% ripper: qsymbols_add!($:1, $:2) %*/
5976 }
5977 ;
5978
5979string_contents : /* none */
5980 {
5981 $$ = 0;
5982 /*% ripper: string_content! %*/
5983 }
5984 | string_contents string_content
5985 {
5986 $$ = literal_concat(p, $1, $2, &@$);
5987 /*% ripper: string_add!($:1, $:2) %*/
5988 }
5989 ;
5990
5991xstring_contents: /* none */
5992 {
5993 $$ = 0;
5994 /*% ripper: xstring_new! %*/
5995 }
5996 | xstring_contents string_content
5997 {
5998 $$ = literal_concat(p, $1, $2, &@$);
5999 /*% ripper: xstring_add!($:1, $:2) %*/
6000 }
6001 ;
6002
6003regexp_contents : /* none */
6004 {
6005 $$ = 0;
6006 /*% ripper: regexp_new! %*/
6007 }
6008 | regexp_contents string_content
6009 {
6010 NODE *head = $1, *tail = $2;
6011 if (!head) {
6012 $$ = tail;
6013 }
6014 else if (!tail) {
6015 $$ = head;
6016 }
6017 else {
6018 switch (nd_type(head)) {
6019 case NODE_STR:
6020 head = str2dstr(p, head);
6021 break;
6022 case NODE_DSTR:
6023 break;
6024 default:
6025 head = list_append(p, NEW_DSTR(0, &@$), head);
6026 break;
6027 }
6028 $$ = list_append(p, head, tail);
6029 }
6030 /*% ripper: regexp_add!($:1, $:2) %*/
6031 }
6032 ;
6033
6034string_content : tSTRING_CONTENT[content]
6035 /*% ripper[brace]: $:content %*/
6036 | tSTRING_DVAR[state]
6037 {
6038 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6039 $$ = p->lex.strterm;
6040 p->lex.strterm = 0;
6041 SET_LEX_STATE(EXPR_BEG);
6042 }[strterm]<strterm>
6043 string_dvar[dvar]
6044 {
6045 p->lex.strterm = $strterm;
6046 $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC);
6047 nd_set_line($$, @dvar.end_pos.lineno);
6048 /*% ripper: string_dvar!($:dvar) %*/
6049 }
6050 | tSTRING_DBEG[state]
6051 {
6052 CMDARG_PUSH(0);
6053 COND_PUSH(0);
6054 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6055 $$ = p->lex.strterm;
6056 p->lex.strterm = 0;
6057 SET_LEX_STATE(EXPR_BEG);
6058 }[term]<strterm>
6059 {
6060 $$ = p->lex.brace_nest;
6061 p->lex.brace_nest = 0;
6062 }[brace]<num>
6063 {
6064 $$ = p->heredoc_indent;
6065 p->heredoc_indent = 0;
6066 }[indent]<num>
6067 compstmt(stmts) string_dend
6068 {
6069 COND_POP();
6070 CMDARG_POP();
6071 p->lex.strterm = $term;
6072 SET_LEX_STATE($state);
6073 p->lex.brace_nest = $brace;
6074 p->heredoc_indent = $indent;
6075 p->heredoc_line_indent = -1;
6076 if ($compstmt) nd_unset_fl_newline($compstmt);
6077 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6078 /*% ripper: string_embexpr!($:compstmt) %*/
6079 }
6080 ;
6081
6082string_dend : tSTRING_DEND
6083 | END_OF_INPUT
6084 ;
6085
6086string_dvar : nonlocal_var
6087 {
6088 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6089 /*% ripper: var_ref!($:1) %*/
6090 }
6091 | backref
6092 ;
6093
6094symbol : ssym
6095 | dsym
6096 ;
6097
6098ssym : tSYMBEG sym
6099 {
6100 SET_LEX_STATE(EXPR_END);
6101 VALUE str = rb_id2str($2);
6102 /*
6103 * TODO:
6104 * set_yylval_noname sets invalid id to yylval.
6105 * This branch can be removed once yylval is changed to
6106 * hold lexed string.
6107 */
6108 if (!str) str = STR_NEW0();
6109 $$ = NEW_SYM(str, &@$);
6110 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6111 }
6112 ;
6113
6114sym : fname
6115 | nonlocal_var
6116 ;
6117
6118dsym : tSYMBEG string_contents tSTRING_END
6119 {
6120 SET_LEX_STATE(EXPR_END);
6121 $$ = dsym_node(p, $2, &@$);
6122 /*% ripper: dyna_symbol!($:2) %*/
6123 }
6124 ;
6125
6126numeric : simple_numeric
6127 | tUMINUS_NUM simple_numeric %prec tLOWEST
6128 {
6129 $$ = $2;
6130 negate_lit(p, $$);
6131 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6132 }
6133 ;
6134
6135simple_numeric : tINTEGER
6136 | tFLOAT
6137 | tRATIONAL
6138 | tIMAGINARY
6139 ;
6140
6141nonlocal_var : tIVAR
6142 | tGVAR
6143 | tCVAR
6144 ;
6145
6146user_variable : ident_or_const
6147 | nonlocal_var
6148 ;
6149
6150keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6151 | keyword_self {$$ = KWD2EID(self, $1);}
6152 | keyword_true {$$ = KWD2EID(true, $1);}
6153 | keyword_false {$$ = KWD2EID(false, $1);}
6154 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6155 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6156 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6157 ;
6158
6159var_ref : user_variable
6160 {
6161 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6162 if (ifdef_ripper(id_is_var(p, $1), false)) {
6163 /*% ripper: var_ref!($:1) %*/
6164 }
6165 else {
6166 /*% ripper: vcall!($:1) %*/
6167 }
6168 }
6169 | keyword_variable
6170 {
6171 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6172 /*% ripper: var_ref!($:1) %*/
6173 }
6174 ;
6175
6176var_lhs : user_or_keyword_variable
6177 {
6178 /*% ripper: var_field!($:1) %*/
6179 $$ = assignable(p, $1, 0, &@$);
6180 }
6181 ;
6182
6183backref : tNTH_REF
6184 | tBACK_REF
6185 ;
6186
6187superclass : '<'
6188 {
6189 SET_LEX_STATE(EXPR_BEG);
6190 p->command_start = TRUE;
6191 }
6192 expr_value term
6193 {
6194 $$ = $3;
6195 /*% ripper: $:3 %*/
6196 }
6197 | none
6198 ;
6199
6200f_opt_paren_args: f_paren_args
6201 | f_empty_arg
6202 {
6203 p->ctxt.in_argdef = 0;
6204 }
6205 ;
6206
6207f_empty_arg : /* none */
6208 {
6209 $$ = new_empty_args_tail(p, &@$);
6210 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6211 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6212 }
6213 ;
6214
6215f_paren_args : '(' f_args rparen
6216 {
6217 $$ = $2;
6218 /*% ripper: paren!($:2) %*/
6219 SET_LEX_STATE(EXPR_BEG);
6220 p->command_start = TRUE;
6221 p->ctxt.in_argdef = 0;
6222 }
6223 ;
6224
6225f_arglist : f_paren_args
6226 | {
6227 $$ = p->ctxt;
6228 p->ctxt.in_kwarg = 1;
6229 p->ctxt.in_argdef = 1;
6230 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6231 }<ctxt>
6232 f_args term
6233 {
6234 p->ctxt.in_kwarg = $1.in_kwarg;
6235 p->ctxt.in_argdef = 0;
6236 $$ = $2;
6237 SET_LEX_STATE(EXPR_BEG);
6238 p->command_start = TRUE;
6239 /*% ripper: $:2 %*/
6240 }
6241 ;
6242
6243args_tail : args_tail_basic(arg_value)
6244 | args_forward
6245 {
6246 add_forwarding_args(p);
6247 $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward);
6248 $$->nd_ainfo.forwarding = 1;
6249 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6250 }
6251 ;
6252
6253largs_tail : args_tail_basic(arg_value)
6254 | args_forward
6255 {
6256 yyerror1(&@args_forward, "unexpected ... in lambda argument");
6257 $$ = new_args_tail(p, 0, 0, 0, &@args_forward);
6258 $$->nd_ainfo.forwarding = 1;
6259 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6260 }
6261 ;
6262
6263%rule args-list(value, tail) <node_args>
6264 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6265 {
6266 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6267 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6268 }
6269 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6270 {
6271 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6272 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6273 }
6274 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6275 {
6276 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6277 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6278 }
6279 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6280 {
6281 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6282 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6283 }
6284 | f_arg[pre] ',' f_rest_arg[rest] tail
6285 {
6286 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6287 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6288 }
6289 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6290 {
6291 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6292 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6293 }
6294 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6295 {
6296 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6297 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6298 }
6299 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6300 {
6301 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6302 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6303 }
6304 | f_opt_arg(value)[opt] tail
6305 {
6306 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6307 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6308 }
6309 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6310 {
6311 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6312 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6313 }
6314 | f_rest_arg[rest] tail
6315 {
6316 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6317 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6318 }
6319 | f_rest_arg[rest] ',' f_arg[post] tail
6320 {
6321 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6322 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6323 }
6324 ;
6325
6326%rule tail-only-args(tail) <node_args>
6327 : tail
6328 {
6329 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6330 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6331 }
6332 ;
6333
6334%rule f_args-opt_tail(tail) <node_args>
6335 : opt_args_tail(tail)
6336 ;
6337
6338
6339%rule f_args-list(tail) <node_args>
6340 : args-list(arg_value, f_args-opt_tail(tail))
6341 | f_arg[pre] opt_args_tail(tail)[tail]
6342 {
6343 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6344 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6345 }
6346 | tail-only-args(tail)
6347 | f_empty_arg
6348 ;
6349
6350f_args : f_args-list(args_tail)
6351 ;
6352
6353f_largs : f_args-list(largs_tail)
6354 ;
6355
6356args_forward : tBDOT3
6357 {
6358 $$ = idFWD_KWREST;
6359 /*% ripper: args_forward! %*/
6360 }
6361 ;
6362
6363f_bad_arg : tCONSTANT
6364 {
6365 static const char mesg[] = "formal argument cannot be a constant";
6366 /*%%%*/
6367 yyerror1(&@1, mesg);
6368 /*% %*/
6369 $$ = 0;
6370 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6371 }
6372 | tIVAR
6373 {
6374 static const char mesg[] = "formal argument cannot be an instance variable";
6375 /*%%%*/
6376 yyerror1(&@1, mesg);
6377 /*% %*/
6378 $$ = 0;
6379 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6380 }
6381 | tGVAR
6382 {
6383 static const char mesg[] = "formal argument cannot be a global variable";
6384 /*%%%*/
6385 yyerror1(&@1, mesg);
6386 /*% %*/
6387 $$ = 0;
6388 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6389 }
6390 | tCVAR
6391 {
6392 static const char mesg[] = "formal argument cannot be a class variable";
6393 /*%%%*/
6394 yyerror1(&@1, mesg);
6395 /*% %*/
6396 $$ = 0;
6397 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6398 }
6399 ;
6400
6401f_norm_arg : f_bad_arg
6402 | tIDENTIFIER
6403 {
6404 VALUE e = formal_argument_error(p, $$ = $1);
6405 if (e) {
6406 /*% ripper[error]: param_error!(?e, $:1) %*/
6407 }
6408 p->max_numparam = ORDINAL_PARAM;
6409 }
6410 ;
6411
6412f_arg_asgn : f_norm_arg
6413 {
6414 arg_var(p, $1);
6415 $$ = $1;
6416 }
6417 ;
6418
6419f_arg_item : f_arg_asgn
6420 {
6421 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6422 /*% ripper: $:1 %*/
6423 }
6424 | tLPAREN f_margs rparen
6425 {
6426 ID tid = internal_id(p);
6427 YYLTYPE loc;
6428 loc.beg_pos = @2.beg_pos;
6429 loc.end_pos = @2.beg_pos;
6430 arg_var(p, tid);
6431 if (dyna_in_block(p)) {
6432 $2->nd_value = NEW_DVAR(tid, &loc);
6433 }
6434 else {
6435 $2->nd_value = NEW_LVAR(tid, &loc);
6436 }
6437 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6438 $$->nd_next = (NODE *)$2;
6439 /*% ripper: mlhs_paren!($:2) %*/
6440 }
6441 ;
6442
6443f_arg : f_arg_item
6444 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6445 | f_arg ',' f_arg_item
6446 {
6447 $$ = $1;
6448 $$->nd_plen++;
6449 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6450 rb_discard_node(p, (NODE *)$3);
6451 /*% ripper: rb_ary_push($:1, $:3) %*/
6452 }
6453 ;
6454
6455
6456f_label : tLABEL
6457 {
6458 VALUE e = formal_argument_error(p, $$ = $1);
6459 if (e) {
6460 $$ = 0;
6461 /*% ripper[error]: param_error!(?e, $:1) %*/
6462 }
6463 /*
6464 * Workaround for Prism::ParseTest#test_filepath for
6465 * "unparser/corpus/literal/def.txt"
6466 *
6467 * See the discussion on https://github.com/ruby/ruby/pull/9923
6468 */
6469 arg_var(p, ifdef_ripper(0, $1));
6470 /*% ripper: $:1 %*/
6471 p->max_numparam = ORDINAL_PARAM;
6472 p->ctxt.in_argdef = 0;
6473 }
6474 ;
6475
6476kwrest_mark : tPOW
6477 | tDSTAR
6478 ;
6479
6480f_no_kwarg : p_kwnorest
6481 {
6482 /*% ripper: nokw_param!(Qnil) %*/
6483 }
6484 ;
6485
6486f_kwrest : kwrest_mark tIDENTIFIER
6487 {
6488 arg_var(p, shadowing_lvar(p, $2));
6489 $$ = $2;
6490 /*% ripper: kwrest_param!($:2) %*/
6491 }
6492 | kwrest_mark
6493 {
6494 arg_var(p, idFWD_KWREST);
6495 $$ = idFWD_KWREST;
6496 /*% ripper: kwrest_param!(Qnil) %*/
6497 }
6498 ;
6499
6500restarg_mark : '*'
6501 | tSTAR
6502 ;
6503
6504f_rest_arg : restarg_mark tIDENTIFIER
6505 {
6506 arg_var(p, shadowing_lvar(p, $2));
6507 $$ = $2;
6508 /*% ripper: rest_param!($:2) %*/
6509 }
6510 | restarg_mark
6511 {
6512 arg_var(p, idFWD_REST);
6513 $$ = idFWD_REST;
6514 /*% ripper: rest_param!(Qnil) %*/
6515 }
6516 ;
6517
6518blkarg_mark : '&'
6519 | tAMPER
6520 ;
6521
6522f_block_arg : blkarg_mark tIDENTIFIER
6523 {
6524 arg_var(p, shadowing_lvar(p, $2));
6525 $$ = $2;
6526 /*% ripper: blockarg!($:2) %*/
6527 }
6528 | blkarg_mark keyword_nil
6529 {
6530 $$ = idNil;
6531 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6532 }
6533 | blkarg_mark
6534 {
6535 arg_var(p, idFWD_BLOCK);
6536 $$ = idFWD_BLOCK;
6537 /*% ripper: blockarg!(Qnil) %*/
6538 }
6539 ;
6540
6541opt_f_block_arg : ',' f_block_arg
6542 {
6543 $$ = $2;
6544 /*% ripper: $:2 %*/
6545 }
6546 | none
6547 ;
6548
6549
6550singleton : value_expr(singleton_expr)
6551 {
6552 NODE *expr = last_expr_node($1);
6553 switch (nd_type(expr)) {
6554 case NODE_STR:
6555 case NODE_DSTR:
6556 case NODE_XSTR:
6557 case NODE_DXSTR:
6558 case NODE_REGX:
6559 case NODE_DREGX:
6560 case NODE_SYM:
6561 case NODE_LINE:
6562 case NODE_FILE:
6563 case NODE_ENCODING:
6564 case NODE_INTEGER:
6565 case NODE_FLOAT:
6566 case NODE_RATIONAL:
6567 case NODE_IMAGINARY:
6568 case NODE_DSYM:
6569 case NODE_LIST:
6570 case NODE_ZLIST:
6571 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6572 break;
6573 default:
6574 break;
6575 }
6576 $$ = $1;
6577 }
6578 ;
6579
6580singleton_expr : var_ref
6581 | '('
6582 {
6583 SET_LEX_STATE(EXPR_BEG);
6584 p->ctxt.in_argdef = 0;
6585 }
6586 expr rparen
6587 {
6588 p->ctxt.in_argdef = 1;
6589 $$ = $3;
6590 /*% ripper: paren!($:3) %*/
6591 }
6592 ;
6593
6594assoc_list : none
6595 | assocs trailer
6596 {
6597 $$ = $1;
6598 /*% ripper: assoclist_from_args!($:1) %*/
6599 }
6600 ;
6601
6602assocs : assoc
6603 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6604 | assocs ',' assoc
6605 {
6606 NODE *assocs = $1;
6607 NODE *tail = $3;
6608 if (!assocs) {
6609 assocs = tail;
6610 }
6611 else if (tail) {
6612 if (RNODE_LIST(assocs)->nd_head) {
6613 NODE *n = RNODE_LIST(tail)->nd_next;
6614 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6615 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6616 /* DSTAR */
6617 tail = RNODE_HASH(n)->nd_head;
6618 }
6619 }
6620 if (tail) {
6621 assocs = list_concat(assocs, tail);
6622 }
6623 }
6624 $$ = assocs;
6625 /*% ripper: rb_ary_push($:1, $:3) %*/
6626 }
6627 ;
6628
6629assoc : arg_value tASSOC arg_value
6630 {
6631 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6632 /*% ripper: assoc_new!($:1, $:3) %*/
6633 }
6634 | tLABEL arg_value
6635 {
6636 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6637 /*% ripper: assoc_new!($:1, $:2) %*/
6638 }
6639 | tLABEL
6640 {
6641 NODE *val = gettable(p, $1, &@$);
6642 if (!val) val = NEW_ERROR(&@$);
6643 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6644 /*% ripper: assoc_new!($:1, Qnil) %*/
6645 }
6646 | tSTRING_BEG string_contents tLABEL_END arg_value
6647 {
6648 YYLTYPE loc = code_loc_gen(&@1, &@3);
6649 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6650 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6651 }
6652 | tDSTAR arg_value
6653 {
6654 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6655 /*% ripper: assoc_splat!($:2) %*/
6656 }
6657 | tDSTAR
6658 {
6659 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6660 $$ = list_append(p, NEW_LIST(0, &@$),
6661 NEW_LVAR(idFWD_KWREST, &@$));
6662 /*% ripper: assoc_splat!(Qnil) %*/
6663 }
6664 ;
6665
6666%rule %inline operation : ident_or_const
6667 | tFID
6668 ;
6669
6670operation2 : operation
6671 | op
6672 ;
6673
6674operation3 : tIDENTIFIER
6675 | tFID
6676 | op
6677 ;
6678
6679dot_or_colon : '.'
6680 | tCOLON2
6681 ;
6682
6683call_op : '.'
6684 | tANDDOT
6685 ;
6686
6687call_op2 : call_op
6688 | tCOLON2
6689 ;
6690
6691rparen : '\n'? ')'
6692 ;
6693
6694rbracket : '\n'? ']'
6695 ;
6696
6697rbrace : '\n'? '}'
6698 ;
6699
6700trailer : '\n'?
6701 | ','
6702 ;
6703
6704term : ';'
6705 {
6706 yyerrok;
6707 token_flush(p);
6708 if (p->ctxt.in_defined) {
6709 p->ctxt.has_trailing_semicolon = 1;
6710 }
6711 }
6712 | '\n'
6713 {
6714 @$.end_pos = @$.beg_pos;
6715 token_flush(p);
6716 }
6717 ;
6718
6719terms : term
6720 | terms ';' {yyerrok;}
6721 ;
6722
6723none : /* none */
6724 {
6725 $$ = 0;
6726 /*% ripper: Qnil %*/
6727 }
6728 ;
6729%%
6730# undef p
6731# undef yylex
6732# undef yylval
6733# define yylval (*p->lval)
6734
6735static int regx_options(struct parser_params*);
6736static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6737static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6738static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6739static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6740
6741#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6742
6743# define set_yylval_node(x) { \
6744 YYLTYPE _cur_loc; \
6745 rb_parser_set_location(p, &_cur_loc); \
6746 yylval.node = (x); \
6747 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6748}
6749# define set_yylval_str(x) \
6750do { \
6751 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6752 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6753} while(0)
6754# define set_yylval_num(x) { \
6755 yylval.num = (x); \
6756 set_parser_s_value(x); \
6757}
6758# define set_yylval_id(x) (yylval.id = (x))
6759# define set_yylval_name(x) { \
6760 (yylval.id = (x)); \
6761 set_parser_s_value(ID2SYM(x)); \
6762}
6763# define yylval_id() (yylval.id)
6764
6765#define set_yylval_noname() set_yylval_id(keyword_nil)
6766#define has_delayed_token(p) (p->delayed.token != NULL)
6767
6768#ifndef RIPPER
6769#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6770#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6771
6772static bool
6773parser_has_token(struct parser_params *p)
6774{
6775 const char *const pcur = p->lex.pcur;
6776 const char *const ptok = p->lex.ptok;
6777 if (p->keep_tokens && (pcur < ptok)) {
6778 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6779 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6780 }
6781 return pcur > ptok;
6782}
6783
6784static const char *
6785escaped_char(int c)
6786{
6787 switch (c) {
6788 case '"': return "\\\"";
6789 case '\\': return "\\\\";
6790 case '\0': return "\\0";
6791 case '\n': return "\\n";
6792 case '\r': return "\\r";
6793 case '\t': return "\\t";
6794 case '\f': return "\\f";
6795 case '\013': return "\\v";
6796 case '\010': return "\\b";
6797 case '\007': return "\\a";
6798 case '\033': return "\\e";
6799 case '\x7f': return "\\c?";
6800 }
6801 return NULL;
6802}
6803
6804static rb_parser_string_t *
6805rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6806{
6807 rb_encoding *enc = p->enc;
6808 const char *ptr = str->ptr;
6809 const char *pend = ptr + str->len;
6810 const char *prev = ptr;
6811 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6812 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6813
6814 while (ptr < pend) {
6815 unsigned int c;
6816 const char *cc;
6817 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6818 if (!MBCLEN_CHARFOUND_P(n)) {
6819 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6820 n = rb_enc_mbminlen(enc);
6821 if (pend < ptr + n)
6822 n = (int)(pend - ptr);
6823 while (n--) {
6824 c = *ptr & 0xf0 >> 4;
6825 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6826 c = *ptr & 0x0f;
6827 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6828 parser_str_cat(result, charbuf, 4);
6829 prev = ++ptr;
6830 }
6831 continue;
6832 }
6833 n = MBCLEN_CHARFOUND_LEN(n);
6834 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6835 ptr += n;
6836 cc = escaped_char(c);
6837 if (cc) {
6838 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6839 parser_str_cat_cstr(result, cc);
6840 prev = ptr;
6841 }
6842 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6843 }
6844 else {
6845 if (ptr - n > prev) {
6846 parser_str_cat(result, prev, ptr - n - prev);
6847 prev = ptr - n;
6848 }
6849 parser_str_cat(result, prev, ptr - prev);
6850 prev = ptr;
6851 }
6852 }
6853 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6854
6855 return result;
6856}
6857
6858static void
6859parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6860{
6861 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6862 token->id = p->token_id;
6863 token->type_name = parser_token2char(p, t);
6864 token->str = str;
6865 token->loc.beg_pos = p->yylloc->beg_pos;
6866 token->loc.end_pos = p->yylloc->end_pos;
6867 rb_parser_ary_push_ast_token(p, p->tokens, token);
6868 p->token_id++;
6869
6870 if (p->debug) {
6871 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6872 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6873 line, token->id, token->type_name, str_escaped->ptr,
6874 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6875 token->loc.end_pos.lineno, token->loc.end_pos.column);
6876 rb_parser_string_free(p, str_escaped);
6877 }
6878}
6879
6880static void
6881parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6882{
6883 debug_token_line(p, "parser_dispatch_scan_event", line);
6884
6885 if (!parser_has_token(p)) return;
6886
6887 RUBY_SET_YYLLOC(*p->yylloc);
6888
6889 if (p->keep_tokens) {
6890 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6891 parser_append_tokens(p, str, t, line);
6892 }
6893
6894 token_flush(p);
6895}
6896
6897#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6898static void
6899parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6900{
6901 debug_token_line(p, "parser_dispatch_delayed_token", line);
6902
6903 if (!has_delayed_token(p)) return;
6904
6905 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6906
6907 if (p->keep_tokens) {
6908 /* p->delayed.token is freed by rb_parser_tokens_free */
6909 parser_append_tokens(p, p->delayed.token, t, line);
6910 }
6911 else {
6912 rb_parser_string_free(p, p->delayed.token);
6913 }
6914
6915 p->delayed.token = NULL;
6916}
6917#else
6918#define literal_flush(p, ptr) ((void)(ptr))
6919
6920static int
6921ripper_has_scan_event(struct parser_params *p)
6922{
6923 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6924 return p->lex.pcur > p->lex.ptok;
6925}
6926
6927static VALUE
6928ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6929{
6930 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6931 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6932 RUBY_SET_YYLLOC(*p->yylloc);
6933 token_flush(p);
6934 return rval;
6935}
6936
6937static void
6938ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6939{
6940 if (!ripper_has_scan_event(p)) return;
6941
6942 set_parser_s_value(ripper_scan_event_val(p, t));
6943}
6944#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6945
6946static void
6947ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6948{
6949 /* save and adjust the location to delayed token for callbacks */
6950 int saved_line = p->ruby_sourceline;
6951 const char *saved_tokp = p->lex.ptok;
6952 VALUE s_value, str;
6953
6954 if (!has_delayed_token(p)) return;
6955 p->ruby_sourceline = p->delayed.beg_line;
6956 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6957 str = rb_str_new_mutable_parser_string(p->delayed.token);
6958 rb_parser_string_free(p, p->delayed.token);
6959 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6960 set_parser_s_value(s_value);
6961 p->delayed.token = NULL;
6962 p->ruby_sourceline = saved_line;
6963 p->lex.ptok = saved_tokp;
6964}
6965#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6966#endif /* RIPPER */
6967
6968static inline int
6969is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6970{
6971 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6972}
6973
6974static inline bool
6975peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
6976{
6977 const char *ptr = p->lex.pcur + at;
6978 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
6979 if (memcmp(ptr, str, len)) return false;
6980 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
6981 switch (ptr[len]) {
6982 case '!': case '?': return false;
6983 }
6984 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
6985}
6986
6987static inline int
6988parser_is_identchar(struct parser_params *p)
6989{
6990 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6991}
6992
6993static inline int
6994parser_isascii(struct parser_params *p)
6995{
6996 return ISASCII(*(p->lex.pcur-1));
6997}
6998
6999static void
7000token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7001{
7002 int column = 1, nonspc = 0, i;
7003 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7004 if (*ptr == '\t') {
7005 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7006 }
7007 column++;
7008 if (*ptr != ' ' && *ptr != '\t') {
7009 nonspc = 1;
7010 }
7011 }
7012
7013 ptinfo->beg = loc->beg_pos;
7014 ptinfo->indent = column;
7015 ptinfo->nonspc = nonspc;
7016}
7017
7018static void
7019token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7020{
7021 token_info *ptinfo;
7022
7023 if (!p->token_info_enabled) return;
7024 ptinfo = ALLOC(token_info);
7025 ptinfo->token = token;
7026 ptinfo->next = p->token_info;
7027 token_info_setup(ptinfo, p->lex.pbeg, loc);
7028
7029 p->token_info = ptinfo;
7030}
7031
7032static void
7033token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7034{
7035 token_info *ptinfo_beg = p->token_info;
7036
7037 if (!ptinfo_beg) return;
7038
7039 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7040 token_info_warn(p, token, ptinfo_beg, 1, loc);
7041
7042 p->token_info = ptinfo_beg->next;
7043 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7044}
7045
7046static void
7047token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7048{
7049 token_info *ptinfo_beg = p->token_info;
7050
7051 if (!ptinfo_beg) return;
7052 p->token_info = ptinfo_beg->next;
7053
7054 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7055 ptinfo_beg->beg.column != beg_pos.column ||
7056 strcmp(ptinfo_beg->token, token)) {
7057 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7058 beg_pos.lineno, beg_pos.column, token,
7059 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7060 ptinfo_beg->token);
7061 }
7062
7063 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7064}
7065
7066static void
7067token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7068{
7069 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7070 if (!p->token_info_enabled) return;
7071 if (!ptinfo_beg) return;
7072 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7073 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7074 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7075 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7076 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7077 rb_warn3L(ptinfo_end->beg.lineno,
7078 "mismatched indentations at '%s' with '%s' at %d",
7079 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7080}
7081
7082static int
7083parser_precise_mbclen(struct parser_params *p, const char *ptr)
7084{
7085 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7086 if (!MBCLEN_CHARFOUND_P(len)) {
7087 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7088 return -1;
7089 }
7090 return len;
7091}
7092
7093#ifndef RIPPER
7094static inline void
7095parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7096{
7097 rb_parser_string_t *str;
7098 int lineno = p->ruby_sourceline;
7099 if (!yylloc) {
7100 return;
7101 }
7102 else if (yylloc->beg_pos.lineno == lineno) {
7103 str = p->lex.lastline;
7104 }
7105 else {
7106 return;
7107 }
7108 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7109}
7110
7111static int
7112parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7113{
7114#if 0
7115 YYLTYPE current;
7116
7117 if (!yylloc) {
7118 yylloc = RUBY_SET_YYLLOC(current);
7119 }
7120 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7121 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7122 yylloc = 0;
7123 }
7124#endif
7125 parser_compile_error(p, yylloc, "%s", msg);
7126 parser_show_error_line(p, yylloc);
7127 return 0;
7128}
7129
7130static int
7131parser_yyerror0(struct parser_params *p, const char *msg)
7132{
7133 YYLTYPE current;
7134 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7135}
7136
7137void
7138ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7139{
7140 VALUE mesg;
7141 const int max_line_margin = 30;
7142 const char *ptr, *ptr_end, *pt, *pb;
7143 const char *pre = "", *post = "", *pend;
7144 const char *code = "", *caret = "";
7145 const char *lim;
7146 const char *const pbeg = PARSER_STRING_PTR(str);
7147 char *buf;
7148 long len;
7149 int i;
7150
7151 if (!yylloc) return;
7152 pend = rb_parser_string_end(str);
7153 if (pend > pbeg && pend[-1] == '\n') {
7154 if (--pend > pbeg && pend[-1] == '\r') --pend;
7155 }
7156
7157 pt = pend;
7158 if (lineno == yylloc->end_pos.lineno &&
7159 (pend - pbeg) > yylloc->end_pos.column) {
7160 pt = pbeg + yylloc->end_pos.column;
7161 }
7162
7163 ptr = ptr_end = pt;
7164 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7165 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7166
7167 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7168 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7169
7170 len = ptr_end - ptr;
7171 if (len > 4) {
7172 if (ptr > pbeg) {
7173 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7174 if (ptr > pbeg) pre = "...";
7175 }
7176 if (ptr_end < pend) {
7177 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7178 if (ptr_end < pend) post = "...";
7179 }
7180 }
7181 pb = pbeg;
7182 if (lineno == yylloc->beg_pos.lineno) {
7183 pb += yylloc->beg_pos.column;
7184 if (pb > pt) pb = pt;
7185 }
7186 if (pb < ptr) pb = ptr;
7187 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7188 return;
7189 }
7190 if (RTEST(errbuf)) {
7191 mesg = rb_attr_get(errbuf, idMesg);
7192 if (char_at_end(p, mesg, '\n') != '\n')
7193 rb_str_cat_cstr(mesg, "\n");
7194 }
7195 else {
7196 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7197 }
7198 if (!errbuf && rb_stderr_tty_p()) {
7199#define CSI_BEGIN "\033["
7200#define CSI_SGR "m"
7201 rb_str_catf(mesg,
7202 CSI_BEGIN""CSI_SGR"%s" /* pre */
7203 CSI_BEGIN"1"CSI_SGR"%.*s"
7204 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7205 CSI_BEGIN";1"CSI_SGR"%.*s"
7206 CSI_BEGIN""CSI_SGR"%s" /* post */
7207 "\n",
7208 pre,
7209 (int)(pb - ptr), ptr,
7210 (int)(pt - pb), pb,
7211 (int)(ptr_end - pt), pt,
7212 post);
7213 }
7214 else {
7215 char *p2;
7216
7217 len = ptr_end - ptr;
7218 lim = pt < pend ? pt : pend;
7219 i = (int)(lim - ptr);
7220 buf = ALLOCA_N(char, i+2);
7221 code = ptr;
7222 caret = p2 = buf;
7223 if (ptr <= pb) {
7224 while (ptr < pb) {
7225 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7226 }
7227 *p2++ = '^';
7228 ptr++;
7229 }
7230 if (lim > ptr) {
7231 memset(p2, '~', (lim - ptr));
7232 p2 += (lim - ptr);
7233 }
7234 *p2 = '\0';
7235 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7236 pre, (int)len, code, post,
7237 pre, caret);
7238 }
7239 if (!errbuf) rb_write_error_str(mesg);
7240}
7241#else
7242
7243static int
7244parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7245{
7246 const char *pcur = 0, *ptok = 0;
7247 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7248 p->ruby_sourceline == yylloc->end_pos.lineno) {
7249 pcur = p->lex.pcur;
7250 ptok = p->lex.ptok;
7251 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7252 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7253 }
7254 parser_yyerror0(p, msg);
7255 if (pcur) {
7256 p->lex.ptok = ptok;
7257 p->lex.pcur = pcur;
7258 }
7259 return 0;
7260}
7261
7262static int
7263parser_yyerror0(struct parser_params *p, const char *msg)
7264{
7265 dispatch1(parse_error, STR_NEW2(msg));
7266 ripper_error(p);
7267 return 0;
7268}
7269
7270static inline void
7271parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7272{
7273}
7274#endif /* !RIPPER */
7275
7276static int
7277vtable_size(const struct vtable *tbl)
7278{
7279 if (!DVARS_TERMINAL_P(tbl)) {
7280 return tbl->pos;
7281 }
7282 else {
7283 return 0;
7284 }
7285}
7286
7287static struct vtable *
7288vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7289{
7290 struct vtable *tbl = ALLOC(struct vtable);
7291 tbl->pos = 0;
7292 tbl->capa = 8;
7293 tbl->tbl = ALLOC_N(ID, tbl->capa);
7294 tbl->prev = prev;
7295#ifndef RIPPER
7296 if (p->debug) {
7297 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7298 }
7299#endif
7300 return tbl;
7301}
7302#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7303
7304static void
7305vtable_free_gen(struct parser_params *p, int line, const char *name,
7306 struct vtable *tbl)
7307{
7308#ifndef RIPPER
7309 if (p->debug) {
7310 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7311 }
7312#endif
7313 if (!DVARS_TERMINAL_P(tbl)) {
7314 if (tbl->tbl) {
7315 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7316 }
7317 ruby_sized_xfree(tbl, sizeof(*tbl));
7318 }
7319}
7320#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7321
7322static void
7323vtable_add_gen(struct parser_params *p, int line, const char *name,
7324 struct vtable *tbl, ID id)
7325{
7326#ifndef RIPPER
7327 if (p->debug) {
7328 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7329 line, name, (void *)tbl, rb_id2name(id));
7330 }
7331#endif
7332 if (DVARS_TERMINAL_P(tbl)) {
7333 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7334 return;
7335 }
7336 if (tbl->pos == tbl->capa) {
7337 tbl->capa = tbl->capa * 2;
7338 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7339 }
7340 tbl->tbl[tbl->pos++] = id;
7341}
7342#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7343
7344static void
7345vtable_pop_gen(struct parser_params *p, int line, const char *name,
7346 struct vtable *tbl, int n)
7347{
7348 if (p->debug) {
7349 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7350 line, name, (void *)tbl, n);
7351 }
7352 if (tbl->pos < n) {
7353 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7354 return;
7355 }
7356 tbl->pos -= n;
7357}
7358#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7359
7360static int
7361vtable_included(const struct vtable * tbl, ID id)
7362{
7363 int i;
7364
7365 if (!DVARS_TERMINAL_P(tbl)) {
7366 for (i = 0; i < tbl->pos; i++) {
7367 if (tbl->tbl[i] == id) {
7368 return i+1;
7369 }
7370 }
7371 }
7372 return 0;
7373}
7374
7375static void parser_prepare(struct parser_params *p);
7376
7377static int
7378e_option_supplied(struct parser_params *p)
7379{
7380 return strcmp(p->ruby_sourcefile, "-e") == 0;
7381}
7382
7383#ifndef RIPPER
7384static NODE *parser_append_options(struct parser_params *p, NODE *node);
7385
7386static VALUE
7387yycompile0(VALUE arg)
7388{
7389 int n;
7390 NODE *tree;
7391 struct parser_params *p = (struct parser_params *)arg;
7392 int cov = FALSE;
7393
7394 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7395 cov = TRUE;
7396 }
7397
7398 if (p->debug_lines) {
7399 p->ast->body.script_lines = p->debug_lines;
7400 }
7401
7402 parser_prepare(p);
7403#define RUBY_DTRACE_PARSE_HOOK(name) \
7404 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7405 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7406 }
7407 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7408 n = yyparse(p);
7409 RUBY_DTRACE_PARSE_HOOK(END);
7410
7411 p->debug_lines = 0;
7412
7413 xfree(p->lex.strterm);
7414 p->lex.strterm = 0;
7415 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7416 if (n || p->error_p) {
7417 VALUE mesg = p->error_buffer;
7418 if (!mesg) {
7419 mesg = syntax_error_new();
7420 }
7421 if (!p->error_tolerant) {
7422 rb_set_errinfo(mesg);
7423 return FALSE;
7424 }
7425 }
7426 tree = p->eval_tree;
7427 if (!tree) {
7428 tree = NEW_NIL(&NULL_LOC);
7429 }
7430 else {
7431 rb_parser_ary_t *tokens = p->tokens;
7432 NODE *prelude;
7433 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7434 prelude = block_append(p, p->eval_tree_begin, body);
7435 RNODE_SCOPE(tree)->nd_body = prelude;
7436 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7437 p->ast->body.coverage_enabled = cov;
7438 if (p->keep_tokens) {
7439 p->ast->node_buffer->tokens = tokens;
7440 p->tokens = NULL;
7441 }
7442 }
7443 p->ast->body.root = tree;
7444 p->ast->body.line_count = p->line_count;
7445 return TRUE;
7446}
7447
7448static rb_ast_t *
7449yycompile(struct parser_params *p, VALUE fname, int line)
7450{
7451 rb_ast_t *ast;
7452 if (NIL_P(fname)) {
7453 p->ruby_sourcefile_string = Qnil;
7454 p->ruby_sourcefile = "(none)";
7455 }
7456 else {
7457 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7458 p->ruby_sourcefile = StringValueCStr(fname);
7459 }
7460 p->ruby_sourceline = line - 1;
7461
7462 p->lvtbl = NULL;
7463
7464 p->ast = ast = rb_ast_new();
7465 compile_callback(yycompile0, (VALUE)p);
7466 p->ast = 0;
7467
7468 while (p->lvtbl) {
7469 local_pop(p);
7470 }
7471
7472 return ast;
7473}
7474#endif /* !RIPPER */
7475
7476static rb_encoding *
7477must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7478{
7479 rb_encoding *enc = rb_parser_str_get_encoding(s);
7480 if (!rb_enc_asciicompat(enc)) {
7481 rb_raise(rb_eArgError, "invalid source encoding");
7482 }
7483 return enc;
7484}
7485
7486static rb_parser_string_t *
7487lex_getline(struct parser_params *p)
7488{
7489 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7490 if (!line) return 0;
7491 p->line_count++;
7492 string_buffer_append(p, line);
7493 must_be_ascii_compatible(p, line);
7494 return line;
7495}
7496
7497#ifndef RIPPER
7498rb_ast_t*
7499rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7500{
7501 p->lex.gets = gets;
7502 p->lex.input = input;
7503 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7504
7505 return yycompile(p, fname, line);
7506}
7507#endif /* !RIPPER */
7508
7509#define STR_FUNC_ESCAPE 0x01
7510#define STR_FUNC_EXPAND 0x02
7511#define STR_FUNC_REGEXP 0x04
7512#define STR_FUNC_QWORDS 0x08
7513#define STR_FUNC_SYMBOL 0x10
7514#define STR_FUNC_INDENT 0x20
7515#define STR_FUNC_LABEL 0x40
7516#define STR_FUNC_LIST 0x4000
7517#define STR_FUNC_TERM 0x8000
7518
7519enum string_type {
7520 str_label = STR_FUNC_LABEL,
7521 str_squote = (0),
7522 str_dquote = (STR_FUNC_EXPAND),
7523 str_xquote = (STR_FUNC_EXPAND),
7524 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7525 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7526 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7527 str_ssym = (STR_FUNC_SYMBOL),
7528 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7529};
7530
7531static rb_parser_string_t *
7532parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7533{
7534 rb_parser_string_t *pstr;
7535
7536 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7537
7538 if (!(func & STR_FUNC_REGEXP)) {
7539 if (rb_parser_is_ascii_string(p, pstr)) {
7540 }
7541 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7542 /* everything is valid in ASCII-8BIT */
7543 enc = rb_ascii8bit_encoding();
7544 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7545 }
7546 }
7547
7548 return pstr;
7549}
7550
7551static int
7552strterm_is_heredoc(rb_strterm_t *strterm)
7553{
7554 return strterm->heredoc;
7555}
7556
7557static rb_strterm_t *
7558new_strterm(struct parser_params *p, int func, int term, int paren)
7559{
7560 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7561 strterm->u.literal.func = func;
7562 strterm->u.literal.term = term;
7563 strterm->u.literal.paren = paren;
7564 return strterm;
7565}
7566
7567static rb_strterm_t *
7568new_heredoc(struct parser_params *p)
7569{
7570 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7571 strterm->heredoc = true;
7572 return strterm;
7573}
7574
7575#define peek(p,c) peek_n(p, (c), 0)
7576#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7577#define peekc(p) peekc_n(p, 0)
7578#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7579
7580#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7581static void
7582parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7583{
7584 debug_token_line(p, "add_delayed_token", line);
7585
7586 if (tok < end) {
7587 if (has_delayed_token(p)) {
7588 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7589 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7590 int end_col = (next_line ? 0 : p->delayed.end_col);
7591 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7592 dispatch_delayed_token(p, tSTRING_CONTENT);
7593 }
7594 }
7595 if (!has_delayed_token(p)) {
7596 p->delayed.token = rb_parser_string_new(p, 0, 0);
7597 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7598 p->delayed.beg_line = p->ruby_sourceline;
7599 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7600 }
7601 parser_str_cat(p->delayed.token, tok, end - tok);
7602 p->delayed.end_line = p->ruby_sourceline;
7603 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7604 p->lex.ptok = end;
7605 }
7606}
7607
7608static void
7609set_lastline(struct parser_params *p, rb_parser_string_t *str)
7610{
7611 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7612 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7613 p->lex.lastline = str;
7614}
7615
7616static int
7617nextline(struct parser_params *p, int set_encoding)
7618{
7619 rb_parser_string_t *str = p->lex.nextline;
7620 p->lex.nextline = 0;
7621 if (!str) {
7622 if (p->eofp)
7623 return -1;
7624
7625 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7626 goto end_of_input;
7627 }
7628
7629 if (!p->lex.input || !(str = lex_getline(p))) {
7630 end_of_input:
7631 p->eofp = 1;
7632 lex_goto_eol(p);
7633 return -1;
7634 }
7635#ifndef RIPPER
7636 if (p->debug_lines) {
7637 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7638 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7639 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7640 }
7641#endif
7642 p->cr_seen = FALSE;
7643 }
7644 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7645 /* after here-document without terminator */
7646 goto end_of_input;
7647 }
7648 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7649 if (p->heredoc_end > 0) {
7650 p->ruby_sourceline = p->heredoc_end;
7651 p->heredoc_end = 0;
7652 }
7653 p->ruby_sourceline++;
7654 set_lastline(p, str);
7655 token_flush(p);
7656 return 0;
7657}
7658
7659static int
7660parser_cr(struct parser_params *p, int c)
7661{
7662 if (peek(p, '\n')) {
7663 p->lex.pcur++;
7664 c = '\n';
7665 }
7666 return c;
7667}
7668
7669static inline int
7670nextc0(struct parser_params *p, int set_encoding)
7671{
7672 int c;
7673
7674 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7675 if (nextline(p, set_encoding)) return -1;
7676 }
7677 c = (unsigned char)*p->lex.pcur++;
7678 if (UNLIKELY(c == '\r')) {
7679 c = parser_cr(p, c);
7680 }
7681
7682 return c;
7683}
7684#define nextc(p) nextc0(p, TRUE)
7685
7686static void
7687pushback(struct parser_params *p, int c)
7688{
7689 if (c == -1) return;
7690 p->eofp = 0;
7691 p->lex.pcur--;
7692 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7693 p->lex.pcur--;
7694 }
7695}
7696
7697#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7698
7699#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7700#define tok(p) (p)->tokenbuf
7701#define toklen(p) (p)->tokidx
7702
7703static int
7704looking_at_eol_p(struct parser_params *p)
7705{
7706 const char *ptr = p->lex.pcur;
7707 while (!lex_eol_ptr_p(p, ptr)) {
7708 int c = (unsigned char)*ptr++;
7709 int eol = (c == '\n' || c == '#');
7710 if (eol || !ISSPACE(c)) {
7711 return eol;
7712 }
7713 }
7714 return TRUE;
7715}
7716
7717static char*
7718newtok(struct parser_params *p)
7719{
7720 p->tokidx = 0;
7721 if (!p->tokenbuf) {
7722 p->toksiz = 60;
7723 p->tokenbuf = ALLOC_N(char, 60);
7724 }
7725 if (p->toksiz > 4096) {
7726 p->toksiz = 60;
7727 REALLOC_N(p->tokenbuf, char, 60);
7728 }
7729 return p->tokenbuf;
7730}
7731
7732static char *
7733tokspace(struct parser_params *p, int n)
7734{
7735 p->tokidx += n;
7736
7737 if (p->tokidx >= p->toksiz) {
7738 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7739 REALLOC_N(p->tokenbuf, char, p->toksiz);
7740 }
7741 return &p->tokenbuf[p->tokidx-n];
7742}
7743
7744static void
7745tokadd(struct parser_params *p, int c)
7746{
7747 p->tokenbuf[p->tokidx++] = (char)c;
7748 if (p->tokidx >= p->toksiz) {
7749 p->toksiz *= 2;
7750 REALLOC_N(p->tokenbuf, char, p->toksiz);
7751 }
7752}
7753
7754static int
7755tok_hex(struct parser_params *p, size_t *numlen)
7756{
7757 int c;
7758
7759 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7760 if (!*numlen) {
7761 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7762 yyerror0("invalid hex escape");
7763 dispatch_scan_event(p, tSTRING_CONTENT);
7764 return 0;
7765 }
7766 p->lex.pcur += *numlen;
7767 return c;
7768}
7769
7770#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7771
7772static int
7773escaped_control_code(int c)
7774{
7775 int c2 = 0;
7776 switch (c) {
7777 case ' ':
7778 c2 = 's';
7779 break;
7780 case '\n':
7781 c2 = 'n';
7782 break;
7783 case '\t':
7784 c2 = 't';
7785 break;
7786 case '\v':
7787 c2 = 'v';
7788 break;
7789 case '\r':
7790 c2 = 'r';
7791 break;
7792 case '\f':
7793 c2 = 'f';
7794 break;
7795 }
7796 return c2;
7797}
7798
7799#define WARN_SPACE_CHAR(c, prefix) \
7800 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7801
7802static int
7803tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7804 int regexp_literal, const char *begin)
7805{
7806 const int wide = !begin;
7807 size_t numlen;
7808 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7809
7810 p->lex.pcur += numlen;
7811 if (p->lex.strterm == NULL ||
7812 strterm_is_heredoc(p->lex.strterm) ||
7813 (p->lex.strterm->u.literal.func != str_regexp)) {
7814 if (!begin) begin = p->lex.pcur;
7815 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7816 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7817 yyerror0("invalid Unicode escape");
7818 dispatch_scan_event(p, tSTRING_CONTENT);
7819 return wide && numlen > 0;
7820 }
7821 if (codepoint > 0x10ffff) {
7822 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7823 yyerror0("invalid Unicode codepoint (too large)");
7824 dispatch_scan_event(p, tSTRING_CONTENT);
7825 return wide;
7826 }
7827 if ((codepoint & 0xfffff800) == 0xd800) {
7828 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7829 yyerror0("invalid Unicode codepoint");
7830 dispatch_scan_event(p, tSTRING_CONTENT);
7831 return wide;
7832 }
7833 }
7834 if (regexp_literal) {
7835 tokcopy(p, (int)numlen);
7836 }
7837 else if (codepoint >= 0x80) {
7838 rb_encoding *utf8 = rb_utf8_encoding();
7839 if (*encp && utf8 != *encp) {
7840 YYLTYPE loc = RUBY_INIT_YYLLOC();
7841 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7842 parser_show_error_line(p, &loc);
7843 return wide;
7844 }
7845 *encp = utf8;
7846 tokaddmbc(p, codepoint, *encp);
7847 }
7848 else {
7849 tokadd(p, codepoint);
7850 }
7851 return TRUE;
7852}
7853
7854static int tokadd_mbchar(struct parser_params *p, int c);
7855
7856static int
7857tokskip_mbchar(struct parser_params *p)
7858{
7859 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7860 if (len > 0) {
7861 p->lex.pcur += len - 1;
7862 }
7863 return len;
7864}
7865
7866/* return value is for ?\u3042 */
7867static void
7868tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7869 int term, int symbol_literal, int regexp_literal)
7870{
7871 /*
7872 * If `term` is not -1, then we allow multiple codepoints in \u{}
7873 * upto `term` byte, otherwise we're parsing a character literal.
7874 * And then add the codepoints to the current token.
7875 */
7876 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7877
7878 const int open_brace = '{', close_brace = '}';
7879
7880 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7881
7882 if (peek(p, open_brace)) { /* handle \u{...} form */
7883 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7884 /*
7885 * Skip parsing validation code and copy bytes as-is until term or
7886 * closing brace, in order to correctly handle extended regexps where
7887 * invalid unicode escapes are allowed in comments. The regexp parser
7888 * does its own validation and will catch any issues.
7889 */
7890 tokadd(p, open_brace);
7891 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7892 int c = peekc(p);
7893 if (c == close_brace) {
7894 tokadd(p, c);
7895 ++p->lex.pcur;
7896 break;
7897 }
7898 else if (c == term) {
7899 break;
7900 }
7901 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7902 tokadd(p, c);
7903 c = *++p->lex.pcur;
7904 }
7905 tokadd_mbchar(p, c);
7906 }
7907 }
7908 else {
7909 const char *second = NULL;
7910 int c, last = nextc(p);
7911 if (lex_eol_p(p)) goto unterminated;
7912 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7913 while (c != close_brace) {
7914 if (c == term) goto unterminated;
7915 if (second == multiple_codepoints)
7916 second = p->lex.pcur;
7917 if (regexp_literal) tokadd(p, last);
7918 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7919 break;
7920 }
7921 while (ISSPACE(c = peekc(p))) {
7922 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7923 last = c;
7924 }
7925 if (term == -1 && !second)
7926 second = multiple_codepoints;
7927 }
7928
7929 if (c != close_brace) {
7930 unterminated:
7931 flush_string_content(p, rb_utf8_encoding(), 0);
7932 yyerror0("unterminated Unicode escape");
7933 dispatch_scan_event(p, tSTRING_CONTENT);
7934 return;
7935 }
7936 if (second && second != multiple_codepoints) {
7937 const char *pcur = p->lex.pcur;
7938 p->lex.pcur = second;
7939 dispatch_scan_event(p, tSTRING_CONTENT);
7940 token_flush(p);
7941 p->lex.pcur = pcur;
7942 yyerror0(multiple_codepoints);
7943 token_flush(p);
7944 }
7945
7946 if (regexp_literal) tokadd(p, close_brace);
7947 nextc(p);
7948 }
7949 }
7950 else { /* handle \uxxxx form */
7951 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7952 token_flush(p);
7953 return;
7954 }
7955 }
7956}
7957
7958#define ESCAPE_CONTROL 1
7959#define ESCAPE_META 2
7960
7961static int
7962read_escape(struct parser_params *p, int flags, const char *begin)
7963{
7964 int c;
7965 size_t numlen;
7966
7967 switch (c = nextc(p)) {
7968 case '\\': /* Backslash */
7969 return c;
7970
7971 case 'n': /* newline */
7972 return '\n';
7973
7974 case 't': /* horizontal tab */
7975 return '\t';
7976
7977 case 'r': /* carriage-return */
7978 return '\r';
7979
7980 case 'f': /* form-feed */
7981 return '\f';
7982
7983 case 'v': /* vertical tab */
7984 return '\13';
7985
7986 case 'a': /* alarm(bell) */
7987 return '\007';
7988
7989 case 'e': /* escape */
7990 return 033;
7991
7992 case '0': case '1': case '2': case '3': /* octal constant */
7993 case '4': case '5': case '6': case '7':
7994 pushback(p, c);
7995 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
7996 p->lex.pcur += numlen;
7997 return c;
7998
7999 case 'x': /* hex constant */
8000 c = tok_hex(p, &numlen);
8001 if (numlen == 0) return 0;
8002 return c;
8003
8004 case 'b': /* backspace */
8005 return '\010';
8006
8007 case 's': /* space */
8008 return ' ';
8009
8010 case 'M':
8011 if (flags & ESCAPE_META) goto eof;
8012 if ((c = nextc(p)) != '-') {
8013 goto eof;
8014 }
8015 if ((c = nextc(p)) == '\\') {
8016 switch (peekc(p)) {
8017 case 'u': case 'U':
8018 nextc(p);
8019 goto eof;
8020 }
8021 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8022 }
8023 else if (c == -1) goto eof;
8024 else if (!ISASCII(c)) {
8025 tokskip_mbchar(p);
8026 goto eof;
8027 }
8028 else {
8029 int c2 = escaped_control_code(c);
8030 if (c2) {
8031 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8032 WARN_SPACE_CHAR(c2, "\\M-");
8033 }
8034 else {
8035 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8036 }
8037 }
8038 else if (ISCNTRL(c)) goto eof;
8039 return ((c & 0xff) | 0x80);
8040 }
8041
8042 case 'C':
8043 if ((c = nextc(p)) != '-') {
8044 goto eof;
8045 }
8046 case 'c':
8047 if (flags & ESCAPE_CONTROL) goto eof;
8048 if ((c = nextc(p))== '\\') {
8049 switch (peekc(p)) {
8050 case 'u': case 'U':
8051 nextc(p);
8052 goto eof;
8053 }
8054 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8055 }
8056 else if (c == '?')
8057 return 0177;
8058 else if (c == -1) goto eof;
8059 else if (!ISASCII(c)) {
8060 tokskip_mbchar(p);
8061 goto eof;
8062 }
8063 else {
8064 int c2 = escaped_control_code(c);
8065 if (c2) {
8066 if (ISCNTRL(c)) {
8067 if (flags & ESCAPE_META) {
8068 WARN_SPACE_CHAR(c2, "\\M-");
8069 }
8070 else {
8071 WARN_SPACE_CHAR(c2, "");
8072 }
8073 }
8074 else {
8075 if (flags & ESCAPE_META) {
8076 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8077 }
8078 else {
8079 WARN_SPACE_CHAR(c2, "\\C-");
8080 }
8081 }
8082 }
8083 else if (ISCNTRL(c)) goto eof;
8084 }
8085 return c & 0x9f;
8086
8087 eof:
8088 case -1:
8089 flush_string_content(p, p->enc, p->lex.pcur - begin);
8090 yyerror0("Invalid escape character syntax");
8091 dispatch_scan_event(p, tSTRING_CONTENT);
8092 return '\0';
8093
8094 default:
8095 if (!ISASCII(c)) {
8096 tokskip_mbchar(p);
8097 goto eof;
8098 }
8099 return c;
8100 }
8101}
8102
8103static void
8104tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8105{
8106 int len = rb_enc_codelen(c, enc);
8107 rb_enc_mbcput(c, tokspace(p, len), enc);
8108}
8109
8110static int
8111tokadd_escape(struct parser_params *p)
8112{
8113 int c;
8114 size_t numlen;
8115 const char *begin = p->lex.pcur;
8116
8117 switch (c = nextc(p)) {
8118 case '\n':
8119 return 0; /* just ignore */
8120
8121 case '0': case '1': case '2': case '3': /* octal constant */
8122 case '4': case '5': case '6': case '7':
8123 {
8124 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8125 if (numlen == 0) goto eof;
8126 p->lex.pcur += numlen;
8127 tokcopy(p, (int)numlen + 1);
8128 }
8129 return 0;
8130
8131 case 'x': /* hex constant */
8132 {
8133 tok_hex(p, &numlen);
8134 if (numlen == 0) return -1;
8135 tokcopy(p, (int)numlen + 2);
8136 }
8137 return 0;
8138
8139 eof:
8140 case -1:
8141 flush_string_content(p, p->enc, p->lex.pcur - begin);
8142 yyerror0("Invalid escape character syntax");
8143 token_flush(p);
8144 return -1;
8145
8146 default:
8147 tokadd(p, '\\');
8148 tokadd(p, c);
8149 }
8150 return 0;
8151}
8152
8153static int
8154char_to_option(int c)
8155{
8156 int val;
8157
8158 switch (c) {
8159 case 'i':
8160 val = RE_ONIG_OPTION_IGNORECASE;
8161 break;
8162 case 'x':
8163 val = RE_ONIG_OPTION_EXTEND;
8164 break;
8165 case 'm':
8166 val = RE_ONIG_OPTION_MULTILINE;
8167 break;
8168 default:
8169 val = 0;
8170 break;
8171 }
8172 return val;
8173}
8174
8175#define ARG_ENCODING_FIXED 16
8176#define ARG_ENCODING_NONE 32
8177#define ENC_ASCII8BIT 1
8178#define ENC_EUC_JP 2
8179#define ENC_Windows_31J 3
8180#define ENC_UTF8 4
8181
8182static int
8183char_to_option_kcode(int c, int *option, int *kcode)
8184{
8185 *option = 0;
8186
8187 switch (c) {
8188 case 'n':
8189 *kcode = ENC_ASCII8BIT;
8190 return (*option = ARG_ENCODING_NONE);
8191 case 'e':
8192 *kcode = ENC_EUC_JP;
8193 break;
8194 case 's':
8195 *kcode = ENC_Windows_31J;
8196 break;
8197 case 'u':
8198 *kcode = ENC_UTF8;
8199 break;
8200 default:
8201 *kcode = -1;
8202 return (*option = char_to_option(c));
8203 }
8204 *option = ARG_ENCODING_FIXED;
8205 return 1;
8206}
8207
8208static int
8209regx_options(struct parser_params *p)
8210{
8211 int kcode = 0;
8212 int kopt = 0;
8213 int options = 0;
8214 int c, opt, kc;
8215
8216 newtok(p);
8217 while (c = nextc(p), ISALPHA(c)) {
8218 if (c == 'o') {
8219 options |= RE_OPTION_ONCE;
8220 }
8221 else if (char_to_option_kcode(c, &opt, &kc)) {
8222 if (kc >= 0) {
8223 if (kc != ENC_ASCII8BIT) kcode = c;
8224 kopt = opt;
8225 }
8226 else {
8227 options |= opt;
8228 }
8229 }
8230 else {
8231 tokadd(p, c);
8232 }
8233 }
8234 options |= kopt;
8235 pushback(p, c);
8236 if (toklen(p)) {
8237 YYLTYPE loc = RUBY_INIT_YYLLOC();
8238 tokfix(p);
8239 compile_error(p, "unknown regexp option%s - %*s",
8240 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8241 parser_show_error_line(p, &loc);
8242 }
8243 return options | RE_OPTION_ENCODING(kcode);
8244}
8245
8246static int
8247tokadd_mbchar(struct parser_params *p, int c)
8248{
8249 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8250 if (len < 0) return -1;
8251 tokadd(p, c);
8252 p->lex.pcur += --len;
8253 if (len > 0) tokcopy(p, len);
8254 return c;
8255}
8256
8257static inline int
8258simple_re_meta(int c)
8259{
8260 switch (c) {
8261 case '$': case '*': case '+': case '.':
8262 case '?': case '^': case '|':
8263 case ')': case ']': case '}': case '>':
8264 return TRUE;
8265 default:
8266 return FALSE;
8267 }
8268}
8269
8270static int
8271parser_update_heredoc_indent(struct parser_params *p, int c)
8272{
8273 if (p->heredoc_line_indent == -1) {
8274 if (c == '\n') p->heredoc_line_indent = 0;
8275 }
8276 else {
8277 if (c == ' ') {
8278 p->heredoc_line_indent++;
8279 return TRUE;
8280 }
8281 else if (c == '\t') {
8282 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8283 p->heredoc_line_indent = w * TAB_WIDTH;
8284 return TRUE;
8285 }
8286 else if (c != '\n') {
8287 if (p->heredoc_indent > p->heredoc_line_indent) {
8288 p->heredoc_indent = p->heredoc_line_indent;
8289 }
8290 p->heredoc_line_indent = -1;
8291 }
8292 else {
8293 /* Whitespace only line has no indentation */
8294 p->heredoc_line_indent = 0;
8295 }
8296 }
8297 return FALSE;
8298}
8299
8300static void
8301parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8302{
8303 YYLTYPE loc = RUBY_INIT_YYLLOC();
8304 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8305 compile_error(p, "%s mixed within %s source", n1, n2);
8306 parser_show_error_line(p, &loc);
8307}
8308
8309static void
8310parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8311{
8312 const char *pos = p->lex.pcur;
8313 p->lex.pcur = beg;
8314 parser_mixed_error(p, enc1, enc2);
8315 p->lex.pcur = pos;
8316}
8317
8318static inline char
8319nibble_char_upper(unsigned int c)
8320{
8321 c &= 0xf;
8322 return c + (c < 10 ? '0' : 'A' - 10);
8323}
8324
8325static int
8326tokadd_string(struct parser_params *p,
8327 int func, int term, int paren, long *nest,
8328 rb_encoding **encp, rb_encoding **enc)
8329{
8330 int c;
8331 bool erred = false;
8332#ifdef RIPPER
8333 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8334 int top_of_line = FALSE;
8335#endif
8336
8337#define mixed_error(enc1, enc2) \
8338 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8339#define mixed_escape(beg, enc1, enc2) \
8340 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8341
8342 while ((c = nextc(p)) != -1) {
8343 if (p->heredoc_indent > 0) {
8344 parser_update_heredoc_indent(p, c);
8345 }
8346#ifdef RIPPER
8347 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8348 pushback(p, c);
8349 break;
8350 }
8351#endif
8352
8353 if (paren && c == paren) {
8354 ++*nest;
8355 }
8356 else if (c == term) {
8357 if (!nest || !*nest) {
8358 pushback(p, c);
8359 break;
8360 }
8361 --*nest;
8362 }
8363 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8364 unsigned char c2 = *p->lex.pcur;
8365 if (c2 == '$' || c2 == '@' || c2 == '{') {
8366 pushback(p, c);
8367 break;
8368 }
8369 }
8370 else if (c == '\\') {
8371 c = nextc(p);
8372 switch (c) {
8373 case '\n':
8374 if (func & STR_FUNC_QWORDS) break;
8375 if (func & STR_FUNC_EXPAND) {
8376 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8377 continue;
8378 if (c == term) {
8379 c = '\\';
8380 goto terminate;
8381 }
8382 }
8383 tokadd(p, '\\');
8384 break;
8385
8386 case '\\':
8387 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8388 break;
8389
8390 case 'u':
8391 if ((func & STR_FUNC_EXPAND) == 0) {
8392 tokadd(p, '\\');
8393 break;
8394 }
8395 tokadd_utf8(p, enc, term,
8396 func & STR_FUNC_SYMBOL,
8397 func & STR_FUNC_REGEXP);
8398 continue;
8399
8400 default:
8401 if (c == -1) return -1;
8402 if (!ISASCII(c)) {
8403 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8404 goto non_ascii;
8405 }
8406 if (func & STR_FUNC_REGEXP) {
8407 switch (c) {
8408 case 'c':
8409 case 'C':
8410 case 'M': {
8411 pushback(p, c);
8412 c = read_escape(p, 0, p->lex.pcur - 1);
8413
8414 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8415 *t++ = '\\';
8416 *t++ = 'x';
8417 *t++ = nibble_char_upper(c >> 4);
8418 *t++ = nibble_char_upper(c);
8419 continue;
8420 }
8421 }
8422
8423 if (c == term && !simple_re_meta(c)) {
8424 tokadd(p, c);
8425 continue;
8426 }
8427 pushback(p, c);
8428 if ((c = tokadd_escape(p)) < 0)
8429 return -1;
8430 if (*enc && *enc != *encp) {
8431 mixed_escape(p->lex.ptok+2, *enc, *encp);
8432 }
8433 continue;
8434 }
8435 else if (func & STR_FUNC_EXPAND) {
8436 pushback(p, c);
8437 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8438 c = read_escape(p, 0, p->lex.pcur - 1);
8439 }
8440 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8441 /* ignore backslashed spaces in %w */
8442 }
8443 else if (c != term && !(paren && c == paren)) {
8444 tokadd(p, '\\');
8445 pushback(p, c);
8446 continue;
8447 }
8448 }
8449 }
8450 else if (!parser_isascii(p)) {
8451 non_ascii:
8452 if (!*enc) {
8453 *enc = *encp;
8454 }
8455 else if (*enc != *encp) {
8456 mixed_error(*enc, *encp);
8457 continue;
8458 }
8459 if (tokadd_mbchar(p, c) == -1) return -1;
8460 continue;
8461 }
8462 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8463 pushback(p, c);
8464 break;
8465 }
8466 if (c & 0x80) {
8467 if (!*enc) {
8468 *enc = *encp;
8469 }
8470 else if (*enc != *encp) {
8471 mixed_error(*enc, *encp);
8472 continue;
8473 }
8474 }
8475 tokadd(p, c);
8476#ifdef RIPPER
8477 top_of_line = (c == '\n');
8478#endif
8479 }
8480 terminate:
8481 if (*enc) *encp = *enc;
8482 return c;
8483}
8484
8485#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8486
8487static void
8488flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8489{
8490 p->lex.pcur -= back;
8491 if (has_delayed_token(p)) {
8492 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8493 if (len > 0) {
8494 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8495 p->delayed.end_line = p->ruby_sourceline;
8496 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8497 }
8498 dispatch_delayed_token(p, tSTRING_CONTENT);
8499 p->lex.ptok = p->lex.pcur;
8500 }
8501 dispatch_scan_event(p, tSTRING_CONTENT);
8502 p->lex.pcur += back;
8503}
8504
8505/* this can be shared with ripper, since it's independent from struct
8506 * parser_params. */
8507#ifndef RIPPER
8508#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8509#define SPECIAL_PUNCT(idx) ( \
8510 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8511 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8512 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8513 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8514 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8515 BIT('0', idx))
8516const uint_least32_t ruby_global_name_punct_bits[] = {
8517 SPECIAL_PUNCT(0),
8518 SPECIAL_PUNCT(1),
8519 SPECIAL_PUNCT(2),
8520};
8521#undef BIT
8522#undef SPECIAL_PUNCT
8523#endif
8524
8525static enum yytokentype
8526parser_peek_variable_name(struct parser_params *p)
8527{
8528 int c;
8529 const char *ptr = p->lex.pcur;
8530
8531 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8532 c = *ptr++;
8533 switch (c) {
8534 case '$':
8535 if ((c = *ptr) == '-') {
8536 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8537 c = *ptr;
8538 }
8539 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8540 return tSTRING_DVAR;
8541 }
8542 break;
8543 case '@':
8544 if ((c = *ptr) == '@') {
8545 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8546 c = *ptr;
8547 }
8548 break;
8549 case '{':
8550 p->lex.pcur = ptr;
8551 p->command_start = TRUE;
8552 yylval.state = p->lex.state;
8553 return tSTRING_DBEG;
8554 default:
8555 return 0;
8556 }
8557 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8558 return tSTRING_DVAR;
8559 return 0;
8560}
8561
8562#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8563#define IS_END() IS_lex_state(EXPR_END_ANY)
8564#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8565#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8566#define IS_LABEL_POSSIBLE() (\
8567 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8568 IS_ARG())
8569#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8570#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8571
8572static inline enum yytokentype
8573parser_string_term(struct parser_params *p, int func)
8574{
8575 xfree(p->lex.strterm);
8576 p->lex.strterm = 0;
8577 if (func & STR_FUNC_REGEXP) {
8578 set_yylval_num(regx_options(p));
8579 dispatch_scan_event(p, tREGEXP_END);
8580 SET_LEX_STATE(EXPR_END);
8581 return tREGEXP_END;
8582 }
8583 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8584 nextc(p);
8585 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8586 return tLABEL_END;
8587 }
8588 SET_LEX_STATE(EXPR_END);
8589 return tSTRING_END;
8590}
8591
8592static enum yytokentype
8593parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8594{
8595 int func = quote->func;
8596 int term = quote->term;
8597 int paren = quote->paren;
8598 int c, space = 0;
8599 rb_encoding *enc = p->enc;
8600 rb_encoding *base_enc = 0;
8601 rb_parser_string_t *lit;
8602
8603 if (func & STR_FUNC_TERM) {
8604 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8605 SET_LEX_STATE(EXPR_END);
8606 xfree(p->lex.strterm);
8607 p->lex.strterm = 0;
8608 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8609 }
8610 c = nextc(p);
8611 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8612 while (c != '\n' && ISSPACE(c = nextc(p)));
8613 space = 1;
8614 }
8615 if (func & STR_FUNC_LIST) {
8616 quote->func &= ~STR_FUNC_LIST;
8617 space = 1;
8618 }
8619 if (c == term && !quote->nest) {
8620 if (func & STR_FUNC_QWORDS) {
8621 quote->func |= STR_FUNC_TERM;
8622 pushback(p, c); /* dispatch the term at tSTRING_END */
8623 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8624 return ' ';
8625 }
8626 return parser_string_term(p, func);
8627 }
8628 if (space) {
8629 if (!ISSPACE(c)) pushback(p, c);
8630 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8631 return ' ';
8632 }
8633 newtok(p);
8634 if ((func & STR_FUNC_EXPAND) && c == '#') {
8635 enum yytokentype t = parser_peek_variable_name(p);
8636 if (t) return t;
8637 tokadd(p, '#');
8638 c = nextc(p);
8639 }
8640 pushback(p, c);
8641 if (tokadd_string(p, func, term, paren, &quote->nest,
8642 &enc, &base_enc) == -1) {
8643 if (p->eofp) {
8644#ifndef RIPPER
8645# define unterminated_literal(mesg) yyerror0(mesg)
8646#else
8647# define unterminated_literal(mesg) compile_error(p, mesg)
8648#endif
8649 literal_flush(p, p->lex.pcur);
8650 if (func & STR_FUNC_QWORDS) {
8651 /* no content to add, bailing out here */
8652 unterminated_literal("unterminated list meets end of file");
8653 xfree(p->lex.strterm);
8654 p->lex.strterm = 0;
8655 return tSTRING_END;
8656 }
8657 if (func & STR_FUNC_REGEXP) {
8658 unterminated_literal("unterminated regexp meets end of file");
8659 }
8660 else {
8661 unterminated_literal("unterminated string meets end of file");
8662 }
8663 quote->func |= STR_FUNC_TERM;
8664 }
8665 }
8666
8667 tokfix(p);
8668 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8669 set_yylval_str(lit);
8670 flush_string_content(p, enc, 0);
8671
8672 return tSTRING_CONTENT;
8673}
8674
8675static enum yytokentype
8676heredoc_identifier(struct parser_params *p)
8677{
8678 /*
8679 * term_len is length of `<<"END"` except `END`,
8680 * in this case term_len is 4 (<, <, " and ").
8681 */
8682 long len, offset = p->lex.pcur - p->lex.pbeg;
8683 int c = nextc(p), term, func = 0, quote = 0;
8684 enum yytokentype token = tSTRING_BEG;
8685 int indent = 0;
8686
8687 if (c == '-') {
8688 c = nextc(p);
8689 func = STR_FUNC_INDENT;
8690 offset++;
8691 }
8692 else if (c == '~') {
8693 c = nextc(p);
8694 func = STR_FUNC_INDENT;
8695 offset++;
8696 indent = INT_MAX;
8697 }
8698 switch (c) {
8699 case '\'':
8700 func |= str_squote; goto quoted;
8701 case '"':
8702 func |= str_dquote; goto quoted;
8703 case '`':
8704 token = tXSTRING_BEG;
8705 func |= str_xquote; goto quoted;
8706
8707 quoted:
8708 quote++;
8709 offset++;
8710 term = c;
8711 len = 0;
8712 while ((c = nextc(p)) != term) {
8713 if (c == -1 || c == '\r' || c == '\n') {
8714 yyerror0("unterminated here document identifier");
8715 return -1;
8716 }
8717 }
8718 break;
8719
8720 default:
8721 if (!parser_is_identchar(p)) {
8722 pushback(p, c);
8723 if (func & STR_FUNC_INDENT) {
8724 pushback(p, indent > 0 ? '~' : '-');
8725 }
8726 return 0;
8727 }
8728 func |= str_dquote;
8729 do {
8730 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8731 if (n < 0) return 0;
8732 p->lex.pcur += --n;
8733 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8734 pushback(p, c);
8735 break;
8736 }
8737
8738 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8739 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8740 yyerror0("too long here document identifier");
8741 dispatch_scan_event(p, tHEREDOC_BEG);
8742 lex_goto_eol(p);
8743
8744 p->lex.strterm = new_heredoc(p);
8745 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8746 here->offset = offset;
8747 here->sourceline = p->ruby_sourceline;
8748 here->length = (unsigned)len;
8749 here->quote = quote;
8750 here->func = func;
8751 here->lastline = p->lex.lastline;
8752
8753 token_flush(p);
8754 p->heredoc_indent = indent;
8755 p->heredoc_line_indent = 0;
8756 return token;
8757}
8758
8759static void
8760heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8761{
8762 rb_parser_string_t *line;
8763 rb_strterm_t *term = p->lex.strterm;
8764
8765 p->lex.strterm = 0;
8766 line = here->lastline;
8767 p->lex.lastline = line;
8768 p->lex.pbeg = PARSER_STRING_PTR(line);
8769 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8770 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8771 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8772 p->heredoc_end = p->ruby_sourceline;
8773 p->ruby_sourceline = (int)here->sourceline;
8774 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8775 p->eofp = 0;
8776 xfree(term);
8777}
8778
8779static int
8780dedent_string_column(const char *str, long len, int width)
8781{
8782 int i, col = 0;
8783
8784 for (i = 0; i < len && col < width; i++) {
8785 if (str[i] == ' ') {
8786 col++;
8787 }
8788 else if (str[i] == '\t') {
8789 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8790 if (n > width) break;
8791 col = n;
8792 }
8793 else {
8794 break;
8795 }
8796 }
8797
8798 return i;
8799}
8800
8801static int
8802dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8803{
8804 char *str;
8805 long len;
8806 int i;
8807
8808 len = PARSER_STRING_LEN(string);
8809 str = PARSER_STRING_PTR(string);
8810
8811 i = dedent_string_column(str, len, width);
8812 if (!i) return 0;
8813
8814 rb_parser_str_modify(string);
8815 str = PARSER_STRING_PTR(string);
8816 if (PARSER_STRING_LEN(string) != len)
8817 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8818 MEMMOVE(str, str + i, char, len - i);
8819 rb_parser_str_set_len(p, string, len - i);
8820 return i;
8821}
8822
8823static NODE *
8824heredoc_dedent(struct parser_params *p, NODE *root)
8825{
8826 NODE *node, *str_node, *prev_node;
8827 int indent = p->heredoc_indent;
8828 rb_parser_string_t *prev_lit = 0;
8829
8830 if (indent <= 0) return root;
8831 if (!root) return root;
8832
8833 prev_node = node = str_node = root;
8834 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8835
8836 while (str_node) {
8837 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8838 if (nd_fl_newline(str_node)) {
8839 dedent_string(p, lit, indent);
8840 }
8841 if (!prev_lit) {
8842 prev_lit = lit;
8843 }
8844 else if (!literal_concat0(p, prev_lit, lit)) {
8845 return 0;
8846 }
8847 else {
8848 NODE *end = RNODE_LIST(node)->as.nd_end;
8849 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8850 if (!node) {
8851 if (nd_type_p(prev_node, NODE_DSTR))
8852 nd_set_type(prev_node, NODE_STR);
8853 break;
8854 }
8855 RNODE_LIST(node)->as.nd_end = end;
8856 goto next_str;
8857 }
8858
8859 str_node = 0;
8860 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8861 next_str:
8862 if (!nd_type_p(node, NODE_LIST)) break;
8863 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8864 enum node_type type = nd_type(str_node);
8865 if (type == NODE_STR || type == NODE_DSTR) break;
8866 prev_lit = 0;
8867 str_node = 0;
8868 }
8869 }
8870 }
8871 return root;
8872}
8873
8874static int
8875whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8876{
8877 const char *beg = p->lex.pbeg;
8878 const char *ptr = p->lex.pend;
8879
8880 if (ptr - beg < len) return FALSE;
8881 if (ptr > beg && ptr[-1] == '\n') {
8882 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8883 if (ptr - beg < len) return FALSE;
8884 }
8885 if (strncmp(eos, ptr -= len, len)) return FALSE;
8886 if (indent) {
8887 while (beg < ptr && ISSPACE(*beg)) beg++;
8888 }
8889 return beg == ptr;
8890}
8891
8892static int
8893word_match_p(struct parser_params *p, const char *word, long len)
8894{
8895 if (strncmp(p->lex.pcur, word, len)) return 0;
8896 if (lex_eol_n_p(p, len)) return 1;
8897 int c = (unsigned char)p->lex.pcur[len];
8898 if (ISSPACE(c)) return 1;
8899 switch (c) {
8900 case '\0': case '\004': case '\032': return 1;
8901 }
8902 return 0;
8903}
8904
8905#define NUM_SUFFIX_R (1<<0)
8906#define NUM_SUFFIX_I (1<<1)
8907#define NUM_SUFFIX_ALL 3
8908
8909static int
8910number_literal_suffix(struct parser_params *p, int mask)
8911{
8912 int c, result = 0;
8913 const char *lastp = p->lex.pcur;
8914
8915 while ((c = nextc(p)) != -1) {
8916 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8917 result |= (mask & NUM_SUFFIX_I);
8918 mask &= ~NUM_SUFFIX_I;
8919 /* r after i, rational of complex is disallowed */
8920 mask &= ~NUM_SUFFIX_R;
8921 continue;
8922 }
8923 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8924 result |= (mask & NUM_SUFFIX_R);
8925 mask &= ~NUM_SUFFIX_R;
8926 continue;
8927 }
8928 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8929 p->lex.pcur = lastp;
8930 literal_flush(p, p->lex.pcur);
8931 return 0;
8932 }
8933 pushback(p, c);
8934 break;
8935 }
8936 return result;
8937}
8938
8939static enum yytokentype
8940set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8941{
8942 enum rb_numeric_type numeric_type = integer_literal;
8943
8944 if (type == tFLOAT) {
8945 numeric_type = float_literal;
8946 }
8947
8948 if (suffix & NUM_SUFFIX_R) {
8949 type = tRATIONAL;
8950 numeric_type = rational_literal;
8951 }
8952 if (suffix & NUM_SUFFIX_I) {
8953 type = tIMAGINARY;
8954 }
8955
8956 switch (type) {
8957 case tINTEGER:
8958 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8959 break;
8960 case tFLOAT:
8961 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8962 break;
8963 case tRATIONAL:
8964 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8965 break;
8966 case tIMAGINARY:
8967 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8968 (void)numeric_type; /* for ripper */
8969 break;
8970 default:
8971 rb_bug("unexpected token: %d", type);
8972 }
8973 SET_LEX_STATE(EXPR_END);
8974 return type;
8975}
8976
8977#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8978static void
8979parser_dispatch_heredoc_end(struct parser_params *p, int line)
8980{
8981 if (has_delayed_token(p))
8982 dispatch_delayed_token(p, tSTRING_CONTENT);
8983
8984#ifdef RIPPER
8985 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8986 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8987#else
8988 if (p->keep_tokens) {
8989 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
8990 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8991 parser_append_tokens(p, str, tHEREDOC_END, line);
8992 }
8993#endif
8994
8995 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8996 lex_goto_eol(p);
8997 token_flush(p);
8998}
8999
9000static enum yytokentype
9001here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9002{
9003 int c, func, indent = 0;
9004 const char *eos, *ptr, *ptr_end;
9005 long len;
9006 rb_parser_string_t *str = 0;
9007 rb_encoding *enc = p->enc;
9008 rb_encoding *base_enc = 0;
9009 int bol;
9010#ifdef RIPPER
9011 VALUE s_value;
9012#endif
9013
9014 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9015 len = here->length;
9016 indent = (func = here->func) & STR_FUNC_INDENT;
9017
9018 if ((c = nextc(p)) == -1) {
9019 error:
9020#ifdef RIPPER
9021 if (!has_delayed_token(p)) {
9022 dispatch_scan_event(p, tSTRING_CONTENT);
9023 }
9024 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9025 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9026 if (!(func & STR_FUNC_REGEXP)) {
9027 int cr = ENC_CODERANGE_UNKNOWN;
9028 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9029 if (cr != ENC_CODERANGE_7BIT &&
9030 rb_is_usascii_enc(p->enc) &&
9031 enc != rb_utf8_encoding()) {
9032 enc = rb_ascii8bit_encoding();
9033 }
9034 }
9035 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9036 }
9037 dispatch_delayed_token(p, tSTRING_CONTENT);
9038 }
9039 else {
9040 dispatch_delayed_token(p, tSTRING_CONTENT);
9041 dispatch_scan_event(p, tSTRING_CONTENT);
9042 }
9043 lex_goto_eol(p);
9044#endif
9045 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9046 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9047 (int)len, eos);
9048 token_flush(p);
9049 SET_LEX_STATE(EXPR_END);
9050 return tSTRING_END;
9051 }
9052 bol = was_bol(p);
9053 if (!bol) {
9054 /* not beginning of line, cannot be the terminator */
9055 }
9056 else if (p->heredoc_line_indent == -1) {
9057 /* `heredoc_line_indent == -1` means
9058 * - "after an interpolation in the same line", or
9059 * - "in a continuing line"
9060 */
9061 p->heredoc_line_indent = 0;
9062 }
9063 else if (whole_match_p(p, eos, len, indent)) {
9064 dispatch_heredoc_end(p);
9065 restore:
9066 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9067 token_flush(p);
9068 SET_LEX_STATE(EXPR_END);
9069 return tSTRING_END;
9070 }
9071
9072 if (!(func & STR_FUNC_EXPAND)) {
9073 do {
9074 ptr = PARSER_STRING_PTR(p->lex.lastline);
9075 ptr_end = p->lex.pend;
9076 if (ptr_end > ptr) {
9077 switch (ptr_end[-1]) {
9078 case '\n':
9079 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9080 ptr_end++;
9081 break;
9082 }
9083 case '\r':
9084 --ptr_end;
9085 }
9086 }
9087
9088 if (p->heredoc_indent > 0) {
9089 long i = 0;
9090 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9091 i++;
9092 p->heredoc_line_indent = 0;
9093 }
9094
9095 if (str)
9096 parser_str_cat(str, ptr, ptr_end - ptr);
9097 else
9098 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9099 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9100 lex_goto_eol(p);
9101 if (p->heredoc_indent > 0) {
9102 goto flush_str;
9103 }
9104 if (nextc(p) == -1) {
9105 if (str) {
9106 rb_parser_string_free(p, str);
9107 str = 0;
9108 }
9109 goto error;
9110 }
9111 } while (!whole_match_p(p, eos, len, indent));
9112 }
9113 else {
9114 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9115 newtok(p);
9116 if (c == '#') {
9117 enum yytokentype t = parser_peek_variable_name(p);
9118 if (p->heredoc_line_indent != -1) {
9119 if (p->heredoc_indent > p->heredoc_line_indent) {
9120 p->heredoc_indent = p->heredoc_line_indent;
9121 }
9122 p->heredoc_line_indent = -1;
9123 }
9124 if (t) return t;
9125 tokadd(p, '#');
9126 c = nextc(p);
9127 }
9128 do {
9129 pushback(p, c);
9130 enc = p->enc;
9131 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9132 if (p->eofp) goto error;
9133 goto restore;
9134 }
9135 if (c != '\n') {
9136 if (c == '\\') p->heredoc_line_indent = -1;
9137 flush:
9138 str = STR_NEW3(tok(p), toklen(p), enc, func);
9139 flush_str:
9140 set_yylval_str(str);
9141#ifndef RIPPER
9142 if (bol) nd_set_fl_newline(yylval.node);
9143#endif
9144 flush_string_content(p, enc, 0);
9145 return tSTRING_CONTENT;
9146 }
9147 tokadd(p, nextc(p));
9148 if (p->heredoc_indent > 0) {
9149 lex_goto_eol(p);
9150 goto flush;
9151 }
9152 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9153 if ((c = nextc(p)) == -1) goto error;
9154 } while (!whole_match_p(p, eos, len, indent));
9155 str = STR_NEW3(tok(p), toklen(p), enc, func);
9156 }
9157 dispatch_heredoc_end(p);
9158 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9159 token_flush(p);
9160 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9161#ifdef RIPPER
9162 /* Preserve s_value for set_yylval_str */
9163 s_value = p->s_value;
9164#endif
9165 set_yylval_str(str);
9166#ifdef RIPPER
9167 set_parser_s_value(s_value);
9168#endif
9169
9170#ifndef RIPPER
9171 if (bol) nd_set_fl_newline(yylval.node);
9172#endif
9173 return tSTRING_CONTENT;
9174}
9175
9176#include "lex.c"
9177
9178static int
9179arg_ambiguous(struct parser_params *p, char c)
9180{
9181#ifndef RIPPER
9182 if (c == '/') {
9183 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9184 }
9185 else {
9186 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9187 }
9188#else
9189 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9190#endif
9191 return TRUE;
9192}
9193
9194/* returns true value if formal argument error;
9195 * Qtrue, or error message if ripper */
9196static VALUE
9197formal_argument_error(struct parser_params *p, ID id)
9198{
9199 switch (id_type(id)) {
9200 case ID_LOCAL:
9201 break;
9202#ifndef RIPPER
9203# define ERR(mesg) (yyerror0(mesg), Qtrue)
9204#else
9205# define ERR(mesg) WARN_S(mesg)
9206#endif
9207 case ID_CONST:
9208 return ERR("formal argument cannot be a constant");
9209 case ID_INSTANCE:
9210 return ERR("formal argument cannot be an instance variable");
9211 case ID_GLOBAL:
9212 return ERR("formal argument cannot be a global variable");
9213 case ID_CLASS:
9214 return ERR("formal argument cannot be a class variable");
9215 default:
9216 return ERR("formal argument must be local variable");
9217#undef ERR
9218 }
9219 shadowing_lvar(p, id);
9220
9221 return Qfalse;
9222}
9223
9224static int
9225lvar_defined(struct parser_params *p, ID id)
9226{
9227 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9228}
9229
9230/* emacsen -*- hack */
9231static long
9232parser_encode_length(struct parser_params *p, const char *name, long len)
9233{
9234 long nlen;
9235
9236 if (len > 5 && name[nlen = len - 5] == '-') {
9237 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9238 return nlen;
9239 }
9240 if (len > 4 && name[nlen = len - 4] == '-') {
9241 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9242 return nlen;
9243 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9244 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9245 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9246 return nlen;
9247 }
9248 return len;
9249}
9250
9251static void
9252parser_set_encode(struct parser_params *p, const char *name)
9253{
9254 rb_encoding *enc;
9255 VALUE excargs[3];
9256 int idx = 0;
9257
9258 const char *wrong = 0;
9259 switch (*name) {
9260 case 'e': case 'E': wrong = "external"; break;
9261 case 'i': case 'I': wrong = "internal"; break;
9262 case 'f': case 'F': wrong = "filesystem"; break;
9263 case 'l': case 'L': wrong = "locale"; break;
9264 }
9265 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9266 idx = rb_enc_find_index(name);
9267 if (idx < 0) {
9268 unknown:
9269 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9270 error:
9271 excargs[0] = rb_eArgError;
9272 excargs[2] = rb_make_backtrace();
9273 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9274 VALUE exc = rb_make_exception(3, excargs);
9275 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9276
9277 rb_ast_free(p->ast);
9278 p->ast = NULL;
9279
9280 rb_exc_raise(exc);
9281 }
9282 enc = rb_enc_from_index(idx);
9283 if (!rb_enc_asciicompat(enc)) {
9284 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9285 goto error;
9286 }
9287 p->enc = enc;
9288#ifndef RIPPER
9289 if (p->debug_lines) {
9290 long i;
9291 for (i = 0; i < p->debug_lines->len; i++) {
9292 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9293 }
9294 }
9295#endif
9296}
9297
9298static bool
9299comment_at_top(struct parser_params *p)
9300{
9301 if (p->token_seen) return false;
9302 return (p->line_count == (p->has_shebang ? 2 : 1));
9303}
9304
9305typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9306typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9307
9308static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9309
9310static void
9311magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9312{
9313 if (!comment_at_top(p)) {
9314 return;
9315 }
9316 parser_set_encode(p, val);
9317}
9318
9319static int
9320parser_get_bool(struct parser_params *p, const char *name, const char *val)
9321{
9322 switch (*val) {
9323 case 't': case 'T':
9324 if (STRCASECMP(val, "true") == 0) {
9325 return TRUE;
9326 }
9327 break;
9328 case 'f': case 'F':
9329 if (STRCASECMP(val, "false") == 0) {
9330 return FALSE;
9331 }
9332 break;
9333 }
9334 return parser_invalid_pragma_value(p, name, val);
9335}
9336
9337static int
9338parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9339{
9340 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9341 return -1;
9342}
9343
9344static void
9345parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9346{
9347 int b = parser_get_bool(p, name, val);
9348 if (b >= 0) p->token_info_enabled = b;
9349}
9350
9351static void
9352parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9353{
9354 int b;
9355
9356 if (p->token_seen) {
9357 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9358 return;
9359 }
9360
9361 b = parser_get_bool(p, name, val);
9362 if (b < 0) return;
9363
9364 p->frozen_string_literal = b;
9365}
9366
9367static void
9368parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9369{
9370 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9371 if (*s == ' ' || *s == '\t') continue;
9372 if (*s == '#') break;
9373 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9374 return;
9375 }
9376
9377 switch (*val) {
9378 case 'n': case 'N':
9379 if (STRCASECMP(val, "none") == 0) {
9380 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9381 return;
9382 }
9383 break;
9384 case 'l': case 'L':
9385 if (STRCASECMP(val, "literal") == 0) {
9386 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9387 return;
9388 }
9389 break;
9390 case 'e': case 'E':
9391 if (STRCASECMP(val, "experimental_copy") == 0) {
9392 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9393 return;
9394 }
9395 if (STRCASECMP(val, "experimental_everything") == 0) {
9396 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9397 return;
9398 }
9399 break;
9400 }
9401 parser_invalid_pragma_value(p, name, val);
9402}
9403
9404# if WARN_PAST_SCOPE
9405static void
9406parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9407{
9408 int b = parser_get_bool(p, name, val);
9409 if (b >= 0) p->past_scope_enabled = b;
9410}
9411# endif
9412
9413struct magic_comment {
9414 const char *name;
9415 rb_magic_comment_setter_t func;
9416 rb_magic_comment_length_t length;
9417};
9418
9419static const struct magic_comment magic_comments[] = {
9420 {"coding", magic_comment_encoding, parser_encode_length},
9421 {"encoding", magic_comment_encoding, parser_encode_length},
9422 {"frozen_string_literal", parser_set_frozen_string_literal},
9423 {"shareable_constant_value", parser_set_shareable_constant_value},
9424 {"warn_indent", parser_set_token_info},
9425# if WARN_PAST_SCOPE
9426 {"warn_past_scope", parser_set_past_scope},
9427# endif
9428};
9429
9430static const char *
9431magic_comment_marker(const char *str, long len)
9432{
9433 long i = 2;
9434
9435 while (i < len) {
9436 switch (str[i]) {
9437 case '-':
9438 if (str[i-1] == '*' && str[i-2] == '-') {
9439 return str + i + 1;
9440 }
9441 i += 2;
9442 break;
9443 case '*':
9444 if (i + 1 >= len) return 0;
9445 if (str[i+1] != '-') {
9446 i += 4;
9447 }
9448 else if (str[i-1] != '-') {
9449 i += 2;
9450 }
9451 else {
9452 return str + i + 2;
9453 }
9454 break;
9455 default:
9456 i += 3;
9457 break;
9458 }
9459 }
9460 return 0;
9461}
9462
9463static int
9464parser_magic_comment(struct parser_params *p, const char *str, long len)
9465{
9466 int indicator = 0;
9467 VALUE name = 0, val = 0;
9468 const char *beg, *end, *vbeg, *vend;
9469#define str_copy(_s, _p, _n) ((_s) \
9470 ? (void)(rb_str_resize((_s), (_n)), \
9471 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9472 : (void)((_s) = STR_NEW((_p), (_n))))
9473
9474 if (len <= 7) return FALSE;
9475 if (!!(beg = magic_comment_marker(str, len))) {
9476 if (!(end = magic_comment_marker(beg, str + len - beg)))
9477 return FALSE;
9478 indicator = TRUE;
9479 str = beg;
9480 len = end - beg - 3;
9481 }
9482
9483 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9484 while (len > 0) {
9485 const struct magic_comment *mc = magic_comments;
9486 char *s;
9487 int i;
9488 long n = 0;
9489
9490 for (; len > 0 && *str; str++, --len) {
9491 switch (*str) {
9492 case '\'': case '"': case ':': case ';':
9493 continue;
9494 }
9495 if (!ISSPACE(*str)) break;
9496 }
9497 for (beg = str; len > 0; str++, --len) {
9498 switch (*str) {
9499 case '\'': case '"': case ':': case ';':
9500 break;
9501 default:
9502 if (ISSPACE(*str)) break;
9503 continue;
9504 }
9505 break;
9506 }
9507 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9508 if (!len) break;
9509 if (*str != ':') {
9510 if (!indicator) return FALSE;
9511 continue;
9512 }
9513
9514 do str++; while (--len > 0 && ISSPACE(*str));
9515 if (!len) break;
9516 const char *tok_beg = str;
9517 if (*str == '"') {
9518 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9519 if (*str == '\\') {
9520 --len;
9521 ++str;
9522 }
9523 }
9524 vend = str;
9525 if (len) {
9526 --len;
9527 ++str;
9528 }
9529 }
9530 else {
9531 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9532 vend = str;
9533 }
9534 const char *tok_end = str;
9535 if (indicator) {
9536 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9537 }
9538 else {
9539 while (len > 0 && (ISSPACE(*str))) --len, str++;
9540 if (len) return FALSE;
9541 }
9542
9543 n = end - beg;
9544 str_copy(name, beg, n);
9545 s = RSTRING_PTR(name);
9546 for (i = 0; i < n; ++i) {
9547 if (s[i] == '-') s[i] = '_';
9548 }
9549 do {
9550 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9551 n = vend - vbeg;
9552 if (mc->length) {
9553 n = (*mc->length)(p, vbeg, n);
9554 }
9555 str_copy(val, vbeg, n);
9556 p->lex.ptok = tok_beg;
9557 p->lex.pcur = tok_end;
9558 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9559 break;
9560 }
9561 } while (++mc < magic_comments + numberof(magic_comments));
9562#ifdef RIPPER
9563 str_copy(val, vbeg, vend - vbeg);
9564 dispatch2(magic_comment, name, val);
9565#endif
9566 }
9567
9568 return TRUE;
9569}
9570
9571static void
9572set_file_encoding(struct parser_params *p, const char *str, const char *send)
9573{
9574 int sep = 0;
9575 const char *beg = str;
9576 VALUE s;
9577
9578 for (;;) {
9579 if (send - str <= 6) return;
9580 switch (str[6]) {
9581 case 'C': case 'c': str += 6; continue;
9582 case 'O': case 'o': str += 5; continue;
9583 case 'D': case 'd': str += 4; continue;
9584 case 'I': case 'i': str += 3; continue;
9585 case 'N': case 'n': str += 2; continue;
9586 case 'G': case 'g': str += 1; continue;
9587 case '=': case ':':
9588 sep = 1;
9589 str += 6;
9590 break;
9591 default:
9592 str += 6;
9593 if (ISSPACE(*str)) break;
9594 continue;
9595 }
9596 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9597 sep = 0;
9598 }
9599 for (;;) {
9600 do {
9601 if (++str >= send) return;
9602 } while (ISSPACE(*str));
9603 if (sep) break;
9604 if (*str != '=' && *str != ':') return;
9605 sep = 1;
9606 str++;
9607 }
9608 beg = str;
9609 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9610 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9611 p->lex.ptok = beg;
9612 p->lex.pcur = str;
9613 parser_set_encode(p, RSTRING_PTR(s));
9614 rb_str_resize(s, 0);
9615}
9616
9617static void
9618parser_prepare(struct parser_params *p)
9619{
9620 int c = nextc0(p, FALSE);
9621 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9622 switch (c) {
9623 case '#':
9624 if (peek(p, '!')) p->has_shebang = 1;
9625 break;
9626 case 0xef: /* UTF-8 BOM marker */
9627 if (!lex_eol_n_p(p, 2) &&
9628 (unsigned char)p->lex.pcur[0] == 0xbb &&
9629 (unsigned char)p->lex.pcur[1] == 0xbf) {
9630 p->enc = rb_utf8_encoding();
9631 p->lex.pcur += 2;
9632#ifndef RIPPER
9633 if (p->debug_lines) {
9634 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9635 }
9636#endif
9637 p->lex.pbeg = p->lex.pcur;
9638 token_flush(p);
9639 return;
9640 }
9641 break;
9642 case -1: /* end of script. */
9643 return;
9644 }
9645 pushback(p, c);
9646 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9647}
9648
9649#ifndef RIPPER
9650#define ambiguous_operator(tok, op, syn) ( \
9651 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9652 rb_warning0("even though it seems like "syn""))
9653#else
9654#define ambiguous_operator(tok, op, syn) \
9655 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9656#endif
9657#define warn_balanced(tok, op, syn) ((void) \
9658 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9659 space_seen && !ISSPACE(c) && \
9660 (ambiguous_operator(tok, op, syn), 0)), \
9661 (enum yytokentype)(tok))
9662
9663static enum yytokentype
9664no_digits(struct parser_params *p)
9665{
9666 yyerror0("numeric literal without digits");
9667 if (peek(p, '_')) nextc(p);
9668 /* dummy 0, for tUMINUS_NUM at numeric */
9669 return set_number_literal(p, tINTEGER, 0, 10, 0);
9670}
9671
9672static enum yytokentype
9673parse_numeric(struct parser_params *p, int c)
9674{
9675 int is_float, seen_point, seen_e, nondigit;
9676 int suffix;
9677
9678 is_float = seen_point = seen_e = nondigit = 0;
9679 SET_LEX_STATE(EXPR_END);
9680 newtok(p);
9681 if (c == '-' || c == '+') {
9682 tokadd(p, c);
9683 c = nextc(p);
9684 }
9685 if (c == '0') {
9686 int start = toklen(p);
9687 c = nextc(p);
9688 if (c == 'x' || c == 'X') {
9689 /* hexadecimal */
9690 c = nextc(p);
9691 if (c != -1 && ISXDIGIT(c)) {
9692 do {
9693 if (c == '_') {
9694 if (nondigit) break;
9695 nondigit = c;
9696 continue;
9697 }
9698 if (!ISXDIGIT(c)) break;
9699 nondigit = 0;
9700 tokadd(p, c);
9701 } while ((c = nextc(p)) != -1);
9702 }
9703 pushback(p, c);
9704 tokfix(p);
9705 if (toklen(p) == start) {
9706 return no_digits(p);
9707 }
9708 else if (nondigit) goto trailing_uc;
9709 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9710 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9711 }
9712 if (c == 'b' || c == 'B') {
9713 /* binary */
9714 c = nextc(p);
9715 if (c == '0' || c == '1') {
9716 do {
9717 if (c == '_') {
9718 if (nondigit) break;
9719 nondigit = c;
9720 continue;
9721 }
9722 if (c != '0' && c != '1') break;
9723 nondigit = 0;
9724 tokadd(p, c);
9725 } while ((c = nextc(p)) != -1);
9726 }
9727 pushback(p, c);
9728 tokfix(p);
9729 if (toklen(p) == start) {
9730 return no_digits(p);
9731 }
9732 else if (nondigit) goto trailing_uc;
9733 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9734 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9735 }
9736 if (c == 'd' || c == 'D') {
9737 /* decimal */
9738 c = nextc(p);
9739 if (c != -1 && ISDIGIT(c)) {
9740 do {
9741 if (c == '_') {
9742 if (nondigit) break;
9743 nondigit = c;
9744 continue;
9745 }
9746 if (!ISDIGIT(c)) break;
9747 nondigit = 0;
9748 tokadd(p, c);
9749 } while ((c = nextc(p)) != -1);
9750 }
9751 pushback(p, c);
9752 tokfix(p);
9753 if (toklen(p) == start) {
9754 return no_digits(p);
9755 }
9756 else if (nondigit) goto trailing_uc;
9757 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9758 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9759 }
9760 if (c == '_') {
9761 /* 0_0 */
9762 goto octal_number;
9763 }
9764 if (c == 'o' || c == 'O') {
9765 /* prefixed octal */
9766 c = nextc(p);
9767 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9768 tokfix(p);
9769 return no_digits(p);
9770 }
9771 }
9772 if (c >= '0' && c <= '7') {
9773 /* octal */
9774 octal_number:
9775 do {
9776 if (c == '_') {
9777 if (nondigit) break;
9778 nondigit = c;
9779 continue;
9780 }
9781 if (c < '0' || c > '9') break;
9782 if (c > '7') goto invalid_octal;
9783 nondigit = 0;
9784 tokadd(p, c);
9785 } while ((c = nextc(p)) != -1);
9786 if (toklen(p) > start) {
9787 pushback(p, c);
9788 tokfix(p);
9789 if (nondigit) goto trailing_uc;
9790 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9791 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9792 }
9793 if (nondigit) {
9794 pushback(p, c);
9795 goto trailing_uc;
9796 }
9797 }
9798 if (c > '7' && c <= '9') {
9799 invalid_octal:
9800 yyerror0("Invalid octal digit");
9801 }
9802 else if (c == '.' || c == 'e' || c == 'E') {
9803 tokadd(p, '0');
9804 }
9805 else {
9806 pushback(p, c);
9807 tokfix(p);
9808 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9809 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9810 }
9811 }
9812
9813 for (;;) {
9814 switch (c) {
9815 case '0': case '1': case '2': case '3': case '4':
9816 case '5': case '6': case '7': case '8': case '9':
9817 nondigit = 0;
9818 tokadd(p, c);
9819 break;
9820
9821 case '.':
9822 if (nondigit) goto trailing_uc;
9823 if (seen_point || seen_e) {
9824 goto decode_num;
9825 }
9826 else {
9827 int c0 = nextc(p);
9828 if (c0 == -1 || !ISDIGIT(c0)) {
9829 pushback(p, c0);
9830 goto decode_num;
9831 }
9832 c = c0;
9833 }
9834 seen_point = toklen(p);
9835 tokadd(p, '.');
9836 tokadd(p, c);
9837 is_float++;
9838 nondigit = 0;
9839 break;
9840
9841 case 'e':
9842 case 'E':
9843 if (nondigit) {
9844 pushback(p, c);
9845 c = nondigit;
9846 goto decode_num;
9847 }
9848 if (seen_e) {
9849 goto decode_num;
9850 }
9851 nondigit = c;
9852 c = nextc(p);
9853 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9854 pushback(p, c);
9855 c = nondigit;
9856 nondigit = 0;
9857 goto decode_num;
9858 }
9859 tokadd(p, nondigit);
9860 seen_e++;
9861 is_float++;
9862 tokadd(p, c);
9863 nondigit = (c == '-' || c == '+') ? c : 0;
9864 break;
9865
9866 case '_': /* `_' in number just ignored */
9867 if (nondigit) goto decode_num;
9868 nondigit = c;
9869 break;
9870
9871 default:
9872 goto decode_num;
9873 }
9874 c = nextc(p);
9875 }
9876
9877 decode_num:
9878 pushback(p, c);
9879 if (nondigit) {
9880 trailing_uc:
9881 literal_flush(p, p->lex.pcur - 1);
9882 YYLTYPE loc = RUBY_INIT_YYLLOC();
9883 compile_error(p, "trailing '%c' in number", nondigit);
9884 parser_show_error_line(p, &loc);
9885 }
9886 tokfix(p);
9887 if (is_float) {
9888 enum yytokentype type = tFLOAT;
9889
9890 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9891 if (suffix & NUM_SUFFIX_R) {
9892 type = tRATIONAL;
9893 }
9894 else {
9895 strtod(tok(p), 0);
9896 if (errno == ERANGE) {
9897 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9898 errno = 0;
9899 }
9900 }
9901 return set_number_literal(p, type, suffix, 0, seen_point);
9902 }
9903 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9904 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9905}
9906
9907static enum yytokentype
9908parse_qmark(struct parser_params *p, int space_seen)
9909{
9910 rb_encoding *enc;
9911 register int c;
9912 rb_parser_string_t *lit;
9913 const char *start = p->lex.pcur;
9914
9915 if (IS_END()) {
9916 SET_LEX_STATE(EXPR_VALUE);
9917 return '?';
9918 }
9919 c = nextc(p);
9920 if (c == -1) {
9921 compile_error(p, "incomplete character syntax");
9922 return 0;
9923 }
9924 if (rb_enc_isspace(c, p->enc)) {
9925 if (!IS_ARG()) {
9926 int c2 = escaped_control_code(c);
9927 if (c2) {
9928 WARN_SPACE_CHAR(c2, "?");
9929 }
9930 }
9931 ternary:
9932 pushback(p, c);
9933 SET_LEX_STATE(EXPR_VALUE);
9934 return '?';
9935 }
9936 newtok(p);
9937 enc = p->enc;
9938 int w = parser_precise_mbclen(p, start);
9939 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9940 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9941 if (space_seen) {
9942 const char *ptr = start;
9943 do {
9944 int n = parser_precise_mbclen(p, ptr);
9945 if (n < 0) return -1;
9946 ptr += n;
9947 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9948 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9949 " a conditional operator, put a space after '?'",
9950 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9951 }
9952 goto ternary;
9953 }
9954 else if (c == '\\') {
9955 if (peek(p, 'u')) {
9956 nextc(p);
9957 enc = rb_utf8_encoding();
9958 tokadd_utf8(p, &enc, -1, 0, 0);
9959 }
9960 else if (!ISASCII(c = peekc(p)) && c != -1) {
9961 nextc(p);
9962 if (tokadd_mbchar(p, c) == -1) return 0;
9963 }
9964 else {
9965 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9966 tokadd(p, c);
9967 }
9968 }
9969 else {
9970 if (tokadd_mbchar(p, c) == -1) return 0;
9971 }
9972 tokfix(p);
9973 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9974 set_yylval_str(lit);
9975 SET_LEX_STATE(EXPR_END);
9976 return tCHAR;
9977}
9978
9979static enum yytokentype
9980parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9981{
9982 register int c;
9983 const char *ptok = p->lex.pcur;
9984
9985 if (IS_BEG()) {
9986 int term;
9987 int paren;
9988
9989 c = nextc(p);
9990 quotation:
9991 if (c == -1) goto unterminated;
9992 if (!ISALNUM(c)) {
9993 term = c;
9994 if (!ISASCII(c)) goto unknown;
9995 c = 'Q';
9996 }
9997 else {
9998 term = nextc(p);
9999 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10000 unknown:
10001 pushback(p, term);
10002 c = parser_precise_mbclen(p, p->lex.pcur);
10003 if (c < 0) return 0;
10004 p->lex.pcur += c;
10005 yyerror0("unknown type of %string");
10006 return 0;
10007 }
10008 }
10009 if (term == -1) {
10010 unterminated:
10011 compile_error(p, "unterminated quoted string meets end of file");
10012 return 0;
10013 }
10014 paren = term;
10015 if (term == '(') term = ')';
10016 else if (term == '[') term = ']';
10017 else if (term == '{') term = '}';
10018 else if (term == '<') term = '>';
10019 else paren = 0;
10020
10021 p->lex.ptok = ptok-1;
10022 switch (c) {
10023 case 'Q':
10024 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10025 return tSTRING_BEG;
10026
10027 case 'q':
10028 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10029 return tSTRING_BEG;
10030
10031 case 'W':
10032 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10033 return tWORDS_BEG;
10034
10035 case 'w':
10036 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10037 return tQWORDS_BEG;
10038
10039 case 'I':
10040 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10041 return tSYMBOLS_BEG;
10042
10043 case 'i':
10044 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10045 return tQSYMBOLS_BEG;
10046
10047 case 'x':
10048 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10049 return tXSTRING_BEG;
10050
10051 case 'r':
10052 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10053 return tREGEXP_BEG;
10054
10055 case 's':
10056 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10057 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10058 return tSYMBEG;
10059
10060 default:
10061 yyerror0("unknown type of %string");
10062 return 0;
10063 }
10064 }
10065 if ((c = nextc(p)) == '=') {
10066 set_yylval_id('%');
10067 SET_LEX_STATE(EXPR_BEG);
10068 return tOP_ASGN;
10069 }
10070 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10071 goto quotation;
10072 }
10073 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10074 pushback(p, c);
10075 return warn_balanced('%', "%%", "string literal");
10076}
10077
10078static int
10079tokadd_ident(struct parser_params *p, int c)
10080{
10081 do {
10082 if (tokadd_mbchar(p, c) == -1) return -1;
10083 c = nextc(p);
10084 } while (parser_is_identchar(p));
10085 pushback(p, c);
10086 return 0;
10087}
10088
10089static ID
10090tokenize_ident(struct parser_params *p)
10091{
10092 ID ident = TOK_INTERN();
10093
10094 set_yylval_name(ident);
10095
10096 return ident;
10097}
10098
10099static int
10100parse_numvar(struct parser_params *p)
10101{
10102 size_t len;
10103 int overflow;
10104 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10105 const unsigned long nth_ref_max =
10106 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10107 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10108 * turned into a Fixnum, in compile.c */
10109
10110 if (overflow || n > nth_ref_max) {
10111 /* compile_error()? */
10112 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10113 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10114 }
10115 else {
10116 return (int)n;
10117 }
10118}
10119
10120static enum yytokentype
10121parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10122{
10123 const char *ptr = p->lex.pcur;
10124 register int c;
10125
10126 SET_LEX_STATE(EXPR_END);
10127 p->lex.ptok = ptr - 1; /* from '$' */
10128 newtok(p);
10129 c = nextc(p);
10130 switch (c) {
10131 case '_': /* $_: last read line string */
10132 c = nextc(p);
10133 if (parser_is_identchar(p)) {
10134 tokadd(p, '$');
10135 tokadd(p, '_');
10136 break;
10137 }
10138 pushback(p, c);
10139 c = '_';
10140 /* fall through */
10141 case '~': /* $~: match-data */
10142 case '*': /* $*: argv */
10143 case '$': /* $$: pid */
10144 case '?': /* $?: last status */
10145 case '!': /* $!: error string */
10146 case '@': /* $@: error position */
10147 case '/': /* $/: input record separator */
10148 case '\\': /* $\: output record separator */
10149 case ';': /* $;: field separator */
10150 case ',': /* $,: output field separator */
10151 case '.': /* $.: last read line number */
10152 case '=': /* $=: ignorecase */
10153 case ':': /* $:: load path */
10154 case '<': /* $<: default input handle */
10155 case '>': /* $>: default output handle */
10156 case '\"': /* $": already loaded files */
10157 tokadd(p, '$');
10158 tokadd(p, c);
10159 goto gvar;
10160
10161 case '-':
10162 tokadd(p, '$');
10163 tokadd(p, c);
10164 c = nextc(p);
10165 if (parser_is_identchar(p)) {
10166 if (tokadd_mbchar(p, c) == -1) return 0;
10167 }
10168 else {
10169 pushback(p, c);
10170 pushback(p, '-');
10171 return '$';
10172 }
10173 gvar:
10174 tokenize_ident(p);
10175 return tGVAR;
10176
10177 case '&': /* $&: last match */
10178 case '`': /* $`: string before last match */
10179 case '\'': /* $': string after last match */
10180 case '+': /* $+: string matches last paren. */
10181 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10182 tokadd(p, '$');
10183 tokadd(p, c);
10184 goto gvar;
10185 }
10186 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10187 return tBACK_REF;
10188
10189 case '1': case '2': case '3':
10190 case '4': case '5': case '6':
10191 case '7': case '8': case '9':
10192 tokadd(p, '$');
10193 do {
10194 tokadd(p, c);
10195 c = nextc(p);
10196 } while (c != -1 && ISDIGIT(c));
10197 pushback(p, c);
10198 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10199 tokfix(p);
10200 c = parse_numvar(p);
10201 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10202 return tNTH_REF;
10203
10204 default:
10205 if (!parser_is_identchar(p)) {
10206 YYLTYPE loc = RUBY_INIT_YYLLOC();
10207 if (c == -1 || ISSPACE(c)) {
10208 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10209 }
10210 else {
10211 pushback(p, c);
10212 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10213 }
10214 parser_show_error_line(p, &loc);
10215 set_yylval_noname();
10216 return tGVAR;
10217 }
10218 /* fall through */
10219 case '0':
10220 tokadd(p, '$');
10221 }
10222
10223 if (tokadd_ident(p, c)) return 0;
10224 SET_LEX_STATE(EXPR_END);
10225 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10226 tokenize_ident(p);
10227 }
10228 else {
10229 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10230 set_yylval_noname();
10231 }
10232 return tGVAR;
10233}
10234
10235static bool
10236parser_numbered_param(struct parser_params *p, int n)
10237{
10238 if (n < 0) return false;
10239
10240 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10241 return false;
10242 }
10243 if (p->max_numparam == ORDINAL_PARAM) {
10244 compile_error(p, "ordinary parameter is defined");
10245 return false;
10246 }
10247 struct vtable *args = p->lvtbl->args;
10248 if (p->max_numparam < n) {
10249 p->max_numparam = n;
10250 }
10251 while (n > args->pos) {
10252 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10253 }
10254 return true;
10255}
10256
10257static enum yytokentype
10258parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10259{
10260 const char *ptr = p->lex.pcur;
10261 enum yytokentype result = tIVAR;
10262 register int c = nextc(p);
10263 YYLTYPE loc;
10264
10265 p->lex.ptok = ptr - 1; /* from '@' */
10266 newtok(p);
10267 tokadd(p, '@');
10268 if (c == '@') {
10269 result = tCVAR;
10270 tokadd(p, '@');
10271 c = nextc(p);
10272 }
10273 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10274 if (c == -1 || !parser_is_identchar(p)) {
10275 pushback(p, c);
10276 RUBY_SET_YYLLOC(loc);
10277 if (result == tIVAR) {
10278 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10279 }
10280 else {
10281 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10282 }
10283 parser_show_error_line(p, &loc);
10284 set_yylval_noname();
10285 SET_LEX_STATE(EXPR_END);
10286 return result;
10287 }
10288 else if (ISDIGIT(c)) {
10289 pushback(p, c);
10290 RUBY_SET_YYLLOC(loc);
10291 if (result == tIVAR) {
10292 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10293 }
10294 else {
10295 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10296 }
10297 parser_show_error_line(p, &loc);
10298 set_yylval_noname();
10299 SET_LEX_STATE(EXPR_END);
10300 return result;
10301 }
10302
10303 if (tokadd_ident(p, c)) return 0;
10304 tokenize_ident(p);
10305 return result;
10306}
10307
10308static enum yytokentype
10309parse_ident(struct parser_params *p, int c, int cmd_state)
10310{
10311 enum yytokentype result;
10312 bool is_ascii = true;
10313 const enum lex_state_e last_state = p->lex.state;
10314 ID ident;
10315 int enforce_keyword_end = 0;
10316
10317 do {
10318 if (!ISASCII(c)) is_ascii = false;
10319 if (tokadd_mbchar(p, c) == -1) return 0;
10320 c = nextc(p);
10321 } while (parser_is_identchar(p));
10322 if ((c == '!' || c == '?') && !peek(p, '=')) {
10323 result = tFID;
10324 tokadd(p, c);
10325 }
10326 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10327 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10328 result = tIDENTIFIER;
10329 tokadd(p, c);
10330 }
10331 else {
10332 result = tCONSTANT; /* assume provisionally */
10333 pushback(p, c);
10334 }
10335 tokfix(p);
10336
10337 if (IS_LABEL_POSSIBLE()) {
10338 if (IS_LABEL_SUFFIX(0)) {
10339 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10340 nextc(p);
10341 tokenize_ident(p);
10342 return tLABEL;
10343 }
10344 }
10345
10346#ifndef RIPPER
10347 if (peek_end_expect_token_locations(p)) {
10348 const rb_code_position_t *end_pos;
10349 int lineno, column;
10350 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10351
10352 end_pos = peek_end_expect_token_locations(p)->pos;
10353 lineno = end_pos->lineno;
10354 column = end_pos->column;
10355
10356 if (p->debug) {
10357 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10358 p->ruby_sourceline, beg_pos, lineno, column);
10359 }
10360
10361 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10362 const struct kwtable *kw;
10363
10364 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10365 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10366 enforce_keyword_end = 1;
10367 }
10368 }
10369 }
10370#endif
10371
10372 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10373 const struct kwtable *kw;
10374
10375 /* See if it is a reserved word. */
10376 kw = rb_reserved_word(tok(p), toklen(p));
10377 if (kw) {
10378 enum lex_state_e state = p->lex.state;
10379 if (IS_lex_state_for(state, EXPR_FNAME)) {
10380 SET_LEX_STATE(EXPR_ENDFN);
10381 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10382 return kw->id[0];
10383 }
10384 SET_LEX_STATE(kw->state);
10385 if (IS_lex_state(EXPR_BEG)) {
10386 p->command_start = TRUE;
10387 }
10388 if (kw->id[0] == keyword_do) {
10389 if (lambda_beginning_p()) {
10390 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10391 return keyword_do_LAMBDA;
10392 }
10393 if (COND_P()) return keyword_do_cond;
10394 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10395 return keyword_do_block;
10396 return keyword_do;
10397 }
10398 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10399 return kw->id[0];
10400 else {
10401 if (kw->id[0] != kw->id[1])
10402 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10403 return kw->id[1];
10404 }
10405 }
10406 }
10407
10408 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10409 if (cmd_state) {
10410 SET_LEX_STATE(EXPR_CMDARG);
10411 }
10412 else {
10413 SET_LEX_STATE(EXPR_ARG);
10414 }
10415 }
10416 else if (p->lex.state == EXPR_FNAME) {
10417 SET_LEX_STATE(EXPR_ENDFN);
10418 }
10419 else {
10420 SET_LEX_STATE(EXPR_END);
10421 }
10422
10423 ident = tokenize_ident(p);
10424 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10425 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10426 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10427 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10428 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10429 }
10430 return result;
10431}
10432
10433static void
10434warn_cr(struct parser_params *p)
10435{
10436 if (!p->cr_seen) {
10437 p->cr_seen = TRUE;
10438 /* carried over with p->lex.nextline for nextc() */
10439 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10440 }
10441}
10442
10443static enum yytokentype
10444parser_yylex(struct parser_params *p)
10445{
10446 register int c;
10447 int space_seen = 0;
10448 int cmd_state;
10449 int label;
10450 enum lex_state_e last_state;
10451 int fallthru = FALSE;
10452 int token_seen = p->token_seen;
10453
10454 if (p->lex.strterm) {
10455 if (strterm_is_heredoc(p->lex.strterm)) {
10456 token_flush(p);
10457 return here_document(p, &p->lex.strterm->u.heredoc);
10458 }
10459 else {
10460 token_flush(p);
10461 return parse_string(p, &p->lex.strterm->u.literal);
10462 }
10463 }
10464 cmd_state = p->command_start;
10465 p->command_start = FALSE;
10466 p->token_seen = TRUE;
10467#ifndef RIPPER
10468 token_flush(p);
10469#endif
10470 retry:
10471 last_state = p->lex.state;
10472 switch (c = nextc(p)) {
10473 case '\0': /* NUL */
10474 case '\004': /* ^D */
10475 case '\032': /* ^Z */
10476 case -1: /* end of script. */
10477 p->eofp = 1;
10478#ifndef RIPPER
10479 if (p->end_expect_token_locations) {
10480 pop_end_expect_token_locations(p);
10481 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10482 return tDUMNY_END;
10483 }
10484#endif
10485 /* Set location for end-of-input because dispatch_scan_event is not called. */
10486 RUBY_SET_YYLLOC(*p->yylloc);
10487 return END_OF_INPUT;
10488
10489 /* white spaces */
10490 case '\r':
10491 warn_cr(p);
10492 /* fall through */
10493 case ' ': case '\t': case '\f':
10494 case '\13': /* '\v' */
10495 space_seen = 1;
10496 while ((c = nextc(p))) {
10497 switch (c) {
10498 case '\r':
10499 warn_cr(p);
10500 /* fall through */
10501 case ' ': case '\t': case '\f':
10502 case '\13': /* '\v' */
10503 break;
10504 default:
10505 goto outofloop;
10506 }
10507 }
10508 outofloop:
10509 pushback(p, c);
10510 dispatch_scan_event(p, tSP);
10511#ifndef RIPPER
10512 token_flush(p);
10513#endif
10514 goto retry;
10515
10516 case '#': /* it's a comment */
10517 p->token_seen = token_seen;
10518 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10519 /* no magic_comment in shebang line */
10520 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10521 if (comment_at_top(p)) {
10522 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10523 }
10524 }
10525 p->lex.pcur = pcur, p->lex.ptok = ptok;
10526 lex_goto_eol(p);
10527 dispatch_scan_event(p, tCOMMENT);
10528 fallthru = TRUE;
10529 /* fall through */
10530 case '\n':
10531 p->token_seen = token_seen;
10532 rb_parser_string_t *prevline = p->lex.lastline;
10533 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10534 !IS_lex_state(EXPR_LABELED));
10535 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10536 if (!fallthru) {
10537 dispatch_scan_event(p, tIGNORED_NL);
10538 }
10539 fallthru = FALSE;
10540 if (!c && p->ctxt.in_kwarg) {
10541 goto normal_newline;
10542 }
10543 goto retry;
10544 }
10545 while (1) {
10546 switch (c = nextc(p)) {
10547 case ' ': case '\t': case '\f': case '\r':
10548 case '\13': /* '\v' */
10549 space_seen = 1;
10550 break;
10551 case '#':
10552 pushback(p, c);
10553 if (space_seen) {
10554 dispatch_scan_event(p, tSP);
10555 token_flush(p);
10556 }
10557 goto retry;
10558 case 'a':
10559 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10560 goto bol;
10561 case 'o':
10562 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10563 goto bol;
10564 case '|':
10565 if (peek(p, '|')) goto leading_logical;
10566 goto bol;
10567 case '&':
10568 if (peek(p, '&')) {
10569 leading_logical:
10570 pushback(p, c);
10571 dispatch_delayed_token(p, tIGNORED_NL);
10572 cmd_state = FALSE;
10573 goto retry;
10574 }
10575 /* fall through */
10576 case '.': {
10577 dispatch_delayed_token(p, tIGNORED_NL);
10578 if (peek(p, '.') == (c == '&')) {
10579 pushback(p, c);
10580 dispatch_scan_event(p, tSP);
10581 goto retry;
10582 }
10583 }
10584 bol:
10585 default:
10586 p->ruby_sourceline--;
10587 p->lex.nextline = p->lex.lastline;
10588 set_lastline(p, prevline);
10589 case -1: /* EOF no decrement*/
10590 if (c == -1 && space_seen) {
10591 dispatch_scan_event(p, tSP);
10592 }
10593 lex_goto_eol(p);
10594 if (c != -1) {
10595 token_flush(p);
10596 RUBY_SET_YYLLOC(*p->yylloc);
10597 }
10598 goto normal_newline;
10599 }
10600 }
10601 normal_newline:
10602 p->command_start = TRUE;
10603 SET_LEX_STATE(EXPR_BEG);
10604 return '\n';
10605
10606 case '*':
10607 if ((c = nextc(p)) == '*') {
10608 if ((c = nextc(p)) == '=') {
10609 set_yylval_id(idPow);
10610 SET_LEX_STATE(EXPR_BEG);
10611 return tOP_ASGN;
10612 }
10613 pushback(p, c);
10614 if (IS_SPCARG(c)) {
10615 rb_warning0("'**' interpreted as argument prefix");
10616 c = tDSTAR;
10617 }
10618 else if (IS_BEG()) {
10619 c = tDSTAR;
10620 }
10621 else {
10622 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10623 }
10624 }
10625 else {
10626 if (c == '=') {
10627 set_yylval_id('*');
10628 SET_LEX_STATE(EXPR_BEG);
10629 return tOP_ASGN;
10630 }
10631 pushback(p, c);
10632 if (IS_SPCARG(c)) {
10633 rb_warning0("'*' interpreted as argument prefix");
10634 c = tSTAR;
10635 }
10636 else if (IS_BEG()) {
10637 c = tSTAR;
10638 }
10639 else {
10640 c = warn_balanced('*', "*", "argument prefix");
10641 }
10642 }
10643 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10644 return c;
10645
10646 case '!':
10647 c = nextc(p);
10648 if (IS_AFTER_OPERATOR()) {
10649 SET_LEX_STATE(EXPR_ARG);
10650 if (c == '@') {
10651 return '!';
10652 }
10653 }
10654 else {
10655 SET_LEX_STATE(EXPR_BEG);
10656 }
10657 if (c == '=') {
10658 return tNEQ;
10659 }
10660 if (c == '~') {
10661 return tNMATCH;
10662 }
10663 pushback(p, c);
10664 return '!';
10665
10666 case '=':
10667 if (was_bol(p)) {
10668 /* skip embedded rd document */
10669 if (word_match_p(p, "begin", 5)) {
10670 int first_p = TRUE;
10671
10672 lex_goto_eol(p);
10673 dispatch_scan_event(p, tEMBDOC_BEG);
10674 for (;;) {
10675 lex_goto_eol(p);
10676 if (!first_p) {
10677 dispatch_scan_event(p, tEMBDOC);
10678 }
10679 first_p = FALSE;
10680 c = nextc(p);
10681 if (c == -1) {
10682 compile_error(p, "embedded document meets end of file");
10683 return END_OF_INPUT;
10684 }
10685 if (c == '=' && word_match_p(p, "end", 3)) {
10686 break;
10687 }
10688 pushback(p, c);
10689 }
10690 lex_goto_eol(p);
10691 dispatch_scan_event(p, tEMBDOC_END);
10692 goto retry;
10693 }
10694 }
10695
10696 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10697 if ((c = nextc(p)) == '=') {
10698 if ((c = nextc(p)) == '=') {
10699 return tEQQ;
10700 }
10701 pushback(p, c);
10702 return tEQ;
10703 }
10704 if (c == '~') {
10705 return tMATCH;
10706 }
10707 else if (c == '>') {
10708 return tASSOC;
10709 }
10710 pushback(p, c);
10711 return '=';
10712
10713 case '<':
10714 c = nextc(p);
10715 if (c == '<' &&
10716 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10717 !IS_END() &&
10718 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10719 enum yytokentype token = heredoc_identifier(p);
10720 if (token) return token < 0 ? 0 : token;
10721 }
10722 if (IS_AFTER_OPERATOR()) {
10723 SET_LEX_STATE(EXPR_ARG);
10724 }
10725 else {
10726 if (IS_lex_state(EXPR_CLASS))
10727 p->command_start = TRUE;
10728 SET_LEX_STATE(EXPR_BEG);
10729 }
10730 if (c == '=') {
10731 if ((c = nextc(p)) == '>') {
10732 return tCMP;
10733 }
10734 pushback(p, c);
10735 return tLEQ;
10736 }
10737 if (c == '<') {
10738 if ((c = nextc(p)) == '=') {
10739 set_yylval_id(idLTLT);
10740 SET_LEX_STATE(EXPR_BEG);
10741 return tOP_ASGN;
10742 }
10743 pushback(p, c);
10744 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10745 }
10746 pushback(p, c);
10747 return '<';
10748
10749 case '>':
10750 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10751 if ((c = nextc(p)) == '=') {
10752 return tGEQ;
10753 }
10754 if (c == '>') {
10755 if ((c = nextc(p)) == '=') {
10756 set_yylval_id(idGTGT);
10757 SET_LEX_STATE(EXPR_BEG);
10758 return tOP_ASGN;
10759 }
10760 pushback(p, c);
10761 return tRSHFT;
10762 }
10763 pushback(p, c);
10764 return '>';
10765
10766 case '"':
10767 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10768 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10769 p->lex.ptok = p->lex.pcur-1;
10770 return tSTRING_BEG;
10771
10772 case '`':
10773 if (IS_lex_state(EXPR_FNAME)) {
10774 SET_LEX_STATE(EXPR_ENDFN);
10775 return c;
10776 }
10777 if (IS_lex_state(EXPR_DOT)) {
10778 if (cmd_state)
10779 SET_LEX_STATE(EXPR_CMDARG);
10780 else
10781 SET_LEX_STATE(EXPR_ARG);
10782 return c;
10783 }
10784 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10785 return tXSTRING_BEG;
10786
10787 case '\'':
10788 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10789 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10790 p->lex.ptok = p->lex.pcur-1;
10791 return tSTRING_BEG;
10792
10793 case '?':
10794 return parse_qmark(p, space_seen);
10795
10796 case '&':
10797 if ((c = nextc(p)) == '&') {
10798 SET_LEX_STATE(EXPR_BEG);
10799 if ((c = nextc(p)) == '=') {
10800 set_yylval_id(idANDOP);
10801 SET_LEX_STATE(EXPR_BEG);
10802 return tOP_ASGN;
10803 }
10804 pushback(p, c);
10805 return tANDOP;
10806 }
10807 else if (c == '=') {
10808 set_yylval_id('&');
10809 SET_LEX_STATE(EXPR_BEG);
10810 return tOP_ASGN;
10811 }
10812 else if (c == '.') {
10813 set_yylval_id(idANDDOT);
10814 SET_LEX_STATE(EXPR_DOT);
10815 return tANDDOT;
10816 }
10817 pushback(p, c);
10818 if (IS_SPCARG(c)) {
10819 if ((c != ':') ||
10820 (c = peekc_n(p, 1)) == -1 ||
10821 !(c == '\'' || c == '"' ||
10822 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10823 rb_warning0("'&' interpreted as argument prefix");
10824 }
10825 c = tAMPER;
10826 }
10827 else if (IS_BEG()) {
10828 c = tAMPER;
10829 }
10830 else {
10831 c = warn_balanced('&', "&", "argument prefix");
10832 }
10833 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10834 return c;
10835
10836 case '|':
10837 if ((c = nextc(p)) == '|') {
10838 SET_LEX_STATE(EXPR_BEG);
10839 if ((c = nextc(p)) == '=') {
10840 set_yylval_id(idOROP);
10841 SET_LEX_STATE(EXPR_BEG);
10842 return tOP_ASGN;
10843 }
10844 pushback(p, c);
10845 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10846 c = '|';
10847 pushback(p, '|');
10848 return c;
10849 }
10850 return tOROP;
10851 }
10852 if (c == '=') {
10853 set_yylval_id('|');
10854 SET_LEX_STATE(EXPR_BEG);
10855 return tOP_ASGN;
10856 }
10857 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10858 pushback(p, c);
10859 return '|';
10860
10861 case '+':
10862 c = nextc(p);
10863 if (IS_AFTER_OPERATOR()) {
10864 SET_LEX_STATE(EXPR_ARG);
10865 if (c == '@') {
10866 return tUPLUS;
10867 }
10868 pushback(p, c);
10869 return '+';
10870 }
10871 if (c == '=') {
10872 set_yylval_id('+');
10873 SET_LEX_STATE(EXPR_BEG);
10874 return tOP_ASGN;
10875 }
10876 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10877 SET_LEX_STATE(EXPR_BEG);
10878 pushback(p, c);
10879 if (c != -1 && ISDIGIT(c)) {
10880 return parse_numeric(p, '+');
10881 }
10882 return tUPLUS;
10883 }
10884 SET_LEX_STATE(EXPR_BEG);
10885 pushback(p, c);
10886 return warn_balanced('+', "+", "unary operator");
10887
10888 case '-':
10889 c = nextc(p);
10890 if (IS_AFTER_OPERATOR()) {
10891 SET_LEX_STATE(EXPR_ARG);
10892 if (c == '@') {
10893 return tUMINUS;
10894 }
10895 pushback(p, c);
10896 return '-';
10897 }
10898 if (c == '=') {
10899 set_yylval_id('-');
10900 SET_LEX_STATE(EXPR_BEG);
10901 return tOP_ASGN;
10902 }
10903 if (c == '>') {
10904 SET_LEX_STATE(EXPR_ENDFN);
10905 yylval.num = p->lex.lpar_beg;
10906 p->lex.lpar_beg = p->lex.paren_nest;
10907 return tLAMBDA;
10908 }
10909 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10910 SET_LEX_STATE(EXPR_BEG);
10911 pushback(p, c);
10912 if (c != -1 && ISDIGIT(c)) {
10913 return tUMINUS_NUM;
10914 }
10915 return tUMINUS;
10916 }
10917 SET_LEX_STATE(EXPR_BEG);
10918 pushback(p, c);
10919 return warn_balanced('-', "-", "unary operator");
10920
10921 case '.': {
10922 int is_beg = IS_BEG();
10923 SET_LEX_STATE(EXPR_BEG);
10924 if ((c = nextc(p)) == '.') {
10925 if ((c = nextc(p)) == '.') {
10926 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10927 SET_LEX_STATE(EXPR_ENDARG);
10928 return tBDOT3;
10929 }
10930 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10931 rb_warn0("... at EOL, should be parenthesized?");
10932 }
10933 return is_beg ? tBDOT3 : tDOT3;
10934 }
10935 pushback(p, c);
10936 return is_beg ? tBDOT2 : tDOT2;
10937 }
10938 pushback(p, c);
10939 if (c != -1 && ISDIGIT(c)) {
10940 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10941 parse_numeric(p, '.');
10942 if (ISDIGIT(prev)) {
10943 yyerror0("unexpected fraction part after numeric literal");
10944 }
10945 else {
10946 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10947 }
10948 SET_LEX_STATE(EXPR_END);
10949 p->lex.ptok = p->lex.pcur;
10950 goto retry;
10951 }
10952 set_yylval_id('.');
10953 SET_LEX_STATE(EXPR_DOT);
10954 return '.';
10955 }
10956
10957 case '0': case '1': case '2': case '3': case '4':
10958 case '5': case '6': case '7': case '8': case '9':
10959 return parse_numeric(p, c);
10960
10961 case ')':
10962 COND_POP();
10963 CMDARG_POP();
10964 SET_LEX_STATE(EXPR_ENDFN);
10965 p->lex.paren_nest--;
10966 return c;
10967
10968 case ']':
10969 COND_POP();
10970 CMDARG_POP();
10971 SET_LEX_STATE(EXPR_END);
10972 p->lex.paren_nest--;
10973 return c;
10974
10975 case '}':
10976 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10977 if (!p->lex.brace_nest--) return tSTRING_DEND;
10978 COND_POP();
10979 CMDARG_POP();
10980 SET_LEX_STATE(EXPR_END);
10981 p->lex.paren_nest--;
10982 return c;
10983
10984 case ':':
10985 c = nextc(p);
10986 if (c == ':') {
10987 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10988 SET_LEX_STATE(EXPR_BEG);
10989 return tCOLON3;
10990 }
10991 set_yylval_id(idCOLON2);
10992 SET_LEX_STATE(EXPR_DOT);
10993 return tCOLON2;
10994 }
10995 if (IS_END() || ISSPACE(c) || c == '#') {
10996 pushback(p, c);
10997 c = warn_balanced(':', ":", "symbol literal");
10998 SET_LEX_STATE(EXPR_BEG);
10999 return c;
11000 }
11001 switch (c) {
11002 case '\'':
11003 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11004 break;
11005 case '"':
11006 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11007 break;
11008 default:
11009 pushback(p, c);
11010 break;
11011 }
11012 SET_LEX_STATE(EXPR_FNAME);
11013 return tSYMBEG;
11014
11015 case '/':
11016 if (IS_BEG()) {
11017 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11018 return tREGEXP_BEG;
11019 }
11020 if ((c = nextc(p)) == '=') {
11021 set_yylval_id('/');
11022 SET_LEX_STATE(EXPR_BEG);
11023 return tOP_ASGN;
11024 }
11025 pushback(p, c);
11026 if (IS_SPCARG(c)) {
11027 arg_ambiguous(p, '/');
11028 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11029 return tREGEXP_BEG;
11030 }
11031 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11032 return warn_balanced('/', "/", "regexp literal");
11033
11034 case '^':
11035 if ((c = nextc(p)) == '=') {
11036 set_yylval_id('^');
11037 SET_LEX_STATE(EXPR_BEG);
11038 return tOP_ASGN;
11039 }
11040 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11041 pushback(p, c);
11042 return '^';
11043
11044 case ';':
11045 SET_LEX_STATE(EXPR_BEG);
11046 p->command_start = TRUE;
11047 return ';';
11048
11049 case ',':
11050 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11051 return ',';
11052
11053 case '~':
11054 if (IS_AFTER_OPERATOR()) {
11055 if ((c = nextc(p)) != '@') {
11056 pushback(p, c);
11057 }
11058 SET_LEX_STATE(EXPR_ARG);
11059 }
11060 else {
11061 SET_LEX_STATE(EXPR_BEG);
11062 }
11063 return '~';
11064
11065 case '(':
11066 if (IS_BEG()) {
11067 c = tLPAREN;
11068 }
11069 else if (!space_seen) {
11070 /* foo( ... ) => method call, no ambiguity */
11071 }
11072 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11073 c = tLPAREN_ARG;
11074 }
11075 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11076 rb_warning0("parentheses after method name is interpreted as "
11077 "an argument list, not a decomposed argument");
11078 }
11079 p->lex.paren_nest++;
11080 COND_PUSH(0);
11081 CMDARG_PUSH(0);
11082 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11083 return c;
11084
11085 case '[':
11086 p->lex.paren_nest++;
11087 if (IS_AFTER_OPERATOR()) {
11088 if ((c = nextc(p)) == ']') {
11089 p->lex.paren_nest--;
11090 SET_LEX_STATE(EXPR_ARG);
11091 if ((c = nextc(p)) == '=') {
11092 return tASET;
11093 }
11094 pushback(p, c);
11095 return tAREF;
11096 }
11097 pushback(p, c);
11098 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11099 return '[';
11100 }
11101 else if (IS_BEG()) {
11102 c = tLBRACK;
11103 }
11104 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11105 c = tLBRACK;
11106 }
11107 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11108 COND_PUSH(0);
11109 CMDARG_PUSH(0);
11110 return c;
11111
11112 case '{':
11113 ++p->lex.brace_nest;
11114 if (lambda_beginning_p())
11115 c = tLAMBEG;
11116 else if (IS_lex_state(EXPR_LABELED))
11117 c = tLBRACE; /* hash */
11118 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11119 c = '{'; /* block (primary) */
11120 else if (IS_lex_state(EXPR_ENDARG))
11121 c = tLBRACE_ARG; /* block (expr) */
11122 else
11123 c = tLBRACE; /* hash */
11124 if (c != tLBRACE) {
11125 p->command_start = TRUE;
11126 SET_LEX_STATE(EXPR_BEG);
11127 }
11128 else {
11129 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11130 }
11131 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11132 COND_PUSH(0);
11133 CMDARG_PUSH(0);
11134 return c;
11135
11136 case '\\':
11137 c = nextc(p);
11138 if (c == '\n') {
11139 space_seen = 1;
11140 dispatch_scan_event(p, tSP);
11141 goto retry; /* skip \\n */
11142 }
11143 if (c == ' ') return tSP;
11144 if (ISSPACE(c)) return c;
11145 pushback(p, c);
11146 return '\\';
11147
11148 case '%':
11149 return parse_percent(p, space_seen, last_state);
11150
11151 case '$':
11152 return parse_gvar(p, last_state);
11153
11154 case '@':
11155 return parse_atmark(p, last_state);
11156
11157 case '_':
11158 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11159 p->ruby__end__seen = 1;
11160 p->eofp = 1;
11161#ifdef RIPPER
11162 lex_goto_eol(p);
11163 dispatch_scan_event(p, k__END__);
11164#endif
11165 return END_OF_INPUT;
11166 }
11167 newtok(p);
11168 break;
11169
11170 default:
11171 if (!parser_is_identchar(p)) {
11172 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11173 token_flush(p);
11174 goto retry;
11175 }
11176
11177 newtok(p);
11178 break;
11179 }
11180
11181 return parse_ident(p, c, cmd_state);
11182}
11183
11184static enum yytokentype
11185yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11186{
11187 enum yytokentype t;
11188
11189 p->lval = lval;
11190 lval->node = 0;
11191 p->yylloc = yylloc;
11192
11193 t = parser_yylex(p);
11194
11195 if (has_delayed_token(p))
11196 dispatch_delayed_token(p, t);
11197 else if (t != END_OF_INPUT)
11198 dispatch_scan_event(p, t);
11199
11200 return t;
11201}
11202
11203#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11204
11205static NODE*
11206node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11207{
11208 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11209
11210 rb_node_init(n, type);
11211 return n;
11212}
11213
11214static NODE *
11215nd_set_loc(NODE *nd, const YYLTYPE *loc)
11216{
11217 nd->nd_loc = *loc;
11218 nd_set_line(nd, loc->beg_pos.lineno);
11219 return nd;
11220}
11221
11222static NODE*
11223node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11224{
11225 NODE *n = node_new_internal(p, type, size, alignment);
11226
11227 nd_set_loc(n, loc);
11228 nd_set_node_id(n, parser_get_node_id(p));
11229 return n;
11230}
11231
11232#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11233
11234static rb_node_scope_t *
11235rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11236{
11237 rb_ast_id_table_t *nd_tbl;
11238 nd_tbl = local_tbl(p);
11239 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11240 n->nd_tbl = nd_tbl;
11241 n->nd_body = nd_body;
11242 n->nd_parent = nd_parent;
11243 n->nd_args = nd_args;
11244
11245 return n;
11246}
11247
11248static rb_node_scope_t *
11249rb_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)
11250{
11251 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11252 n->nd_tbl = nd_tbl;
11253 n->nd_body = nd_body;
11254 n->nd_parent = nd_parent;
11255 n->nd_args = nd_args;
11256
11257 return n;
11258}
11259
11260static rb_node_defn_t *
11261rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11262{
11263 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11264 n->nd_mid = nd_mid;
11265 n->nd_defn = nd_defn;
11266
11267 return n;
11268}
11269
11270static rb_node_defs_t *
11271rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11272{
11273 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11274 n->nd_recv = nd_recv;
11275 n->nd_mid = nd_mid;
11276 n->nd_defn = nd_defn;
11277
11278 return n;
11279}
11280
11281static rb_node_block_t *
11282rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11283{
11284 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11285 n->nd_head = nd_head;
11286 n->nd_end = (NODE *)n;
11287 n->nd_next = 0;
11288
11289 return n;
11290}
11291
11292static rb_node_for_t *
11293rb_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)
11294{
11295 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11296 n->nd_body = nd_body;
11297 n->nd_iter = nd_iter;
11298 n->for_keyword_loc = *for_keyword_loc;
11299 n->in_keyword_loc = *in_keyword_loc;
11300 n->do_keyword_loc = *do_keyword_loc;
11301 n->end_keyword_loc = *end_keyword_loc;
11302
11303 return n;
11304}
11305
11306static rb_node_for_masgn_t *
11307rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11308{
11309 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11310 n->nd_var = nd_var;
11311
11312 return n;
11313}
11314
11315static rb_node_retry_t *
11316rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11317{
11318 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11319
11320 return n;
11321}
11322
11323static rb_node_begin_t *
11324rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11325{
11326 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11327 n->nd_body = nd_body;
11328
11329 return n;
11330}
11331
11332static rb_node_rescue_t *
11333rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11334{
11335 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11336 n->nd_head = nd_head;
11337 n->nd_resq = nd_resq;
11338 n->nd_else = nd_else;
11339
11340 return n;
11341}
11342
11343static rb_node_resbody_t *
11344rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11345{
11346 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11347 n->nd_args = nd_args;
11348 n->nd_exc_var = nd_exc_var;
11349 n->nd_body = nd_body;
11350 n->nd_next = nd_next;
11351
11352 return n;
11353}
11354
11355static rb_node_ensure_t *
11356rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11357{
11358 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11359 n->nd_head = nd_head;
11360 n->nd_ensr = nd_ensr;
11361
11362 return n;
11363}
11364
11365static rb_node_and_t *
11366rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11367{
11368 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11369 n->nd_1st = nd_1st;
11370 n->nd_2nd = nd_2nd;
11371 n->operator_loc = *operator_loc;
11372
11373 return n;
11374}
11375
11376static rb_node_or_t *
11377rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11378{
11379 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11380 n->nd_1st = nd_1st;
11381 n->nd_2nd = nd_2nd;
11382 n->operator_loc = *operator_loc;
11383
11384 return n;
11385}
11386
11387static rb_node_return_t *
11388rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11389{
11390 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11391 n->nd_stts = nd_stts;
11392 n->keyword_loc = *keyword_loc;
11393 return n;
11394}
11395
11396static rb_node_yield_t *
11397rb_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)
11398{
11399 if (nd_head) no_blockarg(p, nd_head);
11400
11401 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11402 n->nd_head = nd_head;
11403 n->keyword_loc = *keyword_loc;
11404 n->lparen_loc = *lparen_loc;
11405 n->rparen_loc = *rparen_loc;
11406
11407 return n;
11408}
11409
11410static rb_node_if_t *
11411rb_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)
11412{
11413 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11414 n->nd_cond = nd_cond;
11415 n->nd_body = nd_body;
11416 n->nd_else = nd_else;
11417 n->if_keyword_loc = *if_keyword_loc;
11418 n->then_keyword_loc = *then_keyword_loc;
11419 n->end_keyword_loc = *end_keyword_loc;
11420
11421 return n;
11422}
11423
11424static rb_node_unless_t *
11425rb_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)
11426{
11427 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11428 n->nd_cond = nd_cond;
11429 n->nd_body = nd_body;
11430 n->nd_else = nd_else;
11431 n->keyword_loc = *keyword_loc;
11432 n->then_keyword_loc = *then_keyword_loc;
11433 n->end_keyword_loc = *end_keyword_loc;
11434
11435 return n;
11436}
11437
11438static rb_node_class_t *
11439rb_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)
11440{
11441 /* Keep the order of node creation */
11442 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11443 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11444 RNODE_SCOPE(scope)->nd_parent = &n->node;
11445 n->nd_cpath = nd_cpath;
11446 n->nd_body = scope;
11447 n->nd_super = nd_super;
11448 n->class_keyword_loc = *class_keyword_loc;
11449 n->inheritance_operator_loc = *inheritance_operator_loc;
11450 n->end_keyword_loc = *end_keyword_loc;
11451
11452 return n;
11453}
11454
11455static rb_node_sclass_t *
11456rb_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)
11457{
11458 /* Keep the order of node creation */
11459 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11460 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11461 RNODE_SCOPE(scope)->nd_parent = &n->node;
11462 n->nd_recv = nd_recv;
11463 n->nd_body = scope;
11464 n->class_keyword_loc = *class_keyword_loc;
11465 n->operator_loc = *operator_loc;
11466 n->end_keyword_loc = *end_keyword_loc;
11467
11468 return n;
11469}
11470
11471static rb_node_module_t *
11472rb_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)
11473{
11474 /* Keep the order of node creation */
11475 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11476 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11477 RNODE_SCOPE(scope)->nd_parent = &n->node;
11478 n->nd_cpath = nd_cpath;
11479 n->nd_body = scope;
11480 n->module_keyword_loc = *module_keyword_loc;
11481 n->end_keyword_loc = *end_keyword_loc;
11482
11483 return n;
11484}
11485
11486static rb_node_iter_t *
11487rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11488{
11489 /* Keep the order of node creation */
11490 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11491 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11492 RNODE_SCOPE(scope)->nd_parent = &n->node;
11493 n->nd_body = scope;
11494 n->nd_iter = 0;
11495
11496 return n;
11497}
11498
11499static rb_node_lambda_t *
11500rb_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)
11501{
11502 /* Keep the order of node creation */
11503 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11504 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11505 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11506 RNODE_SCOPE(scope)->nd_parent = &n->node;
11507 n->nd_body = scope;
11508 n->operator_loc = *operator_loc;
11509 n->opening_loc = *opening_loc;
11510 n->closing_loc = *closing_loc;
11511
11512 return n;
11513}
11514
11515static rb_node_case_t *
11516rb_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)
11517{
11518 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11519 n->nd_head = nd_head;
11520 n->nd_body = nd_body;
11521 n->case_keyword_loc = *case_keyword_loc;
11522 n->end_keyword_loc = *end_keyword_loc;
11523
11524 return n;
11525}
11526
11527static rb_node_case2_t *
11528rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11529{
11530 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11531 n->nd_head = 0;
11532 n->nd_body = nd_body;
11533 n->case_keyword_loc = *case_keyword_loc;
11534 n->end_keyword_loc = *end_keyword_loc;
11535
11536 return n;
11537}
11538
11539static rb_node_case3_t *
11540rb_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)
11541{
11542 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11543 n->nd_head = nd_head;
11544 n->nd_body = nd_body;
11545 n->case_keyword_loc = *case_keyword_loc;
11546 n->end_keyword_loc = *end_keyword_loc;
11547
11548 return n;
11549}
11550
11551static rb_node_when_t *
11552rb_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)
11553{
11554 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11555 n->nd_head = nd_head;
11556 n->nd_body = nd_body;
11557 n->nd_next = nd_next;
11558 n->keyword_loc = *keyword_loc;
11559 n->then_keyword_loc = *then_keyword_loc;
11560
11561 return n;
11562}
11563
11564static rb_node_in_t *
11565rb_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)
11566{
11567 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11568 n->nd_head = nd_head;
11569 n->nd_body = nd_body;
11570 n->nd_next = nd_next;
11571 n->in_keyword_loc = *in_keyword_loc;
11572 n->then_keyword_loc = *then_keyword_loc;
11573 n->operator_loc = *operator_loc;
11574
11575 return n;
11576}
11577
11578static rb_node_while_t *
11579rb_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)
11580{
11581 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11582 n->nd_cond = nd_cond;
11583 n->nd_body = nd_body;
11584 n->nd_state = nd_state;
11585 n->keyword_loc = *keyword_loc;
11586 n->closing_loc = *closing_loc;
11587
11588 return n;
11589}
11590
11591static rb_node_until_t *
11592rb_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)
11593{
11594 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11595 n->nd_cond = nd_cond;
11596 n->nd_body = nd_body;
11597 n->nd_state = nd_state;
11598 n->keyword_loc = *keyword_loc;
11599 n->closing_loc = *closing_loc;
11600
11601 return n;
11602}
11603
11604static rb_node_colon2_t *
11605rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11606{
11607 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11608 n->nd_head = nd_head;
11609 n->nd_mid = nd_mid;
11610 n->delimiter_loc = *delimiter_loc;
11611 n->name_loc = *name_loc;
11612
11613 return n;
11614}
11615
11616static rb_node_colon3_t *
11617rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11618{
11619 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11620 n->nd_mid = nd_mid;
11621 n->delimiter_loc = *delimiter_loc;
11622 n->name_loc = *name_loc;
11623
11624 return n;
11625}
11626
11627static rb_node_dot2_t *
11628rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11629{
11630 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11631 n->nd_beg = nd_beg;
11632 n->nd_end = nd_end;
11633 n->operator_loc = *operator_loc;
11634
11635 return n;
11636}
11637
11638static rb_node_dot3_t *
11639rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11640{
11641 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11642 n->nd_beg = nd_beg;
11643 n->nd_end = nd_end;
11644 n->operator_loc = *operator_loc;
11645
11646 return n;
11647}
11648
11649static rb_node_self_t *
11650rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11651{
11652 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11653 n->nd_state = 1;
11654
11655 return n;
11656}
11657
11658static rb_node_nil_t *
11659rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11660{
11661 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11662
11663 return n;
11664}
11665
11666static rb_node_true_t *
11667rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11668{
11669 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11670
11671 return n;
11672}
11673
11674static rb_node_false_t *
11675rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11676{
11677 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11678
11679 return n;
11680}
11681
11682static rb_node_super_t *
11683rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11684 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11685{
11686 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11687 n->nd_args = nd_args;
11688 n->keyword_loc = *keyword_loc;
11689 n->lparen_loc = *lparen_loc;
11690 n->rparen_loc = *rparen_loc;
11691
11692 return n;
11693}
11694
11695static rb_node_zsuper_t *
11696rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11697{
11698 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11699
11700 return n;
11701}
11702
11703static rb_node_match2_t *
11704rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11705{
11706 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11707 n->nd_recv = nd_recv;
11708 n->nd_value = nd_value;
11709 n->nd_args = 0;
11710
11711 return n;
11712}
11713
11714static rb_node_match3_t *
11715rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11716{
11717 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11718 n->nd_recv = nd_recv;
11719 n->nd_value = nd_value;
11720
11721 return n;
11722}
11723
11724static rb_node_list_t *
11725rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11726{
11727 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11728}
11729
11730static rb_node_list_t *
11731rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11732{
11733 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11734 n->nd_head = nd_head;
11735 n->as.nd_alen = nd_alen;
11736 n->nd_next = nd_next;
11737
11738 return n;
11739}
11740
11741static rb_node_zlist_t *
11742rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11743{
11744 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11745
11746 return n;
11747}
11748
11749static rb_node_hash_t *
11750rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11751{
11752 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11753 n->nd_head = nd_head;
11754 n->nd_brace = 0;
11755
11756 return n;
11757}
11758
11759static rb_node_masgn_t *
11760rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11761{
11762 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11763 n->nd_head = nd_head;
11764 n->nd_value = 0;
11765 n->nd_args = nd_args;
11766
11767 return n;
11768}
11769
11770static rb_node_gasgn_t *
11771rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11772{
11773 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11774 n->nd_vid = nd_vid;
11775 n->nd_value = nd_value;
11776
11777 return n;
11778}
11779
11780static rb_node_lasgn_t *
11781rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11782{
11783 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11784 n->nd_vid = nd_vid;
11785 n->nd_value = nd_value;
11786
11787 return n;
11788}
11789
11790static rb_node_dasgn_t *
11791rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11792{
11793 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11794 n->nd_vid = nd_vid;
11795 n->nd_value = nd_value;
11796
11797 return n;
11798}
11799
11800static rb_node_iasgn_t *
11801rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11802{
11803 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11804 n->nd_vid = nd_vid;
11805 n->nd_value = nd_value;
11806
11807 return n;
11808}
11809
11810static rb_node_cvasgn_t *
11811rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11812{
11813 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11814 n->nd_vid = nd_vid;
11815 n->nd_value = nd_value;
11816
11817 return n;
11818}
11819
11820static rb_node_op_asgn1_t *
11821rb_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)
11822{
11823 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11824 n->nd_recv = nd_recv;
11825 n->nd_mid = nd_mid;
11826 n->nd_index = index;
11827 n->nd_rvalue = rvalue;
11828 n->call_operator_loc = *call_operator_loc;
11829 n->opening_loc = *opening_loc;
11830 n->closing_loc = *closing_loc;
11831 n->binary_operator_loc = *binary_operator_loc;
11832
11833 return n;
11834}
11835
11836static rb_node_op_asgn2_t *
11837rb_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)
11838{
11839 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11840 n->nd_recv = nd_recv;
11841 n->nd_value = nd_value;
11842 n->nd_vid = nd_vid;
11843 n->nd_mid = nd_mid;
11844 n->nd_aid = nd_aid;
11845 n->call_operator_loc = *call_operator_loc;
11846 n->message_loc = *message_loc;
11847 n->binary_operator_loc = *binary_operator_loc;
11848
11849 return n;
11850}
11851
11852static rb_node_op_asgn_or_t *
11853rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11854{
11855 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11856 n->nd_head = nd_head;
11857 n->nd_value = nd_value;
11858
11859 return n;
11860}
11861
11862static rb_node_op_asgn_and_t *
11863rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11864{
11865 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11866 n->nd_head = nd_head;
11867 n->nd_value = nd_value;
11868
11869 return n;
11870}
11871
11872static rb_node_gvar_t *
11873rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11874{
11875 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11876 n->nd_vid = nd_vid;
11877
11878 return n;
11879}
11880
11881static rb_node_lvar_t *
11882rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11883{
11884 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11885 n->nd_vid = nd_vid;
11886
11887 return n;
11888}
11889
11890static rb_node_dvar_t *
11891rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11892{
11893 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11894 n->nd_vid = nd_vid;
11895
11896 return n;
11897}
11898
11899static rb_node_ivar_t *
11900rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11901{
11902 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11903 n->nd_vid = nd_vid;
11904
11905 return n;
11906}
11907
11908static rb_node_const_t *
11909rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11910{
11911 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11912 n->nd_vid = nd_vid;
11913
11914 return n;
11915}
11916
11917static rb_node_cvar_t *
11918rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11919{
11920 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11921 n->nd_vid = nd_vid;
11922
11923 return n;
11924}
11925
11926static rb_node_nth_ref_t *
11927rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11928{
11929 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11930 n->nd_nth = nd_nth;
11931
11932 return n;
11933}
11934
11935static rb_node_back_ref_t *
11936rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11937{
11938 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11939 n->nd_nth = nd_nth;
11940
11941 return n;
11942}
11943
11944static rb_node_integer_t *
11945rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11946{
11947 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11948 n->val = val;
11949 n->minus = FALSE;
11950 n->base = base;
11951
11952 return n;
11953}
11954
11955static rb_node_float_t *
11956rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11957{
11958 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11959 n->val = val;
11960 n->minus = FALSE;
11961
11962 return n;
11963}
11964
11965static rb_node_rational_t *
11966rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11967{
11968 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11969 n->val = val;
11970 n->minus = FALSE;
11971 n->base = base;
11972 n->seen_point = seen_point;
11973
11974 return n;
11975}
11976
11977static rb_node_imaginary_t *
11978rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11979{
11980 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11981 n->val = val;
11982 n->minus = FALSE;
11983 n->base = base;
11984 n->seen_point = seen_point;
11985 n->type = numeric_type;
11986
11987 return n;
11988}
11989
11990static rb_node_str_t *
11991rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11992{
11993 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11994 n->string = string;
11995
11996 return n;
11997}
11998
11999/* TODO; Use union for NODE_DSTR2 */
12000static rb_node_dstr_t *
12001rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12002{
12003 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12004 n->string = string;
12005 n->as.nd_alen = nd_alen;
12006 n->nd_next = (rb_node_list_t *)nd_next;
12007
12008 return n;
12009}
12010
12011static rb_node_dstr_t *
12012rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12013{
12014 return rb_node_dstr_new0(p, string, 1, 0, loc);
12015}
12016
12017static rb_node_xstr_t *
12018rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12019{
12020 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12021 n->string = string;
12022
12023 return n;
12024}
12025
12026static rb_node_dxstr_t *
12027rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12028{
12029 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12030 n->string = string;
12031 n->as.nd_alen = nd_alen;
12032 n->nd_next = (rb_node_list_t *)nd_next;
12033
12034 return n;
12035}
12036
12037static rb_node_sym_t *
12038rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12039{
12040 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12041 n->string = rb_str_to_parser_string(p, str);
12042
12043 return n;
12044}
12045
12046static rb_node_dsym_t *
12047rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12048{
12049 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12050 n->string = string;
12051 n->as.nd_alen = nd_alen;
12052 n->nd_next = (rb_node_list_t *)nd_next;
12053
12054 return n;
12055}
12056
12057static rb_node_evstr_t *
12058rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12059{
12060 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12061 n->nd_body = nd_body;
12062 n->opening_loc = *opening_loc;
12063 n->closing_loc = *closing_loc;
12064
12065 return n;
12066}
12067
12068static rb_node_regx_t *
12069rb_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)
12070{
12071 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12072 n->string = string;
12073 n->options = options & RE_OPTION_MASK;
12074 n->opening_loc = *opening_loc;
12075 n->content_loc = *content_loc;
12076 n->closing_loc = *closing_loc;
12077
12078 return n;
12079}
12080
12081static rb_node_call_t *
12082rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12083{
12084 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12085 n->nd_recv = nd_recv;
12086 n->nd_mid = nd_mid;
12087 n->nd_args = nd_args;
12088
12089 return n;
12090}
12091
12092static rb_node_opcall_t *
12093rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12094{
12095 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12096 n->nd_recv = nd_recv;
12097 n->nd_mid = nd_mid;
12098 n->nd_args = nd_args;
12099
12100 return n;
12101}
12102
12103static rb_node_fcall_t *
12104rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12105{
12106 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12107 n->nd_mid = nd_mid;
12108 n->nd_args = nd_args;
12109
12110 return n;
12111}
12112
12113static rb_node_qcall_t *
12114rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12115{
12116 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12117 n->nd_recv = nd_recv;
12118 n->nd_mid = nd_mid;
12119 n->nd_args = nd_args;
12120
12121 return n;
12122}
12123
12124static rb_node_vcall_t *
12125rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12126{
12127 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12128 n->nd_mid = nd_mid;
12129
12130 return n;
12131}
12132
12133static rb_node_once_t *
12134rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12135{
12136 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12137 n->nd_body = nd_body;
12138
12139 return n;
12140}
12141
12142static rb_node_args_t *
12143rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12144{
12145 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12146 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12147
12148 return n;
12149}
12150
12151static rb_node_args_aux_t *
12152rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12153{
12154 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12155 n->nd_pid = nd_pid;
12156 n->nd_plen = nd_plen;
12157 n->nd_next = 0;
12158
12159 return n;
12160}
12161
12162static rb_node_opt_arg_t *
12163rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12164{
12165 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12166 n->nd_body = nd_body;
12167 n->nd_next = 0;
12168
12169 return n;
12170}
12171
12172static rb_node_kw_arg_t *
12173rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12174{
12175 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12176 n->nd_body = nd_body;
12177 n->nd_next = 0;
12178
12179 return n;
12180}
12181
12182static rb_node_postarg_t *
12183rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12184{
12185 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12186 n->nd_1st = nd_1st;
12187 n->nd_2nd = nd_2nd;
12188
12189 return n;
12190}
12191
12192static rb_node_argscat_t *
12193rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12194{
12195 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12196 n->nd_head = nd_head;
12197 n->nd_body = nd_body;
12198
12199 return n;
12200}
12201
12202static rb_node_argspush_t *
12203rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12204{
12205 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12206 n->nd_head = nd_head;
12207 n->nd_body = nd_body;
12208
12209 return n;
12210}
12211
12212static rb_node_splat_t *
12213rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12214{
12215 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12216 n->nd_head = nd_head;
12217 n->operator_loc = *operator_loc;
12218
12219 return n;
12220}
12221
12222static rb_node_block_pass_t *
12223rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12224{
12225 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12226 n->forwarding = 0;
12227 n->nd_head = 0;
12228 n->nd_body = nd_body;
12229 n->operator_loc = *operator_loc;
12230
12231 return n;
12232}
12233
12234static rb_node_alias_t *
12235rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12236{
12237 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12238 n->nd_1st = nd_1st;
12239 n->nd_2nd = nd_2nd;
12240 n->keyword_loc = *keyword_loc;
12241
12242 return n;
12243}
12244
12245static rb_node_valias_t *
12246rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12247{
12248 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12249 n->nd_alias = nd_alias;
12250 n->nd_orig = nd_orig;
12251 n->keyword_loc = *keyword_loc;
12252
12253 return n;
12254}
12255
12256static rb_node_undef_t *
12257rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12258{
12259 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12260 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12261 n->keyword_loc = NULL_LOC;
12262 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12263
12264 return n;
12265}
12266
12267static rb_node_errinfo_t *
12268rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12269{
12270 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12271
12272 return n;
12273}
12274
12275static rb_node_defined_t *
12276rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12277{
12278 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12279 n->nd_head = nd_head;
12280 n->keyword_loc = *keyword_loc;
12281
12282 return n;
12283}
12284
12285static rb_node_postexe_t *
12286rb_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)
12287{
12288 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12289 n->nd_body = nd_body;
12290 n->keyword_loc = *keyword_loc;
12291 n->opening_loc = *opening_loc;
12292 n->closing_loc = *closing_loc;
12293
12294 return n;
12295}
12296
12297static rb_node_attrasgn_t *
12298rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12299{
12300 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12301 n->nd_recv = nd_recv;
12302 n->nd_mid = nd_mid;
12303 n->nd_args = nd_args;
12304
12305 return n;
12306}
12307
12308static rb_node_aryptn_t *
12309rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12310{
12311 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12312 n->nd_pconst = 0;
12313 n->pre_args = pre_args;
12314 n->rest_arg = rest_arg;
12315 n->post_args = post_args;
12316
12317 return n;
12318}
12319
12320static rb_node_hshptn_t *
12321rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12322{
12323 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12324 n->nd_pconst = nd_pconst;
12325 n->nd_pkwargs = nd_pkwargs;
12326 n->nd_pkwrestarg = nd_pkwrestarg;
12327
12328 return n;
12329}
12330
12331static rb_node_fndptn_t *
12332rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12333{
12334 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12335 n->nd_pconst = 0;
12336 n->pre_rest_arg = pre_rest_arg;
12337 n->args = args;
12338 n->post_rest_arg = post_rest_arg;
12339
12340 return n;
12341}
12342
12343static rb_node_line_t *
12344rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12345{
12346 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12347
12348 return n;
12349}
12350
12351static rb_node_file_t *
12352rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12353{
12354 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12355 n->path = rb_str_to_parser_string(p, str);
12356
12357 return n;
12358}
12359
12360static rb_node_encoding_t *
12361rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12362{
12363 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12364 n->enc = p->enc;
12365
12366 return n;
12367}
12368
12369static rb_node_cdecl_t *
12370rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12371{
12372 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12373 n->nd_vid = nd_vid;
12374 n->nd_value = nd_value;
12375 n->nd_else = nd_else;
12376 n->shareability = shareability;
12377
12378 return n;
12379}
12380
12381static rb_node_op_cdecl_t *
12382rb_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)
12383{
12384 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12385 n->nd_head = nd_head;
12386 n->nd_value = nd_value;
12387 n->nd_aid = nd_aid;
12388 n->shareability = shareability;
12389
12390 return n;
12391}
12392
12393static rb_node_error_t *
12394rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12395{
12396 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12397
12398 return n;
12399}
12400
12401static rb_node_break_t *
12402rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12403{
12404 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12405 n->nd_stts = nd_stts;
12406 n->nd_chain = 0;
12407 n->keyword_loc = *keyword_loc;
12408
12409 return n;
12410}
12411
12412static rb_node_next_t *
12413rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12414{
12415 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12416 n->nd_stts = nd_stts;
12417 n->nd_chain = 0;
12418 n->keyword_loc = *keyword_loc;
12419
12420 return n;
12421}
12422
12423static rb_node_redo_t *
12424rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12425{
12426 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12427 n->nd_chain = 0;
12428 n->keyword_loc = *keyword_loc;
12429
12430 return n;
12431}
12432
12433static rb_node_def_temp_t *
12434rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12435{
12436 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12437 n->save.numparam_save = 0;
12438 n->save.max_numparam = 0;
12439 n->save.ctxt = p->ctxt;
12440 n->nd_def = 0;
12441 n->nd_mid = 0;
12442
12443 return n;
12444}
12445
12446static rb_node_def_temp_t *
12447def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12448{
12449 n->save.numparam_save = numparam_push(p);
12450 n->save.max_numparam = p->max_numparam;
12451 return n;
12452}
12453
12454#ifndef RIPPER
12455static enum node_type
12456nodetype(NODE *node) /* for debug */
12457{
12458 return (enum node_type)nd_type(node);
12459}
12460
12461static int
12462nodeline(NODE *node)
12463{
12464 return nd_line(node);
12465}
12466#endif
12467
12468static NODE*
12469newline_node(NODE *node)
12470{
12471 if (node) {
12472 node = remove_begin(node);
12473 nd_set_fl_newline(node);
12474 }
12475 return node;
12476}
12477
12478static void
12479fixpos(NODE *node, NODE *orig)
12480{
12481 if (!node) return;
12482 if (!orig) return;
12483 nd_set_line(node, nd_line(orig));
12484}
12485
12486static NODE*
12487block_append(struct parser_params *p, NODE *head, NODE *tail)
12488{
12489 NODE *end, *h = head, *nd;
12490
12491 if (tail == 0) return head;
12492
12493 if (h == 0) return tail;
12494 switch (nd_type(h)) {
12495 default:
12496 h = end = NEW_BLOCK(head, &head->nd_loc);
12497 head = end;
12498 break;
12499 case NODE_BLOCK:
12500 end = RNODE_BLOCK(h)->nd_end;
12501 break;
12502 }
12503
12504 nd = RNODE_BLOCK(end)->nd_head;
12505 switch (nd_type(nd)) {
12506 case NODE_RETURN:
12507 case NODE_BREAK:
12508 case NODE_NEXT:
12509 case NODE_REDO:
12510 case NODE_RETRY:
12511 rb_warning0L(nd_line(tail), "statement not reached");
12512 break;
12513
12514 default:
12515 break;
12516 }
12517
12518 if (!nd_type_p(tail, NODE_BLOCK)) {
12519 tail = NEW_BLOCK(tail, &tail->nd_loc);
12520 }
12521 RNODE_BLOCK(end)->nd_next = tail;
12522 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12523 nd_set_last_loc(head, nd_last_loc(tail));
12524 return head;
12525}
12526
12527/* append item to the list */
12528static NODE*
12529list_append(struct parser_params *p, NODE *list, NODE *item)
12530{
12531 NODE *last;
12532
12533 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12534 if (RNODE_LIST(list)->nd_next) {
12535 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12536 }
12537 else {
12538 last = list;
12539 }
12540
12541 RNODE_LIST(list)->as.nd_alen += 1;
12542 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12543 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12544
12545 nd_set_last_loc(list, nd_last_loc(item));
12546
12547 return list;
12548}
12549
12550/* concat two lists */
12551static NODE*
12552list_concat(NODE *head, NODE *tail)
12553{
12554 NODE *last;
12555
12556 if (RNODE_LIST(head)->nd_next) {
12557 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12558 }
12559 else {
12560 last = head;
12561 }
12562
12563 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12564 RNODE_LIST(last)->nd_next = tail;
12565 if (RNODE_LIST(tail)->nd_next) {
12566 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12567 }
12568 else {
12569 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12570 }
12571
12572 nd_set_last_loc(head, nd_last_loc(tail));
12573
12574 return head;
12575}
12576
12577static int
12578literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12579{
12580 if (!tail) return 1;
12581 if (!rb_parser_enc_compatible(p, head, tail)) {
12582 compile_error(p, "string literal encodings differ (%s / %s)",
12583 rb_enc_name(rb_parser_str_get_encoding(head)),
12584 rb_enc_name(rb_parser_str_get_encoding(tail)));
12585 rb_parser_str_resize(p, head, 0);
12586 rb_parser_str_resize(p, tail, 0);
12587 return 0;
12588 }
12589 rb_parser_str_buf_append(p, head, tail);
12590 return 1;
12591}
12592
12593static rb_parser_string_t *
12594string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12595{
12596 if (htype != NODE_DSTR) return NULL;
12597 if (RNODE_DSTR(head)->nd_next) {
12598 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12599 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12600 }
12601 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12602 ASSUME(lit);
12603 return lit;
12604}
12605
12606#ifndef RIPPER
12607static rb_parser_string_t *
12608rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12609{
12610 rb_parser_string_t *copy;
12611 if (!orig) return NULL;
12612 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12613 copy->coderange = orig->coderange;
12614 copy->enc = orig->enc;
12615 return copy;
12616}
12617#endif
12618
12619/* concat two string literals */
12620static NODE *
12621literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12622{
12623 enum node_type htype;
12624 rb_parser_string_t *lit;
12625
12626 if (!head) return tail;
12627 if (!tail) return head;
12628
12629 htype = nd_type(head);
12630 if (htype == NODE_EVSTR) {
12631 head = new_dstr(p, head, loc);
12632 htype = NODE_DSTR;
12633 }
12634 if (p->heredoc_indent > 0) {
12635 switch (htype) {
12636 case NODE_STR:
12637 head = str2dstr(p, head);
12638 case NODE_DSTR:
12639 return list_append(p, head, tail);
12640 default:
12641 break;
12642 }
12643 }
12644 switch (nd_type(tail)) {
12645 case NODE_STR:
12646 if ((lit = string_literal_head(p, htype, head)) != false) {
12647 htype = NODE_STR;
12648 }
12649 else {
12650 lit = RNODE_DSTR(head)->string;
12651 }
12652 if (htype == NODE_STR) {
12653 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12654 error:
12655 rb_discard_node(p, head);
12656 rb_discard_node(p, tail);
12657 return 0;
12658 }
12659 rb_discard_node(p, tail);
12660 }
12661 else {
12662 list_append(p, head, tail);
12663 }
12664 break;
12665
12666 case NODE_DSTR:
12667 if (htype == NODE_STR) {
12668 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12669 goto error;
12670 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12671 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12672 RNODE_STR(head)->string = NULL;
12673 rb_discard_node(p, head);
12674 head = tail;
12675 }
12676 else if (!RNODE_DSTR(tail)->string) {
12677 append:
12678 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12679 if (!RNODE_DSTR(head)->nd_next) {
12680 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12681 }
12682 else if (RNODE_DSTR(tail)->nd_next) {
12683 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12684 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12685 }
12686 rb_discard_node(p, tail);
12687 }
12688 else if ((lit = string_literal_head(p, htype, head)) != false) {
12689 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12690 goto error;
12691 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12692 RNODE_DSTR(tail)->string = 0;
12693 goto append;
12694 }
12695 else {
12696 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));
12697 RNODE_DSTR(tail)->string = 0;
12698 }
12699 break;
12700
12701 case NODE_EVSTR:
12702 if (htype == NODE_STR) {
12703 head = str2dstr(p, head);
12704 RNODE_DSTR(head)->as.nd_alen = 1;
12705 }
12706 list_append(p, head, tail);
12707 break;
12708 }
12709 return head;
12710}
12711
12712static void
12713nd_copy_flag(NODE *new_node, NODE *old_node)
12714{
12715 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12716 nd_set_line(new_node, nd_line(old_node));
12717 new_node->nd_loc = old_node->nd_loc;
12718 new_node->node_id = old_node->node_id;
12719}
12720
12721static NODE *
12722str2dstr(struct parser_params *p, NODE *node)
12723{
12724 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12725 nd_copy_flag(new_node, node);
12726 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12727 RNODE_DSTR(new_node)->as.nd_alen = 0;
12728 RNODE_DSTR(new_node)->nd_next = 0;
12729 RNODE_STR(node)->string = 0;
12730
12731 return new_node;
12732}
12733
12734static NODE *
12735str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12736{
12737 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12738 nd_copy_flag(new_node, node);
12739 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12740 RNODE_REGX(new_node)->options = options;
12741 nd_set_loc(new_node, loc);
12742 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12743 RNODE_REGX(new_node)->content_loc = *content_loc;
12744 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12745 RNODE_STR(node)->string = 0;
12746
12747 return new_node;
12748}
12749
12750static NODE *
12751evstr2dstr(struct parser_params *p, NODE *node)
12752{
12753 if (nd_type_p(node, NODE_EVSTR)) {
12754 node = new_dstr(p, node, &node->nd_loc);
12755 }
12756 return node;
12757}
12758
12759static NODE *
12760new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12761{
12762 NODE *head = node;
12763
12764 if (node) {
12765 switch (nd_type(node)) {
12766 case NODE_STR:
12767 return str2dstr(p, node);
12768 case NODE_DSTR:
12769 break;
12770 case NODE_EVSTR:
12771 return node;
12772 }
12773 }
12774 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12775}
12776
12777static NODE *
12778new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12779{
12780 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12781 return list_append(p, dstr, node);
12782}
12783
12784static NODE *
12785call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12786 const YYLTYPE *op_loc, const YYLTYPE *loc)
12787{
12788 NODE *expr;
12789 value_expr(p, recv);
12790 value_expr(p, arg1);
12791 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12792 nd_set_line(expr, op_loc->beg_pos.lineno);
12793 return expr;
12794}
12795
12796static NODE *
12797call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12798{
12799 NODE *opcall;
12800 value_expr(p, recv);
12801 opcall = NEW_OPCALL(recv, id, 0, loc);
12802 nd_set_line(opcall, op_loc->beg_pos.lineno);
12803 return opcall;
12804}
12805
12806static NODE *
12807new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12808{
12809 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12810 nd_set_line(qcall, op_loc->beg_pos.lineno);
12811 return qcall;
12812}
12813
12814static NODE*
12815new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12816{
12817 NODE *ret;
12818 if (block) block_dup_check(p, args, block);
12819 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12820 if (block) ret = method_add_block(p, ret, block, loc);
12821 fixpos(ret, recv);
12822 return ret;
12823}
12824
12825static rb_locations_lambda_body_t*
12826new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12827{
12828 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12829 body->node = node;
12830 body->opening_loc = *opening_loc;
12831 body->closing_loc = *closing_loc;
12832 return body;
12833}
12834
12835#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12836
12837static NODE*
12838last_expr_once_body(NODE *node)
12839{
12840 if (!node) return 0;
12841 return nd_once_body(node);
12842}
12843
12844static NODE*
12845match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12846{
12847 NODE *n;
12848 int line = op_loc->beg_pos.lineno;
12849
12850 value_expr(p, node1);
12851 value_expr(p, node2);
12852
12853 if ((n = last_expr_once_body(node1)) != 0) {
12854 switch (nd_type(n)) {
12855 case NODE_DREGX:
12856 {
12857 NODE *match = NEW_MATCH2(node1, node2, loc);
12858 nd_set_line(match, line);
12859 return match;
12860 }
12861
12862 case NODE_REGX:
12863 {
12864 const VALUE lit = rb_node_regx_string_val(n);
12865 if (!NIL_P(lit)) {
12866 NODE *match = NEW_MATCH2(node1, node2, loc);
12867 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12868 nd_set_line(match, line);
12869 return match;
12870 }
12871 }
12872 }
12873 }
12874
12875 if ((n = last_expr_once_body(node2)) != 0) {
12876 NODE *match3;
12877
12878 switch (nd_type(n)) {
12879 case NODE_DREGX:
12880 match3 = NEW_MATCH3(node2, node1, loc);
12881 return match3;
12882 }
12883 }
12884
12885 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12886 nd_set_line(n, line);
12887 return n;
12888}
12889
12890# if WARN_PAST_SCOPE
12891static int
12892past_dvar_p(struct parser_params *p, ID id)
12893{
12894 struct vtable *past = p->lvtbl->past;
12895 while (past) {
12896 if (vtable_included(past, id)) return 1;
12897 past = past->prev;
12898 }
12899 return 0;
12900}
12901# endif
12902
12903static int
12904numparam_nested_p(struct parser_params *p)
12905{
12906 struct local_vars *local = p->lvtbl;
12907 NODE *outer = local->numparam.outer;
12908 NODE *inner = local->numparam.inner;
12909 if (outer || inner) {
12910 NODE *used = outer ? outer : inner;
12911 compile_error(p, "numbered parameter is already used in %s block\n"
12912 "%s:%d: numbered parameter is already used here",
12913 outer ? "outer" : "inner",
12914 p->ruby_sourcefile, nd_line(used));
12915 parser_show_error_line(p, &used->nd_loc);
12916 return 1;
12917 }
12918 return 0;
12919}
12920
12921static int
12922numparam_used_p(struct parser_params *p)
12923{
12924 NODE *numparam = p->lvtbl->numparam.current;
12925 if (numparam) {
12926 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12927 "%s:%d: numbered parameter is already used here",
12928 p->ruby_sourcefile, nd_line(numparam));
12929 parser_show_error_line(p, &numparam->nd_loc);
12930 return 1;
12931 }
12932 return 0;
12933}
12934
12935static int
12936it_used_p(struct parser_params *p)
12937{
12938 NODE *it = p->lvtbl->it;
12939 if (it) {
12940 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12941 "%s:%d: 'it' is already used here",
12942 p->ruby_sourcefile, nd_line(it));
12943 parser_show_error_line(p, &it->nd_loc);
12944 return 1;
12945 }
12946 return 0;
12947}
12948
12949static NODE*
12950gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12951{
12952 ID *vidp = NULL;
12953 NODE *node;
12954 switch (id) {
12955 case keyword_self:
12956 return NEW_SELF(loc);
12957 case keyword_nil:
12958 return NEW_NIL(loc);
12959 case keyword_true:
12960 return NEW_TRUE(loc);
12961 case keyword_false:
12962 return NEW_FALSE(loc);
12963 case keyword__FILE__:
12964 {
12965 VALUE file = p->ruby_sourcefile_string;
12966 if (NIL_P(file))
12967 file = rb_str_new(0, 0);
12968 node = NEW_FILE(file, loc);
12969 }
12970 return node;
12971 case keyword__LINE__:
12972 return NEW_LINE(loc);
12973 case keyword__ENCODING__:
12974 return NEW_ENCODING(loc);
12975
12976 }
12977 switch (id_type(id)) {
12978 case ID_LOCAL:
12979 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12980 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
12981 if (vidp) *vidp |= LVAR_USED;
12982 node = NEW_DVAR(id, loc);
12983 return node;
12984 }
12985 if (local_id_ref(p, id, &vidp)) {
12986 if (vidp) *vidp |= LVAR_USED;
12987 node = NEW_LVAR(id, loc);
12988 return node;
12989 }
12990 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12991 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12992 if (numparam_nested_p(p) || it_used_p(p)) return 0;
12993 node = NEW_DVAR(id, loc);
12994 struct local_vars *local = p->lvtbl;
12995 if (!local->numparam.current) local->numparam.current = node;
12996 return node;
12997 }
12998# if WARN_PAST_SCOPE
12999 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13000 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13001 }
13002# endif
13003 /* method call without arguments */
13004 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13005 if (numparam_used_p(p)) return 0;
13006 if (p->max_numparam == ORDINAL_PARAM) {
13007 compile_error(p, "ordinary parameter is defined");
13008 return 0;
13009 }
13010 if (!p->it_id) {
13011 p->it_id = idItImplicit;
13012 vtable_add(p->lvtbl->args, p->it_id);
13013 }
13014 NODE *node = NEW_DVAR(p->it_id, loc);
13015 if (!p->lvtbl->it) p->lvtbl->it = node;
13016 return node;
13017 }
13018 return NEW_VCALL(id, loc);
13019 case ID_GLOBAL:
13020 return NEW_GVAR(id, loc);
13021 case ID_INSTANCE:
13022 return NEW_IVAR(id, loc);
13023 case ID_CONST:
13024 return NEW_CONST(id, loc);
13025 case ID_CLASS:
13026 return NEW_CVAR(id, loc);
13027 }
13028 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13029 return 0;
13030}
13031
13032static rb_node_opt_arg_t *
13033opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13034{
13035 rb_node_opt_arg_t *opts = opt_list;
13036 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13037
13038 while (opts->nd_next) {
13039 opts = opts->nd_next;
13040 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13041 }
13042 opts->nd_next = opt;
13043
13044 return opt_list;
13045}
13046
13047static rb_node_kw_arg_t *
13048kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13049{
13050 if (kwlist) {
13051 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13052 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13053 }
13054 return kwlist;
13055}
13056
13057static NODE *
13058new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13059{
13060 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13061 p->ctxt.has_trailing_semicolon = 0;
13062
13063 NODE *n = expr;
13064 while (n) {
13065 if (nd_type_p(n, NODE_BEGIN)) {
13066 n = RNODE_BEGIN(n)->nd_body;
13067 }
13068 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13069 n = RNODE_BLOCK(n)->nd_head;
13070 }
13071 else {
13072 break;
13073 }
13074 }
13075
13076 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13077 NODE *block = NEW_BLOCK(expr, loc);
13078 return NEW_DEFINED(block, loc, keyword_loc);
13079 }
13080
13081 return NEW_DEFINED(n, loc, keyword_loc);
13082}
13083
13084static NODE*
13085str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13086{
13087 VALUE lit;
13088 rb_parser_string_t *str = RNODE_STR(node)->string;
13089 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13090 yyerror1(loc, "invalid symbol");
13091 lit = STR_NEW0();
13092 }
13093 else {
13094 lit = rb_str_new_parser_string(str);
13095 }
13096 return NEW_SYM(lit, loc);
13097}
13098
13099static NODE*
13100symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13101{
13102 enum node_type type = nd_type(symbol);
13103 switch (type) {
13104 case NODE_DSTR:
13105 nd_set_type(symbol, NODE_DSYM);
13106 break;
13107 case NODE_STR:
13108 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13109 break;
13110 default:
13111 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13112 }
13113 return list_append(p, symbols, symbol);
13114}
13115
13116static void
13117dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13118{
13119 if (dreg->string) {
13120 reg_fragment_setenc(p, dreg->string, options);
13121 }
13122 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13123 NODE *frag = list->nd_head;
13124 if (nd_type_p(frag, NODE_STR)) {
13125 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13126 }
13127 else if (nd_type_p(frag, NODE_DSTR)) {
13128 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13129 }
13130 }
13131}
13132
13133static NODE *
13134new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13135{
13136 if (!node) {
13137 /* Check string is valid regex */
13138 rb_parser_string_t *str = STRING_NEW0();
13139 reg_compile(p, str, options);
13140 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13141 return node;
13142 }
13143 switch (nd_type(node)) {
13144 case NODE_STR:
13145 {
13146 /* Check string is valid regex */
13147 reg_compile(p, RNODE_STR(node)->string, options);
13148 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13149 }
13150 break;
13151 default:
13152 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13153 /* fall through */
13154 case NODE_DSTR:
13155 nd_set_type(node, NODE_DREGX);
13156 nd_set_loc(node, loc);
13157 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13158 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13159 if (dreg->nd_next) {
13160 dregex_fragment_setenc(p, dreg, options);
13161 }
13162 if (options & RE_OPTION_ONCE) {
13163 node = NEW_ONCE(node, loc);
13164 }
13165 break;
13166 }
13167 return node;
13168}
13169
13170static rb_node_kw_arg_t *
13171new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13172{
13173 if (!k) return 0;
13174 return NEW_KW_ARG((k), loc);
13175}
13176
13177static NODE *
13178new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13179{
13180 if (!node) {
13181 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13182 return xstr;
13183 }
13184 switch (nd_type(node)) {
13185 case NODE_STR:
13186 nd_set_type(node, NODE_XSTR);
13187 nd_set_loc(node, loc);
13188 break;
13189 case NODE_DSTR:
13190 nd_set_type(node, NODE_DXSTR);
13191 nd_set_loc(node, loc);
13192 break;
13193 default:
13194 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13195 break;
13196 }
13197 return node;
13198}
13199
13200static const
13201struct st_hash_type literal_type = {
13202 literal_cmp,
13203 literal_hash,
13204};
13205
13206static int nd_type_st_key_enable_p(NODE *node);
13207
13208static void
13209check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13210{
13211 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13212 if (!arg || !p->case_labels) return;
13213 if (!nd_type_st_key_enable_p(arg)) return;
13214
13215 if (p->case_labels == CHECK_LITERAL_WHEN) {
13216 p->case_labels = st_init_table(&literal_type);
13217 }
13218 else {
13219 st_data_t line;
13220 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13221 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13222 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13223 return;
13224 }
13225 }
13226 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13227}
13228
13229#ifdef RIPPER
13230static int
13231id_is_var(struct parser_params *p, ID id)
13232{
13233 if (is_notop_id(id)) {
13234 switch (id & ID_SCOPE_MASK) {
13235 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13236 return 1;
13237 case ID_LOCAL:
13238 if (dyna_in_block(p)) {
13239 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13240 }
13241 if (local_id(p, id)) return 1;
13242 /* method call without arguments */
13243 return 0;
13244 }
13245 }
13246 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13247 return 0;
13248}
13249#endif
13250
13251static inline enum lex_state_e
13252parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13253{
13254 if (p->debug) {
13255 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13256 }
13257 return p->lex.state = ls;
13258}
13259
13260#ifndef RIPPER
13261static void
13262flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13263{
13264 VALUE mesg = p->debug_buffer;
13265
13266 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13267 p->debug_buffer = Qnil;
13268 rb_io_puts(1, &mesg, out);
13269 }
13270 if (!NIL_P(str) && RSTRING_LEN(str)) {
13271 rb_io_write(p->debug_output, str);
13272 }
13273}
13274
13275static const char rb_parser_lex_state_names[][8] = {
13276 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13277 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13278 "LABEL", "LABELED","FITEM",
13279};
13280
13281static VALUE
13282append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13283{
13284 int i, sep = 0;
13285 unsigned int mask = 1;
13286 static const char none[] = "NONE";
13287
13288 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13289 if ((unsigned)state & mask) {
13290 if (sep) {
13291 rb_str_cat(buf, "|", 1);
13292 }
13293 sep = 1;
13294 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13295 }
13296 }
13297 if (!sep) {
13298 rb_str_cat(buf, none, sizeof(none)-1);
13299 }
13300 return buf;
13301}
13302
13303enum lex_state_e
13304rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13305 enum lex_state_e to, int line)
13306{
13307 VALUE mesg;
13308 mesg = rb_str_new_cstr("lex_state: ");
13309 append_lex_state_name(p, from, mesg);
13310 rb_str_cat_cstr(mesg, " -> ");
13311 append_lex_state_name(p, to, mesg);
13312 rb_str_catf(mesg, " at line %d\n", line);
13313 flush_debug_buffer(p, p->debug_output, mesg);
13314 return to;
13315}
13316
13317VALUE
13318rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13319{
13320 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13321}
13322
13323static void
13324append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13325{
13326 if (stack == 0) {
13327 rb_str_cat_cstr(mesg, "0");
13328 }
13329 else {
13330 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13331 for (; mask && !(stack & mask); mask >>= 1) continue;
13332 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13333 }
13334}
13335
13336void
13337rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13338 const char *name, int line)
13339{
13340 VALUE mesg = rb_sprintf("%s: ", name);
13341 append_bitstack_value(p, stack, mesg);
13342 rb_str_catf(mesg, " at line %d\n", line);
13343 flush_debug_buffer(p, p->debug_output, mesg);
13344}
13345
13346void
13347rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13348{
13349 va_list ap;
13350 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13351
13352 va_start(ap, fmt);
13353 rb_str_vcatf(mesg, fmt, ap);
13354 va_end(ap);
13355 yyerror0(RSTRING_PTR(mesg));
13356 RB_GC_GUARD(mesg);
13357
13358 mesg = rb_str_new(0, 0);
13359 append_lex_state_name(p, p->lex.state, mesg);
13360 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13361 rb_str_resize(mesg, 0);
13362 append_bitstack_value(p, p->cond_stack, mesg);
13363 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13364 rb_str_resize(mesg, 0);
13365 append_bitstack_value(p, p->cmdarg_stack, mesg);
13366 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13367 if (p->debug_output == rb_ractor_stdout())
13368 p->debug_output = rb_ractor_stderr();
13369 p->debug = TRUE;
13370}
13371
13372static YYLTYPE *
13373rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13374{
13375 yylloc->beg_pos.lineno = sourceline;
13376 yylloc->beg_pos.column = beg_pos;
13377 yylloc->end_pos.lineno = sourceline;
13378 yylloc->end_pos.column = end_pos;
13379 return yylloc;
13380}
13381
13382YYLTYPE *
13383rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13384{
13385 int sourceline = here->sourceline;
13386 int beg_pos = (int)here->offset - here->quote
13387 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13388 int end_pos = (int)here->offset + here->length + here->quote;
13389
13390 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13391}
13392
13393YYLTYPE *
13394rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13395{
13396 yylloc->beg_pos.lineno = p->delayed.beg_line;
13397 yylloc->beg_pos.column = p->delayed.beg_col;
13398 yylloc->end_pos.lineno = p->delayed.end_line;
13399 yylloc->end_pos.column = p->delayed.end_col;
13400
13401 return yylloc;
13402}
13403
13404YYLTYPE *
13405rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13406{
13407 int sourceline = p->ruby_sourceline;
13408 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13409 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13410 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13411}
13412
13413YYLTYPE *
13414rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13415{
13416 yylloc->end_pos = yylloc->beg_pos;
13417
13418 return yylloc;
13419}
13420
13421YYLTYPE *
13422rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13423{
13424 int sourceline = p->ruby_sourceline;
13425 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13426 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13427 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13428}
13429
13430YYLTYPE *
13431rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13432{
13433 int sourceline = p->ruby_sourceline;
13434 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13435 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13436 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13437}
13438#endif /* !RIPPER */
13439
13440static int
13441assignable0(struct parser_params *p, ID id, const char **err)
13442{
13443 if (!id) return -1;
13444 switch (id) {
13445 case keyword_self:
13446 *err = "Can't change the value of self";
13447 return -1;
13448 case keyword_nil:
13449 *err = "Can't assign to nil";
13450 return -1;
13451 case keyword_true:
13452 *err = "Can't assign to true";
13453 return -1;
13454 case keyword_false:
13455 *err = "Can't assign to false";
13456 return -1;
13457 case keyword__FILE__:
13458 *err = "Can't assign to __FILE__";
13459 return -1;
13460 case keyword__LINE__:
13461 *err = "Can't assign to __LINE__";
13462 return -1;
13463 case keyword__ENCODING__:
13464 *err = "Can't assign to __ENCODING__";
13465 return -1;
13466 }
13467 switch (id_type(id)) {
13468 case ID_LOCAL:
13469 if (dyna_in_block(p)) {
13470 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13471 compile_error(p, "Can't assign to numbered parameter _%d",
13472 NUMPARAM_ID_TO_IDX(id));
13473 return -1;
13474 }
13475 if (dvar_curr(p, id)) return NODE_DASGN;
13476 if (dvar_defined(p, id)) return NODE_DASGN;
13477 if (local_id(p, id)) return NODE_LASGN;
13478 dyna_var(p, id);
13479 return NODE_DASGN;
13480 }
13481 else {
13482 if (!local_id(p, id)) local_var(p, id);
13483 return NODE_LASGN;
13484 }
13485 break;
13486 case ID_GLOBAL: return NODE_GASGN;
13487 case ID_INSTANCE: return NODE_IASGN;
13488 case ID_CONST:
13489 if (!p->ctxt.in_def) return NODE_CDECL;
13490 *err = "dynamic constant assignment";
13491 return -1;
13492 case ID_CLASS: return NODE_CVASGN;
13493 default:
13494 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13495 }
13496 return -1;
13497}
13498
13499static NODE*
13500assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13501{
13502 const char *err = 0;
13503 int node_type = assignable0(p, id, &err);
13504 switch (node_type) {
13505 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13506 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13507 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13508 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13509 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13510 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13511 }
13512/* TODO: FIXME */
13513#ifndef RIPPER
13514 if (err) yyerror1(loc, err);
13515#else
13516 if (err) set_value(assign_error(p, err, p->s_lvalue));
13517#endif
13518 return NEW_ERROR(loc);
13519}
13520
13521static int
13522is_private_local_id(struct parser_params *p, ID name)
13523{
13524 VALUE s;
13525 if (name == idUScore) return 1;
13526 if (!is_local_id(name)) return 0;
13527 s = rb_id2str(name);
13528 if (!s) return 0;
13529 return RSTRING_PTR(s)[0] == '_';
13530}
13531
13532static int
13533shadowing_lvar_0(struct parser_params *p, ID name)
13534{
13535 if (dyna_in_block(p)) {
13536 if (dvar_curr(p, name)) {
13537 if (is_private_local_id(p, name)) return 1;
13538 yyerror0("duplicated argument name");
13539 }
13540 else if (dvar_defined(p, name) || local_id(p, name)) {
13541 vtable_add(p->lvtbl->vars, name);
13542 if (p->lvtbl->used) {
13543 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13544 }
13545 return 0;
13546 }
13547 }
13548 else {
13549 if (local_id(p, name)) {
13550 if (is_private_local_id(p, name)) return 1;
13551 yyerror0("duplicated argument name");
13552 }
13553 }
13554 return 1;
13555}
13556
13557static ID
13558shadowing_lvar(struct parser_params *p, ID name)
13559{
13560 shadowing_lvar_0(p, name);
13561 return name;
13562}
13563
13564static void
13565new_bv(struct parser_params *p, ID name)
13566{
13567 if (!name) return;
13568 if (!is_local_id(name)) {
13569 compile_error(p, "invalid local variable - %"PRIsVALUE,
13570 rb_id2str(name));
13571 return;
13572 }
13573 if (!shadowing_lvar_0(p, name)) return;
13574 dyna_var(p, name);
13575 ID *vidp = 0;
13576 if (dvar_defined_ref(p, name, &vidp)) {
13577 if (vidp) *vidp |= LVAR_USED;
13578 }
13579}
13580
13581static void
13582aryset_check(struct parser_params *p, NODE *args)
13583{
13584 NODE *block = 0, *kwds = 0;
13585 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13586 block = RNODE_BLOCK_PASS(args)->nd_body;
13587 args = RNODE_BLOCK_PASS(args)->nd_head;
13588 }
13589 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13590 args = RNODE_ARGSCAT(args)->nd_body;
13591 }
13592 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13593 kwds = RNODE_ARGSPUSH(args)->nd_body;
13594 }
13595 else {
13596 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13597 next = RNODE_LIST(next)->nd_next) {
13598 kwds = RNODE_LIST(next)->nd_head;
13599 }
13600 }
13601 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13602 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13603 }
13604 if (block) {
13605 yyerror1(&block->nd_loc, "block arg given in index assignment");
13606 }
13607}
13608
13609static NODE *
13610aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13611{
13612 aryset_check(p, idx);
13613 return NEW_ATTRASGN(recv, tASET, idx, loc);
13614}
13615
13616static void
13617block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13618{
13619 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13620 compile_error(p, "both block arg and actual block given");
13621 }
13622}
13623
13624static NODE *
13625attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13626{
13627 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13628 return NEW_ATTRASGN(recv, id, 0, loc);
13629}
13630
13631static VALUE
13632rb_backref_error(struct parser_params *p, NODE *node)
13633{
13634#ifndef RIPPER
13635# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13636#else
13637# define ERR(...) rb_sprintf(__VA_ARGS__)
13638#endif
13639 switch (nd_type(node)) {
13640 case NODE_NTH_REF:
13641 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13642 case NODE_BACK_REF:
13643 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13644 }
13645#undef ERR
13646 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13647}
13648
13649static NODE *
13650arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13651{
13652 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13653 switch (nd_type(node1)) {
13654 case NODE_LIST:
13655 return list_append(p, node1, node2);
13656 case NODE_BLOCK_PASS:
13657 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13658 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13659 return node1;
13660 case NODE_ARGSPUSH:
13661 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13662 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13663 nd_set_type(node1, NODE_ARGSCAT);
13664 return node1;
13665 case NODE_ARGSCAT:
13666 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13667 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13668 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13669 return node1;
13670 }
13671 return NEW_ARGSPUSH(node1, node2, loc);
13672}
13673
13674static NODE *
13675arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13676{
13677 if (!node2) return node1;
13678 switch (nd_type(node1)) {
13679 case NODE_BLOCK_PASS:
13680 if (RNODE_BLOCK_PASS(node1)->nd_head)
13681 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13682 else
13683 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13684 return node1;
13685 case NODE_ARGSPUSH:
13686 if (!nd_type_p(node2, NODE_LIST)) break;
13687 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13688 nd_set_type(node1, NODE_ARGSCAT);
13689 return node1;
13690 case NODE_ARGSCAT:
13691 if (!nd_type_p(node2, NODE_LIST) ||
13692 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13693 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13694 return node1;
13695 }
13696 return NEW_ARGSCAT(node1, node2, loc);
13697}
13698
13699static NODE *
13700last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13701{
13702 NODE *n1;
13703 if ((n1 = splat_array(args)) != 0) {
13704 return list_append(p, n1, last_arg);
13705 }
13706 return arg_append(p, args, last_arg, loc);
13707}
13708
13709static NODE *
13710rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13711{
13712 NODE *n1;
13713 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13714 return list_concat(n1, rest_arg);
13715 }
13716 return arg_concat(p, args, rest_arg, loc);
13717}
13718
13719static NODE *
13720splat_array(NODE* node)
13721{
13722 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13723 if (nd_type_p(node, NODE_LIST)) return node;
13724 return 0;
13725}
13726
13727static void
13728mark_lvar_used(struct parser_params *p, NODE *rhs)
13729{
13730 ID *vidp = NULL;
13731 if (!rhs) return;
13732 switch (nd_type(rhs)) {
13733 case NODE_LASGN:
13734 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13735 if (vidp) *vidp |= LVAR_USED;
13736 }
13737 break;
13738 case NODE_DASGN:
13739 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13740 if (vidp) *vidp |= LVAR_USED;
13741 }
13742 break;
13743#if 0
13744 case NODE_MASGN:
13745 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13746 mark_lvar_used(p, rhs->nd_head);
13747 }
13748 break;
13749#endif
13750 }
13751}
13752
13753static int is_static_content(NODE *node);
13754
13755static NODE *
13756node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13757{
13758 if (!lhs) return 0;
13759
13760 switch (nd_type(lhs)) {
13761 case NODE_CDECL:
13762 case NODE_GASGN:
13763 case NODE_IASGN:
13764 case NODE_LASGN:
13765 case NODE_DASGN:
13766 case NODE_MASGN:
13767 case NODE_CVASGN:
13768 set_nd_value(p, lhs, rhs);
13769 nd_set_loc(lhs, loc);
13770 break;
13771
13772 case NODE_ATTRASGN:
13773 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13774 nd_set_loc(lhs, loc);
13775 break;
13776
13777 default:
13778 /* should not happen */
13779 break;
13780 }
13781
13782 return lhs;
13783}
13784
13785static NODE *
13786value_expr_check(struct parser_params *p, NODE *node)
13787{
13788 NODE *void_node = 0, *vn;
13789
13790 if (!node) {
13791 rb_warning0("empty expression");
13792 }
13793 while (node) {
13794 switch (nd_type(node)) {
13795 case NODE_ENSURE:
13796 vn = RNODE_ENSURE(node)->nd_head;
13797 node = RNODE_ENSURE(node)->nd_ensr;
13798 /* nd_ensr should not be NULL, check it out next */
13799 if (vn && (vn = value_expr_check(p, vn))) {
13800 goto found;
13801 }
13802 break;
13803
13804 case NODE_RESCUE:
13805 /* void only if all children are void */
13806 vn = RNODE_RESCUE(node)->nd_head;
13807 if (!vn || !(vn = value_expr_check(p, vn))) {
13808 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13809 }
13810 if (!void_node) void_node = vn;
13811 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13812 if (!nd_type_p(r, NODE_RESBODY)) {
13813 compile_error(p, "unexpected node");
13814 return NULL;
13815 }
13816 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13817 return NULL;
13818 }
13819 if (!void_node) void_node = vn;
13820 }
13821 node = RNODE_RESCUE(node)->nd_else;
13822 if (!node) return void_node;
13823 break;
13824
13825 case NODE_RETURN:
13826 case NODE_BREAK:
13827 case NODE_NEXT:
13828 case NODE_REDO:
13829 case NODE_RETRY:
13830 goto found;
13831
13832 case NODE_CASE:
13833 case NODE_CASE2:
13834 for (node = RNODE_CASE(node)->nd_body;
13835 node && nd_type_p(node, NODE_WHEN);
13836 node = RNODE_WHEN(node)->nd_next) {
13837 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13838 return NULL;
13839 }
13840 if (!void_node) void_node = vn;
13841 }
13842 break;
13843
13844 case NODE_CASE3:
13845 {
13846 NODE *in = RNODE_CASE3(node)->nd_body;
13847 if (!in || !nd_type_p(in, NODE_IN)) {
13848 compile_error(p, "unexpected node");
13849 return NULL;
13850 }
13851 if (!RNODE_IN(in)->nd_body) {
13852 /* single line pattern matching with "=>" operator */
13853 goto found;
13854 }
13855 do {
13856 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13857 if (!vn) return NULL;
13858 if (!void_node) void_node = vn;
13859 in = RNODE_IN(in)->nd_next;
13860 } while (in && nd_type_p(in, NODE_IN));
13861 node = in; /* else */
13862 }
13863 break;
13864
13865 case NODE_BLOCK:
13866 while (RNODE_BLOCK(node)->nd_next) {
13867 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13868 if (vn) return vn;
13869 node = RNODE_BLOCK(node)->nd_next;
13870 }
13871 node = RNODE_BLOCK(node)->nd_head;
13872 break;
13873
13874 case NODE_BEGIN:
13875 node = RNODE_BEGIN(node)->nd_body;
13876 break;
13877
13878 case NODE_IF:
13879 case NODE_UNLESS:
13880 if (!RNODE_IF(node)->nd_body) {
13881 return NULL;
13882 }
13883 else if (!RNODE_IF(node)->nd_else) {
13884 return NULL;
13885 }
13886 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13887 if (!vn) return NULL;
13888 if (!void_node) void_node = vn;
13889 node = RNODE_IF(node)->nd_else;
13890 break;
13891
13892 case NODE_AND:
13893 case NODE_OR:
13894 node = RNODE_AND(node)->nd_1st;
13895 break;
13896
13897 case NODE_LASGN:
13898 case NODE_DASGN:
13899 case NODE_MASGN:
13900 mark_lvar_used(p, node);
13901 return NULL;
13902
13903 default:
13904 return NULL;
13905 }
13906 }
13907
13908 return NULL;
13909
13910 found:
13911 /* return the first found node */
13912 return void_node ? void_node : node;
13913}
13914
13915static int
13916value_expr(struct parser_params *p, NODE *node)
13917{
13918 NODE *void_node = value_expr_check(p, node);
13919 if (void_node) {
13920 yyerror1(&void_node->nd_loc, "void value expression");
13921 /* or "control never reach"? */
13922 return FALSE;
13923 }
13924 return TRUE;
13925}
13926
13927static void
13928void_expr(struct parser_params *p, NODE *node)
13929{
13930 const char *useless = 0;
13931
13932 if (!RTEST(ruby_verbose)) return;
13933
13934 if (!node || !(node = nd_once_body(node))) return;
13935 switch (nd_type(node)) {
13936 case NODE_OPCALL:
13937 switch (RNODE_OPCALL(node)->nd_mid) {
13938 case '+':
13939 case '-':
13940 case '*':
13941 case '/':
13942 case '%':
13943 case tPOW:
13944 case tUPLUS:
13945 case tUMINUS:
13946 case '|':
13947 case '^':
13948 case '&':
13949 case tCMP:
13950 case '>':
13951 case tGEQ:
13952 case '<':
13953 case tLEQ:
13954 case tEQ:
13955 case tNEQ:
13956 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13957 break;
13958 }
13959 break;
13960
13961 case NODE_LVAR:
13962 case NODE_DVAR:
13963 case NODE_GVAR:
13964 case NODE_IVAR:
13965 case NODE_CVAR:
13966 case NODE_NTH_REF:
13967 case NODE_BACK_REF:
13968 useless = "a variable";
13969 break;
13970 case NODE_CONST:
13971 useless = "a constant";
13972 break;
13973 case NODE_SYM:
13974 case NODE_LINE:
13975 case NODE_FILE:
13976 case NODE_ENCODING:
13977 case NODE_INTEGER:
13978 case NODE_FLOAT:
13979 case NODE_RATIONAL:
13980 case NODE_IMAGINARY:
13981 case NODE_STR:
13982 case NODE_DSTR:
13983 case NODE_REGX:
13984 case NODE_DREGX:
13985 useless = "a literal";
13986 break;
13987 case NODE_COLON2:
13988 case NODE_COLON3:
13989 useless = "::";
13990 break;
13991 case NODE_DOT2:
13992 useless = "..";
13993 break;
13994 case NODE_DOT3:
13995 useless = "...";
13996 break;
13997 case NODE_SELF:
13998 useless = "self";
13999 break;
14000 case NODE_NIL:
14001 useless = "nil";
14002 break;
14003 case NODE_TRUE:
14004 useless = "true";
14005 break;
14006 case NODE_FALSE:
14007 useless = "false";
14008 break;
14009 case NODE_DEFINED:
14010 useless = "defined?";
14011 break;
14012 }
14013
14014 if (useless) {
14015 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14016 }
14017}
14018
14019/* warns useless use of block and returns the last statement node */
14020static NODE *
14021void_stmts(struct parser_params *p, NODE *node)
14022{
14023 NODE *const n = node;
14024 if (!RTEST(ruby_verbose)) return n;
14025 if (!node) return n;
14026 if (!nd_type_p(node, NODE_BLOCK)) return n;
14027
14028 while (RNODE_BLOCK(node)->nd_next) {
14029 void_expr(p, RNODE_BLOCK(node)->nd_head);
14030 node = RNODE_BLOCK(node)->nd_next;
14031 }
14032 return RNODE_BLOCK(node)->nd_head;
14033}
14034
14035static NODE *
14036remove_begin(NODE *node)
14037{
14038 NODE **n = &node, *n1 = node;
14039 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14040 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14041 }
14042 return node;
14043}
14044
14045static void
14046reduce_nodes(struct parser_params *p, NODE **body)
14047{
14048 NODE *node = *body;
14049
14050 if (!node) {
14051 *body = NEW_NIL(&NULL_LOC);
14052 return;
14053 }
14054#define subnodes(type, n1, n2) \
14055 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14056 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14057 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14058
14059 while (node) {
14060 int newline = (int)nd_fl_newline(node);
14061 switch (nd_type(node)) {
14062 end:
14063 case NODE_NIL:
14064 *body = 0;
14065 return;
14066 case NODE_BEGIN:
14067 *body = node = RNODE_BEGIN(node)->nd_body;
14068 if (newline && node) nd_set_fl_newline(node);
14069 continue;
14070 case NODE_BLOCK:
14071 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14072 break;
14073 case NODE_IF:
14074 case NODE_UNLESS:
14075 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14076 return;
14077 case NODE_CASE:
14078 body = &RNODE_CASE(node)->nd_body;
14079 break;
14080 case NODE_WHEN:
14081 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14082 break;
14083 case NODE_ENSURE:
14084 body = &RNODE_ENSURE(node)->nd_head;
14085 break;
14086 case NODE_RESCUE:
14087 newline = 0; // RESBODY should not be a NEWLINE
14088 if (RNODE_RESCUE(node)->nd_else) {
14089 body = &RNODE_RESCUE(node)->nd_resq;
14090 break;
14091 }
14092 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14093 break;
14094 default:
14095 return;
14096 }
14097 node = *body;
14098 if (newline && node) nd_set_fl_newline(node);
14099 }
14100
14101#undef subnodes
14102}
14103
14104static int
14105is_static_content(NODE *node)
14106{
14107 if (!node) return 1;
14108 switch (nd_type(node)) {
14109 case NODE_HASH:
14110 if (!(node = RNODE_HASH(node)->nd_head)) break;
14111 case NODE_LIST:
14112 do {
14113 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14114 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14115 case NODE_SYM:
14116 case NODE_REGX:
14117 case NODE_LINE:
14118 case NODE_FILE:
14119 case NODE_ENCODING:
14120 case NODE_INTEGER:
14121 case NODE_FLOAT:
14122 case NODE_RATIONAL:
14123 case NODE_IMAGINARY:
14124 case NODE_STR:
14125 case NODE_NIL:
14126 case NODE_TRUE:
14127 case NODE_FALSE:
14128 case NODE_ZLIST:
14129 break;
14130 default:
14131 return 0;
14132 }
14133 return 1;
14134}
14135
14136static int
14137assign_in_cond(struct parser_params *p, NODE *node)
14138{
14139 switch (nd_type(node)) {
14140 case NODE_MASGN:
14141 case NODE_LASGN:
14142 case NODE_DASGN:
14143 case NODE_GASGN:
14144 case NODE_IASGN:
14145 case NODE_CVASGN:
14146 case NODE_CDECL:
14147 break;
14148
14149 default:
14150 return 0;
14151 }
14152
14153 if (!get_nd_value(p, node)) return 1;
14154 if (is_static_content(get_nd_value(p, node))) {
14155 /* reports always */
14156 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14157 }
14158 return 1;
14159}
14160
14161enum cond_type {
14162 COND_IN_OP,
14163 COND_IN_COND,
14164 COND_IN_FF
14165};
14166
14167#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14168 switch (t) { \
14169 case COND_IN_OP: break; \
14170 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14171 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14172 } \
14173} while (0)
14174
14175static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14176
14177static NODE*
14178range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14179{
14180 enum node_type type;
14181
14182 if (node == 0) return 0;
14183
14184 type = nd_type(node);
14185 value_expr(p, node);
14186 if (type == NODE_INTEGER) {
14187 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14188 ID lineno = rb_intern("$.");
14189 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14190 }
14191 return cond0(p, node, COND_IN_FF, loc, true);
14192}
14193
14194static NODE*
14195cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14196{
14197 if (node == 0) return 0;
14198 if (!(node = nd_once_body(node))) return 0;
14199 assign_in_cond(p, node);
14200
14201 switch (nd_type(node)) {
14202 case NODE_BEGIN:
14203 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14204 break;
14205
14206 case NODE_DSTR:
14207 case NODE_EVSTR:
14208 case NODE_STR:
14209 case NODE_FILE:
14210 SWITCH_BY_COND_TYPE(type, warn, "string ");
14211 break;
14212
14213 case NODE_REGX:
14214 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14215 nd_set_type(node, NODE_MATCH);
14216 break;
14217
14218 case NODE_DREGX:
14219 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14220
14221 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14222
14223 case NODE_BLOCK:
14224 {
14225 NODE *end = RNODE_BLOCK(node)->nd_end;
14226 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14227 if (top) top = node == end;
14228 *expr = cond0(p, *expr, type, loc, top);
14229 }
14230 break;
14231
14232 case NODE_AND:
14233 case NODE_OR:
14234 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14235 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14236 break;
14237
14238 case NODE_DOT2:
14239 case NODE_DOT3:
14240 if (!top) break;
14241 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14242 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14243 switch (nd_type(node)) {
14244 case NODE_DOT2:
14245 nd_set_type(node,NODE_FLIP2);
14246 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14247 (void)flip2;
14248 break;
14249 case NODE_DOT3:
14250 nd_set_type(node, NODE_FLIP3);
14251 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14252 (void)flip3;
14253 break;
14254 }
14255 break;
14256
14257 case NODE_SYM:
14258 case NODE_DSYM:
14259 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14260 break;
14261
14262 case NODE_LINE:
14263 case NODE_ENCODING:
14264 case NODE_INTEGER:
14265 case NODE_FLOAT:
14266 case NODE_RATIONAL:
14267 case NODE_IMAGINARY:
14268 SWITCH_BY_COND_TYPE(type, warning, "");
14269 break;
14270
14271 default:
14272 break;
14273 }
14274 return node;
14275}
14276
14277static NODE*
14278cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14279{
14280 if (node == 0) return 0;
14281 return cond0(p, node, COND_IN_COND, loc, true);
14282}
14283
14284static NODE*
14285method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14286{
14287 if (node == 0) return 0;
14288 return cond0(p, node, COND_IN_OP, loc, true);
14289}
14290
14291static NODE*
14292new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14293{
14294 YYLTYPE loc = {*pos, *pos};
14295 return NEW_NIL(&loc);
14296}
14297
14298static NODE*
14299new_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)
14300{
14301 if (!cc) return right;
14302 cc = cond0(p, cc, COND_IN_COND, loc, true);
14303 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14304}
14305
14306static NODE*
14307new_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)
14308{
14309 if (!cc) return right;
14310 cc = cond0(p, cc, COND_IN_COND, loc, true);
14311 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14312}
14313
14314#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))
14315
14316static NODE*
14317logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14318 const YYLTYPE *op_loc, const YYLTYPE *loc)
14319{
14320 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14321 NODE *op;
14322 value_expr(p, left);
14323 if (left && nd_type_p(left, type)) {
14324 NODE *node = left, *second;
14325 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14326 node = second;
14327 }
14328 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14329 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14330 left->nd_loc.end_pos = loc->end_pos;
14331 return left;
14332 }
14333 op = NEW_AND_OR(type, left, right, loc, op_loc);
14334 nd_set_line(op, op_loc->beg_pos.lineno);
14335 return op;
14336}
14337
14338#undef NEW_AND_OR
14339
14340static void
14341no_blockarg(struct parser_params *p, NODE *node)
14342{
14343 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14344 compile_error(p, "block argument should not be given");
14345 }
14346}
14347
14348static NODE *
14349ret_args(struct parser_params *p, NODE *node)
14350{
14351 if (node) {
14352 no_blockarg(p, node);
14353 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14354 node = RNODE_LIST(node)->nd_head;
14355 }
14356 }
14357 return node;
14358}
14359
14360static NODE*
14361negate_lit(struct parser_params *p, NODE* node)
14362{
14363 switch (nd_type(node)) {
14364 case NODE_INTEGER:
14365 RNODE_INTEGER(node)->minus = TRUE;
14366 break;
14367 case NODE_FLOAT:
14368 RNODE_FLOAT(node)->minus = TRUE;
14369 break;
14370 case NODE_RATIONAL:
14371 RNODE_RATIONAL(node)->minus = TRUE;
14372 break;
14373 case NODE_IMAGINARY:
14374 RNODE_IMAGINARY(node)->minus = TRUE;
14375 break;
14376 }
14377 return node;
14378}
14379
14380static NODE *
14381arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14382{
14383 if (node2) {
14384 if (!node1) return (NODE *)node2;
14385 node2->nd_head = node1;
14386 nd_set_first_lineno(node2, nd_first_lineno(node1));
14387 nd_set_first_column(node2, nd_first_column(node1));
14388 return (NODE *)node2;
14389 }
14390 return node1;
14391}
14392
14393static bool
14394args_info_empty_p(struct rb_args_info *args)
14395{
14396 if (args->pre_args_num) return false;
14397 if (args->post_args_num) return false;
14398 if (args->rest_arg) return false;
14399 if (args->opt_args) return false;
14400 if (args->block_arg) return false;
14401 if (args->kw_args) return false;
14402 if (args->kw_rest_arg) return false;
14403 return true;
14404}
14405
14406static rb_node_args_t *
14407new_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)
14408{
14409 struct rb_args_info *args = &tail->nd_ainfo;
14410
14411 if (args->forwarding) {
14412 if (rest_arg) {
14413 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14414 return tail;
14415 }
14416 rest_arg = idFWD_REST;
14417 }
14418
14419 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14420 args->pre_init = pre_args ? pre_args->nd_next : 0;
14421
14422 args->post_args_num = post_args ? post_args->nd_plen : 0;
14423 args->post_init = post_args ? post_args->nd_next : 0;
14424 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14425
14426 args->rest_arg = rest_arg;
14427
14428 args->opt_args = opt_args;
14429
14430 nd_set_loc(RNODE(tail), loc);
14431
14432 return tail;
14433}
14434
14435static rb_node_args_t *
14436new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14437{
14438 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14439 struct rb_args_info *args = &node->nd_ainfo;
14440 if (p->error_p) return node;
14441
14442 if (block == idNil) {
14443 block = 0;
14444 args->no_blockarg = TRUE;
14445 }
14446 args->block_arg = block;
14447 args->kw_args = kw_args;
14448
14449 if (kw_args) {
14450 /*
14451 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14452 * variable order: k1, kr1, k2, &b, internal_id, krest
14453 * #=> <reorder>
14454 * variable order: kr1, k1, k2, internal_id, krest, &b
14455 */
14456 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14457 struct vtable *vtargs = p->lvtbl->args;
14458 rb_node_kw_arg_t *kwn = kw_args;
14459
14460 if (block) block = vtargs->tbl[vtargs->pos-1];
14461 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14462 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14463 while (kwn) {
14464 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14465 --kw_vars;
14466 --required_kw_vars;
14467 kwn = kwn->nd_next;
14468 }
14469
14470 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14471 ID vid = get_nd_vid(p, kwn->nd_body);
14472 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14473 *required_kw_vars++ = vid;
14474 }
14475 else {
14476 *kw_vars++ = vid;
14477 }
14478 }
14479
14480 arg_var(p, kw_bits);
14481 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14482 if (block) arg_var(p, block);
14483
14484 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14485 }
14486 else if (kw_rest_arg == idNil) {
14487 args->no_kwarg = 1;
14488 }
14489 else if (kw_rest_arg) {
14490 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14491 }
14492
14493 return node;
14494}
14495
14496static rb_node_args_t *
14497args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14498{
14499 if (max_numparam > NO_PARAM || it_id) {
14500 if (!args) {
14501 YYLTYPE loc = RUBY_INIT_YYLLOC();
14502 args = new_empty_args_tail(p, 0);
14503 nd_set_loc(RNODE(args), &loc);
14504 }
14505 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14506 }
14507 return args;
14508}
14509
14510static NODE*
14511new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14512{
14513 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14514
14515 if (pre_arg) {
14516 NODE *pre_args = NEW_LIST(pre_arg, loc);
14517 if (RNODE_ARYPTN(aryptn)->pre_args) {
14518 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14519 }
14520 else {
14521 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14522 }
14523 }
14524 return aryptn;
14525}
14526
14527static NODE*
14528new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14529{
14530 if (has_rest) {
14531 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14532 }
14533 else {
14534 rest_arg = NULL;
14535 }
14536 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14537
14538 return node;
14539}
14540
14541static NODE*
14542new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14543{
14544 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14545
14546 return fndptn;
14547}
14548
14549static NODE*
14550new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14551{
14552 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14553 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14554 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14555
14556 return node;
14557}
14558
14559static NODE*
14560new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14561{
14562 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14563 return hshptn;
14564}
14565
14566static NODE*
14567new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14568{
14569 NODE *node, *kw_rest_arg_node;
14570
14571 if (kw_rest_arg == idNil) {
14572 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14573 }
14574 else if (kw_rest_arg) {
14575 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14576 }
14577 else {
14578 kw_rest_arg_node = NULL;
14579 }
14580
14581 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14582
14583 return node;
14584}
14585
14586static NODE*
14587dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14588{
14589 if (!node) {
14590 return NEW_SYM(STR_NEW0(), loc);
14591 }
14592
14593 switch (nd_type(node)) {
14594 case NODE_DSTR:
14595 nd_set_type(node, NODE_DSYM);
14596 nd_set_loc(node, loc);
14597 break;
14598 case NODE_STR:
14599 node = str_to_sym_node(p, node, loc);
14600 break;
14601 default:
14602 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14603 break;
14604 }
14605 return node;
14606}
14607
14608static int
14609nd_type_st_key_enable_p(NODE *node)
14610{
14611 switch (nd_type(node)) {
14612 case NODE_INTEGER:
14613 case NODE_FLOAT:
14614 case NODE_RATIONAL:
14615 case NODE_IMAGINARY:
14616 case NODE_STR:
14617 case NODE_SYM:
14618 case NODE_REGX:
14619 case NODE_LINE:
14620 case NODE_FILE:
14621 case NODE_ENCODING:
14622 return true;
14623 default:
14624 return false;
14625 }
14626}
14627
14628static VALUE
14629nd_value(struct parser_params *p, NODE *node)
14630{
14631 switch (nd_type(node)) {
14632 case NODE_STR:
14633 return rb_node_str_string_val(node);
14634 case NODE_INTEGER:
14635 return rb_node_integer_literal_val(node);
14636 case NODE_FLOAT:
14637 return rb_node_float_literal_val(node);
14638 case NODE_RATIONAL:
14639 return rb_node_rational_literal_val(node);
14640 case NODE_IMAGINARY:
14641 return rb_node_imaginary_literal_val(node);
14642 case NODE_SYM:
14643 return rb_node_sym_string_val(node);
14644 case NODE_REGX:
14645 return rb_node_regx_string_val(node);
14646 case NODE_LINE:
14647 return rb_node_line_lineno_val(node);
14648 case NODE_ENCODING:
14649 return rb_node_encoding_val(node);
14650 case NODE_FILE:
14651 return rb_node_file_path_val(node);
14652 default:
14653 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14654 UNREACHABLE_RETURN(0);
14655 }
14656}
14657
14658static void
14659warn_duplicate_keys(struct parser_params *p, NODE *hash)
14660{
14661 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14662 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14663 while (hash && RNODE_LIST(hash)->nd_next) {
14664 NODE *head = RNODE_LIST(hash)->nd_head;
14665 NODE *value = RNODE_LIST(hash)->nd_next;
14666 NODE *next = RNODE_LIST(value)->nd_next;
14667 st_data_t key;
14668 st_data_t data;
14669
14670 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14671 if (!head) {
14672 head = value;
14673 }
14674
14675 if (nd_type_st_key_enable_p(head)) {
14676 key = (st_data_t)head;
14677
14678 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14679 rb_warn2L(nd_line((NODE *)data),
14680 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14681 nd_value(p, head), WARN_I(nd_line(head)));
14682 }
14683 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14684 }
14685 hash = next;
14686 }
14687 st_free_table(p->warn_duplicate_keys_table);
14688 p->warn_duplicate_keys_table = NULL;
14689}
14690
14691static NODE *
14692new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14693{
14694 if (hash) warn_duplicate_keys(p, hash);
14695 return NEW_HASH(hash, loc);
14696}
14697
14698static void
14699error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14700{
14701 if (is_private_local_id(p, id)) {
14702 return;
14703 }
14704 if (st_is_member(p->pvtbl, id)) {
14705 yyerror1(loc, "duplicated variable name");
14706 }
14707 else if (p->ctxt.in_alt_pattern && id) {
14708 yyerror1(loc, "variable capture in alternative pattern");
14709 }
14710 else {
14711 p->ctxt.capture_in_pattern = 1;
14712 st_insert(p->pvtbl, (st_data_t)id, 0);
14713 }
14714}
14715
14716static void
14717error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14718{
14719 if (!p->pktbl) {
14720 p->pktbl = st_init_numtable();
14721 }
14722 else if (st_is_member(p->pktbl, key)) {
14723 yyerror1(loc, "duplicated key name");
14724 return;
14725 }
14726 st_insert(p->pktbl, (st_data_t)key, 0);
14727}
14728
14729static NODE *
14730new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14731{
14732 return NEW_HASH(hash, loc);
14733}
14734
14735static NODE *
14736new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14737{
14738 NODE *asgn;
14739
14740 if (lhs) {
14741 ID vid = get_nd_vid(p, lhs);
14742 YYLTYPE lhs_loc = lhs->nd_loc;
14743 if (op == tOROP) {
14744 set_nd_value(p, lhs, rhs);
14745 nd_set_loc(lhs, loc);
14746 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14747 }
14748 else if (op == tANDOP) {
14749 set_nd_value(p, lhs, rhs);
14750 nd_set_loc(lhs, loc);
14751 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14752 }
14753 else {
14754 asgn = lhs;
14755 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14756 set_nd_value(p, asgn, rhs);
14757 nd_set_loc(asgn, loc);
14758 }
14759 }
14760 else {
14761 asgn = NEW_ERROR(loc);
14762 }
14763 return asgn;
14764}
14765
14766static NODE *
14767new_ary_op_assign(struct parser_params *p, NODE *ary,
14768 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14769 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14770{
14771 NODE *asgn;
14772
14773 aryset_check(p, args);
14774 args = make_list(args, args_loc);
14775 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14776 fixpos(asgn, ary);
14777 return asgn;
14778}
14779
14780static NODE *
14781new_attr_op_assign(struct parser_params *p, NODE *lhs,
14782 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14783 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14784{
14785 NODE *asgn;
14786
14787 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14788 fixpos(asgn, lhs);
14789 return asgn;
14790}
14791
14792static NODE *
14793new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14794{
14795 NODE *asgn;
14796
14797 if (lhs) {
14798 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14799 }
14800 else {
14801 asgn = NEW_ERROR(loc);
14802 }
14803 fixpos(asgn, lhs);
14804 return asgn;
14805}
14806
14807static NODE *
14808const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14809{
14810 if (p->ctxt.in_def) {
14811#ifndef RIPPER
14812 yyerror1(loc, "dynamic constant assignment");
14813#else
14814 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14815#endif
14816 }
14817 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14818}
14819
14820#ifdef RIPPER
14821static VALUE
14822assign_error(struct parser_params *p, const char *mesg, VALUE a)
14823{
14824 a = dispatch2(assign_error, ERR_MESG(), a);
14825 ripper_error(p);
14826 return a;
14827}
14828#endif
14829
14830static NODE *
14831new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14832{
14833 NODE *result = head;
14834 if (rescue) {
14835 NODE *tmp = rescue_else ? rescue_else : rescue;
14836 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14837
14838 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14839 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14840 }
14841 if (ensure) {
14842 result = NEW_ENSURE(result, ensure, loc);
14843 }
14844 fixpos(result, head);
14845 return result;
14846}
14847
14848static void
14849warn_unused_var(struct parser_params *p, struct local_vars *local)
14850{
14851 int cnt;
14852
14853 if (!local->used) return;
14854 cnt = local->used->pos;
14855 if (cnt != local->vars->pos) {
14856 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14857 }
14858#ifndef RIPPER
14859 ID *v = local->vars->tbl;
14860 ID *u = local->used->tbl;
14861 for (int i = 0; i < cnt; ++i) {
14862 if (!v[i] || (u[i] & LVAR_USED)) continue;
14863 if (is_private_local_id(p, v[i])) continue;
14864 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14865 }
14866#endif
14867}
14868
14869static void
14870local_push(struct parser_params *p, int toplevel_scope)
14871{
14872 struct local_vars *local;
14873 int inherits_dvars = toplevel_scope && compile_for_eval;
14874 int warn_unused_vars = RTEST(ruby_verbose);
14875
14876 local = ALLOC(struct local_vars);
14877 local->prev = p->lvtbl;
14878 local->args = vtable_alloc(0);
14879 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14880#ifndef RIPPER
14881 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14882 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14883#endif
14884 local->numparam.outer = 0;
14885 local->numparam.inner = 0;
14886 local->numparam.current = 0;
14887 local->it = 0;
14888 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14889
14890# if WARN_PAST_SCOPE
14891 local->past = 0;
14892# endif
14893 CMDARG_PUSH(0);
14894 COND_PUSH(0);
14895 p->lvtbl = local;
14896}
14897
14898static void
14899vtable_chain_free(struct parser_params *p, struct vtable *table)
14900{
14901 while (!DVARS_TERMINAL_P(table)) {
14902 struct vtable *cur_table = table;
14903 table = cur_table->prev;
14904 vtable_free(cur_table);
14905 }
14906}
14907
14908static void
14909local_free(struct parser_params *p, struct local_vars *local)
14910{
14911 vtable_chain_free(p, local->used);
14912
14913# if WARN_PAST_SCOPE
14914 vtable_chain_free(p, local->past);
14915# endif
14916
14917 vtable_chain_free(p, local->args);
14918 vtable_chain_free(p, local->vars);
14919
14920 ruby_sized_xfree(local, sizeof(struct local_vars));
14921}
14922
14923static void
14924local_pop(struct parser_params *p)
14925{
14926 struct local_vars *local = p->lvtbl->prev;
14927 if (p->lvtbl->used) {
14928 warn_unused_var(p, p->lvtbl);
14929 }
14930
14931 local_free(p, p->lvtbl);
14932 p->lvtbl = local;
14933
14934 CMDARG_POP();
14935 COND_POP();
14936}
14937
14938static rb_ast_id_table_t *
14939local_tbl(struct parser_params *p)
14940{
14941 int cnt_args = vtable_size(p->lvtbl->args);
14942 int cnt_vars = vtable_size(p->lvtbl->vars);
14943 int cnt = cnt_args + cnt_vars;
14944 int i, j;
14945 rb_ast_id_table_t *tbl;
14946
14947 if (cnt <= 0) return 0;
14948 tbl = rb_ast_new_local_table(p->ast, cnt);
14949 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14950 /* remove IDs duplicated to warn shadowing */
14951 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14952 ID id = p->lvtbl->vars->tbl[i];
14953 if (!vtable_included(p->lvtbl->args, id)) {
14954 tbl->ids[j++] = id;
14955 }
14956 }
14957 if (j < cnt) {
14958 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14959 }
14960
14961 return tbl;
14962}
14963
14964static void
14965numparam_name(struct parser_params *p, ID id)
14966{
14967 if (!NUMPARAM_ID_P(id)) return;
14968 compile_error(p, "_%d is reserved for numbered parameter",
14969 NUMPARAM_ID_TO_IDX(id));
14970}
14971
14972static void
14973arg_var(struct parser_params *p, ID id)
14974{
14975 numparam_name(p, id);
14976 vtable_add(p->lvtbl->args, id);
14977}
14978
14979static void
14980local_var(struct parser_params *p, ID id)
14981{
14982 numparam_name(p, id);
14983 vtable_add(p->lvtbl->vars, id);
14984 if (p->lvtbl->used) {
14985 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14986 }
14987}
14988
14989#ifndef RIPPER
14990int
14991rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
14992{
14993 return rb_local_defined(id, iseq);
14994}
14995#endif
14996
14997static int
14998local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14999{
15000 struct vtable *vars, *args, *used;
15001
15002 vars = p->lvtbl->vars;
15003 args = p->lvtbl->args;
15004 used = p->lvtbl->used;
15005
15006 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15007 vars = vars->prev;
15008 args = args->prev;
15009 if (used) used = used->prev;
15010 }
15011
15012 if (vars && vars->prev == DVARS_INHERIT) {
15013 return rb_parser_local_defined(p, id, p->parent_iseq);
15014 }
15015 else if (vtable_included(args, id)) {
15016 return 1;
15017 }
15018 else {
15019 int i = vtable_included(vars, id);
15020 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15021 return i != 0;
15022 }
15023}
15024
15025static int
15026local_id(struct parser_params *p, ID id)
15027{
15028 return local_id_ref(p, id, NULL);
15029}
15030
15031static int
15032check_forwarding_args(struct parser_params *p)
15033{
15034 if (local_id(p, idFWD_ALL)) return TRUE;
15035 compile_error(p, "unexpected ...");
15036 return FALSE;
15037}
15038
15039static void
15040add_forwarding_args(struct parser_params *p)
15041{
15042 arg_var(p, idFWD_REST);
15043 arg_var(p, idFWD_KWREST);
15044 arg_var(p, idFWD_BLOCK);
15045 arg_var(p, idFWD_ALL);
15046}
15047
15048static void
15049forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15050{
15051 bool conflict = false;
15052
15053 struct vtable *vars, *args;
15054
15055 vars = p->lvtbl->vars;
15056 args = p->lvtbl->args;
15057
15058 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15059 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15060 vars = vars->prev;
15061 args = args->prev;
15062 }
15063
15064 bool found = false;
15065 if (vars && vars->prev == DVARS_INHERIT && !found) {
15066 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15067 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15068 }
15069 else {
15070 found = (vtable_included(args, arg) &&
15071 !(all && vtable_included(args, all)));
15072 }
15073
15074 if (!found) {
15075 compile_error(p, "no anonymous %s parameter", var);
15076 }
15077 else if (conflict) {
15078 compile_error(p, "anonymous %s parameter is also used within block", var);
15079 }
15080}
15081
15082static NODE *
15083new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15084{
15085 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15086 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15087 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15088 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15089 block->forwarding = TRUE;
15090 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15091 return arg_blk_pass(args, block);
15092}
15093
15094static NODE *
15095numparam_push(struct parser_params *p)
15096{
15097 struct local_vars *local = p->lvtbl;
15098 NODE *inner = local->numparam.inner;
15099 if (!local->numparam.outer) {
15100 local->numparam.outer = local->numparam.current;
15101 }
15102 local->numparam.inner = 0;
15103 local->numparam.current = 0;
15104 local->it = 0;
15105 return inner;
15106}
15107
15108static void
15109numparam_pop(struct parser_params *p, NODE *prev_inner)
15110{
15111 struct local_vars *local = p->lvtbl;
15112 if (prev_inner) {
15113 /* prefer first one */
15114 local->numparam.inner = prev_inner;
15115 }
15116 else if (local->numparam.current) {
15117 /* current and inner are exclusive */
15118 local->numparam.inner = local->numparam.current;
15119 }
15120 if (p->max_numparam > NO_PARAM) {
15121 /* current and outer are exclusive */
15122 local->numparam.current = local->numparam.outer;
15123 local->numparam.outer = 0;
15124 }
15125 else {
15126 /* no numbered parameter */
15127 local->numparam.current = 0;
15128 }
15129 local->it = 0;
15130}
15131
15132static const struct vtable *
15133dyna_push(struct parser_params *p)
15134{
15135 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15136 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15137 if (p->lvtbl->used) {
15138 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15139 }
15140 return p->lvtbl->args;
15141}
15142
15143static void
15144dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15145{
15146 struct vtable *tmp = *vtblp;
15147 *vtblp = tmp->prev;
15148# if WARN_PAST_SCOPE
15149 if (p->past_scope_enabled) {
15150 tmp->prev = p->lvtbl->past;
15151 p->lvtbl->past = tmp;
15152 return;
15153 }
15154# endif
15155 vtable_free(tmp);
15156}
15157
15158static void
15159dyna_pop_1(struct parser_params *p)
15160{
15161 struct vtable *tmp;
15162
15163 if ((tmp = p->lvtbl->used) != 0) {
15164 warn_unused_var(p, p->lvtbl);
15165 p->lvtbl->used = p->lvtbl->used->prev;
15166 vtable_free(tmp);
15167 }
15168 dyna_pop_vtable(p, &p->lvtbl->args);
15169 dyna_pop_vtable(p, &p->lvtbl->vars);
15170}
15171
15172static void
15173dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15174{
15175 while (p->lvtbl->args != lvargs) {
15176 dyna_pop_1(p);
15177 if (!p->lvtbl->args) {
15178 struct local_vars *local = p->lvtbl->prev;
15179 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15180 p->lvtbl = local;
15181 }
15182 }
15183 dyna_pop_1(p);
15184}
15185
15186static int
15187dyna_in_block(struct parser_params *p)
15188{
15189 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15190}
15191
15192#ifndef RIPPER
15193int
15194dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15195{
15196 struct vtable *vars, *args, *used;
15197 int i;
15198
15199 args = p->lvtbl->args;
15200 vars = p->lvtbl->vars;
15201 used = p->lvtbl->used;
15202
15203 while (!DVARS_TERMINAL_P(vars)) {
15204 if (vtable_included(args, id)) {
15205 return 1;
15206 }
15207 if ((i = vtable_included(vars, id)) != 0) {
15208 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15209 return 1;
15210 }
15211 args = args->prev;
15212 vars = vars->prev;
15213 if (!vidrefp) used = 0;
15214 if (used) used = used->prev;
15215 }
15216
15217 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15218 return rb_dvar_defined(id, p->parent_iseq);
15219 }
15220
15221 return 0;
15222}
15223#endif
15224
15225static int
15226dvar_defined(struct parser_params *p, ID id)
15227{
15228 return dvar_defined_ref(p, id, NULL);
15229}
15230
15231static int
15232dvar_curr(struct parser_params *p, ID id)
15233{
15234 return (vtable_included(p->lvtbl->args, id) ||
15235 vtable_included(p->lvtbl->vars, id));
15236}
15237
15238static void
15239reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15240{
15241 compile_error(p,
15242 "regexp encoding option '%c' differs from source encoding '%s'",
15243 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15244}
15245
15246#ifndef RIPPER
15247static rb_encoding *
15248find_enc(struct parser_params* p, const char *name)
15249{
15250 int idx = rb_enc_find_index(name);
15251 if (idx < 0) {
15252 rb_bug("unknown encoding name: %s", name);
15253 }
15254
15255 return rb_enc_from_index(idx);
15256}
15257
15258static rb_encoding *
15259kcode_to_enc(struct parser_params* p, int kcode)
15260{
15261 rb_encoding *enc;
15262
15263 switch (kcode) {
15264 case ENC_ASCII8BIT:
15265 enc = rb_ascii8bit_encoding();
15266 break;
15267 case ENC_EUC_JP:
15268 enc = find_enc(p, "EUC-JP");
15269 break;
15270 case ENC_Windows_31J:
15271 enc = find_enc(p, "Windows-31J");
15272 break;
15273 case ENC_UTF8:
15274 enc = rb_utf8_encoding();
15275 break;
15276 default:
15277 enc = NULL;
15278 break;
15279 }
15280
15281 return enc;
15282}
15283
15284int
15285rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15286{
15287 int c = RE_OPTION_ENCODING_IDX(options);
15288
15289 if (c) {
15290 int opt, idx;
15291 rb_encoding *enc;
15292
15293 char_to_option_kcode(c, &opt, &idx);
15294 enc = kcode_to_enc(p, idx);
15295 if (enc != rb_parser_str_get_encoding(str) &&
15296 !rb_parser_is_ascii_string(p, str)) {
15297 goto error;
15298 }
15299 rb_parser_string_set_encoding(str, enc);
15300 }
15301 else if (RE_OPTION_ENCODING_NONE(options)) {
15302 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15303 !rb_parser_is_ascii_string(p, str)) {
15304 c = 'n';
15305 goto error;
15306 }
15307 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15308 }
15309 else if (rb_is_usascii_enc(p->enc)) {
15310 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15311 }
15312 return 0;
15313
15314 error:
15315 return c;
15316}
15317#endif
15318
15319static void
15320reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15321{
15322 int c = rb_reg_fragment_setenc(p, str, options);
15323 if (c) reg_fragment_enc_error(p, str, c);
15324}
15325
15326#ifndef UNIVERSAL_PARSER
15327typedef struct {
15328 struct parser_params* parser;
15329 rb_encoding *enc;
15330 NODE *succ_block;
15331 const YYLTYPE *loc;
15332 rb_parser_assignable_func assignable;
15333} reg_named_capture_assign_t;
15334
15335static int
15336reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15337 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15338{
15339 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15340 struct parser_params* p = arg->parser;
15341 rb_encoding *enc = arg->enc;
15342 long len = name_end - name;
15343 const char *s = (const char *)name;
15344
15345 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15346}
15347
15348static NODE *
15349reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15350{
15351 reg_named_capture_assign_t arg;
15352
15353 arg.parser = p;
15354 arg.enc = rb_enc_get(regexp);
15355 arg.succ_block = 0;
15356 arg.loc = loc;
15357 arg.assignable = assignable;
15358 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15359
15360 if (!arg.succ_block) return 0;
15361 return RNODE_BLOCK(arg.succ_block)->nd_next;
15362}
15363#endif
15364
15365#ifndef RIPPER
15366NODE *
15367rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15368{
15369 return assignable(p, id, val, loc);
15370}
15371
15372int
15373rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15374 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15375{
15376 ID var;
15377 NODE *node, *succ;
15378
15379 if (!len) return ST_CONTINUE;
15380 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15381 return ST_CONTINUE;
15382
15383 var = intern_cstr(s, len, enc);
15384 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15385 if (!lvar_defined(p, var)) return ST_CONTINUE;
15386 }
15387 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15388 succ = *succ_block;
15389 if (!succ) succ = NEW_ERROR(loc);
15390 succ = block_append(p, succ, node);
15391 *succ_block = succ;
15392 return ST_CONTINUE;
15393}
15394#endif
15395
15396static VALUE
15397parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15398{
15399 VALUE str2;
15400 reg_fragment_setenc(p, str, options);
15401 str2 = rb_str_new_parser_string(str);
15402 return rb_parser_reg_compile(p, str2, options);
15403}
15404
15405#ifndef RIPPER
15406VALUE
15407rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15408{
15409 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15410}
15411#endif
15412
15413static VALUE
15414reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15415{
15416 VALUE re;
15417 VALUE err;
15418
15419 err = rb_errinfo();
15420 re = parser_reg_compile(p, str, options);
15421 if (NIL_P(re)) {
15422 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15423 rb_set_errinfo(err);
15424 compile_error(p, "%"PRIsVALUE, m);
15425 return Qnil;
15426 }
15427 return re;
15428}
15429
15430#ifndef RIPPER
15431void
15432rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15433{
15434 p->do_print = print;
15435 p->do_loop = loop;
15436 p->do_chomp = chomp;
15437 p->do_split = split;
15438}
15439
15440static NODE *
15441parser_append_options(struct parser_params *p, NODE *node)
15442{
15443 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15444 const YYLTYPE *const LOC = &default_location;
15445
15446 if (p->do_print) {
15447 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15448 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15449 LOC);
15450 node = block_append(p, node, print);
15451 }
15452
15453 if (p->do_loop) {
15454 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15455
15456 if (p->do_split) {
15457 ID ifs = rb_intern("$;");
15458 ID fields = rb_intern("$F");
15459 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15460 NODE *split = NEW_GASGN(fields,
15461 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15462 rb_intern("split"), args, LOC),
15463 LOC);
15464 node = block_append(p, split, node);
15465 }
15466 if (p->do_chomp) {
15467 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15468 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15469 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15470 }
15471
15472 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15473 }
15474
15475 return node;
15476}
15477
15478void
15479rb_init_parse(void)
15480{
15481 /* just to suppress unused-function warnings */
15482 (void)nodetype;
15483 (void)nodeline;
15484}
15485
15486ID
15487internal_id(struct parser_params *p)
15488{
15489 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15490}
15491#endif /* !RIPPER */
15492
15493static void
15494parser_initialize(struct parser_params *p)
15495{
15496 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15497 p->command_start = TRUE;
15498 p->ruby_sourcefile_string = Qnil;
15499 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15500 string_buffer_init(p);
15501 p->node_id = 0;
15502 p->delayed.token = NULL;
15503 p->frozen_string_literal = -1; /* not specified */
15504#ifndef RIPPER
15505 p->error_buffer = Qfalse;
15506 p->end_expect_token_locations = NULL;
15507 p->token_id = 0;
15508 p->tokens = NULL;
15509#else
15510 p->result = Qnil;
15511 p->parsing_thread = Qnil;
15512 p->s_value = Qnil;
15513 p->s_lvalue = Qnil;
15514 p->s_value_stack = rb_ary_new();
15515#endif
15516 p->debug_buffer = Qnil;
15517 p->debug_output = rb_ractor_stdout();
15518 p->enc = rb_utf8_encoding();
15519 p->exits = 0;
15520}
15521
15522#ifdef RIPPER
15523#define rb_ruby_parser_mark ripper_parser_mark
15524#define rb_ruby_parser_free ripper_parser_free
15525#define rb_ruby_parser_memsize ripper_parser_memsize
15526#endif
15527
15528void
15529rb_ruby_parser_mark(void *ptr)
15530{
15531 struct parser_params *p = (struct parser_params*)ptr;
15532
15533 rb_gc_mark(p->ruby_sourcefile_string);
15534#ifndef RIPPER
15535 rb_gc_mark(p->error_buffer);
15536#else
15537 rb_gc_mark(p->value);
15538 rb_gc_mark(p->result);
15539 rb_gc_mark(p->parsing_thread);
15540 rb_gc_mark(p->s_value);
15541 rb_gc_mark(p->s_lvalue);
15542 rb_gc_mark(p->s_value_stack);
15543#endif
15544 rb_gc_mark(p->debug_buffer);
15545 rb_gc_mark(p->debug_output);
15546}
15547
15548void
15549rb_ruby_parser_free(void *ptr)
15550{
15551 struct parser_params *p = (struct parser_params*)ptr;
15552 struct local_vars *local, *prev;
15553
15554 if (p->ast) {
15555 rb_ast_free(p->ast);
15556 }
15557
15558 if (p->warn_duplicate_keys_table) {
15559 st_free_table(p->warn_duplicate_keys_table);
15560 }
15561
15562#ifndef RIPPER
15563 if (p->tokens) {
15564 rb_parser_ary_free(p, p->tokens);
15565 }
15566#endif
15567
15568 if (p->tokenbuf) {
15569 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15570 }
15571
15572 for (local = p->lvtbl; local; local = prev) {
15573 prev = local->prev;
15574 local_free(p, local);
15575 }
15576
15577 {
15578 token_info *ptinfo;
15579 while ((ptinfo = p->token_info) != 0) {
15580 p->token_info = ptinfo->next;
15581 xfree(ptinfo);
15582 }
15583 }
15584 string_buffer_free(p);
15585
15586 if (p->pvtbl) {
15587 st_free_table(p->pvtbl);
15588 }
15589
15590 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15591 st_free_table(p->case_labels);
15592 }
15593
15594 xfree(p->lex.strterm);
15595 p->lex.strterm = 0;
15596
15597 xfree(ptr);
15598}
15599
15600size_t
15601rb_ruby_parser_memsize(const void *ptr)
15602{
15603 struct parser_params *p = (struct parser_params*)ptr;
15604 struct local_vars *local;
15605 size_t size = sizeof(*p);
15606
15607 size += p->toksiz;
15608 for (local = p->lvtbl; local; local = local->prev) {
15609 size += sizeof(*local);
15610 if (local->vars) size += local->vars->capa * sizeof(ID);
15611 }
15612 return size;
15613}
15614
15615#ifndef RIPPER
15616#undef rb_reserved_word
15617
15618const struct kwtable *
15619rb_reserved_word(const char *str, unsigned int len)
15620{
15621 return reserved_word(str, len);
15622}
15623
15624#ifdef UNIVERSAL_PARSER
15625rb_parser_t *
15626rb_ruby_parser_allocate(const rb_parser_config_t *config)
15627{
15628 /* parser_initialize expects fields to be set to 0 */
15629 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15630 p->config = config;
15631 return p;
15632}
15633
15634rb_parser_t *
15635rb_ruby_parser_new(const rb_parser_config_t *config)
15636{
15637 /* parser_initialize expects fields to be set to 0 */
15638 rb_parser_t *p = rb_ruby_parser_allocate(config);
15639 parser_initialize(p);
15640 return p;
15641}
15642#else
15643rb_parser_t *
15644rb_ruby_parser_allocate(void)
15645{
15646 /* parser_initialize expects fields to be set to 0 */
15647 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15648 return p;
15649}
15650
15651rb_parser_t *
15652rb_ruby_parser_new(void)
15653{
15654 /* parser_initialize expects fields to be set to 0 */
15655 rb_parser_t *p = rb_ruby_parser_allocate();
15656 parser_initialize(p);
15657 return p;
15658}
15659#endif
15660
15661rb_parser_t *
15662rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15663{
15664 p->error_buffer = main ? Qfalse : Qnil;
15665 p->parent_iseq = base;
15666 return p;
15667}
15668
15669void
15670rb_ruby_parser_set_script_lines(rb_parser_t *p)
15671{
15672 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15673}
15674
15675void
15676rb_ruby_parser_error_tolerant(rb_parser_t *p)
15677{
15678 p->error_tolerant = 1;
15679}
15680
15681void
15682rb_ruby_parser_keep_tokens(rb_parser_t *p)
15683{
15684 p->keep_tokens = 1;
15685 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15686}
15687
15688rb_encoding *
15689rb_ruby_parser_encoding(rb_parser_t *p)
15690{
15691 return p->enc;
15692}
15693
15694int
15695rb_ruby_parser_end_seen_p(rb_parser_t *p)
15696{
15697 return p->ruby__end__seen;
15698}
15699
15700int
15701rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15702{
15703 p->debug = flag;
15704 return flag;
15705}
15706#endif /* !RIPPER */
15707
15708#ifdef RIPPER
15709int
15710rb_ruby_parser_get_yydebug(rb_parser_t *p)
15711{
15712 return p->debug;
15713}
15714
15715void
15716rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15717{
15718 p->value = value;
15719}
15720
15721int
15722rb_ruby_parser_error_p(rb_parser_t *p)
15723{
15724 return p->error_p;
15725}
15726
15727VALUE
15728rb_ruby_parser_debug_output(rb_parser_t *p)
15729{
15730 return p->debug_output;
15731}
15732
15733void
15734rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15735{
15736 p->debug_output = output;
15737}
15738
15739VALUE
15740rb_ruby_parser_parsing_thread(rb_parser_t *p)
15741{
15742 return p->parsing_thread;
15743}
15744
15745void
15746rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15747{
15748 p->parsing_thread = parsing_thread;
15749}
15750
15751void
15752rb_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)
15753{
15754 p->lex.gets = gets;
15755 p->lex.input = input;
15756 p->eofp = 0;
15757 p->ruby_sourcefile_string = sourcefile_string;
15758 p->ruby_sourcefile = sourcefile;
15759 p->ruby_sourceline = sourceline;
15760}
15761
15762VALUE
15763rb_ruby_parser_result(rb_parser_t *p)
15764{
15765 return p->result;
15766}
15767
15768rb_encoding *
15769rb_ruby_parser_enc(rb_parser_t *p)
15770{
15771 return p->enc;
15772}
15773
15774VALUE
15775rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15776{
15777 return p->ruby_sourcefile_string;
15778}
15779
15780int
15781rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15782{
15783 return p->ruby_sourceline;
15784}
15785
15786int
15787rb_ruby_parser_lex_state(rb_parser_t *p)
15788{
15789 return p->lex.state;
15790}
15791
15792void
15793rb_ruby_ripper_parse0(rb_parser_t *p)
15794{
15795 parser_prepare(p);
15796 p->ast = rb_ast_new();
15797 ripper_yyparse((void*)p);
15798 rb_ast_free(p->ast);
15799 p->ast = 0;
15800 p->eval_tree = 0;
15801 p->eval_tree_begin = 0;
15802}
15803
15804int
15805rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15806{
15807 return dedent_string(p, string, width);
15808}
15809
15810int
15811rb_ruby_ripper_initialized_p(rb_parser_t *p)
15812{
15813 return p->lex.input != 0;
15814}
15815
15816void
15817rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15818{
15819 parser_initialize(p);
15820}
15821
15822long
15823rb_ruby_ripper_column(rb_parser_t *p)
15824{
15825 return p->lex.ptok - p->lex.pbeg;
15826}
15827
15828long
15829rb_ruby_ripper_token_len(rb_parser_t *p)
15830{
15831 return p->lex.pcur - p->lex.ptok;
15832}
15833
15834rb_parser_string_t *
15835rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15836{
15837 return p->lex.lastline;
15838}
15839
15840VALUE
15841rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15842{
15843 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15844}
15845
15846#ifdef UNIVERSAL_PARSER
15847rb_parser_t *
15848rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15849{
15850 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15851 p->config = config;
15852 return p;
15853}
15854#endif
15855
15856struct parser_params*
15857rb_ruby_ripper_parser_allocate(void)
15858{
15859 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15860}
15861#endif /* RIPPER */
15862
15863#ifndef RIPPER
15864void
15865rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15866{
15867 va_list ap;
15868 VALUE mesg = p->debug_buffer;
15869
15870 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15871 va_start(ap, fmt);
15872 rb_str_vcatf(mesg, fmt, ap);
15873 va_end(ap);
15874 if (char_at_end(p, mesg, 0) == '\n') {
15875 rb_io_write(p->debug_output, mesg);
15876 p->debug_buffer = Qnil;
15877 }
15878}
15879
15880static void
15881parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15882{
15883 va_list ap;
15884 int lineno, column;
15885
15886 if (loc) {
15887 lineno = loc->end_pos.lineno;
15888 column = loc->end_pos.column;
15889 }
15890 else {
15891 lineno = p->ruby_sourceline;
15892 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15893 }
15894
15895 rb_io_flush(p->debug_output);
15896 p->error_p = 1;
15897 va_start(ap, fmt);
15898 p->error_buffer =
15899 rb_syntax_error_append(p->error_buffer,
15900 p->ruby_sourcefile_string,
15901 lineno, column,
15902 p->enc, fmt, ap);
15903 va_end(ap);
15904}
15905
15906static size_t
15907count_char(const char *str, int c)
15908{
15909 int n = 0;
15910 while (str[n] == c) ++n;
15911 return n;
15912}
15913
15914/*
15915 * strip enclosing double-quotes, same as the default yytnamerr except
15916 * for that single-quotes matching back-quotes do not stop stripping.
15917 *
15918 * "\"`class' keyword\"" => "`class' keyword"
15919 */
15920size_t
15921rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15922{
15923 if (*yystr == '"') {
15924 size_t yyn = 0, bquote = 0;
15925 const char *yyp = yystr;
15926
15927 while (*++yyp) {
15928 switch (*yyp) {
15929 case '\'':
15930 if (!bquote) {
15931 bquote = count_char(yyp+1, '\'') + 1;
15932 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15933 yyn += bquote;
15934 yyp += bquote - 1;
15935 break;
15936 }
15937 else {
15938 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15939 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15940 yyn += bquote;
15941 yyp += bquote - 1;
15942 bquote = 0;
15943 break;
15944 }
15945 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15946 if (yyres) memcpy(yyres + yyn, yyp, 3);
15947 yyn += 3;
15948 yyp += 2;
15949 break;
15950 }
15951 goto do_not_strip_quotes;
15952 }
15953
15954 case ',':
15955 goto do_not_strip_quotes;
15956
15957 case '\\':
15958 if (*++yyp != '\\')
15959 goto do_not_strip_quotes;
15960 /* Fall through. */
15961 default:
15962 if (yyres)
15963 yyres[yyn] = *yyp;
15964 yyn++;
15965 break;
15966
15967 case '"':
15968 case '\0':
15969 if (yyres)
15970 yyres[yyn] = '\0';
15971 return yyn;
15972 }
15973 }
15974 do_not_strip_quotes: ;
15975 }
15976
15977 if (!yyres) return strlen(yystr);
15978
15979 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
15980}
15981#endif
15982
15983#ifdef RIPPER
15984#define validate(x) (void)(x)
15985
15986static VALUE
15987ripper_dispatch0(struct parser_params *p, ID mid)
15988{
15989 return rb_funcall(p->value, mid, 0);
15990}
15991
15992static VALUE
15993ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
15994{
15995 validate(a);
15996 return rb_funcall(p->value, mid, 1, a);
15997}
15998
15999static VALUE
16000ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16001{
16002 validate(a);
16003 validate(b);
16004 return rb_funcall(p->value, mid, 2, a, b);
16005}
16006
16007static VALUE
16008ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16009{
16010 validate(a);
16011 validate(b);
16012 validate(c);
16013 return rb_funcall(p->value, mid, 3, a, b, c);
16014}
16015
16016static VALUE
16017ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16018{
16019 validate(a);
16020 validate(b);
16021 validate(c);
16022 validate(d);
16023 return rb_funcall(p->value, mid, 4, a, b, c, d);
16024}
16025
16026static VALUE
16027ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16028{
16029 validate(a);
16030 validate(b);
16031 validate(c);
16032 validate(d);
16033 validate(e);
16034 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16035}
16036
16037static VALUE
16038ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16039{
16040 validate(a);
16041 validate(b);
16042 validate(c);
16043 validate(d);
16044 validate(e);
16045 validate(f);
16046 validate(g);
16047 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16048}
16049
16050void
16051ripper_error(struct parser_params *p)
16052{
16053 p->error_p = TRUE;
16054}
16055
16056VALUE
16057ripper_value(struct parser_params *p)
16058{
16059 (void)yystpcpy; /* may not used in newer bison */
16060
16061 return p->value;
16062}
16063
16064#endif /* RIPPER */
16065/*
16066 * Local variables:
16067 * mode: c
16068 * c-file-style: "ruby"
16069 * End:
16070 */