Ruby 4.0.10p0 (2026-11-19 revision a704b7cd95a42cc0cf5f307137bf3aac0285b915)
parse.y
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%{
13
14#if !YYPURE
15# error needs pure parser
16#endif
17#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20
21/* For Ripper */
22#ifdef RUBY_EXTCONF_H
23# include RUBY_EXTCONF_H
24#endif
25
26#include "ruby/internal/config.h"
27
28#include <errno.h>
29
30#ifdef UNIVERSAL_PARSER
31
32#include "internal/ruby_parser.h"
33#include "parser_node.h"
34#include "universal_parser.c"
35
36#ifdef RIPPER
37#define STATIC_ID2SYM p->config->static_id2sym
38#endif
39
40#else
41
42#include "internal.h"
43#include "internal/compile.h"
44#include "internal/compilers.h"
45#include "internal/complex.h"
46#include "internal/encoding.h"
47#include "internal/error.h"
48#include "internal/hash.h"
49#include "internal/io.h"
50#include "internal/numeric.h"
51#include "internal/parse.h"
52#include "internal/rational.h"
53#include "internal/re.h"
54#include "internal/ruby_parser.h"
55#include "internal/symbol.h"
56#include "internal/thread.h"
57#include "internal/variable.h"
58#include "node.h"
59#include "parser_node.h"
60#include "probes.h"
61#include "regenc.h"
62#include "ruby/encoding.h"
63#include "ruby/regex.h"
64#include "ruby/ruby.h"
65#include "ruby/st.h"
66#include "ruby/util.h"
67#include "ruby/ractor.h"
68#include "symbol.h"
69
70#ifndef RIPPER
71static VALUE
72syntax_error_new(void)
73{
74 return rb_class_new_instance(0, 0, rb_eSyntaxError);
75}
76#endif
77
78static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
79
80#define compile_callback rb_suppress_tracing
81#endif /* !UNIVERSAL_PARSER */
82
83#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
84#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
85
86static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
87
88#ifndef RIPPER
89static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
90#endif
91
92static int
93node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
94{
95 return (n1->minus != n2->minus ||
96 n1->base != n2->base ||
97 strcmp(n1->val, n2->val));
98}
99
100static int
101node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
102{
103 return (n1->minus != n2->minus ||
104 strcmp(n1->val, n2->val));
105}
106
107static int
108node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
109{
110 return (n1->minus != n2->minus ||
111 n1->base != n2->base ||
112 n1->seen_point != n2->seen_point ||
113 strcmp(n1->val, n2->val));
114}
115
116static int
117node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
118{
119 return (n1->minus != n2->minus ||
120 n1->base != n2->base ||
121 n1->seen_point != n2->seen_point ||
122 n1->type != n2->type ||
123 strcmp(n1->val, n2->val));
124}
125
126static int
127rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
128{
129 return (n1->options != n2->options ||
130 rb_parser_string_hash_cmp(n1->string, n2->string));
131}
132
133static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
134static st_index_t rb_char_p_hash(const char *c);
135
136static int
137literal_cmp(st_data_t val, st_data_t lit)
138{
139 if (val == lit) return 0;
140
141 NODE *node_val = RNODE(val);
142 NODE *node_lit = RNODE(lit);
143 enum node_type type_val = nd_type(node_val);
144 enum node_type type_lit = nd_type(node_lit);
145
146 if (type_val != type_lit) {
147 return -1;
148 }
149
150 switch (type_lit) {
151 case NODE_INTEGER:
152 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
153 case NODE_FLOAT:
154 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
155 case NODE_RATIONAL:
156 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
157 case NODE_IMAGINARY:
158 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
159 case NODE_STR:
160 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
161 case NODE_SYM:
162 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
163 case NODE_REGX:
164 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
165 case NODE_LINE:
166 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
167 case NODE_FILE:
168 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
169 case NODE_ENCODING:
170 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
171 default:
172#ifdef UNIVERSAL_PARSER
173 abort();
174#else
175 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
176#endif
177 }
178}
179
180static st_index_t
181literal_hash(st_data_t a)
182{
183 NODE *node = (NODE *)a;
184 enum node_type type = nd_type(node);
185
186 switch (type) {
187 case NODE_INTEGER:
188 return rb_char_p_hash(RNODE_INTEGER(node)->val);
189 case NODE_FLOAT:
190 return rb_char_p_hash(RNODE_FLOAT(node)->val);
191 case NODE_RATIONAL:
192 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
193 case NODE_IMAGINARY:
194 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
195 case NODE_STR:
196 return rb_parser_str_hash(RNODE_STR(node)->string);
197 case NODE_SYM:
198 return rb_parser_str_hash(RNODE_SYM(node)->string);
199 case NODE_REGX:
200 return rb_parser_str_hash(RNODE_REGX(node)->string);
201 case NODE_LINE:
202 return (st_index_t)node->nd_loc.beg_pos.lineno;
203 case NODE_FILE:
204 return rb_parser_str_hash(RNODE_FILE(node)->path);
205 case NODE_ENCODING:
206 return (st_index_t)RNODE_ENCODING(node)->enc;
207 default:
208#ifdef UNIVERSAL_PARSER
209 abort();
210#else
211 rb_bug("unexpected node: %s", ruby_node_name(type));
212#endif
213 }
214}
215
216static inline int
217parse_isascii(int c)
218{
219 return '\0' <= c && c <= '\x7f';
220}
221
222#undef ISASCII
223#define ISASCII parse_isascii
224
225static inline int
226parse_isspace(int c)
227{
228 return c == ' ' || ('\t' <= c && c <= '\r');
229}
230
231#undef ISSPACE
232#define ISSPACE parse_isspace
233
234static inline int
235parse_iscntrl(int c)
236{
237 return ('\0' <= c && c < ' ') || c == '\x7f';
238}
239
240#undef ISCNTRL
241#define ISCNTRL(c) parse_iscntrl(c)
242
243static inline int
244parse_isupper(int c)
245{
246 return 'A' <= c && c <= 'Z';
247}
248
249static inline int
250parse_islower(int c)
251{
252 return 'a' <= c && c <= 'z';
253}
254
255static inline int
256parse_isalpha(int c)
257{
258 return parse_isupper(c) || parse_islower(c);
259}
260
261#undef ISALPHA
262#define ISALPHA(c) parse_isalpha(c)
263
264static inline int
265parse_isdigit(int c)
266{
267 return '0' <= c && c <= '9';
268}
269
270#undef ISDIGIT
271#define ISDIGIT(c) parse_isdigit(c)
272
273static inline int
274parse_isalnum(int c)
275{
276 return ISALPHA(c) || ISDIGIT(c);
277}
278
279#undef ISALNUM
280#define ISALNUM(c) parse_isalnum(c)
281
282static inline int
283parse_isxdigit(int c)
284{
285 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
286}
287
288#undef ISXDIGIT
289#define ISXDIGIT(c) parse_isxdigit(c)
290
291#include "parser_st.h"
292
293#undef STRCASECMP
294#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
295
296#undef STRNCASECMP
297#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
298
299#ifdef RIPPER
300#include "ripper_init.h"
301#endif
302
303enum rescue_context {
304 before_rescue,
305 after_rescue,
306 after_else,
307 after_ensure,
308};
309
310struct lex_context {
311 unsigned int in_defined: 1;
312 unsigned int in_kwarg: 1;
313 unsigned int in_argdef: 1;
314 unsigned int in_def: 1;
315 unsigned int in_class: 1;
316 unsigned int has_trailing_semicolon: 1;
317 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
318 BITFIELD(enum rescue_context, in_rescue, 2);
319 unsigned int cant_return: 1;
320 unsigned int in_alt_pattern: 1;
321 unsigned int capture_in_pattern: 1;
322};
323
324typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
325
326#if defined(__GNUC__) && !defined(__clang__)
327// Suppress "parameter passing for argument of type 'struct
328// lex_context' changed" notes. `struct lex_context` is file scope,
329// and has no ABI compatibility issue.
330RBIMPL_WARNING_PUSH()
331RBIMPL_WARNING_IGNORED(-Wpsabi)
332RBIMPL_WARNING_POP()
333// Not sure why effective even after popped.
334#endif
335
336#include "parse.h"
337
338#define NO_LEX_CTXT (struct lex_context){0}
339
340#ifndef WARN_PAST_SCOPE
341# define WARN_PAST_SCOPE 0
342#endif
343
344#define TAB_WIDTH 8
345
346#define yydebug (p->debug) /* disable the global variable definition */
347
348#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
349#define YY_LOCATION_PRINT(File, loc, p) \
350 rb_parser_printf(p, "%d.%d-%d.%d", \
351 (loc).beg_pos.lineno, (loc).beg_pos.column,\
352 (loc).end_pos.lineno, (loc).end_pos.column)
353#define YYLLOC_DEFAULT(Current, Rhs, N) \
354 do \
355 if (N) \
356 { \
357 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
358 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
359 } \
360 else \
361 { \
362 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
363 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
364 } \
365 while (0)
366#define YY_(Msgid) \
367 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
368 "nesting too deep" : (Msgid))
369
370#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
371 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
372#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
373 rb_parser_set_location_of_delayed_token(p, &(Current))
374#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
375 rb_parser_set_location_of_heredoc_end(p, &(Current))
376#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
377 rb_parser_set_location_of_dummy_end(p, &(Current))
378#define RUBY_SET_YYLLOC_OF_NONE(Current) \
379 rb_parser_set_location_of_none(p, &(Current))
380#define RUBY_SET_YYLLOC(Current) \
381 rb_parser_set_location(p, &(Current))
382#define RUBY_INIT_YYLLOC() \
383 { \
384 {p->ruby_sourceline, parser_column(p->lex.ptok - p->lex.pbeg)}, \
385 {p->ruby_sourceline, parser_column(p->lex.pcur - p->lex.pbeg)}, \
386 }
387
388/* Saturate instead of wrapping to a column before the line. */
389static inline int
390parser_column(long pos)
391{
392#if SIZEOF_INT < SIZEOF_LONG
393 if (pos > INT_MAX) return INT_MAX;
394#endif
395 return (int)pos;
396}
397
398#define IS_lex_state_for(x, ls) ((x) & (ls))
399#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
400#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
401#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
402
403# define SET_LEX_STATE(ls) \
404 parser_set_lex_state(p, ls, __LINE__)
405static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
406
407typedef VALUE stack_type;
408
409static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
410
411# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
412# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
413# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
414# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
415# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
416
417/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
418 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
419#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
420#define COND_POP() BITSTACK_POP(cond_stack)
421#define COND_P() BITSTACK_SET_P(cond_stack)
422#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
423
424/* A flag to identify keyword_do_block; "do" keyword after command_call.
425 Example: `foo 1, 2 do`. */
426#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
427#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
428#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
429#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
430
431struct vtable {
432 ID *tbl;
433 int pos;
434 int capa;
435 struct vtable *prev;
436};
437
438struct local_vars {
439 struct vtable *args;
440 struct vtable *vars;
441 struct vtable *used;
442# if WARN_PAST_SCOPE
443 struct vtable *past;
444# endif
445 struct local_vars *prev;
446 struct {
447 NODE *outer, *inner, *current;
448 } numparam;
449 NODE *it;
450};
451
452typedef struct rb_locations_lambda_body_t {
453 NODE *node;
454 YYLTYPE opening_loc;
455 YYLTYPE closing_loc;
456} rb_locations_lambda_body_t;
457
458enum {
459 ORDINAL_PARAM = -1,
460 NO_PARAM = 0,
461 NUMPARAM_MAX = 9,
462};
463
464#define DVARS_INHERIT ((void*)1)
465#define DVARS_TOPSCOPE NULL
466#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
467
468typedef struct token_info {
469 const char *token;
470 rb_code_position_t beg;
471 int indent;
472 int nonspc;
473 struct token_info *next;
474} token_info;
475
476typedef struct end_expect_token_locations {
477 const rb_code_position_t *pos;
478 struct end_expect_token_locations *prev;
479} end_expect_token_locations_t;
480
481typedef struct parser_string_buffer_elem {
482 struct parser_string_buffer_elem *next;
483 long len; /* Total length of allocated buf */
484 long used; /* Current usage of buf */
485 rb_parser_string_t *buf[FLEX_ARY_LEN];
486} parser_string_buffer_elem_t;
487
488typedef struct parser_string_buffer {
489 parser_string_buffer_elem_t *head;
490 parser_string_buffer_elem_t *last;
491} parser_string_buffer_t;
492
493#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
494
495/*
496 Structure of Lexer Buffer:
497
498 lex.pbeg lex.ptok lex.pcur lex.pend
499 | | | |
500 |------------+------------+------------|
501 |<---------->|
502 token
503*/
504struct parser_params {
505 YYSTYPE *lval;
506 YYLTYPE *yylloc;
507
508 struct {
509 rb_strterm_t *strterm;
510 rb_parser_lex_gets_func *gets;
511 rb_parser_input_data input;
512 parser_string_buffer_t string_buffer;
513 rb_parser_string_t *lastline;
514 rb_parser_string_t *nextline;
515 const char *pbeg;
516 const char *pcur;
517 const char *pend;
518 const char *ptok;
519 enum lex_state_e state;
520 /* track the nest level of any parens "()[]{}" */
521 int paren_nest;
522 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
523 int lpar_beg;
524 /* track the nest level of only braces "{}" */
525 int brace_nest;
526 } lex;
527 stack_type cond_stack;
528 stack_type cmdarg_stack;
529 int tokidx;
530 int toksiz;
531 int heredoc_end;
532 int heredoc_indent;
533 int heredoc_line_indent;
534 char *tokenbuf;
535 struct local_vars *lvtbl;
536 st_table *pvtbl;
537 st_table *pktbl;
538 int line_count;
539 int ruby_sourceline; /* current line no. */
540 const char *ruby_sourcefile; /* current source file */
541 VALUE ruby_sourcefile_string;
542 rb_encoding *enc;
543 token_info *token_info;
544 st_table *case_labels;
545 rb_node_exits_t *exits;
546
547 VALUE debug_buffer;
548 VALUE debug_output;
549
550 struct {
551 rb_parser_string_t *token;
552 int beg_line;
553 int beg_col;
554 int end_line;
555 int end_col;
556 } delayed;
557
558 rb_ast_t *ast;
559 int node_id;
560
561 st_table *warn_duplicate_keys_table;
562
563 int max_numparam;
564 ID it_id;
565
566 struct lex_context ctxt;
567
568 NODE *eval_tree_begin;
569 NODE *eval_tree;
570 const struct rb_iseq_struct *parent_iseq;
571
572#ifdef UNIVERSAL_PARSER
573 const rb_parser_config_t *config;
574#endif
575 /* compile_option */
576 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
577
578 unsigned int command_start:1;
579 unsigned int eofp: 1;
580 unsigned int ruby__end__seen: 1;
581 unsigned int debug: 1;
582 unsigned int has_shebang: 1;
583 unsigned int token_seen: 1;
584 unsigned int token_info_enabled: 1;
585# if WARN_PAST_SCOPE
586 unsigned int past_scope_enabled: 1;
587# endif
588 unsigned int error_p: 1;
589 unsigned int cr_seen: 1;
590
591#ifndef RIPPER
592 /* Ruby core only */
593
594 unsigned int do_print: 1;
595 unsigned int do_loop: 1;
596 unsigned int do_chomp: 1;
597 unsigned int do_split: 1;
598 unsigned int error_tolerant: 1;
599 unsigned int keep_tokens: 1;
600
601 VALUE error_buffer;
602 rb_parser_ary_t *debug_lines;
603 /*
604 * Store specific keyword locations to generate dummy end token.
605 * Refer to the tail of list element.
606 */
607 end_expect_token_locations_t *end_expect_token_locations;
608 /* id for terms */
609 int token_id;
610 /* Array for term tokens */
611 rb_parser_ary_t *tokens;
612#else
613 /* Ripper only */
614
615 VALUE value;
616 VALUE result;
617 VALUE parsing_thread;
618 VALUE s_value; /* Token VALUE */
619 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
620 VALUE s_value_stack;
621#endif
622};
623
624#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
625#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
626#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
627static int
628numparam_id_p(struct parser_params *p, ID id)
629{
630 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
631 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
632 return idx > 0 && idx <= NUMPARAM_MAX;
633}
634static void numparam_name(struct parser_params *p, ID id);
635
636#ifdef RIPPER
637static void
638after_shift(struct parser_params *p)
639{
640 if (p->debug) {
641 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
642 }
643 rb_ary_push(p->s_value_stack, p->s_value);
644 p->s_value = Qnil;
645}
646
647static void
648before_reduce(int len, struct parser_params *p)
649{
650 // Initialize $$ with $1.
651 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
652}
653
654static void
655after_reduce(int len, struct parser_params *p)
656{
657 for (int i = 0; i < len; i++) {
658 VALUE tos = rb_ary_pop(p->s_value_stack);
659 if (p->debug) {
660 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
661 }
662 }
663 if (p->debug) {
664 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
665 }
666 rb_ary_push(p->s_value_stack, p->s_lvalue);
667 p->s_lvalue = Qnil;
668}
669
670static void
671after_shift_error_token(struct parser_params *p)
672{
673 if (p->debug) {
674 rb_parser_printf(p, "after-shift-error-token:\n");
675 }
676 rb_ary_push(p->s_value_stack, Qnil);
677}
678
679static void
680after_pop_stack(int len, struct parser_params *p)
681{
682 for (int i = 0; i < len; i++) {
683 VALUE tos = rb_ary_pop(p->s_value_stack);
684 if (p->debug) {
685 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
686 }
687 }
688}
689#else
690static void
691after_shift(struct parser_params *p)
692{
693}
694
695static void
696before_reduce(int len, struct parser_params *p)
697{
698}
699
700static void
701after_reduce(int len, struct parser_params *p)
702{
703}
704
705static void
706after_shift_error_token(struct parser_params *p)
707{
708}
709
710static void
711after_pop_stack(int len, struct parser_params *p)
712{
713}
714#endif
715
716#define intern_cstr(n,l,en) rb_intern3(n,l,en)
717
718#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
719
720#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
721#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
722#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
723#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
724#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
725#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
726
727#ifndef RIPPER
728static inline int
729char_at_end(struct parser_params *p, VALUE str, int when_empty)
730{
731 long len = RSTRING_LEN(str);
732 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
733}
734#endif
735
736static void
737pop_pvtbl(struct parser_params *p, st_table *tbl)
738{
739 st_free_table(p->pvtbl);
740 p->pvtbl = tbl;
741}
742
743static void
744pop_pktbl(struct parser_params *p, st_table *tbl)
745{
746 if (p->pktbl) st_free_table(p->pktbl);
747 p->pktbl = tbl;
748}
749
750#define STRING_BUF_DEFAULT_LEN 16
751
752static void
753string_buffer_init(struct parser_params *p)
754{
755 parser_string_buffer_t *buf = &p->lex.string_buffer;
756 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
757
758 buf->head = buf->last = xmalloc(size);
759 buf->head->len = STRING_BUF_DEFAULT_LEN;
760 buf->head->used = 0;
761 buf->head->next = NULL;
762}
763
764static void
765string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
766{
767 parser_string_buffer_t *buf = &p->lex.string_buffer;
768
769 if (buf->head->used >= buf->head->len) {
770 parser_string_buffer_elem_t *elem;
771 long n = buf->head->len * 2;
772 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
773
774 elem = xmalloc(size);
775 elem->len = n;
776 elem->used = 0;
777 elem->next = NULL;
778 buf->last->next = elem;
779 buf->last = elem;
780 }
781 buf->last->buf[buf->last->used++] = str;
782}
783
784static void
785string_buffer_free(struct parser_params *p)
786{
787 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
788
789 while (elem) {
790 parser_string_buffer_elem_t *next_elem = elem->next;
791
792 for (long i = 0; i < elem->used; i++) {
793 rb_parser_string_free(p, elem->buf[i]);
794 }
795
796 xfree(elem);
797 elem = next_elem;
798 }
799}
800
801#ifndef RIPPER
802static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
803
804static void
805debug_end_expect_token_locations(struct parser_params *p, const char *name)
806{
807 if(p->debug) {
808 VALUE mesg = rb_sprintf("%s: [", name);
809 int i = 0;
810 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
811 if (i > 0)
812 rb_str_cat_cstr(mesg, ", ");
813 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
814 i++;
815 }
816 rb_str_cat_cstr(mesg, "]\n");
817 flush_debug_buffer(p, p->debug_output, mesg);
818 }
819}
820
821static void
822push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
823{
824 if(!p->error_tolerant) return;
825
826 end_expect_token_locations_t *locations;
827 locations = ALLOC(end_expect_token_locations_t);
828 locations->pos = pos;
829 locations->prev = p->end_expect_token_locations;
830 p->end_expect_token_locations = locations;
831
832 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
833}
834
835static void
836pop_end_expect_token_locations(struct parser_params *p)
837{
838 if(!p->end_expect_token_locations) return;
839
840 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
841 ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
842 p->end_expect_token_locations = locations;
843
844 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
845}
846
847static end_expect_token_locations_t *
848peek_end_expect_token_locations(struct parser_params *p)
849{
850 return p->end_expect_token_locations;
851}
852
853static const char *
854parser_token2char(struct parser_params *p, enum yytokentype tok)
855{
856 switch ((int) tok) {
857#define TOKEN2CHAR(tok) case tok: return (#tok);
858#define TOKEN2CHAR2(tok, name) case tok: return (name);
859 TOKEN2CHAR2(' ', "word_sep");
860 TOKEN2CHAR2('!', "!")
861 TOKEN2CHAR2('%', "%");
862 TOKEN2CHAR2('&', "&");
863 TOKEN2CHAR2('*', "*");
864 TOKEN2CHAR2('+', "+");
865 TOKEN2CHAR2('-', "-");
866 TOKEN2CHAR2('/', "/");
867 TOKEN2CHAR2('<', "<");
868 TOKEN2CHAR2('=', "=");
869 TOKEN2CHAR2('>', ">");
870 TOKEN2CHAR2('?', "?");
871 TOKEN2CHAR2('^', "^");
872 TOKEN2CHAR2('|', "|");
873 TOKEN2CHAR2('~', "~");
874 TOKEN2CHAR2(':', ":");
875 TOKEN2CHAR2(',', ",");
876 TOKEN2CHAR2('.', ".");
877 TOKEN2CHAR2(';', ";");
878 TOKEN2CHAR2('`', "`");
879 TOKEN2CHAR2('\n', "nl");
880 TOKEN2CHAR2('{', "\"{\"");
881 TOKEN2CHAR2('}', "\"}\"");
882 TOKEN2CHAR2('[', "\"[\"");
883 TOKEN2CHAR2(']', "\"]\"");
884 TOKEN2CHAR2('(', "\"(\"");
885 TOKEN2CHAR2(')', "\")\"");
886 TOKEN2CHAR2('\\', "backslash");
887 TOKEN2CHAR(keyword_class);
888 TOKEN2CHAR(keyword_module);
889 TOKEN2CHAR(keyword_def);
890 TOKEN2CHAR(keyword_undef);
891 TOKEN2CHAR(keyword_begin);
892 TOKEN2CHAR(keyword_rescue);
893 TOKEN2CHAR(keyword_ensure);
894 TOKEN2CHAR(keyword_end);
895 TOKEN2CHAR(keyword_if);
896 TOKEN2CHAR(keyword_unless);
897 TOKEN2CHAR(keyword_then);
898 TOKEN2CHAR(keyword_elsif);
899 TOKEN2CHAR(keyword_else);
900 TOKEN2CHAR(keyword_case);
901 TOKEN2CHAR(keyword_when);
902 TOKEN2CHAR(keyword_while);
903 TOKEN2CHAR(keyword_until);
904 TOKEN2CHAR(keyword_for);
905 TOKEN2CHAR(keyword_break);
906 TOKEN2CHAR(keyword_next);
907 TOKEN2CHAR(keyword_redo);
908 TOKEN2CHAR(keyword_retry);
909 TOKEN2CHAR(keyword_in);
910 TOKEN2CHAR(keyword_do);
911 TOKEN2CHAR(keyword_do_cond);
912 TOKEN2CHAR(keyword_do_block);
913 TOKEN2CHAR(keyword_do_LAMBDA);
914 TOKEN2CHAR(keyword_return);
915 TOKEN2CHAR(keyword_yield);
916 TOKEN2CHAR(keyword_super);
917 TOKEN2CHAR(keyword_self);
918 TOKEN2CHAR(keyword_nil);
919 TOKEN2CHAR(keyword_true);
920 TOKEN2CHAR(keyword_false);
921 TOKEN2CHAR(keyword_and);
922 TOKEN2CHAR(keyword_or);
923 TOKEN2CHAR(keyword_not);
924 TOKEN2CHAR(modifier_if);
925 TOKEN2CHAR(modifier_unless);
926 TOKEN2CHAR(modifier_while);
927 TOKEN2CHAR(modifier_until);
928 TOKEN2CHAR(modifier_rescue);
929 TOKEN2CHAR(keyword_alias);
930 TOKEN2CHAR(keyword_defined);
931 TOKEN2CHAR(keyword_BEGIN);
932 TOKEN2CHAR(keyword_END);
933 TOKEN2CHAR(keyword__LINE__);
934 TOKEN2CHAR(keyword__FILE__);
935 TOKEN2CHAR(keyword__ENCODING__);
936 TOKEN2CHAR(tIDENTIFIER);
937 TOKEN2CHAR(tFID);
938 TOKEN2CHAR(tGVAR);
939 TOKEN2CHAR(tIVAR);
940 TOKEN2CHAR(tCONSTANT);
941 TOKEN2CHAR(tCVAR);
942 TOKEN2CHAR(tLABEL);
943 TOKEN2CHAR(tINTEGER);
944 TOKEN2CHAR(tFLOAT);
945 TOKEN2CHAR(tRATIONAL);
946 TOKEN2CHAR(tIMAGINARY);
947 TOKEN2CHAR(tCHAR);
948 TOKEN2CHAR(tNTH_REF);
949 TOKEN2CHAR(tBACK_REF);
950 TOKEN2CHAR(tSTRING_CONTENT);
951 TOKEN2CHAR(tREGEXP_END);
952 TOKEN2CHAR(tDUMNY_END);
953 TOKEN2CHAR(tSP);
954 TOKEN2CHAR(tUPLUS);
955 TOKEN2CHAR(tUMINUS);
956 TOKEN2CHAR(tPOW);
957 TOKEN2CHAR(tCMP);
958 TOKEN2CHAR(tEQ);
959 TOKEN2CHAR(tEQQ);
960 TOKEN2CHAR(tNEQ);
961 TOKEN2CHAR(tGEQ);
962 TOKEN2CHAR(tLEQ);
963 TOKEN2CHAR(tANDOP);
964 TOKEN2CHAR(tOROP);
965 TOKEN2CHAR(tMATCH);
966 TOKEN2CHAR(tNMATCH);
967 TOKEN2CHAR(tDOT2);
968 TOKEN2CHAR(tDOT3);
969 TOKEN2CHAR(tBDOT2);
970 TOKEN2CHAR(tBDOT3);
971 TOKEN2CHAR(tAREF);
972 TOKEN2CHAR(tASET);
973 TOKEN2CHAR(tLSHFT);
974 TOKEN2CHAR(tRSHFT);
975 TOKEN2CHAR(tANDDOT);
976 TOKEN2CHAR(tCOLON2);
977 TOKEN2CHAR(tCOLON3);
978 TOKEN2CHAR(tOP_ASGN);
979 TOKEN2CHAR(tASSOC);
980 TOKEN2CHAR(tLPAREN);
981 TOKEN2CHAR(tLPAREN_ARG);
982 TOKEN2CHAR(tLBRACK);
983 TOKEN2CHAR(tLBRACE);
984 TOKEN2CHAR(tLBRACE_ARG);
985 TOKEN2CHAR(tSTAR);
986 TOKEN2CHAR(tDSTAR);
987 TOKEN2CHAR(tAMPER);
988 TOKEN2CHAR(tLAMBDA);
989 TOKEN2CHAR(tSYMBEG);
990 TOKEN2CHAR(tSTRING_BEG);
991 TOKEN2CHAR(tXSTRING_BEG);
992 TOKEN2CHAR(tREGEXP_BEG);
993 TOKEN2CHAR(tWORDS_BEG);
994 TOKEN2CHAR(tQWORDS_BEG);
995 TOKEN2CHAR(tSYMBOLS_BEG);
996 TOKEN2CHAR(tQSYMBOLS_BEG);
997 TOKEN2CHAR(tSTRING_END);
998 TOKEN2CHAR(tSTRING_DEND);
999 TOKEN2CHAR(tSTRING_DBEG);
1000 TOKEN2CHAR(tSTRING_DVAR);
1001 TOKEN2CHAR(tLAMBEG);
1002 TOKEN2CHAR(tLABEL_END);
1003 TOKEN2CHAR(tIGNORED_NL);
1004 TOKEN2CHAR(tCOMMENT);
1005 TOKEN2CHAR(tEMBDOC_BEG);
1006 TOKEN2CHAR(tEMBDOC);
1007 TOKEN2CHAR(tEMBDOC_END);
1008 TOKEN2CHAR(tHEREDOC_BEG);
1009 TOKEN2CHAR(tHEREDOC_END);
1010 TOKEN2CHAR(k__END__);
1011 TOKEN2CHAR(tLOWEST);
1012 TOKEN2CHAR(tUMINUS_NUM);
1013 TOKEN2CHAR(tLAST_TOKEN);
1014#undef TOKEN2CHAR
1015#undef TOKEN2CHAR2
1016 }
1017
1018 rb_bug("parser_token2id: unknown token %d", tok);
1019
1020 UNREACHABLE_RETURN(0);
1021}
1022#else
1023static void
1024push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1025{
1026}
1027
1028static void
1029pop_end_expect_token_locations(struct parser_params *p)
1030{
1031}
1032#endif
1033
1034RBIMPL_ATTR_NONNULL((1, 2, 3))
1035static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1036RBIMPL_ATTR_NONNULL((1, 2))
1037static int parser_yyerror0(struct parser_params*, const char*);
1038#define yyerror0(msg) parser_yyerror0(p, (msg))
1039#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1040#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1041#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1042#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1043#define lex_eol_p(p) lex_eol_n_p(p, 0)
1044#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1045#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1046#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1047
1048static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1049static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1050static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1051static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1052static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1053
1054#ifdef RIPPER
1055#define compile_for_eval (0)
1056#else
1057#define compile_for_eval (p->parent_iseq != 0)
1058#endif
1059
1060#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1061
1062#define CALL_Q_P(q) ((q) == tANDDOT)
1063#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1064
1065#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1066
1067static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1068
1069static inline void
1070rb_discard_node(struct parser_params *p, NODE *n)
1071{
1072 rb_ast_delete_node(p->ast, n);
1073}
1074
1075static 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);
1076static 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);
1077static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1078static 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);
1079static 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);
1080static 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);
1081static 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);
1082static 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);
1083static 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);
1084static 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);
1085static 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);
1086static 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);
1087static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1088static 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);
1089static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1090static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1091static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1092static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1093static 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);
1094static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1095static 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);
1096static 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);
1097static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1098static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1099static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1100static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1101static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1102static 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);
1103static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1104static 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);
1105static 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);
1106static 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);
1107static 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);
1108static 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);
1109static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1110static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1111static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1112static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1113static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1114static 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);
1115static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1116static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1117static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1118static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1119static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1120static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1121static 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);
1122static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1123static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1124static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1125static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1126static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1127static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1128static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1129static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1130static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1131static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1132static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1133static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1134static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1135static 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);
1136static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1137static 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);
1138static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1139static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1140static 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);
1141static 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);
1142static 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);
1143static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1144static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1145static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1146static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1147static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1148static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1149static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1150static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1151static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1152static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1153static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1154static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1155static 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);
1156static 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);
1157static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1158static 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);
1159static 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);
1160static 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);
1161static 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);
1162static 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);
1163static 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);
1164static 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);
1165static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1166static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1167static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1168static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1169static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1170static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1171static 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);
1172static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1173static 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);
1174static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1175static 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);
1176static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1177static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1178static 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);
1179static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1180static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1181static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1182
1183#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1184#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1185#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1186#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)
1187#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)
1188#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1189#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1190#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1191#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)
1192#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)
1193#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)
1194#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)
1195#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1196#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)
1197#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1198#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1199#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1200#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1201#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1202#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1203#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1204#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1205#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1206#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1207#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1208#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1209#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1210#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1211#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1212#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)
1213#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)
1214#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1215#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1216#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1217#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1218#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1219#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1220#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1221#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1222#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)
1223#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1224#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1225#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1226#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1227#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1228#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1229#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)
1230#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1231#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1232#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1233#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1234#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1235#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1236#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1237#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1238#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1239#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1240#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1241#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1242#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1243#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1244#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1245#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1246#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1247#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1248#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1249#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1250#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)
1251#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1252#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1253#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1254#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1255#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1256#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1257#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1258#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1259#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1260#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1261#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1262#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1263#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1264#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1265#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1266#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)
1267#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1268#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)
1269#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1270#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1271#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1272#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1273#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1274#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1275#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1276#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1277#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1278#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1279#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)
1280#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1281#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1282#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1283#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)
1284#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1285#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1286#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1287#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1288#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1289#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1290#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1291
1292enum internal_node_type {
1293 NODE_INTERNAL_ONLY = NODE_LAST,
1294 NODE_DEF_TEMP,
1295 NODE_EXITS,
1296 NODE_INTERNAL_LAST
1297};
1298
1299static const char *
1300parser_node_name(int node)
1301{
1302 switch (node) {
1303 case NODE_DEF_TEMP:
1304 return "NODE_DEF_TEMP";
1305 case NODE_EXITS:
1306 return "NODE_EXITS";
1307 default:
1308 return ruby_node_name(node);
1309 }
1310}
1311
1312/* This node is parse.y internal */
1313struct RNode_DEF_TEMP {
1314 NODE node;
1315
1316 /* for NODE_DEFN/NODE_DEFS */
1317
1318 struct RNode *nd_def;
1319 ID nd_mid;
1320
1321 struct {
1322 int max_numparam;
1323 NODE *numparam_save;
1324 struct lex_context ctxt;
1325 } save;
1326};
1327
1328#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1329
1330static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1331static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1332static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1333static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1334static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1335
1336#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1337#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1338#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1339#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1340
1341/* Make a new internal node, which should not be appeared in the
1342 * result AST and does not have node_id and location. */
1343static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1344#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1345
1346static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1347
1348static int
1349parser_get_node_id(struct parser_params *p)
1350{
1351 int node_id = p->node_id;
1352 p->node_id++;
1353 return node_id;
1354}
1355
1356static void
1357anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1358{
1359 if (id == tANDDOT) {
1360 yyerror1(loc, "&. inside multiple assignment destination");
1361 }
1362}
1363
1364static inline void
1365set_line_body(NODE *body, int line)
1366{
1367 if (!body) return;
1368 switch (nd_type(body)) {
1369 case NODE_RESCUE:
1370 case NODE_ENSURE:
1371 nd_set_line(body, line);
1372 }
1373}
1374
1375static void
1376set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1377{
1378 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1379 nd_set_line(node, beg->end_pos.lineno);
1380}
1381
1382static NODE *
1383last_expr_node(NODE *expr)
1384{
1385 while (expr) {
1386 if (nd_type_p(expr, NODE_BLOCK)) {
1387 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1388 }
1389 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1390 expr = RNODE_BEGIN(expr)->nd_body;
1391 }
1392 else {
1393 break;
1394 }
1395 }
1396 return expr;
1397}
1398
1399#ifndef RIPPER
1400#define yyparse ruby_yyparse
1401#endif
1402
1403static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1404static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1405static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1406static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1407static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1408static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1409
1410static NODE *newline_node(NODE*);
1411static void fixpos(NODE*,NODE*);
1412
1413static int value_expr(struct parser_params*,NODE*);
1414static void void_expr(struct parser_params*,NODE*);
1415static NODE *remove_begin(NODE*);
1416static NODE *void_stmts(struct parser_params*,NODE*);
1417static void reduce_nodes(struct parser_params*,NODE**);
1418static void block_dup_check(struct parser_params*,NODE*,NODE*);
1419
1420static NODE *block_append(struct parser_params*,NODE*,NODE*);
1421static NODE *list_append(struct parser_params*,NODE*,NODE*);
1422static NODE *list_concat(NODE*,NODE*);
1423static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1424static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1425static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1426static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1427static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1428static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1429static NODE *str2dstr(struct parser_params*,NODE*);
1430static NODE *evstr2dstr(struct parser_params*,NODE*);
1431static NODE *splat_array(NODE*);
1432static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1433
1434static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1435static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1436static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1437static 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);
1438static 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;}
1439static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1440
1441static bool args_info_empty_p(struct rb_args_info *args);
1442static 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*);
1443static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1444static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1445static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1446static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1447static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1448static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1449static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1450
1451static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1452static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1453
1454static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1455static void no_blockarg(struct parser_params*,NODE*);
1456static NODE *ret_args(struct parser_params*,NODE*);
1457static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1458static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1459
1460static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1461static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1462
1463static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1464static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1465
1466static VALUE rb_backref_error(struct parser_params*,NODE*);
1467static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1468
1469static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1470static 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);
1471static 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);
1472static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1473static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1474
1475static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1476
1477static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1478static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1479
1480static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1481static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1482
1483static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1484
1485static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1486
1487#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1488
1489static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1490
1491static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1492
1493static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1494
1495static rb_ast_id_table_t *local_tbl(struct parser_params*);
1496
1497static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1498static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1499
1500static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1501static NODE *heredoc_dedent(struct parser_params*,NODE*);
1502
1503static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1504
1505static 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);
1506
1507#ifdef RIPPER
1508#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1509#define set_value(val) (p->s_lvalue = val)
1510static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1511static int id_is_var(struct parser_params *p, ID id);
1512#endif
1513
1514RUBY_SYMBOL_EXPORT_BEGIN
1515VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1516int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1517enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1518VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1519void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1520PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1521YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1522YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1523YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1524YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1525YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1526YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1527void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1528RUBY_SYMBOL_EXPORT_END
1529
1530static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1531static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1532static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1533static VALUE formal_argument_error(struct parser_params*, ID);
1534static ID shadowing_lvar(struct parser_params*,ID);
1535static void new_bv(struct parser_params*,ID);
1536
1537static void local_push(struct parser_params*,int);
1538static void local_pop(struct parser_params*);
1539static void local_var(struct parser_params*, ID);
1540static void arg_var(struct parser_params*, ID);
1541static int local_id(struct parser_params *p, ID id);
1542static int local_id_ref(struct parser_params*, ID, ID **);
1543#define internal_id rb_parser_internal_id
1544ID internal_id(struct parser_params*);
1545static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1546static int check_forwarding_args(struct parser_params*);
1547static void add_forwarding_args(struct parser_params *p);
1548static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1549
1550static const struct vtable *dyna_push(struct parser_params *);
1551static void dyna_pop(struct parser_params*, const struct vtable *);
1552static int dyna_in_block(struct parser_params*);
1553#define dyna_var(p, id) local_var(p, id)
1554static int dvar_defined(struct parser_params*, ID);
1555#define dvar_defined_ref rb_parser_dvar_defined_ref
1556int dvar_defined_ref(struct parser_params*, ID, ID**);
1557static int dvar_curr(struct parser_params*,ID);
1558
1559static int lvar_defined(struct parser_params*, ID);
1560
1561static NODE *numparam_push(struct parser_params *p);
1562static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1563
1564#define METHOD_NOT '!'
1565
1566#define idFWD_REST '*'
1567#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1568#define idFWD_BLOCK '&'
1569#define idFWD_ALL idDot3
1570#define arg_FWD_BLOCK idFWD_BLOCK
1571
1572#define RE_ONIG_OPTION_IGNORECASE 1
1573#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1574#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1575#define RE_OPTION_ONCE (1<<16)
1576#define RE_OPTION_ENCODING_SHIFT 8
1577#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1578#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1579#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1580#define RE_OPTION_MASK 0xff
1581#define RE_OPTION_ARG_ENCODING_NONE 32
1582
1583#define CHECK_LITERAL_WHEN (st_table *)1
1584#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1585
1586#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1587RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1588
1589#define TOKEN2ID(tok) ( \
1590 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1591 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1592 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1593 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1594 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1595 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1596 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1597
1598/****** Ripper *******/
1599
1600#ifdef RIPPER
1601
1602#include "eventids1.h"
1603#include "eventids2.h"
1604
1605extern const struct ripper_parser_ids ripper_parser_ids;
1606
1607static VALUE ripper_dispatch0(struct parser_params*,ID);
1608static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1609static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1610static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1611static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1612static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1613static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1614void ripper_error(struct parser_params *p);
1615
1616#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1617#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1618#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1619#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1620#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1621#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1622#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1623
1624#define yyparse ripper_yyparse
1625
1626static VALUE
1627aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1628{
1629 if (!NIL_P(pre_arg)) {
1630 if (!NIL_P(pre_args)) {
1631 rb_ary_unshift(pre_args, pre_arg);
1632 }
1633 else {
1634 pre_args = rb_ary_new_from_args(1, pre_arg);
1635 }
1636 }
1637 return pre_args;
1638}
1639
1640#define ID2VAL(id) STATIC_ID2SYM(id)
1641#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1642#endif /* RIPPER */
1643
1644#define KWD2EID(t, v) keyword_##t
1645
1646static NODE *
1647new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1648{
1649 body = remove_begin(body);
1650 reduce_nodes(p, &body);
1651 NODE *n = NEW_SCOPE(args, body, parent, loc);
1652 nd_set_line(n, loc->end_pos.lineno);
1653 set_line_body(body, loc->beg_pos.lineno);
1654 return n;
1655}
1656
1657static NODE *
1658rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1659 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1660{
1661 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1662 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1663 loc.beg_pos = arg_loc->beg_pos;
1664 return NEW_RESCUE(arg, rescue, 0, &loc);
1665}
1666
1667static NODE *add_block_exit(struct parser_params *p, NODE *node);
1668static rb_node_exits_t *init_block_exit(struct parser_params *p);
1669static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1670static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1671static void clear_block_exit(struct parser_params *p, bool error);
1672
1673static void
1674next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1675{
1676 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1677}
1678
1679static void
1680restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1681{
1682 /* See: def_name action */
1683 struct lex_context ctxt = temp->save.ctxt;
1684 p->ctxt.in_def = ctxt.in_def;
1685 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1686 p->ctxt.in_rescue = ctxt.in_rescue;
1687 p->max_numparam = temp->save.max_numparam;
1688 numparam_pop(p, temp->save.numparam_save);
1689 clear_block_exit(p, true);
1690}
1691
1692static void
1693endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1694{
1695 if (is_attrset_id(mid)) {
1696 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1697 }
1698 token_info_drop(p, "def", loc->beg_pos);
1699}
1700
1701#define debug_token_line(p, name, line) do { \
1702 if (p->debug) { \
1703 const char *const pcur = p->lex.pcur; \
1704 const char *const ptok = p->lex.ptok; \
1705 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1706 line, p->ruby_sourceline, \
1707 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1708 } \
1709 } while (0)
1710
1711#define begin_definition(k, loc_beg, loc_end) \
1712 do { \
1713 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1714 /* singleton class */ \
1715 p->ctxt.cant_return = !p->ctxt.in_def; \
1716 p->ctxt.in_def = 0; \
1717 } \
1718 else if (p->ctxt.in_def) { \
1719 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1720 yyerror1(&loc, k " definition in method body"); \
1721 } \
1722 else { \
1723 p->ctxt.cant_return = 1; \
1724 } \
1725 local_push(p, 0); \
1726 } while (0)
1727
1728#ifndef RIPPER
1729# define ifndef_ripper(x) (x)
1730# define ifdef_ripper(r,x) (x)
1731#else
1732# define ifndef_ripper(x)
1733# define ifdef_ripper(r,x) (r)
1734#endif
1735
1736# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1737# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1738# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1739# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1740# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1741# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1742# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1743# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1744# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1745# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1746# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1747# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1748# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1749# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1750# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1751# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1752# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1753# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1754# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1755# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1756#ifdef RIPPER
1757extern const ID id_warn, id_warning, id_gets, id_assoc;
1758# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1759# define WARN_S_L(s,l) STR_NEW(s,l)
1760# define WARN_S(s) STR_NEW2(s)
1761# define WARN_I(i) INT2NUM(i)
1762# define WARN_ID(i) rb_id2str(i)
1763# define PRIsWARN PRIsVALUE
1764# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1765# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1766# ifdef HAVE_VA_ARGS_MACRO
1767# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1768# else
1769# define WARN_CALL rb_funcall
1770# endif
1771# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1772# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1773# ifdef HAVE_VA_ARGS_MACRO
1774# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1775# else
1776# define WARNING_CALL rb_funcall
1777# endif
1778# define compile_error ripper_compile_error
1779#else
1780# define WARN_S_L(s,l) s
1781# define WARN_S(s) s
1782# define WARN_I(i) i
1783# define WARN_ID(i) rb_id2name(i)
1784# define PRIsWARN PRIsVALUE
1785# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1786# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1787# define WARN_CALL rb_compile_warn
1788# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1789# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1790# define WARNING_CALL rb_compile_warning
1791PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1792# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1793#endif
1794
1795#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1796
1797static NODE *
1798add_block_exit(struct parser_params *p, NODE *node)
1799{
1800 if (!node) {
1801 compile_error(p, "unexpected null node");
1802 return 0;
1803 }
1804 switch (nd_type(node)) {
1805 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1806 default:
1807 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1808 return node;
1809 }
1810 if (!p->ctxt.in_defined) {
1811 rb_node_exits_t *exits = p->exits;
1812 if (exits) {
1813 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1814 exits->nd_stts = node;
1815 }
1816 }
1817 return node;
1818}
1819
1820static rb_node_exits_t *
1821init_block_exit(struct parser_params *p)
1822{
1823 rb_node_exits_t *old = p->exits;
1824 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1825 exits->nd_chain = 0;
1826 exits->nd_stts = RNODE(exits);
1827 p->exits = exits;
1828 return old;
1829}
1830
1831static rb_node_exits_t *
1832allow_block_exit(struct parser_params *p)
1833{
1834 rb_node_exits_t *exits = p->exits;
1835 p->exits = 0;
1836 return exits;
1837}
1838
1839static void
1840restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1841{
1842 p->exits = exits;
1843}
1844
1845static void
1846clear_block_exit(struct parser_params *p, bool error)
1847{
1848 rb_node_exits_t *exits = p->exits;
1849 if (!exits) return;
1850 if (error) {
1851 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1852 switch (nd_type(e)) {
1853 case NODE_BREAK:
1854 yyerror1(&e->nd_loc, "Invalid break");
1855 break;
1856 case NODE_NEXT:
1857 yyerror1(&e->nd_loc, "Invalid next");
1858 break;
1859 case NODE_REDO:
1860 yyerror1(&e->nd_loc, "Invalid redo");
1861 break;
1862 default:
1863 yyerror1(&e->nd_loc, "unexpected node");
1864 goto end_checks; /* no nd_chain */
1865 }
1866 }
1867 end_checks:;
1868 }
1869 exits->nd_stts = RNODE(exits);
1870 exits->nd_chain = 0;
1871}
1872
1873#define WARN_EOL(tok) \
1874 (looking_at_eol_p(p) ? \
1875 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1876 (void)0)
1877static int looking_at_eol_p(struct parser_params *p);
1878
1879static NODE *
1880get_nd_value(struct parser_params *p, NODE *node)
1881{
1882 switch (nd_type(node)) {
1883 case NODE_GASGN:
1884 return RNODE_GASGN(node)->nd_value;
1885 case NODE_IASGN:
1886 return RNODE_IASGN(node)->nd_value;
1887 case NODE_LASGN:
1888 return RNODE_LASGN(node)->nd_value;
1889 case NODE_DASGN:
1890 return RNODE_DASGN(node)->nd_value;
1891 case NODE_MASGN:
1892 return RNODE_MASGN(node)->nd_value;
1893 case NODE_CVASGN:
1894 return RNODE_CVASGN(node)->nd_value;
1895 case NODE_CDECL:
1896 return RNODE_CDECL(node)->nd_value;
1897 default:
1898 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1899 return 0;
1900 }
1901}
1902
1903static void
1904set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1905{
1906 switch (nd_type(node)) {
1907 case NODE_CDECL:
1908 RNODE_CDECL(node)->nd_value = rhs;
1909 break;
1910 case NODE_GASGN:
1911 RNODE_GASGN(node)->nd_value = rhs;
1912 break;
1913 case NODE_IASGN:
1914 RNODE_IASGN(node)->nd_value = rhs;
1915 break;
1916 case NODE_LASGN:
1917 RNODE_LASGN(node)->nd_value = rhs;
1918 break;
1919 case NODE_DASGN:
1920 RNODE_DASGN(node)->nd_value = rhs;
1921 break;
1922 case NODE_MASGN:
1923 RNODE_MASGN(node)->nd_value = rhs;
1924 break;
1925 case NODE_CVASGN:
1926 RNODE_CVASGN(node)->nd_value = rhs;
1927 break;
1928 default:
1929 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1930 break;
1931 }
1932}
1933
1934static ID
1935get_nd_vid(struct parser_params *p, NODE *node)
1936{
1937 switch (nd_type(node)) {
1938 case NODE_CDECL:
1939 return RNODE_CDECL(node)->nd_vid;
1940 case NODE_GASGN:
1941 return RNODE_GASGN(node)->nd_vid;
1942 case NODE_IASGN:
1943 return RNODE_IASGN(node)->nd_vid;
1944 case NODE_LASGN:
1945 return RNODE_LASGN(node)->nd_vid;
1946 case NODE_DASGN:
1947 return RNODE_DASGN(node)->nd_vid;
1948 case NODE_CVASGN:
1949 return RNODE_CVASGN(node)->nd_vid;
1950 default:
1951 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1952 return 0;
1953 }
1954}
1955
1956static NODE *
1957get_nd_args(struct parser_params *p, NODE *node)
1958{
1959 switch (nd_type(node)) {
1960 case NODE_CALL:
1961 return RNODE_CALL(node)->nd_args;
1962 case NODE_OPCALL:
1963 return RNODE_OPCALL(node)->nd_args;
1964 case NODE_FCALL:
1965 return RNODE_FCALL(node)->nd_args;
1966 case NODE_QCALL:
1967 return RNODE_QCALL(node)->nd_args;
1968 case NODE_SUPER:
1969 return RNODE_SUPER(node)->nd_args;
1970 case NODE_VCALL:
1971 case NODE_ZSUPER:
1972 case NODE_YIELD:
1973 case NODE_RETURN:
1974 case NODE_BREAK:
1975 case NODE_NEXT:
1976 return 0;
1977 default:
1978 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1979 return 0;
1980 }
1981}
1982
1983static st_index_t
1984djb2(const uint8_t *str, size_t len)
1985{
1986 st_index_t hash = 5381;
1987
1988 for (size_t i = 0; i < len; i++) {
1989 hash = ((hash << 5) + hash) + str[i];
1990 }
1991
1992 return hash;
1993}
1994
1995static st_index_t
1996parser_memhash(const void *ptr, long len)
1997{
1998 return djb2(ptr, len);
1999}
2000
2001#define PARSER_STRING_PTR(str) (str->ptr)
2002#define PARSER_STRING_LEN(str) (str->len)
2003#define PARSER_STRING_END(str) (&str->ptr[str->len])
2004#define STRING_SIZE(str) ((size_t)str->len + 1)
2005#define STRING_TERM_LEN(str) (1)
2006#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
2007#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
2008 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
2009 str->len = total; \
2010} while (0)
2011#define STRING_SET_LEN(str, n) do { \
2012 (str)->len = (n); \
2013} while (0)
2014#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2015 ((ptrvar) = str->ptr, \
2016 (lenvar) = str->len)
2017
2018static inline int
2019parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2020{
2021 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2022}
2023
2024static rb_parser_string_t *
2025rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2026{
2027 rb_parser_string_t *str;
2028
2029 if (len < 0) {
2030 rb_bug("negative string size (or size too big): %ld", len);
2031 }
2032
2033 str = xcalloc(1, sizeof(rb_parser_string_t));
2034 str->ptr = xcalloc(len + 1, sizeof(char));
2035
2036 if (ptr) {
2037 memcpy(PARSER_STRING_PTR(str), ptr, len);
2038 }
2039 STRING_SET_LEN(str, len);
2040 STRING_TERM_FILL(str);
2041 return str;
2042}
2043
2044static rb_parser_string_t *
2045rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2046{
2047 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2048 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2049 str->enc = enc;
2050 return str;
2051}
2052
2053#ifndef RIPPER
2054rb_parser_string_t *
2055rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2056{
2057 /* Type check */
2058 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2059 RB_GC_GUARD(str);
2060 return ret;
2061}
2062
2063void
2064rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2065{
2066 if (!str) return;
2067 xfree(PARSER_STRING_PTR(str));
2068 xfree(str);
2069}
2070#endif
2071
2072static st_index_t
2073rb_parser_str_hash(rb_parser_string_t *str)
2074{
2075 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2076}
2077
2078static st_index_t
2079rb_char_p_hash(const char *c)
2080{
2081 return parser_memhash((const void *)c, strlen(c));
2082}
2083
2084static size_t
2085rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2086{
2087 return PARSER_STRING_LEN(str);
2088}
2089
2090#ifndef RIPPER
2091static char *
2092rb_parser_string_end(rb_parser_string_t *str)
2093{
2094 return &str->ptr[str->len];
2095}
2096#endif
2097
2098static void
2099rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2100{
2101 str->enc = enc;
2102}
2103
2104static rb_encoding *
2105rb_parser_str_get_encoding(rb_parser_string_t *str)
2106{
2107 return str->enc;
2108}
2109
2110#ifndef RIPPER
2111static bool
2112PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2113{
2114 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2115}
2116#endif
2117
2118static int
2119PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2120{
2121 return str->coderange;
2122}
2123
2124static void
2125PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2126{
2127 str->coderange = coderange;
2128}
2129
2130static void
2131PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2132{
2133 rb_parser_string_set_encoding(str, enc);
2134 PARSER_ENC_CODERANGE_SET(str, cr);
2135}
2136
2137static void
2138PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2139{
2140 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2141}
2142
2143static bool
2144PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2145{
2146 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2147}
2148
2149static bool
2150PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2151{
2152 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2153}
2154
2155static const char *
2156rb_parser_search_nonascii(const char *p, const char *e)
2157{
2158 const char *s = p;
2159
2160 for (; s < e; s++) {
2161 if (*s & 0x80) return s;
2162 }
2163
2164 return NULL;
2165}
2166
2167static int
2168rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2169{
2170 const char *e = ptr + len;
2171
2172 if (enc == rb_ascii8bit_encoding()) {
2173 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2174 ptr = rb_parser_search_nonascii(ptr, e);
2175 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2176 }
2177
2178 /* parser string encoding is always asciicompat */
2179 ptr = rb_parser_search_nonascii(ptr, e);
2180 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2181 for (;;) {
2182 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2183 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2184 ptr += MBCLEN_CHARFOUND_LEN(ret);
2185 if (ptr == e) break;
2186 ptr = rb_parser_search_nonascii(ptr, e);
2187 if (!ptr) break;
2188 }
2189
2190 return RB_PARSER_ENC_CODERANGE_VALID;
2191}
2192
2193static int
2194rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2195{
2196 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2197}
2198
2199static int
2200rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2201{
2202 int cr = PARSER_ENC_CODERANGE(str);
2203
2204 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2205 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2206 PARSER_ENC_CODERANGE_SET(str, cr);
2207 }
2208
2209 return cr;
2210}
2211
2212static rb_parser_string_t *
2213rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2214{
2215 if (rb_parser_str_get_encoding(str) == enc)
2216 return str;
2217 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2218 PARSER_ENC_CODERANGE_CLEAR(str);
2219 }
2220 rb_parser_string_set_encoding(str, enc);
2221 return str;
2222}
2223
2224static bool
2225rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2226{
2227 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2228}
2229
2230static rb_encoding *
2231rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2232{
2233 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2234 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2235
2236 if (enc1 == NULL || enc2 == NULL)
2237 return 0;
2238
2239 if (enc1 == enc2) {
2240 return enc1;
2241 }
2242
2243 if (PARSER_STRING_LEN(str2) == 0)
2244 return enc1;
2245 if (PARSER_STRING_LEN(str1) == 0)
2246 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2247
2248 int cr1, cr2;
2249
2250 cr1 = rb_parser_enc_str_coderange(p, str1);
2251 cr2 = rb_parser_enc_str_coderange(p, str2);
2252
2253 if (cr1 != cr2) {
2254 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2255 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2256 }
2257
2258 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2259 return enc1;
2260 }
2261
2262 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2263 return enc2;
2264 }
2265
2266 return 0;
2267}
2268
2269static void
2270rb_parser_str_modify(rb_parser_string_t *str)
2271{
2272 PARSER_ENC_CODERANGE_CLEAR(str);
2273}
2274
2275static void
2276rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2277{
2278 long capa;
2279 const int termlen = STRING_TERM_LEN(str);
2280
2281 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2282 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2283 }
2284
2285 int cr = PARSER_ENC_CODERANGE(str);
2286 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2287 /* Leave unknown. */
2288 }
2289 else if (len > PARSER_STRING_LEN(str)) {
2290 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2291 }
2292 else if (len < PARSER_STRING_LEN(str)) {
2293 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2294 /* ASCII-only string is keeping after truncated. Valid
2295 * and broken may be invalid or valid, leave unknown. */
2296 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2297 }
2298 }
2299
2300 STRING_SET_LEN(str, len);
2301 STRING_TERM_FILL(str);
2302}
2303
2304static rb_parser_string_t *
2305rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2306{
2307 rb_parser_str_modify(str);
2308 if (len == 0) return 0;
2309
2310 long total, olen, off = -1;
2311 char *sptr;
2312 const int termlen = STRING_TERM_LEN(str);
2313
2314 PARSER_STRING_GETMEM(str, sptr, olen);
2315 if (ptr >= sptr && ptr <= sptr + olen) {
2316 off = ptr - sptr;
2317 }
2318
2319 if (olen > LONG_MAX - len) {
2320 compile_error(p, "string sizes too big");
2321 return 0;
2322 }
2323 total = olen + len;
2324 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2325 sptr = PARSER_STRING_PTR(str);
2326 if (off != -1) {
2327 ptr = sptr + off;
2328 }
2329 memcpy(sptr + olen, ptr, len);
2330 STRING_SET_LEN(str, total);
2331 STRING_TERM_FILL(str);
2332
2333 return str;
2334}
2335
2336#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2337#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2338
2339static rb_parser_string_t *
2340rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2341 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2342{
2343 int str_cr, res_cr;
2344 rb_encoding *str_enc, *res_enc;
2345
2346 str_enc = rb_parser_str_get_encoding(str);
2347 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2348
2349 if (str_enc == ptr_enc) {
2350 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2351 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2352 }
2353 }
2354 else {
2355 /* parser string encoding is always asciicompat */
2356 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2357 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2358 }
2359 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2360 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2361 str_cr = rb_parser_enc_str_coderange(p, str);
2362 }
2363 }
2364 }
2365 if (ptr_cr_ret)
2366 *ptr_cr_ret = ptr_cr;
2367
2368 if (str_enc != ptr_enc &&
2369 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2370 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2371 goto incompatible;
2372 }
2373
2374 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2375 res_enc = str_enc;
2376 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2377 }
2378 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2379 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2380 res_enc = str_enc;
2381 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2382 }
2383 else {
2384 res_enc = ptr_enc;
2385 res_cr = ptr_cr;
2386 }
2387 }
2388 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2389 res_enc = str_enc;
2390 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2391 res_cr = str_cr;
2392 else
2393 res_cr = ptr_cr;
2394 }
2395 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2396 res_enc = str_enc;
2397 res_cr = str_cr;
2398 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2399 }
2400
2401 if (len < 0) {
2402 compile_error(p, "negative string size (or size too big)");
2403 return 0;
2404 }
2405 parser_str_cat(str, ptr, len);
2406 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2407 return str;
2408
2409 incompatible:
2410 compile_error(p, "incompatible character encodings: %s and %s",
2411 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2412 return 0;
2413}
2414
2415static rb_parser_string_t *
2416rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2417 rb_encoding *ptr_enc)
2418{
2419 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2420}
2421
2422static rb_parser_string_t *
2423rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2424{
2425 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2426
2427 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2428 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2429
2430 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2431
2432 return str;
2433}
2434
2435static rb_parser_string_t *
2436rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2437{
2438 if (len < 0) {
2439 rb_bug("negative string size (or size too big)");
2440 }
2441
2442 long slen = PARSER_STRING_LEN(str);
2443
2444 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2445 PARSER_ENC_CODERANGE_CLEAR(str);
2446 }
2447
2448 {
2449 long capa;
2450 const int termlen = STRING_TERM_LEN(str);
2451
2452 if ((capa = slen) < len) {
2453 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2454 }
2455 else if (len == slen) return str;
2456 STRING_SET_LEN(str, len);
2457 STRING_TERM_FILL(str);
2458 }
2459 return str;
2460}
2461
2462# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2463 ((ptrvar) = str->ptr, \
2464 (lenvar) = str->len, \
2465 (encvar) = str->enc)
2466
2467static int
2468rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2469{
2470 long len1, len2;
2471 const char *ptr1, *ptr2;
2472 rb_encoding *enc1, *enc2;
2473
2474 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2475 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2476
2477 return (len1 != len2 ||
2478 enc1 != enc2 ||
2479 memcmp(ptr1, ptr2, len1) != 0);
2480}
2481
2482static void
2483rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2484{
2485 long i;
2486 if (ary->capa < len) {
2487 ary->capa = len;
2488 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2489 for (i = ary->len; i < len; i++) {
2490 ary->data[i] = 0;
2491 }
2492 }
2493}
2494
2495/*
2496 * Do not call this directly.
2497 * Use rb_parser_ary_new_capa_for_XXX() instead.
2498 */
2499static rb_parser_ary_t *
2500parser_ary_new_capa(rb_parser_t *p, long len)
2501{
2502 if (len < 0) {
2503 rb_bug("negative array size (or size too big): %ld", len);
2504 }
2505 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2506 ary->data_type = 0;
2507 ary->len = 0;
2508 ary->capa = len;
2509 if (0 < len) {
2510 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2511 }
2512 else {
2513 ary->data = NULL;
2514 }
2515 return ary;
2516}
2517
2518#ifndef RIPPER
2519static rb_parser_ary_t *
2520rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2521{
2522 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2523 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2524 return ary;
2525}
2526
2527static rb_parser_ary_t *
2528rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2529{
2530 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2531 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2532 return ary;
2533}
2534#endif
2535
2536static rb_parser_ary_t *
2537rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2538{
2539 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2540 ary->data_type = PARSER_ARY_DATA_NODE;
2541 return ary;
2542}
2543
2544/*
2545 * Do not call this directly.
2546 * Use rb_parser_ary_push_XXX() instead.
2547 */
2548static rb_parser_ary_t *
2549parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2550{
2551 if (ary->len == ary->capa) {
2552 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2553 }
2554 ary->data[ary->len++] = val;
2555 return ary;
2556}
2557
2558#ifndef RIPPER
2559static rb_parser_ary_t *
2560rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2561{
2562 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2563 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2564 }
2565 return parser_ary_push(p, ary, val);
2566}
2567
2568static rb_parser_ary_t *
2569rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2570{
2571 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2572 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2573 }
2574 return parser_ary_push(p, ary, val);
2575}
2576#endif
2577
2578static rb_parser_ary_t *
2579rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2580{
2581 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2582 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2583 }
2584 return parser_ary_push(p, ary, val);
2585}
2586
2587#ifndef RIPPER
2588static void
2589rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2590{
2591 if (!token) return;
2592 rb_parser_string_free(p, token->str);
2593 xfree(token);
2594}
2595
2596static void
2597rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2598{
2599# define foreach_ary(ptr) \
2600 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2601 ptr < end_ary_data; ptr++)
2602 switch (ary->data_type) {
2603 case PARSER_ARY_DATA_AST_TOKEN:
2604 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2605 break;
2606 case PARSER_ARY_DATA_SCRIPT_LINE:
2607 foreach_ary(data) {rb_parser_string_free(p, *data);}
2608 break;
2609 case PARSER_ARY_DATA_NODE:
2610 /* Do nothing because nodes are freed when rb_ast_t is freed */
2611 break;
2612 default:
2613 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2614 break;
2615 }
2616# undef foreach_ary
2617 xfree(ary->data);
2618 xfree(ary);
2619}
2620
2621#endif /* !RIPPER */
2622%}
2623
2624%expect 0
2625%define api.pure
2626%define parse.error verbose
2627%printer {
2628 if ((NODE *)$$ == (NODE *)-1) {
2629 rb_parser_printf(p, "NODE_SPECIAL");
2630 }
2631 else if ($$) {
2632 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2633 }
2634} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2635 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2636%printer {
2637 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2638} <id>
2639%printer {
2640 switch (nd_type(RNODE($$))) {
2641 case NODE_INTEGER:
2642 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2643 break;
2644 case NODE_FLOAT:
2645 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2646 break;
2647 case NODE_RATIONAL:
2648 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2649 break;
2650 case NODE_IMAGINARY:
2651 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2652 break;
2653 default:
2654 break;
2655 }
2656} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2657%printer {
2658 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2659} tNTH_REF
2660%printer {
2661 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2662} tBACK_REF
2663
2664%destructor {
2665 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2666} <labels>
2667
2668%lex-param {struct parser_params *p}
2669%parse-param {struct parser_params *p}
2670%initial-action
2671{
2672 RUBY_SET_YYLLOC_OF_NONE(@$);
2673};
2674%after-shift after_shift
2675%before-reduce before_reduce
2676%after-reduce after_reduce
2677%after-shift-error-token after_shift_error_token
2678%after-pop-stack after_pop_stack
2679
2680%union {
2681 NODE *node;
2682 rb_node_fcall_t *node_fcall;
2683 rb_node_args_t *node_args;
2684 rb_node_args_aux_t *node_args_aux;
2685 rb_node_opt_arg_t *node_opt_arg;
2686 rb_node_kw_arg_t *node_kw_arg;
2687 rb_node_block_pass_t *node_block_pass;
2688 rb_node_masgn_t *node_masgn;
2689 rb_node_def_temp_t *node_def_temp;
2690 rb_node_exits_t *node_exits;
2691 struct rb_locations_lambda_body_t *locations_lambda_body;
2692 ID id;
2693 int num;
2694 st_table *tbl;
2695 st_table *labels;
2696 const struct vtable *vars;
2697 struct rb_strterm_struct *strterm;
2698 struct lex_context ctxt;
2699 enum lex_state_e state;
2700}
2701
2702%token <id>
2703 keyword_class "'class'"
2704 keyword_module "'module'"
2705 keyword_def "'def'"
2706 keyword_undef "'undef'"
2707 keyword_begin "'begin'"
2708 keyword_rescue "'rescue'"
2709 keyword_ensure "'ensure'"
2710 keyword_end "'end'"
2711 keyword_if "'if'"
2712 keyword_unless "'unless'"
2713 keyword_then "'then'"
2714 keyword_elsif "'elsif'"
2715 keyword_else "'else'"
2716 keyword_case "'case'"
2717 keyword_when "'when'"
2718 keyword_while "'while'"
2719 keyword_until "'until'"
2720 keyword_for "'for'"
2721 keyword_break "'break'"
2722 keyword_next "'next'"
2723 keyword_redo "'redo'"
2724 keyword_retry "'retry'"
2725 keyword_in "'in'"
2726 keyword_do "'do'"
2727 keyword_do_cond "'do' for condition"
2728 keyword_do_block "'do' for block"
2729 keyword_do_LAMBDA "'do' for lambda"
2730 keyword_return "'return'"
2731 keyword_yield "'yield'"
2732 keyword_super "'super'"
2733 keyword_self "'self'"
2734 keyword_nil "'nil'"
2735 keyword_true "'true'"
2736 keyword_false "'false'"
2737 keyword_and "'and'"
2738 keyword_or "'or'"
2739 keyword_not "'not'"
2740 modifier_if "'if' modifier"
2741 modifier_unless "'unless' modifier"
2742 modifier_while "'while' modifier"
2743 modifier_until "'until' modifier"
2744 modifier_rescue "'rescue' modifier"
2745 keyword_alias "'alias'"
2746 keyword_defined "'defined?'"
2747 keyword_BEGIN "'BEGIN'"
2748 keyword_END "'END'"
2749 keyword__LINE__ "'__LINE__'"
2750 keyword__FILE__ "'__FILE__'"
2751 keyword__ENCODING__ "'__ENCODING__'"
2752
2753%token <id> tIDENTIFIER "local variable or method"
2754%token <id> tFID "method"
2755%token <id> tGVAR "global variable"
2756%token <id> tIVAR "instance variable"
2757%token <id> tCONSTANT "constant"
2758%token <id> tCVAR "class variable"
2759%token <id> tLABEL "label"
2760%token <node> tINTEGER "integer literal"
2761%token <node> tFLOAT "float literal"
2762%token <node> tRATIONAL "rational literal"
2763%token <node> tIMAGINARY "imaginary literal"
2764%token <node> tCHAR "char literal"
2765%token <node> tNTH_REF "numbered reference"
2766%token <node> tBACK_REF "back reference"
2767%token <node> tSTRING_CONTENT "literal content"
2768%token <num> tREGEXP_END
2769%token <num> tDUMNY_END "dummy end"
2770
2771%type <node> singleton singleton_expr strings string string1 xstring regexp
2772%type <node> string_contents xstring_contents regexp_contents string_content
2773%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2774%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2775%type <node_def_temp> defn_head defs_head k_def
2776%type <node_exits> block_open k_while k_until k_for allow_exits
2777%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2778%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2779%type <node> command command_call command_call_value method_call
2780%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2781%type <node_fcall> fcall
2782%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2783%type <node> args arg_splat call_args opt_call_args
2784%type <node> paren_args opt_paren_args
2785%type <node_args> args_tail block_args_tail
2786%type <node> command_args aref_args
2787%type <node_block_pass> opt_block_arg block_arg
2788%type <node> var_ref var_lhs
2789%type <node> command_rhs arg_rhs
2790%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2791%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2792%type <node_args_aux> f_arg f_arg_item
2793%type <node> f_marg f_rest_marg
2794%type <node_masgn> f_margs
2795%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2796%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2797%type <id> do bv_decls opt_bv_decl bvar
2798%type <node> lambda brace_body do_body
2799%type <locations_lambda_body> lambda_body
2800%type <node_args> f_larglist
2801%type <node> brace_block cmd_brace_block do_block lhs none fitem
2802%type <node> mlhs_head mlhs_item mlhs_node
2803%type <node_masgn> mlhs mlhs_basic mlhs_inner
2804%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2805%type <node> p_expr p_as p_alt p_expr_basic p_find
2806%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2807%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2808%type <node> p_kwargs p_kwarg p_kw
2809%type <id> keyword_variable user_variable sym operation2 operation3
2810%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2811%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2812%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2813%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2814%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2815%type <ctxt> p_in_kwarg
2816%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2817%type <num> max_numparam
2818%type <node> numparam
2819%type <id> it_id
2820%token END_OF_INPUT 0 "end-of-input"
2821%token <id> '.'
2822
2823/* escaped chars, should be ignored otherwise */
2824%token <id> '\\' "backslash"
2825%token tSP "escaped space"
2826%token <id> '\t' "escaped horizontal tab"
2827%token <id> '\f' "escaped form feed"
2828%token <id> '\r' "escaped carriage return"
2829%token <id> '\13' "escaped vertical tab"
2830%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2831%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2832%token tPOW RUBY_TOKEN(POW) "**"
2833%token tCMP RUBY_TOKEN(CMP) "<=>"
2834%token tEQ RUBY_TOKEN(EQ) "=="
2835%token tEQQ RUBY_TOKEN(EQQ) "==="
2836%token tNEQ RUBY_TOKEN(NEQ) "!="
2837%token tGEQ RUBY_TOKEN(GEQ) ">="
2838%token tLEQ RUBY_TOKEN(LEQ) "<="
2839%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2840%token tOROP RUBY_TOKEN(OROP) "||"
2841%token tMATCH RUBY_TOKEN(MATCH) "=~"
2842%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2843%token tDOT2 RUBY_TOKEN(DOT2) ".."
2844%token tDOT3 RUBY_TOKEN(DOT3) "..."
2845%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2846%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2847%token tAREF RUBY_TOKEN(AREF) "[]"
2848%token tASET RUBY_TOKEN(ASET) "[]="
2849%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2850%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2851%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2852%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2853%token tCOLON3 ":: at EXPR_BEG"
2854%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2855%token tASSOC "=>"
2856%token tLPAREN "("
2857%token tLPAREN_ARG "( arg"
2858%token tLBRACK "["
2859%token tLBRACE "{"
2860%token tLBRACE_ARG "{ arg"
2861%token tSTAR "*"
2862%token tDSTAR "**arg"
2863%token tAMPER "&"
2864%token <num> tLAMBDA "->"
2865%token tSYMBEG "symbol literal"
2866%token tSTRING_BEG "string literal"
2867%token tXSTRING_BEG "backtick literal"
2868%token tREGEXP_BEG "regexp literal"
2869%token tWORDS_BEG "word list"
2870%token tQWORDS_BEG "verbatim word list"
2871%token tSYMBOLS_BEG "symbol list"
2872%token tQSYMBOLS_BEG "verbatim symbol list"
2873%token tSTRING_END "terminator"
2874%token tSTRING_DEND "'}'"
2875%token <state> tSTRING_DBEG "'#{'"
2876%token tSTRING_DVAR tLAMBEG tLABEL_END
2877
2878%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2879%token tHEREDOC_BEG tHEREDOC_END k__END__
2880
2881/*
2882 * precedence table
2883 */
2884
2885%nonassoc tLOWEST
2886%nonassoc tLBRACE_ARG
2887
2888%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2889%left keyword_or keyword_and
2890%right keyword_not
2891%nonassoc keyword_defined
2892%right '=' tOP_ASGN
2893%left modifier_rescue
2894%right '?' ':'
2895%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2896%left tOROP
2897%left tANDOP
2898%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2899%left '>' tGEQ '<' tLEQ
2900%left '|' '^'
2901%left '&'
2902%left tLSHFT tRSHFT
2903%left '+' '-'
2904%left '*' '/' '%'
2905%right tUMINUS_NUM tUMINUS
2906%right tPOW
2907%right '!' '~' tUPLUS
2908
2909%token tLAST_TOKEN
2910
2911/*
2912 * inlining rules
2913 */
2914%rule %inline ident_or_const
2915 : tIDENTIFIER
2916 | tCONSTANT
2917 ;
2918
2919%rule %inline user_or_keyword_variable
2920 : user_variable
2921 | keyword_variable
2922 ;
2923
2924/*
2925 * parameterizing rules
2926 */
2927%rule asgn(rhs) <node>
2928 : lhs '=' lex_ctxt rhs
2929 {
2930 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2931 /*% ripper: assign!($:1, $:4) %*/
2932 }
2933 ;
2934
2935%rule args_tail_basic(value) <node_args>
2936 : f_kwarg(value) ',' f_kwrest opt_f_block_arg
2937 {
2938 $$ = new_args_tail(p, $1, $3, $4, &@3);
2939 /*% ripper: [$:1, $:3, $:4] %*/
2940 }
2941 | f_kwarg(value) opt_f_block_arg
2942 {
2943 $$ = new_args_tail(p, $1, 0, $2, &@1);
2944 /*% ripper: [$:1, Qnil, $:2] %*/
2945 }
2946 | f_any_kwrest opt_f_block_arg
2947 {
2948 $$ = new_args_tail(p, 0, $1, $2, &@1);
2949 /*% ripper: [Qnil, $:1, $:2] %*/
2950 }
2951 | f_block_arg
2952 {
2953 $$ = new_args_tail(p, 0, 0, $1, &@1);
2954 /*% ripper: [Qnil, Qnil, $:1] %*/
2955 }
2956 ;
2957
2958%rule def_endless_method(bodystmt) <node>
2959 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2960 {
2961 endless_method_name(p, $head->nd_mid, &@head);
2962 restore_defun(p, $head);
2963 ($$ = $head->nd_def)->nd_loc = @$;
2964 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2965 RNODE_DEFN($$)->nd_defn = $bodystmt;
2966 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2967 /*% ripper: def!($:head, $:args, $:$) %*/
2968 local_pop(p);
2969 }
2970 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2971 {
2972 endless_method_name(p, $head->nd_mid, &@head);
2973 restore_defun(p, $head);
2974 ($$ = $head->nd_def)->nd_loc = @$;
2975 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2976 RNODE_DEFS($$)->nd_defn = $bodystmt;
2977 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2978 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2979 local_pop(p);
2980 }
2981 ;
2982
2983%rule compstmt(stmts) <node>
2984 : stmts terms?
2985 {
2986 void_stmts(p, $$ = $stmts);
2987 }
2988 ;
2989
2990%rule f_opt(value) <node_opt_arg>
2991 : f_arg_asgn f_eq value
2992 {
2993 p->ctxt.in_argdef = 1;
2994 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2995 /*% ripper: [$:$, $:3] %*/
2996 }
2997 ;
2998
2999%rule f_opt_arg(value) <node_opt_arg>
3000 : f_opt(value)
3001 {
3002 $$ = $f_opt;
3003 /*% ripper: rb_ary_new3(1, $:1) %*/
3004 }
3005 | f_opt_arg(value) ',' f_opt(value)
3006 {
3007 $$ = opt_arg_append($f_opt_arg, $f_opt);
3008 /*% ripper: rb_ary_push($:1, $:3) %*/
3009 }
3010 ;
3011
3012%rule f_kw(value) <node_kw_arg>
3013 : f_label value
3014 {
3015 p->ctxt.in_argdef = 1;
3016 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3017 /*% ripper: [$:$, $:value] %*/
3018 }
3019 | f_label
3020 {
3021 p->ctxt.in_argdef = 1;
3022 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3023 /*% ripper: [$:$, 0] %*/
3024 }
3025 ;
3026
3027%rule f_kwarg(value) <node_kw_arg>
3028 : f_kw(value)
3029 {
3030 $$ = $f_kw;
3031 /*% ripper: rb_ary_new3(1, $:1) %*/
3032 }
3033 | f_kwarg(value) ',' f_kw(value)
3034 {
3035 $$ = kwd_append($f_kwarg, $f_kw);
3036 /*% ripper: rb_ary_push($:1, $:3) %*/
3037 }
3038 ;
3039
3040%rule mlhs_items(item) <node>
3041 : item
3042 {
3043 $$ = NEW_LIST($1, &@$);
3044 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3045 }
3046 | mlhs_items(item) ',' item
3047 {
3048 $$ = list_append(p, $1, $3);
3049 /*% ripper: mlhs_add!($:1, $:3) %*/
3050 }
3051 ;
3052
3053%rule op_asgn(rhs) <node>
3054 : var_lhs tOP_ASGN lex_ctxt rhs
3055 {
3056 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3057 /*% ripper: opassign!($:1, $:2, $:4) %*/
3058 }
3059 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3060 {
3061 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3062 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3063 }
3064 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3065 {
3066 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3067 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3068 }
3069 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3070 {
3071 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3072 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3073 }
3074 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3075 {
3076 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3077 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3078 }
3079 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3080 {
3081 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3082 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3083 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3084 }
3085 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3086 {
3087 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3088 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3089 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3090 }
3091 | backref tOP_ASGN lex_ctxt rhs
3092 {
3093 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3094 $$ = NEW_ERROR(&@$);
3095 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3096 }
3097 ;
3098
3099%rule opt_args_tail(tail) <node_args>
3100 : ',' tail
3101 {
3102 $$ = $tail;
3103 /*% ripper: $:2 %*/
3104 }
3105 | /* none */
3106 {
3107 $$ = new_args_tail(p, 0, 0, 0, &@0);
3108 /*% ripper: [Qnil, Qnil, Qnil] %*/
3109 }
3110 ;
3111
3112%rule range_expr(range) <node>
3113 : range tDOT2 range
3114 {
3115 value_expr(p, $1);
3116 value_expr(p, $3);
3117 $$ = NEW_DOT2($1, $3, &@$, &@2);
3118 /*% ripper: dot2!($:1, $:3) %*/
3119 }
3120 | range tDOT3 range
3121 {
3122 value_expr(p, $1);
3123 value_expr(p, $3);
3124 $$ = NEW_DOT3($1, $3, &@$, &@2);
3125 /*% ripper: dot3!($:1, $:3) %*/
3126 }
3127 | range tDOT2
3128 {
3129 value_expr(p, $1);
3130 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3131 /*% ripper: dot2!($:1, Qnil) %*/
3132 }
3133 | range tDOT3
3134 {
3135 value_expr(p, $1);
3136 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3137 /*% ripper: dot3!($:1, Qnil) %*/
3138 }
3139 | tBDOT2 range
3140 {
3141 value_expr(p, $2);
3142 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3143 /*% ripper: dot2!(Qnil, $:2) %*/
3144 }
3145 | tBDOT3 range
3146 {
3147 value_expr(p, $2);
3148 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3149 /*% ripper: dot3!(Qnil, $:2) %*/
3150 }
3151 ;
3152
3153%rule value_expr(value) <node>
3154 : value
3155 {
3156 value_expr(p, $1);
3157 $$ = $1;
3158 }
3159 ;
3160
3161%rule words(begin, word_list) <node>
3162 : begin ' '+ word_list tSTRING_END
3163 {
3164 $$ = make_list($word_list, &@$);
3165 /*% ripper: array!($:3) %*/
3166 }
3167 ;
3168
3169%%
3170program : {
3171 SET_LEX_STATE(EXPR_BEG);
3172 local_push(p, ifndef_ripper(1)+0);
3173 /* jumps are possible in the top-level loop. */
3174 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3175 }
3176 compstmt(top_stmts)
3177 {
3178 if ($2 && !compile_for_eval) {
3179 NODE *node = $2;
3180 /* last expression should not be void */
3181 if (nd_type_p(node, NODE_BLOCK)) {
3182 while (RNODE_BLOCK(node)->nd_next) {
3183 node = RNODE_BLOCK(node)->nd_next;
3184 }
3185 node = RNODE_BLOCK(node)->nd_head;
3186 }
3187 node = remove_begin(node);
3188 void_expr(p, node);
3189 }
3190 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3191 /*% ripper[final]: program!($:2) %*/
3192 local_pop(p);
3193 }
3194 ;
3195
3196top_stmts : none
3197 {
3198 $$ = NEW_BEGIN(0, &@$);
3199 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3200 }
3201 | top_stmt
3202 {
3203 $$ = newline_node($1);
3204 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3205 }
3206 | top_stmts terms top_stmt
3207 {
3208 $$ = block_append(p, $1, newline_node($3));
3209 /*% ripper: stmts_add!($:1, $:3) %*/
3210 }
3211 ;
3212
3213top_stmt : stmt
3214 {
3215 clear_block_exit(p, true);
3216 $$ = $1;
3217 }
3218 | keyword_BEGIN begin_block
3219 {
3220 $$ = $2;
3221 /*% ripper: $:2 %*/
3222 }
3223 ;
3224
3225block_open : '{' {$$ = init_block_exit(p);};
3226
3227begin_block : block_open compstmt(top_stmts) '}'
3228 {
3229 restore_block_exit(p, $block_open);
3230 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3231 NEW_BEGIN($2, &@$));
3232 $$ = NEW_BEGIN(0, &@$);
3233 /*% ripper: BEGIN!($:2) %*/
3234 }
3235 ;
3236
3237bodystmt : compstmt(stmts)[body]
3238 lex_ctxt[ctxt]
3239 opt_rescue
3240 k_else
3241 {
3242 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3243 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3244 }
3245 compstmt(stmts)[elsebody]
3246 {
3247 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3248 }
3249 opt_ensure
3250 {
3251 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3252 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3253 }
3254 | compstmt(stmts)[body]
3255 lex_ctxt[ctxt]
3256 opt_rescue
3257 {
3258 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3259 }
3260 opt_ensure
3261 {
3262 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3263 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3264 }
3265 ;
3266
3267stmts : none
3268 {
3269 $$ = NEW_BEGIN(0, &@$);
3270 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3271 }
3272 | stmt_or_begin
3273 {
3274 $$ = newline_node($1);
3275 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3276 }
3277 | stmts terms stmt_or_begin
3278 {
3279 $$ = block_append(p, $1, newline_node($3));
3280 /*% ripper: stmts_add!($:1, $:3) %*/
3281 }
3282 ;
3283
3284stmt_or_begin : stmt
3285 | keyword_BEGIN
3286 {
3287 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3288 }
3289 begin_block
3290 {
3291 $$ = $3;
3292 }
3293 ;
3294
3295allow_exits : {$$ = allow_block_exit(p);};
3296
3297k_END : keyword_END lex_ctxt
3298 {
3299 $$ = $2;
3300 p->ctxt.in_rescue = before_rescue;
3301 /*% ripper: $:2 %*/
3302 };
3303
3304stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3305 {
3306 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3307 /*% ripper: alias!($:2, $:4) %*/
3308 }
3309 | keyword_alias tGVAR tGVAR
3310 {
3311 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3312 /*% ripper: var_alias!($:2, $:3) %*/
3313 }
3314 | keyword_alias tGVAR tBACK_REF
3315 {
3316 char buf[2];
3317 buf[0] = '$';
3318 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3319 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3320 /*% ripper: var_alias!($:2, $:3) %*/
3321 }
3322 | keyword_alias tGVAR tNTH_REF
3323 {
3324 static const char mesg[] = "can't make alias for the number variables";
3325 /*%%%*/
3326 yyerror1(&@3, mesg);
3327 /*% %*/
3328 $$ = NEW_ERROR(&@$);
3329 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3330 }
3331 | keyword_undef undef_list
3332 {
3333 nd_set_first_loc($2, @1.beg_pos);
3334 RNODE_UNDEF($2)->keyword_loc = @1;
3335 $$ = $2;
3336 /*% ripper: undef!($:2) %*/
3337 }
3338 | stmt modifier_if expr_value
3339 {
3340 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3341 fixpos($$, $3);
3342 /*% ripper: if_mod!($:3, $:1) %*/
3343 }
3344 | stmt modifier_unless expr_value
3345 {
3346 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3347 fixpos($$, $3);
3348 /*% ripper: unless_mod!($:3, $:1) %*/
3349 }
3350 | stmt modifier_while expr_value
3351 {
3352 clear_block_exit(p, false);
3353 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3354 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3355 }
3356 else {
3357 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3358 }
3359 /*% ripper: while_mod!($:3, $:1) %*/
3360 }
3361 | stmt modifier_until expr_value
3362 {
3363 clear_block_exit(p, false);
3364 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3365 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3366 }
3367 else {
3368 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3369 }
3370 /*% ripper: until_mod!($:3, $:1) %*/
3371 }
3372 | stmt modifier_rescue after_rescue stmt
3373 {
3374 p->ctxt.in_rescue = $3.in_rescue;
3375 NODE *resq;
3376 YYLTYPE loc = code_loc_gen(&@2, &@4);
3377 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3378 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3379 /*% ripper: rescue_mod!($:1, $:4) %*/
3380 }
3381 | k_END allow_exits '{' compstmt(stmts) '}'
3382 {
3383 if (p->ctxt.in_def) {
3384 rb_warn0("END in method; use at_exit");
3385 }
3386 restore_block_exit(p, $allow_exits);
3387 p->ctxt = $k_END;
3388 {
3389 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, NULL /* parent */, &@$);
3390 $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
3391 RNODE_SCOPE(scope)->nd_parent = $$;
3392 }
3393 /*% ripper: END!($:compstmt) %*/
3394 }
3395 | command_asgn
3396 | mlhs '=' lex_ctxt command_call_value
3397 {
3398 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3399 /*% ripper: massign!($:1, $:4) %*/
3400 }
3401 | asgn(mrhs)
3402 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3403 after_rescue stmt[resbody]
3404 {
3405 p->ctxt.in_rescue = $3.in_rescue;
3406 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3407 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3408 loc.beg_pos = @mrhs_arg.beg_pos;
3409 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3410 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3411 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3412 }
3413 | mlhs '=' lex_ctxt mrhs_arg
3414 {
3415 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3416 /*% ripper: massign!($:1, $:4) %*/
3417 }
3418 | expr
3419 | error
3420 {
3421 (void)yynerrs;
3422 $$ = NEW_ERROR(&@$);
3423 }
3424 ;
3425
3426command_asgn : asgn(command_rhs)
3427 | op_asgn(command_rhs)
3428 | def_endless_method(endless_command)
3429 ;
3430
3431endless_command : command
3432 | endless_command modifier_rescue after_rescue arg
3433 {
3434 p->ctxt.in_rescue = $3.in_rescue;
3435 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3436 /*% ripper: rescue_mod!($:1, $:4) %*/
3437 }
3438 | keyword_not '\n'? endless_command
3439 {
3440 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3441 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3442 }
3443 ;
3444
3445command_rhs : command_call_value %prec tOP_ASGN
3446 | command_call_value modifier_rescue after_rescue stmt
3447 {
3448 p->ctxt.in_rescue = $3.in_rescue;
3449 YYLTYPE loc = code_loc_gen(&@2, &@4);
3450 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3451 /*% ripper: rescue_mod!($:1, $:4) %*/
3452 }
3453 | command_asgn
3454 ;
3455
3456expr : command_call
3457 | expr keyword_and expr
3458 {
3459 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3460 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3461 }
3462 | expr keyword_or expr
3463 {
3464 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3465 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3466 }
3467 | keyword_not '\n'? expr
3468 {
3469 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3470 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3471 }
3472 | '!' command_call
3473 {
3474 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3475 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3476 }
3477 | arg tASSOC
3478 {
3479 value_expr(p, $arg);
3480 }
3481 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3482 p_top_expr_body[body]
3483 {
3484 pop_pktbl(p, $p_pktbl);
3485 pop_pvtbl(p, $p_pvtbl);
3486 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3487 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3488 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3489 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@2), &@$, &NULL_LOC, &NULL_LOC);
3490 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3491 }
3492 | arg keyword_in
3493 {
3494 value_expr(p, $arg);
3495 }
3496 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3497 p_top_expr_body[body]
3498 {
3499 pop_pktbl(p, $p_pktbl);
3500 pop_pvtbl(p, $p_pvtbl);
3501 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3502 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3503 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3504 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3505 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3506 }
3507 | arg %prec tLBRACE_ARG
3508 ;
3509
3510def_name : fname
3511 {
3512 numparam_name(p, $fname);
3513 local_push(p, 0);
3514 p->ctxt.in_def = 1;
3515 p->ctxt.in_rescue = before_rescue;
3516 p->ctxt.cant_return = 0;
3517 $$ = $fname;
3518 }
3519 ;
3520
3521defn_head : k_def def_name
3522 {
3523 $$ = def_head_save(p, $k_def);
3524 $$->nd_mid = $def_name;
3525 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3526 /*% ripper: $:def_name %*/
3527 }
3528 ;
3529
3530defs_head : k_def singleton dot_or_colon
3531 {
3532 SET_LEX_STATE(EXPR_FNAME);
3533 }
3534 def_name
3535 {
3536 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3537 $$ = def_head_save(p, $k_def);
3538 $$->nd_mid = $def_name;
3539 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3540 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3541 }
3542 ;
3543
3544expr_value : value_expr(expr)
3545 | error
3546 {
3547 $$ = NEW_ERROR(&@$);
3548 }
3549 ;
3550
3551expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3552 {
3553 $$ = $2;
3554 /*% ripper: $:2 %*/
3555 }
3556 ;
3557
3558command_call : command
3559 | block_command
3560 ;
3561
3562command_call_value : value_expr(command_call)
3563 ;
3564
3565block_command : block_call
3566 | block_call call_op2 operation2 command_args
3567 {
3568 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3569 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3570 }
3571 ;
3572
3573cmd_brace_block : tLBRACE_ARG brace_body '}'
3574 {
3575 $$ = $2;
3576 set_embraced_location($$, &@1, &@3);
3577 /*% ripper: $:2 %*/
3578 }
3579 ;
3580
3581fcall : operation
3582 {
3583 $$ = NEW_FCALL($1, 0, &@$);
3584 /*% ripper: $:1 %*/
3585 }
3586 ;
3587
3588command : fcall command_args %prec tLOWEST
3589 {
3590 $1->nd_args = $2;
3591 nd_set_last_loc($1, @2.end_pos);
3592 $$ = (NODE *)$1;
3593 /*% ripper: command!($:1, $:2) %*/
3594 }
3595 | fcall command_args cmd_brace_block
3596 {
3597 block_dup_check(p, $2, $3);
3598 $1->nd_args = $2;
3599 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3600 fixpos($$, RNODE($1));
3601 nd_set_last_loc($1, @2.end_pos);
3602 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3603 }
3604 | primary_value call_op operation2 command_args %prec tLOWEST
3605 {
3606 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3607 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3608 }
3609 | primary_value call_op operation2 command_args cmd_brace_block
3610 {
3611 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3612 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3613 }
3614 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3615 {
3616 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3617 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3618 }
3619 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3620 {
3621 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3622 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3623 }
3624 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3625 {
3626 set_embraced_location($5, &@4, &@6);
3627 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3628 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3629 }
3630 | keyword_super command_args
3631 {
3632 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3633 fixpos($$, $2);
3634 /*% ripper: super!($:2) %*/
3635 }
3636 | k_yield command_args
3637 {
3638 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3639 fixpos($$, $2);
3640 /*% ripper: yield!($:2) %*/
3641 }
3642 | k_return_arg call_args
3643 {
3644 CMDARG_POP();
3645 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3646 /*% ripper: return!($:2) %*/
3647 }
3648 | k_break_arg call_args
3649 {
3650 CMDARG_POP();
3651 NODE *args = 0;
3652 args = ret_args(p, $2);
3653 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3654 /*% ripper: break!($:2) %*/
3655 }
3656 | k_next_arg call_args
3657 {
3658 CMDARG_POP();
3659 NODE *args = 0;
3660 args = ret_args(p, $2);
3661 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3662 /*% ripper: next!($:2) %*/
3663 }
3664 ;
3665
3666mlhs : mlhs_basic
3667 | tLPAREN mlhs_inner rparen
3668 {
3669 $$ = $2;
3670 /*% ripper: mlhs_paren!($:2) %*/
3671 }
3672 ;
3673
3674mlhs_inner : mlhs_basic
3675 | tLPAREN mlhs_inner rparen
3676 {
3677 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3678 /*% ripper: mlhs_paren!($:2) %*/
3679 }
3680 ;
3681
3682mlhs_basic : mlhs_head
3683 {
3684 $$ = NEW_MASGN($1, 0, &@$);
3685 /*% ripper: $:1 %*/
3686 }
3687 | mlhs_head mlhs_item
3688 {
3689 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3690 /*% ripper: mlhs_add!($:1, $:2) %*/
3691 }
3692 | mlhs_head tSTAR mlhs_node
3693 {
3694 $$ = NEW_MASGN($1, $3, &@$);
3695 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3696 }
3697 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3698 {
3699 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3700 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3701 }
3702 | mlhs_head tSTAR
3703 {
3704 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3705 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3706 }
3707 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3708 {
3709 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3710 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3711 }
3712 | tSTAR mlhs_node
3713 {
3714 $$ = NEW_MASGN(0, $2, &@$);
3715 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3716 }
3717 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3718 {
3719 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3720 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3721 }
3722 | tSTAR
3723 {
3724 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3725 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3726 }
3727 | tSTAR ',' mlhs_items(mlhs_item)
3728 {
3729 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3730 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3731 }
3732 ;
3733
3734mlhs_item : mlhs_node
3735 | tLPAREN mlhs_inner rparen
3736 {
3737 $$ = (NODE *)$2;
3738 /*% ripper: mlhs_paren!($:2) %*/
3739 }
3740 ;
3741
3742mlhs_head : mlhs_item ','
3743 {
3744 $$ = NEW_LIST($1, &@1);
3745 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3746 }
3747 | mlhs_head mlhs_item ','
3748 {
3749 $$ = list_append(p, $1, $2);
3750 /*% ripper: mlhs_add!($:1, $:2) %*/
3751 }
3752 ;
3753
3754
3755mlhs_node : user_or_keyword_variable
3756 {
3757 /*% ripper: var_field!($:1) %*/
3758 $$ = assignable(p, $1, 0, &@$);
3759 }
3760 | primary_value '[' opt_call_args rbracket
3761 {
3762 $$ = aryset(p, $1, $3, &@$);
3763 /*% ripper: aref_field!($:1, $:3) %*/
3764 }
3765 | primary_value call_op ident_or_const
3766 {
3767 anddot_multiple_assignment_check(p, &@2, $2);
3768 $$ = attrset(p, $1, $2, $3, &@$);
3769 /*% ripper: field!($:1, $:2, $:3) %*/
3770 }
3771 | primary_value tCOLON2 tIDENTIFIER
3772 {
3773 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3774 /*% ripper: const_path_field!($:1, $:3) %*/
3775 }
3776 | primary_value tCOLON2 tCONSTANT
3777 {
3778 /*% ripper: const_path_field!($:1, $:3) %*/
3779 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3780 }
3781 | tCOLON3 tCONSTANT
3782 {
3783 /*% ripper: top_const_field!($:2) %*/
3784 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3785 }
3786 | backref
3787 {
3788 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3789 $$ = NEW_ERROR(&@$);
3790 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3791 }
3792 ;
3793
3794lhs : user_or_keyword_variable
3795 {
3796 /*% ripper: var_field!($:1) %*/
3797 $$ = assignable(p, $1, 0, &@$);
3798 }
3799 | primary_value '[' opt_call_args rbracket
3800 {
3801 $$ = aryset(p, $1, $3, &@$);
3802 /*% ripper: aref_field!($:1, $:3) %*/
3803 }
3804 | primary_value call_op ident_or_const
3805 {
3806 $$ = attrset(p, $1, $2, $3, &@$);
3807 /*% ripper: field!($:1, $:2, $:3) %*/
3808 }
3809 | primary_value tCOLON2 tIDENTIFIER
3810 {
3811 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3812 /*% ripper: field!($:1, $:2, $:3) %*/
3813 }
3814 | primary_value tCOLON2 tCONSTANT
3815 {
3816 /*% ripper: const_path_field!($:1, $:3) %*/
3817 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3818 }
3819 | tCOLON3 tCONSTANT
3820 {
3821 /*% ripper: top_const_field!($:2) %*/
3822 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3823 }
3824 | backref
3825 {
3826 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3827 $$ = NEW_ERROR(&@$);
3828 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3829 }
3830 ;
3831
3832cname : tIDENTIFIER
3833 {
3834 static const char mesg[] = "class/module name must be CONSTANT";
3835 /*%%%*/
3836 yyerror1(&@1, mesg);
3837 /*% %*/
3838 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3839 }
3840 | tCONSTANT
3841 ;
3842
3843cpath : tCOLON3 cname
3844 {
3845 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3846 /*% ripper: top_const_ref!($:2) %*/
3847 }
3848 | cname
3849 {
3850 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3851 /*% ripper: const_ref!($:1) %*/
3852 }
3853 | primary_value tCOLON2 cname
3854 {
3855 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3856 /*% ripper: const_path_ref!($:1, $:3) %*/
3857 }
3858 ;
3859
3860fname : operation
3861 | op
3862 {
3863 SET_LEX_STATE(EXPR_ENDFN);
3864 $$ = $1;
3865 }
3866 | reswords
3867 ;
3868
3869fitem : fname
3870 {
3871 $$ = NEW_SYM(rb_id2str($1), &@$);
3872 /*% ripper: symbol_literal!($:1) %*/
3873 }
3874 | symbol
3875 ;
3876
3877undef_list : fitem
3878 {
3879 $$ = NEW_UNDEF($1, &@$);
3880 /*% ripper: rb_ary_new3(1, $:1) %*/
3881 }
3882 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3883 {
3884 nd_set_last_loc($1, @4.end_pos);
3885 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3886 /*% ripper: rb_ary_push($:1, $:4) %*/
3887 }
3888 ;
3889
3890op : '|' { $$ = '|'; }
3891 | '^' { $$ = '^'; }
3892 | '&' { $$ = '&'; }
3893 | tCMP { $$ = tCMP; }
3894 | tEQ { $$ = tEQ; }
3895 | tEQQ { $$ = tEQQ; }
3896 | tMATCH { $$ = tMATCH; }
3897 | tNMATCH { $$ = tNMATCH; }
3898 | '>' { $$ = '>'; }
3899 | tGEQ { $$ = tGEQ; }
3900 | '<' { $$ = '<'; }
3901 | tLEQ { $$ = tLEQ; }
3902 | tNEQ { $$ = tNEQ; }
3903 | tLSHFT { $$ = tLSHFT; }
3904 | tRSHFT { $$ = tRSHFT; }
3905 | '+' { $$ = '+'; }
3906 | '-' { $$ = '-'; }
3907 | '*' { $$ = '*'; }
3908 | tSTAR { $$ = '*'; }
3909 | '/' { $$ = '/'; }
3910 | '%' { $$ = '%'; }
3911 | tPOW { $$ = tPOW; }
3912 | tDSTAR { $$ = tDSTAR; }
3913 | '!' { $$ = '!'; }
3914 | '~' { $$ = '~'; }
3915 | tUPLUS { $$ = tUPLUS; }
3916 | tUMINUS { $$ = tUMINUS; }
3917 | tAREF { $$ = tAREF; }
3918 | tASET { $$ = tASET; }
3919 | '`' { $$ = '`'; }
3920 ;
3921
3922reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3923 | keyword_BEGIN | keyword_END
3924 | keyword_alias | keyword_and | keyword_begin
3925 | keyword_break | keyword_case | keyword_class | keyword_def
3926 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3927 | keyword_end | keyword_ensure | keyword_false
3928 | keyword_for | keyword_in | keyword_module | keyword_next
3929 | keyword_nil | keyword_not | keyword_or | keyword_redo
3930 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3931 | keyword_super | keyword_then | keyword_true | keyword_undef
3932 | keyword_when | keyword_yield | keyword_if | keyword_unless
3933 | keyword_while | keyword_until
3934 ;
3935
3936arg : asgn(arg_rhs)
3937 | op_asgn(arg_rhs)
3938 | range_expr(arg)
3939 | arg '+' arg
3940 {
3941 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3942 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3943 }
3944 | arg '-' arg
3945 {
3946 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3947 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3948 }
3949 | arg '*' arg
3950 {
3951 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3952 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3953 }
3954 | arg '/' arg
3955 {
3956 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3957 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3958 }
3959 | arg '%' arg
3960 {
3961 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3962 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3963 }
3964 | arg tPOW arg
3965 {
3966 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3967 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3968 }
3969 | tUMINUS_NUM simple_numeric tPOW arg
3970 {
3971 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3972 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3973 }
3974 | tUPLUS arg
3975 {
3976 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3977 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3978 }
3979 | tUMINUS arg
3980 {
3981 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3982 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3983 }
3984 | arg '|' arg
3985 {
3986 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3987 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3988 }
3989 | arg '^' arg
3990 {
3991 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3992 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3993 }
3994 | arg '&' arg
3995 {
3996 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3997 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3998 }
3999 | arg tCMP arg
4000 {
4001 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
4002 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
4003 }
4004 | rel_expr %prec tCMP
4005 | arg tEQ arg
4006 {
4007 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4008 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4009 }
4010 | arg tEQQ arg
4011 {
4012 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4013 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4014 }
4015 | arg tNEQ arg
4016 {
4017 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4018 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4019 }
4020 | arg tMATCH arg
4021 {
4022 $$ = match_op(p, $1, $3, &@2, &@$);
4023 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4024 }
4025 | arg tNMATCH arg
4026 {
4027 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4028 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4029 }
4030 | '!' arg
4031 {
4032 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4033 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4034 }
4035 | '~' arg
4036 {
4037 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4038 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4039 }
4040 | arg tLSHFT arg
4041 {
4042 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4043 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4044 }
4045 | arg tRSHFT arg
4046 {
4047 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4048 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4049 }
4050 | arg tANDOP arg
4051 {
4052 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4053 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4054 }
4055 | arg tOROP arg
4056 {
4057 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4058 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4059 }
4060 | keyword_defined '\n'? begin_defined arg
4061 {
4062 p->ctxt.in_defined = $3.in_defined;
4063 $$ = new_defined(p, $4, &@$, &@1);
4064 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4065 /*% ripper: defined!($:4) %*/
4066 }
4067 | def_endless_method(endless_arg)
4068 | ternary
4069 | primary
4070 ;
4071
4072ternary : arg '?' arg '\n'? ':' arg
4073 {
4074 value_expr(p, $1);
4075 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4076 fixpos($$, $1);
4077 /*% ripper: ifop!($:1, $:3, $:6) %*/
4078 }
4079 ;
4080
4081endless_arg : arg %prec modifier_rescue
4082 | endless_arg modifier_rescue after_rescue arg
4083 {
4084 p->ctxt.in_rescue = $3.in_rescue;
4085 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4086 /*% ripper: rescue_mod!($:1, $:4) %*/
4087 }
4088 | keyword_not '\n'? endless_arg
4089 {
4090 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4091 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4092 }
4093 ;
4094
4095relop : '>' {$$ = '>';}
4096 | '<' {$$ = '<';}
4097 | tGEQ {$$ = idGE;}
4098 | tLEQ {$$ = idLE;}
4099 ;
4100
4101rel_expr : arg relop arg %prec '>'
4102 {
4103 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4104 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4105 }
4106 | rel_expr relop arg %prec '>'
4107 {
4108 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4109 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4110 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4111 }
4112 ;
4113
4114lex_ctxt : none
4115 {
4116 $$ = p->ctxt;
4117 }
4118 ;
4119
4120begin_defined : lex_ctxt
4121 {
4122 p->ctxt.in_defined = 1;
4123 $$ = $1;
4124 }
4125 ;
4126
4127after_rescue : lex_ctxt
4128 {
4129 p->ctxt.in_rescue = after_rescue;
4130 $$ = $1;
4131 }
4132 ;
4133
4134arg_value : value_expr(arg)
4135 ;
4136
4137aref_args : none
4138 | args trailer
4139 | args ',' assocs trailer
4140 {
4141 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4142 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4143 }
4144 | assocs trailer
4145 {
4146 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4147 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4148 }
4149 ;
4150
4151arg_rhs : arg %prec tOP_ASGN
4152 {
4153 value_expr(p, $1);
4154 $$ = $1;
4155 }
4156 | arg modifier_rescue after_rescue arg
4157 {
4158 p->ctxt.in_rescue = $3.in_rescue;
4159 value_expr(p, $1);
4160 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4161 /*% ripper: rescue_mod!($:1, $:4) %*/
4162 }
4163 ;
4164
4165paren_args : '(' opt_call_args rparen
4166 {
4167 $$ = $2;
4168 /*% ripper: arg_paren!($:2) %*/
4169 }
4170 | '(' args ',' args_forward rparen
4171 {
4172 if (!check_forwarding_args(p)) {
4173 $$ = 0;
4174 }
4175 else {
4176 $$ = new_args_forward_call(p, $2, &@4, &@$);
4177 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4178 }
4179 }
4180 | '(' args_forward rparen
4181 {
4182 if (!check_forwarding_args(p)) {
4183 $$ = 0;
4184 }
4185 else {
4186 $$ = new_args_forward_call(p, 0, &@2, &@$);
4187 /*% ripper: arg_paren!($:2) %*/
4188 }
4189 }
4190 ;
4191
4192opt_paren_args : none
4193 | paren_args
4194 {
4195 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4196 }
4197 ;
4198
4199opt_call_args : none
4200 | call_args
4201 | args ','
4202 | args ',' assocs ','
4203 {
4204 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4205 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4206 }
4207 | assocs ','
4208 {
4209 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4210 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4211 }
4212 ;
4213
4214call_args : value_expr(command)
4215 {
4216 $$ = NEW_LIST($1, &@$);
4217 /*% ripper: args_add!(args_new!, $:1) %*/
4218 }
4219 | def_endless_method(endless_command)
4220 {
4221 $$ = NEW_LIST($1, &@$);
4222 /*% ripper: args_add!(args_new!, $:1) %*/
4223 }
4224 | args opt_block_arg
4225 {
4226 $$ = arg_blk_pass($1, $2);
4227 /*% ripper: args_add_block!($:1, $:2) %*/
4228 }
4229 | assocs opt_block_arg
4230 {
4231 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4232 $$ = arg_blk_pass($$, $2);
4233 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4234 }
4235 | args ',' assocs opt_block_arg
4236 {
4237 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4238 $$ = arg_blk_pass($$, $4);
4239 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4240 }
4241 | block_arg
4242 /*% ripper: args_add_block!(args_new!, $:1) %*/
4243 ;
4244
4245command_args : {
4246 /* If call_args starts with a open paren '(' or '[',
4247 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4248 * but the push must be done after CMDARG_PUSH(1).
4249 * So this code makes them consistent by first cancelling
4250 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4251 * and finally redoing CMDARG_PUSH(0).
4252 */
4253 int lookahead = 0;
4254 switch (yychar) {
4255 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4256 lookahead = 1;
4257 }
4258 if (lookahead) CMDARG_POP();
4259 CMDARG_PUSH(1);
4260 if (lookahead) CMDARG_PUSH(0);
4261 }
4262 call_args
4263 {
4264 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4265 * but the push must be done after CMDARG_POP() in the parser.
4266 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4267 * CMDARG_POP() to pop 1 pushed by command_args,
4268 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4269 */
4270 int lookahead = 0;
4271 switch (yychar) {
4272 case tLBRACE_ARG:
4273 lookahead = 1;
4274 }
4275 if (lookahead) CMDARG_POP();
4276 CMDARG_POP();
4277 if (lookahead) CMDARG_PUSH(0);
4278 $$ = $2;
4279 /*% ripper: $:2 %*/
4280 }
4281 ;
4282
4283block_arg : tAMPER arg_value
4284 {
4285 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4286 /*% ripper: $:2 %*/
4287 }
4288 | tAMPER
4289 {
4290 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4291 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4292 /*% ripper: Qnil %*/
4293 }
4294 ;
4295
4296opt_block_arg : ',' block_arg
4297 {
4298 $$ = $2;
4299 /*% ripper: $:2 %*/
4300 }
4301 | none
4302 {
4303 $$ = 0;
4304 /*% ripper: Qfalse %*/
4305 }
4306 ;
4307
4308/* value */
4309args : arg_value
4310 {
4311 $$ = NEW_LIST($arg_value, &@$);
4312 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4313 }
4314 | arg_splat
4315 {
4316 $$ = $arg_splat;
4317 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4318 }
4319 | args[non_last_args] ',' arg_value
4320 {
4321 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4322 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4323 }
4324 | args[non_last_args] ',' arg_splat
4325 {
4326 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4327 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4328 }
4329 ;
4330
4331/* value */
4332arg_splat : tSTAR arg_value
4333 {
4334 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4335 /*% ripper: $:arg_value %*/
4336 }
4337 | tSTAR /* none */
4338 {
4339 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4340 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4341 /*% ripper: Qnil %*/
4342 }
4343 ;
4344
4345/* value */
4346mrhs_arg : mrhs
4347 | arg_value
4348 ;
4349
4350/* value */
4351mrhs : args ',' arg_value
4352 {
4353 $$ = last_arg_append(p, $args, $arg_value, &@$);
4354 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4355 }
4356 | args ',' tSTAR arg_value
4357 {
4358 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4359 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4360 }
4361 | tSTAR arg_value
4362 {
4363 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4364 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4365 }
4366 ;
4367
4368%rule %inline inline_primary
4369 : literal
4370 | strings
4371 | xstring
4372 | regexp
4373 | words
4374 | qwords
4375 | symbols
4376 | qsymbols
4377 ;
4378
4379primary : inline_primary
4380 | var_ref
4381 | backref
4382 | tFID
4383 {
4384 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4385 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4386 }
4387 | k_begin
4388 {
4389 CMDARG_PUSH(0);
4390 }
4391 bodystmt
4392 k_end
4393 {
4394 CMDARG_POP();
4395 set_line_body($3, @1.end_pos.lineno);
4396 $$ = NEW_BEGIN($3, &@$);
4397 nd_set_line($$, @1.end_pos.lineno);
4398 /*% ripper: begin!($:3) %*/
4399 }
4400 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4401 {
4402 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4403 $$ = $2;
4404 /*% ripper: paren!($:2) %*/
4405 }
4406 | tLPAREN compstmt(stmts) ')'
4407 {
4408 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4409 $$ = NEW_BLOCK($2, &@$);
4410 /*% ripper: paren!($:2) %*/
4411 }
4412 | primary_value tCOLON2 tCONSTANT
4413 {
4414 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
4415 /*% ripper: const_path_ref!($:1, $:3) %*/
4416 }
4417 | tCOLON3 tCONSTANT
4418 {
4419 $$ = NEW_COLON3($2, &@$, &@1, &@2);
4420 /*% ripper: top_const_ref!($:2) %*/
4421 }
4422 | tLBRACK aref_args ']'
4423 {
4424 $$ = make_list($2, &@$);
4425 /*% ripper: array!($:2) %*/
4426 }
4427 | tLBRACE assoc_list '}'
4428 {
4429 $$ = new_hash(p, $2, &@$);
4430 RNODE_HASH($$)->nd_brace = TRUE;
4431 /*% ripper: hash!($:2) %*/
4432 }
4433 | k_return
4434 {
4435 $$ = NEW_RETURN(0, &@$, &@1);
4436 /*% ripper: return0! %*/
4437 }
4438 | k_yield '(' call_args rparen
4439 {
4440 $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4);
4441 /*% ripper: yield!(paren!($:3)) %*/
4442 }
4443 | k_yield '(' rparen
4444 {
4445 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4446 /*% ripper: yield!(paren!(args_new!)) %*/
4447 }
4448 | k_yield
4449 {
4450 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4451 /*% ripper: yield0! %*/
4452 }
4453 | keyword_defined '\n'? '(' begin_defined expr rparen
4454 {
4455 p->ctxt.in_defined = $4.in_defined;
4456 $$ = new_defined(p, $5, &@$, &@1);
4457 p->ctxt.has_trailing_semicolon = $4.has_trailing_semicolon;
4458 /*% ripper: defined!($:5) %*/
4459 }
4460 | keyword_not '(' expr rparen
4461 {
4462 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4463 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4464 }
4465 | keyword_not '(' rparen
4466 {
4467 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@2), &@2), METHOD_NOT, &@1, &@$);
4468 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4469 }
4470 | fcall brace_block
4471 {
4472 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4473 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4474 }
4475 | method_call
4476 | method_call brace_block
4477 {
4478 block_dup_check(p, get_nd_args(p, $1), $2);
4479 $$ = method_add_block(p, $1, $2, &@$);
4480 /*% ripper: method_add_block!($:1, $:2) %*/
4481 }
4482 | lambda
4483 | k_if expr_value then
4484 compstmt(stmts)
4485 if_tail
4486 k_end
4487 {
4488 if ($5 && nd_type_p($5, NODE_IF))
4489 RNODE_IF($5)->end_keyword_loc = @6;
4490
4491 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4492 fixpos($$, $2);
4493 /*% ripper: if!($:2, $:4, $:5) %*/
4494 }
4495 | k_unless expr_value then
4496 compstmt(stmts)
4497 opt_else
4498 k_end
4499 {
4500 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4501 fixpos($$, $2);
4502 /*% ripper: unless!($:2, $:4, $:5) %*/
4503 }
4504 | k_while expr_value_do
4505 compstmt(stmts)
4506 k_end
4507 {
4508 restore_block_exit(p, $1);
4509 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4510 fixpos($$, $2);
4511 /*% ripper: while!($:2, $:3) %*/
4512 }
4513 | k_until expr_value_do
4514 compstmt(stmts)
4515 k_end
4516 {
4517 restore_block_exit(p, $1);
4518 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4519 fixpos($$, $2);
4520 /*% ripper: until!($:2, $:3) %*/
4521 }
4522 | k_case expr_value terms?
4523 {
4524 $$ = p->case_labels;
4525 p->case_labels = CHECK_LITERAL_WHEN;
4526 }<labels>
4527 case_body
4528 k_end
4529 {
4530 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4531 p->case_labels = $4;
4532 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4533 fixpos($$, $2);
4534 /*% ripper: case!($:2, $:5) %*/
4535 }
4536 | k_case terms?
4537 {
4538 $$ = p->case_labels;
4539 p->case_labels = 0;
4540 }<labels>
4541 case_body
4542 k_end
4543 {
4544 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4545 p->case_labels = $3;
4546 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4547 /*% ripper: case!(Qnil, $:4) %*/
4548 }
4549 | k_case expr_value terms?
4550 p_case_body
4551 k_end
4552 {
4553 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4554 /*% ripper: case!($:2, $:4) %*/
4555 }
4556 | k_for for_var keyword_in
4557 {COND_PUSH(1);} expr_value do {COND_POP();}
4558 compstmt(stmts)
4559 k_end
4560 {
4561 restore_block_exit(p, $k_for);
4562 /*
4563 * for a, b, c in e
4564 * #=>
4565 * e.each{|*x| a, b, c = x}
4566 *
4567 * for a in e
4568 * #=>
4569 * e.each{|x| a, = x}
4570 */
4571 ID id = internal_id(p);
4572 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4573 rb_node_args_t *args;
4574 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4575 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4576 tbl->ids[0] = id; /* internal id */
4577
4578 switch (nd_type($for_var)) {
4579 case NODE_LASGN:
4580 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4581 set_nd_value(p, $for_var, internal_var);
4582 id = 0;
4583 m->nd_plen = 1;
4584 m->nd_next = $for_var;
4585 break;
4586 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4587 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4588 break;
4589 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4590 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);
4591 }
4592 /* {|*internal_id| <m> = internal_id; ... } */
4593 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4594 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4595 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4596 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4597 RNODE_SCOPE(scope)->nd_parent = $$;
4598 fixpos($$, $for_var);
4599 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4600 }
4601 | k_class cpath superclass
4602 {
4603 begin_definition("class", &@k_class, &@cpath);
4604 }
4605 bodystmt
4606 k_end
4607 {
4608 YYLTYPE inheritance_operator_loc = NULL_LOC;
4609 if ($superclass) {
4610 inheritance_operator_loc = @superclass;
4611 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4612 }
4613 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4614 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4615 set_line_body($bodystmt, @superclass.end_pos.lineno);
4616 nd_set_line($$, @superclass.end_pos.lineno);
4617 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4618 local_pop(p);
4619 p->ctxt.in_class = $k_class.in_class;
4620 p->ctxt.cant_return = $k_class.cant_return;
4621 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4622 p->ctxt.in_rescue = $k_class.in_rescue;
4623 }
4624 | k_class tLSHFT expr_value
4625 {
4626 begin_definition("", &@k_class, &@tLSHFT);
4627 }
4628 term
4629 bodystmt
4630 k_end
4631 {
4632 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4633 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4634 set_line_body($bodystmt, nd_line($expr_value));
4635 fixpos($$, $expr_value);
4636 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4637 local_pop(p);
4638 p->ctxt.in_def = $k_class.in_def;
4639 p->ctxt.in_class = $k_class.in_class;
4640 p->ctxt.cant_return = $k_class.cant_return;
4641 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4642 p->ctxt.in_rescue = $k_class.in_rescue;
4643 }
4644 | k_module cpath
4645 {
4646 begin_definition("module", &@k_module, &@cpath);
4647 }
4648 bodystmt
4649 k_end
4650 {
4651 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4652 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4653 set_line_body($bodystmt, @cpath.end_pos.lineno);
4654 nd_set_line($$, @cpath.end_pos.lineno);
4655 /*% ripper: module!($:cpath, $:bodystmt) %*/
4656 local_pop(p);
4657 p->ctxt.in_class = $k_module.in_class;
4658 p->ctxt.cant_return = $k_module.cant_return;
4659 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4660 p->ctxt.in_rescue = $k_module.in_rescue;
4661 }
4662 | defn_head[head]
4663 f_arglist[args]
4664 {
4665 push_end_expect_token_locations(p, &@head.beg_pos);
4666 }
4667 bodystmt
4668 k_end
4669 {
4670 restore_defun(p, $head);
4671 ($$ = $head->nd_def)->nd_loc = @$;
4672 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4673 RNODE_DEFN($$)->nd_defn = $bodystmt;
4674 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4675 local_pop(p);
4676 }
4677 | defs_head[head]
4678 f_arglist[args]
4679 {
4680 push_end_expect_token_locations(p, &@head.beg_pos);
4681 }
4682 bodystmt
4683 k_end
4684 {
4685 restore_defun(p, $head);
4686 ($$ = $head->nd_def)->nd_loc = @$;
4687 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4688 RNODE_DEFS($$)->nd_defn = $bodystmt;
4689 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4690 local_pop(p);
4691 }
4692 | keyword_break
4693 {
4694 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4695 /*% ripper: break!(args_new!) %*/
4696 }
4697 | keyword_next
4698 {
4699 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4700 /*% ripper: next!(args_new!) %*/
4701 }
4702 | keyword_redo
4703 {
4704 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4705 /*% ripper: redo! %*/
4706 }
4707 | keyword_retry
4708 {
4709 if (!p->ctxt.in_defined) {
4710 switch (p->ctxt.in_rescue) {
4711 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4712 case after_rescue: /* ok */ break;
4713 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4714 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4715 }
4716 }
4717 $$ = NEW_RETRY(&@$);
4718 /*% ripper: retry! %*/
4719 }
4720 ;
4721
4722primary_value : value_expr(primary)
4723 ;
4724
4725k_begin : keyword_begin
4726 {
4727 token_info_push(p, "begin", &@$);
4728 push_end_expect_token_locations(p, &@1.beg_pos);
4729 }
4730 ;
4731
4732k_if : keyword_if
4733 {
4734 WARN_EOL("if");
4735 token_info_push(p, "if", &@$);
4736 if (p->token_info && p->token_info->nonspc &&
4737 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4738 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4739 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4740 beg += rb_strlen_lit("else");
4741 while (beg < tok && ISSPACE(*beg)) beg++;
4742 if (beg == tok) {
4743 p->token_info->nonspc = 0;
4744 }
4745 }
4746 push_end_expect_token_locations(p, &@1.beg_pos);
4747 }
4748 ;
4749
4750k_unless : keyword_unless
4751 {
4752 token_info_push(p, "unless", &@$);
4753 push_end_expect_token_locations(p, &@1.beg_pos);
4754 }
4755 ;
4756
4757k_while : keyword_while allow_exits
4758 {
4759 $$ = $allow_exits;
4760 token_info_push(p, "while", &@$);
4761 push_end_expect_token_locations(p, &@1.beg_pos);
4762 }
4763 ;
4764
4765k_until : keyword_until allow_exits
4766 {
4767 $$ = $allow_exits;
4768 token_info_push(p, "until", &@$);
4769 push_end_expect_token_locations(p, &@1.beg_pos);
4770 }
4771 ;
4772
4773k_case : keyword_case
4774 {
4775 token_info_push(p, "case", &@$);
4776 push_end_expect_token_locations(p, &@1.beg_pos);
4777 }
4778 ;
4779
4780k_for : keyword_for allow_exits
4781 {
4782 $$ = $allow_exits;
4783 token_info_push(p, "for", &@$);
4784 push_end_expect_token_locations(p, &@1.beg_pos);
4785 }
4786 ;
4787
4788k_class : keyword_class
4789 {
4790 token_info_push(p, "class", &@$);
4791 $$ = p->ctxt;
4792 p->ctxt.in_rescue = before_rescue;
4793 push_end_expect_token_locations(p, &@1.beg_pos);
4794 }
4795 ;
4796
4797k_module : keyword_module
4798 {
4799 token_info_push(p, "module", &@$);
4800 $$ = p->ctxt;
4801 p->ctxt.in_rescue = before_rescue;
4802 push_end_expect_token_locations(p, &@1.beg_pos);
4803 }
4804 ;
4805
4806k_def : keyword_def
4807 {
4808 token_info_push(p, "def", &@$);
4809 $$ = NEW_DEF_TEMP(&@$);
4810 p->ctxt.in_argdef = 1;
4811 }
4812 ;
4813
4814k_do : keyword_do
4815 {
4816 token_info_push(p, "do", &@$);
4817 push_end_expect_token_locations(p, &@1.beg_pos);
4818 }
4819 ;
4820
4821k_do_block : keyword_do_block
4822 {
4823 token_info_push(p, "do", &@$);
4824 push_end_expect_token_locations(p, &@1.beg_pos);
4825 }
4826 ;
4827
4828k_rescue : keyword_rescue
4829 {
4830 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4831 $$ = p->ctxt;
4832 p->ctxt.in_rescue = after_rescue;
4833 }
4834 ;
4835
4836k_ensure : keyword_ensure
4837 {
4838 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4839 $$ = p->ctxt;
4840 }
4841 ;
4842
4843k_when : keyword_when
4844 {
4845 token_info_warn(p, "when", p->token_info, 0, &@$);
4846 }
4847 ;
4848
4849k_else : keyword_else
4850 {
4851 token_info *ptinfo_beg = p->token_info;
4852 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4853 token_info_warn(p, "else", p->token_info, same, &@$);
4854 if (same) {
4855 token_info e;
4856 e.next = ptinfo_beg->next;
4857 e.token = "else";
4858 token_info_setup(&e, p->lex.pbeg, &@$);
4859 if (!e.nonspc) *ptinfo_beg = e;
4860 }
4861 }
4862 ;
4863
4864k_elsif : keyword_elsif
4865 {
4866 WARN_EOL("elsif");
4867 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4868 }
4869 ;
4870
4871k_end : keyword_end
4872 {
4873 token_info_pop(p, "end", &@$);
4874 pop_end_expect_token_locations(p);
4875 }
4876 | tDUMNY_END
4877 {
4878 compile_error(p, "syntax error, unexpected end-of-input");
4879 }
4880 ;
4881
4882k_return : keyword_return
4883 {
4884 if (p->ctxt.cant_return && !dyna_in_block(p))
4885 yyerror1(&@1, "Invalid return in class/module body");
4886 }
4887 ;
4888
4889k_return_arg : k_return
4890 {
4891 CMDARG_PUSH(0);
4892 }
4893 ;
4894
4895k_break_arg : keyword_break
4896 {
4897 CMDARG_PUSH(0);
4898 }
4899 ;
4900
4901k_next_arg : keyword_next
4902 {
4903 CMDARG_PUSH(0);
4904 }
4905 ;
4906
4907k_yield : keyword_yield
4908 {
4909 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4910 yyerror1(&@1, "Invalid yield");
4911 }
4912 ;
4913
4914then : term
4915 | keyword_then
4916 | term keyword_then
4917 ;
4918
4919do : term
4920 | keyword_do_cond { $$ = keyword_do_cond; }
4921 ;
4922
4923if_tail : opt_else
4924 | k_elsif expr_value then
4925 compstmt(stmts)
4926 if_tail
4927 {
4928 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4929 fixpos($$, $2);
4930 /*% ripper: elsif!($:2, $:4, $:5) %*/
4931 }
4932 ;
4933
4934opt_else : none
4935 | k_else compstmt(stmts)
4936 {
4937 $$ = $2;
4938 /*% ripper: else!($:2) %*/
4939 }
4940 ;
4941
4942for_var : lhs
4943 | mlhs
4944 ;
4945
4946f_marg : f_norm_arg
4947 {
4948 $$ = assignable(p, $1, 0, &@$);
4949 mark_lvar_used(p, $$);
4950 }
4951 | tLPAREN f_margs rparen
4952 {
4953 $$ = (NODE *)$2;
4954 /*% ripper: mlhs_paren!($:2) %*/
4955 }
4956 ;
4957
4958
4959f_margs : mlhs_items(f_marg)
4960 {
4961 $$ = NEW_MASGN($1, 0, &@$);
4962 /*% ripper: $:1 %*/
4963 }
4964 | mlhs_items(f_marg) ',' f_rest_marg
4965 {
4966 $$ = NEW_MASGN($1, $3, &@$);
4967 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4968 }
4969 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4970 {
4971 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4972 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4973 }
4974 | f_rest_marg
4975 {
4976 $$ = NEW_MASGN(0, $1, &@$);
4977 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4978 }
4979 | f_rest_marg ',' mlhs_items(f_marg)
4980 {
4981 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4982 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4983 }
4984 ;
4985
4986f_rest_marg : tSTAR f_norm_arg
4987 {
4988 /*% ripper: $:2 %*/
4989 $$ = assignable(p, $2, 0, &@$);
4990 mark_lvar_used(p, $$);
4991 }
4992 | tSTAR
4993 {
4994 $$ = NODE_SPECIAL_NO_NAME_REST;
4995 /*% ripper: Qnil %*/
4996 }
4997 ;
4998
4999f_any_kwrest : f_kwrest
5000 | f_no_kwarg
5001 {
5002 $$ = idNil;
5003 /*% ripper: ID2VAL(idNil) %*/
5004 }
5005 ;
5006
5007f_eq : {p->ctxt.in_argdef = 0;} '=';
5008
5009block_args_tail : args_tail_basic(primary_value)
5010 ;
5011
5012excessed_comma : ','
5013 {
5014 /* magic number for rest_id in iseq_set_arguments() */
5015 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5016 /*% ripper: excessed_comma! %*/
5017 }
5018 ;
5019
5020block_param : f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5021 {
5022 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
5023 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
5024 }
5025 | f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5026 {
5027 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5028 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
5029 }
5030 | f_arg ',' f_opt_arg(primary_value) opt_args_tail(block_args_tail)
5031 {
5032 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
5033 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
5034 }
5035 | f_arg ',' f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5036 {
5037 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5038 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5039 }
5040 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5041 {
5042 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5043 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5044 }
5045 | f_arg excessed_comma
5046 {
5047 $$ = new_args_tail(p, 0, 0, 0, &@2);
5048 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5049 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5050 }
5051 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5052 {
5053 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5054 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5055 }
5056 | f_arg opt_args_tail(block_args_tail)
5057 {
5058 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5059 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5060 }
5061 | f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5062 {
5063 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5064 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5065 }
5066 | f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5067 {
5068 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5069 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5070 }
5071 | f_opt_arg(primary_value) opt_args_tail(block_args_tail)
5072 {
5073 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5074 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5075 }
5076 | f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5077 {
5078 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5079 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5080 }
5081 | f_rest_arg opt_args_tail(block_args_tail)
5082 {
5083 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5084 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5085 }
5086 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5087 {
5088 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5089 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5090 }
5091 | block_args_tail
5092 {
5093 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5094 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5095 }
5096 ;
5097
5098opt_block_param_def : none
5099 | block_param_def
5100 {
5101 p->command_start = TRUE;
5102 }
5103 ;
5104
5105block_param_def : '|' opt_block_param opt_bv_decl '|'
5106 {
5107 p->max_numparam = ORDINAL_PARAM;
5108 p->ctxt.in_argdef = 0;
5109 $$ = $2;
5110 /*% ripper: block_var!($:2, $:3) %*/
5111 }
5112 ;
5113
5114opt_block_param : /* none */
5115 {
5116 $$ = 0;
5117 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5118 }
5119 | block_param
5120 ;
5121
5122opt_bv_decl : '\n'?
5123 {
5124 $$ = 0;
5125 /*% ripper: Qfalse %*/
5126 }
5127 | '\n'? ';' bv_decls '\n'?
5128 {
5129 $$ = 0;
5130 /*% ripper: $:3 %*/
5131 }
5132 ;
5133
5134bv_decls : bvar
5135 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5136 | bv_decls ',' bvar
5137 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5138 ;
5139
5140bvar : tIDENTIFIER
5141 {
5142 new_bv(p, $1);
5143 /*% ripper: $:1 %*/
5144 }
5145 | f_bad_arg
5146 ;
5147
5148max_numparam : {
5149 $$ = p->max_numparam;
5150 p->max_numparam = 0;
5151 }
5152 ;
5153
5154numparam : {
5155 $$ = numparam_push(p);
5156 }
5157 ;
5158
5159it_id : {
5160 $$ = p->it_id;
5161 p->it_id = 0;
5162 }
5163 ;
5164
5165lambda : tLAMBDA[lpar]
5166 {
5167 token_info_push(p, "->", &@1);
5168 $$ = dyna_push(p);
5169 }[dyna]<vars>
5170 max_numparam numparam it_id allow_exits
5171 f_larglist[args]
5172 {
5173 CMDARG_PUSH(0);
5174 }
5175 lambda_body[body]
5176 {
5177 int max_numparam = p->max_numparam;
5178 ID it_id = p->it_id;
5179 p->lex.lpar_beg = $lpar;
5180 p->max_numparam = $max_numparam;
5181 p->it_id = $it_id;
5182 restore_block_exit(p, $allow_exits);
5183 CMDARG_POP();
5184 $args = args_with_numbered(p, $args, max_numparam, it_id);
5185 {
5186 YYLTYPE loc = code_loc_gen(&@args, &@body);
5187 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5188 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5189 nd_set_line($$, @args.end_pos.lineno);
5190 nd_set_first_loc($$, @1.beg_pos);
5191 xfree($body);
5192 }
5193 /*% ripper: lambda!($:args, $:body) %*/
5194 numparam_pop(p, $numparam);
5195 dyna_pop(p, $dyna);
5196 }
5197 ;
5198
5199f_larglist : '(' f_args opt_bv_decl ')'
5200 {
5201 p->ctxt.in_argdef = 0;
5202 $$ = $f_args;
5203 p->max_numparam = ORDINAL_PARAM;
5204 /*% ripper: paren!($:2) %*/
5205 }
5206 | f_args
5207 {
5208 p->ctxt.in_argdef = 0;
5209 if (!args_info_empty_p(&$1->nd_ainfo))
5210 p->max_numparam = ORDINAL_PARAM;
5211 $$ = $f_args;
5212 }
5213 ;
5214
5215lambda_body : tLAMBEG compstmt(stmts) '}'
5216 {
5217 token_info_pop(p, "}", &@3);
5218 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5219 /*% ripper: $:2 %*/
5220 }
5221 | keyword_do_LAMBDA
5222 {
5223 push_end_expect_token_locations(p, &@1.beg_pos);
5224 }
5225 bodystmt k_end
5226 {
5227 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5228 /*% ripper: $:3 %*/
5229 }
5230 ;
5231
5232do_block : k_do_block do_body k_end
5233 {
5234 $$ = $2;
5235 set_embraced_location($$, &@1, &@3);
5236 /*% ripper: $:2 %*/
5237 }
5238 ;
5239
5240block_call : command do_block
5241 {
5242 $$ = command_add_block(p, $1, $2, &@$);
5243 fixpos($$, $1);
5244 /*% ripper: method_add_block!($:1, $:2) %*/
5245 }
5246 | block_call call_op2 operation2 opt_paren_args
5247 {
5248 bool has_args = $4 != 0;
5249 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5250 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5251 /*% ripper: call!($:1, $:2, $:3) %*/
5252 if (has_args) {
5253 /*% ripper: method_add_arg!($:$, $:4) %*/
5254 }
5255 }
5256 | block_call call_op2 operation2 opt_paren_args brace_block
5257 {
5258 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5259 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5260 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5261 }
5262 | block_call call_op2 operation2 command_args do_block
5263 {
5264 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5265 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5266 }
5267 | block_call call_op2 paren_args
5268 {
5269 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5270 nd_set_line($$, @2.end_pos.lineno);
5271 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5272 }
5273 ;
5274
5275method_call : fcall paren_args
5276 {
5277 $1->nd_args = $2;
5278 $$ = (NODE *)$1;
5279 nd_set_last_loc($1, @2.end_pos);
5280 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5281 }
5282 | primary_value call_op operation2 opt_paren_args
5283 {
5284 bool has_args = $4 != 0;
5285 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5286 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5287 nd_set_line($$, @3.end_pos.lineno);
5288 /*% ripper: call!($:1, $:2, $:3) %*/
5289 if (has_args) {
5290 /*% ripper: method_add_arg!($:$, $:4) %*/
5291 }
5292 }
5293 | primary_value tCOLON2 operation2 paren_args
5294 {
5295 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5296 nd_set_line($$, @3.end_pos.lineno);
5297 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5298 }
5299 | primary_value tCOLON2 operation3
5300 {
5301 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5302 /*% ripper: call!($:1, $:2, $:3) %*/
5303 }
5304 | primary_value call_op2 paren_args
5305 {
5306 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5307 nd_set_line($$, @2.end_pos.lineno);
5308 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5309 }
5310 | keyword_super paren_args
5311 {
5312 rb_code_location_t lparen_loc = @2;
5313 rb_code_location_t rparen_loc = @2;
5314 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5315 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5316
5317 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5318 /*% ripper: super!($:2) %*/
5319 }
5320 | keyword_super
5321 {
5322 $$ = NEW_ZSUPER(&@$);
5323 /*% ripper: zsuper! %*/
5324 }
5325 | primary_value '[' opt_call_args rbracket
5326 {
5327 $$ = NEW_CALL($1, tAREF, $3, &@$);
5328 fixpos($$, $1);
5329 /*% ripper: aref!($:1, $:3) %*/
5330 }
5331 ;
5332
5333brace_block : '{' brace_body '}'
5334 {
5335 $$ = $2;
5336 set_embraced_location($$, &@1, &@3);
5337 /*% ripper: $:2 %*/
5338 }
5339 | k_do do_body k_end
5340 {
5341 $$ = $2;
5342 set_embraced_location($$, &@1, &@3);
5343 /*% ripper: $:2 %*/
5344 }
5345 ;
5346
5347brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5348 max_numparam numparam it_id allow_exits
5349 opt_block_param_def[args] compstmt(stmts)
5350 {
5351 int max_numparam = p->max_numparam;
5352 ID it_id = p->it_id;
5353 p->max_numparam = $max_numparam;
5354 p->it_id = $it_id;
5355 $args = args_with_numbered(p, $args, max_numparam, it_id);
5356 $$ = NEW_ITER($args, $compstmt, &@$);
5357 /*% ripper: brace_block!($:args, $:compstmt) %*/
5358 restore_block_exit(p, $allow_exits);
5359 numparam_pop(p, $numparam);
5360 dyna_pop(p, $dyna);
5361 }
5362 ;
5363
5364do_body : {
5365 $$ = dyna_push(p);
5366 CMDARG_PUSH(0);
5367 }[dyna]<vars>
5368 max_numparam numparam it_id allow_exits
5369 opt_block_param_def[args] bodystmt
5370 {
5371 int max_numparam = p->max_numparam;
5372 ID it_id = p->it_id;
5373 p->max_numparam = $max_numparam;
5374 p->it_id = $it_id;
5375 $args = args_with_numbered(p, $args, max_numparam, it_id);
5376 $$ = NEW_ITER($args, $bodystmt, &@$);
5377 /*% ripper: do_block!($:args, $:bodystmt) %*/
5378 CMDARG_POP();
5379 restore_block_exit(p, $allow_exits);
5380 numparam_pop(p, $numparam);
5381 dyna_pop(p, $dyna);
5382 }
5383 ;
5384
5385case_args : arg_value
5386 {
5387 check_literal_when(p, $arg_value, &@arg_value);
5388 $$ = NEW_LIST($arg_value, &@$);
5389 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5390 }
5391 | tSTAR arg_value
5392 {
5393 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5394 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5395 }
5396 | case_args[non_last_args] ',' arg_value
5397 {
5398 check_literal_when(p, $arg_value, &@arg_value);
5399 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5400 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5401 }
5402 | case_args[non_last_args] ',' tSTAR arg_value
5403 {
5404 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5405 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5406 }
5407 ;
5408
5409case_body : k_when case_args then
5410 compstmt(stmts)
5411 cases
5412 {
5413 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5414 fixpos($$, $2);
5415 /*% ripper: when!($:2, $:4, $:5) %*/
5416 }
5417 ;
5418
5419cases : opt_else
5420 | case_body
5421 ;
5422
5423p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5424p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5425
5426p_in_kwarg : {
5427 $$ = p->ctxt;
5428 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5429 p->command_start = FALSE;
5430 p->ctxt.in_kwarg = 1;
5431 p->ctxt.in_alt_pattern = 0;
5432 p->ctxt.capture_in_pattern = 0;
5433 }
5434 ;
5435
5436p_case_body : keyword_in
5437 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5438 p_top_expr[expr] then
5439 {
5440 pop_pktbl(p, $p_pktbl);
5441 pop_pvtbl(p, $p_pvtbl);
5442 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5443 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5444 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5445 }
5446 compstmt(stmts)
5447 p_cases[cases]
5448 {
5449 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5450 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5451 }
5452 ;
5453
5454p_cases : opt_else
5455 | p_case_body
5456 ;
5457
5458p_top_expr : p_top_expr_body
5459 | p_top_expr_body modifier_if expr_value
5460 {
5461 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5462 fixpos($$, $3);
5463 /*% ripper: if_mod!($:3, $:1) %*/
5464 }
5465 | p_top_expr_body modifier_unless expr_value
5466 {
5467 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5468 fixpos($$, $3);
5469 /*% ripper: unless_mod!($:3, $:1) %*/
5470 }
5471 ;
5472
5473p_top_expr_body : p_expr
5474 | p_expr ','
5475 {
5476 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5477 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5478 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5479 }
5480 | p_expr ',' p_args
5481 {
5482 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5483 nd_set_first_loc($$, @1.beg_pos);
5484 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5485 }
5486 | p_find
5487 {
5488 $$ = new_find_pattern(p, 0, $1, &@$);
5489 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5490 }
5491 | p_args_tail
5492 {
5493 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5494 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5495 }
5496 | p_kwargs
5497 {
5498 $$ = new_hash_pattern(p, 0, $1, &@$);
5499 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5500 }
5501 ;
5502
5503p_expr : p_as
5504 ;
5505
5506p_as : p_expr tASSOC p_variable
5507 {
5508 NODE *n = NEW_LIST($1, &@$);
5509 n = list_append(p, n, $3);
5510 $$ = new_hash(p, n, &@$);
5511 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5512 }
5513 | p_alt
5514 ;
5515
5516p_alt : p_alt[left] '|'[alt]
5517 {
5518 p->ctxt.in_alt_pattern = 1;
5519 }
5520 p_expr_basic[right]
5521 {
5522 if (p->ctxt.capture_in_pattern) {
5523 yyerror1(&@alt, "alternative pattern after variable capture");
5524 }
5525 p->ctxt.in_alt_pattern = 0;
5526 $$ = NEW_OR($left, $right, &@$, &@alt);
5527 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5528 }
5529 | p_expr_basic
5530 ;
5531
5532p_lparen : '(' p_pktbl
5533 {
5534 $$ = $2;
5535 /*% ripper: $:2 %*/
5536 }
5537 ;
5538
5539p_lbracket : '[' p_pktbl
5540 {
5541 $$ = $2;
5542 /*% ripper: $:2 %*/
5543 }
5544 ;
5545
5546p_expr_basic : p_value
5547 | p_variable
5548 | p_const p_lparen[p_pktbl] p_args rparen
5549 {
5550 pop_pktbl(p, $p_pktbl);
5551 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5552 nd_set_first_loc($$, @p_const.beg_pos);
5553 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5554 }
5555 | p_const p_lparen[p_pktbl] p_find rparen
5556 {
5557 pop_pktbl(p, $p_pktbl);
5558 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5559 nd_set_first_loc($$, @p_const.beg_pos);
5560 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5561 }
5562 | p_const p_lparen[p_pktbl] p_kwargs rparen
5563 {
5564 pop_pktbl(p, $p_pktbl);
5565 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5566 nd_set_first_loc($$, @p_const.beg_pos);
5567 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5568 }
5569 | p_const '(' rparen
5570 {
5571 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5572 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5573 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5574 }
5575 | p_const p_lbracket[p_pktbl] p_args rbracket
5576 {
5577 pop_pktbl(p, $p_pktbl);
5578 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5579 nd_set_first_loc($$, @p_const.beg_pos);
5580 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5581 }
5582 | p_const p_lbracket[p_pktbl] p_find rbracket
5583 {
5584 pop_pktbl(p, $p_pktbl);
5585 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5586 nd_set_first_loc($$, @p_const.beg_pos);
5587 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5588 }
5589 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5590 {
5591 pop_pktbl(p, $p_pktbl);
5592 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5593 nd_set_first_loc($$, @p_const.beg_pos);
5594 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5595 }
5596 | p_const '[' rbracket
5597 {
5598 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5599 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5600 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5601 }
5602 | tLBRACK p_args rbracket
5603 {
5604 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5605 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5606 }
5607 | tLBRACK p_find rbracket
5608 {
5609 $$ = new_find_pattern(p, 0, $p_find, &@$);
5610 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5611 }
5612 | tLBRACK rbracket
5613 {
5614 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5615 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5616 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5617 }
5618 | tLBRACE p_pktbl lex_ctxt[ctxt]
5619 {
5620 p->ctxt.in_kwarg = 0;
5621 }
5622 p_kwargs rbrace
5623 {
5624 pop_pktbl(p, $p_pktbl);
5625 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5626 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5627 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5628 }
5629 | tLBRACE rbrace
5630 {
5631 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5632 $$ = new_hash_pattern(p, 0, $$, &@$);
5633 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5634 }
5635 | tLPAREN p_pktbl p_expr rparen
5636 {
5637 pop_pktbl(p, $p_pktbl);
5638 $$ = $p_expr;
5639 /*% ripper: $:p_expr %*/
5640 }
5641 ;
5642
5643p_args : p_expr
5644 {
5645 NODE *pre_args = NEW_LIST($1, &@$);
5646 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5647 /*% ripper: [[$:1], Qnil, Qnil] %*/
5648 }
5649 | p_args_head
5650 {
5651 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5652 /*% ripper: [$:1, Qnil, Qnil] %*/
5653 }
5654 | p_args_head p_arg
5655 {
5656 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5657 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5658 }
5659 | p_args_head p_rest
5660 {
5661 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5662 /*% ripper: [$:1, $:2, Qnil] %*/
5663 }
5664 | p_args_head p_rest ',' p_args_post
5665 {
5666 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5667 /*% ripper: [$:1, $:2, $:4] %*/
5668 }
5669 | p_args_tail
5670 ;
5671
5672p_args_head : p_arg ','
5673 | p_args_head p_arg ','
5674 {
5675 $$ = list_concat($1, $2);
5676 /*% ripper: rb_ary_concat($:1, $:2) %*/
5677 }
5678 ;
5679
5680p_args_tail : p_rest
5681 {
5682 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5683 /*% ripper: [Qnil, $:1, Qnil] %*/
5684 }
5685 | p_rest ',' p_args_post
5686 {
5687 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5688 /*% ripper: [Qnil, $:1, $:3] %*/
5689 }
5690 ;
5691
5692p_find : p_rest ',' p_args_post ',' p_rest
5693 {
5694 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5695 /*% ripper: [$:1, $:3, $:5] %*/
5696 }
5697 ;
5698
5699
5700p_rest : tSTAR tIDENTIFIER
5701 {
5702 error_duplicate_pattern_variable(p, $2, &@2);
5703 /*% ripper: var_field!($:2) %*/
5704 $$ = assignable(p, $2, 0, &@$);
5705 }
5706 | tSTAR
5707 {
5708 $$ = 0;
5709 /*% ripper: var_field!(Qnil) %*/
5710 }
5711 ;
5712
5713p_args_post : p_arg
5714 | p_args_post ',' p_arg
5715 {
5716 $$ = list_concat($1, $3);
5717 /*% ripper: rb_ary_concat($:1, $:3) %*/
5718 }
5719 ;
5720
5721p_arg : p_expr
5722 {
5723 $$ = NEW_LIST($1, &@$);
5724 /*% ripper: [$:1] %*/
5725 }
5726 ;
5727
5728p_kwargs : p_kwarg ',' p_any_kwrest
5729 {
5730 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5731 /*% ripper: [$:1, $:3] %*/
5732 }
5733 | p_kwarg
5734 {
5735 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5736 /*% ripper: [$:1, Qnil] %*/
5737 }
5738 | p_kwarg ','
5739 {
5740 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5741 /*% ripper: [$:1, Qnil] %*/
5742 }
5743 | p_any_kwrest
5744 {
5745 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5746 /*% ripper: [[], $:1] %*/
5747 }
5748 ;
5749
5750p_kwarg : p_kw
5751 /*% ripper[brace]: [$:1] %*/
5752 | p_kwarg ',' p_kw
5753 {
5754 $$ = list_concat($1, $3);
5755 /*% ripper: rb_ary_push($:1, $:3) %*/
5756 }
5757 ;
5758
5759p_kw : p_kw_label p_expr
5760 {
5761 error_duplicate_pattern_key(p, $1, &@1);
5762 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5763 /*% ripper: [$:1, $:2] %*/
5764 }
5765 | p_kw_label
5766 {
5767 error_duplicate_pattern_key(p, $1, &@1);
5768 if ($1 && !is_local_id($1)) {
5769 yyerror1(&@1, "key must be valid as local variables");
5770 }
5771 error_duplicate_pattern_variable(p, $1, &@1);
5772 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5773 /*% ripper: [$:1, Qnil] %*/
5774 }
5775 ;
5776
5777p_kw_label : tLABEL
5778 | tSTRING_BEG string_contents tLABEL_END
5779 {
5780 YYLTYPE loc = code_loc_gen(&@1, &@3);
5781 if (!$2 || nd_type_p($2, NODE_STR)) {
5782 NODE *node = dsym_node(p, $2, &loc);
5783 $$ = rb_sym2id(rb_node_sym_string_val(node));
5784 }
5785 else {
5786 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5787 $$ = rb_intern_str(STR_NEW0());
5788 }
5789 /*% ripper: $:2 %*/
5790 }
5791 ;
5792
5793p_kwrest : kwrest_mark tIDENTIFIER
5794 {
5795 $$ = $2;
5796 /*% ripper: var_field!($:2) %*/
5797 }
5798 | kwrest_mark
5799 {
5800 $$ = 0;
5801 /*% ripper: Qnil %*/
5802 }
5803 ;
5804
5805p_kwnorest : kwrest_mark keyword_nil
5806 {
5807 $$ = 0;
5808 }
5809 ;
5810
5811p_any_kwrest : p_kwrest
5812 | p_kwnorest
5813 {
5814 $$ = idNil;
5815 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5816 }
5817 ;
5818
5819p_value : p_primitive
5820 | range_expr(p_primitive)
5821 | p_var_ref
5822 | p_expr_ref
5823 | p_const
5824 ;
5825
5826p_primitive : inline_primary
5827 | keyword_variable
5828 {
5829 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5830 /*% ripper: var_ref!($:1) %*/
5831 }
5832 | lambda
5833 ;
5834
5835p_variable : tIDENTIFIER
5836 {
5837 error_duplicate_pattern_variable(p, $1, &@1);
5838 /*% ripper: var_field!($:1) %*/
5839 $$ = assignable(p, $1, 0, &@$);
5840 }
5841 ;
5842
5843p_var_ref : '^' tIDENTIFIER
5844 {
5845 NODE *n = gettable(p, $2, &@$);
5846 if (!n) {
5847 n = NEW_ERROR(&@$);
5848 }
5849 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5850 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5851 }
5852 $$ = n;
5853 /*% ripper: var_ref!($:2) %*/
5854 }
5855 | '^' nonlocal_var
5856 {
5857 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5858 /*% ripper: var_ref!($:2) %*/
5859 }
5860 ;
5861
5862p_expr_ref : '^' tLPAREN expr_value rparen
5863 {
5864 $$ = NEW_BLOCK($3, &@$);
5865 /*% ripper: begin!($:3) %*/
5866 }
5867 ;
5868
5869p_const : tCOLON3 cname
5870 {
5871 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5872 /*% ripper: top_const_ref!($:2) %*/
5873 }
5874 | p_const tCOLON2 cname
5875 {
5876 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5877 /*% ripper: const_path_ref!($:1, $:3) %*/
5878 }
5879 | tCONSTANT
5880 {
5881 $$ = gettable(p, $1, &@$);
5882 /*% ripper: var_ref!($:1) %*/
5883 }
5884 ;
5885
5886opt_rescue : k_rescue exc_list exc_var then
5887 compstmt(stmts)
5888 opt_rescue
5889 {
5890 NODE *err = $3;
5891 if ($3) {
5892 err = NEW_ERRINFO(&@3);
5893 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5894 }
5895 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5896 if ($2) {
5897 fixpos($$, $2);
5898 }
5899 else if ($3) {
5900 fixpos($$, $3);
5901 }
5902 else {
5903 fixpos($$, $5);
5904 }
5905 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5906 }
5907 | none
5908 ;
5909
5910exc_list : arg_value
5911 {
5912 $$ = NEW_LIST($1, &@$);
5913 /*% ripper: rb_ary_new3(1, $:1) %*/
5914 }
5915 | mrhs
5916 {
5917 if (!($$ = splat_array($1))) $$ = $1;
5918 }
5919 | none
5920 ;
5921
5922exc_var : tASSOC lhs
5923 {
5924 $$ = $2;
5925 /*% ripper: $:2 %*/
5926 }
5927 | none
5928 ;
5929
5930opt_ensure : k_ensure stmts terms?
5931 {
5932 p->ctxt.in_rescue = $1.in_rescue;
5933 $$ = $2;
5934 void_expr(p, void_stmts(p, $$));
5935 /*% ripper: ensure!($:2) %*/
5936 }
5937 | none
5938 ;
5939
5940literal : numeric
5941 | symbol
5942 ;
5943
5944strings : string
5945 {
5946 if (!$1) {
5947 $$ = NEW_STR(STRING_NEW0(), &@$);
5948 }
5949 else {
5950 $$ = evstr2dstr(p, $1);
5951 }
5952 /*% ripper: $:1 %*/
5953 }
5954 ;
5955
5956string : tCHAR
5957 | string1
5958 | string string1
5959 {
5960 $$ = literal_concat(p, $1, $2, &@$);
5961 /*% ripper: string_concat!($:1, $:2) %*/
5962 }
5963 ;
5964
5965string1 : tSTRING_BEG string_contents tSTRING_END
5966 {
5967 $$ = heredoc_dedent(p, $2);
5968 if ($$) nd_set_loc($$, &@$);
5969 /*% ripper: $:2 %*/
5970 if (p->heredoc_indent > 0) {
5971 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5972 p->heredoc_indent = 0;
5973 }
5974 /*% ripper: string_literal!($:$) %*/
5975 }
5976 ;
5977
5978xstring : tXSTRING_BEG xstring_contents tSTRING_END
5979 {
5980 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5981 /*% ripper: $:2 %*/
5982 if (p->heredoc_indent > 0) {
5983 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5984 p->heredoc_indent = 0;
5985 }
5986 /*% ripper: xstring_literal!($:$) %*/
5987 }
5988 ;
5989
5990regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5991 {
5992 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5993 /*% ripper: regexp_literal!($:2, $:3) %*/
5994 }
5995 ;
5996
5997words : words(tWORDS_BEG, word_list)
5998 ;
5999
6000word_list : /* none */
6001 {
6002 $$ = 0;
6003 /*% ripper: words_new! %*/
6004 }
6005 | word_list word ' '+
6006 {
6007 $$ = list_append(p, $1, evstr2dstr(p, $2));
6008 /*% ripper: words_add!($:1, $:2) %*/
6009 }
6010 ;
6011
6012word : string_content
6013 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
6014 | word string_content
6015 {
6016 $$ = literal_concat(p, $1, $2, &@$);
6017 /*% ripper: word_add!($:1, $:2) %*/
6018 }
6019 ;
6020
6021symbols : words(tSYMBOLS_BEG, symbol_list)
6022 ;
6023
6024symbol_list : /* none */
6025 {
6026 $$ = 0;
6027 /*% ripper: symbols_new! %*/
6028 }
6029 | symbol_list word ' '+
6030 {
6031 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
6032 /*% ripper: symbols_add!($:1, $:2) %*/
6033 }
6034 ;
6035
6036qwords : words(tQWORDS_BEG, qword_list)
6037 ;
6038
6039qsymbols : words(tQSYMBOLS_BEG, qsym_list)
6040 ;
6041
6042qword_list : /* none */
6043 {
6044 $$ = 0;
6045 /*% ripper: qwords_new! %*/
6046 }
6047 | qword_list tSTRING_CONTENT ' '+
6048 {
6049 $$ = list_append(p, $1, $2);
6050 /*% ripper: qwords_add!($:1, $:2) %*/
6051 }
6052 ;
6053
6054qsym_list : /* none */
6055 {
6056 $$ = 0;
6057 /*% ripper: qsymbols_new! %*/
6058 }
6059 | qsym_list tSTRING_CONTENT ' '+
6060 {
6061 $$ = symbol_append(p, $1, $2);
6062 /*% ripper: qsymbols_add!($:1, $:2) %*/
6063 }
6064 ;
6065
6066string_contents : /* none */
6067 {
6068 $$ = 0;
6069 /*% ripper: string_content! %*/
6070 }
6071 | string_contents string_content
6072 {
6073 $$ = literal_concat(p, $1, $2, &@$);
6074 /*% ripper: string_add!($:1, $:2) %*/
6075 }
6076 ;
6077
6078xstring_contents: /* none */
6079 {
6080 $$ = 0;
6081 /*% ripper: xstring_new! %*/
6082 }
6083 | xstring_contents string_content
6084 {
6085 $$ = literal_concat(p, $1, $2, &@$);
6086 /*% ripper: xstring_add!($:1, $:2) %*/
6087 }
6088 ;
6089
6090regexp_contents : /* none */
6091 {
6092 $$ = 0;
6093 /*% ripper: regexp_new! %*/
6094 }
6095 | regexp_contents string_content
6096 {
6097 NODE *head = $1, *tail = $2;
6098 if (!head) {
6099 $$ = tail;
6100 }
6101 else if (!tail) {
6102 $$ = head;
6103 }
6104 else {
6105 switch (nd_type(head)) {
6106 case NODE_STR:
6107 head = str2dstr(p, head);
6108 break;
6109 case NODE_DSTR:
6110 break;
6111 default:
6112 head = list_append(p, NEW_DSTR(0, &@$), head);
6113 break;
6114 }
6115 $$ = list_append(p, head, tail);
6116 }
6117 /*% ripper: regexp_add!($:1, $:2) %*/
6118 }
6119 ;
6120
6121string_content : tSTRING_CONTENT
6122 /*% ripper[brace]: $:1 %*/
6123 | tSTRING_DVAR
6124 {
6125 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6126 $$ = p->lex.strterm;
6127 p->lex.strterm = 0;
6128 SET_LEX_STATE(EXPR_BEG);
6129 }<strterm>
6130 string_dvar
6131 {
6132 p->lex.strterm = $2;
6133 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6134 nd_set_line($$, @3.end_pos.lineno);
6135 /*% ripper: string_dvar!($:3) %*/
6136 }
6137 | tSTRING_DBEG[state]
6138 {
6139 CMDARG_PUSH(0);
6140 COND_PUSH(0);
6141 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6142 $$ = p->lex.strterm;
6143 p->lex.strterm = 0;
6144 SET_LEX_STATE(EXPR_BEG);
6145 }[term]<strterm>
6146 {
6147 $$ = p->lex.brace_nest;
6148 p->lex.brace_nest = 0;
6149 }[brace]<num>
6150 {
6151 $$ = p->lex.lpar_beg;
6152 p->lex.lpar_beg = -1;
6153 }[lpar]<num>
6154 {
6155 $$ = p->heredoc_indent;
6156 p->heredoc_indent = 0;
6157 }[indent]<num>
6158 compstmt(stmts) string_dend
6159 {
6160 COND_POP();
6161 CMDARG_POP();
6162 p->lex.strterm = $term;
6163 SET_LEX_STATE($state);
6164 p->lex.brace_nest = $brace;
6165 p->lex.lpar_beg = $lpar;
6166 p->heredoc_indent = $indent;
6167 p->heredoc_line_indent = -1;
6168 if ($compstmt) nd_unset_fl_newline($compstmt);
6169 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6170 /*% ripper: string_embexpr!($:compstmt) %*/
6171 }
6172 ;
6173
6174string_dend : tSTRING_DEND
6175 | END_OF_INPUT
6176 ;
6177
6178string_dvar : nonlocal_var
6179 {
6180 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6181 /*% ripper: var_ref!($:1) %*/
6182 }
6183 | backref
6184 ;
6185
6186symbol : ssym
6187 | dsym
6188 ;
6189
6190ssym : tSYMBEG sym
6191 {
6192 SET_LEX_STATE(EXPR_END);
6193 VALUE str = rb_id2str($2);
6194 /*
6195 * TODO:
6196 * set_yylval_noname sets invalid id to yylval.
6197 * This branch can be removed once yylval is changed to
6198 * hold lexed string.
6199 */
6200 if (!str) str = STR_NEW0();
6201 $$ = NEW_SYM(str, &@$);
6202 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6203 }
6204 ;
6205
6206sym : fname
6207 | nonlocal_var
6208 ;
6209
6210dsym : tSYMBEG string_contents tSTRING_END
6211 {
6212 SET_LEX_STATE(EXPR_END);
6213 $$ = dsym_node(p, $2, &@$);
6214 /*% ripper: dyna_symbol!($:2) %*/
6215 }
6216 ;
6217
6218numeric : simple_numeric
6219 | tUMINUS_NUM simple_numeric %prec tLOWEST
6220 {
6221 $$ = $2;
6222 negate_lit(p, $$, &@$);
6223 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6224 }
6225 ;
6226
6227simple_numeric : tINTEGER
6228 | tFLOAT
6229 | tRATIONAL
6230 | tIMAGINARY
6231 ;
6232
6233nonlocal_var : tIVAR
6234 | tGVAR
6235 | tCVAR
6236 ;
6237
6238user_variable : ident_or_const
6239 | nonlocal_var
6240 ;
6241
6242keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6243 | keyword_self {$$ = KWD2EID(self, $1);}
6244 | keyword_true {$$ = KWD2EID(true, $1);}
6245 | keyword_false {$$ = KWD2EID(false, $1);}
6246 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6247 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6248 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6249 ;
6250
6251var_ref : user_variable
6252 {
6253 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6254 if (ifdef_ripper(id_is_var(p, $1), false)) {
6255 /*% ripper: var_ref!($:1) %*/
6256 }
6257 else {
6258 /*% ripper: vcall!($:1) %*/
6259 }
6260 }
6261 | keyword_variable
6262 {
6263 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6264 /*% ripper: var_ref!($:1) %*/
6265 }
6266 ;
6267
6268var_lhs : user_or_keyword_variable
6269 {
6270 /*% ripper: var_field!($:1) %*/
6271 $$ = assignable(p, $1, 0, &@$);
6272 }
6273 ;
6274
6275backref : tNTH_REF
6276 | tBACK_REF
6277 ;
6278
6279superclass : '<'
6280 {
6281 SET_LEX_STATE(EXPR_BEG);
6282 p->command_start = TRUE;
6283 }
6284 expr_value term
6285 {
6286 $$ = $3;
6287 /*% ripper: $:3 %*/
6288 }
6289 | none
6290 ;
6291
6292f_opt_paren_args: f_paren_args
6293 | none
6294 {
6295 p->ctxt.in_argdef = 0;
6296 $$ = new_args_tail(p, 0, 0, 0, &@0);
6297 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6298 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6299 }
6300 ;
6301
6302f_paren_args : '(' f_args rparen
6303 {
6304 $$ = $2;
6305 /*% ripper: paren!($:2) %*/
6306 SET_LEX_STATE(EXPR_BEG);
6307 p->command_start = TRUE;
6308 p->ctxt.in_argdef = 0;
6309 }
6310 ;
6311
6312f_arglist : f_paren_args
6313 | {
6314 $$ = p->ctxt;
6315 p->ctxt.in_kwarg = 1;
6316 p->ctxt.in_argdef = 1;
6317 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6318 }<ctxt>
6319 f_args term
6320 {
6321 p->ctxt.in_kwarg = $1.in_kwarg;
6322 p->ctxt.in_argdef = 0;
6323 $$ = $2;
6324 SET_LEX_STATE(EXPR_BEG);
6325 p->command_start = TRUE;
6326 /*% ripper: $:2 %*/
6327 }
6328 ;
6329
6330args_tail : args_tail_basic(arg_value)
6331 | args_forward
6332 {
6333 ID fwd = $args_forward;
6334 if (lambda_beginning_p() ||
6335 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6336 yyerror0("unexpected ... in lambda argument");
6337 fwd = 0;
6338 }
6339 else {
6340 add_forwarding_args(p);
6341 }
6342 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6343 $$->nd_ainfo.forwarding = 1;
6344 /*% ripper: [Qnil, $:1, Qnil] %*/
6345 }
6346 ;
6347
6348f_args : f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6349 {
6350 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6351 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6352 }
6353 | f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6354 {
6355 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6356 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6357 }
6358 | f_arg ',' f_opt_arg(arg_value) opt_args_tail(args_tail)
6359 {
6360 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6361 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6362 }
6363 | f_arg ',' f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6364 {
6365 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6366 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6367 }
6368 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6369 {
6370 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6371 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6372 }
6373 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6374 {
6375 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6376 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6377 }
6378 | f_arg opt_args_tail(args_tail)
6379 {
6380 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6381 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6382 }
6383 | f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6384 {
6385 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6386 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6387 }
6388 | f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6389 {
6390 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6391 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6392 }
6393 | f_opt_arg(arg_value) opt_args_tail(args_tail)
6394 {
6395 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6396 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6397 }
6398 | f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6399 {
6400 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6401 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6402 }
6403 | f_rest_arg opt_args_tail(args_tail)
6404 {
6405 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6406 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6407 }
6408 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6409 {
6410 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6411 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6412 }
6413 | args_tail
6414 {
6415 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6416 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6417 }
6418 | /* none */
6419 {
6420 $$ = new_args_tail(p, 0, 0, 0, &@0);
6421 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6422 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6423 }
6424 ;
6425
6426args_forward : tBDOT3
6427 {
6428 $$ = idFWD_KWREST;
6429 /*% ripper: args_forward! %*/
6430 }
6431 ;
6432
6433f_bad_arg : tCONSTANT
6434 {
6435 static const char mesg[] = "formal argument cannot be a constant";
6436 /*%%%*/
6437 yyerror1(&@1, mesg);
6438 /*% %*/
6439 $$ = 0;
6440 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6441 }
6442 | tIVAR
6443 {
6444 static const char mesg[] = "formal argument cannot be an instance variable";
6445 /*%%%*/
6446 yyerror1(&@1, mesg);
6447 /*% %*/
6448 $$ = 0;
6449 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6450 }
6451 | tGVAR
6452 {
6453 static const char mesg[] = "formal argument cannot be a global variable";
6454 /*%%%*/
6455 yyerror1(&@1, mesg);
6456 /*% %*/
6457 $$ = 0;
6458 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6459 }
6460 | tCVAR
6461 {
6462 static const char mesg[] = "formal argument cannot be a class variable";
6463 /*%%%*/
6464 yyerror1(&@1, mesg);
6465 /*% %*/
6466 $$ = 0;
6467 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6468 }
6469 ;
6470
6471f_norm_arg : f_bad_arg
6472 | tIDENTIFIER
6473 {
6474 VALUE e = formal_argument_error(p, $$ = $1);
6475 if (e) {
6476 /*% ripper[error]: param_error!(?e, $:1) %*/
6477 }
6478 p->max_numparam = ORDINAL_PARAM;
6479 }
6480 ;
6481
6482f_arg_asgn : f_norm_arg
6483 {
6484 arg_var(p, $1);
6485 $$ = $1;
6486 }
6487 ;
6488
6489f_arg_item : f_arg_asgn
6490 {
6491 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6492 /*% ripper: $:1 %*/
6493 }
6494 | tLPAREN f_margs rparen
6495 {
6496 ID tid = internal_id(p);
6497 YYLTYPE loc;
6498 loc.beg_pos = @2.beg_pos;
6499 loc.end_pos = @2.beg_pos;
6500 arg_var(p, tid);
6501 if (dyna_in_block(p)) {
6502 $2->nd_value = NEW_DVAR(tid, &loc);
6503 }
6504 else {
6505 $2->nd_value = NEW_LVAR(tid, &loc);
6506 }
6507 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6508 $$->nd_next = (NODE *)$2;
6509 /*% ripper: mlhs_paren!($:2) %*/
6510 }
6511 ;
6512
6513f_arg : f_arg_item
6514 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6515 | f_arg ',' f_arg_item
6516 {
6517 $$ = $1;
6518 $$->nd_plen++;
6519 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6520 rb_discard_node(p, (NODE *)$3);
6521 /*% ripper: rb_ary_push($:1, $:3) %*/
6522 }
6523 ;
6524
6525
6526f_label : tLABEL
6527 {
6528 VALUE e = formal_argument_error(p, $$ = $1);
6529 if (e) {
6530 $$ = 0;
6531 /*% ripper[error]: param_error!(?e, $:1) %*/
6532 }
6533 /*
6534 * Workaround for Prism::ParseTest#test_filepath for
6535 * "unparser/corpus/literal/def.txt"
6536 *
6537 * See the discussion on https://github.com/ruby/ruby/pull/9923
6538 */
6539 arg_var(p, ifdef_ripper(0, $1));
6540 /*% ripper: $:1 %*/
6541 p->max_numparam = ORDINAL_PARAM;
6542 p->ctxt.in_argdef = 0;
6543 }
6544 ;
6545
6546kwrest_mark : tPOW
6547 | tDSTAR
6548 ;
6549
6550f_no_kwarg : p_kwnorest
6551 {
6552 /*% ripper: nokw_param!(Qnil) %*/
6553 }
6554 ;
6555
6556f_kwrest : kwrest_mark tIDENTIFIER
6557 {
6558 arg_var(p, shadowing_lvar(p, $2));
6559 $$ = $2;
6560 /*% ripper: kwrest_param!($:2) %*/
6561 }
6562 | kwrest_mark
6563 {
6564 arg_var(p, idFWD_KWREST);
6565 $$ = idFWD_KWREST;
6566 /*% ripper: kwrest_param!(Qnil) %*/
6567 }
6568 ;
6569
6570restarg_mark : '*'
6571 | tSTAR
6572 ;
6573
6574f_rest_arg : restarg_mark tIDENTIFIER
6575 {
6576 arg_var(p, shadowing_lvar(p, $2));
6577 $$ = $2;
6578 /*% ripper: rest_param!($:2) %*/
6579 }
6580 | restarg_mark
6581 {
6582 arg_var(p, idFWD_REST);
6583 $$ = idFWD_REST;
6584 /*% ripper: rest_param!(Qnil) %*/
6585 }
6586 ;
6587
6588blkarg_mark : '&'
6589 | tAMPER
6590 ;
6591
6592f_block_arg : blkarg_mark tIDENTIFIER
6593 {
6594 arg_var(p, shadowing_lvar(p, $2));
6595 $$ = $2;
6596 /*% ripper: blockarg!($:2) %*/
6597 }
6598 | blkarg_mark
6599 {
6600 arg_var(p, idFWD_BLOCK);
6601 $$ = idFWD_BLOCK;
6602 /*% ripper: blockarg!(Qnil) %*/
6603 }
6604 ;
6605
6606opt_f_block_arg : ',' f_block_arg
6607 {
6608 $$ = $2;
6609 /*% ripper: $:2 %*/
6610 }
6611 | none
6612 ;
6613
6614
6615singleton : value_expr(singleton_expr)
6616 {
6617 NODE *expr = last_expr_node($1);
6618 switch (nd_type(expr)) {
6619 case NODE_STR:
6620 case NODE_DSTR:
6621 case NODE_XSTR:
6622 case NODE_DXSTR:
6623 case NODE_REGX:
6624 case NODE_DREGX:
6625 case NODE_SYM:
6626 case NODE_LINE:
6627 case NODE_FILE:
6628 case NODE_ENCODING:
6629 case NODE_INTEGER:
6630 case NODE_FLOAT:
6631 case NODE_RATIONAL:
6632 case NODE_IMAGINARY:
6633 case NODE_DSYM:
6634 case NODE_LIST:
6635 case NODE_ZLIST:
6636 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6637 break;
6638 default:
6639 break;
6640 }
6641 $$ = $1;
6642 }
6643 ;
6644
6645singleton_expr : var_ref
6646 | '('
6647 {
6648 SET_LEX_STATE(EXPR_BEG);
6649 p->ctxt.in_argdef = 0;
6650 }
6651 expr rparen
6652 {
6653 p->ctxt.in_argdef = 1;
6654 $$ = $3;
6655 /*% ripper: paren!($:3) %*/
6656 }
6657 ;
6658
6659assoc_list : none
6660 | assocs trailer
6661 {
6662 $$ = $1;
6663 /*% ripper: assoclist_from_args!($:1) %*/
6664 }
6665 ;
6666
6667assocs : assoc
6668 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6669 | assocs ',' assoc
6670 {
6671 NODE *assocs = $1;
6672 NODE *tail = $3;
6673 if (!assocs) {
6674 assocs = tail;
6675 }
6676 else if (tail) {
6677 NODE *n = RNODE_LIST(tail)->nd_next;
6678 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6679 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6680 /* DSTAR */
6681 tail = RNODE_HASH(n)->nd_head;
6682 }
6683 if (tail) {
6684 assocs = list_concat(assocs, tail);
6685 }
6686 }
6687 $$ = assocs;
6688 /*% ripper: rb_ary_push($:1, $:3) %*/
6689 }
6690 ;
6691
6692assoc : arg_value tASSOC arg_value
6693 {
6694 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6695 /*% ripper: assoc_new!($:1, $:3) %*/
6696 }
6697 | tLABEL arg_value
6698 {
6699 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6700 /*% ripper: assoc_new!($:1, $:2) %*/
6701 }
6702 | tLABEL
6703 {
6704 NODE *val = gettable(p, $1, &@$);
6705 if (!val) val = NEW_ERROR(&@$);
6706 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6707 /*% ripper: assoc_new!($:1, Qnil) %*/
6708 }
6709 | tSTRING_BEG string_contents tLABEL_END arg_value
6710 {
6711 YYLTYPE loc = code_loc_gen(&@1, &@3);
6712 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6713 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6714 }
6715 | tDSTAR arg_value
6716 {
6717 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6718 /*% ripper: assoc_splat!($:2) %*/
6719 }
6720 | tDSTAR
6721 {
6722 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6723 $$ = list_append(p, NEW_LIST(0, &@$),
6724 NEW_LVAR(idFWD_KWREST, &@$));
6725 /*% ripper: assoc_splat!(Qnil) %*/
6726 }
6727 ;
6728
6729%rule %inline operation : ident_or_const
6730 | tFID
6731 ;
6732
6733operation2 : operation
6734 | op
6735 ;
6736
6737operation3 : tIDENTIFIER
6738 | tFID
6739 | op
6740 ;
6741
6742dot_or_colon : '.'
6743 | tCOLON2
6744 ;
6745
6746call_op : '.'
6747 | tANDDOT
6748 ;
6749
6750call_op2 : call_op
6751 | tCOLON2
6752 ;
6753
6754rparen : '\n'? ')'
6755 ;
6756
6757rbracket : '\n'? ']'
6758 ;
6759
6760rbrace : '\n'? '}'
6761 ;
6762
6763trailer : '\n'?
6764 | ','
6765 ;
6766
6767term : ';'
6768 {
6769 yyerrok;
6770 token_flush(p);
6771 if (p->ctxt.in_defined) {
6772 p->ctxt.has_trailing_semicolon = 1;
6773 }
6774 }
6775 | '\n'
6776 {
6777 @$.end_pos = @$.beg_pos;
6778 token_flush(p);
6779 }
6780 ;
6781
6782terms : term
6783 | terms ';' {yyerrok;}
6784 ;
6785
6786none : /* none */
6787 {
6788 $$ = 0;
6789 /*% ripper: Qnil %*/
6790 }
6791 ;
6792%%
6793# undef p
6794# undef yylex
6795# undef yylval
6796# define yylval (*p->lval)
6797
6798static int regx_options(struct parser_params*);
6799static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6800static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6801static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6802static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6803
6804#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6805
6806# define set_yylval_node(x) { \
6807 YYLTYPE _cur_loc; \
6808 rb_parser_set_location(p, &_cur_loc); \
6809 yylval.node = (x); \
6810 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6811}
6812# define set_yylval_str(x) \
6813do { \
6814 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6815 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6816} while(0)
6817# define set_yylval_num(x) { \
6818 yylval.num = (x); \
6819 set_parser_s_value(x); \
6820}
6821# define set_yylval_id(x) (yylval.id = (x))
6822# define set_yylval_name(x) { \
6823 (yylval.id = (x)); \
6824 set_parser_s_value(ID2SYM(x)); \
6825}
6826# define yylval_id() (yylval.id)
6827
6828#define set_yylval_noname() set_yylval_id(keyword_nil)
6829#define has_delayed_token(p) (p->delayed.token != NULL)
6830
6831#ifndef RIPPER
6832#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6833#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6834
6835static bool
6836parser_has_token(struct parser_params *p)
6837{
6838 const char *const pcur = p->lex.pcur;
6839 const char *const ptok = p->lex.ptok;
6840 if (p->keep_tokens && (pcur < ptok)) {
6841 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6842 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6843 }
6844 return pcur > ptok;
6845}
6846
6847static const char *
6848escaped_char(int c)
6849{
6850 switch (c) {
6851 case '"': return "\\\"";
6852 case '\\': return "\\\\";
6853 case '\0': return "\\0";
6854 case '\n': return "\\n";
6855 case '\r': return "\\r";
6856 case '\t': return "\\t";
6857 case '\f': return "\\f";
6858 case '\013': return "\\v";
6859 case '\010': return "\\b";
6860 case '\007': return "\\a";
6861 case '\033': return "\\e";
6862 case '\x7f': return "\\c?";
6863 }
6864 return NULL;
6865}
6866
6867static rb_parser_string_t *
6868rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6869{
6870 rb_encoding *enc = p->enc;
6871 const char *ptr = str->ptr;
6872 const char *pend = ptr + str->len;
6873 const char *prev = ptr;
6874 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6875 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6876
6877 while (ptr < pend) {
6878 unsigned int c;
6879 const char *cc;
6880 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6881 if (!MBCLEN_CHARFOUND_P(n)) {
6882 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6883 n = rb_enc_mbminlen(enc);
6884 if (pend < ptr + n)
6885 n = (int)(pend - ptr);
6886 while (n--) {
6887 c = *ptr & 0xf0 >> 4;
6888 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6889 c = *ptr & 0x0f;
6890 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6891 parser_str_cat(result, charbuf, 4);
6892 prev = ++ptr;
6893 }
6894 continue;
6895 }
6896 n = MBCLEN_CHARFOUND_LEN(n);
6897 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6898 ptr += n;
6899 cc = escaped_char(c);
6900 if (cc) {
6901 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6902 parser_str_cat_cstr(result, cc);
6903 prev = ptr;
6904 }
6905 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6906 }
6907 else {
6908 if (ptr - n > prev) {
6909 parser_str_cat(result, prev, ptr - n - prev);
6910 prev = ptr - n;
6911 }
6912 parser_str_cat(result, prev, ptr - prev);
6913 prev = ptr;
6914 }
6915 }
6916 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6917
6918 return result;
6919}
6920
6921static void
6922parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6923{
6924 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6925 token->id = p->token_id;
6926 token->type_name = parser_token2char(p, t);
6927 token->str = str;
6928 token->loc.beg_pos = p->yylloc->beg_pos;
6929 token->loc.end_pos = p->yylloc->end_pos;
6930 rb_parser_ary_push_ast_token(p, p->tokens, token);
6931 p->token_id++;
6932
6933 if (p->debug) {
6934 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6935 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6936 line, token->id, token->type_name, str_escaped->ptr,
6937 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6938 token->loc.end_pos.lineno, token->loc.end_pos.column);
6939 rb_parser_string_free(p, str_escaped);
6940 }
6941}
6942
6943static void
6944parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6945{
6946 debug_token_line(p, "parser_dispatch_scan_event", line);
6947
6948 if (!parser_has_token(p)) return;
6949
6950 RUBY_SET_YYLLOC(*p->yylloc);
6951
6952 if (p->keep_tokens) {
6953 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6954 parser_append_tokens(p, str, t, line);
6955 }
6956
6957 token_flush(p);
6958}
6959
6960#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6961static void
6962parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6963{
6964 debug_token_line(p, "parser_dispatch_delayed_token", line);
6965
6966 if (!has_delayed_token(p)) return;
6967
6968 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6969
6970 if (p->keep_tokens) {
6971 /* p->delayed.token is freed by rb_parser_tokens_free */
6972 parser_append_tokens(p, p->delayed.token, t, line);
6973 }
6974 else {
6975 rb_parser_string_free(p, p->delayed.token);
6976 }
6977
6978 p->delayed.token = NULL;
6979}
6980#else
6981#define literal_flush(p, ptr) ((void)(ptr))
6982
6983static int
6984ripper_has_scan_event(struct parser_params *p)
6985{
6986 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6987 return p->lex.pcur > p->lex.ptok;
6988}
6989
6990static VALUE
6991ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6992{
6993 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6994 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6995 RUBY_SET_YYLLOC(*p->yylloc);
6996 token_flush(p);
6997 return rval;
6998}
6999
7000static void
7001ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
7002{
7003 if (!ripper_has_scan_event(p)) return;
7004
7005 set_parser_s_value(ripper_scan_event_val(p, t));
7006}
7007#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
7008
7009static void
7010ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
7011{
7012 /* save and adjust the location to delayed token for callbacks */
7013 int saved_line = p->ruby_sourceline;
7014 const char *saved_tokp = p->lex.ptok;
7015 VALUE s_value, str;
7016
7017 if (!has_delayed_token(p)) return;
7018 p->ruby_sourceline = p->delayed.beg_line;
7019 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
7020 str = rb_str_new_mutable_parser_string(p->delayed.token);
7021 rb_parser_string_free(p, p->delayed.token);
7022 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
7023 set_parser_s_value(s_value);
7024 p->delayed.token = NULL;
7025 p->ruby_sourceline = saved_line;
7026 p->lex.ptok = saved_tokp;
7027}
7028#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7029#endif /* RIPPER */
7030
7031static inline int
7032is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7033{
7034 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7035}
7036
7037static inline bool
7038peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
7039{
7040 const char *ptr = p->lex.pcur + at;
7041 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
7042 if (memcmp(ptr, str, len)) return false;
7043 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
7044 switch (ptr[len]) {
7045 case '!': case '?': return false;
7046 }
7047 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
7048}
7049
7050static inline int
7051parser_is_identchar(struct parser_params *p)
7052{
7053 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7054}
7055
7056static inline int
7057parser_isascii(struct parser_params *p)
7058{
7059 return ISASCII(*(p->lex.pcur-1));
7060}
7061
7062static void
7063token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7064{
7065 int column = 1, nonspc = 0, i;
7066 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7067 if (*ptr == '\t') {
7068 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7069 }
7070 column++;
7071 if (*ptr != ' ' && *ptr != '\t') {
7072 nonspc = 1;
7073 }
7074 }
7075
7076 ptinfo->beg = loc->beg_pos;
7077 ptinfo->indent = column;
7078 ptinfo->nonspc = nonspc;
7079}
7080
7081static void
7082token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7083{
7084 token_info *ptinfo;
7085
7086 if (!p->token_info_enabled) return;
7087 ptinfo = ALLOC(token_info);
7088 ptinfo->token = token;
7089 ptinfo->next = p->token_info;
7090 token_info_setup(ptinfo, p->lex.pbeg, loc);
7091
7092 p->token_info = ptinfo;
7093}
7094
7095static void
7096token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7097{
7098 token_info *ptinfo_beg = p->token_info;
7099
7100 if (!ptinfo_beg) return;
7101
7102 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7103 token_info_warn(p, token, ptinfo_beg, 1, loc);
7104
7105 p->token_info = ptinfo_beg->next;
7106 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7107}
7108
7109static void
7110token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7111{
7112 token_info *ptinfo_beg = p->token_info;
7113
7114 if (!ptinfo_beg) return;
7115 p->token_info = ptinfo_beg->next;
7116
7117 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7118 ptinfo_beg->beg.column != beg_pos.column ||
7119 strcmp(ptinfo_beg->token, token)) {
7120 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7121 beg_pos.lineno, beg_pos.column, token,
7122 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7123 ptinfo_beg->token);
7124 }
7125
7126 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7127}
7128
7129static void
7130token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7131{
7132 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7133 if (!p->token_info_enabled) return;
7134 if (!ptinfo_beg) return;
7135 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7136 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7137 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7138 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7139 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7140 rb_warn3L(ptinfo_end->beg.lineno,
7141 "mismatched indentations at '%s' with '%s' at %d",
7142 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7143}
7144
7145static int
7146parser_precise_mbclen(struct parser_params *p, const char *ptr)
7147{
7148 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7149 if (!MBCLEN_CHARFOUND_P(len)) {
7150 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7151 return -1;
7152 }
7153 return len;
7154}
7155
7156#ifndef RIPPER
7157static inline void
7158parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7159{
7160 rb_parser_string_t *str;
7161 int lineno = p->ruby_sourceline;
7162 if (!yylloc) {
7163 return;
7164 }
7165 else if (yylloc->beg_pos.lineno == lineno) {
7166 str = p->lex.lastline;
7167 }
7168 else {
7169 return;
7170 }
7171 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7172}
7173
7174static int
7175parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7176{
7177#if 0
7178 YYLTYPE current;
7179
7180 if (!yylloc) {
7181 yylloc = RUBY_SET_YYLLOC(current);
7182 }
7183 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7184 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7185 yylloc = 0;
7186 }
7187#endif
7188 parser_compile_error(p, yylloc, "%s", msg);
7189 parser_show_error_line(p, yylloc);
7190 return 0;
7191}
7192
7193static int
7194parser_yyerror0(struct parser_params *p, const char *msg)
7195{
7196 YYLTYPE current;
7197 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7198}
7199
7200void
7201ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7202{
7203 VALUE mesg;
7204 const int max_line_margin = 30;
7205 const char *ptr, *ptr_end, *pt, *pb;
7206 const char *pre = "", *post = "", *pend;
7207 const char *code = "", *caret = "";
7208 const char *lim;
7209 const char *const pbeg = PARSER_STRING_PTR(str);
7210 char *buf;
7211 long len;
7212 int i;
7213
7214 if (!yylloc) return;
7215 pend = rb_parser_string_end(str);
7216 if (pend > pbeg && pend[-1] == '\n') {
7217 if (--pend > pbeg && pend[-1] == '\r') --pend;
7218 }
7219
7220 pt = pend;
7221 if (lineno == yylloc->end_pos.lineno &&
7222 (pend - pbeg) > yylloc->end_pos.column) {
7223 pt = pbeg + yylloc->end_pos.column;
7224 }
7225
7226 ptr = ptr_end = pt;
7227 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7228 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7229
7230 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7231 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7232
7233 len = ptr_end - ptr;
7234 if (len > 4) {
7235 if (ptr > pbeg) {
7236 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7237 if (ptr > pbeg) pre = "...";
7238 }
7239 if (ptr_end < pend) {
7240 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7241 if (ptr_end < pend) post = "...";
7242 }
7243 }
7244 pb = pbeg;
7245 if (lineno == yylloc->beg_pos.lineno) {
7246 pb += yylloc->beg_pos.column;
7247 if (pb > pt) pb = pt;
7248 }
7249 if (pb < ptr) pb = ptr;
7250 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7251 return;
7252 }
7253 if (RTEST(errbuf)) {
7254 mesg = rb_attr_get(errbuf, idMesg);
7255 if (char_at_end(p, mesg, '\n') != '\n')
7256 rb_str_cat_cstr(mesg, "\n");
7257 }
7258 else {
7259 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7260 }
7261 if (!errbuf && rb_stderr_tty_p()) {
7262#define CSI_BEGIN "\033["
7263#define CSI_SGR "m"
7264 rb_str_catf(mesg,
7265 CSI_BEGIN""CSI_SGR"%s" /* pre */
7266 CSI_BEGIN"1"CSI_SGR"%.*s"
7267 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7268 CSI_BEGIN";1"CSI_SGR"%.*s"
7269 CSI_BEGIN""CSI_SGR"%s" /* post */
7270 "\n",
7271 pre,
7272 (int)(pb - ptr), ptr,
7273 (int)(pt - pb), pb,
7274 (int)(ptr_end - pt), pt,
7275 post);
7276 }
7277 else {
7278 char *p2;
7279
7280 len = ptr_end - ptr;
7281 lim = pt < pend ? pt : pend;
7282 i = (int)(lim - ptr);
7283 buf = ALLOCA_N(char, i+2);
7284 code = ptr;
7285 caret = p2 = buf;
7286 if (ptr <= pb) {
7287 while (ptr < pb) {
7288 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7289 }
7290 *p2++ = '^';
7291 ptr++;
7292 }
7293 if (lim > ptr) {
7294 memset(p2, '~', (lim - ptr));
7295 p2 += (lim - ptr);
7296 }
7297 *p2 = '\0';
7298 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7299 pre, (int)len, code, post,
7300 pre, caret);
7301 }
7302 if (!errbuf) rb_write_error_str(mesg);
7303}
7304#else
7305
7306static int
7307parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7308{
7309 const char *pcur = 0, *ptok = 0;
7310 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7311 p->ruby_sourceline == yylloc->end_pos.lineno) {
7312 pcur = p->lex.pcur;
7313 ptok = p->lex.ptok;
7314 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7315 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7316 }
7317 parser_yyerror0(p, msg);
7318 if (pcur) {
7319 p->lex.ptok = ptok;
7320 p->lex.pcur = pcur;
7321 }
7322 return 0;
7323}
7324
7325static int
7326parser_yyerror0(struct parser_params *p, const char *msg)
7327{
7328 dispatch1(parse_error, STR_NEW2(msg));
7329 ripper_error(p);
7330 return 0;
7331}
7332
7333static inline void
7334parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7335{
7336}
7337#endif /* !RIPPER */
7338
7339static int
7340vtable_size(const struct vtable *tbl)
7341{
7342 if (!DVARS_TERMINAL_P(tbl)) {
7343 return tbl->pos;
7344 }
7345 else {
7346 return 0;
7347 }
7348}
7349
7350static struct vtable *
7351vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7352{
7353 struct vtable *tbl = ALLOC(struct vtable);
7354 tbl->pos = 0;
7355 tbl->capa = 8;
7356 tbl->tbl = ALLOC_N(ID, tbl->capa);
7357 tbl->prev = prev;
7358#ifndef RIPPER
7359 if (p->debug) {
7360 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7361 }
7362#endif
7363 return tbl;
7364}
7365#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7366
7367static void
7368vtable_free_gen(struct parser_params *p, int line, const char *name,
7369 struct vtable *tbl)
7370{
7371#ifndef RIPPER
7372 if (p->debug) {
7373 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7374 }
7375#endif
7376 if (!DVARS_TERMINAL_P(tbl)) {
7377 if (tbl->tbl) {
7378 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7379 }
7380 ruby_sized_xfree(tbl, sizeof(*tbl));
7381 }
7382}
7383#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7384
7385static void
7386vtable_add_gen(struct parser_params *p, int line, const char *name,
7387 struct vtable *tbl, ID id)
7388{
7389#ifndef RIPPER
7390 if (p->debug) {
7391 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7392 line, name, (void *)tbl, rb_id2name(id));
7393 }
7394#endif
7395 if (DVARS_TERMINAL_P(tbl)) {
7396 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7397 return;
7398 }
7399 if (tbl->pos == tbl->capa) {
7400 tbl->capa = tbl->capa * 2;
7401 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7402 }
7403 tbl->tbl[tbl->pos++] = id;
7404}
7405#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7406
7407static void
7408vtable_pop_gen(struct parser_params *p, int line, const char *name,
7409 struct vtable *tbl, int n)
7410{
7411 if (p->debug) {
7412 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7413 line, name, (void *)tbl, n);
7414 }
7415 if (tbl->pos < n) {
7416 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7417 return;
7418 }
7419 tbl->pos -= n;
7420}
7421#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7422
7423static int
7424vtable_included(const struct vtable * tbl, ID id)
7425{
7426 int i;
7427
7428 if (!DVARS_TERMINAL_P(tbl)) {
7429 for (i = 0; i < tbl->pos; i++) {
7430 if (tbl->tbl[i] == id) {
7431 return i+1;
7432 }
7433 }
7434 }
7435 return 0;
7436}
7437
7438static void parser_prepare(struct parser_params *p);
7439
7440static int
7441e_option_supplied(struct parser_params *p)
7442{
7443 return strcmp(p->ruby_sourcefile, "-e") == 0;
7444}
7445
7446#ifndef RIPPER
7447static NODE *parser_append_options(struct parser_params *p, NODE *node);
7448
7449static VALUE
7450yycompile0(VALUE arg)
7451{
7452 int n;
7453 NODE *tree;
7454 struct parser_params *p = (struct parser_params *)arg;
7455 int cov = FALSE;
7456
7457 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7458 cov = TRUE;
7459 }
7460
7461 if (p->debug_lines) {
7462 p->ast->body.script_lines = p->debug_lines;
7463 }
7464
7465 parser_prepare(p);
7466#define RUBY_DTRACE_PARSE_HOOK(name) \
7467 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7468 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7469 }
7470 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7471 n = yyparse(p);
7472 RUBY_DTRACE_PARSE_HOOK(END);
7473
7474 p->debug_lines = 0;
7475
7476 xfree(p->lex.strterm);
7477 p->lex.strterm = 0;
7478 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7479 if (n || p->error_p) {
7480 VALUE mesg = p->error_buffer;
7481 if (!mesg) {
7482 mesg = syntax_error_new();
7483 }
7484 if (!p->error_tolerant) {
7485 rb_set_errinfo(mesg);
7486 return FALSE;
7487 }
7488 }
7489 tree = p->eval_tree;
7490 if (!tree) {
7491 tree = NEW_NIL(&NULL_LOC);
7492 }
7493 else {
7494 rb_parser_ary_t *tokens = p->tokens;
7495 NODE *prelude;
7496 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7497 prelude = block_append(p, p->eval_tree_begin, body);
7498 RNODE_SCOPE(tree)->nd_body = prelude;
7499 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7500 p->ast->body.coverage_enabled = cov;
7501 if (p->keep_tokens) {
7502 p->ast->node_buffer->tokens = tokens;
7503 p->tokens = NULL;
7504 }
7505 }
7506 p->ast->body.root = tree;
7507 p->ast->body.line_count = p->line_count;
7508 return TRUE;
7509}
7510
7511static rb_ast_t *
7512yycompile(struct parser_params *p, VALUE fname, int line)
7513{
7514 rb_ast_t *ast;
7515 if (NIL_P(fname)) {
7516 p->ruby_sourcefile_string = Qnil;
7517 p->ruby_sourcefile = "(none)";
7518 }
7519 else {
7520 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7521 p->ruby_sourcefile = StringValueCStr(fname);
7522 }
7523 p->ruby_sourceline = line - 1;
7524
7525 p->lvtbl = NULL;
7526
7527 p->ast = ast = rb_ast_new();
7528 compile_callback(yycompile0, (VALUE)p);
7529 p->ast = 0;
7530
7531 while (p->lvtbl) {
7532 local_pop(p);
7533 }
7534
7535 return ast;
7536}
7537#endif /* !RIPPER */
7538
7539static rb_encoding *
7540must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7541{
7542 rb_encoding *enc = rb_parser_str_get_encoding(s);
7543 if (!rb_enc_asciicompat(enc)) {
7544 rb_raise(rb_eArgError, "invalid source encoding");
7545 }
7546 return enc;
7547}
7548
7549static rb_parser_string_t *
7550lex_getline(struct parser_params *p)
7551{
7552 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7553 if (!line) return 0;
7554 p->line_count++;
7555 string_buffer_append(p, line);
7556 must_be_ascii_compatible(p, line);
7557 return line;
7558}
7559
7560#ifndef RIPPER
7561rb_ast_t*
7562rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7563{
7564 p->lex.gets = gets;
7565 p->lex.input = input;
7566 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7567
7568 return yycompile(p, fname, line);
7569}
7570#endif /* !RIPPER */
7571
7572#define STR_FUNC_ESCAPE 0x01
7573#define STR_FUNC_EXPAND 0x02
7574#define STR_FUNC_REGEXP 0x04
7575#define STR_FUNC_QWORDS 0x08
7576#define STR_FUNC_SYMBOL 0x10
7577#define STR_FUNC_INDENT 0x20
7578#define STR_FUNC_LABEL 0x40
7579#define STR_FUNC_LIST 0x4000
7580#define STR_FUNC_TERM 0x8000
7581
7582enum string_type {
7583 str_label = STR_FUNC_LABEL,
7584 str_squote = (0),
7585 str_dquote = (STR_FUNC_EXPAND),
7586 str_xquote = (STR_FUNC_EXPAND),
7587 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7588 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7589 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7590 str_ssym = (STR_FUNC_SYMBOL),
7591 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7592};
7593
7594static rb_parser_string_t *
7595parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7596{
7597 rb_parser_string_t *pstr;
7598
7599 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7600
7601 if (!(func & STR_FUNC_REGEXP)) {
7602 if (rb_parser_is_ascii_string(p, pstr)) {
7603 }
7604 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7605 /* everything is valid in ASCII-8BIT */
7606 enc = rb_ascii8bit_encoding();
7607 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7608 }
7609 }
7610
7611 return pstr;
7612}
7613
7614static int
7615strterm_is_heredoc(rb_strterm_t *strterm)
7616{
7617 return strterm->heredoc;
7618}
7619
7620static rb_strterm_t *
7621new_strterm(struct parser_params *p, int func, int term, int paren)
7622{
7623 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7624 strterm->u.literal.func = func;
7625 strterm->u.literal.term = term;
7626 strterm->u.literal.paren = paren;
7627 return strterm;
7628}
7629
7630static rb_strterm_t *
7631new_heredoc(struct parser_params *p)
7632{
7633 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7634 strterm->heredoc = true;
7635 return strterm;
7636}
7637
7638#define peek(p,c) peek_n(p, (c), 0)
7639#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7640#define peekc(p) peekc_n(p, 0)
7641#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7642
7643#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7644static void
7645parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7646{
7647 debug_token_line(p, "add_delayed_token", line);
7648
7649 if (tok < end) {
7650 if (has_delayed_token(p)) {
7651 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7652 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7653 int end_col = (next_line ? 0 : p->delayed.end_col);
7654 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7655 dispatch_delayed_token(p, tSTRING_CONTENT);
7656 }
7657 }
7658 if (!has_delayed_token(p)) {
7659 p->delayed.token = rb_parser_string_new(p, 0, 0);
7660 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7661 p->delayed.beg_line = p->ruby_sourceline;
7662 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7663 }
7664 parser_str_cat(p->delayed.token, tok, end - tok);
7665 p->delayed.end_line = p->ruby_sourceline;
7666 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7667 p->lex.ptok = end;
7668 }
7669}
7670
7671static void
7672set_lastline(struct parser_params *p, rb_parser_string_t *str)
7673{
7674 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7675 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7676 p->lex.lastline = str;
7677}
7678
7679static int
7680nextline(struct parser_params *p, int set_encoding)
7681{
7682 rb_parser_string_t *str = p->lex.nextline;
7683 p->lex.nextline = 0;
7684 if (!str) {
7685 if (p->eofp)
7686 return -1;
7687
7688 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7689 goto end_of_input;
7690 }
7691
7692 if (!p->lex.input || !(str = lex_getline(p))) {
7693 end_of_input:
7694 p->eofp = 1;
7695 lex_goto_eol(p);
7696 return -1;
7697 }
7698#ifndef RIPPER
7699 if (p->debug_lines) {
7700 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7701 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7702 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7703 }
7704#endif
7705 p->cr_seen = FALSE;
7706 }
7707 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7708 /* after here-document without terminator */
7709 goto end_of_input;
7710 }
7711 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7712 if (p->heredoc_end > 0) {
7713 p->ruby_sourceline = p->heredoc_end;
7714 p->heredoc_end = 0;
7715 }
7716 p->ruby_sourceline++;
7717 set_lastline(p, str);
7718 token_flush(p);
7719 return 0;
7720}
7721
7722static int
7723parser_cr(struct parser_params *p, int c)
7724{
7725 if (peek(p, '\n')) {
7726 p->lex.pcur++;
7727 c = '\n';
7728 }
7729 return c;
7730}
7731
7732static inline int
7733nextc0(struct parser_params *p, int set_encoding)
7734{
7735 int c;
7736
7737 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7738 if (nextline(p, set_encoding)) return -1;
7739 }
7740 c = (unsigned char)*p->lex.pcur++;
7741 if (UNLIKELY(c == '\r')) {
7742 c = parser_cr(p, c);
7743 }
7744
7745 return c;
7746}
7747#define nextc(p) nextc0(p, TRUE)
7748
7749static void
7750pushback(struct parser_params *p, int c)
7751{
7752 if (c == -1) return;
7753 p->eofp = 0;
7754 p->lex.pcur--;
7755 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7756 p->lex.pcur--;
7757 }
7758}
7759
7760#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7761
7762#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7763#define tok(p) (p)->tokenbuf
7764#define toklen(p) (p)->tokidx
7765
7766static int
7767looking_at_eol_p(struct parser_params *p)
7768{
7769 const char *ptr = p->lex.pcur;
7770 while (!lex_eol_ptr_p(p, ptr)) {
7771 int c = (unsigned char)*ptr++;
7772 int eol = (c == '\n' || c == '#');
7773 if (eol || !ISSPACE(c)) {
7774 return eol;
7775 }
7776 }
7777 return TRUE;
7778}
7779
7780static char*
7781newtok(struct parser_params *p)
7782{
7783 p->tokidx = 0;
7784 if (!p->tokenbuf) {
7785 p->toksiz = 60;
7786 p->tokenbuf = ALLOC_N(char, 60);
7787 }
7788 if (p->toksiz > 4096) {
7789 p->toksiz = 60;
7790 REALLOC_N(p->tokenbuf, char, 60);
7791 }
7792 return p->tokenbuf;
7793}
7794
7795static char *
7796tokspace(struct parser_params *p, int n)
7797{
7798 p->tokidx += n;
7799
7800 if (p->tokidx >= p->toksiz) {
7801 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7802 REALLOC_N(p->tokenbuf, char, p->toksiz);
7803 }
7804 return &p->tokenbuf[p->tokidx-n];
7805}
7806
7807static void
7808tokadd(struct parser_params *p, int c)
7809{
7810 p->tokenbuf[p->tokidx++] = (char)c;
7811 if (p->tokidx >= p->toksiz) {
7812 p->toksiz *= 2;
7813 REALLOC_N(p->tokenbuf, char, p->toksiz);
7814 }
7815}
7816
7817static int
7818tok_hex(struct parser_params *p, size_t *numlen)
7819{
7820 int c;
7821
7822 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7823 if (!*numlen) {
7824 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7825 yyerror0("invalid hex escape");
7826 dispatch_scan_event(p, tSTRING_CONTENT);
7827 return 0;
7828 }
7829 p->lex.pcur += *numlen;
7830 return c;
7831}
7832
7833#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7834
7835static int
7836escaped_control_code(int c)
7837{
7838 int c2 = 0;
7839 switch (c) {
7840 case ' ':
7841 c2 = 's';
7842 break;
7843 case '\n':
7844 c2 = 'n';
7845 break;
7846 case '\t':
7847 c2 = 't';
7848 break;
7849 case '\v':
7850 c2 = 'v';
7851 break;
7852 case '\r':
7853 c2 = 'r';
7854 break;
7855 case '\f':
7856 c2 = 'f';
7857 break;
7858 }
7859 return c2;
7860}
7861
7862#define WARN_SPACE_CHAR(c, prefix) \
7863 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7864
7865static int
7866tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7867 int regexp_literal, const char *begin)
7868{
7869 const int wide = !begin;
7870 size_t numlen;
7871 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7872
7873 p->lex.pcur += numlen;
7874 if (p->lex.strterm == NULL ||
7875 strterm_is_heredoc(p->lex.strterm) ||
7876 (p->lex.strterm->u.literal.func != str_regexp)) {
7877 if (!begin) begin = p->lex.pcur;
7878 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7879 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7880 yyerror0("invalid Unicode escape");
7881 dispatch_scan_event(p, tSTRING_CONTENT);
7882 return wide && numlen > 0;
7883 }
7884 if (codepoint > 0x10ffff) {
7885 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7886 yyerror0("invalid Unicode codepoint (too large)");
7887 dispatch_scan_event(p, tSTRING_CONTENT);
7888 return wide;
7889 }
7890 if ((codepoint & 0xfffff800) == 0xd800) {
7891 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7892 yyerror0("invalid Unicode codepoint");
7893 dispatch_scan_event(p, tSTRING_CONTENT);
7894 return wide;
7895 }
7896 }
7897 if (regexp_literal) {
7898 tokcopy(p, (int)numlen);
7899 }
7900 else if (codepoint >= 0x80) {
7901 rb_encoding *utf8 = rb_utf8_encoding();
7902 if (*encp && utf8 != *encp) {
7903 YYLTYPE loc = RUBY_INIT_YYLLOC();
7904 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7905 parser_show_error_line(p, &loc);
7906 return wide;
7907 }
7908 *encp = utf8;
7909 tokaddmbc(p, codepoint, *encp);
7910 }
7911 else {
7912 tokadd(p, codepoint);
7913 }
7914 return TRUE;
7915}
7916
7917static int tokadd_mbchar(struct parser_params *p, int c);
7918
7919static int
7920tokskip_mbchar(struct parser_params *p)
7921{
7922 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7923 if (len > 0) {
7924 p->lex.pcur += len - 1;
7925 }
7926 return len;
7927}
7928
7929/* return value is for ?\u3042 */
7930static void
7931tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7932 int term, int symbol_literal, int regexp_literal)
7933{
7934 /*
7935 * If `term` is not -1, then we allow multiple codepoints in \u{}
7936 * upto `term` byte, otherwise we're parsing a character literal.
7937 * And then add the codepoints to the current token.
7938 */
7939 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7940
7941 const int open_brace = '{', close_brace = '}';
7942
7943 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7944
7945 if (peek(p, open_brace)) { /* handle \u{...} form */
7946 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7947 /*
7948 * Skip parsing validation code and copy bytes as-is until term or
7949 * closing brace, in order to correctly handle extended regexps where
7950 * invalid unicode escapes are allowed in comments. The regexp parser
7951 * does its own validation and will catch any issues.
7952 */
7953 tokadd(p, open_brace);
7954 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7955 int c = peekc(p);
7956 if (c == close_brace) {
7957 tokadd(p, c);
7958 ++p->lex.pcur;
7959 break;
7960 }
7961 else if (c == term) {
7962 break;
7963 }
7964 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7965 tokadd(p, c);
7966 c = *++p->lex.pcur;
7967 }
7968 tokadd_mbchar(p, c);
7969 }
7970 }
7971 else {
7972 const char *second = NULL;
7973 int c, last = nextc(p);
7974 if (lex_eol_p(p)) goto unterminated;
7975 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7976 while (c != close_brace) {
7977 if (c == term) goto unterminated;
7978 if (second == multiple_codepoints)
7979 second = p->lex.pcur;
7980 if (regexp_literal) tokadd(p, last);
7981 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7982 break;
7983 }
7984 while (ISSPACE(c = peekc(p))) {
7985 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7986 last = c;
7987 }
7988 if (term == -1 && !second)
7989 second = multiple_codepoints;
7990 }
7991
7992 if (c != close_brace) {
7993 unterminated:
7994 flush_string_content(p, rb_utf8_encoding(), 0);
7995 yyerror0("unterminated Unicode escape");
7996 dispatch_scan_event(p, tSTRING_CONTENT);
7997 return;
7998 }
7999 if (second && second != multiple_codepoints) {
8000 const char *pcur = p->lex.pcur;
8001 p->lex.pcur = second;
8002 dispatch_scan_event(p, tSTRING_CONTENT);
8003 token_flush(p);
8004 p->lex.pcur = pcur;
8005 yyerror0(multiple_codepoints);
8006 token_flush(p);
8007 }
8008
8009 if (regexp_literal) tokadd(p, close_brace);
8010 nextc(p);
8011 }
8012 }
8013 else { /* handle \uxxxx form */
8014 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
8015 token_flush(p);
8016 return;
8017 }
8018 }
8019}
8020
8021#define ESCAPE_CONTROL 1
8022#define ESCAPE_META 2
8023
8024static int
8025read_escape(struct parser_params *p, int flags, const char *begin)
8026{
8027 int c;
8028 size_t numlen;
8029
8030 switch (c = nextc(p)) {
8031 case '\\': /* Backslash */
8032 return c;
8033
8034 case 'n': /* newline */
8035 return '\n';
8036
8037 case 't': /* horizontal tab */
8038 return '\t';
8039
8040 case 'r': /* carriage-return */
8041 return '\r';
8042
8043 case 'f': /* form-feed */
8044 return '\f';
8045
8046 case 'v': /* vertical tab */
8047 return '\13';
8048
8049 case 'a': /* alarm(bell) */
8050 return '\007';
8051
8052 case 'e': /* escape */
8053 return 033;
8054
8055 case '0': case '1': case '2': case '3': /* octal constant */
8056 case '4': case '5': case '6': case '7':
8057 pushback(p, c);
8058 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8059 p->lex.pcur += numlen;
8060 return c;
8061
8062 case 'x': /* hex constant */
8063 c = tok_hex(p, &numlen);
8064 if (numlen == 0) return 0;
8065 return c;
8066
8067 case 'b': /* backspace */
8068 return '\010';
8069
8070 case 's': /* space */
8071 return ' ';
8072
8073 case 'M':
8074 if (flags & ESCAPE_META) goto eof;
8075 if ((c = nextc(p)) != '-') {
8076 goto eof;
8077 }
8078 if ((c = nextc(p)) == '\\') {
8079 switch (peekc(p)) {
8080 case 'u': case 'U':
8081 nextc(p);
8082 goto eof;
8083 }
8084 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8085 }
8086 else if (c == -1) goto eof;
8087 else if (!ISASCII(c)) {
8088 tokskip_mbchar(p);
8089 goto eof;
8090 }
8091 else {
8092 int c2 = escaped_control_code(c);
8093 if (c2) {
8094 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8095 WARN_SPACE_CHAR(c2, "\\M-");
8096 }
8097 else {
8098 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8099 }
8100 }
8101 else if (ISCNTRL(c)) goto eof;
8102 return ((c & 0xff) | 0x80);
8103 }
8104
8105 case 'C':
8106 if ((c = nextc(p)) != '-') {
8107 goto eof;
8108 }
8109 case 'c':
8110 if (flags & ESCAPE_CONTROL) goto eof;
8111 if ((c = nextc(p))== '\\') {
8112 switch (peekc(p)) {
8113 case 'u': case 'U':
8114 nextc(p);
8115 goto eof;
8116 }
8117 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8118 }
8119 else if (c == '?')
8120 return 0177;
8121 else if (c == -1) goto eof;
8122 else if (!ISASCII(c)) {
8123 tokskip_mbchar(p);
8124 goto eof;
8125 }
8126 else {
8127 int c2 = escaped_control_code(c);
8128 if (c2) {
8129 if (ISCNTRL(c)) {
8130 if (flags & ESCAPE_META) {
8131 WARN_SPACE_CHAR(c2, "\\M-");
8132 }
8133 else {
8134 WARN_SPACE_CHAR(c2, "");
8135 }
8136 }
8137 else {
8138 if (flags & ESCAPE_META) {
8139 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8140 }
8141 else {
8142 WARN_SPACE_CHAR(c2, "\\C-");
8143 }
8144 }
8145 }
8146 else if (ISCNTRL(c)) goto eof;
8147 }
8148 return c & 0x9f;
8149
8150 eof:
8151 case -1:
8152 flush_string_content(p, p->enc, p->lex.pcur - begin);
8153 yyerror0("Invalid escape character syntax");
8154 dispatch_scan_event(p, tSTRING_CONTENT);
8155 return '\0';
8156
8157 default:
8158 if (!ISASCII(c)) {
8159 tokskip_mbchar(p);
8160 goto eof;
8161 }
8162 return c;
8163 }
8164}
8165
8166static void
8167tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8168{
8169 int len = rb_enc_codelen(c, enc);
8170 rb_enc_mbcput(c, tokspace(p, len), enc);
8171}
8172
8173static int
8174tokadd_escape(struct parser_params *p)
8175{
8176 int c;
8177 size_t numlen;
8178 const char *begin = p->lex.pcur;
8179
8180 switch (c = nextc(p)) {
8181 case '\n':
8182 return 0; /* just ignore */
8183
8184 case '0': case '1': case '2': case '3': /* octal constant */
8185 case '4': case '5': case '6': case '7':
8186 {
8187 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8188 if (numlen == 0) goto eof;
8189 p->lex.pcur += numlen;
8190 tokcopy(p, (int)numlen + 1);
8191 }
8192 return 0;
8193
8194 case 'x': /* hex constant */
8195 {
8196 tok_hex(p, &numlen);
8197 if (numlen == 0) return -1;
8198 tokcopy(p, (int)numlen + 2);
8199 }
8200 return 0;
8201
8202 eof:
8203 case -1:
8204 flush_string_content(p, p->enc, p->lex.pcur - begin);
8205 yyerror0("Invalid escape character syntax");
8206 token_flush(p);
8207 return -1;
8208
8209 default:
8210 tokadd(p, '\\');
8211 tokadd(p, c);
8212 }
8213 return 0;
8214}
8215
8216static int
8217char_to_option(int c)
8218{
8219 int val;
8220
8221 switch (c) {
8222 case 'i':
8223 val = RE_ONIG_OPTION_IGNORECASE;
8224 break;
8225 case 'x':
8226 val = RE_ONIG_OPTION_EXTEND;
8227 break;
8228 case 'm':
8229 val = RE_ONIG_OPTION_MULTILINE;
8230 break;
8231 default:
8232 val = 0;
8233 break;
8234 }
8235 return val;
8236}
8237
8238#define ARG_ENCODING_FIXED 16
8239#define ARG_ENCODING_NONE 32
8240#define ENC_ASCII8BIT 1
8241#define ENC_EUC_JP 2
8242#define ENC_Windows_31J 3
8243#define ENC_UTF8 4
8244
8245static int
8246char_to_option_kcode(int c, int *option, int *kcode)
8247{
8248 *option = 0;
8249
8250 switch (c) {
8251 case 'n':
8252 *kcode = ENC_ASCII8BIT;
8253 return (*option = ARG_ENCODING_NONE);
8254 case 'e':
8255 *kcode = ENC_EUC_JP;
8256 break;
8257 case 's':
8258 *kcode = ENC_Windows_31J;
8259 break;
8260 case 'u':
8261 *kcode = ENC_UTF8;
8262 break;
8263 default:
8264 *kcode = -1;
8265 return (*option = char_to_option(c));
8266 }
8267 *option = ARG_ENCODING_FIXED;
8268 return 1;
8269}
8270
8271static int
8272regx_options(struct parser_params *p)
8273{
8274 int kcode = 0;
8275 int kopt = 0;
8276 int options = 0;
8277 int c, opt, kc;
8278
8279 newtok(p);
8280 while (c = nextc(p), ISALPHA(c)) {
8281 if (c == 'o') {
8282 options |= RE_OPTION_ONCE;
8283 }
8284 else if (char_to_option_kcode(c, &opt, &kc)) {
8285 if (kc >= 0) {
8286 if (kc != ENC_ASCII8BIT) kcode = c;
8287 kopt = opt;
8288 }
8289 else {
8290 options |= opt;
8291 }
8292 }
8293 else {
8294 tokadd(p, c);
8295 }
8296 }
8297 options |= kopt;
8298 pushback(p, c);
8299 if (toklen(p)) {
8300 YYLTYPE loc = RUBY_INIT_YYLLOC();
8301 tokfix(p);
8302 compile_error(p, "unknown regexp option%s - %*s",
8303 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8304 parser_show_error_line(p, &loc);
8305 }
8306 return options | RE_OPTION_ENCODING(kcode);
8307}
8308
8309static int
8310tokadd_mbchar(struct parser_params *p, int c)
8311{
8312 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8313 if (len < 0) return -1;
8314 tokadd(p, c);
8315 p->lex.pcur += --len;
8316 if (len > 0) tokcopy(p, len);
8317 return c;
8318}
8319
8320static inline int
8321simple_re_meta(int c)
8322{
8323 switch (c) {
8324 case '$': case '*': case '+': case '.':
8325 case '?': case '^': case '|':
8326 case ')': case ']': case '}': case '>':
8327 return TRUE;
8328 default:
8329 return FALSE;
8330 }
8331}
8332
8333static int
8334parser_update_heredoc_indent(struct parser_params *p, int c)
8335{
8336 if (p->heredoc_line_indent == -1) {
8337 if (c == '\n') p->heredoc_line_indent = 0;
8338 }
8339 else {
8340 if (c == ' ') {
8341 p->heredoc_line_indent++;
8342 return TRUE;
8343 }
8344 else if (c == '\t') {
8345 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8346 p->heredoc_line_indent = w * TAB_WIDTH;
8347 return TRUE;
8348 }
8349 else if (c != '\n') {
8350 if (p->heredoc_indent > p->heredoc_line_indent) {
8351 p->heredoc_indent = p->heredoc_line_indent;
8352 }
8353 p->heredoc_line_indent = -1;
8354 }
8355 else {
8356 /* Whitespace only line has no indentation */
8357 p->heredoc_line_indent = 0;
8358 }
8359 }
8360 return FALSE;
8361}
8362
8363static void
8364parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8365{
8366 YYLTYPE loc = RUBY_INIT_YYLLOC();
8367 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8368 compile_error(p, "%s mixed within %s source", n1, n2);
8369 parser_show_error_line(p, &loc);
8370}
8371
8372static void
8373parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8374{
8375 const char *pos = p->lex.pcur;
8376 p->lex.pcur = beg;
8377 parser_mixed_error(p, enc1, enc2);
8378 p->lex.pcur = pos;
8379}
8380
8381static inline char
8382nibble_char_upper(unsigned int c)
8383{
8384 c &= 0xf;
8385 return c + (c < 10 ? '0' : 'A' - 10);
8386}
8387
8388static int
8389tokadd_string(struct parser_params *p,
8390 int func, int term, int paren, long *nest,
8391 rb_encoding **encp, rb_encoding **enc)
8392{
8393 int c;
8394 bool erred = false;
8395#ifdef RIPPER
8396 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8397 int top_of_line = FALSE;
8398#endif
8399
8400#define mixed_error(enc1, enc2) \
8401 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8402#define mixed_escape(beg, enc1, enc2) \
8403 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8404
8405 while ((c = nextc(p)) != -1) {
8406 if (p->heredoc_indent > 0) {
8407 parser_update_heredoc_indent(p, c);
8408 }
8409#ifdef RIPPER
8410 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8411 pushback(p, c);
8412 break;
8413 }
8414#endif
8415
8416 if (paren && c == paren) {
8417 ++*nest;
8418 }
8419 else if (c == term) {
8420 if (!nest || !*nest) {
8421 pushback(p, c);
8422 break;
8423 }
8424 --*nest;
8425 }
8426 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8427 unsigned char c2 = *p->lex.pcur;
8428 if (c2 == '$' || c2 == '@' || c2 == '{') {
8429 pushback(p, c);
8430 break;
8431 }
8432 }
8433 else if (c == '\\') {
8434 c = nextc(p);
8435 switch (c) {
8436 case '\n':
8437 if (func & STR_FUNC_QWORDS) break;
8438 if (func & STR_FUNC_EXPAND) {
8439 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8440 continue;
8441 if (c == term) {
8442 c = '\\';
8443 goto terminate;
8444 }
8445 }
8446 tokadd(p, '\\');
8447 break;
8448
8449 case '\\':
8450 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8451 break;
8452
8453 case 'u':
8454 if ((func & STR_FUNC_EXPAND) == 0) {
8455 tokadd(p, '\\');
8456 break;
8457 }
8458 tokadd_utf8(p, enc, term,
8459 func & STR_FUNC_SYMBOL,
8460 func & STR_FUNC_REGEXP);
8461 continue;
8462
8463 default:
8464 if (c == -1) return -1;
8465 if (!ISASCII(c)) {
8466 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8467 goto non_ascii;
8468 }
8469 if (func & STR_FUNC_REGEXP) {
8470 switch (c) {
8471 case 'c':
8472 case 'C':
8473 case 'M': {
8474 pushback(p, c);
8475 c = read_escape(p, 0, p->lex.pcur - 1);
8476
8477 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8478 *t++ = '\\';
8479 *t++ = 'x';
8480 *t++ = nibble_char_upper(c >> 4);
8481 *t++ = nibble_char_upper(c);
8482 continue;
8483 }
8484 }
8485
8486 if (c == term && !simple_re_meta(c)) {
8487 tokadd(p, c);
8488 continue;
8489 }
8490 pushback(p, c);
8491 if ((c = tokadd_escape(p)) < 0)
8492 return -1;
8493 if (*enc && *enc != *encp) {
8494 mixed_escape(p->lex.ptok+2, *enc, *encp);
8495 }
8496 continue;
8497 }
8498 else if (func & STR_FUNC_EXPAND) {
8499 pushback(p, c);
8500 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8501 c = read_escape(p, 0, p->lex.pcur - 1);
8502 }
8503 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8504 /* ignore backslashed spaces in %w */
8505 }
8506 else if (c != term && !(paren && c == paren)) {
8507 tokadd(p, '\\');
8508 pushback(p, c);
8509 continue;
8510 }
8511 }
8512 }
8513 else if (!parser_isascii(p)) {
8514 non_ascii:
8515 if (!*enc) {
8516 *enc = *encp;
8517 }
8518 else if (*enc != *encp) {
8519 mixed_error(*enc, *encp);
8520 continue;
8521 }
8522 if (tokadd_mbchar(p, c) == -1) return -1;
8523 continue;
8524 }
8525 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8526 pushback(p, c);
8527 break;
8528 }
8529 if (c & 0x80) {
8530 if (!*enc) {
8531 *enc = *encp;
8532 }
8533 else if (*enc != *encp) {
8534 mixed_error(*enc, *encp);
8535 continue;
8536 }
8537 }
8538 tokadd(p, c);
8539#ifdef RIPPER
8540 top_of_line = (c == '\n');
8541#endif
8542 }
8543 terminate:
8544 if (*enc) *encp = *enc;
8545 return c;
8546}
8547
8548#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8549
8550static void
8551flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8552{
8553 p->lex.pcur -= back;
8554 if (has_delayed_token(p)) {
8555 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8556 if (len > 0) {
8557 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8558 p->delayed.end_line = p->ruby_sourceline;
8559 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8560 }
8561 dispatch_delayed_token(p, tSTRING_CONTENT);
8562 p->lex.ptok = p->lex.pcur;
8563 }
8564 dispatch_scan_event(p, tSTRING_CONTENT);
8565 p->lex.pcur += back;
8566}
8567
8568/* this can be shared with ripper, since it's independent from struct
8569 * parser_params. */
8570#ifndef RIPPER
8571#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8572#define SPECIAL_PUNCT(idx) ( \
8573 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8574 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8575 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8576 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8577 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8578 BIT('0', idx))
8579const uint_least32_t ruby_global_name_punct_bits[] = {
8580 SPECIAL_PUNCT(0),
8581 SPECIAL_PUNCT(1),
8582 SPECIAL_PUNCT(2),
8583};
8584#undef BIT
8585#undef SPECIAL_PUNCT
8586#endif
8587
8588static enum yytokentype
8589parser_peek_variable_name(struct parser_params *p)
8590{
8591 int c;
8592 const char *ptr = p->lex.pcur;
8593
8594 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8595 c = *ptr++;
8596 switch (c) {
8597 case '$':
8598 if ((c = *ptr) == '-') {
8599 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8600 c = *ptr;
8601 }
8602 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8603 return tSTRING_DVAR;
8604 }
8605 break;
8606 case '@':
8607 if ((c = *ptr) == '@') {
8608 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8609 c = *ptr;
8610 }
8611 break;
8612 case '{':
8613 p->lex.pcur = ptr;
8614 p->command_start = TRUE;
8615 yylval.state = p->lex.state;
8616 return tSTRING_DBEG;
8617 default:
8618 return 0;
8619 }
8620 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8621 return tSTRING_DVAR;
8622 return 0;
8623}
8624
8625#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8626#define IS_END() IS_lex_state(EXPR_END_ANY)
8627#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8628#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8629#define IS_LABEL_POSSIBLE() (\
8630 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8631 IS_ARG())
8632#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8633#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8634
8635static inline enum yytokentype
8636parser_string_term(struct parser_params *p, int func)
8637{
8638 xfree(p->lex.strterm);
8639 p->lex.strterm = 0;
8640 if (func & STR_FUNC_REGEXP) {
8641 set_yylval_num(regx_options(p));
8642 dispatch_scan_event(p, tREGEXP_END);
8643 SET_LEX_STATE(EXPR_END);
8644 return tREGEXP_END;
8645 }
8646 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8647 nextc(p);
8648 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8649 return tLABEL_END;
8650 }
8651 SET_LEX_STATE(EXPR_END);
8652 return tSTRING_END;
8653}
8654
8655static enum yytokentype
8656parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8657{
8658 int func = quote->func;
8659 int term = quote->term;
8660 int paren = quote->paren;
8661 int c, space = 0;
8662 rb_encoding *enc = p->enc;
8663 rb_encoding *base_enc = 0;
8664 rb_parser_string_t *lit;
8665
8666 if (func & STR_FUNC_TERM) {
8667 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8668 SET_LEX_STATE(EXPR_END);
8669 xfree(p->lex.strterm);
8670 p->lex.strterm = 0;
8671 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8672 }
8673 c = nextc(p);
8674 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8675 while (c != '\n' && ISSPACE(c = nextc(p)));
8676 space = 1;
8677 }
8678 if (func & STR_FUNC_LIST) {
8679 quote->func &= ~STR_FUNC_LIST;
8680 space = 1;
8681 }
8682 if (c == term && !quote->nest) {
8683 if (func & STR_FUNC_QWORDS) {
8684 quote->func |= STR_FUNC_TERM;
8685 pushback(p, c); /* dispatch the term at tSTRING_END */
8686 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8687 return ' ';
8688 }
8689 return parser_string_term(p, func);
8690 }
8691 if (space) {
8692 if (!ISSPACE(c)) pushback(p, c);
8693 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8694 return ' ';
8695 }
8696 newtok(p);
8697 if ((func & STR_FUNC_EXPAND) && c == '#') {
8698 enum yytokentype t = parser_peek_variable_name(p);
8699 if (t) return t;
8700 tokadd(p, '#');
8701 c = nextc(p);
8702 }
8703 pushback(p, c);
8704 if (tokadd_string(p, func, term, paren, &quote->nest,
8705 &enc, &base_enc) == -1) {
8706 if (p->eofp) {
8707#ifndef RIPPER
8708# define unterminated_literal(mesg) yyerror0(mesg)
8709#else
8710# define unterminated_literal(mesg) compile_error(p, mesg)
8711#endif
8712 literal_flush(p, p->lex.pcur);
8713 if (func & STR_FUNC_QWORDS) {
8714 /* no content to add, bailing out here */
8715 unterminated_literal("unterminated list meets end of file");
8716 xfree(p->lex.strterm);
8717 p->lex.strterm = 0;
8718 return tSTRING_END;
8719 }
8720 if (func & STR_FUNC_REGEXP) {
8721 unterminated_literal("unterminated regexp meets end of file");
8722 }
8723 else {
8724 unterminated_literal("unterminated string meets end of file");
8725 }
8726 quote->func |= STR_FUNC_TERM;
8727 }
8728 }
8729
8730 tokfix(p);
8731 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8732 set_yylval_str(lit);
8733 flush_string_content(p, enc, 0);
8734
8735 return tSTRING_CONTENT;
8736}
8737
8738static enum yytokentype
8739heredoc_identifier(struct parser_params *p)
8740{
8741 /*
8742 * term_len is length of `<<"END"` except `END`,
8743 * in this case term_len is 4 (<, <, " and ").
8744 */
8745 long len, offset = p->lex.pcur - p->lex.pbeg;
8746 int c = nextc(p), term, func = 0, quote = 0;
8747 enum yytokentype token = tSTRING_BEG;
8748 int indent = 0;
8749
8750 if (c == '-') {
8751 c = nextc(p);
8752 func = STR_FUNC_INDENT;
8753 offset++;
8754 }
8755 else if (c == '~') {
8756 c = nextc(p);
8757 func = STR_FUNC_INDENT;
8758 offset++;
8759 indent = INT_MAX;
8760 }
8761 switch (c) {
8762 case '\'':
8763 func |= str_squote; goto quoted;
8764 case '"':
8765 func |= str_dquote; goto quoted;
8766 case '`':
8767 token = tXSTRING_BEG;
8768 func |= str_xquote; goto quoted;
8769
8770 quoted:
8771 quote++;
8772 offset++;
8773 term = c;
8774 len = 0;
8775 while ((c = nextc(p)) != term) {
8776 if (c == -1 || c == '\r' || c == '\n') {
8777 yyerror0("unterminated here document identifier");
8778 return -1;
8779 }
8780 }
8781 break;
8782
8783 default:
8784 if (!parser_is_identchar(p)) {
8785 pushback(p, c);
8786 if (func & STR_FUNC_INDENT) {
8787 pushback(p, indent > 0 ? '~' : '-');
8788 }
8789 return 0;
8790 }
8791 func |= str_dquote;
8792 do {
8793 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8794 if (n < 0) return 0;
8795 p->lex.pcur += --n;
8796 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8797 pushback(p, c);
8798 break;
8799 }
8800
8801 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8802 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8803 yyerror0("too long here document identifier");
8804 dispatch_scan_event(p, tHEREDOC_BEG);
8805 lex_goto_eol(p);
8806
8807 p->lex.strterm = new_heredoc(p);
8808 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8809 here->offset = offset;
8810 here->sourceline = p->ruby_sourceline;
8811 here->length = (unsigned)len;
8812 here->quote = quote;
8813 here->func = func;
8814 here->lastline = p->lex.lastline;
8815
8816 token_flush(p);
8817 p->heredoc_indent = indent;
8818 p->heredoc_line_indent = 0;
8819 return token;
8820}
8821
8822static void
8823heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8824{
8825 rb_parser_string_t *line;
8826 rb_strterm_t *term = p->lex.strterm;
8827
8828 p->lex.strterm = 0;
8829 line = here->lastline;
8830 p->lex.lastline = line;
8831 p->lex.pbeg = PARSER_STRING_PTR(line);
8832 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8833 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8834 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8835 p->heredoc_end = p->ruby_sourceline;
8836 p->ruby_sourceline = (int)here->sourceline;
8837 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8838 p->eofp = 0;
8839 xfree(term);
8840}
8841
8842static int
8843dedent_string_column(const char *str, long len, int width)
8844{
8845 int i, col = 0;
8846
8847 for (i = 0; i < len && col < width; i++) {
8848 if (str[i] == ' ') {
8849 col++;
8850 }
8851 else if (str[i] == '\t') {
8852 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8853 if (n > width) break;
8854 col = n;
8855 }
8856 else {
8857 break;
8858 }
8859 }
8860
8861 return i;
8862}
8863
8864static int
8865dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8866{
8867 char *str;
8868 long len;
8869 int i;
8870
8871 len = PARSER_STRING_LEN(string);
8872 str = PARSER_STRING_PTR(string);
8873
8874 i = dedent_string_column(str, len, width);
8875 if (!i) return 0;
8876
8877 rb_parser_str_modify(string);
8878 str = PARSER_STRING_PTR(string);
8879 if (PARSER_STRING_LEN(string) != len)
8880 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8881 MEMMOVE(str, str + i, char, len - i);
8882 rb_parser_str_set_len(p, string, len - i);
8883 return i;
8884}
8885
8886static NODE *
8887heredoc_dedent(struct parser_params *p, NODE *root)
8888{
8889 NODE *node, *str_node, *prev_node;
8890 int indent = p->heredoc_indent;
8891 rb_parser_string_t *prev_lit = 0;
8892
8893 if (indent <= 0) return root;
8894 if (!root) return root;
8895
8896 prev_node = node = str_node = root;
8897 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8898
8899 while (str_node) {
8900 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8901 if (nd_fl_newline(str_node)) {
8902 dedent_string(p, lit, indent);
8903 }
8904 if (!prev_lit) {
8905 prev_lit = lit;
8906 }
8907 else if (!literal_concat0(p, prev_lit, lit)) {
8908 return 0;
8909 }
8910 else {
8911 NODE *end = RNODE_LIST(node)->as.nd_end;
8912 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8913 if (!node) {
8914 if (nd_type_p(prev_node, NODE_DSTR))
8915 nd_set_type(prev_node, NODE_STR);
8916 break;
8917 }
8918 RNODE_LIST(node)->as.nd_end = end;
8919 goto next_str;
8920 }
8921
8922 str_node = 0;
8923 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8924 next_str:
8925 if (!nd_type_p(node, NODE_LIST)) break;
8926 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8927 enum node_type type = nd_type(str_node);
8928 if (type == NODE_STR || type == NODE_DSTR) break;
8929 prev_lit = 0;
8930 str_node = 0;
8931 }
8932 }
8933 }
8934 return root;
8935}
8936
8937static int
8938whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8939{
8940 const char *beg = p->lex.pbeg;
8941 const char *ptr = p->lex.pend;
8942
8943 if (ptr - beg < len) return FALSE;
8944 if (ptr > beg && ptr[-1] == '\n') {
8945 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8946 if (ptr - beg < len) return FALSE;
8947 }
8948 if (strncmp(eos, ptr -= len, len)) return FALSE;
8949 if (indent) {
8950 while (beg < ptr && ISSPACE(*beg)) beg++;
8951 }
8952 return beg == ptr;
8953}
8954
8955static int
8956word_match_p(struct parser_params *p, const char *word, long len)
8957{
8958 if (strncmp(p->lex.pcur, word, len)) return 0;
8959 if (lex_eol_n_p(p, len)) return 1;
8960 int c = (unsigned char)p->lex.pcur[len];
8961 if (ISSPACE(c)) return 1;
8962 switch (c) {
8963 case '\0': case '\004': case '\032': return 1;
8964 }
8965 return 0;
8966}
8967
8968#define NUM_SUFFIX_R (1<<0)
8969#define NUM_SUFFIX_I (1<<1)
8970#define NUM_SUFFIX_ALL 3
8971
8972static int
8973number_literal_suffix(struct parser_params *p, int mask)
8974{
8975 int c, result = 0;
8976 const char *lastp = p->lex.pcur;
8977
8978 while ((c = nextc(p)) != -1) {
8979 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8980 result |= (mask & NUM_SUFFIX_I);
8981 mask &= ~NUM_SUFFIX_I;
8982 /* r after i, rational of complex is disallowed */
8983 mask &= ~NUM_SUFFIX_R;
8984 continue;
8985 }
8986 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8987 result |= (mask & NUM_SUFFIX_R);
8988 mask &= ~NUM_SUFFIX_R;
8989 continue;
8990 }
8991 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8992 p->lex.pcur = lastp;
8993 return 0;
8994 }
8995 pushback(p, c);
8996 break;
8997 }
8998 return result;
8999}
9000
9001static enum yytokentype
9002set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
9003{
9004 enum rb_numeric_type numeric_type = integer_literal;
9005
9006 if (type == tFLOAT) {
9007 numeric_type = float_literal;
9008 }
9009
9010 if (suffix & NUM_SUFFIX_R) {
9011 type = tRATIONAL;
9012 numeric_type = rational_literal;
9013 }
9014 if (suffix & NUM_SUFFIX_I) {
9015 type = tIMAGINARY;
9016 }
9017
9018 switch (type) {
9019 case tINTEGER:
9020 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
9021 break;
9022 case tFLOAT:
9023 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
9024 break;
9025 case tRATIONAL:
9026 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9027 break;
9028 case tIMAGINARY:
9029 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9030 (void)numeric_type; /* for ripper */
9031 break;
9032 default:
9033 rb_bug("unexpected token: %d", type);
9034 }
9035 SET_LEX_STATE(EXPR_END);
9036 return type;
9037}
9038
9039#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9040static void
9041parser_dispatch_heredoc_end(struct parser_params *p, int line)
9042{
9043 if (has_delayed_token(p))
9044 dispatch_delayed_token(p, tSTRING_CONTENT);
9045
9046#ifdef RIPPER
9047 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9048 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9049#else
9050 if (p->keep_tokens) {
9051 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9052 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9053 parser_append_tokens(p, str, tHEREDOC_END, line);
9054 }
9055#endif
9056
9057 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9058 lex_goto_eol(p);
9059 token_flush(p);
9060}
9061
9062static enum yytokentype
9063here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9064{
9065 int c, func, indent = 0;
9066 const char *eos, *ptr, *ptr_end;
9067 long len;
9068 rb_parser_string_t *str = 0;
9069 rb_encoding *enc = p->enc;
9070 rb_encoding *base_enc = 0;
9071 int bol;
9072#ifdef RIPPER
9073 VALUE s_value;
9074#endif
9075
9076 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9077 len = here->length;
9078 indent = (func = here->func) & STR_FUNC_INDENT;
9079
9080 if ((c = nextc(p)) == -1) {
9081 error:
9082#ifdef RIPPER
9083 if (!has_delayed_token(p)) {
9084 dispatch_scan_event(p, tSTRING_CONTENT);
9085 }
9086 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9087 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9088 if (!(func & STR_FUNC_REGEXP)) {
9089 int cr = ENC_CODERANGE_UNKNOWN;
9090 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9091 if (cr != ENC_CODERANGE_7BIT &&
9092 rb_is_usascii_enc(p->enc) &&
9093 enc != rb_utf8_encoding()) {
9094 enc = rb_ascii8bit_encoding();
9095 }
9096 }
9097 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9098 }
9099 dispatch_delayed_token(p, tSTRING_CONTENT);
9100 }
9101 else {
9102 dispatch_delayed_token(p, tSTRING_CONTENT);
9103 dispatch_scan_event(p, tSTRING_CONTENT);
9104 }
9105 lex_goto_eol(p);
9106#endif
9107 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9108 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9109 (int)len, eos);
9110 token_flush(p);
9111 SET_LEX_STATE(EXPR_END);
9112 return tSTRING_END;
9113 }
9114 bol = was_bol(p);
9115 if (!bol) {
9116 /* not beginning of line, cannot be the terminator */
9117 }
9118 else if (p->heredoc_line_indent == -1) {
9119 /* `heredoc_line_indent == -1` means
9120 * - "after an interpolation in the same line", or
9121 * - "in a continuing line"
9122 */
9123 p->heredoc_line_indent = 0;
9124 }
9125 else if (whole_match_p(p, eos, len, indent)) {
9126 dispatch_heredoc_end(p);
9127 restore:
9128 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9129 token_flush(p);
9130 SET_LEX_STATE(EXPR_END);
9131 return tSTRING_END;
9132 }
9133
9134 if (!(func & STR_FUNC_EXPAND)) {
9135 do {
9136 ptr = PARSER_STRING_PTR(p->lex.lastline);
9137 ptr_end = p->lex.pend;
9138 if (ptr_end > ptr) {
9139 switch (ptr_end[-1]) {
9140 case '\n':
9141 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9142 ptr_end++;
9143 break;
9144 }
9145 case '\r':
9146 --ptr_end;
9147 }
9148 }
9149
9150 if (p->heredoc_indent > 0) {
9151 long i = 0;
9152 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9153 i++;
9154 p->heredoc_line_indent = 0;
9155 }
9156
9157 if (str)
9158 parser_str_cat(str, ptr, ptr_end - ptr);
9159 else
9160 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9161 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9162 lex_goto_eol(p);
9163 if (p->heredoc_indent > 0) {
9164 goto flush_str;
9165 }
9166 if (nextc(p) == -1) {
9167 if (str) {
9168 rb_parser_string_free(p, str);
9169 str = 0;
9170 }
9171 goto error;
9172 }
9173 } while (!whole_match_p(p, eos, len, indent));
9174 }
9175 else {
9176 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9177 newtok(p);
9178 if (c == '#') {
9179 enum yytokentype t = parser_peek_variable_name(p);
9180 if (p->heredoc_line_indent != -1) {
9181 if (p->heredoc_indent > p->heredoc_line_indent) {
9182 p->heredoc_indent = p->heredoc_line_indent;
9183 }
9184 p->heredoc_line_indent = -1;
9185 }
9186 if (t) return t;
9187 tokadd(p, '#');
9188 c = nextc(p);
9189 }
9190 do {
9191 pushback(p, c);
9192 enc = p->enc;
9193 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9194 if (p->eofp) goto error;
9195 goto restore;
9196 }
9197 if (c != '\n') {
9198 if (c == '\\') p->heredoc_line_indent = -1;
9199 flush:
9200 str = STR_NEW3(tok(p), toklen(p), enc, func);
9201 flush_str:
9202 set_yylval_str(str);
9203#ifndef RIPPER
9204 if (bol) nd_set_fl_newline(yylval.node);
9205#endif
9206 flush_string_content(p, enc, 0);
9207 return tSTRING_CONTENT;
9208 }
9209 tokadd(p, nextc(p));
9210 if (p->heredoc_indent > 0) {
9211 lex_goto_eol(p);
9212 goto flush;
9213 }
9214 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9215 if ((c = nextc(p)) == -1) goto error;
9216 } while (!whole_match_p(p, eos, len, indent));
9217 str = STR_NEW3(tok(p), toklen(p), enc, func);
9218 }
9219 dispatch_heredoc_end(p);
9220 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9221 token_flush(p);
9222 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9223#ifdef RIPPER
9224 /* Preserve s_value for set_yylval_str */
9225 s_value = p->s_value;
9226#endif
9227 set_yylval_str(str);
9228#ifdef RIPPER
9229 set_parser_s_value(s_value);
9230#endif
9231
9232#ifndef RIPPER
9233 if (bol) nd_set_fl_newline(yylval.node);
9234#endif
9235 return tSTRING_CONTENT;
9236}
9237
9238#include "lex.c"
9239
9240static int
9241arg_ambiguous(struct parser_params *p, char c)
9242{
9243#ifndef RIPPER
9244 if (c == '/') {
9245 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9246 }
9247 else {
9248 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9249 }
9250#else
9251 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9252#endif
9253 return TRUE;
9254}
9255
9256/* returns true value if formal argument error;
9257 * Qtrue, or error message if ripper */
9258static VALUE
9259formal_argument_error(struct parser_params *p, ID id)
9260{
9261 switch (id_type(id)) {
9262 case ID_LOCAL:
9263 break;
9264#ifndef RIPPER
9265# define ERR(mesg) (yyerror0(mesg), Qtrue)
9266#else
9267# define ERR(mesg) WARN_S(mesg)
9268#endif
9269 case ID_CONST:
9270 return ERR("formal argument cannot be a constant");
9271 case ID_INSTANCE:
9272 return ERR("formal argument cannot be an instance variable");
9273 case ID_GLOBAL:
9274 return ERR("formal argument cannot be a global variable");
9275 case ID_CLASS:
9276 return ERR("formal argument cannot be a class variable");
9277 default:
9278 return ERR("formal argument must be local variable");
9279#undef ERR
9280 }
9281 shadowing_lvar(p, id);
9282
9283 return Qfalse;
9284}
9285
9286static int
9287lvar_defined(struct parser_params *p, ID id)
9288{
9289 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9290}
9291
9292/* emacsen -*- hack */
9293static long
9294parser_encode_length(struct parser_params *p, const char *name, long len)
9295{
9296 long nlen;
9297
9298 if (len > 5 && name[nlen = len - 5] == '-') {
9299 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9300 return nlen;
9301 }
9302 if (len > 4 && name[nlen = len - 4] == '-') {
9303 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9304 return nlen;
9305 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9306 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9307 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9308 return nlen;
9309 }
9310 return len;
9311}
9312
9313static void
9314parser_set_encode(struct parser_params *p, const char *name)
9315{
9316 rb_encoding *enc;
9317 VALUE excargs[3];
9318 int idx = 0;
9319
9320 const char *wrong = 0;
9321 switch (*name) {
9322 case 'e': case 'E': wrong = "external"; break;
9323 case 'i': case 'I': wrong = "internal"; break;
9324 case 'f': case 'F': wrong = "filesystem"; break;
9325 case 'l': case 'L': wrong = "locale"; break;
9326 }
9327 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9328 idx = rb_enc_find_index(name);
9329 if (idx < 0) {
9330 unknown:
9331 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9332 error:
9333 excargs[0] = rb_eArgError;
9334 excargs[2] = rb_make_backtrace();
9335 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9336 VALUE exc = rb_make_exception(3, excargs);
9337 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9338
9339 rb_ast_free(p->ast);
9340 p->ast = NULL;
9341
9342 rb_exc_raise(exc);
9343 }
9344 enc = rb_enc_from_index(idx);
9345 if (!rb_enc_asciicompat(enc)) {
9346 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9347 goto error;
9348 }
9349 p->enc = enc;
9350#ifndef RIPPER
9351 if (p->debug_lines) {
9352 long i;
9353 for (i = 0; i < p->debug_lines->len; i++) {
9354 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9355 }
9356 }
9357#endif
9358}
9359
9360static bool
9361comment_at_top(struct parser_params *p)
9362{
9363 if (p->token_seen) return false;
9364 return (p->line_count == (p->has_shebang ? 2 : 1));
9365}
9366
9367typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9368typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9369
9370static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9371
9372static void
9373magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9374{
9375 if (!comment_at_top(p)) {
9376 return;
9377 }
9378 parser_set_encode(p, val);
9379}
9380
9381static int
9382parser_get_bool(struct parser_params *p, const char *name, const char *val)
9383{
9384 switch (*val) {
9385 case 't': case 'T':
9386 if (STRCASECMP(val, "true") == 0) {
9387 return TRUE;
9388 }
9389 break;
9390 case 'f': case 'F':
9391 if (STRCASECMP(val, "false") == 0) {
9392 return FALSE;
9393 }
9394 break;
9395 }
9396 return parser_invalid_pragma_value(p, name, val);
9397}
9398
9399static int
9400parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9401{
9402 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9403 return -1;
9404}
9405
9406static void
9407parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9408{
9409 int b = parser_get_bool(p, name, val);
9410 if (b >= 0) p->token_info_enabled = b;
9411}
9412
9413static void
9414parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9415{
9416 int b;
9417
9418 if (p->token_seen) {
9419 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9420 return;
9421 }
9422
9423 b = parser_get_bool(p, name, val);
9424 if (b < 0) return;
9425
9426 p->frozen_string_literal = b;
9427}
9428
9429static void
9430parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9431{
9432 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9433 if (*s == ' ' || *s == '\t') continue;
9434 if (*s == '#') break;
9435 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9436 return;
9437 }
9438
9439 switch (*val) {
9440 case 'n': case 'N':
9441 if (STRCASECMP(val, "none") == 0) {
9442 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9443 return;
9444 }
9445 break;
9446 case 'l': case 'L':
9447 if (STRCASECMP(val, "literal") == 0) {
9448 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9449 return;
9450 }
9451 break;
9452 case 'e': case 'E':
9453 if (STRCASECMP(val, "experimental_copy") == 0) {
9454 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9455 return;
9456 }
9457 if (STRCASECMP(val, "experimental_everything") == 0) {
9458 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9459 return;
9460 }
9461 break;
9462 }
9463 parser_invalid_pragma_value(p, name, val);
9464}
9465
9466# if WARN_PAST_SCOPE
9467static void
9468parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9469{
9470 int b = parser_get_bool(p, name, val);
9471 if (b >= 0) p->past_scope_enabled = b;
9472}
9473# endif
9474
9475struct magic_comment {
9476 const char *name;
9477 rb_magic_comment_setter_t func;
9478 rb_magic_comment_length_t length;
9479};
9480
9481static const struct magic_comment magic_comments[] = {
9482 {"coding", magic_comment_encoding, parser_encode_length},
9483 {"encoding", magic_comment_encoding, parser_encode_length},
9484 {"frozen_string_literal", parser_set_frozen_string_literal},
9485 {"shareable_constant_value", parser_set_shareable_constant_value},
9486 {"warn_indent", parser_set_token_info},
9487# if WARN_PAST_SCOPE
9488 {"warn_past_scope", parser_set_past_scope},
9489# endif
9490};
9491
9492static const char *
9493magic_comment_marker(const char *str, long len)
9494{
9495 long i = 2;
9496
9497 while (i < len) {
9498 switch (str[i]) {
9499 case '-':
9500 if (str[i-1] == '*' && str[i-2] == '-') {
9501 return str + i + 1;
9502 }
9503 i += 2;
9504 break;
9505 case '*':
9506 if (i + 1 >= len) return 0;
9507 if (str[i+1] != '-') {
9508 i += 4;
9509 }
9510 else if (str[i-1] != '-') {
9511 i += 2;
9512 }
9513 else {
9514 return str + i + 2;
9515 }
9516 break;
9517 default:
9518 i += 3;
9519 break;
9520 }
9521 }
9522 return 0;
9523}
9524
9525static int
9526parser_magic_comment(struct parser_params *p, const char *str, long len)
9527{
9528 int indicator = 0;
9529 VALUE name = 0, val = 0;
9530 const char *beg, *end, *vbeg, *vend;
9531#define str_copy(_s, _p, _n) ((_s) \
9532 ? (void)(rb_str_resize((_s), (_n)), \
9533 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9534 : (void)((_s) = STR_NEW((_p), (_n))))
9535
9536 if (len <= 7) return FALSE;
9537 if (!!(beg = magic_comment_marker(str, len))) {
9538 if (!(end = magic_comment_marker(beg, str + len - beg)))
9539 return FALSE;
9540 indicator = TRUE;
9541 str = beg;
9542 len = end - beg - 3;
9543 }
9544
9545 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9546 while (len > 0) {
9547 const struct magic_comment *mc = magic_comments;
9548 char *s;
9549 int i;
9550 long n = 0;
9551
9552 for (; len > 0 && *str; str++, --len) {
9553 switch (*str) {
9554 case '\'': case '"': case ':': case ';':
9555 continue;
9556 }
9557 if (!ISSPACE(*str)) break;
9558 }
9559 for (beg = str; len > 0; str++, --len) {
9560 switch (*str) {
9561 case '\'': case '"': case ':': case ';':
9562 break;
9563 default:
9564 if (ISSPACE(*str)) break;
9565 continue;
9566 }
9567 break;
9568 }
9569 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9570 if (!len) break;
9571 if (*str != ':') {
9572 if (!indicator) return FALSE;
9573 continue;
9574 }
9575
9576 do str++; while (--len > 0 && ISSPACE(*str));
9577 if (!len) break;
9578 const char *tok_beg = str;
9579 if (*str == '"') {
9580 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9581 if (*str == '\\') {
9582 --len;
9583 ++str;
9584 }
9585 }
9586 vend = str;
9587 if (len) {
9588 --len;
9589 ++str;
9590 }
9591 }
9592 else {
9593 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9594 vend = str;
9595 }
9596 const char *tok_end = str;
9597 if (indicator) {
9598 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9599 }
9600 else {
9601 while (len > 0 && (ISSPACE(*str))) --len, str++;
9602 if (len) return FALSE;
9603 }
9604
9605 n = end - beg;
9606 str_copy(name, beg, n);
9607 s = RSTRING_PTR(name);
9608 for (i = 0; i < n; ++i) {
9609 if (s[i] == '-') s[i] = '_';
9610 }
9611 do {
9612 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9613 n = vend - vbeg;
9614 if (mc->length) {
9615 n = (*mc->length)(p, vbeg, n);
9616 }
9617 str_copy(val, vbeg, n);
9618 p->lex.ptok = tok_beg;
9619 p->lex.pcur = tok_end;
9620 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9621 break;
9622 }
9623 } while (++mc < magic_comments + numberof(magic_comments));
9624#ifdef RIPPER
9625 str_copy(val, vbeg, vend - vbeg);
9626 dispatch2(magic_comment, name, val);
9627#endif
9628 }
9629
9630 return TRUE;
9631}
9632
9633static void
9634set_file_encoding(struct parser_params *p, const char *str, const char *send)
9635{
9636 int sep = 0;
9637 const char *beg = str;
9638 VALUE s;
9639
9640 for (;;) {
9641 if (send - str <= 6) return;
9642 switch (str[6]) {
9643 case 'C': case 'c': str += 6; continue;
9644 case 'O': case 'o': str += 5; continue;
9645 case 'D': case 'd': str += 4; continue;
9646 case 'I': case 'i': str += 3; continue;
9647 case 'N': case 'n': str += 2; continue;
9648 case 'G': case 'g': str += 1; continue;
9649 case '=': case ':':
9650 sep = 1;
9651 str += 6;
9652 break;
9653 default:
9654 str += 6;
9655 if (ISSPACE(*str)) break;
9656 continue;
9657 }
9658 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9659 sep = 0;
9660 }
9661 for (;;) {
9662 do {
9663 if (++str >= send) return;
9664 } while (ISSPACE(*str));
9665 if (sep) break;
9666 if (*str != '=' && *str != ':') return;
9667 sep = 1;
9668 str++;
9669 }
9670 beg = str;
9671 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9672 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9673 p->lex.ptok = beg;
9674 p->lex.pcur = str;
9675 parser_set_encode(p, RSTRING_PTR(s));
9676 rb_str_resize(s, 0);
9677}
9678
9679static void
9680parser_prepare(struct parser_params *p)
9681{
9682 int c = nextc0(p, FALSE);
9683 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9684 switch (c) {
9685 case '#':
9686 if (peek(p, '!')) p->has_shebang = 1;
9687 break;
9688 case 0xef: /* UTF-8 BOM marker */
9689 if (!lex_eol_n_p(p, 2) &&
9690 (unsigned char)p->lex.pcur[0] == 0xbb &&
9691 (unsigned char)p->lex.pcur[1] == 0xbf) {
9692 p->enc = rb_utf8_encoding();
9693 p->lex.pcur += 2;
9694#ifndef RIPPER
9695 if (p->debug_lines) {
9696 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9697 }
9698#endif
9699 p->lex.pbeg = p->lex.pcur;
9700 token_flush(p);
9701 return;
9702 }
9703 break;
9704 case -1: /* end of script. */
9705 return;
9706 }
9707 pushback(p, c);
9708 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9709}
9710
9711#ifndef RIPPER
9712#define ambiguous_operator(tok, op, syn) ( \
9713 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9714 rb_warning0("even though it seems like "syn""))
9715#else
9716#define ambiguous_operator(tok, op, syn) \
9717 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9718#endif
9719#define warn_balanced(tok, op, syn) ((void) \
9720 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9721 space_seen && !ISSPACE(c) && \
9722 (ambiguous_operator(tok, op, syn), 0)), \
9723 (enum yytokentype)(tok))
9724
9725static enum yytokentype
9726no_digits(struct parser_params *p)
9727{
9728 yyerror0("numeric literal without digits");
9729 if (peek(p, '_')) nextc(p);
9730 /* dummy 0, for tUMINUS_NUM at numeric */
9731 return set_number_literal(p, tINTEGER, 0, 10, 0);
9732}
9733
9734static enum yytokentype
9735parse_numeric(struct parser_params *p, int c)
9736{
9737 int is_float, seen_point, seen_e, nondigit;
9738 int suffix;
9739
9740 is_float = seen_point = seen_e = nondigit = 0;
9741 SET_LEX_STATE(EXPR_END);
9742 newtok(p);
9743 if (c == '-' || c == '+') {
9744 tokadd(p, c);
9745 c = nextc(p);
9746 }
9747 if (c == '0') {
9748 int start = toklen(p);
9749 c = nextc(p);
9750 if (c == 'x' || c == 'X') {
9751 /* hexadecimal */
9752 c = nextc(p);
9753 if (c != -1 && ISXDIGIT(c)) {
9754 do {
9755 if (c == '_') {
9756 if (nondigit) break;
9757 nondigit = c;
9758 continue;
9759 }
9760 if (!ISXDIGIT(c)) break;
9761 nondigit = 0;
9762 tokadd(p, c);
9763 } while ((c = nextc(p)) != -1);
9764 }
9765 pushback(p, c);
9766 tokfix(p);
9767 if (toklen(p) == start) {
9768 return no_digits(p);
9769 }
9770 else if (nondigit) goto trailing_uc;
9771 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9772 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9773 }
9774 if (c == 'b' || c == 'B') {
9775 /* binary */
9776 c = nextc(p);
9777 if (c == '0' || c == '1') {
9778 do {
9779 if (c == '_') {
9780 if (nondigit) break;
9781 nondigit = c;
9782 continue;
9783 }
9784 if (c != '0' && c != '1') break;
9785 nondigit = 0;
9786 tokadd(p, c);
9787 } while ((c = nextc(p)) != -1);
9788 }
9789 pushback(p, c);
9790 tokfix(p);
9791 if (toklen(p) == start) {
9792 return no_digits(p);
9793 }
9794 else if (nondigit) goto trailing_uc;
9795 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9796 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9797 }
9798 if (c == 'd' || c == 'D') {
9799 /* decimal */
9800 c = nextc(p);
9801 if (c != -1 && ISDIGIT(c)) {
9802 do {
9803 if (c == '_') {
9804 if (nondigit) break;
9805 nondigit = c;
9806 continue;
9807 }
9808 if (!ISDIGIT(c)) break;
9809 nondigit = 0;
9810 tokadd(p, c);
9811 } while ((c = nextc(p)) != -1);
9812 }
9813 pushback(p, c);
9814 tokfix(p);
9815 if (toklen(p) == start) {
9816 return no_digits(p);
9817 }
9818 else if (nondigit) goto trailing_uc;
9819 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9820 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9821 }
9822 if (c == '_') {
9823 /* 0_0 */
9824 goto octal_number;
9825 }
9826 if (c == 'o' || c == 'O') {
9827 /* prefixed octal */
9828 c = nextc(p);
9829 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9830 tokfix(p);
9831 return no_digits(p);
9832 }
9833 }
9834 if (c >= '0' && c <= '7') {
9835 /* octal */
9836 octal_number:
9837 do {
9838 if (c == '_') {
9839 if (nondigit) break;
9840 nondigit = c;
9841 continue;
9842 }
9843 if (c < '0' || c > '9') break;
9844 if (c > '7') goto invalid_octal;
9845 nondigit = 0;
9846 tokadd(p, c);
9847 } while ((c = nextc(p)) != -1);
9848 if (toklen(p) > start) {
9849 pushback(p, c);
9850 tokfix(p);
9851 if (nondigit) goto trailing_uc;
9852 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9853 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9854 }
9855 if (nondigit) {
9856 pushback(p, c);
9857 goto trailing_uc;
9858 }
9859 }
9860 if (c > '7' && c <= '9') {
9861 invalid_octal:
9862 yyerror0("Invalid octal digit");
9863 }
9864 else if (c == '.' || c == 'e' || c == 'E') {
9865 tokadd(p, '0');
9866 }
9867 else {
9868 pushback(p, c);
9869 tokfix(p);
9870 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9871 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9872 }
9873 }
9874
9875 for (;;) {
9876 switch (c) {
9877 case '0': case '1': case '2': case '3': case '4':
9878 case '5': case '6': case '7': case '8': case '9':
9879 nondigit = 0;
9880 tokadd(p, c);
9881 break;
9882
9883 case '.':
9884 if (nondigit) goto trailing_uc;
9885 if (seen_point || seen_e) {
9886 goto decode_num;
9887 }
9888 else {
9889 int c0 = nextc(p);
9890 if (c0 == -1 || !ISDIGIT(c0)) {
9891 pushback(p, c0);
9892 goto decode_num;
9893 }
9894 c = c0;
9895 }
9896 seen_point = toklen(p);
9897 tokadd(p, '.');
9898 tokadd(p, c);
9899 is_float++;
9900 nondigit = 0;
9901 break;
9902
9903 case 'e':
9904 case 'E':
9905 if (nondigit) {
9906 pushback(p, c);
9907 c = nondigit;
9908 goto decode_num;
9909 }
9910 if (seen_e) {
9911 goto decode_num;
9912 }
9913 nondigit = c;
9914 c = nextc(p);
9915 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9916 pushback(p, c);
9917 c = nondigit;
9918 nondigit = 0;
9919 goto decode_num;
9920 }
9921 tokadd(p, nondigit);
9922 seen_e++;
9923 is_float++;
9924 tokadd(p, c);
9925 nondigit = (c == '-' || c == '+') ? c : 0;
9926 break;
9927
9928 case '_': /* `_' in number just ignored */
9929 if (nondigit) goto decode_num;
9930 nondigit = c;
9931 break;
9932
9933 default:
9934 goto decode_num;
9935 }
9936 c = nextc(p);
9937 }
9938
9939 decode_num:
9940 pushback(p, c);
9941 if (nondigit) {
9942 trailing_uc:
9943 literal_flush(p, p->lex.pcur - 1);
9944 YYLTYPE loc = RUBY_INIT_YYLLOC();
9945 compile_error(p, "trailing '%c' in number", nondigit);
9946 parser_show_error_line(p, &loc);
9947 }
9948 tokfix(p);
9949 if (is_float) {
9950 enum yytokentype type = tFLOAT;
9951
9952 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9953 if (suffix & NUM_SUFFIX_R) {
9954 type = tRATIONAL;
9955 }
9956 else {
9957 strtod(tok(p), 0);
9958 if (errno == ERANGE) {
9959 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9960 errno = 0;
9961 }
9962 }
9963 return set_number_literal(p, type, suffix, 0, seen_point);
9964 }
9965 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9966 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9967}
9968
9969static enum yytokentype
9970parse_qmark(struct parser_params *p, int space_seen)
9971{
9972 rb_encoding *enc;
9973 register int c;
9974 rb_parser_string_t *lit;
9975 const char *start = p->lex.pcur;
9976
9977 if (IS_END()) {
9978 SET_LEX_STATE(EXPR_VALUE);
9979 return '?';
9980 }
9981 c = nextc(p);
9982 if (c == -1) {
9983 compile_error(p, "incomplete character syntax");
9984 return 0;
9985 }
9986 if (rb_enc_isspace(c, p->enc)) {
9987 if (!IS_ARG()) {
9988 int c2 = escaped_control_code(c);
9989 if (c2) {
9990 WARN_SPACE_CHAR(c2, "?");
9991 }
9992 }
9993 ternary:
9994 pushback(p, c);
9995 SET_LEX_STATE(EXPR_VALUE);
9996 return '?';
9997 }
9998 newtok(p);
9999 enc = p->enc;
10000 int w = parser_precise_mbclen(p, start);
10001 if (is_identchar(p, start, p->lex.pend, p->enc) &&
10002 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
10003 if (space_seen) {
10004 const char *ptr = start;
10005 do {
10006 int n = parser_precise_mbclen(p, ptr);
10007 if (n < 0) return -1;
10008 ptr += n;
10009 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
10010 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
10011 " a conditional operator, put a space after '?'",
10012 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
10013 }
10014 goto ternary;
10015 }
10016 else if (c == '\\') {
10017 if (peek(p, 'u')) {
10018 nextc(p);
10019 enc = rb_utf8_encoding();
10020 tokadd_utf8(p, &enc, -1, 0, 0);
10021 }
10022 else if (!ISASCII(c = peekc(p)) && c != -1) {
10023 nextc(p);
10024 if (tokadd_mbchar(p, c) == -1) return 0;
10025 }
10026 else {
10027 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10028 tokadd(p, c);
10029 }
10030 }
10031 else {
10032 if (tokadd_mbchar(p, c) == -1) return 0;
10033 }
10034 tokfix(p);
10035 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10036 set_yylval_str(lit);
10037 SET_LEX_STATE(EXPR_END);
10038 return tCHAR;
10039}
10040
10041static enum yytokentype
10042parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10043{
10044 register int c;
10045 const char *ptok = p->lex.pcur;
10046
10047 if (IS_BEG()) {
10048 int term;
10049 int paren;
10050
10051 c = nextc(p);
10052 quotation:
10053 if (c == -1) goto unterminated;
10054 if (!ISALNUM(c)) {
10055 term = c;
10056 if (!ISASCII(c)) goto unknown;
10057 c = 'Q';
10058 }
10059 else {
10060 term = nextc(p);
10061 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10062 unknown:
10063 pushback(p, term);
10064 c = parser_precise_mbclen(p, p->lex.pcur);
10065 if (c < 0) return 0;
10066 p->lex.pcur += c;
10067 yyerror0("unknown type of %string");
10068 return 0;
10069 }
10070 }
10071 if (term == -1) {
10072 unterminated:
10073 compile_error(p, "unterminated quoted string meets end of file");
10074 return 0;
10075 }
10076 paren = term;
10077 if (term == '(') term = ')';
10078 else if (term == '[') term = ']';
10079 else if (term == '{') term = '}';
10080 else if (term == '<') term = '>';
10081 else paren = 0;
10082
10083 p->lex.ptok = ptok-1;
10084 switch (c) {
10085 case 'Q':
10086 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10087 return tSTRING_BEG;
10088
10089 case 'q':
10090 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10091 return tSTRING_BEG;
10092
10093 case 'W':
10094 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10095 return tWORDS_BEG;
10096
10097 case 'w':
10098 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10099 return tQWORDS_BEG;
10100
10101 case 'I':
10102 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10103 return tSYMBOLS_BEG;
10104
10105 case 'i':
10106 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10107 return tQSYMBOLS_BEG;
10108
10109 case 'x':
10110 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10111 return tXSTRING_BEG;
10112
10113 case 'r':
10114 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10115 return tREGEXP_BEG;
10116
10117 case 's':
10118 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10119 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10120 return tSYMBEG;
10121
10122 default:
10123 yyerror0("unknown type of %string");
10124 return 0;
10125 }
10126 }
10127 if ((c = nextc(p)) == '=') {
10128 set_yylval_id('%');
10129 SET_LEX_STATE(EXPR_BEG);
10130 return tOP_ASGN;
10131 }
10132 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10133 goto quotation;
10134 }
10135 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10136 pushback(p, c);
10137 return warn_balanced('%', "%%", "string literal");
10138}
10139
10140static int
10141tokadd_ident(struct parser_params *p, int c)
10142{
10143 do {
10144 if (tokadd_mbchar(p, c) == -1) return -1;
10145 c = nextc(p);
10146 } while (parser_is_identchar(p));
10147 pushback(p, c);
10148 return 0;
10149}
10150
10151static ID
10152tokenize_ident(struct parser_params *p)
10153{
10154 ID ident = TOK_INTERN();
10155
10156 set_yylval_name(ident);
10157
10158 return ident;
10159}
10160
10161static int
10162parse_numvar(struct parser_params *p)
10163{
10164 size_t len;
10165 int overflow;
10166 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10167 const unsigned long nth_ref_max =
10168 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10169 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10170 * turned into a Fixnum, in compile.c */
10171
10172 if (overflow || n > nth_ref_max) {
10173 /* compile_error()? */
10174 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10175 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10176 }
10177 else {
10178 return (int)n;
10179 }
10180}
10181
10182static enum yytokentype
10183parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10184{
10185 const char *ptr = p->lex.pcur;
10186 register int c;
10187
10188 SET_LEX_STATE(EXPR_END);
10189 p->lex.ptok = ptr - 1; /* from '$' */
10190 newtok(p);
10191 c = nextc(p);
10192 switch (c) {
10193 case '_': /* $_: last read line string */
10194 c = nextc(p);
10195 if (parser_is_identchar(p)) {
10196 tokadd(p, '$');
10197 tokadd(p, '_');
10198 break;
10199 }
10200 pushback(p, c);
10201 c = '_';
10202 /* fall through */
10203 case '~': /* $~: match-data */
10204 case '*': /* $*: argv */
10205 case '$': /* $$: pid */
10206 case '?': /* $?: last status */
10207 case '!': /* $!: error string */
10208 case '@': /* $@: error position */
10209 case '/': /* $/: input record separator */
10210 case '\\': /* $\: output record separator */
10211 case ';': /* $;: field separator */
10212 case ',': /* $,: output field separator */
10213 case '.': /* $.: last read line number */
10214 case '=': /* $=: ignorecase */
10215 case ':': /* $:: load path */
10216 case '<': /* $<: default input handle */
10217 case '>': /* $>: default output handle */
10218 case '\"': /* $": already loaded files */
10219 tokadd(p, '$');
10220 tokadd(p, c);
10221 goto gvar;
10222
10223 case '-':
10224 tokadd(p, '$');
10225 tokadd(p, c);
10226 c = nextc(p);
10227 if (parser_is_identchar(p)) {
10228 if (tokadd_mbchar(p, c) == -1) return 0;
10229 }
10230 else {
10231 pushback(p, c);
10232 pushback(p, '-');
10233 return '$';
10234 }
10235 gvar:
10236 tokenize_ident(p);
10237 return tGVAR;
10238
10239 case '&': /* $&: last match */
10240 case '`': /* $`: string before last match */
10241 case '\'': /* $': string after last match */
10242 case '+': /* $+: string matches last paren. */
10243 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10244 tokadd(p, '$');
10245 tokadd(p, c);
10246 goto gvar;
10247 }
10248 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10249 return tBACK_REF;
10250
10251 case '1': case '2': case '3':
10252 case '4': case '5': case '6':
10253 case '7': case '8': case '9':
10254 tokadd(p, '$');
10255 do {
10256 tokadd(p, c);
10257 c = nextc(p);
10258 } while (c != -1 && ISDIGIT(c));
10259 pushback(p, c);
10260 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10261 tokfix(p);
10262 c = parse_numvar(p);
10263 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10264 return tNTH_REF;
10265
10266 default:
10267 if (!parser_is_identchar(p)) {
10268 YYLTYPE loc = RUBY_INIT_YYLLOC();
10269 if (c == -1 || ISSPACE(c)) {
10270 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10271 }
10272 else {
10273 pushback(p, c);
10274 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10275 }
10276 parser_show_error_line(p, &loc);
10277 set_yylval_noname();
10278 return tGVAR;
10279 }
10280 /* fall through */
10281 case '0':
10282 tokadd(p, '$');
10283 }
10284
10285 if (tokadd_ident(p, c)) return 0;
10286 SET_LEX_STATE(EXPR_END);
10287 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10288 tokenize_ident(p);
10289 }
10290 else {
10291 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10292 set_yylval_noname();
10293 }
10294 return tGVAR;
10295}
10296
10297static bool
10298parser_numbered_param(struct parser_params *p, int n)
10299{
10300 if (n < 0) return false;
10301
10302 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10303 return false;
10304 }
10305 if (p->max_numparam == ORDINAL_PARAM) {
10306 compile_error(p, "ordinary parameter is defined");
10307 return false;
10308 }
10309 struct vtable *args = p->lvtbl->args;
10310 if (p->max_numparam < n) {
10311 p->max_numparam = n;
10312 }
10313 while (n > args->pos) {
10314 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10315 }
10316 return true;
10317}
10318
10319static enum yytokentype
10320parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10321{
10322 const char *ptr = p->lex.pcur;
10323 enum yytokentype result = tIVAR;
10324 register int c = nextc(p);
10325 YYLTYPE loc;
10326
10327 p->lex.ptok = ptr - 1; /* from '@' */
10328 newtok(p);
10329 tokadd(p, '@');
10330 if (c == '@') {
10331 result = tCVAR;
10332 tokadd(p, '@');
10333 c = nextc(p);
10334 }
10335 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10336 if (c == -1 || !parser_is_identchar(p)) {
10337 pushback(p, c);
10338 RUBY_SET_YYLLOC(loc);
10339 if (result == tIVAR) {
10340 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10341 }
10342 else {
10343 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10344 }
10345 parser_show_error_line(p, &loc);
10346 set_yylval_noname();
10347 SET_LEX_STATE(EXPR_END);
10348 return result;
10349 }
10350 else if (ISDIGIT(c)) {
10351 pushback(p, c);
10352 RUBY_SET_YYLLOC(loc);
10353 if (result == tIVAR) {
10354 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10355 }
10356 else {
10357 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10358 }
10359 parser_show_error_line(p, &loc);
10360 set_yylval_noname();
10361 SET_LEX_STATE(EXPR_END);
10362 return result;
10363 }
10364
10365 if (tokadd_ident(p, c)) return 0;
10366 tokenize_ident(p);
10367 return result;
10368}
10369
10370static enum yytokentype
10371parse_ident(struct parser_params *p, int c, int cmd_state)
10372{
10373 enum yytokentype result;
10374 bool is_ascii = true;
10375 const enum lex_state_e last_state = p->lex.state;
10376 ID ident;
10377 int enforce_keyword_end = 0;
10378
10379 do {
10380 if (!ISASCII(c)) is_ascii = false;
10381 if (tokadd_mbchar(p, c) == -1) return 0;
10382 c = nextc(p);
10383 } while (parser_is_identchar(p));
10384 if ((c == '!' || c == '?') && !peek(p, '=')) {
10385 result = tFID;
10386 tokadd(p, c);
10387 }
10388 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10389 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10390 result = tIDENTIFIER;
10391 tokadd(p, c);
10392 }
10393 else {
10394 result = tCONSTANT; /* assume provisionally */
10395 pushback(p, c);
10396 }
10397 tokfix(p);
10398
10399 if (IS_LABEL_POSSIBLE()) {
10400 if (IS_LABEL_SUFFIX(0)) {
10401 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10402 nextc(p);
10403 tokenize_ident(p);
10404 return tLABEL;
10405 }
10406 }
10407
10408#ifndef RIPPER
10409 if (peek_end_expect_token_locations(p)) {
10410 const rb_code_position_t *end_pos;
10411 int lineno, column;
10412 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
10413
10414 end_pos = peek_end_expect_token_locations(p)->pos;
10415 lineno = end_pos->lineno;
10416 column = end_pos->column;
10417
10418 if (p->debug) {
10419 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10420 p->ruby_sourceline, beg_pos, lineno, column);
10421 }
10422
10423 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10424 const struct kwtable *kw;
10425
10426 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10427 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10428 enforce_keyword_end = 1;
10429 }
10430 }
10431 }
10432#endif
10433
10434 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10435 const struct kwtable *kw;
10436
10437 /* See if it is a reserved word. */
10438 kw = rb_reserved_word(tok(p), toklen(p));
10439 if (kw) {
10440 enum lex_state_e state = p->lex.state;
10441 if (IS_lex_state_for(state, EXPR_FNAME)) {
10442 SET_LEX_STATE(EXPR_ENDFN);
10443 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10444 return kw->id[0];
10445 }
10446 SET_LEX_STATE(kw->state);
10447 if (IS_lex_state(EXPR_BEG)) {
10448 p->command_start = TRUE;
10449 }
10450 if (kw->id[0] == keyword_do) {
10451 if (lambda_beginning_p()) {
10452 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10453 return keyword_do_LAMBDA;
10454 }
10455 if (COND_P()) return keyword_do_cond;
10456 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10457 return keyword_do_block;
10458 return keyword_do;
10459 }
10460 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10461 return kw->id[0];
10462 else {
10463 if (kw->id[0] != kw->id[1])
10464 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10465 return kw->id[1];
10466 }
10467 }
10468 }
10469
10470 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10471 if (cmd_state) {
10472 SET_LEX_STATE(EXPR_CMDARG);
10473 }
10474 else {
10475 SET_LEX_STATE(EXPR_ARG);
10476 }
10477 }
10478 else if (p->lex.state == EXPR_FNAME) {
10479 SET_LEX_STATE(EXPR_ENDFN);
10480 }
10481 else {
10482 SET_LEX_STATE(EXPR_END);
10483 }
10484
10485 ident = tokenize_ident(p);
10486 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10487 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10488 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10489 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10490 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10491 }
10492 return result;
10493}
10494
10495static void
10496warn_cr(struct parser_params *p)
10497{
10498 if (!p->cr_seen) {
10499 p->cr_seen = TRUE;
10500 /* carried over with p->lex.nextline for nextc() */
10501 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10502 }
10503}
10504
10505static enum yytokentype
10506parser_yylex(struct parser_params *p)
10507{
10508 register int c;
10509 int space_seen = 0;
10510 int cmd_state;
10511 int label;
10512 enum lex_state_e last_state;
10513 int fallthru = FALSE;
10514 int token_seen = p->token_seen;
10515
10516 if (p->lex.strterm) {
10517 if (strterm_is_heredoc(p->lex.strterm)) {
10518 token_flush(p);
10519 return here_document(p, &p->lex.strterm->u.heredoc);
10520 }
10521 else {
10522 token_flush(p);
10523 return parse_string(p, &p->lex.strterm->u.literal);
10524 }
10525 }
10526 cmd_state = p->command_start;
10527 p->command_start = FALSE;
10528 p->token_seen = TRUE;
10529#ifndef RIPPER
10530 token_flush(p);
10531#endif
10532 retry:
10533 last_state = p->lex.state;
10534 switch (c = nextc(p)) {
10535 case '\0': /* NUL */
10536 case '\004': /* ^D */
10537 case '\032': /* ^Z */
10538 case -1: /* end of script. */
10539 p->eofp = 1;
10540#ifndef RIPPER
10541 if (p->end_expect_token_locations) {
10542 pop_end_expect_token_locations(p);
10543 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10544 return tDUMNY_END;
10545 }
10546#endif
10547 /* Set location for end-of-input because dispatch_scan_event is not called. */
10548 RUBY_SET_YYLLOC(*p->yylloc);
10549 return END_OF_INPUT;
10550
10551 /* white spaces */
10552 case '\r':
10553 warn_cr(p);
10554 /* fall through */
10555 case ' ': case '\t': case '\f':
10556 case '\13': /* '\v' */
10557 space_seen = 1;
10558 while ((c = nextc(p))) {
10559 switch (c) {
10560 case '\r':
10561 warn_cr(p);
10562 /* fall through */
10563 case ' ': case '\t': case '\f':
10564 case '\13': /* '\v' */
10565 break;
10566 default:
10567 goto outofloop;
10568 }
10569 }
10570 outofloop:
10571 pushback(p, c);
10572 dispatch_scan_event(p, tSP);
10573#ifndef RIPPER
10574 token_flush(p);
10575#endif
10576 goto retry;
10577
10578 case '#': /* it's a comment */
10579 p->token_seen = token_seen;
10580 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10581 /* no magic_comment in shebang line */
10582 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10583 if (comment_at_top(p)) {
10584 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10585 }
10586 }
10587 p->lex.pcur = pcur, p->lex.ptok = ptok;
10588 lex_goto_eol(p);
10589 dispatch_scan_event(p, tCOMMENT);
10590 fallthru = TRUE;
10591 /* fall through */
10592 case '\n':
10593 p->token_seen = token_seen;
10594 rb_parser_string_t *prevline = p->lex.lastline;
10595 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10596 !IS_lex_state(EXPR_LABELED));
10597 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10598 if (!fallthru) {
10599 dispatch_scan_event(p, tIGNORED_NL);
10600 }
10601 fallthru = FALSE;
10602 if (!c && p->ctxt.in_kwarg) {
10603 goto normal_newline;
10604 }
10605 goto retry;
10606 }
10607 while (1) {
10608 switch (c = nextc(p)) {
10609 case ' ': case '\t': case '\f': case '\r':
10610 case '\13': /* '\v' */
10611 space_seen = 1;
10612 break;
10613 case '#':
10614 pushback(p, c);
10615 if (space_seen) {
10616 dispatch_scan_event(p, tSP);
10617 token_flush(p);
10618 }
10619 goto retry;
10620 case 'a':
10621 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10622 goto bol;
10623 case 'o':
10624 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10625 goto bol;
10626 case '|':
10627 if (peek(p, '|') && !peek_n(p, '=', 1)) goto leading_logical;
10628 goto bol;
10629 case '&':
10630 if (peek(p, '&') && !peek_n(p, '=', 1)) {
10631 leading_logical:
10632 pushback(p, c);
10633 dispatch_delayed_token(p, tIGNORED_NL);
10634 cmd_state = FALSE;
10635 goto retry;
10636 }
10637 /* fall through */
10638 case '.': {
10639 dispatch_delayed_token(p, tIGNORED_NL);
10640 if (peek(p, '.') == (c == '&')) {
10641 pushback(p, c);
10642 dispatch_scan_event(p, tSP);
10643 goto retry;
10644 }
10645 }
10646 bol:
10647 default:
10648 p->ruby_sourceline--;
10649 p->lex.nextline = p->lex.lastline;
10650 set_lastline(p, prevline);
10651 case -1: /* EOF no decrement*/
10652 if (c == -1 && space_seen) {
10653 dispatch_scan_event(p, tSP);
10654 }
10655 lex_goto_eol(p);
10656 if (c != -1) {
10657 token_flush(p);
10658 RUBY_SET_YYLLOC(*p->yylloc);
10659 }
10660 goto normal_newline;
10661 }
10662 }
10663 normal_newline:
10664 p->command_start = TRUE;
10665 SET_LEX_STATE(EXPR_BEG);
10666 return '\n';
10667
10668 case '*':
10669 if ((c = nextc(p)) == '*') {
10670 if ((c = nextc(p)) == '=') {
10671 set_yylval_id(idPow);
10672 SET_LEX_STATE(EXPR_BEG);
10673 return tOP_ASGN;
10674 }
10675 pushback(p, c);
10676 if (IS_SPCARG(c)) {
10677 rb_warning0("'**' interpreted as argument prefix");
10678 c = tDSTAR;
10679 }
10680 else if (IS_BEG()) {
10681 c = tDSTAR;
10682 }
10683 else {
10684 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10685 }
10686 }
10687 else {
10688 if (c == '=') {
10689 set_yylval_id('*');
10690 SET_LEX_STATE(EXPR_BEG);
10691 return tOP_ASGN;
10692 }
10693 pushback(p, c);
10694 if (IS_SPCARG(c)) {
10695 rb_warning0("'*' interpreted as argument prefix");
10696 c = tSTAR;
10697 }
10698 else if (IS_BEG()) {
10699 c = tSTAR;
10700 }
10701 else {
10702 c = warn_balanced('*', "*", "argument prefix");
10703 }
10704 }
10705 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10706 return c;
10707
10708 case '!':
10709 c = nextc(p);
10710 if (IS_AFTER_OPERATOR()) {
10711 SET_LEX_STATE(EXPR_ARG);
10712 if (c == '@') {
10713 return '!';
10714 }
10715 }
10716 else {
10717 SET_LEX_STATE(EXPR_BEG);
10718 }
10719 if (c == '=') {
10720 return tNEQ;
10721 }
10722 if (c == '~') {
10723 return tNMATCH;
10724 }
10725 pushback(p, c);
10726 return '!';
10727
10728 case '=':
10729 if (was_bol(p)) {
10730 /* skip embedded rd document */
10731 if (word_match_p(p, "begin", 5)) {
10732 int first_p = TRUE;
10733
10734 lex_goto_eol(p);
10735 dispatch_scan_event(p, tEMBDOC_BEG);
10736 for (;;) {
10737 lex_goto_eol(p);
10738 if (!first_p) {
10739 dispatch_scan_event(p, tEMBDOC);
10740 }
10741 first_p = FALSE;
10742 c = nextc(p);
10743 if (c == -1) {
10744 compile_error(p, "embedded document meets end of file");
10745 return END_OF_INPUT;
10746 }
10747 if (c == '=' && word_match_p(p, "end", 3)) {
10748 break;
10749 }
10750 pushback(p, c);
10751 }
10752 lex_goto_eol(p);
10753 dispatch_scan_event(p, tEMBDOC_END);
10754 goto retry;
10755 }
10756 }
10757
10758 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10759 if ((c = nextc(p)) == '=') {
10760 if ((c = nextc(p)) == '=') {
10761 return tEQQ;
10762 }
10763 pushback(p, c);
10764 return tEQ;
10765 }
10766 if (c == '~') {
10767 return tMATCH;
10768 }
10769 else if (c == '>') {
10770 return tASSOC;
10771 }
10772 pushback(p, c);
10773 return '=';
10774
10775 case '<':
10776 c = nextc(p);
10777 if (c == '<' &&
10778 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10779 !IS_END() &&
10780 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10781 enum yytokentype token = heredoc_identifier(p);
10782 if (token) return token < 0 ? 0 : token;
10783 }
10784 if (IS_AFTER_OPERATOR()) {
10785 SET_LEX_STATE(EXPR_ARG);
10786 }
10787 else {
10788 if (IS_lex_state(EXPR_CLASS))
10789 p->command_start = TRUE;
10790 SET_LEX_STATE(EXPR_BEG);
10791 }
10792 if (c == '=') {
10793 if ((c = nextc(p)) == '>') {
10794 return tCMP;
10795 }
10796 pushback(p, c);
10797 return tLEQ;
10798 }
10799 if (c == '<') {
10800 if ((c = nextc(p)) == '=') {
10801 set_yylval_id(idLTLT);
10802 SET_LEX_STATE(EXPR_BEG);
10803 return tOP_ASGN;
10804 }
10805 pushback(p, c);
10806 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10807 }
10808 pushback(p, c);
10809 return '<';
10810
10811 case '>':
10812 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10813 if ((c = nextc(p)) == '=') {
10814 return tGEQ;
10815 }
10816 if (c == '>') {
10817 if ((c = nextc(p)) == '=') {
10818 set_yylval_id(idGTGT);
10819 SET_LEX_STATE(EXPR_BEG);
10820 return tOP_ASGN;
10821 }
10822 pushback(p, c);
10823 return tRSHFT;
10824 }
10825 pushback(p, c);
10826 return '>';
10827
10828 case '"':
10829 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10830 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10831 p->lex.ptok = p->lex.pcur-1;
10832 return tSTRING_BEG;
10833
10834 case '`':
10835 if (IS_lex_state(EXPR_FNAME)) {
10836 SET_LEX_STATE(EXPR_ENDFN);
10837 return c;
10838 }
10839 if (IS_lex_state(EXPR_DOT)) {
10840 if (cmd_state)
10841 SET_LEX_STATE(EXPR_CMDARG);
10842 else
10843 SET_LEX_STATE(EXPR_ARG);
10844 return c;
10845 }
10846 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10847 return tXSTRING_BEG;
10848
10849 case '\'':
10850 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10851 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10852 p->lex.ptok = p->lex.pcur-1;
10853 return tSTRING_BEG;
10854
10855 case '?':
10856 return parse_qmark(p, space_seen);
10857
10858 case '&':
10859 if ((c = nextc(p)) == '&') {
10860 SET_LEX_STATE(EXPR_BEG);
10861 if ((c = nextc(p)) == '=') {
10862 set_yylval_id(idANDOP);
10863 SET_LEX_STATE(EXPR_BEG);
10864 return tOP_ASGN;
10865 }
10866 pushback(p, c);
10867 return tANDOP;
10868 }
10869 else if (c == '=') {
10870 set_yylval_id('&');
10871 SET_LEX_STATE(EXPR_BEG);
10872 return tOP_ASGN;
10873 }
10874 else if (c == '.') {
10875 set_yylval_id(idANDDOT);
10876 SET_LEX_STATE(EXPR_DOT);
10877 return tANDDOT;
10878 }
10879 pushback(p, c);
10880 if (IS_SPCARG(c)) {
10881 if ((c != ':') ||
10882 (c = peekc_n(p, 1)) == -1 ||
10883 !(c == '\'' || c == '"' ||
10884 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10885 rb_warning0("'&' interpreted as argument prefix");
10886 }
10887 c = tAMPER;
10888 }
10889 else if (IS_BEG()) {
10890 c = tAMPER;
10891 }
10892 else {
10893 c = warn_balanced('&', "&", "argument prefix");
10894 }
10895 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10896 return c;
10897
10898 case '|':
10899 if ((c = nextc(p)) == '|') {
10900 SET_LEX_STATE(EXPR_BEG);
10901 if ((c = nextc(p)) == '=') {
10902 set_yylval_id(idOROP);
10903 SET_LEX_STATE(EXPR_BEG);
10904 return tOP_ASGN;
10905 }
10906 pushback(p, c);
10907 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10908 c = '|';
10909 pushback(p, '|');
10910 return c;
10911 }
10912 return tOROP;
10913 }
10914 if (c == '=') {
10915 set_yylval_id('|');
10916 SET_LEX_STATE(EXPR_BEG);
10917 return tOP_ASGN;
10918 }
10919 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10920 pushback(p, c);
10921 return '|';
10922
10923 case '+':
10924 c = nextc(p);
10925 if (IS_AFTER_OPERATOR()) {
10926 SET_LEX_STATE(EXPR_ARG);
10927 if (c == '@') {
10928 return tUPLUS;
10929 }
10930 pushback(p, c);
10931 return '+';
10932 }
10933 if (c == '=') {
10934 set_yylval_id('+');
10935 SET_LEX_STATE(EXPR_BEG);
10936 return tOP_ASGN;
10937 }
10938 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10939 SET_LEX_STATE(EXPR_BEG);
10940 pushback(p, c);
10941 if (c != -1 && ISDIGIT(c)) {
10942 return parse_numeric(p, '+');
10943 }
10944 return tUPLUS;
10945 }
10946 SET_LEX_STATE(EXPR_BEG);
10947 pushback(p, c);
10948 return warn_balanced('+', "+", "unary operator");
10949
10950 case '-':
10951 c = nextc(p);
10952 if (IS_AFTER_OPERATOR()) {
10953 SET_LEX_STATE(EXPR_ARG);
10954 if (c == '@') {
10955 return tUMINUS;
10956 }
10957 pushback(p, c);
10958 return '-';
10959 }
10960 if (c == '=') {
10961 set_yylval_id('-');
10962 SET_LEX_STATE(EXPR_BEG);
10963 return tOP_ASGN;
10964 }
10965 if (c == '>') {
10966 SET_LEX_STATE(EXPR_ENDFN);
10967 yylval.num = p->lex.lpar_beg;
10968 p->lex.lpar_beg = p->lex.paren_nest;
10969 return tLAMBDA;
10970 }
10971 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10972 SET_LEX_STATE(EXPR_BEG);
10973 pushback(p, c);
10974 if (c != -1 && ISDIGIT(c)) {
10975 return tUMINUS_NUM;
10976 }
10977 return tUMINUS;
10978 }
10979 SET_LEX_STATE(EXPR_BEG);
10980 pushback(p, c);
10981 return warn_balanced('-', "-", "unary operator");
10982
10983 case '.': {
10984 int is_beg = IS_BEG();
10985 SET_LEX_STATE(EXPR_BEG);
10986 if ((c = nextc(p)) == '.') {
10987 if ((c = nextc(p)) == '.') {
10988 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10989 SET_LEX_STATE(EXPR_ENDARG);
10990 return tBDOT3;
10991 }
10992 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10993 rb_warn0("... at EOL, should be parenthesized?");
10994 }
10995 return is_beg ? tBDOT3 : tDOT3;
10996 }
10997 pushback(p, c);
10998 return is_beg ? tBDOT2 : tDOT2;
10999 }
11000 pushback(p, c);
11001 if (c != -1 && ISDIGIT(c)) {
11002 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
11003 parse_numeric(p, '.');
11004 if (ISDIGIT(prev)) {
11005 yyerror0("unexpected fraction part after numeric literal");
11006 }
11007 else {
11008 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
11009 }
11010 set_yylval_id(idNULL);
11011 SET_LEX_STATE(EXPR_END);
11012 p->lex.ptok = p->lex.pcur;
11013 goto retry;
11014 }
11015 set_yylval_id('.');
11016 SET_LEX_STATE(EXPR_DOT);
11017 return '.';
11018 }
11019
11020 case '0': case '1': case '2': case '3': case '4':
11021 case '5': case '6': case '7': case '8': case '9':
11022 return parse_numeric(p, c);
11023
11024 case ')':
11025 COND_POP();
11026 CMDARG_POP();
11027 SET_LEX_STATE(EXPR_ENDFN);
11028 p->lex.paren_nest--;
11029 return c;
11030
11031 case ']':
11032 COND_POP();
11033 CMDARG_POP();
11034 SET_LEX_STATE(EXPR_END);
11035 p->lex.paren_nest--;
11036 return c;
11037
11038 case '}':
11039 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11040 if (!p->lex.brace_nest--) return tSTRING_DEND;
11041 COND_POP();
11042 CMDARG_POP();
11043 SET_LEX_STATE(EXPR_END);
11044 p->lex.paren_nest--;
11045 return c;
11046
11047 case ':':
11048 c = nextc(p);
11049 if (c == ':') {
11050 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11051 SET_LEX_STATE(EXPR_BEG);
11052 return tCOLON3;
11053 }
11054 set_yylval_id(idCOLON2);
11055 SET_LEX_STATE(EXPR_DOT);
11056 return tCOLON2;
11057 }
11058 if (IS_END() || ISSPACE(c) || c == '#') {
11059 pushback(p, c);
11060 c = warn_balanced(':', ":", "symbol literal");
11061 SET_LEX_STATE(EXPR_BEG);
11062 return c;
11063 }
11064 switch (c) {
11065 case '\'':
11066 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11067 break;
11068 case '"':
11069 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11070 break;
11071 default:
11072 pushback(p, c);
11073 break;
11074 }
11075 SET_LEX_STATE(EXPR_FNAME);
11076 return tSYMBEG;
11077
11078 case '/':
11079 if (IS_BEG()) {
11080 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11081 return tREGEXP_BEG;
11082 }
11083 if ((c = nextc(p)) == '=') {
11084 set_yylval_id('/');
11085 SET_LEX_STATE(EXPR_BEG);
11086 return tOP_ASGN;
11087 }
11088 pushback(p, c);
11089 if (IS_SPCARG(c)) {
11090 arg_ambiguous(p, '/');
11091 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11092 return tREGEXP_BEG;
11093 }
11094 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11095 return warn_balanced('/', "/", "regexp literal");
11096
11097 case '^':
11098 if ((c = nextc(p)) == '=') {
11099 set_yylval_id('^');
11100 SET_LEX_STATE(EXPR_BEG);
11101 return tOP_ASGN;
11102 }
11103 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11104 pushback(p, c);
11105 return '^';
11106
11107 case ';':
11108 SET_LEX_STATE(EXPR_BEG);
11109 p->command_start = TRUE;
11110 return ';';
11111
11112 case ',':
11113 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11114 return ',';
11115
11116 case '~':
11117 if (IS_AFTER_OPERATOR()) {
11118 if ((c = nextc(p)) != '@') {
11119 pushback(p, c);
11120 }
11121 SET_LEX_STATE(EXPR_ARG);
11122 }
11123 else {
11124 SET_LEX_STATE(EXPR_BEG);
11125 }
11126 return '~';
11127
11128 case '(':
11129 if (IS_BEG()) {
11130 c = tLPAREN;
11131 }
11132 else if (!space_seen) {
11133 /* foo( ... ) => method call, no ambiguity */
11134 }
11135 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11136 c = tLPAREN_ARG;
11137 }
11138 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11139 rb_warning0("parentheses after method name is interpreted as "
11140 "an argument list, not a decomposed argument");
11141 }
11142 p->lex.paren_nest++;
11143 COND_PUSH(0);
11144 CMDARG_PUSH(0);
11145 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11146 return c;
11147
11148 case '[':
11149 p->lex.paren_nest++;
11150 if (IS_AFTER_OPERATOR()) {
11151 if ((c = nextc(p)) == ']') {
11152 p->lex.paren_nest--;
11153 SET_LEX_STATE(EXPR_ARG);
11154 if ((c = nextc(p)) == '=') {
11155 return tASET;
11156 }
11157 pushback(p, c);
11158 return tAREF;
11159 }
11160 pushback(p, c);
11161 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11162 return '[';
11163 }
11164 else if (IS_BEG()) {
11165 c = tLBRACK;
11166 }
11167 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11168 c = tLBRACK;
11169 }
11170 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11171 COND_PUSH(0);
11172 CMDARG_PUSH(0);
11173 return c;
11174
11175 case '{':
11176 ++p->lex.brace_nest;
11177 if (lambda_beginning_p())
11178 c = tLAMBEG;
11179 else if (IS_lex_state(EXPR_LABELED))
11180 c = tLBRACE; /* hash */
11181 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11182 c = '{'; /* block (primary) */
11183 else if (IS_lex_state(EXPR_ENDARG))
11184 c = tLBRACE_ARG; /* block (expr) */
11185 else
11186 c = tLBRACE; /* hash */
11187 if (c != tLBRACE) {
11188 p->command_start = TRUE;
11189 SET_LEX_STATE(EXPR_BEG);
11190 }
11191 else {
11192 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11193 }
11194 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11195 COND_PUSH(0);
11196 CMDARG_PUSH(0);
11197 return c;
11198
11199 case '\\':
11200 c = nextc(p);
11201 if (c == '\n') {
11202 space_seen = 1;
11203 dispatch_scan_event(p, tSP);
11204 goto retry; /* skip \\n */
11205 }
11206 if (c == ' ') return tSP;
11207 if (ISSPACE(c)) return c;
11208 pushback(p, c);
11209 return '\\';
11210
11211 case '%':
11212 return parse_percent(p, space_seen, last_state);
11213
11214 case '$':
11215 return parse_gvar(p, last_state);
11216
11217 case '@':
11218 return parse_atmark(p, last_state);
11219
11220 case '_':
11221 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11222 p->ruby__end__seen = 1;
11223 p->eofp = 1;
11224#ifdef RIPPER
11225 lex_goto_eol(p);
11226 dispatch_scan_event(p, k__END__);
11227#endif
11228 return END_OF_INPUT;
11229 }
11230 newtok(p);
11231 break;
11232
11233 default:
11234 if (!parser_is_identchar(p)) {
11235 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11236 token_flush(p);
11237 goto retry;
11238 }
11239
11240 newtok(p);
11241 break;
11242 }
11243
11244 return parse_ident(p, c, cmd_state);
11245}
11246
11247static enum yytokentype
11248yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11249{
11250 enum yytokentype t;
11251
11252 p->lval = lval;
11253 lval->node = 0;
11254 p->yylloc = yylloc;
11255
11256 t = parser_yylex(p);
11257
11258 if (has_delayed_token(p))
11259 dispatch_delayed_token(p, t);
11260 else if (t != END_OF_INPUT)
11261 dispatch_scan_event(p, t);
11262
11263 return t;
11264}
11265
11266#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11267
11268static NODE*
11269node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11270{
11271 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11272
11273 rb_node_init(n, type);
11274 return n;
11275}
11276
11277static NODE *
11278nd_set_loc(NODE *nd, const YYLTYPE *loc)
11279{
11280 nd->nd_loc = *loc;
11281 nd_set_line(nd, loc->beg_pos.lineno);
11282 return nd;
11283}
11284
11285static NODE*
11286node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11287{
11288 NODE *n = node_new_internal(p, type, size, alignment);
11289
11290 nd_set_loc(n, loc);
11291 nd_set_node_id(n, parser_get_node_id(p));
11292 return n;
11293}
11294
11295#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11296
11297static rb_node_scope_t *
11298rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11299{
11300 rb_ast_id_table_t *nd_tbl;
11301 nd_tbl = local_tbl(p);
11302 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11303 n->nd_tbl = nd_tbl;
11304 n->nd_body = nd_body;
11305 n->nd_parent = nd_parent;
11306 n->nd_args = nd_args;
11307
11308 return n;
11309}
11310
11311static rb_node_scope_t *
11312rb_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)
11313{
11314 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11315 n->nd_tbl = nd_tbl;
11316 n->nd_body = nd_body;
11317 n->nd_parent = nd_parent;
11318 n->nd_args = nd_args;
11319
11320 return n;
11321}
11322
11323static rb_node_defn_t *
11324rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11325{
11326 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11327 n->nd_mid = nd_mid;
11328 n->nd_defn = nd_defn;
11329
11330 return n;
11331}
11332
11333static rb_node_defs_t *
11334rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11335{
11336 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11337 n->nd_recv = nd_recv;
11338 n->nd_mid = nd_mid;
11339 n->nd_defn = nd_defn;
11340
11341 return n;
11342}
11343
11344static rb_node_block_t *
11345rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11346{
11347 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11348 n->nd_head = nd_head;
11349 n->nd_end = (NODE *)n;
11350 n->nd_next = 0;
11351
11352 return n;
11353}
11354
11355static rb_node_for_t *
11356rb_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)
11357{
11358 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11359 n->nd_body = nd_body;
11360 n->nd_iter = nd_iter;
11361 n->for_keyword_loc = *for_keyword_loc;
11362 n->in_keyword_loc = *in_keyword_loc;
11363 n->do_keyword_loc = *do_keyword_loc;
11364 n->end_keyword_loc = *end_keyword_loc;
11365
11366 return n;
11367}
11368
11369static rb_node_for_masgn_t *
11370rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11371{
11372 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11373 n->nd_var = nd_var;
11374
11375 return n;
11376}
11377
11378static rb_node_retry_t *
11379rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11380{
11381 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11382
11383 return n;
11384}
11385
11386static rb_node_begin_t *
11387rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11388{
11389 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11390 n->nd_body = nd_body;
11391
11392 return n;
11393}
11394
11395static rb_node_rescue_t *
11396rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11397{
11398 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11399 n->nd_head = nd_head;
11400 n->nd_resq = nd_resq;
11401 n->nd_else = nd_else;
11402
11403 return n;
11404}
11405
11406static rb_node_resbody_t *
11407rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11408{
11409 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11410 n->nd_args = nd_args;
11411 n->nd_exc_var = nd_exc_var;
11412 n->nd_body = nd_body;
11413 n->nd_next = nd_next;
11414
11415 return n;
11416}
11417
11418static rb_node_ensure_t *
11419rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11420{
11421 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11422 n->nd_head = nd_head;
11423 n->nd_ensr = nd_ensr;
11424
11425 return n;
11426}
11427
11428static rb_node_and_t *
11429rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11430{
11431 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11432 n->nd_1st = nd_1st;
11433 n->nd_2nd = nd_2nd;
11434 n->operator_loc = *operator_loc;
11435
11436 return n;
11437}
11438
11439static rb_node_or_t *
11440rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11441{
11442 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11443 n->nd_1st = nd_1st;
11444 n->nd_2nd = nd_2nd;
11445 n->operator_loc = *operator_loc;
11446
11447 return n;
11448}
11449
11450static rb_node_return_t *
11451rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11452{
11453 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11454 n->nd_stts = nd_stts;
11455 n->keyword_loc = *keyword_loc;
11456 return n;
11457}
11458
11459static rb_node_yield_t *
11460rb_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)
11461{
11462 if (nd_head) no_blockarg(p, nd_head);
11463
11464 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11465 n->nd_head = nd_head;
11466 n->keyword_loc = *keyword_loc;
11467 n->lparen_loc = *lparen_loc;
11468 n->rparen_loc = *rparen_loc;
11469
11470 return n;
11471}
11472
11473static rb_node_if_t *
11474rb_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)
11475{
11476 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11477 n->nd_cond = nd_cond;
11478 n->nd_body = nd_body;
11479 n->nd_else = nd_else;
11480 n->if_keyword_loc = *if_keyword_loc;
11481 n->then_keyword_loc = *then_keyword_loc;
11482 n->end_keyword_loc = *end_keyword_loc;
11483
11484 return n;
11485}
11486
11487static rb_node_unless_t *
11488rb_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)
11489{
11490 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11491 n->nd_cond = nd_cond;
11492 n->nd_body = nd_body;
11493 n->nd_else = nd_else;
11494 n->keyword_loc = *keyword_loc;
11495 n->then_keyword_loc = *then_keyword_loc;
11496 n->end_keyword_loc = *end_keyword_loc;
11497
11498 return n;
11499}
11500
11501static rb_node_class_t *
11502rb_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)
11503{
11504 /* Keep the order of node creation */
11505 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11506 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11507 RNODE_SCOPE(scope)->nd_parent = &n->node;
11508 n->nd_cpath = nd_cpath;
11509 n->nd_body = scope;
11510 n->nd_super = nd_super;
11511 n->class_keyword_loc = *class_keyword_loc;
11512 n->inheritance_operator_loc = *inheritance_operator_loc;
11513 n->end_keyword_loc = *end_keyword_loc;
11514
11515 return n;
11516}
11517
11518static rb_node_sclass_t *
11519rb_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)
11520{
11521 /* Keep the order of node creation */
11522 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11523 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11524 RNODE_SCOPE(scope)->nd_parent = &n->node;
11525 n->nd_recv = nd_recv;
11526 n->nd_body = scope;
11527 n->class_keyword_loc = *class_keyword_loc;
11528 n->operator_loc = *operator_loc;
11529 n->end_keyword_loc = *end_keyword_loc;
11530
11531 return n;
11532}
11533
11534static rb_node_module_t *
11535rb_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)
11536{
11537 /* Keep the order of node creation */
11538 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11539 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11540 RNODE_SCOPE(scope)->nd_parent = &n->node;
11541 n->nd_cpath = nd_cpath;
11542 n->nd_body = scope;
11543 n->module_keyword_loc = *module_keyword_loc;
11544 n->end_keyword_loc = *end_keyword_loc;
11545
11546 return n;
11547}
11548
11549static rb_node_iter_t *
11550rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11551{
11552 /* Keep the order of node creation */
11553 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11554 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11555 RNODE_SCOPE(scope)->nd_parent = &n->node;
11556 n->nd_body = scope;
11557 n->nd_iter = 0;
11558
11559 return n;
11560}
11561
11562static rb_node_lambda_t *
11563rb_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)
11564{
11565 /* Keep the order of node creation */
11566 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11567 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11568 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11569 RNODE_SCOPE(scope)->nd_parent = &n->node;
11570 n->nd_body = scope;
11571 n->operator_loc = *operator_loc;
11572 n->opening_loc = *opening_loc;
11573 n->closing_loc = *closing_loc;
11574
11575 return n;
11576}
11577
11578static rb_node_case_t *
11579rb_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)
11580{
11581 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11582 n->nd_head = nd_head;
11583 n->nd_body = nd_body;
11584 n->case_keyword_loc = *case_keyword_loc;
11585 n->end_keyword_loc = *end_keyword_loc;
11586
11587 return n;
11588}
11589
11590static rb_node_case2_t *
11591rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11592{
11593 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11594 n->nd_head = 0;
11595 n->nd_body = nd_body;
11596 n->case_keyword_loc = *case_keyword_loc;
11597 n->end_keyword_loc = *end_keyword_loc;
11598
11599 return n;
11600}
11601
11602static rb_node_case3_t *
11603rb_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)
11604{
11605 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11606 n->nd_head = nd_head;
11607 n->nd_body = nd_body;
11608 n->case_keyword_loc = *case_keyword_loc;
11609 n->end_keyword_loc = *end_keyword_loc;
11610
11611 return n;
11612}
11613
11614static rb_node_when_t *
11615rb_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)
11616{
11617 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11618 n->nd_head = nd_head;
11619 n->nd_body = nd_body;
11620 n->nd_next = nd_next;
11621 n->keyword_loc = *keyword_loc;
11622 n->then_keyword_loc = *then_keyword_loc;
11623
11624 return n;
11625}
11626
11627static rb_node_in_t *
11628rb_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)
11629{
11630 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11631 n->nd_head = nd_head;
11632 n->nd_body = nd_body;
11633 n->nd_next = nd_next;
11634 n->in_keyword_loc = *in_keyword_loc;
11635 n->then_keyword_loc = *then_keyword_loc;
11636 n->operator_loc = *operator_loc;
11637
11638 return n;
11639}
11640
11641static rb_node_while_t *
11642rb_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)
11643{
11644 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11645 n->nd_cond = nd_cond;
11646 n->nd_body = nd_body;
11647 n->nd_state = nd_state;
11648 n->keyword_loc = *keyword_loc;
11649 n->closing_loc = *closing_loc;
11650
11651 return n;
11652}
11653
11654static rb_node_until_t *
11655rb_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)
11656{
11657 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11658 n->nd_cond = nd_cond;
11659 n->nd_body = nd_body;
11660 n->nd_state = nd_state;
11661 n->keyword_loc = *keyword_loc;
11662 n->closing_loc = *closing_loc;
11663
11664 return n;
11665}
11666
11667static rb_node_colon2_t *
11668rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11669{
11670 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11671 n->nd_head = nd_head;
11672 n->nd_mid = nd_mid;
11673 n->delimiter_loc = *delimiter_loc;
11674 n->name_loc = *name_loc;
11675
11676 return n;
11677}
11678
11679static rb_node_colon3_t *
11680rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11681{
11682 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11683 n->nd_mid = nd_mid;
11684 n->delimiter_loc = *delimiter_loc;
11685 n->name_loc = *name_loc;
11686
11687 return n;
11688}
11689
11690static rb_node_dot2_t *
11691rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11692{
11693 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11694 n->nd_beg = nd_beg;
11695 n->nd_end = nd_end;
11696 n->operator_loc = *operator_loc;
11697
11698 return n;
11699}
11700
11701static rb_node_dot3_t *
11702rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11703{
11704 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11705 n->nd_beg = nd_beg;
11706 n->nd_end = nd_end;
11707 n->operator_loc = *operator_loc;
11708
11709 return n;
11710}
11711
11712static rb_node_self_t *
11713rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11714{
11715 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11716 n->nd_state = 1;
11717
11718 return n;
11719}
11720
11721static rb_node_nil_t *
11722rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11723{
11724 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11725
11726 return n;
11727}
11728
11729static rb_node_true_t *
11730rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11731{
11732 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11733
11734 return n;
11735}
11736
11737static rb_node_false_t *
11738rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11739{
11740 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11741
11742 return n;
11743}
11744
11745static rb_node_super_t *
11746rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11747 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11748{
11749 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11750 n->nd_args = nd_args;
11751 n->keyword_loc = *keyword_loc;
11752 n->lparen_loc = *lparen_loc;
11753 n->rparen_loc = *rparen_loc;
11754
11755 return n;
11756}
11757
11758static rb_node_zsuper_t *
11759rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11760{
11761 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11762
11763 return n;
11764}
11765
11766static rb_node_match2_t *
11767rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11768{
11769 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11770 n->nd_recv = nd_recv;
11771 n->nd_value = nd_value;
11772 n->nd_args = 0;
11773
11774 return n;
11775}
11776
11777static rb_node_match3_t *
11778rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11779{
11780 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11781 n->nd_recv = nd_recv;
11782 n->nd_value = nd_value;
11783
11784 return n;
11785}
11786
11787/* TODO: Use union for NODE_LIST2 */
11788static rb_node_list_t *
11789rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11790{
11791 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11792 n->nd_head = nd_head;
11793 n->as.nd_alen = 1;
11794 n->nd_next = 0;
11795
11796 return n;
11797}
11798
11799static rb_node_list_t *
11800rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11801{
11802 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11803 n->nd_head = nd_head;
11804 n->as.nd_alen = nd_alen;
11805 n->nd_next = nd_next;
11806
11807 return n;
11808}
11809
11810static rb_node_zlist_t *
11811rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11812{
11813 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11814
11815 return n;
11816}
11817
11818static rb_node_hash_t *
11819rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11820{
11821 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11822 n->nd_head = nd_head;
11823 n->nd_brace = 0;
11824
11825 return n;
11826}
11827
11828static rb_node_masgn_t *
11829rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11830{
11831 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11832 n->nd_head = nd_head;
11833 n->nd_value = 0;
11834 n->nd_args = nd_args;
11835
11836 return n;
11837}
11838
11839static rb_node_gasgn_t *
11840rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11841{
11842 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11843 n->nd_vid = nd_vid;
11844 n->nd_value = nd_value;
11845
11846 return n;
11847}
11848
11849static rb_node_lasgn_t *
11850rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11851{
11852 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11853 n->nd_vid = nd_vid;
11854 n->nd_value = nd_value;
11855
11856 return n;
11857}
11858
11859static rb_node_dasgn_t *
11860rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11861{
11862 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11863 n->nd_vid = nd_vid;
11864 n->nd_value = nd_value;
11865
11866 return n;
11867}
11868
11869static rb_node_iasgn_t *
11870rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11871{
11872 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11873 n->nd_vid = nd_vid;
11874 n->nd_value = nd_value;
11875
11876 return n;
11877}
11878
11879static rb_node_cvasgn_t *
11880rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11881{
11882 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11883 n->nd_vid = nd_vid;
11884 n->nd_value = nd_value;
11885
11886 return n;
11887}
11888
11889static rb_node_op_asgn1_t *
11890rb_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)
11891{
11892 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11893 n->nd_recv = nd_recv;
11894 n->nd_mid = nd_mid;
11895 n->nd_index = index;
11896 n->nd_rvalue = rvalue;
11897 n->call_operator_loc = *call_operator_loc;
11898 n->opening_loc = *opening_loc;
11899 n->closing_loc = *closing_loc;
11900 n->binary_operator_loc = *binary_operator_loc;
11901
11902 return n;
11903}
11904
11905static rb_node_op_asgn2_t *
11906rb_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)
11907{
11908 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11909 n->nd_recv = nd_recv;
11910 n->nd_value = nd_value;
11911 n->nd_vid = nd_vid;
11912 n->nd_mid = nd_mid;
11913 n->nd_aid = nd_aid;
11914 n->call_operator_loc = *call_operator_loc;
11915 n->message_loc = *message_loc;
11916 n->binary_operator_loc = *binary_operator_loc;
11917
11918 return n;
11919}
11920
11921static rb_node_op_asgn_or_t *
11922rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11923{
11924 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11925 n->nd_head = nd_head;
11926 n->nd_value = nd_value;
11927
11928 return n;
11929}
11930
11931static rb_node_op_asgn_and_t *
11932rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11933{
11934 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11935 n->nd_head = nd_head;
11936 n->nd_value = nd_value;
11937
11938 return n;
11939}
11940
11941static rb_node_gvar_t *
11942rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11943{
11944 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11945 n->nd_vid = nd_vid;
11946
11947 return n;
11948}
11949
11950static rb_node_lvar_t *
11951rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11952{
11953 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11954 n->nd_vid = nd_vid;
11955
11956 return n;
11957}
11958
11959static rb_node_dvar_t *
11960rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11961{
11962 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11963 n->nd_vid = nd_vid;
11964
11965 return n;
11966}
11967
11968static rb_node_ivar_t *
11969rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11970{
11971 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11972 n->nd_vid = nd_vid;
11973
11974 return n;
11975}
11976
11977static rb_node_const_t *
11978rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11979{
11980 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11981 n->nd_vid = nd_vid;
11982
11983 return n;
11984}
11985
11986static rb_node_cvar_t *
11987rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11988{
11989 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11990 n->nd_vid = nd_vid;
11991
11992 return n;
11993}
11994
11995static rb_node_nth_ref_t *
11996rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11997{
11998 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11999 n->nd_nth = nd_nth;
12000
12001 return n;
12002}
12003
12004static rb_node_back_ref_t *
12005rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12006{
12007 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
12008 n->nd_nth = nd_nth;
12009
12010 return n;
12011}
12012
12013static rb_node_integer_t *
12014rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
12015{
12016 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
12017 n->val = val;
12018 n->minus = FALSE;
12019 n->base = base;
12020
12021 return n;
12022}
12023
12024static rb_node_float_t *
12025rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
12026{
12027 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12028 n->val = val;
12029 n->minus = FALSE;
12030
12031 return n;
12032}
12033
12034static rb_node_rational_t *
12035rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12036{
12037 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12038 n->val = val;
12039 n->minus = FALSE;
12040 n->base = base;
12041 n->seen_point = seen_point;
12042
12043 return n;
12044}
12045
12046static rb_node_imaginary_t *
12047rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12048{
12049 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12050 n->val = val;
12051 n->minus = FALSE;
12052 n->base = base;
12053 n->seen_point = seen_point;
12054 n->type = numeric_type;
12055
12056 return n;
12057}
12058
12059static rb_node_str_t *
12060rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12061{
12062 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12063 n->string = string;
12064
12065 return n;
12066}
12067
12068/* TODO; Use union for NODE_DSTR2 */
12069static rb_node_dstr_t *
12070rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12071{
12072 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12073 n->string = string;
12074 n->as.nd_alen = nd_alen;
12075 n->nd_next = (rb_node_list_t *)nd_next;
12076
12077 return n;
12078}
12079
12080static rb_node_dstr_t *
12081rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12082{
12083 return rb_node_dstr_new0(p, string, 1, 0, loc);
12084}
12085
12086static rb_node_xstr_t *
12087rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12088{
12089 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12090 n->string = string;
12091
12092 return n;
12093}
12094
12095static rb_node_dxstr_t *
12096rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12097{
12098 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12099 n->string = string;
12100 n->as.nd_alen = nd_alen;
12101 n->nd_next = (rb_node_list_t *)nd_next;
12102
12103 return n;
12104}
12105
12106static rb_node_sym_t *
12107rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12108{
12109 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12110 n->string = rb_str_to_parser_string(p, str);
12111
12112 return n;
12113}
12114
12115static rb_node_dsym_t *
12116rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12117{
12118 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12119 n->string = string;
12120 n->as.nd_alen = nd_alen;
12121 n->nd_next = (rb_node_list_t *)nd_next;
12122
12123 return n;
12124}
12125
12126static rb_node_evstr_t *
12127rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12128{
12129 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12130 n->nd_body = nd_body;
12131 n->opening_loc = *opening_loc;
12132 n->closing_loc = *closing_loc;
12133
12134 return n;
12135}
12136
12137static rb_node_regx_t *
12138rb_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)
12139{
12140 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12141 n->string = string;
12142 n->options = options & RE_OPTION_MASK;
12143 n->opening_loc = *opening_loc;
12144 n->content_loc = *content_loc;
12145 n->closing_loc = *closing_loc;
12146
12147 return n;
12148}
12149
12150static rb_node_call_t *
12151rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12152{
12153 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12154 n->nd_recv = nd_recv;
12155 n->nd_mid = nd_mid;
12156 n->nd_args = nd_args;
12157
12158 return n;
12159}
12160
12161static rb_node_opcall_t *
12162rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12163{
12164 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12165 n->nd_recv = nd_recv;
12166 n->nd_mid = nd_mid;
12167 n->nd_args = nd_args;
12168
12169 return n;
12170}
12171
12172static rb_node_fcall_t *
12173rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12174{
12175 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12176 n->nd_mid = nd_mid;
12177 n->nd_args = nd_args;
12178
12179 return n;
12180}
12181
12182static rb_node_qcall_t *
12183rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12184{
12185 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12186 n->nd_recv = nd_recv;
12187 n->nd_mid = nd_mid;
12188 n->nd_args = nd_args;
12189
12190 return n;
12191}
12192
12193static rb_node_vcall_t *
12194rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12195{
12196 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12197 n->nd_mid = nd_mid;
12198
12199 return n;
12200}
12201
12202static rb_node_once_t *
12203rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12204{
12205 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12206 n->nd_body = nd_body;
12207
12208 return n;
12209}
12210
12211static rb_node_args_t *
12212rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12213{
12214 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12215 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12216
12217 return n;
12218}
12219
12220static rb_node_args_aux_t *
12221rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12222{
12223 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12224 n->nd_pid = nd_pid;
12225 n->nd_plen = nd_plen;
12226 n->nd_next = 0;
12227
12228 return n;
12229}
12230
12231static rb_node_opt_arg_t *
12232rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12233{
12234 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12235 n->nd_body = nd_body;
12236 n->nd_next = 0;
12237
12238 return n;
12239}
12240
12241static rb_node_kw_arg_t *
12242rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12243{
12244 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12245 n->nd_body = nd_body;
12246 n->nd_next = 0;
12247
12248 return n;
12249}
12250
12251static rb_node_postarg_t *
12252rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12253{
12254 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12255 n->nd_1st = nd_1st;
12256 n->nd_2nd = nd_2nd;
12257
12258 return n;
12259}
12260
12261static rb_node_argscat_t *
12262rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12263{
12264 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12265 n->nd_head = nd_head;
12266 n->nd_body = nd_body;
12267
12268 return n;
12269}
12270
12271static rb_node_argspush_t *
12272rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12273{
12274 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12275 n->nd_head = nd_head;
12276 n->nd_body = nd_body;
12277
12278 return n;
12279}
12280
12281static rb_node_splat_t *
12282rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12283{
12284 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12285 n->nd_head = nd_head;
12286 n->operator_loc = *operator_loc;
12287
12288 return n;
12289}
12290
12291static rb_node_block_pass_t *
12292rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12293{
12294 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12295 n->forwarding = 0;
12296 n->nd_head = 0;
12297 n->nd_body = nd_body;
12298 n->operator_loc = *operator_loc;
12299
12300 return n;
12301}
12302
12303static rb_node_alias_t *
12304rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12305{
12306 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12307 n->nd_1st = nd_1st;
12308 n->nd_2nd = nd_2nd;
12309 n->keyword_loc = *keyword_loc;
12310
12311 return n;
12312}
12313
12314static rb_node_valias_t *
12315rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12316{
12317 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12318 n->nd_alias = nd_alias;
12319 n->nd_orig = nd_orig;
12320 n->keyword_loc = *keyword_loc;
12321
12322 return n;
12323}
12324
12325static rb_node_undef_t *
12326rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12327{
12328 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12329 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12330 n->keyword_loc = NULL_LOC;
12331 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12332
12333 return n;
12334}
12335
12336static rb_node_errinfo_t *
12337rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12338{
12339 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12340
12341 return n;
12342}
12343
12344static rb_node_defined_t *
12345rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12346{
12347 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12348 n->nd_head = nd_head;
12349 n->keyword_loc = *keyword_loc;
12350
12351 return n;
12352}
12353
12354static rb_node_postexe_t *
12355rb_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)
12356{
12357 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12358 n->nd_body = nd_body;
12359 n->keyword_loc = *keyword_loc;
12360 n->opening_loc = *opening_loc;
12361 n->closing_loc = *closing_loc;
12362
12363 return n;
12364}
12365
12366static rb_node_attrasgn_t *
12367rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12368{
12369 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12370 n->nd_recv = nd_recv;
12371 n->nd_mid = nd_mid;
12372 n->nd_args = nd_args;
12373
12374 return n;
12375}
12376
12377static rb_node_aryptn_t *
12378rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12379{
12380 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12381 n->nd_pconst = 0;
12382 n->pre_args = pre_args;
12383 n->rest_arg = rest_arg;
12384 n->post_args = post_args;
12385
12386 return n;
12387}
12388
12389static rb_node_hshptn_t *
12390rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12391{
12392 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12393 n->nd_pconst = nd_pconst;
12394 n->nd_pkwargs = nd_pkwargs;
12395 n->nd_pkwrestarg = nd_pkwrestarg;
12396
12397 return n;
12398}
12399
12400static rb_node_fndptn_t *
12401rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12402{
12403 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12404 n->nd_pconst = 0;
12405 n->pre_rest_arg = pre_rest_arg;
12406 n->args = args;
12407 n->post_rest_arg = post_rest_arg;
12408
12409 return n;
12410}
12411
12412static rb_node_line_t *
12413rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12414{
12415 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12416
12417 return n;
12418}
12419
12420static rb_node_file_t *
12421rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12422{
12423 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12424 n->path = rb_str_to_parser_string(p, str);
12425
12426 return n;
12427}
12428
12429static rb_node_encoding_t *
12430rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12431{
12432 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12433 n->enc = p->enc;
12434
12435 return n;
12436}
12437
12438static rb_node_cdecl_t *
12439rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12440{
12441 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12442 n->nd_vid = nd_vid;
12443 n->nd_value = nd_value;
12444 n->nd_else = nd_else;
12445 n->shareability = shareability;
12446
12447 return n;
12448}
12449
12450static rb_node_op_cdecl_t *
12451rb_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)
12452{
12453 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12454 n->nd_head = nd_head;
12455 n->nd_value = nd_value;
12456 n->nd_aid = nd_aid;
12457 n->shareability = shareability;
12458
12459 return n;
12460}
12461
12462static rb_node_error_t *
12463rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12464{
12465 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12466
12467 return n;
12468}
12469
12470static rb_node_break_t *
12471rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12472{
12473 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12474 n->nd_stts = nd_stts;
12475 n->nd_chain = 0;
12476 n->keyword_loc = *keyword_loc;
12477
12478 return n;
12479}
12480
12481static rb_node_next_t *
12482rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12483{
12484 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12485 n->nd_stts = nd_stts;
12486 n->nd_chain = 0;
12487 n->keyword_loc = *keyword_loc;
12488
12489 return n;
12490}
12491
12492static rb_node_redo_t *
12493rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12494{
12495 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12496 n->nd_chain = 0;
12497 n->keyword_loc = *keyword_loc;
12498
12499 return n;
12500}
12501
12502static rb_node_def_temp_t *
12503rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12504{
12505 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12506 n->save.numparam_save = 0;
12507 n->save.max_numparam = 0;
12508 n->save.ctxt = p->ctxt;
12509 n->nd_def = 0;
12510 n->nd_mid = 0;
12511
12512 return n;
12513}
12514
12515static rb_node_def_temp_t *
12516def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12517{
12518 n->save.numparam_save = numparam_push(p);
12519 n->save.max_numparam = p->max_numparam;
12520 return n;
12521}
12522
12523#ifndef RIPPER
12524static enum node_type
12525nodetype(NODE *node) /* for debug */
12526{
12527 return (enum node_type)nd_type(node);
12528}
12529
12530static int
12531nodeline(NODE *node)
12532{
12533 return nd_line(node);
12534}
12535#endif
12536
12537static NODE*
12538newline_node(NODE *node)
12539{
12540 if (node) {
12541 node = remove_begin(node);
12542 nd_set_fl_newline(node);
12543 }
12544 return node;
12545}
12546
12547static void
12548fixpos(NODE *node, NODE *orig)
12549{
12550 if (!node) return;
12551 if (!orig) return;
12552 nd_set_line(node, nd_line(orig));
12553}
12554
12555static NODE*
12556block_append(struct parser_params *p, NODE *head, NODE *tail)
12557{
12558 NODE *end, *h = head, *nd;
12559
12560 if (tail == 0) return head;
12561
12562 if (h == 0) return tail;
12563 switch (nd_type(h)) {
12564 default:
12565 h = end = NEW_BLOCK(head, &head->nd_loc);
12566 head = end;
12567 break;
12568 case NODE_BLOCK:
12569 end = RNODE_BLOCK(h)->nd_end;
12570 break;
12571 }
12572
12573 nd = RNODE_BLOCK(end)->nd_head;
12574 switch (nd_type(nd)) {
12575 case NODE_RETURN:
12576 case NODE_BREAK:
12577 case NODE_NEXT:
12578 case NODE_REDO:
12579 case NODE_RETRY:
12580 rb_warning0L(nd_line(tail), "statement not reached");
12581 break;
12582
12583 default:
12584 break;
12585 }
12586
12587 if (!nd_type_p(tail, NODE_BLOCK)) {
12588 tail = NEW_BLOCK(tail, &tail->nd_loc);
12589 }
12590 RNODE_BLOCK(end)->nd_next = tail;
12591 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12592 nd_set_last_loc(head, nd_last_loc(tail));
12593 return head;
12594}
12595
12596/* append item to the list */
12597static NODE*
12598list_append(struct parser_params *p, NODE *list, NODE *item)
12599{
12600 NODE *last;
12601
12602 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12603 if (RNODE_LIST(list)->nd_next) {
12604 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12605 }
12606 else {
12607 last = list;
12608 }
12609
12610 RNODE_LIST(list)->as.nd_alen += 1;
12611 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12612 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12613
12614 nd_set_last_loc(list, nd_last_loc(item));
12615
12616 return list;
12617}
12618
12619/* concat two lists */
12620static NODE*
12621list_concat(NODE *head, NODE *tail)
12622{
12623 NODE *last;
12624
12625 if (RNODE_LIST(head)->nd_next) {
12626 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12627 }
12628 else {
12629 last = head;
12630 }
12631
12632 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12633 RNODE_LIST(last)->nd_next = tail;
12634 if (RNODE_LIST(tail)->nd_next) {
12635 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12636 }
12637 else {
12638 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12639 }
12640
12641 nd_set_last_loc(head, nd_last_loc(tail));
12642
12643 return head;
12644}
12645
12646static int
12647literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12648{
12649 if (!tail) return 1;
12650 if (!rb_parser_enc_compatible(p, head, tail)) {
12651 compile_error(p, "string literal encodings differ (%s / %s)",
12652 rb_enc_name(rb_parser_str_get_encoding(head)),
12653 rb_enc_name(rb_parser_str_get_encoding(tail)));
12654 rb_parser_str_resize(p, head, 0);
12655 rb_parser_str_resize(p, tail, 0);
12656 return 0;
12657 }
12658 rb_parser_str_buf_append(p, head, tail);
12659 return 1;
12660}
12661
12662static rb_parser_string_t *
12663string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12664{
12665 if (htype != NODE_DSTR) return NULL;
12666 if (RNODE_DSTR(head)->nd_next) {
12667 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12668 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12669 }
12670 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12671 ASSUME(lit);
12672 return lit;
12673}
12674
12675#ifndef RIPPER
12676static rb_parser_string_t *
12677rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12678{
12679 rb_parser_string_t *copy;
12680 if (!orig) return NULL;
12681 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12682 copy->coderange = orig->coderange;
12683 copy->enc = orig->enc;
12684 return copy;
12685}
12686#endif
12687
12688/* concat two string literals */
12689static NODE *
12690literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12691{
12692 enum node_type htype;
12693 rb_parser_string_t *lit;
12694
12695 if (!head) return tail;
12696 if (!tail) return head;
12697
12698 htype = nd_type(head);
12699 if (htype == NODE_EVSTR) {
12700 head = new_dstr(p, head, loc);
12701 htype = NODE_DSTR;
12702 }
12703 if (p->heredoc_indent > 0) {
12704 switch (htype) {
12705 case NODE_STR:
12706 head = str2dstr(p, head);
12707 case NODE_DSTR:
12708 return list_append(p, head, tail);
12709 default:
12710 break;
12711 }
12712 }
12713 switch (nd_type(tail)) {
12714 case NODE_STR:
12715 if ((lit = string_literal_head(p, htype, head)) != false) {
12716 htype = NODE_STR;
12717 }
12718 else {
12719 lit = RNODE_DSTR(head)->string;
12720 }
12721 if (htype == NODE_STR) {
12722 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12723 error:
12724 rb_discard_node(p, head);
12725 rb_discard_node(p, tail);
12726 return 0;
12727 }
12728 rb_discard_node(p, tail);
12729 }
12730 else {
12731 list_append(p, head, tail);
12732 }
12733 break;
12734
12735 case NODE_DSTR:
12736 if (htype == NODE_STR) {
12737 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12738 goto error;
12739 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12740 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12741 RNODE_STR(head)->string = NULL;
12742 rb_discard_node(p, head);
12743 head = tail;
12744 }
12745 else if (!RNODE_DSTR(tail)->string) {
12746 append:
12747 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12748 if (!RNODE_DSTR(head)->nd_next) {
12749 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12750 }
12751 else if (RNODE_DSTR(tail)->nd_next) {
12752 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12753 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12754 }
12755 rb_discard_node(p, tail);
12756 }
12757 else if ((lit = string_literal_head(p, htype, head)) != false) {
12758 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12759 goto error;
12760 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12761 RNODE_DSTR(tail)->string = 0;
12762 goto append;
12763 }
12764 else {
12765 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));
12766 RNODE_DSTR(tail)->string = 0;
12767 }
12768 break;
12769
12770 case NODE_EVSTR:
12771 if (htype == NODE_STR) {
12772 head = str2dstr(p, head);
12773 RNODE_DSTR(head)->as.nd_alen = 1;
12774 }
12775 list_append(p, head, tail);
12776 break;
12777 }
12778 return head;
12779}
12780
12781static void
12782nd_copy_flag(NODE *new_node, NODE *old_node)
12783{
12784 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12785 nd_set_line(new_node, nd_line(old_node));
12786 new_node->nd_loc = old_node->nd_loc;
12787 new_node->node_id = old_node->node_id;
12788}
12789
12790static NODE *
12791str2dstr(struct parser_params *p, NODE *node)
12792{
12793 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12794 nd_copy_flag(new_node, node);
12795 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12796 RNODE_DSTR(new_node)->as.nd_alen = 0;
12797 RNODE_DSTR(new_node)->nd_next = 0;
12798 RNODE_STR(node)->string = 0;
12799
12800 return new_node;
12801}
12802
12803static NODE *
12804str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12805{
12806 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12807 nd_copy_flag(new_node, node);
12808 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12809 RNODE_REGX(new_node)->options = options;
12810 nd_set_loc(new_node, loc);
12811 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12812 RNODE_REGX(new_node)->content_loc = *content_loc;
12813 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12814 RNODE_STR(node)->string = 0;
12815
12816 return new_node;
12817}
12818
12819static NODE *
12820evstr2dstr(struct parser_params *p, NODE *node)
12821{
12822 if (nd_type_p(node, NODE_EVSTR)) {
12823 node = new_dstr(p, node, &node->nd_loc);
12824 }
12825 return node;
12826}
12827
12828static NODE *
12829new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12830{
12831 NODE *head = node;
12832
12833 if (node) {
12834 switch (nd_type(node)) {
12835 case NODE_STR:
12836 return str2dstr(p, node);
12837 case NODE_DSTR:
12838 break;
12839 case NODE_EVSTR:
12840 return node;
12841 }
12842 }
12843 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12844}
12845
12846static NODE *
12847new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12848{
12849 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12850 return list_append(p, dstr, node);
12851}
12852
12853static NODE *
12854call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12855 const YYLTYPE *op_loc, const YYLTYPE *loc)
12856{
12857 NODE *expr;
12858 value_expr(p, recv);
12859 value_expr(p, arg1);
12860 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12861 nd_set_line(expr, op_loc->beg_pos.lineno);
12862 return expr;
12863}
12864
12865static NODE *
12866call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12867{
12868 NODE *opcall;
12869 value_expr(p, recv);
12870 opcall = NEW_OPCALL(recv, id, 0, loc);
12871 nd_set_line(opcall, op_loc->beg_pos.lineno);
12872 return opcall;
12873}
12874
12875static NODE *
12876new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12877{
12878 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12879 nd_set_line(qcall, op_loc->beg_pos.lineno);
12880 return qcall;
12881}
12882
12883static NODE*
12884new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12885{
12886 NODE *ret;
12887 if (block) block_dup_check(p, args, block);
12888 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12889 if (block) ret = method_add_block(p, ret, block, loc);
12890 fixpos(ret, recv);
12891 return ret;
12892}
12893
12894static rb_locations_lambda_body_t*
12895new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12896{
12897 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12898 body->node = node;
12899 body->opening_loc = *opening_loc;
12900 body->closing_loc = *closing_loc;
12901 return body;
12902}
12903
12904static NODE *
12905command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12906{
12907 NODE **body;
12908 enum node_type type = nd_type(m);
12909 switch (type) {
12910 case NODE_YIELD:
12911 compile_error(p, "block given to yield");
12912 return m;
12913 case NODE_RETURN:
12914 body = &RNODE_RETURN(m)->nd_stts;
12915 break;
12916 case NODE_BREAK:
12917 case NODE_NEXT:
12918 body = &RNODE_EXITS(m)->nd_stts;
12919 break;
12920 case NODE_REDO: /* `redo` has no argument */
12921 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12922 return m;
12923 case NODE_RETRY: /* `retry` has no argument */
12924 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12925 return m;
12926 default:
12927 block_dup_check(p, get_nd_args(p, m), b);
12928 return method_add_block(p, m, b, loc);
12929 }
12930 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12931 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12932 *body = command_add_block(p, *body, b, &b_loc);
12933 m->nd_loc.end_pos = loc->end_pos;
12934 return m;
12935}
12936
12937#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12938
12939static NODE*
12940last_expr_once_body(NODE *node)
12941{
12942 if (!node) return 0;
12943 return nd_once_body(node);
12944}
12945
12946static NODE*
12947match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12948{
12949 NODE *n;
12950 int line = op_loc->beg_pos.lineno;
12951
12952 value_expr(p, node1);
12953 value_expr(p, node2);
12954
12955 if ((n = last_expr_once_body(node1)) != 0) {
12956 switch (nd_type(n)) {
12957 case NODE_DREGX:
12958 {
12959 NODE *match = NEW_MATCH2(node1, node2, loc);
12960 nd_set_line(match, line);
12961 return match;
12962 }
12963
12964 case NODE_REGX:
12965 {
12966 const VALUE lit = rb_node_regx_string_val(n);
12967 if (!NIL_P(lit)) {
12968 NODE *match = NEW_MATCH2(node1, node2, loc);
12969 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12970 nd_set_line(match, line);
12971 return match;
12972 }
12973 }
12974 }
12975 }
12976
12977 if ((n = last_expr_once_body(node2)) != 0) {
12978 NODE *match3;
12979
12980 switch (nd_type(n)) {
12981 case NODE_DREGX:
12982 match3 = NEW_MATCH3(node2, node1, loc);
12983 return match3;
12984 }
12985 }
12986
12987 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12988 nd_set_line(n, line);
12989 return n;
12990}
12991
12992# if WARN_PAST_SCOPE
12993static int
12994past_dvar_p(struct parser_params *p, ID id)
12995{
12996 struct vtable *past = p->lvtbl->past;
12997 while (past) {
12998 if (vtable_included(past, id)) return 1;
12999 past = past->prev;
13000 }
13001 return 0;
13002}
13003# endif
13004
13005static int
13006numparam_nested_p(struct parser_params *p)
13007{
13008 struct local_vars *local = p->lvtbl;
13009 NODE *outer = local->numparam.outer;
13010 NODE *inner = local->numparam.inner;
13011 if (outer || inner) {
13012 NODE *used = outer ? outer : inner;
13013 compile_error(p, "numbered parameter is already used in %s block\n"
13014 "%s:%d: numbered parameter is already used here",
13015 outer ? "outer" : "inner",
13016 p->ruby_sourcefile, nd_line(used));
13017 parser_show_error_line(p, &used->nd_loc);
13018 return 1;
13019 }
13020 return 0;
13021}
13022
13023static int
13024numparam_used_p(struct parser_params *p)
13025{
13026 NODE *numparam = p->lvtbl->numparam.current;
13027 if (numparam) {
13028 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
13029 "%s:%d: numbered parameter is already used here",
13030 p->ruby_sourcefile, nd_line(numparam));
13031 parser_show_error_line(p, &numparam->nd_loc);
13032 return 1;
13033 }
13034 return 0;
13035}
13036
13037static int
13038it_used_p(struct parser_params *p)
13039{
13040 NODE *it = p->lvtbl->it;
13041 if (it) {
13042 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
13043 "%s:%d: 'it' is already used here",
13044 p->ruby_sourcefile, nd_line(it));
13045 parser_show_error_line(p, &it->nd_loc);
13046 return 1;
13047 }
13048 return 0;
13049}
13050
13051static NODE*
13052gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13053{
13054 ID *vidp = NULL;
13055 NODE *node;
13056 switch (id) {
13057 case keyword_self:
13058 return NEW_SELF(loc);
13059 case keyword_nil:
13060 return NEW_NIL(loc);
13061 case keyword_true:
13062 return NEW_TRUE(loc);
13063 case keyword_false:
13064 return NEW_FALSE(loc);
13065 case keyword__FILE__:
13066 {
13067 VALUE file = p->ruby_sourcefile_string;
13068 if (NIL_P(file))
13069 file = rb_str_new(0, 0);
13070 node = NEW_FILE(file, loc);
13071 }
13072 return node;
13073 case keyword__LINE__:
13074 return NEW_LINE(loc);
13075 case keyword__ENCODING__:
13076 return NEW_ENCODING(loc);
13077
13078 }
13079 switch (id_type(id)) {
13080 case ID_LOCAL:
13081 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13082 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13083 if (vidp) *vidp |= LVAR_USED;
13084 node = NEW_DVAR(id, loc);
13085 return node;
13086 }
13087 if (local_id_ref(p, id, &vidp)) {
13088 if (vidp) *vidp |= LVAR_USED;
13089 node = NEW_LVAR(id, loc);
13090 return node;
13091 }
13092 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13093 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13094 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13095 node = NEW_DVAR(id, loc);
13096 struct local_vars *local = p->lvtbl;
13097 if (!local->numparam.current) local->numparam.current = node;
13098 return node;
13099 }
13100# if WARN_PAST_SCOPE
13101 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13102 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13103 }
13104# endif
13105 /* method call without arguments */
13106 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13107 if (numparam_used_p(p)) return 0;
13108 if (p->max_numparam == ORDINAL_PARAM) {
13109 compile_error(p, "ordinary parameter is defined");
13110 return 0;
13111 }
13112 if (!p->it_id) {
13113 p->it_id = idItImplicit;
13114 vtable_add(p->lvtbl->args, p->it_id);
13115 }
13116 NODE *node = NEW_DVAR(p->it_id, loc);
13117 if (!p->lvtbl->it) p->lvtbl->it = node;
13118 return node;
13119 }
13120 return NEW_VCALL(id, loc);
13121 case ID_GLOBAL:
13122 return NEW_GVAR(id, loc);
13123 case ID_INSTANCE:
13124 return NEW_IVAR(id, loc);
13125 case ID_CONST:
13126 return NEW_CONST(id, loc);
13127 case ID_CLASS:
13128 return NEW_CVAR(id, loc);
13129 }
13130 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13131 return 0;
13132}
13133
13134static rb_node_opt_arg_t *
13135opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13136{
13137 rb_node_opt_arg_t *opts = opt_list;
13138 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13139
13140 while (opts->nd_next) {
13141 opts = opts->nd_next;
13142 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13143 }
13144 opts->nd_next = opt;
13145
13146 return opt_list;
13147}
13148
13149static rb_node_kw_arg_t *
13150kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13151{
13152 if (kwlist) {
13153 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13154 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13155 }
13156 return kwlist;
13157}
13158
13159static NODE *
13160new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13161{
13162 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13163 p->ctxt.has_trailing_semicolon = 0;
13164
13165 NODE *n = expr;
13166 while (n) {
13167 if (nd_type_p(n, NODE_BEGIN)) {
13168 n = RNODE_BEGIN(n)->nd_body;
13169 }
13170 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13171 n = RNODE_BLOCK(n)->nd_head;
13172 }
13173 else {
13174 break;
13175 }
13176 }
13177
13178 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13179 NODE *block = NEW_BLOCK(expr, loc);
13180 return NEW_DEFINED(block, loc, keyword_loc);
13181 }
13182
13183 return NEW_DEFINED(n, loc, keyword_loc);
13184}
13185
13186static NODE*
13187str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13188{
13189 VALUE lit;
13190 rb_parser_string_t *str = RNODE_STR(node)->string;
13191 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13192 yyerror1(loc, "invalid symbol");
13193 lit = STR_NEW0();
13194 }
13195 else {
13196 lit = rb_str_new_parser_string(str);
13197 }
13198 return NEW_SYM(lit, loc);
13199}
13200
13201static NODE*
13202symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13203{
13204 enum node_type type = nd_type(symbol);
13205 switch (type) {
13206 case NODE_DSTR:
13207 nd_set_type(symbol, NODE_DSYM);
13208 break;
13209 case NODE_STR:
13210 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13211 break;
13212 default:
13213 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13214 }
13215 return list_append(p, symbols, symbol);
13216}
13217
13218static void
13219dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13220{
13221 if (dreg->string) {
13222 reg_fragment_setenc(p, dreg->string, options);
13223 }
13224 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13225 NODE *frag = list->nd_head;
13226 if (nd_type_p(frag, NODE_STR)) {
13227 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13228 }
13229 else if (nd_type_p(frag, NODE_DSTR)) {
13230 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13231 }
13232 }
13233}
13234
13235static NODE *
13236new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13237{
13238 if (!node) {
13239 /* Check string is valid regex */
13240 rb_parser_string_t *str = STRING_NEW0();
13241 reg_compile(p, str, options);
13242 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13243 return node;
13244 }
13245 switch (nd_type(node)) {
13246 case NODE_STR:
13247 {
13248 /* Check string is valid regex */
13249 reg_compile(p, RNODE_STR(node)->string, options);
13250 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13251 }
13252 break;
13253 default:
13254 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13255 /* fall through */
13256 case NODE_DSTR:
13257 nd_set_type(node, NODE_DREGX);
13258 nd_set_loc(node, loc);
13259 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13260 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13261 if (dreg->nd_next) {
13262 dregex_fragment_setenc(p, dreg, options);
13263 }
13264 if (options & RE_OPTION_ONCE) {
13265 node = NEW_ONCE(node, loc);
13266 }
13267 break;
13268 }
13269 return node;
13270}
13271
13272static rb_node_kw_arg_t *
13273new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13274{
13275 if (!k) return 0;
13276 return NEW_KW_ARG((k), loc);
13277}
13278
13279static NODE *
13280new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13281{
13282 if (!node) {
13283 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13284 return xstr;
13285 }
13286 switch (nd_type(node)) {
13287 case NODE_STR:
13288 nd_set_type(node, NODE_XSTR);
13289 nd_set_loc(node, loc);
13290 break;
13291 case NODE_DSTR:
13292 nd_set_type(node, NODE_DXSTR);
13293 nd_set_loc(node, loc);
13294 break;
13295 default:
13296 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13297 break;
13298 }
13299 return node;
13300}
13301
13302static const
13303struct st_hash_type literal_type = {
13304 literal_cmp,
13305 literal_hash,
13306};
13307
13308static int nd_type_st_key_enable_p(NODE *node);
13309
13310static void
13311check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13312{
13313 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13314 if (!arg || !p->case_labels) return;
13315 if (!nd_type_st_key_enable_p(arg)) return;
13316
13317 if (p->case_labels == CHECK_LITERAL_WHEN) {
13318 p->case_labels = st_init_table(&literal_type);
13319 }
13320 else {
13321 st_data_t line;
13322 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13323 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13324 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13325 return;
13326 }
13327 }
13328 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13329}
13330
13331#ifdef RIPPER
13332static int
13333id_is_var(struct parser_params *p, ID id)
13334{
13335 if (is_notop_id(id)) {
13336 switch (id & ID_SCOPE_MASK) {
13337 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13338 return 1;
13339 case ID_LOCAL:
13340 if (dyna_in_block(p)) {
13341 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13342 }
13343 if (local_id(p, id)) return 1;
13344 /* method call without arguments */
13345 return 0;
13346 }
13347 }
13348 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13349 return 0;
13350}
13351#endif
13352
13353static inline enum lex_state_e
13354parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13355{
13356 if (p->debug) {
13357 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13358 }
13359 return p->lex.state = ls;
13360}
13361
13362#ifndef RIPPER
13363static void
13364flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13365{
13366 VALUE mesg = p->debug_buffer;
13367
13368 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13369 p->debug_buffer = Qnil;
13370 rb_io_puts(1, &mesg, out);
13371 }
13372 if (!NIL_P(str) && RSTRING_LEN(str)) {
13373 rb_io_write(p->debug_output, str);
13374 }
13375}
13376
13377static const char rb_parser_lex_state_names[][8] = {
13378 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13379 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13380 "LABEL", "LABELED","FITEM",
13381};
13382
13383static VALUE
13384append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13385{
13386 int i, sep = 0;
13387 unsigned int mask = 1;
13388 static const char none[] = "NONE";
13389
13390 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13391 if ((unsigned)state & mask) {
13392 if (sep) {
13393 rb_str_cat(buf, "|", 1);
13394 }
13395 sep = 1;
13396 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13397 }
13398 }
13399 if (!sep) {
13400 rb_str_cat(buf, none, sizeof(none)-1);
13401 }
13402 return buf;
13403}
13404
13405enum lex_state_e
13406rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13407 enum lex_state_e to, int line)
13408{
13409 VALUE mesg;
13410 mesg = rb_str_new_cstr("lex_state: ");
13411 append_lex_state_name(p, from, mesg);
13412 rb_str_cat_cstr(mesg, " -> ");
13413 append_lex_state_name(p, to, mesg);
13414 rb_str_catf(mesg, " at line %d\n", line);
13415 flush_debug_buffer(p, p->debug_output, mesg);
13416 return to;
13417}
13418
13419VALUE
13420rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13421{
13422 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13423}
13424
13425static void
13426append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13427{
13428 if (stack == 0) {
13429 rb_str_cat_cstr(mesg, "0");
13430 }
13431 else {
13432 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13433 for (; mask && !(stack & mask); mask >>= 1) continue;
13434 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13435 }
13436}
13437
13438void
13439rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13440 const char *name, int line)
13441{
13442 VALUE mesg = rb_sprintf("%s: ", name);
13443 append_bitstack_value(p, stack, mesg);
13444 rb_str_catf(mesg, " at line %d\n", line);
13445 flush_debug_buffer(p, p->debug_output, mesg);
13446}
13447
13448void
13449rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13450{
13451 va_list ap;
13452 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13453
13454 va_start(ap, fmt);
13455 rb_str_vcatf(mesg, fmt, ap);
13456 va_end(ap);
13457 yyerror0(RSTRING_PTR(mesg));
13458 RB_GC_GUARD(mesg);
13459
13460 mesg = rb_str_new(0, 0);
13461 append_lex_state_name(p, p->lex.state, mesg);
13462 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13463 rb_str_resize(mesg, 0);
13464 append_bitstack_value(p, p->cond_stack, mesg);
13465 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13466 rb_str_resize(mesg, 0);
13467 append_bitstack_value(p, p->cmdarg_stack, mesg);
13468 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13469 if (p->debug_output == rb_ractor_stdout())
13470 p->debug_output = rb_ractor_stderr();
13471 p->debug = TRUE;
13472}
13473
13474static YYLTYPE *
13475rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13476{
13477 yylloc->beg_pos.lineno = sourceline;
13478 yylloc->beg_pos.column = beg_pos;
13479 yylloc->end_pos.lineno = sourceline;
13480 yylloc->end_pos.column = end_pos;
13481 return yylloc;
13482}
13483
13484YYLTYPE *
13485rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13486{
13487 int sourceline = here->sourceline;
13488 int beg_pos = parser_column(here->offset - here->quote
13489 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT)));
13490 int end_pos = parser_column(here->offset + here->length + here->quote);
13491
13492 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13493}
13494
13495YYLTYPE *
13496rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13497{
13498 yylloc->beg_pos.lineno = p->delayed.beg_line;
13499 yylloc->beg_pos.column = p->delayed.beg_col;
13500 yylloc->end_pos.lineno = p->delayed.end_line;
13501 yylloc->end_pos.column = p->delayed.end_col;
13502
13503 return yylloc;
13504}
13505
13506YYLTYPE *
13507rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13508{
13509 int sourceline = p->ruby_sourceline;
13510 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13511 int end_pos = parser_column(p->lex.pend - p->lex.pbeg);
13512 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13513}
13514
13515YYLTYPE *
13516rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13517{
13518 yylloc->end_pos = yylloc->beg_pos;
13519
13520 return yylloc;
13521}
13522
13523YYLTYPE *
13524rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13525{
13526 int sourceline = p->ruby_sourceline;
13527 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13528 int end_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13529 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13530}
13531
13532YYLTYPE *
13533rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13534{
13535 int sourceline = p->ruby_sourceline;
13536 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13537 int end_pos = parser_column(p->lex.pcur - p->lex.pbeg);
13538 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13539}
13540#endif /* !RIPPER */
13541
13542static int
13543assignable0(struct parser_params *p, ID id, const char **err)
13544{
13545 if (!id) return -1;
13546 switch (id) {
13547 case keyword_self:
13548 *err = "Can't change the value of self";
13549 return -1;
13550 case keyword_nil:
13551 *err = "Can't assign to nil";
13552 return -1;
13553 case keyword_true:
13554 *err = "Can't assign to true";
13555 return -1;
13556 case keyword_false:
13557 *err = "Can't assign to false";
13558 return -1;
13559 case keyword__FILE__:
13560 *err = "Can't assign to __FILE__";
13561 return -1;
13562 case keyword__LINE__:
13563 *err = "Can't assign to __LINE__";
13564 return -1;
13565 case keyword__ENCODING__:
13566 *err = "Can't assign to __ENCODING__";
13567 return -1;
13568 }
13569 switch (id_type(id)) {
13570 case ID_LOCAL:
13571 if (dyna_in_block(p)) {
13572 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13573 compile_error(p, "Can't assign to numbered parameter _%d",
13574 NUMPARAM_ID_TO_IDX(id));
13575 return -1;
13576 }
13577 if (dvar_curr(p, id)) return NODE_DASGN;
13578 if (dvar_defined(p, id)) return NODE_DASGN;
13579 if (local_id(p, id)) return NODE_LASGN;
13580 dyna_var(p, id);
13581 return NODE_DASGN;
13582 }
13583 else {
13584 if (!local_id(p, id)) local_var(p, id);
13585 return NODE_LASGN;
13586 }
13587 break;
13588 case ID_GLOBAL: return NODE_GASGN;
13589 case ID_INSTANCE: return NODE_IASGN;
13590 case ID_CONST:
13591 if (!p->ctxt.in_def) return NODE_CDECL;
13592 *err = "dynamic constant assignment";
13593 return -1;
13594 case ID_CLASS: return NODE_CVASGN;
13595 default:
13596 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13597 }
13598 return -1;
13599}
13600
13601static NODE*
13602assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13603{
13604 const char *err = 0;
13605 int node_type = assignable0(p, id, &err);
13606 switch (node_type) {
13607 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13608 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13609 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13610 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13611 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13612 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13613 }
13614/* TODO: FIXME */
13615#ifndef RIPPER
13616 if (err) yyerror1(loc, err);
13617#else
13618 if (err) set_value(assign_error(p, err, p->s_lvalue));
13619#endif
13620 return NEW_ERROR(loc);
13621}
13622
13623static int
13624is_private_local_id(struct parser_params *p, ID name)
13625{
13626 VALUE s;
13627 if (name == idUScore) return 1;
13628 if (!is_local_id(name)) return 0;
13629 s = rb_id2str(name);
13630 if (!s) return 0;
13631 return RSTRING_PTR(s)[0] == '_';
13632}
13633
13634static int
13635shadowing_lvar_0(struct parser_params *p, ID name)
13636{
13637 if (dyna_in_block(p)) {
13638 if (dvar_curr(p, name)) {
13639 if (is_private_local_id(p, name)) return 1;
13640 yyerror0("duplicated argument name");
13641 }
13642 else if (dvar_defined(p, name) || local_id(p, name)) {
13643 vtable_add(p->lvtbl->vars, name);
13644 if (p->lvtbl->used) {
13645 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13646 }
13647 return 0;
13648 }
13649 }
13650 else {
13651 if (local_id(p, name)) {
13652 if (is_private_local_id(p, name)) return 1;
13653 yyerror0("duplicated argument name");
13654 }
13655 }
13656 return 1;
13657}
13658
13659static ID
13660shadowing_lvar(struct parser_params *p, ID name)
13661{
13662 shadowing_lvar_0(p, name);
13663 return name;
13664}
13665
13666static void
13667new_bv(struct parser_params *p, ID name)
13668{
13669 if (!name) return;
13670 if (!is_local_id(name)) {
13671 compile_error(p, "invalid local variable - %"PRIsVALUE,
13672 rb_id2str(name));
13673 return;
13674 }
13675 if (!shadowing_lvar_0(p, name)) return;
13676 dyna_var(p, name);
13677 ID *vidp = 0;
13678 if (dvar_defined_ref(p, name, &vidp)) {
13679 if (vidp) *vidp |= LVAR_USED;
13680 }
13681}
13682
13683static void
13684aryset_check(struct parser_params *p, NODE *args)
13685{
13686 NODE *block = 0, *kwds = 0;
13687 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13688 block = RNODE_BLOCK_PASS(args)->nd_body;
13689 args = RNODE_BLOCK_PASS(args)->nd_head;
13690 }
13691 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13692 args = RNODE_ARGSCAT(args)->nd_body;
13693 }
13694 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13695 kwds = RNODE_ARGSPUSH(args)->nd_body;
13696 }
13697 else {
13698 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13699 next = RNODE_LIST(next)->nd_next) {
13700 kwds = RNODE_LIST(next)->nd_head;
13701 }
13702 }
13703 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13704 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13705 }
13706 if (block) {
13707 yyerror1(&block->nd_loc, "block arg given in index assignment");
13708 }
13709}
13710
13711static NODE *
13712aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13713{
13714 aryset_check(p, idx);
13715 return NEW_ATTRASGN(recv, tASET, idx, loc);
13716}
13717
13718static void
13719block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13720{
13721 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13722 compile_error(p, "both block arg and actual block given");
13723 }
13724}
13725
13726static NODE *
13727attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13728{
13729 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13730 return NEW_ATTRASGN(recv, id, 0, loc);
13731}
13732
13733static VALUE
13734rb_backref_error(struct parser_params *p, NODE *node)
13735{
13736#ifndef RIPPER
13737# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13738#else
13739# define ERR(...) rb_sprintf(__VA_ARGS__)
13740#endif
13741 switch (nd_type(node)) {
13742 case NODE_NTH_REF:
13743 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13744 case NODE_BACK_REF:
13745 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13746 }
13747#undef ERR
13748 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13749}
13750
13751static NODE *
13752arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13753{
13754 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13755 switch (nd_type(node1)) {
13756 case NODE_LIST:
13757 return list_append(p, node1, node2);
13758 case NODE_BLOCK_PASS:
13759 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13760 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13761 return node1;
13762 case NODE_ARGSPUSH:
13763 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13764 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13765 nd_set_type(node1, NODE_ARGSCAT);
13766 return node1;
13767 case NODE_ARGSCAT:
13768 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13769 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13770 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13771 return node1;
13772 }
13773 return NEW_ARGSPUSH(node1, node2, loc);
13774}
13775
13776static NODE *
13777arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13778{
13779 if (!node2) return node1;
13780 switch (nd_type(node1)) {
13781 case NODE_BLOCK_PASS:
13782 if (RNODE_BLOCK_PASS(node1)->nd_head)
13783 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13784 else
13785 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13786 return node1;
13787 case NODE_ARGSPUSH:
13788 if (!nd_type_p(node2, NODE_LIST)) break;
13789 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13790 nd_set_type(node1, NODE_ARGSCAT);
13791 return node1;
13792 case NODE_ARGSCAT:
13793 if (!nd_type_p(node2, NODE_LIST) ||
13794 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13795 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13796 return node1;
13797 }
13798 return NEW_ARGSCAT(node1, node2, loc);
13799}
13800
13801static NODE *
13802last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13803{
13804 NODE *n1;
13805 if ((n1 = splat_array(args)) != 0) {
13806 return list_append(p, n1, last_arg);
13807 }
13808 return arg_append(p, args, last_arg, loc);
13809}
13810
13811static NODE *
13812rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13813{
13814 NODE *n1;
13815 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13816 return list_concat(n1, rest_arg);
13817 }
13818 return arg_concat(p, args, rest_arg, loc);
13819}
13820
13821static NODE *
13822splat_array(NODE* node)
13823{
13824 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13825 if (nd_type_p(node, NODE_LIST)) return node;
13826 return 0;
13827}
13828
13829static void
13830mark_lvar_used(struct parser_params *p, NODE *rhs)
13831{
13832 ID *vidp = NULL;
13833 if (!rhs) return;
13834 switch (nd_type(rhs)) {
13835 case NODE_LASGN:
13836 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13837 if (vidp) *vidp |= LVAR_USED;
13838 }
13839 break;
13840 case NODE_DASGN:
13841 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13842 if (vidp) *vidp |= LVAR_USED;
13843 }
13844 break;
13845#if 0
13846 case NODE_MASGN:
13847 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13848 mark_lvar_used(p, rhs->nd_head);
13849 }
13850 break;
13851#endif
13852 }
13853}
13854
13855static int is_static_content(NODE *node);
13856
13857static NODE *
13858node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13859{
13860 if (!lhs) return 0;
13861
13862 switch (nd_type(lhs)) {
13863 case NODE_CDECL:
13864 case NODE_GASGN:
13865 case NODE_IASGN:
13866 case NODE_LASGN:
13867 case NODE_DASGN:
13868 case NODE_MASGN:
13869 case NODE_CVASGN:
13870 set_nd_value(p, lhs, rhs);
13871 nd_set_loc(lhs, loc);
13872 break;
13873
13874 case NODE_ATTRASGN:
13875 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13876 nd_set_loc(lhs, loc);
13877 break;
13878
13879 default:
13880 /* should not happen */
13881 break;
13882 }
13883
13884 return lhs;
13885}
13886
13887static NODE *
13888value_expr_check(struct parser_params *p, NODE *node)
13889{
13890 NODE *void_node = 0, *vn;
13891
13892 if (!node) {
13893 rb_warning0("empty expression");
13894 }
13895 while (node) {
13896 switch (nd_type(node)) {
13897 case NODE_ENSURE:
13898 vn = RNODE_ENSURE(node)->nd_head;
13899 node = RNODE_ENSURE(node)->nd_ensr;
13900 /* nd_ensr should not be NULL, check it out next */
13901 if (vn && (vn = value_expr_check(p, vn))) {
13902 goto found;
13903 }
13904 break;
13905
13906 case NODE_RESCUE:
13907 /* void only if all children are void */
13908 vn = RNODE_RESCUE(node)->nd_head;
13909 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13910 if (!void_node) void_node = vn;
13911 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13912 if (!nd_type_p(r, NODE_RESBODY)) {
13913 compile_error(p, "unexpected node");
13914 return NULL;
13915 }
13916 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13917 void_node = 0;
13918 break;
13919 }
13920 if (!void_node) void_node = vn;
13921 }
13922 node = RNODE_RESCUE(node)->nd_else;
13923 if (!node) return void_node;
13924 break;
13925
13926 case NODE_RETURN:
13927 case NODE_BREAK:
13928 case NODE_NEXT:
13929 case NODE_REDO:
13930 case NODE_RETRY:
13931 goto found;
13932
13933 case NODE_CASE3:
13934 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13935 compile_error(p, "unexpected node");
13936 return NULL;
13937 }
13938 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13939 return NULL;
13940 }
13941 /* single line pattern matching with "=>" operator */
13942 goto found;
13943
13944 case NODE_BLOCK:
13945 while (RNODE_BLOCK(node)->nd_next) {
13946 node = RNODE_BLOCK(node)->nd_next;
13947 }
13948 node = RNODE_BLOCK(node)->nd_head;
13949 break;
13950
13951 case NODE_BEGIN:
13952 node = RNODE_BEGIN(node)->nd_body;
13953 break;
13954
13955 case NODE_IF:
13956 case NODE_UNLESS:
13957 if (!RNODE_IF(node)->nd_body) {
13958 return NULL;
13959 }
13960 else if (!RNODE_IF(node)->nd_else) {
13961 return NULL;
13962 }
13963 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13964 if (!vn) return NULL;
13965 if (!void_node) void_node = vn;
13966 node = RNODE_IF(node)->nd_else;
13967 break;
13968
13969 case NODE_AND:
13970 case NODE_OR:
13971 node = RNODE_AND(node)->nd_1st;
13972 break;
13973
13974 case NODE_LASGN:
13975 case NODE_DASGN:
13976 case NODE_MASGN:
13977 mark_lvar_used(p, node);
13978 return NULL;
13979
13980 default:
13981 return NULL;
13982 }
13983 }
13984
13985 return NULL;
13986
13987 found:
13988 /* return the first found node */
13989 return void_node ? void_node : node;
13990}
13991
13992static int
13993value_expr(struct parser_params *p, NODE *node)
13994{
13995 NODE *void_node = value_expr_check(p, node);
13996 if (void_node) {
13997 yyerror1(&void_node->nd_loc, "void value expression");
13998 /* or "control never reach"? */
13999 return FALSE;
14000 }
14001 return TRUE;
14002}
14003
14004static void
14005void_expr(struct parser_params *p, NODE *node)
14006{
14007 const char *useless = 0;
14008
14009 if (!RTEST(ruby_verbose)) return;
14010
14011 if (!node || !(node = nd_once_body(node))) return;
14012 switch (nd_type(node)) {
14013 case NODE_OPCALL:
14014 switch (RNODE_OPCALL(node)->nd_mid) {
14015 case '+':
14016 case '-':
14017 case '*':
14018 case '/':
14019 case '%':
14020 case tPOW:
14021 case tUPLUS:
14022 case tUMINUS:
14023 case '|':
14024 case '^':
14025 case '&':
14026 case tCMP:
14027 case '>':
14028 case tGEQ:
14029 case '<':
14030 case tLEQ:
14031 case tEQ:
14032 case tNEQ:
14033 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
14034 break;
14035 }
14036 break;
14037
14038 case NODE_LVAR:
14039 case NODE_DVAR:
14040 case NODE_GVAR:
14041 case NODE_IVAR:
14042 case NODE_CVAR:
14043 case NODE_NTH_REF:
14044 case NODE_BACK_REF:
14045 useless = "a variable";
14046 break;
14047 case NODE_CONST:
14048 useless = "a constant";
14049 break;
14050 case NODE_SYM:
14051 case NODE_LINE:
14052 case NODE_FILE:
14053 case NODE_ENCODING:
14054 case NODE_INTEGER:
14055 case NODE_FLOAT:
14056 case NODE_RATIONAL:
14057 case NODE_IMAGINARY:
14058 case NODE_STR:
14059 case NODE_DSTR:
14060 case NODE_REGX:
14061 case NODE_DREGX:
14062 useless = "a literal";
14063 break;
14064 case NODE_COLON2:
14065 case NODE_COLON3:
14066 useless = "::";
14067 break;
14068 case NODE_DOT2:
14069 useless = "..";
14070 break;
14071 case NODE_DOT3:
14072 useless = "...";
14073 break;
14074 case NODE_SELF:
14075 useless = "self";
14076 break;
14077 case NODE_NIL:
14078 useless = "nil";
14079 break;
14080 case NODE_TRUE:
14081 useless = "true";
14082 break;
14083 case NODE_FALSE:
14084 useless = "false";
14085 break;
14086 case NODE_DEFINED:
14087 useless = "defined?";
14088 break;
14089 }
14090
14091 if (useless) {
14092 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14093 }
14094}
14095
14096/* warns useless use of block and returns the last statement node */
14097static NODE *
14098void_stmts(struct parser_params *p, NODE *node)
14099{
14100 NODE *const n = node;
14101 if (!RTEST(ruby_verbose)) return n;
14102 if (!node) return n;
14103 if (!nd_type_p(node, NODE_BLOCK)) return n;
14104
14105 while (RNODE_BLOCK(node)->nd_next) {
14106 void_expr(p, RNODE_BLOCK(node)->nd_head);
14107 node = RNODE_BLOCK(node)->nd_next;
14108 }
14109 return RNODE_BLOCK(node)->nd_head;
14110}
14111
14112static NODE *
14113remove_begin(NODE *node)
14114{
14115 NODE **n = &node, *n1 = node;
14116 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14117 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14118 }
14119 return node;
14120}
14121
14122static void
14123reduce_nodes(struct parser_params *p, NODE **body)
14124{
14125 NODE *node = *body;
14126
14127 if (!node) {
14128 *body = NEW_NIL(&NULL_LOC);
14129 return;
14130 }
14131#define subnodes(type, n1, n2) \
14132 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14133 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14134 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14135
14136 while (node) {
14137 int newline = (int)nd_fl_newline(node);
14138 switch (nd_type(node)) {
14139 end:
14140 case NODE_NIL:
14141 *body = 0;
14142 return;
14143 case NODE_BEGIN:
14144 *body = node = RNODE_BEGIN(node)->nd_body;
14145 if (newline && node) nd_set_fl_newline(node);
14146 continue;
14147 case NODE_BLOCK:
14148 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14149 break;
14150 case NODE_IF:
14151 case NODE_UNLESS:
14152 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14153 return;
14154 case NODE_CASE:
14155 body = &RNODE_CASE(node)->nd_body;
14156 break;
14157 case NODE_WHEN:
14158 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14159 break;
14160 case NODE_ENSURE:
14161 body = &RNODE_ENSURE(node)->nd_head;
14162 break;
14163 case NODE_RESCUE:
14164 newline = 0; // RESBODY should not be a NEWLINE
14165 if (RNODE_RESCUE(node)->nd_else) {
14166 body = &RNODE_RESCUE(node)->nd_resq;
14167 break;
14168 }
14169 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14170 break;
14171 default:
14172 return;
14173 }
14174 node = *body;
14175 if (newline && node) nd_set_fl_newline(node);
14176 }
14177
14178#undef subnodes
14179}
14180
14181static int
14182is_static_content(NODE *node)
14183{
14184 if (!node) return 1;
14185 switch (nd_type(node)) {
14186 case NODE_HASH:
14187 if (!(node = RNODE_HASH(node)->nd_head)) break;
14188 case NODE_LIST:
14189 do {
14190 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14191 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14192 case NODE_SYM:
14193 case NODE_REGX:
14194 case NODE_LINE:
14195 case NODE_FILE:
14196 case NODE_ENCODING:
14197 case NODE_INTEGER:
14198 case NODE_FLOAT:
14199 case NODE_RATIONAL:
14200 case NODE_IMAGINARY:
14201 case NODE_STR:
14202 case NODE_NIL:
14203 case NODE_TRUE:
14204 case NODE_FALSE:
14205 case NODE_ZLIST:
14206 break;
14207 default:
14208 return 0;
14209 }
14210 return 1;
14211}
14212
14213static int
14214assign_in_cond(struct parser_params *p, NODE *node)
14215{
14216 switch (nd_type(node)) {
14217 case NODE_MASGN:
14218 case NODE_LASGN:
14219 case NODE_DASGN:
14220 case NODE_GASGN:
14221 case NODE_IASGN:
14222 case NODE_CVASGN:
14223 case NODE_CDECL:
14224 break;
14225
14226 default:
14227 return 0;
14228 }
14229
14230 if (!get_nd_value(p, node)) return 1;
14231 if (is_static_content(get_nd_value(p, node))) {
14232 /* reports always */
14233 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14234 }
14235 return 1;
14236}
14237
14238enum cond_type {
14239 COND_IN_OP,
14240 COND_IN_COND,
14241 COND_IN_FF
14242};
14243
14244#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14245 switch (t) { \
14246 case COND_IN_OP: break; \
14247 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14248 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14249 } \
14250} while (0)
14251
14252static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14253
14254static NODE*
14255range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14256{
14257 enum node_type type;
14258
14259 if (node == 0) return 0;
14260
14261 type = nd_type(node);
14262 value_expr(p, node);
14263 if (type == NODE_INTEGER) {
14264 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14265 ID lineno = rb_intern("$.");
14266 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14267 }
14268 return cond0(p, node, COND_IN_FF, loc, true);
14269}
14270
14271static NODE*
14272cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14273{
14274 if (node == 0) return 0;
14275 if (!(node = nd_once_body(node))) return 0;
14276 assign_in_cond(p, node);
14277
14278 switch (nd_type(node)) {
14279 case NODE_BEGIN:
14280 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14281 break;
14282
14283 case NODE_DSTR:
14284 case NODE_EVSTR:
14285 case NODE_STR:
14286 case NODE_FILE:
14287 SWITCH_BY_COND_TYPE(type, warn, "string ");
14288 break;
14289
14290 case NODE_REGX:
14291 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14292 nd_set_type(node, NODE_MATCH);
14293 break;
14294
14295 case NODE_DREGX:
14296 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14297
14298 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14299
14300 case NODE_BLOCK:
14301 {
14302 NODE *end = RNODE_BLOCK(node)->nd_end;
14303 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14304 if (top) top = node == end;
14305 *expr = cond0(p, *expr, type, loc, top);
14306 }
14307 break;
14308
14309 case NODE_AND:
14310 case NODE_OR:
14311 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14312 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14313 break;
14314
14315 case NODE_DOT2:
14316 case NODE_DOT3:
14317 if (!top) break;
14318 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14319 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14320 switch (nd_type(node)) {
14321 case NODE_DOT2:
14322 nd_set_type(node,NODE_FLIP2);
14323 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14324 (void)flip2;
14325 break;
14326 case NODE_DOT3:
14327 nd_set_type(node, NODE_FLIP3);
14328 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14329 (void)flip3;
14330 break;
14331 }
14332 break;
14333
14334 case NODE_SYM:
14335 case NODE_DSYM:
14336 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14337 break;
14338
14339 case NODE_LINE:
14340 case NODE_ENCODING:
14341 case NODE_INTEGER:
14342 case NODE_FLOAT:
14343 case NODE_RATIONAL:
14344 case NODE_IMAGINARY:
14345 SWITCH_BY_COND_TYPE(type, warning, "");
14346 break;
14347
14348 default:
14349 break;
14350 }
14351 return node;
14352}
14353
14354static NODE*
14355cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14356{
14357 if (node == 0) return 0;
14358 return cond0(p, node, COND_IN_COND, loc, true);
14359}
14360
14361static NODE*
14362method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14363{
14364 if (node == 0) return 0;
14365 return cond0(p, node, COND_IN_OP, loc, true);
14366}
14367
14368static NODE*
14369new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14370{
14371 YYLTYPE loc = {*pos, *pos};
14372 return NEW_NIL(&loc);
14373}
14374
14375static NODE*
14376new_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)
14377{
14378 if (!cc) return right;
14379 cc = cond0(p, cc, COND_IN_COND, loc, true);
14380 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14381}
14382
14383static NODE*
14384new_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)
14385{
14386 if (!cc) return right;
14387 cc = cond0(p, cc, COND_IN_COND, loc, true);
14388 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14389}
14390
14391#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))
14392
14393static NODE*
14394logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14395 const YYLTYPE *op_loc, const YYLTYPE *loc)
14396{
14397 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14398 NODE *op;
14399 value_expr(p, left);
14400 if (left && nd_type_p(left, type)) {
14401 NODE *node = left, *second;
14402 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14403 node = second;
14404 }
14405 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14406 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14407 left->nd_loc.end_pos = loc->end_pos;
14408 return left;
14409 }
14410 op = NEW_AND_OR(type, left, right, loc, op_loc);
14411 nd_set_line(op, op_loc->beg_pos.lineno);
14412 return op;
14413}
14414
14415#undef NEW_AND_OR
14416
14417static void
14418no_blockarg(struct parser_params *p, NODE *node)
14419{
14420 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14421 compile_error(p, "block argument should not be given");
14422 }
14423}
14424
14425static NODE *
14426ret_args(struct parser_params *p, NODE *node)
14427{
14428 if (node) {
14429 no_blockarg(p, node);
14430 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14431 node = RNODE_LIST(node)->nd_head;
14432 }
14433 }
14434 return node;
14435}
14436
14437static NODE*
14438negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14439{
14440 switch (nd_type(node)) {
14441 case NODE_INTEGER:
14442 RNODE_INTEGER(node)->minus = TRUE;
14443 break;
14444 case NODE_FLOAT:
14445 RNODE_FLOAT(node)->minus = TRUE;
14446 break;
14447 case NODE_RATIONAL:
14448 RNODE_RATIONAL(node)->minus = TRUE;
14449 break;
14450 case NODE_IMAGINARY:
14451 RNODE_IMAGINARY(node)->minus = TRUE;
14452 break;
14453 }
14454 node->nd_loc = *loc;
14455 return node;
14456}
14457
14458static NODE *
14459arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14460{
14461 if (node2) {
14462 if (!node1) return (NODE *)node2;
14463 node2->nd_head = node1;
14464 nd_set_first_lineno(node2, nd_first_lineno(node1));
14465 nd_set_first_column(node2, nd_first_column(node1));
14466 return (NODE *)node2;
14467 }
14468 return node1;
14469}
14470
14471static bool
14472args_info_empty_p(struct rb_args_info *args)
14473{
14474 if (args->pre_args_num) return false;
14475 if (args->post_args_num) return false;
14476 if (args->rest_arg) return false;
14477 if (args->opt_args) return false;
14478 if (args->block_arg) return false;
14479 if (args->kw_args) return false;
14480 if (args->kw_rest_arg) return false;
14481 return true;
14482}
14483
14484static rb_node_args_t *
14485new_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)
14486{
14487 struct rb_args_info *args = &tail->nd_ainfo;
14488
14489 if (args->forwarding) {
14490 if (rest_arg) {
14491 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14492 return tail;
14493 }
14494 rest_arg = idFWD_REST;
14495 }
14496
14497 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14498 args->pre_init = pre_args ? pre_args->nd_next : 0;
14499
14500 args->post_args_num = post_args ? post_args->nd_plen : 0;
14501 args->post_init = post_args ? post_args->nd_next : 0;
14502 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14503
14504 args->rest_arg = rest_arg;
14505
14506 args->opt_args = opt_args;
14507
14508 nd_set_loc(RNODE(tail), loc);
14509
14510 return tail;
14511}
14512
14513static rb_node_args_t *
14514new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14515{
14516 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14517 struct rb_args_info *args = &node->nd_ainfo;
14518 if (p->error_p) return node;
14519
14520 args->block_arg = block;
14521 args->kw_args = kw_args;
14522
14523 if (kw_args) {
14524 /*
14525 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14526 * variable order: k1, kr1, k2, &b, internal_id, krest
14527 * #=> <reorder>
14528 * variable order: kr1, k1, k2, internal_id, krest, &b
14529 */
14530 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14531 struct vtable *vtargs = p->lvtbl->args;
14532 rb_node_kw_arg_t *kwn = kw_args;
14533
14534 if (block) block = vtargs->tbl[vtargs->pos-1];
14535 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14536 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14537 while (kwn) {
14538 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14539 --kw_vars;
14540 --required_kw_vars;
14541 kwn = kwn->nd_next;
14542 }
14543
14544 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14545 ID vid = get_nd_vid(p, kwn->nd_body);
14546 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14547 *required_kw_vars++ = vid;
14548 }
14549 else {
14550 *kw_vars++ = vid;
14551 }
14552 }
14553
14554 arg_var(p, kw_bits);
14555 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14556 if (block) arg_var(p, block);
14557
14558 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14559 }
14560 else if (kw_rest_arg == idNil) {
14561 args->no_kwarg = 1;
14562 }
14563 else if (kw_rest_arg) {
14564 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14565 }
14566
14567 return node;
14568}
14569
14570static rb_node_args_t *
14571args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14572{
14573 if (max_numparam > NO_PARAM || it_id) {
14574 if (!args) {
14575 YYLTYPE loc = RUBY_INIT_YYLLOC();
14576 args = new_args_tail(p, 0, 0, 0, 0);
14577 nd_set_loc(RNODE(args), &loc);
14578 }
14579 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14580 }
14581 return args;
14582}
14583
14584static NODE*
14585new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14586{
14587 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14588
14589 if (pre_arg) {
14590 NODE *pre_args = NEW_LIST(pre_arg, loc);
14591 if (RNODE_ARYPTN(aryptn)->pre_args) {
14592 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14593 }
14594 else {
14595 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14596 }
14597 }
14598 return aryptn;
14599}
14600
14601static NODE*
14602new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14603{
14604 if (has_rest) {
14605 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14606 }
14607 else {
14608 rest_arg = NULL;
14609 }
14610 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14611
14612 return node;
14613}
14614
14615static NODE*
14616new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14617{
14618 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14619
14620 return fndptn;
14621}
14622
14623static NODE*
14624new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14625{
14626 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14627 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14628 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14629
14630 return node;
14631}
14632
14633static NODE*
14634new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14635{
14636 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14637 return hshptn;
14638}
14639
14640static NODE*
14641new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14642{
14643 NODE *node, *kw_rest_arg_node;
14644
14645 if (kw_rest_arg == idNil) {
14646 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14647 }
14648 else if (kw_rest_arg) {
14649 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14650 }
14651 else {
14652 kw_rest_arg_node = NULL;
14653 }
14654
14655 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14656
14657 return node;
14658}
14659
14660static NODE*
14661dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14662{
14663 if (!node) {
14664 return NEW_SYM(STR_NEW0(), loc);
14665 }
14666
14667 switch (nd_type(node)) {
14668 case NODE_DSTR:
14669 nd_set_type(node, NODE_DSYM);
14670 nd_set_loc(node, loc);
14671 break;
14672 case NODE_STR:
14673 node = str_to_sym_node(p, node, loc);
14674 break;
14675 default:
14676 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14677 break;
14678 }
14679 return node;
14680}
14681
14682static int
14683nd_type_st_key_enable_p(NODE *node)
14684{
14685 switch (nd_type(node)) {
14686 case NODE_INTEGER:
14687 case NODE_FLOAT:
14688 case NODE_RATIONAL:
14689 case NODE_IMAGINARY:
14690 case NODE_STR:
14691 case NODE_SYM:
14692 case NODE_REGX:
14693 case NODE_LINE:
14694 case NODE_FILE:
14695 case NODE_ENCODING:
14696 return true;
14697 default:
14698 return false;
14699 }
14700}
14701
14702static VALUE
14703nd_value(struct parser_params *p, NODE *node)
14704{
14705 switch (nd_type(node)) {
14706 case NODE_STR:
14707 return rb_node_str_string_val(node);
14708 case NODE_INTEGER:
14709 return rb_node_integer_literal_val(node);
14710 case NODE_FLOAT:
14711 return rb_node_float_literal_val(node);
14712 case NODE_RATIONAL:
14713 return rb_node_rational_literal_val(node);
14714 case NODE_IMAGINARY:
14715 return rb_node_imaginary_literal_val(node);
14716 case NODE_SYM:
14717 return rb_node_sym_string_val(node);
14718 case NODE_REGX:
14719 return rb_node_regx_string_val(node);
14720 case NODE_LINE:
14721 return rb_node_line_lineno_val(node);
14722 case NODE_ENCODING:
14723 return rb_node_encoding_val(node);
14724 case NODE_FILE:
14725 return rb_node_file_path_val(node);
14726 default:
14727 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14728 UNREACHABLE_RETURN(0);
14729 }
14730}
14731
14732static void
14733warn_duplicate_keys(struct parser_params *p, NODE *hash)
14734{
14735 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14736 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14737 while (hash && RNODE_LIST(hash)->nd_next) {
14738 NODE *head = RNODE_LIST(hash)->nd_head;
14739 NODE *value = RNODE_LIST(hash)->nd_next;
14740 NODE *next = RNODE_LIST(value)->nd_next;
14741 st_data_t key;
14742 st_data_t data;
14743
14744 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14745 if (!head) {
14746 head = value;
14747 }
14748
14749 if (nd_type_st_key_enable_p(head)) {
14750 key = (st_data_t)head;
14751
14752 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14753 rb_warn2L(nd_line((NODE *)data),
14754 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14755 nd_value(p, head), WARN_I(nd_line(head)));
14756 }
14757 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14758 }
14759 hash = next;
14760 }
14761 st_free_table(p->warn_duplicate_keys_table);
14762 p->warn_duplicate_keys_table = NULL;
14763}
14764
14765static NODE *
14766new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14767{
14768 if (hash) warn_duplicate_keys(p, hash);
14769 return NEW_HASH(hash, loc);
14770}
14771
14772static void
14773error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14774{
14775 if (is_private_local_id(p, id)) {
14776 return;
14777 }
14778 if (st_is_member(p->pvtbl, id)) {
14779 yyerror1(loc, "duplicated variable name");
14780 }
14781 else if (p->ctxt.in_alt_pattern && id) {
14782 yyerror1(loc, "variable capture in alternative pattern");
14783 }
14784 else {
14785 p->ctxt.capture_in_pattern = 1;
14786 st_insert(p->pvtbl, (st_data_t)id, 0);
14787 }
14788}
14789
14790static void
14791error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14792{
14793 if (!p->pktbl) {
14794 p->pktbl = st_init_numtable();
14795 }
14796 else if (st_is_member(p->pktbl, key)) {
14797 yyerror1(loc, "duplicated key name");
14798 return;
14799 }
14800 st_insert(p->pktbl, (st_data_t)key, 0);
14801}
14802
14803static NODE *
14804new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14805{
14806 return NEW_HASH(hash, loc);
14807}
14808
14809static NODE *
14810new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14811{
14812 NODE *asgn;
14813
14814 if (lhs) {
14815 ID vid = get_nd_vid(p, lhs);
14816 YYLTYPE lhs_loc = lhs->nd_loc;
14817 if (op == tOROP) {
14818 set_nd_value(p, lhs, rhs);
14819 nd_set_loc(lhs, loc);
14820 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14821 }
14822 else if (op == tANDOP) {
14823 set_nd_value(p, lhs, rhs);
14824 nd_set_loc(lhs, loc);
14825 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14826 }
14827 else {
14828 asgn = lhs;
14829 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14830 set_nd_value(p, asgn, rhs);
14831 nd_set_loc(asgn, loc);
14832 }
14833 }
14834 else {
14835 asgn = NEW_ERROR(loc);
14836 }
14837 return asgn;
14838}
14839
14840static NODE *
14841new_ary_op_assign(struct parser_params *p, NODE *ary,
14842 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14843 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14844{
14845 NODE *asgn;
14846
14847 aryset_check(p, args);
14848 args = make_list(args, args_loc);
14849 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14850 fixpos(asgn, ary);
14851 return asgn;
14852}
14853
14854static NODE *
14855new_attr_op_assign(struct parser_params *p, NODE *lhs,
14856 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14857 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14858{
14859 NODE *asgn;
14860
14861 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14862 fixpos(asgn, lhs);
14863 return asgn;
14864}
14865
14866static NODE *
14867new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14868{
14869 NODE *asgn;
14870
14871 if (lhs) {
14872 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14873 }
14874 else {
14875 asgn = NEW_ERROR(loc);
14876 }
14877 fixpos(asgn, lhs);
14878 return asgn;
14879}
14880
14881static NODE *
14882const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14883{
14884 if (p->ctxt.in_def) {
14885#ifndef RIPPER
14886 yyerror1(loc, "dynamic constant assignment");
14887#else
14888 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14889#endif
14890 }
14891 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14892}
14893
14894#ifdef RIPPER
14895static VALUE
14896assign_error(struct parser_params *p, const char *mesg, VALUE a)
14897{
14898 a = dispatch2(assign_error, ERR_MESG(), a);
14899 ripper_error(p);
14900 return a;
14901}
14902#endif
14903
14904static NODE *
14905new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14906{
14907 NODE *result = head;
14908 if (rescue) {
14909 NODE *tmp = rescue_else ? rescue_else : rescue;
14910 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14911
14912 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14913 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14914 }
14915 if (ensure) {
14916 result = NEW_ENSURE(result, ensure, loc);
14917 }
14918 fixpos(result, head);
14919 return result;
14920}
14921
14922static void
14923warn_unused_var(struct parser_params *p, struct local_vars *local)
14924{
14925 int cnt;
14926
14927 if (!local->used) return;
14928 cnt = local->used->pos;
14929 if (cnt != local->vars->pos) {
14930 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14931 }
14932#ifndef RIPPER
14933 ID *v = local->vars->tbl;
14934 ID *u = local->used->tbl;
14935 for (int i = 0; i < cnt; ++i) {
14936 if (!v[i] || (u[i] & LVAR_USED)) continue;
14937 if (is_private_local_id(p, v[i])) continue;
14938 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14939 }
14940#endif
14941}
14942
14943static void
14944local_push(struct parser_params *p, int toplevel_scope)
14945{
14946 struct local_vars *local;
14947 int inherits_dvars = toplevel_scope && compile_for_eval;
14948 int warn_unused_vars = RTEST(ruby_verbose);
14949
14950 local = ALLOC(struct local_vars);
14951 local->prev = p->lvtbl;
14952 local->args = vtable_alloc(0);
14953 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14954#ifndef RIPPER
14955 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14956 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14957#endif
14958 local->numparam.outer = 0;
14959 local->numparam.inner = 0;
14960 local->numparam.current = 0;
14961 local->it = 0;
14962 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14963
14964# if WARN_PAST_SCOPE
14965 local->past = 0;
14966# endif
14967 CMDARG_PUSH(0);
14968 COND_PUSH(0);
14969 p->lvtbl = local;
14970}
14971
14972static void
14973vtable_chain_free(struct parser_params *p, struct vtable *table)
14974{
14975 while (!DVARS_TERMINAL_P(table)) {
14976 struct vtable *cur_table = table;
14977 table = cur_table->prev;
14978 vtable_free(cur_table);
14979 }
14980}
14981
14982static void
14983local_free(struct parser_params *p, struct local_vars *local)
14984{
14985 vtable_chain_free(p, local->used);
14986
14987# if WARN_PAST_SCOPE
14988 vtable_chain_free(p, local->past);
14989# endif
14990
14991 vtable_chain_free(p, local->args);
14992 vtable_chain_free(p, local->vars);
14993
14994 ruby_sized_xfree(local, sizeof(struct local_vars));
14995}
14996
14997static void
14998local_pop(struct parser_params *p)
14999{
15000 struct local_vars *local = p->lvtbl->prev;
15001 if (p->lvtbl->used) {
15002 warn_unused_var(p, p->lvtbl);
15003 }
15004
15005 local_free(p, p->lvtbl);
15006 p->lvtbl = local;
15007
15008 CMDARG_POP();
15009 COND_POP();
15010}
15011
15012static rb_ast_id_table_t *
15013local_tbl(struct parser_params *p)
15014{
15015 int cnt_args = vtable_size(p->lvtbl->args);
15016 int cnt_vars = vtable_size(p->lvtbl->vars);
15017 int cnt = cnt_args + cnt_vars;
15018 int i, j;
15019 rb_ast_id_table_t *tbl;
15020
15021 if (cnt <= 0) return 0;
15022 tbl = rb_ast_new_local_table(p->ast, cnt);
15023 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
15024 /* remove IDs duplicated to warn shadowing */
15025 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15026 ID id = p->lvtbl->vars->tbl[i];
15027 if (!vtable_included(p->lvtbl->args, id)) {
15028 tbl->ids[j++] = id;
15029 }
15030 }
15031 if (j < cnt) {
15032 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15033 }
15034
15035 return tbl;
15036}
15037
15038static void
15039numparam_name(struct parser_params *p, ID id)
15040{
15041 if (!NUMPARAM_ID_P(id)) return;
15042 compile_error(p, "_%d is reserved for numbered parameter",
15043 NUMPARAM_ID_TO_IDX(id));
15044}
15045
15046static void
15047arg_var(struct parser_params *p, ID id)
15048{
15049 numparam_name(p, id);
15050 vtable_add(p->lvtbl->args, id);
15051}
15052
15053static void
15054local_var(struct parser_params *p, ID id)
15055{
15056 numparam_name(p, id);
15057 vtable_add(p->lvtbl->vars, id);
15058 if (p->lvtbl->used) {
15059 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15060 }
15061}
15062
15063#ifndef RIPPER
15064int
15065rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15066{
15067 return rb_local_defined(id, iseq);
15068}
15069#endif
15070
15071static int
15072local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15073{
15074 struct vtable *vars, *args, *used;
15075
15076 vars = p->lvtbl->vars;
15077 args = p->lvtbl->args;
15078 used = p->lvtbl->used;
15079
15080 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15081 vars = vars->prev;
15082 args = args->prev;
15083 if (used) used = used->prev;
15084 }
15085
15086 if (vars && vars->prev == DVARS_INHERIT) {
15087 return rb_parser_local_defined(p, id, p->parent_iseq);
15088 }
15089 else if (vtable_included(args, id)) {
15090 return 1;
15091 }
15092 else {
15093 int i = vtable_included(vars, id);
15094 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15095 return i != 0;
15096 }
15097}
15098
15099static int
15100local_id(struct parser_params *p, ID id)
15101{
15102 return local_id_ref(p, id, NULL);
15103}
15104
15105static int
15106check_forwarding_args(struct parser_params *p)
15107{
15108 if (local_id(p, idFWD_ALL)) return TRUE;
15109 compile_error(p, "unexpected ...");
15110 return FALSE;
15111}
15112
15113static void
15114add_forwarding_args(struct parser_params *p)
15115{
15116 arg_var(p, idFWD_REST);
15117 arg_var(p, idFWD_KWREST);
15118 arg_var(p, idFWD_BLOCK);
15119 arg_var(p, idFWD_ALL);
15120}
15121
15122static void
15123forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15124{
15125 bool conflict = false;
15126
15127 struct vtable *vars, *args;
15128
15129 vars = p->lvtbl->vars;
15130 args = p->lvtbl->args;
15131
15132 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15133 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15134 vars = vars->prev;
15135 args = args->prev;
15136 }
15137
15138 bool found = false;
15139 if (vars && vars->prev == DVARS_INHERIT && !found) {
15140 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15141 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15142 }
15143 else {
15144 found = (vtable_included(args, arg) &&
15145 !(all && vtable_included(args, all)));
15146 }
15147
15148 if (!found) {
15149 compile_error(p, "no anonymous %s parameter", var);
15150 }
15151 else if (conflict) {
15152 compile_error(p, "anonymous %s parameter is also used within block", var);
15153 }
15154}
15155
15156static NODE *
15157new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15158{
15159 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15160 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15161 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15162 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15163 block->forwarding = TRUE;
15164 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15165 return arg_blk_pass(args, block);
15166}
15167
15168static NODE *
15169numparam_push(struct parser_params *p)
15170{
15171 struct local_vars *local = p->lvtbl;
15172 NODE *inner = local->numparam.inner;
15173 if (!local->numparam.outer) {
15174 local->numparam.outer = local->numparam.current;
15175 }
15176 local->numparam.inner = 0;
15177 local->numparam.current = 0;
15178 local->it = 0;
15179 return inner;
15180}
15181
15182static void
15183numparam_pop(struct parser_params *p, NODE *prev_inner)
15184{
15185 struct local_vars *local = p->lvtbl;
15186 if (prev_inner) {
15187 /* prefer first one */
15188 local->numparam.inner = prev_inner;
15189 }
15190 else if (local->numparam.current) {
15191 /* current and inner are exclusive */
15192 local->numparam.inner = local->numparam.current;
15193 }
15194 if (p->max_numparam > NO_PARAM) {
15195 /* current and outer are exclusive */
15196 local->numparam.current = local->numparam.outer;
15197 local->numparam.outer = 0;
15198 }
15199 else {
15200 /* no numbered parameter */
15201 local->numparam.current = 0;
15202 }
15203 local->it = 0;
15204}
15205
15206static const struct vtable *
15207dyna_push(struct parser_params *p)
15208{
15209 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15210 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15211 if (p->lvtbl->used) {
15212 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15213 }
15214 return p->lvtbl->args;
15215}
15216
15217static void
15218dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15219{
15220 struct vtable *tmp = *vtblp;
15221 *vtblp = tmp->prev;
15222# if WARN_PAST_SCOPE
15223 if (p->past_scope_enabled) {
15224 tmp->prev = p->lvtbl->past;
15225 p->lvtbl->past = tmp;
15226 return;
15227 }
15228# endif
15229 vtable_free(tmp);
15230}
15231
15232static void
15233dyna_pop_1(struct parser_params *p)
15234{
15235 struct vtable *tmp;
15236
15237 if ((tmp = p->lvtbl->used) != 0) {
15238 warn_unused_var(p, p->lvtbl);
15239 p->lvtbl->used = p->lvtbl->used->prev;
15240 vtable_free(tmp);
15241 }
15242 dyna_pop_vtable(p, &p->lvtbl->args);
15243 dyna_pop_vtable(p, &p->lvtbl->vars);
15244}
15245
15246static void
15247dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15248{
15249 while (p->lvtbl->args != lvargs) {
15250 dyna_pop_1(p);
15251 if (!p->lvtbl->args) {
15252 struct local_vars *local = p->lvtbl->prev;
15253 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15254 p->lvtbl = local;
15255 }
15256 }
15257 dyna_pop_1(p);
15258}
15259
15260static int
15261dyna_in_block(struct parser_params *p)
15262{
15263 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15264}
15265
15266#ifndef RIPPER
15267int
15268dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15269{
15270 struct vtable *vars, *args, *used;
15271 int i;
15272
15273 args = p->lvtbl->args;
15274 vars = p->lvtbl->vars;
15275 used = p->lvtbl->used;
15276
15277 while (!DVARS_TERMINAL_P(vars)) {
15278 if (vtable_included(args, id)) {
15279 return 1;
15280 }
15281 if ((i = vtable_included(vars, id)) != 0) {
15282 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15283 return 1;
15284 }
15285 args = args->prev;
15286 vars = vars->prev;
15287 if (!vidrefp) used = 0;
15288 if (used) used = used->prev;
15289 }
15290
15291 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15292 return rb_dvar_defined(id, p->parent_iseq);
15293 }
15294
15295 return 0;
15296}
15297#endif
15298
15299static int
15300dvar_defined(struct parser_params *p, ID id)
15301{
15302 return dvar_defined_ref(p, id, NULL);
15303}
15304
15305static int
15306dvar_curr(struct parser_params *p, ID id)
15307{
15308 return (vtable_included(p->lvtbl->args, id) ||
15309 vtable_included(p->lvtbl->vars, id));
15310}
15311
15312static void
15313reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15314{
15315 compile_error(p,
15316 "regexp encoding option '%c' differs from source encoding '%s'",
15317 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15318}
15319
15320#ifndef RIPPER
15321static rb_encoding *
15322find_enc(struct parser_params* p, const char *name)
15323{
15324 int idx = rb_enc_find_index(name);
15325 if (idx < 0) {
15326 rb_bug("unknown encoding name: %s", name);
15327 }
15328
15329 return rb_enc_from_index(idx);
15330}
15331
15332static rb_encoding *
15333kcode_to_enc(struct parser_params* p, int kcode)
15334{
15335 rb_encoding *enc;
15336
15337 switch (kcode) {
15338 case ENC_ASCII8BIT:
15339 enc = rb_ascii8bit_encoding();
15340 break;
15341 case ENC_EUC_JP:
15342 enc = find_enc(p, "EUC-JP");
15343 break;
15344 case ENC_Windows_31J:
15345 enc = find_enc(p, "Windows-31J");
15346 break;
15347 case ENC_UTF8:
15348 enc = rb_utf8_encoding();
15349 break;
15350 default:
15351 enc = NULL;
15352 break;
15353 }
15354
15355 return enc;
15356}
15357
15358int
15359rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15360{
15361 int c = RE_OPTION_ENCODING_IDX(options);
15362
15363 if (c) {
15364 int opt, idx;
15365 rb_encoding *enc;
15366
15367 char_to_option_kcode(c, &opt, &idx);
15368 enc = kcode_to_enc(p, idx);
15369 if (enc != rb_parser_str_get_encoding(str) &&
15370 !rb_parser_is_ascii_string(p, str)) {
15371 goto error;
15372 }
15373 rb_parser_string_set_encoding(str, enc);
15374 }
15375 else if (RE_OPTION_ENCODING_NONE(options)) {
15376 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15377 !rb_parser_is_ascii_string(p, str)) {
15378 c = 'n';
15379 goto error;
15380 }
15381 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15382 }
15383 else if (rb_is_usascii_enc(p->enc)) {
15384 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15385 }
15386 return 0;
15387
15388 error:
15389 return c;
15390}
15391#endif
15392
15393static void
15394reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15395{
15396 int c = rb_reg_fragment_setenc(p, str, options);
15397 if (c) reg_fragment_enc_error(p, str, c);
15398}
15399
15400#ifndef UNIVERSAL_PARSER
15401typedef struct {
15402 struct parser_params* parser;
15403 rb_encoding *enc;
15404 NODE *succ_block;
15405 const YYLTYPE *loc;
15406 rb_parser_assignable_func assignable;
15407} reg_named_capture_assign_t;
15408
15409static int
15410reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15411 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15412{
15413 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15414 struct parser_params* p = arg->parser;
15415 rb_encoding *enc = arg->enc;
15416 long len = name_end - name;
15417 const char *s = (const char *)name;
15418
15419 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15420}
15421
15422static NODE *
15423reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15424{
15425 reg_named_capture_assign_t arg;
15426
15427 arg.parser = p;
15428 arg.enc = rb_enc_get(regexp);
15429 arg.succ_block = 0;
15430 arg.loc = loc;
15431 arg.assignable = assignable;
15432 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15433
15434 if (!arg.succ_block) return 0;
15435 return RNODE_BLOCK(arg.succ_block)->nd_next;
15436}
15437#endif
15438
15439#ifndef RIPPER
15440NODE *
15441rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15442{
15443 return assignable(p, id, val, loc);
15444}
15445
15446int
15447rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15448 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15449{
15450 ID var;
15451 NODE *node, *succ;
15452
15453 if (!len) return ST_CONTINUE;
15454
15455 if (UNLIKELY(p->enc != enc)) {
15456 int cr = ENC_CODERANGE_7BIT;
15457 rb_str_coderange_scan_restartable(s, s + len, rb_ascii8bit_encoding(), &cr);
15458 if (UNLIKELY(cr != ENC_CODERANGE_7BIT)) return ST_CONTINUE;
15459 }
15460
15461 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15462 return ST_CONTINUE;
15463
15464 var = intern_cstr(s, len, enc);
15465 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15466 if (!lvar_defined(p, var)) return ST_CONTINUE;
15467 }
15468 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15469 succ = *succ_block;
15470 if (!succ) succ = NEW_ERROR(loc);
15471 succ = block_append(p, succ, node);
15472 *succ_block = succ;
15473 return ST_CONTINUE;
15474}
15475#endif
15476
15477static VALUE
15478parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15479{
15480 VALUE str2;
15481 reg_fragment_setenc(p, str, options);
15482 str2 = rb_str_new_parser_string(str);
15483 return rb_parser_reg_compile(p, str2, options);
15484}
15485
15486#ifndef RIPPER
15487VALUE
15488rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15489{
15490 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15491}
15492#endif
15493
15494static VALUE
15495reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15496{
15497 VALUE re;
15498 VALUE err;
15499
15500 err = rb_errinfo();
15501 re = parser_reg_compile(p, str, options);
15502 if (NIL_P(re)) {
15503 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15504 rb_set_errinfo(err);
15505 compile_error(p, "%"PRIsVALUE, m);
15506 return Qnil;
15507 }
15508 return re;
15509}
15510
15511#ifndef RIPPER
15512void
15513rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15514{
15515 p->do_print = print;
15516 p->do_loop = loop;
15517 p->do_chomp = chomp;
15518 p->do_split = split;
15519}
15520
15521static NODE *
15522parser_append_options(struct parser_params *p, NODE *node)
15523{
15524 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15525 const YYLTYPE *const LOC = &default_location;
15526
15527 if (p->do_print) {
15528 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15529 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15530 LOC);
15531 node = block_append(p, node, print);
15532 }
15533
15534 if (p->do_loop) {
15535 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15536
15537 if (p->do_split) {
15538 ID ifs = rb_intern("$;");
15539 ID fields = rb_intern("$F");
15540 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15541 NODE *split = NEW_GASGN(fields,
15542 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15543 rb_intern("split"), args, LOC),
15544 LOC);
15545 node = block_append(p, split, node);
15546 }
15547 if (p->do_chomp) {
15548 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15549 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15550 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15551 }
15552
15553 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15554 }
15555
15556 return node;
15557}
15558
15559void
15560rb_init_parse(void)
15561{
15562 /* just to suppress unused-function warnings */
15563 (void)nodetype;
15564 (void)nodeline;
15565}
15566
15567ID
15568internal_id(struct parser_params *p)
15569{
15570 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15571}
15572#endif /* !RIPPER */
15573
15574static void
15575parser_initialize(struct parser_params *p)
15576{
15577 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15578 p->command_start = TRUE;
15579 p->ruby_sourcefile_string = Qnil;
15580 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15581 string_buffer_init(p);
15582 p->node_id = 0;
15583 p->delayed.token = NULL;
15584 p->frozen_string_literal = -1; /* not specified */
15585#ifndef RIPPER
15586 p->error_buffer = Qfalse;
15587 p->end_expect_token_locations = NULL;
15588 p->token_id = 0;
15589 p->tokens = NULL;
15590#else
15591 p->result = Qnil;
15592 p->parsing_thread = Qnil;
15593 p->s_value = Qnil;
15594 p->s_lvalue = Qnil;
15595 p->s_value_stack = rb_ary_new();
15596#endif
15597 p->debug_buffer = Qnil;
15598 p->debug_output = rb_ractor_stdout();
15599 p->enc = rb_utf8_encoding();
15600 p->exits = 0;
15601}
15602
15603#ifdef RIPPER
15604#define rb_ruby_parser_mark ripper_parser_mark
15605#define rb_ruby_parser_free ripper_parser_free
15606#define rb_ruby_parser_memsize ripper_parser_memsize
15607#endif
15608
15609void
15610rb_ruby_parser_mark(void *ptr)
15611{
15612 struct parser_params *p = (struct parser_params*)ptr;
15613
15614 rb_gc_mark(p->ruby_sourcefile_string);
15615#ifndef RIPPER
15616 rb_gc_mark(p->error_buffer);
15617#else
15618 rb_gc_mark(p->value);
15619 rb_gc_mark(p->result);
15620 rb_gc_mark(p->parsing_thread);
15621 rb_gc_mark(p->s_value);
15622 rb_gc_mark(p->s_lvalue);
15623 rb_gc_mark(p->s_value_stack);
15624#endif
15625 rb_gc_mark(p->debug_buffer);
15626 rb_gc_mark(p->debug_output);
15627}
15628
15629void
15630rb_ruby_parser_free(void *ptr)
15631{
15632 struct parser_params *p = (struct parser_params*)ptr;
15633 struct local_vars *local, *prev;
15634
15635 if (p->ast) {
15636 rb_ast_free(p->ast);
15637 }
15638
15639 if (p->warn_duplicate_keys_table) {
15640 st_free_table(p->warn_duplicate_keys_table);
15641 }
15642
15643#ifndef RIPPER
15644 if (p->tokens) {
15645 rb_parser_ary_free(p, p->tokens);
15646 }
15647#endif
15648
15649 if (p->tokenbuf) {
15650 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15651 }
15652
15653 for (local = p->lvtbl; local; local = prev) {
15654 prev = local->prev;
15655 local_free(p, local);
15656 }
15657
15658 {
15659 token_info *ptinfo;
15660 while ((ptinfo = p->token_info) != 0) {
15661 p->token_info = ptinfo->next;
15662 xfree(ptinfo);
15663 }
15664 }
15665 string_buffer_free(p);
15666
15667 if (p->pvtbl) {
15668 st_free_table(p->pvtbl);
15669 }
15670
15671 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15672 st_free_table(p->case_labels);
15673 }
15674
15675 xfree(p->lex.strterm);
15676 p->lex.strterm = 0;
15677
15678 xfree(ptr);
15679}
15680
15681size_t
15682rb_ruby_parser_memsize(const void *ptr)
15683{
15684 struct parser_params *p = (struct parser_params*)ptr;
15685 struct local_vars *local;
15686 size_t size = sizeof(*p);
15687
15688 size += p->toksiz;
15689 for (local = p->lvtbl; local; local = local->prev) {
15690 size += sizeof(*local);
15691 if (local->vars) size += local->vars->capa * sizeof(ID);
15692 }
15693 return size;
15694}
15695
15696#ifndef RIPPER
15697#undef rb_reserved_word
15698
15699const struct kwtable *
15700rb_reserved_word(const char *str, unsigned int len)
15701{
15702 return reserved_word(str, len);
15703}
15704
15705#ifdef UNIVERSAL_PARSER
15706rb_parser_t *
15707rb_ruby_parser_allocate(const rb_parser_config_t *config)
15708{
15709 /* parser_initialize expects fields to be set to 0 */
15710 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15711 p->config = config;
15712 return p;
15713}
15714
15715rb_parser_t *
15716rb_ruby_parser_new(const rb_parser_config_t *config)
15717{
15718 /* parser_initialize expects fields to be set to 0 */
15719 rb_parser_t *p = rb_ruby_parser_allocate(config);
15720 parser_initialize(p);
15721 return p;
15722}
15723#else
15724rb_parser_t *
15725rb_ruby_parser_allocate(void)
15726{
15727 /* parser_initialize expects fields to be set to 0 */
15728 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15729 return p;
15730}
15731
15732rb_parser_t *
15733rb_ruby_parser_new(void)
15734{
15735 /* parser_initialize expects fields to be set to 0 */
15736 rb_parser_t *p = rb_ruby_parser_allocate();
15737 parser_initialize(p);
15738 return p;
15739}
15740#endif
15741
15742rb_parser_t *
15743rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15744{
15745 p->error_buffer = main ? Qfalse : Qnil;
15746 p->parent_iseq = base;
15747 return p;
15748}
15749
15750void
15751rb_ruby_parser_set_script_lines(rb_parser_t *p)
15752{
15753 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15754}
15755
15756void
15757rb_ruby_parser_error_tolerant(rb_parser_t *p)
15758{
15759 p->error_tolerant = 1;
15760}
15761
15762void
15763rb_ruby_parser_keep_tokens(rb_parser_t *p)
15764{
15765 p->keep_tokens = 1;
15766 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15767}
15768
15769rb_encoding *
15770rb_ruby_parser_encoding(rb_parser_t *p)
15771{
15772 return p->enc;
15773}
15774
15775int
15776rb_ruby_parser_end_seen_p(rb_parser_t *p)
15777{
15778 return p->ruby__end__seen;
15779}
15780
15781int
15782rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15783{
15784 p->debug = flag;
15785 return flag;
15786}
15787#endif /* !RIPPER */
15788
15789#ifdef RIPPER
15790int
15791rb_ruby_parser_get_yydebug(rb_parser_t *p)
15792{
15793 return p->debug;
15794}
15795
15796void
15797rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15798{
15799 p->value = value;
15800}
15801
15802int
15803rb_ruby_parser_error_p(rb_parser_t *p)
15804{
15805 return p->error_p;
15806}
15807
15808VALUE
15809rb_ruby_parser_debug_output(rb_parser_t *p)
15810{
15811 return p->debug_output;
15812}
15813
15814void
15815rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15816{
15817 p->debug_output = output;
15818}
15819
15820VALUE
15821rb_ruby_parser_parsing_thread(rb_parser_t *p)
15822{
15823 return p->parsing_thread;
15824}
15825
15826void
15827rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15828{
15829 p->parsing_thread = parsing_thread;
15830}
15831
15832void
15833rb_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)
15834{
15835 p->lex.gets = gets;
15836 p->lex.input = input;
15837 p->eofp = 0;
15838 p->ruby_sourcefile_string = sourcefile_string;
15839 p->ruby_sourcefile = sourcefile;
15840 p->ruby_sourceline = sourceline;
15841}
15842
15843VALUE
15844rb_ruby_parser_result(rb_parser_t *p)
15845{
15846 return p->result;
15847}
15848
15849rb_encoding *
15850rb_ruby_parser_enc(rb_parser_t *p)
15851{
15852 return p->enc;
15853}
15854
15855VALUE
15856rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15857{
15858 return p->ruby_sourcefile_string;
15859}
15860
15861int
15862rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15863{
15864 return p->ruby_sourceline;
15865}
15866
15867int
15868rb_ruby_parser_lex_state(rb_parser_t *p)
15869{
15870 return p->lex.state;
15871}
15872
15873void
15874rb_ruby_ripper_parse0(rb_parser_t *p)
15875{
15876 parser_prepare(p);
15877 p->ast = rb_ast_new();
15878 ripper_yyparse((void*)p);
15879 rb_ast_free(p->ast);
15880 p->ast = 0;
15881 p->eval_tree = 0;
15882 p->eval_tree_begin = 0;
15883}
15884
15885int
15886rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15887{
15888 return dedent_string(p, string, width);
15889}
15890
15891int
15892rb_ruby_ripper_initialized_p(rb_parser_t *p)
15893{
15894 return p->lex.input != 0;
15895}
15896
15897void
15898rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15899{
15900 parser_initialize(p);
15901}
15902
15903long
15904rb_ruby_ripper_column(rb_parser_t *p)
15905{
15906 return p->lex.ptok - p->lex.pbeg;
15907}
15908
15909long
15910rb_ruby_ripper_token_len(rb_parser_t *p)
15911{
15912 return p->lex.pcur - p->lex.ptok;
15913}
15914
15915rb_parser_string_t *
15916rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15917{
15918 return p->lex.lastline;
15919}
15920
15921VALUE
15922rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15923{
15924 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15925}
15926
15927#ifdef UNIVERSAL_PARSER
15928rb_parser_t *
15929rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15930{
15931 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15932 p->config = config;
15933 return p;
15934}
15935#endif
15936
15937struct parser_params*
15938rb_ruby_ripper_parser_allocate(void)
15939{
15940 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15941}
15942#endif /* RIPPER */
15943
15944#ifndef RIPPER
15945void
15946rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15947{
15948 va_list ap;
15949 VALUE mesg = p->debug_buffer;
15950
15951 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15952 va_start(ap, fmt);
15953 rb_str_vcatf(mesg, fmt, ap);
15954 va_end(ap);
15955 if (char_at_end(p, mesg, 0) == '\n') {
15956 rb_io_write(p->debug_output, mesg);
15957 p->debug_buffer = Qnil;
15958 }
15959}
15960
15961static void
15962parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15963{
15964 va_list ap;
15965 int lineno, column;
15966
15967 if (loc) {
15968 lineno = loc->end_pos.lineno;
15969 column = loc->end_pos.column;
15970 }
15971 else {
15972 lineno = p->ruby_sourceline;
15973 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15974 }
15975
15976 rb_io_flush(p->debug_output);
15977 p->error_p = 1;
15978 va_start(ap, fmt);
15979 p->error_buffer =
15980 rb_syntax_error_append(p->error_buffer,
15981 p->ruby_sourcefile_string,
15982 lineno, column,
15983 p->enc, fmt, ap);
15984 va_end(ap);
15985}
15986
15987static size_t
15988count_char(const char *str, int c)
15989{
15990 int n = 0;
15991 while (str[n] == c) ++n;
15992 return n;
15993}
15994
15995/*
15996 * strip enclosing double-quotes, same as the default yytnamerr except
15997 * for that single-quotes matching back-quotes do not stop stripping.
15998 *
15999 * "\"`class' keyword\"" => "`class' keyword"
16000 */
16001size_t
16002rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16003{
16004 if (*yystr == '"') {
16005 size_t yyn = 0, bquote = 0;
16006 const char *yyp = yystr;
16007
16008 while (*++yyp) {
16009 switch (*yyp) {
16010 case '\'':
16011 if (!bquote) {
16012 bquote = count_char(yyp+1, '\'') + 1;
16013 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16014 yyn += bquote;
16015 yyp += bquote - 1;
16016 break;
16017 }
16018 else {
16019 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16020 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16021 yyn += bquote;
16022 yyp += bquote - 1;
16023 bquote = 0;
16024 break;
16025 }
16026 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16027 if (yyres) memcpy(yyres + yyn, yyp, 3);
16028 yyn += 3;
16029 yyp += 2;
16030 break;
16031 }
16032 goto do_not_strip_quotes;
16033 }
16034
16035 case ',':
16036 goto do_not_strip_quotes;
16037
16038 case '\\':
16039 if (*++yyp != '\\')
16040 goto do_not_strip_quotes;
16041 /* Fall through. */
16042 default:
16043 if (yyres)
16044 yyres[yyn] = *yyp;
16045 yyn++;
16046 break;
16047
16048 case '"':
16049 case '\0':
16050 if (yyres)
16051 yyres[yyn] = '\0';
16052 return yyn;
16053 }
16054 }
16055 do_not_strip_quotes: ;
16056 }
16057
16058 if (!yyres) return strlen(yystr);
16059
16060 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16061}
16062#endif
16063
16064#ifdef RIPPER
16065#define validate(x) (void)(x)
16066
16067static VALUE
16068ripper_dispatch0(struct parser_params *p, ID mid)
16069{
16070 return rb_funcall(p->value, mid, 0);
16071}
16072
16073static VALUE
16074ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16075{
16076 validate(a);
16077 return rb_funcall(p->value, mid, 1, a);
16078}
16079
16080static VALUE
16081ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16082{
16083 validate(a);
16084 validate(b);
16085 return rb_funcall(p->value, mid, 2, a, b);
16086}
16087
16088static VALUE
16089ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16090{
16091 validate(a);
16092 validate(b);
16093 validate(c);
16094 return rb_funcall(p->value, mid, 3, a, b, c);
16095}
16096
16097static VALUE
16098ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16099{
16100 validate(a);
16101 validate(b);
16102 validate(c);
16103 validate(d);
16104 return rb_funcall(p->value, mid, 4, a, b, c, d);
16105}
16106
16107static VALUE
16108ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16109{
16110 validate(a);
16111 validate(b);
16112 validate(c);
16113 validate(d);
16114 validate(e);
16115 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16116}
16117
16118static VALUE
16119ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16120{
16121 validate(a);
16122 validate(b);
16123 validate(c);
16124 validate(d);
16125 validate(e);
16126 validate(f);
16127 validate(g);
16128 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16129}
16130
16131void
16132ripper_error(struct parser_params *p)
16133{
16134 p->error_p = TRUE;
16135}
16136
16137VALUE
16138ripper_value(struct parser_params *p)
16139{
16140 (void)yystpcpy; /* may not used in newer bison */
16141
16142 return p->value;
16143}
16144
16145#endif /* RIPPER */
16146/*
16147 * Local variables:
16148 * mode: c
16149 * c-file-style: "ruby"
16150 * End:
16151 */