Ruby 4.0.0dev (2025-12-08 revision be882278f22444e7d27db091bbd5f8bf63e882c2)
parse.y (be882278f22444e7d27db091bbd5f8bf63e882c2)
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*);
1434static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1435static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1436static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1437static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1438static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1439static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1440
1441static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1442static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1443
1444static NODE* negate_lit(struct parser_params*, NODE*);
1445static void no_blockarg(struct parser_params*,NODE*);
1446static NODE *ret_args(struct parser_params*,NODE*);
1447static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1448static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1449
1450static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1451static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1452
1453static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1454static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1455
1456static VALUE rb_backref_error(struct parser_params*,NODE*);
1457static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1458
1459static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1460static 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);
1461static 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);
1462static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1463static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1464
1465static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1466
1467static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1468static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1469
1470static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1471static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1472
1473static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1474
1475static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1476
1477#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1478
1479static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1480
1481static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1482
1483static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1484
1485static rb_ast_id_table_t *local_tbl(struct parser_params*);
1486
1487static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1488static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1489
1490static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1491static NODE *heredoc_dedent(struct parser_params*,NODE*);
1492
1493static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1494
1495static 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);
1496
1497#ifdef RIPPER
1498#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1499#define set_value(val) (p->s_lvalue = val)
1500static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1501static int id_is_var(struct parser_params *p, ID id);
1502#endif
1503
1504RUBY_SYMBOL_EXPORT_BEGIN
1505VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1506int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1507enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1508VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1509void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1510PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1511YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1512YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1513YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1514YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1515YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1516YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1517void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1518RUBY_SYMBOL_EXPORT_END
1519
1520static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1521static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1522static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1523static VALUE formal_argument_error(struct parser_params*, ID);
1524static ID shadowing_lvar(struct parser_params*,ID);
1525static void new_bv(struct parser_params*,ID);
1526
1527static void local_push(struct parser_params*,int);
1528static void local_pop(struct parser_params*);
1529static void local_var(struct parser_params*, ID);
1530static void arg_var(struct parser_params*, ID);
1531static int local_id(struct parser_params *p, ID id);
1532static int local_id_ref(struct parser_params*, ID, ID **);
1533#define internal_id rb_parser_internal_id
1534ID internal_id(struct parser_params*);
1535static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1536static int check_forwarding_args(struct parser_params*);
1537static void add_forwarding_args(struct parser_params *p);
1538static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1539
1540static const struct vtable *dyna_push(struct parser_params *);
1541static void dyna_pop(struct parser_params*, const struct vtable *);
1542static int dyna_in_block(struct parser_params*);
1543#define dyna_var(p, id) local_var(p, id)
1544static int dvar_defined(struct parser_params*, ID);
1545#define dvar_defined_ref rb_parser_dvar_defined_ref
1546int dvar_defined_ref(struct parser_params*, ID, ID**);
1547static int dvar_curr(struct parser_params*,ID);
1548
1549static int lvar_defined(struct parser_params*, ID);
1550
1551static NODE *numparam_push(struct parser_params *p);
1552static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1553
1554#define METHOD_NOT '!'
1555
1556#define idFWD_REST '*'
1557#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1558#define idFWD_BLOCK '&'
1559#define idFWD_ALL idDot3
1560#define arg_FWD_BLOCK idFWD_BLOCK
1561
1562#define RE_ONIG_OPTION_IGNORECASE 1
1563#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1564#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1565#define RE_OPTION_ONCE (1<<16)
1566#define RE_OPTION_ENCODING_SHIFT 8
1567#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1568#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1569#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1570#define RE_OPTION_MASK 0xff
1571#define RE_OPTION_ARG_ENCODING_NONE 32
1572
1573#define CHECK_LITERAL_WHEN (st_table *)1
1574#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1575
1576#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1577RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1578
1579#define TOKEN2ID(tok) ( \
1580 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1581 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1582 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1583 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1584 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1585 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1586 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1587
1588/****** Ripper *******/
1589
1590#ifdef RIPPER
1591
1592#include "eventids1.h"
1593#include "eventids2.h"
1594
1595extern const struct ripper_parser_ids ripper_parser_ids;
1596
1597static VALUE ripper_dispatch0(struct parser_params*,ID);
1598static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1599static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1600static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1601static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1602static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1603static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1604void ripper_error(struct parser_params *p);
1605
1606#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1607#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1608#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1609#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1610#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1611#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1612#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1613
1614#define yyparse ripper_yyparse
1615
1616static VALUE
1617aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1618{
1619 if (!NIL_P(pre_arg)) {
1620 if (!NIL_P(pre_args)) {
1621 rb_ary_unshift(pre_args, pre_arg);
1622 }
1623 else {
1624 pre_args = rb_ary_new_from_args(1, pre_arg);
1625 }
1626 }
1627 return pre_args;
1628}
1629
1630#define ID2VAL(id) STATIC_ID2SYM(id)
1631#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1632#endif /* RIPPER */
1633
1634#define KWD2EID(t, v) keyword_##t
1635
1636static NODE *
1637new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1638{
1639 body = remove_begin(body);
1640 reduce_nodes(p, &body);
1641 NODE *n = NEW_SCOPE(args, body, parent, loc);
1642 nd_set_line(n, loc->end_pos.lineno);
1643 set_line_body(body, loc->beg_pos.lineno);
1644 return n;
1645}
1646
1647static NODE *
1648rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1649 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1650{
1651 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1652 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1653 loc.beg_pos = arg_loc->beg_pos;
1654 return NEW_RESCUE(arg, rescue, 0, &loc);
1655}
1656
1657static NODE *add_block_exit(struct parser_params *p, NODE *node);
1658static rb_node_exits_t *init_block_exit(struct parser_params *p);
1659static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1660static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1661static void clear_block_exit(struct parser_params *p, bool error);
1662
1663static void
1664next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1665{
1666 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1667}
1668
1669static void
1670restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1671{
1672 /* See: def_name action */
1673 struct lex_context ctxt = temp->save.ctxt;
1674 p->ctxt.in_def = ctxt.in_def;
1675 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1676 p->ctxt.in_rescue = ctxt.in_rescue;
1677 p->max_numparam = temp->save.max_numparam;
1678 numparam_pop(p, temp->save.numparam_save);
1679 clear_block_exit(p, true);
1680}
1681
1682static void
1683endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1684{
1685 if (is_attrset_id(mid)) {
1686 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1687 }
1688 token_info_drop(p, "def", loc->beg_pos);
1689}
1690
1691#define debug_token_line(p, name, line) do { \
1692 if (p->debug) { \
1693 const char *const pcur = p->lex.pcur; \
1694 const char *const ptok = p->lex.ptok; \
1695 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1696 line, p->ruby_sourceline, \
1697 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1698 } \
1699 } while (0)
1700
1701#define begin_definition(k, loc_beg, loc_end) \
1702 do { \
1703 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1704 /* singleton class */ \
1705 p->ctxt.cant_return = !p->ctxt.in_def; \
1706 p->ctxt.in_def = 0; \
1707 } \
1708 else if (p->ctxt.in_def) { \
1709 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1710 yyerror1(&loc, k " definition in method body"); \
1711 } \
1712 else { \
1713 p->ctxt.cant_return = 1; \
1714 } \
1715 local_push(p, 0); \
1716 } while (0)
1717
1718#ifndef RIPPER
1719# define ifndef_ripper(x) (x)
1720# define ifdef_ripper(r,x) (x)
1721#else
1722# define ifndef_ripper(x)
1723# define ifdef_ripper(r,x) (r)
1724#endif
1725
1726# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1727# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1728# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1729# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1730# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1731# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1732# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1733# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1734# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1735# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1736# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1737# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1738# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1739# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1740# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1741# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1742# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1743# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1744# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1745# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1746#ifdef RIPPER
1747extern const ID id_warn, id_warning, id_gets, id_assoc;
1748# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1749# define WARN_S_L(s,l) STR_NEW(s,l)
1750# define WARN_S(s) STR_NEW2(s)
1751# define WARN_I(i) INT2NUM(i)
1752# define WARN_ID(i) rb_id2str(i)
1753# define PRIsWARN PRIsVALUE
1754# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1755# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1756# ifdef HAVE_VA_ARGS_MACRO
1757# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1758# else
1759# define WARN_CALL rb_funcall
1760# endif
1761# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1762# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1763# ifdef HAVE_VA_ARGS_MACRO
1764# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1765# else
1766# define WARNING_CALL rb_funcall
1767# endif
1768# define compile_error ripper_compile_error
1769#else
1770# define WARN_S_L(s,l) s
1771# define WARN_S(s) s
1772# define WARN_I(i) i
1773# define WARN_ID(i) rb_id2name(i)
1774# define PRIsWARN PRIsVALUE
1775# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1776# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1777# define WARN_CALL rb_compile_warn
1778# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1779# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1780# define WARNING_CALL rb_compile_warning
1781PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1782# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1783#endif
1784
1785#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1786
1787static NODE *
1788add_block_exit(struct parser_params *p, NODE *node)
1789{
1790 if (!node) {
1791 compile_error(p, "unexpected null node");
1792 return 0;
1793 }
1794 switch (nd_type(node)) {
1795 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1796 default:
1797 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1798 return node;
1799 }
1800 if (!p->ctxt.in_defined) {
1801 rb_node_exits_t *exits = p->exits;
1802 if (exits) {
1803 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1804 exits->nd_stts = node;
1805 }
1806 }
1807 return node;
1808}
1809
1810static rb_node_exits_t *
1811init_block_exit(struct parser_params *p)
1812{
1813 rb_node_exits_t *old = p->exits;
1814 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1815 exits->nd_chain = 0;
1816 exits->nd_stts = RNODE(exits);
1817 p->exits = exits;
1818 return old;
1819}
1820
1821static rb_node_exits_t *
1822allow_block_exit(struct parser_params *p)
1823{
1824 rb_node_exits_t *exits = p->exits;
1825 p->exits = 0;
1826 return exits;
1827}
1828
1829static void
1830restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1831{
1832 p->exits = exits;
1833}
1834
1835static void
1836clear_block_exit(struct parser_params *p, bool error)
1837{
1838 rb_node_exits_t *exits = p->exits;
1839 if (!exits) return;
1840 if (error) {
1841 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1842 switch (nd_type(e)) {
1843 case NODE_BREAK:
1844 yyerror1(&e->nd_loc, "Invalid break");
1845 break;
1846 case NODE_NEXT:
1847 yyerror1(&e->nd_loc, "Invalid next");
1848 break;
1849 case NODE_REDO:
1850 yyerror1(&e->nd_loc, "Invalid redo");
1851 break;
1852 default:
1853 yyerror1(&e->nd_loc, "unexpected node");
1854 goto end_checks; /* no nd_chain */
1855 }
1856 }
1857 end_checks:;
1858 }
1859 exits->nd_stts = RNODE(exits);
1860 exits->nd_chain = 0;
1861}
1862
1863#define WARN_EOL(tok) \
1864 (looking_at_eol_p(p) ? \
1865 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1866 (void)0)
1867static int looking_at_eol_p(struct parser_params *p);
1868
1869static NODE *
1870get_nd_value(struct parser_params *p, NODE *node)
1871{
1872 switch (nd_type(node)) {
1873 case NODE_GASGN:
1874 return RNODE_GASGN(node)->nd_value;
1875 case NODE_IASGN:
1876 return RNODE_IASGN(node)->nd_value;
1877 case NODE_LASGN:
1878 return RNODE_LASGN(node)->nd_value;
1879 case NODE_DASGN:
1880 return RNODE_DASGN(node)->nd_value;
1881 case NODE_MASGN:
1882 return RNODE_MASGN(node)->nd_value;
1883 case NODE_CVASGN:
1884 return RNODE_CVASGN(node)->nd_value;
1885 case NODE_CDECL:
1886 return RNODE_CDECL(node)->nd_value;
1887 default:
1888 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1889 return 0;
1890 }
1891}
1892
1893static void
1894set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1895{
1896 switch (nd_type(node)) {
1897 case NODE_CDECL:
1898 RNODE_CDECL(node)->nd_value = rhs;
1899 break;
1900 case NODE_GASGN:
1901 RNODE_GASGN(node)->nd_value = rhs;
1902 break;
1903 case NODE_IASGN:
1904 RNODE_IASGN(node)->nd_value = rhs;
1905 break;
1906 case NODE_LASGN:
1907 RNODE_LASGN(node)->nd_value = rhs;
1908 break;
1909 case NODE_DASGN:
1910 RNODE_DASGN(node)->nd_value = rhs;
1911 break;
1912 case NODE_MASGN:
1913 RNODE_MASGN(node)->nd_value = rhs;
1914 break;
1915 case NODE_CVASGN:
1916 RNODE_CVASGN(node)->nd_value = rhs;
1917 break;
1918 default:
1919 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1920 break;
1921 }
1922}
1923
1924static ID
1925get_nd_vid(struct parser_params *p, NODE *node)
1926{
1927 switch (nd_type(node)) {
1928 case NODE_CDECL:
1929 return RNODE_CDECL(node)->nd_vid;
1930 case NODE_GASGN:
1931 return RNODE_GASGN(node)->nd_vid;
1932 case NODE_IASGN:
1933 return RNODE_IASGN(node)->nd_vid;
1934 case NODE_LASGN:
1935 return RNODE_LASGN(node)->nd_vid;
1936 case NODE_DASGN:
1937 return RNODE_DASGN(node)->nd_vid;
1938 case NODE_CVASGN:
1939 return RNODE_CVASGN(node)->nd_vid;
1940 default:
1941 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1942 return 0;
1943 }
1944}
1945
1946static NODE *
1947get_nd_args(struct parser_params *p, NODE *node)
1948{
1949 switch (nd_type(node)) {
1950 case NODE_CALL:
1951 return RNODE_CALL(node)->nd_args;
1952 case NODE_OPCALL:
1953 return RNODE_OPCALL(node)->nd_args;
1954 case NODE_FCALL:
1955 return RNODE_FCALL(node)->nd_args;
1956 case NODE_QCALL:
1957 return RNODE_QCALL(node)->nd_args;
1958 case NODE_SUPER:
1959 return RNODE_SUPER(node)->nd_args;
1960 case NODE_VCALL:
1961 case NODE_ZSUPER:
1962 case NODE_YIELD:
1963 case NODE_RETURN:
1964 case NODE_BREAK:
1965 case NODE_NEXT:
1966 return 0;
1967 default:
1968 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1969 return 0;
1970 }
1971}
1972
1973static st_index_t
1974djb2(const uint8_t *str, size_t len)
1975{
1976 st_index_t hash = 5381;
1977
1978 for (size_t i = 0; i < len; i++) {
1979 hash = ((hash << 5) + hash) + str[i];
1980 }
1981
1982 return hash;
1983}
1984
1985static st_index_t
1986parser_memhash(const void *ptr, long len)
1987{
1988 return djb2(ptr, len);
1989}
1990
1991#define PARSER_STRING_PTR(str) (str->ptr)
1992#define PARSER_STRING_LEN(str) (str->len)
1993#define PARSER_STRING_END(str) (&str->ptr[str->len])
1994#define STRING_SIZE(str) ((size_t)str->len + 1)
1995#define STRING_TERM_LEN(str) (1)
1996#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1997#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1998 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
1999 str->len = total; \
2000} while (0)
2001#define STRING_SET_LEN(str, n) do { \
2002 (str)->len = (n); \
2003} while (0)
2004#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2005 ((ptrvar) = str->ptr, \
2006 (lenvar) = str->len)
2007
2008static inline int
2009parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2010{
2011 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2012}
2013
2014static rb_parser_string_t *
2015rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2016{
2017 rb_parser_string_t *str;
2018
2019 if (len < 0) {
2020 rb_bug("negative string size (or size too big): %ld", len);
2021 }
2022
2023 str = xcalloc(1, sizeof(rb_parser_string_t));
2024 str->ptr = xcalloc(len + 1, sizeof(char));
2025
2026 if (ptr) {
2027 memcpy(PARSER_STRING_PTR(str), ptr, len);
2028 }
2029 STRING_SET_LEN(str, len);
2030 STRING_TERM_FILL(str);
2031 return str;
2032}
2033
2034static rb_parser_string_t *
2035rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2036{
2037 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2038 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2039 str->enc = enc;
2040 return str;
2041}
2042
2043#ifndef RIPPER
2044rb_parser_string_t *
2045rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2046{
2047 /* Type check */
2048 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2049 RB_GC_GUARD(str);
2050 return ret;
2051}
2052
2053void
2054rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2055{
2056 if (!str) return;
2057 xfree(PARSER_STRING_PTR(str));
2058 xfree(str);
2059}
2060#endif
2061
2062static st_index_t
2063rb_parser_str_hash(rb_parser_string_t *str)
2064{
2065 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2066}
2067
2068static st_index_t
2069rb_char_p_hash(const char *c)
2070{
2071 return parser_memhash((const void *)c, strlen(c));
2072}
2073
2074static size_t
2075rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2076{
2077 return PARSER_STRING_LEN(str);
2078}
2079
2080#ifndef RIPPER
2081static char *
2082rb_parser_string_end(rb_parser_string_t *str)
2083{
2084 return &str->ptr[str->len];
2085}
2086#endif
2087
2088static void
2089rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2090{
2091 str->enc = enc;
2092}
2093
2094static rb_encoding *
2095rb_parser_str_get_encoding(rb_parser_string_t *str)
2096{
2097 return str->enc;
2098}
2099
2100#ifndef RIPPER
2101static bool
2102PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2103{
2104 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2105}
2106#endif
2107
2108static int
2109PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2110{
2111 return str->coderange;
2112}
2113
2114static void
2115PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2116{
2117 str->coderange = coderange;
2118}
2119
2120static void
2121PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2122{
2123 rb_parser_string_set_encoding(str, enc);
2124 PARSER_ENC_CODERANGE_SET(str, cr);
2125}
2126
2127static void
2128PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2129{
2130 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2131}
2132
2133static bool
2134PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2135{
2136 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2137}
2138
2139static bool
2140PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2141{
2142 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2143}
2144
2145static const char *
2146rb_parser_search_nonascii(const char *p, const char *e)
2147{
2148 const char *s = p;
2149
2150 for (; s < e; s++) {
2151 if (*s & 0x80) return s;
2152 }
2153
2154 return NULL;
2155}
2156
2157static int
2158rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2159{
2160 const char *e = ptr + len;
2161
2162 if (enc == rb_ascii8bit_encoding()) {
2163 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2164 ptr = rb_parser_search_nonascii(ptr, e);
2165 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2166 }
2167
2168 /* parser string encoding is always asciicompat */
2169 ptr = rb_parser_search_nonascii(ptr, e);
2170 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2171 for (;;) {
2172 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2173 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2174 ptr += MBCLEN_CHARFOUND_LEN(ret);
2175 if (ptr == e) break;
2176 ptr = rb_parser_search_nonascii(ptr, e);
2177 if (!ptr) break;
2178 }
2179
2180 return RB_PARSER_ENC_CODERANGE_VALID;
2181}
2182
2183static int
2184rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2185{
2186 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2187}
2188
2189static int
2190rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2191{
2192 int cr = PARSER_ENC_CODERANGE(str);
2193
2194 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2195 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2196 PARSER_ENC_CODERANGE_SET(str, cr);
2197 }
2198
2199 return cr;
2200}
2201
2202static rb_parser_string_t *
2203rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2204{
2205 if (rb_parser_str_get_encoding(str) == enc)
2206 return str;
2207 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2208 PARSER_ENC_CODERANGE_CLEAR(str);
2209 }
2210 rb_parser_string_set_encoding(str, enc);
2211 return str;
2212}
2213
2214static bool
2215rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2216{
2217 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2218}
2219
2220static rb_encoding *
2221rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2222{
2223 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2224 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2225
2226 if (enc1 == NULL || enc2 == NULL)
2227 return 0;
2228
2229 if (enc1 == enc2) {
2230 return enc1;
2231 }
2232
2233 if (PARSER_STRING_LEN(str2) == 0)
2234 return enc1;
2235 if (PARSER_STRING_LEN(str1) == 0)
2236 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2237
2238 int cr1, cr2;
2239
2240 cr1 = rb_parser_enc_str_coderange(p, str1);
2241 cr2 = rb_parser_enc_str_coderange(p, str2);
2242
2243 if (cr1 != cr2) {
2244 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2245 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2246 }
2247
2248 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2249 return enc1;
2250 }
2251
2252 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2253 return enc2;
2254 }
2255
2256 return 0;
2257}
2258
2259static void
2260rb_parser_str_modify(rb_parser_string_t *str)
2261{
2262 PARSER_ENC_CODERANGE_CLEAR(str);
2263}
2264
2265static void
2266rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2267{
2268 long capa;
2269 const int termlen = STRING_TERM_LEN(str);
2270
2271 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2272 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2273 }
2274
2275 int cr = PARSER_ENC_CODERANGE(str);
2276 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2277 /* Leave unknown. */
2278 }
2279 else if (len > PARSER_STRING_LEN(str)) {
2280 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2281 }
2282 else if (len < PARSER_STRING_LEN(str)) {
2283 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2284 /* ASCII-only string is keeping after truncated. Valid
2285 * and broken may be invalid or valid, leave unknown. */
2286 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2287 }
2288 }
2289
2290 STRING_SET_LEN(str, len);
2291 STRING_TERM_FILL(str);
2292}
2293
2294static rb_parser_string_t *
2295rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2296{
2297 rb_parser_str_modify(str);
2298 if (len == 0) return 0;
2299
2300 long total, olen, off = -1;
2301 char *sptr;
2302 const int termlen = STRING_TERM_LEN(str);
2303
2304 PARSER_STRING_GETMEM(str, sptr, olen);
2305 if (ptr >= sptr && ptr <= sptr + olen) {
2306 off = ptr - sptr;
2307 }
2308
2309 if (olen > LONG_MAX - len) {
2310 compile_error(p, "string sizes too big");
2311 return 0;
2312 }
2313 total = olen + len;
2314 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2315 sptr = PARSER_STRING_PTR(str);
2316 if (off != -1) {
2317 ptr = sptr + off;
2318 }
2319 memcpy(sptr + olen, ptr, len);
2320 STRING_SET_LEN(str, total);
2321 STRING_TERM_FILL(str);
2322
2323 return str;
2324}
2325
2326#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2327#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2328
2329static rb_parser_string_t *
2330rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2331 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2332{
2333 int str_cr, res_cr;
2334 rb_encoding *str_enc, *res_enc;
2335
2336 str_enc = rb_parser_str_get_encoding(str);
2337 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2338
2339 if (str_enc == ptr_enc) {
2340 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2341 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2342 }
2343 }
2344 else {
2345 /* parser string encoding is always asciicompat */
2346 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2347 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2348 }
2349 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2350 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2351 str_cr = rb_parser_enc_str_coderange(p, str);
2352 }
2353 }
2354 }
2355 if (ptr_cr_ret)
2356 *ptr_cr_ret = ptr_cr;
2357
2358 if (str_enc != ptr_enc &&
2359 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2360 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2361 goto incompatible;
2362 }
2363
2364 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2365 res_enc = str_enc;
2366 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2367 }
2368 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2369 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2370 res_enc = str_enc;
2371 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2372 }
2373 else {
2374 res_enc = ptr_enc;
2375 res_cr = ptr_cr;
2376 }
2377 }
2378 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2379 res_enc = str_enc;
2380 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2381 res_cr = str_cr;
2382 else
2383 res_cr = ptr_cr;
2384 }
2385 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2386 res_enc = str_enc;
2387 res_cr = str_cr;
2388 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2389 }
2390
2391 if (len < 0) {
2392 compile_error(p, "negative string size (or size too big)");
2393 }
2394 parser_str_cat(str, ptr, len);
2395 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2396 return str;
2397
2398 incompatible:
2399 compile_error(p, "incompatible character encodings: %s and %s",
2400 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2401 UNREACHABLE_RETURN(0);
2402
2403}
2404
2405static rb_parser_string_t *
2406rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2407 rb_encoding *ptr_enc)
2408{
2409 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2410}
2411
2412static rb_parser_string_t *
2413rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2414{
2415 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2416
2417 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2418 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2419
2420 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2421
2422 return str;
2423}
2424
2425static rb_parser_string_t *
2426rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2427{
2428 if (len < 0) {
2429 rb_bug("negative string size (or size too big)");
2430 }
2431
2432 long slen = PARSER_STRING_LEN(str);
2433
2434 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2435 PARSER_ENC_CODERANGE_CLEAR(str);
2436 }
2437
2438 {
2439 long capa;
2440 const int termlen = STRING_TERM_LEN(str);
2441
2442 if ((capa = slen) < len) {
2443 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2444 }
2445 else if (len == slen) return str;
2446 STRING_SET_LEN(str, len);
2447 STRING_TERM_FILL(str);
2448 }
2449 return str;
2450}
2451
2452# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2453 ((ptrvar) = str->ptr, \
2454 (lenvar) = str->len, \
2455 (encvar) = str->enc)
2456
2457static int
2458rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2459{
2460 long len1, len2;
2461 const char *ptr1, *ptr2;
2462 rb_encoding *enc1, *enc2;
2463
2464 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2465 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2466
2467 return (len1 != len2 ||
2468 enc1 != enc2 ||
2469 memcmp(ptr1, ptr2, len1) != 0);
2470}
2471
2472static void
2473rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2474{
2475 long i;
2476 if (ary->capa < len) {
2477 ary->capa = len;
2478 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2479 for (i = ary->len; i < len; i++) {
2480 ary->data[i] = 0;
2481 }
2482 }
2483}
2484
2485/*
2486 * Do not call this directly.
2487 * Use rb_parser_ary_new_capa_for_XXX() instead.
2488 */
2489static rb_parser_ary_t *
2490parser_ary_new_capa(rb_parser_t *p, long len)
2491{
2492 if (len < 0) {
2493 rb_bug("negative array size (or size too big): %ld", len);
2494 }
2495 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2496 ary->data_type = 0;
2497 ary->len = 0;
2498 ary->capa = len;
2499 if (0 < len) {
2500 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2501 }
2502 else {
2503 ary->data = NULL;
2504 }
2505 return ary;
2506}
2507
2508#ifndef RIPPER
2509static rb_parser_ary_t *
2510rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2511{
2512 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2513 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2514 return ary;
2515}
2516
2517static rb_parser_ary_t *
2518rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2519{
2520 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2521 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2522 return ary;
2523}
2524#endif
2525
2526static rb_parser_ary_t *
2527rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2528{
2529 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2530 ary->data_type = PARSER_ARY_DATA_NODE;
2531 return ary;
2532}
2533
2534/*
2535 * Do not call this directly.
2536 * Use rb_parser_ary_push_XXX() instead.
2537 */
2538static rb_parser_ary_t *
2539parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2540{
2541 if (ary->len == ary->capa) {
2542 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2543 }
2544 ary->data[ary->len++] = val;
2545 return ary;
2546}
2547
2548#ifndef RIPPER
2549static rb_parser_ary_t *
2550rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2551{
2552 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2553 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2554 }
2555 return parser_ary_push(p, ary, val);
2556}
2557
2558static rb_parser_ary_t *
2559rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2560{
2561 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2562 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2563 }
2564 return parser_ary_push(p, ary, val);
2565}
2566#endif
2567
2568static rb_parser_ary_t *
2569rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2570{
2571 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2572 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2573 }
2574 return parser_ary_push(p, ary, val);
2575}
2576
2577#ifndef RIPPER
2578static void
2579rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2580{
2581 if (!token) return;
2582 rb_parser_string_free(p, token->str);
2583 xfree(token);
2584}
2585
2586static void
2587rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2588{
2589# define foreach_ary(ptr) \
2590 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2591 ptr < end_ary_data; ptr++)
2592 switch (ary->data_type) {
2593 case PARSER_ARY_DATA_AST_TOKEN:
2594 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2595 break;
2596 case PARSER_ARY_DATA_SCRIPT_LINE:
2597 foreach_ary(data) {rb_parser_string_free(p, *data);}
2598 break;
2599 case PARSER_ARY_DATA_NODE:
2600 /* Do nothing because nodes are freed when rb_ast_t is freed */
2601 break;
2602 default:
2603 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2604 break;
2605 }
2606# undef foreach_ary
2607 xfree(ary->data);
2608 xfree(ary);
2609}
2610
2611#endif /* !RIPPER */
2612%}
2613
2614%expect 0
2615%define api.pure
2616%define parse.error verbose
2617%printer {
2618 if ((NODE *)$$ == (NODE *)-1) {
2619 rb_parser_printf(p, "NODE_SPECIAL");
2620 }
2621 else if ($$) {
2622 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2623 }
2624} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2625 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2626%printer {
2627 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2628} <id>
2629%printer {
2630 switch (nd_type(RNODE($$))) {
2631 case NODE_INTEGER:
2632 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2633 break;
2634 case NODE_FLOAT:
2635 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2636 break;
2637 case NODE_RATIONAL:
2638 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2639 break;
2640 case NODE_IMAGINARY:
2641 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2642 break;
2643 default:
2644 break;
2645 }
2646} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2647%printer {
2648 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2649} tNTH_REF
2650%printer {
2651 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2652} tBACK_REF
2653
2654%destructor {
2655 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2656} <labels>
2657
2658%lex-param {struct parser_params *p}
2659%parse-param {struct parser_params *p}
2660%initial-action
2661{
2662 RUBY_SET_YYLLOC_OF_NONE(@$);
2663};
2664%after-shift after_shift
2665%before-reduce before_reduce
2666%after-reduce after_reduce
2667%after-shift-error-token after_shift_error_token
2668%after-pop-stack after_pop_stack
2669
2670%union {
2671 NODE *node;
2672 rb_node_fcall_t *node_fcall;
2673 rb_node_args_t *node_args;
2674 rb_node_args_aux_t *node_args_aux;
2675 rb_node_opt_arg_t *node_opt_arg;
2676 rb_node_kw_arg_t *node_kw_arg;
2677 rb_node_block_pass_t *node_block_pass;
2678 rb_node_masgn_t *node_masgn;
2679 rb_node_def_temp_t *node_def_temp;
2680 rb_node_exits_t *node_exits;
2681 struct rb_locations_lambda_body_t *locations_lambda_body;
2682 ID id;
2683 int num;
2684 st_table *tbl;
2685 st_table *labels;
2686 const struct vtable *vars;
2687 struct rb_strterm_struct *strterm;
2688 struct lex_context ctxt;
2689 enum lex_state_e state;
2690}
2691
2692%token <id>
2693 keyword_class "'class'"
2694 keyword_module "'module'"
2695 keyword_def "'def'"
2696 keyword_undef "'undef'"
2697 keyword_begin "'begin'"
2698 keyword_rescue "'rescue'"
2699 keyword_ensure "'ensure'"
2700 keyword_end "'end'"
2701 keyword_if "'if'"
2702 keyword_unless "'unless'"
2703 keyword_then "'then'"
2704 keyword_elsif "'elsif'"
2705 keyword_else "'else'"
2706 keyword_case "'case'"
2707 keyword_when "'when'"
2708 keyword_while "'while'"
2709 keyword_until "'until'"
2710 keyword_for "'for'"
2711 keyword_break "'break'"
2712 keyword_next "'next'"
2713 keyword_redo "'redo'"
2714 keyword_retry "'retry'"
2715 keyword_in "'in'"
2716 keyword_do "'do'"
2717 keyword_do_cond "'do' for condition"
2718 keyword_do_block "'do' for block"
2719 keyword_do_LAMBDA "'do' for lambda"
2720 keyword_return "'return'"
2721 keyword_yield "'yield'"
2722 keyword_super "'super'"
2723 keyword_self "'self'"
2724 keyword_nil "'nil'"
2725 keyword_true "'true'"
2726 keyword_false "'false'"
2727 keyword_and "'and'"
2728 keyword_or "'or'"
2729 keyword_not "'not'"
2730 modifier_if "'if' modifier"
2731 modifier_unless "'unless' modifier"
2732 modifier_while "'while' modifier"
2733 modifier_until "'until' modifier"
2734 modifier_rescue "'rescue' modifier"
2735 keyword_alias "'alias'"
2736 keyword_defined "'defined?'"
2737 keyword_BEGIN "'BEGIN'"
2738 keyword_END "'END'"
2739 keyword__LINE__ "'__LINE__'"
2740 keyword__FILE__ "'__FILE__'"
2741 keyword__ENCODING__ "'__ENCODING__'"
2742
2743%token <id> tIDENTIFIER "local variable or method"
2744%token <id> tFID "method"
2745%token <id> tGVAR "global variable"
2746%token <id> tIVAR "instance variable"
2747%token <id> tCONSTANT "constant"
2748%token <id> tCVAR "class variable"
2749%token <id> tLABEL "label"
2750%token <node> tINTEGER "integer literal"
2751%token <node> tFLOAT "float literal"
2752%token <node> tRATIONAL "rational literal"
2753%token <node> tIMAGINARY "imaginary literal"
2754%token <node> tCHAR "char literal"
2755%token <node> tNTH_REF "numbered reference"
2756%token <node> tBACK_REF "back reference"
2757%token <node> tSTRING_CONTENT "literal content"
2758%token <num> tREGEXP_END
2759%token <num> tDUMNY_END "dummy end"
2760
2761%type <node> singleton singleton_expr strings string string1 xstring regexp
2762%type <node> string_contents xstring_contents regexp_contents string_content
2763%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2764%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2765%type <node_def_temp> defn_head defs_head k_def
2766%type <node_exits> block_open k_while k_until k_for allow_exits
2767%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2768%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2769%type <node> command command_call command_call_value method_call
2770%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2771%type <node_fcall> fcall
2772%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2773%type <node> args arg_splat call_args opt_call_args
2774%type <node> paren_args opt_paren_args
2775%type <node_args> args_tail block_args_tail
2776%type <node> command_args aref_args
2777%type <node_block_pass> opt_block_arg block_arg
2778%type <node> var_ref var_lhs
2779%type <node> command_rhs arg_rhs
2780%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2781%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2782%type <node_args_aux> f_arg f_arg_item
2783%type <node> f_marg f_rest_marg
2784%type <node_masgn> f_margs
2785%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2786%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2787%type <id> do bv_decls opt_bv_decl bvar
2788%type <node> lambda brace_body do_body
2789%type <locations_lambda_body> lambda_body
2790%type <node_args> f_larglist
2791%type <node> brace_block cmd_brace_block do_block lhs none fitem
2792%type <node> mlhs_head mlhs_item mlhs_node
2793%type <node_masgn> mlhs mlhs_basic mlhs_inner
2794%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2795%type <node> p_expr p_as p_alt p_expr_basic p_find
2796%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2797%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2798%type <node> p_kwargs p_kwarg p_kw
2799%type <id> keyword_variable user_variable sym operation2 operation3
2800%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2801%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2802%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2803%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2804%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2805%type <ctxt> p_in_kwarg
2806%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2807%type <num> max_numparam
2808%type <node> numparam
2809%type <id> it_id
2810%token END_OF_INPUT 0 "end-of-input"
2811%token <id> '.'
2812
2813/* escaped chars, should be ignored otherwise */
2814%token <id> '\\' "backslash"
2815%token tSP "escaped space"
2816%token <id> '\t' "escaped horizontal tab"
2817%token <id> '\f' "escaped form feed"
2818%token <id> '\r' "escaped carriage return"
2819%token <id> '\13' "escaped vertical tab"
2820%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2821%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2822%token tPOW RUBY_TOKEN(POW) "**"
2823%token tCMP RUBY_TOKEN(CMP) "<=>"
2824%token tEQ RUBY_TOKEN(EQ) "=="
2825%token tEQQ RUBY_TOKEN(EQQ) "==="
2826%token tNEQ RUBY_TOKEN(NEQ) "!="
2827%token tGEQ RUBY_TOKEN(GEQ) ">="
2828%token tLEQ RUBY_TOKEN(LEQ) "<="
2829%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2830%token tOROP RUBY_TOKEN(OROP) "||"
2831%token tMATCH RUBY_TOKEN(MATCH) "=~"
2832%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2833%token tDOT2 RUBY_TOKEN(DOT2) ".."
2834%token tDOT3 RUBY_TOKEN(DOT3) "..."
2835%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2836%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2837%token tAREF RUBY_TOKEN(AREF) "[]"
2838%token tASET RUBY_TOKEN(ASET) "[]="
2839%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2840%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2841%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2842%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2843%token tCOLON3 ":: at EXPR_BEG"
2844%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2845%token tASSOC "=>"
2846%token tLPAREN "("
2847%token tLPAREN_ARG "( arg"
2848%token tLBRACK "["
2849%token tLBRACE "{"
2850%token tLBRACE_ARG "{ arg"
2851%token tSTAR "*"
2852%token tDSTAR "**arg"
2853%token tAMPER "&"
2854%token <num> tLAMBDA "->"
2855%token tSYMBEG "symbol literal"
2856%token tSTRING_BEG "string literal"
2857%token tXSTRING_BEG "backtick literal"
2858%token tREGEXP_BEG "regexp literal"
2859%token tWORDS_BEG "word list"
2860%token tQWORDS_BEG "verbatim word list"
2861%token tSYMBOLS_BEG "symbol list"
2862%token tQSYMBOLS_BEG "verbatim symbol list"
2863%token tSTRING_END "terminator"
2864%token tSTRING_DEND "'}'"
2865%token <state> tSTRING_DBEG "'#{'"
2866%token tSTRING_DVAR tLAMBEG tLABEL_END
2867
2868%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2869%token tHEREDOC_BEG tHEREDOC_END k__END__
2870
2871/*
2872 * precedence table
2873 */
2874
2875%nonassoc tLOWEST
2876%nonassoc tLBRACE_ARG
2877
2878%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2879%left keyword_or keyword_and
2880%right keyword_not
2881%nonassoc keyword_defined
2882%right '=' tOP_ASGN
2883%left modifier_rescue
2884%right '?' ':'
2885%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2886%left tOROP
2887%left tANDOP
2888%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2889%left '>' tGEQ '<' tLEQ
2890%left '|' '^'
2891%left '&'
2892%left tLSHFT tRSHFT
2893%left '+' '-'
2894%left '*' '/' '%'
2895%right tUMINUS_NUM tUMINUS
2896%right tPOW
2897%right '!' '~' tUPLUS
2898
2899%token tLAST_TOKEN
2900
2901/*
2902 * inlining rules
2903 */
2904%rule %inline ident_or_const
2905 : tIDENTIFIER
2906 | tCONSTANT
2907 ;
2908
2909%rule %inline user_or_keyword_variable
2910 : user_variable
2911 | keyword_variable
2912 ;
2913
2914/*
2915 * parameterizing rules
2916 */
2917%rule asgn(rhs) <node>
2918 : lhs '=' lex_ctxt rhs
2919 {
2920 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2921 /*% ripper: assign!($:1, $:4) %*/
2922 }
2923 ;
2924
2925%rule args_tail_basic(value) <node_args>
2926 : f_kwarg(value) ',' f_kwrest opt_f_block_arg
2927 {
2928 $$ = new_args_tail(p, $1, $3, $4, &@3);
2929 /*% ripper: [$:1, $:3, $:4] %*/
2930 }
2931 | f_kwarg(value) opt_f_block_arg
2932 {
2933 $$ = new_args_tail(p, $1, 0, $2, &@1);
2934 /*% ripper: [$:1, Qnil, $:2] %*/
2935 }
2936 | f_any_kwrest opt_f_block_arg
2937 {
2938 $$ = new_args_tail(p, 0, $1, $2, &@1);
2939 /*% ripper: [Qnil, $:1, $:2] %*/
2940 }
2941 | f_block_arg
2942 {
2943 $$ = new_args_tail(p, 0, 0, $1, &@1);
2944 /*% ripper: [Qnil, Qnil, $:1] %*/
2945 }
2946
2947%rule def_endless_method(bodystmt) <node>
2948 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2949 {
2950 endless_method_name(p, $head->nd_mid, &@head);
2951 restore_defun(p, $head);
2952 ($$ = $head->nd_def)->nd_loc = @$;
2953 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2954 RNODE_DEFN($$)->nd_defn = $bodystmt;
2955 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2956 /*% ripper: def!($:head, $:args, $:$) %*/
2957 local_pop(p);
2958 }
2959 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2960 {
2961 endless_method_name(p, $head->nd_mid, &@head);
2962 restore_defun(p, $head);
2963 ($$ = $head->nd_def)->nd_loc = @$;
2964 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2965 RNODE_DEFS($$)->nd_defn = $bodystmt;
2966 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2967 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2968 local_pop(p);
2969 }
2970 ;
2971
2972%rule compstmt(stmts) <node>
2973 : stmts terms?
2974 {
2975 void_stmts(p, $$ = $stmts);
2976 }
2977 ;
2978
2979%rule f_opt(value) <node_opt_arg>
2980 : f_arg_asgn f_eq value
2981 {
2982 p->ctxt.in_argdef = 1;
2983 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2984 /*% ripper: [$:$, $:3] %*/
2985 }
2986 ;
2987
2988%rule f_opt_arg(value) <node_opt_arg>
2989 : f_opt(value)
2990 {
2991 $$ = $f_opt;
2992 /*% ripper: rb_ary_new3(1, $:1) %*/
2993 }
2994 | f_opt_arg(value) ',' f_opt(value)
2995 {
2996 $$ = opt_arg_append($f_opt_arg, $f_opt);
2997 /*% ripper: rb_ary_push($:1, $:3) %*/
2998 }
2999 ;
3000
3001%rule f_kw(value) <node_kw_arg>
3002 : f_label value
3003 {
3004 p->ctxt.in_argdef = 1;
3005 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3006 /*% ripper: [$:$, $:value] %*/
3007 }
3008 | f_label
3009 {
3010 p->ctxt.in_argdef = 1;
3011 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3012 /*% ripper: [$:$, 0] %*/
3013 }
3014 ;
3015
3016%rule f_kwarg(value) <node_kw_arg>
3017 : f_kw(value)
3018 {
3019 $$ = $f_kw;
3020 /*% ripper: rb_ary_new3(1, $:1) %*/
3021 }
3022 | f_kwarg(value) ',' f_kw(value)
3023 {
3024 $$ = kwd_append($f_kwarg, $f_kw);
3025 /*% ripper: rb_ary_push($:1, $:3) %*/
3026 }
3027 ;
3028
3029%rule mlhs_items(item) <node>
3030 : item
3031 {
3032 $$ = NEW_LIST($1, &@$);
3033 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3034 }
3035 | mlhs_items(item) ',' item
3036 {
3037 $$ = list_append(p, $1, $3);
3038 /*% ripper: mlhs_add!($:1, $:3) %*/
3039 }
3040 ;
3041
3042%rule op_asgn(rhs) <node>
3043 : var_lhs tOP_ASGN lex_ctxt rhs
3044 {
3045 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3046 /*% ripper: opassign!($:1, $:2, $:4) %*/
3047 }
3048 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3049 {
3050 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3051 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3052 }
3053 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3054 {
3055 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3056 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3057 }
3058 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3059 {
3060 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3061 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3062 }
3063 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3064 {
3065 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3066 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3067 }
3068 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3069 {
3070 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3071 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3072 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3073 }
3074 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3075 {
3076 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3077 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3078 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3079 }
3080 | backref tOP_ASGN lex_ctxt rhs
3081 {
3082 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3083 $$ = NEW_ERROR(&@$);
3084 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3085 }
3086 ;
3087
3088%rule opt_args_tail(tail) <node_args>
3089 : ',' tail
3090 {
3091 $$ = $tail;
3092 /*% ripper: $:2 %*/
3093 }
3094 | /* none */
3095 {
3096 $$ = new_args_tail(p, 0, 0, 0, &@0);
3097 /*% ripper: [Qnil, Qnil, Qnil] %*/
3098 }
3099 ;
3100
3101%rule range_expr(range) <node>
3102 : range tDOT2 range
3103 {
3104 value_expr(p, $1);
3105 value_expr(p, $3);
3106 $$ = NEW_DOT2($1, $3, &@$, &@2);
3107 /*% ripper: dot2!($:1, $:3) %*/
3108 }
3109 | range tDOT3 range
3110 {
3111 value_expr(p, $1);
3112 value_expr(p, $3);
3113 $$ = NEW_DOT3($1, $3, &@$, &@2);
3114 /*% ripper: dot3!($:1, $:3) %*/
3115 }
3116 | range tDOT2
3117 {
3118 value_expr(p, $1);
3119 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3120 /*% ripper: dot2!($:1, Qnil) %*/
3121 }
3122 | range tDOT3
3123 {
3124 value_expr(p, $1);
3125 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3126 /*% ripper: dot3!($:1, Qnil) %*/
3127 }
3128 | tBDOT2 range
3129 {
3130 value_expr(p, $2);
3131 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3132 /*% ripper: dot2!(Qnil, $:2) %*/
3133 }
3134 | tBDOT3 range
3135 {
3136 value_expr(p, $2);
3137 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3138 /*% ripper: dot3!(Qnil, $:2) %*/
3139 }
3140 ;
3141
3142%rule value_expr(value) <node>
3143 : value
3144 {
3145 value_expr(p, $1);
3146 $$ = $1;
3147 }
3148 ;
3149
3150%rule words(begin, word_list) <node>
3151 : begin ' '+ word_list tSTRING_END
3152 {
3153 $$ = make_list($word_list, &@$);
3154 /*% ripper: array!($:3) %*/
3155 }
3156 ;
3157
3158%%
3159program : {
3160 SET_LEX_STATE(EXPR_BEG);
3161 local_push(p, ifndef_ripper(1)+0);
3162 /* jumps are possible in the top-level loop. */
3163 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3164 }
3165 compstmt(top_stmts)
3166 {
3167 if ($2 && !compile_for_eval) {
3168 NODE *node = $2;
3169 /* last expression should not be void */
3170 if (nd_type_p(node, NODE_BLOCK)) {
3171 while (RNODE_BLOCK(node)->nd_next) {
3172 node = RNODE_BLOCK(node)->nd_next;
3173 }
3174 node = RNODE_BLOCK(node)->nd_head;
3175 }
3176 node = remove_begin(node);
3177 void_expr(p, node);
3178 }
3179 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3180 /*% ripper[final]: program!($:2) %*/
3181 local_pop(p);
3182 }
3183 ;
3184
3185top_stmts : none
3186 {
3187 $$ = NEW_BEGIN(0, &@$);
3188 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3189 }
3190 | top_stmt
3191 {
3192 $$ = newline_node($1);
3193 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3194 }
3195 | top_stmts terms top_stmt
3196 {
3197 $$ = block_append(p, $1, newline_node($3));
3198 /*% ripper: stmts_add!($:1, $:3) %*/
3199 }
3200 ;
3201
3202top_stmt : stmt
3203 {
3204 clear_block_exit(p, true);
3205 $$ = $1;
3206 }
3207 | keyword_BEGIN begin_block
3208 {
3209 $$ = $2;
3210 /*% ripper: $:2 %*/
3211 }
3212 ;
3213
3214block_open : '{' {$$ = init_block_exit(p);};
3215
3216begin_block : block_open compstmt(top_stmts) '}'
3217 {
3218 restore_block_exit(p, $block_open);
3219 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3220 NEW_BEGIN($2, &@$));
3221 $$ = NEW_BEGIN(0, &@$);
3222 /*% ripper: BEGIN!($:2) %*/
3223 }
3224 ;
3225
3226bodystmt : compstmt(stmts)[body]
3227 lex_ctxt[ctxt]
3228 opt_rescue
3229 k_else
3230 {
3231 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3232 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3233 }
3234 compstmt(stmts)[elsebody]
3235 {
3236 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3237 }
3238 opt_ensure
3239 {
3240 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3241 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3242 }
3243 | compstmt(stmts)[body]
3244 lex_ctxt[ctxt]
3245 opt_rescue
3246 {
3247 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3248 }
3249 opt_ensure
3250 {
3251 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3252 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3253 }
3254 ;
3255
3256stmts : none
3257 {
3258 $$ = NEW_BEGIN(0, &@$);
3259 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3260 }
3261 | stmt_or_begin
3262 {
3263 $$ = newline_node($1);
3264 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3265 }
3266 | stmts terms stmt_or_begin
3267 {
3268 $$ = block_append(p, $1, newline_node($3));
3269 /*% ripper: stmts_add!($:1, $:3) %*/
3270 }
3271 ;
3272
3273stmt_or_begin : stmt
3274 | keyword_BEGIN
3275 {
3276 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3277 }
3278 begin_block
3279 {
3280 $$ = $3;
3281 }
3282 ;
3283
3284allow_exits : {$$ = allow_block_exit(p);};
3285
3286k_END : keyword_END lex_ctxt
3287 {
3288 $$ = $2;
3289 p->ctxt.in_rescue = before_rescue;
3290 /*% ripper: $:2 %*/
3291 };
3292
3293stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3294 {
3295 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3296 /*% ripper: alias!($:2, $:4) %*/
3297 }
3298 | keyword_alias tGVAR tGVAR
3299 {
3300 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3301 /*% ripper: var_alias!($:2, $:3) %*/
3302 }
3303 | keyword_alias tGVAR tBACK_REF
3304 {
3305 char buf[2];
3306 buf[0] = '$';
3307 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3308 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3309 /*% ripper: var_alias!($:2, $:3) %*/
3310 }
3311 | keyword_alias tGVAR tNTH_REF
3312 {
3313 static const char mesg[] = "can't make alias for the number variables";
3314 /*%%%*/
3315 yyerror1(&@3, mesg);
3316 /*% %*/
3317 $$ = NEW_ERROR(&@$);
3318 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3319 }
3320 | keyword_undef undef_list
3321 {
3322 nd_set_first_loc($2, @1.beg_pos);
3323 RNODE_UNDEF($2)->keyword_loc = @1;
3324 $$ = $2;
3325 /*% ripper: undef!($:2) %*/
3326 }
3327 | stmt modifier_if expr_value
3328 {
3329 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3330 fixpos($$, $3);
3331 /*% ripper: if_mod!($:3, $:1) %*/
3332 }
3333 | stmt modifier_unless expr_value
3334 {
3335 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3336 fixpos($$, $3);
3337 /*% ripper: unless_mod!($:3, $:1) %*/
3338 }
3339 | stmt modifier_while expr_value
3340 {
3341 clear_block_exit(p, false);
3342 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3343 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3344 }
3345 else {
3346 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3347 }
3348 /*% ripper: while_mod!($:3, $:1) %*/
3349 }
3350 | stmt modifier_until expr_value
3351 {
3352 clear_block_exit(p, false);
3353 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3354 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3355 }
3356 else {
3357 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3358 }
3359 /*% ripper: until_mod!($:3, $:1) %*/
3360 }
3361 | stmt modifier_rescue after_rescue stmt
3362 {
3363 p->ctxt.in_rescue = $3.in_rescue;
3364 NODE *resq;
3365 YYLTYPE loc = code_loc_gen(&@2, &@4);
3366 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3367 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3368 /*% ripper: rescue_mod!($:1, $:4) %*/
3369 }
3370 | k_END allow_exits '{' compstmt(stmts) '}'
3371 {
3372 if (p->ctxt.in_def) {
3373 rb_warn0("END in method; use at_exit");
3374 }
3375 restore_block_exit(p, $allow_exits);
3376 p->ctxt = $k_END;
3377 {
3378 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, NULL /* parent */, &@$);
3379 $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
3380 RNODE_SCOPE(scope)->nd_parent = $$;
3381 }
3382 /*% ripper: END!($:compstmt) %*/
3383 }
3384 | command_asgn
3385 | mlhs '=' lex_ctxt command_call_value
3386 {
3387 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3388 /*% ripper: massign!($:1, $:4) %*/
3389 }
3390 | asgn(mrhs)
3391 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3392 after_rescue stmt[resbody]
3393 {
3394 p->ctxt.in_rescue = $3.in_rescue;
3395 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3396 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3397 loc.beg_pos = @mrhs_arg.beg_pos;
3398 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3399 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3400 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3401 }
3402 | mlhs '=' lex_ctxt mrhs_arg
3403 {
3404 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3405 /*% ripper: massign!($:1, $:4) %*/
3406 }
3407 | expr
3408 | error
3409 {
3410 (void)yynerrs;
3411 $$ = NEW_ERROR(&@$);
3412 }
3413 ;
3414
3415command_asgn : asgn(command_rhs)
3416 | op_asgn(command_rhs)
3417 | def_endless_method(endless_command)
3418 ;
3419
3420endless_command : command
3421 | endless_command modifier_rescue after_rescue arg
3422 {
3423 p->ctxt.in_rescue = $3.in_rescue;
3424 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3425 /*% ripper: rescue_mod!($:1, $:4) %*/
3426 }
3427 | keyword_not '\n'? endless_command
3428 {
3429 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3430 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3431 }
3432 ;
3433
3434command_rhs : command_call_value %prec tOP_ASGN
3435 | command_call_value modifier_rescue after_rescue stmt
3436 {
3437 p->ctxt.in_rescue = $3.in_rescue;
3438 YYLTYPE loc = code_loc_gen(&@2, &@4);
3439 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3440 /*% ripper: rescue_mod!($:1, $:4) %*/
3441 }
3442 | command_asgn
3443 ;
3444
3445expr : command_call
3446 | expr keyword_and expr
3447 {
3448 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3449 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3450 }
3451 | expr keyword_or expr
3452 {
3453 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3454 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3455 }
3456 | keyword_not '\n'? expr
3457 {
3458 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3459 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3460 }
3461 | '!' command_call
3462 {
3463 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3464 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3465 }
3466 | arg tASSOC
3467 {
3468 value_expr(p, $arg);
3469 }
3470 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3471 p_top_expr_body[body]
3472 {
3473 pop_pktbl(p, $p_pktbl);
3474 pop_pvtbl(p, $p_pvtbl);
3475 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3476 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3477 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3478 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@2), &@$, &NULL_LOC, &NULL_LOC);
3479 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3480 }
3481 | arg keyword_in
3482 {
3483 value_expr(p, $arg);
3484 }
3485 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3486 p_top_expr_body[body]
3487 {
3488 pop_pktbl(p, $p_pktbl);
3489 pop_pvtbl(p, $p_pvtbl);
3490 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3491 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3492 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3493 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3494 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3495 }
3496 | arg %prec tLBRACE_ARG
3497 ;
3498
3499def_name : fname
3500 {
3501 numparam_name(p, $fname);
3502 local_push(p, 0);
3503 p->ctxt.in_def = 1;
3504 p->ctxt.in_rescue = before_rescue;
3505 p->ctxt.cant_return = 0;
3506 $$ = $fname;
3507 }
3508 ;
3509
3510defn_head : k_def def_name
3511 {
3512 $$ = def_head_save(p, $k_def);
3513 $$->nd_mid = $def_name;
3514 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3515 /*% ripper: $:def_name %*/
3516 }
3517 ;
3518
3519defs_head : k_def singleton dot_or_colon
3520 {
3521 SET_LEX_STATE(EXPR_FNAME);
3522 }
3523 def_name
3524 {
3525 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3526 $$ = def_head_save(p, $k_def);
3527 $$->nd_mid = $def_name;
3528 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3529 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3530 }
3531 ;
3532
3533expr_value : value_expr(expr)
3534 | error
3535 {
3536 $$ = NEW_ERROR(&@$);
3537 }
3538 ;
3539
3540expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3541 {
3542 $$ = $2;
3543 /*% ripper: $:2 %*/
3544 }
3545 ;
3546
3547command_call : command
3548 | block_command
3549 ;
3550
3551command_call_value : value_expr(command_call)
3552 ;
3553
3554block_command : block_call
3555 | block_call call_op2 operation2 command_args
3556 {
3557 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3558 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3559 }
3560 ;
3561
3562cmd_brace_block : tLBRACE_ARG brace_body '}'
3563 {
3564 $$ = $2;
3565 set_embraced_location($$, &@1, &@3);
3566 /*% ripper: $:2 %*/
3567 }
3568 ;
3569
3570fcall : operation
3571 {
3572 $$ = NEW_FCALL($1, 0, &@$);
3573 /*% ripper: $:1 %*/
3574 }
3575 ;
3576
3577command : fcall command_args %prec tLOWEST
3578 {
3579 $1->nd_args = $2;
3580 nd_set_last_loc($1, @2.end_pos);
3581 $$ = (NODE *)$1;
3582 /*% ripper: command!($:1, $:2) %*/
3583 }
3584 | fcall command_args cmd_brace_block
3585 {
3586 block_dup_check(p, $2, $3);
3587 $1->nd_args = $2;
3588 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3589 fixpos($$, RNODE($1));
3590 nd_set_last_loc($1, @2.end_pos);
3591 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3592 }
3593 | primary_value call_op operation2 command_args %prec tLOWEST
3594 {
3595 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3596 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3597 }
3598 | primary_value call_op operation2 command_args cmd_brace_block
3599 {
3600 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3601 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3602 }
3603 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3604 {
3605 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3606 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3607 }
3608 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3609 {
3610 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3611 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3612 }
3613 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3614 {
3615 set_embraced_location($5, &@4, &@6);
3616 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3617 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3618 }
3619 | keyword_super command_args
3620 {
3621 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3622 fixpos($$, $2);
3623 /*% ripper: super!($:2) %*/
3624 }
3625 | k_yield command_args
3626 {
3627 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3628 fixpos($$, $2);
3629 /*% ripper: yield!($:2) %*/
3630 }
3631 | k_return call_args
3632 {
3633 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3634 /*% ripper: return!($:2) %*/
3635 }
3636 | keyword_break call_args
3637 {
3638 NODE *args = 0;
3639 args = ret_args(p, $2);
3640 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3641 /*% ripper: break!($:2) %*/
3642 }
3643 | keyword_next call_args
3644 {
3645 NODE *args = 0;
3646 args = ret_args(p, $2);
3647 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3648 /*% ripper: next!($:2) %*/
3649 }
3650 ;
3651
3652mlhs : mlhs_basic
3653 | tLPAREN mlhs_inner rparen
3654 {
3655 $$ = $2;
3656 /*% ripper: mlhs_paren!($:2) %*/
3657 }
3658 ;
3659
3660mlhs_inner : mlhs_basic
3661 | tLPAREN mlhs_inner rparen
3662 {
3663 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3664 /*% ripper: mlhs_paren!($:2) %*/
3665 }
3666 ;
3667
3668mlhs_basic : mlhs_head
3669 {
3670 $$ = NEW_MASGN($1, 0, &@$);
3671 /*% ripper: $:1 %*/
3672 }
3673 | mlhs_head mlhs_item
3674 {
3675 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3676 /*% ripper: mlhs_add!($:1, $:2) %*/
3677 }
3678 | mlhs_head tSTAR mlhs_node
3679 {
3680 $$ = NEW_MASGN($1, $3, &@$);
3681 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3682 }
3683 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3684 {
3685 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3686 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3687 }
3688 | mlhs_head tSTAR
3689 {
3690 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3691 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3692 }
3693 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3694 {
3695 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3696 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3697 }
3698 | tSTAR mlhs_node
3699 {
3700 $$ = NEW_MASGN(0, $2, &@$);
3701 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3702 }
3703 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3704 {
3705 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3706 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3707 }
3708 | tSTAR
3709 {
3710 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3711 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3712 }
3713 | tSTAR ',' mlhs_items(mlhs_item)
3714 {
3715 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3716 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3717 }
3718 ;
3719
3720mlhs_item : mlhs_node
3721 | tLPAREN mlhs_inner rparen
3722 {
3723 $$ = (NODE *)$2;
3724 /*% ripper: mlhs_paren!($:2) %*/
3725 }
3726 ;
3727
3728mlhs_head : mlhs_item ','
3729 {
3730 $$ = NEW_LIST($1, &@1);
3731 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3732 }
3733 | mlhs_head mlhs_item ','
3734 {
3735 $$ = list_append(p, $1, $2);
3736 /*% ripper: mlhs_add!($:1, $:2) %*/
3737 }
3738 ;
3739
3740
3741mlhs_node : user_or_keyword_variable
3742 {
3743 /*% ripper: var_field!($:1) %*/
3744 $$ = assignable(p, $1, 0, &@$);
3745 }
3746 | primary_value '[' opt_call_args rbracket
3747 {
3748 $$ = aryset(p, $1, $3, &@$);
3749 /*% ripper: aref_field!($:1, $:3) %*/
3750 }
3751 | primary_value call_op ident_or_const
3752 {
3753 anddot_multiple_assignment_check(p, &@2, $2);
3754 $$ = attrset(p, $1, $2, $3, &@$);
3755 /*% ripper: field!($:1, $:2, $:3) %*/
3756 }
3757 | primary_value tCOLON2 tIDENTIFIER
3758 {
3759 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3760 /*% ripper: const_path_field!($:1, $:3) %*/
3761 }
3762 | primary_value tCOLON2 tCONSTANT
3763 {
3764 /*% ripper: const_path_field!($:1, $:3) %*/
3765 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3766 }
3767 | tCOLON3 tCONSTANT
3768 {
3769 /*% ripper: top_const_field!($:2) %*/
3770 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3771 }
3772 | backref
3773 {
3774 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3775 $$ = NEW_ERROR(&@$);
3776 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3777 }
3778 ;
3779
3780lhs : user_or_keyword_variable
3781 {
3782 /*% ripper: var_field!($:1) %*/
3783 $$ = assignable(p, $1, 0, &@$);
3784 }
3785 | primary_value '[' opt_call_args rbracket
3786 {
3787 $$ = aryset(p, $1, $3, &@$);
3788 /*% ripper: aref_field!($:1, $:3) %*/
3789 }
3790 | primary_value call_op ident_or_const
3791 {
3792 $$ = attrset(p, $1, $2, $3, &@$);
3793 /*% ripper: field!($:1, $:2, $:3) %*/
3794 }
3795 | primary_value tCOLON2 tIDENTIFIER
3796 {
3797 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3798 /*% ripper: field!($:1, $:2, $:3) %*/
3799 }
3800 | primary_value tCOLON2 tCONSTANT
3801 {
3802 /*% ripper: const_path_field!($:1, $:3) %*/
3803 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3804 }
3805 | tCOLON3 tCONSTANT
3806 {
3807 /*% ripper: top_const_field!($:2) %*/
3808 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3809 }
3810 | backref
3811 {
3812 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3813 $$ = NEW_ERROR(&@$);
3814 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3815 }
3816 ;
3817
3818cname : tIDENTIFIER
3819 {
3820 static const char mesg[] = "class/module name must be CONSTANT";
3821 /*%%%*/
3822 yyerror1(&@1, mesg);
3823 /*% %*/
3824 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3825 }
3826 | tCONSTANT
3827 ;
3828
3829cpath : tCOLON3 cname
3830 {
3831 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3832 /*% ripper: top_const_ref!($:2) %*/
3833 }
3834 | cname
3835 {
3836 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3837 /*% ripper: const_ref!($:1) %*/
3838 }
3839 | primary_value tCOLON2 cname
3840 {
3841 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3842 /*% ripper: const_path_ref!($:1, $:3) %*/
3843 }
3844 ;
3845
3846fname : operation
3847 | op
3848 {
3849 SET_LEX_STATE(EXPR_ENDFN);
3850 $$ = $1;
3851 }
3852 | reswords
3853 ;
3854
3855fitem : fname
3856 {
3857 $$ = NEW_SYM(rb_id2str($1), &@$);
3858 /*% ripper: symbol_literal!($:1) %*/
3859 }
3860 | symbol
3861 ;
3862
3863undef_list : fitem
3864 {
3865 $$ = NEW_UNDEF($1, &@$);
3866 /*% ripper: rb_ary_new3(1, $:1) %*/
3867 }
3868 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3869 {
3870 nd_set_last_loc($1, @4.end_pos);
3871 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3872 /*% ripper: rb_ary_push($:1, $:4) %*/
3873 }
3874 ;
3875
3876op : '|' { $$ = '|'; }
3877 | '^' { $$ = '^'; }
3878 | '&' { $$ = '&'; }
3879 | tCMP { $$ = tCMP; }
3880 | tEQ { $$ = tEQ; }
3881 | tEQQ { $$ = tEQQ; }
3882 | tMATCH { $$ = tMATCH; }
3883 | tNMATCH { $$ = tNMATCH; }
3884 | '>' { $$ = '>'; }
3885 | tGEQ { $$ = tGEQ; }
3886 | '<' { $$ = '<'; }
3887 | tLEQ { $$ = tLEQ; }
3888 | tNEQ { $$ = tNEQ; }
3889 | tLSHFT { $$ = tLSHFT; }
3890 | tRSHFT { $$ = tRSHFT; }
3891 | '+' { $$ = '+'; }
3892 | '-' { $$ = '-'; }
3893 | '*' { $$ = '*'; }
3894 | tSTAR { $$ = '*'; }
3895 | '/' { $$ = '/'; }
3896 | '%' { $$ = '%'; }
3897 | tPOW { $$ = tPOW; }
3898 | tDSTAR { $$ = tDSTAR; }
3899 | '!' { $$ = '!'; }
3900 | '~' { $$ = '~'; }
3901 | tUPLUS { $$ = tUPLUS; }
3902 | tUMINUS { $$ = tUMINUS; }
3903 | tAREF { $$ = tAREF; }
3904 | tASET { $$ = tASET; }
3905 | '`' { $$ = '`'; }
3906 ;
3907
3908reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3909 | keyword_BEGIN | keyword_END
3910 | keyword_alias | keyword_and | keyword_begin
3911 | keyword_break | keyword_case | keyword_class | keyword_def
3912 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3913 | keyword_end | keyword_ensure | keyword_false
3914 | keyword_for | keyword_in | keyword_module | keyword_next
3915 | keyword_nil | keyword_not | keyword_or | keyword_redo
3916 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3917 | keyword_super | keyword_then | keyword_true | keyword_undef
3918 | keyword_when | keyword_yield | keyword_if | keyword_unless
3919 | keyword_while | keyword_until
3920 ;
3921
3922arg : asgn(arg_rhs)
3923 | op_asgn(arg_rhs)
3924 | range_expr(arg)
3925 | arg '+' arg
3926 {
3927 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3928 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3929 }
3930 | arg '-' arg
3931 {
3932 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3933 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3934 }
3935 | arg '*' arg
3936 {
3937 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3938 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3939 }
3940 | arg '/' arg
3941 {
3942 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3943 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3944 }
3945 | arg '%' arg
3946 {
3947 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3948 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3949 }
3950 | arg tPOW arg
3951 {
3952 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3953 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3954 }
3955 | tUMINUS_NUM simple_numeric tPOW arg
3956 {
3957 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3958 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3959 }
3960 | tUPLUS arg
3961 {
3962 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3963 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3964 }
3965 | tUMINUS arg
3966 {
3967 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3968 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3969 }
3970 | arg '|' arg
3971 {
3972 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3973 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3974 }
3975 | arg '^' arg
3976 {
3977 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3978 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3979 }
3980 | arg '&' arg
3981 {
3982 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3983 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3984 }
3985 | arg tCMP arg
3986 {
3987 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3988 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3989 }
3990 | rel_expr %prec tCMP
3991 | arg tEQ arg
3992 {
3993 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3994 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3995 }
3996 | arg tEQQ arg
3997 {
3998 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3999 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4000 }
4001 | arg tNEQ arg
4002 {
4003 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4004 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4005 }
4006 | arg tMATCH arg
4007 {
4008 $$ = match_op(p, $1, $3, &@2, &@$);
4009 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4010 }
4011 | arg tNMATCH arg
4012 {
4013 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4014 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4015 }
4016 | '!' arg
4017 {
4018 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4019 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4020 }
4021 | '~' arg
4022 {
4023 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4024 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4025 }
4026 | arg tLSHFT arg
4027 {
4028 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4029 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4030 }
4031 | arg tRSHFT arg
4032 {
4033 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4034 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4035 }
4036 | arg tANDOP arg
4037 {
4038 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4039 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4040 }
4041 | arg tOROP arg
4042 {
4043 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4044 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4045 }
4046 | keyword_defined '\n'? begin_defined arg
4047 {
4048 p->ctxt.in_defined = $3.in_defined;
4049 $$ = new_defined(p, $4, &@$, &@1);
4050 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4051 /*% ripper: defined!($:4) %*/
4052 }
4053 | def_endless_method(endless_arg)
4054 | ternary
4055 | primary
4056 ;
4057
4058ternary : arg '?' arg '\n'? ':' arg
4059 {
4060 value_expr(p, $1);
4061 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4062 fixpos($$, $1);
4063 /*% ripper: ifop!($:1, $:3, $:6) %*/
4064 }
4065 ;
4066
4067endless_arg : arg %prec modifier_rescue
4068 | endless_arg modifier_rescue after_rescue arg
4069 {
4070 p->ctxt.in_rescue = $3.in_rescue;
4071 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4072 /*% ripper: rescue_mod!($:1, $:4) %*/
4073 }
4074 | keyword_not '\n'? endless_arg
4075 {
4076 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4077 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4078 }
4079 ;
4080
4081relop : '>' {$$ = '>';}
4082 | '<' {$$ = '<';}
4083 | tGEQ {$$ = idGE;}
4084 | tLEQ {$$ = idLE;}
4085 ;
4086
4087rel_expr : arg relop arg %prec '>'
4088 {
4089 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4090 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4091 }
4092 | rel_expr relop arg %prec '>'
4093 {
4094 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4095 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4096 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4097 }
4098 ;
4099
4100lex_ctxt : none
4101 {
4102 $$ = p->ctxt;
4103 }
4104 ;
4105
4106begin_defined : lex_ctxt
4107 {
4108 p->ctxt.in_defined = 1;
4109 $$ = $1;
4110 }
4111 ;
4112
4113after_rescue : lex_ctxt
4114 {
4115 p->ctxt.in_rescue = after_rescue;
4116 $$ = $1;
4117 }
4118 ;
4119
4120arg_value : value_expr(arg)
4121 ;
4122
4123aref_args : none
4124 | args trailer
4125 | args ',' assocs trailer
4126 {
4127 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4128 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4129 }
4130 | assocs trailer
4131 {
4132 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4133 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4134 }
4135 ;
4136
4137arg_rhs : arg %prec tOP_ASGN
4138 {
4139 value_expr(p, $1);
4140 $$ = $1;
4141 }
4142 | arg modifier_rescue after_rescue arg
4143 {
4144 p->ctxt.in_rescue = $3.in_rescue;
4145 value_expr(p, $1);
4146 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4147 /*% ripper: rescue_mod!($:1, $:4) %*/
4148 }
4149 ;
4150
4151paren_args : '(' opt_call_args rparen
4152 {
4153 $$ = $2;
4154 /*% ripper: arg_paren!($:2) %*/
4155 }
4156 | '(' args ',' args_forward rparen
4157 {
4158 if (!check_forwarding_args(p)) {
4159 $$ = 0;
4160 }
4161 else {
4162 $$ = new_args_forward_call(p, $2, &@4, &@$);
4163 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4164 }
4165 }
4166 | '(' args_forward rparen
4167 {
4168 if (!check_forwarding_args(p)) {
4169 $$ = 0;
4170 }
4171 else {
4172 $$ = new_args_forward_call(p, 0, &@2, &@$);
4173 /*% ripper: arg_paren!($:2) %*/
4174 }
4175 }
4176 ;
4177
4178opt_paren_args : none
4179 | paren_args
4180 {
4181 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4182 }
4183 ;
4184
4185opt_call_args : none
4186 | call_args
4187 | args ','
4188 | args ',' assocs ','
4189 {
4190 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4191 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4192 }
4193 | assocs ','
4194 {
4195 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4196 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4197 }
4198 ;
4199
4200call_args : value_expr(command)
4201 {
4202 $$ = NEW_LIST($1, &@$);
4203 /*% ripper: args_add!(args_new!, $:1) %*/
4204 }
4205 | def_endless_method(endless_command)
4206 {
4207 $$ = NEW_LIST($1, &@$);
4208 /*% ripper: args_add!(args_new!, $:1) %*/
4209 }
4210 | args opt_block_arg
4211 {
4212 $$ = arg_blk_pass($1, $2);
4213 /*% ripper: args_add_block!($:1, $:2) %*/
4214 }
4215 | assocs opt_block_arg
4216 {
4217 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4218 $$ = arg_blk_pass($$, $2);
4219 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4220 }
4221 | args ',' assocs opt_block_arg
4222 {
4223 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4224 $$ = arg_blk_pass($$, $4);
4225 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4226 }
4227 | block_arg
4228 /*% ripper: args_add_block!(args_new!, $:1) %*/
4229 ;
4230
4231command_args : {
4232 /* If call_args starts with a open paren '(' or '[',
4233 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4234 * but the push must be done after CMDARG_PUSH(1).
4235 * So this code makes them consistent by first cancelling
4236 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4237 * and finally redoing CMDARG_PUSH(0).
4238 */
4239 int lookahead = 0;
4240 switch (yychar) {
4241 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4242 lookahead = 1;
4243 }
4244 if (lookahead) CMDARG_POP();
4245 CMDARG_PUSH(1);
4246 if (lookahead) CMDARG_PUSH(0);
4247 }
4248 call_args
4249 {
4250 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4251 * but the push must be done after CMDARG_POP() in the parser.
4252 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4253 * CMDARG_POP() to pop 1 pushed by command_args,
4254 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4255 */
4256 int lookahead = 0;
4257 switch (yychar) {
4258 case tLBRACE_ARG:
4259 lookahead = 1;
4260 }
4261 if (lookahead) CMDARG_POP();
4262 CMDARG_POP();
4263 if (lookahead) CMDARG_PUSH(0);
4264 $$ = $2;
4265 /*% ripper: $:2 %*/
4266 }
4267 ;
4268
4269block_arg : tAMPER arg_value
4270 {
4271 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4272 /*% ripper: $:2 %*/
4273 }
4274 | tAMPER
4275 {
4276 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4277 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4278 /*% ripper: Qnil %*/
4279 }
4280 ;
4281
4282opt_block_arg : ',' block_arg
4283 {
4284 $$ = $2;
4285 /*% ripper: $:2 %*/
4286 }
4287 | none
4288 {
4289 $$ = 0;
4290 /*% ripper: Qfalse %*/
4291 }
4292 ;
4293
4294/* value */
4295args : arg_value
4296 {
4297 $$ = NEW_LIST($arg_value, &@$);
4298 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4299 }
4300 | arg_splat
4301 {
4302 $$ = $arg_splat;
4303 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4304 }
4305 | args[non_last_args] ',' arg_value
4306 {
4307 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4308 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4309 }
4310 | args[non_last_args] ',' arg_splat
4311 {
4312 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4313 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4314 }
4315 ;
4316
4317/* value */
4318arg_splat : tSTAR arg_value
4319 {
4320 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4321 /*% ripper: $:arg_value %*/
4322 }
4323 | tSTAR /* none */
4324 {
4325 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4326 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4327 /*% ripper: Qnil %*/
4328 }
4329 ;
4330
4331/* value */
4332mrhs_arg : mrhs
4333 | arg_value
4334 ;
4335
4336/* value */
4337mrhs : args ',' arg_value
4338 {
4339 $$ = last_arg_append(p, $args, $arg_value, &@$);
4340 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4341 }
4342 | args ',' tSTAR arg_value
4343 {
4344 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4345 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4346 }
4347 | tSTAR arg_value
4348 {
4349 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4350 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4351 }
4352 ;
4353
4354%rule %inline inline_primary
4355 : literal
4356 | strings
4357 | xstring
4358 | regexp
4359 | words
4360 | qwords
4361 | symbols
4362 | qsymbols
4363 ;
4364
4365primary : inline_primary
4366 | var_ref
4367 | backref
4368 | tFID
4369 {
4370 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4371 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4372 }
4373 | k_begin
4374 {
4375 CMDARG_PUSH(0);
4376 }
4377 bodystmt
4378 k_end
4379 {
4380 CMDARG_POP();
4381 set_line_body($3, @1.end_pos.lineno);
4382 $$ = NEW_BEGIN($3, &@$);
4383 nd_set_line($$, @1.end_pos.lineno);
4384 /*% ripper: begin!($:3) %*/
4385 }
4386 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4387 {
4388 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4389 $$ = $2;
4390 /*% ripper: paren!($:2) %*/
4391 }
4392 | tLPAREN compstmt(stmts) ')'
4393 {
4394 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4395 $$ = NEW_BLOCK($2, &@$);
4396 /*% ripper: paren!($:2) %*/
4397 }
4398 | primary_value tCOLON2 tCONSTANT
4399 {
4400 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
4401 /*% ripper: const_path_ref!($:1, $:3) %*/
4402 }
4403 | tCOLON3 tCONSTANT
4404 {
4405 $$ = NEW_COLON3($2, &@$, &@1, &@2);
4406 /*% ripper: top_const_ref!($:2) %*/
4407 }
4408 | tLBRACK aref_args ']'
4409 {
4410 $$ = make_list($2, &@$);
4411 /*% ripper: array!($:2) %*/
4412 }
4413 | tLBRACE assoc_list '}'
4414 {
4415 $$ = new_hash(p, $2, &@$);
4416 RNODE_HASH($$)->nd_brace = TRUE;
4417 /*% ripper: hash!($:2) %*/
4418 }
4419 | k_return
4420 {
4421 $$ = NEW_RETURN(0, &@$, &@1);
4422 /*% ripper: return0! %*/
4423 }
4424 | k_yield '(' call_args rparen
4425 {
4426 $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4);
4427 /*% ripper: yield!(paren!($:3)) %*/
4428 }
4429 | k_yield '(' rparen
4430 {
4431 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4432 /*% ripper: yield!(paren!(args_new!)) %*/
4433 }
4434 | k_yield
4435 {
4436 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4437 /*% ripper: yield0! %*/
4438 }
4439 | keyword_defined '\n'? '(' begin_defined expr rparen
4440 {
4441 p->ctxt.in_defined = $4.in_defined;
4442 $$ = new_defined(p, $5, &@$, &@1);
4443 p->ctxt.has_trailing_semicolon = $4.has_trailing_semicolon;
4444 /*% ripper: defined!($:5) %*/
4445 }
4446 | keyword_not '(' expr rparen
4447 {
4448 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4449 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4450 }
4451 | keyword_not '(' rparen
4452 {
4453 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@2), &@2), METHOD_NOT, &@1, &@$);
4454 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4455 }
4456 | fcall brace_block
4457 {
4458 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4459 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4460 }
4461 | method_call
4462 | method_call brace_block
4463 {
4464 block_dup_check(p, get_nd_args(p, $1), $2);
4465 $$ = method_add_block(p, $1, $2, &@$);
4466 /*% ripper: method_add_block!($:1, $:2) %*/
4467 }
4468 | lambda
4469 | k_if expr_value then
4470 compstmt(stmts)
4471 if_tail
4472 k_end
4473 {
4474 if ($5 && nd_type_p($5, NODE_IF))
4475 RNODE_IF($5)->end_keyword_loc = @6;
4476
4477 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4478 fixpos($$, $2);
4479 /*% ripper: if!($:2, $:4, $:5) %*/
4480 }
4481 | k_unless expr_value then
4482 compstmt(stmts)
4483 opt_else
4484 k_end
4485 {
4486 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4487 fixpos($$, $2);
4488 /*% ripper: unless!($:2, $:4, $:5) %*/
4489 }
4490 | k_while expr_value_do
4491 compstmt(stmts)
4492 k_end
4493 {
4494 restore_block_exit(p, $1);
4495 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4496 fixpos($$, $2);
4497 /*% ripper: while!($:2, $:3) %*/
4498 }
4499 | k_until expr_value_do
4500 compstmt(stmts)
4501 k_end
4502 {
4503 restore_block_exit(p, $1);
4504 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4505 fixpos($$, $2);
4506 /*% ripper: until!($:2, $:3) %*/
4507 }
4508 | k_case expr_value terms?
4509 {
4510 $$ = p->case_labels;
4511 p->case_labels = CHECK_LITERAL_WHEN;
4512 }<labels>
4513 case_body
4514 k_end
4515 {
4516 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4517 p->case_labels = $4;
4518 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4519 fixpos($$, $2);
4520 /*% ripper: case!($:2, $:5) %*/
4521 }
4522 | k_case terms?
4523 {
4524 $$ = p->case_labels;
4525 p->case_labels = 0;
4526 }<labels>
4527 case_body
4528 k_end
4529 {
4530 if (p->case_labels) st_free_table(p->case_labels);
4531 p->case_labels = $3;
4532 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4533 /*% ripper: case!(Qnil, $:4) %*/
4534 }
4535 | k_case expr_value terms?
4536 p_case_body
4537 k_end
4538 {
4539 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4540 /*% ripper: case!($:2, $:4) %*/
4541 }
4542 | k_for for_var keyword_in
4543 {COND_PUSH(1);} expr_value do {COND_POP();}
4544 compstmt(stmts)
4545 k_end
4546 {
4547 restore_block_exit(p, $k_for);
4548 /*
4549 * for a, b, c in e
4550 * #=>
4551 * e.each{|*x| a, b, c = x}
4552 *
4553 * for a in e
4554 * #=>
4555 * e.each{|x| a, = x}
4556 */
4557 ID id = internal_id(p);
4558 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4559 rb_node_args_t *args;
4560 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4561 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4562 tbl->ids[0] = id; /* internal id */
4563
4564 switch (nd_type($for_var)) {
4565 case NODE_LASGN:
4566 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4567 set_nd_value(p, $for_var, internal_var);
4568 id = 0;
4569 m->nd_plen = 1;
4570 m->nd_next = $for_var;
4571 break;
4572 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4573 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4574 break;
4575 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4576 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);
4577 }
4578 /* {|*internal_id| <m> = internal_id; ... } */
4579 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4580 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4581 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4582 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4583 RNODE_SCOPE(scope)->nd_parent = $$;
4584 fixpos($$, $for_var);
4585 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4586 }
4587 | k_class cpath superclass
4588 {
4589 begin_definition("class", &@k_class, &@cpath);
4590 }
4591 bodystmt
4592 k_end
4593 {
4594 YYLTYPE inheritance_operator_loc = NULL_LOC;
4595 if ($superclass) {
4596 inheritance_operator_loc = @superclass;
4597 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4598 }
4599 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4600 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4601 set_line_body($bodystmt, @superclass.end_pos.lineno);
4602 nd_set_line($$, @superclass.end_pos.lineno);
4603 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4604 local_pop(p);
4605 p->ctxt.in_class = $k_class.in_class;
4606 p->ctxt.cant_return = $k_class.cant_return;
4607 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4608 }
4609 | k_class tLSHFT expr_value
4610 {
4611 begin_definition("", &@k_class, &@tLSHFT);
4612 }
4613 term
4614 bodystmt
4615 k_end
4616 {
4617 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4618 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4619 set_line_body($bodystmt, nd_line($expr_value));
4620 fixpos($$, $expr_value);
4621 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4622 local_pop(p);
4623 p->ctxt.in_def = $k_class.in_def;
4624 p->ctxt.in_class = $k_class.in_class;
4625 p->ctxt.cant_return = $k_class.cant_return;
4626 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4627 }
4628 | k_module cpath
4629 {
4630 begin_definition("module", &@k_module, &@cpath);
4631 }
4632 bodystmt
4633 k_end
4634 {
4635 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4636 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4637 set_line_body($bodystmt, @cpath.end_pos.lineno);
4638 nd_set_line($$, @cpath.end_pos.lineno);
4639 /*% ripper: module!($:cpath, $:bodystmt) %*/
4640 local_pop(p);
4641 p->ctxt.in_class = $k_module.in_class;
4642 p->ctxt.cant_return = $k_module.cant_return;
4643 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4644 }
4645 | defn_head[head]
4646 f_arglist[args]
4647 {
4648 push_end_expect_token_locations(p, &@head.beg_pos);
4649 }
4650 bodystmt
4651 k_end
4652 {
4653 restore_defun(p, $head);
4654 ($$ = $head->nd_def)->nd_loc = @$;
4655 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4656 RNODE_DEFN($$)->nd_defn = $bodystmt;
4657 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4658 local_pop(p);
4659 }
4660 | defs_head[head]
4661 f_arglist[args]
4662 {
4663 push_end_expect_token_locations(p, &@head.beg_pos);
4664 }
4665 bodystmt
4666 k_end
4667 {
4668 restore_defun(p, $head);
4669 ($$ = $head->nd_def)->nd_loc = @$;
4670 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4671 RNODE_DEFS($$)->nd_defn = $bodystmt;
4672 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4673 local_pop(p);
4674 }
4675 | keyword_break
4676 {
4677 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4678 /*% ripper: break!(args_new!) %*/
4679 }
4680 | keyword_next
4681 {
4682 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4683 /*% ripper: next!(args_new!) %*/
4684 }
4685 | keyword_redo
4686 {
4687 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4688 /*% ripper: redo! %*/
4689 }
4690 | keyword_retry
4691 {
4692 if (!p->ctxt.in_defined) {
4693 switch (p->ctxt.in_rescue) {
4694 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4695 case after_rescue: /* ok */ break;
4696 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4697 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4698 }
4699 }
4700 $$ = NEW_RETRY(&@$);
4701 /*% ripper: retry! %*/
4702 }
4703 ;
4704
4705primary_value : value_expr(primary)
4706 ;
4707
4708k_begin : keyword_begin
4709 {
4710 token_info_push(p, "begin", &@$);
4711 push_end_expect_token_locations(p, &@1.beg_pos);
4712 }
4713 ;
4714
4715k_if : keyword_if
4716 {
4717 WARN_EOL("if");
4718 token_info_push(p, "if", &@$);
4719 if (p->token_info && p->token_info->nonspc &&
4720 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4721 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4722 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4723 beg += rb_strlen_lit("else");
4724 while (beg < tok && ISSPACE(*beg)) beg++;
4725 if (beg == tok) {
4726 p->token_info->nonspc = 0;
4727 }
4728 }
4729 push_end_expect_token_locations(p, &@1.beg_pos);
4730 }
4731 ;
4732
4733k_unless : keyword_unless
4734 {
4735 token_info_push(p, "unless", &@$);
4736 push_end_expect_token_locations(p, &@1.beg_pos);
4737 }
4738 ;
4739
4740k_while : keyword_while allow_exits
4741 {
4742 $$ = $allow_exits;
4743 token_info_push(p, "while", &@$);
4744 push_end_expect_token_locations(p, &@1.beg_pos);
4745 }
4746 ;
4747
4748k_until : keyword_until allow_exits
4749 {
4750 $$ = $allow_exits;
4751 token_info_push(p, "until", &@$);
4752 push_end_expect_token_locations(p, &@1.beg_pos);
4753 }
4754 ;
4755
4756k_case : keyword_case
4757 {
4758 token_info_push(p, "case", &@$);
4759 push_end_expect_token_locations(p, &@1.beg_pos);
4760 }
4761 ;
4762
4763k_for : keyword_for allow_exits
4764 {
4765 $$ = $allow_exits;
4766 token_info_push(p, "for", &@$);
4767 push_end_expect_token_locations(p, &@1.beg_pos);
4768 }
4769 ;
4770
4771k_class : keyword_class
4772 {
4773 token_info_push(p, "class", &@$);
4774 $$ = p->ctxt;
4775 p->ctxt.in_rescue = before_rescue;
4776 push_end_expect_token_locations(p, &@1.beg_pos);
4777 }
4778 ;
4779
4780k_module : keyword_module
4781 {
4782 token_info_push(p, "module", &@$);
4783 $$ = p->ctxt;
4784 p->ctxt.in_rescue = before_rescue;
4785 push_end_expect_token_locations(p, &@1.beg_pos);
4786 }
4787 ;
4788
4789k_def : keyword_def
4790 {
4791 token_info_push(p, "def", &@$);
4792 $$ = NEW_DEF_TEMP(&@$);
4793 p->ctxt.in_argdef = 1;
4794 }
4795 ;
4796
4797k_do : keyword_do
4798 {
4799 token_info_push(p, "do", &@$);
4800 push_end_expect_token_locations(p, &@1.beg_pos);
4801 }
4802 ;
4803
4804k_do_block : keyword_do_block
4805 {
4806 token_info_push(p, "do", &@$);
4807 push_end_expect_token_locations(p, &@1.beg_pos);
4808 }
4809 ;
4810
4811k_rescue : keyword_rescue
4812 {
4813 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4814 $$ = p->ctxt;
4815 p->ctxt.in_rescue = after_rescue;
4816 }
4817 ;
4818
4819k_ensure : keyword_ensure
4820 {
4821 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4822 $$ = p->ctxt;
4823 }
4824 ;
4825
4826k_when : keyword_when
4827 {
4828 token_info_warn(p, "when", p->token_info, 0, &@$);
4829 }
4830 ;
4831
4832k_else : keyword_else
4833 {
4834 token_info *ptinfo_beg = p->token_info;
4835 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4836 token_info_warn(p, "else", p->token_info, same, &@$);
4837 if (same) {
4838 token_info e;
4839 e.next = ptinfo_beg->next;
4840 e.token = "else";
4841 token_info_setup(&e, p->lex.pbeg, &@$);
4842 if (!e.nonspc) *ptinfo_beg = e;
4843 }
4844 }
4845 ;
4846
4847k_elsif : keyword_elsif
4848 {
4849 WARN_EOL("elsif");
4850 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4851 }
4852 ;
4853
4854k_end : keyword_end
4855 {
4856 token_info_pop(p, "end", &@$);
4857 pop_end_expect_token_locations(p);
4858 }
4859 | tDUMNY_END
4860 {
4861 compile_error(p, "syntax error, unexpected end-of-input");
4862 }
4863 ;
4864
4865k_return : keyword_return
4866 {
4867 if (p->ctxt.cant_return && !dyna_in_block(p))
4868 yyerror1(&@1, "Invalid return in class/module body");
4869 }
4870 ;
4871
4872k_yield : keyword_yield
4873 {
4874 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4875 yyerror1(&@1, "Invalid yield");
4876 }
4877 ;
4878
4879then : term
4880 | keyword_then
4881 | term keyword_then
4882 ;
4883
4884do : term
4885 | keyword_do_cond { $$ = keyword_do_cond; }
4886 ;
4887
4888if_tail : opt_else
4889 | k_elsif expr_value then
4890 compstmt(stmts)
4891 if_tail
4892 {
4893 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4894 fixpos($$, $2);
4895 /*% ripper: elsif!($:2, $:4, $:5) %*/
4896 }
4897 ;
4898
4899opt_else : none
4900 | k_else compstmt(stmts)
4901 {
4902 $$ = $2;
4903 /*% ripper: else!($:2) %*/
4904 }
4905 ;
4906
4907for_var : lhs
4908 | mlhs
4909 ;
4910
4911f_marg : f_norm_arg
4912 {
4913 $$ = assignable(p, $1, 0, &@$);
4914 mark_lvar_used(p, $$);
4915 }
4916 | tLPAREN f_margs rparen
4917 {
4918 $$ = (NODE *)$2;
4919 /*% ripper: mlhs_paren!($:2) %*/
4920 }
4921 ;
4922
4923
4924f_margs : mlhs_items(f_marg)
4925 {
4926 $$ = NEW_MASGN($1, 0, &@$);
4927 /*% ripper: $:1 %*/
4928 }
4929 | mlhs_items(f_marg) ',' f_rest_marg
4930 {
4931 $$ = NEW_MASGN($1, $3, &@$);
4932 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4933 }
4934 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4935 {
4936 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4937 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4938 }
4939 | f_rest_marg
4940 {
4941 $$ = NEW_MASGN(0, $1, &@$);
4942 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4943 }
4944 | f_rest_marg ',' mlhs_items(f_marg)
4945 {
4946 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4947 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4948 }
4949 ;
4950
4951f_rest_marg : tSTAR f_norm_arg
4952 {
4953 /*% ripper: $:2 %*/
4954 $$ = assignable(p, $2, 0, &@$);
4955 mark_lvar_used(p, $$);
4956 }
4957 | tSTAR
4958 {
4959 $$ = NODE_SPECIAL_NO_NAME_REST;
4960 /*% ripper: Qnil %*/
4961 }
4962 ;
4963
4964f_any_kwrest : f_kwrest
4965 | f_no_kwarg
4966 {
4967 $$ = idNil;
4968 /*% ripper: ID2VAL(idNil) %*/
4969 }
4970 ;
4971
4972f_eq : {p->ctxt.in_argdef = 0;} '=';
4973
4974block_args_tail : args_tail_basic(primary_value)
4975 ;
4976
4977excessed_comma : ','
4978 {
4979 /* magic number for rest_id in iseq_set_arguments() */
4980 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4981 /*% ripper: excessed_comma! %*/
4982 }
4983 ;
4984
4985block_param : f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
4986 {
4987 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
4988 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
4989 }
4990 | f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4991 {
4992 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4993 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
4994 }
4995 | f_arg ',' f_opt_arg(primary_value) opt_args_tail(block_args_tail)
4996 {
4997 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
4998 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
4999 }
5000 | f_arg ',' f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5001 {
5002 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5003 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5004 }
5005 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5006 {
5007 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5008 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5009 }
5010 | f_arg excessed_comma
5011 {
5012 $$ = new_args_tail(p, 0, 0, 0, &@2);
5013 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5014 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5015 }
5016 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5017 {
5018 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5019 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5020 }
5021 | f_arg opt_args_tail(block_args_tail)
5022 {
5023 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5024 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5025 }
5026 | f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5027 {
5028 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5029 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5030 }
5031 | f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5032 {
5033 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5034 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5035 }
5036 | f_opt_arg(primary_value) opt_args_tail(block_args_tail)
5037 {
5038 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5039 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5040 }
5041 | f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5042 {
5043 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5044 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5045 }
5046 | f_rest_arg opt_args_tail(block_args_tail)
5047 {
5048 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5049 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5050 }
5051 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5052 {
5053 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5054 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5055 }
5056 | block_args_tail
5057 {
5058 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5059 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5060 }
5061 ;
5062
5063opt_block_param_def : none
5064 | block_param_def
5065 {
5066 p->command_start = TRUE;
5067 }
5068 ;
5069
5070block_param_def : '|' opt_block_param opt_bv_decl '|'
5071 {
5072 p->max_numparam = ORDINAL_PARAM;
5073 p->ctxt.in_argdef = 0;
5074 $$ = $2;
5075 /*% ripper: block_var!($:2, $:3) %*/
5076 }
5077 ;
5078
5079opt_block_param : /* none */
5080 {
5081 $$ = 0;
5082 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5083 }
5084 | block_param
5085 ;
5086
5087opt_bv_decl : '\n'?
5088 {
5089 $$ = 0;
5090 /*% ripper: Qfalse %*/
5091 }
5092 | '\n'? ';' bv_decls '\n'?
5093 {
5094 $$ = 0;
5095 /*% ripper: $:3 %*/
5096 }
5097 ;
5098
5099bv_decls : bvar
5100 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5101 | bv_decls ',' bvar
5102 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5103 ;
5104
5105bvar : tIDENTIFIER
5106 {
5107 new_bv(p, $1);
5108 /*% ripper: $:1 %*/
5109 }
5110 | f_bad_arg
5111 ;
5112
5113max_numparam : {
5114 $$ = p->max_numparam;
5115 p->max_numparam = 0;
5116 }
5117 ;
5118
5119numparam : {
5120 $$ = numparam_push(p);
5121 }
5122 ;
5123
5124it_id : {
5125 $$ = p->it_id;
5126 p->it_id = 0;
5127 }
5128 ;
5129
5130lambda : tLAMBDA[lpar]
5131 {
5132 token_info_push(p, "->", &@1);
5133 $$ = dyna_push(p);
5134 }[dyna]<vars>
5135 max_numparam numparam it_id allow_exits
5136 f_larglist[args]
5137 {
5138 CMDARG_PUSH(0);
5139 }
5140 lambda_body[body]
5141 {
5142 int max_numparam = p->max_numparam;
5143 ID it_id = p->it_id;
5144 p->lex.lpar_beg = $lpar;
5145 p->max_numparam = $max_numparam;
5146 p->it_id = $it_id;
5147 restore_block_exit(p, $allow_exits);
5148 CMDARG_POP();
5149 $args = args_with_numbered(p, $args, max_numparam, it_id);
5150 {
5151 YYLTYPE loc = code_loc_gen(&@args, &@body);
5152 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5153 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5154 nd_set_line($$, @args.end_pos.lineno);
5155 nd_set_first_loc($$, @1.beg_pos);
5156 xfree($body);
5157 }
5158 /*% ripper: lambda!($:args, $:body) %*/
5159 numparam_pop(p, $numparam);
5160 dyna_pop(p, $dyna);
5161 }
5162 ;
5163
5164f_larglist : '(' f_args opt_bv_decl ')'
5165 {
5166 p->ctxt.in_argdef = 0;
5167 $$ = $f_args;
5168 p->max_numparam = ORDINAL_PARAM;
5169 /*% ripper: paren!($:2) %*/
5170 }
5171 | f_args
5172 {
5173 p->ctxt.in_argdef = 0;
5174 if (!args_info_empty_p(&$1->nd_ainfo))
5175 p->max_numparam = ORDINAL_PARAM;
5176 $$ = $f_args;
5177 }
5178 ;
5179
5180lambda_body : tLAMBEG compstmt(stmts) '}'
5181 {
5182 token_info_pop(p, "}", &@3);
5183 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5184 /*% ripper: $:2 %*/
5185 }
5186 | keyword_do_LAMBDA
5187 {
5188 push_end_expect_token_locations(p, &@1.beg_pos);
5189 }
5190 bodystmt k_end
5191 {
5192 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5193 /*% ripper: $:3 %*/
5194 }
5195 ;
5196
5197do_block : k_do_block do_body k_end
5198 {
5199 $$ = $2;
5200 set_embraced_location($$, &@1, &@3);
5201 /*% ripper: $:2 %*/
5202 }
5203 ;
5204
5205block_call : command do_block
5206 {
5207 if (nd_type_p($1, NODE_YIELD)) {
5208 compile_error(p, "block given to yield");
5209 }
5210 else {
5211 block_dup_check(p, get_nd_args(p, $1), $2);
5212 }
5213 $$ = method_add_block(p, $1, $2, &@$);
5214 fixpos($$, $1);
5215 /*% ripper: method_add_block!($:1, $:2) %*/
5216 }
5217 | block_call call_op2 operation2 opt_paren_args
5218 {
5219 bool has_args = $4 != 0;
5220 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5221 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5222 /*% ripper: call!($:1, $:2, $:3) %*/
5223 if (has_args) {
5224 /*% ripper: method_add_arg!($:$, $:4) %*/
5225 }
5226 }
5227 | block_call call_op2 operation2 opt_paren_args brace_block
5228 {
5229 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5230 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5231 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5232 }
5233 | block_call call_op2 operation2 command_args do_block
5234 {
5235 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5236 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5237 }
5238 ;
5239
5240method_call : fcall paren_args
5241 {
5242 $1->nd_args = $2;
5243 $$ = (NODE *)$1;
5244 nd_set_last_loc($1, @2.end_pos);
5245 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5246 }
5247 | primary_value call_op operation2 opt_paren_args
5248 {
5249 bool has_args = $4 != 0;
5250 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5251 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5252 nd_set_line($$, @3.end_pos.lineno);
5253 /*% ripper: call!($:1, $:2, $:3) %*/
5254 if (has_args) {
5255 /*% ripper: method_add_arg!($:$, $:4) %*/
5256 }
5257 }
5258 | primary_value tCOLON2 operation2 paren_args
5259 {
5260 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5261 nd_set_line($$, @3.end_pos.lineno);
5262 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5263 }
5264 | primary_value tCOLON2 operation3
5265 {
5266 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5267 /*% ripper: call!($:1, $:2, $:3) %*/
5268 }
5269 | primary_value call_op2 paren_args
5270 {
5271 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5272 nd_set_line($$, @2.end_pos.lineno);
5273 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5274 }
5275 | keyword_super paren_args
5276 {
5277 rb_code_location_t lparen_loc = @2;
5278 rb_code_location_t rparen_loc = @2;
5279 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5280 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5281
5282 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5283 /*% ripper: super!($:2) %*/
5284 }
5285 | keyword_super
5286 {
5287 $$ = NEW_ZSUPER(&@$);
5288 /*% ripper: zsuper! %*/
5289 }
5290 | primary_value '[' opt_call_args rbracket
5291 {
5292 $$ = NEW_CALL($1, tAREF, $3, &@$);
5293 fixpos($$, $1);
5294 /*% ripper: aref!($:1, $:3) %*/
5295 }
5296 ;
5297
5298brace_block : '{' brace_body '}'
5299 {
5300 $$ = $2;
5301 set_embraced_location($$, &@1, &@3);
5302 /*% ripper: $:2 %*/
5303 }
5304 | k_do do_body k_end
5305 {
5306 $$ = $2;
5307 set_embraced_location($$, &@1, &@3);
5308 /*% ripper: $:2 %*/
5309 }
5310 ;
5311
5312brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5313 max_numparam numparam it_id allow_exits
5314 opt_block_param_def[args] compstmt(stmts)
5315 {
5316 int max_numparam = p->max_numparam;
5317 ID it_id = p->it_id;
5318 p->max_numparam = $max_numparam;
5319 p->it_id = $it_id;
5320 $args = args_with_numbered(p, $args, max_numparam, it_id);
5321 $$ = NEW_ITER($args, $compstmt, &@$);
5322 /*% ripper: brace_block!($:args, $:compstmt) %*/
5323 restore_block_exit(p, $allow_exits);
5324 numparam_pop(p, $numparam);
5325 dyna_pop(p, $dyna);
5326 }
5327 ;
5328
5329do_body : {
5330 $$ = dyna_push(p);
5331 CMDARG_PUSH(0);
5332 }[dyna]<vars>
5333 max_numparam numparam it_id allow_exits
5334 opt_block_param_def[args] bodystmt
5335 {
5336 int max_numparam = p->max_numparam;
5337 ID it_id = p->it_id;
5338 p->max_numparam = $max_numparam;
5339 p->it_id = $it_id;
5340 $args = args_with_numbered(p, $args, max_numparam, it_id);
5341 $$ = NEW_ITER($args, $bodystmt, &@$);
5342 /*% ripper: do_block!($:args, $:bodystmt) %*/
5343 CMDARG_POP();
5344 restore_block_exit(p, $allow_exits);
5345 numparam_pop(p, $numparam);
5346 dyna_pop(p, $dyna);
5347 }
5348 ;
5349
5350case_args : arg_value
5351 {
5352 check_literal_when(p, $arg_value, &@arg_value);
5353 $$ = NEW_LIST($arg_value, &@$);
5354 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5355 }
5356 | tSTAR arg_value
5357 {
5358 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5359 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5360 }
5361 | case_args[non_last_args] ',' arg_value
5362 {
5363 check_literal_when(p, $arg_value, &@arg_value);
5364 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5365 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5366 }
5367 | case_args[non_last_args] ',' tSTAR arg_value
5368 {
5369 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5370 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5371 }
5372 ;
5373
5374case_body : k_when case_args then
5375 compstmt(stmts)
5376 cases
5377 {
5378 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5379 fixpos($$, $2);
5380 /*% ripper: when!($:2, $:4, $:5) %*/
5381 }
5382 ;
5383
5384cases : opt_else
5385 | case_body
5386 ;
5387
5388p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5389p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5390
5391p_in_kwarg : {
5392 $$ = p->ctxt;
5393 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5394 p->command_start = FALSE;
5395 p->ctxt.in_kwarg = 1;
5396 p->ctxt.in_alt_pattern = 0;
5397 p->ctxt.capture_in_pattern = 0;
5398 }
5399 ;
5400
5401p_case_body : keyword_in
5402 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5403 p_top_expr[expr] then
5404 {
5405 pop_pktbl(p, $p_pktbl);
5406 pop_pvtbl(p, $p_pvtbl);
5407 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5408 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5409 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5410 }
5411 compstmt(stmts)
5412 p_cases[cases]
5413 {
5414 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5415 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5416 }
5417 ;
5418
5419p_cases : opt_else
5420 | p_case_body
5421 ;
5422
5423p_top_expr : p_top_expr_body
5424 | p_top_expr_body modifier_if expr_value
5425 {
5426 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5427 fixpos($$, $3);
5428 /*% ripper: if_mod!($:3, $:1) %*/
5429 }
5430 | p_top_expr_body modifier_unless expr_value
5431 {
5432 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5433 fixpos($$, $3);
5434 /*% ripper: unless_mod!($:3, $:1) %*/
5435 }
5436 ;
5437
5438p_top_expr_body : p_expr
5439 | p_expr ','
5440 {
5441 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5442 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5443 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5444 }
5445 | p_expr ',' p_args
5446 {
5447 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5448 nd_set_first_loc($$, @1.beg_pos);
5449 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5450 }
5451 | p_find
5452 {
5453 $$ = new_find_pattern(p, 0, $1, &@$);
5454 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5455 }
5456 | p_args_tail
5457 {
5458 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5459 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5460 }
5461 | p_kwargs
5462 {
5463 $$ = new_hash_pattern(p, 0, $1, &@$);
5464 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5465 }
5466 ;
5467
5468p_expr : p_as
5469 ;
5470
5471p_as : p_expr tASSOC p_variable
5472 {
5473 NODE *n = NEW_LIST($1, &@$);
5474 n = list_append(p, n, $3);
5475 $$ = new_hash(p, n, &@$);
5476 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5477 }
5478 | p_alt
5479 ;
5480
5481p_alt : p_alt[left] '|'[alt]
5482 {
5483 p->ctxt.in_alt_pattern = 1;
5484 }
5485 p_expr_basic[right]
5486 {
5487 if (p->ctxt.capture_in_pattern) {
5488 yyerror1(&@alt, "alternative pattern after variable capture");
5489 }
5490 p->ctxt.in_alt_pattern = 0;
5491 $$ = NEW_OR($left, $right, &@$, &@alt);
5492 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5493 }
5494 | p_expr_basic
5495 ;
5496
5497p_lparen : '(' p_pktbl
5498 {
5499 $$ = $2;
5500 /*% ripper: $:2 %*/
5501 }
5502 ;
5503
5504p_lbracket : '[' p_pktbl
5505 {
5506 $$ = $2;
5507 /*% ripper: $:2 %*/
5508 }
5509 ;
5510
5511p_expr_basic : p_value
5512 | p_variable
5513 | p_const p_lparen[p_pktbl] p_args rparen
5514 {
5515 pop_pktbl(p, $p_pktbl);
5516 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5517 nd_set_first_loc($$, @p_const.beg_pos);
5518 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5519 }
5520 | p_const p_lparen[p_pktbl] p_find rparen
5521 {
5522 pop_pktbl(p, $p_pktbl);
5523 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5524 nd_set_first_loc($$, @p_const.beg_pos);
5525 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5526 }
5527 | p_const p_lparen[p_pktbl] p_kwargs rparen
5528 {
5529 pop_pktbl(p, $p_pktbl);
5530 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5531 nd_set_first_loc($$, @p_const.beg_pos);
5532 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5533 }
5534 | p_const '(' rparen
5535 {
5536 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5537 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5538 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5539 }
5540 | p_const p_lbracket[p_pktbl] p_args rbracket
5541 {
5542 pop_pktbl(p, $p_pktbl);
5543 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5544 nd_set_first_loc($$, @p_const.beg_pos);
5545 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5546 }
5547 | p_const p_lbracket[p_pktbl] p_find rbracket
5548 {
5549 pop_pktbl(p, $p_pktbl);
5550 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5551 nd_set_first_loc($$, @p_const.beg_pos);
5552 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5553 }
5554 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5555 {
5556 pop_pktbl(p, $p_pktbl);
5557 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5558 nd_set_first_loc($$, @p_const.beg_pos);
5559 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5560 }
5561 | p_const '[' rbracket
5562 {
5563 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5564 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5565 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5566 }
5567 | tLBRACK p_args rbracket
5568 {
5569 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5570 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5571 }
5572 | tLBRACK p_find rbracket
5573 {
5574 $$ = new_find_pattern(p, 0, $p_find, &@$);
5575 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5576 }
5577 | tLBRACK rbracket
5578 {
5579 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5580 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5581 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5582 }
5583 | tLBRACE p_pktbl lex_ctxt[ctxt]
5584 {
5585 p->ctxt.in_kwarg = 0;
5586 }
5587 p_kwargs rbrace
5588 {
5589 pop_pktbl(p, $p_pktbl);
5590 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5591 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5592 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5593 }
5594 | tLBRACE rbrace
5595 {
5596 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5597 $$ = new_hash_pattern(p, 0, $$, &@$);
5598 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5599 }
5600 | tLPAREN p_pktbl p_expr rparen
5601 {
5602 pop_pktbl(p, $p_pktbl);
5603 $$ = $p_expr;
5604 /*% ripper: $:p_expr %*/
5605 }
5606 ;
5607
5608p_args : p_expr
5609 {
5610 NODE *pre_args = NEW_LIST($1, &@$);
5611 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5612 /*% ripper: [[$:1], Qnil, Qnil] %*/
5613 }
5614 | p_args_head
5615 {
5616 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5617 /*% ripper: [$:1, Qnil, Qnil] %*/
5618 }
5619 | p_args_head p_arg
5620 {
5621 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5622 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5623 }
5624 | p_args_head p_rest
5625 {
5626 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5627 /*% ripper: [$:1, $:2, Qnil] %*/
5628 }
5629 | p_args_head p_rest ',' p_args_post
5630 {
5631 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5632 /*% ripper: [$:1, $:2, $:4] %*/
5633 }
5634 | p_args_tail
5635 ;
5636
5637p_args_head : p_arg ','
5638 | p_args_head p_arg ','
5639 {
5640 $$ = list_concat($1, $2);
5641 /*% ripper: rb_ary_concat($:1, $:2) %*/
5642 }
5643 ;
5644
5645p_args_tail : p_rest
5646 {
5647 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5648 /*% ripper: [Qnil, $:1, Qnil] %*/
5649 }
5650 | p_rest ',' p_args_post
5651 {
5652 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5653 /*% ripper: [Qnil, $:1, $:3] %*/
5654 }
5655 ;
5656
5657p_find : p_rest ',' p_args_post ',' p_rest
5658 {
5659 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5660 /*% ripper: [$:1, $:3, $:5] %*/
5661 }
5662 ;
5663
5664
5665p_rest : tSTAR tIDENTIFIER
5666 {
5667 error_duplicate_pattern_variable(p, $2, &@2);
5668 /*% ripper: var_field!($:2) %*/
5669 $$ = assignable(p, $2, 0, &@$);
5670 }
5671 | tSTAR
5672 {
5673 $$ = 0;
5674 /*% ripper: var_field!(Qnil) %*/
5675 }
5676 ;
5677
5678p_args_post : p_arg
5679 | p_args_post ',' p_arg
5680 {
5681 $$ = list_concat($1, $3);
5682 /*% ripper: rb_ary_concat($:1, $:3) %*/
5683 }
5684 ;
5685
5686p_arg : p_expr
5687 {
5688 $$ = NEW_LIST($1, &@$);
5689 /*% ripper: [$:1] %*/
5690 }
5691 ;
5692
5693p_kwargs : p_kwarg ',' p_any_kwrest
5694 {
5695 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5696 /*% ripper: [$:1, $:3] %*/
5697 }
5698 | p_kwarg
5699 {
5700 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5701 /*% ripper: [$:1, Qnil] %*/
5702 }
5703 | p_kwarg ','
5704 {
5705 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5706 /*% ripper: [$:1, Qnil] %*/
5707 }
5708 | p_any_kwrest
5709 {
5710 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5711 /*% ripper: [[], $:1] %*/
5712 }
5713 ;
5714
5715p_kwarg : p_kw
5716 /*% ripper[brace]: [$:1] %*/
5717 | p_kwarg ',' p_kw
5718 {
5719 $$ = list_concat($1, $3);
5720 /*% ripper: rb_ary_push($:1, $:3) %*/
5721 }
5722 ;
5723
5724p_kw : p_kw_label p_expr
5725 {
5726 error_duplicate_pattern_key(p, $1, &@1);
5727 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5728 /*% ripper: [$:1, $:2] %*/
5729 }
5730 | p_kw_label
5731 {
5732 error_duplicate_pattern_key(p, $1, &@1);
5733 if ($1 && !is_local_id($1)) {
5734 yyerror1(&@1, "key must be valid as local variables");
5735 }
5736 error_duplicate_pattern_variable(p, $1, &@1);
5737 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5738 /*% ripper: [$:1, Qnil] %*/
5739 }
5740 ;
5741
5742p_kw_label : tLABEL
5743 | tSTRING_BEG string_contents tLABEL_END
5744 {
5745 YYLTYPE loc = code_loc_gen(&@1, &@3);
5746 if (!$2 || nd_type_p($2, NODE_STR)) {
5747 NODE *node = dsym_node(p, $2, &loc);
5748 $$ = rb_sym2id(rb_node_sym_string_val(node));
5749 }
5750 else {
5751 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5752 $$ = rb_intern_str(STR_NEW0());
5753 }
5754 /*% ripper: $:2 %*/
5755 }
5756 ;
5757
5758p_kwrest : kwrest_mark tIDENTIFIER
5759 {
5760 $$ = $2;
5761 /*% ripper: var_field!($:2) %*/
5762 }
5763 | kwrest_mark
5764 {
5765 $$ = 0;
5766 /*% ripper: Qnil %*/
5767 }
5768 ;
5769
5770p_kwnorest : kwrest_mark keyword_nil
5771 {
5772 $$ = 0;
5773 }
5774 ;
5775
5776p_any_kwrest : p_kwrest
5777 | p_kwnorest
5778 {
5779 $$ = idNil;
5780 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5781 }
5782 ;
5783
5784p_value : p_primitive
5785 | range_expr(p_primitive)
5786 | p_var_ref
5787 | p_expr_ref
5788 | p_const
5789 ;
5790
5791p_primitive : inline_primary
5792 | keyword_variable
5793 {
5794 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5795 /*% ripper: var_ref!($:1) %*/
5796 }
5797 | lambda
5798 ;
5799
5800p_variable : tIDENTIFIER
5801 {
5802 error_duplicate_pattern_variable(p, $1, &@1);
5803 /*% ripper: var_field!($:1) %*/
5804 $$ = assignable(p, $1, 0, &@$);
5805 }
5806 ;
5807
5808p_var_ref : '^' tIDENTIFIER
5809 {
5810 NODE *n = gettable(p, $2, &@$);
5811 if (!n) {
5812 n = NEW_ERROR(&@$);
5813 }
5814 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5815 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5816 }
5817 $$ = n;
5818 /*% ripper: var_ref!($:2) %*/
5819 }
5820 | '^' nonlocal_var
5821 {
5822 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5823 /*% ripper: var_ref!($:2) %*/
5824 }
5825 ;
5826
5827p_expr_ref : '^' tLPAREN expr_value rparen
5828 {
5829 $$ = NEW_BLOCK($3, &@$);
5830 /*% ripper: begin!($:3) %*/
5831 }
5832 ;
5833
5834p_const : tCOLON3 cname
5835 {
5836 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5837 /*% ripper: top_const_ref!($:2) %*/
5838 }
5839 | p_const tCOLON2 cname
5840 {
5841 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5842 /*% ripper: const_path_ref!($:1, $:3) %*/
5843 }
5844 | tCONSTANT
5845 {
5846 $$ = gettable(p, $1, &@$);
5847 /*% ripper: var_ref!($:1) %*/
5848 }
5849 ;
5850
5851opt_rescue : k_rescue exc_list exc_var then
5852 compstmt(stmts)
5853 opt_rescue
5854 {
5855 NODE *err = $3;
5856 if ($3) {
5857 err = NEW_ERRINFO(&@3);
5858 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5859 }
5860 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5861 if ($2) {
5862 fixpos($$, $2);
5863 }
5864 else if ($3) {
5865 fixpos($$, $3);
5866 }
5867 else {
5868 fixpos($$, $5);
5869 }
5870 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5871 }
5872 | none
5873 ;
5874
5875exc_list : arg_value
5876 {
5877 $$ = NEW_LIST($1, &@$);
5878 /*% ripper: rb_ary_new3(1, $:1) %*/
5879 }
5880 | mrhs
5881 {
5882 if (!($$ = splat_array($1))) $$ = $1;
5883 }
5884 | none
5885 ;
5886
5887exc_var : tASSOC lhs
5888 {
5889 $$ = $2;
5890 /*% ripper: $:2 %*/
5891 }
5892 | none
5893 ;
5894
5895opt_ensure : k_ensure stmts terms?
5896 {
5897 p->ctxt.in_rescue = $1.in_rescue;
5898 $$ = $2;
5899 void_expr(p, void_stmts(p, $$));
5900 /*% ripper: ensure!($:2) %*/
5901 }
5902 | none
5903 ;
5904
5905literal : numeric
5906 | symbol
5907 ;
5908
5909strings : string
5910 {
5911 if (!$1) {
5912 $$ = NEW_STR(STRING_NEW0(), &@$);
5913 }
5914 else {
5915 $$ = evstr2dstr(p, $1);
5916 }
5917 /*% ripper: $:1 %*/
5918 }
5919 ;
5920
5921string : tCHAR
5922 | string1
5923 | string string1
5924 {
5925 $$ = literal_concat(p, $1, $2, &@$);
5926 /*% ripper: string_concat!($:1, $:2) %*/
5927 }
5928 ;
5929
5930string1 : tSTRING_BEG string_contents tSTRING_END
5931 {
5932 $$ = heredoc_dedent(p, $2);
5933 if ($$) nd_set_loc($$, &@$);
5934 /*% ripper: $:2 %*/
5935 if (p->heredoc_indent > 0) {
5936 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5937 p->heredoc_indent = 0;
5938 }
5939 /*% ripper: string_literal!($:$) %*/
5940 }
5941 ;
5942
5943xstring : tXSTRING_BEG xstring_contents tSTRING_END
5944 {
5945 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5946 /*% ripper: $:2 %*/
5947 if (p->heredoc_indent > 0) {
5948 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5949 p->heredoc_indent = 0;
5950 }
5951 /*% ripper: xstring_literal!($:$) %*/
5952 }
5953 ;
5954
5955regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5956 {
5957 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5958 /*% ripper: regexp_literal!($:2, $:3) %*/
5959 }
5960 ;
5961
5962words : words(tWORDS_BEG, word_list)
5963 ;
5964
5965word_list : /* none */
5966 {
5967 $$ = 0;
5968 /*% ripper: words_new! %*/
5969 }
5970 | word_list word ' '+
5971 {
5972 $$ = list_append(p, $1, evstr2dstr(p, $2));
5973 /*% ripper: words_add!($:1, $:2) %*/
5974 }
5975 ;
5976
5977word : string_content
5978 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5979 | word string_content
5980 {
5981 $$ = literal_concat(p, $1, $2, &@$);
5982 /*% ripper: word_add!($:1, $:2) %*/
5983 }
5984 ;
5985
5986symbols : words(tSYMBOLS_BEG, symbol_list)
5987 ;
5988
5989symbol_list : /* none */
5990 {
5991 $$ = 0;
5992 /*% ripper: symbols_new! %*/
5993 }
5994 | symbol_list word ' '+
5995 {
5996 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5997 /*% ripper: symbols_add!($:1, $:2) %*/
5998 }
5999 ;
6000
6001qwords : words(tQWORDS_BEG, qword_list)
6002 ;
6003
6004qsymbols : words(tQSYMBOLS_BEG, qsym_list)
6005 ;
6006
6007qword_list : /* none */
6008 {
6009 $$ = 0;
6010 /*% ripper: qwords_new! %*/
6011 }
6012 | qword_list tSTRING_CONTENT ' '+
6013 {
6014 $$ = list_append(p, $1, $2);
6015 /*% ripper: qwords_add!($:1, $:2) %*/
6016 }
6017 ;
6018
6019qsym_list : /* none */
6020 {
6021 $$ = 0;
6022 /*% ripper: qsymbols_new! %*/
6023 }
6024 | qsym_list tSTRING_CONTENT ' '+
6025 {
6026 $$ = symbol_append(p, $1, $2);
6027 /*% ripper: qsymbols_add!($:1, $:2) %*/
6028 }
6029 ;
6030
6031string_contents : /* none */
6032 {
6033 $$ = 0;
6034 /*% ripper: string_content! %*/
6035 }
6036 | string_contents string_content
6037 {
6038 $$ = literal_concat(p, $1, $2, &@$);
6039 /*% ripper: string_add!($:1, $:2) %*/
6040 }
6041 ;
6042
6043xstring_contents: /* none */
6044 {
6045 $$ = 0;
6046 /*% ripper: xstring_new! %*/
6047 }
6048 | xstring_contents string_content
6049 {
6050 $$ = literal_concat(p, $1, $2, &@$);
6051 /*% ripper: xstring_add!($:1, $:2) %*/
6052 }
6053 ;
6054
6055regexp_contents: /* none */
6056 {
6057 $$ = 0;
6058 /*% ripper: regexp_new! %*/
6059 }
6060 | regexp_contents string_content
6061 {
6062 NODE *head = $1, *tail = $2;
6063 if (!head) {
6064 $$ = tail;
6065 }
6066 else if (!tail) {
6067 $$ = head;
6068 }
6069 else {
6070 switch (nd_type(head)) {
6071 case NODE_STR:
6072 head = str2dstr(p, head);
6073 break;
6074 case NODE_DSTR:
6075 break;
6076 default:
6077 head = list_append(p, NEW_DSTR(0, &@$), head);
6078 break;
6079 }
6080 $$ = list_append(p, head, tail);
6081 }
6082 /*% ripper: regexp_add!($:1, $:2) %*/
6083 }
6084 ;
6085
6086string_content : tSTRING_CONTENT
6087 /*% ripper[brace]: $:1 %*/
6088 | tSTRING_DVAR
6089 {
6090 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6091 $$ = p->lex.strterm;
6092 p->lex.strterm = 0;
6093 SET_LEX_STATE(EXPR_BEG);
6094 }<strterm>
6095 string_dvar
6096 {
6097 p->lex.strterm = $2;
6098 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6099 nd_set_line($$, @3.end_pos.lineno);
6100 /*% ripper: string_dvar!($:3) %*/
6101 }
6102 | tSTRING_DBEG[state]
6103 {
6104 CMDARG_PUSH(0);
6105 COND_PUSH(0);
6106 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6107 $$ = p->lex.strterm;
6108 p->lex.strterm = 0;
6109 SET_LEX_STATE(EXPR_BEG);
6110 }[term]<strterm>
6111 {
6112 $$ = p->lex.brace_nest;
6113 p->lex.brace_nest = 0;
6114 }[brace]<num>
6115 {
6116 $$ = p->heredoc_indent;
6117 p->heredoc_indent = 0;
6118 }[indent]<num>
6119 compstmt(stmts) string_dend
6120 {
6121 COND_POP();
6122 CMDARG_POP();
6123 p->lex.strterm = $term;
6124 SET_LEX_STATE($state);
6125 p->lex.brace_nest = $brace;
6126 p->heredoc_indent = $indent;
6127 p->heredoc_line_indent = -1;
6128 if ($compstmt) nd_unset_fl_newline($compstmt);
6129 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6130 /*% ripper: string_embexpr!($:compstmt) %*/
6131 }
6132 ;
6133
6134string_dend : tSTRING_DEND
6135 | END_OF_INPUT
6136 ;
6137
6138string_dvar : nonlocal_var
6139 {
6140 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6141 /*% ripper: var_ref!($:1) %*/
6142 }
6143 | backref
6144 ;
6145
6146symbol : ssym
6147 | dsym
6148 ;
6149
6150ssym : tSYMBEG sym
6151 {
6152 SET_LEX_STATE(EXPR_END);
6153 VALUE str = rb_id2str($2);
6154 /*
6155 * TODO:
6156 * set_yylval_noname sets invalid id to yylval.
6157 * This branch can be removed once yylval is changed to
6158 * hold lexed string.
6159 */
6160 if (!str) str = STR_NEW0();
6161 $$ = NEW_SYM(str, &@$);
6162 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6163 }
6164 ;
6165
6166sym : fname
6167 | nonlocal_var
6168 ;
6169
6170dsym : tSYMBEG string_contents tSTRING_END
6171 {
6172 SET_LEX_STATE(EXPR_END);
6173 $$ = dsym_node(p, $2, &@$);
6174 /*% ripper: dyna_symbol!($:2) %*/
6175 }
6176 ;
6177
6178numeric : simple_numeric
6179 | tUMINUS_NUM simple_numeric %prec tLOWEST
6180 {
6181 $$ = $2;
6182 negate_lit(p, $$);
6183 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6184 }
6185 ;
6186
6187simple_numeric : tINTEGER
6188 | tFLOAT
6189 | tRATIONAL
6190 | tIMAGINARY
6191 ;
6192
6193nonlocal_var : tIVAR
6194 | tGVAR
6195 | tCVAR
6196 ;
6197
6198user_variable : ident_or_const
6199 | nonlocal_var
6200 ;
6201
6202keyword_variable : keyword_nil {$$ = KWD2EID(nil, $1);}
6203 | keyword_self {$$ = KWD2EID(self, $1);}
6204 | keyword_true {$$ = KWD2EID(true, $1);}
6205 | keyword_false {$$ = KWD2EID(false, $1);}
6206 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6207 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6208 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6209 ;
6210
6211var_ref : user_variable
6212 {
6213 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6214 if (ifdef_ripper(id_is_var(p, $1), false)) {
6215 /*% ripper: var_ref!($:1) %*/
6216 }
6217 else {
6218 /*% ripper: vcall!($:1) %*/
6219 }
6220 }
6221 | keyword_variable
6222 {
6223 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6224 /*% ripper: var_ref!($:1) %*/
6225 }
6226 ;
6227
6228var_lhs : user_or_keyword_variable
6229 {
6230 /*% ripper: var_field!($:1) %*/
6231 $$ = assignable(p, $1, 0, &@$);
6232 }
6233 ;
6234
6235backref : tNTH_REF
6236 | tBACK_REF
6237 ;
6238
6239superclass : '<'
6240 {
6241 SET_LEX_STATE(EXPR_BEG);
6242 p->command_start = TRUE;
6243 }
6244 expr_value term
6245 {
6246 $$ = $3;
6247 /*% ripper: $:3 %*/
6248 }
6249 | none
6250 ;
6251
6252f_opt_paren_args: f_paren_args
6253 | none
6254 {
6255 p->ctxt.in_argdef = 0;
6256 $$ = new_args_tail(p, 0, 0, 0, &@0);
6257 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6258 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6259 }
6260 ;
6261
6262f_paren_args : '(' f_args rparen
6263 {
6264 $$ = $2;
6265 /*% ripper: paren!($:2) %*/
6266 SET_LEX_STATE(EXPR_BEG);
6267 p->command_start = TRUE;
6268 p->ctxt.in_argdef = 0;
6269 }
6270 ;
6271
6272f_arglist : f_paren_args
6273 | {
6274 $$ = p->ctxt;
6275 p->ctxt.in_kwarg = 1;
6276 p->ctxt.in_argdef = 1;
6277 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6278 }<ctxt>
6279 f_args term
6280 {
6281 p->ctxt.in_kwarg = $1.in_kwarg;
6282 p->ctxt.in_argdef = 0;
6283 $$ = $2;
6284 SET_LEX_STATE(EXPR_BEG);
6285 p->command_start = TRUE;
6286 /*% ripper: $:2 %*/
6287 }
6288 ;
6289
6290args_tail : args_tail_basic(arg_value)
6291 | args_forward
6292 {
6293 ID fwd = $args_forward;
6294 if (lambda_beginning_p() ||
6295 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6296 yyerror0("unexpected ... in lambda argument");
6297 fwd = 0;
6298 }
6299 else {
6300 add_forwarding_args(p);
6301 }
6302 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6303 $$->nd_ainfo.forwarding = 1;
6304 /*% ripper: [Qnil, $:1, Qnil] %*/
6305 }
6306 ;
6307
6308f_args : f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6309 {
6310 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6311 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6312 }
6313 | f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6314 {
6315 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6316 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6317 }
6318 | f_arg ',' f_opt_arg(arg_value) opt_args_tail(args_tail)
6319 {
6320 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6321 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6322 }
6323 | f_arg ',' f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6324 {
6325 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6326 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6327 }
6328 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6329 {
6330 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6331 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6332 }
6333 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6334 {
6335 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6336 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6337 }
6338 | f_arg opt_args_tail(args_tail)
6339 {
6340 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6341 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6342 }
6343 | f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6344 {
6345 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6346 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6347 }
6348 | f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6349 {
6350 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6351 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6352 }
6353 | f_opt_arg(arg_value) opt_args_tail(args_tail)
6354 {
6355 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6356 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6357 }
6358 | f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6359 {
6360 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6361 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6362 }
6363 | f_rest_arg opt_args_tail(args_tail)
6364 {
6365 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6366 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6367 }
6368 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6369 {
6370 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6371 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6372 }
6373 | args_tail
6374 {
6375 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6376 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6377 }
6378 | /* none */
6379 {
6380 $$ = new_args_tail(p, 0, 0, 0, &@0);
6381 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6382 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6383 }
6384 ;
6385
6386args_forward : tBDOT3
6387 {
6388#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6389 $$ = 0;
6390#else
6391 $$ = idFWD_KWREST;
6392#endif
6393 /*% ripper: args_forward! %*/
6394 }
6395 ;
6396
6397f_bad_arg : tCONSTANT
6398 {
6399 static const char mesg[] = "formal argument cannot be a constant";
6400 /*%%%*/
6401 yyerror1(&@1, mesg);
6402 /*% %*/
6403 $$ = 0;
6404 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6405 }
6406 | tIVAR
6407 {
6408 static const char mesg[] = "formal argument cannot be an instance variable";
6409 /*%%%*/
6410 yyerror1(&@1, mesg);
6411 /*% %*/
6412 $$ = 0;
6413 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6414 }
6415 | tGVAR
6416 {
6417 static const char mesg[] = "formal argument cannot be a global variable";
6418 /*%%%*/
6419 yyerror1(&@1, mesg);
6420 /*% %*/
6421 $$ = 0;
6422 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6423 }
6424 | tCVAR
6425 {
6426 static const char mesg[] = "formal argument cannot be a class variable";
6427 /*%%%*/
6428 yyerror1(&@1, mesg);
6429 /*% %*/
6430 $$ = 0;
6431 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6432 }
6433 ;
6434
6435f_norm_arg : f_bad_arg
6436 | tIDENTIFIER
6437 {
6438 VALUE e = formal_argument_error(p, $$ = $1);
6439 if (e) {
6440 /*% ripper[error]: param_error!(?e, $:1) %*/
6441 }
6442 p->max_numparam = ORDINAL_PARAM;
6443 }
6444 ;
6445
6446f_arg_asgn : f_norm_arg
6447 {
6448 arg_var(p, $1);
6449 $$ = $1;
6450 }
6451 ;
6452
6453f_arg_item : f_arg_asgn
6454 {
6455 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6456 /*% ripper: $:1 %*/
6457 }
6458 | tLPAREN f_margs rparen
6459 {
6460 ID tid = internal_id(p);
6461 YYLTYPE loc;
6462 loc.beg_pos = @2.beg_pos;
6463 loc.end_pos = @2.beg_pos;
6464 arg_var(p, tid);
6465 if (dyna_in_block(p)) {
6466 $2->nd_value = NEW_DVAR(tid, &loc);
6467 }
6468 else {
6469 $2->nd_value = NEW_LVAR(tid, &loc);
6470 }
6471 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6472 $$->nd_next = (NODE *)$2;
6473 /*% ripper: mlhs_paren!($:2) %*/
6474 }
6475 ;
6476
6477f_arg : f_arg_item
6478 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6479 | f_arg ',' f_arg_item
6480 {
6481 $$ = $1;
6482 $$->nd_plen++;
6483 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6484 rb_discard_node(p, (NODE *)$3);
6485 /*% ripper: rb_ary_push($:1, $:3) %*/
6486 }
6487 ;
6488
6489
6490f_label : tLABEL
6491 {
6492 VALUE e = formal_argument_error(p, $$ = $1);
6493 if (e) {
6494 $$ = 0;
6495 /*% ripper[error]: param_error!(?e, $:1) %*/
6496 }
6497 /*
6498 * Workaround for Prism::ParseTest#test_filepath for
6499 * "unparser/corpus/literal/def.txt"
6500 *
6501 * See the discussion on https://github.com/ruby/ruby/pull/9923
6502 */
6503 arg_var(p, ifdef_ripper(0, $1));
6504 /*% ripper: $:1 %*/
6505 p->max_numparam = ORDINAL_PARAM;
6506 p->ctxt.in_argdef = 0;
6507 }
6508 ;
6509
6510kwrest_mark : tPOW
6511 | tDSTAR
6512 ;
6513
6514f_no_kwarg : p_kwnorest
6515 {
6516 /*% ripper: nokw_param!(Qnil) %*/
6517 }
6518 ;
6519
6520f_kwrest : kwrest_mark tIDENTIFIER
6521 {
6522 arg_var(p, shadowing_lvar(p, $2));
6523 $$ = $2;
6524 /*% ripper: kwrest_param!($:2) %*/
6525 }
6526 | kwrest_mark
6527 {
6528 arg_var(p, idFWD_KWREST);
6529 $$ = idFWD_KWREST;
6530 /*% ripper: kwrest_param!(Qnil) %*/
6531 }
6532 ;
6533
6534restarg_mark : '*'
6535 | tSTAR
6536 ;
6537
6538f_rest_arg : restarg_mark tIDENTIFIER
6539 {
6540 arg_var(p, shadowing_lvar(p, $2));
6541 $$ = $2;
6542 /*% ripper: rest_param!($:2) %*/
6543 }
6544 | restarg_mark
6545 {
6546 arg_var(p, idFWD_REST);
6547 $$ = idFWD_REST;
6548 /*% ripper: rest_param!(Qnil) %*/
6549 }
6550 ;
6551
6552blkarg_mark : '&'
6553 | tAMPER
6554 ;
6555
6556f_block_arg : blkarg_mark tIDENTIFIER
6557 {
6558 arg_var(p, shadowing_lvar(p, $2));
6559 $$ = $2;
6560 /*% ripper: blockarg!($:2) %*/
6561 }
6562 | blkarg_mark
6563 {
6564 arg_var(p, idFWD_BLOCK);
6565 $$ = idFWD_BLOCK;
6566 /*% ripper: blockarg!(Qnil) %*/
6567 }
6568 ;
6569
6570opt_f_block_arg : ',' f_block_arg
6571 {
6572 $$ = $2;
6573 /*% ripper: $:2 %*/
6574 }
6575 | none
6576 ;
6577
6578
6579singleton : value_expr(singleton_expr)
6580 {
6581 NODE *expr = last_expr_node($1);
6582 switch (nd_type(expr)) {
6583 case NODE_STR:
6584 case NODE_DSTR:
6585 case NODE_XSTR:
6586 case NODE_DXSTR:
6587 case NODE_REGX:
6588 case NODE_DREGX:
6589 case NODE_SYM:
6590 case NODE_LINE:
6591 case NODE_FILE:
6592 case NODE_ENCODING:
6593 case NODE_INTEGER:
6594 case NODE_FLOAT:
6595 case NODE_RATIONAL:
6596 case NODE_IMAGINARY:
6597 case NODE_DSYM:
6598 case NODE_LIST:
6599 case NODE_ZLIST:
6600 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6601 break;
6602 default:
6603 break;
6604 }
6605 $$ = $1;
6606 }
6607 ;
6608
6609singleton_expr : var_ref
6610 | '('
6611 {
6612 SET_LEX_STATE(EXPR_BEG);
6613 p->ctxt.in_argdef = 0;
6614 }
6615 expr rparen
6616 {
6617 p->ctxt.in_argdef = 1;
6618 $$ = $3;
6619 /*% ripper: paren!($:3) %*/
6620 }
6621 ;
6622
6623assoc_list : none
6624 | assocs trailer
6625 {
6626 $$ = $1;
6627 /*% ripper: assoclist_from_args!($:1) %*/
6628 }
6629 ;
6630
6631assocs : assoc
6632 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6633 | assocs ',' assoc
6634 {
6635 NODE *assocs = $1;
6636 NODE *tail = $3;
6637 if (!assocs) {
6638 assocs = tail;
6639 }
6640 else if (tail) {
6641 if (RNODE_LIST(assocs)->nd_head) {
6642 NODE *n = RNODE_LIST(tail)->nd_next;
6643 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6644 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6645 /* DSTAR */
6646 tail = RNODE_HASH(n)->nd_head;
6647 }
6648 }
6649 if (tail) {
6650 assocs = list_concat(assocs, tail);
6651 }
6652 }
6653 $$ = assocs;
6654 /*% ripper: rb_ary_push($:1, $:3) %*/
6655 }
6656 ;
6657
6658assoc : arg_value tASSOC arg_value
6659 {
6660 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6661 /*% ripper: assoc_new!($:1, $:3) %*/
6662 }
6663 | tLABEL arg_value
6664 {
6665 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6666 /*% ripper: assoc_new!($:1, $:2) %*/
6667 }
6668 | tLABEL
6669 {
6670 NODE *val = gettable(p, $1, &@$);
6671 if (!val) val = NEW_ERROR(&@$);
6672 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6673 /*% ripper: assoc_new!($:1, Qnil) %*/
6674 }
6675 | tSTRING_BEG string_contents tLABEL_END arg_value
6676 {
6677 YYLTYPE loc = code_loc_gen(&@1, &@3);
6678 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6679 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6680 }
6681 | tDSTAR arg_value
6682 {
6683 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6684 /*% ripper: assoc_splat!($:2) %*/
6685 }
6686 | tDSTAR
6687 {
6688 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6689 $$ = list_append(p, NEW_LIST(0, &@$),
6690 NEW_LVAR(idFWD_KWREST, &@$));
6691 /*% ripper: assoc_splat!(Qnil) %*/
6692 }
6693 ;
6694
6695%rule %inline operation : ident_or_const
6696 | tFID
6697 ;
6698
6699operation2 : operation
6700 | op
6701 ;
6702
6703operation3 : tIDENTIFIER
6704 | tFID
6705 | op
6706 ;
6707
6708dot_or_colon : '.'
6709 | tCOLON2
6710 ;
6711
6712call_op : '.'
6713 | tANDDOT
6714 ;
6715
6716call_op2 : call_op
6717 | tCOLON2
6718 ;
6719
6720rparen : '\n'? ')'
6721 ;
6722
6723rbracket : '\n'? ']'
6724 ;
6725
6726rbrace : '\n'? '}'
6727 ;
6728
6729trailer : '\n'?
6730 | ','
6731 ;
6732
6733term : ';'
6734 {
6735 yyerrok;
6736 token_flush(p);
6737 if (p->ctxt.in_defined) {
6738 p->ctxt.has_trailing_semicolon = 1;
6739 }
6740 }
6741 | '\n'
6742 {
6743 @$.end_pos = @$.beg_pos;
6744 token_flush(p);
6745 }
6746 ;
6747
6748terms : term
6749 | terms ';' {yyerrok;}
6750 ;
6751
6752none : /* none */
6753 {
6754 $$ = 0;
6755 /*% ripper: Qnil %*/
6756 }
6757 ;
6758%%
6759# undef p
6760# undef yylex
6761# undef yylval
6762# define yylval (*p->lval)
6763
6764static int regx_options(struct parser_params*);
6765static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6766static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6767static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6768static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6769
6770#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6771
6772# define set_yylval_node(x) { \
6773 YYLTYPE _cur_loc; \
6774 rb_parser_set_location(p, &_cur_loc); \
6775 yylval.node = (x); \
6776 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6777}
6778# define set_yylval_str(x) \
6779do { \
6780 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6781 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6782} while(0)
6783# define set_yylval_num(x) { \
6784 yylval.num = (x); \
6785 set_parser_s_value(x); \
6786}
6787# define set_yylval_id(x) (yylval.id = (x))
6788# define set_yylval_name(x) { \
6789 (yylval.id = (x)); \
6790 set_parser_s_value(ID2SYM(x)); \
6791}
6792# define yylval_id() (yylval.id)
6793
6794#define set_yylval_noname() set_yylval_id(keyword_nil)
6795#define has_delayed_token(p) (p->delayed.token != NULL)
6796
6797#ifndef RIPPER
6798#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6799#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6800
6801static bool
6802parser_has_token(struct parser_params *p)
6803{
6804 const char *const pcur = p->lex.pcur;
6805 const char *const ptok = p->lex.ptok;
6806 if (p->keep_tokens && (pcur < ptok)) {
6807 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6808 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6809 }
6810 return pcur > ptok;
6811}
6812
6813static const char *
6814escaped_char(int c)
6815{
6816 switch (c) {
6817 case '"': return "\\\"";
6818 case '\\': return "\\\\";
6819 case '\0': return "\\0";
6820 case '\n': return "\\n";
6821 case '\r': return "\\r";
6822 case '\t': return "\\t";
6823 case '\f': return "\\f";
6824 case '\013': return "\\v";
6825 case '\010': return "\\b";
6826 case '\007': return "\\a";
6827 case '\033': return "\\e";
6828 case '\x7f': return "\\c?";
6829 }
6830 return NULL;
6831}
6832
6833static rb_parser_string_t *
6834rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6835{
6836 rb_encoding *enc = p->enc;
6837 const char *ptr = str->ptr;
6838 const char *pend = ptr + str->len;
6839 const char *prev = ptr;
6840 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6841 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6842
6843 while (ptr < pend) {
6844 unsigned int c;
6845 const char *cc;
6846 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6847 if (!MBCLEN_CHARFOUND_P(n)) {
6848 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6849 n = rb_enc_mbminlen(enc);
6850 if (pend < ptr + n)
6851 n = (int)(pend - ptr);
6852 while (n--) {
6853 c = *ptr & 0xf0 >> 4;
6854 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6855 c = *ptr & 0x0f;
6856 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6857 parser_str_cat(result, charbuf, 4);
6858 prev = ++ptr;
6859 }
6860 continue;
6861 }
6862 n = MBCLEN_CHARFOUND_LEN(n);
6863 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6864 ptr += n;
6865 cc = escaped_char(c);
6866 if (cc) {
6867 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6868 parser_str_cat_cstr(result, cc);
6869 prev = ptr;
6870 }
6871 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6872 }
6873 else {
6874 if (ptr - n > prev) {
6875 parser_str_cat(result, prev, ptr - n - prev);
6876 prev = ptr - n;
6877 }
6878 parser_str_cat(result, prev, ptr - prev);
6879 prev = ptr;
6880 }
6881 }
6882 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6883
6884 return result;
6885}
6886
6887static void
6888parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6889{
6890 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6891 token->id = p->token_id;
6892 token->type_name = parser_token2char(p, t);
6893 token->str = str;
6894 token->loc.beg_pos = p->yylloc->beg_pos;
6895 token->loc.end_pos = p->yylloc->end_pos;
6896 rb_parser_ary_push_ast_token(p, p->tokens, token);
6897 p->token_id++;
6898
6899 if (p->debug) {
6900 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6901 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6902 line, token->id, token->type_name, str_escaped->ptr,
6903 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6904 token->loc.end_pos.lineno, token->loc.end_pos.column);
6905 rb_parser_string_free(p, str_escaped);
6906 }
6907}
6908
6909static void
6910parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6911{
6912 debug_token_line(p, "parser_dispatch_scan_event", line);
6913
6914 if (!parser_has_token(p)) return;
6915
6916 RUBY_SET_YYLLOC(*p->yylloc);
6917
6918 if (p->keep_tokens) {
6919 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6920 parser_append_tokens(p, str, t, line);
6921 }
6922
6923 token_flush(p);
6924}
6925
6926#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6927static void
6928parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6929{
6930 debug_token_line(p, "parser_dispatch_delayed_token", line);
6931
6932 if (!has_delayed_token(p)) return;
6933
6934 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6935
6936 if (p->keep_tokens) {
6937 /* p->delayed.token is freed by rb_parser_tokens_free */
6938 parser_append_tokens(p, p->delayed.token, t, line);
6939 }
6940 else {
6941 rb_parser_string_free(p, p->delayed.token);
6942 }
6943
6944 p->delayed.token = NULL;
6945}
6946#else
6947#define literal_flush(p, ptr) ((void)(ptr))
6948
6949static int
6950ripper_has_scan_event(struct parser_params *p)
6951{
6952 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6953 return p->lex.pcur > p->lex.ptok;
6954}
6955
6956static VALUE
6957ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6958{
6959 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6960 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6961 RUBY_SET_YYLLOC(*p->yylloc);
6962 token_flush(p);
6963 return rval;
6964}
6965
6966static void
6967ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6968{
6969 if (!ripper_has_scan_event(p)) return;
6970
6971 set_parser_s_value(ripper_scan_event_val(p, t));
6972}
6973#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6974
6975static void
6976ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6977{
6978 /* save and adjust the location to delayed token for callbacks */
6979 int saved_line = p->ruby_sourceline;
6980 const char *saved_tokp = p->lex.ptok;
6981 VALUE s_value, str;
6982
6983 if (!has_delayed_token(p)) return;
6984 p->ruby_sourceline = p->delayed.beg_line;
6985 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6986 str = rb_str_new_mutable_parser_string(p->delayed.token);
6987 rb_parser_string_free(p, p->delayed.token);
6988 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6989 set_parser_s_value(s_value);
6990 p->delayed.token = NULL;
6991 p->ruby_sourceline = saved_line;
6992 p->lex.ptok = saved_tokp;
6993}
6994#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6995#endif /* RIPPER */
6996
6997static inline int
6998is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6999{
7000 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7001}
7002
7003static inline bool
7004peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
7005{
7006 const char *ptr = p->lex.pcur + at;
7007 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
7008 if (memcmp(ptr, str, len)) return false;
7009 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
7010 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
7011}
7012
7013static inline int
7014parser_is_identchar(struct parser_params *p)
7015{
7016 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7017}
7018
7019static inline int
7020parser_isascii(struct parser_params *p)
7021{
7022 return ISASCII(*(p->lex.pcur-1));
7023}
7024
7025static void
7026token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7027{
7028 int column = 1, nonspc = 0, i;
7029 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7030 if (*ptr == '\t') {
7031 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7032 }
7033 column++;
7034 if (*ptr != ' ' && *ptr != '\t') {
7035 nonspc = 1;
7036 }
7037 }
7038
7039 ptinfo->beg = loc->beg_pos;
7040 ptinfo->indent = column;
7041 ptinfo->nonspc = nonspc;
7042}
7043
7044static void
7045token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7046{
7047 token_info *ptinfo;
7048
7049 if (!p->token_info_enabled) return;
7050 ptinfo = ALLOC(token_info);
7051 ptinfo->token = token;
7052 ptinfo->next = p->token_info;
7053 token_info_setup(ptinfo, p->lex.pbeg, loc);
7054
7055 p->token_info = ptinfo;
7056}
7057
7058static void
7059token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7060{
7061 token_info *ptinfo_beg = p->token_info;
7062
7063 if (!ptinfo_beg) return;
7064
7065 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7066 token_info_warn(p, token, ptinfo_beg, 1, loc);
7067
7068 p->token_info = ptinfo_beg->next;
7069 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7070}
7071
7072static void
7073token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7074{
7075 token_info *ptinfo_beg = p->token_info;
7076
7077 if (!ptinfo_beg) return;
7078 p->token_info = ptinfo_beg->next;
7079
7080 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7081 ptinfo_beg->beg.column != beg_pos.column ||
7082 strcmp(ptinfo_beg->token, token)) {
7083 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7084 beg_pos.lineno, beg_pos.column, token,
7085 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7086 ptinfo_beg->token);
7087 }
7088
7089 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7090}
7091
7092static void
7093token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7094{
7095 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7096 if (!p->token_info_enabled) return;
7097 if (!ptinfo_beg) return;
7098 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7099 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7100 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7101 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7102 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7103 rb_warn3L(ptinfo_end->beg.lineno,
7104 "mismatched indentations at '%s' with '%s' at %d",
7105 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7106}
7107
7108static int
7109parser_precise_mbclen(struct parser_params *p, const char *ptr)
7110{
7111 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7112 if (!MBCLEN_CHARFOUND_P(len)) {
7113 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7114 return -1;
7115 }
7116 return len;
7117}
7118
7119#ifndef RIPPER
7120static inline void
7121parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7122{
7123 rb_parser_string_t *str;
7124 int lineno = p->ruby_sourceline;
7125 if (!yylloc) {
7126 return;
7127 }
7128 else if (yylloc->beg_pos.lineno == lineno) {
7129 str = p->lex.lastline;
7130 }
7131 else {
7132 return;
7133 }
7134 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7135}
7136
7137static int
7138parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7139{
7140#if 0
7141 YYLTYPE current;
7142
7143 if (!yylloc) {
7144 yylloc = RUBY_SET_YYLLOC(current);
7145 }
7146 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7147 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7148 yylloc = 0;
7149 }
7150#endif
7151 parser_compile_error(p, yylloc, "%s", msg);
7152 parser_show_error_line(p, yylloc);
7153 return 0;
7154}
7155
7156static int
7157parser_yyerror0(struct parser_params *p, const char *msg)
7158{
7159 YYLTYPE current;
7160 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7161}
7162
7163void
7164ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7165{
7166 VALUE mesg;
7167 const int max_line_margin = 30;
7168 const char *ptr, *ptr_end, *pt, *pb;
7169 const char *pre = "", *post = "", *pend;
7170 const char *code = "", *caret = "";
7171 const char *lim;
7172 const char *const pbeg = PARSER_STRING_PTR(str);
7173 char *buf;
7174 long len;
7175 int i;
7176
7177 if (!yylloc) return;
7178 pend = rb_parser_string_end(str);
7179 if (pend > pbeg && pend[-1] == '\n') {
7180 if (--pend > pbeg && pend[-1] == '\r') --pend;
7181 }
7182
7183 pt = pend;
7184 if (lineno == yylloc->end_pos.lineno &&
7185 (pend - pbeg) > yylloc->end_pos.column) {
7186 pt = pbeg + yylloc->end_pos.column;
7187 }
7188
7189 ptr = ptr_end = pt;
7190 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7191 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7192
7193 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7194 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7195
7196 len = ptr_end - ptr;
7197 if (len > 4) {
7198 if (ptr > pbeg) {
7199 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7200 if (ptr > pbeg) pre = "...";
7201 }
7202 if (ptr_end < pend) {
7203 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7204 if (ptr_end < pend) post = "...";
7205 }
7206 }
7207 pb = pbeg;
7208 if (lineno == yylloc->beg_pos.lineno) {
7209 pb += yylloc->beg_pos.column;
7210 if (pb > pt) pb = pt;
7211 }
7212 if (pb < ptr) pb = ptr;
7213 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7214 return;
7215 }
7216 if (RTEST(errbuf)) {
7217 mesg = rb_attr_get(errbuf, idMesg);
7218 if (char_at_end(p, mesg, '\n') != '\n')
7219 rb_str_cat_cstr(mesg, "\n");
7220 }
7221 else {
7222 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7223 }
7224 if (!errbuf && rb_stderr_tty_p()) {
7225#define CSI_BEGIN "\033["
7226#define CSI_SGR "m"
7227 rb_str_catf(mesg,
7228 CSI_BEGIN""CSI_SGR"%s" /* pre */
7229 CSI_BEGIN"1"CSI_SGR"%.*s"
7230 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7231 CSI_BEGIN";1"CSI_SGR"%.*s"
7232 CSI_BEGIN""CSI_SGR"%s" /* post */
7233 "\n",
7234 pre,
7235 (int)(pb - ptr), ptr,
7236 (int)(pt - pb), pb,
7237 (int)(ptr_end - pt), pt,
7238 post);
7239 }
7240 else {
7241 char *p2;
7242
7243 len = ptr_end - ptr;
7244 lim = pt < pend ? pt : pend;
7245 i = (int)(lim - ptr);
7246 buf = ALLOCA_N(char, i+2);
7247 code = ptr;
7248 caret = p2 = buf;
7249 if (ptr <= pb) {
7250 while (ptr < pb) {
7251 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7252 }
7253 *p2++ = '^';
7254 ptr++;
7255 }
7256 if (lim > ptr) {
7257 memset(p2, '~', (lim - ptr));
7258 p2 += (lim - ptr);
7259 }
7260 *p2 = '\0';
7261 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7262 pre, (int)len, code, post,
7263 pre, caret);
7264 }
7265 if (!errbuf) rb_write_error_str(mesg);
7266}
7267#else
7268
7269static int
7270parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7271{
7272 const char *pcur = 0, *ptok = 0;
7273 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7274 p->ruby_sourceline == yylloc->end_pos.lineno) {
7275 pcur = p->lex.pcur;
7276 ptok = p->lex.ptok;
7277 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7278 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7279 }
7280 parser_yyerror0(p, msg);
7281 if (pcur) {
7282 p->lex.ptok = ptok;
7283 p->lex.pcur = pcur;
7284 }
7285 return 0;
7286}
7287
7288static int
7289parser_yyerror0(struct parser_params *p, const char *msg)
7290{
7291 dispatch1(parse_error, STR_NEW2(msg));
7292 ripper_error(p);
7293 return 0;
7294}
7295
7296static inline void
7297parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7298{
7299}
7300#endif /* !RIPPER */
7301
7302static int
7303vtable_size(const struct vtable *tbl)
7304{
7305 if (!DVARS_TERMINAL_P(tbl)) {
7306 return tbl->pos;
7307 }
7308 else {
7309 return 0;
7310 }
7311}
7312
7313static struct vtable *
7314vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7315{
7316 struct vtable *tbl = ALLOC(struct vtable);
7317 tbl->pos = 0;
7318 tbl->capa = 8;
7319 tbl->tbl = ALLOC_N(ID, tbl->capa);
7320 tbl->prev = prev;
7321#ifndef RIPPER
7322 if (p->debug) {
7323 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7324 }
7325#endif
7326 return tbl;
7327}
7328#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7329
7330static void
7331vtable_free_gen(struct parser_params *p, int line, const char *name,
7332 struct vtable *tbl)
7333{
7334#ifndef RIPPER
7335 if (p->debug) {
7336 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7337 }
7338#endif
7339 if (!DVARS_TERMINAL_P(tbl)) {
7340 if (tbl->tbl) {
7341 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7342 }
7343 ruby_sized_xfree(tbl, sizeof(*tbl));
7344 }
7345}
7346#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7347
7348static void
7349vtable_add_gen(struct parser_params *p, int line, const char *name,
7350 struct vtable *tbl, ID id)
7351{
7352#ifndef RIPPER
7353 if (p->debug) {
7354 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7355 line, name, (void *)tbl, rb_id2name(id));
7356 }
7357#endif
7358 if (DVARS_TERMINAL_P(tbl)) {
7359 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7360 return;
7361 }
7362 if (tbl->pos == tbl->capa) {
7363 tbl->capa = tbl->capa * 2;
7364 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7365 }
7366 tbl->tbl[tbl->pos++] = id;
7367}
7368#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7369
7370static void
7371vtable_pop_gen(struct parser_params *p, int line, const char *name,
7372 struct vtable *tbl, int n)
7373{
7374 if (p->debug) {
7375 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7376 line, name, (void *)tbl, n);
7377 }
7378 if (tbl->pos < n) {
7379 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7380 return;
7381 }
7382 tbl->pos -= n;
7383}
7384#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7385
7386static int
7387vtable_included(const struct vtable * tbl, ID id)
7388{
7389 int i;
7390
7391 if (!DVARS_TERMINAL_P(tbl)) {
7392 for (i = 0; i < tbl->pos; i++) {
7393 if (tbl->tbl[i] == id) {
7394 return i+1;
7395 }
7396 }
7397 }
7398 return 0;
7399}
7400
7401static void parser_prepare(struct parser_params *p);
7402
7403static int
7404e_option_supplied(struct parser_params *p)
7405{
7406 return strcmp(p->ruby_sourcefile, "-e") == 0;
7407}
7408
7409#ifndef RIPPER
7410static NODE *parser_append_options(struct parser_params *p, NODE *node);
7411
7412static VALUE
7413yycompile0(VALUE arg)
7414{
7415 int n;
7416 NODE *tree;
7417 struct parser_params *p = (struct parser_params *)arg;
7418 int cov = FALSE;
7419
7420 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7421 cov = TRUE;
7422 }
7423
7424 if (p->debug_lines) {
7425 p->ast->body.script_lines = p->debug_lines;
7426 }
7427
7428 parser_prepare(p);
7429#define RUBY_DTRACE_PARSE_HOOK(name) \
7430 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7431 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7432 }
7433 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7434 n = yyparse(p);
7435 RUBY_DTRACE_PARSE_HOOK(END);
7436
7437 p->debug_lines = 0;
7438
7439 xfree(p->lex.strterm);
7440 p->lex.strterm = 0;
7441 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7442 if (n || p->error_p) {
7443 VALUE mesg = p->error_buffer;
7444 if (!mesg) {
7445 mesg = syntax_error_new();
7446 }
7447 if (!p->error_tolerant) {
7448 rb_set_errinfo(mesg);
7449 return FALSE;
7450 }
7451 }
7452 tree = p->eval_tree;
7453 if (!tree) {
7454 tree = NEW_NIL(&NULL_LOC);
7455 }
7456 else {
7457 rb_parser_ary_t *tokens = p->tokens;
7458 NODE *prelude;
7459 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7460 prelude = block_append(p, p->eval_tree_begin, body);
7461 RNODE_SCOPE(tree)->nd_body = prelude;
7462 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7463 p->ast->body.coverage_enabled = cov;
7464 if (p->keep_tokens) {
7465 p->ast->node_buffer->tokens = tokens;
7466 p->tokens = NULL;
7467 }
7468 }
7469 p->ast->body.root = tree;
7470 p->ast->body.line_count = p->line_count;
7471 return TRUE;
7472}
7473
7474static rb_ast_t *
7475yycompile(struct parser_params *p, VALUE fname, int line)
7476{
7477 rb_ast_t *ast;
7478 if (NIL_P(fname)) {
7479 p->ruby_sourcefile_string = Qnil;
7480 p->ruby_sourcefile = "(none)";
7481 }
7482 else {
7483 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7484 p->ruby_sourcefile = StringValueCStr(fname);
7485 }
7486 p->ruby_sourceline = line - 1;
7487
7488 p->lvtbl = NULL;
7489
7490 p->ast = ast = rb_ast_new();
7491 compile_callback(yycompile0, (VALUE)p);
7492 p->ast = 0;
7493
7494 while (p->lvtbl) {
7495 local_pop(p);
7496 }
7497
7498 return ast;
7499}
7500#endif /* !RIPPER */
7501
7502static rb_encoding *
7503must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7504{
7505 rb_encoding *enc = rb_parser_str_get_encoding(s);
7506 if (!rb_enc_asciicompat(enc)) {
7507 rb_raise(rb_eArgError, "invalid source encoding");
7508 }
7509 return enc;
7510}
7511
7512static rb_parser_string_t *
7513lex_getline(struct parser_params *p)
7514{
7515 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7516 if (!line) return 0;
7517 p->line_count++;
7518 string_buffer_append(p, line);
7519 must_be_ascii_compatible(p, line);
7520 return line;
7521}
7522
7523#ifndef RIPPER
7524rb_ast_t*
7525rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7526{
7527 p->lex.gets = gets;
7528 p->lex.input = input;
7529 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7530
7531 return yycompile(p, fname, line);
7532}
7533#endif /* !RIPPER */
7534
7535#define STR_FUNC_ESCAPE 0x01
7536#define STR_FUNC_EXPAND 0x02
7537#define STR_FUNC_REGEXP 0x04
7538#define STR_FUNC_QWORDS 0x08
7539#define STR_FUNC_SYMBOL 0x10
7540#define STR_FUNC_INDENT 0x20
7541#define STR_FUNC_LABEL 0x40
7542#define STR_FUNC_LIST 0x4000
7543#define STR_FUNC_TERM 0x8000
7544
7545enum string_type {
7546 str_label = STR_FUNC_LABEL,
7547 str_squote = (0),
7548 str_dquote = (STR_FUNC_EXPAND),
7549 str_xquote = (STR_FUNC_EXPAND),
7550 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7551 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7552 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7553 str_ssym = (STR_FUNC_SYMBOL),
7554 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7555};
7556
7557static rb_parser_string_t *
7558parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7559{
7560 rb_parser_string_t *pstr;
7561
7562 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7563
7564 if (!(func & STR_FUNC_REGEXP)) {
7565 if (rb_parser_is_ascii_string(p, pstr)) {
7566 }
7567 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7568 /* everything is valid in ASCII-8BIT */
7569 enc = rb_ascii8bit_encoding();
7570 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7571 }
7572 }
7573
7574 return pstr;
7575}
7576
7577static int
7578strterm_is_heredoc(rb_strterm_t *strterm)
7579{
7580 return strterm->heredoc;
7581}
7582
7583static rb_strterm_t *
7584new_strterm(struct parser_params *p, int func, int term, int paren)
7585{
7586 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7587 strterm->u.literal.func = func;
7588 strterm->u.literal.term = term;
7589 strterm->u.literal.paren = paren;
7590 return strterm;
7591}
7592
7593static rb_strterm_t *
7594new_heredoc(struct parser_params *p)
7595{
7596 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7597 strterm->heredoc = true;
7598 return strterm;
7599}
7600
7601#define peek(p,c) peek_n(p, (c), 0)
7602#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7603#define peekc(p) peekc_n(p, 0)
7604#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7605
7606#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7607static void
7608parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7609{
7610 debug_token_line(p, "add_delayed_token", line);
7611
7612 if (tok < end) {
7613 if (has_delayed_token(p)) {
7614 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7615 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7616 int end_col = (next_line ? 0 : p->delayed.end_col);
7617 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7618 dispatch_delayed_token(p, tSTRING_CONTENT);
7619 }
7620 }
7621 if (!has_delayed_token(p)) {
7622 p->delayed.token = rb_parser_string_new(p, 0, 0);
7623 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7624 p->delayed.beg_line = p->ruby_sourceline;
7625 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7626 }
7627 parser_str_cat(p->delayed.token, tok, end - tok);
7628 p->delayed.end_line = p->ruby_sourceline;
7629 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7630 p->lex.ptok = end;
7631 }
7632}
7633
7634static void
7635set_lastline(struct parser_params *p, rb_parser_string_t *str)
7636{
7637 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7638 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7639 p->lex.lastline = str;
7640}
7641
7642static int
7643nextline(struct parser_params *p, int set_encoding)
7644{
7645 rb_parser_string_t *str = p->lex.nextline;
7646 p->lex.nextline = 0;
7647 if (!str) {
7648 if (p->eofp)
7649 return -1;
7650
7651 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7652 goto end_of_input;
7653 }
7654
7655 if (!p->lex.input || !(str = lex_getline(p))) {
7656 end_of_input:
7657 p->eofp = 1;
7658 lex_goto_eol(p);
7659 return -1;
7660 }
7661#ifndef RIPPER
7662 if (p->debug_lines) {
7663 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7664 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7665 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7666 }
7667#endif
7668 p->cr_seen = FALSE;
7669 }
7670 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7671 /* after here-document without terminator */
7672 goto end_of_input;
7673 }
7674 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7675 if (p->heredoc_end > 0) {
7676 p->ruby_sourceline = p->heredoc_end;
7677 p->heredoc_end = 0;
7678 }
7679 p->ruby_sourceline++;
7680 set_lastline(p, str);
7681 token_flush(p);
7682 return 0;
7683}
7684
7685static int
7686parser_cr(struct parser_params *p, int c)
7687{
7688 if (peek(p, '\n')) {
7689 p->lex.pcur++;
7690 c = '\n';
7691 }
7692 return c;
7693}
7694
7695static inline int
7696nextc0(struct parser_params *p, int set_encoding)
7697{
7698 int c;
7699
7700 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7701 if (nextline(p, set_encoding)) return -1;
7702 }
7703 c = (unsigned char)*p->lex.pcur++;
7704 if (UNLIKELY(c == '\r')) {
7705 c = parser_cr(p, c);
7706 }
7707
7708 return c;
7709}
7710#define nextc(p) nextc0(p, TRUE)
7711
7712static void
7713pushback(struct parser_params *p, int c)
7714{
7715 if (c == -1) return;
7716 p->eofp = 0;
7717 p->lex.pcur--;
7718 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7719 p->lex.pcur--;
7720 }
7721}
7722
7723#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7724
7725#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7726#define tok(p) (p)->tokenbuf
7727#define toklen(p) (p)->tokidx
7728
7729static int
7730looking_at_eol_p(struct parser_params *p)
7731{
7732 const char *ptr = p->lex.pcur;
7733 while (!lex_eol_ptr_p(p, ptr)) {
7734 int c = (unsigned char)*ptr++;
7735 int eol = (c == '\n' || c == '#');
7736 if (eol || !ISSPACE(c)) {
7737 return eol;
7738 }
7739 }
7740 return TRUE;
7741}
7742
7743static char*
7744newtok(struct parser_params *p)
7745{
7746 p->tokidx = 0;
7747 if (!p->tokenbuf) {
7748 p->toksiz = 60;
7749 p->tokenbuf = ALLOC_N(char, 60);
7750 }
7751 if (p->toksiz > 4096) {
7752 p->toksiz = 60;
7753 REALLOC_N(p->tokenbuf, char, 60);
7754 }
7755 return p->tokenbuf;
7756}
7757
7758static char *
7759tokspace(struct parser_params *p, int n)
7760{
7761 p->tokidx += n;
7762
7763 if (p->tokidx >= p->toksiz) {
7764 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7765 REALLOC_N(p->tokenbuf, char, p->toksiz);
7766 }
7767 return &p->tokenbuf[p->tokidx-n];
7768}
7769
7770static void
7771tokadd(struct parser_params *p, int c)
7772{
7773 p->tokenbuf[p->tokidx++] = (char)c;
7774 if (p->tokidx >= p->toksiz) {
7775 p->toksiz *= 2;
7776 REALLOC_N(p->tokenbuf, char, p->toksiz);
7777 }
7778}
7779
7780static int
7781tok_hex(struct parser_params *p, size_t *numlen)
7782{
7783 int c;
7784
7785 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7786 if (!*numlen) {
7787 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7788 yyerror0("invalid hex escape");
7789 dispatch_scan_event(p, tSTRING_CONTENT);
7790 return 0;
7791 }
7792 p->lex.pcur += *numlen;
7793 return c;
7794}
7795
7796#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7797
7798static int
7799escaped_control_code(int c)
7800{
7801 int c2 = 0;
7802 switch (c) {
7803 case ' ':
7804 c2 = 's';
7805 break;
7806 case '\n':
7807 c2 = 'n';
7808 break;
7809 case '\t':
7810 c2 = 't';
7811 break;
7812 case '\v':
7813 c2 = 'v';
7814 break;
7815 case '\r':
7816 c2 = 'r';
7817 break;
7818 case '\f':
7819 c2 = 'f';
7820 break;
7821 }
7822 return c2;
7823}
7824
7825#define WARN_SPACE_CHAR(c, prefix) \
7826 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7827
7828static int
7829tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7830 int regexp_literal, const char *begin)
7831{
7832 const int wide = !begin;
7833 size_t numlen;
7834 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7835
7836 p->lex.pcur += numlen;
7837 if (p->lex.strterm == NULL ||
7838 strterm_is_heredoc(p->lex.strterm) ||
7839 (p->lex.strterm->u.literal.func != str_regexp)) {
7840 if (!begin) begin = p->lex.pcur;
7841 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7842 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7843 yyerror0("invalid Unicode escape");
7844 dispatch_scan_event(p, tSTRING_CONTENT);
7845 return wide && numlen > 0;
7846 }
7847 if (codepoint > 0x10ffff) {
7848 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7849 yyerror0("invalid Unicode codepoint (too large)");
7850 dispatch_scan_event(p, tSTRING_CONTENT);
7851 return wide;
7852 }
7853 if ((codepoint & 0xfffff800) == 0xd800) {
7854 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7855 yyerror0("invalid Unicode codepoint");
7856 dispatch_scan_event(p, tSTRING_CONTENT);
7857 return wide;
7858 }
7859 }
7860 if (regexp_literal) {
7861 tokcopy(p, (int)numlen);
7862 }
7863 else if (codepoint >= 0x80) {
7864 rb_encoding *utf8 = rb_utf8_encoding();
7865 if (*encp && utf8 != *encp) {
7866 YYLTYPE loc = RUBY_INIT_YYLLOC();
7867 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7868 parser_show_error_line(p, &loc);
7869 return wide;
7870 }
7871 *encp = utf8;
7872 tokaddmbc(p, codepoint, *encp);
7873 }
7874 else {
7875 tokadd(p, codepoint);
7876 }
7877 return TRUE;
7878}
7879
7880static int tokadd_mbchar(struct parser_params *p, int c);
7881
7882static int
7883tokskip_mbchar(struct parser_params *p)
7884{
7885 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7886 if (len > 0) {
7887 p->lex.pcur += len - 1;
7888 }
7889 return len;
7890}
7891
7892/* return value is for ?\u3042 */
7893static void
7894tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7895 int term, int symbol_literal, int regexp_literal)
7896{
7897 /*
7898 * If `term` is not -1, then we allow multiple codepoints in \u{}
7899 * upto `term` byte, otherwise we're parsing a character literal.
7900 * And then add the codepoints to the current token.
7901 */
7902 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7903
7904 const int open_brace = '{', close_brace = '}';
7905
7906 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7907
7908 if (peek(p, open_brace)) { /* handle \u{...} form */
7909 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7910 /*
7911 * Skip parsing validation code and copy bytes as-is until term or
7912 * closing brace, in order to correctly handle extended regexps where
7913 * invalid unicode escapes are allowed in comments. The regexp parser
7914 * does its own validation and will catch any issues.
7915 */
7916 tokadd(p, open_brace);
7917 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7918 int c = peekc(p);
7919 if (c == close_brace) {
7920 tokadd(p, c);
7921 ++p->lex.pcur;
7922 break;
7923 }
7924 else if (c == term) {
7925 break;
7926 }
7927 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7928 tokadd(p, c);
7929 c = *++p->lex.pcur;
7930 }
7931 tokadd_mbchar(p, c);
7932 }
7933 }
7934 else {
7935 const char *second = NULL;
7936 int c, last = nextc(p);
7937 if (lex_eol_p(p)) goto unterminated;
7938 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7939 while (c != close_brace) {
7940 if (c == term) goto unterminated;
7941 if (second == multiple_codepoints)
7942 second = p->lex.pcur;
7943 if (regexp_literal) tokadd(p, last);
7944 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7945 break;
7946 }
7947 while (ISSPACE(c = peekc(p))) {
7948 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7949 last = c;
7950 }
7951 if (term == -1 && !second)
7952 second = multiple_codepoints;
7953 }
7954
7955 if (c != close_brace) {
7956 unterminated:
7957 flush_string_content(p, rb_utf8_encoding(), 0);
7958 yyerror0("unterminated Unicode escape");
7959 dispatch_scan_event(p, tSTRING_CONTENT);
7960 return;
7961 }
7962 if (second && second != multiple_codepoints) {
7963 const char *pcur = p->lex.pcur;
7964 p->lex.pcur = second;
7965 dispatch_scan_event(p, tSTRING_CONTENT);
7966 token_flush(p);
7967 p->lex.pcur = pcur;
7968 yyerror0(multiple_codepoints);
7969 token_flush(p);
7970 }
7971
7972 if (regexp_literal) tokadd(p, close_brace);
7973 nextc(p);
7974 }
7975 }
7976 else { /* handle \uxxxx form */
7977 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7978 token_flush(p);
7979 return;
7980 }
7981 }
7982}
7983
7984#define ESCAPE_CONTROL 1
7985#define ESCAPE_META 2
7986
7987static int
7988read_escape(struct parser_params *p, int flags, const char *begin)
7989{
7990 int c;
7991 size_t numlen;
7992
7993 switch (c = nextc(p)) {
7994 case '\\': /* Backslash */
7995 return c;
7996
7997 case 'n': /* newline */
7998 return '\n';
7999
8000 case 't': /* horizontal tab */
8001 return '\t';
8002
8003 case 'r': /* carriage-return */
8004 return '\r';
8005
8006 case 'f': /* form-feed */
8007 return '\f';
8008
8009 case 'v': /* vertical tab */
8010 return '\13';
8011
8012 case 'a': /* alarm(bell) */
8013 return '\007';
8014
8015 case 'e': /* escape */
8016 return 033;
8017
8018 case '0': case '1': case '2': case '3': /* octal constant */
8019 case '4': case '5': case '6': case '7':
8020 pushback(p, c);
8021 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8022 p->lex.pcur += numlen;
8023 return c;
8024
8025 case 'x': /* hex constant */
8026 c = tok_hex(p, &numlen);
8027 if (numlen == 0) return 0;
8028 return c;
8029
8030 case 'b': /* backspace */
8031 return '\010';
8032
8033 case 's': /* space */
8034 return ' ';
8035
8036 case 'M':
8037 if (flags & ESCAPE_META) goto eof;
8038 if ((c = nextc(p)) != '-') {
8039 goto eof;
8040 }
8041 if ((c = nextc(p)) == '\\') {
8042 switch (peekc(p)) {
8043 case 'u': case 'U':
8044 nextc(p);
8045 goto eof;
8046 }
8047 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8048 }
8049 else if (c == -1) goto eof;
8050 else if (!ISASCII(c)) {
8051 tokskip_mbchar(p);
8052 goto eof;
8053 }
8054 else {
8055 int c2 = escaped_control_code(c);
8056 if (c2) {
8057 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8058 WARN_SPACE_CHAR(c2, "\\M-");
8059 }
8060 else {
8061 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8062 }
8063 }
8064 else if (ISCNTRL(c)) goto eof;
8065 return ((c & 0xff) | 0x80);
8066 }
8067
8068 case 'C':
8069 if ((c = nextc(p)) != '-') {
8070 goto eof;
8071 }
8072 case 'c':
8073 if (flags & ESCAPE_CONTROL) goto eof;
8074 if ((c = nextc(p))== '\\') {
8075 switch (peekc(p)) {
8076 case 'u': case 'U':
8077 nextc(p);
8078 goto eof;
8079 }
8080 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8081 }
8082 else if (c == '?')
8083 return 0177;
8084 else if (c == -1) goto eof;
8085 else if (!ISASCII(c)) {
8086 tokskip_mbchar(p);
8087 goto eof;
8088 }
8089 else {
8090 int c2 = escaped_control_code(c);
8091 if (c2) {
8092 if (ISCNTRL(c)) {
8093 if (flags & ESCAPE_META) {
8094 WARN_SPACE_CHAR(c2, "\\M-");
8095 }
8096 else {
8097 WARN_SPACE_CHAR(c2, "");
8098 }
8099 }
8100 else {
8101 if (flags & ESCAPE_META) {
8102 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8103 }
8104 else {
8105 WARN_SPACE_CHAR(c2, "\\C-");
8106 }
8107 }
8108 }
8109 else if (ISCNTRL(c)) goto eof;
8110 }
8111 return c & 0x9f;
8112
8113 eof:
8114 case -1:
8115 flush_string_content(p, p->enc, p->lex.pcur - begin);
8116 yyerror0("Invalid escape character syntax");
8117 dispatch_scan_event(p, tSTRING_CONTENT);
8118 return '\0';
8119
8120 default:
8121 if (!ISASCII(c)) {
8122 tokskip_mbchar(p);
8123 goto eof;
8124 }
8125 return c;
8126 }
8127}
8128
8129static void
8130tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8131{
8132 int len = rb_enc_codelen(c, enc);
8133 rb_enc_mbcput(c, tokspace(p, len), enc);
8134}
8135
8136static int
8137tokadd_escape(struct parser_params *p)
8138{
8139 int c;
8140 size_t numlen;
8141 const char *begin = p->lex.pcur;
8142
8143 switch (c = nextc(p)) {
8144 case '\n':
8145 return 0; /* just ignore */
8146
8147 case '0': case '1': case '2': case '3': /* octal constant */
8148 case '4': case '5': case '6': case '7':
8149 {
8150 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8151 if (numlen == 0) goto eof;
8152 p->lex.pcur += numlen;
8153 tokcopy(p, (int)numlen + 1);
8154 }
8155 return 0;
8156
8157 case 'x': /* hex constant */
8158 {
8159 tok_hex(p, &numlen);
8160 if (numlen == 0) return -1;
8161 tokcopy(p, (int)numlen + 2);
8162 }
8163 return 0;
8164
8165 eof:
8166 case -1:
8167 flush_string_content(p, p->enc, p->lex.pcur - begin);
8168 yyerror0("Invalid escape character syntax");
8169 token_flush(p);
8170 return -1;
8171
8172 default:
8173 tokadd(p, '\\');
8174 tokadd(p, c);
8175 }
8176 return 0;
8177}
8178
8179static int
8180char_to_option(int c)
8181{
8182 int val;
8183
8184 switch (c) {
8185 case 'i':
8186 val = RE_ONIG_OPTION_IGNORECASE;
8187 break;
8188 case 'x':
8189 val = RE_ONIG_OPTION_EXTEND;
8190 break;
8191 case 'm':
8192 val = RE_ONIG_OPTION_MULTILINE;
8193 break;
8194 default:
8195 val = 0;
8196 break;
8197 }
8198 return val;
8199}
8200
8201#define ARG_ENCODING_FIXED 16
8202#define ARG_ENCODING_NONE 32
8203#define ENC_ASCII8BIT 1
8204#define ENC_EUC_JP 2
8205#define ENC_Windows_31J 3
8206#define ENC_UTF8 4
8207
8208static int
8209char_to_option_kcode(int c, int *option, int *kcode)
8210{
8211 *option = 0;
8212
8213 switch (c) {
8214 case 'n':
8215 *kcode = ENC_ASCII8BIT;
8216 return (*option = ARG_ENCODING_NONE);
8217 case 'e':
8218 *kcode = ENC_EUC_JP;
8219 break;
8220 case 's':
8221 *kcode = ENC_Windows_31J;
8222 break;
8223 case 'u':
8224 *kcode = ENC_UTF8;
8225 break;
8226 default:
8227 *kcode = -1;
8228 return (*option = char_to_option(c));
8229 }
8230 *option = ARG_ENCODING_FIXED;
8231 return 1;
8232}
8233
8234static int
8235regx_options(struct parser_params *p)
8236{
8237 int kcode = 0;
8238 int kopt = 0;
8239 int options = 0;
8240 int c, opt, kc;
8241
8242 newtok(p);
8243 while (c = nextc(p), ISALPHA(c)) {
8244 if (c == 'o') {
8245 options |= RE_OPTION_ONCE;
8246 }
8247 else if (char_to_option_kcode(c, &opt, &kc)) {
8248 if (kc >= 0) {
8249 if (kc != ENC_ASCII8BIT) kcode = c;
8250 kopt = opt;
8251 }
8252 else {
8253 options |= opt;
8254 }
8255 }
8256 else {
8257 tokadd(p, c);
8258 }
8259 }
8260 options |= kopt;
8261 pushback(p, c);
8262 if (toklen(p)) {
8263 YYLTYPE loc = RUBY_INIT_YYLLOC();
8264 tokfix(p);
8265 compile_error(p, "unknown regexp option%s - %*s",
8266 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8267 parser_show_error_line(p, &loc);
8268 }
8269 return options | RE_OPTION_ENCODING(kcode);
8270}
8271
8272static int
8273tokadd_mbchar(struct parser_params *p, int c)
8274{
8275 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8276 if (len < 0) return -1;
8277 tokadd(p, c);
8278 p->lex.pcur += --len;
8279 if (len > 0) tokcopy(p, len);
8280 return c;
8281}
8282
8283static inline int
8284simple_re_meta(int c)
8285{
8286 switch (c) {
8287 case '$': case '*': case '+': case '.':
8288 case '?': case '^': case '|':
8289 case ')': case ']': case '}': case '>':
8290 return TRUE;
8291 default:
8292 return FALSE;
8293 }
8294}
8295
8296static int
8297parser_update_heredoc_indent(struct parser_params *p, int c)
8298{
8299 if (p->heredoc_line_indent == -1) {
8300 if (c == '\n') p->heredoc_line_indent = 0;
8301 }
8302 else {
8303 if (c == ' ') {
8304 p->heredoc_line_indent++;
8305 return TRUE;
8306 }
8307 else if (c == '\t') {
8308 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8309 p->heredoc_line_indent = w * TAB_WIDTH;
8310 return TRUE;
8311 }
8312 else if (c != '\n') {
8313 if (p->heredoc_indent > p->heredoc_line_indent) {
8314 p->heredoc_indent = p->heredoc_line_indent;
8315 }
8316 p->heredoc_line_indent = -1;
8317 }
8318 else {
8319 /* Whitespace only line has no indentation */
8320 p->heredoc_line_indent = 0;
8321 }
8322 }
8323 return FALSE;
8324}
8325
8326static void
8327parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8328{
8329 YYLTYPE loc = RUBY_INIT_YYLLOC();
8330 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8331 compile_error(p, "%s mixed within %s source", n1, n2);
8332 parser_show_error_line(p, &loc);
8333}
8334
8335static void
8336parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8337{
8338 const char *pos = p->lex.pcur;
8339 p->lex.pcur = beg;
8340 parser_mixed_error(p, enc1, enc2);
8341 p->lex.pcur = pos;
8342}
8343
8344static inline char
8345nibble_char_upper(unsigned int c)
8346{
8347 c &= 0xf;
8348 return c + (c < 10 ? '0' : 'A' - 10);
8349}
8350
8351static int
8352tokadd_string(struct parser_params *p,
8353 int func, int term, int paren, long *nest,
8354 rb_encoding **encp, rb_encoding **enc)
8355{
8356 int c;
8357 bool erred = false;
8358#ifdef RIPPER
8359 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8360 int top_of_line = FALSE;
8361#endif
8362
8363#define mixed_error(enc1, enc2) \
8364 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8365#define mixed_escape(beg, enc1, enc2) \
8366 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8367
8368 while ((c = nextc(p)) != -1) {
8369 if (p->heredoc_indent > 0) {
8370 parser_update_heredoc_indent(p, c);
8371 }
8372#ifdef RIPPER
8373 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8374 pushback(p, c);
8375 break;
8376 }
8377#endif
8378
8379 if (paren && c == paren) {
8380 ++*nest;
8381 }
8382 else if (c == term) {
8383 if (!nest || !*nest) {
8384 pushback(p, c);
8385 break;
8386 }
8387 --*nest;
8388 }
8389 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8390 unsigned char c2 = *p->lex.pcur;
8391 if (c2 == '$' || c2 == '@' || c2 == '{') {
8392 pushback(p, c);
8393 break;
8394 }
8395 }
8396 else if (c == '\\') {
8397 c = nextc(p);
8398 switch (c) {
8399 case '\n':
8400 if (func & STR_FUNC_QWORDS) break;
8401 if (func & STR_FUNC_EXPAND) {
8402 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8403 continue;
8404 if (c == term) {
8405 c = '\\';
8406 goto terminate;
8407 }
8408 }
8409 tokadd(p, '\\');
8410 break;
8411
8412 case '\\':
8413 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8414 break;
8415
8416 case 'u':
8417 if ((func & STR_FUNC_EXPAND) == 0) {
8418 tokadd(p, '\\');
8419 break;
8420 }
8421 tokadd_utf8(p, enc, term,
8422 func & STR_FUNC_SYMBOL,
8423 func & STR_FUNC_REGEXP);
8424 continue;
8425
8426 default:
8427 if (c == -1) return -1;
8428 if (!ISASCII(c)) {
8429 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8430 goto non_ascii;
8431 }
8432 if (func & STR_FUNC_REGEXP) {
8433 switch (c) {
8434 case 'c':
8435 case 'C':
8436 case 'M': {
8437 pushback(p, c);
8438 c = read_escape(p, 0, p->lex.pcur - 1);
8439
8440 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8441 *t++ = '\\';
8442 *t++ = 'x';
8443 *t++ = nibble_char_upper(c >> 4);
8444 *t++ = nibble_char_upper(c);
8445 continue;
8446 }
8447 }
8448
8449 if (c == term && !simple_re_meta(c)) {
8450 tokadd(p, c);
8451 continue;
8452 }
8453 pushback(p, c);
8454 if ((c = tokadd_escape(p)) < 0)
8455 return -1;
8456 if (*enc && *enc != *encp) {
8457 mixed_escape(p->lex.ptok+2, *enc, *encp);
8458 }
8459 continue;
8460 }
8461 else if (func & STR_FUNC_EXPAND) {
8462 pushback(p, c);
8463 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8464 c = read_escape(p, 0, p->lex.pcur - 1);
8465 }
8466 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8467 /* ignore backslashed spaces in %w */
8468 }
8469 else if (c != term && !(paren && c == paren)) {
8470 tokadd(p, '\\');
8471 pushback(p, c);
8472 continue;
8473 }
8474 }
8475 }
8476 else if (!parser_isascii(p)) {
8477 non_ascii:
8478 if (!*enc) {
8479 *enc = *encp;
8480 }
8481 else if (*enc != *encp) {
8482 mixed_error(*enc, *encp);
8483 continue;
8484 }
8485 if (tokadd_mbchar(p, c) == -1) return -1;
8486 continue;
8487 }
8488 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8489 pushback(p, c);
8490 break;
8491 }
8492 if (c & 0x80) {
8493 if (!*enc) {
8494 *enc = *encp;
8495 }
8496 else if (*enc != *encp) {
8497 mixed_error(*enc, *encp);
8498 continue;
8499 }
8500 }
8501 tokadd(p, c);
8502#ifdef RIPPER
8503 top_of_line = (c == '\n');
8504#endif
8505 }
8506 terminate:
8507 if (*enc) *encp = *enc;
8508 return c;
8509}
8510
8511#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8512
8513static void
8514flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8515{
8516 p->lex.pcur -= back;
8517 if (has_delayed_token(p)) {
8518 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8519 if (len > 0) {
8520 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8521 p->delayed.end_line = p->ruby_sourceline;
8522 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8523 }
8524 dispatch_delayed_token(p, tSTRING_CONTENT);
8525 p->lex.ptok = p->lex.pcur;
8526 }
8527 dispatch_scan_event(p, tSTRING_CONTENT);
8528 p->lex.pcur += back;
8529}
8530
8531/* this can be shared with ripper, since it's independent from struct
8532 * parser_params. */
8533#ifndef RIPPER
8534#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8535#define SPECIAL_PUNCT(idx) ( \
8536 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8537 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8538 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8539 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8540 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8541 BIT('0', idx))
8542const uint_least32_t ruby_global_name_punct_bits[] = {
8543 SPECIAL_PUNCT(0),
8544 SPECIAL_PUNCT(1),
8545 SPECIAL_PUNCT(2),
8546};
8547#undef BIT
8548#undef SPECIAL_PUNCT
8549#endif
8550
8551static enum yytokentype
8552parser_peek_variable_name(struct parser_params *p)
8553{
8554 int c;
8555 const char *ptr = p->lex.pcur;
8556
8557 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8558 c = *ptr++;
8559 switch (c) {
8560 case '$':
8561 if ((c = *ptr) == '-') {
8562 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8563 c = *ptr;
8564 }
8565 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8566 return tSTRING_DVAR;
8567 }
8568 break;
8569 case '@':
8570 if ((c = *ptr) == '@') {
8571 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8572 c = *ptr;
8573 }
8574 break;
8575 case '{':
8576 p->lex.pcur = ptr;
8577 p->command_start = TRUE;
8578 yylval.state = p->lex.state;
8579 return tSTRING_DBEG;
8580 default:
8581 return 0;
8582 }
8583 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8584 return tSTRING_DVAR;
8585 return 0;
8586}
8587
8588#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8589#define IS_END() IS_lex_state(EXPR_END_ANY)
8590#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8591#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8592#define IS_LABEL_POSSIBLE() (\
8593 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8594 IS_ARG())
8595#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8596#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8597
8598static inline enum yytokentype
8599parser_string_term(struct parser_params *p, int func)
8600{
8601 xfree(p->lex.strterm);
8602 p->lex.strterm = 0;
8603 if (func & STR_FUNC_REGEXP) {
8604 set_yylval_num(regx_options(p));
8605 dispatch_scan_event(p, tREGEXP_END);
8606 SET_LEX_STATE(EXPR_END);
8607 return tREGEXP_END;
8608 }
8609 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8610 nextc(p);
8611 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8612 return tLABEL_END;
8613 }
8614 SET_LEX_STATE(EXPR_END);
8615 return tSTRING_END;
8616}
8617
8618static enum yytokentype
8619parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8620{
8621 int func = quote->func;
8622 int term = quote->term;
8623 int paren = quote->paren;
8624 int c, space = 0;
8625 rb_encoding *enc = p->enc;
8626 rb_encoding *base_enc = 0;
8627 rb_parser_string_t *lit;
8628
8629 if (func & STR_FUNC_TERM) {
8630 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8631 SET_LEX_STATE(EXPR_END);
8632 xfree(p->lex.strterm);
8633 p->lex.strterm = 0;
8634 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8635 }
8636 c = nextc(p);
8637 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8638 while (c != '\n' && ISSPACE(c = nextc(p)));
8639 space = 1;
8640 }
8641 if (func & STR_FUNC_LIST) {
8642 quote->func &= ~STR_FUNC_LIST;
8643 space = 1;
8644 }
8645 if (c == term && !quote->nest) {
8646 if (func & STR_FUNC_QWORDS) {
8647 quote->func |= STR_FUNC_TERM;
8648 pushback(p, c); /* dispatch the term at tSTRING_END */
8649 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8650 return ' ';
8651 }
8652 return parser_string_term(p, func);
8653 }
8654 if (space) {
8655 if (!ISSPACE(c)) pushback(p, c);
8656 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8657 return ' ';
8658 }
8659 newtok(p);
8660 if ((func & STR_FUNC_EXPAND) && c == '#') {
8661 enum yytokentype t = parser_peek_variable_name(p);
8662 if (t) return t;
8663 tokadd(p, '#');
8664 c = nextc(p);
8665 }
8666 pushback(p, c);
8667 if (tokadd_string(p, func, term, paren, &quote->nest,
8668 &enc, &base_enc) == -1) {
8669 if (p->eofp) {
8670#ifndef RIPPER
8671# define unterminated_literal(mesg) yyerror0(mesg)
8672#else
8673# define unterminated_literal(mesg) compile_error(p, mesg)
8674#endif
8675 literal_flush(p, p->lex.pcur);
8676 if (func & STR_FUNC_QWORDS) {
8677 /* no content to add, bailing out here */
8678 unterminated_literal("unterminated list meets end of file");
8679 xfree(p->lex.strterm);
8680 p->lex.strterm = 0;
8681 return tSTRING_END;
8682 }
8683 if (func & STR_FUNC_REGEXP) {
8684 unterminated_literal("unterminated regexp meets end of file");
8685 }
8686 else {
8687 unterminated_literal("unterminated string meets end of file");
8688 }
8689 quote->func |= STR_FUNC_TERM;
8690 }
8691 }
8692
8693 tokfix(p);
8694 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8695 set_yylval_str(lit);
8696 flush_string_content(p, enc, 0);
8697
8698 return tSTRING_CONTENT;
8699}
8700
8701static enum yytokentype
8702heredoc_identifier(struct parser_params *p)
8703{
8704 /*
8705 * term_len is length of `<<"END"` except `END`,
8706 * in this case term_len is 4 (<, <, " and ").
8707 */
8708 long len, offset = p->lex.pcur - p->lex.pbeg;
8709 int c = nextc(p), term, func = 0, quote = 0;
8710 enum yytokentype token = tSTRING_BEG;
8711 int indent = 0;
8712
8713 if (c == '-') {
8714 c = nextc(p);
8715 func = STR_FUNC_INDENT;
8716 offset++;
8717 }
8718 else if (c == '~') {
8719 c = nextc(p);
8720 func = STR_FUNC_INDENT;
8721 offset++;
8722 indent = INT_MAX;
8723 }
8724 switch (c) {
8725 case '\'':
8726 func |= str_squote; goto quoted;
8727 case '"':
8728 func |= str_dquote; goto quoted;
8729 case '`':
8730 token = tXSTRING_BEG;
8731 func |= str_xquote; goto quoted;
8732
8733 quoted:
8734 quote++;
8735 offset++;
8736 term = c;
8737 len = 0;
8738 while ((c = nextc(p)) != term) {
8739 if (c == -1 || c == '\r' || c == '\n') {
8740 yyerror0("unterminated here document identifier");
8741 return -1;
8742 }
8743 }
8744 break;
8745
8746 default:
8747 if (!parser_is_identchar(p)) {
8748 pushback(p, c);
8749 if (func & STR_FUNC_INDENT) {
8750 pushback(p, indent > 0 ? '~' : '-');
8751 }
8752 return 0;
8753 }
8754 func |= str_dquote;
8755 do {
8756 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8757 if (n < 0) return 0;
8758 p->lex.pcur += --n;
8759 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8760 pushback(p, c);
8761 break;
8762 }
8763
8764 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8765 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8766 yyerror0("too long here document identifier");
8767 dispatch_scan_event(p, tHEREDOC_BEG);
8768 lex_goto_eol(p);
8769
8770 p->lex.strterm = new_heredoc(p);
8771 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8772 here->offset = offset;
8773 here->sourceline = p->ruby_sourceline;
8774 here->length = (unsigned)len;
8775 here->quote = quote;
8776 here->func = func;
8777 here->lastline = p->lex.lastline;
8778
8779 token_flush(p);
8780 p->heredoc_indent = indent;
8781 p->heredoc_line_indent = 0;
8782 return token;
8783}
8784
8785static void
8786heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8787{
8788 rb_parser_string_t *line;
8789 rb_strterm_t *term = p->lex.strterm;
8790
8791 p->lex.strterm = 0;
8792 line = here->lastline;
8793 p->lex.lastline = line;
8794 p->lex.pbeg = PARSER_STRING_PTR(line);
8795 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8796 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8797 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8798 p->heredoc_end = p->ruby_sourceline;
8799 p->ruby_sourceline = (int)here->sourceline;
8800 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8801 p->eofp = 0;
8802 xfree(term);
8803}
8804
8805static int
8806dedent_string_column(const char *str, long len, int width)
8807{
8808 int i, col = 0;
8809
8810 for (i = 0; i < len && col < width; i++) {
8811 if (str[i] == ' ') {
8812 col++;
8813 }
8814 else if (str[i] == '\t') {
8815 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8816 if (n > width) break;
8817 col = n;
8818 }
8819 else {
8820 break;
8821 }
8822 }
8823
8824 return i;
8825}
8826
8827static int
8828dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8829{
8830 char *str;
8831 long len;
8832 int i;
8833
8834 len = PARSER_STRING_LEN(string);
8835 str = PARSER_STRING_PTR(string);
8836
8837 i = dedent_string_column(str, len, width);
8838 if (!i) return 0;
8839
8840 rb_parser_str_modify(string);
8841 str = PARSER_STRING_PTR(string);
8842 if (PARSER_STRING_LEN(string) != len)
8843 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8844 MEMMOVE(str, str + i, char, len - i);
8845 rb_parser_str_set_len(p, string, len - i);
8846 return i;
8847}
8848
8849static NODE *
8850heredoc_dedent(struct parser_params *p, NODE *root)
8851{
8852 NODE *node, *str_node, *prev_node;
8853 int indent = p->heredoc_indent;
8854 rb_parser_string_t *prev_lit = 0;
8855
8856 if (indent <= 0) return root;
8857 if (!root) return root;
8858
8859 prev_node = node = str_node = root;
8860 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8861
8862 while (str_node) {
8863 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8864 if (nd_fl_newline(str_node)) {
8865 dedent_string(p, lit, indent);
8866 }
8867 if (!prev_lit) {
8868 prev_lit = lit;
8869 }
8870 else if (!literal_concat0(p, prev_lit, lit)) {
8871 return 0;
8872 }
8873 else {
8874 NODE *end = RNODE_LIST(node)->as.nd_end;
8875 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8876 if (!node) {
8877 if (nd_type_p(prev_node, NODE_DSTR))
8878 nd_set_type(prev_node, NODE_STR);
8879 break;
8880 }
8881 RNODE_LIST(node)->as.nd_end = end;
8882 goto next_str;
8883 }
8884
8885 str_node = 0;
8886 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8887 next_str:
8888 if (!nd_type_p(node, NODE_LIST)) break;
8889 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8890 enum node_type type = nd_type(str_node);
8891 if (type == NODE_STR || type == NODE_DSTR) break;
8892 prev_lit = 0;
8893 str_node = 0;
8894 }
8895 }
8896 }
8897 return root;
8898}
8899
8900static int
8901whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8902{
8903 const char *beg = p->lex.pbeg;
8904 const char *ptr = p->lex.pend;
8905
8906 if (ptr - beg < len) return FALSE;
8907 if (ptr > beg && ptr[-1] == '\n') {
8908 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8909 if (ptr - beg < len) return FALSE;
8910 }
8911 if (strncmp(eos, ptr -= len, len)) return FALSE;
8912 if (indent) {
8913 while (beg < ptr && ISSPACE(*beg)) beg++;
8914 }
8915 return beg == ptr;
8916}
8917
8918static int
8919word_match_p(struct parser_params *p, const char *word, long len)
8920{
8921 if (strncmp(p->lex.pcur, word, len)) return 0;
8922 if (lex_eol_n_p(p, len)) return 1;
8923 int c = (unsigned char)p->lex.pcur[len];
8924 if (ISSPACE(c)) return 1;
8925 switch (c) {
8926 case '\0': case '\004': case '\032': return 1;
8927 }
8928 return 0;
8929}
8930
8931#define NUM_SUFFIX_R (1<<0)
8932#define NUM_SUFFIX_I (1<<1)
8933#define NUM_SUFFIX_ALL 3
8934
8935static int
8936number_literal_suffix(struct parser_params *p, int mask)
8937{
8938 int c, result = 0;
8939 const char *lastp = p->lex.pcur;
8940
8941 while ((c = nextc(p)) != -1) {
8942 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8943 result |= (mask & NUM_SUFFIX_I);
8944 mask &= ~NUM_SUFFIX_I;
8945 /* r after i, rational of complex is disallowed */
8946 mask &= ~NUM_SUFFIX_R;
8947 continue;
8948 }
8949 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8950 result |= (mask & NUM_SUFFIX_R);
8951 mask &= ~NUM_SUFFIX_R;
8952 continue;
8953 }
8954 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8955 p->lex.pcur = lastp;
8956 literal_flush(p, p->lex.pcur);
8957 return 0;
8958 }
8959 pushback(p, c);
8960 break;
8961 }
8962 return result;
8963}
8964
8965static enum yytokentype
8966set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8967{
8968 enum rb_numeric_type numeric_type = integer_literal;
8969
8970 if (type == tFLOAT) {
8971 numeric_type = float_literal;
8972 }
8973
8974 if (suffix & NUM_SUFFIX_R) {
8975 type = tRATIONAL;
8976 numeric_type = rational_literal;
8977 }
8978 if (suffix & NUM_SUFFIX_I) {
8979 type = tIMAGINARY;
8980 }
8981
8982 switch (type) {
8983 case tINTEGER:
8984 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8985 break;
8986 case tFLOAT:
8987 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8988 break;
8989 case tRATIONAL:
8990 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8991 break;
8992 case tIMAGINARY:
8993 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8994 (void)numeric_type; /* for ripper */
8995 break;
8996 default:
8997 rb_bug("unexpected token: %d", type);
8998 }
8999 SET_LEX_STATE(EXPR_END);
9000 return type;
9001}
9002
9003#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9004static void
9005parser_dispatch_heredoc_end(struct parser_params *p, int line)
9006{
9007 if (has_delayed_token(p))
9008 dispatch_delayed_token(p, tSTRING_CONTENT);
9009
9010#ifdef RIPPER
9011 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9012 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9013#else
9014 if (p->keep_tokens) {
9015 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9016 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9017 parser_append_tokens(p, str, tHEREDOC_END, line);
9018 }
9019#endif
9020
9021 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9022 lex_goto_eol(p);
9023 token_flush(p);
9024}
9025
9026static enum yytokentype
9027here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9028{
9029 int c, func, indent = 0;
9030 const char *eos, *ptr, *ptr_end;
9031 long len;
9032 rb_parser_string_t *str = 0;
9033 rb_encoding *enc = p->enc;
9034 rb_encoding *base_enc = 0;
9035 int bol;
9036#ifdef RIPPER
9037 VALUE s_value;
9038#endif
9039
9040 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9041 len = here->length;
9042 indent = (func = here->func) & STR_FUNC_INDENT;
9043
9044 if ((c = nextc(p)) == -1) {
9045 error:
9046#ifdef RIPPER
9047 if (!has_delayed_token(p)) {
9048 dispatch_scan_event(p, tSTRING_CONTENT);
9049 }
9050 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9051 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9052 if (!(func & STR_FUNC_REGEXP)) {
9053 int cr = ENC_CODERANGE_UNKNOWN;
9054 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9055 if (cr != ENC_CODERANGE_7BIT &&
9056 rb_is_usascii_enc(p->enc) &&
9057 enc != rb_utf8_encoding()) {
9058 enc = rb_ascii8bit_encoding();
9059 }
9060 }
9061 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9062 }
9063 dispatch_delayed_token(p, tSTRING_CONTENT);
9064 }
9065 else {
9066 dispatch_delayed_token(p, tSTRING_CONTENT);
9067 dispatch_scan_event(p, tSTRING_CONTENT);
9068 }
9069 lex_goto_eol(p);
9070#endif
9071 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9072 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9073 (int)len, eos);
9074 token_flush(p);
9075 SET_LEX_STATE(EXPR_END);
9076 return tSTRING_END;
9077 }
9078 bol = was_bol(p);
9079 if (!bol) {
9080 /* not beginning of line, cannot be the terminator */
9081 }
9082 else if (p->heredoc_line_indent == -1) {
9083 /* `heredoc_line_indent == -1` means
9084 * - "after an interpolation in the same line", or
9085 * - "in a continuing line"
9086 */
9087 p->heredoc_line_indent = 0;
9088 }
9089 else if (whole_match_p(p, eos, len, indent)) {
9090 dispatch_heredoc_end(p);
9091 restore:
9092 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9093 token_flush(p);
9094 SET_LEX_STATE(EXPR_END);
9095 return tSTRING_END;
9096 }
9097
9098 if (!(func & STR_FUNC_EXPAND)) {
9099 do {
9100 ptr = PARSER_STRING_PTR(p->lex.lastline);
9101 ptr_end = p->lex.pend;
9102 if (ptr_end > ptr) {
9103 switch (ptr_end[-1]) {
9104 case '\n':
9105 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9106 ptr_end++;
9107 break;
9108 }
9109 case '\r':
9110 --ptr_end;
9111 }
9112 }
9113
9114 if (p->heredoc_indent > 0) {
9115 long i = 0;
9116 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9117 i++;
9118 p->heredoc_line_indent = 0;
9119 }
9120
9121 if (str)
9122 parser_str_cat(str, ptr, ptr_end - ptr);
9123 else
9124 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9125 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9126 lex_goto_eol(p);
9127 if (p->heredoc_indent > 0) {
9128 goto flush_str;
9129 }
9130 if (nextc(p) == -1) {
9131 if (str) {
9132 rb_parser_string_free(p, str);
9133 str = 0;
9134 }
9135 goto error;
9136 }
9137 } while (!whole_match_p(p, eos, len, indent));
9138 }
9139 else {
9140 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9141 newtok(p);
9142 if (c == '#') {
9143 enum yytokentype t = parser_peek_variable_name(p);
9144 if (p->heredoc_line_indent != -1) {
9145 if (p->heredoc_indent > p->heredoc_line_indent) {
9146 p->heredoc_indent = p->heredoc_line_indent;
9147 }
9148 p->heredoc_line_indent = -1;
9149 }
9150 if (t) return t;
9151 tokadd(p, '#');
9152 c = nextc(p);
9153 }
9154 do {
9155 pushback(p, c);
9156 enc = p->enc;
9157 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9158 if (p->eofp) goto error;
9159 goto restore;
9160 }
9161 if (c != '\n') {
9162 if (c == '\\') p->heredoc_line_indent = -1;
9163 flush:
9164 str = STR_NEW3(tok(p), toklen(p), enc, func);
9165 flush_str:
9166 set_yylval_str(str);
9167#ifndef RIPPER
9168 if (bol) nd_set_fl_newline(yylval.node);
9169#endif
9170 flush_string_content(p, enc, 0);
9171 return tSTRING_CONTENT;
9172 }
9173 tokadd(p, nextc(p));
9174 if (p->heredoc_indent > 0) {
9175 lex_goto_eol(p);
9176 goto flush;
9177 }
9178 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9179 if ((c = nextc(p)) == -1) goto error;
9180 } while (!whole_match_p(p, eos, len, indent));
9181 str = STR_NEW3(tok(p), toklen(p), enc, func);
9182 }
9183 dispatch_heredoc_end(p);
9184 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9185 token_flush(p);
9186 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9187#ifdef RIPPER
9188 /* Preserve s_value for set_yylval_str */
9189 s_value = p->s_value;
9190#endif
9191 set_yylval_str(str);
9192#ifdef RIPPER
9193 set_parser_s_value(s_value);
9194#endif
9195
9196#ifndef RIPPER
9197 if (bol) nd_set_fl_newline(yylval.node);
9198#endif
9199 return tSTRING_CONTENT;
9200}
9201
9202#include "lex.c"
9203
9204static int
9205arg_ambiguous(struct parser_params *p, char c)
9206{
9207#ifndef RIPPER
9208 if (c == '/') {
9209 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9210 }
9211 else {
9212 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9213 }
9214#else
9215 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9216#endif
9217 return TRUE;
9218}
9219
9220/* returns true value if formal argument error;
9221 * Qtrue, or error message if ripper */
9222static VALUE
9223formal_argument_error(struct parser_params *p, ID id)
9224{
9225 switch (id_type(id)) {
9226 case ID_LOCAL:
9227 break;
9228#ifndef RIPPER
9229# define ERR(mesg) (yyerror0(mesg), Qtrue)
9230#else
9231# define ERR(mesg) WARN_S(mesg)
9232#endif
9233 case ID_CONST:
9234 return ERR("formal argument cannot be a constant");
9235 case ID_INSTANCE:
9236 return ERR("formal argument cannot be an instance variable");
9237 case ID_GLOBAL:
9238 return ERR("formal argument cannot be a global variable");
9239 case ID_CLASS:
9240 return ERR("formal argument cannot be a class variable");
9241 default:
9242 return ERR("formal argument must be local variable");
9243#undef ERR
9244 }
9245 shadowing_lvar(p, id);
9246
9247 return Qfalse;
9248}
9249
9250static int
9251lvar_defined(struct parser_params *p, ID id)
9252{
9253 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9254}
9255
9256/* emacsen -*- hack */
9257static long
9258parser_encode_length(struct parser_params *p, const char *name, long len)
9259{
9260 long nlen;
9261
9262 if (len > 5 && name[nlen = len - 5] == '-') {
9263 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9264 return nlen;
9265 }
9266 if (len > 4 && name[nlen = len - 4] == '-') {
9267 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9268 return nlen;
9269 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9270 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9271 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9272 return nlen;
9273 }
9274 return len;
9275}
9276
9277static void
9278parser_set_encode(struct parser_params *p, const char *name)
9279{
9280 rb_encoding *enc;
9281 VALUE excargs[3];
9282 int idx = 0;
9283
9284 const char *wrong = 0;
9285 switch (*name) {
9286 case 'e': case 'E': wrong = "external"; break;
9287 case 'i': case 'I': wrong = "internal"; break;
9288 case 'f': case 'F': wrong = "filesystem"; break;
9289 case 'l': case 'L': wrong = "locale"; break;
9290 }
9291 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9292 idx = rb_enc_find_index(name);
9293 if (idx < 0) {
9294 unknown:
9295 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9296 error:
9297 excargs[0] = rb_eArgError;
9298 excargs[2] = rb_make_backtrace();
9299 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9300 VALUE exc = rb_make_exception(3, excargs);
9301 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9302
9303 rb_ast_free(p->ast);
9304 p->ast = NULL;
9305
9306 rb_exc_raise(exc);
9307 }
9308 enc = rb_enc_from_index(idx);
9309 if (!rb_enc_asciicompat(enc)) {
9310 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9311 goto error;
9312 }
9313 p->enc = enc;
9314#ifndef RIPPER
9315 if (p->debug_lines) {
9316 long i;
9317 for (i = 0; i < p->debug_lines->len; i++) {
9318 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9319 }
9320 }
9321#endif
9322}
9323
9324static bool
9325comment_at_top(struct parser_params *p)
9326{
9327 if (p->token_seen) return false;
9328 return (p->line_count == (p->has_shebang ? 2 : 1));
9329}
9330
9331typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9332typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9333
9334static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9335
9336static void
9337magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9338{
9339 if (!comment_at_top(p)) {
9340 return;
9341 }
9342 parser_set_encode(p, val);
9343}
9344
9345static int
9346parser_get_bool(struct parser_params *p, const char *name, const char *val)
9347{
9348 switch (*val) {
9349 case 't': case 'T':
9350 if (STRCASECMP(val, "true") == 0) {
9351 return TRUE;
9352 }
9353 break;
9354 case 'f': case 'F':
9355 if (STRCASECMP(val, "false") == 0) {
9356 return FALSE;
9357 }
9358 break;
9359 }
9360 return parser_invalid_pragma_value(p, name, val);
9361}
9362
9363static int
9364parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9365{
9366 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9367 return -1;
9368}
9369
9370static void
9371parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9372{
9373 int b = parser_get_bool(p, name, val);
9374 if (b >= 0) p->token_info_enabled = b;
9375}
9376
9377static void
9378parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9379{
9380 int b;
9381
9382 if (p->token_seen) {
9383 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9384 return;
9385 }
9386
9387 b = parser_get_bool(p, name, val);
9388 if (b < 0) return;
9389
9390 p->frozen_string_literal = b;
9391}
9392
9393static void
9394parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9395{
9396 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9397 if (*s == ' ' || *s == '\t') continue;
9398 if (*s == '#') break;
9399 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9400 return;
9401 }
9402
9403 switch (*val) {
9404 case 'n': case 'N':
9405 if (STRCASECMP(val, "none") == 0) {
9406 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9407 return;
9408 }
9409 break;
9410 case 'l': case 'L':
9411 if (STRCASECMP(val, "literal") == 0) {
9412 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9413 return;
9414 }
9415 break;
9416 case 'e': case 'E':
9417 if (STRCASECMP(val, "experimental_copy") == 0) {
9418 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9419 return;
9420 }
9421 if (STRCASECMP(val, "experimental_everything") == 0) {
9422 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9423 return;
9424 }
9425 break;
9426 }
9427 parser_invalid_pragma_value(p, name, val);
9428}
9429
9430# if WARN_PAST_SCOPE
9431static void
9432parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9433{
9434 int b = parser_get_bool(p, name, val);
9435 if (b >= 0) p->past_scope_enabled = b;
9436}
9437# endif
9438
9439struct magic_comment {
9440 const char *name;
9441 rb_magic_comment_setter_t func;
9442 rb_magic_comment_length_t length;
9443};
9444
9445static const struct magic_comment magic_comments[] = {
9446 {"coding", magic_comment_encoding, parser_encode_length},
9447 {"encoding", magic_comment_encoding, parser_encode_length},
9448 {"frozen_string_literal", parser_set_frozen_string_literal},
9449 {"shareable_constant_value", parser_set_shareable_constant_value},
9450 {"warn_indent", parser_set_token_info},
9451# if WARN_PAST_SCOPE
9452 {"warn_past_scope", parser_set_past_scope},
9453# endif
9454};
9455
9456static const char *
9457magic_comment_marker(const char *str, long len)
9458{
9459 long i = 2;
9460
9461 while (i < len) {
9462 switch (str[i]) {
9463 case '-':
9464 if (str[i-1] == '*' && str[i-2] == '-') {
9465 return str + i + 1;
9466 }
9467 i += 2;
9468 break;
9469 case '*':
9470 if (i + 1 >= len) return 0;
9471 if (str[i+1] != '-') {
9472 i += 4;
9473 }
9474 else if (str[i-1] != '-') {
9475 i += 2;
9476 }
9477 else {
9478 return str + i + 2;
9479 }
9480 break;
9481 default:
9482 i += 3;
9483 break;
9484 }
9485 }
9486 return 0;
9487}
9488
9489static int
9490parser_magic_comment(struct parser_params *p, const char *str, long len)
9491{
9492 int indicator = 0;
9493 VALUE name = 0, val = 0;
9494 const char *beg, *end, *vbeg, *vend;
9495#define str_copy(_s, _p, _n) ((_s) \
9496 ? (void)(rb_str_resize((_s), (_n)), \
9497 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9498 : (void)((_s) = STR_NEW((_p), (_n))))
9499
9500 if (len <= 7) return FALSE;
9501 if (!!(beg = magic_comment_marker(str, len))) {
9502 if (!(end = magic_comment_marker(beg, str + len - beg)))
9503 return FALSE;
9504 indicator = TRUE;
9505 str = beg;
9506 len = end - beg - 3;
9507 }
9508
9509 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9510 while (len > 0) {
9511 const struct magic_comment *mc = magic_comments;
9512 char *s;
9513 int i;
9514 long n = 0;
9515
9516 for (; len > 0 && *str; str++, --len) {
9517 switch (*str) {
9518 case '\'': case '"': case ':': case ';':
9519 continue;
9520 }
9521 if (!ISSPACE(*str)) break;
9522 }
9523 for (beg = str; len > 0; str++, --len) {
9524 switch (*str) {
9525 case '\'': case '"': case ':': case ';':
9526 break;
9527 default:
9528 if (ISSPACE(*str)) break;
9529 continue;
9530 }
9531 break;
9532 }
9533 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9534 if (!len) break;
9535 if (*str != ':') {
9536 if (!indicator) return FALSE;
9537 continue;
9538 }
9539
9540 do str++; while (--len > 0 && ISSPACE(*str));
9541 if (!len) break;
9542 const char *tok_beg = str;
9543 if (*str == '"') {
9544 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9545 if (*str == '\\') {
9546 --len;
9547 ++str;
9548 }
9549 }
9550 vend = str;
9551 if (len) {
9552 --len;
9553 ++str;
9554 }
9555 }
9556 else {
9557 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9558 vend = str;
9559 }
9560 const char *tok_end = str;
9561 if (indicator) {
9562 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9563 }
9564 else {
9565 while (len > 0 && (ISSPACE(*str))) --len, str++;
9566 if (len) return FALSE;
9567 }
9568
9569 n = end - beg;
9570 str_copy(name, beg, n);
9571 s = RSTRING_PTR(name);
9572 for (i = 0; i < n; ++i) {
9573 if (s[i] == '-') s[i] = '_';
9574 }
9575 do {
9576 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9577 n = vend - vbeg;
9578 if (mc->length) {
9579 n = (*mc->length)(p, vbeg, n);
9580 }
9581 str_copy(val, vbeg, n);
9582 p->lex.ptok = tok_beg;
9583 p->lex.pcur = tok_end;
9584 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9585 break;
9586 }
9587 } while (++mc < magic_comments + numberof(magic_comments));
9588#ifdef RIPPER
9589 str_copy(val, vbeg, vend - vbeg);
9590 dispatch2(magic_comment, name, val);
9591#endif
9592 }
9593
9594 return TRUE;
9595}
9596
9597static void
9598set_file_encoding(struct parser_params *p, const char *str, const char *send)
9599{
9600 int sep = 0;
9601 const char *beg = str;
9602 VALUE s;
9603
9604 for (;;) {
9605 if (send - str <= 6) return;
9606 switch (str[6]) {
9607 case 'C': case 'c': str += 6; continue;
9608 case 'O': case 'o': str += 5; continue;
9609 case 'D': case 'd': str += 4; continue;
9610 case 'I': case 'i': str += 3; continue;
9611 case 'N': case 'n': str += 2; continue;
9612 case 'G': case 'g': str += 1; continue;
9613 case '=': case ':':
9614 sep = 1;
9615 str += 6;
9616 break;
9617 default:
9618 str += 6;
9619 if (ISSPACE(*str)) break;
9620 continue;
9621 }
9622 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9623 sep = 0;
9624 }
9625 for (;;) {
9626 do {
9627 if (++str >= send) return;
9628 } while (ISSPACE(*str));
9629 if (sep) break;
9630 if (*str != '=' && *str != ':') return;
9631 sep = 1;
9632 str++;
9633 }
9634 beg = str;
9635 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9636 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9637 p->lex.ptok = beg;
9638 p->lex.pcur = str;
9639 parser_set_encode(p, RSTRING_PTR(s));
9640 rb_str_resize(s, 0);
9641}
9642
9643static void
9644parser_prepare(struct parser_params *p)
9645{
9646 int c = nextc0(p, FALSE);
9647 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9648 switch (c) {
9649 case '#':
9650 if (peek(p, '!')) p->has_shebang = 1;
9651 break;
9652 case 0xef: /* UTF-8 BOM marker */
9653 if (!lex_eol_n_p(p, 2) &&
9654 (unsigned char)p->lex.pcur[0] == 0xbb &&
9655 (unsigned char)p->lex.pcur[1] == 0xbf) {
9656 p->enc = rb_utf8_encoding();
9657 p->lex.pcur += 2;
9658#ifndef RIPPER
9659 if (p->debug_lines) {
9660 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9661 }
9662#endif
9663 p->lex.pbeg = p->lex.pcur;
9664 token_flush(p);
9665 return;
9666 }
9667 break;
9668 case -1: /* end of script. */
9669 return;
9670 }
9671 pushback(p, c);
9672 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9673}
9674
9675#ifndef RIPPER
9676#define ambiguous_operator(tok, op, syn) ( \
9677 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9678 rb_warning0("even though it seems like "syn""))
9679#else
9680#define ambiguous_operator(tok, op, syn) \
9681 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9682#endif
9683#define warn_balanced(tok, op, syn) ((void) \
9684 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9685 space_seen && !ISSPACE(c) && \
9686 (ambiguous_operator(tok, op, syn), 0)), \
9687 (enum yytokentype)(tok))
9688
9689static enum yytokentype
9690no_digits(struct parser_params *p)
9691{
9692 yyerror0("numeric literal without digits");
9693 if (peek(p, '_')) nextc(p);
9694 /* dummy 0, for tUMINUS_NUM at numeric */
9695 return set_number_literal(p, tINTEGER, 0, 10, 0);
9696}
9697
9698static enum yytokentype
9699parse_numeric(struct parser_params *p, int c)
9700{
9701 int is_float, seen_point, seen_e, nondigit;
9702 int suffix;
9703
9704 is_float = seen_point = seen_e = nondigit = 0;
9705 SET_LEX_STATE(EXPR_END);
9706 newtok(p);
9707 if (c == '-' || c == '+') {
9708 tokadd(p, c);
9709 c = nextc(p);
9710 }
9711 if (c == '0') {
9712 int start = toklen(p);
9713 c = nextc(p);
9714 if (c == 'x' || c == 'X') {
9715 /* hexadecimal */
9716 c = nextc(p);
9717 if (c != -1 && ISXDIGIT(c)) {
9718 do {
9719 if (c == '_') {
9720 if (nondigit) break;
9721 nondigit = c;
9722 continue;
9723 }
9724 if (!ISXDIGIT(c)) break;
9725 nondigit = 0;
9726 tokadd(p, c);
9727 } while ((c = nextc(p)) != -1);
9728 }
9729 pushback(p, c);
9730 tokfix(p);
9731 if (toklen(p) == start) {
9732 return no_digits(p);
9733 }
9734 else if (nondigit) goto trailing_uc;
9735 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9736 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9737 }
9738 if (c == 'b' || c == 'B') {
9739 /* binary */
9740 c = nextc(p);
9741 if (c == '0' || c == '1') {
9742 do {
9743 if (c == '_') {
9744 if (nondigit) break;
9745 nondigit = c;
9746 continue;
9747 }
9748 if (c != '0' && c != '1') break;
9749 nondigit = 0;
9750 tokadd(p, c);
9751 } while ((c = nextc(p)) != -1);
9752 }
9753 pushback(p, c);
9754 tokfix(p);
9755 if (toklen(p) == start) {
9756 return no_digits(p);
9757 }
9758 else if (nondigit) goto trailing_uc;
9759 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9760 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9761 }
9762 if (c == 'd' || c == 'D') {
9763 /* decimal */
9764 c = nextc(p);
9765 if (c != -1 && ISDIGIT(c)) {
9766 do {
9767 if (c == '_') {
9768 if (nondigit) break;
9769 nondigit = c;
9770 continue;
9771 }
9772 if (!ISDIGIT(c)) break;
9773 nondigit = 0;
9774 tokadd(p, c);
9775 } while ((c = nextc(p)) != -1);
9776 }
9777 pushback(p, c);
9778 tokfix(p);
9779 if (toklen(p) == start) {
9780 return no_digits(p);
9781 }
9782 else if (nondigit) goto trailing_uc;
9783 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9784 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9785 }
9786 if (c == '_') {
9787 /* 0_0 */
9788 goto octal_number;
9789 }
9790 if (c == 'o' || c == 'O') {
9791 /* prefixed octal */
9792 c = nextc(p);
9793 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9794 tokfix(p);
9795 return no_digits(p);
9796 }
9797 }
9798 if (c >= '0' && c <= '7') {
9799 /* octal */
9800 octal_number:
9801 do {
9802 if (c == '_') {
9803 if (nondigit) break;
9804 nondigit = c;
9805 continue;
9806 }
9807 if (c < '0' || c > '9') break;
9808 if (c > '7') goto invalid_octal;
9809 nondigit = 0;
9810 tokadd(p, c);
9811 } while ((c = nextc(p)) != -1);
9812 if (toklen(p) > start) {
9813 pushback(p, c);
9814 tokfix(p);
9815 if (nondigit) goto trailing_uc;
9816 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9817 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9818 }
9819 if (nondigit) {
9820 pushback(p, c);
9821 goto trailing_uc;
9822 }
9823 }
9824 if (c > '7' && c <= '9') {
9825 invalid_octal:
9826 yyerror0("Invalid octal digit");
9827 }
9828 else if (c == '.' || c == 'e' || c == 'E') {
9829 tokadd(p, '0');
9830 }
9831 else {
9832 pushback(p, c);
9833 tokfix(p);
9834 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9835 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9836 }
9837 }
9838
9839 for (;;) {
9840 switch (c) {
9841 case '0': case '1': case '2': case '3': case '4':
9842 case '5': case '6': case '7': case '8': case '9':
9843 nondigit = 0;
9844 tokadd(p, c);
9845 break;
9846
9847 case '.':
9848 if (nondigit) goto trailing_uc;
9849 if (seen_point || seen_e) {
9850 goto decode_num;
9851 }
9852 else {
9853 int c0 = nextc(p);
9854 if (c0 == -1 || !ISDIGIT(c0)) {
9855 pushback(p, c0);
9856 goto decode_num;
9857 }
9858 c = c0;
9859 }
9860 seen_point = toklen(p);
9861 tokadd(p, '.');
9862 tokadd(p, c);
9863 is_float++;
9864 nondigit = 0;
9865 break;
9866
9867 case 'e':
9868 case 'E':
9869 if (nondigit) {
9870 pushback(p, c);
9871 c = nondigit;
9872 goto decode_num;
9873 }
9874 if (seen_e) {
9875 goto decode_num;
9876 }
9877 nondigit = c;
9878 c = nextc(p);
9879 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9880 pushback(p, c);
9881 c = nondigit;
9882 nondigit = 0;
9883 goto decode_num;
9884 }
9885 tokadd(p, nondigit);
9886 seen_e++;
9887 is_float++;
9888 tokadd(p, c);
9889 nondigit = (c == '-' || c == '+') ? c : 0;
9890 break;
9891
9892 case '_': /* `_' in number just ignored */
9893 if (nondigit) goto decode_num;
9894 nondigit = c;
9895 break;
9896
9897 default:
9898 goto decode_num;
9899 }
9900 c = nextc(p);
9901 }
9902
9903 decode_num:
9904 pushback(p, c);
9905 if (nondigit) {
9906 trailing_uc:
9907 literal_flush(p, p->lex.pcur - 1);
9908 YYLTYPE loc = RUBY_INIT_YYLLOC();
9909 compile_error(p, "trailing '%c' in number", nondigit);
9910 parser_show_error_line(p, &loc);
9911 }
9912 tokfix(p);
9913 if (is_float) {
9914 enum yytokentype type = tFLOAT;
9915
9916 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9917 if (suffix & NUM_SUFFIX_R) {
9918 type = tRATIONAL;
9919 }
9920 else {
9921 strtod(tok(p), 0);
9922 if (errno == ERANGE) {
9923 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9924 errno = 0;
9925 }
9926 }
9927 return set_number_literal(p, type, suffix, 0, seen_point);
9928 }
9929 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9930 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9931}
9932
9933static enum yytokentype
9934parse_qmark(struct parser_params *p, int space_seen)
9935{
9936 rb_encoding *enc;
9937 register int c;
9938 rb_parser_string_t *lit;
9939 const char *start = p->lex.pcur;
9940
9941 if (IS_END()) {
9942 SET_LEX_STATE(EXPR_VALUE);
9943 return '?';
9944 }
9945 c = nextc(p);
9946 if (c == -1) {
9947 compile_error(p, "incomplete character syntax");
9948 return 0;
9949 }
9950 if (rb_enc_isspace(c, p->enc)) {
9951 if (!IS_ARG()) {
9952 int c2 = escaped_control_code(c);
9953 if (c2) {
9954 WARN_SPACE_CHAR(c2, "?");
9955 }
9956 }
9957 ternary:
9958 pushback(p, c);
9959 SET_LEX_STATE(EXPR_VALUE);
9960 return '?';
9961 }
9962 newtok(p);
9963 enc = p->enc;
9964 int w = parser_precise_mbclen(p, start);
9965 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9966 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9967 if (space_seen) {
9968 const char *ptr = start;
9969 do {
9970 int n = parser_precise_mbclen(p, ptr);
9971 if (n < 0) return -1;
9972 ptr += n;
9973 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9974 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9975 " a conditional operator, put a space after '?'",
9976 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9977 }
9978 goto ternary;
9979 }
9980 else if (c == '\\') {
9981 if (peek(p, 'u')) {
9982 nextc(p);
9983 enc = rb_utf8_encoding();
9984 tokadd_utf8(p, &enc, -1, 0, 0);
9985 }
9986 else if (!ISASCII(c = peekc(p)) && c != -1) {
9987 nextc(p);
9988 if (tokadd_mbchar(p, c) == -1) return 0;
9989 }
9990 else {
9991 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9992 tokadd(p, c);
9993 }
9994 }
9995 else {
9996 if (tokadd_mbchar(p, c) == -1) return 0;
9997 }
9998 tokfix(p);
9999 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10000 set_yylval_str(lit);
10001 SET_LEX_STATE(EXPR_END);
10002 return tCHAR;
10003}
10004
10005static enum yytokentype
10006parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10007{
10008 register int c;
10009 const char *ptok = p->lex.pcur;
10010
10011 if (IS_BEG()) {
10012 int term;
10013 int paren;
10014
10015 c = nextc(p);
10016 quotation:
10017 if (c == -1) goto unterminated;
10018 if (!ISALNUM(c)) {
10019 term = c;
10020 if (!ISASCII(c)) goto unknown;
10021 c = 'Q';
10022 }
10023 else {
10024 term = nextc(p);
10025 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10026 unknown:
10027 pushback(p, term);
10028 c = parser_precise_mbclen(p, p->lex.pcur);
10029 if (c < 0) return 0;
10030 p->lex.pcur += c;
10031 yyerror0("unknown type of %string");
10032 return 0;
10033 }
10034 }
10035 if (term == -1) {
10036 unterminated:
10037 compile_error(p, "unterminated quoted string meets end of file");
10038 return 0;
10039 }
10040 paren = term;
10041 if (term == '(') term = ')';
10042 else if (term == '[') term = ']';
10043 else if (term == '{') term = '}';
10044 else if (term == '<') term = '>';
10045 else paren = 0;
10046
10047 p->lex.ptok = ptok-1;
10048 switch (c) {
10049 case 'Q':
10050 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10051 return tSTRING_BEG;
10052
10053 case 'q':
10054 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10055 return tSTRING_BEG;
10056
10057 case 'W':
10058 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10059 return tWORDS_BEG;
10060
10061 case 'w':
10062 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10063 return tQWORDS_BEG;
10064
10065 case 'I':
10066 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10067 return tSYMBOLS_BEG;
10068
10069 case 'i':
10070 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10071 return tQSYMBOLS_BEG;
10072
10073 case 'x':
10074 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10075 return tXSTRING_BEG;
10076
10077 case 'r':
10078 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10079 return tREGEXP_BEG;
10080
10081 case 's':
10082 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10083 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10084 return tSYMBEG;
10085
10086 default:
10087 yyerror0("unknown type of %string");
10088 return 0;
10089 }
10090 }
10091 if ((c = nextc(p)) == '=') {
10092 set_yylval_id('%');
10093 SET_LEX_STATE(EXPR_BEG);
10094 return tOP_ASGN;
10095 }
10096 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10097 goto quotation;
10098 }
10099 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10100 pushback(p, c);
10101 return warn_balanced('%', "%%", "string literal");
10102}
10103
10104static int
10105tokadd_ident(struct parser_params *p, int c)
10106{
10107 do {
10108 if (tokadd_mbchar(p, c) == -1) return -1;
10109 c = nextc(p);
10110 } while (parser_is_identchar(p));
10111 pushback(p, c);
10112 return 0;
10113}
10114
10115static ID
10116tokenize_ident(struct parser_params *p)
10117{
10118 ID ident = TOK_INTERN();
10119
10120 set_yylval_name(ident);
10121
10122 return ident;
10123}
10124
10125static int
10126parse_numvar(struct parser_params *p)
10127{
10128 size_t len;
10129 int overflow;
10130 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10131 const unsigned long nth_ref_max =
10132 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10133 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10134 * turned into a Fixnum, in compile.c */
10135
10136 if (overflow || n > nth_ref_max) {
10137 /* compile_error()? */
10138 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10139 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10140 }
10141 else {
10142 return (int)n;
10143 }
10144}
10145
10146static enum yytokentype
10147parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10148{
10149 const char *ptr = p->lex.pcur;
10150 register int c;
10151
10152 SET_LEX_STATE(EXPR_END);
10153 p->lex.ptok = ptr - 1; /* from '$' */
10154 newtok(p);
10155 c = nextc(p);
10156 switch (c) {
10157 case '_': /* $_: last read line string */
10158 c = nextc(p);
10159 if (parser_is_identchar(p)) {
10160 tokadd(p, '$');
10161 tokadd(p, '_');
10162 break;
10163 }
10164 pushback(p, c);
10165 c = '_';
10166 /* fall through */
10167 case '~': /* $~: match-data */
10168 case '*': /* $*: argv */
10169 case '$': /* $$: pid */
10170 case '?': /* $?: last status */
10171 case '!': /* $!: error string */
10172 case '@': /* $@: error position */
10173 case '/': /* $/: input record separator */
10174 case '\\': /* $\: output record separator */
10175 case ';': /* $;: field separator */
10176 case ',': /* $,: output field separator */
10177 case '.': /* $.: last read line number */
10178 case '=': /* $=: ignorecase */
10179 case ':': /* $:: load path */
10180 case '<': /* $<: default input handle */
10181 case '>': /* $>: default output handle */
10182 case '\"': /* $": already loaded files */
10183 tokadd(p, '$');
10184 tokadd(p, c);
10185 goto gvar;
10186
10187 case '-':
10188 tokadd(p, '$');
10189 tokadd(p, c);
10190 c = nextc(p);
10191 if (parser_is_identchar(p)) {
10192 if (tokadd_mbchar(p, c) == -1) return 0;
10193 }
10194 else {
10195 pushback(p, c);
10196 pushback(p, '-');
10197 return '$';
10198 }
10199 gvar:
10200 tokenize_ident(p);
10201 return tGVAR;
10202
10203 case '&': /* $&: last match */
10204 case '`': /* $`: string before last match */
10205 case '\'': /* $': string after last match */
10206 case '+': /* $+: string matches last paren. */
10207 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10208 tokadd(p, '$');
10209 tokadd(p, c);
10210 goto gvar;
10211 }
10212 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10213 return tBACK_REF;
10214
10215 case '1': case '2': case '3':
10216 case '4': case '5': case '6':
10217 case '7': case '8': case '9':
10218 tokadd(p, '$');
10219 do {
10220 tokadd(p, c);
10221 c = nextc(p);
10222 } while (c != -1 && ISDIGIT(c));
10223 pushback(p, c);
10224 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10225 tokfix(p);
10226 c = parse_numvar(p);
10227 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10228 return tNTH_REF;
10229
10230 default:
10231 if (!parser_is_identchar(p)) {
10232 YYLTYPE loc = RUBY_INIT_YYLLOC();
10233 if (c == -1 || ISSPACE(c)) {
10234 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10235 }
10236 else {
10237 pushback(p, c);
10238 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10239 }
10240 parser_show_error_line(p, &loc);
10241 set_yylval_noname();
10242 return tGVAR;
10243 }
10244 /* fall through */
10245 case '0':
10246 tokadd(p, '$');
10247 }
10248
10249 if (tokadd_ident(p, c)) return 0;
10250 SET_LEX_STATE(EXPR_END);
10251 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10252 tokenize_ident(p);
10253 }
10254 else {
10255 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10256 set_yylval_noname();
10257 }
10258 return tGVAR;
10259}
10260
10261static bool
10262parser_numbered_param(struct parser_params *p, int n)
10263{
10264 if (n < 0) return false;
10265
10266 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10267 return false;
10268 }
10269 if (p->max_numparam == ORDINAL_PARAM) {
10270 compile_error(p, "ordinary parameter is defined");
10271 return false;
10272 }
10273 struct vtable *args = p->lvtbl->args;
10274 if (p->max_numparam < n) {
10275 p->max_numparam = n;
10276 }
10277 while (n > args->pos) {
10278 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10279 }
10280 return true;
10281}
10282
10283static enum yytokentype
10284parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10285{
10286 const char *ptr = p->lex.pcur;
10287 enum yytokentype result = tIVAR;
10288 register int c = nextc(p);
10289 YYLTYPE loc;
10290
10291 p->lex.ptok = ptr - 1; /* from '@' */
10292 newtok(p);
10293 tokadd(p, '@');
10294 if (c == '@') {
10295 result = tCVAR;
10296 tokadd(p, '@');
10297 c = nextc(p);
10298 }
10299 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10300 if (c == -1 || !parser_is_identchar(p)) {
10301 pushback(p, c);
10302 RUBY_SET_YYLLOC(loc);
10303 if (result == tIVAR) {
10304 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10305 }
10306 else {
10307 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10308 }
10309 parser_show_error_line(p, &loc);
10310 set_yylval_noname();
10311 SET_LEX_STATE(EXPR_END);
10312 return result;
10313 }
10314 else if (ISDIGIT(c)) {
10315 pushback(p, c);
10316 RUBY_SET_YYLLOC(loc);
10317 if (result == tIVAR) {
10318 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10319 }
10320 else {
10321 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10322 }
10323 parser_show_error_line(p, &loc);
10324 set_yylval_noname();
10325 SET_LEX_STATE(EXPR_END);
10326 return result;
10327 }
10328
10329 if (tokadd_ident(p, c)) return 0;
10330 tokenize_ident(p);
10331 return result;
10332}
10333
10334static enum yytokentype
10335parse_ident(struct parser_params *p, int c, int cmd_state)
10336{
10337 enum yytokentype result;
10338 bool is_ascii = true;
10339 const enum lex_state_e last_state = p->lex.state;
10340 ID ident;
10341 int enforce_keyword_end = 0;
10342
10343 do {
10344 if (!ISASCII(c)) is_ascii = false;
10345 if (tokadd_mbchar(p, c) == -1) return 0;
10346 c = nextc(p);
10347 } while (parser_is_identchar(p));
10348 if ((c == '!' || c == '?') && !peek(p, '=')) {
10349 result = tFID;
10350 tokadd(p, c);
10351 }
10352 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10353 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10354 result = tIDENTIFIER;
10355 tokadd(p, c);
10356 }
10357 else {
10358 result = tCONSTANT; /* assume provisionally */
10359 pushback(p, c);
10360 }
10361 tokfix(p);
10362
10363 if (IS_LABEL_POSSIBLE()) {
10364 if (IS_LABEL_SUFFIX(0)) {
10365 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10366 nextc(p);
10367 tokenize_ident(p);
10368 return tLABEL;
10369 }
10370 }
10371
10372#ifndef RIPPER
10373 if (peek_end_expect_token_locations(p)) {
10374 const rb_code_position_t *end_pos;
10375 int lineno, column;
10376 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10377
10378 end_pos = peek_end_expect_token_locations(p)->pos;
10379 lineno = end_pos->lineno;
10380 column = end_pos->column;
10381
10382 if (p->debug) {
10383 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10384 p->ruby_sourceline, beg_pos, lineno, column);
10385 }
10386
10387 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10388 const struct kwtable *kw;
10389
10390 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10391 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10392 enforce_keyword_end = 1;
10393 }
10394 }
10395 }
10396#endif
10397
10398 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10399 const struct kwtable *kw;
10400
10401 /* See if it is a reserved word. */
10402 kw = rb_reserved_word(tok(p), toklen(p));
10403 if (kw) {
10404 enum lex_state_e state = p->lex.state;
10405 if (IS_lex_state_for(state, EXPR_FNAME)) {
10406 SET_LEX_STATE(EXPR_ENDFN);
10407 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10408 return kw->id[0];
10409 }
10410 SET_LEX_STATE(kw->state);
10411 if (IS_lex_state(EXPR_BEG)) {
10412 p->command_start = TRUE;
10413 }
10414 if (kw->id[0] == keyword_do) {
10415 if (lambda_beginning_p()) {
10416 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10417 return keyword_do_LAMBDA;
10418 }
10419 if (COND_P()) return keyword_do_cond;
10420 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10421 return keyword_do_block;
10422 return keyword_do;
10423 }
10424 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10425 return kw->id[0];
10426 else {
10427 if (kw->id[0] != kw->id[1])
10428 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10429 return kw->id[1];
10430 }
10431 }
10432 }
10433
10434 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10435 if (cmd_state) {
10436 SET_LEX_STATE(EXPR_CMDARG);
10437 }
10438 else {
10439 SET_LEX_STATE(EXPR_ARG);
10440 }
10441 }
10442 else if (p->lex.state == EXPR_FNAME) {
10443 SET_LEX_STATE(EXPR_ENDFN);
10444 }
10445 else {
10446 SET_LEX_STATE(EXPR_END);
10447 }
10448
10449 ident = tokenize_ident(p);
10450 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10451 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10452 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10453 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10454 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10455 }
10456 return result;
10457}
10458
10459static void
10460warn_cr(struct parser_params *p)
10461{
10462 if (!p->cr_seen) {
10463 p->cr_seen = TRUE;
10464 /* carried over with p->lex.nextline for nextc() */
10465 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10466 }
10467}
10468
10469static enum yytokentype
10470parser_yylex(struct parser_params *p)
10471{
10472 register int c;
10473 int space_seen = 0;
10474 int cmd_state;
10475 int label;
10476 enum lex_state_e last_state;
10477 int fallthru = FALSE;
10478 int token_seen = p->token_seen;
10479
10480 if (p->lex.strterm) {
10481 if (strterm_is_heredoc(p->lex.strterm)) {
10482 token_flush(p);
10483 return here_document(p, &p->lex.strterm->u.heredoc);
10484 }
10485 else {
10486 token_flush(p);
10487 return parse_string(p, &p->lex.strterm->u.literal);
10488 }
10489 }
10490 cmd_state = p->command_start;
10491 p->command_start = FALSE;
10492 p->token_seen = TRUE;
10493#ifndef RIPPER
10494 token_flush(p);
10495#endif
10496 retry:
10497 last_state = p->lex.state;
10498 switch (c = nextc(p)) {
10499 case '\0': /* NUL */
10500 case '\004': /* ^D */
10501 case '\032': /* ^Z */
10502 case -1: /* end of script. */
10503 p->eofp = 1;
10504#ifndef RIPPER
10505 if (p->end_expect_token_locations) {
10506 pop_end_expect_token_locations(p);
10507 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10508 return tDUMNY_END;
10509 }
10510#endif
10511 /* Set location for end-of-input because dispatch_scan_event is not called. */
10512 RUBY_SET_YYLLOC(*p->yylloc);
10513 return END_OF_INPUT;
10514
10515 /* white spaces */
10516 case '\r':
10517 warn_cr(p);
10518 /* fall through */
10519 case ' ': case '\t': case '\f':
10520 case '\13': /* '\v' */
10521 space_seen = 1;
10522 while ((c = nextc(p))) {
10523 switch (c) {
10524 case '\r':
10525 warn_cr(p);
10526 /* fall through */
10527 case ' ': case '\t': case '\f':
10528 case '\13': /* '\v' */
10529 break;
10530 default:
10531 goto outofloop;
10532 }
10533 }
10534 outofloop:
10535 pushback(p, c);
10536 dispatch_scan_event(p, tSP);
10537#ifndef RIPPER
10538 token_flush(p);
10539#endif
10540 goto retry;
10541
10542 case '#': /* it's a comment */
10543 p->token_seen = token_seen;
10544 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10545 /* no magic_comment in shebang line */
10546 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10547 if (comment_at_top(p)) {
10548 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10549 }
10550 }
10551 p->lex.pcur = pcur, p->lex.ptok = ptok;
10552 lex_goto_eol(p);
10553 dispatch_scan_event(p, tCOMMENT);
10554 fallthru = TRUE;
10555 /* fall through */
10556 case '\n':
10557 p->token_seen = token_seen;
10558 rb_parser_string_t *prevline = p->lex.lastline;
10559 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10560 !IS_lex_state(EXPR_LABELED));
10561 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10562 if (!fallthru) {
10563 dispatch_scan_event(p, tIGNORED_NL);
10564 }
10565 fallthru = FALSE;
10566 if (!c && p->ctxt.in_kwarg) {
10567 goto normal_newline;
10568 }
10569 goto retry;
10570 }
10571 while (1) {
10572 switch (c = nextc(p)) {
10573 case ' ': case '\t': case '\f': case '\r':
10574 case '\13': /* '\v' */
10575 space_seen = 1;
10576 break;
10577 case '#':
10578 pushback(p, c);
10579 if (space_seen) {
10580 dispatch_scan_event(p, tSP);
10581 token_flush(p);
10582 }
10583 goto retry;
10584 case 'a':
10585 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10586 goto bol;
10587 case 'o':
10588 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10589 goto bol;
10590 case '|':
10591 if (peek(p, '|')) goto leading_logical;
10592 goto bol;
10593 case '&':
10594 if (peek(p, '&')) {
10595 leading_logical:
10596 pushback(p, c);
10597 dispatch_delayed_token(p, tIGNORED_NL);
10598 cmd_state = FALSE;
10599 goto retry;
10600 }
10601 /* fall through */
10602 case '.': {
10603 dispatch_delayed_token(p, tIGNORED_NL);
10604 if (peek(p, '.') == (c == '&')) {
10605 pushback(p, c);
10606 dispatch_scan_event(p, tSP);
10607 goto retry;
10608 }
10609 }
10610 bol:
10611 default:
10612 p->ruby_sourceline--;
10613 p->lex.nextline = p->lex.lastline;
10614 set_lastline(p, prevline);
10615 case -1: /* EOF no decrement*/
10616 if (c == -1 && space_seen) {
10617 dispatch_scan_event(p, tSP);
10618 }
10619 lex_goto_eol(p);
10620 if (c != -1) {
10621 token_flush(p);
10622 RUBY_SET_YYLLOC(*p->yylloc);
10623 }
10624 goto normal_newline;
10625 }
10626 }
10627 normal_newline:
10628 p->command_start = TRUE;
10629 SET_LEX_STATE(EXPR_BEG);
10630 return '\n';
10631
10632 case '*':
10633 if ((c = nextc(p)) == '*') {
10634 if ((c = nextc(p)) == '=') {
10635 set_yylval_id(idPow);
10636 SET_LEX_STATE(EXPR_BEG);
10637 return tOP_ASGN;
10638 }
10639 pushback(p, c);
10640 if (IS_SPCARG(c)) {
10641 rb_warning0("'**' interpreted as argument prefix");
10642 c = tDSTAR;
10643 }
10644 else if (IS_BEG()) {
10645 c = tDSTAR;
10646 }
10647 else {
10648 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10649 }
10650 }
10651 else {
10652 if (c == '=') {
10653 set_yylval_id('*');
10654 SET_LEX_STATE(EXPR_BEG);
10655 return tOP_ASGN;
10656 }
10657 pushback(p, c);
10658 if (IS_SPCARG(c)) {
10659 rb_warning0("'*' interpreted as argument prefix");
10660 c = tSTAR;
10661 }
10662 else if (IS_BEG()) {
10663 c = tSTAR;
10664 }
10665 else {
10666 c = warn_balanced('*', "*", "argument prefix");
10667 }
10668 }
10669 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10670 return c;
10671
10672 case '!':
10673 c = nextc(p);
10674 if (IS_AFTER_OPERATOR()) {
10675 SET_LEX_STATE(EXPR_ARG);
10676 if (c == '@') {
10677 return '!';
10678 }
10679 }
10680 else {
10681 SET_LEX_STATE(EXPR_BEG);
10682 }
10683 if (c == '=') {
10684 return tNEQ;
10685 }
10686 if (c == '~') {
10687 return tNMATCH;
10688 }
10689 pushback(p, c);
10690 return '!';
10691
10692 case '=':
10693 if (was_bol(p)) {
10694 /* skip embedded rd document */
10695 if (word_match_p(p, "begin", 5)) {
10696 int first_p = TRUE;
10697
10698 lex_goto_eol(p);
10699 dispatch_scan_event(p, tEMBDOC_BEG);
10700 for (;;) {
10701 lex_goto_eol(p);
10702 if (!first_p) {
10703 dispatch_scan_event(p, tEMBDOC);
10704 }
10705 first_p = FALSE;
10706 c = nextc(p);
10707 if (c == -1) {
10708 compile_error(p, "embedded document meets end of file");
10709 return END_OF_INPUT;
10710 }
10711 if (c == '=' && word_match_p(p, "end", 3)) {
10712 break;
10713 }
10714 pushback(p, c);
10715 }
10716 lex_goto_eol(p);
10717 dispatch_scan_event(p, tEMBDOC_END);
10718 goto retry;
10719 }
10720 }
10721
10722 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10723 if ((c = nextc(p)) == '=') {
10724 if ((c = nextc(p)) == '=') {
10725 return tEQQ;
10726 }
10727 pushback(p, c);
10728 return tEQ;
10729 }
10730 if (c == '~') {
10731 return tMATCH;
10732 }
10733 else if (c == '>') {
10734 return tASSOC;
10735 }
10736 pushback(p, c);
10737 return '=';
10738
10739 case '<':
10740 c = nextc(p);
10741 if (c == '<' &&
10742 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10743 !IS_END() &&
10744 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10745 enum yytokentype token = heredoc_identifier(p);
10746 if (token) return token < 0 ? 0 : token;
10747 }
10748 if (IS_AFTER_OPERATOR()) {
10749 SET_LEX_STATE(EXPR_ARG);
10750 }
10751 else {
10752 if (IS_lex_state(EXPR_CLASS))
10753 p->command_start = TRUE;
10754 SET_LEX_STATE(EXPR_BEG);
10755 }
10756 if (c == '=') {
10757 if ((c = nextc(p)) == '>') {
10758 return tCMP;
10759 }
10760 pushback(p, c);
10761 return tLEQ;
10762 }
10763 if (c == '<') {
10764 if ((c = nextc(p)) == '=') {
10765 set_yylval_id(idLTLT);
10766 SET_LEX_STATE(EXPR_BEG);
10767 return tOP_ASGN;
10768 }
10769 pushback(p, c);
10770 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10771 }
10772 pushback(p, c);
10773 return '<';
10774
10775 case '>':
10776 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10777 if ((c = nextc(p)) == '=') {
10778 return tGEQ;
10779 }
10780 if (c == '>') {
10781 if ((c = nextc(p)) == '=') {
10782 set_yylval_id(idGTGT);
10783 SET_LEX_STATE(EXPR_BEG);
10784 return tOP_ASGN;
10785 }
10786 pushback(p, c);
10787 return tRSHFT;
10788 }
10789 pushback(p, c);
10790 return '>';
10791
10792 case '"':
10793 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10794 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10795 p->lex.ptok = p->lex.pcur-1;
10796 return tSTRING_BEG;
10797
10798 case '`':
10799 if (IS_lex_state(EXPR_FNAME)) {
10800 SET_LEX_STATE(EXPR_ENDFN);
10801 return c;
10802 }
10803 if (IS_lex_state(EXPR_DOT)) {
10804 if (cmd_state)
10805 SET_LEX_STATE(EXPR_CMDARG);
10806 else
10807 SET_LEX_STATE(EXPR_ARG);
10808 return c;
10809 }
10810 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10811 return tXSTRING_BEG;
10812
10813 case '\'':
10814 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10815 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10816 p->lex.ptok = p->lex.pcur-1;
10817 return tSTRING_BEG;
10818
10819 case '?':
10820 return parse_qmark(p, space_seen);
10821
10822 case '&':
10823 if ((c = nextc(p)) == '&') {
10824 SET_LEX_STATE(EXPR_BEG);
10825 if ((c = nextc(p)) == '=') {
10826 set_yylval_id(idANDOP);
10827 SET_LEX_STATE(EXPR_BEG);
10828 return tOP_ASGN;
10829 }
10830 pushback(p, c);
10831 return tANDOP;
10832 }
10833 else if (c == '=') {
10834 set_yylval_id('&');
10835 SET_LEX_STATE(EXPR_BEG);
10836 return tOP_ASGN;
10837 }
10838 else if (c == '.') {
10839 set_yylval_id(idANDDOT);
10840 SET_LEX_STATE(EXPR_DOT);
10841 return tANDDOT;
10842 }
10843 pushback(p, c);
10844 if (IS_SPCARG(c)) {
10845 if ((c != ':') ||
10846 (c = peekc_n(p, 1)) == -1 ||
10847 !(c == '\'' || c == '"' ||
10848 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10849 rb_warning0("'&' interpreted as argument prefix");
10850 }
10851 c = tAMPER;
10852 }
10853 else if (IS_BEG()) {
10854 c = tAMPER;
10855 }
10856 else {
10857 c = warn_balanced('&', "&", "argument prefix");
10858 }
10859 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10860 return c;
10861
10862 case '|':
10863 if ((c = nextc(p)) == '|') {
10864 SET_LEX_STATE(EXPR_BEG);
10865 if ((c = nextc(p)) == '=') {
10866 set_yylval_id(idOROP);
10867 SET_LEX_STATE(EXPR_BEG);
10868 return tOP_ASGN;
10869 }
10870 pushback(p, c);
10871 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10872 c = '|';
10873 pushback(p, '|');
10874 return c;
10875 }
10876 return tOROP;
10877 }
10878 if (c == '=') {
10879 set_yylval_id('|');
10880 SET_LEX_STATE(EXPR_BEG);
10881 return tOP_ASGN;
10882 }
10883 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10884 pushback(p, c);
10885 return '|';
10886
10887 case '+':
10888 c = nextc(p);
10889 if (IS_AFTER_OPERATOR()) {
10890 SET_LEX_STATE(EXPR_ARG);
10891 if (c == '@') {
10892 return tUPLUS;
10893 }
10894 pushback(p, c);
10895 return '+';
10896 }
10897 if (c == '=') {
10898 set_yylval_id('+');
10899 SET_LEX_STATE(EXPR_BEG);
10900 return tOP_ASGN;
10901 }
10902 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10903 SET_LEX_STATE(EXPR_BEG);
10904 pushback(p, c);
10905 if (c != -1 && ISDIGIT(c)) {
10906 return parse_numeric(p, '+');
10907 }
10908 return tUPLUS;
10909 }
10910 SET_LEX_STATE(EXPR_BEG);
10911 pushback(p, c);
10912 return warn_balanced('+', "+", "unary operator");
10913
10914 case '-':
10915 c = nextc(p);
10916 if (IS_AFTER_OPERATOR()) {
10917 SET_LEX_STATE(EXPR_ARG);
10918 if (c == '@') {
10919 return tUMINUS;
10920 }
10921 pushback(p, c);
10922 return '-';
10923 }
10924 if (c == '=') {
10925 set_yylval_id('-');
10926 SET_LEX_STATE(EXPR_BEG);
10927 return tOP_ASGN;
10928 }
10929 if (c == '>') {
10930 SET_LEX_STATE(EXPR_ENDFN);
10931 yylval.num = p->lex.lpar_beg;
10932 p->lex.lpar_beg = p->lex.paren_nest;
10933 return tLAMBDA;
10934 }
10935 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10936 SET_LEX_STATE(EXPR_BEG);
10937 pushback(p, c);
10938 if (c != -1 && ISDIGIT(c)) {
10939 return tUMINUS_NUM;
10940 }
10941 return tUMINUS;
10942 }
10943 SET_LEX_STATE(EXPR_BEG);
10944 pushback(p, c);
10945 return warn_balanced('-', "-", "unary operator");
10946
10947 case '.': {
10948 int is_beg = IS_BEG();
10949 SET_LEX_STATE(EXPR_BEG);
10950 if ((c = nextc(p)) == '.') {
10951 if ((c = nextc(p)) == '.') {
10952 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10953 SET_LEX_STATE(EXPR_ENDARG);
10954 return tBDOT3;
10955 }
10956 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10957 rb_warn0("... at EOL, should be parenthesized?");
10958 }
10959 return is_beg ? tBDOT3 : tDOT3;
10960 }
10961 pushback(p, c);
10962 return is_beg ? tBDOT2 : tDOT2;
10963 }
10964 pushback(p, c);
10965 if (c != -1 && ISDIGIT(c)) {
10966 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10967 parse_numeric(p, '.');
10968 if (ISDIGIT(prev)) {
10969 yyerror0("unexpected fraction part after numeric literal");
10970 }
10971 else {
10972 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10973 }
10974 SET_LEX_STATE(EXPR_END);
10975 p->lex.ptok = p->lex.pcur;
10976 goto retry;
10977 }
10978 set_yylval_id('.');
10979 SET_LEX_STATE(EXPR_DOT);
10980 return '.';
10981 }
10982
10983 case '0': case '1': case '2': case '3': case '4':
10984 case '5': case '6': case '7': case '8': case '9':
10985 return parse_numeric(p, c);
10986
10987 case ')':
10988 COND_POP();
10989 CMDARG_POP();
10990 SET_LEX_STATE(EXPR_ENDFN);
10991 p->lex.paren_nest--;
10992 return c;
10993
10994 case ']':
10995 COND_POP();
10996 CMDARG_POP();
10997 SET_LEX_STATE(EXPR_END);
10998 p->lex.paren_nest--;
10999 return c;
11000
11001 case '}':
11002 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11003 if (!p->lex.brace_nest--) return tSTRING_DEND;
11004 COND_POP();
11005 CMDARG_POP();
11006 SET_LEX_STATE(EXPR_END);
11007 p->lex.paren_nest--;
11008 return c;
11009
11010 case ':':
11011 c = nextc(p);
11012 if (c == ':') {
11013 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11014 SET_LEX_STATE(EXPR_BEG);
11015 return tCOLON3;
11016 }
11017 set_yylval_id(idCOLON2);
11018 SET_LEX_STATE(EXPR_DOT);
11019 return tCOLON2;
11020 }
11021 if (IS_END() || ISSPACE(c) || c == '#') {
11022 pushback(p, c);
11023 c = warn_balanced(':', ":", "symbol literal");
11024 SET_LEX_STATE(EXPR_BEG);
11025 return c;
11026 }
11027 switch (c) {
11028 case '\'':
11029 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11030 break;
11031 case '"':
11032 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11033 break;
11034 default:
11035 pushback(p, c);
11036 break;
11037 }
11038 SET_LEX_STATE(EXPR_FNAME);
11039 return tSYMBEG;
11040
11041 case '/':
11042 if (IS_BEG()) {
11043 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11044 return tREGEXP_BEG;
11045 }
11046 if ((c = nextc(p)) == '=') {
11047 set_yylval_id('/');
11048 SET_LEX_STATE(EXPR_BEG);
11049 return tOP_ASGN;
11050 }
11051 pushback(p, c);
11052 if (IS_SPCARG(c)) {
11053 arg_ambiguous(p, '/');
11054 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11055 return tREGEXP_BEG;
11056 }
11057 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11058 return warn_balanced('/', "/", "regexp literal");
11059
11060 case '^':
11061 if ((c = nextc(p)) == '=') {
11062 set_yylval_id('^');
11063 SET_LEX_STATE(EXPR_BEG);
11064 return tOP_ASGN;
11065 }
11066 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11067 pushback(p, c);
11068 return '^';
11069
11070 case ';':
11071 SET_LEX_STATE(EXPR_BEG);
11072 p->command_start = TRUE;
11073 return ';';
11074
11075 case ',':
11076 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11077 return ',';
11078
11079 case '~':
11080 if (IS_AFTER_OPERATOR()) {
11081 if ((c = nextc(p)) != '@') {
11082 pushback(p, c);
11083 }
11084 SET_LEX_STATE(EXPR_ARG);
11085 }
11086 else {
11087 SET_LEX_STATE(EXPR_BEG);
11088 }
11089 return '~';
11090
11091 case '(':
11092 if (IS_BEG()) {
11093 c = tLPAREN;
11094 }
11095 else if (!space_seen) {
11096 /* foo( ... ) => method call, no ambiguity */
11097 }
11098 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11099 c = tLPAREN_ARG;
11100 }
11101 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11102 rb_warning0("parentheses after method name is interpreted as "
11103 "an argument list, not a decomposed argument");
11104 }
11105 p->lex.paren_nest++;
11106 COND_PUSH(0);
11107 CMDARG_PUSH(0);
11108 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11109 return c;
11110
11111 case '[':
11112 p->lex.paren_nest++;
11113 if (IS_AFTER_OPERATOR()) {
11114 if ((c = nextc(p)) == ']') {
11115 p->lex.paren_nest--;
11116 SET_LEX_STATE(EXPR_ARG);
11117 if ((c = nextc(p)) == '=') {
11118 return tASET;
11119 }
11120 pushback(p, c);
11121 return tAREF;
11122 }
11123 pushback(p, c);
11124 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11125 return '[';
11126 }
11127 else if (IS_BEG()) {
11128 c = tLBRACK;
11129 }
11130 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11131 c = tLBRACK;
11132 }
11133 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11134 COND_PUSH(0);
11135 CMDARG_PUSH(0);
11136 return c;
11137
11138 case '{':
11139 ++p->lex.brace_nest;
11140 if (lambda_beginning_p())
11141 c = tLAMBEG;
11142 else if (IS_lex_state(EXPR_LABELED))
11143 c = tLBRACE; /* hash */
11144 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11145 c = '{'; /* block (primary) */
11146 else if (IS_lex_state(EXPR_ENDARG))
11147 c = tLBRACE_ARG; /* block (expr) */
11148 else
11149 c = tLBRACE; /* hash */
11150 if (c != tLBRACE) {
11151 p->command_start = TRUE;
11152 SET_LEX_STATE(EXPR_BEG);
11153 }
11154 else {
11155 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11156 }
11157 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11158 COND_PUSH(0);
11159 CMDARG_PUSH(0);
11160 return c;
11161
11162 case '\\':
11163 c = nextc(p);
11164 if (c == '\n') {
11165 space_seen = 1;
11166 dispatch_scan_event(p, tSP);
11167 goto retry; /* skip \\n */
11168 }
11169 if (c == ' ') return tSP;
11170 if (ISSPACE(c)) return c;
11171 pushback(p, c);
11172 return '\\';
11173
11174 case '%':
11175 return parse_percent(p, space_seen, last_state);
11176
11177 case '$':
11178 return parse_gvar(p, last_state);
11179
11180 case '@':
11181 return parse_atmark(p, last_state);
11182
11183 case '_':
11184 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11185 p->ruby__end__seen = 1;
11186 p->eofp = 1;
11187#ifdef RIPPER
11188 lex_goto_eol(p);
11189 dispatch_scan_event(p, k__END__);
11190#endif
11191 return END_OF_INPUT;
11192 }
11193 newtok(p);
11194 break;
11195
11196 default:
11197 if (!parser_is_identchar(p)) {
11198 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11199 token_flush(p);
11200 goto retry;
11201 }
11202
11203 newtok(p);
11204 break;
11205 }
11206
11207 return parse_ident(p, c, cmd_state);
11208}
11209
11210static enum yytokentype
11211yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11212{
11213 enum yytokentype t;
11214
11215 p->lval = lval;
11216 lval->node = 0;
11217 p->yylloc = yylloc;
11218
11219 t = parser_yylex(p);
11220
11221 if (has_delayed_token(p))
11222 dispatch_delayed_token(p, t);
11223 else if (t != END_OF_INPUT)
11224 dispatch_scan_event(p, t);
11225
11226 return t;
11227}
11228
11229#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11230
11231static NODE*
11232node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11233{
11234 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11235
11236 rb_node_init(n, type);
11237 return n;
11238}
11239
11240static NODE *
11241nd_set_loc(NODE *nd, const YYLTYPE *loc)
11242{
11243 nd->nd_loc = *loc;
11244 nd_set_line(nd, loc->beg_pos.lineno);
11245 return nd;
11246}
11247
11248static NODE*
11249node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11250{
11251 NODE *n = node_new_internal(p, type, size, alignment);
11252
11253 nd_set_loc(n, loc);
11254 nd_set_node_id(n, parser_get_node_id(p));
11255 return n;
11256}
11257
11258#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11259
11260static rb_node_scope_t *
11261rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11262{
11263 rb_ast_id_table_t *nd_tbl;
11264 nd_tbl = local_tbl(p);
11265 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11266 n->nd_tbl = nd_tbl;
11267 n->nd_body = nd_body;
11268 n->nd_parent = nd_parent;
11269 n->nd_args = nd_args;
11270
11271 return n;
11272}
11273
11274static rb_node_scope_t *
11275rb_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)
11276{
11277 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11278 n->nd_tbl = nd_tbl;
11279 n->nd_body = nd_body;
11280 n->nd_parent = nd_parent;
11281 n->nd_args = nd_args;
11282
11283 return n;
11284}
11285
11286static rb_node_defn_t *
11287rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11288{
11289 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11290 n->nd_mid = nd_mid;
11291 n->nd_defn = nd_defn;
11292
11293 return n;
11294}
11295
11296static rb_node_defs_t *
11297rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11298{
11299 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11300 n->nd_recv = nd_recv;
11301 n->nd_mid = nd_mid;
11302 n->nd_defn = nd_defn;
11303
11304 return n;
11305}
11306
11307static rb_node_block_t *
11308rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11309{
11310 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11311 n->nd_head = nd_head;
11312 n->nd_end = (NODE *)n;
11313 n->nd_next = 0;
11314
11315 return n;
11316}
11317
11318static rb_node_for_t *
11319rb_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)
11320{
11321 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11322 n->nd_body = nd_body;
11323 n->nd_iter = nd_iter;
11324 n->for_keyword_loc = *for_keyword_loc;
11325 n->in_keyword_loc = *in_keyword_loc;
11326 n->do_keyword_loc = *do_keyword_loc;
11327 n->end_keyword_loc = *end_keyword_loc;
11328
11329 return n;
11330}
11331
11332static rb_node_for_masgn_t *
11333rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11334{
11335 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11336 n->nd_var = nd_var;
11337
11338 return n;
11339}
11340
11341static rb_node_retry_t *
11342rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11343{
11344 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11345
11346 return n;
11347}
11348
11349static rb_node_begin_t *
11350rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11351{
11352 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11353 n->nd_body = nd_body;
11354
11355 return n;
11356}
11357
11358static rb_node_rescue_t *
11359rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11360{
11361 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11362 n->nd_head = nd_head;
11363 n->nd_resq = nd_resq;
11364 n->nd_else = nd_else;
11365
11366 return n;
11367}
11368
11369static rb_node_resbody_t *
11370rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11371{
11372 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11373 n->nd_args = nd_args;
11374 n->nd_exc_var = nd_exc_var;
11375 n->nd_body = nd_body;
11376 n->nd_next = nd_next;
11377
11378 return n;
11379}
11380
11381static rb_node_ensure_t *
11382rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11383{
11384 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11385 n->nd_head = nd_head;
11386 n->nd_ensr = nd_ensr;
11387
11388 return n;
11389}
11390
11391static rb_node_and_t *
11392rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11393{
11394 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11395 n->nd_1st = nd_1st;
11396 n->nd_2nd = nd_2nd;
11397 n->operator_loc = *operator_loc;
11398
11399 return n;
11400}
11401
11402static rb_node_or_t *
11403rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11404{
11405 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11406 n->nd_1st = nd_1st;
11407 n->nd_2nd = nd_2nd;
11408 n->operator_loc = *operator_loc;
11409
11410 return n;
11411}
11412
11413static rb_node_return_t *
11414rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11415{
11416 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11417 n->nd_stts = nd_stts;
11418 n->keyword_loc = *keyword_loc;
11419 return n;
11420}
11421
11422static rb_node_yield_t *
11423rb_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)
11424{
11425 if (nd_head) no_blockarg(p, nd_head);
11426
11427 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11428 n->nd_head = nd_head;
11429 n->keyword_loc = *keyword_loc;
11430 n->lparen_loc = *lparen_loc;
11431 n->rparen_loc = *rparen_loc;
11432
11433 return n;
11434}
11435
11436static rb_node_if_t *
11437rb_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)
11438{
11439 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11440 n->nd_cond = nd_cond;
11441 n->nd_body = nd_body;
11442 n->nd_else = nd_else;
11443 n->if_keyword_loc = *if_keyword_loc;
11444 n->then_keyword_loc = *then_keyword_loc;
11445 n->end_keyword_loc = *end_keyword_loc;
11446
11447 return n;
11448}
11449
11450static rb_node_unless_t *
11451rb_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)
11452{
11453 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11454 n->nd_cond = nd_cond;
11455 n->nd_body = nd_body;
11456 n->nd_else = nd_else;
11457 n->keyword_loc = *keyword_loc;
11458 n->then_keyword_loc = *then_keyword_loc;
11459 n->end_keyword_loc = *end_keyword_loc;
11460
11461 return n;
11462}
11463
11464static rb_node_class_t *
11465rb_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)
11466{
11467 /* Keep the order of node creation */
11468 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11469 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11470 RNODE_SCOPE(scope)->nd_parent = &n->node;
11471 n->nd_cpath = nd_cpath;
11472 n->nd_body = scope;
11473 n->nd_super = nd_super;
11474 n->class_keyword_loc = *class_keyword_loc;
11475 n->inheritance_operator_loc = *inheritance_operator_loc;
11476 n->end_keyword_loc = *end_keyword_loc;
11477
11478 return n;
11479}
11480
11481static rb_node_sclass_t *
11482rb_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)
11483{
11484 /* Keep the order of node creation */
11485 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11486 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11487 RNODE_SCOPE(scope)->nd_parent = &n->node;
11488 n->nd_recv = nd_recv;
11489 n->nd_body = scope;
11490 n->class_keyword_loc = *class_keyword_loc;
11491 n->operator_loc = *operator_loc;
11492 n->end_keyword_loc = *end_keyword_loc;
11493
11494 return n;
11495}
11496
11497static rb_node_module_t *
11498rb_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)
11499{
11500 /* Keep the order of node creation */
11501 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11502 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11503 RNODE_SCOPE(scope)->nd_parent = &n->node;
11504 n->nd_cpath = nd_cpath;
11505 n->nd_body = scope;
11506 n->module_keyword_loc = *module_keyword_loc;
11507 n->end_keyword_loc = *end_keyword_loc;
11508
11509 return n;
11510}
11511
11512static rb_node_iter_t *
11513rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11514{
11515 /* Keep the order of node creation */
11516 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11517 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11518 RNODE_SCOPE(scope)->nd_parent = &n->node;
11519 n->nd_body = scope;
11520 n->nd_iter = 0;
11521
11522 return n;
11523}
11524
11525static rb_node_lambda_t *
11526rb_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)
11527{
11528 /* Keep the order of node creation */
11529 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11530 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11531 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11532 RNODE_SCOPE(scope)->nd_parent = &n->node;
11533 n->nd_body = scope;
11534 n->operator_loc = *operator_loc;
11535 n->opening_loc = *opening_loc;
11536 n->closing_loc = *closing_loc;
11537
11538 return n;
11539}
11540
11541static rb_node_case_t *
11542rb_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)
11543{
11544 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11545 n->nd_head = nd_head;
11546 n->nd_body = nd_body;
11547 n->case_keyword_loc = *case_keyword_loc;
11548 n->end_keyword_loc = *end_keyword_loc;
11549
11550 return n;
11551}
11552
11553static rb_node_case2_t *
11554rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11555{
11556 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11557 n->nd_head = 0;
11558 n->nd_body = nd_body;
11559 n->case_keyword_loc = *case_keyword_loc;
11560 n->end_keyword_loc = *end_keyword_loc;
11561
11562 return n;
11563}
11564
11565static rb_node_case3_t *
11566rb_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)
11567{
11568 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11569 n->nd_head = nd_head;
11570 n->nd_body = nd_body;
11571 n->case_keyword_loc = *case_keyword_loc;
11572 n->end_keyword_loc = *end_keyword_loc;
11573
11574 return n;
11575}
11576
11577static rb_node_when_t *
11578rb_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)
11579{
11580 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11581 n->nd_head = nd_head;
11582 n->nd_body = nd_body;
11583 n->nd_next = nd_next;
11584 n->keyword_loc = *keyword_loc;
11585 n->then_keyword_loc = *then_keyword_loc;
11586
11587 return n;
11588}
11589
11590static rb_node_in_t *
11591rb_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)
11592{
11593 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11594 n->nd_head = nd_head;
11595 n->nd_body = nd_body;
11596 n->nd_next = nd_next;
11597 n->in_keyword_loc = *in_keyword_loc;
11598 n->then_keyword_loc = *then_keyword_loc;
11599 n->operator_loc = *operator_loc;
11600
11601 return n;
11602}
11603
11604static rb_node_while_t *
11605rb_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)
11606{
11607 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11608 n->nd_cond = nd_cond;
11609 n->nd_body = nd_body;
11610 n->nd_state = nd_state;
11611 n->keyword_loc = *keyword_loc;
11612 n->closing_loc = *closing_loc;
11613
11614 return n;
11615}
11616
11617static rb_node_until_t *
11618rb_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)
11619{
11620 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11621 n->nd_cond = nd_cond;
11622 n->nd_body = nd_body;
11623 n->nd_state = nd_state;
11624 n->keyword_loc = *keyword_loc;
11625 n->closing_loc = *closing_loc;
11626
11627 return n;
11628}
11629
11630static rb_node_colon2_t *
11631rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11632{
11633 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11634 n->nd_head = nd_head;
11635 n->nd_mid = nd_mid;
11636 n->delimiter_loc = *delimiter_loc;
11637 n->name_loc = *name_loc;
11638
11639 return n;
11640}
11641
11642static rb_node_colon3_t *
11643rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11644{
11645 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11646 n->nd_mid = nd_mid;
11647 n->delimiter_loc = *delimiter_loc;
11648 n->name_loc = *name_loc;
11649
11650 return n;
11651}
11652
11653static rb_node_dot2_t *
11654rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11655{
11656 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11657 n->nd_beg = nd_beg;
11658 n->nd_end = nd_end;
11659 n->operator_loc = *operator_loc;
11660
11661 return n;
11662}
11663
11664static rb_node_dot3_t *
11665rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11666{
11667 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11668 n->nd_beg = nd_beg;
11669 n->nd_end = nd_end;
11670 n->operator_loc = *operator_loc;
11671
11672 return n;
11673}
11674
11675static rb_node_self_t *
11676rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11677{
11678 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11679 n->nd_state = 1;
11680
11681 return n;
11682}
11683
11684static rb_node_nil_t *
11685rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11686{
11687 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11688
11689 return n;
11690}
11691
11692static rb_node_true_t *
11693rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11694{
11695 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11696
11697 return n;
11698}
11699
11700static rb_node_false_t *
11701rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11702{
11703 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11704
11705 return n;
11706}
11707
11708static rb_node_super_t *
11709rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11710 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11711{
11712 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11713 n->nd_args = nd_args;
11714 n->keyword_loc = *keyword_loc;
11715 n->lparen_loc = *lparen_loc;
11716 n->rparen_loc = *rparen_loc;
11717
11718 return n;
11719}
11720
11721static rb_node_zsuper_t *
11722rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11723{
11724 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11725
11726 return n;
11727}
11728
11729static rb_node_match2_t *
11730rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11731{
11732 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11733 n->nd_recv = nd_recv;
11734 n->nd_value = nd_value;
11735 n->nd_args = 0;
11736
11737 return n;
11738}
11739
11740static rb_node_match3_t *
11741rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11742{
11743 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11744 n->nd_recv = nd_recv;
11745 n->nd_value = nd_value;
11746
11747 return n;
11748}
11749
11750/* TODO: Use union for NODE_LIST2 */
11751static rb_node_list_t *
11752rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11753{
11754 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11755 n->nd_head = nd_head;
11756 n->as.nd_alen = 1;
11757 n->nd_next = 0;
11758
11759 return n;
11760}
11761
11762static rb_node_list_t *
11763rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11764{
11765 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11766 n->nd_head = nd_head;
11767 n->as.nd_alen = nd_alen;
11768 n->nd_next = nd_next;
11769
11770 return n;
11771}
11772
11773static rb_node_zlist_t *
11774rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11775{
11776 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11777
11778 return n;
11779}
11780
11781static rb_node_hash_t *
11782rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11783{
11784 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11785 n->nd_head = nd_head;
11786 n->nd_brace = 0;
11787
11788 return n;
11789}
11790
11791static rb_node_masgn_t *
11792rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11793{
11794 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11795 n->nd_head = nd_head;
11796 n->nd_value = 0;
11797 n->nd_args = nd_args;
11798
11799 return n;
11800}
11801
11802static rb_node_gasgn_t *
11803rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11804{
11805 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11806 n->nd_vid = nd_vid;
11807 n->nd_value = nd_value;
11808
11809 return n;
11810}
11811
11812static rb_node_lasgn_t *
11813rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11814{
11815 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11816 n->nd_vid = nd_vid;
11817 n->nd_value = nd_value;
11818
11819 return n;
11820}
11821
11822static rb_node_dasgn_t *
11823rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11824{
11825 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11826 n->nd_vid = nd_vid;
11827 n->nd_value = nd_value;
11828
11829 return n;
11830}
11831
11832static rb_node_iasgn_t *
11833rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11834{
11835 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11836 n->nd_vid = nd_vid;
11837 n->nd_value = nd_value;
11838
11839 return n;
11840}
11841
11842static rb_node_cvasgn_t *
11843rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11844{
11845 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11846 n->nd_vid = nd_vid;
11847 n->nd_value = nd_value;
11848
11849 return n;
11850}
11851
11852static rb_node_op_asgn1_t *
11853rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
11854{
11855 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11856 n->nd_recv = nd_recv;
11857 n->nd_mid = nd_mid;
11858 n->nd_index = index;
11859 n->nd_rvalue = rvalue;
11860 n->call_operator_loc = *call_operator_loc;
11861 n->opening_loc = *opening_loc;
11862 n->closing_loc = *closing_loc;
11863 n->binary_operator_loc = *binary_operator_loc;
11864
11865 return n;
11866}
11867
11868static rb_node_op_asgn2_t *
11869rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
11870{
11871 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11872 n->nd_recv = nd_recv;
11873 n->nd_value = nd_value;
11874 n->nd_vid = nd_vid;
11875 n->nd_mid = nd_mid;
11876 n->nd_aid = nd_aid;
11877 n->call_operator_loc = *call_operator_loc;
11878 n->message_loc = *message_loc;
11879 n->binary_operator_loc = *binary_operator_loc;
11880
11881 return n;
11882}
11883
11884static rb_node_op_asgn_or_t *
11885rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11886{
11887 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11888 n->nd_head = nd_head;
11889 n->nd_value = nd_value;
11890
11891 return n;
11892}
11893
11894static rb_node_op_asgn_and_t *
11895rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11896{
11897 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11898 n->nd_head = nd_head;
11899 n->nd_value = nd_value;
11900
11901 return n;
11902}
11903
11904static rb_node_gvar_t *
11905rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11906{
11907 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11908 n->nd_vid = nd_vid;
11909
11910 return n;
11911}
11912
11913static rb_node_lvar_t *
11914rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11915{
11916 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11917 n->nd_vid = nd_vid;
11918
11919 return n;
11920}
11921
11922static rb_node_dvar_t *
11923rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11924{
11925 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11926 n->nd_vid = nd_vid;
11927
11928 return n;
11929}
11930
11931static rb_node_ivar_t *
11932rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11933{
11934 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11935 n->nd_vid = nd_vid;
11936
11937 return n;
11938}
11939
11940static rb_node_const_t *
11941rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11942{
11943 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11944 n->nd_vid = nd_vid;
11945
11946 return n;
11947}
11948
11949static rb_node_cvar_t *
11950rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11951{
11952 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11953 n->nd_vid = nd_vid;
11954
11955 return n;
11956}
11957
11958static rb_node_nth_ref_t *
11959rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11960{
11961 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11962 n->nd_nth = nd_nth;
11963
11964 return n;
11965}
11966
11967static rb_node_back_ref_t *
11968rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11969{
11970 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11971 n->nd_nth = nd_nth;
11972
11973 return n;
11974}
11975
11976static rb_node_integer_t *
11977rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11978{
11979 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11980 n->val = val;
11981 n->minus = FALSE;
11982 n->base = base;
11983
11984 return n;
11985}
11986
11987static rb_node_float_t *
11988rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11989{
11990 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11991 n->val = val;
11992 n->minus = FALSE;
11993
11994 return n;
11995}
11996
11997static rb_node_rational_t *
11998rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11999{
12000 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12001 n->val = val;
12002 n->minus = FALSE;
12003 n->base = base;
12004 n->seen_point = seen_point;
12005
12006 return n;
12007}
12008
12009static rb_node_imaginary_t *
12010rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12011{
12012 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12013 n->val = val;
12014 n->minus = FALSE;
12015 n->base = base;
12016 n->seen_point = seen_point;
12017 n->type = numeric_type;
12018
12019 return n;
12020}
12021
12022static rb_node_str_t *
12023rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12024{
12025 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12026 n->string = string;
12027
12028 return n;
12029}
12030
12031/* TODO; Use union for NODE_DSTR2 */
12032static rb_node_dstr_t *
12033rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12034{
12035 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12036 n->string = string;
12037 n->as.nd_alen = nd_alen;
12038 n->nd_next = (rb_node_list_t *)nd_next;
12039
12040 return n;
12041}
12042
12043static rb_node_dstr_t *
12044rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12045{
12046 return rb_node_dstr_new0(p, string, 1, 0, loc);
12047}
12048
12049static rb_node_xstr_t *
12050rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12051{
12052 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12053 n->string = string;
12054
12055 return n;
12056}
12057
12058static rb_node_dxstr_t *
12059rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12060{
12061 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12062 n->string = string;
12063 n->as.nd_alen = nd_alen;
12064 n->nd_next = (rb_node_list_t *)nd_next;
12065
12066 return n;
12067}
12068
12069static rb_node_sym_t *
12070rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12071{
12072 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12073 n->string = rb_str_to_parser_string(p, str);
12074
12075 return n;
12076}
12077
12078static rb_node_dsym_t *
12079rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12080{
12081 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12082 n->string = string;
12083 n->as.nd_alen = nd_alen;
12084 n->nd_next = (rb_node_list_t *)nd_next;
12085
12086 return n;
12087}
12088
12089static rb_node_evstr_t *
12090rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12091{
12092 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12093 n->nd_body = nd_body;
12094 n->opening_loc = *opening_loc;
12095 n->closing_loc = *closing_loc;
12096
12097 return n;
12098}
12099
12100static rb_node_regx_t *
12101rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12102{
12103 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12104 n->string = string;
12105 n->options = options & RE_OPTION_MASK;
12106 n->opening_loc = *opening_loc;
12107 n->content_loc = *content_loc;
12108 n->closing_loc = *closing_loc;
12109
12110 return n;
12111}
12112
12113static rb_node_call_t *
12114rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12115{
12116 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12117 n->nd_recv = nd_recv;
12118 n->nd_mid = nd_mid;
12119 n->nd_args = nd_args;
12120
12121 return n;
12122}
12123
12124static rb_node_opcall_t *
12125rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12126{
12127 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12128 n->nd_recv = nd_recv;
12129 n->nd_mid = nd_mid;
12130 n->nd_args = nd_args;
12131
12132 return n;
12133}
12134
12135static rb_node_fcall_t *
12136rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12137{
12138 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12139 n->nd_mid = nd_mid;
12140 n->nd_args = nd_args;
12141
12142 return n;
12143}
12144
12145static rb_node_qcall_t *
12146rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12147{
12148 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12149 n->nd_recv = nd_recv;
12150 n->nd_mid = nd_mid;
12151 n->nd_args = nd_args;
12152
12153 return n;
12154}
12155
12156static rb_node_vcall_t *
12157rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12158{
12159 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12160 n->nd_mid = nd_mid;
12161
12162 return n;
12163}
12164
12165static rb_node_once_t *
12166rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12167{
12168 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12169 n->nd_body = nd_body;
12170
12171 return n;
12172}
12173
12174static rb_node_args_t *
12175rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12176{
12177 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12178 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12179
12180 return n;
12181}
12182
12183static rb_node_args_aux_t *
12184rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12185{
12186 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12187 n->nd_pid = nd_pid;
12188 n->nd_plen = nd_plen;
12189 n->nd_next = 0;
12190
12191 return n;
12192}
12193
12194static rb_node_opt_arg_t *
12195rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12196{
12197 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12198 n->nd_body = nd_body;
12199 n->nd_next = 0;
12200
12201 return n;
12202}
12203
12204static rb_node_kw_arg_t *
12205rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12206{
12207 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12208 n->nd_body = nd_body;
12209 n->nd_next = 0;
12210
12211 return n;
12212}
12213
12214static rb_node_postarg_t *
12215rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12216{
12217 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12218 n->nd_1st = nd_1st;
12219 n->nd_2nd = nd_2nd;
12220
12221 return n;
12222}
12223
12224static rb_node_argscat_t *
12225rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12226{
12227 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12228 n->nd_head = nd_head;
12229 n->nd_body = nd_body;
12230
12231 return n;
12232}
12233
12234static rb_node_argspush_t *
12235rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12236{
12237 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12238 n->nd_head = nd_head;
12239 n->nd_body = nd_body;
12240
12241 return n;
12242}
12243
12244static rb_node_splat_t *
12245rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12246{
12247 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12248 n->nd_head = nd_head;
12249 n->operator_loc = *operator_loc;
12250
12251 return n;
12252}
12253
12254static rb_node_block_pass_t *
12255rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12256{
12257 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12258 n->forwarding = 0;
12259 n->nd_head = 0;
12260 n->nd_body = nd_body;
12261 n->operator_loc = *operator_loc;
12262
12263 return n;
12264}
12265
12266static rb_node_alias_t *
12267rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12268{
12269 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12270 n->nd_1st = nd_1st;
12271 n->nd_2nd = nd_2nd;
12272 n->keyword_loc = *keyword_loc;
12273
12274 return n;
12275}
12276
12277static rb_node_valias_t *
12278rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12279{
12280 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12281 n->nd_alias = nd_alias;
12282 n->nd_orig = nd_orig;
12283 n->keyword_loc = *keyword_loc;
12284
12285 return n;
12286}
12287
12288static rb_node_undef_t *
12289rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12290{
12291 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12292 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12293 n->keyword_loc = NULL_LOC;
12294 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12295
12296 return n;
12297}
12298
12299static rb_node_errinfo_t *
12300rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12301{
12302 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12303
12304 return n;
12305}
12306
12307static rb_node_defined_t *
12308rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12309{
12310 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12311 n->nd_head = nd_head;
12312 n->keyword_loc = *keyword_loc;
12313
12314 return n;
12315}
12316
12317static rb_node_postexe_t *
12318rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12319{
12320 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12321 n->nd_body = nd_body;
12322 n->keyword_loc = *keyword_loc;
12323 n->opening_loc = *opening_loc;
12324 n->closing_loc = *closing_loc;
12325
12326 return n;
12327}
12328
12329static rb_node_attrasgn_t *
12330rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12331{
12332 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12333 n->nd_recv = nd_recv;
12334 n->nd_mid = nd_mid;
12335 n->nd_args = nd_args;
12336
12337 return n;
12338}
12339
12340static rb_node_aryptn_t *
12341rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12342{
12343 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12344 n->nd_pconst = 0;
12345 n->pre_args = pre_args;
12346 n->rest_arg = rest_arg;
12347 n->post_args = post_args;
12348
12349 return n;
12350}
12351
12352static rb_node_hshptn_t *
12353rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12354{
12355 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12356 n->nd_pconst = nd_pconst;
12357 n->nd_pkwargs = nd_pkwargs;
12358 n->nd_pkwrestarg = nd_pkwrestarg;
12359
12360 return n;
12361}
12362
12363static rb_node_fndptn_t *
12364rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12365{
12366 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12367 n->nd_pconst = 0;
12368 n->pre_rest_arg = pre_rest_arg;
12369 n->args = args;
12370 n->post_rest_arg = post_rest_arg;
12371
12372 return n;
12373}
12374
12375static rb_node_line_t *
12376rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12377{
12378 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12379
12380 return n;
12381}
12382
12383static rb_node_file_t *
12384rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12385{
12386 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12387 n->path = rb_str_to_parser_string(p, str);
12388
12389 return n;
12390}
12391
12392static rb_node_encoding_t *
12393rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12394{
12395 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12396 n->enc = p->enc;
12397
12398 return n;
12399}
12400
12401static rb_node_cdecl_t *
12402rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12403{
12404 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12405 n->nd_vid = nd_vid;
12406 n->nd_value = nd_value;
12407 n->nd_else = nd_else;
12408 n->shareability = shareability;
12409
12410 return n;
12411}
12412
12413static rb_node_op_cdecl_t *
12414rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12415{
12416 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12417 n->nd_head = nd_head;
12418 n->nd_value = nd_value;
12419 n->nd_aid = nd_aid;
12420 n->shareability = shareability;
12421
12422 return n;
12423}
12424
12425static rb_node_error_t *
12426rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12427{
12428 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12429
12430 return n;
12431}
12432
12433static rb_node_break_t *
12434rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12435{
12436 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12437 n->nd_stts = nd_stts;
12438 n->nd_chain = 0;
12439 n->keyword_loc = *keyword_loc;
12440
12441 return n;
12442}
12443
12444static rb_node_next_t *
12445rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12446{
12447 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12448 n->nd_stts = nd_stts;
12449 n->nd_chain = 0;
12450 n->keyword_loc = *keyword_loc;
12451
12452 return n;
12453}
12454
12455static rb_node_redo_t *
12456rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12457{
12458 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12459 n->nd_chain = 0;
12460 n->keyword_loc = *keyword_loc;
12461
12462 return n;
12463}
12464
12465static rb_node_def_temp_t *
12466rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12467{
12468 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12469 n->save.numparam_save = 0;
12470 n->save.max_numparam = 0;
12471 n->save.ctxt = p->ctxt;
12472 n->nd_def = 0;
12473 n->nd_mid = 0;
12474
12475 return n;
12476}
12477
12478static rb_node_def_temp_t *
12479def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12480{
12481 n->save.numparam_save = numparam_push(p);
12482 n->save.max_numparam = p->max_numparam;
12483 return n;
12484}
12485
12486#ifndef RIPPER
12487static enum node_type
12488nodetype(NODE *node) /* for debug */
12489{
12490 return (enum node_type)nd_type(node);
12491}
12492
12493static int
12494nodeline(NODE *node)
12495{
12496 return nd_line(node);
12497}
12498#endif
12499
12500static NODE*
12501newline_node(NODE *node)
12502{
12503 if (node) {
12504 node = remove_begin(node);
12505 nd_set_fl_newline(node);
12506 }
12507 return node;
12508}
12509
12510static void
12511fixpos(NODE *node, NODE *orig)
12512{
12513 if (!node) return;
12514 if (!orig) return;
12515 nd_set_line(node, nd_line(orig));
12516}
12517
12518static NODE*
12519block_append(struct parser_params *p, NODE *head, NODE *tail)
12520{
12521 NODE *end, *h = head, *nd;
12522
12523 if (tail == 0) return head;
12524
12525 if (h == 0) return tail;
12526 switch (nd_type(h)) {
12527 default:
12528 h = end = NEW_BLOCK(head, &head->nd_loc);
12529 head = end;
12530 break;
12531 case NODE_BLOCK:
12532 end = RNODE_BLOCK(h)->nd_end;
12533 break;
12534 }
12535
12536 nd = RNODE_BLOCK(end)->nd_head;
12537 switch (nd_type(nd)) {
12538 case NODE_RETURN:
12539 case NODE_BREAK:
12540 case NODE_NEXT:
12541 case NODE_REDO:
12542 case NODE_RETRY:
12543 rb_warning0L(nd_line(tail), "statement not reached");
12544 break;
12545
12546 default:
12547 break;
12548 }
12549
12550 if (!nd_type_p(tail, NODE_BLOCK)) {
12551 tail = NEW_BLOCK(tail, &tail->nd_loc);
12552 }
12553 RNODE_BLOCK(end)->nd_next = tail;
12554 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12555 nd_set_last_loc(head, nd_last_loc(tail));
12556 return head;
12557}
12558
12559/* append item to the list */
12560static NODE*
12561list_append(struct parser_params *p, NODE *list, NODE *item)
12562{
12563 NODE *last;
12564
12565 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12566 if (RNODE_LIST(list)->nd_next) {
12567 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12568 }
12569 else {
12570 last = list;
12571 }
12572
12573 RNODE_LIST(list)->as.nd_alen += 1;
12574 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12575 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12576
12577 nd_set_last_loc(list, nd_last_loc(item));
12578
12579 return list;
12580}
12581
12582/* concat two lists */
12583static NODE*
12584list_concat(NODE *head, NODE *tail)
12585{
12586 NODE *last;
12587
12588 if (RNODE_LIST(head)->nd_next) {
12589 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12590 }
12591 else {
12592 last = head;
12593 }
12594
12595 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12596 RNODE_LIST(last)->nd_next = tail;
12597 if (RNODE_LIST(tail)->nd_next) {
12598 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12599 }
12600 else {
12601 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12602 }
12603
12604 nd_set_last_loc(head, nd_last_loc(tail));
12605
12606 return head;
12607}
12608
12609static int
12610literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12611{
12612 if (!tail) return 1;
12613 if (!rb_parser_enc_compatible(p, head, tail)) {
12614 compile_error(p, "string literal encodings differ (%s / %s)",
12615 rb_enc_name(rb_parser_str_get_encoding(head)),
12616 rb_enc_name(rb_parser_str_get_encoding(tail)));
12617 rb_parser_str_resize(p, head, 0);
12618 rb_parser_str_resize(p, tail, 0);
12619 return 0;
12620 }
12621 rb_parser_str_buf_append(p, head, tail);
12622 return 1;
12623}
12624
12625static rb_parser_string_t *
12626string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12627{
12628 if (htype != NODE_DSTR) return NULL;
12629 if (RNODE_DSTR(head)->nd_next) {
12630 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12631 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12632 }
12633 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12634 ASSUME(lit);
12635 return lit;
12636}
12637
12638#ifndef RIPPER
12639static rb_parser_string_t *
12640rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12641{
12642 rb_parser_string_t *copy;
12643 if (!orig) return NULL;
12644 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12645 copy->coderange = orig->coderange;
12646 copy->enc = orig->enc;
12647 return copy;
12648}
12649#endif
12650
12651/* concat two string literals */
12652static NODE *
12653literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12654{
12655 enum node_type htype;
12656 rb_parser_string_t *lit;
12657
12658 if (!head) return tail;
12659 if (!tail) return head;
12660
12661 htype = nd_type(head);
12662 if (htype == NODE_EVSTR) {
12663 head = new_dstr(p, head, loc);
12664 htype = NODE_DSTR;
12665 }
12666 if (p->heredoc_indent > 0) {
12667 switch (htype) {
12668 case NODE_STR:
12669 head = str2dstr(p, head);
12670 case NODE_DSTR:
12671 return list_append(p, head, tail);
12672 default:
12673 break;
12674 }
12675 }
12676 switch (nd_type(tail)) {
12677 case NODE_STR:
12678 if ((lit = string_literal_head(p, htype, head)) != false) {
12679 htype = NODE_STR;
12680 }
12681 else {
12682 lit = RNODE_DSTR(head)->string;
12683 }
12684 if (htype == NODE_STR) {
12685 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12686 error:
12687 rb_discard_node(p, head);
12688 rb_discard_node(p, tail);
12689 return 0;
12690 }
12691 rb_discard_node(p, tail);
12692 }
12693 else {
12694 list_append(p, head, tail);
12695 }
12696 break;
12697
12698 case NODE_DSTR:
12699 if (htype == NODE_STR) {
12700 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12701 goto error;
12702 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12703 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12704 RNODE_STR(head)->string = NULL;
12705 rb_discard_node(p, head);
12706 head = tail;
12707 }
12708 else if (!RNODE_DSTR(tail)->string) {
12709 append:
12710 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12711 if (!RNODE_DSTR(head)->nd_next) {
12712 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12713 }
12714 else if (RNODE_DSTR(tail)->nd_next) {
12715 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12716 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12717 }
12718 rb_discard_node(p, tail);
12719 }
12720 else if ((lit = string_literal_head(p, htype, head)) != false) {
12721 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12722 goto error;
12723 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12724 RNODE_DSTR(tail)->string = 0;
12725 goto append;
12726 }
12727 else {
12728 list_concat(head, NEW_LIST2(NEW_STR(RNODE_DSTR(tail)->string, loc), RNODE_DSTR(tail)->as.nd_alen, (NODE *)RNODE_DSTR(tail)->nd_next, loc));
12729 RNODE_DSTR(tail)->string = 0;
12730 }
12731 break;
12732
12733 case NODE_EVSTR:
12734 if (htype == NODE_STR) {
12735 head = str2dstr(p, head);
12736 RNODE_DSTR(head)->as.nd_alen = 1;
12737 }
12738 list_append(p, head, tail);
12739 break;
12740 }
12741 return head;
12742}
12743
12744static void
12745nd_copy_flag(NODE *new_node, NODE *old_node)
12746{
12747 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12748 nd_set_line(new_node, nd_line(old_node));
12749 new_node->nd_loc = old_node->nd_loc;
12750 new_node->node_id = old_node->node_id;
12751}
12752
12753static NODE *
12754str2dstr(struct parser_params *p, NODE *node)
12755{
12756 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12757 nd_copy_flag(new_node, node);
12758 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12759 RNODE_DSTR(new_node)->as.nd_alen = 0;
12760 RNODE_DSTR(new_node)->nd_next = 0;
12761 RNODE_STR(node)->string = 0;
12762
12763 return new_node;
12764}
12765
12766static NODE *
12767str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12768{
12769 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12770 nd_copy_flag(new_node, node);
12771 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12772 RNODE_REGX(new_node)->options = options;
12773 nd_set_loc(new_node, loc);
12774 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12775 RNODE_REGX(new_node)->content_loc = *content_loc;
12776 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12777 RNODE_STR(node)->string = 0;
12778
12779 return new_node;
12780}
12781
12782static NODE *
12783evstr2dstr(struct parser_params *p, NODE *node)
12784{
12785 if (nd_type_p(node, NODE_EVSTR)) {
12786 node = new_dstr(p, node, &node->nd_loc);
12787 }
12788 return node;
12789}
12790
12791static NODE *
12792new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12793{
12794 NODE *head = node;
12795
12796 if (node) {
12797 switch (nd_type(node)) {
12798 case NODE_STR:
12799 return str2dstr(p, node);
12800 case NODE_DSTR:
12801 break;
12802 case NODE_EVSTR:
12803 return node;
12804 }
12805 }
12806 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12807}
12808
12809static NODE *
12810new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12811{
12812 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12813 return list_append(p, dstr, node);
12814}
12815
12816static NODE *
12817call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12818 const YYLTYPE *op_loc, const YYLTYPE *loc)
12819{
12820 NODE *expr;
12821 value_expr(p, recv);
12822 value_expr(p, arg1);
12823 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12824 nd_set_line(expr, op_loc->beg_pos.lineno);
12825 return expr;
12826}
12827
12828static NODE *
12829call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12830{
12831 NODE *opcall;
12832 value_expr(p, recv);
12833 opcall = NEW_OPCALL(recv, id, 0, loc);
12834 nd_set_line(opcall, op_loc->beg_pos.lineno);
12835 return opcall;
12836}
12837
12838static NODE *
12839new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12840{
12841 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12842 nd_set_line(qcall, op_loc->beg_pos.lineno);
12843 return qcall;
12844}
12845
12846static NODE*
12847new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12848{
12849 NODE *ret;
12850 if (block) block_dup_check(p, args, block);
12851 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12852 if (block) ret = method_add_block(p, ret, block, loc);
12853 fixpos(ret, recv);
12854 return ret;
12855}
12856
12857static rb_locations_lambda_body_t*
12858new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12859{
12860 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12861 body->node = node;
12862 body->opening_loc = *opening_loc;
12863 body->closing_loc = *closing_loc;
12864 return body;
12865}
12866
12867#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12868
12869static NODE*
12870last_expr_once_body(NODE *node)
12871{
12872 if (!node) return 0;
12873 return nd_once_body(node);
12874}
12875
12876static NODE*
12877match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12878{
12879 NODE *n;
12880 int line = op_loc->beg_pos.lineno;
12881
12882 value_expr(p, node1);
12883 value_expr(p, node2);
12884
12885 if ((n = last_expr_once_body(node1)) != 0) {
12886 switch (nd_type(n)) {
12887 case NODE_DREGX:
12888 {
12889 NODE *match = NEW_MATCH2(node1, node2, loc);
12890 nd_set_line(match, line);
12891 return match;
12892 }
12893
12894 case NODE_REGX:
12895 {
12896 const VALUE lit = rb_node_regx_string_val(n);
12897 if (!NIL_P(lit)) {
12898 NODE *match = NEW_MATCH2(node1, node2, loc);
12899 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12900 nd_set_line(match, line);
12901 return match;
12902 }
12903 }
12904 }
12905 }
12906
12907 if ((n = last_expr_once_body(node2)) != 0) {
12908 NODE *match3;
12909
12910 switch (nd_type(n)) {
12911 case NODE_DREGX:
12912 match3 = NEW_MATCH3(node2, node1, loc);
12913 return match3;
12914 }
12915 }
12916
12917 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12918 nd_set_line(n, line);
12919 return n;
12920}
12921
12922# if WARN_PAST_SCOPE
12923static int
12924past_dvar_p(struct parser_params *p, ID id)
12925{
12926 struct vtable *past = p->lvtbl->past;
12927 while (past) {
12928 if (vtable_included(past, id)) return 1;
12929 past = past->prev;
12930 }
12931 return 0;
12932}
12933# endif
12934
12935static int
12936numparam_nested_p(struct parser_params *p)
12937{
12938 struct local_vars *local = p->lvtbl;
12939 NODE *outer = local->numparam.outer;
12940 NODE *inner = local->numparam.inner;
12941 if (outer || inner) {
12942 NODE *used = outer ? outer : inner;
12943 compile_error(p, "numbered parameter is already used in %s block\n"
12944 "%s:%d: numbered parameter is already used here",
12945 outer ? "outer" : "inner",
12946 p->ruby_sourcefile, nd_line(used));
12947 parser_show_error_line(p, &used->nd_loc);
12948 return 1;
12949 }
12950 return 0;
12951}
12952
12953static int
12954numparam_used_p(struct parser_params *p)
12955{
12956 NODE *numparam = p->lvtbl->numparam.current;
12957 if (numparam) {
12958 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12959 "%s:%d: numbered parameter is already used here",
12960 p->ruby_sourcefile, nd_line(numparam));
12961 parser_show_error_line(p, &numparam->nd_loc);
12962 return 1;
12963 }
12964 return 0;
12965}
12966
12967static int
12968it_used_p(struct parser_params *p)
12969{
12970 NODE *it = p->lvtbl->it;
12971 if (it) {
12972 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12973 "%s:%d: 'it' is already used here",
12974 p->ruby_sourcefile, nd_line(it));
12975 parser_show_error_line(p, &it->nd_loc);
12976 return 1;
12977 }
12978 return 0;
12979}
12980
12981static NODE*
12982gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12983{
12984 ID *vidp = NULL;
12985 NODE *node;
12986 switch (id) {
12987 case keyword_self:
12988 return NEW_SELF(loc);
12989 case keyword_nil:
12990 return NEW_NIL(loc);
12991 case keyword_true:
12992 return NEW_TRUE(loc);
12993 case keyword_false:
12994 return NEW_FALSE(loc);
12995 case keyword__FILE__:
12996 {
12997 VALUE file = p->ruby_sourcefile_string;
12998 if (NIL_P(file))
12999 file = rb_str_new(0, 0);
13000 node = NEW_FILE(file, loc);
13001 }
13002 return node;
13003 case keyword__LINE__:
13004 return NEW_LINE(loc);
13005 case keyword__ENCODING__:
13006 return NEW_ENCODING(loc);
13007
13008 }
13009 switch (id_type(id)) {
13010 case ID_LOCAL:
13011 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13012 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13013 if (vidp) *vidp |= LVAR_USED;
13014 node = NEW_DVAR(id, loc);
13015 return node;
13016 }
13017 if (local_id_ref(p, id, &vidp)) {
13018 if (vidp) *vidp |= LVAR_USED;
13019 node = NEW_LVAR(id, loc);
13020 return node;
13021 }
13022 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13023 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13024 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13025 node = NEW_DVAR(id, loc);
13026 struct local_vars *local = p->lvtbl;
13027 if (!local->numparam.current) local->numparam.current = node;
13028 return node;
13029 }
13030# if WARN_PAST_SCOPE
13031 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13032 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13033 }
13034# endif
13035 /* method call without arguments */
13036 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13037 if (numparam_used_p(p)) return 0;
13038 if (p->max_numparam == ORDINAL_PARAM) {
13039 compile_error(p, "ordinary parameter is defined");
13040 return 0;
13041 }
13042 if (!p->it_id) {
13043 p->it_id = internal_id(p);
13044 vtable_add(p->lvtbl->args, p->it_id);
13045 }
13046 NODE *node = NEW_DVAR(p->it_id, loc);
13047 if (!p->lvtbl->it) p->lvtbl->it = node;
13048 return node;
13049 }
13050 return NEW_VCALL(id, loc);
13051 case ID_GLOBAL:
13052 return NEW_GVAR(id, loc);
13053 case ID_INSTANCE:
13054 return NEW_IVAR(id, loc);
13055 case ID_CONST:
13056 return NEW_CONST(id, loc);
13057 case ID_CLASS:
13058 return NEW_CVAR(id, loc);
13059 }
13060 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13061 return 0;
13062}
13063
13064static rb_node_opt_arg_t *
13065opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13066{
13067 rb_node_opt_arg_t *opts = opt_list;
13068 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13069
13070 while (opts->nd_next) {
13071 opts = opts->nd_next;
13072 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13073 }
13074 opts->nd_next = opt;
13075
13076 return opt_list;
13077}
13078
13079static rb_node_kw_arg_t *
13080kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13081{
13082 if (kwlist) {
13083 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13084 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13085 }
13086 return kwlist;
13087}
13088
13089static NODE *
13090new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13091{
13092 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13093 p->ctxt.has_trailing_semicolon = 0;
13094
13095 NODE *n = expr;
13096 while (n) {
13097 if (nd_type_p(n, NODE_BEGIN)) {
13098 n = RNODE_BEGIN(n)->nd_body;
13099 }
13100 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13101 n = RNODE_BLOCK(n)->nd_head;
13102 }
13103 else {
13104 break;
13105 }
13106 }
13107
13108 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13109 NODE *block = NEW_BLOCK(expr, loc);
13110 return NEW_DEFINED(block, loc, keyword_loc);
13111 }
13112
13113 return NEW_DEFINED(n, loc, keyword_loc);
13114}
13115
13116static NODE*
13117str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13118{
13119 VALUE lit;
13120 rb_parser_string_t *str = RNODE_STR(node)->string;
13121 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13122 yyerror1(loc, "invalid symbol");
13123 lit = STR_NEW0();
13124 }
13125 else {
13126 lit = rb_str_new_parser_string(str);
13127 }
13128 return NEW_SYM(lit, loc);
13129}
13130
13131static NODE*
13132symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13133{
13134 enum node_type type = nd_type(symbol);
13135 switch (type) {
13136 case NODE_DSTR:
13137 nd_set_type(symbol, NODE_DSYM);
13138 break;
13139 case NODE_STR:
13140 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13141 break;
13142 default:
13143 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13144 }
13145 return list_append(p, symbols, symbol);
13146}
13147
13148static void
13149dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13150{
13151 if (dreg->string) {
13152 reg_fragment_setenc(p, dreg->string, options);
13153 }
13154 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13155 NODE *frag = list->nd_head;
13156 if (nd_type_p(frag, NODE_STR)) {
13157 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13158 }
13159 else if (nd_type_p(frag, NODE_DSTR)) {
13160 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13161 }
13162 }
13163}
13164
13165static NODE *
13166new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13167{
13168 if (!node) {
13169 /* Check string is valid regex */
13170 rb_parser_string_t *str = STRING_NEW0();
13171 reg_compile(p, str, options);
13172 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13173 return node;
13174 }
13175 switch (nd_type(node)) {
13176 case NODE_STR:
13177 {
13178 /* Check string is valid regex */
13179 reg_compile(p, RNODE_STR(node)->string, options);
13180 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13181 }
13182 break;
13183 default:
13184 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13185 /* fall through */
13186 case NODE_DSTR:
13187 nd_set_type(node, NODE_DREGX);
13188 nd_set_loc(node, loc);
13189 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13190 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13191 if (dreg->nd_next) {
13192 dregex_fragment_setenc(p, dreg, options);
13193 }
13194 if (options & RE_OPTION_ONCE) {
13195 node = NEW_ONCE(node, loc);
13196 }
13197 break;
13198 }
13199 return node;
13200}
13201
13202static rb_node_kw_arg_t *
13203new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13204{
13205 if (!k) return 0;
13206 return NEW_KW_ARG((k), loc);
13207}
13208
13209static NODE *
13210new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13211{
13212 if (!node) {
13213 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13214 return xstr;
13215 }
13216 switch (nd_type(node)) {
13217 case NODE_STR:
13218 nd_set_type(node, NODE_XSTR);
13219 nd_set_loc(node, loc);
13220 break;
13221 case NODE_DSTR:
13222 nd_set_type(node, NODE_DXSTR);
13223 nd_set_loc(node, loc);
13224 break;
13225 default:
13226 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13227 break;
13228 }
13229 return node;
13230}
13231
13232static const
13233struct st_hash_type literal_type = {
13234 literal_cmp,
13235 literal_hash,
13236};
13237
13238static int nd_type_st_key_enable_p(NODE *node);
13239
13240static void
13241check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13242{
13243 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13244 if (!arg || !p->case_labels) return;
13245 if (!nd_type_st_key_enable_p(arg)) return;
13246
13247 if (p->case_labels == CHECK_LITERAL_WHEN) {
13248 p->case_labels = st_init_table(&literal_type);
13249 }
13250 else {
13251 st_data_t line;
13252 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13253 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13254 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13255 return;
13256 }
13257 }
13258 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13259}
13260
13261#ifdef RIPPER
13262static int
13263id_is_var(struct parser_params *p, ID id)
13264{
13265 if (is_notop_id(id)) {
13266 switch (id & ID_SCOPE_MASK) {
13267 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13268 return 1;
13269 case ID_LOCAL:
13270 if (dyna_in_block(p)) {
13271 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13272 }
13273 if (local_id(p, id)) return 1;
13274 /* method call without arguments */
13275 return 0;
13276 }
13277 }
13278 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13279 return 0;
13280}
13281#endif
13282
13283static inline enum lex_state_e
13284parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13285{
13286 if (p->debug) {
13287 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13288 }
13289 return p->lex.state = ls;
13290}
13291
13292#ifndef RIPPER
13293static void
13294flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13295{
13296 VALUE mesg = p->debug_buffer;
13297
13298 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13299 p->debug_buffer = Qnil;
13300 rb_io_puts(1, &mesg, out);
13301 }
13302 if (!NIL_P(str) && RSTRING_LEN(str)) {
13303 rb_io_write(p->debug_output, str);
13304 }
13305}
13306
13307static const char rb_parser_lex_state_names[][8] = {
13308 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13309 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13310 "LABEL", "LABELED","FITEM",
13311};
13312
13313static VALUE
13314append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13315{
13316 int i, sep = 0;
13317 unsigned int mask = 1;
13318 static const char none[] = "NONE";
13319
13320 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13321 if ((unsigned)state & mask) {
13322 if (sep) {
13323 rb_str_cat(buf, "|", 1);
13324 }
13325 sep = 1;
13326 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13327 }
13328 }
13329 if (!sep) {
13330 rb_str_cat(buf, none, sizeof(none)-1);
13331 }
13332 return buf;
13333}
13334
13335enum lex_state_e
13336rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13337 enum lex_state_e to, int line)
13338{
13339 VALUE mesg;
13340 mesg = rb_str_new_cstr("lex_state: ");
13341 append_lex_state_name(p, from, mesg);
13342 rb_str_cat_cstr(mesg, " -> ");
13343 append_lex_state_name(p, to, mesg);
13344 rb_str_catf(mesg, " at line %d\n", line);
13345 flush_debug_buffer(p, p->debug_output, mesg);
13346 return to;
13347}
13348
13349VALUE
13350rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13351{
13352 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13353}
13354
13355static void
13356append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13357{
13358 if (stack == 0) {
13359 rb_str_cat_cstr(mesg, "0");
13360 }
13361 else {
13362 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13363 for (; mask && !(stack & mask); mask >>= 1) continue;
13364 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13365 }
13366}
13367
13368void
13369rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13370 const char *name, int line)
13371{
13372 VALUE mesg = rb_sprintf("%s: ", name);
13373 append_bitstack_value(p, stack, mesg);
13374 rb_str_catf(mesg, " at line %d\n", line);
13375 flush_debug_buffer(p, p->debug_output, mesg);
13376}
13377
13378void
13379rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13380{
13381 va_list ap;
13382 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13383
13384 va_start(ap, fmt);
13385 rb_str_vcatf(mesg, fmt, ap);
13386 va_end(ap);
13387 yyerror0(RSTRING_PTR(mesg));
13388 RB_GC_GUARD(mesg);
13389
13390 mesg = rb_str_new(0, 0);
13391 append_lex_state_name(p, p->lex.state, mesg);
13392 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13393 rb_str_resize(mesg, 0);
13394 append_bitstack_value(p, p->cond_stack, mesg);
13395 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13396 rb_str_resize(mesg, 0);
13397 append_bitstack_value(p, p->cmdarg_stack, mesg);
13398 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13399 if (p->debug_output == rb_ractor_stdout())
13400 p->debug_output = rb_ractor_stderr();
13401 p->debug = TRUE;
13402}
13403
13404static YYLTYPE *
13405rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13406{
13407 yylloc->beg_pos.lineno = sourceline;
13408 yylloc->beg_pos.column = beg_pos;
13409 yylloc->end_pos.lineno = sourceline;
13410 yylloc->end_pos.column = end_pos;
13411 return yylloc;
13412}
13413
13414YYLTYPE *
13415rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13416{
13417 int sourceline = here->sourceline;
13418 int beg_pos = (int)here->offset - here->quote
13419 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13420 int end_pos = (int)here->offset + here->length + here->quote;
13421
13422 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13423}
13424
13425YYLTYPE *
13426rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13427{
13428 yylloc->beg_pos.lineno = p->delayed.beg_line;
13429 yylloc->beg_pos.column = p->delayed.beg_col;
13430 yylloc->end_pos.lineno = p->delayed.end_line;
13431 yylloc->end_pos.column = p->delayed.end_col;
13432
13433 return yylloc;
13434}
13435
13436YYLTYPE *
13437rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13438{
13439 int sourceline = p->ruby_sourceline;
13440 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13441 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13442 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13443}
13444
13445YYLTYPE *
13446rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13447{
13448 yylloc->end_pos = yylloc->beg_pos;
13449
13450 return yylloc;
13451}
13452
13453YYLTYPE *
13454rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13455{
13456 int sourceline = p->ruby_sourceline;
13457 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13458 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13459 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13460}
13461
13462YYLTYPE *
13463rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13464{
13465 int sourceline = p->ruby_sourceline;
13466 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13467 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13468 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13469}
13470#endif /* !RIPPER */
13471
13472static int
13473assignable0(struct parser_params *p, ID id, const char **err)
13474{
13475 if (!id) return -1;
13476 switch (id) {
13477 case keyword_self:
13478 *err = "Can't change the value of self";
13479 return -1;
13480 case keyword_nil:
13481 *err = "Can't assign to nil";
13482 return -1;
13483 case keyword_true:
13484 *err = "Can't assign to true";
13485 return -1;
13486 case keyword_false:
13487 *err = "Can't assign to false";
13488 return -1;
13489 case keyword__FILE__:
13490 *err = "Can't assign to __FILE__";
13491 return -1;
13492 case keyword__LINE__:
13493 *err = "Can't assign to __LINE__";
13494 return -1;
13495 case keyword__ENCODING__:
13496 *err = "Can't assign to __ENCODING__";
13497 return -1;
13498 }
13499 switch (id_type(id)) {
13500 case ID_LOCAL:
13501 if (dyna_in_block(p)) {
13502 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13503 compile_error(p, "Can't assign to numbered parameter _%d",
13504 NUMPARAM_ID_TO_IDX(id));
13505 return -1;
13506 }
13507 if (dvar_curr(p, id)) return NODE_DASGN;
13508 if (dvar_defined(p, id)) return NODE_DASGN;
13509 if (local_id(p, id)) return NODE_LASGN;
13510 dyna_var(p, id);
13511 return NODE_DASGN;
13512 }
13513 else {
13514 if (!local_id(p, id)) local_var(p, id);
13515 return NODE_LASGN;
13516 }
13517 break;
13518 case ID_GLOBAL: return NODE_GASGN;
13519 case ID_INSTANCE: return NODE_IASGN;
13520 case ID_CONST:
13521 if (!p->ctxt.in_def) return NODE_CDECL;
13522 *err = "dynamic constant assignment";
13523 return -1;
13524 case ID_CLASS: return NODE_CVASGN;
13525 default:
13526 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13527 }
13528 return -1;
13529}
13530
13531static NODE*
13532assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13533{
13534 const char *err = 0;
13535 int node_type = assignable0(p, id, &err);
13536 switch (node_type) {
13537 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13538 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13539 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13540 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13541 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13542 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13543 }
13544/* TODO: FIXME */
13545#ifndef RIPPER
13546 if (err) yyerror1(loc, err);
13547#else
13548 if (err) set_value(assign_error(p, err, p->s_lvalue));
13549#endif
13550 return NEW_ERROR(loc);
13551}
13552
13553static int
13554is_private_local_id(struct parser_params *p, ID name)
13555{
13556 VALUE s;
13557 if (name == idUScore) return 1;
13558 if (!is_local_id(name)) return 0;
13559 s = rb_id2str(name);
13560 if (!s) return 0;
13561 return RSTRING_PTR(s)[0] == '_';
13562}
13563
13564static int
13565shadowing_lvar_0(struct parser_params *p, ID name)
13566{
13567 if (dyna_in_block(p)) {
13568 if (dvar_curr(p, name)) {
13569 if (is_private_local_id(p, name)) return 1;
13570 yyerror0("duplicated argument name");
13571 }
13572 else if (dvar_defined(p, name) || local_id(p, name)) {
13573 vtable_add(p->lvtbl->vars, name);
13574 if (p->lvtbl->used) {
13575 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13576 }
13577 return 0;
13578 }
13579 }
13580 else {
13581 if (local_id(p, name)) {
13582 if (is_private_local_id(p, name)) return 1;
13583 yyerror0("duplicated argument name");
13584 }
13585 }
13586 return 1;
13587}
13588
13589static ID
13590shadowing_lvar(struct parser_params *p, ID name)
13591{
13592 shadowing_lvar_0(p, name);
13593 return name;
13594}
13595
13596static void
13597new_bv(struct parser_params *p, ID name)
13598{
13599 if (!name) return;
13600 if (!is_local_id(name)) {
13601 compile_error(p, "invalid local variable - %"PRIsVALUE,
13602 rb_id2str(name));
13603 return;
13604 }
13605 if (!shadowing_lvar_0(p, name)) return;
13606 dyna_var(p, name);
13607 ID *vidp = 0;
13608 if (dvar_defined_ref(p, name, &vidp)) {
13609 if (vidp) *vidp |= LVAR_USED;
13610 }
13611}
13612
13613static void
13614aryset_check(struct parser_params *p, NODE *args)
13615{
13616 NODE *block = 0, *kwds = 0;
13617 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13618 block = RNODE_BLOCK_PASS(args)->nd_body;
13619 args = RNODE_BLOCK_PASS(args)->nd_head;
13620 }
13621 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13622 args = RNODE_ARGSCAT(args)->nd_body;
13623 }
13624 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13625 kwds = RNODE_ARGSPUSH(args)->nd_body;
13626 }
13627 else {
13628 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13629 next = RNODE_LIST(next)->nd_next) {
13630 kwds = RNODE_LIST(next)->nd_head;
13631 }
13632 }
13633 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13634 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13635 }
13636 if (block) {
13637 yyerror1(&block->nd_loc, "block arg given in index assignment");
13638 }
13639}
13640
13641static NODE *
13642aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13643{
13644 aryset_check(p, idx);
13645 return NEW_ATTRASGN(recv, tASET, idx, loc);
13646}
13647
13648static void
13649block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13650{
13651 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13652 compile_error(p, "both block arg and actual block given");
13653 }
13654}
13655
13656static NODE *
13657attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13658{
13659 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13660 return NEW_ATTRASGN(recv, id, 0, loc);
13661}
13662
13663static VALUE
13664rb_backref_error(struct parser_params *p, NODE *node)
13665{
13666#ifndef RIPPER
13667# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13668#else
13669# define ERR(...) rb_sprintf(__VA_ARGS__)
13670#endif
13671 switch (nd_type(node)) {
13672 case NODE_NTH_REF:
13673 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13674 case NODE_BACK_REF:
13675 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13676 }
13677#undef ERR
13678 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13679}
13680
13681static NODE *
13682arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13683{
13684 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13685 switch (nd_type(node1)) {
13686 case NODE_LIST:
13687 return list_append(p, node1, node2);
13688 case NODE_BLOCK_PASS:
13689 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13690 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13691 return node1;
13692 case NODE_ARGSPUSH:
13693 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13694 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13695 nd_set_type(node1, NODE_ARGSCAT);
13696 return node1;
13697 case NODE_ARGSCAT:
13698 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13699 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13700 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13701 return node1;
13702 }
13703 return NEW_ARGSPUSH(node1, node2, loc);
13704}
13705
13706static NODE *
13707arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13708{
13709 if (!node2) return node1;
13710 switch (nd_type(node1)) {
13711 case NODE_BLOCK_PASS:
13712 if (RNODE_BLOCK_PASS(node1)->nd_head)
13713 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13714 else
13715 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13716 return node1;
13717 case NODE_ARGSPUSH:
13718 if (!nd_type_p(node2, NODE_LIST)) break;
13719 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13720 nd_set_type(node1, NODE_ARGSCAT);
13721 return node1;
13722 case NODE_ARGSCAT:
13723 if (!nd_type_p(node2, NODE_LIST) ||
13724 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13725 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13726 return node1;
13727 }
13728 return NEW_ARGSCAT(node1, node2, loc);
13729}
13730
13731static NODE *
13732last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13733{
13734 NODE *n1;
13735 if ((n1 = splat_array(args)) != 0) {
13736 return list_append(p, n1, last_arg);
13737 }
13738 return arg_append(p, args, last_arg, loc);
13739}
13740
13741static NODE *
13742rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13743{
13744 NODE *n1;
13745 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13746 return list_concat(n1, rest_arg);
13747 }
13748 return arg_concat(p, args, rest_arg, loc);
13749}
13750
13751static NODE *
13752splat_array(NODE* node)
13753{
13754 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13755 if (nd_type_p(node, NODE_LIST)) return node;
13756 return 0;
13757}
13758
13759static void
13760mark_lvar_used(struct parser_params *p, NODE *rhs)
13761{
13762 ID *vidp = NULL;
13763 if (!rhs) return;
13764 switch (nd_type(rhs)) {
13765 case NODE_LASGN:
13766 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13767 if (vidp) *vidp |= LVAR_USED;
13768 }
13769 break;
13770 case NODE_DASGN:
13771 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13772 if (vidp) *vidp |= LVAR_USED;
13773 }
13774 break;
13775#if 0
13776 case NODE_MASGN:
13777 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13778 mark_lvar_used(p, rhs->nd_head);
13779 }
13780 break;
13781#endif
13782 }
13783}
13784
13785static int is_static_content(NODE *node);
13786
13787static NODE *
13788node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13789{
13790 if (!lhs) return 0;
13791
13792 switch (nd_type(lhs)) {
13793 case NODE_CDECL:
13794 case NODE_GASGN:
13795 case NODE_IASGN:
13796 case NODE_LASGN:
13797 case NODE_DASGN:
13798 case NODE_MASGN:
13799 case NODE_CVASGN:
13800 set_nd_value(p, lhs, rhs);
13801 nd_set_loc(lhs, loc);
13802 break;
13803
13804 case NODE_ATTRASGN:
13805 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13806 nd_set_loc(lhs, loc);
13807 break;
13808
13809 default:
13810 /* should not happen */
13811 break;
13812 }
13813
13814 return lhs;
13815}
13816
13817static NODE *
13818value_expr_check(struct parser_params *p, NODE *node)
13819{
13820 NODE *void_node = 0, *vn;
13821
13822 if (!node) {
13823 rb_warning0("empty expression");
13824 }
13825 while (node) {
13826 switch (nd_type(node)) {
13827 case NODE_ENSURE:
13828 vn = RNODE_ENSURE(node)->nd_head;
13829 node = RNODE_ENSURE(node)->nd_ensr;
13830 /* nd_ensr should not be NULL, check it out next */
13831 if (vn && (vn = value_expr_check(p, vn))) {
13832 goto found;
13833 }
13834 break;
13835
13836 case NODE_RESCUE:
13837 /* void only if all children are void */
13838 vn = RNODE_RESCUE(node)->nd_head;
13839 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13840 if (!void_node) void_node = vn;
13841 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13842 if (!nd_type_p(r, NODE_RESBODY)) {
13843 compile_error(p, "unexpected node");
13844 return NULL;
13845 }
13846 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13847 void_node = 0;
13848 break;
13849 }
13850 if (!void_node) void_node = vn;
13851 }
13852 node = RNODE_RESCUE(node)->nd_else;
13853 if (!node) return void_node;
13854 break;
13855
13856 case NODE_RETURN:
13857 case NODE_BREAK:
13858 case NODE_NEXT:
13859 case NODE_REDO:
13860 case NODE_RETRY:
13861 goto found;
13862
13863 case NODE_CASE3:
13864 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13865 compile_error(p, "unexpected node");
13866 return NULL;
13867 }
13868 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13869 return NULL;
13870 }
13871 /* single line pattern matching with "=>" operator */
13872 goto found;
13873
13874 case NODE_BLOCK:
13875 while (RNODE_BLOCK(node)->nd_next) {
13876 node = RNODE_BLOCK(node)->nd_next;
13877 }
13878 node = RNODE_BLOCK(node)->nd_head;
13879 break;
13880
13881 case NODE_BEGIN:
13882 node = RNODE_BEGIN(node)->nd_body;
13883 break;
13884
13885 case NODE_IF:
13886 case NODE_UNLESS:
13887 if (!RNODE_IF(node)->nd_body) {
13888 return NULL;
13889 }
13890 else if (!RNODE_IF(node)->nd_else) {
13891 return NULL;
13892 }
13893 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13894 if (!vn) return NULL;
13895 if (!void_node) void_node = vn;
13896 node = RNODE_IF(node)->nd_else;
13897 break;
13898
13899 case NODE_AND:
13900 case NODE_OR:
13901 node = RNODE_AND(node)->nd_1st;
13902 break;
13903
13904 case NODE_LASGN:
13905 case NODE_DASGN:
13906 case NODE_MASGN:
13907 mark_lvar_used(p, node);
13908 return NULL;
13909
13910 default:
13911 return NULL;
13912 }
13913 }
13914
13915 return NULL;
13916
13917 found:
13918 /* return the first found node */
13919 return void_node ? void_node : node;
13920}
13921
13922static int
13923value_expr(struct parser_params *p, NODE *node)
13924{
13925 NODE *void_node = value_expr_check(p, node);
13926 if (void_node) {
13927 yyerror1(&void_node->nd_loc, "void value expression");
13928 /* or "control never reach"? */
13929 return FALSE;
13930 }
13931 return TRUE;
13932}
13933
13934static void
13935void_expr(struct parser_params *p, NODE *node)
13936{
13937 const char *useless = 0;
13938
13939 if (!RTEST(ruby_verbose)) return;
13940
13941 if (!node || !(node = nd_once_body(node))) return;
13942 switch (nd_type(node)) {
13943 case NODE_OPCALL:
13944 switch (RNODE_OPCALL(node)->nd_mid) {
13945 case '+':
13946 case '-':
13947 case '*':
13948 case '/':
13949 case '%':
13950 case tPOW:
13951 case tUPLUS:
13952 case tUMINUS:
13953 case '|':
13954 case '^':
13955 case '&':
13956 case tCMP:
13957 case '>':
13958 case tGEQ:
13959 case '<':
13960 case tLEQ:
13961 case tEQ:
13962 case tNEQ:
13963 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13964 break;
13965 }
13966 break;
13967
13968 case NODE_LVAR:
13969 case NODE_DVAR:
13970 case NODE_GVAR:
13971 case NODE_IVAR:
13972 case NODE_CVAR:
13973 case NODE_NTH_REF:
13974 case NODE_BACK_REF:
13975 useless = "a variable";
13976 break;
13977 case NODE_CONST:
13978 useless = "a constant";
13979 break;
13980 case NODE_SYM:
13981 case NODE_LINE:
13982 case NODE_FILE:
13983 case NODE_ENCODING:
13984 case NODE_INTEGER:
13985 case NODE_FLOAT:
13986 case NODE_RATIONAL:
13987 case NODE_IMAGINARY:
13988 case NODE_STR:
13989 case NODE_DSTR:
13990 case NODE_REGX:
13991 case NODE_DREGX:
13992 useless = "a literal";
13993 break;
13994 case NODE_COLON2:
13995 case NODE_COLON3:
13996 useless = "::";
13997 break;
13998 case NODE_DOT2:
13999 useless = "..";
14000 break;
14001 case NODE_DOT3:
14002 useless = "...";
14003 break;
14004 case NODE_SELF:
14005 useless = "self";
14006 break;
14007 case NODE_NIL:
14008 useless = "nil";
14009 break;
14010 case NODE_TRUE:
14011 useless = "true";
14012 break;
14013 case NODE_FALSE:
14014 useless = "false";
14015 break;
14016 case NODE_DEFINED:
14017 useless = "defined?";
14018 break;
14019 }
14020
14021 if (useless) {
14022 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14023 }
14024}
14025
14026/* warns useless use of block and returns the last statement node */
14027static NODE *
14028void_stmts(struct parser_params *p, NODE *node)
14029{
14030 NODE *const n = node;
14031 if (!RTEST(ruby_verbose)) return n;
14032 if (!node) return n;
14033 if (!nd_type_p(node, NODE_BLOCK)) return n;
14034
14035 while (RNODE_BLOCK(node)->nd_next) {
14036 void_expr(p, RNODE_BLOCK(node)->nd_head);
14037 node = RNODE_BLOCK(node)->nd_next;
14038 }
14039 return RNODE_BLOCK(node)->nd_head;
14040}
14041
14042static NODE *
14043remove_begin(NODE *node)
14044{
14045 NODE **n = &node, *n1 = node;
14046 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14047 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14048 }
14049 return node;
14050}
14051
14052static void
14053reduce_nodes(struct parser_params *p, NODE **body)
14054{
14055 NODE *node = *body;
14056
14057 if (!node) {
14058 *body = NEW_NIL(&NULL_LOC);
14059 return;
14060 }
14061#define subnodes(type, n1, n2) \
14062 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14063 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14064 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14065
14066 while (node) {
14067 int newline = (int)nd_fl_newline(node);
14068 switch (nd_type(node)) {
14069 end:
14070 case NODE_NIL:
14071 *body = 0;
14072 return;
14073 case NODE_BEGIN:
14074 *body = node = RNODE_BEGIN(node)->nd_body;
14075 if (newline && node) nd_set_fl_newline(node);
14076 continue;
14077 case NODE_BLOCK:
14078 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14079 break;
14080 case NODE_IF:
14081 case NODE_UNLESS:
14082 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14083 return;
14084 case NODE_CASE:
14085 body = &RNODE_CASE(node)->nd_body;
14086 break;
14087 case NODE_WHEN:
14088 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14089 break;
14090 case NODE_ENSURE:
14091 body = &RNODE_ENSURE(node)->nd_head;
14092 break;
14093 case NODE_RESCUE:
14094 newline = 0; // RESBODY should not be a NEWLINE
14095 if (RNODE_RESCUE(node)->nd_else) {
14096 body = &RNODE_RESCUE(node)->nd_resq;
14097 break;
14098 }
14099 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14100 break;
14101 default:
14102 return;
14103 }
14104 node = *body;
14105 if (newline && node) nd_set_fl_newline(node);
14106 }
14107
14108#undef subnodes
14109}
14110
14111static int
14112is_static_content(NODE *node)
14113{
14114 if (!node) return 1;
14115 switch (nd_type(node)) {
14116 case NODE_HASH:
14117 if (!(node = RNODE_HASH(node)->nd_head)) break;
14118 case NODE_LIST:
14119 do {
14120 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14121 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14122 case NODE_SYM:
14123 case NODE_REGX:
14124 case NODE_LINE:
14125 case NODE_FILE:
14126 case NODE_ENCODING:
14127 case NODE_INTEGER:
14128 case NODE_FLOAT:
14129 case NODE_RATIONAL:
14130 case NODE_IMAGINARY:
14131 case NODE_STR:
14132 case NODE_NIL:
14133 case NODE_TRUE:
14134 case NODE_FALSE:
14135 case NODE_ZLIST:
14136 break;
14137 default:
14138 return 0;
14139 }
14140 return 1;
14141}
14142
14143static int
14144assign_in_cond(struct parser_params *p, NODE *node)
14145{
14146 switch (nd_type(node)) {
14147 case NODE_MASGN:
14148 case NODE_LASGN:
14149 case NODE_DASGN:
14150 case NODE_GASGN:
14151 case NODE_IASGN:
14152 case NODE_CVASGN:
14153 case NODE_CDECL:
14154 break;
14155
14156 default:
14157 return 0;
14158 }
14159
14160 if (!get_nd_value(p, node)) return 1;
14161 if (is_static_content(get_nd_value(p, node))) {
14162 /* reports always */
14163 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14164 }
14165 return 1;
14166}
14167
14168enum cond_type {
14169 COND_IN_OP,
14170 COND_IN_COND,
14171 COND_IN_FF
14172};
14173
14174#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14175 switch (t) { \
14176 case COND_IN_OP: break; \
14177 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14178 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14179 } \
14180} while (0)
14181
14182static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14183
14184static NODE*
14185range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14186{
14187 enum node_type type;
14188
14189 if (node == 0) return 0;
14190
14191 type = nd_type(node);
14192 value_expr(p, node);
14193 if (type == NODE_INTEGER) {
14194 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14195 ID lineno = rb_intern("$.");
14196 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14197 }
14198 return cond0(p, node, COND_IN_FF, loc, true);
14199}
14200
14201static NODE*
14202cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14203{
14204 if (node == 0) return 0;
14205 if (!(node = nd_once_body(node))) return 0;
14206 assign_in_cond(p, node);
14207
14208 switch (nd_type(node)) {
14209 case NODE_BEGIN:
14210 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14211 break;
14212
14213 case NODE_DSTR:
14214 case NODE_EVSTR:
14215 case NODE_STR:
14216 case NODE_FILE:
14217 SWITCH_BY_COND_TYPE(type, warn, "string ");
14218 break;
14219
14220 case NODE_REGX:
14221 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14222 nd_set_type(node, NODE_MATCH);
14223 break;
14224
14225 case NODE_DREGX:
14226 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14227
14228 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14229
14230 case NODE_BLOCK:
14231 {
14232 NODE *end = RNODE_BLOCK(node)->nd_end;
14233 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14234 if (top) top = node == end;
14235 *expr = cond0(p, *expr, type, loc, top);
14236 }
14237 break;
14238
14239 case NODE_AND:
14240 case NODE_OR:
14241 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14242 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14243 break;
14244
14245 case NODE_DOT2:
14246 case NODE_DOT3:
14247 if (!top) break;
14248 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14249 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14250 switch (nd_type(node)) {
14251 case NODE_DOT2:
14252 nd_set_type(node,NODE_FLIP2);
14253 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14254 (void)flip2;
14255 break;
14256 case NODE_DOT3:
14257 nd_set_type(node, NODE_FLIP3);
14258 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14259 (void)flip3;
14260 break;
14261 }
14262 break;
14263
14264 case NODE_SYM:
14265 case NODE_DSYM:
14266 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14267 break;
14268
14269 case NODE_LINE:
14270 case NODE_ENCODING:
14271 case NODE_INTEGER:
14272 case NODE_FLOAT:
14273 case NODE_RATIONAL:
14274 case NODE_IMAGINARY:
14275 SWITCH_BY_COND_TYPE(type, warning, "");
14276 break;
14277
14278 default:
14279 break;
14280 }
14281 return node;
14282}
14283
14284static NODE*
14285cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14286{
14287 if (node == 0) return 0;
14288 return cond0(p, node, COND_IN_COND, loc, true);
14289}
14290
14291static NODE*
14292method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14293{
14294 if (node == 0) return 0;
14295 return cond0(p, node, COND_IN_OP, loc, true);
14296}
14297
14298static NODE*
14299new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14300{
14301 YYLTYPE loc = {*pos, *pos};
14302 return NEW_NIL(&loc);
14303}
14304
14305static NODE*
14306new_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)
14307{
14308 if (!cc) return right;
14309 cc = cond0(p, cc, COND_IN_COND, loc, true);
14310 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14311}
14312
14313static NODE*
14314new_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)
14315{
14316 if (!cc) return right;
14317 cc = cond0(p, cc, COND_IN_COND, loc, true);
14318 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14319}
14320
14321#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))
14322
14323static NODE*
14324logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14325 const YYLTYPE *op_loc, const YYLTYPE *loc)
14326{
14327 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14328 NODE *op;
14329 value_expr(p, left);
14330 if (left && nd_type_p(left, type)) {
14331 NODE *node = left, *second;
14332 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14333 node = second;
14334 }
14335 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14336 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14337 left->nd_loc.end_pos = loc->end_pos;
14338 return left;
14339 }
14340 op = NEW_AND_OR(type, left, right, loc, op_loc);
14341 nd_set_line(op, op_loc->beg_pos.lineno);
14342 return op;
14343}
14344
14345#undef NEW_AND_OR
14346
14347static void
14348no_blockarg(struct parser_params *p, NODE *node)
14349{
14350 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14351 compile_error(p, "block argument should not be given");
14352 }
14353}
14354
14355static NODE *
14356ret_args(struct parser_params *p, NODE *node)
14357{
14358 if (node) {
14359 no_blockarg(p, node);
14360 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14361 node = RNODE_LIST(node)->nd_head;
14362 }
14363 }
14364 return node;
14365}
14366
14367static NODE*
14368negate_lit(struct parser_params *p, NODE* node)
14369{
14370 switch (nd_type(node)) {
14371 case NODE_INTEGER:
14372 RNODE_INTEGER(node)->minus = TRUE;
14373 break;
14374 case NODE_FLOAT:
14375 RNODE_FLOAT(node)->minus = TRUE;
14376 break;
14377 case NODE_RATIONAL:
14378 RNODE_RATIONAL(node)->minus = TRUE;
14379 break;
14380 case NODE_IMAGINARY:
14381 RNODE_IMAGINARY(node)->minus = TRUE;
14382 break;
14383 }
14384 return node;
14385}
14386
14387static NODE *
14388arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14389{
14390 if (node2) {
14391 if (!node1) return (NODE *)node2;
14392 node2->nd_head = node1;
14393 nd_set_first_lineno(node2, nd_first_lineno(node1));
14394 nd_set_first_column(node2, nd_first_column(node1));
14395 return (NODE *)node2;
14396 }
14397 return node1;
14398}
14399
14400static bool
14401args_info_empty_p(struct rb_args_info *args)
14402{
14403 if (args->pre_args_num) return false;
14404 if (args->post_args_num) return false;
14405 if (args->rest_arg) return false;
14406 if (args->opt_args) return false;
14407 if (args->block_arg) return false;
14408 if (args->kw_args) return false;
14409 if (args->kw_rest_arg) return false;
14410 return true;
14411}
14412
14413static rb_node_args_t *
14414new_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)
14415{
14416 struct rb_args_info *args = &tail->nd_ainfo;
14417
14418 if (args->forwarding) {
14419 if (rest_arg) {
14420 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14421 return tail;
14422 }
14423 rest_arg = idFWD_REST;
14424 }
14425
14426 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14427 args->pre_init = pre_args ? pre_args->nd_next : 0;
14428
14429 args->post_args_num = post_args ? post_args->nd_plen : 0;
14430 args->post_init = post_args ? post_args->nd_next : 0;
14431 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14432
14433 args->rest_arg = rest_arg;
14434
14435 args->opt_args = opt_args;
14436
14437#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14438 args->ruby2_keywords = args->forwarding;
14439#else
14440 args->ruby2_keywords = 0;
14441#endif
14442
14443 nd_set_loc(RNODE(tail), loc);
14444
14445 return tail;
14446}
14447
14448static rb_node_args_t *
14449new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14450{
14451 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14452 struct rb_args_info *args = &node->nd_ainfo;
14453 if (p->error_p) return node;
14454
14455 args->block_arg = block;
14456 args->kw_args = kw_args;
14457
14458 if (kw_args) {
14459 /*
14460 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14461 * variable order: k1, kr1, k2, &b, internal_id, krest
14462 * #=> <reorder>
14463 * variable order: kr1, k1, k2, internal_id, krest, &b
14464 */
14465 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14466 struct vtable *vtargs = p->lvtbl->args;
14467 rb_node_kw_arg_t *kwn = kw_args;
14468
14469 if (block) block = vtargs->tbl[vtargs->pos-1];
14470 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14471 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14472 while (kwn) {
14473 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14474 --kw_vars;
14475 --required_kw_vars;
14476 kwn = kwn->nd_next;
14477 }
14478
14479 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14480 ID vid = get_nd_vid(p, kwn->nd_body);
14481 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14482 *required_kw_vars++ = vid;
14483 }
14484 else {
14485 *kw_vars++ = vid;
14486 }
14487 }
14488
14489 arg_var(p, kw_bits);
14490 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14491 if (block) arg_var(p, block);
14492
14493 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14494 }
14495 else if (kw_rest_arg == idNil) {
14496 args->no_kwarg = 1;
14497 }
14498 else if (kw_rest_arg) {
14499 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14500 }
14501
14502 return node;
14503}
14504
14505static rb_node_args_t *
14506args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14507{
14508 if (max_numparam > NO_PARAM || it_id) {
14509 if (!args) {
14510 YYLTYPE loc = RUBY_INIT_YYLLOC();
14511 args = new_args_tail(p, 0, 0, 0, 0);
14512 nd_set_loc(RNODE(args), &loc);
14513 }
14514 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14515 }
14516 return args;
14517}
14518
14519static NODE*
14520new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14521{
14522 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14523
14524 if (pre_arg) {
14525 NODE *pre_args = NEW_LIST(pre_arg, loc);
14526 if (RNODE_ARYPTN(aryptn)->pre_args) {
14527 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14528 }
14529 else {
14530 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14531 }
14532 }
14533 return aryptn;
14534}
14535
14536static NODE*
14537new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14538{
14539 if (has_rest) {
14540 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14541 }
14542 else {
14543 rest_arg = NULL;
14544 }
14545 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14546
14547 return node;
14548}
14549
14550static NODE*
14551new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14552{
14553 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14554
14555 return fndptn;
14556}
14557
14558static NODE*
14559new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14560{
14561 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14562 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14563 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14564
14565 return node;
14566}
14567
14568static NODE*
14569new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14570{
14571 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14572 return hshptn;
14573}
14574
14575static NODE*
14576new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14577{
14578 NODE *node, *kw_rest_arg_node;
14579
14580 if (kw_rest_arg == idNil) {
14581 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14582 }
14583 else if (kw_rest_arg) {
14584 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14585 }
14586 else {
14587 kw_rest_arg_node = NULL;
14588 }
14589
14590 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14591
14592 return node;
14593}
14594
14595static NODE*
14596dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14597{
14598 if (!node) {
14599 return NEW_SYM(STR_NEW0(), loc);
14600 }
14601
14602 switch (nd_type(node)) {
14603 case NODE_DSTR:
14604 nd_set_type(node, NODE_DSYM);
14605 nd_set_loc(node, loc);
14606 break;
14607 case NODE_STR:
14608 node = str_to_sym_node(p, node, loc);
14609 break;
14610 default:
14611 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14612 break;
14613 }
14614 return node;
14615}
14616
14617static int
14618nd_type_st_key_enable_p(NODE *node)
14619{
14620 switch (nd_type(node)) {
14621 case NODE_INTEGER:
14622 case NODE_FLOAT:
14623 case NODE_RATIONAL:
14624 case NODE_IMAGINARY:
14625 case NODE_STR:
14626 case NODE_SYM:
14627 case NODE_REGX:
14628 case NODE_LINE:
14629 case NODE_FILE:
14630 case NODE_ENCODING:
14631 return true;
14632 default:
14633 return false;
14634 }
14635}
14636
14637static VALUE
14638nd_value(struct parser_params *p, NODE *node)
14639{
14640 switch (nd_type(node)) {
14641 case NODE_STR:
14642 return rb_node_str_string_val(node);
14643 case NODE_INTEGER:
14644 return rb_node_integer_literal_val(node);
14645 case NODE_FLOAT:
14646 return rb_node_float_literal_val(node);
14647 case NODE_RATIONAL:
14648 return rb_node_rational_literal_val(node);
14649 case NODE_IMAGINARY:
14650 return rb_node_imaginary_literal_val(node);
14651 case NODE_SYM:
14652 return rb_node_sym_string_val(node);
14653 case NODE_REGX:
14654 return rb_node_regx_string_val(node);
14655 case NODE_LINE:
14656 return rb_node_line_lineno_val(node);
14657 case NODE_ENCODING:
14658 return rb_node_encoding_val(node);
14659 case NODE_FILE:
14660 return rb_node_file_path_val(node);
14661 default:
14662 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14663 UNREACHABLE_RETURN(0);
14664 }
14665}
14666
14667static void
14668warn_duplicate_keys(struct parser_params *p, NODE *hash)
14669{
14670 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14671 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14672 while (hash && RNODE_LIST(hash)->nd_next) {
14673 NODE *head = RNODE_LIST(hash)->nd_head;
14674 NODE *value = RNODE_LIST(hash)->nd_next;
14675 NODE *next = RNODE_LIST(value)->nd_next;
14676 st_data_t key;
14677 st_data_t data;
14678
14679 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14680 if (!head) {
14681 head = value;
14682 }
14683
14684 if (nd_type_st_key_enable_p(head)) {
14685 key = (st_data_t)head;
14686
14687 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14688 rb_warn2L(nd_line((NODE *)data),
14689 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14690 nd_value(p, head), WARN_I(nd_line(head)));
14691 }
14692 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14693 }
14694 hash = next;
14695 }
14696 st_free_table(p->warn_duplicate_keys_table);
14697 p->warn_duplicate_keys_table = NULL;
14698}
14699
14700static NODE *
14701new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14702{
14703 if (hash) warn_duplicate_keys(p, hash);
14704 return NEW_HASH(hash, loc);
14705}
14706
14707static void
14708error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14709{
14710 if (is_private_local_id(p, id)) {
14711 return;
14712 }
14713 if (st_is_member(p->pvtbl, id)) {
14714 yyerror1(loc, "duplicated variable name");
14715 }
14716 else if (p->ctxt.in_alt_pattern && id) {
14717 yyerror1(loc, "variable capture in alternative pattern");
14718 }
14719 else {
14720 p->ctxt.capture_in_pattern = 1;
14721 st_insert(p->pvtbl, (st_data_t)id, 0);
14722 }
14723}
14724
14725static void
14726error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14727{
14728 if (!p->pktbl) {
14729 p->pktbl = st_init_numtable();
14730 }
14731 else if (st_is_member(p->pktbl, key)) {
14732 yyerror1(loc, "duplicated key name");
14733 return;
14734 }
14735 st_insert(p->pktbl, (st_data_t)key, 0);
14736}
14737
14738static NODE *
14739new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14740{
14741 return NEW_HASH(hash, loc);
14742}
14743
14744static NODE *
14745new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14746{
14747 NODE *asgn;
14748
14749 if (lhs) {
14750 ID vid = get_nd_vid(p, lhs);
14751 YYLTYPE lhs_loc = lhs->nd_loc;
14752 if (op == tOROP) {
14753 set_nd_value(p, lhs, rhs);
14754 nd_set_loc(lhs, loc);
14755 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14756 }
14757 else if (op == tANDOP) {
14758 set_nd_value(p, lhs, rhs);
14759 nd_set_loc(lhs, loc);
14760 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14761 }
14762 else {
14763 asgn = lhs;
14764 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14765 set_nd_value(p, asgn, rhs);
14766 nd_set_loc(asgn, loc);
14767 }
14768 }
14769 else {
14770 asgn = NEW_ERROR(loc);
14771 }
14772 return asgn;
14773}
14774
14775static NODE *
14776new_ary_op_assign(struct parser_params *p, NODE *ary,
14777 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14778 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14779{
14780 NODE *asgn;
14781
14782 aryset_check(p, args);
14783 args = make_list(args, args_loc);
14784 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14785 fixpos(asgn, ary);
14786 return asgn;
14787}
14788
14789static NODE *
14790new_attr_op_assign(struct parser_params *p, NODE *lhs,
14791 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14792 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14793{
14794 NODE *asgn;
14795
14796 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14797 fixpos(asgn, lhs);
14798 return asgn;
14799}
14800
14801static NODE *
14802new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14803{
14804 NODE *asgn;
14805
14806 if (lhs) {
14807 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14808 }
14809 else {
14810 asgn = NEW_ERROR(loc);
14811 }
14812 fixpos(asgn, lhs);
14813 return asgn;
14814}
14815
14816static NODE *
14817const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14818{
14819 if (p->ctxt.in_def) {
14820#ifndef RIPPER
14821 yyerror1(loc, "dynamic constant assignment");
14822#else
14823 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14824#endif
14825 }
14826 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14827}
14828
14829#ifdef RIPPER
14830static VALUE
14831assign_error(struct parser_params *p, const char *mesg, VALUE a)
14832{
14833 a = dispatch2(assign_error, ERR_MESG(), a);
14834 ripper_error(p);
14835 return a;
14836}
14837#endif
14838
14839static NODE *
14840new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14841{
14842 NODE *result = head;
14843 if (rescue) {
14844 NODE *tmp = rescue_else ? rescue_else : rescue;
14845 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14846
14847 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14848 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14849 }
14850 if (ensure) {
14851 result = NEW_ENSURE(result, ensure, loc);
14852 }
14853 fixpos(result, head);
14854 return result;
14855}
14856
14857static void
14858warn_unused_var(struct parser_params *p, struct local_vars *local)
14859{
14860 int cnt;
14861
14862 if (!local->used) return;
14863 cnt = local->used->pos;
14864 if (cnt != local->vars->pos) {
14865 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14866 }
14867#ifndef RIPPER
14868 ID *v = local->vars->tbl;
14869 ID *u = local->used->tbl;
14870 for (int i = 0; i < cnt; ++i) {
14871 if (!v[i] || (u[i] & LVAR_USED)) continue;
14872 if (is_private_local_id(p, v[i])) continue;
14873 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14874 }
14875#endif
14876}
14877
14878static void
14879local_push(struct parser_params *p, int toplevel_scope)
14880{
14881 struct local_vars *local;
14882 int inherits_dvars = toplevel_scope && compile_for_eval;
14883 int warn_unused_vars = RTEST(ruby_verbose);
14884
14885 local = ALLOC(struct local_vars);
14886 local->prev = p->lvtbl;
14887 local->args = vtable_alloc(0);
14888 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14889#ifndef RIPPER
14890 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14891 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14892#endif
14893 local->numparam.outer = 0;
14894 local->numparam.inner = 0;
14895 local->numparam.current = 0;
14896 local->it = 0;
14897 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14898
14899# if WARN_PAST_SCOPE
14900 local->past = 0;
14901# endif
14902 CMDARG_PUSH(0);
14903 COND_PUSH(0);
14904 p->lvtbl = local;
14905}
14906
14907static void
14908vtable_chain_free(struct parser_params *p, struct vtable *table)
14909{
14910 while (!DVARS_TERMINAL_P(table)) {
14911 struct vtable *cur_table = table;
14912 table = cur_table->prev;
14913 vtable_free(cur_table);
14914 }
14915}
14916
14917static void
14918local_free(struct parser_params *p, struct local_vars *local)
14919{
14920 vtable_chain_free(p, local->used);
14921
14922# if WARN_PAST_SCOPE
14923 vtable_chain_free(p, local->past);
14924# endif
14925
14926 vtable_chain_free(p, local->args);
14927 vtable_chain_free(p, local->vars);
14928
14929 ruby_sized_xfree(local, sizeof(struct local_vars));
14930}
14931
14932static void
14933local_pop(struct parser_params *p)
14934{
14935 struct local_vars *local = p->lvtbl->prev;
14936 if (p->lvtbl->used) {
14937 warn_unused_var(p, p->lvtbl);
14938 }
14939
14940 local_free(p, p->lvtbl);
14941 p->lvtbl = local;
14942
14943 CMDARG_POP();
14944 COND_POP();
14945}
14946
14947static rb_ast_id_table_t *
14948local_tbl(struct parser_params *p)
14949{
14950 int cnt_args = vtable_size(p->lvtbl->args);
14951 int cnt_vars = vtable_size(p->lvtbl->vars);
14952 int cnt = cnt_args + cnt_vars;
14953 int i, j;
14954 rb_ast_id_table_t *tbl;
14955
14956 if (cnt <= 0) return 0;
14957 tbl = rb_ast_new_local_table(p->ast, cnt);
14958 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14959 /* remove IDs duplicated to warn shadowing */
14960 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14961 ID id = p->lvtbl->vars->tbl[i];
14962 if (!vtable_included(p->lvtbl->args, id)) {
14963 tbl->ids[j++] = id;
14964 }
14965 }
14966 if (j < cnt) {
14967 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14968 }
14969
14970 return tbl;
14971}
14972
14973static void
14974numparam_name(struct parser_params *p, ID id)
14975{
14976 if (!NUMPARAM_ID_P(id)) return;
14977 compile_error(p, "_%d is reserved for numbered parameter",
14978 NUMPARAM_ID_TO_IDX(id));
14979}
14980
14981static void
14982arg_var(struct parser_params *p, ID id)
14983{
14984 numparam_name(p, id);
14985 vtable_add(p->lvtbl->args, id);
14986}
14987
14988static void
14989local_var(struct parser_params *p, ID id)
14990{
14991 numparam_name(p, id);
14992 vtable_add(p->lvtbl->vars, id);
14993 if (p->lvtbl->used) {
14994 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14995 }
14996}
14997
14998#ifndef RIPPER
14999int
15000rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15001{
15002 return rb_local_defined(id, iseq);
15003}
15004#endif
15005
15006static int
15007local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15008{
15009 struct vtable *vars, *args, *used;
15010
15011 vars = p->lvtbl->vars;
15012 args = p->lvtbl->args;
15013 used = p->lvtbl->used;
15014
15015 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15016 vars = vars->prev;
15017 args = args->prev;
15018 if (used) used = used->prev;
15019 }
15020
15021 if (vars && vars->prev == DVARS_INHERIT) {
15022 return rb_parser_local_defined(p, id, p->parent_iseq);
15023 }
15024 else if (vtable_included(args, id)) {
15025 return 1;
15026 }
15027 else {
15028 int i = vtable_included(vars, id);
15029 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15030 return i != 0;
15031 }
15032}
15033
15034static int
15035local_id(struct parser_params *p, ID id)
15036{
15037 return local_id_ref(p, id, NULL);
15038}
15039
15040static int
15041check_forwarding_args(struct parser_params *p)
15042{
15043 if (local_id(p, idFWD_ALL)) return TRUE;
15044 compile_error(p, "unexpected ...");
15045 return FALSE;
15046}
15047
15048static void
15049add_forwarding_args(struct parser_params *p)
15050{
15051 arg_var(p, idFWD_REST);
15052#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15053 arg_var(p, idFWD_KWREST);
15054#endif
15055 arg_var(p, idFWD_BLOCK);
15056 arg_var(p, idFWD_ALL);
15057}
15058
15059static void
15060forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15061{
15062 bool conflict = false;
15063
15064 struct vtable *vars, *args;
15065
15066 vars = p->lvtbl->vars;
15067 args = p->lvtbl->args;
15068
15069 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15070 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15071 vars = vars->prev;
15072 args = args->prev;
15073 }
15074
15075 bool found = false;
15076 if (vars && vars->prev == DVARS_INHERIT && !found) {
15077 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15078 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15079 }
15080 else {
15081 found = (vtable_included(args, arg) &&
15082 !(all && vtable_included(args, all)));
15083 }
15084
15085 if (!found) {
15086 compile_error(p, "no anonymous %s parameter", var);
15087 }
15088 else if (conflict) {
15089 compile_error(p, "anonymous %s parameter is also used within block", var);
15090 }
15091}
15092
15093static NODE *
15094new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15095{
15096 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15097#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15098 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15099#endif
15100 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15101 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15102 block->forwarding = TRUE;
15103#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15104 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15105#endif
15106 return arg_blk_pass(args, block);
15107}
15108
15109static NODE *
15110numparam_push(struct parser_params *p)
15111{
15112 struct local_vars *local = p->lvtbl;
15113 NODE *inner = local->numparam.inner;
15114 if (!local->numparam.outer) {
15115 local->numparam.outer = local->numparam.current;
15116 }
15117 local->numparam.inner = 0;
15118 local->numparam.current = 0;
15119 local->it = 0;
15120 return inner;
15121}
15122
15123static void
15124numparam_pop(struct parser_params *p, NODE *prev_inner)
15125{
15126 struct local_vars *local = p->lvtbl;
15127 if (prev_inner) {
15128 /* prefer first one */
15129 local->numparam.inner = prev_inner;
15130 }
15131 else if (local->numparam.current) {
15132 /* current and inner are exclusive */
15133 local->numparam.inner = local->numparam.current;
15134 }
15135 if (p->max_numparam > NO_PARAM) {
15136 /* current and outer are exclusive */
15137 local->numparam.current = local->numparam.outer;
15138 local->numparam.outer = 0;
15139 }
15140 else {
15141 /* no numbered parameter */
15142 local->numparam.current = 0;
15143 }
15144 local->it = 0;
15145}
15146
15147static const struct vtable *
15148dyna_push(struct parser_params *p)
15149{
15150 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15151 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15152 if (p->lvtbl->used) {
15153 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15154 }
15155 return p->lvtbl->args;
15156}
15157
15158static void
15159dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15160{
15161 struct vtable *tmp = *vtblp;
15162 *vtblp = tmp->prev;
15163# if WARN_PAST_SCOPE
15164 if (p->past_scope_enabled) {
15165 tmp->prev = p->lvtbl->past;
15166 p->lvtbl->past = tmp;
15167 return;
15168 }
15169# endif
15170 vtable_free(tmp);
15171}
15172
15173static void
15174dyna_pop_1(struct parser_params *p)
15175{
15176 struct vtable *tmp;
15177
15178 if ((tmp = p->lvtbl->used) != 0) {
15179 warn_unused_var(p, p->lvtbl);
15180 p->lvtbl->used = p->lvtbl->used->prev;
15181 vtable_free(tmp);
15182 }
15183 dyna_pop_vtable(p, &p->lvtbl->args);
15184 dyna_pop_vtable(p, &p->lvtbl->vars);
15185}
15186
15187static void
15188dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15189{
15190 while (p->lvtbl->args != lvargs) {
15191 dyna_pop_1(p);
15192 if (!p->lvtbl->args) {
15193 struct local_vars *local = p->lvtbl->prev;
15194 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15195 p->lvtbl = local;
15196 }
15197 }
15198 dyna_pop_1(p);
15199}
15200
15201static int
15202dyna_in_block(struct parser_params *p)
15203{
15204 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15205}
15206
15207#ifndef RIPPER
15208int
15209dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15210{
15211 struct vtable *vars, *args, *used;
15212 int i;
15213
15214 args = p->lvtbl->args;
15215 vars = p->lvtbl->vars;
15216 used = p->lvtbl->used;
15217
15218 while (!DVARS_TERMINAL_P(vars)) {
15219 if (vtable_included(args, id)) {
15220 return 1;
15221 }
15222 if ((i = vtable_included(vars, id)) != 0) {
15223 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15224 return 1;
15225 }
15226 args = args->prev;
15227 vars = vars->prev;
15228 if (!vidrefp) used = 0;
15229 if (used) used = used->prev;
15230 }
15231
15232 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15233 return rb_dvar_defined(id, p->parent_iseq);
15234 }
15235
15236 return 0;
15237}
15238#endif
15239
15240static int
15241dvar_defined(struct parser_params *p, ID id)
15242{
15243 return dvar_defined_ref(p, id, NULL);
15244}
15245
15246static int
15247dvar_curr(struct parser_params *p, ID id)
15248{
15249 return (vtable_included(p->lvtbl->args, id) ||
15250 vtable_included(p->lvtbl->vars, id));
15251}
15252
15253static void
15254reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15255{
15256 compile_error(p,
15257 "regexp encoding option '%c' differs from source encoding '%s'",
15258 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15259}
15260
15261#ifndef RIPPER
15262static rb_encoding *
15263find_enc(struct parser_params* p, const char *name)
15264{
15265 int idx = rb_enc_find_index(name);
15266 if (idx < 0) {
15267 rb_bug("unknown encoding name: %s", name);
15268 }
15269
15270 return rb_enc_from_index(idx);
15271}
15272
15273static rb_encoding *
15274kcode_to_enc(struct parser_params* p, int kcode)
15275{
15276 rb_encoding *enc;
15277
15278 switch (kcode) {
15279 case ENC_ASCII8BIT:
15280 enc = rb_ascii8bit_encoding();
15281 break;
15282 case ENC_EUC_JP:
15283 enc = find_enc(p, "EUC-JP");
15284 break;
15285 case ENC_Windows_31J:
15286 enc = find_enc(p, "Windows-31J");
15287 break;
15288 case ENC_UTF8:
15289 enc = rb_utf8_encoding();
15290 break;
15291 default:
15292 enc = NULL;
15293 break;
15294 }
15295
15296 return enc;
15297}
15298
15299int
15300rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15301{
15302 int c = RE_OPTION_ENCODING_IDX(options);
15303
15304 if (c) {
15305 int opt, idx;
15306 rb_encoding *enc;
15307
15308 char_to_option_kcode(c, &opt, &idx);
15309 enc = kcode_to_enc(p, idx);
15310 if (enc != rb_parser_str_get_encoding(str) &&
15311 !rb_parser_is_ascii_string(p, str)) {
15312 goto error;
15313 }
15314 rb_parser_string_set_encoding(str, enc);
15315 }
15316 else if (RE_OPTION_ENCODING_NONE(options)) {
15317 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15318 !rb_parser_is_ascii_string(p, str)) {
15319 c = 'n';
15320 goto error;
15321 }
15322 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15323 }
15324 else if (rb_is_usascii_enc(p->enc)) {
15325 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15326 }
15327 return 0;
15328
15329 error:
15330 return c;
15331}
15332#endif
15333
15334static void
15335reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15336{
15337 int c = rb_reg_fragment_setenc(p, str, options);
15338 if (c) reg_fragment_enc_error(p, str, c);
15339}
15340
15341#ifndef UNIVERSAL_PARSER
15342typedef struct {
15343 struct parser_params* parser;
15344 rb_encoding *enc;
15345 NODE *succ_block;
15346 const YYLTYPE *loc;
15347 rb_parser_assignable_func assignable;
15348} reg_named_capture_assign_t;
15349
15350static int
15351reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15352 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15353{
15354 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15355 struct parser_params* p = arg->parser;
15356 rb_encoding *enc = arg->enc;
15357 long len = name_end - name;
15358 const char *s = (const char *)name;
15359
15360 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15361}
15362
15363static NODE *
15364reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15365{
15366 reg_named_capture_assign_t arg;
15367
15368 arg.parser = p;
15369 arg.enc = rb_enc_get(regexp);
15370 arg.succ_block = 0;
15371 arg.loc = loc;
15372 arg.assignable = assignable;
15373 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15374
15375 if (!arg.succ_block) return 0;
15376 return RNODE_BLOCK(arg.succ_block)->nd_next;
15377}
15378#endif
15379
15380#ifndef RIPPER
15381NODE *
15382rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15383{
15384 return assignable(p, id, val, loc);
15385}
15386
15387int
15388rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15389 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15390{
15391 ID var;
15392 NODE *node, *succ;
15393
15394 if (!len) return ST_CONTINUE;
15395 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15396 return ST_CONTINUE;
15397
15398 var = intern_cstr(s, len, enc);
15399 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15400 if (!lvar_defined(p, var)) return ST_CONTINUE;
15401 }
15402 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15403 succ = *succ_block;
15404 if (!succ) succ = NEW_ERROR(loc);
15405 succ = block_append(p, succ, node);
15406 *succ_block = succ;
15407 return ST_CONTINUE;
15408}
15409#endif
15410
15411static VALUE
15412parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15413{
15414 VALUE str2;
15415 reg_fragment_setenc(p, str, options);
15416 str2 = rb_str_new_parser_string(str);
15417 return rb_parser_reg_compile(p, str2, options);
15418}
15419
15420#ifndef RIPPER
15421VALUE
15422rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15423{
15424 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15425}
15426#endif
15427
15428static VALUE
15429reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15430{
15431 VALUE re;
15432 VALUE err;
15433
15434 err = rb_errinfo();
15435 re = parser_reg_compile(p, str, options);
15436 if (NIL_P(re)) {
15437 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15438 rb_set_errinfo(err);
15439 compile_error(p, "%"PRIsVALUE, m);
15440 return Qnil;
15441 }
15442 return re;
15443}
15444
15445#ifndef RIPPER
15446void
15447rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15448{
15449 p->do_print = print;
15450 p->do_loop = loop;
15451 p->do_chomp = chomp;
15452 p->do_split = split;
15453}
15454
15455static NODE *
15456parser_append_options(struct parser_params *p, NODE *node)
15457{
15458 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15459 const YYLTYPE *const LOC = &default_location;
15460
15461 if (p->do_print) {
15462 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15463 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15464 LOC);
15465 node = block_append(p, node, print);
15466 }
15467
15468 if (p->do_loop) {
15469 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15470
15471 if (p->do_split) {
15472 ID ifs = rb_intern("$;");
15473 ID fields = rb_intern("$F");
15474 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15475 NODE *split = NEW_GASGN(fields,
15476 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15477 rb_intern("split"), args, LOC),
15478 LOC);
15479 node = block_append(p, split, node);
15480 }
15481 if (p->do_chomp) {
15482 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15483 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15484 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15485 }
15486
15487 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15488 }
15489
15490 return node;
15491}
15492
15493void
15494rb_init_parse(void)
15495{
15496 /* just to suppress unused-function warnings */
15497 (void)nodetype;
15498 (void)nodeline;
15499}
15500
15501ID
15502internal_id(struct parser_params *p)
15503{
15504 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15505}
15506#endif /* !RIPPER */
15507
15508static void
15509parser_initialize(struct parser_params *p)
15510{
15511 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15512 p->command_start = TRUE;
15513 p->ruby_sourcefile_string = Qnil;
15514 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15515 string_buffer_init(p);
15516 p->node_id = 0;
15517 p->delayed.token = NULL;
15518 p->frozen_string_literal = -1; /* not specified */
15519#ifndef RIPPER
15520 p->error_buffer = Qfalse;
15521 p->end_expect_token_locations = NULL;
15522 p->token_id = 0;
15523 p->tokens = NULL;
15524#else
15525 p->result = Qnil;
15526 p->parsing_thread = Qnil;
15527 p->s_value = Qnil;
15528 p->s_lvalue = Qnil;
15529 p->s_value_stack = rb_ary_new();
15530#endif
15531 p->debug_buffer = Qnil;
15532 p->debug_output = rb_ractor_stdout();
15533 p->enc = rb_utf8_encoding();
15534 p->exits = 0;
15535}
15536
15537#ifdef RIPPER
15538#define rb_ruby_parser_mark ripper_parser_mark
15539#define rb_ruby_parser_free ripper_parser_free
15540#define rb_ruby_parser_memsize ripper_parser_memsize
15541#endif
15542
15543void
15544rb_ruby_parser_mark(void *ptr)
15545{
15546 struct parser_params *p = (struct parser_params*)ptr;
15547
15548 rb_gc_mark(p->ruby_sourcefile_string);
15549#ifndef RIPPER
15550 rb_gc_mark(p->error_buffer);
15551#else
15552 rb_gc_mark(p->value);
15553 rb_gc_mark(p->result);
15554 rb_gc_mark(p->parsing_thread);
15555 rb_gc_mark(p->s_value);
15556 rb_gc_mark(p->s_lvalue);
15557 rb_gc_mark(p->s_value_stack);
15558#endif
15559 rb_gc_mark(p->debug_buffer);
15560 rb_gc_mark(p->debug_output);
15561}
15562
15563void
15564rb_ruby_parser_free(void *ptr)
15565{
15566 struct parser_params *p = (struct parser_params*)ptr;
15567 struct local_vars *local, *prev;
15568
15569 if (p->ast) {
15570 rb_ast_free(p->ast);
15571 }
15572
15573 if (p->warn_duplicate_keys_table) {
15574 st_free_table(p->warn_duplicate_keys_table);
15575 }
15576
15577#ifndef RIPPER
15578 if (p->tokens) {
15579 rb_parser_ary_free(p, p->tokens);
15580 }
15581#endif
15582
15583 if (p->tokenbuf) {
15584 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15585 }
15586
15587 for (local = p->lvtbl; local; local = prev) {
15588 prev = local->prev;
15589 local_free(p, local);
15590 }
15591
15592 {
15593 token_info *ptinfo;
15594 while ((ptinfo = p->token_info) != 0) {
15595 p->token_info = ptinfo->next;
15596 xfree(ptinfo);
15597 }
15598 }
15599 string_buffer_free(p);
15600
15601 if (p->pvtbl) {
15602 st_free_table(p->pvtbl);
15603 }
15604
15605 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15606 st_free_table(p->case_labels);
15607 }
15608
15609 xfree(p->lex.strterm);
15610 p->lex.strterm = 0;
15611
15612 xfree(ptr);
15613}
15614
15615size_t
15616rb_ruby_parser_memsize(const void *ptr)
15617{
15618 struct parser_params *p = (struct parser_params*)ptr;
15619 struct local_vars *local;
15620 size_t size = sizeof(*p);
15621
15622 size += p->toksiz;
15623 for (local = p->lvtbl; local; local = local->prev) {
15624 size += sizeof(*local);
15625 if (local->vars) size += local->vars->capa * sizeof(ID);
15626 }
15627 return size;
15628}
15629
15630#ifndef RIPPER
15631#undef rb_reserved_word
15632
15633const struct kwtable *
15634rb_reserved_word(const char *str, unsigned int len)
15635{
15636 return reserved_word(str, len);
15637}
15638
15639#ifdef UNIVERSAL_PARSER
15640rb_parser_t *
15641rb_ruby_parser_allocate(const rb_parser_config_t *config)
15642{
15643 /* parser_initialize expects fields to be set to 0 */
15644 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15645 p->config = config;
15646 return p;
15647}
15648
15649rb_parser_t *
15650rb_ruby_parser_new(const rb_parser_config_t *config)
15651{
15652 /* parser_initialize expects fields to be set to 0 */
15653 rb_parser_t *p = rb_ruby_parser_allocate(config);
15654 parser_initialize(p);
15655 return p;
15656}
15657#else
15658rb_parser_t *
15659rb_ruby_parser_allocate(void)
15660{
15661 /* parser_initialize expects fields to be set to 0 */
15662 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15663 return p;
15664}
15665
15666rb_parser_t *
15667rb_ruby_parser_new(void)
15668{
15669 /* parser_initialize expects fields to be set to 0 */
15670 rb_parser_t *p = rb_ruby_parser_allocate();
15671 parser_initialize(p);
15672 return p;
15673}
15674#endif
15675
15676rb_parser_t *
15677rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15678{
15679 p->error_buffer = main ? Qfalse : Qnil;
15680 p->parent_iseq = base;
15681 return p;
15682}
15683
15684void
15685rb_ruby_parser_set_script_lines(rb_parser_t *p)
15686{
15687 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15688}
15689
15690void
15691rb_ruby_parser_error_tolerant(rb_parser_t *p)
15692{
15693 p->error_tolerant = 1;
15694}
15695
15696void
15697rb_ruby_parser_keep_tokens(rb_parser_t *p)
15698{
15699 p->keep_tokens = 1;
15700 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15701}
15702
15703rb_encoding *
15704rb_ruby_parser_encoding(rb_parser_t *p)
15705{
15706 return p->enc;
15707}
15708
15709int
15710rb_ruby_parser_end_seen_p(rb_parser_t *p)
15711{
15712 return p->ruby__end__seen;
15713}
15714
15715int
15716rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15717{
15718 p->debug = flag;
15719 return flag;
15720}
15721#endif /* !RIPPER */
15722
15723#ifdef RIPPER
15724int
15725rb_ruby_parser_get_yydebug(rb_parser_t *p)
15726{
15727 return p->debug;
15728}
15729
15730void
15731rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15732{
15733 p->value = value;
15734}
15735
15736int
15737rb_ruby_parser_error_p(rb_parser_t *p)
15738{
15739 return p->error_p;
15740}
15741
15742VALUE
15743rb_ruby_parser_debug_output(rb_parser_t *p)
15744{
15745 return p->debug_output;
15746}
15747
15748void
15749rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15750{
15751 p->debug_output = output;
15752}
15753
15754VALUE
15755rb_ruby_parser_parsing_thread(rb_parser_t *p)
15756{
15757 return p->parsing_thread;
15758}
15759
15760void
15761rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15762{
15763 p->parsing_thread = parsing_thread;
15764}
15765
15766void
15767rb_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)
15768{
15769 p->lex.gets = gets;
15770 p->lex.input = input;
15771 p->eofp = 0;
15772 p->ruby_sourcefile_string = sourcefile_string;
15773 p->ruby_sourcefile = sourcefile;
15774 p->ruby_sourceline = sourceline;
15775}
15776
15777VALUE
15778rb_ruby_parser_result(rb_parser_t *p)
15779{
15780 return p->result;
15781}
15782
15783rb_encoding *
15784rb_ruby_parser_enc(rb_parser_t *p)
15785{
15786 return p->enc;
15787}
15788
15789VALUE
15790rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15791{
15792 return p->ruby_sourcefile_string;
15793}
15794
15795int
15796rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15797{
15798 return p->ruby_sourceline;
15799}
15800
15801int
15802rb_ruby_parser_lex_state(rb_parser_t *p)
15803{
15804 return p->lex.state;
15805}
15806
15807void
15808rb_ruby_ripper_parse0(rb_parser_t *p)
15809{
15810 parser_prepare(p);
15811 p->ast = rb_ast_new();
15812 ripper_yyparse((void*)p);
15813 rb_ast_free(p->ast);
15814 p->ast = 0;
15815 p->eval_tree = 0;
15816 p->eval_tree_begin = 0;
15817}
15818
15819int
15820rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15821{
15822 return dedent_string(p, string, width);
15823}
15824
15825int
15826rb_ruby_ripper_initialized_p(rb_parser_t *p)
15827{
15828 return p->lex.input != 0;
15829}
15830
15831void
15832rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15833{
15834 parser_initialize(p);
15835}
15836
15837long
15838rb_ruby_ripper_column(rb_parser_t *p)
15839{
15840 return p->lex.ptok - p->lex.pbeg;
15841}
15842
15843long
15844rb_ruby_ripper_token_len(rb_parser_t *p)
15845{
15846 return p->lex.pcur - p->lex.ptok;
15847}
15848
15849rb_parser_string_t *
15850rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15851{
15852 return p->lex.lastline;
15853}
15854
15855VALUE
15856rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15857{
15858 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15859}
15860
15861#ifdef UNIVERSAL_PARSER
15862rb_parser_t *
15863rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15864{
15865 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15866 p->config = config;
15867 return p;
15868}
15869#endif
15870
15871struct parser_params*
15872rb_ruby_ripper_parser_allocate(void)
15873{
15874 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15875}
15876#endif /* RIPPER */
15877
15878#ifndef RIPPER
15879void
15880rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15881{
15882 va_list ap;
15883 VALUE mesg = p->debug_buffer;
15884
15885 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15886 va_start(ap, fmt);
15887 rb_str_vcatf(mesg, fmt, ap);
15888 va_end(ap);
15889 if (char_at_end(p, mesg, 0) == '\n') {
15890 rb_io_write(p->debug_output, mesg);
15891 p->debug_buffer = Qnil;
15892 }
15893}
15894
15895static void
15896parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15897{
15898 va_list ap;
15899 int lineno, column;
15900
15901 if (loc) {
15902 lineno = loc->end_pos.lineno;
15903 column = loc->end_pos.column;
15904 }
15905 else {
15906 lineno = p->ruby_sourceline;
15907 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15908 }
15909
15910 rb_io_flush(p->debug_output);
15911 p->error_p = 1;
15912 va_start(ap, fmt);
15913 p->error_buffer =
15914 rb_syntax_error_append(p->error_buffer,
15915 p->ruby_sourcefile_string,
15916 lineno, column,
15917 p->enc, fmt, ap);
15918 va_end(ap);
15919}
15920
15921static size_t
15922count_char(const char *str, int c)
15923{
15924 int n = 0;
15925 while (str[n] == c) ++n;
15926 return n;
15927}
15928
15929/*
15930 * strip enclosing double-quotes, same as the default yytnamerr except
15931 * for that single-quotes matching back-quotes do not stop stripping.
15932 *
15933 * "\"`class' keyword\"" => "`class' keyword"
15934 */
15935size_t
15936rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15937{
15938 if (*yystr == '"') {
15939 size_t yyn = 0, bquote = 0;
15940 const char *yyp = yystr;
15941
15942 while (*++yyp) {
15943 switch (*yyp) {
15944 case '\'':
15945 if (!bquote) {
15946 bquote = count_char(yyp+1, '\'') + 1;
15947 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15948 yyn += bquote;
15949 yyp += bquote - 1;
15950 break;
15951 }
15952 else {
15953 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15954 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15955 yyn += bquote;
15956 yyp += bquote - 1;
15957 bquote = 0;
15958 break;
15959 }
15960 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15961 if (yyres) memcpy(yyres + yyn, yyp, 3);
15962 yyn += 3;
15963 yyp += 2;
15964 break;
15965 }
15966 goto do_not_strip_quotes;
15967 }
15968
15969 case ',':
15970 goto do_not_strip_quotes;
15971
15972 case '\\':
15973 if (*++yyp != '\\')
15974 goto do_not_strip_quotes;
15975 /* Fall through. */
15976 default:
15977 if (yyres)
15978 yyres[yyn] = *yyp;
15979 yyn++;
15980 break;
15981
15982 case '"':
15983 case '\0':
15984 if (yyres)
15985 yyres[yyn] = '\0';
15986 return yyn;
15987 }
15988 }
15989 do_not_strip_quotes: ;
15990 }
15991
15992 if (!yyres) return strlen(yystr);
15993
15994 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
15995}
15996#endif
15997
15998#ifdef RIPPER
15999#define validate(x) (void)(x)
16000
16001static VALUE
16002ripper_dispatch0(struct parser_params *p, ID mid)
16003{
16004 return rb_funcall(p->value, mid, 0);
16005}
16006
16007static VALUE
16008ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16009{
16010 validate(a);
16011 return rb_funcall(p->value, mid, 1, a);
16012}
16013
16014static VALUE
16015ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16016{
16017 validate(a);
16018 validate(b);
16019 return rb_funcall(p->value, mid, 2, a, b);
16020}
16021
16022static VALUE
16023ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16024{
16025 validate(a);
16026 validate(b);
16027 validate(c);
16028 return rb_funcall(p->value, mid, 3, a, b, c);
16029}
16030
16031static VALUE
16032ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16033{
16034 validate(a);
16035 validate(b);
16036 validate(c);
16037 validate(d);
16038 return rb_funcall(p->value, mid, 4, a, b, c, d);
16039}
16040
16041static VALUE
16042ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16043{
16044 validate(a);
16045 validate(b);
16046 validate(c);
16047 validate(d);
16048 validate(e);
16049 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16050}
16051
16052static VALUE
16053ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16054{
16055 validate(a);
16056 validate(b);
16057 validate(c);
16058 validate(d);
16059 validate(e);
16060 validate(f);
16061 validate(g);
16062 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16063}
16064
16065void
16066ripper_error(struct parser_params *p)
16067{
16068 p->error_p = TRUE;
16069}
16070
16071VALUE
16072ripper_value(struct parser_params *p)
16073{
16074 (void)yystpcpy; /* may not used in newer bison */
16075
16076 return p->value;
16077}
16078
16079#endif /* RIPPER */
16080/*
16081 * Local variables:
16082 * mode: c
16083 * c-file-style: "ruby"
16084 * End:
16085 */