Ruby 4.1.0dev (2026-10-14 revision d037ba85ef1ff1b8a503f240e5ed083b644f1ec5)
parse.y (d037ba85ef1ff1b8a503f240e5ed083b644f1ec5)
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%{
13
14#if !YYPURE
15# error needs pure parser
16#endif
17#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20
21/* For Ripper */
22#ifdef RUBY_EXTCONF_H
23# include RUBY_EXTCONF_H
24#endif
25
26#include "ruby/internal/config.h"
27#include "internal/thread.h"
28
29#include <errno.h>
30
31#ifdef UNIVERSAL_PARSER
32
33#include "internal/ruby_parser.h"
34#include "parser_node.h"
35#include "universal_parser.c"
36
37#ifdef RIPPER
38#define STATIC_ID2SYM p->config->static_id2sym
39#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
40#endif
41
42#else
43
44#include "internal.h"
45#include "internal/array.h"
46#include "internal/compile.h"
47#include "internal/compilers.h"
48#include "internal/complex.h"
49#include "internal/encoding.h"
50#include "internal/error.h"
51#include "internal/gc.h"
52#include "internal/hash.h"
53#include "internal/io.h"
54#include "internal/numeric.h"
55#include "internal/parse.h"
56#include "internal/rational.h"
57#include "internal/re.h"
58#include "internal/ruby_parser.h"
59#include "internal/symbol.h"
60#include "internal/thread.h"
61#include "node.h"
62#include "parser_node.h"
63#include "probes.h"
64#include "regenc.h"
65#include "ruby/encoding.h"
66#include "ruby/regex.h"
67#include "ruby/ruby.h"
68#include "ruby/st.h"
69#include "ruby/util.h"
70#include "ruby/ractor.h"
71#include "symbol.h"
72
73#ifndef RIPPER
74static VALUE
75syntax_error_new(void)
76{
77 return rb_class_new_instance(0, 0, rb_eSyntaxError);
78}
79#endif
80
81static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
82
83#define compile_callback rb_suppress_tracing
84#endif /* !UNIVERSAL_PARSER */
85
86#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
87#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
88
89static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
90
91#ifndef RIPPER
92static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
93#endif
94
95static int
96node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
97{
98 return (n1->minus != n2->minus ||
99 n1->base != n2->base ||
100 strcmp(n1->val, n2->val));
101}
102
103static int
104node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
105{
106 return (n1->minus != n2->minus ||
107 strcmp(n1->val, n2->val));
108}
109
110static int
111node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
112{
113 return (n1->minus != n2->minus ||
114 n1->base != n2->base ||
115 n1->seen_point != n2->seen_point ||
116 strcmp(n1->val, n2->val));
117}
118
119static int
120node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
121{
122 return (n1->minus != n2->minus ||
123 n1->base != n2->base ||
124 n1->seen_point != n2->seen_point ||
125 n1->type != n2->type ||
126 strcmp(n1->val, n2->val));
127}
128
129static int
130rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
131{
132 return (n1->options != n2->options ||
133 rb_parser_string_hash_cmp(n1->string, n2->string));
134}
135
136static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
137static st_index_t rb_char_p_hash(const char *c);
138
139static int
140literal_cmp(st_data_t val, st_data_t lit)
141{
142 if (val == lit) return 0;
143
144 NODE *node_val = RNODE(val);
145 NODE *node_lit = RNODE(lit);
146 enum node_type type_val = nd_type(node_val);
147 enum node_type type_lit = nd_type(node_lit);
148
149 if (type_val != type_lit) {
150 return -1;
151 }
152
153 switch (type_lit) {
154 case NODE_INTEGER:
155 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
156 case NODE_FLOAT:
157 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
158 case NODE_RATIONAL:
159 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
160 case NODE_IMAGINARY:
161 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
162 case NODE_STR:
163 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
164 case NODE_SYM:
165 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
166 case NODE_REGX:
167 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
168 case NODE_LINE:
169 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
170 case NODE_FILE:
171 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
172 case NODE_ENCODING:
173 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
174 default:
175#ifdef UNIVERSAL_PARSER
176 abort();
177#else
178 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
179#endif
180 }
181}
182
183static st_index_t
184literal_hash(st_data_t a)
185{
186 NODE *node = (NODE *)a;
187 enum node_type type = nd_type(node);
188
189 switch (type) {
190 case NODE_INTEGER:
191 return rb_char_p_hash(RNODE_INTEGER(node)->val);
192 case NODE_FLOAT:
193 return rb_char_p_hash(RNODE_FLOAT(node)->val);
194 case NODE_RATIONAL:
195 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
196 case NODE_IMAGINARY:
197 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
198 case NODE_STR:
199 return rb_parser_str_hash(RNODE_STR(node)->string);
200 case NODE_SYM:
201 return rb_parser_str_hash(RNODE_SYM(node)->string);
202 case NODE_REGX:
203 return rb_parser_str_hash(RNODE_REGX(node)->string);
204 case NODE_LINE:
205 return (st_index_t)node->nd_loc.beg_pos.lineno;
206 case NODE_FILE:
207 return rb_parser_str_hash(RNODE_FILE(node)->path);
208 case NODE_ENCODING:
209 return (st_index_t)RNODE_ENCODING(node)->enc;
210 default:
211#ifdef UNIVERSAL_PARSER
212 abort();
213#else
214 rb_bug("unexpected node: %s", ruby_node_name(type));
215#endif
216 }
217}
218
219static inline int
220parse_isascii(int c)
221{
222 return '\0' <= c && c <= '\x7f';
223}
224
225#undef ISASCII
226#define ISASCII parse_isascii
227
228static inline int
229parse_isspace(int c)
230{
231 return c == ' ' || ('\t' <= c && c <= '\r');
232}
233
234#undef ISSPACE
235#define ISSPACE parse_isspace
236
237static inline int
238parse_iscntrl(int c)
239{
240 return ('\0' <= c && c < ' ') || c == '\x7f';
241}
242
243#undef ISCNTRL
244#define ISCNTRL(c) parse_iscntrl(c)
245
246static inline int
247parse_isupper(int c)
248{
249 return 'A' <= c && c <= 'Z';
250}
251
252static inline int
253parse_islower(int c)
254{
255 return 'a' <= c && c <= 'z';
256}
257
258static inline int
259parse_isalpha(int c)
260{
261 return parse_isupper(c) || parse_islower(c);
262}
263
264#undef ISALPHA
265#define ISALPHA(c) parse_isalpha(c)
266
267static inline int
268parse_isdigit(int c)
269{
270 return '0' <= c && c <= '9';
271}
272
273#undef ISDIGIT
274#define ISDIGIT(c) parse_isdigit(c)
275
276static inline int
277parse_isalnum(int c)
278{
279 return ISALPHA(c) || ISDIGIT(c);
280}
281
282#undef ISALNUM
283#define ISALNUM(c) parse_isalnum(c)
284
285static inline int
286parse_isxdigit(int c)
287{
288 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
289}
290
291#undef ISXDIGIT
292#define ISXDIGIT(c) parse_isxdigit(c)
293
294#include "parser_st.h"
295
296#undef STRCASECMP
297#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
298
299#undef STRNCASECMP
300#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
301
302#ifdef RIPPER
303#include "ripper_init.h"
304#endif
305
306enum rescue_context {
307 before_rescue,
308 after_rescue,
309 after_else,
310 after_ensure,
311};
312
313struct lex_context {
314 unsigned int in_defined: 1;
315 unsigned int in_kwarg: 1;
316 unsigned int in_argdef: 1;
317 unsigned int in_def: 1;
318 unsigned int in_class: 1;
319 unsigned int has_trailing_semicolon: 1;
320 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
321 BITFIELD(enum rescue_context, in_rescue, 2);
322 unsigned int cant_return: 1;
323 unsigned int in_alt_pattern: 1;
324 unsigned int capture_in_pattern: 1;
325};
326
327typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
328
329#if defined(__GNUC__) && !defined(__clang__)
330// Suppress "parameter passing for argument of type 'struct
331// lex_context' changed" notes. `struct lex_context` is file scope,
332// and has no ABI compatibility issue.
333RBIMPL_WARNING_PUSH()
334RBIMPL_WARNING_IGNORED(-Wpsabi)
335RBIMPL_WARNING_POP()
336// Not sure why effective even after popped.
337#endif
338
339#include "parse.h"
340
341#define NO_LEX_CTXT (struct lex_context){0}
342
343#ifndef WARN_PAST_SCOPE
344# define WARN_PAST_SCOPE 0
345#endif
346
347#define TAB_WIDTH 8
348
349#define yydebug (p->debug) /* disable the global variable definition */
350
351#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
352#define YY_LOCATION_PRINT(File, loc, p) \
353 rb_parser_printf(p, "%d.%d-%d.%d", \
354 (loc).beg_pos.lineno, (loc).beg_pos.column,\
355 (loc).end_pos.lineno, (loc).end_pos.column)
356#define YYLLOC_DEFAULT(Current, Rhs, N) \
357 do \
358 if (N) \
359 { \
360 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
361 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
362 } \
363 else \
364 { \
365 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
366 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
367 } \
368 while (0)
369#define YY_(Msgid) \
370 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
371 "nesting too deep" : (Msgid))
372
373#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
374 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
375#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
376 rb_parser_set_location_of_delayed_token(p, &(Current))
377#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
378 rb_parser_set_location_of_heredoc_end(p, &(Current))
379#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
380 rb_parser_set_location_of_dummy_end(p, &(Current))
381#define RUBY_SET_YYLLOC_OF_NONE(Current) \
382 rb_parser_set_location_of_none(p, &(Current))
383#define RUBY_SET_YYLLOC(Current) \
384 rb_parser_set_location(p, &(Current))
385#define RUBY_INIT_YYLLOC() \
386 { \
387 {p->ruby_sourceline, parser_column(p->lex.ptok - p->lex.pbeg)}, \
388 {p->ruby_sourceline, parser_column(p->lex.pcur - p->lex.pbeg)}, \
389 }
390
391/* Saturate instead of wrapping to a column before the line. */
392static inline int
393parser_column(rb_len_t pos)
394{
395#if SIZEOF_INT < SIZEOF_RB_LEN_T
396 if (pos > INT_MAX) return INT_MAX;
397#endif
398 return (int)pos;
399}
400
401#define IS_lex_state_for(x, ls) ((x) & (ls))
402#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
403#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
404#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
405
406# define SET_LEX_STATE(ls) \
407 parser_set_lex_state(p, ls, __LINE__)
408static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
409
410typedef VALUE stack_type;
411
412static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
413
414# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
415# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
416# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
417# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
418# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
419
420/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
421 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
422#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
423#define COND_POP() BITSTACK_POP(cond_stack)
424#define COND_P() BITSTACK_SET_P(cond_stack)
425#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
426
427/* A flag to identify keyword_do_block; "do" keyword after command_call.
428 Example: `foo 1, 2 do`. */
429#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
430#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
431#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
432#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
433
434struct vtable {
435 ID *tbl;
436 int pos;
437 int capa;
438 struct vtable *prev;
439};
440
441struct local_vars {
442 struct vtable *args;
443 struct vtable *vars;
444 struct vtable *used;
445# if WARN_PAST_SCOPE
446 struct vtable *past;
447# endif
448 struct local_vars *prev;
449 struct {
450 NODE *outer, *inner, *current;
451 } numparam;
452 NODE *it;
453};
454
455typedef struct rb_locations_lambda_body_t {
456 NODE *node;
457 YYLTYPE opening_loc;
458 YYLTYPE closing_loc;
459} rb_locations_lambda_body_t;
460
461enum {
462 ORDINAL_PARAM = -1,
463 NO_PARAM = 0,
464 NUMPARAM_MAX = 9,
465};
466
467#define DVARS_INHERIT ((void*)1)
468#define DVARS_TOPSCOPE NULL
469#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
470
471typedef struct token_info {
472 const char *token;
473 rb_code_position_t beg;
474 int indent;
475 int nonspc;
476 struct token_info *next;
477} token_info;
478
479typedef struct end_expect_token_locations {
480 const rb_code_position_t *pos;
481 struct end_expect_token_locations *prev;
482} end_expect_token_locations_t;
483
484typedef struct parser_string_buffer_elem {
485 struct parser_string_buffer_elem *next;
486 long len; /* Total length of allocated buf */
487 long used; /* Current usage of buf */
488 rb_parser_string_t *buf[FLEX_ARY_LEN];
489} parser_string_buffer_elem_t;
490
491typedef struct parser_string_buffer {
492 parser_string_buffer_elem_t *head;
493 parser_string_buffer_elem_t *last;
494} parser_string_buffer_t;
495
496#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
497
498/*
499 Structure of Lexer Buffer:
500
501 lex.pbeg lex.ptok lex.pcur lex.pend
502 | | | |
503 |------------+------------+------------|
504 |<---------->|
505 token
506*/
507struct parser_params {
508 YYSTYPE *lval;
509 YYLTYPE *yylloc;
510
511 struct {
512 rb_strterm_t *strterm;
513 rb_parser_lex_gets_func *gets;
514 rb_parser_input_data input;
515 parser_string_buffer_t string_buffer;
516 rb_parser_string_t *lastline;
517 rb_parser_string_t *nextline;
518 const char *pbeg;
519 const char *pcur;
520 const char *pend;
521 const char *ptok;
522 enum lex_state_e state;
523 /* track the nest level of any parens "()[]{}" */
524 int paren_nest;
525 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
526 int lpar_beg;
527 /* track the nest level of only braces "{}" */
528 int brace_nest;
529 } lex;
530 /* Cache the tail of the and/or chain most recently built by logop(), so a
531 * left-associative chain does not rescan its right branch on every
532 * operator. head is the chain's top node, tail the node whose nd_2nd is the
533 * next insertion point. */
534 struct {
535 NODE *head;
536 NODE *tail;
537 } logop;
538 stack_type cond_stack;
539 stack_type cmdarg_stack;
540 int tokidx;
541 int toksiz;
542 int heredoc_end;
543 int heredoc_indent;
544 int heredoc_line_indent;
545 char *tokenbuf;
546 struct local_vars *lvtbl;
547 st_table *pvtbl;
548 st_table *pktbl;
549 int line_count;
550 int ruby_sourceline; /* current line no. */
551 const char *ruby_sourcefile; /* current source file */
552 VALUE ruby_sourcefile_string;
553 rb_encoding *enc;
554 token_info *token_info;
555 st_table *case_labels;
556 rb_node_exits_t *exits;
557
558 VALUE debug_buffer;
559 VALUE debug_output;
560
561 struct {
562 rb_parser_string_t *token;
563 int beg_line;
564 int beg_col;
565 int end_line;
566 int end_col;
567 } delayed;
568
569 rb_ast_t *ast;
570 int node_id;
571
572 st_table *warn_duplicate_keys_table;
573
574 int max_numparam;
575 ID it_id;
576
577 struct lex_context ctxt;
578
579 NODE *eval_tree_begin;
580 NODE *eval_tree;
581 const struct rb_iseq_struct *parent_iseq;
582
583#ifdef UNIVERSAL_PARSER
584 const rb_parser_config_t *config;
585#endif
586 /* compile_option */
587 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
588
589 unsigned int command_start:1;
590 unsigned int eofp: 1;
591 unsigned int ruby__end__seen: 1;
592 unsigned int debug: 1;
593 unsigned int has_shebang: 1;
594 unsigned int token_seen: 1;
595 unsigned int token_info_enabled: 1;
596# if WARN_PAST_SCOPE
597 unsigned int past_scope_enabled: 1;
598# endif
599 unsigned int error_p: 1;
600 unsigned int cr_seen: 1;
601
602 /* Streaming hash state of the source bytes read so far. */
603 rb_source_hash_state_t source_hash;
604
605#ifndef RIPPER
606 /* Ruby core only */
607
608 unsigned int do_print: 1;
609 unsigned int do_loop: 1;
610 unsigned int do_chomp: 1;
611 unsigned int do_split: 1;
612 unsigned int error_tolerant: 1;
613 unsigned int keep_tokens: 1;
614
615 VALUE error_buffer;
616 rb_parser_ary_t *debug_lines;
617 /*
618 * Store specific keyword locations to generate dummy end token.
619 * Refer to the tail of list element.
620 */
621 end_expect_token_locations_t *end_expect_token_locations;
622 /* id for terms */
623 int token_id;
624 /* Array for term tokens */
625 rb_parser_ary_t *tokens;
626#else
627 /* Ripper only */
628
629 VALUE value;
630 VALUE result;
631 VALUE parsing_thread;
632 VALUE s_value; /* Token VALUE */
633 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
634 VALUE s_value_stack;
635#endif
636};
637
638#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
639#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
640#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
641static int
642numparam_id_p(struct parser_params *p, ID id)
643{
644 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
645 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
646 return idx > 0 && idx <= NUMPARAM_MAX;
647}
648static void numparam_name(struct parser_params *p, ID id);
649
650#ifdef RIPPER
651static void
652after_shift(struct parser_params *p)
653{
654 if (p->debug) {
655 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
656 }
657 rb_ary_push(p->s_value_stack, p->s_value);
658 p->s_value = Qnil;
659}
660
661static void
662before_reduce(int len, struct parser_params *p)
663{
664 // Initialize $$ with $1.
665 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
666}
667
668static void
669after_reduce(int len, struct parser_params *p)
670{
671 for (int i = 0; i < len; i++) {
672 VALUE tos = rb_ary_pop(p->s_value_stack);
673 if (p->debug) {
674 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
675 }
676 }
677 if (p->debug) {
678 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
679 }
680 rb_ary_push(p->s_value_stack, p->s_lvalue);
681 p->s_lvalue = Qnil;
682}
683
684static void
685after_shift_error_token(struct parser_params *p)
686{
687 if (p->debug) {
688 rb_parser_printf(p, "after-shift-error-token:\n");
689 }
690 rb_ary_push(p->s_value_stack, Qnil);
691}
692
693static void
694after_pop_stack(int len, struct parser_params *p)
695{
696 for (int i = 0; i < len; i++) {
697 VALUE tos = rb_ary_pop(p->s_value_stack);
698 if (p->debug) {
699 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
700 }
701 }
702}
703#else
704static void
705after_shift(struct parser_params *p)
706{
707}
708
709static void
710before_reduce(int len, struct parser_params *p)
711{
712}
713
714static void
715after_reduce(int len, struct parser_params *p)
716{
717}
718
719static void
720after_shift_error_token(struct parser_params *p)
721{
722}
723
724static void
725after_pop_stack(int len, struct parser_params *p)
726{
727}
728#endif
729
730#define intern_cstr(n,l,en) rb_intern3(n,l,en)
731
732#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
733
734#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
735#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
736#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
737#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
738#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
739#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
740
741#ifndef RIPPER
742static inline int
743char_at_end(struct parser_params *p, VALUE str, int when_empty)
744{
745 rb_len_t len = RSTRING_LEN(str);
746 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
747}
748#endif
749
750static void
751pop_pvtbl(struct parser_params *p, st_table *tbl)
752{
753 st_free_table(p->pvtbl);
754 p->pvtbl = tbl;
755}
756
757static void
758pop_pktbl(struct parser_params *p, st_table *tbl)
759{
760 if (p->pktbl) st_free_table(p->pktbl);
761 p->pktbl = tbl;
762}
763
764#define STRING_BUF_DEFAULT_LEN 16
765
766static void
767string_buffer_init(struct parser_params *p)
768{
769 parser_string_buffer_t *buf = &p->lex.string_buffer;
770 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
771
772 buf->head = buf->last = xmalloc(size);
773 buf->head->len = STRING_BUF_DEFAULT_LEN;
774 buf->head->used = 0;
775 buf->head->next = NULL;
776}
777
778static void
779string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
780{
781 parser_string_buffer_t *buf = &p->lex.string_buffer;
782
783 if (buf->head->used >= buf->head->len) {
784 parser_string_buffer_elem_t *elem;
785 long n = buf->head->len * 2;
786 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
787
788 elem = xmalloc(size);
789 elem->len = n;
790 elem->used = 0;
791 elem->next = NULL;
792 buf->last->next = elem;
793 buf->last = elem;
794 }
795 buf->last->buf[buf->last->used++] = str;
796}
797
798static void
799string_buffer_free(struct parser_params *p)
800{
801 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
802
803 while (elem) {
804 parser_string_buffer_elem_t *next_elem = elem->next;
805
806 for (long i = 0; i < elem->used; i++) {
807 rb_parser_string_free(p, elem->buf[i]);
808 }
809
810 xfree(elem);
811 elem = next_elem;
812 }
813}
814
815#ifndef RIPPER
816static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
817
818static void
819debug_end_expect_token_locations(struct parser_params *p, const char *name)
820{
821 if(p->debug) {
822 VALUE mesg = rb_sprintf("%s: [", name);
823 int i = 0;
824 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
825 if (i > 0)
826 rb_str_cat_cstr(mesg, ", ");
827 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
828 i++;
829 }
830 rb_str_cat_cstr(mesg, "]\n");
831 flush_debug_buffer(p, p->debug_output, mesg);
832 }
833}
834
835static void
836push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
837{
838 if(!p->error_tolerant) return;
839
840 end_expect_token_locations_t *locations;
841 locations = ALLOC(end_expect_token_locations_t);
842 locations->pos = pos;
843 locations->prev = p->end_expect_token_locations;
844 p->end_expect_token_locations = locations;
845
846 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
847}
848
849static void
850pop_end_expect_token_locations(struct parser_params *p)
851{
852 if(!p->end_expect_token_locations) return;
853
854 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
855 ruby_xfree_sized(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
856 p->end_expect_token_locations = locations;
857
858 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
859}
860
861static end_expect_token_locations_t *
862peek_end_expect_token_locations(struct parser_params *p)
863{
864 return p->end_expect_token_locations;
865}
866
867static const char *
868parser_token2char(struct parser_params *p, enum yytokentype tok)
869{
870 switch ((int) tok) {
871#define TOKEN2CHAR(tok) case tok: return (#tok);
872#define TOKEN2CHAR2(tok, name) case tok: return (name);
873 TOKEN2CHAR2(' ', "word_sep");
874 TOKEN2CHAR2('!', "!")
875 TOKEN2CHAR2('%', "%");
876 TOKEN2CHAR2('&', "&");
877 TOKEN2CHAR2('*', "*");
878 TOKEN2CHAR2('+', "+");
879 TOKEN2CHAR2('-', "-");
880 TOKEN2CHAR2('/', "/");
881 TOKEN2CHAR2('<', "<");
882 TOKEN2CHAR2('=', "=");
883 TOKEN2CHAR2('>', ">");
884 TOKEN2CHAR2('?', "?");
885 TOKEN2CHAR2('^', "^");
886 TOKEN2CHAR2('|', "|");
887 TOKEN2CHAR2('~', "~");
888 TOKEN2CHAR2(':', ":");
889 TOKEN2CHAR2(',', ",");
890 TOKEN2CHAR2('.', ".");
891 TOKEN2CHAR2(';', ";");
892 TOKEN2CHAR2('`', "`");
893 TOKEN2CHAR2('\n', "nl");
894 TOKEN2CHAR2('{', "\"{\"");
895 TOKEN2CHAR2('}', "\"}\"");
896 TOKEN2CHAR2('[', "\"[\"");
897 TOKEN2CHAR2(']', "\"]\"");
898 TOKEN2CHAR2('(', "\"(\"");
899 TOKEN2CHAR2(')', "\")\"");
900 TOKEN2CHAR2('\\', "backslash");
901 TOKEN2CHAR(keyword_class);
902 TOKEN2CHAR(keyword_module);
903 TOKEN2CHAR(keyword_def);
904 TOKEN2CHAR(keyword_undef);
905 TOKEN2CHAR(keyword_begin);
906 TOKEN2CHAR(keyword_rescue);
907 TOKEN2CHAR(keyword_ensure);
908 TOKEN2CHAR(keyword_end);
909 TOKEN2CHAR(keyword_if);
910 TOKEN2CHAR(keyword_unless);
911 TOKEN2CHAR(keyword_then);
912 TOKEN2CHAR(keyword_elsif);
913 TOKEN2CHAR(keyword_else);
914 TOKEN2CHAR(keyword_case);
915 TOKEN2CHAR(keyword_when);
916 TOKEN2CHAR(keyword_while);
917 TOKEN2CHAR(keyword_until);
918 TOKEN2CHAR(keyword_for);
919 TOKEN2CHAR(keyword_break);
920 TOKEN2CHAR(keyword_next);
921 TOKEN2CHAR(keyword_redo);
922 TOKEN2CHAR(keyword_retry);
923 TOKEN2CHAR(keyword_in);
924 TOKEN2CHAR(keyword_do);
925 TOKEN2CHAR(keyword_do_cond);
926 TOKEN2CHAR(keyword_do_block);
927 TOKEN2CHAR(keyword_do_LAMBDA);
928 TOKEN2CHAR(keyword_return);
929 TOKEN2CHAR(keyword_yield);
930 TOKEN2CHAR(keyword_super);
931 TOKEN2CHAR(keyword_self);
932 TOKEN2CHAR(keyword_nil);
933 TOKEN2CHAR(keyword_true);
934 TOKEN2CHAR(keyword_false);
935 TOKEN2CHAR(keyword_and);
936 TOKEN2CHAR(keyword_or);
937 TOKEN2CHAR(keyword_not);
938 TOKEN2CHAR(modifier_if);
939 TOKEN2CHAR(modifier_unless);
940 TOKEN2CHAR(modifier_while);
941 TOKEN2CHAR(modifier_until);
942 TOKEN2CHAR(modifier_rescue);
943 TOKEN2CHAR(keyword_alias);
944 TOKEN2CHAR(keyword_defined);
945 TOKEN2CHAR(keyword_BEGIN);
946 TOKEN2CHAR(keyword_END);
947 TOKEN2CHAR(keyword__LINE__);
948 TOKEN2CHAR(keyword__FILE__);
949 TOKEN2CHAR(keyword__ENCODING__);
950 TOKEN2CHAR(tIDENTIFIER);
951 TOKEN2CHAR(tFID);
952 TOKEN2CHAR(tGVAR);
953 TOKEN2CHAR(tIVAR);
954 TOKEN2CHAR(tCONSTANT);
955 TOKEN2CHAR(tCVAR);
956 TOKEN2CHAR(tLABEL);
957 TOKEN2CHAR(tINTEGER);
958 TOKEN2CHAR(tFLOAT);
959 TOKEN2CHAR(tRATIONAL);
960 TOKEN2CHAR(tIMAGINARY);
961 TOKEN2CHAR(tCHAR);
962 TOKEN2CHAR(tNTH_REF);
963 TOKEN2CHAR(tBACK_REF);
964 TOKEN2CHAR(tSTRING_CONTENT);
965 TOKEN2CHAR(tREGEXP_END);
966 TOKEN2CHAR(tDUMNY_END);
967 TOKEN2CHAR(tSP);
968 TOKEN2CHAR(tUPLUS);
969 TOKEN2CHAR(tUMINUS);
970 TOKEN2CHAR(tPOW);
971 TOKEN2CHAR(tCMP);
972 TOKEN2CHAR(tEQ);
973 TOKEN2CHAR(tEQQ);
974 TOKEN2CHAR(tNEQ);
975 TOKEN2CHAR(tGEQ);
976 TOKEN2CHAR(tLEQ);
977 TOKEN2CHAR(tANDOP);
978 TOKEN2CHAR(tOROP);
979 TOKEN2CHAR(tMATCH);
980 TOKEN2CHAR(tNMATCH);
981 TOKEN2CHAR(tDOT2);
982 TOKEN2CHAR(tDOT3);
983 TOKEN2CHAR(tBDOT2);
984 TOKEN2CHAR(tBDOT3);
985 TOKEN2CHAR(tAREF);
986 TOKEN2CHAR(tASET);
987 TOKEN2CHAR(tLSHFT);
988 TOKEN2CHAR(tRSHFT);
989 TOKEN2CHAR(tANDDOT);
990 TOKEN2CHAR(tCOLON2);
991 TOKEN2CHAR(tCOLON3);
992 TOKEN2CHAR(tOP_ASGN);
993 TOKEN2CHAR(tASSOC);
994 TOKEN2CHAR(tLPAREN);
995 TOKEN2CHAR(tLPAREN_ARG);
996 TOKEN2CHAR(tLBRACK);
997 TOKEN2CHAR(tLBRACE);
998 TOKEN2CHAR(tLBRACE_ARG);
999 TOKEN2CHAR(tSTAR);
1000 TOKEN2CHAR(tDSTAR);
1001 TOKEN2CHAR(tAMPER);
1002 TOKEN2CHAR(tLAMBDA);
1003 TOKEN2CHAR(tSYMBEG);
1004 TOKEN2CHAR(tSTRING_BEG);
1005 TOKEN2CHAR(tXSTRING_BEG);
1006 TOKEN2CHAR(tREGEXP_BEG);
1007 TOKEN2CHAR(tWORDS_BEG);
1008 TOKEN2CHAR(tQWORDS_BEG);
1009 TOKEN2CHAR(tSYMBOLS_BEG);
1010 TOKEN2CHAR(tQSYMBOLS_BEG);
1011 TOKEN2CHAR(tSTRING_END);
1012 TOKEN2CHAR(tSTRING_DEND);
1013 TOKEN2CHAR(tSTRING_DBEG);
1014 TOKEN2CHAR(tSTRING_DVAR);
1015 TOKEN2CHAR(tLAMBEG);
1016 TOKEN2CHAR(tLABEL_END);
1017 TOKEN2CHAR(tIGNORED_NL);
1018 TOKEN2CHAR(tCOMMENT);
1019 TOKEN2CHAR(tEMBDOC_BEG);
1020 TOKEN2CHAR(tEMBDOC);
1021 TOKEN2CHAR(tEMBDOC_END);
1022 TOKEN2CHAR(tHEREDOC_BEG);
1023 TOKEN2CHAR(tHEREDOC_END);
1024 TOKEN2CHAR(k__END__);
1025 TOKEN2CHAR(tLOWEST);
1026 TOKEN2CHAR(tUMINUS_NUM);
1027 TOKEN2CHAR(tLAST_TOKEN);
1028#undef TOKEN2CHAR
1029#undef TOKEN2CHAR2
1030 }
1031
1032 rb_bug("parser_token2id: unknown token %d", tok);
1033
1034 UNREACHABLE_RETURN(0);
1035}
1036#else
1037static void
1038push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1039{
1040}
1041
1042static void
1043pop_end_expect_token_locations(struct parser_params *p)
1044{
1045}
1046#endif
1047
1048RBIMPL_ATTR_NONNULL((1, 2, 3))
1049static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1050RBIMPL_ATTR_NONNULL((1, 2))
1051static int parser_yyerror0(struct parser_params*, const char*);
1052#define yyerror0(msg) parser_yyerror0(p, (msg))
1053#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1054#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1055#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1056#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1057#define lex_eol_p(p) lex_eol_n_p(p, 0)
1058#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1059#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1060#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1061
1062static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1063static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1064static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1065static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1066static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1067
1068#ifdef RIPPER
1069#define compile_for_eval (0)
1070#else
1071#define compile_for_eval (p->parent_iseq != 0)
1072#endif
1073
1074#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1075
1076#define CALL_Q_P(q) ((q) == tANDDOT)
1077#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1078
1079#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1080
1081static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1082
1083static inline void
1084rb_discard_node(struct parser_params *p, NODE *n)
1085{
1086 rb_ast_delete_node(p->ast, n);
1087}
1088
1089static 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);
1090static 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);
1091static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1092static 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);
1093static 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);
1094static 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);
1095static 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);
1096static 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);
1097static 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);
1098static 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);
1099static 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);
1100static 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);
1101static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1102static 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);
1103static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1104static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1105static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1106static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1107static 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);
1108static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1109static 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);
1110static 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);
1111static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1112static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1113static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1114static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1115static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1116static 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);
1117static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1118static 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);
1119static 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);
1120static 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);
1121static 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);
1122static 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);
1123static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1124static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1125static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1126static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1127static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1128static 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);
1129static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1130static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1131static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1132static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1133static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1134static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1135static 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);
1136static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1137static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1138static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1139static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1140static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1141static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1142static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1143static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1144static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1145static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1146static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1147static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1148static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1149static 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);
1150static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1151static 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);
1152static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1153static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1154static 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);
1155static 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);
1156static 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);
1157static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1158static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1159static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1160static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1161static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1162static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1163static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1164static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1165static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1166static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1167static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1168static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1169static 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);
1170static 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);
1171static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1172static 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);
1173static 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);
1174static 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);
1175static 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);
1176static 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);
1177static 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);
1178static 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);
1179static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1180static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1181static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1182static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1183static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1184static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1185static 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);
1186static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1187static 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);
1188static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1189static 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);
1190static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1191static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1192static 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);
1193static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1194static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1195static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1196
1197#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1198#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1199#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1200#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)
1201#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)
1202#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1203#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1204#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1205#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)
1206#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)
1207#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)
1208#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)
1209#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1210#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)
1211#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1212#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1213#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1214#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1215#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1216#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1217#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1218#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1219#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1220#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1221#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1222#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1223#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1224#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1225#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1226#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)
1227#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)
1228#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1229#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1230#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1231#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1232#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1233#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1234#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1235#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1236#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)
1237#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1238#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1239#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1240#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1241#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1242#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1243#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)
1244#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1245#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1246#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1247#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1248#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1249#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1250#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1251#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1252#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1253#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1254#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1255#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1256#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1257#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1258#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1259#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1260#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1261#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1262#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1263#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1264#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)
1265#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1266#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1267#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1268#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1269#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1270#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1271#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1272#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1273#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1274#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1275#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1276#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1277#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1278#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1279#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1280#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)
1281#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1282#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)
1283#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1284#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1285#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1286#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1287#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1288#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1289#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1290#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1291#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1292#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1293#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)
1294#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1295#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1296#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1297#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)
1298#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1299#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1300#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1301#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1302#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1303#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1304#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1305
1306enum internal_node_type {
1307 NODE_INTERNAL_ONLY = NODE_LAST,
1308 NODE_DEF_TEMP,
1309 NODE_EXITS,
1310 NODE_INTERNAL_LAST
1311};
1312
1313static const char *
1314parser_node_name(int node)
1315{
1316 switch (node) {
1317 case NODE_DEF_TEMP:
1318 return "NODE_DEF_TEMP";
1319 case NODE_EXITS:
1320 return "NODE_EXITS";
1321 default:
1322 return ruby_node_name(node);
1323 }
1324}
1325
1326/* This node is parse.y internal */
1327struct RNode_DEF_TEMP {
1328 NODE node;
1329
1330 /* for NODE_DEFN/NODE_DEFS */
1331
1332 struct RNode *nd_def;
1333 ID nd_mid;
1334
1335 struct {
1336 int max_numparam;
1337 NODE *numparam_save;
1338 struct lex_context ctxt;
1339 } save;
1340};
1341
1342#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1343
1344static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1345static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1346static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1347static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1348static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1349
1350#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1351#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1352#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1353#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1354
1355/* Make a new internal node, which should not be appeared in the
1356 * result AST and does not have node_id and location. */
1357static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1358#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1359
1360static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1361
1362static int
1363parser_get_node_id(struct parser_params *p)
1364{
1365 int node_id = p->node_id;
1366 p->node_id++;
1367 return node_id;
1368}
1369
1370static void
1371anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1372{
1373 if (id == tANDDOT) {
1374 yyerror1(loc, "&. inside multiple assignment destination");
1375 }
1376}
1377
1378static inline void
1379set_line_body(NODE *body, int line)
1380{
1381 if (!body) return;
1382 switch (nd_type(body)) {
1383 case NODE_RESCUE:
1384 case NODE_ENSURE:
1385 nd_set_line(body, line);
1386 }
1387}
1388
1389static void
1390set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1391{
1392 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1393 nd_set_line(node, beg->end_pos.lineno);
1394}
1395
1396static NODE *
1397last_expr_node(NODE *expr)
1398{
1399 while (expr) {
1400 if (nd_type_p(expr, NODE_BLOCK)) {
1401 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1402 }
1403 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1404 expr = RNODE_BEGIN(expr)->nd_body;
1405 }
1406 else {
1407 break;
1408 }
1409 }
1410 return expr;
1411}
1412
1413#ifndef RIPPER
1414#define yyparse ruby_yyparse
1415#endif
1416
1417static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1418static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1419static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1420static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1421static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1422static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1423
1424static NODE *newline_node(NODE*);
1425static void fixpos(NODE*,NODE*);
1426
1427static int value_expr(struct parser_params*,NODE*);
1428static void void_expr(struct parser_params*,NODE*);
1429static NODE *remove_begin(NODE*);
1430static NODE *void_stmts(struct parser_params*,NODE*);
1431static void reduce_nodes(struct parser_params*,NODE**);
1432static void block_dup_check(struct parser_params*,NODE*,NODE*);
1433
1434static NODE *block_append(struct parser_params*,NODE*,NODE*);
1435static NODE *list_append(struct parser_params*,NODE*,NODE*);
1436static NODE *list_concat(NODE*,NODE*);
1437static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1438static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1439static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1440static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1441static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1442static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1443static NODE *str2dstr(struct parser_params*,NODE*);
1444static NODE *evstr2dstr(struct parser_params*,NODE*);
1445static NODE *splat_array(NODE*);
1446static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1447
1448static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1449static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1450static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1451static 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);
1452static 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;}
1453static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1454
1455static bool args_info_empty_p(struct rb_args_info *args);
1456static 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*);
1457static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1458#define new_empty_args_tail(p, loc) new_args_tail(p, 0, 0, 0, loc)
1459static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1460static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1461static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1462static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1463static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1464static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1465
1466static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1467static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1468
1469static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1470static void no_blockarg(struct parser_params*,NODE*);
1471static NODE *ret_args(struct parser_params*,NODE*);
1472static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1473static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1474
1475static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1476static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1477
1478static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1479static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1480
1481static VALUE rb_backref_error(struct parser_params*,NODE*);
1482static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1483
1484static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1485static 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);
1486static 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);
1487static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1488static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1489
1490static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1491
1492static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1493static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1494
1495static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1496static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1497
1498static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1499
1500static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1501
1502#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1503
1504static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1505
1506static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1507
1508static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1509
1510static rb_ast_id_table_t *local_tbl(struct parser_params*);
1511
1512static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1513static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1514
1515static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1516static NODE *heredoc_dedent(struct parser_params*,NODE*);
1517
1518static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1519
1520static 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);
1521
1522#ifdef RIPPER
1523#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1524#define set_value(val) (p->s_lvalue = val)
1525static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1526static int id_is_var(struct parser_params *p, ID id);
1527#endif
1528
1529RUBY_SYMBOL_EXPORT_BEGIN
1530VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1531int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1532enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1533VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1534void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1535PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1536YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1537YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1538YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1539YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1540YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1541YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1542void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1543RUBY_SYMBOL_EXPORT_END
1544
1545static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1546static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1547static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1548static VALUE formal_argument_error(struct parser_params*, ID);
1549static ID shadowing_lvar(struct parser_params*,ID);
1550static void new_bv(struct parser_params*,ID);
1551
1552static void local_push(struct parser_params*,int);
1553static void local_pop(struct parser_params*);
1554static void local_var(struct parser_params*, ID);
1555static void arg_var(struct parser_params*, ID);
1556static int local_id(struct parser_params *p, ID id);
1557static int local_id_ref(struct parser_params*, ID, ID **);
1558#define internal_id rb_parser_internal_id
1559ID internal_id(struct parser_params*);
1560static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1561static int check_forwarding_args(struct parser_params*);
1562static void add_forwarding_args(struct parser_params *p);
1563static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1564
1565static const struct vtable *dyna_push(struct parser_params *);
1566static void dyna_pop(struct parser_params*, const struct vtable *);
1567static int dyna_in_block(struct parser_params*);
1568#define dyna_var(p, id) local_var(p, id)
1569static int dvar_defined(struct parser_params*, ID);
1570#define dvar_defined_ref rb_parser_dvar_defined_ref
1571int dvar_defined_ref(struct parser_params*, ID, ID**);
1572static int dvar_curr(struct parser_params*,ID);
1573
1574static int lvar_defined(struct parser_params*, ID);
1575
1576static NODE *numparam_push(struct parser_params *p);
1577static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1578
1579#define METHOD_NOT '!'
1580
1581#define idFWD_REST '*'
1582#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1583#define idFWD_BLOCK '&'
1584#define idFWD_ALL idDot3
1585#define arg_FWD_BLOCK idFWD_BLOCK
1586
1587#define RE_ONIG_OPTION_IGNORECASE 1
1588#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1589#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1590#define RE_OPTION_ONCE (1<<16)
1591#define RE_OPTION_ENCODING_SHIFT 8
1592#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1593#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1594#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1595#define RE_OPTION_MASK 0xff
1596#define RE_OPTION_ARG_ENCODING_NONE 32
1597
1598#define CHECK_LITERAL_WHEN (st_table *)1
1599#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1600
1601#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1602RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1603
1604#define TOKEN2ID(tok) ( \
1605 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1606 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1607 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1608 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1609 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1610 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1611 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1612
1613/****** Ripper *******/
1614
1615#ifdef RIPPER
1616
1617#include "eventids1.h"
1618#include "eventids2.h"
1619
1620extern const struct ripper_parser_ids ripper_parser_ids;
1621
1622static VALUE ripper_dispatch0(struct parser_params*,ID);
1623static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1624static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1625static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1626static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1627static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1628static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1629void ripper_error(struct parser_params *p);
1630
1631#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1632#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1633#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1634#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1635#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1636#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1637#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1638
1639#define yyparse ripper_yyparse
1640
1641static VALUE
1642aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1643{
1644 if (!NIL_P(pre_arg)) {
1645 if (!NIL_P(pre_args)) {
1646 rb_ary_unshift(pre_args, pre_arg);
1647 }
1648 else {
1649 pre_args = rb_ary_new_from_args(1, pre_arg);
1650 }
1651 }
1652 return pre_args;
1653}
1654
1655#define ID2VAL(id) STATIC_ID2SYM(id)
1656#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1657#endif /* RIPPER */
1658
1659#define KWD2EID(t, v) keyword_##t
1660
1661static NODE *
1662new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1663{
1664 body = remove_begin(body);
1665 reduce_nodes(p, &body);
1666 NODE *n = NEW_SCOPE(args, body, parent, loc);
1667 nd_set_line(n, loc->end_pos.lineno);
1668 set_line_body(body, loc->beg_pos.lineno);
1669 return n;
1670}
1671
1672static NODE *
1673rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1674 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1675{
1676 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1677 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1678 loc.beg_pos = arg_loc->beg_pos;
1679 return NEW_RESCUE(arg, rescue, 0, &loc);
1680}
1681
1682static NODE *add_block_exit(struct parser_params *p, NODE *node);
1683static rb_node_exits_t *init_block_exit(struct parser_params *p);
1684static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1685static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1686static void clear_block_exit(struct parser_params *p, bool error);
1687
1688static void
1689next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1690{
1691 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1692}
1693
1694static void
1695restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1696{
1697 /* See: def_name action */
1698 struct lex_context ctxt = temp->save.ctxt;
1699 p->ctxt.in_def = ctxt.in_def;
1700 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1701 p->ctxt.in_rescue = ctxt.in_rescue;
1702 p->max_numparam = temp->save.max_numparam;
1703 numparam_pop(p, temp->save.numparam_save);
1704 clear_block_exit(p, true);
1705}
1706
1707static void
1708endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1709{
1710 if (is_attrset_id(mid)) {
1711 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1712 }
1713 token_info_drop(p, "def", loc->beg_pos);
1714}
1715
1716#define debug_token_line(p, name, line) do { \
1717 if (p->debug) { \
1718 const char *const pcur = p->lex.pcur; \
1719 const char *const ptok = p->lex.ptok; \
1720 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1721 line, p->ruby_sourceline, \
1722 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1723 } \
1724 } while (0)
1725
1726#define begin_definition(k, loc_beg, loc_end) \
1727 do { \
1728 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1729 /* singleton class */ \
1730 p->ctxt.cant_return = !p->ctxt.in_def; \
1731 p->ctxt.in_def = 0; \
1732 } \
1733 else if (p->ctxt.in_def) { \
1734 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1735 yyerror1(&loc, k " definition in method body"); \
1736 } \
1737 else { \
1738 p->ctxt.cant_return = 1; \
1739 } \
1740 local_push(p, 0); \
1741 } while (0)
1742
1743#ifndef RIPPER
1744# define ifndef_ripper(x) (x)
1745# define ifdef_ripper(r,x) (x)
1746#else
1747# define ifndef_ripper(x)
1748# define ifdef_ripper(r,x) (r)
1749#endif
1750
1751# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1752# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1753# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1754# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1755# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1756# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1757# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1758# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1759# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1760# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1761# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1762# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1763# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1764# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1765# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1766# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1767# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1768# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1769# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1770# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1771#ifdef RIPPER
1772extern const ID id_warn, id_warning, id_gets, id_assoc;
1773# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1774# define WARN_S_L(s,l) STR_NEW(s,l)
1775# define WARN_S(s) STR_NEW2(s)
1776# define WARN_I(i) INT2NUM(i)
1777# define WARN_ID(i) rb_id2str(i)
1778# define PRIsWARN PRIsVALUE
1779# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1780# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1781# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1782# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1783# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1784# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1785# define compile_error ripper_compile_error
1786#else
1787# define WARN_S_L(s,l) s
1788# define WARN_S(s) s
1789# define WARN_I(i) i
1790# define WARN_ID(i) rb_id2name(i)
1791# define PRIsWARN PRIsVALUE
1792# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1793# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1794# define WARN_CALL rb_compile_warn
1795# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1796# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1797# define WARNING_CALL rb_compile_warning
1798PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1799# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1800#endif
1801
1802#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1803
1804static NODE *
1805add_block_exit(struct parser_params *p, NODE *node)
1806{
1807 if (!node) {
1808 compile_error(p, "unexpected null node");
1809 return 0;
1810 }
1811 switch (nd_type(node)) {
1812 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1813 default:
1814 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1815 return node;
1816 }
1817 if (!p->ctxt.in_defined) {
1818 rb_node_exits_t *exits = p->exits;
1819 if (exits) {
1820 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1821 exits->nd_stts = node;
1822 }
1823 }
1824 return node;
1825}
1826
1827static rb_node_exits_t *
1828init_block_exit(struct parser_params *p)
1829{
1830 rb_node_exits_t *old = p->exits;
1831 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1832 exits->nd_chain = 0;
1833 exits->nd_stts = RNODE(exits);
1834 p->exits = exits;
1835 return old;
1836}
1837
1838static rb_node_exits_t *
1839allow_block_exit(struct parser_params *p)
1840{
1841 rb_node_exits_t *exits = p->exits;
1842 p->exits = 0;
1843 return exits;
1844}
1845
1846static void
1847restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1848{
1849 p->exits = exits;
1850}
1851
1852static void
1853clear_block_exit(struct parser_params *p, bool error)
1854{
1855 rb_node_exits_t *exits = p->exits;
1856 if (!exits) return;
1857 if (error) {
1858 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1859 switch (nd_type(e)) {
1860 case NODE_BREAK:
1861 yyerror1(&e->nd_loc, "Invalid break");
1862 break;
1863 case NODE_NEXT:
1864 yyerror1(&e->nd_loc, "Invalid next");
1865 break;
1866 case NODE_REDO:
1867 yyerror1(&e->nd_loc, "Invalid redo");
1868 break;
1869 default:
1870 yyerror1(&e->nd_loc, "unexpected node");
1871 goto end_checks; /* no nd_chain */
1872 }
1873 }
1874 end_checks:;
1875 }
1876 exits->nd_stts = RNODE(exits);
1877 exits->nd_chain = 0;
1878}
1879
1880#define WARN_EOL(tok) \
1881 (looking_at_eol_p(p) ? \
1882 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1883 (void)0)
1884static int looking_at_eol_p(struct parser_params *p);
1885
1886static NODE *
1887get_nd_value(struct parser_params *p, NODE *node)
1888{
1889 switch (nd_type(node)) {
1890 case NODE_GASGN:
1891 return RNODE_GASGN(node)->nd_value;
1892 case NODE_IASGN:
1893 return RNODE_IASGN(node)->nd_value;
1894 case NODE_LASGN:
1895 return RNODE_LASGN(node)->nd_value;
1896 case NODE_DASGN:
1897 return RNODE_DASGN(node)->nd_value;
1898 case NODE_MASGN:
1899 return RNODE_MASGN(node)->nd_value;
1900 case NODE_CVASGN:
1901 return RNODE_CVASGN(node)->nd_value;
1902 case NODE_CDECL:
1903 return RNODE_CDECL(node)->nd_value;
1904 default:
1905 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1906 return 0;
1907 }
1908}
1909
1910static void
1911set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1912{
1913 switch (nd_type(node)) {
1914 case NODE_CDECL:
1915 RNODE_CDECL(node)->nd_value = rhs;
1916 break;
1917 case NODE_GASGN:
1918 RNODE_GASGN(node)->nd_value = rhs;
1919 break;
1920 case NODE_IASGN:
1921 RNODE_IASGN(node)->nd_value = rhs;
1922 break;
1923 case NODE_LASGN:
1924 RNODE_LASGN(node)->nd_value = rhs;
1925 break;
1926 case NODE_DASGN:
1927 RNODE_DASGN(node)->nd_value = rhs;
1928 break;
1929 case NODE_MASGN:
1930 RNODE_MASGN(node)->nd_value = rhs;
1931 break;
1932 case NODE_CVASGN:
1933 RNODE_CVASGN(node)->nd_value = rhs;
1934 break;
1935 default:
1936 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1937 break;
1938 }
1939}
1940
1941static ID
1942get_nd_vid(struct parser_params *p, NODE *node)
1943{
1944 switch (nd_type(node)) {
1945 case NODE_CDECL:
1946 return RNODE_CDECL(node)->nd_vid;
1947 case NODE_GASGN:
1948 return RNODE_GASGN(node)->nd_vid;
1949 case NODE_IASGN:
1950 return RNODE_IASGN(node)->nd_vid;
1951 case NODE_LASGN:
1952 return RNODE_LASGN(node)->nd_vid;
1953 case NODE_DASGN:
1954 return RNODE_DASGN(node)->nd_vid;
1955 case NODE_CVASGN:
1956 return RNODE_CVASGN(node)->nd_vid;
1957 default:
1958 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1959 return 0;
1960 }
1961}
1962
1963static NODE *
1964get_nd_args(struct parser_params *p, NODE *node)
1965{
1966 switch (nd_type(node)) {
1967 case NODE_CALL:
1968 return RNODE_CALL(node)->nd_args;
1969 case NODE_OPCALL:
1970 return RNODE_OPCALL(node)->nd_args;
1971 case NODE_FCALL:
1972 return RNODE_FCALL(node)->nd_args;
1973 case NODE_QCALL:
1974 return RNODE_QCALL(node)->nd_args;
1975 case NODE_SUPER:
1976 return RNODE_SUPER(node)->nd_args;
1977 case NODE_VCALL:
1978 case NODE_ZSUPER:
1979 case NODE_YIELD:
1980 case NODE_RETURN:
1981 case NODE_BREAK:
1982 case NODE_NEXT:
1983 return 0;
1984 default:
1985 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1986 return 0;
1987 }
1988}
1989
1990static st_index_t
1991djb2(const uint8_t *str, size_t len)
1992{
1993 st_index_t hash = 5381;
1994
1995 for (size_t i = 0; i < len; i++) {
1996 hash = ((hash << 5) + hash) + str[i];
1997 }
1998
1999 return hash;
2000}
2001
2002static st_index_t
2003parser_memhash(const void *ptr, rb_len_t len)
2004{
2005 return djb2(ptr, len);
2006}
2007
2008#define PARSER_STRING_PTR(str) (str->ptr)
2009#define PARSER_STRING_LEN(str) (str->len)
2010#define PARSER_STRING_END(str) (&str->ptr[str->len])
2011#define STRING_SIZE(str) ((size_t)str->len + 1)
2012#define STRING_TERM_LEN(str) (1)
2013#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
2014#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
2015 REALLOC_N(str->ptr, char, (size_t)total + termlen); \
2016 str->len = total; \
2017} while (0)
2018#define STRING_SET_LEN(str, n) do { \
2019 (str)->len = (n); \
2020} while (0)
2021#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2022 ((ptrvar) = str->ptr, \
2023 (lenvar) = str->len)
2024
2025static inline int
2026parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2027{
2028 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2029}
2030
2031static rb_parser_string_t *
2032rb_parser_string_new(rb_parser_t *p, const char *ptr, rb_len_t len)
2033{
2034 rb_parser_string_t *str;
2035
2036 if (len < 0) {
2037 rb_bug("negative string size (or size too big): %"PRIdLEN, len);
2038 }
2039
2040 str = xcalloc(1, sizeof(rb_parser_string_t));
2041 str->ptr = xcalloc(len + 1, sizeof(char));
2042
2043 if (ptr) {
2044 memcpy(PARSER_STRING_PTR(str), ptr, len);
2045 }
2046 STRING_SET_LEN(str, len);
2047 STRING_TERM_FILL(str);
2048 return str;
2049}
2050
2051static rb_parser_string_t *
2052rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, rb_len_t len, rb_encoding *enc)
2053{
2054 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2055 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2056 str->enc = enc;
2057 return str;
2058}
2059
2060#ifndef RIPPER
2061rb_parser_string_t *
2062rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2063{
2064 /* Type check */
2065 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2066 RB_GC_GUARD(str);
2067 return ret;
2068}
2069
2070void
2071rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2072{
2073 if (!str) return;
2074 xfree(PARSER_STRING_PTR(str));
2075 xfree(str);
2076}
2077#endif
2078
2079static st_index_t
2080rb_parser_str_hash(rb_parser_string_t *str)
2081{
2082 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2083}
2084
2085static st_index_t
2086rb_char_p_hash(const char *c)
2087{
2088 return parser_memhash((const void *)c, strlen(c));
2089}
2090
2091static size_t
2092rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2093{
2094 return PARSER_STRING_LEN(str);
2095}
2096
2097#ifndef RIPPER
2098static char *
2099rb_parser_string_end(rb_parser_string_t *str)
2100{
2101 return &str->ptr[str->len];
2102}
2103#endif
2104
2105static void
2106rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2107{
2108 str->enc = enc;
2109}
2110
2111static rb_encoding *
2112rb_parser_str_get_encoding(rb_parser_string_t *str)
2113{
2114 return str->enc;
2115}
2116
2117#ifndef RIPPER
2118static bool
2119PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2120{
2121 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2122}
2123#endif
2124
2125static int
2126PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2127{
2128 return str->coderange;
2129}
2130
2131static void
2132PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2133{
2134 str->coderange = coderange;
2135}
2136
2137static void
2138PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2139{
2140 rb_parser_string_set_encoding(str, enc);
2141 PARSER_ENC_CODERANGE_SET(str, cr);
2142}
2143
2144static void
2145PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2146{
2147 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2148}
2149
2150static bool
2151PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2152{
2153 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2154}
2155
2156static bool
2157PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2158{
2159 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2160}
2161
2162static const char *
2163rb_parser_search_nonascii(const char *p, const char *e)
2164{
2165 const char *s = p;
2166
2167 for (; s < e; s++) {
2168 if (*s & 0x80) return s;
2169 }
2170
2171 return NULL;
2172}
2173
2174static int
2175rb_parser_coderange_scan(struct parser_params *p, const char *ptr, rb_len_t len, rb_encoding *enc)
2176{
2177 const char *e = ptr + len;
2178
2179 if (enc == rb_ascii8bit_encoding()) {
2180 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2181 ptr = rb_parser_search_nonascii(ptr, e);
2182 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2183 }
2184
2185 /* parser string encoding is always asciicompat */
2186 ptr = rb_parser_search_nonascii(ptr, e);
2187 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2188 for (;;) {
2189 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2190 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2191 ptr += MBCLEN_CHARFOUND_LEN(ret);
2192 if (ptr == e) break;
2193 ptr = rb_parser_search_nonascii(ptr, e);
2194 if (!ptr) break;
2195 }
2196
2197 return RB_PARSER_ENC_CODERANGE_VALID;
2198}
2199
2200static int
2201rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2202{
2203 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2204}
2205
2206static int
2207rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2208{
2209 int cr = PARSER_ENC_CODERANGE(str);
2210
2211 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2212 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2213 PARSER_ENC_CODERANGE_SET(str, cr);
2214 }
2215
2216 return cr;
2217}
2218
2219static rb_parser_string_t *
2220rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2221{
2222 if (rb_parser_str_get_encoding(str) == enc)
2223 return str;
2224 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2225 PARSER_ENC_CODERANGE_CLEAR(str);
2226 }
2227 rb_parser_string_set_encoding(str, enc);
2228 return str;
2229}
2230
2231static bool
2232rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2233{
2234 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2235}
2236
2237static rb_encoding *
2238rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2239{
2240 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2241 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2242
2243 if (enc1 == NULL || enc2 == NULL)
2244 return 0;
2245
2246 if (enc1 == enc2) {
2247 return enc1;
2248 }
2249
2250 if (PARSER_STRING_LEN(str2) == 0)
2251 return enc1;
2252 if (PARSER_STRING_LEN(str1) == 0)
2253 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2254
2255 int cr1, cr2;
2256
2257 cr1 = rb_parser_enc_str_coderange(p, str1);
2258 cr2 = rb_parser_enc_str_coderange(p, str2);
2259
2260 if (cr1 != cr2) {
2261 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2262 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2263 }
2264
2265 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2266 return enc1;
2267 }
2268
2269 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2270 return enc2;
2271 }
2272
2273 return 0;
2274}
2275
2276static void
2277rb_parser_str_modify(rb_parser_string_t *str)
2278{
2279 PARSER_ENC_CODERANGE_CLEAR(str);
2280}
2281
2282static void
2283rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, rb_len_t len)
2284{
2285 rb_len_t capa;
2286 const int termlen = STRING_TERM_LEN(str);
2287
2288 if (len > (capa = (rb_len_t)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2289 rb_bug("probable buffer overflow: %"PRIdLEN" for %"PRIdLEN, len, capa);
2290 }
2291
2292 int cr = PARSER_ENC_CODERANGE(str);
2293 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2294 /* Leave unknown. */
2295 }
2296 else if (len > PARSER_STRING_LEN(str)) {
2297 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2298 }
2299 else if (len < PARSER_STRING_LEN(str)) {
2300 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2301 /* ASCII-only string is keeping after truncated. Valid
2302 * and broken may be invalid or valid, leave unknown. */
2303 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2304 }
2305 }
2306
2307 STRING_SET_LEN(str, len);
2308 STRING_TERM_FILL(str);
2309}
2310
2311static rb_parser_string_t *
2312rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len)
2313{
2314 rb_parser_str_modify(str);
2315 if (len == 0) return 0;
2316
2317 rb_len_t total, olen, off = -1;
2318 char *sptr;
2319 const int termlen = STRING_TERM_LEN(str);
2320
2321 PARSER_STRING_GETMEM(str, sptr, olen);
2322 if (ptr >= sptr && ptr <= sptr + olen) {
2323 off = ptr - sptr;
2324 }
2325
2326 if (olen > RB_LEN_MAX - len) {
2327 compile_error(p, "string sizes too big");
2328 return 0;
2329 }
2330 total = olen + len;
2331 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2332 sptr = PARSER_STRING_PTR(str);
2333 if (off != -1) {
2334 ptr = sptr + off;
2335 }
2336 memcpy(sptr + olen, ptr, len);
2337 STRING_SET_LEN(str, total);
2338 STRING_TERM_FILL(str);
2339
2340 return str;
2341}
2342
2343#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2344#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2345
2346static rb_parser_string_t *
2347rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len,
2348 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2349{
2350 int str_cr, res_cr;
2351 rb_encoding *str_enc, *res_enc;
2352
2353 str_enc = rb_parser_str_get_encoding(str);
2354 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2355
2356 if (str_enc == ptr_enc) {
2357 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2358 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2359 }
2360 }
2361 else {
2362 /* parser string encoding is always asciicompat */
2363 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2364 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2365 }
2366 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2367 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2368 str_cr = rb_parser_enc_str_coderange(p, str);
2369 }
2370 }
2371 }
2372 if (ptr_cr_ret)
2373 *ptr_cr_ret = ptr_cr;
2374
2375 if (str_enc != ptr_enc &&
2376 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2377 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2378 goto incompatible;
2379 }
2380
2381 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2382 res_enc = str_enc;
2383 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2384 }
2385 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2386 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2387 res_enc = str_enc;
2388 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2389 }
2390 else {
2391 res_enc = ptr_enc;
2392 res_cr = ptr_cr;
2393 }
2394 }
2395 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2396 res_enc = str_enc;
2397 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2398 res_cr = str_cr;
2399 else
2400 res_cr = ptr_cr;
2401 }
2402 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2403 res_enc = str_enc;
2404 res_cr = str_cr;
2405 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2406 }
2407
2408 if (len < 0) {
2409 compile_error(p, "negative string size (or size too big)");
2410 }
2411 parser_str_cat(str, ptr, len);
2412 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2413 return str;
2414
2415 incompatible:
2416 compile_error(p, "incompatible character encodings: %s and %s",
2417 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2418 UNREACHABLE_RETURN(0);
2419
2420}
2421
2422static rb_parser_string_t *
2423rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len,
2424 rb_encoding *ptr_enc)
2425{
2426 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2427}
2428
2429static rb_parser_string_t *
2430rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2431{
2432 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2433
2434 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2435 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2436
2437 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2438
2439 return str;
2440}
2441
2442static rb_parser_string_t *
2443rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, rb_len_t len)
2444{
2445 if (len < 0) {
2446 rb_bug("negative string size (or size too big)");
2447 }
2448
2449 rb_len_t slen = PARSER_STRING_LEN(str);
2450
2451 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2452 PARSER_ENC_CODERANGE_CLEAR(str);
2453 }
2454
2455 {
2456 rb_len_t capa;
2457 const int termlen = STRING_TERM_LEN(str);
2458
2459 if ((capa = slen) < len) {
2460 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2461 }
2462 else if (len == slen) return str;
2463 STRING_SET_LEN(str, len);
2464 STRING_TERM_FILL(str);
2465 }
2466 return str;
2467}
2468
2469# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2470 ((ptrvar) = str->ptr, \
2471 (lenvar) = str->len, \
2472 (encvar) = str->enc)
2473
2474static int
2475rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2476{
2477 rb_len_t len1, len2;
2478 const char *ptr1, *ptr2;
2479 rb_encoding *enc1, *enc2;
2480
2481 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2482 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2483
2484 return (len1 != len2 ||
2485 enc1 != enc2 ||
2486 memcmp(ptr1, ptr2, len1) != 0);
2487}
2488
2489static void
2490rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2491{
2492 long i;
2493 if (ary->capa < len) {
2494 ary->capa = len;
2495 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2496 for (i = ary->len; i < len; i++) {
2497 ary->data[i] = 0;
2498 }
2499 }
2500}
2501
2502/*
2503 * Do not call this directly.
2504 * Use rb_parser_ary_new_capa_for_XXX() instead.
2505 */
2506static rb_parser_ary_t *
2507parser_ary_new_capa(rb_parser_t *p, long len)
2508{
2509 if (len < 0) {
2510 rb_bug("negative array size (or size too big): %ld", len);
2511 }
2512 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2513 ary->data_type = 0;
2514 ary->len = 0;
2515 ary->capa = len;
2516 if (0 < len) {
2517 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2518 }
2519 else {
2520 ary->data = NULL;
2521 }
2522 return ary;
2523}
2524
2525#ifndef RIPPER
2526static rb_parser_ary_t *
2527rb_parser_ary_new_capa_for_script_line(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_SCRIPT_LINE;
2531 return ary;
2532}
2533
2534static rb_parser_ary_t *
2535rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2536{
2537 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2538 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2539 return ary;
2540}
2541#endif
2542
2543static rb_parser_ary_t *
2544rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2545{
2546 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2547 ary->data_type = PARSER_ARY_DATA_NODE;
2548 return ary;
2549}
2550
2551/*
2552 * Do not call this directly.
2553 * Use rb_parser_ary_push_XXX() instead.
2554 */
2555static rb_parser_ary_t *
2556parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2557{
2558 if (ary->len == ary->capa) {
2559 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2560 }
2561 ary->data[ary->len++] = val;
2562 return ary;
2563}
2564
2565#ifndef RIPPER
2566static rb_parser_ary_t *
2567rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2568{
2569 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2570 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2571 }
2572 return parser_ary_push(p, ary, val);
2573}
2574
2575static rb_parser_ary_t *
2576rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2577{
2578 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2579 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2580 }
2581 return parser_ary_push(p, ary, val);
2582}
2583#endif
2584
2585static rb_parser_ary_t *
2586rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2587{
2588 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2589 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2590 }
2591 return parser_ary_push(p, ary, val);
2592}
2593
2594#ifndef RIPPER
2595static void
2596rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2597{
2598 if (!token) return;
2599 rb_parser_string_free(p, token->str);
2600 xfree(token);
2601}
2602
2603static void
2604rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2605{
2606# define foreach_ary(ptr) \
2607 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2608 ptr < end_ary_data; ptr++)
2609 switch (ary->data_type) {
2610 case PARSER_ARY_DATA_AST_TOKEN:
2611 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2612 break;
2613 case PARSER_ARY_DATA_SCRIPT_LINE:
2614 foreach_ary(data) {rb_parser_string_free(p, *data);}
2615 break;
2616 case PARSER_ARY_DATA_NODE:
2617 /* Do nothing because nodes are freed when rb_ast_t is freed */
2618 break;
2619 default:
2620 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2621 break;
2622 }
2623# undef foreach_ary
2624 xfree(ary->data);
2625 xfree(ary);
2626}
2627
2628#endif /* !RIPPER */
2629%}
2630
2631%expect 0
2632%define api.pure
2633%define parse.error verbose
2634%printer {
2635 if ((NODE *)$$ == (NODE *)-1) {
2636 rb_parser_printf(p, "NODE_SPECIAL");
2637 }
2638 else if ($$) {
2639 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2640 }
2641} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2642 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2643%printer {
2644 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2645} <id>
2646%printer {
2647 switch (nd_type(RNODE($$))) {
2648 case NODE_INTEGER:
2649 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2650 break;
2651 case NODE_FLOAT:
2652 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2653 break;
2654 case NODE_RATIONAL:
2655 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2656 break;
2657 case NODE_IMAGINARY:
2658 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2659 break;
2660 default:
2661 break;
2662 }
2663} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2664%printer {
2665 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2666} tNTH_REF
2667%printer {
2668 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2669} tBACK_REF
2670
2671%destructor {
2672 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2673} <labels>
2674
2675%lex-param {struct parser_params *p}
2676%parse-param {struct parser_params *p}
2677%initial-action
2678{
2679 RUBY_SET_YYLLOC_OF_NONE(@$);
2680};
2681%after-shift after_shift
2682%before-reduce before_reduce
2683%after-reduce after_reduce
2684%after-shift-error-token after_shift_error_token
2685%after-pop-stack after_pop_stack
2686
2687%union {
2688 NODE *node;
2689 rb_node_fcall_t *node_fcall;
2690 rb_node_args_t *node_args;
2691 rb_node_args_aux_t *node_args_aux;
2692 rb_node_opt_arg_t *node_opt_arg;
2693 rb_node_kw_arg_t *node_kw_arg;
2694 rb_node_block_pass_t *node_block_pass;
2695 rb_node_masgn_t *node_masgn;
2696 rb_node_def_temp_t *node_def_temp;
2697 rb_node_exits_t *node_exits;
2698 struct rb_locations_lambda_body_t *locations_lambda_body;
2699 ID id;
2700 int num;
2701 st_table *tbl;
2702 st_table *labels;
2703 const struct vtable *vars;
2704 struct rb_strterm_struct *strterm;
2705 struct lex_context ctxt;
2706 enum lex_state_e state;
2707}
2708
2709%token <id>
2710 keyword_class "'class'"
2711 keyword_module "'module'"
2712 keyword_def "'def'"
2713 keyword_undef "'undef'"
2714 keyword_begin "'begin'"
2715 keyword_rescue "'rescue'"
2716 keyword_ensure "'ensure'"
2717 keyword_end "'end'"
2718 keyword_if "'if'"
2719 keyword_unless "'unless'"
2720 keyword_then "'then'"
2721 keyword_elsif "'elsif'"
2722 keyword_else "'else'"
2723 keyword_case "'case'"
2724 keyword_when "'when'"
2725 keyword_while "'while'"
2726 keyword_until "'until'"
2727 keyword_for "'for'"
2728 keyword_break "'break'"
2729 keyword_next "'next'"
2730 keyword_redo "'redo'"
2731 keyword_retry "'retry'"
2732 keyword_in "'in'"
2733 keyword_do "'do'"
2734 keyword_do_cond "'do' for condition"
2735 keyword_do_block "'do' for block"
2736 keyword_do_LAMBDA "'do' for lambda"
2737 keyword_return "'return'"
2738 keyword_yield "'yield'"
2739 keyword_super "'super'"
2740 keyword_self "'self'"
2741 keyword_nil "'nil'"
2742 keyword_true "'true'"
2743 keyword_false "'false'"
2744 keyword_and "'and'"
2745 keyword_or "'or'"
2746 keyword_not "'not'"
2747 modifier_if "'if' modifier"
2748 modifier_unless "'unless' modifier"
2749 modifier_while "'while' modifier"
2750 modifier_until "'until' modifier"
2751 modifier_rescue "'rescue' modifier"
2752 keyword_alias "'alias'"
2753 keyword_defined "'defined?'"
2754 keyword_BEGIN "'BEGIN'"
2755 keyword_END "'END'"
2756 keyword__LINE__ "'__LINE__'"
2757 keyword__FILE__ "'__FILE__'"
2758 keyword__ENCODING__ "'__ENCODING__'"
2759
2760%token <id> tIDENTIFIER "local variable or method"
2761%token <id> tFID "method"
2762%token <id> tGVAR "global variable"
2763%token <id> tIVAR "instance variable"
2764%token <id> tCONSTANT "constant"
2765%token <id> tCVAR "class variable"
2766%token <id> tLABEL "label"
2767%token <node> tINTEGER "integer literal"
2768%token <node> tFLOAT "float literal"
2769%token <node> tRATIONAL "rational literal"
2770%token <node> tIMAGINARY "imaginary literal"
2771%token <node> tCHAR "char literal"
2772%token <node> tNTH_REF "numbered reference"
2773%token <node> tBACK_REF "back reference"
2774%token <node> tSTRING_CONTENT "literal content"
2775%token <num> tREGEXP_END
2776%token <num> tDUMNY_END "dummy end"
2777
2778%type <node> singleton singleton_expr strings string string1 xstring regexp
2779%type <node> string_contents xstring_contents regexp_contents string_content
2780%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2781%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2782%type <node_def_temp> defn_head defs_head k_def
2783%type <node_exits> block_open k_while k_until k_for allow_exits
2784%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2785%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2786%type <node> command command_call command_call_value method_call
2787%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2788%type <node_fcall> fcall
2789%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2790%type <node> args arg_splat call_args opt_call_args
2791%type <node> paren_args opt_paren_args
2792%type <node_args> args_tail block_args_tail
2793%type <node> command_args aref_args
2794%type <node_block_pass> opt_block_arg block_arg
2795%type <node> var_ref var_lhs
2796%type <node> command_rhs arg_rhs
2797%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2798%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args f_empty_arg
2799%type <node_args_aux> f_arg f_arg_item
2800%type <node> f_marg f_rest_marg
2801%type <node_masgn> f_margs
2802%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2803%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2804%type <id> do bv_decls opt_bv_decl bvar
2805%type <node> lambda brace_body do_body
2806%type <locations_lambda_body> lambda_body
2807%type <node_args> f_larglist f_largs largs_tail
2808%type <node> brace_block cmd_brace_block do_block lhs none fitem
2809%type <node> mlhs_head mlhs_item mlhs_node
2810%type <node_masgn> mlhs mlhs_basic mlhs_inner
2811%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2812%type <node> p_expr p_as p_alt p_expr_basic p_find
2813%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2814%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2815%type <node> p_kwargs p_kwarg p_kw
2816%type <id> keyword_variable user_variable sym operation2 operation3
2817%type <id> cname fname op f_rest_arg f_block_arg opt_comma f_norm_arg f_bad_arg
2818%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2819%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2820%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2821%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2822%type <ctxt> p_in_kwarg
2823%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2824%type <num> max_numparam
2825%type <node> numparam
2826%type <id> it_id
2827%token END_OF_INPUT 0 "end-of-input"
2828%token <id> '.'
2829
2830/* escaped chars, should be ignored otherwise */
2831%token <id> '\\' "backslash"
2832%token tSP "escaped space"
2833%token <id> '\t' "escaped horizontal tab"
2834%token <id> '\f' "escaped form feed"
2835%token <id> '\r' "escaped carriage return"
2836%token <id> '\13' "escaped vertical tab"
2837%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2838%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2839%token tPOW RUBY_TOKEN(POW) "**"
2840%token tCMP RUBY_TOKEN(CMP) "<=>"
2841%token tEQ RUBY_TOKEN(EQ) "=="
2842%token tEQQ RUBY_TOKEN(EQQ) "==="
2843%token tNEQ RUBY_TOKEN(NEQ) "!="
2844%token tGEQ RUBY_TOKEN(GEQ) ">="
2845%token tLEQ RUBY_TOKEN(LEQ) "<="
2846%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2847%token tOROP RUBY_TOKEN(OROP) "||"
2848%token tMATCH RUBY_TOKEN(MATCH) "=~"
2849%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2850%token tDOT2 RUBY_TOKEN(DOT2) ".."
2851%token tDOT3 RUBY_TOKEN(DOT3) "..."
2852%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2853%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2854%token tAREF RUBY_TOKEN(AREF) "[]"
2855%token tASET RUBY_TOKEN(ASET) "[]="
2856%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2857%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2858%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2859%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2860%token tCOLON3 ":: at EXPR_BEG"
2861%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2862%token tASSOC "=>"
2863%token tLPAREN "("
2864%token tLPAREN_ARG "( arg"
2865%token tLBRACK "["
2866%token tLBRACE "{"
2867%token tLBRACE_ARG "{ arg"
2868%token tSTAR "*"
2869%token tDSTAR "**arg"
2870%token tAMPER "&"
2871%token <num> tLAMBDA "->"
2872%token tSYMBEG "symbol literal"
2873%token tSTRING_BEG "string literal"
2874%token tXSTRING_BEG "backtick literal"
2875%token tREGEXP_BEG "regexp literal"
2876%token tWORDS_BEG "word list"
2877%token tQWORDS_BEG "verbatim word list"
2878%token tSYMBOLS_BEG "symbol list"
2879%token tQSYMBOLS_BEG "verbatim symbol list"
2880%token tSTRING_END "terminator"
2881%token tSTRING_DEND "'}'"
2882%token <state> tSTRING_DBEG "'#{'"
2883%token tSTRING_DVAR tLAMBEG tLABEL_END
2884
2885%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2886%token tHEREDOC_BEG tHEREDOC_END k__END__
2887
2888/*
2889 * precedence table
2890 */
2891
2892%nonassoc tLOWEST
2893%nonassoc tLBRACE_ARG
2894
2895%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2896%left keyword_or keyword_and
2897%right keyword_not
2898%nonassoc keyword_defined
2899%right '=' tOP_ASGN
2900%left modifier_rescue
2901%right '?' ':'
2902%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2903%left tOROP
2904%left tANDOP
2905%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2906%left '>' tGEQ '<' tLEQ
2907%left '|' '^'
2908%left '&'
2909%left tLSHFT tRSHFT
2910%left '+' '-'
2911%left '*' '/' '%'
2912%right tUMINUS_NUM tUMINUS
2913%right tPOW
2914%right '!' '~' tUPLUS
2915
2916%token tLAST_TOKEN
2917
2918/*
2919 * inlining rules
2920 */
2921%rule %inline ident_or_const
2922 : tIDENTIFIER
2923 | tCONSTANT
2924 ;
2925
2926%rule %inline user_or_keyword_variable
2927 : user_variable
2928 | keyword_variable
2929 ;
2930
2931/*
2932 * parameterizing rules
2933 */
2934%rule asgn(rhs) <node>
2935 : lhs '=' lex_ctxt rhs
2936 {
2937 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2938 /*% ripper: assign!($:1, $:4) %*/
2939 }
2940 ;
2941
2942%rule args_tail_basic(value, trailing) <node_args>
2943 : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing)
2944 {
2945 $$ = new_args_tail(p, $1, $3, $4, &@3);
2946 /*% ripper: [$:1, $:3, $:4] %*/
2947 }
2948 | f_kwarg(value) opt_f_block_arg(trailing)
2949 {
2950 $$ = new_args_tail(p, $1, 0, $2, &@1);
2951 /*% ripper: [$:1, Qnil, $:2] %*/
2952 }
2953 | f_any_kwrest opt_f_block_arg(trailing)
2954 {
2955 $$ = new_args_tail(p, 0, $1, $2, &@1);
2956 /*% ripper: [Qnil, $:1, $:2] %*/
2957 }
2958 | f_block_arg
2959 {
2960 $$ = new_args_tail(p, 0, 0, $1, &@1);
2961 /*% ripper: [Qnil, Qnil, $:1] %*/
2962 }
2963 ;
2964
2965%rule opt_f_block_arg(trailing) <id>
2966 : ',' f_block_arg
2967 {
2968 $$ = $2;
2969 /*% ripper: $:2 %*/
2970 }
2971 | trailing
2972 ;
2973
2974%rule def_endless_method(bodystmt) <node>
2975 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2976 {
2977 endless_method_name(p, $head->nd_mid, &@head);
2978 restore_defun(p, $head);
2979 ($$ = $head->nd_def)->nd_loc = @$;
2980 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2981 RNODE_DEFN($$)->nd_defn = $bodystmt;
2982 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2983 /*% ripper: def!($:head, $:args, $:$) %*/
2984 local_pop(p);
2985 }
2986 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2987 {
2988 endless_method_name(p, $head->nd_mid, &@head);
2989 restore_defun(p, $head);
2990 ($$ = $head->nd_def)->nd_loc = @$;
2991 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2992 RNODE_DEFS($$)->nd_defn = $bodystmt;
2993 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2994 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2995 local_pop(p);
2996 }
2997 ;
2998
2999%rule compstmt(stmts) <node>
3000 : stmts terms?
3001 {
3002 void_stmts(p, $$ = $stmts);
3003 }
3004 ;
3005
3006%rule f_opt(value) <node_opt_arg>
3007 : f_arg_asgn f_eq value
3008 {
3009 p->ctxt.in_argdef = 1;
3010 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
3011 /*% ripper: [$:$, $:3] %*/
3012 }
3013 ;
3014
3015%rule f_opt_arg(value) <node_opt_arg>
3016 : f_opt(value)
3017 {
3018 $$ = $f_opt;
3019 /*% ripper: rb_ary_new3(1, $:1) %*/
3020 }
3021 | f_opt_arg(value) ',' f_opt(value)
3022 {
3023 $$ = opt_arg_append($f_opt_arg, $f_opt);
3024 /*% ripper: rb_ary_push($:1, $:3) %*/
3025 }
3026 ;
3027
3028%rule f_kw(value) <node_kw_arg>
3029 : f_label value
3030 {
3031 p->ctxt.in_argdef = 1;
3032 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3033 /*% ripper: [$:$, $:value] %*/
3034 }
3035 | f_label
3036 {
3037 p->ctxt.in_argdef = 1;
3038 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3039 /*% ripper: [$:$, 0] %*/
3040 }
3041 ;
3042
3043%rule f_kwarg(value) <node_kw_arg>
3044 : f_kw(value)
3045 {
3046 $$ = $f_kw;
3047 /*% ripper: rb_ary_new3(1, $:1) %*/
3048 }
3049 | f_kwarg(value) ',' f_kw(value)
3050 {
3051 $$ = kwd_append($f_kwarg, $f_kw);
3052 /*% ripper: rb_ary_push($:1, $:3) %*/
3053 }
3054 ;
3055
3056%rule mlhs_items(item) <node>
3057 : item
3058 {
3059 $$ = NEW_LIST($1, &@$);
3060 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3061 }
3062 | mlhs_items(item) ',' item
3063 {
3064 $$ = list_append(p, $1, $3);
3065 /*% ripper: mlhs_add!($:1, $:3) %*/
3066 }
3067 ;
3068
3069%rule op_asgn(rhs) <node>
3070 : var_lhs tOP_ASGN lex_ctxt rhs
3071 {
3072 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3073 /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
3074 }
3075 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3076 {
3077 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3078 /*% ripper: opassign!(aref_field!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/
3079 }
3080 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3081 {
3082 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3083 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3084 }
3085 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3086 {
3087 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3088 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3089 }
3090 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3091 {
3092 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3093 /*% ripper: opassign!(field!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3094 }
3095 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3096 {
3097 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3098 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3099 /*% ripper: opassign!(const_path_field!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3100 }
3101 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3102 {
3103 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3104 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3105 /*% ripper: opassign!(top_const_field!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3106 }
3107 | backref tOP_ASGN lex_ctxt rhs
3108 {
3109 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3110 $$ = NEW_ERROR(&@$);
3111 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:backref), $:tOP_ASGN, $:rhs)) %*/
3112 }
3113 ;
3114
3115%rule opt_args_tail(tail, trailing) <node_args>
3116 : ',' tail
3117 {
3118 $$ = $tail;
3119 /*% ripper: $:tail %*/
3120 }
3121 | trailing
3122 {
3123 $$ = new_empty_args_tail(p, &@$);
3124 /*% ripper: [Qnil, Qnil, Qnil] %*/
3125 }
3126 ;
3127
3128%rule range_expr(range) <node>
3129 : range tDOT2 range
3130 {
3131 value_expr(p, $1);
3132 value_expr(p, $3);
3133 $$ = NEW_DOT2($1, $3, &@$, &@2);
3134 /*% ripper: dot2!($:1, $:3) %*/
3135 }
3136 | range tDOT3 range
3137 {
3138 value_expr(p, $1);
3139 value_expr(p, $3);
3140 $$ = NEW_DOT3($1, $3, &@$, &@2);
3141 /*% ripper: dot3!($:1, $:3) %*/
3142 }
3143 | range tDOT2
3144 {
3145 value_expr(p, $1);
3146 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3147 /*% ripper: dot2!($:1, Qnil) %*/
3148 }
3149 | range tDOT3
3150 {
3151 value_expr(p, $1);
3152 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3153 /*% ripper: dot3!($:1, Qnil) %*/
3154 }
3155 | tBDOT2 range
3156 {
3157 value_expr(p, $2);
3158 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3159 /*% ripper: dot2!(Qnil, $:2) %*/
3160 }
3161 | tBDOT3 range
3162 {
3163 value_expr(p, $2);
3164 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3165 /*% ripper: dot3!(Qnil, $:2) %*/
3166 }
3167 ;
3168
3169%rule value_expr(value) <node>
3170 : value
3171 {
3172 value_expr(p, $1);
3173 $$ = $1;
3174 }
3175 ;
3176
3177%rule words(begin, word_list) <node>
3178 : begin ' '+ word_list tSTRING_END
3179 {
3180 $$ = make_list($word_list, &@$);
3181 /*% ripper: array!($:word_list) %*/
3182 }
3183 ;
3184
3185%%
3186program : {
3187 SET_LEX_STATE(EXPR_BEG);
3188 local_push(p, ifndef_ripper(1)+0);
3189 /* jumps are possible in the top-level loop. */
3190 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3191 }
3192 compstmt(top_stmts)
3193 {
3194 if ($2 && !compile_for_eval) {
3195 NODE *node = $2;
3196 /* last expression should not be void */
3197 if (nd_type_p(node, NODE_BLOCK)) {
3198 while (RNODE_BLOCK(node)->nd_next) {
3199 node = RNODE_BLOCK(node)->nd_next;
3200 }
3201 node = RNODE_BLOCK(node)->nd_head;
3202 }
3203 node = remove_begin(node);
3204 void_expr(p, node);
3205 }
3206 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3207 /*% ripper[final]: program!($:2) %*/
3208 local_pop(p);
3209 }
3210 ;
3211
3212top_stmts : none
3213 {
3214 $$ = NEW_BEGIN(0, &@$);
3215 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3216 }
3217 | top_stmt
3218 {
3219 $$ = newline_node($1);
3220 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3221 }
3222 | top_stmts terms top_stmt
3223 {
3224 $$ = block_append(p, $1, newline_node($3));
3225 /*% ripper: stmts_add!($:1, $:3) %*/
3226 }
3227 ;
3228
3229top_stmt : stmt
3230 {
3231 clear_block_exit(p, true);
3232 $$ = $1;
3233 }
3234 | keyword_BEGIN begin_block
3235 {
3236 $$ = $2;
3237 /*% ripper: $:2 %*/
3238 }
3239 ;
3240
3241block_open : '{' {$$ = init_block_exit(p);};
3242
3243begin_block : block_open compstmt(top_stmts) '}'
3244 {
3245 restore_block_exit(p, $block_open);
3246 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3247 NEW_BEGIN($compstmt, &@$));
3248 $$ = NEW_BEGIN(0, &@$);
3249 /*% ripper: BEGIN!($:compstmt) %*/
3250 }
3251 ;
3252
3253bodystmt : compstmt(stmts)[body]
3254 lex_ctxt[ctxt]
3255 opt_rescue
3256 k_else
3257 {
3258 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3259 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3260 }
3261 compstmt(stmts)[elsebody]
3262 {
3263 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3264 }
3265 opt_ensure
3266 {
3267 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3268 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3269 }
3270 | compstmt(stmts)[body]
3271 lex_ctxt[ctxt]
3272 opt_rescue
3273 {
3274 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3275 }
3276 opt_ensure
3277 {
3278 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3279 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3280 }
3281 ;
3282
3283stmts : none
3284 {
3285 $$ = NEW_BEGIN(0, &@$);
3286 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3287 }
3288 | stmt_or_begin
3289 {
3290 $$ = newline_node($1);
3291 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3292 }
3293 | stmts terms stmt_or_begin
3294 {
3295 $$ = block_append(p, $1, newline_node($3));
3296 /*% ripper: stmts_add!($:1, $:3) %*/
3297 }
3298 ;
3299
3300stmt_or_begin : stmt
3301 | keyword_BEGIN
3302 {
3303 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3304 }
3305 begin_block
3306 {
3307 $$ = $3;
3308 }
3309 ;
3310
3311allow_exits : {$$ = allow_block_exit(p);};
3312
3313k_END : keyword_END lex_ctxt
3314 {
3315 if (p->ctxt.in_def) {
3316 rb_warn0("END in method; use at_exit");
3317 }
3318 $$ = $2;
3319 p->ctxt.in_rescue = before_rescue;
3320 /*% ripper: $:2 %*/
3321 };
3322
3323stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3324 {
3325 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3326 /*% ripper: alias!($:new, $:old) %*/
3327 }
3328 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3329 {
3330 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3331 /*% ripper: var_alias!($:new, $:old) %*/
3332 }
3333 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
3334 {
3335 char buf[2];
3336 buf[0] = '$';
3337 buf[1] = (char)RNODE_BACK_REF($old)->nd_nth;
3338 $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw);
3339 /*% ripper: var_alias!($:new, $:old) %*/
3340 }
3341 | keyword_alias tGVAR tNTH_REF[nth]
3342 {
3343 static const char mesg[] = "can't make alias for the number variables";
3344 /*%%%*/
3345 yyerror1(&@nth, mesg);
3346 /*% %*/
3347 $$ = NEW_ERROR(&@$);
3348 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3349 }
3350 | keyword_undef[kw] undef_list[list]
3351 {
3352 nd_set_first_loc($list, @kw.beg_pos);
3353 RNODE_UNDEF($list)->keyword_loc = @kw;
3354 $$ = $list;
3355 /*% ripper: undef!($:list) %*/
3356 }
3357 | stmt[body] modifier_if[mod] expr_value[cond]
3358 {
3359 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3360 fixpos($$, $cond);
3361 /*% ripper: if_mod!($:cond, $:body) %*/
3362 }
3363 | stmt[body] modifier_unless[mod] expr_value[cond]
3364 {
3365 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3366 fixpos($$, $cond);
3367 /*% ripper: unless_mod!($:cond, $:body) %*/
3368 }
3369 | stmt[body] modifier_while[mod] expr_value[cond_expr]
3370 {
3371 clear_block_exit(p, false);
3372 if ($body && nd_type_p($body, NODE_BEGIN)) {
3373 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3374 }
3375 else {
3376 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3377 }
3378 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3379 }
3380 | stmt[body] modifier_until[mod] expr_value[cond_expr]
3381 {
3382 clear_block_exit(p, 0);
3383 if ($body && nd_type_p($body, NODE_BEGIN)) {
3384 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3385 }
3386 else {
3387 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3388 }
3389 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3390 }
3391 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3392 {
3393 p->ctxt.in_rescue = $ctxt.in_rescue;
3394 NODE *resq;
3395 YYLTYPE loc = code_loc_gen(&@mod, &@resbody);
3396 resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3397 $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$);
3398 /*% ripper: rescue_mod!($:body, $:resbody) %*/
3399 }
3400 | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace]
3401 {
3402 clear_block_exit(p, true);
3403 restore_block_exit(p, $block_open);
3404 p->ctxt = $k_end;
3405 {
3406 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$);
3407 $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace);
3408 RNODE_SCOPE(scope)->nd_parent = $$;
3409 }
3410 /*% ripper: END!($:body) %*/
3411 }
3412 | command_asgn
3413 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3414 {
3415 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3416 /*% ripper: massign!($:lhs, $:rhs) %*/
3417 }
3418 | asgn(mrhs)
3419 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3420 after_rescue[after_rescue] stmt[resbody]
3421 {
3422 p->ctxt.in_rescue = $after_rescue.in_rescue;
3423 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3424 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3425 loc.beg_pos = @mrhs_arg.beg_pos;
3426 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3427 $$ = node_assign(p, (NODE *)$lhs, $mrhs_arg, $lex_ctxt, &@$);
3428 /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/
3429 }
3430 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3431 {
3432 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3433 /*% ripper: massign!($:lhs, $:rhs) %*/
3434 }
3435 | expr
3436 | error
3437 {
3438 (void)yynerrs;
3439 $$ = NEW_ERROR(&@$);
3440 }
3441 ;
3442
3443command_asgn : asgn(command_rhs)
3444 | op_asgn(command_rhs)
3445 | def_endless_method(endless_command)
3446 ;
3447
3448endless_command : command
3449 | endless_command modifier_rescue after_rescue arg
3450 {
3451 p->ctxt.in_rescue = $3.in_rescue;
3452 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3453 /*% ripper: rescue_mod!($:1, $:4) %*/
3454 }
3455 | keyword_not '\n'? endless_command
3456 {
3457 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3458 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3459 }
3460 ;
3461
3462command_rhs : command_call_value %prec tOP_ASGN
3463 | command_call_value modifier_rescue after_rescue stmt
3464 {
3465 p->ctxt.in_rescue = $3.in_rescue;
3466 YYLTYPE loc = code_loc_gen(&@2, &@4);
3467 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3468 /*% ripper: rescue_mod!($:1, $:4) %*/
3469 }
3470 | command_asgn
3471 ;
3472
3473expr : command_call
3474 | expr[left] keyword_and[op] expr[right]
3475 {
3476 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3477 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3478 }
3479 | expr[left] keyword_or[op] expr[right]
3480 {
3481 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3482 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3483 }
3484 | keyword_not[not] '\n'? expr[arg]
3485 {
3486 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3487 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3488 }
3489 | '!'[not] command_call[arg]
3490 {
3491 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3492 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3493 }
3494 | arg tASSOC[assoc]
3495 {
3496 value_expr(p, $arg);
3497 }
3498 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3499 p_top_expr_body[body]
3500 {
3501 pop_pktbl(p, $p_pktbl);
3502 pop_pvtbl(p, $p_pvtbl);
3503 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3504 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3505 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3506 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@assoc), &@$, &NULL_LOC, &NULL_LOC);
3507 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3508 }
3509 | arg keyword_in
3510 {
3511 value_expr(p, $arg);
3512 }
3513 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3514 p_top_expr_body[body]
3515 {
3516 pop_pktbl(p, $p_pktbl);
3517 pop_pvtbl(p, $p_pvtbl);
3518 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3519 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3520 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3521 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3522 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3523 }
3524 | arg %prec tLBRACE_ARG
3525 ;
3526
3527def_name : fname
3528 {
3529 numparam_name(p, $fname);
3530 local_push(p, 0);
3531 p->ctxt.in_def = 1;
3532 p->ctxt.in_rescue = before_rescue;
3533 p->ctxt.cant_return = 0;
3534 $$ = $fname;
3535 }
3536 ;
3537
3538defn_head : k_def def_name
3539 {
3540 $$ = def_head_save(p, $k_def);
3541 $$->nd_mid = $def_name;
3542 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3543 /*% ripper: $:def_name %*/
3544 }
3545 ;
3546
3547defs_head : k_def singleton dot_or_colon
3548 {
3549 SET_LEX_STATE(EXPR_FNAME);
3550 }
3551 def_name
3552 {
3553 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3554 $$ = def_head_save(p, $k_def);
3555 $$->nd_mid = $def_name;
3556 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3557 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3558 }
3559 ;
3560
3561expr_value : value_expr(expr)
3562 | error
3563 {
3564 $$ = NEW_ERROR(&@$);
3565 }
3566 ;
3567
3568expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3569 {
3570 $$ = $2;
3571 /*% ripper: $:2 %*/
3572 }
3573 ;
3574
3575command_call : command
3576 | block_command
3577 ;
3578
3579command_call_value : value_expr(command_call)
3580 ;
3581
3582block_command : block_call
3583 | block_call call_op2 operation2 command_args
3584 {
3585 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3586 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3587 }
3588 ;
3589
3590cmd_brace_block : tLBRACE_ARG brace_body '}'
3591 {
3592 $$ = $2;
3593 set_embraced_location($$, &@1, &@3);
3594 /*% ripper: $:2 %*/
3595 }
3596 ;
3597
3598fcall : operation
3599 {
3600 $$ = NEW_FCALL($1, 0, &@$);
3601 /*% ripper: $:1 %*/
3602 }
3603 ;
3604
3605command : fcall command_args %prec tLOWEST
3606 {
3607 $1->nd_args = $2;
3608 nd_set_last_loc($1, @2.end_pos);
3609 $$ = (NODE *)$1;
3610 /*% ripper: command!($:1, $:2) %*/
3611 }
3612 | fcall command_args cmd_brace_block
3613 {
3614 block_dup_check(p, $2, $3);
3615 $1->nd_args = $2;
3616 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3617 fixpos($$, RNODE($1));
3618 nd_set_last_loc($1, @2.end_pos);
3619 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3620 }
3621 | primary_value call_op operation2 command_args %prec tLOWEST
3622 {
3623 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3624 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3625 }
3626 | primary_value call_op operation2 command_args cmd_brace_block
3627 {
3628 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3629 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3630 }
3631 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3632 {
3633 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3634 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3635 }
3636 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3637 {
3638 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3639 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3640 }
3641 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3642 {
3643 set_embraced_location($5, &@4, &@6);
3644 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3645 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3646 }
3647 | keyword_super command_args
3648 {
3649 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3650 fixpos($$, $2);
3651 /*% ripper: super!($:2) %*/
3652 }
3653 | k_yield command_args
3654 {
3655 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3656 fixpos($$, $2);
3657 /*% ripper: yield!($:2) %*/
3658 }
3659 | k_return call_args
3660 {
3661 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3662 /*% ripper: return!($:2) %*/
3663 }
3664 | keyword_break call_args
3665 {
3666 NODE *args = 0;
3667 args = ret_args(p, $2);
3668 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3669 /*% ripper: break!($:2) %*/
3670 }
3671 | keyword_next call_args
3672 {
3673 NODE *args = 0;
3674 args = ret_args(p, $2);
3675 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3676 /*% ripper: next!($:2) %*/
3677 }
3678 ;
3679
3680mlhs : mlhs_basic
3681 | tLPAREN mlhs_inner rparen
3682 {
3683 $$ = $2;
3684 /*% ripper: mlhs_paren!($:2) %*/
3685 }
3686 ;
3687
3688mlhs_inner : mlhs_basic
3689 | tLPAREN mlhs_inner rparen
3690 {
3691 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3692 /*% ripper: mlhs_paren!($:2) %*/
3693 }
3694 ;
3695
3696mlhs_basic : mlhs_head
3697 {
3698 $$ = NEW_MASGN($1, 0, &@$);
3699 /*% ripper: $:1 %*/
3700 }
3701 | mlhs_head mlhs_item
3702 {
3703 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3704 /*% ripper: mlhs_add!($:1, $:2) %*/
3705 }
3706 | mlhs_head tSTAR mlhs_node
3707 {
3708 $$ = NEW_MASGN($1, $3, &@$);
3709 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3710 }
3711 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3712 {
3713 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3714 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3715 }
3716 | mlhs_head tSTAR
3717 {
3718 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3719 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3720 }
3721 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3722 {
3723 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3724 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3725 }
3726 | tSTAR mlhs_node
3727 {
3728 $$ = NEW_MASGN(0, $2, &@$);
3729 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3730 }
3731 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3732 {
3733 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3734 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3735 }
3736 | tSTAR
3737 {
3738 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3739 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3740 }
3741 | tSTAR ',' mlhs_items(mlhs_item)
3742 {
3743 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3744 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3745 }
3746 ;
3747
3748mlhs_item : mlhs_node
3749 | tLPAREN mlhs_inner rparen
3750 {
3751 $$ = (NODE *)$2;
3752 /*% ripper: mlhs_paren!($:2) %*/
3753 }
3754 ;
3755
3756mlhs_head : mlhs_item ','
3757 {
3758 $$ = NEW_LIST($1, &@1);
3759 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3760 }
3761 | mlhs_head mlhs_item ','
3762 {
3763 $$ = list_append(p, $1, $2);
3764 /*% ripper: mlhs_add!($:1, $:2) %*/
3765 }
3766 ;
3767
3768
3769mlhs_node : user_or_keyword_variable
3770 {
3771 /*% ripper: var_field!($:1) %*/
3772 $$ = assignable(p, $1, 0, &@$);
3773 }
3774 | primary_value '[' opt_call_args rbracket
3775 {
3776 $$ = aryset(p, $1, $3, &@$);
3777 /*% ripper: aref_field!($:1, $:3) %*/
3778 }
3779 | primary_value call_op ident_or_const
3780 {
3781 anddot_multiple_assignment_check(p, &@2, $2);
3782 $$ = attrset(p, $1, $2, $3, &@$);
3783 /*% ripper: field!($:1, $:2, $:3) %*/
3784 }
3785 | primary_value tCOLON2 tIDENTIFIER
3786 {
3787 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3788 /*% ripper: const_path_field!($:1, $:3) %*/
3789 }
3790 | primary_value tCOLON2 tCONSTANT
3791 {
3792 /*% ripper: const_path_field!($:1, $:3) %*/
3793 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3794 }
3795 | tCOLON3 tCONSTANT
3796 {
3797 /*% ripper: top_const_field!($:2) %*/
3798 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3799 }
3800 | backref
3801 {
3802 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3803 $$ = NEW_ERROR(&@$);
3804 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3805 }
3806 ;
3807
3808lhs : user_or_keyword_variable
3809 {
3810 /*% ripper: var_field!($:1) %*/
3811 $$ = assignable(p, $1, 0, &@$);
3812 }
3813 | primary_value '[' opt_call_args rbracket
3814 {
3815 $$ = aryset(p, $1, $3, &@$);
3816 /*% ripper: aref_field!($:1, $:3) %*/
3817 }
3818 | primary_value call_op ident_or_const
3819 {
3820 $$ = attrset(p, $1, $2, $3, &@$);
3821 /*% ripper: field!($:1, $:2, $:3) %*/
3822 }
3823 | primary_value tCOLON2 tIDENTIFIER
3824 {
3825 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3826 /*% ripper: field!($:1, $:2, $:3) %*/
3827 }
3828 | primary_value tCOLON2 tCONSTANT
3829 {
3830 /*% ripper: const_path_field!($:1, $:3) %*/
3831 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3832 }
3833 | tCOLON3 tCONSTANT
3834 {
3835 /*% ripper: top_const_field!($:2) %*/
3836 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3837 }
3838 | backref
3839 {
3840 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3841 $$ = NEW_ERROR(&@$);
3842 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3843 }
3844 ;
3845
3846cname : tIDENTIFIER
3847 {
3848 static const char mesg[] = "class/module name must be CONSTANT";
3849 /*%%%*/
3850 yyerror1(&@1, mesg);
3851 /*% %*/
3852 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3853 }
3854 | tCONSTANT
3855 ;
3856
3857cpath : tCOLON3 cname
3858 {
3859 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3860 /*% ripper: top_const_ref!($:2) %*/
3861 }
3862 | cname
3863 {
3864 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3865 /*% ripper: const_ref!($:1) %*/
3866 }
3867 | primary_value tCOLON2 cname
3868 {
3869 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3870 /*% ripper: const_path_ref!($:1, $:3) %*/
3871 }
3872 ;
3873
3874fname : operation
3875 | op
3876 {
3877 SET_LEX_STATE(EXPR_ENDFN);
3878 $$ = $1;
3879 }
3880 | reswords
3881 ;
3882
3883fitem : fname
3884 {
3885 $$ = NEW_SYM(rb_id2str($1), &@$);
3886 /*% ripper: symbol_literal!($:1) %*/
3887 }
3888 | symbol
3889 ;
3890
3891undef_list : fitem
3892 {
3893 $$ = NEW_UNDEF($1, &@$);
3894 /*% ripper: rb_ary_new3(1, $:1) %*/
3895 }
3896 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3897 {
3898 nd_set_last_loc($1, @4.end_pos);
3899 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3900 /*% ripper: rb_ary_push($:1, $:4) %*/
3901 }
3902 ;
3903
3904op : '|' { $$ = '|'; }
3905 | '^' { $$ = '^'; }
3906 | '&' { $$ = '&'; }
3907 | tCMP { $$ = tCMP; }
3908 | tEQ { $$ = tEQ; }
3909 | tEQQ { $$ = tEQQ; }
3910 | tMATCH { $$ = tMATCH; }
3911 | tNMATCH { $$ = tNMATCH; }
3912 | '>' { $$ = '>'; }
3913 | tGEQ { $$ = tGEQ; }
3914 | '<' { $$ = '<'; }
3915 | tLEQ { $$ = tLEQ; }
3916 | tNEQ { $$ = tNEQ; }
3917 | tLSHFT { $$ = tLSHFT; }
3918 | tRSHFT { $$ = tRSHFT; }
3919 | '+' { $$ = '+'; }
3920 | '-' { $$ = '-'; }
3921 | '*' { $$ = '*'; }
3922 | tSTAR { $$ = '*'; }
3923 | '/' { $$ = '/'; }
3924 | '%' { $$ = '%'; }
3925 | tPOW { $$ = tPOW; }
3926 | tDSTAR { $$ = tDSTAR; }
3927 | '!' { $$ = '!'; }
3928 | '~' { $$ = '~'; }
3929 | tUPLUS { $$ = tUPLUS; }
3930 | tUMINUS { $$ = tUMINUS; }
3931 | tAREF { $$ = tAREF; }
3932 | tASET { $$ = tASET; }
3933 | '`' { $$ = '`'; }
3934 ;
3935
3936reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3937 | keyword_BEGIN | keyword_END
3938 | keyword_alias | keyword_and | keyword_begin
3939 | keyword_break | keyword_case | keyword_class | keyword_def
3940 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3941 | keyword_end | keyword_ensure | keyword_false
3942 | keyword_for | keyword_in | keyword_module | keyword_next
3943 | keyword_nil | keyword_not | keyword_or | keyword_redo
3944 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3945 | keyword_super | keyword_then | keyword_true | keyword_undef
3946 | keyword_when | keyword_yield | keyword_if | keyword_unless
3947 | keyword_while | keyword_until
3948 ;
3949
3950arg : asgn(arg_rhs)
3951 | op_asgn(arg_rhs)
3952 | range_expr(arg)
3953 | arg '+' arg
3954 {
3955 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3956 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3957 }
3958 | arg '-' arg
3959 {
3960 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3961 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3962 }
3963 | arg '*' arg
3964 {
3965 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3966 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3967 }
3968 | arg '/' arg
3969 {
3970 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3971 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3972 }
3973 | arg '%' arg
3974 {
3975 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3976 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3977 }
3978 | arg tPOW arg
3979 {
3980 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3981 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3982 }
3983 | tUMINUS_NUM simple_numeric tPOW arg
3984 {
3985 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3986 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3987 }
3988 | tUPLUS arg
3989 {
3990 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3991 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3992 }
3993 | tUMINUS arg
3994 {
3995 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3996 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3997 }
3998 | arg '|' arg
3999 {
4000 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
4001 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
4002 }
4003 | arg '^' arg
4004 {
4005 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
4006 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
4007 }
4008 | arg '&' arg
4009 {
4010 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
4011 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
4012 }
4013 | arg tCMP arg
4014 {
4015 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
4016 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
4017 }
4018 | rel_expr %prec tCMP
4019 | arg tEQ arg
4020 {
4021 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4022 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4023 }
4024 | arg tEQQ arg
4025 {
4026 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4027 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4028 }
4029 | arg tNEQ arg
4030 {
4031 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4032 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4033 }
4034 | arg tMATCH arg
4035 {
4036 $$ = match_op(p, $1, $3, &@2, &@$);
4037 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4038 }
4039 | arg tNMATCH arg
4040 {
4041 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4042 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4043 }
4044 | '!' arg
4045 {
4046 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4047 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4048 }
4049 | '~' arg
4050 {
4051 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4052 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4053 }
4054 | arg tLSHFT arg
4055 {
4056 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4057 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4058 }
4059 | arg tRSHFT arg
4060 {
4061 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4062 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4063 }
4064 | arg tANDOP arg
4065 {
4066 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4067 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4068 }
4069 | arg tOROP arg
4070 {
4071 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4072 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4073 }
4074 | keyword_defined '\n'? begin_defined arg
4075 {
4076 p->ctxt.in_defined = $3.in_defined;
4077 $$ = new_defined(p, $4, &@$, &@1);
4078 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4079 /*% ripper: defined!($:4) %*/
4080 }
4081 | def_endless_method(endless_arg)
4082 | ternary
4083 | primary
4084 ;
4085
4086ternary : arg '?' arg '\n'? ':' arg
4087 {
4088 value_expr(p, $1);
4089 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4090 fixpos($$, $1);
4091 /*% ripper: ifop!($:1, $:3, $:6) %*/
4092 }
4093 ;
4094
4095endless_arg : arg %prec modifier_rescue
4096 | endless_arg modifier_rescue after_rescue arg
4097 {
4098 p->ctxt.in_rescue = $3.in_rescue;
4099 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4100 /*% ripper: rescue_mod!($:1, $:4) %*/
4101 }
4102 | keyword_not '\n'? endless_arg
4103 {
4104 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4105 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4106 }
4107 ;
4108
4109relop : '>' {$$ = '>';}
4110 | '<' {$$ = '<';}
4111 | tGEQ {$$ = idGE;}
4112 | tLEQ {$$ = idLE;}
4113 ;
4114
4115rel_expr : arg relop arg %prec '>'
4116 {
4117 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4118 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4119 }
4120 | rel_expr relop arg %prec '>'
4121 {
4122 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4123 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4124 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4125 }
4126 ;
4127
4128lex_ctxt : none
4129 {
4130 $$ = p->ctxt;
4131 }
4132 ;
4133
4134begin_defined : lex_ctxt
4135 {
4136 p->ctxt.in_defined = 1;
4137 $$ = $1;
4138 }
4139 ;
4140
4141after_rescue : lex_ctxt
4142 {
4143 p->ctxt.in_rescue = after_rescue;
4144 $$ = $1;
4145 }
4146 ;
4147
4148arg_value : value_expr(arg)
4149 ;
4150
4151aref_args : none
4152 | args trailer
4153 | args ',' assocs trailer
4154 {
4155 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4156 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4157 }
4158 | assocs trailer
4159 {
4160 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4161 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4162 }
4163 ;
4164
4165arg_rhs : arg %prec tOP_ASGN
4166 {
4167 value_expr(p, $1);
4168 $$ = $1;
4169 }
4170 | arg modifier_rescue after_rescue arg
4171 {
4172 p->ctxt.in_rescue = $3.in_rescue;
4173 value_expr(p, $1);
4174 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4175 /*% ripper: rescue_mod!($:1, $:4) %*/
4176 }
4177 ;
4178
4179paren_args : '(' opt_call_args rparen
4180 {
4181 $$ = $2;
4182 /*% ripper: arg_paren!($:2) %*/
4183 }
4184 | '(' args ',' args_forward rparen
4185 {
4186 if (!check_forwarding_args(p)) {
4187 $$ = 0;
4188 }
4189 else {
4190 $$ = new_args_forward_call(p, $2, &@4, &@$);
4191 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4192 }
4193 }
4194 | '(' args_forward rparen
4195 {
4196 if (!check_forwarding_args(p)) {
4197 $$ = 0;
4198 }
4199 else {
4200 $$ = new_args_forward_call(p, 0, &@2, &@$);
4201 /*% ripper: arg_paren!($:2) %*/
4202 }
4203 }
4204 ;
4205
4206opt_paren_args : none
4207 | paren_args
4208 {
4209 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4210 }
4211 ;
4212
4213opt_call_args : none
4214 | call_args
4215 | args ','
4216 | args ',' assocs ','
4217 {
4218 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4219 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4220 }
4221 | assocs ','
4222 {
4223 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4224 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4225 }
4226 ;
4227
4228call_args : value_expr(command)
4229 {
4230 $$ = NEW_LIST($1, &@$);
4231 /*% ripper: args_add!(args_new!, $:1) %*/
4232 }
4233 | def_endless_method(endless_command)
4234 {
4235 $$ = NEW_LIST($1, &@$);
4236 /*% ripper: args_add!(args_new!, $:1) %*/
4237 }
4238 | args opt_block_arg
4239 {
4240 $$ = arg_blk_pass($1, $2);
4241 /*% ripper: args_add_block!($:1, $:2) %*/
4242 }
4243 | assocs opt_block_arg
4244 {
4245 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4246 $$ = arg_blk_pass($$, $2);
4247 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4248 }
4249 | args ',' assocs opt_block_arg
4250 {
4251 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4252 $$ = arg_blk_pass($$, $4);
4253 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4254 }
4255 | block_arg
4256 /*% ripper: args_add_block!(args_new!, $:1) %*/
4257 ;
4258
4259command_args : {
4260 /* If call_args starts with a open paren '(' or '[',
4261 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4262 * but the push must be done after CMDARG_PUSH(1).
4263 * So this code makes them consistent by first cancelling
4264 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4265 * and finally redoing CMDARG_PUSH(0).
4266 */
4267 int lookahead = 0;
4268 switch (yychar) {
4269 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4270 lookahead = 1;
4271 }
4272 if (lookahead) CMDARG_POP();
4273 CMDARG_PUSH(1);
4274 if (lookahead) CMDARG_PUSH(0);
4275 }
4276 call_args
4277 {
4278 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4279 * but the push must be done after CMDARG_POP() in the parser.
4280 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4281 * CMDARG_POP() to pop 1 pushed by command_args,
4282 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4283 */
4284 int lookahead = 0;
4285 switch (yychar) {
4286 case tLBRACE_ARG:
4287 lookahead = 1;
4288 }
4289 if (lookahead) CMDARG_POP();
4290 CMDARG_POP();
4291 if (lookahead) CMDARG_PUSH(0);
4292 $$ = $2;
4293 /*% ripper: $:2 %*/
4294 }
4295 ;
4296
4297block_arg : tAMPER arg_value
4298 {
4299 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4300 /*% ripper: $:2 %*/
4301 }
4302 | tAMPER
4303 {
4304 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4305 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4306 /*% ripper: Qnil %*/
4307 }
4308 ;
4309
4310opt_block_arg : ',' block_arg
4311 {
4312 $$ = $2;
4313 /*% ripper: $:2 %*/
4314 }
4315 | none
4316 {
4317 $$ = 0;
4318 /*% ripper: Qfalse %*/
4319 }
4320 ;
4321
4322/* value */
4323args : arg_value
4324 {
4325 $$ = NEW_LIST($arg_value, &@$);
4326 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4327 }
4328 | arg_splat
4329 {
4330 $$ = $arg_splat;
4331 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4332 }
4333 | args[non_last_args] ',' arg_value
4334 {
4335 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4336 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4337 }
4338 | args[non_last_args] ',' arg_splat
4339 {
4340 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4341 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4342 }
4343 ;
4344
4345/* value */
4346arg_splat : tSTAR arg_value
4347 {
4348 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4349 /*% ripper: $:arg_value %*/
4350 }
4351 | tSTAR /* none */
4352 {
4353 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4354 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4355 /*% ripper: Qnil %*/
4356 }
4357 ;
4358
4359/* value */
4360mrhs_arg : mrhs
4361 | arg_value
4362 ;
4363
4364/* value */
4365mrhs : args ',' arg_value
4366 {
4367 $$ = last_arg_append(p, $args, $arg_value, &@$);
4368 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4369 }
4370 | args ',' tSTAR arg_value
4371 {
4372 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4373 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4374 }
4375 | tSTAR arg_value
4376 {
4377 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4378 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4379 }
4380 ;
4381
4382%rule %inline inline_primary
4383 : literal
4384 | strings
4385 | xstring
4386 | regexp
4387 | words
4388 | qwords
4389 | symbols
4390 | qsymbols
4391 ;
4392
4393primary : inline_primary
4394 | var_ref
4395 | backref
4396 | tFID[fid]
4397 {
4398 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4399 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
4400 }
4401 | k_begin[kw]
4402 {
4403 CMDARG_PUSH(0);
4404 }
4405 bodystmt[body]
4406 k_end[k_end]
4407 {
4408 CMDARG_POP();
4409 set_line_body($body, @kw.end_pos.lineno);
4410 $$ = NEW_BEGIN($body, &@$);
4411 nd_set_line($$, @kw.end_pos.lineno);
4412 /*% ripper: begin!($:body) %*/
4413 }
4414 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4415 {
4416 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4417 $$ = $body;
4418 /*% ripper: paren!($:body) %*/
4419 }
4420 | tLPAREN compstmt(stmts)[body] ')'
4421 {
4422 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4423 $$ = NEW_BLOCK($body, &@$);
4424 /*% ripper: paren!($:body) %*/
4425 }
4426 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4427 {
4428 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4429 /*% ripper: const_path_ref!($:recv, $:name) %*/
4430 }
4431 | tCOLON3[top] tCONSTANT[name]
4432 {
4433 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4434 /*% ripper: top_const_ref!($:name) %*/
4435 }
4436 | tLBRACK aref_args[args] ']'
4437 {
4438 $$ = make_list($args, &@$);
4439 /*% ripper: array!($:args) %*/
4440 }
4441 | tLBRACE assoc_list[list] '}'
4442 {
4443 $$ = new_hash(p, $list, &@$);
4444 RNODE_HASH($$)->nd_brace = TRUE;
4445 /*% ripper: hash!($:list) %*/
4446 }
4447 | k_return[kw]
4448 {
4449 $$ = NEW_RETURN(0, &@$, &@kw);
4450 /*% ripper: return0! %*/
4451 }
4452 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4453 {
4454 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4455 /*% ripper: yield!(paren!($:args)) %*/
4456 }
4457 | k_yield[kw] '('[lpar] rparen[rpar]
4458 {
4459 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4460 /*% ripper: yield!(paren!(args_new!)) %*/
4461 }
4462 | k_yield[kw]
4463 {
4464 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4465 /*% ripper: yield0! %*/
4466 }
4467 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
4468 {
4469 p->ctxt.in_defined = $ctxt.in_defined;
4470 $$ = new_defined(p, $arg, &@$, &@kw);
4471 p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon;
4472 /*% ripper: defined!($:arg) %*/
4473 }
4474 | keyword_not[kw] '(' expr[arg] rparen
4475 {
4476 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4477 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4478 }
4479 | keyword_not[kw] '('[lpar] rparen
4480 {
4481 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4482 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4483 }
4484 | fcall[call] brace_block[block]
4485 {
4486 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4487 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4488 }
4489 | method_call
4490 | method_call[call] brace_block[block]
4491 {
4492 block_dup_check(p, get_nd_args(p, $call), $block);
4493 $$ = method_add_block(p, $call, $block, &@$);
4494 /*% ripper: method_add_block!($:call, $:block) %*/
4495 }
4496 | lambda
4497 | k_if[kw] expr_value[cond] then[then]
4498 compstmt(stmts)[body]
4499 if_tail[tail]
4500 k_end[k_end]
4501 {
4502 if ($tail && nd_type_p($tail, NODE_IF))
4503 RNODE_IF($tail)->end_keyword_loc = @k_end;
4504
4505 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4506 fixpos($$, $cond);
4507 /*% ripper: if!($:cond, $:body, $:tail) %*/
4508 }
4509 | k_unless[kw] expr_value[cond] then[then]
4510 compstmt(stmts)[body]
4511 opt_else[tail]
4512 k_end[k_end]
4513 {
4514 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4515 fixpos($$, $cond);
4516 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4517 }
4518 | k_while[kw] expr_value_do[cond]
4519 compstmt(stmts)[body]
4520 k_end[k_end]
4521 {
4522 restore_block_exit(p, $kw);
4523 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4524 fixpos($$, $cond);
4525 /*% ripper: while!($:cond, $:body) %*/
4526 }
4527 | k_until[kw] expr_value_do[cond]
4528 compstmt(stmts)[body]
4529 k_end[k_end]
4530 {
4531 restore_block_exit(p, $kw);
4532 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4533 fixpos($$, $cond);
4534 /*% ripper: until!($:cond, $:body) %*/
4535 }
4536 | k_case[k_case] expr_value[expr] terms?
4537 {
4538 $$ = p->case_labels;
4539 p->case_labels = CHECK_LITERAL_WHEN;
4540 }[labels]<labels>
4541 case_body[body]
4542 k_end[k_end]
4543 {
4544 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4545 p->case_labels = $labels;
4546 $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end);
4547 fixpos($$, $expr);
4548 /*% ripper: case!($:expr, $:body) %*/
4549 }
4550 | k_case[k_case] terms?
4551 {
4552 $$ = p->case_labels;
4553 p->case_labels = 0;
4554 }[labels]<labels>
4555 case_body[body]
4556 k_end[k_end]
4557 {
4558 if (p->case_labels) st_free_table(p->case_labels);
4559 p->case_labels = $labels;
4560 $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end);
4561 /*% ripper: case!(Qnil, $:body) %*/
4562 }
4563 | k_case[k_case] expr_value[expr] terms?
4564 p_case_body[body]
4565 k_end[k_end]
4566 {
4567 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4568 /*% ripper: case!($:expr, $:body) %*/
4569 }
4570 | k_for[k_for] for_var[for_var] keyword_in[keyword_in]
4571 {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();}
4572 compstmt(stmts)[compstmt]
4573 k_end[k_end]
4574 {
4575 restore_block_exit(p, $k_for);
4576 /*
4577 * for a, b, c in e
4578 * #=>
4579 * e.each{|*x| a, b, c = x}
4580 *
4581 * for a in e
4582 * #=>
4583 * e.each{|x| a, = x}
4584 */
4585 ID id = internal_id(p);
4586 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4587 rb_node_args_t *args;
4588 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4589 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4590 tbl->ids[0] = id; /* internal id */
4591
4592 switch (nd_type($for_var)) {
4593 case NODE_LASGN:
4594 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4595 set_nd_value(p, $for_var, internal_var);
4596 id = 0;
4597 m->nd_plen = 1;
4598 m->nd_next = $for_var;
4599 break;
4600 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4601 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4602 break;
4603 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4604 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);
4605 }
4606 /* {|*internal_id| <m> = internal_id; ... } */
4607 args = new_args(p, m, 0, id, 0, new_empty_args_tail(p, &@for_var), &@for_var);
4608 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4609 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4610 $$ = NEW_FOR($expr_value, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4611 RNODE_SCOPE(scope)->nd_parent = $$;
4612 fixpos($$, $for_var);
4613 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4614 }
4615 | k_class cpath superclass
4616 {
4617 begin_definition("class", &@k_class, &@cpath);
4618 }
4619 bodystmt
4620 k_end
4621 {
4622 YYLTYPE inheritance_operator_loc = NULL_LOC;
4623 if ($superclass) {
4624 inheritance_operator_loc = @superclass;
4625 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4626 }
4627 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4628 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4629 set_line_body($bodystmt, @superclass.end_pos.lineno);
4630 nd_set_line($$, @superclass.end_pos.lineno);
4631 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4632 local_pop(p);
4633 p->ctxt.in_class = $k_class.in_class;
4634 p->ctxt.cant_return = $k_class.cant_return;
4635 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4636 }
4637 | k_class tLSHFT expr_value
4638 {
4639 begin_definition("", &@k_class, &@tLSHFT);
4640 }
4641 term
4642 bodystmt
4643 k_end
4644 {
4645 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4646 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4647 set_line_body($bodystmt, nd_line($expr_value));
4648 fixpos($$, $expr_value);
4649 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4650 local_pop(p);
4651 p->ctxt.in_def = $k_class.in_def;
4652 p->ctxt.in_class = $k_class.in_class;
4653 p->ctxt.cant_return = $k_class.cant_return;
4654 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4655 }
4656 | k_module cpath
4657 {
4658 begin_definition("module", &@k_module, &@cpath);
4659 }
4660 bodystmt
4661 k_end
4662 {
4663 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4664 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4665 set_line_body($bodystmt, @cpath.end_pos.lineno);
4666 nd_set_line($$, @cpath.end_pos.lineno);
4667 /*% ripper: module!($:cpath, $:bodystmt) %*/
4668 local_pop(p);
4669 p->ctxt.in_class = $k_module.in_class;
4670 p->ctxt.cant_return = $k_module.cant_return;
4671 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4672 }
4673 | defn_head[head]
4674 f_arglist[args]
4675 {
4676 push_end_expect_token_locations(p, &@head.beg_pos);
4677 }
4678 bodystmt
4679 k_end
4680 {
4681 restore_defun(p, $head);
4682 ($$ = $head->nd_def)->nd_loc = @$;
4683 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4684 RNODE_DEFN($$)->nd_defn = $bodystmt;
4685 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4686 local_pop(p);
4687 }
4688 | defs_head[head]
4689 f_arglist[args]
4690 {
4691 push_end_expect_token_locations(p, &@head.beg_pos);
4692 }
4693 bodystmt
4694 k_end
4695 {
4696 restore_defun(p, $head);
4697 ($$ = $head->nd_def)->nd_loc = @$;
4698 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4699 RNODE_DEFS($$)->nd_defn = $bodystmt;
4700 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4701 local_pop(p);
4702 }
4703 | keyword_break[kw]
4704 {
4705 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4706 /*% ripper: break!(args_new!) %*/
4707 }
4708 | keyword_next[kw]
4709 {
4710 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4711 /*% ripper: next!(args_new!) %*/
4712 }
4713 | keyword_redo[kw]
4714 {
4715 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4716 /*% ripper: redo! %*/
4717 }
4718 | keyword_retry[kw]
4719 {
4720 if (!p->ctxt.in_defined) {
4721 switch (p->ctxt.in_rescue) {
4722 case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break;
4723 case after_rescue: /* ok */ break;
4724 case after_else: yyerror1(&@kw, "Invalid retry after else"); break;
4725 case after_ensure: yyerror1(&@kw, "Invalid retry after ensure"); break;
4726 }
4727 }
4728 $$ = NEW_RETRY(&@$);
4729 /*% ripper: retry! %*/
4730 }
4731 ;
4732
4733primary_value : value_expr(primary)
4734 ;
4735
4736k_begin : keyword_begin
4737 {
4738 token_info_push(p, "begin", &@$);
4739 push_end_expect_token_locations(p, &@1.beg_pos);
4740 }
4741 ;
4742
4743k_if : keyword_if
4744 {
4745 WARN_EOL("if");
4746 token_info_push(p, "if", &@$);
4747 if (p->token_info && p->token_info->nonspc &&
4748 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4749 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4750 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4751 beg += rb_strlen_lit("else");
4752 while (beg < tok && ISSPACE(*beg)) beg++;
4753 if (beg == tok) {
4754 p->token_info->nonspc = 0;
4755 }
4756 }
4757 push_end_expect_token_locations(p, &@1.beg_pos);
4758 }
4759 ;
4760
4761k_unless : keyword_unless
4762 {
4763 token_info_push(p, "unless", &@$);
4764 push_end_expect_token_locations(p, &@1.beg_pos);
4765 }
4766 ;
4767
4768k_while : keyword_while[kw] allow_exits
4769 {
4770 $$ = $allow_exits;
4771 token_info_push(p, "while", &@$);
4772 push_end_expect_token_locations(p, &@kw.beg_pos);
4773 }
4774 ;
4775
4776k_until : keyword_until[kw] allow_exits
4777 {
4778 $$ = $allow_exits;
4779 token_info_push(p, "until", &@$);
4780 push_end_expect_token_locations(p, &@kw.beg_pos);
4781 }
4782 ;
4783
4784k_case : keyword_case
4785 {
4786 token_info_push(p, "case", &@$);
4787 push_end_expect_token_locations(p, &@1.beg_pos);
4788 }
4789 ;
4790
4791k_for : keyword_for[kw] allow_exits
4792 {
4793 $$ = $allow_exits;
4794 token_info_push(p, "for", &@$);
4795 push_end_expect_token_locations(p, &@kw.beg_pos);
4796 }
4797 ;
4798
4799k_class : keyword_class
4800 {
4801 token_info_push(p, "class", &@$);
4802 $$ = p->ctxt;
4803 p->ctxt.in_rescue = before_rescue;
4804 push_end_expect_token_locations(p, &@1.beg_pos);
4805 }
4806 ;
4807
4808k_module : keyword_module
4809 {
4810 token_info_push(p, "module", &@$);
4811 $$ = p->ctxt;
4812 p->ctxt.in_rescue = before_rescue;
4813 push_end_expect_token_locations(p, &@1.beg_pos);
4814 }
4815 ;
4816
4817k_def : keyword_def
4818 {
4819 token_info_push(p, "def", &@$);
4820 $$ = NEW_DEF_TEMP(&@$);
4821 p->ctxt.in_argdef = 1;
4822 }
4823 ;
4824
4825k_do : keyword_do
4826 {
4827 token_info_push(p, "do", &@$);
4828 push_end_expect_token_locations(p, &@1.beg_pos);
4829 }
4830 ;
4831
4832k_do_block : keyword_do_block
4833 {
4834 token_info_push(p, "do", &@$);
4835 push_end_expect_token_locations(p, &@1.beg_pos);
4836 }
4837 ;
4838
4839k_rescue : keyword_rescue
4840 {
4841 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4842 $$ = p->ctxt;
4843 p->ctxt.in_rescue = after_rescue;
4844 }
4845 ;
4846
4847k_ensure : keyword_ensure
4848 {
4849 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4850 $$ = p->ctxt;
4851 }
4852 ;
4853
4854k_when : keyword_when
4855 {
4856 token_info_warn(p, "when", p->token_info, 0, &@$);
4857 }
4858 ;
4859
4860k_else : keyword_else
4861 {
4862 token_info *ptinfo_beg = p->token_info;
4863 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4864 token_info_warn(p, "else", p->token_info, same, &@$);
4865 if (same) {
4866 token_info e;
4867 e.next = ptinfo_beg->next;
4868 e.token = "else";
4869 token_info_setup(&e, p->lex.pbeg, &@$);
4870 if (!e.nonspc) *ptinfo_beg = e;
4871 }
4872 }
4873 ;
4874
4875k_elsif : keyword_elsif
4876 {
4877 WARN_EOL("elsif");
4878 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4879 }
4880 ;
4881
4882k_end : keyword_end
4883 {
4884 token_info_pop(p, "end", &@$);
4885 pop_end_expect_token_locations(p);
4886 }
4887 | tDUMNY_END
4888 {
4889 compile_error(p, "syntax error, unexpected end-of-input");
4890 }
4891 ;
4892
4893k_return : keyword_return
4894 {
4895 if (p->ctxt.cant_return && !dyna_in_block(p))
4896 yyerror1(&@1, "Invalid return in class/module body");
4897 }
4898 ;
4899
4900k_yield : keyword_yield
4901 {
4902 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4903 yyerror1(&@1, "Invalid yield");
4904 }
4905 ;
4906
4907then : term
4908 | keyword_then
4909 | term keyword_then
4910 ;
4911
4912do : term
4913 | keyword_do_cond { $$ = keyword_do_cond; }
4914 ;
4915
4916if_tail : opt_else
4917 | k_elsif expr_value then
4918 compstmt(stmts)
4919 if_tail
4920 {
4921 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4922 fixpos($$, $2);
4923 /*% ripper: elsif!($:2, $:4, $:5) %*/
4924 }
4925 ;
4926
4927opt_else : none
4928 | k_else compstmt(stmts)
4929 {
4930 $$ = $2;
4931 /*% ripper: else!($:2) %*/
4932 }
4933 ;
4934
4935for_var : lhs
4936 | mlhs
4937 ;
4938
4939f_marg : f_norm_arg
4940 {
4941 $$ = assignable(p, $1, 0, &@$);
4942 mark_lvar_used(p, $$);
4943 }
4944 | tLPAREN f_margs rparen
4945 {
4946 $$ = (NODE *)$2;
4947 /*% ripper: mlhs_paren!($:2) %*/
4948 }
4949 ;
4950
4951
4952f_margs : mlhs_items(f_marg)
4953 {
4954 $$ = NEW_MASGN($1, 0, &@$);
4955 /*% ripper: $:1 %*/
4956 }
4957 | mlhs_items(f_marg) ',' f_rest_marg
4958 {
4959 $$ = NEW_MASGN($1, $3, &@$);
4960 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4961 }
4962 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4963 {
4964 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4965 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4966 }
4967 | f_rest_marg
4968 {
4969 $$ = NEW_MASGN(0, $1, &@$);
4970 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4971 }
4972 | f_rest_marg ',' mlhs_items(f_marg)
4973 {
4974 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4975 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4976 }
4977 ;
4978
4979f_rest_marg : tSTAR f_norm_arg
4980 {
4981 /*% ripper: $:2 %*/
4982 $$ = assignable(p, $2, 0, &@$);
4983 mark_lvar_used(p, $$);
4984 }
4985 | tSTAR
4986 {
4987 $$ = NODE_SPECIAL_NO_NAME_REST;
4988 /*% ripper: Qnil %*/
4989 }
4990 ;
4991
4992f_any_kwrest : f_kwrest
4993 | f_no_kwarg
4994 {
4995 $$ = idNil;
4996 /*% ripper: ID2VAL(idNil) %*/
4997 }
4998 ;
4999
5000f_eq : {p->ctxt.in_argdef = 0;} '=';
5001
5002block_args_tail : args_tail_basic(primary_value, none)
5003 ;
5004
5005excessed_comma : ','
5006 {
5007 /* magic number for rest_id in iseq_set_arguments() */
5008 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5009 /*% ripper: excessed_comma! %*/
5010 }
5011 ;
5012
5013block_param : args-list(primary_value, opt_args_tail(block_args_tail, none))
5014 | f_arg[pre] excessed_comma
5015 {
5016 $$ = new_empty_args_tail(p, &@excessed_comma);
5017 $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$);
5018 /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/
5019 }
5020 | f_arg[pre] opt_args_tail(block_args_tail, none)[tail]
5021 {
5022 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5023 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5024 }
5025 | tail-only-args(block_args_tail)
5026 ;
5027
5028opt_block_param_def : none
5029 | block_param_def
5030 {
5031 p->command_start = TRUE;
5032 }
5033 ;
5034
5035block_param_def : '|' opt_block_param opt_bv_decl '|'
5036 {
5037 p->max_numparam = ORDINAL_PARAM;
5038 p->ctxt.in_argdef = 0;
5039 $$ = $2;
5040 /*% ripper: block_var!($:2, $:3) %*/
5041 }
5042 ;
5043
5044opt_block_param : /* none */
5045 {
5046 $$ = 0;
5047 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5048 }
5049 | block_param
5050 ;
5051
5052opt_bv_decl : '\n'?
5053 {
5054 $$ = 0;
5055 /*% ripper: Qfalse %*/
5056 }
5057 | '\n'? ';' bv_decls '\n'?
5058 {
5059 $$ = 0;
5060 /*% ripper: $:3 %*/
5061 }
5062 ;
5063
5064bv_decls : bvar
5065 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5066 | bv_decls ',' bvar
5067 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5068 ;
5069
5070bvar : tIDENTIFIER
5071 {
5072 new_bv(p, $1);
5073 /*% ripper: $:1 %*/
5074 }
5075 | f_bad_arg
5076 ;
5077
5078max_numparam : {
5079 $$ = p->max_numparam;
5080 p->max_numparam = 0;
5081 }
5082 ;
5083
5084numparam : {
5085 $$ = numparam_push(p);
5086 }
5087 ;
5088
5089it_id : {
5090 $$ = p->it_id;
5091 p->it_id = 0;
5092 }
5093 ;
5094
5095lambda : tLAMBDA[lpar]
5096 {
5097 token_info_push(p, "->", &@lpar);
5098 $$ = dyna_push(p);
5099 }[dyna]<vars>
5100 max_numparam numparam it_id allow_exits
5101 f_larglist[args]
5102 {
5103 CMDARG_PUSH(0);
5104 }
5105 lambda_body[body]
5106 {
5107 int max_numparam = p->max_numparam;
5108 ID it_id = p->it_id;
5109 p->lex.lpar_beg = $lpar;
5110 p->max_numparam = $max_numparam;
5111 p->it_id = $it_id;
5112 restore_block_exit(p, $allow_exits);
5113 CMDARG_POP();
5114 $args = args_with_numbered(p, $args, max_numparam, it_id);
5115 {
5116 YYLTYPE loc = code_loc_gen(&@lpar, &@body);
5117 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5118 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5119 nd_set_line($$, @args.end_pos.lineno);
5120 nd_set_first_loc($$, @lpar.beg_pos);
5121 xfree($body);
5122 }
5123 /*% ripper: lambda!($:args, $:body) %*/
5124 numparam_pop(p, $numparam);
5125 dyna_pop(p, $dyna);
5126 }
5127 ;
5128
5129f_larglist : '(' f_largs[args] opt_bv_decl ')'
5130 {
5131 p->ctxt.in_argdef = 0;
5132 $$ = $args;
5133 p->max_numparam = ORDINAL_PARAM;
5134 /*% ripper: paren!($:args) %*/
5135 }
5136 | f_largs[args]
5137 {
5138 p->ctxt.in_argdef = 0;
5139 if (!args_info_empty_p(&$args->nd_ainfo))
5140 p->max_numparam = ORDINAL_PARAM;
5141 $$ = $args;
5142 }
5143 ;
5144
5145lambda_body : tLAMBEG compstmt(stmts) '}'
5146 {
5147 token_info_pop(p, "}", &@3);
5148 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5149 /*% ripper: $:2 %*/
5150 }
5151 | keyword_do_LAMBDA
5152 {
5153 push_end_expect_token_locations(p, &@1.beg_pos);
5154 }
5155 bodystmt k_end
5156 {
5157 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5158 /*% ripper: $:3 %*/
5159 }
5160 ;
5161
5162do_block : k_do_block do_body k_end
5163 {
5164 $$ = $2;
5165 set_embraced_location($$, &@1, &@3);
5166 /*% ripper: $:2 %*/
5167 }
5168 ;
5169
5170block_call : command do_block
5171 {
5172 $$ = command_add_block(p, $1, $2, &@$);
5173 fixpos($$, $1);
5174 /*% ripper: method_add_block!($:1, $:2) %*/
5175 }
5176 | block_call call_op2 operation2 opt_paren_args
5177 {
5178 bool has_args = $4 != 0;
5179 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5180 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5181 /*% ripper: call!($:1, $:2, $:3) %*/
5182 if (has_args) {
5183 /*% ripper: method_add_arg!($:$, $:4) %*/
5184 }
5185 }
5186 | block_call call_op2 operation2 opt_paren_args brace_block
5187 {
5188 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5189 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5190 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5191 }
5192 | block_call call_op2 operation2 command_args do_block
5193 {
5194 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5195 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5196 }
5197 | block_call call_op2 paren_args
5198 {
5199 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5200 nd_set_line($$, @2.end_pos.lineno);
5201 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5202 }
5203 ;
5204
5205method_call : fcall paren_args
5206 {
5207 $1->nd_args = $2;
5208 $$ = (NODE *)$1;
5209 nd_set_last_loc($1, @2.end_pos);
5210 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5211 }
5212 | primary_value call_op operation2 opt_paren_args
5213 {
5214 bool has_args = $4 != 0;
5215 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5216 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5217 nd_set_line($$, @3.end_pos.lineno);
5218 /*% ripper: call!($:1, $:2, $:3) %*/
5219 if (has_args) {
5220 /*% ripper: method_add_arg!($:$, $:4) %*/
5221 }
5222 }
5223 | primary_value tCOLON2 operation2 paren_args
5224 {
5225 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5226 nd_set_line($$, @3.end_pos.lineno);
5227 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5228 }
5229 | primary_value tCOLON2 operation3
5230 {
5231 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5232 /*% ripper: call!($:1, $:2, $:3) %*/
5233 }
5234 | primary_value call_op2 paren_args
5235 {
5236 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5237 nd_set_line($$, @2.end_pos.lineno);
5238 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5239 }
5240 | keyword_super paren_args
5241 {
5242 rb_code_location_t lparen_loc = @2;
5243 rb_code_location_t rparen_loc = @2;
5244 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5245 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5246
5247 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5248 /*% ripper: super!($:2) %*/
5249 }
5250 | keyword_super
5251 {
5252 $$ = NEW_ZSUPER(&@$);
5253 /*% ripper: zsuper! %*/
5254 }
5255 | primary_value '[' opt_call_args rbracket
5256 {
5257 $$ = NEW_CALL($1, tAREF, $3, &@$);
5258 fixpos($$, $1);
5259 /*% ripper: aref!($:1, $:3) %*/
5260 }
5261 ;
5262
5263brace_block : '{' brace_body '}'
5264 {
5265 $$ = $2;
5266 set_embraced_location($$, &@1, &@3);
5267 /*% ripper: $:2 %*/
5268 }
5269 | k_do do_body k_end
5270 {
5271 $$ = $2;
5272 set_embraced_location($$, &@1, &@3);
5273 /*% ripper: $:2 %*/
5274 }
5275 ;
5276
5277brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5278 max_numparam numparam it_id allow_exits
5279 opt_block_param_def[args] compstmt(stmts)
5280 {
5281 int max_numparam = p->max_numparam;
5282 ID it_id = p->it_id;
5283 p->max_numparam = $max_numparam;
5284 p->it_id = $it_id;
5285 $args = args_with_numbered(p, $args, max_numparam, it_id);
5286 $$ = NEW_ITER($args, $compstmt, &@$);
5287 /*% ripper: brace_block!($:args, $:compstmt) %*/
5288 restore_block_exit(p, $allow_exits);
5289 numparam_pop(p, $numparam);
5290 dyna_pop(p, $dyna);
5291 }
5292 ;
5293
5294do_body : {
5295 $$ = dyna_push(p);
5296 CMDARG_PUSH(0);
5297 }[dyna]<vars>
5298 max_numparam numparam it_id allow_exits
5299 opt_block_param_def[args] bodystmt
5300 {
5301 int max_numparam = p->max_numparam;
5302 ID it_id = p->it_id;
5303 p->max_numparam = $max_numparam;
5304 p->it_id = $it_id;
5305 $args = args_with_numbered(p, $args, max_numparam, it_id);
5306 $$ = NEW_ITER($args, $bodystmt, &@$);
5307 /*% ripper: do_block!($:args, $:bodystmt) %*/
5308 CMDARG_POP();
5309 restore_block_exit(p, $allow_exits);
5310 numparam_pop(p, $numparam);
5311 dyna_pop(p, $dyna);
5312 }
5313 ;
5314
5315case_args : arg_value
5316 {
5317 check_literal_when(p, $arg_value, &@arg_value);
5318 $$ = NEW_LIST($arg_value, &@$);
5319 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5320 }
5321 | tSTAR arg_value
5322 {
5323 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5324 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5325 }
5326 | case_args[non_last_args] ',' arg_value
5327 {
5328 check_literal_when(p, $arg_value, &@arg_value);
5329 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5330 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5331 }
5332 | case_args[non_last_args] ',' tSTAR arg_value
5333 {
5334 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5335 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5336 }
5337 ;
5338
5339case_body : k_when case_args then
5340 compstmt(stmts)
5341 cases
5342 {
5343 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5344 fixpos($$, $2);
5345 /*% ripper: when!($:2, $:4, $:5) %*/
5346 }
5347 ;
5348
5349cases : opt_else
5350 | case_body
5351 ;
5352
5353p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5354p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5355
5356p_in_kwarg : {
5357 $$ = p->ctxt;
5358 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5359 p->command_start = FALSE;
5360 p->ctxt.in_kwarg = 1;
5361 p->ctxt.in_alt_pattern = 0;
5362 p->ctxt.capture_in_pattern = 0;
5363 }
5364 ;
5365
5366p_case_body : keyword_in
5367 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5368 p_top_expr[expr] then
5369 {
5370 pop_pktbl(p, $p_pktbl);
5371 pop_pvtbl(p, $p_pvtbl);
5372 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5373 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5374 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5375 }
5376 compstmt(stmts)
5377 p_cases[cases]
5378 {
5379 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5380 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5381 }
5382 ;
5383
5384p_cases : opt_else
5385 | p_case_body
5386 ;
5387
5388p_top_expr : p_top_expr_body
5389 | p_top_expr_body modifier_if expr_value
5390 {
5391 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5392 fixpos($$, $3);
5393 /*% ripper: if_mod!($:3, $:1) %*/
5394 }
5395 | p_top_expr_body modifier_unless expr_value
5396 {
5397 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5398 fixpos($$, $3);
5399 /*% ripper: unless_mod!($:3, $:1) %*/
5400 }
5401 ;
5402
5403p_top_expr_body : p_expr
5404 | p_expr ','
5405 {
5406 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5407 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5408 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5409 }
5410 | p_expr ',' p_args
5411 {
5412 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5413 nd_set_first_loc($$, @1.beg_pos);
5414 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5415 }
5416 | p_find
5417 {
5418 $$ = new_find_pattern(p, 0, $1, &@$);
5419 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5420 }
5421 | p_args_tail
5422 {
5423 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5424 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5425 }
5426 | p_kwargs
5427 {
5428 $$ = new_hash_pattern(p, 0, $1, &@$);
5429 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5430 }
5431 ;
5432
5433p_expr : p_as
5434 ;
5435
5436p_as : p_expr tASSOC p_variable
5437 {
5438 NODE *n = NEW_LIST($1, &@$);
5439 n = list_append(p, n, $3);
5440 $$ = new_hash(p, n, &@$);
5441 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5442 }
5443 | p_alt
5444 ;
5445
5446p_alt : p_alt[left] '|'[alt]
5447 {
5448 p->ctxt.in_alt_pattern = 1;
5449 }
5450 p_expr_basic[right]
5451 {
5452 if (p->ctxt.capture_in_pattern) {
5453 yyerror1(&@alt, "alternative pattern after variable capture");
5454 }
5455 p->ctxt.in_alt_pattern = 0;
5456 $$ = NEW_OR($left, $right, &@$, &@alt);
5457 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5458 }
5459 | p_expr_basic
5460 ;
5461
5462p_lparen : '(' p_pktbl
5463 {
5464 $$ = $2;
5465 /*% ripper: $:2 %*/
5466 }
5467 ;
5468
5469p_lbracket : '[' p_pktbl
5470 {
5471 $$ = $2;
5472 /*% ripper: $:2 %*/
5473 }
5474 ;
5475
5476p_expr_basic : p_value
5477 | p_variable
5478 | p_const p_lparen[p_pktbl] p_args rparen
5479 {
5480 pop_pktbl(p, $p_pktbl);
5481 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5482 nd_set_first_loc($$, @p_const.beg_pos);
5483 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5484 }
5485 | p_const p_lparen[p_pktbl] p_find rparen
5486 {
5487 pop_pktbl(p, $p_pktbl);
5488 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5489 nd_set_first_loc($$, @p_const.beg_pos);
5490 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5491 }
5492 | p_const p_lparen[p_pktbl] p_kwargs rparen
5493 {
5494 pop_pktbl(p, $p_pktbl);
5495 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5496 nd_set_first_loc($$, @p_const.beg_pos);
5497 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5498 }
5499 | p_const '(' rparen
5500 {
5501 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5502 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5503 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5504 }
5505 | p_const p_lbracket[p_pktbl] p_args rbracket
5506 {
5507 pop_pktbl(p, $p_pktbl);
5508 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5509 nd_set_first_loc($$, @p_const.beg_pos);
5510 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5511 }
5512 | p_const p_lbracket[p_pktbl] p_find rbracket
5513 {
5514 pop_pktbl(p, $p_pktbl);
5515 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5516 nd_set_first_loc($$, @p_const.beg_pos);
5517 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5518 }
5519 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5520 {
5521 pop_pktbl(p, $p_pktbl);
5522 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5523 nd_set_first_loc($$, @p_const.beg_pos);
5524 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5525 }
5526 | p_const '[' rbracket
5527 {
5528 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5529 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5530 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5531 }
5532 | tLBRACK p_args rbracket
5533 {
5534 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5535 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5536 }
5537 | tLBRACK p_find rbracket
5538 {
5539 $$ = new_find_pattern(p, 0, $p_find, &@$);
5540 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5541 }
5542 | tLBRACK rbracket
5543 {
5544 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5545 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5546 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5547 }
5548 | tLBRACE p_pktbl lex_ctxt[ctxt]
5549 {
5550 p->ctxt.in_kwarg = 0;
5551 }
5552 p_kwargs rbrace
5553 {
5554 pop_pktbl(p, $p_pktbl);
5555 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5556 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5557 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5558 }
5559 | tLBRACE rbrace
5560 {
5561 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5562 $$ = new_hash_pattern(p, 0, $$, &@$);
5563 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5564 }
5565 | tLPAREN p_pktbl p_expr rparen
5566 {
5567 pop_pktbl(p, $p_pktbl);
5568 $$ = $p_expr;
5569 /*% ripper: $:p_expr %*/
5570 }
5571 ;
5572
5573p_args : p_expr
5574 {
5575 NODE *pre_args = NEW_LIST($1, &@$);
5576 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5577 /*% ripper: [[$:1], Qnil, Qnil] %*/
5578 }
5579 | p_args_head
5580 {
5581 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5582 /*% ripper: [$:1, Qnil, Qnil] %*/
5583 }
5584 | p_args_head p_arg
5585 {
5586 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5587 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5588 }
5589 | p_args_head p_rest
5590 {
5591 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5592 /*% ripper: [$:1, $:2, Qnil] %*/
5593 }
5594 | p_args_head p_rest ',' p_args_post
5595 {
5596 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5597 /*% ripper: [$:1, $:2, $:4] %*/
5598 }
5599 | p_args_tail
5600 ;
5601
5602p_args_head : p_arg ','
5603 | p_args_head p_arg ','
5604 {
5605 $$ = list_concat($1, $2);
5606 /*% ripper: rb_ary_concat($:1, $:2) %*/
5607 }
5608 ;
5609
5610p_args_tail : p_rest
5611 {
5612 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5613 /*% ripper: [Qnil, $:1, Qnil] %*/
5614 }
5615 | p_rest ',' p_args_post
5616 {
5617 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5618 /*% ripper: [Qnil, $:1, $:3] %*/
5619 }
5620 ;
5621
5622p_find : p_rest ',' p_args_post ',' p_rest
5623 {
5624 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5625 /*% ripper: [$:1, $:3, $:5] %*/
5626 }
5627 ;
5628
5629
5630p_rest : tSTAR tIDENTIFIER
5631 {
5632 error_duplicate_pattern_variable(p, $2, &@2);
5633 /*% ripper: var_field!($:2) %*/
5634 $$ = assignable(p, $2, 0, &@$);
5635 }
5636 | tSTAR
5637 {
5638 $$ = 0;
5639 /*% ripper: var_field!(Qnil) %*/
5640 }
5641 ;
5642
5643p_args_post : p_arg
5644 | p_args_post ',' p_arg
5645 {
5646 $$ = list_concat($1, $3);
5647 /*% ripper: rb_ary_concat($:1, $:3) %*/
5648 }
5649 ;
5650
5651p_arg : p_expr
5652 {
5653 $$ = NEW_LIST($1, &@$);
5654 /*% ripper: [$:1] %*/
5655 }
5656 ;
5657
5658p_kwargs : p_kwarg ',' p_any_kwrest
5659 {
5660 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5661 /*% ripper: [$:1, $:3] %*/
5662 }
5663 | p_kwarg
5664 {
5665 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5666 /*% ripper: [$:1, Qnil] %*/
5667 }
5668 | p_kwarg ','
5669 {
5670 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5671 /*% ripper: [$:1, Qnil] %*/
5672 }
5673 | p_any_kwrest
5674 {
5675 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5676 /*% ripper: [[], $:1] %*/
5677 }
5678 ;
5679
5680p_kwarg : p_kw
5681 /*% ripper[brace]: [$:1] %*/
5682 | p_kwarg ',' p_kw
5683 {
5684 $$ = list_concat($1, $3);
5685 /*% ripper: rb_ary_push($:1, $:3) %*/
5686 }
5687 ;
5688
5689p_kw : p_kw_label p_expr
5690 {
5691 error_duplicate_pattern_key(p, $1, &@1);
5692 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5693 /*% ripper: [$:1, $:2] %*/
5694 }
5695 | p_kw_label
5696 {
5697 error_duplicate_pattern_key(p, $1, &@1);
5698 if ($1 && !is_local_id($1)) {
5699 yyerror1(&@1, "key must be valid as local variables");
5700 }
5701 error_duplicate_pattern_variable(p, $1, &@1);
5702 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5703 /*% ripper: [$:1, Qnil] %*/
5704 }
5705 ;
5706
5707p_kw_label : tLABEL
5708 | tSTRING_BEG string_contents tLABEL_END
5709 {
5710 YYLTYPE loc = code_loc_gen(&@1, &@3);
5711 if (!$2 || nd_type_p($2, NODE_STR)) {
5712 NODE *node = dsym_node(p, $2, &loc);
5713 $$ = rb_sym2id(rb_node_sym_string_val(node));
5714 }
5715 else {
5716 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5717 $$ = rb_intern_str(STR_NEW0());
5718 }
5719 /*% ripper: $:2 %*/
5720 }
5721 ;
5722
5723p_kwrest : kwrest_mark tIDENTIFIER
5724 {
5725 $$ = $2;
5726 /*% ripper: var_field!($:2) %*/
5727 }
5728 | kwrest_mark
5729 {
5730 $$ = 0;
5731 /*% ripper: Qnil %*/
5732 }
5733 ;
5734
5735p_kwnorest : kwrest_mark keyword_nil
5736 {
5737 $$ = 0;
5738 }
5739 ;
5740
5741p_any_kwrest : p_kwrest
5742 | p_kwnorest
5743 {
5744 $$ = idNil;
5745 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5746 }
5747 ;
5748
5749p_value : p_primitive
5750 | range_expr(p_primitive)
5751 | p_var_ref
5752 | p_expr_ref
5753 | p_const
5754 ;
5755
5756p_primitive : inline_primary
5757 | keyword_variable
5758 {
5759 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5760 /*% ripper: var_ref!($:1) %*/
5761 }
5762 | lambda
5763 ;
5764
5765p_variable : tIDENTIFIER
5766 {
5767 error_duplicate_pattern_variable(p, $1, &@1);
5768 /*% ripper: var_field!($:1) %*/
5769 $$ = assignable(p, $1, 0, &@$);
5770 }
5771 ;
5772
5773p_var_ref : '^' tIDENTIFIER
5774 {
5775 NODE *n = gettable(p, $2, &@$);
5776 if (!n) {
5777 n = NEW_ERROR(&@$);
5778 }
5779 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5780 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5781 }
5782 $$ = n;
5783 /*% ripper: var_ref!($:2) %*/
5784 }
5785 | '^' nonlocal_var
5786 {
5787 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5788 /*% ripper: var_ref!($:2) %*/
5789 }
5790 ;
5791
5792p_expr_ref : '^' tLPAREN expr_value rparen
5793 {
5794 $$ = NEW_BLOCK($3, &@$);
5795 /*% ripper: begin!($:3) %*/
5796 }
5797 ;
5798
5799p_const : tCOLON3 cname
5800 {
5801 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5802 /*% ripper: top_const_ref!($:2) %*/
5803 }
5804 | p_const tCOLON2 cname
5805 {
5806 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5807 /*% ripper: const_path_ref!($:1, $:3) %*/
5808 }
5809 | tCONSTANT
5810 {
5811 $$ = gettable(p, $1, &@$);
5812 /*% ripper: var_ref!($:1) %*/
5813 }
5814 ;
5815
5816opt_rescue : k_rescue exc_list exc_var then
5817 compstmt(stmts)
5818 opt_rescue
5819 {
5820 NODE *err = $3;
5821 if ($3) {
5822 err = NEW_ERRINFO(&@3);
5823 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5824 }
5825 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5826 if ($2) {
5827 fixpos($$, $2);
5828 }
5829 else if ($3) {
5830 fixpos($$, $3);
5831 }
5832 else {
5833 fixpos($$, $5);
5834 }
5835 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5836 }
5837 | none
5838 ;
5839
5840exc_list : arg_value
5841 {
5842 $$ = NEW_LIST($1, &@$);
5843 /*% ripper: rb_ary_new3(1, $:1) %*/
5844 }
5845 | mrhs
5846 {
5847 if (!($$ = splat_array($1))) $$ = $1;
5848 }
5849 | none
5850 ;
5851
5852exc_var : tASSOC lhs
5853 {
5854 $$ = $2;
5855 /*% ripper: $:2 %*/
5856 }
5857 | none
5858 ;
5859
5860opt_ensure : k_ensure stmts terms?
5861 {
5862 p->ctxt.in_rescue = $1.in_rescue;
5863 $$ = $2;
5864 void_expr(p, void_stmts(p, $$));
5865 /*% ripper: ensure!($:2) %*/
5866 }
5867 | none
5868 ;
5869
5870literal : numeric
5871 | symbol
5872 ;
5873
5874strings : string
5875 {
5876 if (!$1) {
5877 $$ = NEW_STR(STRING_NEW0(), &@$);
5878 }
5879 else {
5880 $$ = evstr2dstr(p, $1);
5881 }
5882 /*% ripper: $:1 %*/
5883 }
5884 ;
5885
5886string : tCHAR
5887 | string1
5888 | string string1
5889 {
5890 $$ = literal_concat(p, $1, $2, &@$);
5891 /*% ripper: string_concat!($:1, $:2) %*/
5892 }
5893 ;
5894
5895string1 : tSTRING_BEG string_contents tSTRING_END
5896 {
5897 $$ = heredoc_dedent(p, $2);
5898 if ($$) nd_set_loc($$, &@$);
5899 /*% ripper: $:2 %*/
5900 if (p->heredoc_indent > 0) {
5901 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5902 p->heredoc_indent = 0;
5903 }
5904 /*% ripper: string_literal!($:$) %*/
5905 }
5906 ;
5907
5908xstring : tXSTRING_BEG xstring_contents tSTRING_END
5909 {
5910 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5911 /*% ripper: $:2 %*/
5912 if (p->heredoc_indent > 0) {
5913 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5914 p->heredoc_indent = 0;
5915 }
5916 /*% ripper: xstring_literal!($:$) %*/
5917 }
5918 ;
5919
5920regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5921 {
5922 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5923 /*% ripper: regexp_literal!($:2, $:3) %*/
5924 }
5925 ;
5926
5927words : words(tWORDS_BEG, word_list)
5928 ;
5929
5930word_list : /* none */
5931 {
5932 $$ = 0;
5933 /*% ripper: words_new! %*/
5934 }
5935 | word_list word ' '+
5936 {
5937 $$ = list_append(p, $1, evstr2dstr(p, $2));
5938 /*% ripper: words_add!($:1, $:2) %*/
5939 }
5940 ;
5941
5942word : string_content
5943 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5944 | word string_content
5945 {
5946 $$ = literal_concat(p, $1, $2, &@$);
5947 /*% ripper: word_add!($:1, $:2) %*/
5948 }
5949 ;
5950
5951symbols : words(tSYMBOLS_BEG, symbol_list)
5952 ;
5953
5954symbol_list : /* none */
5955 {
5956 $$ = 0;
5957 /*% ripper: symbols_new! %*/
5958 }
5959 | symbol_list word ' '+
5960 {
5961 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5962 /*% ripper: symbols_add!($:1, $:2) %*/
5963 }
5964 ;
5965
5966qwords : words(tQWORDS_BEG, qword_list)
5967 ;
5968
5969qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5970 ;
5971
5972qword_list : /* none */
5973 {
5974 $$ = 0;
5975 /*% ripper: qwords_new! %*/
5976 }
5977 | qword_list tSTRING_CONTENT ' '+
5978 {
5979 $$ = list_append(p, $1, $2);
5980 /*% ripper: qwords_add!($:1, $:2) %*/
5981 }
5982 ;
5983
5984qsym_list : /* none */
5985 {
5986 $$ = 0;
5987 /*% ripper: qsymbols_new! %*/
5988 }
5989 | qsym_list tSTRING_CONTENT ' '+
5990 {
5991 $$ = symbol_append(p, $1, $2);
5992 /*% ripper: qsymbols_add!($:1, $:2) %*/
5993 }
5994 ;
5995
5996string_contents : /* none */
5997 {
5998 $$ = 0;
5999 /*% ripper: string_content! %*/
6000 }
6001 | string_contents string_content
6002 {
6003 $$ = literal_concat(p, $1, $2, &@$);
6004 /*% ripper: string_add!($:1, $:2) %*/
6005 }
6006 ;
6007
6008xstring_contents: /* none */
6009 {
6010 $$ = 0;
6011 /*% ripper: xstring_new! %*/
6012 }
6013 | xstring_contents string_content
6014 {
6015 $$ = literal_concat(p, $1, $2, &@$);
6016 /*% ripper: xstring_add!($:1, $:2) %*/
6017 }
6018 ;
6019
6020regexp_contents : /* none */
6021 {
6022 $$ = 0;
6023 /*% ripper: regexp_new! %*/
6024 }
6025 | regexp_contents string_content
6026 {
6027 NODE *head = $1, *tail = $2;
6028 if (!head) {
6029 $$ = tail;
6030 }
6031 else if (!tail) {
6032 $$ = head;
6033 }
6034 else {
6035 switch (nd_type(head)) {
6036 case NODE_STR:
6037 head = str2dstr(p, head);
6038 break;
6039 case NODE_DSTR:
6040 break;
6041 default:
6042 head = list_append(p, NEW_DSTR(0, &@$), head);
6043 break;
6044 }
6045 $$ = list_append(p, head, tail);
6046 }
6047 /*% ripper: regexp_add!($:1, $:2) %*/
6048 }
6049 ;
6050
6051string_content : tSTRING_CONTENT[content]
6052 /*% ripper[brace]: $:content %*/
6053 | tSTRING_DVAR[state]
6054 {
6055 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6056 $$ = p->lex.strterm;
6057 p->lex.strterm = 0;
6058 SET_LEX_STATE(EXPR_BEG);
6059 }[strterm]<strterm>
6060 string_dvar[dvar]
6061 {
6062 p->lex.strterm = $strterm;
6063 $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC);
6064 nd_set_line($$, @dvar.end_pos.lineno);
6065 /*% ripper: string_dvar!($:dvar) %*/
6066 }
6067 | tSTRING_DBEG[state]
6068 {
6069 CMDARG_PUSH(0);
6070 COND_PUSH(0);
6071 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6072 $$ = p->lex.strterm;
6073 p->lex.strterm = 0;
6074 SET_LEX_STATE(EXPR_BEG);
6075 }[term]<strterm>
6076 {
6077 $$ = p->lex.brace_nest;
6078 p->lex.brace_nest = 0;
6079 }[brace]<num>
6080 {
6081 $$ = p->lex.lpar_beg;
6082 p->lex.lpar_beg = -1;
6083 }[lpar]<num>
6084 {
6085 $$ = p->heredoc_indent;
6086 p->heredoc_indent = 0;
6087 }[indent]<num>
6088 compstmt(stmts) string_dend
6089 {
6090 COND_POP();
6091 CMDARG_POP();
6092 p->lex.strterm = $term;
6093 SET_LEX_STATE($state);
6094 p->lex.brace_nest = $brace;
6095 p->lex.lpar_beg = $lpar;
6096 p->heredoc_indent = $indent;
6097 p->heredoc_line_indent = -1;
6098 if ($compstmt) nd_unset_fl_newline($compstmt);
6099 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6100 /*% ripper: string_embexpr!($:compstmt) %*/
6101 }
6102 ;
6103
6104string_dend : tSTRING_DEND
6105 | END_OF_INPUT
6106 ;
6107
6108string_dvar : nonlocal_var
6109 {
6110 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6111 /*% ripper: var_ref!($:1) %*/
6112 }
6113 | backref
6114 ;
6115
6116symbol : ssym
6117 | dsym
6118 ;
6119
6120ssym : tSYMBEG sym
6121 {
6122 SET_LEX_STATE(EXPR_END);
6123 VALUE str = rb_id2str($2);
6124 /*
6125 * TODO:
6126 * set_yylval_noname sets invalid id to yylval.
6127 * This branch can be removed once yylval is changed to
6128 * hold lexed string.
6129 */
6130 if (!str) str = STR_NEW0();
6131 $$ = NEW_SYM(str, &@$);
6132 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6133 }
6134 ;
6135
6136sym : fname
6137 | nonlocal_var
6138 ;
6139
6140dsym : tSYMBEG string_contents tSTRING_END
6141 {
6142 SET_LEX_STATE(EXPR_END);
6143 $$ = dsym_node(p, $2, &@$);
6144 /*% ripper: dyna_symbol!($:2) %*/
6145 }
6146 ;
6147
6148numeric : simple_numeric
6149 | tUMINUS_NUM simple_numeric %prec tLOWEST
6150 {
6151 $$ = $2;
6152 negate_lit(p, $$, &@$);
6153 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6154 }
6155 ;
6156
6157simple_numeric : tINTEGER
6158 | tFLOAT
6159 | tRATIONAL
6160 | tIMAGINARY
6161 ;
6162
6163nonlocal_var : tIVAR
6164 | tGVAR
6165 | tCVAR
6166 ;
6167
6168user_variable : ident_or_const
6169 | nonlocal_var
6170 ;
6171
6172keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6173 | keyword_self {$$ = KWD2EID(self, $1);}
6174 | keyword_true {$$ = KWD2EID(true, $1);}
6175 | keyword_false {$$ = KWD2EID(false, $1);}
6176 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6177 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6178 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6179 ;
6180
6181var_ref : user_variable
6182 {
6183 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6184 if (ifdef_ripper(id_is_var(p, $1), false)) {
6185 /*% ripper: var_ref!($:1) %*/
6186 }
6187 else {
6188 /*% ripper: vcall!($:1) %*/
6189 }
6190 }
6191 | keyword_variable
6192 {
6193 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6194 /*% ripper: var_ref!($:1) %*/
6195 }
6196 ;
6197
6198var_lhs : user_or_keyword_variable
6199 {
6200 /*% ripper: var_field!($:1) %*/
6201 $$ = assignable(p, $1, 0, &@$);
6202 }
6203 ;
6204
6205backref : tNTH_REF
6206 | tBACK_REF
6207 ;
6208
6209superclass : '<'
6210 {
6211 SET_LEX_STATE(EXPR_BEG);
6212 p->command_start = TRUE;
6213 }
6214 expr_value term
6215 {
6216 $$ = $3;
6217 /*% ripper: $:3 %*/
6218 }
6219 | none
6220 ;
6221
6222f_opt_paren_args: f_paren_args
6223 | f_empty_arg
6224 {
6225 p->ctxt.in_argdef = 0;
6226 }
6227 ;
6228
6229f_empty_arg : /* none */
6230 {
6231 $$ = new_empty_args_tail(p, &@$);
6232 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6233 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6234 }
6235 ;
6236
6237f_paren_args : '(' f_args rparen
6238 {
6239 $$ = $2;
6240 /*% ripper: paren!($:2) %*/
6241 SET_LEX_STATE(EXPR_BEG);
6242 p->command_start = TRUE;
6243 p->ctxt.in_argdef = 0;
6244 }
6245 ;
6246
6247f_arglist : f_paren_args
6248 | {
6249 $$ = p->ctxt;
6250 p->ctxt.in_kwarg = 1;
6251 p->ctxt.in_argdef = 1;
6252 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6253 }<ctxt>
6254 f_args term
6255 {
6256 p->ctxt.in_kwarg = $1.in_kwarg;
6257 p->ctxt.in_argdef = 0;
6258 $$ = $2;
6259 SET_LEX_STATE(EXPR_BEG);
6260 p->command_start = TRUE;
6261 /*% ripper: $:2 %*/
6262 }
6263 ;
6264
6265args_tail : args_tail_basic(arg_value, opt_comma)
6266 | args_forward
6267 {
6268 add_forwarding_args(p);
6269 $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward);
6270 $$->nd_ainfo.forwarding = 1;
6271 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6272 }
6273 ;
6274
6275largs_tail : args_tail_basic(arg_value, none)
6276 | args_forward
6277 {
6278 yyerror1(&@args_forward, "unexpected ... in lambda argument");
6279 $$ = new_args_tail(p, 0, 0, 0, &@args_forward);
6280 $$->nd_ainfo.forwarding = 1;
6281 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6282 }
6283 ;
6284
6285%rule args-list(value, tail) <node_args>
6286 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6287 {
6288 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6289 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6290 }
6291 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6292 {
6293 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6294 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6295 }
6296 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6297 {
6298 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6299 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6300 }
6301 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6302 {
6303 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6304 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6305 }
6306 | f_arg[pre] ',' f_rest_arg[rest] tail
6307 {
6308 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6309 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6310 }
6311 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6312 {
6313 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6314 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6315 }
6316 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6317 {
6318 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6319 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6320 }
6321 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6322 {
6323 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6324 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6325 }
6326 | f_opt_arg(value)[opt] tail
6327 {
6328 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6329 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6330 }
6331 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6332 {
6333 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6334 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6335 }
6336 | f_rest_arg[rest] tail
6337 {
6338 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6339 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6340 }
6341 | f_rest_arg[rest] ',' f_arg[post] tail
6342 {
6343 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6344 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6345 }
6346 ;
6347
6348%rule tail-only-args(tail) <node_args>
6349 : tail
6350 {
6351 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6352 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6353 }
6354 ;
6355
6356%rule f_args-list(tail, trailing) <node_args>
6357 : args-list(arg_value, opt_args_tail(tail, trailing))
6358 | f_arg[pre] opt_args_tail(tail, trailing)[tail]
6359 {
6360 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6361 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6362 }
6363 | tail-only-args(tail)
6364 | f_empty_arg
6365 ;
6366
6367f_args : f_args-list(args_tail, opt_comma)
6368 ;
6369
6370f_largs : f_args-list(largs_tail, none)
6371 ;
6372
6373args_forward : tBDOT3
6374 {
6375 $$ = idFWD_KWREST;
6376 /*% ripper: args_forward! %*/
6377 }
6378 ;
6379
6380f_bad_arg : tCONSTANT
6381 {
6382 static const char mesg[] = "formal argument cannot be a constant";
6383 /*%%%*/
6384 yyerror1(&@1, mesg);
6385 /*% %*/
6386 $$ = 0;
6387 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6388 }
6389 | tIVAR
6390 {
6391 static const char mesg[] = "formal argument cannot be an instance variable";
6392 /*%%%*/
6393 yyerror1(&@1, mesg);
6394 /*% %*/
6395 $$ = 0;
6396 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6397 }
6398 | tGVAR
6399 {
6400 static const char mesg[] = "formal argument cannot be a global variable";
6401 /*%%%*/
6402 yyerror1(&@1, mesg);
6403 /*% %*/
6404 $$ = 0;
6405 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6406 }
6407 | tCVAR
6408 {
6409 static const char mesg[] = "formal argument cannot be a class variable";
6410 /*%%%*/
6411 yyerror1(&@1, mesg);
6412 /*% %*/
6413 $$ = 0;
6414 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6415 }
6416 ;
6417
6418f_norm_arg : f_bad_arg
6419 | tIDENTIFIER
6420 {
6421 VALUE e = formal_argument_error(p, $$ = $1);
6422 if (e) {
6423 /*% ripper[error]: param_error!(?e, $:1) %*/
6424 }
6425 p->max_numparam = ORDINAL_PARAM;
6426 }
6427 ;
6428
6429f_arg_asgn : f_norm_arg
6430 {
6431 arg_var(p, $1);
6432 $$ = $1;
6433 }
6434 ;
6435
6436f_arg_item : f_arg_asgn
6437 {
6438 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6439 /*% ripper: $:1 %*/
6440 }
6441 | tLPAREN f_margs rparen
6442 {
6443 ID tid = internal_id(p);
6444 YYLTYPE loc;
6445 loc.beg_pos = @2.beg_pos;
6446 loc.end_pos = @2.beg_pos;
6447 arg_var(p, tid);
6448 if (dyna_in_block(p)) {
6449 $2->nd_value = NEW_DVAR(tid, &loc);
6450 }
6451 else {
6452 $2->nd_value = NEW_LVAR(tid, &loc);
6453 }
6454 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6455 $$->nd_next = (NODE *)$2;
6456 /*% ripper: mlhs_paren!($:2) %*/
6457 }
6458 ;
6459
6460f_arg : f_arg_item
6461 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6462 | f_arg ',' f_arg_item
6463 {
6464 $$ = $1;
6465 $$->nd_plen++;
6466 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6467 rb_discard_node(p, (NODE *)$3);
6468 /*% ripper: rb_ary_push($:1, $:3) %*/
6469 }
6470 ;
6471
6472
6473f_label : tLABEL
6474 {
6475 VALUE e = formal_argument_error(p, $$ = $1);
6476 if (e) {
6477 $$ = 0;
6478 /*% ripper[error]: param_error!(?e, $:1) %*/
6479 }
6480 /*
6481 * Workaround for Prism::ParseTest#test_filepath for
6482 * "unparser/corpus/literal/def.txt"
6483 *
6484 * See the discussion on https://github.com/ruby/ruby/pull/9923
6485 */
6486 arg_var(p, ifdef_ripper(0, $1));
6487 /*% ripper: $:1 %*/
6488 p->max_numparam = ORDINAL_PARAM;
6489 p->ctxt.in_argdef = 0;
6490 }
6491 ;
6492
6493kwrest_mark : tPOW
6494 | tDSTAR
6495 ;
6496
6497f_no_kwarg : p_kwnorest
6498 {
6499 /*% ripper: nokw_param!(Qnil) %*/
6500 }
6501 ;
6502
6503f_kwrest : kwrest_mark tIDENTIFIER
6504 {
6505 arg_var(p, shadowing_lvar(p, $2));
6506 $$ = $2;
6507 /*% ripper: kwrest_param!($:2) %*/
6508 }
6509 | kwrest_mark
6510 {
6511 arg_var(p, idFWD_KWREST);
6512 $$ = idFWD_KWREST;
6513 /*% ripper: kwrest_param!(Qnil) %*/
6514 }
6515 ;
6516
6517restarg_mark : '*'
6518 | tSTAR
6519 ;
6520
6521f_rest_arg : restarg_mark tIDENTIFIER
6522 {
6523 arg_var(p, shadowing_lvar(p, $2));
6524 $$ = $2;
6525 /*% ripper: rest_param!($:2) %*/
6526 }
6527 | restarg_mark
6528 {
6529 arg_var(p, idFWD_REST);
6530 $$ = idFWD_REST;
6531 /*% ripper: rest_param!(Qnil) %*/
6532 }
6533 ;
6534
6535blkarg_mark : '&'
6536 | tAMPER
6537 ;
6538
6539f_block_arg : blkarg_mark tIDENTIFIER
6540 {
6541 arg_var(p, shadowing_lvar(p, $2));
6542 $$ = $2;
6543 /*% ripper: blockarg!($:2) %*/
6544 }
6545 | blkarg_mark keyword_nil
6546 {
6547 $$ = idNil;
6548 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6549 }
6550 | blkarg_mark
6551 {
6552 arg_var(p, idFWD_BLOCK);
6553 $$ = idFWD_BLOCK;
6554 /*% ripper: blockarg!(Qnil) %*/
6555 }
6556 ;
6557
6558opt_comma : ','?
6559 {
6560 $$ = 0;
6561 /*% ripper: Qnil %*/
6562 }
6563 ;
6564
6565
6566singleton : value_expr(singleton_expr)
6567 {
6568 NODE *expr = last_expr_node($1);
6569 switch (nd_type(expr)) {
6570 case NODE_STR:
6571 case NODE_DSTR:
6572 case NODE_XSTR:
6573 case NODE_DXSTR:
6574 case NODE_REGX:
6575 case NODE_DREGX:
6576 case NODE_SYM:
6577 case NODE_LINE:
6578 case NODE_FILE:
6579 case NODE_ENCODING:
6580 case NODE_INTEGER:
6581 case NODE_FLOAT:
6582 case NODE_RATIONAL:
6583 case NODE_IMAGINARY:
6584 case NODE_DSYM:
6585 case NODE_LIST:
6586 case NODE_ZLIST:
6587 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6588 break;
6589 default:
6590 break;
6591 }
6592 $$ = $1;
6593 }
6594 ;
6595
6596singleton_expr : var_ref
6597 | '('
6598 {
6599 SET_LEX_STATE(EXPR_BEG);
6600 p->ctxt.in_argdef = 0;
6601 }
6602 expr rparen
6603 {
6604 p->ctxt.in_argdef = 1;
6605 $$ = $3;
6606 /*% ripper: paren!($:3) %*/
6607 }
6608 ;
6609
6610assoc_list : none
6611 | assocs trailer
6612 {
6613 $$ = $1;
6614 /*% ripper: assoclist_from_args!($:1) %*/
6615 }
6616 ;
6617
6618assocs : assoc
6619 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6620 | assocs ',' assoc
6621 {
6622 NODE *assocs = $1;
6623 NODE *tail = $3;
6624 if (!assocs) {
6625 assocs = tail;
6626 }
6627 else if (tail) {
6628 NODE *n = RNODE_LIST(tail)->nd_next;
6629 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6630 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6631 /* DSTAR */
6632 tail = RNODE_HASH(n)->nd_head;
6633 }
6634 if (tail) {
6635 assocs = list_concat(assocs, tail);
6636 }
6637 }
6638 $$ = assocs;
6639 /*% ripper: rb_ary_push($:1, $:3) %*/
6640 }
6641 ;
6642
6643assoc : arg_value tASSOC arg_value
6644 {
6645 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6646 /*% ripper: assoc_new!($:1, $:3) %*/
6647 }
6648 | tLABEL arg_value
6649 {
6650 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6651 /*% ripper: assoc_new!($:1, $:2) %*/
6652 }
6653 | tLABEL
6654 {
6655 NODE *val = gettable(p, $1, &@$);
6656 if (!val) val = NEW_ERROR(&@$);
6657 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6658 /*% ripper: assoc_new!($:1, Qnil) %*/
6659 }
6660 | tSTRING_BEG string_contents tLABEL_END arg_value
6661 {
6662 YYLTYPE loc = code_loc_gen(&@1, &@3);
6663 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6664 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6665 }
6666 | tDSTAR arg_value
6667 {
6668 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6669 /*% ripper: assoc_splat!($:2) %*/
6670 }
6671 | tDSTAR
6672 {
6673 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6674 $$ = list_append(p, NEW_LIST(0, &@$),
6675 NEW_LVAR(idFWD_KWREST, &@$));
6676 /*% ripper: assoc_splat!(Qnil) %*/
6677 }
6678 ;
6679
6680%rule %inline operation : ident_or_const
6681 | tFID
6682 ;
6683
6684operation2 : operation
6685 | op
6686 ;
6687
6688operation3 : tIDENTIFIER
6689 | tFID
6690 | op
6691 ;
6692
6693dot_or_colon : '.'
6694 | tCOLON2
6695 ;
6696
6697call_op : '.'
6698 | tANDDOT
6699 ;
6700
6701call_op2 : call_op
6702 | tCOLON2
6703 ;
6704
6705rparen : '\n'? ')'
6706 ;
6707
6708rbracket : '\n'? ']'
6709 ;
6710
6711rbrace : '\n'? '}'
6712 ;
6713
6714trailer : '\n'?
6715 | ','
6716 ;
6717
6718term : ';'
6719 {
6720 yyerrok;
6721 token_flush(p);
6722 if (p->ctxt.in_defined) {
6723 p->ctxt.has_trailing_semicolon = 1;
6724 }
6725 }
6726 | '\n'
6727 {
6728 @$.end_pos = @$.beg_pos;
6729 token_flush(p);
6730 }
6731 ;
6732
6733terms : term
6734 | terms ';' {yyerrok;}
6735 ;
6736
6737none : /* none */
6738 {
6739 $$ = 0;
6740 /*% ripper: Qnil %*/
6741 }
6742 ;
6743%%
6744# undef p
6745# undef yylex
6746# undef yylval
6747# define yylval (*p->lval)
6748
6749static int regx_options(struct parser_params*);
6750static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6751static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6752static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6753static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6754
6755#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6756
6757# define set_yylval_node(x) { \
6758 YYLTYPE _cur_loc; \
6759 rb_parser_set_location(p, &_cur_loc); \
6760 yylval.node = (x); \
6761 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6762}
6763# define set_yylval_str(x) \
6764do { \
6765 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6766 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6767} while(0)
6768# define set_yylval_num(x) { \
6769 yylval.num = (x); \
6770 set_parser_s_value(x); \
6771}
6772# define set_yylval_id(x) (yylval.id = (x))
6773# define set_yylval_name(x) { \
6774 (yylval.id = (x)); \
6775 set_parser_s_value(ID2SYM(x)); \
6776}
6777# define yylval_id() (yylval.id)
6778
6779#define set_yylval_noname() set_yylval_id(keyword_nil)
6780#define has_delayed_token(p) (p->delayed.token != NULL)
6781
6782#ifndef RIPPER
6783#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6784#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6785
6786static bool
6787parser_has_token(struct parser_params *p)
6788{
6789 const char *const pcur = p->lex.pcur;
6790 const char *const ptok = p->lex.ptok;
6791 if (p->keep_tokens && (pcur < ptok)) {
6792 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6793 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6794 }
6795 return pcur > ptok;
6796}
6797
6798static const char *
6799escaped_char(int c)
6800{
6801 switch (c) {
6802 case '"': return "\\\"";
6803 case '\\': return "\\\\";
6804 case '\0': return "\\0";
6805 case '\n': return "\\n";
6806 case '\r': return "\\r";
6807 case '\t': return "\\t";
6808 case '\f': return "\\f";
6809 case '\013': return "\\v";
6810 case '\010': return "\\b";
6811 case '\007': return "\\a";
6812 case '\033': return "\\e";
6813 case '\x7f': return "\\c?";
6814 }
6815 return NULL;
6816}
6817
6818static rb_parser_string_t *
6819rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6820{
6821 rb_encoding *enc = p->enc;
6822 const char *ptr = str->ptr;
6823 const char *pend = ptr + str->len;
6824 const char *prev = ptr;
6825 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6826 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6827
6828 while (ptr < pend) {
6829 unsigned int c;
6830 const char *cc;
6831 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6832 if (!MBCLEN_CHARFOUND_P(n)) {
6833 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6834 n = rb_enc_mbminlen(enc);
6835 if (pend < ptr + n)
6836 n = (int)(pend - ptr);
6837 while (n--) {
6838 c = ((unsigned char)*ptr >> 4) & 0x0f;
6839 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6840 c = *ptr & 0x0f;
6841 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6842 parser_str_cat(result, charbuf, 4);
6843 prev = ++ptr;
6844 }
6845 continue;
6846 }
6847 n = MBCLEN_CHARFOUND_LEN(n);
6848 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6849 ptr += n;
6850 cc = escaped_char(c);
6851 if (cc) {
6852 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6853 parser_str_cat_cstr(result, cc);
6854 prev = ptr;
6855 }
6856 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6857 }
6858 else {
6859 if (ptr - n > prev) {
6860 parser_str_cat(result, prev, ptr - n - prev);
6861 prev = ptr - n;
6862 }
6863 parser_str_cat(result, prev, ptr - prev);
6864 prev = ptr;
6865 }
6866 }
6867 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6868
6869 return result;
6870}
6871
6872static void
6873parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6874{
6875 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6876 token->id = p->token_id;
6877 token->type_name = parser_token2char(p, t);
6878 token->str = str;
6879 token->loc.beg_pos = p->yylloc->beg_pos;
6880 token->loc.end_pos = p->yylloc->end_pos;
6881 rb_parser_ary_push_ast_token(p, p->tokens, token);
6882 p->token_id++;
6883
6884 if (p->debug) {
6885 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6886 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6887 line, token->id, token->type_name, str_escaped->ptr,
6888 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6889 token->loc.end_pos.lineno, token->loc.end_pos.column);
6890 rb_parser_string_free(p, str_escaped);
6891 }
6892}
6893
6894static void
6895parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6896{
6897 debug_token_line(p, "parser_dispatch_scan_event", line);
6898
6899 if (!parser_has_token(p)) return;
6900
6901 RUBY_SET_YYLLOC(*p->yylloc);
6902
6903 if (p->keep_tokens) {
6904 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6905 parser_append_tokens(p, str, t, line);
6906 }
6907
6908 token_flush(p);
6909}
6910
6911#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6912static void
6913parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6914{
6915 debug_token_line(p, "parser_dispatch_delayed_token", line);
6916
6917 if (!has_delayed_token(p)) return;
6918
6919 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6920
6921 if (p->keep_tokens) {
6922 /* p->delayed.token is freed by rb_parser_tokens_free */
6923 parser_append_tokens(p, p->delayed.token, t, line);
6924 }
6925 else {
6926 rb_parser_string_free(p, p->delayed.token);
6927 }
6928
6929 p->delayed.token = NULL;
6930}
6931#else
6932#define literal_flush(p, ptr) ((void)(ptr))
6933
6934static int
6935ripper_has_scan_event(struct parser_params *p)
6936{
6937 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6938 return p->lex.pcur > p->lex.ptok;
6939}
6940
6941static VALUE
6942ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6943{
6944 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6945 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6946 RUBY_SET_YYLLOC(*p->yylloc);
6947 token_flush(p);
6948 return rval;
6949}
6950
6951static void
6952ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6953{
6954 if (!ripper_has_scan_event(p)) return;
6955
6956 set_parser_s_value(ripper_scan_event_val(p, t));
6957}
6958#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6959
6960static void
6961ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6962{
6963 /* save and adjust the location to delayed token for callbacks */
6964 int saved_line = p->ruby_sourceline;
6965 const char *saved_tokp = p->lex.ptok;
6966 VALUE s_value, str;
6967
6968 if (!has_delayed_token(p)) return;
6969 p->ruby_sourceline = p->delayed.beg_line;
6970 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6971 str = rb_str_new_mutable_parser_string(p->delayed.token);
6972 rb_parser_string_free(p, p->delayed.token);
6973 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6974 set_parser_s_value(s_value);
6975 p->delayed.token = NULL;
6976 p->ruby_sourceline = saved_line;
6977 p->lex.ptok = saved_tokp;
6978}
6979#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6980#endif /* RIPPER */
6981
6982static inline int
6983is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6984{
6985 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6986}
6987
6988static inline bool
6989peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
6990{
6991 const char *ptr = p->lex.pcur + at;
6992 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
6993 if (memcmp(ptr, str, len)) return false;
6994 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
6995 switch (ptr[len]) {
6996 case '!': case '?': return false;
6997 }
6998 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
6999}
7000
7001static inline int
7002parser_is_identchar(struct parser_params *p)
7003{
7004 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7005}
7006
7007static inline int
7008parser_isascii(struct parser_params *p)
7009{
7010 return ISASCII(*(p->lex.pcur-1));
7011}
7012
7013static void
7014token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7015{
7016 int column = 1, nonspc = 0, i;
7017 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7018 if (*ptr == '\t') {
7019 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7020 }
7021 column++;
7022 if (*ptr != ' ' && *ptr != '\t') {
7023 nonspc = 1;
7024 }
7025 }
7026
7027 ptinfo->beg = loc->beg_pos;
7028 ptinfo->indent = column;
7029 ptinfo->nonspc = nonspc;
7030}
7031
7032static void
7033token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7034{
7035 token_info *ptinfo;
7036
7037 if (!p->token_info_enabled) return;
7038 ptinfo = ALLOC(token_info);
7039 ptinfo->token = token;
7040 ptinfo->next = p->token_info;
7041 token_info_setup(ptinfo, p->lex.pbeg, loc);
7042
7043 p->token_info = ptinfo;
7044}
7045
7046static void
7047token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7048{
7049 token_info *ptinfo_beg = p->token_info;
7050
7051 if (!ptinfo_beg) return;
7052
7053 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7054 token_info_warn(p, token, ptinfo_beg, 1, loc);
7055
7056 p->token_info = ptinfo_beg->next;
7057 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7058}
7059
7060static void
7061token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7062{
7063 token_info *ptinfo_beg = p->token_info;
7064
7065 if (!ptinfo_beg) return;
7066 p->token_info = ptinfo_beg->next;
7067
7068 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7069 ptinfo_beg->beg.column != beg_pos.column ||
7070 strcmp(ptinfo_beg->token, token)) {
7071 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7072 beg_pos.lineno, beg_pos.column, token,
7073 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7074 ptinfo_beg->token);
7075 }
7076
7077 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7078}
7079
7080static void
7081token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7082{
7083 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7084 if (!p->token_info_enabled) return;
7085 if (!ptinfo_beg) return;
7086 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7087 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7088 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7089 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7090 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7091 rb_warn3L(ptinfo_end->beg.lineno,
7092 "mismatched indentations at '%s' with '%s' at %d",
7093 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7094}
7095
7096static int
7097parser_precise_mbclen(struct parser_params *p, const char *ptr)
7098{
7099 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7100 if (!MBCLEN_CHARFOUND_P(len)) {
7101 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7102 return -1;
7103 }
7104 return len;
7105}
7106
7107#ifndef RIPPER
7108static inline void
7109parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7110{
7111 rb_parser_string_t *str;
7112 int lineno = p->ruby_sourceline;
7113 if (!yylloc) {
7114 return;
7115 }
7116 else if (yylloc->beg_pos.lineno == lineno) {
7117 str = p->lex.lastline;
7118 }
7119 else {
7120 return;
7121 }
7122 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7123}
7124
7125static int
7126parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7127{
7128#if 0
7129 YYLTYPE current;
7130
7131 if (!yylloc) {
7132 yylloc = RUBY_SET_YYLLOC(current);
7133 }
7134 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7135 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7136 yylloc = 0;
7137 }
7138#endif
7139 parser_compile_error(p, yylloc, "%s", msg);
7140 parser_show_error_line(p, yylloc);
7141 return 0;
7142}
7143
7144static int
7145parser_yyerror0(struct parser_params *p, const char *msg)
7146{
7147 YYLTYPE current;
7148 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7149}
7150
7151void
7152ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7153{
7154 VALUE mesg;
7155 const int max_line_margin = 30;
7156 const char *ptr, *ptr_end, *pt, *pb;
7157 const char *pre = "", *post = "", *pend;
7158 const char *code = "", *caret = "";
7159 const char *lim;
7160 const char *const pbeg = PARSER_STRING_PTR(str);
7161 char *buf;
7162 long len;
7163 int i;
7164
7165 if (!yylloc) return;
7166 pend = rb_parser_string_end(str);
7167 if (pend > pbeg && pend[-1] == '\n') {
7168 if (--pend > pbeg && pend[-1] == '\r') --pend;
7169 }
7170
7171 pt = pend;
7172 if (lineno == yylloc->end_pos.lineno &&
7173 (pend - pbeg) > yylloc->end_pos.column) {
7174 pt = pbeg + yylloc->end_pos.column;
7175 }
7176
7177 ptr = ptr_end = pt;
7178 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7179 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7180
7181 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7182 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7183
7184 len = ptr_end - ptr;
7185 if (len > 4) {
7186 if (ptr > pbeg) {
7187 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7188 if (ptr > pbeg) pre = "...";
7189 }
7190 if (ptr_end < pend) {
7191 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7192 if (ptr_end < pend) post = "...";
7193 }
7194 }
7195 pb = pbeg;
7196 if (lineno == yylloc->beg_pos.lineno) {
7197 pb += yylloc->beg_pos.column;
7198 if (pb > pt) pb = pt;
7199 }
7200 if (pb < ptr) pb = ptr;
7201 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7202 return;
7203 }
7204 if (RTEST(errbuf)) {
7205 mesg = rb_attr_get(errbuf, idMesg);
7206 if (char_at_end(p, mesg, '\n') != '\n')
7207 rb_str_cat_cstr(mesg, "\n");
7208 }
7209 else {
7210 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7211 }
7212 if (!errbuf && rb_stderr_tty_p()) {
7213#define CSI_BEGIN "\033["
7214#define CSI_SGR "m"
7215 rb_str_catf(mesg,
7216 CSI_BEGIN""CSI_SGR"%s" /* pre */
7217 CSI_BEGIN"1"CSI_SGR"%.*s"
7218 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7219 CSI_BEGIN";1"CSI_SGR"%.*s"
7220 CSI_BEGIN""CSI_SGR"%s" /* post */
7221 "\n",
7222 pre,
7223 (int)(pb - ptr), ptr,
7224 (int)(pt - pb), pb,
7225 (int)(ptr_end - pt), pt,
7226 post);
7227 }
7228 else {
7229 char *p2;
7230
7231 len = ptr_end - ptr;
7232 lim = pt < pend ? pt : pend;
7233 i = (int)(lim - ptr);
7234 buf = ALLOCA_N(char, i+2);
7235 code = ptr;
7236 caret = p2 = buf;
7237 if (ptr <= pb) {
7238 while (ptr < pb) {
7239 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7240 }
7241 *p2++ = '^';
7242 ptr++;
7243 }
7244 if (lim > ptr) {
7245 memset(p2, '~', (lim - ptr));
7246 p2 += (lim - ptr);
7247 }
7248 *p2 = '\0';
7249 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7250 pre, (int)len, code, post,
7251 pre, caret);
7252 }
7253 if (!errbuf) rb_write_error_str(mesg);
7254}
7255#else
7256
7257static int
7258parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7259{
7260 const char *pcur = 0, *ptok = 0;
7261 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7262 p->ruby_sourceline == yylloc->end_pos.lineno) {
7263 pcur = p->lex.pcur;
7264 ptok = p->lex.ptok;
7265 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7266 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7267 }
7268 parser_yyerror0(p, msg);
7269 if (pcur) {
7270 p->lex.ptok = ptok;
7271 p->lex.pcur = pcur;
7272 }
7273 return 0;
7274}
7275
7276static int
7277parser_yyerror0(struct parser_params *p, const char *msg)
7278{
7279 dispatch1(parse_error, STR_NEW2(msg));
7280 ripper_error(p);
7281 return 0;
7282}
7283
7284static inline void
7285parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7286{
7287}
7288#endif /* !RIPPER */
7289
7290static int
7291vtable_size(const struct vtable *tbl)
7292{
7293 if (!DVARS_TERMINAL_P(tbl)) {
7294 return tbl->pos;
7295 }
7296 else {
7297 return 0;
7298 }
7299}
7300
7301static struct vtable *
7302vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7303{
7304 struct vtable *tbl = ALLOC(struct vtable);
7305 tbl->pos = 0;
7306 tbl->capa = 8;
7307 tbl->tbl = ALLOC_N(ID, tbl->capa);
7308 tbl->prev = prev;
7309#ifndef RIPPER
7310 if (p->debug) {
7311 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7312 }
7313#endif
7314 return tbl;
7315}
7316#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7317
7318static void
7319vtable_free_gen(struct parser_params *p, int line, const char *name,
7320 struct vtable *tbl)
7321{
7322#ifndef RIPPER
7323 if (p->debug) {
7324 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7325 }
7326#endif
7327 if (!DVARS_TERMINAL_P(tbl)) {
7328 if (tbl->tbl) {
7329 ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID));
7330 }
7331 ruby_xfree_sized(tbl, sizeof(*tbl));
7332 }
7333}
7334#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7335
7336static void
7337vtable_add_gen(struct parser_params *p, int line, const char *name,
7338 struct vtable *tbl, ID id)
7339{
7340#ifndef RIPPER
7341 if (p->debug) {
7342 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7343 line, name, (void *)tbl, rb_id2name(id));
7344 }
7345#endif
7346 if (DVARS_TERMINAL_P(tbl)) {
7347 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7348 return;
7349 }
7350 if (tbl->pos == tbl->capa) {
7351 tbl->capa = tbl->capa * 2;
7352 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7353 }
7354 tbl->tbl[tbl->pos++] = id;
7355}
7356#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7357
7358static void
7359vtable_pop_gen(struct parser_params *p, int line, const char *name,
7360 struct vtable *tbl, int n)
7361{
7362 if (p->debug) {
7363 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7364 line, name, (void *)tbl, n);
7365 }
7366 if (tbl->pos < n) {
7367 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7368 return;
7369 }
7370 tbl->pos -= n;
7371}
7372#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7373
7374static int
7375vtable_included(const struct vtable * tbl, ID id)
7376{
7377 int i;
7378
7379 if (!DVARS_TERMINAL_P(tbl)) {
7380 for (i = 0; i < tbl->pos; i++) {
7381 if (tbl->tbl[i] == id) {
7382 return i+1;
7383 }
7384 }
7385 }
7386 return 0;
7387}
7388
7389static void parser_prepare(struct parser_params *p);
7390
7391static int
7392e_option_supplied(struct parser_params *p)
7393{
7394 return strcmp(p->ruby_sourcefile, "-e") == 0;
7395}
7396
7397#ifndef RIPPER
7398static NODE *parser_append_options(struct parser_params *p, NODE *node);
7399
7400static VALUE
7401yycompile0(VALUE arg)
7402{
7403 int n;
7404 NODE *tree;
7405 struct parser_params *p = (struct parser_params *)arg;
7406 int cov = FALSE;
7407
7408 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7409 cov = TRUE;
7410 }
7411
7412 if (p->debug_lines) {
7413 p->ast->body.script_lines = p->debug_lines;
7414 }
7415
7416 parser_prepare(p);
7417#define RUBY_DTRACE_PARSE_HOOK(name) \
7418 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7419 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7420 }
7421 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7422 n = yyparse(p);
7423 RUBY_DTRACE_PARSE_HOOK(END);
7424
7425 p->debug_lines = 0;
7426
7427 xfree(p->lex.strterm);
7428 p->lex.strterm = 0;
7429 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7430 if (n || p->error_p) {
7431 VALUE mesg = p->error_buffer;
7432 if (!mesg) {
7433 mesg = syntax_error_new();
7434 }
7435 if (!p->error_tolerant) {
7436 rb_set_errinfo(mesg);
7437 return FALSE;
7438 }
7439 }
7440 tree = p->eval_tree;
7441 if (!tree) {
7442 tree = NEW_NIL(&NULL_LOC);
7443 }
7444 else {
7445 rb_parser_ary_t *tokens = p->tokens;
7446 NODE *prelude;
7447 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7448 prelude = block_append(p, p->eval_tree_begin, body);
7449 RNODE_SCOPE(tree)->nd_body = prelude;
7450 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7451 p->ast->body.coverage_enabled = cov;
7452 if (p->keep_tokens) {
7453 p->ast->node_buffer->tokens = tokens;
7454 p->tokens = NULL;
7455 }
7456 }
7457 p->ast->body.root = tree;
7458 p->ast->body.line_count = p->line_count;
7459 return TRUE;
7460}
7461
7462static rb_ast_t *
7463yycompile(struct parser_params *p, VALUE fname, int line)
7464{
7465 rb_ast_t *ast;
7466 if (NIL_P(fname)) {
7467 p->ruby_sourcefile_string = Qnil;
7468 p->ruby_sourcefile = "(none)";
7469 }
7470 else {
7471 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7472 p->ruby_sourcefile = StringValueCStr(fname);
7473 }
7474 p->ruby_sourceline = line - 1;
7475
7476 p->lvtbl = NULL;
7477
7478 p->ast = ast = rb_ast_new();
7479 compile_callback(yycompile0, (VALUE)p);
7480 ast->body.source_hash = rb_source_hash_finalize(&p->source_hash);
7481 ast->body.has_source_hash = 1;
7482 p->ast = 0;
7483
7484 while (p->lvtbl) {
7485 local_pop(p);
7486 }
7487
7488 return ast;
7489}
7490#endif /* !RIPPER */
7491
7492static rb_encoding *
7493must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7494{
7495 rb_encoding *enc = rb_parser_str_get_encoding(s);
7496 if (!rb_enc_asciicompat(enc)) {
7497 rb_raise(rb_eArgError, "invalid source encoding");
7498 }
7499 return enc;
7500}
7501
7502static rb_parser_string_t *
7503lex_getline(struct parser_params *p)
7504{
7505 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7506 if (!line) return 0;
7507 p->line_count++;
7508 rb_source_hash_update(&p->source_hash, (const uint8_t *)line->ptr, (size_t)line->len);
7509 string_buffer_append(p, line);
7510 must_be_ascii_compatible(p, line);
7511 return line;
7512}
7513
7514#ifndef RIPPER
7515rb_ast_t*
7516rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7517{
7518 p->lex.gets = gets;
7519 p->lex.input = input;
7520 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7521
7522 return yycompile(p, fname, line);
7523}
7524#endif /* !RIPPER */
7525
7526#define STR_FUNC_ESCAPE 0x01
7527#define STR_FUNC_EXPAND 0x02
7528#define STR_FUNC_REGEXP 0x04
7529#define STR_FUNC_QWORDS 0x08
7530#define STR_FUNC_SYMBOL 0x10
7531#define STR_FUNC_INDENT 0x20
7532#define STR_FUNC_LABEL 0x40
7533#define STR_FUNC_LIST 0x4000
7534#define STR_FUNC_TERM 0x8000
7535
7536enum string_type {
7537 str_label = STR_FUNC_LABEL,
7538 str_squote = (0),
7539 str_dquote = (STR_FUNC_EXPAND),
7540 str_xquote = (STR_FUNC_EXPAND),
7541 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7542 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7543 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7544 str_ssym = (STR_FUNC_SYMBOL),
7545 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7546};
7547
7548static rb_parser_string_t *
7549parser_str_new(struct parser_params *p, const char *ptr, rb_len_t len, rb_encoding *enc, int func, rb_encoding *enc0)
7550{
7551 rb_parser_string_t *pstr;
7552
7553 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7554
7555 if (!(func & STR_FUNC_REGEXP)) {
7556 if (rb_parser_is_ascii_string(p, pstr)) {
7557 }
7558 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7559 /* everything is valid in ASCII-8BIT */
7560 enc = rb_ascii8bit_encoding();
7561 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7562 }
7563 }
7564
7565 return pstr;
7566}
7567
7568static int
7569strterm_is_heredoc(rb_strterm_t *strterm)
7570{
7571 return strterm->heredoc;
7572}
7573
7574static rb_strterm_t *
7575new_strterm(struct parser_params *p, int func, int term, int paren)
7576{
7577 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7578 strterm->u.literal.func = func;
7579 strterm->u.literal.term = term;
7580 strterm->u.literal.paren = paren;
7581 return strterm;
7582}
7583
7584static rb_strterm_t *
7585new_heredoc(struct parser_params *p)
7586{
7587 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7588 strterm->heredoc = true;
7589 return strterm;
7590}
7591
7592#define peek(p,c) peek_n(p, (c), 0)
7593#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7594#define peekc(p) peekc_n(p, 0)
7595#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7596
7597#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7598static void
7599parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7600{
7601 debug_token_line(p, "add_delayed_token", line);
7602
7603 if (tok < end) {
7604 if (has_delayed_token(p)) {
7605 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7606 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7607 int end_col = (next_line ? 0 : p->delayed.end_col);
7608 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7609 dispatch_delayed_token(p, tSTRING_CONTENT);
7610 }
7611 }
7612 if (!has_delayed_token(p)) {
7613 p->delayed.token = rb_parser_string_new(p, 0, 0);
7614 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7615 p->delayed.beg_line = p->ruby_sourceline;
7616 p->delayed.beg_col = rb_len2int(tok - p->lex.pbeg);
7617 }
7618 parser_str_cat(p->delayed.token, tok, end - tok);
7619 p->delayed.end_line = p->ruby_sourceline;
7620 p->delayed.end_col = rb_len2int(end - p->lex.pbeg);
7621 p->lex.ptok = end;
7622 }
7623}
7624
7625static void
7626set_lastline(struct parser_params *p, rb_parser_string_t *str)
7627{
7628 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7629 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7630 p->lex.lastline = str;
7631}
7632
7633static int
7634nextline(struct parser_params *p, int set_encoding)
7635{
7636 rb_parser_string_t *str = p->lex.nextline;
7637 p->lex.nextline = 0;
7638 if (!str) {
7639 if (p->eofp)
7640 return -1;
7641
7642 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7643 goto end_of_input;
7644 }
7645
7646 if (!p->lex.input || !(str = lex_getline(p))) {
7647 end_of_input:
7648 p->eofp = 1;
7649 lex_goto_eol(p);
7650 return -1;
7651 }
7652#ifndef RIPPER
7653 if (p->debug_lines) {
7654 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7655 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7656 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7657 }
7658#endif
7659 p->cr_seen = FALSE;
7660 }
7661 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7662 /* after here-document without terminator */
7663 goto end_of_input;
7664 }
7665 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7666 if (p->heredoc_end > 0) {
7667 p->ruby_sourceline = p->heredoc_end;
7668 p->heredoc_end = 0;
7669 }
7670 p->ruby_sourceline++;
7671 set_lastline(p, str);
7672 token_flush(p);
7673 return 0;
7674}
7675
7676static int
7677parser_cr(struct parser_params *p, int c)
7678{
7679 if (peek(p, '\n')) {
7680 p->lex.pcur++;
7681 c = '\n';
7682 }
7683 return c;
7684}
7685
7686static inline int
7687nextc0(struct parser_params *p, int set_encoding)
7688{
7689 int c;
7690
7691 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7692 if (nextline(p, set_encoding)) return -1;
7693 }
7694 c = (unsigned char)*p->lex.pcur++;
7695 if (UNLIKELY(c == '\r')) {
7696 c = parser_cr(p, c);
7697 }
7698
7699 return c;
7700}
7701#define nextc(p) nextc0(p, TRUE)
7702
7703static void
7704pushback(struct parser_params *p, int c)
7705{
7706 if (c == -1) return;
7707 p->eofp = 0;
7708 p->lex.pcur--;
7709 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7710 p->lex.pcur--;
7711 }
7712}
7713
7714#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7715
7716#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7717#define tok(p) (p)->tokenbuf
7718#define toklen(p) (p)->tokidx
7719
7720static int
7721looking_at_eol_p(struct parser_params *p)
7722{
7723 const char *ptr = p->lex.pcur;
7724 while (!lex_eol_ptr_p(p, ptr)) {
7725 int c = (unsigned char)*ptr++;
7726 int eol = (c == '\n' || c == '#');
7727 if (eol || !ISSPACE(c)) {
7728 return eol;
7729 }
7730 }
7731 return TRUE;
7732}
7733
7734static char*
7735newtok(struct parser_params *p)
7736{
7737 p->tokidx = 0;
7738 if (!p->tokenbuf) {
7739 p->toksiz = 60;
7740 p->tokenbuf = ALLOC_N(char, 60);
7741 }
7742 if (p->toksiz > 4096) {
7743 p->toksiz = 60;
7744 REALLOC_N(p->tokenbuf, char, 60);
7745 }
7746 return p->tokenbuf;
7747}
7748
7749static char *
7750tokspace(struct parser_params *p, int n)
7751{
7752 p->tokidx += n;
7753
7754 if (p->tokidx >= p->toksiz) {
7755 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7756 REALLOC_N(p->tokenbuf, char, p->toksiz);
7757 }
7758 return &p->tokenbuf[p->tokidx-n];
7759}
7760
7761static void
7762tokadd(struct parser_params *p, int c)
7763{
7764 p->tokenbuf[p->tokidx++] = (char)c;
7765 if (p->tokidx >= p->toksiz) {
7766 p->toksiz *= 2;
7767 REALLOC_N(p->tokenbuf, char, p->toksiz);
7768 }
7769}
7770
7771static int
7772tok_hex(struct parser_params *p, size_t *numlen)
7773{
7774 int c;
7775
7776 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7777 if (!*numlen) {
7778 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7779 yyerror0("invalid hex escape");
7780 dispatch_scan_event(p, tSTRING_CONTENT);
7781 return 0;
7782 }
7783 p->lex.pcur += *numlen;
7784 return c;
7785}
7786
7787#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7788
7789static int
7790escaped_control_code(int c)
7791{
7792 int c2 = 0;
7793 switch (c) {
7794 case ' ':
7795 c2 = 's';
7796 break;
7797 case '\n':
7798 c2 = 'n';
7799 break;
7800 case '\t':
7801 c2 = 't';
7802 break;
7803 case '\v':
7804 c2 = 'v';
7805 break;
7806 case '\r':
7807 c2 = 'r';
7808 break;
7809 case '\f':
7810 c2 = 'f';
7811 break;
7812 }
7813 return c2;
7814}
7815
7816#define WARN_SPACE_CHAR(c, prefix) \
7817 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7818
7819static int
7820tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7821 int regexp_literal, const char *begin)
7822{
7823 const int wide = !begin;
7824 size_t numlen;
7825 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7826
7827 p->lex.pcur += numlen;
7828 if (p->lex.strterm == NULL ||
7829 strterm_is_heredoc(p->lex.strterm) ||
7830 (p->lex.strterm->u.literal.func != str_regexp)) {
7831 if (!begin) begin = p->lex.pcur;
7832 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7833 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7834 yyerror0("invalid Unicode escape");
7835 dispatch_scan_event(p, tSTRING_CONTENT);
7836 return wide && numlen > 0;
7837 }
7838 if (codepoint > 0x10ffff) {
7839 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7840 yyerror0("invalid Unicode codepoint (too large)");
7841 dispatch_scan_event(p, tSTRING_CONTENT);
7842 return wide;
7843 }
7844 if ((codepoint & 0xfffff800) == 0xd800) {
7845 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7846 yyerror0("invalid Unicode codepoint");
7847 dispatch_scan_event(p, tSTRING_CONTENT);
7848 return wide;
7849 }
7850 }
7851 if (regexp_literal) {
7852 tokcopy(p, (int)numlen);
7853 }
7854 else if (codepoint >= 0x80) {
7855 rb_encoding *utf8 = rb_utf8_encoding();
7856 if (*encp && utf8 != *encp) {
7857 YYLTYPE loc = RUBY_INIT_YYLLOC();
7858 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7859 parser_show_error_line(p, &loc);
7860 return wide;
7861 }
7862 *encp = utf8;
7863 tokaddmbc(p, codepoint, *encp);
7864 }
7865 else {
7866 tokadd(p, codepoint);
7867 }
7868 return TRUE;
7869}
7870
7871static int tokadd_mbchar(struct parser_params *p, int c);
7872
7873static int
7874tokskip_mbchar(struct parser_params *p)
7875{
7876 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7877 if (len > 0) {
7878 p->lex.pcur += len - 1;
7879 }
7880 return len;
7881}
7882
7883/* return value is for ?\u3042 */
7884static void
7885tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7886 int term, int symbol_literal, int regexp_literal)
7887{
7888 /*
7889 * If `term` is not -1, then we allow multiple codepoints in \u{}
7890 * upto `term` byte, otherwise we're parsing a character literal.
7891 * And then add the codepoints to the current token.
7892 */
7893 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7894
7895 const int open_brace = '{', close_brace = '}';
7896
7897 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7898
7899 if (peek(p, open_brace)) { /* handle \u{...} form */
7900 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7901 /*
7902 * Skip parsing validation code and copy bytes as-is until term or
7903 * closing brace, in order to correctly handle extended regexps where
7904 * invalid unicode escapes are allowed in comments. The regexp parser
7905 * does its own validation and will catch any issues.
7906 */
7907 tokadd(p, open_brace);
7908 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7909 int c = peekc(p);
7910 if (c == close_brace) {
7911 tokadd(p, c);
7912 ++p->lex.pcur;
7913 break;
7914 }
7915 else if (c == term) {
7916 break;
7917 }
7918 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7919 tokadd(p, c);
7920 c = *++p->lex.pcur;
7921 }
7922 tokadd_mbchar(p, c);
7923 }
7924 }
7925 else {
7926 const char *second = NULL;
7927 int c, last = nextc(p);
7928 if (lex_eol_p(p)) goto unterminated;
7929 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7930 while (c != close_brace) {
7931 if (c == term) goto unterminated;
7932 if (second == multiple_codepoints)
7933 second = p->lex.pcur;
7934 if (regexp_literal) tokadd(p, last);
7935 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7936 break;
7937 }
7938 while (ISSPACE(c = peekc(p))) {
7939 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7940 last = c;
7941 }
7942 if (term == -1 && !second)
7943 second = multiple_codepoints;
7944 }
7945
7946 if (c != close_brace) {
7947 unterminated:
7948 flush_string_content(p, rb_utf8_encoding(), 0);
7949 yyerror0("unterminated Unicode escape");
7950 dispatch_scan_event(p, tSTRING_CONTENT);
7951 return;
7952 }
7953 if (second && second != multiple_codepoints) {
7954 const char *pcur = p->lex.pcur;
7955 p->lex.pcur = second;
7956 dispatch_scan_event(p, tSTRING_CONTENT);
7957 token_flush(p);
7958 p->lex.pcur = pcur;
7959 yyerror0(multiple_codepoints);
7960 token_flush(p);
7961 }
7962
7963 if (regexp_literal) tokadd(p, close_brace);
7964 nextc(p);
7965 }
7966 }
7967 else { /* handle \uxxxx form */
7968 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7969 token_flush(p);
7970 return;
7971 }
7972 }
7973}
7974
7975#define ESCAPE_CONTROL 1
7976#define ESCAPE_META 2
7977
7978static int
7979read_escape(struct parser_params *p, int flags, const char *begin)
7980{
7981 int c;
7982 size_t numlen;
7983
7984 switch (c = nextc(p)) {
7985 case '\\': /* Backslash */
7986 return c;
7987
7988 case 'n': /* newline */
7989 return '\n';
7990
7991 case 't': /* horizontal tab */
7992 return '\t';
7993
7994 case 'r': /* carriage-return */
7995 return '\r';
7996
7997 case 'f': /* form-feed */
7998 return '\f';
7999
8000 case 'v': /* vertical tab */
8001 return '\13';
8002
8003 case 'a': /* alarm(bell) */
8004 return '\007';
8005
8006 case 'e': /* escape */
8007 return 033;
8008
8009 case '0': case '1': case '2': case '3': /* octal constant */
8010 case '4': case '5': case '6': case '7':
8011 pushback(p, c);
8012 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8013 p->lex.pcur += numlen;
8014 return c;
8015
8016 case 'x': /* hex constant */
8017 c = tok_hex(p, &numlen);
8018 if (numlen == 0) return 0;
8019 return c;
8020
8021 case 'b': /* backspace */
8022 return '\010';
8023
8024 case 's': /* space */
8025 return ' ';
8026
8027 case 'M':
8028 if (flags & ESCAPE_META) goto eof;
8029 if ((c = nextc(p)) != '-') {
8030 goto eof;
8031 }
8032 if ((c = nextc(p)) == '\\') {
8033 switch (peekc(p)) {
8034 case 'u': case 'U':
8035 nextc(p);
8036 goto eof;
8037 }
8038 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8039 }
8040 else if (c == -1) goto eof;
8041 else if (!ISASCII(c)) {
8042 tokskip_mbchar(p);
8043 goto eof;
8044 }
8045 else {
8046 int c2 = escaped_control_code(c);
8047 if (c2) {
8048 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8049 WARN_SPACE_CHAR(c2, "\\M-");
8050 }
8051 else {
8052 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8053 }
8054 }
8055 else if (ISCNTRL(c)) goto eof;
8056 return ((c & 0xff) | 0x80);
8057 }
8058
8059 case 'C':
8060 if ((c = nextc(p)) != '-') {
8061 goto eof;
8062 }
8063 case 'c':
8064 if (flags & ESCAPE_CONTROL) goto eof;
8065 if ((c = nextc(p))== '\\') {
8066 switch (peekc(p)) {
8067 case 'u': case 'U':
8068 nextc(p);
8069 goto eof;
8070 }
8071 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8072 }
8073 else if (c == '?')
8074 return 0177;
8075 else if (c == -1) goto eof;
8076 else if (!ISASCII(c)) {
8077 tokskip_mbchar(p);
8078 goto eof;
8079 }
8080 else {
8081 int c2 = escaped_control_code(c);
8082 if (c2) {
8083 if (ISCNTRL(c)) {
8084 if (flags & ESCAPE_META) {
8085 WARN_SPACE_CHAR(c2, "\\M-");
8086 }
8087 else {
8088 WARN_SPACE_CHAR(c2, "");
8089 }
8090 }
8091 else {
8092 if (flags & ESCAPE_META) {
8093 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8094 }
8095 else {
8096 WARN_SPACE_CHAR(c2, "\\C-");
8097 }
8098 }
8099 }
8100 else if (ISCNTRL(c)) goto eof;
8101 }
8102 return c & 0x9f;
8103
8104 eof:
8105 case -1:
8106 flush_string_content(p, p->enc, p->lex.pcur - begin);
8107 yyerror0("Invalid escape character syntax");
8108 dispatch_scan_event(p, tSTRING_CONTENT);
8109 return '\0';
8110
8111 default:
8112 if (!ISASCII(c)) {
8113 tokskip_mbchar(p);
8114 goto eof;
8115 }
8116 return c;
8117 }
8118}
8119
8120static void
8121tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8122{
8123 int len = rb_enc_codelen(c, enc);
8124 rb_enc_mbcput(c, tokspace(p, len), enc);
8125}
8126
8127static int
8128tokadd_escape(struct parser_params *p)
8129{
8130 int c;
8131 size_t numlen;
8132 const char *begin = p->lex.pcur;
8133
8134 switch (c = nextc(p)) {
8135 case '\n':
8136 return 0; /* just ignore */
8137
8138 case '0': case '1': case '2': case '3': /* octal constant */
8139 case '4': case '5': case '6': case '7':
8140 {
8141 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8142 if (numlen == 0) goto eof;
8143 p->lex.pcur += numlen;
8144 tokcopy(p, (int)numlen + 1);
8145 }
8146 return 0;
8147
8148 case 'x': /* hex constant */
8149 {
8150 tok_hex(p, &numlen);
8151 if (numlen == 0) return -1;
8152 tokcopy(p, (int)numlen + 2);
8153 }
8154 return 0;
8155
8156 eof:
8157 case -1:
8158 flush_string_content(p, p->enc, p->lex.pcur - begin);
8159 yyerror0("Invalid escape character syntax");
8160 token_flush(p);
8161 return -1;
8162
8163 default:
8164 tokadd(p, '\\');
8165 tokadd(p, c);
8166 }
8167 return 0;
8168}
8169
8170static int
8171char_to_option(int c)
8172{
8173 int val;
8174
8175 switch (c) {
8176 case 'i':
8177 val = RE_ONIG_OPTION_IGNORECASE;
8178 break;
8179 case 'x':
8180 val = RE_ONIG_OPTION_EXTEND;
8181 break;
8182 case 'm':
8183 val = RE_ONIG_OPTION_MULTILINE;
8184 break;
8185 default:
8186 val = 0;
8187 break;
8188 }
8189 return val;
8190}
8191
8192#define ARG_ENCODING_FIXED 16
8193#define ARG_ENCODING_NONE 32
8194#define ENC_ASCII8BIT 1
8195#define ENC_EUC_JP 2
8196#define ENC_Windows_31J 3
8197#define ENC_UTF8 4
8198
8199static int
8200char_to_option_kcode(int c, int *option, int *kcode)
8201{
8202 *option = 0;
8203
8204 switch (c) {
8205 case 'n':
8206 *kcode = ENC_ASCII8BIT;
8207 return (*option = ARG_ENCODING_NONE);
8208 case 'e':
8209 *kcode = ENC_EUC_JP;
8210 break;
8211 case 's':
8212 *kcode = ENC_Windows_31J;
8213 break;
8214 case 'u':
8215 *kcode = ENC_UTF8;
8216 break;
8217 default:
8218 *kcode = -1;
8219 return (*option = char_to_option(c));
8220 }
8221 *option = ARG_ENCODING_FIXED;
8222 return 1;
8223}
8224
8225static int
8226regx_options(struct parser_params *p)
8227{
8228 int kcode = 0;
8229 int kopt = 0;
8230 int options = 0;
8231 int c, opt, kc;
8232
8233 newtok(p);
8234 while (c = nextc(p), ISALPHA(c)) {
8235 if (c == 'o') {
8236 options |= RE_OPTION_ONCE;
8237 }
8238 else if (char_to_option_kcode(c, &opt, &kc)) {
8239 if (kc >= 0) {
8240 if (kc != ENC_ASCII8BIT) kcode = c;
8241 kopt = opt;
8242 }
8243 else {
8244 options |= opt;
8245 }
8246 }
8247 else {
8248 tokadd(p, c);
8249 }
8250 }
8251 options |= kopt;
8252 pushback(p, c);
8253 if (toklen(p)) {
8254 YYLTYPE loc = RUBY_INIT_YYLLOC();
8255 tokfix(p);
8256 compile_error(p, "unknown regexp option%s - %*s",
8257 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8258 parser_show_error_line(p, &loc);
8259 }
8260 return options | RE_OPTION_ENCODING(kcode);
8261}
8262
8263static int
8264tokadd_mbchar(struct parser_params *p, int c)
8265{
8266 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8267 if (len < 0) return -1;
8268 tokadd(p, c);
8269 p->lex.pcur += --len;
8270 if (len > 0) tokcopy(p, len);
8271 return c;
8272}
8273
8274static inline int
8275simple_re_meta(int c)
8276{
8277 switch (c) {
8278 case '$': case '*': case '+': case '.':
8279 case '?': case '^': case '|':
8280 case ')': case ']': case '}': case '>':
8281 return TRUE;
8282 default:
8283 return FALSE;
8284 }
8285}
8286
8287static int
8288parser_update_heredoc_indent(struct parser_params *p, int c)
8289{
8290 if (p->heredoc_line_indent == -1) {
8291 if (c == '\n') p->heredoc_line_indent = 0;
8292 }
8293 else {
8294 if (c == ' ') {
8295 p->heredoc_line_indent++;
8296 return TRUE;
8297 }
8298 else if (c == '\t') {
8299 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8300 p->heredoc_line_indent = w * TAB_WIDTH;
8301 return TRUE;
8302 }
8303 else if (c != '\n') {
8304 if (p->heredoc_indent > p->heredoc_line_indent) {
8305 p->heredoc_indent = p->heredoc_line_indent;
8306 }
8307 p->heredoc_line_indent = -1;
8308 }
8309 else {
8310 /* Whitespace only line has no indentation */
8311 p->heredoc_line_indent = 0;
8312 }
8313 }
8314 return FALSE;
8315}
8316
8317static void
8318parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8319{
8320 YYLTYPE loc = RUBY_INIT_YYLLOC();
8321 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8322 compile_error(p, "%s mixed within %s source", n1, n2);
8323 parser_show_error_line(p, &loc);
8324}
8325
8326static void
8327parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8328{
8329 const char *pos = p->lex.pcur;
8330 p->lex.pcur = beg;
8331 parser_mixed_error(p, enc1, enc2);
8332 p->lex.pcur = pos;
8333}
8334
8335static inline char
8336nibble_char_upper(unsigned int c)
8337{
8338 c &= 0xf;
8339 return c + (c < 10 ? '0' : 'A' - 10);
8340}
8341
8342static int
8343tokadd_string(struct parser_params *p,
8344 int func, int term, int paren, long *nest,
8345 rb_encoding **encp, rb_encoding **enc)
8346{
8347 int c;
8348 bool erred = false;
8349#ifdef RIPPER
8350 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8351 int top_of_line = FALSE;
8352#endif
8353
8354#define mixed_error(enc1, enc2) \
8355 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8356#define mixed_escape(beg, enc1, enc2) \
8357 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8358
8359 while ((c = nextc(p)) != -1) {
8360 if (p->heredoc_indent > 0) {
8361 parser_update_heredoc_indent(p, c);
8362 }
8363#ifdef RIPPER
8364 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8365 pushback(p, c);
8366 break;
8367 }
8368#endif
8369
8370 if (paren && c == paren) {
8371 ++*nest;
8372 }
8373 else if (c == term) {
8374 if (!nest || !*nest) {
8375 pushback(p, c);
8376 break;
8377 }
8378 --*nest;
8379 }
8380 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8381 unsigned char c2 = *p->lex.pcur;
8382 if (c2 == '$' || c2 == '@' || c2 == '{') {
8383 pushback(p, c);
8384 break;
8385 }
8386 }
8387 else if (c == '\\') {
8388 c = nextc(p);
8389 switch (c) {
8390 case '\n':
8391 if (func & STR_FUNC_QWORDS) break;
8392 if (func & STR_FUNC_EXPAND) {
8393 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8394 continue;
8395 if (c == term) {
8396 c = '\\';
8397 goto terminate;
8398 }
8399 }
8400 tokadd(p, '\\');
8401 break;
8402
8403 case '\\':
8404 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8405 break;
8406
8407 case 'u':
8408 if ((func & STR_FUNC_EXPAND) == 0) {
8409 tokadd(p, '\\');
8410 break;
8411 }
8412 tokadd_utf8(p, enc, term,
8413 func & STR_FUNC_SYMBOL,
8414 func & STR_FUNC_REGEXP);
8415 continue;
8416
8417 default:
8418 if (c == -1) return -1;
8419 if (!ISASCII(c)) {
8420 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8421 goto non_ascii;
8422 }
8423 if (func & STR_FUNC_REGEXP) {
8424 switch (c) {
8425 case 'c':
8426 case 'C':
8427 case 'M': {
8428 pushback(p, c);
8429 c = read_escape(p, 0, p->lex.pcur - 1);
8430
8431 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8432 *t++ = '\\';
8433 *t++ = 'x';
8434 *t++ = nibble_char_upper(c >> 4);
8435 *t++ = nibble_char_upper(c);
8436 continue;
8437 }
8438 }
8439
8440 if (c == term && !simple_re_meta(c)) {
8441 tokadd(p, c);
8442 continue;
8443 }
8444 pushback(p, c);
8445 if ((c = tokadd_escape(p)) < 0)
8446 return -1;
8447 if (*enc && *enc != *encp) {
8448 mixed_escape(p->lex.ptok+2, *enc, *encp);
8449 }
8450 continue;
8451 }
8452 else if (func & STR_FUNC_EXPAND) {
8453 pushback(p, c);
8454 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8455 c = read_escape(p, 0, p->lex.pcur - 1);
8456 }
8457 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8458 /* ignore backslashed spaces in %w */
8459 }
8460 else if (c != term && !(paren && c == paren)) {
8461 tokadd(p, '\\');
8462 pushback(p, c);
8463 continue;
8464 }
8465 }
8466 }
8467 else if (!parser_isascii(p)) {
8468 non_ascii:
8469 if (!*enc) {
8470 *enc = *encp;
8471 }
8472 else if (*enc != *encp) {
8473 mixed_error(*enc, *encp);
8474 continue;
8475 }
8476 if (tokadd_mbchar(p, c) == -1) return -1;
8477 continue;
8478 }
8479 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8480 pushback(p, c);
8481 break;
8482 }
8483 if (c & 0x80) {
8484 if (!*enc) {
8485 *enc = *encp;
8486 }
8487 else if (*enc != *encp) {
8488 mixed_error(*enc, *encp);
8489 continue;
8490 }
8491 }
8492 tokadd(p, c);
8493#ifdef RIPPER
8494 top_of_line = (c == '\n');
8495#endif
8496 }
8497 terminate:
8498 if (*enc) *encp = *enc;
8499 return c;
8500}
8501
8502#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8503
8504static void
8505flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8506{
8507 p->lex.pcur -= back;
8508 if (has_delayed_token(p)) {
8509 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8510 if (len > 0) {
8511 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8512 p->delayed.end_line = p->ruby_sourceline;
8513 p->delayed.end_col = rb_len2int(p->lex.pcur - p->lex.pbeg);
8514 }
8515 dispatch_delayed_token(p, tSTRING_CONTENT);
8516 p->lex.ptok = p->lex.pcur;
8517 }
8518 dispatch_scan_event(p, tSTRING_CONTENT);
8519 p->lex.pcur += back;
8520}
8521
8522/* this can be shared with ripper, since it's independent from struct
8523 * parser_params. */
8524#ifndef RIPPER
8525#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8526#define SPECIAL_PUNCT(idx) ( \
8527 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8528 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8529 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8530 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8531 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8532 BIT('0', idx))
8533const uint_least32_t ruby_global_name_punct_bits[] = {
8534 SPECIAL_PUNCT(0),
8535 SPECIAL_PUNCT(1),
8536 SPECIAL_PUNCT(2),
8537};
8538#undef BIT
8539#undef SPECIAL_PUNCT
8540#endif
8541
8542static enum yytokentype
8543parser_peek_variable_name(struct parser_params *p)
8544{
8545 int c;
8546 const char *ptr = p->lex.pcur;
8547
8548 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8549 c = *ptr++;
8550 switch (c) {
8551 case '$':
8552 if ((c = *ptr) == '-') {
8553 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8554 c = *ptr;
8555 }
8556 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8557 return tSTRING_DVAR;
8558 }
8559 break;
8560 case '@':
8561 if ((c = *ptr) == '@') {
8562 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8563 c = *ptr;
8564 }
8565 break;
8566 case '{':
8567 p->lex.pcur = ptr;
8568 p->command_start = TRUE;
8569 yylval.state = p->lex.state;
8570 return tSTRING_DBEG;
8571 default:
8572 return 0;
8573 }
8574 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8575 return tSTRING_DVAR;
8576 return 0;
8577}
8578
8579#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8580#define IS_END() IS_lex_state(EXPR_END_ANY)
8581#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8582#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8583#define IS_LABEL_POSSIBLE() (\
8584 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8585 IS_ARG())
8586#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8587#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8588
8589static inline enum yytokentype
8590parser_string_term(struct parser_params *p, int func)
8591{
8592 xfree(p->lex.strterm);
8593 p->lex.strterm = 0;
8594 if (func & STR_FUNC_REGEXP) {
8595 set_yylval_num(regx_options(p));
8596 dispatch_scan_event(p, tREGEXP_END);
8597 SET_LEX_STATE(EXPR_END);
8598 return tREGEXP_END;
8599 }
8600 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8601 nextc(p);
8602 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8603 return tLABEL_END;
8604 }
8605 SET_LEX_STATE(EXPR_END);
8606 return tSTRING_END;
8607}
8608
8609static enum yytokentype
8610parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8611{
8612 int func = quote->func;
8613 int term = quote->term;
8614 int paren = quote->paren;
8615 int c, space = 0;
8616 rb_encoding *enc = p->enc;
8617 rb_encoding *base_enc = 0;
8618 rb_parser_string_t *lit;
8619
8620 if (func & STR_FUNC_TERM) {
8621 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8622 SET_LEX_STATE(EXPR_END);
8623 xfree(p->lex.strterm);
8624 p->lex.strterm = 0;
8625 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8626 }
8627 c = nextc(p);
8628 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8629 while (c != '\n' && ISSPACE(c = nextc(p)));
8630 space = 1;
8631 }
8632 if (func & STR_FUNC_LIST) {
8633 quote->func &= ~STR_FUNC_LIST;
8634 space = 1;
8635 }
8636 if (c == term && !quote->nest) {
8637 if (func & STR_FUNC_QWORDS) {
8638 quote->func |= STR_FUNC_TERM;
8639 pushback(p, c); /* dispatch the term at tSTRING_END */
8640 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8641 return ' ';
8642 }
8643 return parser_string_term(p, func);
8644 }
8645 if (space) {
8646 if (!ISSPACE(c)) pushback(p, c);
8647 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8648 return ' ';
8649 }
8650 newtok(p);
8651 if ((func & STR_FUNC_EXPAND) && c == '#') {
8652 enum yytokentype t = parser_peek_variable_name(p);
8653 if (t) return t;
8654 tokadd(p, '#');
8655 c = nextc(p);
8656 }
8657 pushback(p, c);
8658 if (tokadd_string(p, func, term, paren, &quote->nest,
8659 &enc, &base_enc) == -1) {
8660 if (p->eofp) {
8661#ifndef RIPPER
8662# define unterminated_literal(mesg) yyerror0(mesg)
8663#else
8664# define unterminated_literal(mesg) compile_error(p, mesg)
8665#endif
8666 literal_flush(p, p->lex.pcur);
8667 if (func & STR_FUNC_QWORDS) {
8668 /* no content to add, bailing out here */
8669 unterminated_literal("unterminated list meets end of file");
8670 xfree(p->lex.strterm);
8671 p->lex.strterm = 0;
8672 return tSTRING_END;
8673 }
8674 if (func & STR_FUNC_REGEXP) {
8675 unterminated_literal("unterminated regexp meets end of file");
8676 }
8677 else {
8678 unterminated_literal("unterminated string meets end of file");
8679 }
8680 quote->func |= STR_FUNC_TERM;
8681 }
8682 }
8683
8684 tokfix(p);
8685 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8686 set_yylval_str(lit);
8687 flush_string_content(p, enc, 0);
8688
8689 return tSTRING_CONTENT;
8690}
8691
8692static enum yytokentype
8693heredoc_identifier(struct parser_params *p)
8694{
8695 /*
8696 * term_len is length of `<<"END"` except `END`,
8697 * in this case term_len is 4 (<, <, " and ").
8698 */
8699 rb_len_t len, offset = p->lex.pcur - p->lex.pbeg;
8700 int c = nextc(p), term, func = 0, quote = 0;
8701 enum yytokentype token = tSTRING_BEG;
8702 int indent = 0;
8703
8704 if (c == '-') {
8705 c = nextc(p);
8706 func = STR_FUNC_INDENT;
8707 offset++;
8708 }
8709 else if (c == '~') {
8710 c = nextc(p);
8711 func = STR_FUNC_INDENT;
8712 offset++;
8713 indent = INT_MAX;
8714 }
8715 switch (c) {
8716 case '\'':
8717 func |= str_squote; goto quoted;
8718 case '"':
8719 func |= str_dquote; goto quoted;
8720 case '`':
8721 token = tXSTRING_BEG;
8722 func |= str_xquote; goto quoted;
8723
8724 quoted:
8725 quote++;
8726 offset++;
8727 term = c;
8728 len = 0;
8729 while ((c = nextc(p)) != term) {
8730 if (c == -1 || c == '\r' || c == '\n') {
8731 yyerror0("unterminated here document identifier");
8732 return -1;
8733 }
8734 }
8735 break;
8736
8737 default:
8738 if (!parser_is_identchar(p)) {
8739 pushback(p, c);
8740 if (func & STR_FUNC_INDENT) {
8741 pushback(p, indent > 0 ? '~' : '-');
8742 }
8743 return 0;
8744 }
8745 func |= str_dquote;
8746 do {
8747 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8748 if (n < 0) return 0;
8749 p->lex.pcur += --n;
8750 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8751 pushback(p, c);
8752 break;
8753 }
8754
8755 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8756 if ((rb_ulen_t)len >= HERETERM_LENGTH_MAX)
8757 yyerror0("too long here document identifier");
8758 dispatch_scan_event(p, tHEREDOC_BEG);
8759 lex_goto_eol(p);
8760
8761 p->lex.strterm = new_heredoc(p);
8762 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8763 here->offset = offset;
8764 here->sourceline = p->ruby_sourceline;
8765 here->length = (unsigned)len;
8766 here->quote = quote;
8767 here->func = func;
8768 here->lastline = p->lex.lastline;
8769
8770 token_flush(p);
8771 p->heredoc_indent = indent;
8772 p->heredoc_line_indent = 0;
8773 return token;
8774}
8775
8776static void
8777heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8778{
8779 rb_parser_string_t *line;
8780 rb_strterm_t *term = p->lex.strterm;
8781
8782 p->lex.strterm = 0;
8783 line = here->lastline;
8784 p->lex.lastline = line;
8785 p->lex.pbeg = PARSER_STRING_PTR(line);
8786 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8787 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8788 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8789 p->heredoc_end = p->ruby_sourceline;
8790 p->ruby_sourceline = (int)here->sourceline;
8791 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8792 p->eofp = 0;
8793 xfree(term);
8794}
8795
8796static int
8797dedent_string_column(const char *str, rb_len_t len, int width)
8798{
8799 int i, col = 0;
8800
8801 for (i = 0; i < len && col < width; i++) {
8802 if (str[i] == ' ') {
8803 col++;
8804 }
8805 else if (str[i] == '\t') {
8806 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8807 if (n > width) break;
8808 col = n;
8809 }
8810 else {
8811 break;
8812 }
8813 }
8814
8815 return i;
8816}
8817
8818static int
8819dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8820{
8821 char *str;
8822 rb_len_t len;
8823 int i;
8824
8825 len = PARSER_STRING_LEN(string);
8826 str = PARSER_STRING_PTR(string);
8827
8828 i = dedent_string_column(str, len, width);
8829 if (!i) return 0;
8830
8831 rb_parser_str_modify(string);
8832 str = PARSER_STRING_PTR(string);
8833 if (PARSER_STRING_LEN(string) != len)
8834 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8835 MEMMOVE(str, str + i, char, len - i);
8836 rb_parser_str_set_len(p, string, len - i);
8837 return i;
8838}
8839
8840static NODE *
8841heredoc_dedent(struct parser_params *p, NODE *root)
8842{
8843 NODE *node, *str_node, *prev_node;
8844 int indent = p->heredoc_indent;
8845 rb_parser_string_t *prev_lit = 0;
8846
8847 if (indent <= 0) return root;
8848 if (!root) return root;
8849
8850 prev_node = node = str_node = root;
8851 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8852
8853 while (str_node) {
8854 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8855 if (nd_fl_newline(str_node)) {
8856 dedent_string(p, lit, indent);
8857 }
8858 if (!prev_lit) {
8859 prev_lit = lit;
8860 }
8861 else if (!literal_concat0(p, prev_lit, lit)) {
8862 return 0;
8863 }
8864 else {
8865 NODE *end = RNODE_LIST(node)->as.nd_end;
8866 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8867 if (!node) {
8868 if (nd_type_p(prev_node, NODE_DSTR))
8869 nd_set_type(prev_node, NODE_STR);
8870 break;
8871 }
8872 RNODE_LIST(node)->as.nd_end = end;
8873 goto next_str;
8874 }
8875
8876 str_node = 0;
8877 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8878 next_str:
8879 if (!nd_type_p(node, NODE_LIST)) break;
8880 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8881 enum node_type type = nd_type(str_node);
8882 if (type == NODE_STR || type == NODE_DSTR) break;
8883 prev_lit = 0;
8884 str_node = 0;
8885 }
8886 }
8887 }
8888 return root;
8889}
8890
8891static int
8892whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8893{
8894 const char *beg = p->lex.pbeg;
8895 const char *ptr = p->lex.pend;
8896
8897 if (ptr - beg < len) return FALSE;
8898 if (ptr > beg && ptr[-1] == '\n') {
8899 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8900 if (ptr - beg < len) return FALSE;
8901 }
8902 if (strncmp(eos, ptr -= len, len)) return FALSE;
8903 if (indent) {
8904 while (beg < ptr && ISSPACE(*beg)) beg++;
8905 }
8906 return beg == ptr;
8907}
8908
8909static int
8910word_match_p(struct parser_params *p, const char *word, long len)
8911{
8912 if (strncmp(p->lex.pcur, word, len)) return 0;
8913 if (lex_eol_n_p(p, len)) return 1;
8914 int c = (unsigned char)p->lex.pcur[len];
8915 if (ISSPACE(c)) return 1;
8916 switch (c) {
8917 case '\0': case '\004': case '\032': return 1;
8918 }
8919 return 0;
8920}
8921
8922#define NUM_SUFFIX_R (1<<0)
8923#define NUM_SUFFIX_I (1<<1)
8924#define NUM_SUFFIX_ALL 3
8925
8926static int
8927number_literal_suffix(struct parser_params *p, int mask)
8928{
8929 int c, result = 0;
8930 const char *lastp = p->lex.pcur;
8931
8932 while ((c = nextc(p)) != -1) {
8933 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8934 result |= (mask & NUM_SUFFIX_I);
8935 mask &= ~NUM_SUFFIX_I;
8936 /* r after i, rational of complex is disallowed */
8937 mask &= ~NUM_SUFFIX_R;
8938 continue;
8939 }
8940 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8941 result |= (mask & NUM_SUFFIX_R);
8942 mask &= ~NUM_SUFFIX_R;
8943 continue;
8944 }
8945 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8946 p->lex.pcur = lastp;
8947 return 0;
8948 }
8949 pushback(p, c);
8950 break;
8951 }
8952 return result;
8953}
8954
8955static enum yytokentype
8956set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8957{
8958 enum rb_numeric_type numeric_type = integer_literal;
8959
8960 if (type == tFLOAT) {
8961 numeric_type = float_literal;
8962 }
8963
8964 if (suffix & NUM_SUFFIX_R) {
8965 type = tRATIONAL;
8966 numeric_type = rational_literal;
8967 }
8968 if (suffix & NUM_SUFFIX_I) {
8969 type = tIMAGINARY;
8970 }
8971
8972 switch (type) {
8973 case tINTEGER:
8974 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8975 break;
8976 case tFLOAT:
8977 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8978 break;
8979 case tRATIONAL:
8980 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8981 break;
8982 case tIMAGINARY:
8983 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8984 (void)numeric_type; /* for ripper */
8985 break;
8986 default:
8987 rb_bug("unexpected token: %d", type);
8988 }
8989 SET_LEX_STATE(EXPR_END);
8990 return type;
8991}
8992
8993#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8994static void
8995parser_dispatch_heredoc_end(struct parser_params *p, int line)
8996{
8997 if (has_delayed_token(p))
8998 dispatch_delayed_token(p, tSTRING_CONTENT);
8999
9000#ifdef RIPPER
9001 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9002 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9003#else
9004 if (p->keep_tokens) {
9005 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9006 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9007 parser_append_tokens(p, str, tHEREDOC_END, line);
9008 }
9009#endif
9010
9011 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9012 lex_goto_eol(p);
9013 token_flush(p);
9014}
9015
9016static enum yytokentype
9017here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9018{
9019 int c, func, indent = 0;
9020 const char *eos, *ptr, *ptr_end;
9021 long len;
9022 rb_parser_string_t *str = 0;
9023 rb_encoding *enc = p->enc;
9024 rb_encoding *base_enc = 0;
9025 int bol;
9026#ifdef RIPPER
9027 VALUE s_value;
9028#endif
9029
9030 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9031 len = here->length;
9032 indent = (func = here->func) & STR_FUNC_INDENT;
9033
9034 if ((c = nextc(p)) == -1) {
9035 error:
9036#ifdef RIPPER
9037 if (!has_delayed_token(p)) {
9038 dispatch_scan_event(p, tSTRING_CONTENT);
9039 }
9040 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9041 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9042 if (!(func & STR_FUNC_REGEXP)) {
9043 int cr = ENC_CODERANGE_UNKNOWN;
9044 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9045 if (cr != ENC_CODERANGE_7BIT &&
9046 rb_is_usascii_enc(p->enc) &&
9047 enc != rb_utf8_encoding()) {
9048 enc = rb_ascii8bit_encoding();
9049 }
9050 }
9051 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9052 }
9053 dispatch_delayed_token(p, tSTRING_CONTENT);
9054 }
9055 else {
9056 dispatch_delayed_token(p, tSTRING_CONTENT);
9057 dispatch_scan_event(p, tSTRING_CONTENT);
9058 }
9059 lex_goto_eol(p);
9060#endif
9061 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9062 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9063 (int)len, eos);
9064 token_flush(p);
9065 SET_LEX_STATE(EXPR_END);
9066 return tSTRING_END;
9067 }
9068 bol = was_bol(p);
9069 if (!bol) {
9070 /* not beginning of line, cannot be the terminator */
9071 }
9072 else if (p->heredoc_line_indent == -1) {
9073 /* `heredoc_line_indent == -1` means
9074 * - "after an interpolation in the same line", or
9075 * - "in a continuing line"
9076 */
9077 p->heredoc_line_indent = 0;
9078 }
9079 else if (whole_match_p(p, eos, len, indent)) {
9080 dispatch_heredoc_end(p);
9081 restore:
9082 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9083 token_flush(p);
9084 SET_LEX_STATE(EXPR_END);
9085 return tSTRING_END;
9086 }
9087
9088 if (!(func & STR_FUNC_EXPAND)) {
9089 do {
9090 ptr = PARSER_STRING_PTR(p->lex.lastline);
9091 ptr_end = p->lex.pend;
9092 if (ptr_end > ptr) {
9093 switch (ptr_end[-1]) {
9094 case '\n':
9095 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9096 ptr_end++;
9097 break;
9098 }
9099 case '\r':
9100 --ptr_end;
9101 }
9102 }
9103
9104 if (p->heredoc_indent > 0) {
9105 long i = 0;
9106 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9107 i++;
9108 p->heredoc_line_indent = 0;
9109 }
9110
9111 if (str)
9112 parser_str_cat(str, ptr, ptr_end - ptr);
9113 else
9114 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9115 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9116 lex_goto_eol(p);
9117 if (p->heredoc_indent > 0) {
9118 goto flush_str;
9119 }
9120 if (nextc(p) == -1) {
9121 if (str) {
9122 rb_parser_string_free(p, str);
9123 str = 0;
9124 }
9125 goto error;
9126 }
9127 } while (!whole_match_p(p, eos, len, indent));
9128 }
9129 else {
9130 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9131 newtok(p);
9132 if (c == '#') {
9133 enum yytokentype t = parser_peek_variable_name(p);
9134 if (p->heredoc_line_indent != -1) {
9135 if (p->heredoc_indent > p->heredoc_line_indent) {
9136 p->heredoc_indent = p->heredoc_line_indent;
9137 }
9138 p->heredoc_line_indent = -1;
9139 }
9140 if (t) return t;
9141 tokadd(p, '#');
9142 c = nextc(p);
9143 }
9144 do {
9145 pushback(p, c);
9146 enc = p->enc;
9147 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9148 if (p->eofp) goto error;
9149 goto restore;
9150 }
9151 if (c != '\n') {
9152 if (c == '\\') p->heredoc_line_indent = -1;
9153 flush:
9154 str = STR_NEW3(tok(p), toklen(p), enc, func);
9155 flush_str:
9156 set_yylval_str(str);
9157#ifndef RIPPER
9158 if (bol) nd_set_fl_newline(yylval.node);
9159#endif
9160 flush_string_content(p, enc, 0);
9161 return tSTRING_CONTENT;
9162 }
9163 tokadd(p, nextc(p));
9164 if (p->heredoc_indent > 0) {
9165 lex_goto_eol(p);
9166 goto flush;
9167 }
9168 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9169 if ((c = nextc(p)) == -1) goto error;
9170 } while (!whole_match_p(p, eos, len, indent));
9171 str = STR_NEW3(tok(p), toklen(p), enc, func);
9172 }
9173 dispatch_heredoc_end(p);
9174 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9175 token_flush(p);
9176 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9177#ifdef RIPPER
9178 /* Preserve s_value for set_yylval_str */
9179 s_value = p->s_value;
9180#endif
9181 set_yylval_str(str);
9182#ifdef RIPPER
9183 set_parser_s_value(s_value);
9184#endif
9185
9186#ifndef RIPPER
9187 if (bol) nd_set_fl_newline(yylval.node);
9188#endif
9189 return tSTRING_CONTENT;
9190}
9191
9192#include "lex.c"
9193
9194static int
9195arg_ambiguous(struct parser_params *p, char c)
9196{
9197#ifndef RIPPER
9198 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9199#else
9200 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9201#endif
9202 return TRUE;
9203}
9204
9205/* returns true value if formal argument error;
9206 * Qtrue, or error message if ripper */
9207static VALUE
9208formal_argument_error(struct parser_params *p, ID id)
9209{
9210 switch (id_type(id)) {
9211 case ID_LOCAL:
9212 break;
9213#ifndef RIPPER
9214# define ERR(mesg) (yyerror0(mesg), Qtrue)
9215#else
9216# define ERR(mesg) WARN_S(mesg)
9217#endif
9218 case ID_CONST:
9219 return ERR("formal argument cannot be a constant");
9220 case ID_INSTANCE:
9221 return ERR("formal argument cannot be an instance variable");
9222 case ID_GLOBAL:
9223 return ERR("formal argument cannot be a global variable");
9224 case ID_CLASS:
9225 return ERR("formal argument cannot be a class variable");
9226 default:
9227 return ERR("formal argument must be local variable");
9228#undef ERR
9229 }
9230 shadowing_lvar(p, id);
9231
9232 return Qfalse;
9233}
9234
9235static int
9236lvar_defined(struct parser_params *p, ID id)
9237{
9238 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9239}
9240
9241/* emacsen -*- hack */
9242static long
9243parser_encode_length(struct parser_params *p, const char *name, long len)
9244{
9245 long nlen;
9246
9247 if (len > 5 && name[nlen = len - 5] == '-') {
9248 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9249 return nlen;
9250 }
9251 if (len > 4 && name[nlen = len - 4] == '-') {
9252 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9253 return nlen;
9254 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9255 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9256 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9257 return nlen;
9258 }
9259 return len;
9260}
9261
9262static void
9263parser_set_encode(struct parser_params *p, const char *name)
9264{
9265 rb_encoding *enc;
9266 VALUE excargs[3];
9267 int idx = 0;
9268
9269 const char *wrong = 0;
9270 switch (*name) {
9271 case 'e': case 'E': wrong = "external"; break;
9272 case 'i': case 'I': wrong = "internal"; break;
9273 case 'f': case 'F': wrong = "filesystem"; break;
9274 case 'l': case 'L': wrong = "locale"; break;
9275 }
9276 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9277 idx = rb_enc_find_index(name);
9278 if (idx < 0) {
9279 unknown:
9280 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9281 error:
9282 excargs[0] = rb_eArgError;
9283 excargs[2] = rb_make_backtrace();
9284 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9285 VALUE exc = rb_make_exception(3, excargs);
9286 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9287
9288 rb_ast_free(p->ast);
9289 p->ast = NULL;
9290
9291 rb_exc_raise(exc);
9292 }
9293 enc = rb_enc_from_index(idx);
9294 if (!rb_enc_asciicompat(enc)) {
9295 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9296 goto error;
9297 }
9298 p->enc = enc;
9299#ifndef RIPPER
9300 if (p->debug_lines) {
9301 long i;
9302 for (i = 0; i < p->debug_lines->len; i++) {
9303 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9304 }
9305 }
9306#endif
9307}
9308
9309static bool
9310comment_at_top(struct parser_params *p)
9311{
9312 if (p->token_seen) return false;
9313 return (p->line_count == (p->has_shebang ? 2 : 1));
9314}
9315
9316typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9317typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9318
9319static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9320
9321static void
9322magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9323{
9324 if (!comment_at_top(p)) {
9325 return;
9326 }
9327 parser_set_encode(p, val);
9328}
9329
9330static int
9331parser_get_bool(struct parser_params *p, const char *name, const char *val)
9332{
9333 switch (*val) {
9334 case 't': case 'T':
9335 if (STRCASECMP(val, "true") == 0) {
9336 return TRUE;
9337 }
9338 break;
9339 case 'f': case 'F':
9340 if (STRCASECMP(val, "false") == 0) {
9341 return FALSE;
9342 }
9343 break;
9344 }
9345 return parser_invalid_pragma_value(p, name, val);
9346}
9347
9348static int
9349parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9350{
9351 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9352 return -1;
9353}
9354
9355static void
9356parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9357{
9358 int b = parser_get_bool(p, name, val);
9359 if (b >= 0) p->token_info_enabled = b;
9360}
9361
9362static void
9363parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9364{
9365 int b;
9366
9367 if (p->token_seen) {
9368 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9369 return;
9370 }
9371
9372 b = parser_get_bool(p, name, val);
9373 if (b < 0) return;
9374
9375 p->frozen_string_literal = b;
9376}
9377
9378static void
9379parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9380{
9381 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9382 if (*s == ' ' || *s == '\t') continue;
9383 if (*s == '#') break;
9384 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9385 return;
9386 }
9387
9388 switch (*val) {
9389 case 'n': case 'N':
9390 if (STRCASECMP(val, "none") == 0) {
9391 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9392 return;
9393 }
9394 break;
9395 case 'l': case 'L':
9396 if (STRCASECMP(val, "literal") == 0) {
9397 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9398 return;
9399 }
9400 break;
9401 case 'e': case 'E':
9402 if (STRCASECMP(val, "experimental_copy") == 0) {
9403 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9404 return;
9405 }
9406 if (STRCASECMP(val, "experimental_everything") == 0) {
9407 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9408 return;
9409 }
9410 break;
9411 }
9412 parser_invalid_pragma_value(p, name, val);
9413}
9414
9415# if WARN_PAST_SCOPE
9416static void
9417parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9418{
9419 int b = parser_get_bool(p, name, val);
9420 if (b >= 0) p->past_scope_enabled = b;
9421}
9422# endif
9423
9424struct magic_comment {
9425 const char *name;
9426 rb_magic_comment_setter_t func;
9427 rb_magic_comment_length_t length;
9428};
9429
9430static const struct magic_comment magic_comments[] = {
9431 {"coding", magic_comment_encoding, parser_encode_length},
9432 {"encoding", magic_comment_encoding, parser_encode_length},
9433 {"frozen_string_literal", parser_set_frozen_string_literal},
9434 {"shareable_constant_value", parser_set_shareable_constant_value},
9435 {"warn_indent", parser_set_token_info},
9436# if WARN_PAST_SCOPE
9437 {"warn_past_scope", parser_set_past_scope},
9438# endif
9439};
9440
9441static const char *
9442magic_comment_marker(const char *str, long len)
9443{
9444 long i = 2;
9445
9446 while (i < len) {
9447 switch (str[i]) {
9448 case '-':
9449 if (str[i-1] == '*' && str[i-2] == '-') {
9450 return str + i + 1;
9451 }
9452 i += 2;
9453 break;
9454 case '*':
9455 if (i + 1 >= len) return 0;
9456 if (str[i+1] != '-') {
9457 i += 4;
9458 }
9459 else if (str[i-1] != '-') {
9460 i += 2;
9461 }
9462 else {
9463 return str + i + 2;
9464 }
9465 break;
9466 default:
9467 i += 3;
9468 break;
9469 }
9470 }
9471 return 0;
9472}
9473
9474static int
9475parser_magic_comment(struct parser_params *p, const char *str, long len)
9476{
9477 int indicator = 0;
9478 VALUE name = 0, val = 0;
9479 const char *beg, *end, *vbeg, *vend;
9480#define str_copy(_s, _p, _n) ((_s) \
9481 ? (void)(rb_str_resize((_s), (_n)), \
9482 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9483 : (void)((_s) = STR_NEW((_p), (_n))))
9484
9485 if (len <= 7) return FALSE;
9486 if (!!(beg = magic_comment_marker(str, len))) {
9487 if (!(end = magic_comment_marker(beg, str + len - beg)))
9488 return FALSE;
9489 indicator = TRUE;
9490 str = beg;
9491 len = end - beg - 3;
9492 }
9493
9494 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9495 while (len > 0) {
9496 const struct magic_comment *mc = magic_comments;
9497 char *s;
9498 int i;
9499 long n = 0;
9500
9501 for (; len > 0 && *str; str++, --len) {
9502 switch (*str) {
9503 case '\'': case '"': case ':': case ';':
9504 continue;
9505 }
9506 if (!ISSPACE(*str)) break;
9507 }
9508 for (beg = str; len > 0; str++, --len) {
9509 switch (*str) {
9510 case '\'': case '"': case ':': case ';':
9511 break;
9512 default:
9513 if (ISSPACE(*str)) break;
9514 continue;
9515 }
9516 break;
9517 }
9518 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9519 if (!len) break;
9520 if (*str != ':') {
9521 if (!indicator) return FALSE;
9522 continue;
9523 }
9524
9525 do str++; while (--len > 0 && ISSPACE(*str));
9526 if (!len) break;
9527 const char *tok_beg = str;
9528 if (*str == '"') {
9529 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9530 if (*str == '\\') {
9531 --len;
9532 ++str;
9533 }
9534 }
9535 vend = str;
9536 if (len) {
9537 --len;
9538 ++str;
9539 }
9540 }
9541 else {
9542 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9543 vend = str;
9544 }
9545 const char *tok_end = str;
9546 if (indicator) {
9547 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9548 }
9549 else {
9550 while (len > 0 && (ISSPACE(*str))) --len, str++;
9551 if (len) return FALSE;
9552 }
9553
9554 n = end - beg;
9555 str_copy(name, beg, n);
9556 s = RSTRING_PTR(name);
9557 for (i = 0; i < n; ++i) {
9558 if (s[i] == '-') s[i] = '_';
9559 }
9560 do {
9561 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9562 n = vend - vbeg;
9563 if (mc->length) {
9564 n = (*mc->length)(p, vbeg, n);
9565 }
9566 str_copy(val, vbeg, n);
9567 p->lex.ptok = tok_beg;
9568 p->lex.pcur = tok_end;
9569 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9570 break;
9571 }
9572 } while (++mc < magic_comments + numberof(magic_comments));
9573#ifdef RIPPER
9574 str_copy(val, vbeg, vend - vbeg);
9575 dispatch2(magic_comment, name, val);
9576#endif
9577 }
9578
9579 return TRUE;
9580}
9581
9582static void
9583set_file_encoding(struct parser_params *p, const char *str, const char *send)
9584{
9585 int sep = 0;
9586 const char *beg = str;
9587 VALUE s;
9588
9589 for (;;) {
9590 if (send - str <= 6) return;
9591 switch (str[6]) {
9592 case 'C': case 'c': str += 6; continue;
9593 case 'O': case 'o': str += 5; continue;
9594 case 'D': case 'd': str += 4; continue;
9595 case 'I': case 'i': str += 3; continue;
9596 case 'N': case 'n': str += 2; continue;
9597 case 'G': case 'g': str += 1; continue;
9598 case '=': case ':':
9599 sep = 1;
9600 str += 6;
9601 break;
9602 default:
9603 str += 6;
9604 if (ISSPACE(*str)) break;
9605 continue;
9606 }
9607 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9608 sep = 0;
9609 }
9610 for (;;) {
9611 do {
9612 if (++str >= send) return;
9613 } while (ISSPACE(*str));
9614 if (sep) break;
9615 if (*str != '=' && *str != ':') return;
9616 sep = 1;
9617 str++;
9618 }
9619 beg = str;
9620 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9621 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9622 p->lex.ptok = beg;
9623 p->lex.pcur = str;
9624 parser_set_encode(p, RSTRING_PTR(s));
9625 rb_str_resize(s, 0);
9626}
9627
9628static void
9629parser_prepare(struct parser_params *p)
9630{
9631 int c = nextc0(p, FALSE);
9632 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9633 switch (c) {
9634 case '#':
9635 if (peek(p, '!')) p->has_shebang = 1;
9636 break;
9637 case 0xef: /* UTF-8 BOM marker */
9638 if (!lex_eol_n_p(p, 2) &&
9639 (unsigned char)p->lex.pcur[0] == 0xbb &&
9640 (unsigned char)p->lex.pcur[1] == 0xbf) {
9641 p->enc = rb_utf8_encoding();
9642 p->lex.pcur += 2;
9643#ifndef RIPPER
9644 if (p->debug_lines) {
9645 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9646 }
9647#endif
9648 p->lex.pbeg = p->lex.pcur;
9649 token_flush(p);
9650 return;
9651 }
9652 break;
9653 case -1: /* end of script. */
9654 return;
9655 }
9656 pushback(p, c);
9657 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9658}
9659
9660#ifndef RIPPER
9661#define ambiguous_operator(tok, op, syn) ( \
9662 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9663 rb_warning0("even though it seems like "syn""))
9664#else
9665#define ambiguous_operator(tok, op, syn) \
9666 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9667#endif
9668#define warn_balanced(tok, op, syn) ((void) \
9669 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9670 space_seen && !ISSPACE(c) && \
9671 (ambiguous_operator(tok, op, syn), 0)), \
9672 (enum yytokentype)(tok))
9673
9674static enum yytokentype
9675no_digits(struct parser_params *p)
9676{
9677 yyerror0("numeric literal without digits");
9678 if (peek(p, '_')) nextc(p);
9679 /* dummy 0, for tUMINUS_NUM at numeric */
9680 return set_number_literal(p, tINTEGER, 0, 10, 0);
9681}
9682
9683static enum yytokentype
9684parse_numeric(struct parser_params *p, int c)
9685{
9686 int is_float, seen_point, seen_e, nondigit;
9687 int suffix;
9688
9689 is_float = seen_point = seen_e = nondigit = 0;
9690 SET_LEX_STATE(EXPR_END);
9691 newtok(p);
9692 if (c == '-' || c == '+') {
9693 tokadd(p, c);
9694 c = nextc(p);
9695 }
9696 if (c == '0') {
9697 int start = toklen(p);
9698 c = nextc(p);
9699 if (c == 'x' || c == 'X') {
9700 /* hexadecimal */
9701 c = nextc(p);
9702 if (c != -1 && ISXDIGIT(c)) {
9703 do {
9704 if (c == '_') {
9705 if (nondigit) break;
9706 nondigit = c;
9707 continue;
9708 }
9709 if (!ISXDIGIT(c)) break;
9710 nondigit = 0;
9711 tokadd(p, c);
9712 } while ((c = nextc(p)) != -1);
9713 }
9714 pushback(p, c);
9715 tokfix(p);
9716 if (toklen(p) == start) {
9717 return no_digits(p);
9718 }
9719 else if (nondigit) goto trailing_uc;
9720 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9721 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9722 }
9723 if (c == 'b' || c == 'B') {
9724 /* binary */
9725 c = nextc(p);
9726 if (c == '0' || c == '1') {
9727 do {
9728 if (c == '_') {
9729 if (nondigit) break;
9730 nondigit = c;
9731 continue;
9732 }
9733 if (c != '0' && c != '1') break;
9734 nondigit = 0;
9735 tokadd(p, c);
9736 } while ((c = nextc(p)) != -1);
9737 }
9738 pushback(p, c);
9739 tokfix(p);
9740 if (toklen(p) == start) {
9741 return no_digits(p);
9742 }
9743 else if (nondigit) goto trailing_uc;
9744 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9745 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9746 }
9747 if (c == 'd' || c == 'D') {
9748 /* decimal */
9749 c = nextc(p);
9750 if (c != -1 && ISDIGIT(c)) {
9751 do {
9752 if (c == '_') {
9753 if (nondigit) break;
9754 nondigit = c;
9755 continue;
9756 }
9757 if (!ISDIGIT(c)) break;
9758 nondigit = 0;
9759 tokadd(p, c);
9760 } while ((c = nextc(p)) != -1);
9761 }
9762 pushback(p, c);
9763 tokfix(p);
9764 if (toklen(p) == start) {
9765 return no_digits(p);
9766 }
9767 else if (nondigit) goto trailing_uc;
9768 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9769 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9770 }
9771 if (c == '_') {
9772 /* 0_0 */
9773 goto octal_number;
9774 }
9775 if (c == 'o' || c == 'O') {
9776 /* prefixed octal */
9777 c = nextc(p);
9778 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9779 tokfix(p);
9780 return no_digits(p);
9781 }
9782 }
9783 if (c >= '0' && c <= '7') {
9784 /* octal */
9785 octal_number:
9786 do {
9787 if (c == '_') {
9788 if (nondigit) break;
9789 nondigit = c;
9790 continue;
9791 }
9792 if (c < '0' || c > '9') break;
9793 if (c > '7') goto invalid_octal;
9794 nondigit = 0;
9795 tokadd(p, c);
9796 } while ((c = nextc(p)) != -1);
9797 if (toklen(p) > start) {
9798 pushback(p, c);
9799 tokfix(p);
9800 if (nondigit) goto trailing_uc;
9801 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9802 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9803 }
9804 if (nondigit) {
9805 pushback(p, c);
9806 goto trailing_uc;
9807 }
9808 }
9809 if (c > '7' && c <= '9') {
9810 invalid_octal:
9811 yyerror0("Invalid octal digit");
9812 }
9813 else if (c == '.' || c == 'e' || c == 'E') {
9814 tokadd(p, '0');
9815 }
9816 else {
9817 pushback(p, c);
9818 tokfix(p);
9819 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9820 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9821 }
9822 }
9823
9824 for (;;) {
9825 switch (c) {
9826 case '0': case '1': case '2': case '3': case '4':
9827 case '5': case '6': case '7': case '8': case '9':
9828 nondigit = 0;
9829 tokadd(p, c);
9830 break;
9831
9832 case '.':
9833 if (nondigit) goto trailing_uc;
9834 if (seen_point || seen_e) {
9835 goto decode_num;
9836 }
9837 else {
9838 int c0 = nextc(p);
9839 if (c0 == -1 || !ISDIGIT(c0)) {
9840 pushback(p, c0);
9841 goto decode_num;
9842 }
9843 c = c0;
9844 }
9845 seen_point = toklen(p);
9846 tokadd(p, '.');
9847 tokadd(p, c);
9848 is_float++;
9849 nondigit = 0;
9850 break;
9851
9852 case 'e':
9853 case 'E':
9854 if (nondigit) {
9855 pushback(p, c);
9856 c = nondigit;
9857 goto decode_num;
9858 }
9859 if (seen_e) {
9860 goto decode_num;
9861 }
9862 nondigit = c;
9863 c = nextc(p);
9864 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9865 pushback(p, c);
9866 c = nondigit;
9867 nondigit = 0;
9868 goto decode_num;
9869 }
9870 tokadd(p, nondigit);
9871 seen_e++;
9872 is_float++;
9873 tokadd(p, c);
9874 nondigit = (c == '-' || c == '+') ? c : 0;
9875 break;
9876
9877 case '_': /* `_' in number just ignored */
9878 if (nondigit) goto decode_num;
9879 nondigit = c;
9880 break;
9881
9882 default:
9883 goto decode_num;
9884 }
9885 c = nextc(p);
9886 }
9887
9888 decode_num:
9889 pushback(p, c);
9890 if (nondigit) {
9891 trailing_uc:
9892 literal_flush(p, p->lex.pcur - 1);
9893 YYLTYPE loc = RUBY_INIT_YYLLOC();
9894 compile_error(p, "trailing '%c' in number", nondigit);
9895 parser_show_error_line(p, &loc);
9896 }
9897 tokfix(p);
9898 if (is_float) {
9899 enum yytokentype type = tFLOAT;
9900
9901 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9902 if (suffix & NUM_SUFFIX_R) {
9903 type = tRATIONAL;
9904 }
9905 else {
9906 errno = 0;
9907 strtod(tok(p), 0);
9908 if (errno == ERANGE) {
9909 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9910 errno = 0;
9911 }
9912 }
9913 return set_number_literal(p, type, suffix, 0, seen_point);
9914 }
9915 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9916 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9917}
9918
9919static enum yytokentype
9920parse_qmark(struct parser_params *p, int space_seen)
9921{
9922 rb_encoding *enc;
9923 register int c;
9924 rb_parser_string_t *lit;
9925 const char *start = p->lex.pcur;
9926
9927 if (IS_END()) {
9928 SET_LEX_STATE(EXPR_VALUE);
9929 return '?';
9930 }
9931 c = nextc(p);
9932 if (c == -1) {
9933 compile_error(p, "incomplete character syntax");
9934 return 0;
9935 }
9936 if (rb_enc_isspace(c, p->enc)) {
9937 if (!IS_ARG()) {
9938 int c2 = escaped_control_code(c);
9939 if (c2) {
9940 WARN_SPACE_CHAR(c2, "?");
9941 }
9942 }
9943 ternary:
9944 pushback(p, c);
9945 SET_LEX_STATE(EXPR_VALUE);
9946 return '?';
9947 }
9948 newtok(p);
9949 enc = p->enc;
9950 int w = parser_precise_mbclen(p, start);
9951 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9952 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9953 if (space_seen) {
9954 const char *ptr = start;
9955 do {
9956 int n = parser_precise_mbclen(p, ptr);
9957 if (n < 0) return -1;
9958 ptr += n;
9959 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9960 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9961 " a conditional operator, put a space after '?'",
9962 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9963 }
9964 goto ternary;
9965 }
9966 else if (c == '\\') {
9967 if (peek(p, 'u')) {
9968 nextc(p);
9969 enc = rb_utf8_encoding();
9970 tokadd_utf8(p, &enc, -1, 0, 0);
9971 }
9972 else if (!ISASCII(c = peekc(p)) && c != -1) {
9973 nextc(p);
9974 if (tokadd_mbchar(p, c) == -1) return 0;
9975 }
9976 else {
9977 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9978 tokadd(p, c);
9979 }
9980 }
9981 else {
9982 if (tokadd_mbchar(p, c) == -1) return 0;
9983 }
9984 tokfix(p);
9985 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9986 set_yylval_str(lit);
9987 SET_LEX_STATE(EXPR_END);
9988 return tCHAR;
9989}
9990
9991static enum yytokentype
9992parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9993{
9994 register int c;
9995 const char *ptok = p->lex.pcur;
9996
9997 if (IS_BEG()) {
9998 int term;
9999 int paren;
10000
10001 c = nextc(p);
10002 quotation:
10003 if (c == -1) goto unterminated;
10004 if (!ISALNUM(c)) {
10005 term = c;
10006 if (!ISASCII(c)) goto unknown;
10007 c = 'Q';
10008 }
10009 else {
10010 term = nextc(p);
10011 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10012 unknown:
10013 pushback(p, term);
10014 c = parser_precise_mbclen(p, p->lex.pcur);
10015 if (c < 0) return 0;
10016 p->lex.pcur += c;
10017 yyerror0("unknown type of %string");
10018 return 0;
10019 }
10020 }
10021 if (term == -1) {
10022 unterminated:
10023 compile_error(p, "unterminated quoted string meets end of file");
10024 return 0;
10025 }
10026 paren = term;
10027 if (term == '(') term = ')';
10028 else if (term == '[') term = ']';
10029 else if (term == '{') term = '}';
10030 else if (term == '<') term = '>';
10031 else paren = 0;
10032
10033 p->lex.ptok = ptok-1;
10034 switch (c) {
10035 case 'Q':
10036 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10037 return tSTRING_BEG;
10038
10039 case 'q':
10040 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10041 return tSTRING_BEG;
10042
10043 case 'W':
10044 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10045 return tWORDS_BEG;
10046
10047 case 'w':
10048 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10049 return tQWORDS_BEG;
10050
10051 case 'I':
10052 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10053 return tSYMBOLS_BEG;
10054
10055 case 'i':
10056 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10057 return tQSYMBOLS_BEG;
10058
10059 case 'x':
10060 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10061 return tXSTRING_BEG;
10062
10063 case 'r':
10064 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10065 return tREGEXP_BEG;
10066
10067 case 's':
10068 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10069 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10070 return tSYMBEG;
10071
10072 default:
10073 yyerror0("unknown type of %string");
10074 return 0;
10075 }
10076 }
10077 if ((c = nextc(p)) == '=') {
10078 set_yylval_id('%');
10079 SET_LEX_STATE(EXPR_BEG);
10080 return tOP_ASGN;
10081 }
10082 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10083 goto quotation;
10084 }
10085 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10086 pushback(p, c);
10087 return warn_balanced('%', "%%", "string literal");
10088}
10089
10090static int
10091tokadd_ident(struct parser_params *p, int c)
10092{
10093 do {
10094 if (tokadd_mbchar(p, c) == -1) return -1;
10095 c = nextc(p);
10096 } while (parser_is_identchar(p));
10097 pushback(p, c);
10098 return 0;
10099}
10100
10101static ID
10102tokenize_ident(struct parser_params *p)
10103{
10104 ID ident = TOK_INTERN();
10105
10106 set_yylval_name(ident);
10107
10108 return ident;
10109}
10110
10111static int
10112parse_numvar(struct parser_params *p)
10113{
10114 size_t len;
10115 int overflow;
10116 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10117 const unsigned long nth_ref_max =
10118 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10119 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10120 * turned into a Fixnum, in compile.c */
10121
10122 if (overflow || n > nth_ref_max) {
10123 /* compile_error()? */
10124 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10125 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10126 }
10127 else {
10128 return (int)n;
10129 }
10130}
10131
10132static enum yytokentype
10133parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10134{
10135 const char *ptr = p->lex.pcur;
10136 register int c;
10137
10138 SET_LEX_STATE(EXPR_END);
10139 p->lex.ptok = ptr - 1; /* from '$' */
10140 newtok(p);
10141 c = nextc(p);
10142 switch (c) {
10143 case '_': /* $_: last read line string */
10144 c = nextc(p);
10145 if (parser_is_identchar(p)) {
10146 tokadd(p, '$');
10147 tokadd(p, '_');
10148 break;
10149 }
10150 pushback(p, c);
10151 c = '_';
10152 /* fall through */
10153 case '~': /* $~: match-data */
10154 case '*': /* $*: argv */
10155 case '$': /* $$: pid */
10156 case '?': /* $?: last status */
10157 case '!': /* $!: error string */
10158 case '@': /* $@: error position */
10159 case '/': /* $/: input record separator */
10160 case '\\': /* $\: output record separator */
10161 case ';': /* $;: field separator */
10162 case ',': /* $,: output field separator */
10163 case '.': /* $.: last read line number */
10164 case '=': /* $=: ignorecase */
10165 case ':': /* $:: load path */
10166 case '<': /* $<: default input handle */
10167 case '>': /* $>: default output handle */
10168 case '\"': /* $": already loaded files */
10169 tokadd(p, '$');
10170 tokadd(p, c);
10171 goto gvar;
10172
10173 case '-':
10174 tokadd(p, '$');
10175 tokadd(p, c);
10176 c = nextc(p);
10177 if (parser_is_identchar(p)) {
10178 if (tokadd_mbchar(p, c) == -1) return 0;
10179 }
10180 else {
10181 pushback(p, c);
10182 pushback(p, '-');
10183 return '$';
10184 }
10185 gvar:
10186 tokenize_ident(p);
10187 return tGVAR;
10188
10189 case '&': /* $&: last match */
10190 case '`': /* $`: string before last match */
10191 case '\'': /* $': string after last match */
10192 case '+': /* $+: string matches last paren. */
10193 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10194 tokadd(p, '$');
10195 tokadd(p, c);
10196 goto gvar;
10197 }
10198 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10199 return tBACK_REF;
10200
10201 case '1': case '2': case '3':
10202 case '4': case '5': case '6':
10203 case '7': case '8': case '9':
10204 tokadd(p, '$');
10205 do {
10206 tokadd(p, c);
10207 c = nextc(p);
10208 } while (c != -1 && ISDIGIT(c));
10209 pushback(p, c);
10210 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10211 tokfix(p);
10212 c = parse_numvar(p);
10213 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10214 return tNTH_REF;
10215
10216 default:
10217 if (!parser_is_identchar(p)) {
10218 YYLTYPE loc = RUBY_INIT_YYLLOC();
10219 if (c == -1 || ISSPACE(c)) {
10220 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10221 }
10222 else {
10223 pushback(p, c);
10224 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10225 }
10226 parser_show_error_line(p, &loc);
10227 set_yylval_noname();
10228 return tGVAR;
10229 }
10230 /* fall through */
10231 case '0':
10232 tokadd(p, '$');
10233 }
10234
10235 if (tokadd_ident(p, c)) return 0;
10236 SET_LEX_STATE(EXPR_END);
10237 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10238 tokenize_ident(p);
10239 }
10240 else {
10241 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10242 set_yylval_noname();
10243 }
10244 return tGVAR;
10245}
10246
10247static bool
10248parser_numbered_param(struct parser_params *p, int n)
10249{
10250 if (n < 0) return false;
10251
10252 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10253 return false;
10254 }
10255 if (p->max_numparam == ORDINAL_PARAM) {
10256 compile_error(p, "ordinary parameter is defined");
10257 return false;
10258 }
10259 struct vtable *args = p->lvtbl->args;
10260 if (p->max_numparam < n) {
10261 p->max_numparam = n;
10262 }
10263 while (n > args->pos) {
10264 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10265 }
10266 return true;
10267}
10268
10269static enum yytokentype
10270parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10271{
10272 const char *ptr = p->lex.pcur;
10273 enum yytokentype result = tIVAR;
10274 register int c = nextc(p);
10275 YYLTYPE loc;
10276
10277 p->lex.ptok = ptr - 1; /* from '@' */
10278 newtok(p);
10279 tokadd(p, '@');
10280 if (c == '@') {
10281 result = tCVAR;
10282 tokadd(p, '@');
10283 c = nextc(p);
10284 }
10285 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10286 if (c == -1 || !parser_is_identchar(p)) {
10287 pushback(p, c);
10288 RUBY_SET_YYLLOC(loc);
10289 if (result == tIVAR) {
10290 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10291 }
10292 else {
10293 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10294 }
10295 parser_show_error_line(p, &loc);
10296 set_yylval_noname();
10297 SET_LEX_STATE(EXPR_END);
10298 return result;
10299 }
10300 else if (ISDIGIT(c)) {
10301 pushback(p, c);
10302 RUBY_SET_YYLLOC(loc);
10303 if (result == tIVAR) {
10304 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10305 }
10306 else {
10307 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10308 }
10309 parser_show_error_line(p, &loc);
10310 set_yylval_noname();
10311 SET_LEX_STATE(EXPR_END);
10312 return result;
10313 }
10314
10315 if (tokadd_ident(p, c)) return 0;
10316 tokenize_ident(p);
10317 return result;
10318}
10319
10320static enum yytokentype
10321parse_ident(struct parser_params *p, int c, int cmd_state)
10322{
10323 enum yytokentype result;
10324 bool is_ascii = true;
10325 const enum lex_state_e last_state = p->lex.state;
10326 ID ident;
10327 int enforce_keyword_end = 0;
10328
10329 do {
10330 if (!ISASCII(c)) is_ascii = false;
10331 if (tokadd_mbchar(p, c) == -1) return 0;
10332 c = nextc(p);
10333 } while (parser_is_identchar(p));
10334 if ((c == '!' || c == '?') && !peek(p, '=')) {
10335 result = tFID;
10336 tokadd(p, c);
10337 }
10338 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10339 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10340 result = tIDENTIFIER;
10341 tokadd(p, c);
10342 }
10343 else {
10344 result = tCONSTANT; /* assume provisionally */
10345 pushback(p, c);
10346 }
10347 tokfix(p);
10348
10349 if (IS_LABEL_POSSIBLE()) {
10350 if (IS_LABEL_SUFFIX(0)) {
10351 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10352 nextc(p);
10353 tokenize_ident(p);
10354 return tLABEL;
10355 }
10356 }
10357
10358#ifndef RIPPER
10359 if (peek_end_expect_token_locations(p)) {
10360 const rb_code_position_t *end_pos;
10361 int lineno, column;
10362 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
10363
10364 end_pos = peek_end_expect_token_locations(p)->pos;
10365 lineno = end_pos->lineno;
10366 column = end_pos->column;
10367
10368 if (p->debug) {
10369 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10370 p->ruby_sourceline, beg_pos, lineno, column);
10371 }
10372
10373 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10374 const struct kwtable *kw;
10375
10376 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10377 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10378 enforce_keyword_end = 1;
10379 }
10380 }
10381 }
10382#endif
10383
10384 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10385 const struct kwtable *kw;
10386
10387 /* See if it is a reserved word. */
10388 kw = rb_reserved_word(tok(p), toklen(p));
10389 if (kw) {
10390 enum lex_state_e state = p->lex.state;
10391 if (IS_lex_state_for(state, EXPR_FNAME)) {
10392 SET_LEX_STATE(EXPR_ENDFN);
10393 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10394 return kw->id[0];
10395 }
10396 SET_LEX_STATE(kw->state);
10397 if (IS_lex_state(EXPR_BEG)) {
10398 p->command_start = TRUE;
10399 }
10400 if (kw->id[0] == keyword_do) {
10401 if (lambda_beginning_p()) {
10402 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10403 return keyword_do_LAMBDA;
10404 }
10405 if (COND_P()) return keyword_do_cond;
10406 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10407 return keyword_do_block;
10408 return keyword_do;
10409 }
10410 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10411 return kw->id[0];
10412 else {
10413 if (kw->id[0] != kw->id[1])
10414 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10415 return kw->id[1];
10416 }
10417 }
10418 }
10419
10420 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10421 if (cmd_state) {
10422 SET_LEX_STATE(EXPR_CMDARG);
10423 }
10424 else {
10425 SET_LEX_STATE(EXPR_ARG);
10426 }
10427 }
10428 else if (p->lex.state == EXPR_FNAME) {
10429 SET_LEX_STATE(EXPR_ENDFN);
10430 }
10431 else {
10432 SET_LEX_STATE(EXPR_END);
10433 }
10434
10435 ident = tokenize_ident(p);
10436 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10437 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10438 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10439 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10440 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10441 }
10442 return result;
10443}
10444
10445static void
10446warn_cr(struct parser_params *p)
10447{
10448 if (!p->cr_seen) {
10449 p->cr_seen = TRUE;
10450 /* carried over with p->lex.nextline for nextc() */
10451 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10452 }
10453}
10454
10455static enum yytokentype
10456parser_yylex(struct parser_params *p)
10457{
10458 register int c;
10459 int space_seen = 0;
10460 int cmd_state;
10461 int label;
10462 enum lex_state_e last_state;
10463 int fallthru = FALSE;
10464 int token_seen = p->token_seen;
10465
10466 if (p->lex.strterm) {
10467 if (strterm_is_heredoc(p->lex.strterm)) {
10468 token_flush(p);
10469 return here_document(p, &p->lex.strterm->u.heredoc);
10470 }
10471 else {
10472 token_flush(p);
10473 return parse_string(p, &p->lex.strterm->u.literal);
10474 }
10475 }
10476 cmd_state = p->command_start;
10477 p->command_start = FALSE;
10478 p->token_seen = TRUE;
10479#ifndef RIPPER
10480 token_flush(p);
10481#endif
10482 retry:
10483 last_state = p->lex.state;
10484 switch (c = nextc(p)) {
10485 case '\0': /* NUL */
10486 case '\004': /* ^D */
10487 case '\032': /* ^Z */
10488 case -1: /* end of script. */
10489 p->eofp = 1;
10490#ifndef RIPPER
10491 if (p->end_expect_token_locations) {
10492 pop_end_expect_token_locations(p);
10493 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10494 return tDUMNY_END;
10495 }
10496#endif
10497 /* Set location for end-of-input because dispatch_scan_event is not called. */
10498 RUBY_SET_YYLLOC(*p->yylloc);
10499 return END_OF_INPUT;
10500
10501 /* white spaces */
10502 case '\r':
10503 warn_cr(p);
10504 /* fall through */
10505 case ' ': case '\t': case '\f':
10506 case '\13': /* '\v' */
10507 space_seen = 1;
10508 while ((c = nextc(p))) {
10509 switch (c) {
10510 case '\r':
10511 warn_cr(p);
10512 /* fall through */
10513 case ' ': case '\t': case '\f':
10514 case '\13': /* '\v' */
10515 break;
10516 default:
10517 goto outofloop;
10518 }
10519 }
10520 outofloop:
10521 pushback(p, c);
10522 dispatch_scan_event(p, tSP);
10523#ifndef RIPPER
10524 token_flush(p);
10525#endif
10526 goto retry;
10527
10528 case '#': /* it's a comment */
10529 p->token_seen = token_seen;
10530 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10531 /* no magic_comment in shebang line */
10532 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10533 if (comment_at_top(p)) {
10534 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10535 }
10536 }
10537 p->lex.pcur = pcur, p->lex.ptok = ptok;
10538 lex_goto_eol(p);
10539 dispatch_scan_event(p, tCOMMENT);
10540 fallthru = TRUE;
10541 /* fall through */
10542 case '\n':
10543 p->token_seen = token_seen;
10544 rb_parser_string_t *prevline = p->lex.lastline;
10545 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10546 !IS_lex_state(EXPR_LABELED));
10547 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10548 if (!fallthru) {
10549 dispatch_scan_event(p, tIGNORED_NL);
10550 }
10551 fallthru = FALSE;
10552 if (!c && p->ctxt.in_kwarg) {
10553 goto normal_newline;
10554 }
10555 goto retry;
10556 }
10557 while (1) {
10558 switch (c = nextc(p)) {
10559 case ' ': case '\t': case '\f': case '\r':
10560 case '\13': /* '\v' */
10561 space_seen = 1;
10562 break;
10563 case '#':
10564 pushback(p, c);
10565 if (space_seen) {
10566 dispatch_scan_event(p, tSP);
10567 token_flush(p);
10568 }
10569 goto retry;
10570 case 'a':
10571 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10572 goto bol;
10573 case 'o':
10574 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10575 goto bol;
10576 case '|':
10577 if (peek(p, '|')) goto leading_logical;
10578 goto bol;
10579 case '&':
10580 if (peek(p, '&')) {
10581 leading_logical:
10582 pushback(p, c);
10583 dispatch_delayed_token(p, tIGNORED_NL);
10584 cmd_state = FALSE;
10585 goto retry;
10586 }
10587 /* fall through */
10588 case '.': {
10589 dispatch_delayed_token(p, tIGNORED_NL);
10590 if (peek(p, '.') == (c == '&')) {
10591 pushback(p, c);
10592 dispatch_scan_event(p, tSP);
10593 goto retry;
10594 }
10595 }
10596 bol:
10597 default:
10598 p->ruby_sourceline--;
10599 p->lex.nextline = p->lex.lastline;
10600 set_lastline(p, prevline);
10601 case -1: /* EOF no decrement*/
10602 if (c == -1 && space_seen) {
10603 dispatch_scan_event(p, tSP);
10604 }
10605 lex_goto_eol(p);
10606 if (c != -1) {
10607 token_flush(p);
10608 RUBY_SET_YYLLOC(*p->yylloc);
10609 }
10610 goto normal_newline;
10611 }
10612 }
10613 normal_newline:
10614 p->command_start = TRUE;
10615 SET_LEX_STATE(EXPR_BEG);
10616 return '\n';
10617
10618 case '*':
10619 if ((c = nextc(p)) == '*') {
10620 if ((c = nextc(p)) == '=') {
10621 set_yylval_id(idPow);
10622 SET_LEX_STATE(EXPR_BEG);
10623 return tOP_ASGN;
10624 }
10625 pushback(p, c);
10626 if (IS_SPCARG(c)) {
10627 rb_warning0("'**' interpreted as argument prefix");
10628 c = tDSTAR;
10629 }
10630 else if (IS_BEG()) {
10631 c = tDSTAR;
10632 }
10633 else {
10634 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10635 }
10636 }
10637 else {
10638 if (c == '=') {
10639 set_yylval_id('*');
10640 SET_LEX_STATE(EXPR_BEG);
10641 return tOP_ASGN;
10642 }
10643 pushback(p, c);
10644 if (IS_SPCARG(c)) {
10645 rb_warning0("'*' interpreted as argument prefix");
10646 c = tSTAR;
10647 }
10648 else if (IS_BEG()) {
10649 c = tSTAR;
10650 }
10651 else {
10652 c = warn_balanced('*', "*", "argument prefix");
10653 }
10654 }
10655 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10656 return c;
10657
10658 case '!':
10659 c = nextc(p);
10660 if (IS_AFTER_OPERATOR()) {
10661 SET_LEX_STATE(EXPR_ARG);
10662 if (c == '@') {
10663 return '!';
10664 }
10665 }
10666 else {
10667 SET_LEX_STATE(EXPR_BEG);
10668 }
10669 if (c == '=') {
10670 return tNEQ;
10671 }
10672 if (c == '~') {
10673 return tNMATCH;
10674 }
10675 pushback(p, c);
10676 return '!';
10677
10678 case '=':
10679 if (was_bol(p)) {
10680 /* skip embedded rd document */
10681 if (word_match_p(p, "begin", 5)) {
10682 int first_p = TRUE;
10683
10684 lex_goto_eol(p);
10685 dispatch_scan_event(p, tEMBDOC_BEG);
10686 for (;;) {
10687 lex_goto_eol(p);
10688 if (!first_p) {
10689 dispatch_scan_event(p, tEMBDOC);
10690 }
10691 first_p = FALSE;
10692 c = nextc(p);
10693 if (c == -1) {
10694 compile_error(p, "embedded document meets end of file");
10695 return END_OF_INPUT;
10696 }
10697 if (c == '=' && word_match_p(p, "end", 3)) {
10698 break;
10699 }
10700 pushback(p, c);
10701 }
10702 lex_goto_eol(p);
10703 dispatch_scan_event(p, tEMBDOC_END);
10704 goto retry;
10705 }
10706 }
10707
10708 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10709 if ((c = nextc(p)) == '=') {
10710 if ((c = nextc(p)) == '=') {
10711 return tEQQ;
10712 }
10713 pushback(p, c);
10714 return tEQ;
10715 }
10716 if (c == '~') {
10717 return tMATCH;
10718 }
10719 else if (c == '>') {
10720 return tASSOC;
10721 }
10722 pushback(p, c);
10723 return '=';
10724
10725 case '<':
10726 c = nextc(p);
10727 if (c == '<' &&
10728 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10729 !IS_END() &&
10730 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10731 enum yytokentype token = heredoc_identifier(p);
10732 if (token) return token < 0 ? 0 : token;
10733 }
10734 if (IS_AFTER_OPERATOR()) {
10735 SET_LEX_STATE(EXPR_ARG);
10736 }
10737 else {
10738 if (IS_lex_state(EXPR_CLASS))
10739 p->command_start = TRUE;
10740 SET_LEX_STATE(EXPR_BEG);
10741 }
10742 if (c == '=') {
10743 if ((c = nextc(p)) == '>') {
10744 return tCMP;
10745 }
10746 pushback(p, c);
10747 return tLEQ;
10748 }
10749 if (c == '<') {
10750 if ((c = nextc(p)) == '=') {
10751 set_yylval_id(idLTLT);
10752 SET_LEX_STATE(EXPR_BEG);
10753 return tOP_ASGN;
10754 }
10755 pushback(p, c);
10756 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10757 }
10758 pushback(p, c);
10759 return '<';
10760
10761 case '>':
10762 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10763 if ((c = nextc(p)) == '=') {
10764 return tGEQ;
10765 }
10766 if (c == '>') {
10767 if ((c = nextc(p)) == '=') {
10768 set_yylval_id(idGTGT);
10769 SET_LEX_STATE(EXPR_BEG);
10770 return tOP_ASGN;
10771 }
10772 pushback(p, c);
10773 return tRSHFT;
10774 }
10775 pushback(p, c);
10776 return '>';
10777
10778 case '"':
10779 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10780 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10781 p->lex.ptok = p->lex.pcur-1;
10782 return tSTRING_BEG;
10783
10784 case '`':
10785 if (IS_lex_state(EXPR_FNAME)) {
10786 SET_LEX_STATE(EXPR_ENDFN);
10787 return c;
10788 }
10789 if (IS_lex_state(EXPR_DOT)) {
10790 if (cmd_state)
10791 SET_LEX_STATE(EXPR_CMDARG);
10792 else
10793 SET_LEX_STATE(EXPR_ARG);
10794 return c;
10795 }
10796 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10797 return tXSTRING_BEG;
10798
10799 case '\'':
10800 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10801 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10802 p->lex.ptok = p->lex.pcur-1;
10803 return tSTRING_BEG;
10804
10805 case '?':
10806 return parse_qmark(p, space_seen);
10807
10808 case '&':
10809 if ((c = nextc(p)) == '&') {
10810 SET_LEX_STATE(EXPR_BEG);
10811 if ((c = nextc(p)) == '=') {
10812 set_yylval_id(idANDOP);
10813 SET_LEX_STATE(EXPR_BEG);
10814 return tOP_ASGN;
10815 }
10816 pushback(p, c);
10817 return tANDOP;
10818 }
10819 else if (c == '=') {
10820 set_yylval_id('&');
10821 SET_LEX_STATE(EXPR_BEG);
10822 return tOP_ASGN;
10823 }
10824 else if (c == '.') {
10825 set_yylval_id(idANDDOT);
10826 SET_LEX_STATE(EXPR_DOT);
10827 return tANDDOT;
10828 }
10829 pushback(p, c);
10830 if (IS_SPCARG(c)) {
10831 if ((c != ':') ||
10832 (c = peekc_n(p, 1)) == -1 ||
10833 !(c == '\'' || c == '"' ||
10834 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10835 rb_warning0("'&' interpreted as argument prefix");
10836 }
10837 c = tAMPER;
10838 }
10839 else if (IS_BEG()) {
10840 c = tAMPER;
10841 }
10842 else {
10843 c = warn_balanced('&', "&", "argument prefix");
10844 }
10845 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10846 return c;
10847
10848 case '|':
10849 if ((c = nextc(p)) == '|') {
10850 SET_LEX_STATE(EXPR_BEG);
10851 if ((c = nextc(p)) == '=') {
10852 set_yylval_id(idOROP);
10853 SET_LEX_STATE(EXPR_BEG);
10854 return tOP_ASGN;
10855 }
10856 pushback(p, c);
10857 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10858 c = '|';
10859 pushback(p, '|');
10860 return c;
10861 }
10862 return tOROP;
10863 }
10864 if (c == '=') {
10865 set_yylval_id('|');
10866 SET_LEX_STATE(EXPR_BEG);
10867 return tOP_ASGN;
10868 }
10869 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10870 pushback(p, c);
10871 return '|';
10872
10873 case '+':
10874 c = nextc(p);
10875 if (IS_AFTER_OPERATOR()) {
10876 SET_LEX_STATE(EXPR_ARG);
10877 if (c == '@') {
10878 return tUPLUS;
10879 }
10880 pushback(p, c);
10881 return '+';
10882 }
10883 if (c == '=') {
10884 set_yylval_id('+');
10885 SET_LEX_STATE(EXPR_BEG);
10886 return tOP_ASGN;
10887 }
10888 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10889 SET_LEX_STATE(EXPR_BEG);
10890 pushback(p, c);
10891 if (c != -1 && ISDIGIT(c)) {
10892 return parse_numeric(p, '+');
10893 }
10894 return tUPLUS;
10895 }
10896 SET_LEX_STATE(EXPR_BEG);
10897 pushback(p, c);
10898 return warn_balanced('+', "+", "unary operator");
10899
10900 case '-':
10901 c = nextc(p);
10902 if (IS_AFTER_OPERATOR()) {
10903 SET_LEX_STATE(EXPR_ARG);
10904 if (c == '@') {
10905 return tUMINUS;
10906 }
10907 pushback(p, c);
10908 return '-';
10909 }
10910 if (c == '=') {
10911 set_yylval_id('-');
10912 SET_LEX_STATE(EXPR_BEG);
10913 return tOP_ASGN;
10914 }
10915 if (c == '>') {
10916 SET_LEX_STATE(EXPR_ENDFN);
10917 yylval.num = p->lex.lpar_beg;
10918 p->lex.lpar_beg = p->lex.paren_nest;
10919 return tLAMBDA;
10920 }
10921 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10922 SET_LEX_STATE(EXPR_BEG);
10923 pushback(p, c);
10924 if (c != -1 && ISDIGIT(c)) {
10925 return tUMINUS_NUM;
10926 }
10927 return tUMINUS;
10928 }
10929 SET_LEX_STATE(EXPR_BEG);
10930 pushback(p, c);
10931 return warn_balanced('-', "-", "unary operator");
10932
10933 case '.': {
10934 int is_beg = IS_BEG();
10935 SET_LEX_STATE(EXPR_BEG);
10936 if ((c = nextc(p)) == '.') {
10937 if ((c = nextc(p)) == '.') {
10938 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10939 SET_LEX_STATE(EXPR_ENDARG);
10940 return tBDOT3;
10941 }
10942 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10943 rb_warn0("... at EOL, should be parenthesized?");
10944 }
10945 return is_beg ? tBDOT3 : tDOT3;
10946 }
10947 pushback(p, c);
10948 return is_beg ? tBDOT2 : tDOT2;
10949 }
10950 pushback(p, c);
10951 if (c != -1 && ISDIGIT(c)) {
10952 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10953 parse_numeric(p, '.');
10954 if (ISDIGIT(prev)) {
10955 yyerror0("unexpected fraction part after numeric literal");
10956 }
10957 else {
10958 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10959 }
10960 SET_LEX_STATE(EXPR_END);
10961 p->lex.ptok = p->lex.pcur;
10962 goto retry;
10963 }
10964 set_yylval_id('.');
10965 SET_LEX_STATE(EXPR_DOT);
10966 return '.';
10967 }
10968
10969 case '0': case '1': case '2': case '3': case '4':
10970 case '5': case '6': case '7': case '8': case '9':
10971 return parse_numeric(p, c);
10972
10973 case ')':
10974 COND_POP();
10975 CMDARG_POP();
10976 SET_LEX_STATE(EXPR_ENDFN);
10977 p->lex.paren_nest--;
10978 return c;
10979
10980 case ']':
10981 COND_POP();
10982 CMDARG_POP();
10983 SET_LEX_STATE(EXPR_END);
10984 p->lex.paren_nest--;
10985 return c;
10986
10987 case '}':
10988 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10989 if (!p->lex.brace_nest--) return tSTRING_DEND;
10990 COND_POP();
10991 CMDARG_POP();
10992 SET_LEX_STATE(EXPR_END);
10993 p->lex.paren_nest--;
10994 return c;
10995
10996 case ':':
10997 c = nextc(p);
10998 if (c == ':') {
10999 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11000 SET_LEX_STATE(EXPR_BEG);
11001 return tCOLON3;
11002 }
11003 set_yylval_id(idCOLON2);
11004 SET_LEX_STATE(EXPR_DOT);
11005 return tCOLON2;
11006 }
11007 if (IS_END() || ISSPACE(c) || c == '#') {
11008 pushback(p, c);
11009 c = warn_balanced(':', ":", "symbol literal");
11010 SET_LEX_STATE(EXPR_BEG);
11011 return c;
11012 }
11013 switch (c) {
11014 case '\'':
11015 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11016 break;
11017 case '"':
11018 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11019 break;
11020 default:
11021 pushback(p, c);
11022 break;
11023 }
11024 SET_LEX_STATE(EXPR_FNAME);
11025 return tSYMBEG;
11026
11027 case '/':
11028 if (IS_BEG()) {
11029 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11030 return tREGEXP_BEG;
11031 }
11032 if ((c = nextc(p)) == '=') {
11033 set_yylval_id('/');
11034 SET_LEX_STATE(EXPR_BEG);
11035 return tOP_ASGN;
11036 }
11037 pushback(p, c);
11038 if (IS_SPCARG(c)) {
11039 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11040 return tREGEXP_BEG;
11041 }
11042 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11043 return warn_balanced('/', "/", "regexp literal");
11044
11045 case '^':
11046 if ((c = nextc(p)) == '=') {
11047 set_yylval_id('^');
11048 SET_LEX_STATE(EXPR_BEG);
11049 return tOP_ASGN;
11050 }
11051 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11052 pushback(p, c);
11053 return '^';
11054
11055 case ';':
11056 SET_LEX_STATE(EXPR_BEG);
11057 p->command_start = TRUE;
11058 return ';';
11059
11060 case ',':
11061 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11062 return ',';
11063
11064 case '~':
11065 if (IS_AFTER_OPERATOR()) {
11066 if ((c = nextc(p)) != '@') {
11067 pushback(p, c);
11068 }
11069 SET_LEX_STATE(EXPR_ARG);
11070 }
11071 else {
11072 SET_LEX_STATE(EXPR_BEG);
11073 }
11074 return '~';
11075
11076 case '(':
11077 if (IS_BEG()) {
11078 c = tLPAREN;
11079 }
11080 else if (!space_seen) {
11081 /* foo( ... ) => method call, no ambiguity */
11082 }
11083 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11084 c = tLPAREN_ARG;
11085 }
11086 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11087 rb_warning0("parentheses after method name is interpreted as "
11088 "an argument list, not a decomposed argument");
11089 }
11090 p->lex.paren_nest++;
11091 COND_PUSH(0);
11092 CMDARG_PUSH(0);
11093 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11094 return c;
11095
11096 case '[':
11097 p->lex.paren_nest++;
11098 if (IS_AFTER_OPERATOR()) {
11099 if ((c = nextc(p)) == ']') {
11100 p->lex.paren_nest--;
11101 SET_LEX_STATE(EXPR_ARG);
11102 if ((c = nextc(p)) == '=') {
11103 return tASET;
11104 }
11105 pushback(p, c);
11106 return tAREF;
11107 }
11108 pushback(p, c);
11109 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11110 return '[';
11111 }
11112 else if (IS_BEG()) {
11113 c = tLBRACK;
11114 }
11115 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11116 c = tLBRACK;
11117 }
11118 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11119 COND_PUSH(0);
11120 CMDARG_PUSH(0);
11121 return c;
11122
11123 case '{':
11124 ++p->lex.brace_nest;
11125 if (lambda_beginning_p())
11126 c = tLAMBEG;
11127 else if (IS_lex_state(EXPR_LABELED))
11128 c = tLBRACE; /* hash */
11129 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11130 c = '{'; /* block (primary) */
11131 else if (IS_lex_state(EXPR_ENDARG))
11132 c = tLBRACE_ARG; /* block (expr) */
11133 else
11134 c = tLBRACE; /* hash */
11135 if (c != tLBRACE) {
11136 p->command_start = TRUE;
11137 SET_LEX_STATE(EXPR_BEG);
11138 }
11139 else {
11140 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11141 }
11142 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11143 COND_PUSH(0);
11144 CMDARG_PUSH(0);
11145 return c;
11146
11147 case '\\':
11148 c = nextc(p);
11149 if (c == '\n') {
11150 space_seen = 1;
11151 dispatch_scan_event(p, tSP);
11152 goto retry; /* skip \\n */
11153 }
11154 if (c == ' ') return tSP;
11155 if (ISSPACE(c)) return c;
11156 pushback(p, c);
11157 return '\\';
11158
11159 case '%':
11160 return parse_percent(p, space_seen, last_state);
11161
11162 case '$':
11163 return parse_gvar(p, last_state);
11164
11165 case '@':
11166 return parse_atmark(p, last_state);
11167
11168 case '_':
11169 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11170 p->ruby__end__seen = 1;
11171 p->eofp = 1;
11172#ifdef RIPPER
11173 lex_goto_eol(p);
11174 dispatch_scan_event(p, k__END__);
11175#endif
11176 return END_OF_INPUT;
11177 }
11178 newtok(p);
11179 break;
11180
11181 default:
11182 if (!parser_is_identchar(p)) {
11183 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11184 token_flush(p);
11185 goto retry;
11186 }
11187
11188 newtok(p);
11189 break;
11190 }
11191
11192 return parse_ident(p, c, cmd_state);
11193}
11194
11195static enum yytokentype
11196yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11197{
11198 enum yytokentype t;
11199
11200 p->lval = lval;
11201 lval->node = 0;
11202 p->yylloc = yylloc;
11203
11204 t = parser_yylex(p);
11205
11206 if (has_delayed_token(p))
11207 dispatch_delayed_token(p, t);
11208 else if (t != END_OF_INPUT)
11209 dispatch_scan_event(p, t);
11210
11211 return t;
11212}
11213
11214#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11215
11216static NODE*
11217node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11218{
11219 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11220
11221 rb_node_init(n, type);
11222 return n;
11223}
11224
11225static NODE *
11226nd_set_loc(NODE *nd, const YYLTYPE *loc)
11227{
11228 nd->nd_loc = *loc;
11229 nd_set_line(nd, loc->beg_pos.lineno);
11230 return nd;
11231}
11232
11233static NODE*
11234node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11235{
11236 NODE *n = node_new_internal(p, type, size, alignment);
11237
11238 nd_set_loc(n, loc);
11239 nd_set_node_id(n, parser_get_node_id(p));
11240 return n;
11241}
11242
11243#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11244
11245static rb_node_scope_t *
11246rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11247{
11248 rb_ast_id_table_t *nd_tbl;
11249 nd_tbl = local_tbl(p);
11250 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11251 n->nd_tbl = nd_tbl;
11252 n->nd_body = nd_body;
11253 n->nd_parent = nd_parent;
11254 n->nd_args = nd_args;
11255
11256 return n;
11257}
11258
11259static rb_node_scope_t *
11260rb_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)
11261{
11262 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11263 n->nd_tbl = nd_tbl;
11264 n->nd_body = nd_body;
11265 n->nd_parent = nd_parent;
11266 n->nd_args = nd_args;
11267
11268 return n;
11269}
11270
11271static rb_node_defn_t *
11272rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11273{
11274 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11275 n->nd_mid = nd_mid;
11276 n->nd_defn = nd_defn;
11277
11278 return n;
11279}
11280
11281static rb_node_defs_t *
11282rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11283{
11284 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11285 n->nd_recv = nd_recv;
11286 n->nd_mid = nd_mid;
11287 n->nd_defn = nd_defn;
11288
11289 return n;
11290}
11291
11292static rb_node_block_t *
11293rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11294{
11295 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11296 n->nd_head = nd_head;
11297 n->nd_end = (NODE *)n;
11298 n->nd_next = 0;
11299
11300 return n;
11301}
11302
11303static rb_node_for_t *
11304rb_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)
11305{
11306 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11307 n->nd_body = nd_body;
11308 n->nd_iter = nd_iter;
11309 n->for_keyword_loc = *for_keyword_loc;
11310 n->in_keyword_loc = *in_keyword_loc;
11311 n->do_keyword_loc = *do_keyword_loc;
11312 n->end_keyword_loc = *end_keyword_loc;
11313
11314 return n;
11315}
11316
11317static rb_node_for_masgn_t *
11318rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11319{
11320 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11321 n->nd_var = nd_var;
11322
11323 return n;
11324}
11325
11326static rb_node_retry_t *
11327rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11328{
11329 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11330
11331 return n;
11332}
11333
11334static rb_node_begin_t *
11335rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11336{
11337 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11338 n->nd_body = nd_body;
11339
11340 return n;
11341}
11342
11343static rb_node_rescue_t *
11344rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11345{
11346 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11347 n->nd_head = nd_head;
11348 n->nd_resq = nd_resq;
11349 n->nd_else = nd_else;
11350
11351 return n;
11352}
11353
11354static rb_node_resbody_t *
11355rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11356{
11357 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11358 n->nd_args = nd_args;
11359 n->nd_exc_var = nd_exc_var;
11360 n->nd_body = nd_body;
11361 n->nd_next = nd_next;
11362
11363 return n;
11364}
11365
11366static rb_node_ensure_t *
11367rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11368{
11369 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11370 n->nd_head = nd_head;
11371 n->nd_ensr = nd_ensr;
11372
11373 return n;
11374}
11375
11376static rb_node_and_t *
11377rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11378{
11379 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11380 n->nd_1st = nd_1st;
11381 n->nd_2nd = nd_2nd;
11382 n->operator_loc = *operator_loc;
11383
11384 return n;
11385}
11386
11387static rb_node_or_t *
11388rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11389{
11390 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11391 n->nd_1st = nd_1st;
11392 n->nd_2nd = nd_2nd;
11393 n->operator_loc = *operator_loc;
11394
11395 return n;
11396}
11397
11398static rb_node_return_t *
11399rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11400{
11401 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11402 n->nd_stts = nd_stts;
11403 n->keyword_loc = *keyword_loc;
11404 return n;
11405}
11406
11407static rb_node_yield_t *
11408rb_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)
11409{
11410 if (nd_head) no_blockarg(p, nd_head);
11411
11412 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11413 n->nd_head = nd_head;
11414 n->keyword_loc = *keyword_loc;
11415 n->lparen_loc = *lparen_loc;
11416 n->rparen_loc = *rparen_loc;
11417
11418 return n;
11419}
11420
11421static rb_node_if_t *
11422rb_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)
11423{
11424 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11425 n->nd_cond = nd_cond;
11426 n->nd_body = nd_body;
11427 n->nd_else = nd_else;
11428 n->if_keyword_loc = *if_keyword_loc;
11429 n->then_keyword_loc = *then_keyword_loc;
11430 n->end_keyword_loc = *end_keyword_loc;
11431
11432 return n;
11433}
11434
11435static rb_node_unless_t *
11436rb_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)
11437{
11438 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11439 n->nd_cond = nd_cond;
11440 n->nd_body = nd_body;
11441 n->nd_else = nd_else;
11442 n->keyword_loc = *keyword_loc;
11443 n->then_keyword_loc = *then_keyword_loc;
11444 n->end_keyword_loc = *end_keyword_loc;
11445
11446 return n;
11447}
11448
11449static rb_node_class_t *
11450rb_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)
11451{
11452 /* Keep the order of node creation */
11453 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11454 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11455 RNODE_SCOPE(scope)->nd_parent = &n->node;
11456 n->nd_cpath = nd_cpath;
11457 n->nd_body = scope;
11458 n->nd_super = nd_super;
11459 n->class_keyword_loc = *class_keyword_loc;
11460 n->inheritance_operator_loc = *inheritance_operator_loc;
11461 n->end_keyword_loc = *end_keyword_loc;
11462
11463 return n;
11464}
11465
11466static rb_node_sclass_t *
11467rb_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)
11468{
11469 /* Keep the order of node creation */
11470 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11471 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11472 RNODE_SCOPE(scope)->nd_parent = &n->node;
11473 n->nd_recv = nd_recv;
11474 n->nd_body = scope;
11475 n->class_keyword_loc = *class_keyword_loc;
11476 n->operator_loc = *operator_loc;
11477 n->end_keyword_loc = *end_keyword_loc;
11478
11479 return n;
11480}
11481
11482static rb_node_module_t *
11483rb_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)
11484{
11485 /* Keep the order of node creation */
11486 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11487 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11488 RNODE_SCOPE(scope)->nd_parent = &n->node;
11489 n->nd_cpath = nd_cpath;
11490 n->nd_body = scope;
11491 n->module_keyword_loc = *module_keyword_loc;
11492 n->end_keyword_loc = *end_keyword_loc;
11493
11494 return n;
11495}
11496
11497static rb_node_iter_t *
11498rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11499{
11500 /* Keep the order of node creation */
11501 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11502 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11503 RNODE_SCOPE(scope)->nd_parent = &n->node;
11504 n->nd_body = scope;
11505 n->nd_iter = 0;
11506
11507 return n;
11508}
11509
11510static rb_node_lambda_t *
11511rb_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)
11512{
11513 /* Keep the order of node creation */
11514 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11515 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11516 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11517 RNODE_SCOPE(scope)->nd_parent = &n->node;
11518 n->nd_body = scope;
11519 n->operator_loc = *operator_loc;
11520 n->opening_loc = *opening_loc;
11521 n->closing_loc = *closing_loc;
11522
11523 return n;
11524}
11525
11526static rb_node_case_t *
11527rb_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)
11528{
11529 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11530 n->nd_head = nd_head;
11531 n->nd_body = nd_body;
11532 n->case_keyword_loc = *case_keyword_loc;
11533 n->end_keyword_loc = *end_keyword_loc;
11534
11535 return n;
11536}
11537
11538static rb_node_case2_t *
11539rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11540{
11541 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11542 n->nd_head = 0;
11543 n->nd_body = nd_body;
11544 n->case_keyword_loc = *case_keyword_loc;
11545 n->end_keyword_loc = *end_keyword_loc;
11546
11547 return n;
11548}
11549
11550static rb_node_case3_t *
11551rb_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)
11552{
11553 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11554 n->nd_head = nd_head;
11555 n->nd_body = nd_body;
11556 n->case_keyword_loc = *case_keyword_loc;
11557 n->end_keyword_loc = *end_keyword_loc;
11558
11559 return n;
11560}
11561
11562static rb_node_when_t *
11563rb_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)
11564{
11565 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11566 n->nd_head = nd_head;
11567 n->nd_body = nd_body;
11568 n->nd_next = nd_next;
11569 n->keyword_loc = *keyword_loc;
11570 n->then_keyword_loc = *then_keyword_loc;
11571
11572 return n;
11573}
11574
11575static rb_node_in_t *
11576rb_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)
11577{
11578 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11579 n->nd_head = nd_head;
11580 n->nd_body = nd_body;
11581 n->nd_next = nd_next;
11582 n->in_keyword_loc = *in_keyword_loc;
11583 n->then_keyword_loc = *then_keyword_loc;
11584 n->operator_loc = *operator_loc;
11585
11586 return n;
11587}
11588
11589static rb_node_while_t *
11590rb_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)
11591{
11592 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11593 n->nd_cond = nd_cond;
11594 n->nd_body = nd_body;
11595 n->nd_state = nd_state;
11596 n->keyword_loc = *keyword_loc;
11597 n->closing_loc = *closing_loc;
11598
11599 return n;
11600}
11601
11602static rb_node_until_t *
11603rb_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)
11604{
11605 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11606 n->nd_cond = nd_cond;
11607 n->nd_body = nd_body;
11608 n->nd_state = nd_state;
11609 n->keyword_loc = *keyword_loc;
11610 n->closing_loc = *closing_loc;
11611
11612 return n;
11613}
11614
11615static rb_node_colon2_t *
11616rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11617{
11618 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11619 n->nd_head = nd_head;
11620 n->nd_mid = nd_mid;
11621 n->delimiter_loc = *delimiter_loc;
11622 n->name_loc = *name_loc;
11623
11624 return n;
11625}
11626
11627static rb_node_colon3_t *
11628rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11629{
11630 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11631 n->nd_mid = nd_mid;
11632 n->delimiter_loc = *delimiter_loc;
11633 n->name_loc = *name_loc;
11634
11635 return n;
11636}
11637
11638static rb_node_dot2_t *
11639rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11640{
11641 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11642 n->nd_beg = nd_beg;
11643 n->nd_end = nd_end;
11644 n->operator_loc = *operator_loc;
11645
11646 return n;
11647}
11648
11649static rb_node_dot3_t *
11650rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11651{
11652 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11653 n->nd_beg = nd_beg;
11654 n->nd_end = nd_end;
11655 n->operator_loc = *operator_loc;
11656
11657 return n;
11658}
11659
11660static rb_node_self_t *
11661rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11662{
11663 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11664 n->nd_state = 1;
11665
11666 return n;
11667}
11668
11669static rb_node_nil_t *
11670rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11671{
11672 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11673
11674 return n;
11675}
11676
11677static rb_node_true_t *
11678rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11679{
11680 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11681
11682 return n;
11683}
11684
11685static rb_node_false_t *
11686rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11687{
11688 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11689
11690 return n;
11691}
11692
11693static rb_node_super_t *
11694rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11695 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11696{
11697 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11698 n->nd_args = nd_args;
11699 n->keyword_loc = *keyword_loc;
11700 n->lparen_loc = *lparen_loc;
11701 n->rparen_loc = *rparen_loc;
11702
11703 return n;
11704}
11705
11706static rb_node_zsuper_t *
11707rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11708{
11709 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11710
11711 return n;
11712}
11713
11714static rb_node_match2_t *
11715rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11716{
11717 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11718 n->nd_recv = nd_recv;
11719 n->nd_value = nd_value;
11720 n->nd_args = 0;
11721
11722 return n;
11723}
11724
11725static rb_node_match3_t *
11726rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11727{
11728 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11729 n->nd_recv = nd_recv;
11730 n->nd_value = nd_value;
11731
11732 return n;
11733}
11734
11735static rb_node_list_t *
11736rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11737{
11738 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11739}
11740
11741static rb_node_list_t *
11742rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11743{
11744 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11745 n->nd_head = nd_head;
11746 n->as.nd_alen = nd_alen;
11747 n->nd_next = nd_next;
11748
11749 return n;
11750}
11751
11752static rb_node_zlist_t *
11753rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11754{
11755 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11756
11757 return n;
11758}
11759
11760static rb_node_hash_t *
11761rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11762{
11763 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11764 n->nd_head = nd_head;
11765 n->nd_brace = 0;
11766
11767 return n;
11768}
11769
11770static rb_node_masgn_t *
11771rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11772{
11773 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11774 n->nd_head = nd_head;
11775 n->nd_value = 0;
11776 n->nd_args = nd_args;
11777
11778 return n;
11779}
11780
11781static rb_node_gasgn_t *
11782rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11783{
11784 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11785 n->nd_vid = nd_vid;
11786 n->nd_value = nd_value;
11787
11788 return n;
11789}
11790
11791static rb_node_lasgn_t *
11792rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11793{
11794 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11795 n->nd_vid = nd_vid;
11796 n->nd_value = nd_value;
11797
11798 return n;
11799}
11800
11801static rb_node_dasgn_t *
11802rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11803{
11804 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11805 n->nd_vid = nd_vid;
11806 n->nd_value = nd_value;
11807
11808 return n;
11809}
11810
11811static rb_node_iasgn_t *
11812rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11813{
11814 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11815 n->nd_vid = nd_vid;
11816 n->nd_value = nd_value;
11817
11818 return n;
11819}
11820
11821static rb_node_cvasgn_t *
11822rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11823{
11824 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11825 n->nd_vid = nd_vid;
11826 n->nd_value = nd_value;
11827
11828 return n;
11829}
11830
11831static rb_node_op_asgn1_t *
11832rb_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)
11833{
11834 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11835 n->nd_recv = nd_recv;
11836 n->nd_mid = nd_mid;
11837 n->nd_index = index;
11838 n->nd_rvalue = rvalue;
11839 n->call_operator_loc = *call_operator_loc;
11840 n->opening_loc = *opening_loc;
11841 n->closing_loc = *closing_loc;
11842 n->binary_operator_loc = *binary_operator_loc;
11843
11844 return n;
11845}
11846
11847static rb_node_op_asgn2_t *
11848rb_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)
11849{
11850 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11851 n->nd_recv = nd_recv;
11852 n->nd_value = nd_value;
11853 n->nd_vid = nd_vid;
11854 n->nd_mid = nd_mid;
11855 n->nd_aid = nd_aid;
11856 n->call_operator_loc = *call_operator_loc;
11857 n->message_loc = *message_loc;
11858 n->binary_operator_loc = *binary_operator_loc;
11859
11860 return n;
11861}
11862
11863static rb_node_op_asgn_or_t *
11864rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11865{
11866 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11867 n->nd_head = nd_head;
11868 n->nd_value = nd_value;
11869
11870 return n;
11871}
11872
11873static rb_node_op_asgn_and_t *
11874rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11875{
11876 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11877 n->nd_head = nd_head;
11878 n->nd_value = nd_value;
11879
11880 return n;
11881}
11882
11883static rb_node_gvar_t *
11884rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11885{
11886 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11887 n->nd_vid = nd_vid;
11888
11889 return n;
11890}
11891
11892static rb_node_lvar_t *
11893rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11894{
11895 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11896 n->nd_vid = nd_vid;
11897
11898 return n;
11899}
11900
11901static rb_node_dvar_t *
11902rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11903{
11904 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11905 n->nd_vid = nd_vid;
11906
11907 return n;
11908}
11909
11910static rb_node_ivar_t *
11911rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11912{
11913 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11914 n->nd_vid = nd_vid;
11915
11916 return n;
11917}
11918
11919static rb_node_const_t *
11920rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11921{
11922 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11923 n->nd_vid = nd_vid;
11924
11925 return n;
11926}
11927
11928static rb_node_cvar_t *
11929rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11930{
11931 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11932 n->nd_vid = nd_vid;
11933
11934 return n;
11935}
11936
11937static rb_node_nth_ref_t *
11938rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11939{
11940 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11941 n->nd_nth = nd_nth;
11942
11943 return n;
11944}
11945
11946static rb_node_back_ref_t *
11947rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11948{
11949 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11950 n->nd_nth = nd_nth;
11951
11952 return n;
11953}
11954
11955static rb_node_integer_t *
11956rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11957{
11958 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11959 n->val = val;
11960 n->minus = FALSE;
11961 n->base = base;
11962
11963 return n;
11964}
11965
11966static rb_node_float_t *
11967rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11968{
11969 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11970 n->val = val;
11971 n->minus = FALSE;
11972
11973 return n;
11974}
11975
11976static rb_node_rational_t *
11977rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11978{
11979 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11980 n->val = val;
11981 n->minus = FALSE;
11982 n->base = base;
11983 n->seen_point = seen_point;
11984
11985 return n;
11986}
11987
11988static rb_node_imaginary_t *
11989rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11990{
11991 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11992 n->val = val;
11993 n->minus = FALSE;
11994 n->base = base;
11995 n->seen_point = seen_point;
11996 n->type = numeric_type;
11997
11998 return n;
11999}
12000
12001static rb_node_str_t *
12002rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12003{
12004 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12005 n->string = string;
12006
12007 return n;
12008}
12009
12010/* TODO; Use union for NODE_DSTR2 */
12011static rb_node_dstr_t *
12012rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12013{
12014 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12015 n->string = string;
12016 n->as.nd_alen = nd_alen;
12017 n->nd_next = (rb_node_list_t *)nd_next;
12018
12019 return n;
12020}
12021
12022static rb_node_dstr_t *
12023rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12024{
12025 return rb_node_dstr_new0(p, string, 1, 0, loc);
12026}
12027
12028static rb_node_xstr_t *
12029rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12030{
12031 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12032 n->string = string;
12033
12034 return n;
12035}
12036
12037static rb_node_dxstr_t *
12038rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12039{
12040 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12041 n->string = string;
12042 n->as.nd_alen = nd_alen;
12043 n->nd_next = (rb_node_list_t *)nd_next;
12044
12045 return n;
12046}
12047
12048static rb_node_sym_t *
12049rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12050{
12051 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12052 n->string = rb_str_to_parser_string(p, str);
12053
12054 return n;
12055}
12056
12057static rb_node_dsym_t *
12058rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12059{
12060 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12061 n->string = string;
12062 n->as.nd_alen = nd_alen;
12063 n->nd_next = (rb_node_list_t *)nd_next;
12064
12065 return n;
12066}
12067
12068static rb_node_evstr_t *
12069rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12070{
12071 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12072 n->nd_body = nd_body;
12073 n->opening_loc = *opening_loc;
12074 n->closing_loc = *closing_loc;
12075
12076 return n;
12077}
12078
12079static rb_node_regx_t *
12080rb_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)
12081{
12082 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12083 n->string = string;
12084 n->options = options & RE_OPTION_MASK;
12085 n->opening_loc = *opening_loc;
12086 n->content_loc = *content_loc;
12087 n->closing_loc = *closing_loc;
12088
12089 return n;
12090}
12091
12092static rb_node_call_t *
12093rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12094{
12095 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12096 n->nd_recv = nd_recv;
12097 n->nd_mid = nd_mid;
12098 n->nd_args = nd_args;
12099
12100 return n;
12101}
12102
12103static rb_node_opcall_t *
12104rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12105{
12106 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12107 n->nd_recv = nd_recv;
12108 n->nd_mid = nd_mid;
12109 n->nd_args = nd_args;
12110
12111 return n;
12112}
12113
12114static rb_node_fcall_t *
12115rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12116{
12117 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12118 n->nd_mid = nd_mid;
12119 n->nd_args = nd_args;
12120
12121 return n;
12122}
12123
12124static rb_node_qcall_t *
12125rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12126{
12127 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_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_vcall_t *
12136rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12137{
12138 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12139 n->nd_mid = nd_mid;
12140
12141 return n;
12142}
12143
12144static rb_node_once_t *
12145rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12146{
12147 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12148 n->nd_body = nd_body;
12149
12150 return n;
12151}
12152
12153static rb_node_args_t *
12154rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12155{
12156 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12157 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12158
12159 return n;
12160}
12161
12162static rb_node_args_aux_t *
12163rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12164{
12165 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12166 n->nd_pid = nd_pid;
12167 n->nd_plen = nd_plen;
12168 n->nd_next = 0;
12169
12170 return n;
12171}
12172
12173static rb_node_opt_arg_t *
12174rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12175{
12176 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12177 n->nd_body = nd_body;
12178 n->nd_next = 0;
12179
12180 return n;
12181}
12182
12183static rb_node_kw_arg_t *
12184rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12185{
12186 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12187 n->nd_body = nd_body;
12188 n->nd_next = 0;
12189
12190 return n;
12191}
12192
12193static rb_node_postarg_t *
12194rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12195{
12196 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12197 n->nd_1st = nd_1st;
12198 n->nd_2nd = nd_2nd;
12199
12200 return n;
12201}
12202
12203static rb_node_argscat_t *
12204rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12205{
12206 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12207 n->nd_head = nd_head;
12208 n->nd_body = nd_body;
12209
12210 return n;
12211}
12212
12213static rb_node_argspush_t *
12214rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12215{
12216 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12217 n->nd_head = nd_head;
12218 n->nd_body = nd_body;
12219
12220 return n;
12221}
12222
12223static rb_node_splat_t *
12224rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12225{
12226 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12227 n->nd_head = nd_head;
12228 n->operator_loc = *operator_loc;
12229
12230 return n;
12231}
12232
12233static rb_node_block_pass_t *
12234rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12235{
12236 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12237 n->forwarding = 0;
12238 n->nd_head = 0;
12239 n->nd_body = nd_body;
12240 n->operator_loc = *operator_loc;
12241
12242 return n;
12243}
12244
12245static rb_node_alias_t *
12246rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12247{
12248 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12249 n->nd_1st = nd_1st;
12250 n->nd_2nd = nd_2nd;
12251 n->keyword_loc = *keyword_loc;
12252
12253 return n;
12254}
12255
12256static rb_node_valias_t *
12257rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12258{
12259 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12260 n->nd_alias = nd_alias;
12261 n->nd_orig = nd_orig;
12262 n->keyword_loc = *keyword_loc;
12263
12264 return n;
12265}
12266
12267static rb_node_undef_t *
12268rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12269{
12270 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12271 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12272 n->keyword_loc = NULL_LOC;
12273 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12274
12275 return n;
12276}
12277
12278static rb_node_errinfo_t *
12279rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12280{
12281 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12282
12283 return n;
12284}
12285
12286static rb_node_defined_t *
12287rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12288{
12289 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12290 n->nd_head = nd_head;
12291 n->keyword_loc = *keyword_loc;
12292
12293 return n;
12294}
12295
12296static rb_node_postexe_t *
12297rb_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)
12298{
12299 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12300 n->nd_body = nd_body;
12301 n->keyword_loc = *keyword_loc;
12302 n->opening_loc = *opening_loc;
12303 n->closing_loc = *closing_loc;
12304
12305 return n;
12306}
12307
12308static rb_node_attrasgn_t *
12309rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12310{
12311 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12312 n->nd_recv = nd_recv;
12313 n->nd_mid = nd_mid;
12314 n->nd_args = nd_args;
12315
12316 return n;
12317}
12318
12319static rb_node_aryptn_t *
12320rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12321{
12322 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12323 n->nd_pconst = 0;
12324 n->pre_args = pre_args;
12325 n->rest_arg = rest_arg;
12326 n->post_args = post_args;
12327
12328 return n;
12329}
12330
12331static rb_node_hshptn_t *
12332rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12333{
12334 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12335 n->nd_pconst = nd_pconst;
12336 n->nd_pkwargs = nd_pkwargs;
12337 n->nd_pkwrestarg = nd_pkwrestarg;
12338
12339 return n;
12340}
12341
12342static rb_node_fndptn_t *
12343rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12344{
12345 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12346 n->nd_pconst = 0;
12347 n->pre_rest_arg = pre_rest_arg;
12348 n->args = args;
12349 n->post_rest_arg = post_rest_arg;
12350
12351 return n;
12352}
12353
12354static rb_node_line_t *
12355rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12356{
12357 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12358
12359 return n;
12360}
12361
12362static rb_node_file_t *
12363rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12364{
12365 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12366 n->path = rb_str_to_parser_string(p, str);
12367
12368 return n;
12369}
12370
12371static rb_node_encoding_t *
12372rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12373{
12374 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12375 n->enc = p->enc;
12376
12377 return n;
12378}
12379
12380static rb_node_cdecl_t *
12381rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12382{
12383 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12384 n->nd_vid = nd_vid;
12385 n->nd_value = nd_value;
12386 n->nd_else = nd_else;
12387 n->shareability = shareability;
12388
12389 return n;
12390}
12391
12392static rb_node_op_cdecl_t *
12393rb_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)
12394{
12395 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12396 n->nd_head = nd_head;
12397 n->nd_value = nd_value;
12398 n->nd_aid = nd_aid;
12399 n->shareability = shareability;
12400
12401 return n;
12402}
12403
12404static rb_node_error_t *
12405rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12406{
12407 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12408
12409 return n;
12410}
12411
12412static rb_node_break_t *
12413rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12414{
12415 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12416 n->nd_stts = nd_stts;
12417 n->nd_chain = 0;
12418 n->keyword_loc = *keyword_loc;
12419
12420 return n;
12421}
12422
12423static rb_node_next_t *
12424rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12425{
12426 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12427 n->nd_stts = nd_stts;
12428 n->nd_chain = 0;
12429 n->keyword_loc = *keyword_loc;
12430
12431 return n;
12432}
12433
12434static rb_node_redo_t *
12435rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12436{
12437 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12438 n->nd_chain = 0;
12439 n->keyword_loc = *keyword_loc;
12440
12441 return n;
12442}
12443
12444static rb_node_def_temp_t *
12445rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12446{
12447 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12448 n->save.numparam_save = 0;
12449 n->save.max_numparam = 0;
12450 n->save.ctxt = p->ctxt;
12451 n->nd_def = 0;
12452 n->nd_mid = 0;
12453
12454 return n;
12455}
12456
12457static rb_node_def_temp_t *
12458def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12459{
12460 n->save.numparam_save = numparam_push(p);
12461 n->save.max_numparam = p->max_numparam;
12462 return n;
12463}
12464
12465#ifndef RIPPER
12466static enum node_type
12467nodetype(NODE *node) /* for debug */
12468{
12469 return (enum node_type)nd_type(node);
12470}
12471
12472static int
12473nodeline(NODE *node)
12474{
12475 return nd_line(node);
12476}
12477#endif
12478
12479static NODE*
12480newline_node(NODE *node)
12481{
12482 if (node) {
12483 node = remove_begin(node);
12484 nd_set_fl_newline(node);
12485 }
12486 return node;
12487}
12488
12489static void
12490fixpos(NODE *node, NODE *orig)
12491{
12492 if (!node) return;
12493 if (!orig) return;
12494 nd_set_line(node, nd_line(orig));
12495}
12496
12497static NODE*
12498block_append(struct parser_params *p, NODE *head, NODE *tail)
12499{
12500 NODE *end, *h = head, *nd;
12501
12502 if (tail == 0) return head;
12503
12504 if (h == 0) return tail;
12505 switch (nd_type(h)) {
12506 default:
12507 h = end = NEW_BLOCK(head, &head->nd_loc);
12508 head = end;
12509 break;
12510 case NODE_BLOCK:
12511 end = RNODE_BLOCK(h)->nd_end;
12512 break;
12513 }
12514
12515 nd = RNODE_BLOCK(end)->nd_head;
12516 switch (nd_type(nd)) {
12517 case NODE_RETURN:
12518 case NODE_BREAK:
12519 case NODE_NEXT:
12520 case NODE_REDO:
12521 case NODE_RETRY:
12522 rb_warning0L(nd_line(tail), "statement not reached");
12523 break;
12524
12525 default:
12526 break;
12527 }
12528
12529 if (!nd_type_p(tail, NODE_BLOCK)) {
12530 tail = NEW_BLOCK(tail, &tail->nd_loc);
12531 }
12532 RNODE_BLOCK(end)->nd_next = tail;
12533 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12534 nd_set_last_loc(head, nd_last_loc(tail));
12535 return head;
12536}
12537
12538/* append item to the list */
12539static NODE*
12540list_append(struct parser_params *p, NODE *list, NODE *item)
12541{
12542 NODE *last;
12543
12544 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12545 if (RNODE_LIST(list)->nd_next) {
12546 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12547 }
12548 else {
12549 last = list;
12550 }
12551
12552 RNODE_LIST(list)->as.nd_alen += 1;
12553 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12554 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12555
12556 nd_set_last_loc(list, nd_last_loc(item));
12557
12558 return list;
12559}
12560
12561/* concat two lists */
12562static NODE*
12563list_concat(NODE *head, NODE *tail)
12564{
12565 NODE *last;
12566
12567 if (RNODE_LIST(head)->nd_next) {
12568 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12569 }
12570 else {
12571 last = head;
12572 }
12573
12574 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12575 RNODE_LIST(last)->nd_next = tail;
12576 if (RNODE_LIST(tail)->nd_next) {
12577 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12578 }
12579 else {
12580 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12581 }
12582
12583 nd_set_last_loc(head, nd_last_loc(tail));
12584
12585 return head;
12586}
12587
12588static int
12589literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12590{
12591 if (!tail) return 1;
12592 if (!rb_parser_enc_compatible(p, head, tail)) {
12593 compile_error(p, "string literal encodings differ (%s / %s)",
12594 rb_enc_name(rb_parser_str_get_encoding(head)),
12595 rb_enc_name(rb_parser_str_get_encoding(tail)));
12596 rb_parser_str_resize(p, head, 0);
12597 rb_parser_str_resize(p, tail, 0);
12598 return 0;
12599 }
12600 rb_parser_str_buf_append(p, head, tail);
12601 return 1;
12602}
12603
12604static rb_parser_string_t *
12605string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12606{
12607 if (htype != NODE_DSTR) return NULL;
12608 if (RNODE_DSTR(head)->nd_next) {
12609 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12610 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12611 }
12612 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12613 ASSUME(lit);
12614 return lit;
12615}
12616
12617#ifndef RIPPER
12618static rb_parser_string_t *
12619rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12620{
12621 rb_parser_string_t *copy;
12622 if (!orig) return NULL;
12623 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12624 copy->coderange = orig->coderange;
12625 copy->enc = orig->enc;
12626 return copy;
12627}
12628#endif
12629
12630/* concat two string literals */
12631static NODE *
12632literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12633{
12634 enum node_type htype;
12635 rb_parser_string_t *lit;
12636
12637 if (!head) return tail;
12638 if (!tail) return head;
12639
12640 htype = nd_type(head);
12641 if (htype == NODE_EVSTR) {
12642 head = new_dstr(p, head, loc);
12643 htype = NODE_DSTR;
12644 }
12645 if (p->heredoc_indent > 0) {
12646 switch (htype) {
12647 case NODE_STR:
12648 head = str2dstr(p, head);
12649 case NODE_DSTR:
12650 return list_append(p, head, tail);
12651 default:
12652 break;
12653 }
12654 }
12655 switch (nd_type(tail)) {
12656 case NODE_STR:
12657 if ((lit = string_literal_head(p, htype, head)) != false) {
12658 htype = NODE_STR;
12659 }
12660 else {
12661 lit = RNODE_DSTR(head)->string;
12662 }
12663 if (htype == NODE_STR) {
12664 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12665 error:
12666 rb_discard_node(p, head);
12667 rb_discard_node(p, tail);
12668 return 0;
12669 }
12670 rb_discard_node(p, tail);
12671 }
12672 else {
12673 list_append(p, head, tail);
12674 }
12675 break;
12676
12677 case NODE_DSTR:
12678 if (htype == NODE_STR) {
12679 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12680 goto error;
12681 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12682 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12683 RNODE_STR(head)->string = NULL;
12684 rb_discard_node(p, head);
12685 head = tail;
12686 }
12687 else if (!RNODE_DSTR(tail)->string) {
12688 append:
12689 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12690 if (!RNODE_DSTR(head)->nd_next) {
12691 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12692 }
12693 else if (RNODE_DSTR(tail)->nd_next) {
12694 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12695 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12696 }
12697 rb_discard_node(p, tail);
12698 }
12699 else if ((lit = string_literal_head(p, htype, head)) != false) {
12700 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12701 goto error;
12702 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12703 RNODE_DSTR(tail)->string = 0;
12704 goto append;
12705 }
12706 else {
12707 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));
12708 RNODE_DSTR(tail)->string = 0;
12709 }
12710 break;
12711
12712 case NODE_EVSTR:
12713 if (htype == NODE_STR) {
12714 head = str2dstr(p, head);
12715 RNODE_DSTR(head)->as.nd_alen = 1;
12716 }
12717 list_append(p, head, tail);
12718 break;
12719 }
12720 return head;
12721}
12722
12723static void
12724nd_copy_flag(NODE *new_node, NODE *old_node)
12725{
12726 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12727 nd_set_line(new_node, nd_line(old_node));
12728 new_node->nd_loc = old_node->nd_loc;
12729 new_node->node_id = old_node->node_id;
12730}
12731
12732static NODE *
12733str2dstr(struct parser_params *p, NODE *node)
12734{
12735 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12736 nd_copy_flag(new_node, node);
12737 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12738 RNODE_DSTR(new_node)->as.nd_alen = 0;
12739 RNODE_DSTR(new_node)->nd_next = 0;
12740 RNODE_STR(node)->string = 0;
12741
12742 return new_node;
12743}
12744
12745static NODE *
12746str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12747{
12748 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12749 nd_copy_flag(new_node, node);
12750 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12751 RNODE_REGX(new_node)->options = options;
12752 nd_set_loc(new_node, loc);
12753 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12754 RNODE_REGX(new_node)->content_loc = *content_loc;
12755 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12756 RNODE_STR(node)->string = 0;
12757
12758 return new_node;
12759}
12760
12761static NODE *
12762evstr2dstr(struct parser_params *p, NODE *node)
12763{
12764 if (nd_type_p(node, NODE_EVSTR)) {
12765 node = new_dstr(p, node, &node->nd_loc);
12766 }
12767 return node;
12768}
12769
12770static NODE *
12771new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12772{
12773 NODE *head = node;
12774
12775 if (node) {
12776 switch (nd_type(node)) {
12777 case NODE_STR:
12778 return str2dstr(p, node);
12779 case NODE_DSTR:
12780 break;
12781 case NODE_EVSTR:
12782 return node;
12783 }
12784 }
12785 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12786}
12787
12788static NODE *
12789new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12790{
12791 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12792 return list_append(p, dstr, node);
12793}
12794
12795static NODE *
12796call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12797 const YYLTYPE *op_loc, const YYLTYPE *loc)
12798{
12799 NODE *expr;
12800 value_expr(p, recv);
12801 value_expr(p, arg1);
12802 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12803 nd_set_line(expr, op_loc->beg_pos.lineno);
12804 return expr;
12805}
12806
12807static NODE *
12808call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12809{
12810 NODE *opcall;
12811 value_expr(p, recv);
12812 opcall = NEW_OPCALL(recv, id, 0, loc);
12813 nd_set_line(opcall, op_loc->beg_pos.lineno);
12814 return opcall;
12815}
12816
12817static NODE *
12818new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12819{
12820 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12821 nd_set_line(qcall, op_loc->beg_pos.lineno);
12822 return qcall;
12823}
12824
12825static NODE*
12826new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12827{
12828 NODE *ret;
12829 if (block) block_dup_check(p, args, block);
12830 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12831 if (block) ret = method_add_block(p, ret, block, loc);
12832 fixpos(ret, recv);
12833 return ret;
12834}
12835
12836static rb_locations_lambda_body_t*
12837new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12838{
12839 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12840 body->node = node;
12841 body->opening_loc = *opening_loc;
12842 body->closing_loc = *closing_loc;
12843 return body;
12844}
12845
12846static NODE *
12847command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12848{
12849 NODE **body;
12850 enum node_type type = nd_type(m);
12851 switch (type) {
12852 case NODE_YIELD:
12853 compile_error(p, "block given to yield");
12854 return m;
12855 case NODE_RETURN:
12856 body = &RNODE_RETURN(m)->nd_stts;
12857 break;
12858 case NODE_BREAK:
12859 case NODE_NEXT:
12860 body = &RNODE_EXITS(m)->nd_stts;
12861 break;
12862 case NODE_REDO: /* `redo` has no argument */
12863 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12864 return m;
12865 case NODE_RETRY: /* `retry` has no argument */
12866 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12867 return m;
12868 default:
12869 block_dup_check(p, get_nd_args(p, m), b);
12870 return method_add_block(p, m, b, loc);
12871 }
12872 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12873 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12874 *body = command_add_block(p, *body, b, &b_loc);
12875 m->nd_loc.end_pos = loc->end_pos;
12876 return m;
12877}
12878
12879#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12880
12881static NODE*
12882last_expr_once_body(NODE *node)
12883{
12884 if (!node) return 0;
12885 return nd_once_body(node);
12886}
12887
12888static NODE*
12889match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12890{
12891 NODE *n;
12892 int line = op_loc->beg_pos.lineno;
12893
12894 value_expr(p, node1);
12895 value_expr(p, node2);
12896
12897 if ((n = last_expr_once_body(node1)) != 0) {
12898 switch (nd_type(n)) {
12899 case NODE_DREGX:
12900 {
12901 NODE *match = NEW_MATCH2(node1, node2, loc);
12902 nd_set_line(match, line);
12903 return match;
12904 }
12905
12906 case NODE_REGX:
12907 {
12908 const VALUE lit = rb_node_regx_string_val(n);
12909 if (!NIL_P(lit)) {
12910 NODE *match = NEW_MATCH2(node1, node2, loc);
12911 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12912 nd_set_line(match, line);
12913 return match;
12914 }
12915 }
12916 }
12917 }
12918
12919 if ((n = last_expr_once_body(node2)) != 0) {
12920 NODE *match3;
12921
12922 switch (nd_type(n)) {
12923 case NODE_DREGX:
12924 match3 = NEW_MATCH3(node2, node1, loc);
12925 return match3;
12926 }
12927 }
12928
12929 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12930 nd_set_line(n, line);
12931 return n;
12932}
12933
12934# if WARN_PAST_SCOPE
12935static int
12936past_dvar_p(struct parser_params *p, ID id)
12937{
12938 struct vtable *past = p->lvtbl->past;
12939 while (past) {
12940 if (vtable_included(past, id)) return 1;
12941 past = past->prev;
12942 }
12943 return 0;
12944}
12945# endif
12946
12947static int
12948numparam_nested_p(struct parser_params *p)
12949{
12950 struct local_vars *local = p->lvtbl;
12951 NODE *outer = local->numparam.outer;
12952 NODE *inner = local->numparam.inner;
12953 if (outer || inner) {
12954 NODE *used = outer ? outer : inner;
12955 compile_error(p, "numbered parameter is already used in %s block\n"
12956 "%s:%d: numbered parameter is already used here",
12957 outer ? "outer" : "inner",
12958 p->ruby_sourcefile, nd_line(used));
12959 parser_show_error_line(p, &used->nd_loc);
12960 return 1;
12961 }
12962 return 0;
12963}
12964
12965static int
12966numparam_used_p(struct parser_params *p)
12967{
12968 NODE *numparam = p->lvtbl->numparam.current;
12969 if (numparam) {
12970 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12971 "%s:%d: numbered parameter is already used here",
12972 p->ruby_sourcefile, nd_line(numparam));
12973 parser_show_error_line(p, &numparam->nd_loc);
12974 return 1;
12975 }
12976 return 0;
12977}
12978
12979static int
12980it_used_p(struct parser_params *p)
12981{
12982 NODE *it = p->lvtbl->it;
12983 if (it) {
12984 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12985 "%s:%d: 'it' is already used here",
12986 p->ruby_sourcefile, nd_line(it));
12987 parser_show_error_line(p, &it->nd_loc);
12988 return 1;
12989 }
12990 return 0;
12991}
12992
12993static NODE*
12994gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12995{
12996 ID *vidp = NULL;
12997 NODE *node;
12998 switch (id) {
12999 case keyword_self:
13000 return NEW_SELF(loc);
13001 case keyword_nil:
13002 return NEW_NIL(loc);
13003 case keyword_true:
13004 return NEW_TRUE(loc);
13005 case keyword_false:
13006 return NEW_FALSE(loc);
13007 case keyword__FILE__:
13008 {
13009 VALUE file = p->ruby_sourcefile_string;
13010 if (NIL_P(file))
13011 file = rb_str_new(0, 0);
13012 node = NEW_FILE(file, loc);
13013 }
13014 return node;
13015 case keyword__LINE__:
13016 return NEW_LINE(loc);
13017 case keyword__ENCODING__:
13018 return NEW_ENCODING(loc);
13019
13020 }
13021 switch (id_type(id)) {
13022 case ID_LOCAL:
13023 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13024 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13025 if (vidp) *vidp |= LVAR_USED;
13026 node = NEW_DVAR(id, loc);
13027 return node;
13028 }
13029 if (local_id_ref(p, id, &vidp)) {
13030 if (vidp) *vidp |= LVAR_USED;
13031 node = NEW_LVAR(id, loc);
13032 return node;
13033 }
13034 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13035 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13036 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13037 node = NEW_DVAR(id, loc);
13038 struct local_vars *local = p->lvtbl;
13039 if (!local->numparam.current) local->numparam.current = node;
13040 return node;
13041 }
13042# if WARN_PAST_SCOPE
13043 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13044 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13045 }
13046# endif
13047 /* method call without arguments */
13048 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13049 if (numparam_used_p(p)) return 0;
13050 if (p->max_numparam == ORDINAL_PARAM) {
13051 compile_error(p, "ordinary parameter is defined");
13052 return 0;
13053 }
13054 if (!p->it_id) {
13055 p->it_id = idItImplicit;
13056 vtable_add(p->lvtbl->args, p->it_id);
13057 }
13058 NODE *node = NEW_DVAR(p->it_id, loc);
13059 if (!p->lvtbl->it) p->lvtbl->it = node;
13060 return node;
13061 }
13062 return NEW_VCALL(id, loc);
13063 case ID_GLOBAL:
13064 return NEW_GVAR(id, loc);
13065 case ID_INSTANCE:
13066 return NEW_IVAR(id, loc);
13067 case ID_CONST:
13068 return NEW_CONST(id, loc);
13069 case ID_CLASS:
13070 return NEW_CVAR(id, loc);
13071 }
13072 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13073 return 0;
13074}
13075
13076static rb_node_opt_arg_t *
13077opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13078{
13079 rb_node_opt_arg_t *opts = opt_list;
13080 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13081
13082 while (opts->nd_next) {
13083 opts = opts->nd_next;
13084 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13085 }
13086 opts->nd_next = opt;
13087
13088 return opt_list;
13089}
13090
13091static rb_node_kw_arg_t *
13092kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13093{
13094 if (kwlist) {
13095 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13096 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13097 }
13098 return kwlist;
13099}
13100
13101static NODE *
13102new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13103{
13104 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13105 p->ctxt.has_trailing_semicolon = 0;
13106
13107 NODE *n = expr;
13108 while (n) {
13109 if (nd_type_p(n, NODE_BEGIN)) {
13110 n = RNODE_BEGIN(n)->nd_body;
13111 }
13112 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13113 n = RNODE_BLOCK(n)->nd_head;
13114 }
13115 else {
13116 break;
13117 }
13118 }
13119
13120 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13121 NODE *block = NEW_BLOCK(expr, loc);
13122 return NEW_DEFINED(block, loc, keyword_loc);
13123 }
13124
13125 return NEW_DEFINED(n, loc, keyword_loc);
13126}
13127
13128static NODE*
13129str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13130{
13131 VALUE lit;
13132 rb_parser_string_t *str = RNODE_STR(node)->string;
13133 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13134 yyerror1(loc, "invalid symbol");
13135 lit = STR_NEW0();
13136 }
13137 else {
13138 lit = rb_str_new_parser_string(str);
13139 }
13140 return NEW_SYM(lit, loc);
13141}
13142
13143static NODE*
13144symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13145{
13146 enum node_type type = nd_type(symbol);
13147 switch (type) {
13148 case NODE_DSTR:
13149 nd_set_type(symbol, NODE_DSYM);
13150 break;
13151 case NODE_STR:
13152 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13153 break;
13154 default:
13155 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13156 }
13157 return list_append(p, symbols, symbol);
13158}
13159
13160static void
13161dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13162{
13163 if (dreg->string) {
13164 reg_fragment_setenc(p, dreg->string, options);
13165 }
13166 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13167 NODE *frag = list->nd_head;
13168 if (nd_type_p(frag, NODE_STR)) {
13169 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13170 }
13171 else if (nd_type_p(frag, NODE_DSTR)) {
13172 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13173 }
13174 }
13175}
13176
13177static NODE *
13178new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13179{
13180 if (!node) {
13181 /* Check string is valid regex */
13182 rb_parser_string_t *str = STRING_NEW0();
13183 reg_compile(p, str, options);
13184 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13185 return node;
13186 }
13187 switch (nd_type(node)) {
13188 case NODE_STR:
13189 {
13190 /* Check string is valid regex */
13191 reg_compile(p, RNODE_STR(node)->string, options);
13192 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13193 }
13194 break;
13195 default:
13196 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13197 /* fall through */
13198 case NODE_DSTR:
13199 nd_set_type(node, NODE_DREGX);
13200 nd_set_loc(node, loc);
13201 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13202 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13203 if (dreg->nd_next) {
13204 dregex_fragment_setenc(p, dreg, options);
13205 }
13206 if (options & RE_OPTION_ONCE) {
13207 node = NEW_ONCE(node, loc);
13208 }
13209 break;
13210 }
13211 return node;
13212}
13213
13214static rb_node_kw_arg_t *
13215new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13216{
13217 if (!k) return 0;
13218 return NEW_KW_ARG((k), loc);
13219}
13220
13221static NODE *
13222new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13223{
13224 if (!node) {
13225 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13226 return xstr;
13227 }
13228 switch (nd_type(node)) {
13229 case NODE_STR:
13230 nd_set_type(node, NODE_XSTR);
13231 nd_set_loc(node, loc);
13232 break;
13233 case NODE_DSTR:
13234 nd_set_type(node, NODE_DXSTR);
13235 nd_set_loc(node, loc);
13236 break;
13237 default:
13238 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13239 break;
13240 }
13241 return node;
13242}
13243
13244static const
13245struct st_hash_type literal_type = {
13246 literal_cmp,
13247 literal_hash,
13248};
13249
13250static int nd_type_st_key_enable_p(NODE *node);
13251
13252static void
13253check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13254{
13255 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13256 if (!arg || !p->case_labels) return;
13257 if (!nd_type_st_key_enable_p(arg)) return;
13258
13259 if (p->case_labels == CHECK_LITERAL_WHEN) {
13260 p->case_labels = st_init_table(&literal_type);
13261 }
13262 else {
13263 st_data_t line;
13264 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13265 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13266 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13267 return;
13268 }
13269 }
13270 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13271}
13272
13273#ifdef RIPPER
13274static int
13275id_is_var(struct parser_params *p, ID id)
13276{
13277 if (is_notop_id(id)) {
13278 switch (id & ID_SCOPE_MASK) {
13279 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13280 return 1;
13281 case ID_LOCAL:
13282 if (dyna_in_block(p)) {
13283 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13284 }
13285 if (local_id(p, id)) return 1;
13286 /* method call without arguments */
13287 return 0;
13288 }
13289 }
13290 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13291 return 0;
13292}
13293#endif
13294
13295static inline enum lex_state_e
13296parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13297{
13298 if (p->debug) {
13299 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13300 }
13301 return p->lex.state = ls;
13302}
13303
13304#ifndef RIPPER
13305static void
13306flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13307{
13308 VALUE mesg = p->debug_buffer;
13309
13310 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13311 p->debug_buffer = Qnil;
13312 rb_io_puts(1, &mesg, out);
13313 }
13314 if (!NIL_P(str) && RSTRING_LEN(str)) {
13315 rb_io_write(p->debug_output, str);
13316 }
13317}
13318
13319static const char rb_parser_lex_state_names[][8] = {
13320 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13321 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13322 "LABEL", "LABELED","FITEM",
13323};
13324
13325static VALUE
13326append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13327{
13328 int i, sep = 0;
13329 unsigned int mask = 1;
13330 static const char none[] = "NONE";
13331
13332 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13333 if ((unsigned)state & mask) {
13334 if (sep) {
13335 rb_str_cat(buf, "|", 1);
13336 }
13337 sep = 1;
13338 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13339 }
13340 }
13341 if (!sep) {
13342 rb_str_cat(buf, none, sizeof(none)-1);
13343 }
13344 return buf;
13345}
13346
13347enum lex_state_e
13348rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13349 enum lex_state_e to, int line)
13350{
13351 VALUE mesg;
13352 mesg = rb_str_new_cstr("lex_state: ");
13353 append_lex_state_name(p, from, mesg);
13354 rb_str_cat_cstr(mesg, " -> ");
13355 append_lex_state_name(p, to, mesg);
13356 rb_str_catf(mesg, " at line %d\n", line);
13357 flush_debug_buffer(p, p->debug_output, mesg);
13358 return to;
13359}
13360
13361VALUE
13362rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13363{
13364 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13365}
13366
13367static void
13368append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13369{
13370 if (stack == 0) {
13371 rb_str_cat_cstr(mesg, "0");
13372 }
13373 else {
13374 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13375 for (; mask && !(stack & mask); mask >>= 1) continue;
13376 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13377 }
13378}
13379
13380void
13381rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13382 const char *name, int line)
13383{
13384 VALUE mesg = rb_sprintf("%s: ", name);
13385 append_bitstack_value(p, stack, mesg);
13386 rb_str_catf(mesg, " at line %d\n", line);
13387 flush_debug_buffer(p, p->debug_output, mesg);
13388}
13389
13390void
13391rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13392{
13393 va_list ap;
13394 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13395
13396 va_start(ap, fmt);
13397 rb_str_vcatf(mesg, fmt, ap);
13398 va_end(ap);
13399 yyerror0(RSTRING_PTR(mesg));
13400 RB_GC_GUARD(mesg);
13401
13402 mesg = rb_str_new(0, 0);
13403 append_lex_state_name(p, p->lex.state, mesg);
13404 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13405 rb_str_resize(mesg, 0);
13406 append_bitstack_value(p, p->cond_stack, mesg);
13407 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13408 rb_str_resize(mesg, 0);
13409 append_bitstack_value(p, p->cmdarg_stack, mesg);
13410 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13411 if (p->debug_output == rb_ractor_stdout())
13412 p->debug_output = rb_ractor_stderr();
13413 p->debug = TRUE;
13414}
13415
13416static YYLTYPE *
13417rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13418{
13419 yylloc->beg_pos.lineno = sourceline;
13420 yylloc->beg_pos.column = beg_pos;
13421 yylloc->end_pos.lineno = sourceline;
13422 yylloc->end_pos.column = end_pos;
13423 return yylloc;
13424}
13425
13426YYLTYPE *
13427rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13428{
13429 int sourceline = here->sourceline;
13430 int beg_pos = parser_column(here->offset - here->quote
13431 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT)));
13432 int end_pos = parser_column(here->offset + here->length + here->quote);
13433
13434 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13435}
13436
13437YYLTYPE *
13438rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13439{
13440 yylloc->beg_pos.lineno = p->delayed.beg_line;
13441 yylloc->beg_pos.column = p->delayed.beg_col;
13442 yylloc->end_pos.lineno = p->delayed.end_line;
13443 yylloc->end_pos.column = p->delayed.end_col;
13444
13445 return yylloc;
13446}
13447
13448YYLTYPE *
13449rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13450{
13451 int sourceline = p->ruby_sourceline;
13452 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13453 int end_pos = parser_column(p->lex.pend - p->lex.pbeg);
13454 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13455}
13456
13457YYLTYPE *
13458rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13459{
13460 yylloc->end_pos = yylloc->beg_pos;
13461
13462 return yylloc;
13463}
13464
13465YYLTYPE *
13466rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13467{
13468 int sourceline = p->ruby_sourceline;
13469 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13470 int end_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13471 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13472}
13473
13474YYLTYPE *
13475rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13476{
13477 int sourceline = p->ruby_sourceline;
13478 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13479 int end_pos = parser_column(p->lex.pcur - p->lex.pbeg);
13480 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13481}
13482#endif /* !RIPPER */
13483
13484static int
13485assignable0(struct parser_params *p, ID id, const char **err)
13486{
13487 if (!id) return -1;
13488 switch (id) {
13489 case keyword_self:
13490 *err = "Can't change the value of self";
13491 return -1;
13492 case keyword_nil:
13493 *err = "Can't assign to nil";
13494 return -1;
13495 case keyword_true:
13496 *err = "Can't assign to true";
13497 return -1;
13498 case keyword_false:
13499 *err = "Can't assign to false";
13500 return -1;
13501 case keyword__FILE__:
13502 *err = "Can't assign to __FILE__";
13503 return -1;
13504 case keyword__LINE__:
13505 *err = "Can't assign to __LINE__";
13506 return -1;
13507 case keyword__ENCODING__:
13508 *err = "Can't assign to __ENCODING__";
13509 return -1;
13510 }
13511 switch (id_type(id)) {
13512 case ID_LOCAL:
13513 if (dyna_in_block(p)) {
13514 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13515 compile_error(p, "Can't assign to numbered parameter _%d",
13516 NUMPARAM_ID_TO_IDX(id));
13517 return -1;
13518 }
13519 if (dvar_curr(p, id)) return NODE_DASGN;
13520 if (dvar_defined(p, id)) return NODE_DASGN;
13521 if (local_id(p, id)) return NODE_LASGN;
13522 dyna_var(p, id);
13523 return NODE_DASGN;
13524 }
13525 else {
13526 if (!local_id(p, id)) local_var(p, id);
13527 return NODE_LASGN;
13528 }
13529 break;
13530 case ID_GLOBAL: return NODE_GASGN;
13531 case ID_INSTANCE: return NODE_IASGN;
13532 case ID_CONST:
13533 if (!p->ctxt.in_def) return NODE_CDECL;
13534 *err = "dynamic constant assignment";
13535 return -1;
13536 case ID_CLASS: return NODE_CVASGN;
13537 default:
13538 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13539 }
13540 return -1;
13541}
13542
13543static NODE*
13544assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13545{
13546 const char *err = 0;
13547 int node_type = assignable0(p, id, &err);
13548 switch (node_type) {
13549 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13550 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13551 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13552 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13553 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13554 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13555 }
13556/* TODO: FIXME */
13557#ifndef RIPPER
13558 if (err) yyerror1(loc, err);
13559#else
13560 if (err) set_value(assign_error(p, err, p->s_lvalue));
13561#endif
13562 return NEW_ERROR(loc);
13563}
13564
13565static int
13566is_private_local_id(struct parser_params *p, ID name)
13567{
13568 VALUE s;
13569 if (name == idUScore) return 1;
13570 if (!is_local_id(name)) return 0;
13571 s = rb_id2str(name);
13572 if (!s) return 0;
13573 return RSTRING_PTR(s)[0] == '_';
13574}
13575
13576static int
13577shadowing_lvar_0(struct parser_params *p, ID name)
13578{
13579 if (dyna_in_block(p)) {
13580 if (dvar_curr(p, name)) {
13581 if (is_private_local_id(p, name)) return 1;
13582 yyerror0("duplicated argument name");
13583 }
13584 else if (dvar_defined(p, name) || local_id(p, name)) {
13585 vtable_add(p->lvtbl->vars, name);
13586 if (p->lvtbl->used) {
13587 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13588 }
13589 return 0;
13590 }
13591 }
13592 else {
13593 if (local_id(p, name)) {
13594 if (is_private_local_id(p, name)) return 1;
13595 yyerror0("duplicated argument name");
13596 }
13597 }
13598 return 1;
13599}
13600
13601static ID
13602shadowing_lvar(struct parser_params *p, ID name)
13603{
13604 shadowing_lvar_0(p, name);
13605 return name;
13606}
13607
13608static void
13609new_bv(struct parser_params *p, ID name)
13610{
13611 if (!name) return;
13612 if (!is_local_id(name)) {
13613 compile_error(p, "invalid local variable - %"PRIsVALUE,
13614 rb_id2str(name));
13615 return;
13616 }
13617 if (!shadowing_lvar_0(p, name)) return;
13618 dyna_var(p, name);
13619 ID *vidp = 0;
13620 if (dvar_defined_ref(p, name, &vidp)) {
13621 if (vidp) *vidp |= LVAR_USED;
13622 }
13623}
13624
13625static void
13626aryset_check(struct parser_params *p, NODE *args)
13627{
13628 NODE *block = 0, *kwds = 0;
13629 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13630 block = RNODE_BLOCK_PASS(args)->nd_body;
13631 args = RNODE_BLOCK_PASS(args)->nd_head;
13632 }
13633 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13634 args = RNODE_ARGSCAT(args)->nd_body;
13635 }
13636 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13637 kwds = RNODE_ARGSPUSH(args)->nd_body;
13638 }
13639 else {
13640 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13641 next = RNODE_LIST(next)->nd_next) {
13642 kwds = RNODE_LIST(next)->nd_head;
13643 }
13644 }
13645 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13646 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13647 }
13648 if (block) {
13649 yyerror1(&block->nd_loc, "block arg given in index assignment");
13650 }
13651}
13652
13653static NODE *
13654aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13655{
13656 aryset_check(p, idx);
13657 return NEW_ATTRASGN(recv, tASET, idx, loc);
13658}
13659
13660static void
13661block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13662{
13663 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13664 compile_error(p, "both block arg and actual block given");
13665 }
13666}
13667
13668static NODE *
13669attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13670{
13671 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13672 return NEW_ATTRASGN(recv, id, 0, loc);
13673}
13674
13675static VALUE
13676rb_backref_error(struct parser_params *p, NODE *node)
13677{
13678#ifndef RIPPER
13679# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13680#else
13681# define ERR(...) rb_sprintf(__VA_ARGS__)
13682#endif
13683 switch (nd_type(node)) {
13684 case NODE_NTH_REF:
13685 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13686 case NODE_BACK_REF:
13687 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13688 }
13689#undef ERR
13690 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13691}
13692
13693static NODE *
13694arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13695{
13696 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13697 switch (nd_type(node1)) {
13698 case NODE_LIST:
13699 return list_append(p, node1, node2);
13700 case NODE_BLOCK_PASS:
13701 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13702 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13703 return node1;
13704 case NODE_ARGSPUSH:
13705 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13706 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13707 nd_set_type(node1, NODE_ARGSCAT);
13708 return node1;
13709 case NODE_ARGSCAT:
13710 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13711 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13712 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13713 return node1;
13714 }
13715 return NEW_ARGSPUSH(node1, node2, loc);
13716}
13717
13718static NODE *
13719arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13720{
13721 if (!node2) return node1;
13722 switch (nd_type(node1)) {
13723 case NODE_BLOCK_PASS:
13724 if (RNODE_BLOCK_PASS(node1)->nd_head)
13725 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13726 else
13727 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13728 return node1;
13729 case NODE_ARGSPUSH:
13730 if (!nd_type_p(node2, NODE_LIST)) break;
13731 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13732 nd_set_type(node1, NODE_ARGSCAT);
13733 return node1;
13734 case NODE_ARGSCAT:
13735 if (!nd_type_p(node2, NODE_LIST) ||
13736 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13737 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13738 return node1;
13739 }
13740 return NEW_ARGSCAT(node1, node2, loc);
13741}
13742
13743static NODE *
13744last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13745{
13746 NODE *n1;
13747 if ((n1 = splat_array(args)) != 0) {
13748 return list_append(p, n1, last_arg);
13749 }
13750 return arg_append(p, args, last_arg, loc);
13751}
13752
13753static NODE *
13754rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13755{
13756 NODE *n1;
13757 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13758 return list_concat(n1, rest_arg);
13759 }
13760 return arg_concat(p, args, rest_arg, loc);
13761}
13762
13763static NODE *
13764splat_array(NODE* node)
13765{
13766 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13767 if (nd_type_p(node, NODE_LIST)) return node;
13768 return 0;
13769}
13770
13771static void
13772mark_lvar_used(struct parser_params *p, NODE *rhs)
13773{
13774 ID *vidp = NULL;
13775 if (!rhs) return;
13776 switch (nd_type(rhs)) {
13777 case NODE_LASGN:
13778 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13779 if (vidp) *vidp |= LVAR_USED;
13780 }
13781 break;
13782 case NODE_DASGN:
13783 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13784 if (vidp) *vidp |= LVAR_USED;
13785 }
13786 break;
13787#if 0
13788 case NODE_MASGN:
13789 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13790 mark_lvar_used(p, rhs->nd_head);
13791 }
13792 break;
13793#endif
13794 }
13795}
13796
13797static int is_static_content(NODE *node);
13798
13799static NODE *
13800node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13801{
13802 if (!lhs) return 0;
13803
13804 switch (nd_type(lhs)) {
13805 case NODE_CDECL:
13806 case NODE_GASGN:
13807 case NODE_IASGN:
13808 case NODE_LASGN:
13809 case NODE_DASGN:
13810 case NODE_MASGN:
13811 case NODE_CVASGN:
13812 set_nd_value(p, lhs, rhs);
13813 nd_set_loc(lhs, loc);
13814 break;
13815
13816 case NODE_ATTRASGN:
13817 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13818 nd_set_loc(lhs, loc);
13819 break;
13820
13821 default:
13822 /* should not happen */
13823 break;
13824 }
13825
13826 return lhs;
13827}
13828
13829static NODE *
13830value_expr_check(struct parser_params *p, NODE *node)
13831{
13832 NODE *void_node = 0, *vn;
13833
13834 if (!node) {
13835 rb_warning0("empty expression");
13836 }
13837 while (node) {
13838 switch (nd_type(node)) {
13839 case NODE_ENSURE:
13840 vn = RNODE_ENSURE(node)->nd_head;
13841 node = RNODE_ENSURE(node)->nd_ensr;
13842 /* nd_ensr should not be NULL, check it out next */
13843 if (vn && (vn = value_expr_check(p, vn))) {
13844 goto found;
13845 }
13846 break;
13847
13848 case NODE_RESCUE:
13849 /* void only if all children are void */
13850 vn = RNODE_RESCUE(node)->nd_head;
13851 if (!vn || !(vn = value_expr_check(p, vn))) {
13852 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13853 }
13854 if (!void_node) void_node = vn;
13855 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13856 if (!nd_type_p(r, NODE_RESBODY)) {
13857 compile_error(p, "unexpected node");
13858 return NULL;
13859 }
13860 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13861 return NULL;
13862 }
13863 if (!void_node) void_node = vn;
13864 }
13865 node = RNODE_RESCUE(node)->nd_else;
13866 if (!node) return void_node;
13867 break;
13868
13869 case NODE_RETURN:
13870 case NODE_BREAK:
13871 case NODE_NEXT:
13872 case NODE_REDO:
13873 case NODE_RETRY:
13874 goto found;
13875
13876 case NODE_CASE:
13877 case NODE_CASE2:
13878 for (node = RNODE_CASE(node)->nd_body;
13879 node && nd_type_p(node, NODE_WHEN);
13880 node = RNODE_WHEN(node)->nd_next) {
13881 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13882 return NULL;
13883 }
13884 if (!void_node) void_node = vn;
13885 }
13886 break;
13887
13888 case NODE_CASE3:
13889 {
13890 NODE *in = RNODE_CASE3(node)->nd_body;
13891 if (!in || !nd_type_p(in, NODE_IN)) {
13892 compile_error(p, "unexpected node");
13893 return NULL;
13894 }
13895 if (!RNODE_IN(in)->nd_body) {
13896 /* single line pattern matching with "=>" operator */
13897 goto found;
13898 }
13899 do {
13900 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13901 if (!vn) return NULL;
13902 if (!void_node) void_node = vn;
13903 in = RNODE_IN(in)->nd_next;
13904 } while (in && nd_type_p(in, NODE_IN));
13905 node = in; /* else */
13906 }
13907 break;
13908
13909 case NODE_BLOCK:
13910 while (RNODE_BLOCK(node)->nd_next) {
13911 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13912 if (vn) return vn;
13913 node = RNODE_BLOCK(node)->nd_next;
13914 }
13915 node = RNODE_BLOCK(node)->nd_head;
13916 break;
13917
13918 case NODE_BEGIN:
13919 node = RNODE_BEGIN(node)->nd_body;
13920 break;
13921
13922 case NODE_IF:
13923 case NODE_UNLESS:
13924 if (!RNODE_IF(node)->nd_body) {
13925 return NULL;
13926 }
13927 else if (!RNODE_IF(node)->nd_else) {
13928 return NULL;
13929 }
13930 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13931 if (!vn) return NULL;
13932 if (!void_node) void_node = vn;
13933 node = RNODE_IF(node)->nd_else;
13934 break;
13935
13936 case NODE_AND:
13937 case NODE_OR:
13938 /* The left operand was already checked for a value when logop()
13939 * built this node, so stop here instead of re-reporting the same
13940 * void value once per operator in a chain. */
13941 return NULL;
13942
13943 case NODE_LASGN:
13944 case NODE_DASGN:
13945 case NODE_MASGN:
13946 mark_lvar_used(p, node);
13947 return NULL;
13948
13949 default:
13950 return NULL;
13951 }
13952 }
13953
13954 return NULL;
13955
13956 found:
13957 /* return the first found node */
13958 return void_node ? void_node : node;
13959}
13960
13961static int
13962value_expr(struct parser_params *p, NODE *node)
13963{
13964 NODE *void_node = value_expr_check(p, node);
13965 if (void_node) {
13966 yyerror1(&void_node->nd_loc, "void value expression");
13967 /* or "control never reach"? */
13968 return FALSE;
13969 }
13970 return TRUE;
13971}
13972
13973static void
13974void_expr(struct parser_params *p, NODE *node)
13975{
13976 const char *useless = 0;
13977
13978 if (!RTEST(ruby_verbose)) return;
13979
13980 if (!node || !(node = nd_once_body(node))) return;
13981 switch (nd_type(node)) {
13982 case NODE_OPCALL:
13983 switch (RNODE_OPCALL(node)->nd_mid) {
13984 case '+':
13985 case '-':
13986 case '*':
13987 case '/':
13988 case '%':
13989 case tPOW:
13990 case tUPLUS:
13991 case tUMINUS:
13992 case '|':
13993 case '^':
13994 case '&':
13995 case tCMP:
13996 case '>':
13997 case tGEQ:
13998 case '<':
13999 case tLEQ:
14000 case tEQ:
14001 case tNEQ:
14002 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
14003 break;
14004 }
14005 break;
14006
14007 case NODE_LVAR:
14008 case NODE_DVAR:
14009 case NODE_GVAR:
14010 case NODE_IVAR:
14011 case NODE_CVAR:
14012 case NODE_NTH_REF:
14013 case NODE_BACK_REF:
14014 useless = "a variable";
14015 break;
14016 case NODE_CONST:
14017 useless = "a constant";
14018 break;
14019 case NODE_SYM:
14020 case NODE_LINE:
14021 case NODE_FILE:
14022 case NODE_ENCODING:
14023 case NODE_INTEGER:
14024 case NODE_FLOAT:
14025 case NODE_RATIONAL:
14026 case NODE_IMAGINARY:
14027 case NODE_STR:
14028 case NODE_DSTR:
14029 case NODE_REGX:
14030 case NODE_DREGX:
14031 useless = "a literal";
14032 break;
14033 case NODE_COLON2:
14034 case NODE_COLON3:
14035 useless = "::";
14036 break;
14037 case NODE_DOT2:
14038 useless = "..";
14039 break;
14040 case NODE_DOT3:
14041 useless = "...";
14042 break;
14043 case NODE_SELF:
14044 useless = "self";
14045 break;
14046 case NODE_NIL:
14047 useless = "nil";
14048 break;
14049 case NODE_TRUE:
14050 useless = "true";
14051 break;
14052 case NODE_FALSE:
14053 useless = "false";
14054 break;
14055 case NODE_DEFINED:
14056 useless = "defined?";
14057 break;
14058 }
14059
14060 if (useless) {
14061 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14062 }
14063}
14064
14065/* warns useless use of block and returns the last statement node */
14066static NODE *
14067void_stmts(struct parser_params *p, NODE *node)
14068{
14069 NODE *const n = node;
14070 if (!RTEST(ruby_verbose)) return n;
14071 if (!node) return n;
14072 if (!nd_type_p(node, NODE_BLOCK)) return n;
14073
14074 while (RNODE_BLOCK(node)->nd_next) {
14075 void_expr(p, RNODE_BLOCK(node)->nd_head);
14076 node = RNODE_BLOCK(node)->nd_next;
14077 }
14078 return RNODE_BLOCK(node)->nd_head;
14079}
14080
14081static NODE *
14082remove_begin(NODE *node)
14083{
14084 NODE **n = &node, *n1 = node;
14085 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14086 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14087 }
14088 return node;
14089}
14090
14091static void
14092reduce_nodes(struct parser_params *p, NODE **body)
14093{
14094 NODE *node = *body;
14095
14096 if (!node) {
14097 *body = NEW_NIL(&NULL_LOC);
14098 return;
14099 }
14100#define subnodes(type, n1, n2) \
14101 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14102 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14103 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14104
14105 while (node) {
14106 int newline = (int)nd_fl_newline(node);
14107 switch (nd_type(node)) {
14108 case NODE_NIL:
14109 // Keep an explicit nil in a method's tail (value) position when it
14110 // is on its own line, so it still emits a :line event and records
14111 // line coverage for that line. [Bug #22302]
14112 if (newline) return;
14113 end:
14114 *body = 0;
14115 return;
14116 case NODE_BEGIN:
14117 *body = node = RNODE_BEGIN(node)->nd_body;
14118 if (newline && node) nd_set_fl_newline(node);
14119 continue;
14120 case NODE_BLOCK:
14121 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14122 break;
14123 case NODE_IF:
14124 case NODE_UNLESS:
14125 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14126 return;
14127 case NODE_CASE:
14128 body = &RNODE_CASE(node)->nd_body;
14129 break;
14130 case NODE_WHEN:
14131 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14132 break;
14133 case NODE_ENSURE:
14134 body = &RNODE_ENSURE(node)->nd_head;
14135 break;
14136 case NODE_RESCUE:
14137 newline = 0; // RESBODY should not be a NEWLINE
14138 if (RNODE_RESCUE(node)->nd_else) {
14139 body = &RNODE_RESCUE(node)->nd_resq;
14140 break;
14141 }
14142 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14143 break;
14144 default:
14145 return;
14146 }
14147 node = *body;
14148 if (newline && node) nd_set_fl_newline(node);
14149 }
14150
14151#undef subnodes
14152}
14153
14154static int
14155is_static_content(NODE *node)
14156{
14157 if (!node) return 1;
14158 switch (nd_type(node)) {
14159 case NODE_HASH:
14160 if (!(node = RNODE_HASH(node)->nd_head)) break;
14161 case NODE_LIST:
14162 do {
14163 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14164 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14165 case NODE_SYM:
14166 case NODE_REGX:
14167 case NODE_LINE:
14168 case NODE_FILE:
14169 case NODE_ENCODING:
14170 case NODE_INTEGER:
14171 case NODE_FLOAT:
14172 case NODE_RATIONAL:
14173 case NODE_IMAGINARY:
14174 case NODE_STR:
14175 case NODE_NIL:
14176 case NODE_TRUE:
14177 case NODE_FALSE:
14178 case NODE_ZLIST:
14179 break;
14180 default:
14181 return 0;
14182 }
14183 return 1;
14184}
14185
14186static int
14187assign_in_cond(struct parser_params *p, NODE *node)
14188{
14189 switch (nd_type(node)) {
14190 case NODE_MASGN:
14191 case NODE_LASGN:
14192 case NODE_DASGN:
14193 case NODE_GASGN:
14194 case NODE_IASGN:
14195 case NODE_CVASGN:
14196 case NODE_CDECL:
14197 break;
14198
14199 default:
14200 return 0;
14201 }
14202
14203 if (!get_nd_value(p, node)) return 1;
14204 if (is_static_content(get_nd_value(p, node))) {
14205 /* reports always */
14206 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14207 }
14208 return 1;
14209}
14210
14211enum cond_type {
14212 COND_IN_OP,
14213 COND_IN_COND,
14214 COND_IN_FF
14215};
14216
14217#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14218 switch (t) { \
14219 case COND_IN_OP: break; \
14220 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14221 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14222 } \
14223} while (0)
14224
14225static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14226
14227static NODE*
14228range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14229{
14230 enum node_type type;
14231
14232 if (node == 0) return 0;
14233
14234 type = nd_type(node);
14235 value_expr(p, node);
14236 if (type == NODE_INTEGER) {
14237 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14238 ID lineno = rb_intern("$.");
14239 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14240 }
14241 return cond0(p, node, COND_IN_FF, loc, true);
14242}
14243
14244static NODE*
14245cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14246{
14247 if (node == 0) return 0;
14248 if (!(node = nd_once_body(node))) return 0;
14249 assign_in_cond(p, node);
14250
14251 switch (nd_type(node)) {
14252 case NODE_BEGIN:
14253 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14254 break;
14255
14256 case NODE_DSTR:
14257 case NODE_EVSTR:
14258 case NODE_STR:
14259 case NODE_FILE:
14260 SWITCH_BY_COND_TYPE(type, warn, "string ");
14261 break;
14262
14263 case NODE_REGX:
14264 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14265 nd_set_type(node, NODE_MATCH);
14266 break;
14267
14268 case NODE_DREGX:
14269 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14270
14271 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14272
14273 case NODE_BLOCK:
14274 {
14275 NODE *end = RNODE_BLOCK(node)->nd_end;
14276 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14277 if (top) top = node == end;
14278 *expr = cond0(p, *expr, type, loc, top);
14279 }
14280 break;
14281
14282 case NODE_AND:
14283 case NODE_OR:
14284 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14285 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14286 break;
14287
14288 case NODE_DOT2:
14289 case NODE_DOT3:
14290 if (!top) break;
14291 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14292 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14293 switch (nd_type(node)) {
14294 case NODE_DOT2:
14295 nd_set_type(node,NODE_FLIP2);
14296 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14297 (void)flip2;
14298 break;
14299 case NODE_DOT3:
14300 nd_set_type(node, NODE_FLIP3);
14301 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14302 (void)flip3;
14303 break;
14304 }
14305 break;
14306
14307 case NODE_SYM:
14308 case NODE_DSYM:
14309 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14310 break;
14311
14312 case NODE_LINE:
14313 case NODE_ENCODING:
14314 case NODE_INTEGER:
14315 case NODE_FLOAT:
14316 case NODE_RATIONAL:
14317 case NODE_IMAGINARY:
14318 SWITCH_BY_COND_TYPE(type, warning, "");
14319 break;
14320
14321 default:
14322 break;
14323 }
14324 return node;
14325}
14326
14327static NODE*
14328cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14329{
14330 if (node == 0) return 0;
14331 return cond0(p, node, COND_IN_COND, loc, true);
14332}
14333
14334static NODE*
14335method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14336{
14337 if (node == 0) return 0;
14338 return cond0(p, node, COND_IN_OP, loc, true);
14339}
14340
14341static NODE*
14342new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14343{
14344 YYLTYPE loc = {*pos, *pos};
14345 return NEW_NIL(&loc);
14346}
14347
14348static NODE*
14349new_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)
14350{
14351 if (!cc) return right;
14352 cc = cond0(p, cc, COND_IN_COND, loc, true);
14353 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14354}
14355
14356static NODE*
14357new_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)
14358{
14359 if (!cc) return right;
14360 cc = cond0(p, cc, COND_IN_COND, loc, true);
14361 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14362}
14363
14364#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))
14365
14366/* A cached logop tail is usable only if it is still a node of the chain's type
14367 * whose nd_2nd is the insertion point, i.e. not itself another node of that
14368 * type. This rejects a stale cache left over from an earlier parse. */
14369static int
14370logop_valid_tail(NODE *tail, enum node_type type)
14371{
14372 NODE *second;
14373 return tail && nd_type_p(tail, type) &&
14374 ((second = RNODE_AND(tail)->nd_2nd) == 0 || !nd_type_p(second, type));
14375}
14376
14377static NODE*
14378logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14379 const YYLTYPE *op_loc, const YYLTYPE *loc)
14380{
14381 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14382 NODE *op;
14383 /* Snapshot the cache from the previous logop() call before overwriting it. */
14384 NODE *prev_head = p->logop.head;
14385 NODE *prev_tail = p->logop.tail;
14386 NODE *node, *second;
14387 value_expr(p, left);
14388 if (left && nd_type_p(left, type)) {
14389 /* The insertion point is the far end of left's right branch.
14390 * Reusing the cached tail keeps a chain such as `a && a && ... && a`
14391 * linear instead of rescanning the whole branch on every operator. The
14392 * cache is validated (its nd_2nd is not another node of the same type)
14393 * so a stale entry falls back to the scan. */
14394 if (left == prev_head && logop_valid_tail(prev_tail, type)) {
14395 node = prev_tail;
14396 }
14397 else {
14398 node = left;
14399 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14400 node = second;
14401 }
14402 }
14403 second = RNODE_AND(node)->nd_2nd;
14404 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14405 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14406 left->nd_loc.end_pos = loc->end_pos;
14407 p->logop.head = left;
14408 p->logop.tail = RNODE_AND(node)->nd_2nd;
14409 return left;
14410 }
14411 op = NEW_AND_OR(type, left, right, loc, op_loc);
14412 nd_set_line(op, op_loc->beg_pos.lineno);
14413 /* Record where the next operator will extend op, mirroring the scan the
14414 * chained branch above would perform: if right is itself a chain of the
14415 * same type (from parentheses), its far end; otherwise op itself. */
14416 p->logop.head = op;
14417 if (right == prev_head && logop_valid_tail(prev_tail, type)) {
14418 p->logop.tail = prev_tail;
14419 }
14420 else if (right && nd_type_p(right, type)) {
14421 node = right;
14422 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14423 node = second;
14424 }
14425 p->logop.tail = node;
14426 }
14427 else {
14428 p->logop.tail = op;
14429 }
14430 return op;
14431}
14432
14433#undef NEW_AND_OR
14434
14435static void
14436no_blockarg(struct parser_params *p, NODE *node)
14437{
14438 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14439 compile_error(p, "block argument should not be given");
14440 }
14441}
14442
14443static NODE *
14444ret_args(struct parser_params *p, NODE *node)
14445{
14446 if (node) {
14447 no_blockarg(p, node);
14448 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14449 node = RNODE_LIST(node)->nd_head;
14450 }
14451 }
14452 return node;
14453}
14454
14455static NODE*
14456negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14457{
14458 switch (nd_type(node)) {
14459 case NODE_INTEGER:
14460 RNODE_INTEGER(node)->minus = TRUE;
14461 break;
14462 case NODE_FLOAT:
14463 RNODE_FLOAT(node)->minus = TRUE;
14464 break;
14465 case NODE_RATIONAL:
14466 RNODE_RATIONAL(node)->minus = TRUE;
14467 break;
14468 case NODE_IMAGINARY:
14469 RNODE_IMAGINARY(node)->minus = TRUE;
14470 break;
14471 }
14472 node->nd_loc = *loc;
14473 return node;
14474}
14475
14476static NODE *
14477arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14478{
14479 if (node2) {
14480 if (!node1) return (NODE *)node2;
14481 node2->nd_head = node1;
14482 nd_set_first_lineno(node2, nd_first_lineno(node1));
14483 nd_set_first_column(node2, nd_first_column(node1));
14484 return (NODE *)node2;
14485 }
14486 return node1;
14487}
14488
14489static bool
14490args_info_empty_p(struct rb_args_info *args)
14491{
14492 if (args->pre_args_num) return false;
14493 if (args->post_args_num) return false;
14494 if (args->rest_arg) return false;
14495 if (args->opt_args) return false;
14496 if (args->block_arg) return false;
14497 if (args->kw_args) return false;
14498 if (args->kw_rest_arg) return false;
14499 return true;
14500}
14501
14502static rb_node_args_t *
14503new_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)
14504{
14505 struct rb_args_info *args = &tail->nd_ainfo;
14506
14507 if (args->forwarding) {
14508 if (rest_arg) {
14509 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14510 return tail;
14511 }
14512 rest_arg = idFWD_REST;
14513 }
14514
14515 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14516 args->pre_init = pre_args ? pre_args->nd_next : 0;
14517
14518 args->post_args_num = post_args ? post_args->nd_plen : 0;
14519 args->post_init = post_args ? post_args->nd_next : 0;
14520 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14521
14522 args->rest_arg = rest_arg;
14523
14524 args->opt_args = opt_args;
14525
14526 nd_set_loc(RNODE(tail), loc);
14527
14528 return tail;
14529}
14530
14531static rb_node_args_t *
14532new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14533{
14534 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14535 struct rb_args_info *args = &node->nd_ainfo;
14536 if (p->error_p) return node;
14537
14538 if (block == idNil) {
14539 block = 0;
14540 args->no_blockarg = TRUE;
14541 }
14542 args->block_arg = block;
14543 args->kw_args = kw_args;
14544
14545 if (kw_args) {
14546 /*
14547 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14548 * variable order: k1, kr1, k2, &b, internal_id, krest
14549 * #=> <reorder>
14550 * variable order: kr1, k1, k2, internal_id, krest, &b
14551 */
14552 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14553 struct vtable *vtargs = p->lvtbl->args;
14554 rb_node_kw_arg_t *kwn = kw_args;
14555
14556 if (block) block = vtargs->tbl[vtargs->pos-1];
14557 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14558 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14559 while (kwn) {
14560 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14561 --kw_vars;
14562 --required_kw_vars;
14563 kwn = kwn->nd_next;
14564 }
14565
14566 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14567 ID vid = get_nd_vid(p, kwn->nd_body);
14568 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14569 *required_kw_vars++ = vid;
14570 }
14571 else {
14572 *kw_vars++ = vid;
14573 }
14574 }
14575
14576 arg_var(p, kw_bits);
14577 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14578 if (block) arg_var(p, block);
14579
14580 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14581 }
14582 else if (kw_rest_arg == idNil) {
14583 args->no_kwarg = 1;
14584 }
14585 else if (kw_rest_arg) {
14586 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14587 }
14588
14589 return node;
14590}
14591
14592static rb_node_args_t *
14593args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14594{
14595 if (max_numparam > NO_PARAM || it_id) {
14596 if (!args) {
14597 YYLTYPE loc = RUBY_INIT_YYLLOC();
14598 args = new_empty_args_tail(p, 0);
14599 nd_set_loc(RNODE(args), &loc);
14600 }
14601 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14602 }
14603 return args;
14604}
14605
14606static NODE*
14607new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14608{
14609 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14610
14611 if (pre_arg) {
14612 NODE *pre_args = NEW_LIST(pre_arg, loc);
14613 if (RNODE_ARYPTN(aryptn)->pre_args) {
14614 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14615 }
14616 else {
14617 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14618 }
14619 }
14620 return aryptn;
14621}
14622
14623static NODE*
14624new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14625{
14626 if (has_rest) {
14627 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14628 }
14629 else {
14630 rest_arg = NULL;
14631 }
14632 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14633
14634 return node;
14635}
14636
14637static NODE*
14638new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14639{
14640 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14641
14642 return fndptn;
14643}
14644
14645static NODE*
14646new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14647{
14648 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14649 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14650 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14651
14652 return node;
14653}
14654
14655static NODE*
14656new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14657{
14658 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14659 return hshptn;
14660}
14661
14662static NODE*
14663new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14664{
14665 NODE *node, *kw_rest_arg_node;
14666
14667 if (kw_rest_arg == idNil) {
14668 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14669 }
14670 else if (kw_rest_arg) {
14671 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14672 }
14673 else {
14674 kw_rest_arg_node = NULL;
14675 }
14676
14677 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14678
14679 return node;
14680}
14681
14682static NODE*
14683dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14684{
14685 if (!node) {
14686 return NEW_SYM(STR_NEW0(), loc);
14687 }
14688
14689 switch (nd_type(node)) {
14690 case NODE_DSTR:
14691 nd_set_type(node, NODE_DSYM);
14692 nd_set_loc(node, loc);
14693 break;
14694 case NODE_STR:
14695 node = str_to_sym_node(p, node, loc);
14696 break;
14697 default:
14698 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14699 break;
14700 }
14701 return node;
14702}
14703
14704static int
14705nd_type_st_key_enable_p(NODE *node)
14706{
14707 switch (nd_type(node)) {
14708 case NODE_INTEGER:
14709 case NODE_FLOAT:
14710 case NODE_RATIONAL:
14711 case NODE_IMAGINARY:
14712 case NODE_STR:
14713 case NODE_SYM:
14714 case NODE_REGX:
14715 case NODE_LINE:
14716 case NODE_FILE:
14717 case NODE_ENCODING:
14718 return true;
14719 default:
14720 return false;
14721 }
14722}
14723
14724static VALUE
14725nd_value(struct parser_params *p, NODE *node)
14726{
14727 switch (nd_type(node)) {
14728 case NODE_STR:
14729 return rb_node_str_string_val(node);
14730 case NODE_INTEGER:
14731 return rb_node_integer_literal_val(node);
14732 case NODE_FLOAT:
14733 return rb_node_float_literal_val(node);
14734 case NODE_RATIONAL:
14735 return rb_node_rational_literal_val(node);
14736 case NODE_IMAGINARY:
14737 return rb_node_imaginary_literal_val(node);
14738 case NODE_SYM:
14739 return rb_node_sym_string_val(node);
14740 case NODE_REGX:
14741 return rb_node_regx_string_val(node);
14742 case NODE_LINE:
14743 return rb_node_line_lineno_val(node);
14744 case NODE_ENCODING:
14745 return rb_node_encoding_val(node);
14746 case NODE_FILE:
14747 return rb_node_file_path_val(node);
14748 default:
14749 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14750 UNREACHABLE_RETURN(0);
14751 }
14752}
14753
14754static void
14755warn_duplicate_keys(struct parser_params *p, NODE *hash)
14756{
14757 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14758 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14759 while (hash && RNODE_LIST(hash)->nd_next) {
14760 NODE *head = RNODE_LIST(hash)->nd_head;
14761 NODE *value = RNODE_LIST(hash)->nd_next;
14762 NODE *next = RNODE_LIST(value)->nd_next;
14763 st_data_t key;
14764 st_data_t data;
14765
14766 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14767 if (!head) {
14768 head = value;
14769 }
14770
14771 if (nd_type_st_key_enable_p(head)) {
14772 key = (st_data_t)head;
14773
14774 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14775 rb_warn2L(nd_line((NODE *)data),
14776 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14777 nd_value(p, head), WARN_I(nd_line(head)));
14778 }
14779 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14780 }
14781 hash = next;
14782 }
14783 st_free_table(p->warn_duplicate_keys_table);
14784 p->warn_duplicate_keys_table = NULL;
14785}
14786
14787static NODE *
14788new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14789{
14790 if (hash) warn_duplicate_keys(p, hash);
14791 return NEW_HASH(hash, loc);
14792}
14793
14794static void
14795error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14796{
14797 if (is_private_local_id(p, id)) {
14798 return;
14799 }
14800 if (st_is_member(p->pvtbl, id)) {
14801 yyerror1(loc, "duplicated variable name");
14802 }
14803 else if (p->ctxt.in_alt_pattern && id) {
14804 yyerror1(loc, "variable capture in alternative pattern");
14805 }
14806 else {
14807 p->ctxt.capture_in_pattern = 1;
14808 st_insert(p->pvtbl, (st_data_t)id, 0);
14809 }
14810}
14811
14812static void
14813error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14814{
14815 if (!p->pktbl) {
14816 p->pktbl = st_init_numtable();
14817 }
14818 else if (st_is_member(p->pktbl, key)) {
14819 yyerror1(loc, "duplicated key name");
14820 return;
14821 }
14822 st_insert(p->pktbl, (st_data_t)key, 0);
14823}
14824
14825static NODE *
14826new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14827{
14828 return NEW_HASH(hash, loc);
14829}
14830
14831static NODE *
14832new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14833{
14834 NODE *asgn;
14835
14836 if (lhs) {
14837 ID vid = get_nd_vid(p, lhs);
14838 YYLTYPE lhs_loc = lhs->nd_loc;
14839 /* A constant read can raise NameError. Prism has no separate read node
14840 * for `Const op= value` and reports the whole expression, so give the
14841 * NODE_CONST the location of the whole operator assignment as well,
14842 * for consistent Thread::Backtrace::Location#source_range between both
14843 * parsers. */
14844 const YYLTYPE *read_loc = nd_type_p(lhs, NODE_CDECL) ? loc : &lhs_loc;
14845 if (op == tOROP) {
14846 set_nd_value(p, lhs, rhs);
14847 nd_set_loc(lhs, loc);
14848 asgn = NEW_OP_ASGN_OR(gettable(p, vid, read_loc), lhs, loc);
14849 }
14850 else if (op == tANDOP) {
14851 set_nd_value(p, lhs, rhs);
14852 nd_set_loc(lhs, loc);
14853 asgn = NEW_OP_ASGN_AND(gettable(p, vid, read_loc), lhs, loc);
14854 }
14855 else {
14856 asgn = lhs;
14857 rhs = NEW_CALL(gettable(p, vid, read_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14858 set_nd_value(p, asgn, rhs);
14859 nd_set_loc(asgn, loc);
14860 }
14861 }
14862 else {
14863 asgn = NEW_ERROR(loc);
14864 }
14865 return asgn;
14866}
14867
14868static NODE *
14869new_ary_op_assign(struct parser_params *p, NODE *ary,
14870 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14871 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14872{
14873 NODE *asgn;
14874
14875 aryset_check(p, args);
14876 args = make_list(args, args_loc);
14877 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14878 fixpos(asgn, ary);
14879 return asgn;
14880}
14881
14882static NODE *
14883new_attr_op_assign(struct parser_params *p, NODE *lhs,
14884 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14885 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14886{
14887 NODE *asgn;
14888
14889 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14890 fixpos(asgn, lhs);
14891 return asgn;
14892}
14893
14894static NODE *
14895new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14896{
14897 NODE *asgn;
14898
14899 if (lhs) {
14900 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14901 }
14902 else {
14903 asgn = NEW_ERROR(loc);
14904 }
14905 fixpos(asgn, lhs);
14906 return asgn;
14907}
14908
14909static NODE *
14910const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14911{
14912 if (p->ctxt.in_def) {
14913#ifndef RIPPER
14914 yyerror1(loc, "dynamic constant assignment");
14915#else
14916 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14917#endif
14918 }
14919 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14920}
14921
14922#ifdef RIPPER
14923static VALUE
14924assign_error(struct parser_params *p, const char *mesg, VALUE a)
14925{
14926 a = dispatch2(assign_error, ERR_MESG(), a);
14927 ripper_error(p);
14928 return a;
14929}
14930#endif
14931
14932static NODE *
14933new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14934{
14935 NODE *result = head;
14936 if (rescue) {
14937 NODE *tmp = rescue_else ? rescue_else : rescue;
14938 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14939
14940 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14941 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14942 }
14943 if (ensure) {
14944 result = NEW_ENSURE(result, ensure, loc);
14945 }
14946 fixpos(result, head);
14947 return result;
14948}
14949
14950static void
14951warn_unused_var(struct parser_params *p, struct local_vars *local)
14952{
14953 int cnt;
14954
14955 if (!local->used) return;
14956 cnt = local->used->pos;
14957 if (cnt != local->vars->pos) {
14958 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14959 }
14960#ifndef RIPPER
14961 ID *v = local->vars->tbl;
14962 ID *u = local->used->tbl;
14963 for (int i = 0; i < cnt; ++i) {
14964 if (!v[i] || (u[i] & LVAR_USED)) continue;
14965 if (is_private_local_id(p, v[i])) continue;
14966 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14967 }
14968#endif
14969}
14970
14971static void
14972local_push(struct parser_params *p, int toplevel_scope)
14973{
14974 struct local_vars *local;
14975 int inherits_dvars = toplevel_scope && compile_for_eval;
14976 int warn_unused_vars = RTEST(ruby_verbose);
14977
14978 local = ALLOC(struct local_vars);
14979 local->prev = p->lvtbl;
14980 local->args = vtable_alloc(0);
14981 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14982#ifndef RIPPER
14983 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14984 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14985#endif
14986 local->numparam.outer = 0;
14987 local->numparam.inner = 0;
14988 local->numparam.current = 0;
14989 local->it = 0;
14990 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14991
14992# if WARN_PAST_SCOPE
14993 local->past = 0;
14994# endif
14995 CMDARG_PUSH(0);
14996 COND_PUSH(0);
14997 p->lvtbl = local;
14998}
14999
15000static void
15001vtable_chain_free(struct parser_params *p, struct vtable *table)
15002{
15003 while (!DVARS_TERMINAL_P(table)) {
15004 struct vtable *cur_table = table;
15005 table = cur_table->prev;
15006 vtable_free(cur_table);
15007 }
15008}
15009
15010static void
15011local_free(struct parser_params *p, struct local_vars *local)
15012{
15013 vtable_chain_free(p, local->used);
15014
15015# if WARN_PAST_SCOPE
15016 vtable_chain_free(p, local->past);
15017# endif
15018
15019 vtable_chain_free(p, local->args);
15020 vtable_chain_free(p, local->vars);
15021
15022 ruby_xfree_sized(local, sizeof(struct local_vars));
15023}
15024
15025static void
15026local_pop(struct parser_params *p)
15027{
15028 struct local_vars *local = p->lvtbl->prev;
15029 if (p->lvtbl->used) {
15030 warn_unused_var(p, p->lvtbl);
15031 }
15032
15033 local_free(p, p->lvtbl);
15034 p->lvtbl = local;
15035
15036 CMDARG_POP();
15037 COND_POP();
15038}
15039
15040static rb_ast_id_table_t *
15041local_tbl(struct parser_params *p)
15042{
15043 int cnt_args = vtable_size(p->lvtbl->args);
15044 int cnt_vars = vtable_size(p->lvtbl->vars);
15045 int cnt = cnt_args + cnt_vars;
15046 int i, j;
15047 rb_ast_id_table_t *tbl;
15048
15049 if (cnt <= 0) return 0;
15050 tbl = rb_ast_new_local_table(p->ast, cnt);
15051 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
15052 /* remove IDs duplicated to warn shadowing */
15053 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15054 ID id = p->lvtbl->vars->tbl[i];
15055 if (!vtable_included(p->lvtbl->args, id)) {
15056 tbl->ids[j++] = id;
15057 }
15058 }
15059 if (j < cnt) {
15060 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15061 }
15062
15063 return tbl;
15064}
15065
15066static void
15067numparam_name(struct parser_params *p, ID id)
15068{
15069 if (!NUMPARAM_ID_P(id)) return;
15070 compile_error(p, "_%d is reserved for numbered parameter",
15071 NUMPARAM_ID_TO_IDX(id));
15072}
15073
15074static void
15075arg_var(struct parser_params *p, ID id)
15076{
15077 numparam_name(p, id);
15078 vtable_add(p->lvtbl->args, id);
15079}
15080
15081static void
15082local_var(struct parser_params *p, ID id)
15083{
15084 numparam_name(p, id);
15085 vtable_add(p->lvtbl->vars, id);
15086 if (p->lvtbl->used) {
15087 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15088 }
15089}
15090
15091#ifndef RIPPER
15092int
15093rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15094{
15095 return rb_local_defined(id, iseq);
15096}
15097#endif
15098
15099static int
15100local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15101{
15102 struct vtable *vars, *args, *used;
15103
15104 vars = p->lvtbl->vars;
15105 args = p->lvtbl->args;
15106 used = p->lvtbl->used;
15107
15108 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15109 vars = vars->prev;
15110 args = args->prev;
15111 if (used) used = used->prev;
15112 }
15113
15114 if (vars && vars->prev == DVARS_INHERIT) {
15115 return rb_parser_local_defined(p, id, p->parent_iseq);
15116 }
15117 else if (vtable_included(args, id)) {
15118 return 1;
15119 }
15120 else {
15121 int i = vtable_included(vars, id);
15122 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15123 return i != 0;
15124 }
15125}
15126
15127static int
15128local_id(struct parser_params *p, ID id)
15129{
15130 return local_id_ref(p, id, NULL);
15131}
15132
15133static int
15134check_forwarding_args(struct parser_params *p)
15135{
15136 if (local_id(p, idFWD_ALL)) return TRUE;
15137 compile_error(p, "unexpected ...");
15138 return FALSE;
15139}
15140
15141static void
15142add_forwarding_args(struct parser_params *p)
15143{
15144 arg_var(p, idFWD_REST);
15145 arg_var(p, idFWD_KWREST);
15146 arg_var(p, idFWD_BLOCK);
15147 arg_var(p, idFWD_ALL);
15148}
15149
15150static void
15151forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15152{
15153 bool conflict = false;
15154
15155 struct vtable *vars, *args;
15156
15157 vars = p->lvtbl->vars;
15158 args = p->lvtbl->args;
15159
15160 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15161 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15162 vars = vars->prev;
15163 args = args->prev;
15164 }
15165
15166 bool found = false;
15167 if (vars && vars->prev == DVARS_INHERIT && !found) {
15168 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15169 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15170 }
15171 else {
15172 found = (vtable_included(args, arg) &&
15173 !(all && vtable_included(args, all)));
15174 }
15175
15176 if (!found) {
15177 compile_error(p, "no anonymous %s parameter", var);
15178 }
15179 else if (conflict) {
15180 compile_error(p, "anonymous %s parameter is also used within block", var);
15181 }
15182}
15183
15184static NODE *
15185new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15186{
15187 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15188 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15189 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15190 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15191 block->forwarding = TRUE;
15192 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15193 return arg_blk_pass(args, block);
15194}
15195
15196static NODE *
15197numparam_push(struct parser_params *p)
15198{
15199 struct local_vars *local = p->lvtbl;
15200 NODE *inner = local->numparam.inner;
15201 if (!local->numparam.outer) {
15202 local->numparam.outer = local->numparam.current;
15203 }
15204 local->numparam.inner = 0;
15205 local->numparam.current = 0;
15206 local->it = 0;
15207 return inner;
15208}
15209
15210static void
15211numparam_pop(struct parser_params *p, NODE *prev_inner)
15212{
15213 struct local_vars *local = p->lvtbl;
15214 if (prev_inner) {
15215 /* prefer first one */
15216 local->numparam.inner = prev_inner;
15217 }
15218 else if (local->numparam.current) {
15219 /* current and inner are exclusive */
15220 local->numparam.inner = local->numparam.current;
15221 }
15222 if (p->max_numparam > NO_PARAM) {
15223 /* current and outer are exclusive */
15224 local->numparam.current = local->numparam.outer;
15225 local->numparam.outer = 0;
15226 }
15227 else {
15228 /* no numbered parameter */
15229 local->numparam.current = 0;
15230 }
15231 local->it = 0;
15232}
15233
15234static const struct vtable *
15235dyna_push(struct parser_params *p)
15236{
15237 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15238 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15239 if (p->lvtbl->used) {
15240 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15241 }
15242 return p->lvtbl->args;
15243}
15244
15245static void
15246dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15247{
15248 struct vtable *tmp = *vtblp;
15249 *vtblp = tmp->prev;
15250# if WARN_PAST_SCOPE
15251 if (p->past_scope_enabled) {
15252 tmp->prev = p->lvtbl->past;
15253 p->lvtbl->past = tmp;
15254 return;
15255 }
15256# endif
15257 vtable_free(tmp);
15258}
15259
15260static void
15261dyna_pop_1(struct parser_params *p)
15262{
15263 struct vtable *tmp;
15264
15265 if ((tmp = p->lvtbl->used) != 0) {
15266 warn_unused_var(p, p->lvtbl);
15267 p->lvtbl->used = p->lvtbl->used->prev;
15268 vtable_free(tmp);
15269 }
15270 dyna_pop_vtable(p, &p->lvtbl->args);
15271 dyna_pop_vtable(p, &p->lvtbl->vars);
15272}
15273
15274static void
15275dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15276{
15277 while (p->lvtbl->args != lvargs) {
15278 dyna_pop_1(p);
15279 if (!p->lvtbl->args) {
15280 struct local_vars *local = p->lvtbl->prev;
15281 ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl));
15282 p->lvtbl = local;
15283 }
15284 }
15285 dyna_pop_1(p);
15286}
15287
15288static int
15289dyna_in_block(struct parser_params *p)
15290{
15291 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15292}
15293
15294#ifndef RIPPER
15295int
15296dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15297{
15298 struct vtable *vars, *args, *used;
15299 int i;
15300
15301 args = p->lvtbl->args;
15302 vars = p->lvtbl->vars;
15303 used = p->lvtbl->used;
15304
15305 while (!DVARS_TERMINAL_P(vars)) {
15306 if (vtable_included(args, id)) {
15307 return 1;
15308 }
15309 if ((i = vtable_included(vars, id)) != 0) {
15310 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15311 return 1;
15312 }
15313 args = args->prev;
15314 vars = vars->prev;
15315 if (!vidrefp) used = 0;
15316 if (used) used = used->prev;
15317 }
15318
15319 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15320 return rb_dvar_defined(id, p->parent_iseq);
15321 }
15322
15323 return 0;
15324}
15325#endif
15326
15327static int
15328dvar_defined(struct parser_params *p, ID id)
15329{
15330 return dvar_defined_ref(p, id, NULL);
15331}
15332
15333static int
15334dvar_curr(struct parser_params *p, ID id)
15335{
15336 return (vtable_included(p->lvtbl->args, id) ||
15337 vtable_included(p->lvtbl->vars, id));
15338}
15339
15340static void
15341reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15342{
15343 compile_error(p,
15344 "regexp encoding option '%c' differs from source encoding '%s'",
15345 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15346}
15347
15348#ifndef RIPPER
15349static rb_encoding *
15350find_enc(struct parser_params* p, const char *name)
15351{
15352 int idx = rb_enc_find_index(name);
15353 if (idx < 0) {
15354 rb_bug("unknown encoding name: %s", name);
15355 }
15356
15357 return rb_enc_from_index(idx);
15358}
15359
15360static rb_encoding *
15361kcode_to_enc(struct parser_params* p, int kcode)
15362{
15363 rb_encoding *enc;
15364
15365 switch (kcode) {
15366 case ENC_ASCII8BIT:
15367 enc = rb_ascii8bit_encoding();
15368 break;
15369 case ENC_EUC_JP:
15370 enc = find_enc(p, "EUC-JP");
15371 break;
15372 case ENC_Windows_31J:
15373 enc = find_enc(p, "Windows-31J");
15374 break;
15375 case ENC_UTF8:
15376 enc = rb_utf8_encoding();
15377 break;
15378 default:
15379 enc = NULL;
15380 break;
15381 }
15382
15383 return enc;
15384}
15385
15386int
15387rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15388{
15389 int c = RE_OPTION_ENCODING_IDX(options);
15390
15391 if (c) {
15392 int opt, idx;
15393 rb_encoding *enc;
15394
15395 char_to_option_kcode(c, &opt, &idx);
15396 enc = kcode_to_enc(p, idx);
15397 if (enc != rb_parser_str_get_encoding(str) &&
15398 !rb_parser_is_ascii_string(p, str)) {
15399 goto error;
15400 }
15401 rb_parser_string_set_encoding(str, enc);
15402 }
15403 else if (RE_OPTION_ENCODING_NONE(options)) {
15404 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15405 !rb_parser_is_ascii_string(p, str)) {
15406 c = 'n';
15407 goto error;
15408 }
15409 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15410 }
15411 else if (rb_is_usascii_enc(p->enc)) {
15412 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15413 }
15414 return 0;
15415
15416 error:
15417 return c;
15418}
15419#endif
15420
15421static void
15422reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15423{
15424 int c = rb_reg_fragment_setenc(p, str, options);
15425 if (c) reg_fragment_enc_error(p, str, c);
15426}
15427
15428#ifndef UNIVERSAL_PARSER
15429typedef struct {
15430 struct parser_params* parser;
15431 rb_encoding *enc;
15432 NODE *succ_block;
15433 const YYLTYPE *loc;
15434 rb_parser_assignable_func assignable;
15435} reg_named_capture_assign_t;
15436
15437static int
15438reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15439 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15440{
15441 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15442 struct parser_params* p = arg->parser;
15443 rb_encoding *enc = arg->enc;
15444 long len = name_end - name;
15445 const char *s = (const char *)name;
15446
15447 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15448}
15449
15450static NODE *
15451reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15452{
15453 reg_named_capture_assign_t arg;
15454
15455 arg.parser = p;
15456 arg.enc = rb_enc_get(regexp);
15457 arg.succ_block = 0;
15458 arg.loc = loc;
15459 arg.assignable = assignable;
15460 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15461
15462 if (!arg.succ_block) return 0;
15463 return RNODE_BLOCK(arg.succ_block)->nd_next;
15464}
15465#endif
15466
15467#ifndef RIPPER
15468NODE *
15469rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15470{
15471 return assignable(p, id, val, loc);
15472}
15473
15474int
15475rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15476 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15477{
15478 ID var;
15479 NODE *node, *succ;
15480
15481 if (!len) return ST_CONTINUE;
15482 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15483 return ST_CONTINUE;
15484
15485 var = intern_cstr(s, len, enc);
15486 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15487 if (!lvar_defined(p, var)) return ST_CONTINUE;
15488 }
15489 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15490 succ = *succ_block;
15491 if (!succ) succ = NEW_ERROR(loc);
15492 succ = block_append(p, succ, node);
15493 *succ_block = succ;
15494 return ST_CONTINUE;
15495}
15496#endif
15497
15498static VALUE
15499parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15500{
15501 VALUE str2;
15502 reg_fragment_setenc(p, str, options);
15503 str2 = rb_str_new_parser_string(str);
15504 return rb_parser_reg_compile(p, str2, options);
15505}
15506
15507#ifndef RIPPER
15508VALUE
15509rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15510{
15511 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15512}
15513#endif
15514
15515static VALUE
15516reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15517{
15518 VALUE re;
15519 VALUE err;
15520
15521 err = rb_errinfo();
15522 re = parser_reg_compile(p, str, options);
15523 if (NIL_P(re)) {
15524 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15525 rb_set_errinfo(err);
15526 compile_error(p, "%"PRIsVALUE, m);
15527 return Qnil;
15528 }
15529 return re;
15530}
15531
15532#ifndef RIPPER
15533void
15534rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15535{
15536 p->do_print = print;
15537 p->do_loop = loop;
15538 p->do_chomp = chomp;
15539 p->do_split = split;
15540}
15541
15542static NODE *
15543parser_append_options(struct parser_params *p, NODE *node)
15544{
15545 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15546 const YYLTYPE *const LOC = &default_location;
15547
15548 if (p->do_print) {
15549 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15550 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15551 LOC);
15552 node = block_append(p, node, print);
15553 }
15554
15555 if (p->do_loop) {
15556 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15557
15558 if (p->do_split) {
15559 ID ifs = rb_intern("$;");
15560 ID fields = rb_intern("$F");
15561 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15562 NODE *split = NEW_GASGN(fields,
15563 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15564 rb_intern("split"), args, LOC),
15565 LOC);
15566 node = block_append(p, split, node);
15567 }
15568 if (p->do_chomp) {
15569 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15570 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15571 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15572 }
15573
15574 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15575 }
15576
15577 return node;
15578}
15579
15580void
15581rb_init_parse(void)
15582{
15583 /* just to suppress unused-function warnings */
15584 (void)nodetype;
15585 (void)nodeline;
15586}
15587
15588ID
15589internal_id(struct parser_params *p)
15590{
15591 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15592}
15593#endif /* !RIPPER */
15594
15595static void
15596parser_initialize(struct parser_params *p)
15597{
15598 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15599 p->command_start = TRUE;
15600 p->ruby_sourcefile_string = Qnil;
15601 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15602 string_buffer_init(p);
15603 p->node_id = 0;
15604 p->delayed.token = NULL;
15605 p->frozen_string_literal = -1; /* not specified */
15606 rb_source_hash_init(&p->source_hash);
15607#ifndef RIPPER
15608 p->error_buffer = Qfalse;
15609 p->end_expect_token_locations = NULL;
15610 p->token_id = 0;
15611 p->tokens = NULL;
15612#else
15613 p->result = Qnil;
15614 p->parsing_thread = Qnil;
15615 p->s_value = Qnil;
15616 p->s_lvalue = Qnil;
15617 p->s_value_stack = rb_ary_new();
15618#endif
15619 p->debug_buffer = Qnil;
15620 p->debug_output = rb_ractor_stdout();
15621 p->enc = rb_utf8_encoding();
15622 p->exits = 0;
15623}
15624
15625#ifdef RIPPER
15626#define rb_ruby_parser_mark ripper_parser_mark
15627#define rb_ruby_parser_free ripper_parser_free
15628#define rb_ruby_parser_memsize ripper_parser_memsize
15629#endif
15630
15631void
15632rb_ruby_parser_mark(void *ptr)
15633{
15634 struct parser_params *p = (struct parser_params*)ptr;
15635
15636 rb_gc_mark(p->ruby_sourcefile_string);
15637#ifndef RIPPER
15638 rb_gc_mark(p->error_buffer);
15639#else
15640 rb_gc_mark(p->value);
15641 rb_gc_mark(p->result);
15642 rb_gc_mark(p->parsing_thread);
15643 rb_gc_mark(p->s_value);
15644 rb_gc_mark(p->s_lvalue);
15645 rb_gc_mark(p->s_value_stack);
15646#endif
15647 rb_gc_mark(p->debug_buffer);
15648 rb_gc_mark(p->debug_output);
15649}
15650
15651void
15652rb_ruby_parser_free(void *ptr)
15653{
15654 struct parser_params *p = (struct parser_params*)ptr;
15655 struct local_vars *local, *prev;
15656
15657 if (p->ast) {
15658 rb_ast_free(p->ast);
15659 }
15660
15661 if (p->warn_duplicate_keys_table) {
15662 st_free_table(p->warn_duplicate_keys_table);
15663 }
15664
15665#ifndef RIPPER
15666 if (p->tokens) {
15667 rb_parser_ary_free(p, p->tokens);
15668 }
15669#endif
15670
15671 if (p->tokenbuf) {
15672 ruby_xfree_sized(p->tokenbuf, p->toksiz);
15673 }
15674
15675 for (local = p->lvtbl; local; local = prev) {
15676 prev = local->prev;
15677 local_free(p, local);
15678 }
15679
15680 {
15681 token_info *ptinfo;
15682 while ((ptinfo = p->token_info) != 0) {
15683 p->token_info = ptinfo->next;
15684 xfree(ptinfo);
15685 }
15686 }
15687 string_buffer_free(p);
15688
15689 if (p->pvtbl) {
15690 st_free_table(p->pvtbl);
15691 }
15692
15693 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15694 st_free_table(p->case_labels);
15695 }
15696
15697 xfree(p->lex.strterm);
15698 p->lex.strterm = 0;
15699
15700 xfree(ptr);
15701}
15702
15703size_t
15704rb_ruby_parser_memsize(const void *ptr)
15705{
15706 struct parser_params *p = (struct parser_params*)ptr;
15707 struct local_vars *local;
15708 size_t size = sizeof(*p);
15709
15710 size += p->toksiz;
15711 for (local = p->lvtbl; local; local = local->prev) {
15712 size += sizeof(*local);
15713 if (local->vars) size += local->vars->capa * sizeof(ID);
15714 }
15715 return size;
15716}
15717
15718#ifndef RIPPER
15719#undef rb_reserved_word
15720
15721const struct kwtable *
15722rb_reserved_word(const char *str, unsigned int len)
15723{
15724 return reserved_word(str, len);
15725}
15726
15727#ifdef UNIVERSAL_PARSER
15728rb_parser_t *
15729rb_ruby_parser_allocate(const rb_parser_config_t *config)
15730{
15731 /* parser_initialize expects fields to be set to 0 */
15732 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15733 p->config = config;
15734 return p;
15735}
15736
15737rb_parser_t *
15738rb_ruby_parser_new(const rb_parser_config_t *config)
15739{
15740 /* parser_initialize expects fields to be set to 0 */
15741 rb_parser_t *p = rb_ruby_parser_allocate(config);
15742 parser_initialize(p);
15743 return p;
15744}
15745#else
15746rb_parser_t *
15747rb_ruby_parser_allocate(void)
15748{
15749 /* parser_initialize expects fields to be set to 0 */
15750 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15751 return p;
15752}
15753
15754rb_parser_t *
15755rb_ruby_parser_new(void)
15756{
15757 /* parser_initialize expects fields to be set to 0 */
15758 rb_parser_t *p = rb_ruby_parser_allocate();
15759 parser_initialize(p);
15760 return p;
15761}
15762#endif
15763
15764rb_parser_t *
15765rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15766{
15767 p->error_buffer = main ? Qfalse : Qnil;
15768 p->parent_iseq = base;
15769 return p;
15770}
15771
15772void
15773rb_ruby_parser_set_script_lines(rb_parser_t *p)
15774{
15775 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15776}
15777
15778void
15779rb_ruby_parser_error_tolerant(rb_parser_t *p)
15780{
15781 p->error_tolerant = 1;
15782}
15783
15784void
15785rb_ruby_parser_keep_tokens(rb_parser_t *p)
15786{
15787 p->keep_tokens = 1;
15788 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15789}
15790
15791rb_encoding *
15792rb_ruby_parser_encoding(rb_parser_t *p)
15793{
15794 return p->enc;
15795}
15796
15797int
15798rb_ruby_parser_end_seen_p(rb_parser_t *p)
15799{
15800 return p->ruby__end__seen;
15801}
15802
15803int
15804rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15805{
15806 p->debug = flag;
15807 return flag;
15808}
15809#endif /* !RIPPER */
15810
15811#ifdef RIPPER
15812int
15813rb_ruby_parser_get_yydebug(rb_parser_t *p)
15814{
15815 return p->debug;
15816}
15817
15818void
15819rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15820{
15821 p->value = value;
15822}
15823
15824int
15825rb_ruby_parser_error_p(rb_parser_t *p)
15826{
15827 return p->error_p;
15828}
15829
15830VALUE
15831rb_ruby_parser_debug_output(rb_parser_t *p)
15832{
15833 return p->debug_output;
15834}
15835
15836void
15837rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15838{
15839 p->debug_output = output;
15840}
15841
15842VALUE
15843rb_ruby_parser_parsing_thread(rb_parser_t *p)
15844{
15845 return p->parsing_thread;
15846}
15847
15848void
15849rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15850{
15851 p->parsing_thread = parsing_thread;
15852}
15853
15854void
15855rb_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)
15856{
15857 p->lex.gets = gets;
15858 p->lex.input = input;
15859 p->eofp = 0;
15860 p->ruby_sourcefile_string = sourcefile_string;
15861 p->ruby_sourcefile = sourcefile;
15862 p->ruby_sourceline = sourceline;
15863}
15864
15865VALUE
15866rb_ruby_parser_result(rb_parser_t *p)
15867{
15868 return p->result;
15869}
15870
15871rb_encoding *
15872rb_ruby_parser_enc(rb_parser_t *p)
15873{
15874 return p->enc;
15875}
15876
15877VALUE
15878rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15879{
15880 return p->ruby_sourcefile_string;
15881}
15882
15883int
15884rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15885{
15886 return p->ruby_sourceline;
15887}
15888
15889int
15890rb_ruby_parser_lex_state(rb_parser_t *p)
15891{
15892 return p->lex.state;
15893}
15894
15895void
15896rb_ruby_ripper_parse0(rb_parser_t *p)
15897{
15898 parser_prepare(p);
15899 p->ast = rb_ast_new();
15900 ripper_yyparse((void*)p);
15901 rb_ast_free(p->ast);
15902 p->ast = 0;
15903 p->eval_tree = 0;
15904 p->eval_tree_begin = 0;
15905}
15906
15907int
15908rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15909{
15910 return dedent_string(p, string, width);
15911}
15912
15913int
15914rb_ruby_ripper_initialized_p(rb_parser_t *p)
15915{
15916 return p->lex.input != 0;
15917}
15918
15919void
15920rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15921{
15922 parser_initialize(p);
15923}
15924
15925long
15926rb_ruby_ripper_column(rb_parser_t *p)
15927{
15928 return p->lex.ptok - p->lex.pbeg;
15929}
15930
15931long
15932rb_ruby_ripper_token_len(rb_parser_t *p)
15933{
15934 return p->lex.pcur - p->lex.ptok;
15935}
15936
15937rb_parser_string_t *
15938rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15939{
15940 return p->lex.lastline;
15941}
15942
15943VALUE
15944rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15945{
15946 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15947}
15948
15949#ifdef UNIVERSAL_PARSER
15950rb_parser_t *
15951rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15952{
15953 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15954 p->config = config;
15955 return p;
15956}
15957#endif
15958
15959struct parser_params*
15960rb_ruby_ripper_parser_allocate(void)
15961{
15962 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15963}
15964#endif /* RIPPER */
15965
15966#ifndef RIPPER
15967void
15968rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15969{
15970 va_list ap;
15971 VALUE mesg = p->debug_buffer;
15972
15973 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15974 va_start(ap, fmt);
15975 rb_str_vcatf(mesg, fmt, ap);
15976 va_end(ap);
15977 if (char_at_end(p, mesg, 0) == '\n') {
15978 rb_io_write(p->debug_output, mesg);
15979 p->debug_buffer = Qnil;
15980 }
15981}
15982
15983static void
15984parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15985{
15986 va_list ap;
15987 int lineno, column;
15988
15989 if (loc) {
15990 lineno = loc->end_pos.lineno;
15991 column = loc->end_pos.column;
15992 }
15993 else {
15994 lineno = p->ruby_sourceline;
15995 column = rb_len2int(p->lex.pcur - p->lex.pbeg);
15996 }
15997
15998 rb_io_flush(p->debug_output);
15999 p->error_p = 1;
16000 va_start(ap, fmt);
16001 p->error_buffer =
16002 rb_syntax_error_append(p->error_buffer,
16003 p->ruby_sourcefile_string,
16004 lineno, column,
16005 p->enc, fmt, ap);
16006 va_end(ap);
16007}
16008
16009static size_t
16010count_char(const char *str, int c)
16011{
16012 int n = 0;
16013 while (str[n] == c) ++n;
16014 return n;
16015}
16016
16017/*
16018 * strip enclosing double-quotes, same as the default yytnamerr except
16019 * for that single-quotes matching back-quotes do not stop stripping.
16020 *
16021 * "\"`class' keyword\"" => "`class' keyword"
16022 */
16023size_t
16024rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16025{
16026 if (*yystr == '"') {
16027 size_t yyn = 0, bquote = 0;
16028 const char *yyp = yystr;
16029
16030 while (*++yyp) {
16031 switch (*yyp) {
16032 case '\'':
16033 if (!bquote) {
16034 bquote = count_char(yyp+1, '\'') + 1;
16035 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16036 yyn += bquote;
16037 yyp += bquote - 1;
16038 break;
16039 }
16040 else {
16041 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16042 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16043 yyn += bquote;
16044 yyp += bquote - 1;
16045 bquote = 0;
16046 break;
16047 }
16048 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16049 if (yyres) memcpy(yyres + yyn, yyp, 3);
16050 yyn += 3;
16051 yyp += 2;
16052 break;
16053 }
16054 goto do_not_strip_quotes;
16055 }
16056
16057 case ',':
16058 goto do_not_strip_quotes;
16059
16060 case '\\':
16061 if (*++yyp != '\\')
16062 goto do_not_strip_quotes;
16063 /* Fall through. */
16064 default:
16065 if (yyres)
16066 yyres[yyn] = *yyp;
16067 yyn++;
16068 break;
16069
16070 case '"':
16071 case '\0':
16072 if (yyres)
16073 yyres[yyn] = '\0';
16074 return yyn;
16075 }
16076 }
16077 do_not_strip_quotes: ;
16078 }
16079
16080 if (!yyres) return strlen(yystr);
16081
16082 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16083}
16084#endif
16085
16086#ifdef RIPPER
16087#define validate(x) (void)(x)
16088
16089static VALUE
16090ripper_dispatch0(struct parser_params *p, ID mid)
16091{
16092 return rb_funcall(p->value, mid, 0);
16093}
16094
16095static VALUE
16096ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16097{
16098 validate(a);
16099 return rb_funcall(p->value, mid, 1, a);
16100}
16101
16102static VALUE
16103ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16104{
16105 validate(a);
16106 validate(b);
16107 return rb_funcall(p->value, mid, 2, a, b);
16108}
16109
16110static VALUE
16111ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16112{
16113 validate(a);
16114 validate(b);
16115 validate(c);
16116 return rb_funcall(p->value, mid, 3, a, b, c);
16117}
16118
16119static VALUE
16120ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16121{
16122 validate(a);
16123 validate(b);
16124 validate(c);
16125 validate(d);
16126 return rb_funcall(p->value, mid, 4, a, b, c, d);
16127}
16128
16129static VALUE
16130ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16131{
16132 validate(a);
16133 validate(b);
16134 validate(c);
16135 validate(d);
16136 validate(e);
16137 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16138}
16139
16140static VALUE
16141ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16142{
16143 validate(a);
16144 validate(b);
16145 validate(c);
16146 validate(d);
16147 validate(e);
16148 validate(f);
16149 validate(g);
16150 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16151}
16152
16153void
16154ripper_error(struct parser_params *p)
16155{
16156 p->error_p = TRUE;
16157}
16158
16159VALUE
16160ripper_value(struct parser_params *p)
16161{
16162 (void)yystpcpy; /* may not used in newer bison */
16163
16164 return p->value;
16165}
16166
16167#endif /* RIPPER */
16168/*
16169 * Local variables:
16170 * mode: c
16171 * c-file-style: "ruby"
16172 * End:
16173 */