Ruby 4.1.0dev (2026-09-07 revision 11ce3778c6eacc10897729314e5ab3bed7830971)
parse.y (11ce3778c6eacc10897729314e5ab3bed7830971)
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%{
13
14#if !YYPURE
15# error needs pure parser
16#endif
17#define YYDEBUG 1
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20
21/* For Ripper */
22#ifdef RUBY_EXTCONF_H
23# include RUBY_EXTCONF_H
24#endif
25
26#include "ruby/internal/config.h"
27#include "internal/thread.h"
28
29#include <errno.h>
30
31#ifdef UNIVERSAL_PARSER
32
33#include "internal/ruby_parser.h"
34#include "parser_node.h"
35#include "universal_parser.c"
36
37#ifdef RIPPER
38#define STATIC_ID2SYM p->config->static_id2sym
39#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
40#endif
41
42#else
43
44#include "internal.h"
45#include "internal/array.h"
46#include "internal/compile.h"
47#include "internal/compilers.h"
48#include "internal/complex.h"
49#include "internal/encoding.h"
50#include "internal/error.h"
51#include "internal/gc.h"
52#include "internal/hash.h"
53#include "internal/io.h"
54#include "internal/numeric.h"
55#include "internal/parse.h"
56#include "internal/rational.h"
57#include "internal/re.h"
58#include "internal/ruby_parser.h"
59#include "internal/symbol.h"
60#include "internal/thread.h"
61#include "node.h"
62#include "parser_node.h"
63#include "probes.h"
64#include "regenc.h"
65#include "ruby/encoding.h"
66#include "ruby/regex.h"
67#include "ruby/ruby.h"
68#include "ruby/st.h"
69#include "ruby/util.h"
70#include "ruby/ractor.h"
71#include "symbol.h"
72
73#ifndef RIPPER
74static VALUE
75syntax_error_new(void)
76{
77 return rb_class_new_instance(0, 0, rb_eSyntaxError);
78}
79#endif
80
81static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
82
83#define compile_callback rb_suppress_tracing
84#endif /* !UNIVERSAL_PARSER */
85
86#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
87#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
88
89static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
90
91#ifndef RIPPER
92static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
93#endif
94
95static int
96node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
97{
98 return (n1->minus != n2->minus ||
99 n1->base != n2->base ||
100 strcmp(n1->val, n2->val));
101}
102
103static int
104node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
105{
106 return (n1->minus != n2->minus ||
107 strcmp(n1->val, n2->val));
108}
109
110static int
111node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
112{
113 return (n1->minus != n2->minus ||
114 n1->base != n2->base ||
115 n1->seen_point != n2->seen_point ||
116 strcmp(n1->val, n2->val));
117}
118
119static int
120node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
121{
122 return (n1->minus != n2->minus ||
123 n1->base != n2->base ||
124 n1->seen_point != n2->seen_point ||
125 n1->type != n2->type ||
126 strcmp(n1->val, n2->val));
127}
128
129static int
130rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
131{
132 return (n1->options != n2->options ||
133 rb_parser_string_hash_cmp(n1->string, n2->string));
134}
135
136static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
137static st_index_t rb_char_p_hash(const char *c);
138
139static int
140literal_cmp(st_data_t val, st_data_t lit)
141{
142 if (val == lit) return 0;
143
144 NODE *node_val = RNODE(val);
145 NODE *node_lit = RNODE(lit);
146 enum node_type type_val = nd_type(node_val);
147 enum node_type type_lit = nd_type(node_lit);
148
149 if (type_val != type_lit) {
150 return -1;
151 }
152
153 switch (type_lit) {
154 case NODE_INTEGER:
155 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
156 case NODE_FLOAT:
157 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
158 case NODE_RATIONAL:
159 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
160 case NODE_IMAGINARY:
161 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
162 case NODE_STR:
163 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
164 case NODE_SYM:
165 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
166 case NODE_REGX:
167 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
168 case NODE_LINE:
169 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
170 case NODE_FILE:
171 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
172 case NODE_ENCODING:
173 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
174 default:
175#ifdef UNIVERSAL_PARSER
176 abort();
177#else
178 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
179#endif
180 }
181}
182
183static st_index_t
184literal_hash(st_data_t a)
185{
186 NODE *node = (NODE *)a;
187 enum node_type type = nd_type(node);
188
189 switch (type) {
190 case NODE_INTEGER:
191 return rb_char_p_hash(RNODE_INTEGER(node)->val);
192 case NODE_FLOAT:
193 return rb_char_p_hash(RNODE_FLOAT(node)->val);
194 case NODE_RATIONAL:
195 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
196 case NODE_IMAGINARY:
197 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
198 case NODE_STR:
199 return rb_parser_str_hash(RNODE_STR(node)->string);
200 case NODE_SYM:
201 return rb_parser_str_hash(RNODE_SYM(node)->string);
202 case NODE_REGX:
203 return rb_parser_str_hash(RNODE_REGX(node)->string);
204 case NODE_LINE:
205 return (st_index_t)node->nd_loc.beg_pos.lineno;
206 case NODE_FILE:
207 return rb_parser_str_hash(RNODE_FILE(node)->path);
208 case NODE_ENCODING:
209 return (st_index_t)RNODE_ENCODING(node)->enc;
210 default:
211#ifdef UNIVERSAL_PARSER
212 abort();
213#else
214 rb_bug("unexpected node: %s", ruby_node_name(type));
215#endif
216 }
217}
218
219static inline int
220parse_isascii(int c)
221{
222 return '\0' <= c && c <= '\x7f';
223}
224
225#undef ISASCII
226#define ISASCII parse_isascii
227
228static inline int
229parse_isspace(int c)
230{
231 return c == ' ' || ('\t' <= c && c <= '\r');
232}
233
234#undef ISSPACE
235#define ISSPACE parse_isspace
236
237static inline int
238parse_iscntrl(int c)
239{
240 return ('\0' <= c && c < ' ') || c == '\x7f';
241}
242
243#undef ISCNTRL
244#define ISCNTRL(c) parse_iscntrl(c)
245
246static inline int
247parse_isupper(int c)
248{
249 return 'A' <= c && c <= 'Z';
250}
251
252static inline int
253parse_islower(int c)
254{
255 return 'a' <= c && c <= 'z';
256}
257
258static inline int
259parse_isalpha(int c)
260{
261 return parse_isupper(c) || parse_islower(c);
262}
263
264#undef ISALPHA
265#define ISALPHA(c) parse_isalpha(c)
266
267static inline int
268parse_isdigit(int c)
269{
270 return '0' <= c && c <= '9';
271}
272
273#undef ISDIGIT
274#define ISDIGIT(c) parse_isdigit(c)
275
276static inline int
277parse_isalnum(int c)
278{
279 return ISALPHA(c) || ISDIGIT(c);
280}
281
282#undef ISALNUM
283#define ISALNUM(c) parse_isalnum(c)
284
285static inline int
286parse_isxdigit(int c)
287{
288 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
289}
290
291#undef ISXDIGIT
292#define ISXDIGIT(c) parse_isxdigit(c)
293
294#include "parser_st.h"
295
296#undef STRCASECMP
297#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
298
299#undef STRNCASECMP
300#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
301
302#ifdef RIPPER
303#include "ripper_init.h"
304#endif
305
306enum rescue_context {
307 before_rescue,
308 after_rescue,
309 after_else,
310 after_ensure,
311};
312
313struct lex_context {
314 unsigned int in_defined: 1;
315 unsigned int in_kwarg: 1;
316 unsigned int in_argdef: 1;
317 unsigned int in_def: 1;
318 unsigned int in_class: 1;
319 unsigned int has_trailing_semicolon: 1;
320 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
321 BITFIELD(enum rescue_context, in_rescue, 2);
322 unsigned int cant_return: 1;
323 unsigned int in_alt_pattern: 1;
324 unsigned int capture_in_pattern: 1;
325};
326
327typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
328
329#if defined(__GNUC__) && !defined(__clang__)
330// Suppress "parameter passing for argument of type 'struct
331// lex_context' changed" notes. `struct lex_context` is file scope,
332// and has no ABI compatibility issue.
333RBIMPL_WARNING_PUSH()
334RBIMPL_WARNING_IGNORED(-Wpsabi)
335RBIMPL_WARNING_POP()
336// Not sure why effective even after popped.
337#endif
338
339#include "parse.h"
340
341#define NO_LEX_CTXT (struct lex_context){0}
342
343#ifndef WARN_PAST_SCOPE
344# define WARN_PAST_SCOPE 0
345#endif
346
347#define TAB_WIDTH 8
348
349#define yydebug (p->debug) /* disable the global variable definition */
350
351#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
352#define YY_LOCATION_PRINT(File, loc, p) \
353 rb_parser_printf(p, "%d.%d-%d.%d", \
354 (loc).beg_pos.lineno, (loc).beg_pos.column,\
355 (loc).end_pos.lineno, (loc).end_pos.column)
356#define YYLLOC_DEFAULT(Current, Rhs, N) \
357 do \
358 if (N) \
359 { \
360 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
361 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
362 } \
363 else \
364 { \
365 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
366 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
367 } \
368 while (0)
369#define YY_(Msgid) \
370 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
371 "nesting too deep" : (Msgid))
372
373#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
374 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
375#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
376 rb_parser_set_location_of_delayed_token(p, &(Current))
377#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
378 rb_parser_set_location_of_heredoc_end(p, &(Current))
379#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
380 rb_parser_set_location_of_dummy_end(p, &(Current))
381#define RUBY_SET_YYLLOC_OF_NONE(Current) \
382 rb_parser_set_location_of_none(p, &(Current))
383#define RUBY_SET_YYLLOC(Current) \
384 rb_parser_set_location(p, &(Current))
385#define RUBY_INIT_YYLLOC() \
386 { \
387 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
388 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
389 }
390
391#define IS_lex_state_for(x, ls) ((x) & (ls))
392#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
393#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
394#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
395
396# define SET_LEX_STATE(ls) \
397 parser_set_lex_state(p, ls, __LINE__)
398static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
399
400typedef VALUE stack_type;
401
402static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
403
404# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
405# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
406# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
407# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
408# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
409
410/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
411 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
412#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
413#define COND_POP() BITSTACK_POP(cond_stack)
414#define COND_P() BITSTACK_SET_P(cond_stack)
415#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
416
417/* A flag to identify keyword_do_block; "do" keyword after command_call.
418 Example: `foo 1, 2 do`. */
419#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
420#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
421#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
422#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
423
424struct vtable {
425 ID *tbl;
426 int pos;
427 int capa;
428 struct vtable *prev;
429};
430
431struct local_vars {
432 struct vtable *args;
433 struct vtable *vars;
434 struct vtable *used;
435# if WARN_PAST_SCOPE
436 struct vtable *past;
437# endif
438 struct local_vars *prev;
439 struct {
440 NODE *outer, *inner, *current;
441 } numparam;
442 NODE *it;
443};
444
445typedef struct rb_locations_lambda_body_t {
446 NODE *node;
447 YYLTYPE opening_loc;
448 YYLTYPE closing_loc;
449} rb_locations_lambda_body_t;
450
451enum {
452 ORDINAL_PARAM = -1,
453 NO_PARAM = 0,
454 NUMPARAM_MAX = 9,
455};
456
457#define DVARS_INHERIT ((void*)1)
458#define DVARS_TOPSCOPE NULL
459#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
460
461typedef struct token_info {
462 const char *token;
463 rb_code_position_t beg;
464 int indent;
465 int nonspc;
466 struct token_info *next;
467} token_info;
468
469typedef struct end_expect_token_locations {
470 const rb_code_position_t *pos;
471 struct end_expect_token_locations *prev;
472} end_expect_token_locations_t;
473
474typedef struct parser_string_buffer_elem {
475 struct parser_string_buffer_elem *next;
476 long len; /* Total length of allocated buf */
477 long used; /* Current usage of buf */
478 rb_parser_string_t *buf[FLEX_ARY_LEN];
479} parser_string_buffer_elem_t;
480
481typedef struct parser_string_buffer {
482 parser_string_buffer_elem_t *head;
483 parser_string_buffer_elem_t *last;
484} parser_string_buffer_t;
485
486#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
487
488/*
489 Structure of Lexer Buffer:
490
491 lex.pbeg lex.ptok lex.pcur lex.pend
492 | | | |
493 |------------+------------+------------|
494 |<---------->|
495 token
496*/
497struct parser_params {
498 YYSTYPE *lval;
499 YYLTYPE *yylloc;
500
501 struct {
502 rb_strterm_t *strterm;
503 rb_parser_lex_gets_func *gets;
504 rb_parser_input_data input;
505 parser_string_buffer_t string_buffer;
506 rb_parser_string_t *lastline;
507 rb_parser_string_t *nextline;
508 const char *pbeg;
509 const char *pcur;
510 const char *pend;
511 const char *ptok;
512 enum lex_state_e state;
513 /* track the nest level of any parens "()[]{}" */
514 int paren_nest;
515 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
516 int lpar_beg;
517 /* track the nest level of only braces "{}" */
518 int brace_nest;
519 } lex;
520 stack_type cond_stack;
521 stack_type cmdarg_stack;
522 int tokidx;
523 int toksiz;
524 int heredoc_end;
525 int heredoc_indent;
526 int heredoc_line_indent;
527 char *tokenbuf;
528 struct local_vars *lvtbl;
529 st_table *pvtbl;
530 st_table *pktbl;
531 int line_count;
532 int ruby_sourceline; /* current line no. */
533 const char *ruby_sourcefile; /* current source file */
534 VALUE ruby_sourcefile_string;
535 rb_encoding *enc;
536 token_info *token_info;
537 st_table *case_labels;
538 rb_node_exits_t *exits;
539
540 VALUE debug_buffer;
541 VALUE debug_output;
542
543 struct {
544 rb_parser_string_t *token;
545 int beg_line;
546 int beg_col;
547 int end_line;
548 int end_col;
549 } delayed;
550
551 rb_ast_t *ast;
552 int node_id;
553
554 st_table *warn_duplicate_keys_table;
555
556 int max_numparam;
557 ID it_id;
558
559 struct lex_context ctxt;
560
561 NODE *eval_tree_begin;
562 NODE *eval_tree;
563 const struct rb_iseq_struct *parent_iseq;
564
565#ifdef UNIVERSAL_PARSER
566 const rb_parser_config_t *config;
567#endif
568 /* compile_option */
569 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
570
571 unsigned int command_start:1;
572 unsigned int eofp: 1;
573 unsigned int ruby__end__seen: 1;
574 unsigned int debug: 1;
575 unsigned int has_shebang: 1;
576 unsigned int token_seen: 1;
577 unsigned int token_info_enabled: 1;
578# if WARN_PAST_SCOPE
579 unsigned int past_scope_enabled: 1;
580# endif
581 unsigned int error_p: 1;
582 unsigned int cr_seen: 1;
583
584 /* Streaming hash state of the source bytes read so far. */
585 rb_source_hash_state_t source_hash;
586
587#ifndef RIPPER
588 /* Ruby core only */
589
590 unsigned int do_print: 1;
591 unsigned int do_loop: 1;
592 unsigned int do_chomp: 1;
593 unsigned int do_split: 1;
594 unsigned int error_tolerant: 1;
595 unsigned int keep_tokens: 1;
596
597 VALUE error_buffer;
598 rb_parser_ary_t *debug_lines;
599 /*
600 * Store specific keyword locations to generate dummy end token.
601 * Refer to the tail of list element.
602 */
603 end_expect_token_locations_t *end_expect_token_locations;
604 /* id for terms */
605 int token_id;
606 /* Array for term tokens */
607 rb_parser_ary_t *tokens;
608#else
609 /* Ripper only */
610
611 VALUE value;
612 VALUE result;
613 VALUE parsing_thread;
614 VALUE s_value; /* Token VALUE */
615 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
616 VALUE s_value_stack;
617#endif
618};
619
620#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
621#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
622#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
623static int
624numparam_id_p(struct parser_params *p, ID id)
625{
626 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
627 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
628 return idx > 0 && idx <= NUMPARAM_MAX;
629}
630static void numparam_name(struct parser_params *p, ID id);
631
632#ifdef RIPPER
633static void
634after_shift(struct parser_params *p)
635{
636 if (p->debug) {
637 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
638 }
639 rb_ary_push(p->s_value_stack, p->s_value);
640 p->s_value = Qnil;
641}
642
643static void
644before_reduce(int len, struct parser_params *p)
645{
646 // Initialize $$ with $1.
647 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
648}
649
650static void
651after_reduce(int len, struct parser_params *p)
652{
653 for (int i = 0; i < len; i++) {
654 VALUE tos = rb_ary_pop(p->s_value_stack);
655 if (p->debug) {
656 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
657 }
658 }
659 if (p->debug) {
660 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
661 }
662 rb_ary_push(p->s_value_stack, p->s_lvalue);
663 p->s_lvalue = Qnil;
664}
665
666static void
667after_shift_error_token(struct parser_params *p)
668{
669 if (p->debug) {
670 rb_parser_printf(p, "after-shift-error-token:\n");
671 }
672 rb_ary_push(p->s_value_stack, Qnil);
673}
674
675static void
676after_pop_stack(int len, struct parser_params *p)
677{
678 for (int i = 0; i < len; i++) {
679 VALUE tos = rb_ary_pop(p->s_value_stack);
680 if (p->debug) {
681 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
682 }
683 }
684}
685#else
686static void
687after_shift(struct parser_params *p)
688{
689}
690
691static void
692before_reduce(int len, struct parser_params *p)
693{
694}
695
696static void
697after_reduce(int len, struct parser_params *p)
698{
699}
700
701static void
702after_shift_error_token(struct parser_params *p)
703{
704}
705
706static void
707after_pop_stack(int len, struct parser_params *p)
708{
709}
710#endif
711
712#define intern_cstr(n,l,en) rb_intern3(n,l,en)
713
714#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
715
716#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
717#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
718#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
719#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
720#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
721#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
722
723#ifndef RIPPER
724static inline int
725char_at_end(struct parser_params *p, VALUE str, int when_empty)
726{
727 long len = RSTRING_LEN(str);
728 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
729}
730#endif
731
732static void
733pop_pvtbl(struct parser_params *p, st_table *tbl)
734{
735 st_free_table(p->pvtbl);
736 p->pvtbl = tbl;
737}
738
739static void
740pop_pktbl(struct parser_params *p, st_table *tbl)
741{
742 if (p->pktbl) st_free_table(p->pktbl);
743 p->pktbl = tbl;
744}
745
746#define STRING_BUF_DEFAULT_LEN 16
747
748static void
749string_buffer_init(struct parser_params *p)
750{
751 parser_string_buffer_t *buf = &p->lex.string_buffer;
752 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
753
754 buf->head = buf->last = xmalloc(size);
755 buf->head->len = STRING_BUF_DEFAULT_LEN;
756 buf->head->used = 0;
757 buf->head->next = NULL;
758}
759
760static void
761string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
762{
763 parser_string_buffer_t *buf = &p->lex.string_buffer;
764
765 if (buf->head->used >= buf->head->len) {
766 parser_string_buffer_elem_t *elem;
767 long n = buf->head->len * 2;
768 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
769
770 elem = xmalloc(size);
771 elem->len = n;
772 elem->used = 0;
773 elem->next = NULL;
774 buf->last->next = elem;
775 buf->last = elem;
776 }
777 buf->last->buf[buf->last->used++] = str;
778}
779
780static void
781string_buffer_free(struct parser_params *p)
782{
783 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
784
785 while (elem) {
786 parser_string_buffer_elem_t *next_elem = elem->next;
787
788 for (long i = 0; i < elem->used; i++) {
789 rb_parser_string_free(p, elem->buf[i]);
790 }
791
792 xfree(elem);
793 elem = next_elem;
794 }
795}
796
797#ifndef RIPPER
798static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
799
800static void
801debug_end_expect_token_locations(struct parser_params *p, const char *name)
802{
803 if(p->debug) {
804 VALUE mesg = rb_sprintf("%s: [", name);
805 int i = 0;
806 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
807 if (i > 0)
808 rb_str_cat_cstr(mesg, ", ");
809 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
810 i++;
811 }
812 rb_str_cat_cstr(mesg, "]\n");
813 flush_debug_buffer(p, p->debug_output, mesg);
814 }
815}
816
817static void
818push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
819{
820 if(!p->error_tolerant) return;
821
822 end_expect_token_locations_t *locations;
823 locations = ALLOC(end_expect_token_locations_t);
824 locations->pos = pos;
825 locations->prev = p->end_expect_token_locations;
826 p->end_expect_token_locations = locations;
827
828 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
829}
830
831static void
832pop_end_expect_token_locations(struct parser_params *p)
833{
834 if(!p->end_expect_token_locations) return;
835
836 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
837 ruby_xfree_sized(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
838 p->end_expect_token_locations = locations;
839
840 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
841}
842
843static end_expect_token_locations_t *
844peek_end_expect_token_locations(struct parser_params *p)
845{
846 return p->end_expect_token_locations;
847}
848
849static const char *
850parser_token2char(struct parser_params *p, enum yytokentype tok)
851{
852 switch ((int) tok) {
853#define TOKEN2CHAR(tok) case tok: return (#tok);
854#define TOKEN2CHAR2(tok, name) case tok: return (name);
855 TOKEN2CHAR2(' ', "word_sep");
856 TOKEN2CHAR2('!', "!")
857 TOKEN2CHAR2('%', "%");
858 TOKEN2CHAR2('&', "&");
859 TOKEN2CHAR2('*', "*");
860 TOKEN2CHAR2('+', "+");
861 TOKEN2CHAR2('-', "-");
862 TOKEN2CHAR2('/', "/");
863 TOKEN2CHAR2('<', "<");
864 TOKEN2CHAR2('=', "=");
865 TOKEN2CHAR2('>', ">");
866 TOKEN2CHAR2('?', "?");
867 TOKEN2CHAR2('^', "^");
868 TOKEN2CHAR2('|', "|");
869 TOKEN2CHAR2('~', "~");
870 TOKEN2CHAR2(':', ":");
871 TOKEN2CHAR2(',', ",");
872 TOKEN2CHAR2('.', ".");
873 TOKEN2CHAR2(';', ";");
874 TOKEN2CHAR2('`', "`");
875 TOKEN2CHAR2('\n', "nl");
876 TOKEN2CHAR2('{', "\"{\"");
877 TOKEN2CHAR2('}', "\"}\"");
878 TOKEN2CHAR2('[', "\"[\"");
879 TOKEN2CHAR2(']', "\"]\"");
880 TOKEN2CHAR2('(', "\"(\"");
881 TOKEN2CHAR2(')', "\")\"");
882 TOKEN2CHAR2('\\', "backslash");
883 TOKEN2CHAR(keyword_class);
884 TOKEN2CHAR(keyword_module);
885 TOKEN2CHAR(keyword_def);
886 TOKEN2CHAR(keyword_undef);
887 TOKEN2CHAR(keyword_begin);
888 TOKEN2CHAR(keyword_rescue);
889 TOKEN2CHAR(keyword_ensure);
890 TOKEN2CHAR(keyword_end);
891 TOKEN2CHAR(keyword_if);
892 TOKEN2CHAR(keyword_unless);
893 TOKEN2CHAR(keyword_then);
894 TOKEN2CHAR(keyword_elsif);
895 TOKEN2CHAR(keyword_else);
896 TOKEN2CHAR(keyword_case);
897 TOKEN2CHAR(keyword_when);
898 TOKEN2CHAR(keyword_while);
899 TOKEN2CHAR(keyword_until);
900 TOKEN2CHAR(keyword_for);
901 TOKEN2CHAR(keyword_break);
902 TOKEN2CHAR(keyword_next);
903 TOKEN2CHAR(keyword_redo);
904 TOKEN2CHAR(keyword_retry);
905 TOKEN2CHAR(keyword_in);
906 TOKEN2CHAR(keyword_do);
907 TOKEN2CHAR(keyword_do_cond);
908 TOKEN2CHAR(keyword_do_block);
909 TOKEN2CHAR(keyword_do_LAMBDA);
910 TOKEN2CHAR(keyword_return);
911 TOKEN2CHAR(keyword_yield);
912 TOKEN2CHAR(keyword_super);
913 TOKEN2CHAR(keyword_self);
914 TOKEN2CHAR(keyword_nil);
915 TOKEN2CHAR(keyword_true);
916 TOKEN2CHAR(keyword_false);
917 TOKEN2CHAR(keyword_and);
918 TOKEN2CHAR(keyword_or);
919 TOKEN2CHAR(keyword_not);
920 TOKEN2CHAR(modifier_if);
921 TOKEN2CHAR(modifier_unless);
922 TOKEN2CHAR(modifier_while);
923 TOKEN2CHAR(modifier_until);
924 TOKEN2CHAR(modifier_rescue);
925 TOKEN2CHAR(keyword_alias);
926 TOKEN2CHAR(keyword_defined);
927 TOKEN2CHAR(keyword_BEGIN);
928 TOKEN2CHAR(keyword_END);
929 TOKEN2CHAR(keyword__LINE__);
930 TOKEN2CHAR(keyword__FILE__);
931 TOKEN2CHAR(keyword__ENCODING__);
932 TOKEN2CHAR(tIDENTIFIER);
933 TOKEN2CHAR(tFID);
934 TOKEN2CHAR(tGVAR);
935 TOKEN2CHAR(tIVAR);
936 TOKEN2CHAR(tCONSTANT);
937 TOKEN2CHAR(tCVAR);
938 TOKEN2CHAR(tLABEL);
939 TOKEN2CHAR(tINTEGER);
940 TOKEN2CHAR(tFLOAT);
941 TOKEN2CHAR(tRATIONAL);
942 TOKEN2CHAR(tIMAGINARY);
943 TOKEN2CHAR(tCHAR);
944 TOKEN2CHAR(tNTH_REF);
945 TOKEN2CHAR(tBACK_REF);
946 TOKEN2CHAR(tSTRING_CONTENT);
947 TOKEN2CHAR(tREGEXP_END);
948 TOKEN2CHAR(tDUMNY_END);
949 TOKEN2CHAR(tSP);
950 TOKEN2CHAR(tUPLUS);
951 TOKEN2CHAR(tUMINUS);
952 TOKEN2CHAR(tPOW);
953 TOKEN2CHAR(tCMP);
954 TOKEN2CHAR(tEQ);
955 TOKEN2CHAR(tEQQ);
956 TOKEN2CHAR(tNEQ);
957 TOKEN2CHAR(tGEQ);
958 TOKEN2CHAR(tLEQ);
959 TOKEN2CHAR(tANDOP);
960 TOKEN2CHAR(tOROP);
961 TOKEN2CHAR(tMATCH);
962 TOKEN2CHAR(tNMATCH);
963 TOKEN2CHAR(tDOT2);
964 TOKEN2CHAR(tDOT3);
965 TOKEN2CHAR(tBDOT2);
966 TOKEN2CHAR(tBDOT3);
967 TOKEN2CHAR(tAREF);
968 TOKEN2CHAR(tASET);
969 TOKEN2CHAR(tLSHFT);
970 TOKEN2CHAR(tRSHFT);
971 TOKEN2CHAR(tANDDOT);
972 TOKEN2CHAR(tCOLON2);
973 TOKEN2CHAR(tCOLON3);
974 TOKEN2CHAR(tOP_ASGN);
975 TOKEN2CHAR(tASSOC);
976 TOKEN2CHAR(tLPAREN);
977 TOKEN2CHAR(tLPAREN_ARG);
978 TOKEN2CHAR(tLBRACK);
979 TOKEN2CHAR(tLBRACE);
980 TOKEN2CHAR(tLBRACE_ARG);
981 TOKEN2CHAR(tSTAR);
982 TOKEN2CHAR(tDSTAR);
983 TOKEN2CHAR(tAMPER);
984 TOKEN2CHAR(tLAMBDA);
985 TOKEN2CHAR(tSYMBEG);
986 TOKEN2CHAR(tSTRING_BEG);
987 TOKEN2CHAR(tXSTRING_BEG);
988 TOKEN2CHAR(tREGEXP_BEG);
989 TOKEN2CHAR(tWORDS_BEG);
990 TOKEN2CHAR(tQWORDS_BEG);
991 TOKEN2CHAR(tSYMBOLS_BEG);
992 TOKEN2CHAR(tQSYMBOLS_BEG);
993 TOKEN2CHAR(tSTRING_END);
994 TOKEN2CHAR(tSTRING_DEND);
995 TOKEN2CHAR(tSTRING_DBEG);
996 TOKEN2CHAR(tSTRING_DVAR);
997 TOKEN2CHAR(tLAMBEG);
998 TOKEN2CHAR(tLABEL_END);
999 TOKEN2CHAR(tIGNORED_NL);
1000 TOKEN2CHAR(tCOMMENT);
1001 TOKEN2CHAR(tEMBDOC_BEG);
1002 TOKEN2CHAR(tEMBDOC);
1003 TOKEN2CHAR(tEMBDOC_END);
1004 TOKEN2CHAR(tHEREDOC_BEG);
1005 TOKEN2CHAR(tHEREDOC_END);
1006 TOKEN2CHAR(k__END__);
1007 TOKEN2CHAR(tLOWEST);
1008 TOKEN2CHAR(tUMINUS_NUM);
1009 TOKEN2CHAR(tLAST_TOKEN);
1010#undef TOKEN2CHAR
1011#undef TOKEN2CHAR2
1012 }
1013
1014 rb_bug("parser_token2id: unknown token %d", tok);
1015
1016 UNREACHABLE_RETURN(0);
1017}
1018#else
1019static void
1020push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1021{
1022}
1023
1024static void
1025pop_end_expect_token_locations(struct parser_params *p)
1026{
1027}
1028#endif
1029
1030RBIMPL_ATTR_NONNULL((1, 2, 3))
1031static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1032RBIMPL_ATTR_NONNULL((1, 2))
1033static int parser_yyerror0(struct parser_params*, const char*);
1034#define yyerror0(msg) parser_yyerror0(p, (msg))
1035#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1036#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1037#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1038#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1039#define lex_eol_p(p) lex_eol_n_p(p, 0)
1040#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1041#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1042#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1043
1044static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1045static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1046static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1047static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1048static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1049
1050#ifdef RIPPER
1051#define compile_for_eval (0)
1052#else
1053#define compile_for_eval (p->parent_iseq != 0)
1054#endif
1055
1056#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1057
1058#define CALL_Q_P(q) ((q) == tANDDOT)
1059#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1060
1061#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1062
1063static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1064
1065static inline void
1066rb_discard_node(struct parser_params *p, NODE *n)
1067{
1068 rb_ast_delete_node(p->ast, n);
1069}
1070
1071static 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);
1072static 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);
1073static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1074static 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);
1075static 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);
1076static 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);
1077static 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);
1078static 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);
1079static 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);
1080static 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);
1081static 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);
1082static 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);
1083static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1084static 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);
1085static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1086static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1087static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1088static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1089static 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);
1090static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1091static 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);
1092static 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);
1093static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1094static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1095static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1096static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1097static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1098static 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);
1099static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1100static 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);
1101static 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);
1102static 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);
1103static 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);
1104static 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);
1105static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1106static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1107static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1108static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1109static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1110static 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);
1111static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1112static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1113static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1114static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1115static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1116static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1117static 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);
1118static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1119static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1120static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1121static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1122static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1123static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1124static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1125static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1126static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1127static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1128static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1129static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1130static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1131static 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);
1132static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1133static 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);
1134static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1135static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1136static 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);
1137static 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);
1138static 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);
1139static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1140static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1141static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1142static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1143static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1144static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1145static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1146static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1147static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1148static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1149static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1150static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1151static 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);
1152static 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);
1153static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1154static 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);
1155static 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);
1156static 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);
1157static 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);
1158static 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);
1159static 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);
1160static 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);
1161static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1162static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1163static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1164static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1165static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1166static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1167static 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);
1168static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1169static 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);
1170static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1171static 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);
1172static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1173static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1174static 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);
1175static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1176static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1177static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1178
1179#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1180#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1181#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1182#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)
1183#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)
1184#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1185#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1186#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1187#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)
1188#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)
1189#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)
1190#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)
1191#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1192#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)
1193#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1194#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1195#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1196#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1197#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1198#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1199#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1200#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1201#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1202#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1203#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1204#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1205#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1206#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1207#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1208#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)
1209#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)
1210#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1211#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1212#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1213#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1214#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1215#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1216#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1217#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1218#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)
1219#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1220#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1221#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1222#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1223#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1224#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1225#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)
1226#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1227#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1228#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1229#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1230#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1231#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1232#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1233#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1234#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1235#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1236#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1237#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1238#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1239#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1240#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1241#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1242#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1243#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1244#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1245#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1246#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)
1247#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1248#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1249#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1250#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1251#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1252#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1253#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1254#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1255#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1256#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1257#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1258#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1259#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1260#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1261#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1262#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)
1263#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1264#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)
1265#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1266#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1267#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1268#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1269#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1270#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1271#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1272#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1273#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1274#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1275#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)
1276#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1277#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1278#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1279#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)
1280#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1281#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1282#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1283#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1284#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1285#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1286#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1287
1288enum internal_node_type {
1289 NODE_INTERNAL_ONLY = NODE_LAST,
1290 NODE_DEF_TEMP,
1291 NODE_EXITS,
1292 NODE_INTERNAL_LAST
1293};
1294
1295static const char *
1296parser_node_name(int node)
1297{
1298 switch (node) {
1299 case NODE_DEF_TEMP:
1300 return "NODE_DEF_TEMP";
1301 case NODE_EXITS:
1302 return "NODE_EXITS";
1303 default:
1304 return ruby_node_name(node);
1305 }
1306}
1307
1308/* This node is parse.y internal */
1309struct RNode_DEF_TEMP {
1310 NODE node;
1311
1312 /* for NODE_DEFN/NODE_DEFS */
1313
1314 struct RNode *nd_def;
1315 ID nd_mid;
1316
1317 struct {
1318 int max_numparam;
1319 NODE *numparam_save;
1320 struct lex_context ctxt;
1321 } save;
1322};
1323
1324#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1325
1326static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1327static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1328static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1329static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1330static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1331
1332#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1333#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1334#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1335#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1336
1337/* Make a new internal node, which should not be appeared in the
1338 * result AST and does not have node_id and location. */
1339static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1340#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1341
1342static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1343
1344static int
1345parser_get_node_id(struct parser_params *p)
1346{
1347 int node_id = p->node_id;
1348 p->node_id++;
1349 return node_id;
1350}
1351
1352static void
1353anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1354{
1355 if (id == tANDDOT) {
1356 yyerror1(loc, "&. inside multiple assignment destination");
1357 }
1358}
1359
1360static inline void
1361set_line_body(NODE *body, int line)
1362{
1363 if (!body) return;
1364 switch (nd_type(body)) {
1365 case NODE_RESCUE:
1366 case NODE_ENSURE:
1367 nd_set_line(body, line);
1368 }
1369}
1370
1371static void
1372set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1373{
1374 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1375 nd_set_line(node, beg->end_pos.lineno);
1376}
1377
1378static NODE *
1379last_expr_node(NODE *expr)
1380{
1381 while (expr) {
1382 if (nd_type_p(expr, NODE_BLOCK)) {
1383 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1384 }
1385 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1386 expr = RNODE_BEGIN(expr)->nd_body;
1387 }
1388 else {
1389 break;
1390 }
1391 }
1392 return expr;
1393}
1394
1395#ifndef RIPPER
1396#define yyparse ruby_yyparse
1397#endif
1398
1399static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1400static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1401static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1402static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1403static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1404static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1405
1406static NODE *newline_node(NODE*);
1407static void fixpos(NODE*,NODE*);
1408
1409static int value_expr(struct parser_params*,NODE*);
1410static void void_expr(struct parser_params*,NODE*);
1411static NODE *remove_begin(NODE*);
1412static NODE *void_stmts(struct parser_params*,NODE*);
1413static void reduce_nodes(struct parser_params*,NODE**);
1414static void block_dup_check(struct parser_params*,NODE*,NODE*);
1415
1416static NODE *block_append(struct parser_params*,NODE*,NODE*);
1417static NODE *list_append(struct parser_params*,NODE*,NODE*);
1418static NODE *list_concat(NODE*,NODE*);
1419static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1420static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1421static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1422static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1423static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1424static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1425static NODE *str2dstr(struct parser_params*,NODE*);
1426static NODE *evstr2dstr(struct parser_params*,NODE*);
1427static NODE *splat_array(NODE*);
1428static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1429
1430static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1431static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1432static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1433static 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);
1434static 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;}
1435static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1436
1437static bool args_info_empty_p(struct rb_args_info *args);
1438static 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*);
1439static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1440#define new_empty_args_tail(p, loc) new_args_tail(p, 0, 0, 0, loc)
1441static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1442static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1443static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1444static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1445static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1446static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1447
1448static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1449static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1450
1451static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1452static void no_blockarg(struct parser_params*,NODE*);
1453static NODE *ret_args(struct parser_params*,NODE*);
1454static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1455static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1456
1457static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1458static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1459
1460static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1461static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1462
1463static VALUE rb_backref_error(struct parser_params*,NODE*);
1464static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1465
1466static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1467static 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);
1468static 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);
1469static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1470static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1471
1472static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1473
1474static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1475static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1476
1477static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1478static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1479
1480static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1481
1482static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1483
1484#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1485
1486static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1487
1488static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1489
1490static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1491
1492static rb_ast_id_table_t *local_tbl(struct parser_params*);
1493
1494static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1495static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1496
1497static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1498static NODE *heredoc_dedent(struct parser_params*,NODE*);
1499
1500static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1501
1502static 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);
1503
1504#ifdef RIPPER
1505#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1506#define set_value(val) (p->s_lvalue = val)
1507static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1508static int id_is_var(struct parser_params *p, ID id);
1509#endif
1510
1511RUBY_SYMBOL_EXPORT_BEGIN
1512VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1513int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1514enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1515VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1516void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1517PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1518YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1519YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1520YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1521YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1522YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1523YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1524void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1525RUBY_SYMBOL_EXPORT_END
1526
1527static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1528static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1529static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1530static VALUE formal_argument_error(struct parser_params*, ID);
1531static ID shadowing_lvar(struct parser_params*,ID);
1532static void new_bv(struct parser_params*,ID);
1533
1534static void local_push(struct parser_params*,int);
1535static void local_pop(struct parser_params*);
1536static void local_var(struct parser_params*, ID);
1537static void arg_var(struct parser_params*, ID);
1538static int local_id(struct parser_params *p, ID id);
1539static int local_id_ref(struct parser_params*, ID, ID **);
1540#define internal_id rb_parser_internal_id
1541ID internal_id(struct parser_params*);
1542static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1543static int check_forwarding_args(struct parser_params*);
1544static void add_forwarding_args(struct parser_params *p);
1545static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1546
1547static const struct vtable *dyna_push(struct parser_params *);
1548static void dyna_pop(struct parser_params*, const struct vtable *);
1549static int dyna_in_block(struct parser_params*);
1550#define dyna_var(p, id) local_var(p, id)
1551static int dvar_defined(struct parser_params*, ID);
1552#define dvar_defined_ref rb_parser_dvar_defined_ref
1553int dvar_defined_ref(struct parser_params*, ID, ID**);
1554static int dvar_curr(struct parser_params*,ID);
1555
1556static int lvar_defined(struct parser_params*, ID);
1557
1558static NODE *numparam_push(struct parser_params *p);
1559static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1560
1561#define METHOD_NOT '!'
1562
1563#define idFWD_REST '*'
1564#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1565#define idFWD_BLOCK '&'
1566#define idFWD_ALL idDot3
1567#define arg_FWD_BLOCK idFWD_BLOCK
1568
1569#define RE_ONIG_OPTION_IGNORECASE 1
1570#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1571#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1572#define RE_OPTION_ONCE (1<<16)
1573#define RE_OPTION_ENCODING_SHIFT 8
1574#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1575#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1576#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1577#define RE_OPTION_MASK 0xff
1578#define RE_OPTION_ARG_ENCODING_NONE 32
1579
1580#define CHECK_LITERAL_WHEN (st_table *)1
1581#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1582
1583#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1584RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1585
1586#define TOKEN2ID(tok) ( \
1587 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1588 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1589 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1590 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1591 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1592 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1593 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1594
1595/****** Ripper *******/
1596
1597#ifdef RIPPER
1598
1599#include "eventids1.h"
1600#include "eventids2.h"
1601
1602extern const struct ripper_parser_ids ripper_parser_ids;
1603
1604static VALUE ripper_dispatch0(struct parser_params*,ID);
1605static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1606static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1607static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1608static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1609static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1610static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1611void ripper_error(struct parser_params *p);
1612
1613#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1614#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1615#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1616#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1617#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1618#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1619#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1620
1621#define yyparse ripper_yyparse
1622
1623static VALUE
1624aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1625{
1626 if (!NIL_P(pre_arg)) {
1627 if (!NIL_P(pre_args)) {
1628 rb_ary_unshift(pre_args, pre_arg);
1629 }
1630 else {
1631 pre_args = rb_ary_new_from_args(1, pre_arg);
1632 }
1633 }
1634 return pre_args;
1635}
1636
1637#define ID2VAL(id) STATIC_ID2SYM(id)
1638#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1639#endif /* RIPPER */
1640
1641#define KWD2EID(t, v) keyword_##t
1642
1643static NODE *
1644new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1645{
1646 body = remove_begin(body);
1647 reduce_nodes(p, &body);
1648 NODE *n = NEW_SCOPE(args, body, parent, loc);
1649 nd_set_line(n, loc->end_pos.lineno);
1650 set_line_body(body, loc->beg_pos.lineno);
1651 return n;
1652}
1653
1654static NODE *
1655rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1656 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1657{
1658 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1659 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1660 loc.beg_pos = arg_loc->beg_pos;
1661 return NEW_RESCUE(arg, rescue, 0, &loc);
1662}
1663
1664static NODE *add_block_exit(struct parser_params *p, NODE *node);
1665static rb_node_exits_t *init_block_exit(struct parser_params *p);
1666static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1667static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1668static void clear_block_exit(struct parser_params *p, bool error);
1669
1670static void
1671next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1672{
1673 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1674}
1675
1676static void
1677restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1678{
1679 /* See: def_name action */
1680 struct lex_context ctxt = temp->save.ctxt;
1681 p->ctxt.in_def = ctxt.in_def;
1682 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1683 p->ctxt.in_rescue = ctxt.in_rescue;
1684 p->max_numparam = temp->save.max_numparam;
1685 numparam_pop(p, temp->save.numparam_save);
1686 clear_block_exit(p, true);
1687}
1688
1689static void
1690endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1691{
1692 if (is_attrset_id(mid)) {
1693 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1694 }
1695 token_info_drop(p, "def", loc->beg_pos);
1696}
1697
1698#define debug_token_line(p, name, line) do { \
1699 if (p->debug) { \
1700 const char *const pcur = p->lex.pcur; \
1701 const char *const ptok = p->lex.ptok; \
1702 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1703 line, p->ruby_sourceline, \
1704 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1705 } \
1706 } while (0)
1707
1708#define begin_definition(k, loc_beg, loc_end) \
1709 do { \
1710 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1711 /* singleton class */ \
1712 p->ctxt.cant_return = !p->ctxt.in_def; \
1713 p->ctxt.in_def = 0; \
1714 } \
1715 else if (p->ctxt.in_def) { \
1716 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1717 yyerror1(&loc, k " definition in method body"); \
1718 } \
1719 else { \
1720 p->ctxt.cant_return = 1; \
1721 } \
1722 local_push(p, 0); \
1723 } while (0)
1724
1725#ifndef RIPPER
1726# define ifndef_ripper(x) (x)
1727# define ifdef_ripper(r,x) (x)
1728#else
1729# define ifndef_ripper(x)
1730# define ifdef_ripper(r,x) (r)
1731#endif
1732
1733# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1734# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1735# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1736# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1737# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1738# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1739# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1740# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1741# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1742# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1743# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1744# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1745# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1746# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1747# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1748# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1749# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1750# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1751# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1752# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1753#ifdef RIPPER
1754extern const ID id_warn, id_warning, id_gets, id_assoc;
1755# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1756# define WARN_S_L(s,l) STR_NEW(s,l)
1757# define WARN_S(s) STR_NEW2(s)
1758# define WARN_I(i) INT2NUM(i)
1759# define WARN_ID(i) rb_id2str(i)
1760# define PRIsWARN PRIsVALUE
1761# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1762# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1763# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1764# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1765# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1766# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1767# define compile_error ripper_compile_error
1768#else
1769# define WARN_S_L(s,l) s
1770# define WARN_S(s) s
1771# define WARN_I(i) i
1772# define WARN_ID(i) rb_id2name(i)
1773# define PRIsWARN PRIsVALUE
1774# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1775# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1776# define WARN_CALL rb_compile_warn
1777# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1778# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1779# define WARNING_CALL rb_compile_warning
1780PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1781# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1782#endif
1783
1784#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1785
1786static NODE *
1787add_block_exit(struct parser_params *p, NODE *node)
1788{
1789 if (!node) {
1790 compile_error(p, "unexpected null node");
1791 return 0;
1792 }
1793 switch (nd_type(node)) {
1794 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1795 default:
1796 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1797 return node;
1798 }
1799 if (!p->ctxt.in_defined) {
1800 rb_node_exits_t *exits = p->exits;
1801 if (exits) {
1802 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1803 exits->nd_stts = node;
1804 }
1805 }
1806 return node;
1807}
1808
1809static rb_node_exits_t *
1810init_block_exit(struct parser_params *p)
1811{
1812 rb_node_exits_t *old = p->exits;
1813 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1814 exits->nd_chain = 0;
1815 exits->nd_stts = RNODE(exits);
1816 p->exits = exits;
1817 return old;
1818}
1819
1820static rb_node_exits_t *
1821allow_block_exit(struct parser_params *p)
1822{
1823 rb_node_exits_t *exits = p->exits;
1824 p->exits = 0;
1825 return exits;
1826}
1827
1828static void
1829restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1830{
1831 p->exits = exits;
1832}
1833
1834static void
1835clear_block_exit(struct parser_params *p, bool error)
1836{
1837 rb_node_exits_t *exits = p->exits;
1838 if (!exits) return;
1839 if (error) {
1840 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1841 switch (nd_type(e)) {
1842 case NODE_BREAK:
1843 yyerror1(&e->nd_loc, "Invalid break");
1844 break;
1845 case NODE_NEXT:
1846 yyerror1(&e->nd_loc, "Invalid next");
1847 break;
1848 case NODE_REDO:
1849 yyerror1(&e->nd_loc, "Invalid redo");
1850 break;
1851 default:
1852 yyerror1(&e->nd_loc, "unexpected node");
1853 goto end_checks; /* no nd_chain */
1854 }
1855 }
1856 end_checks:;
1857 }
1858 exits->nd_stts = RNODE(exits);
1859 exits->nd_chain = 0;
1860}
1861
1862#define WARN_EOL(tok) \
1863 (looking_at_eol_p(p) ? \
1864 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1865 (void)0)
1866static int looking_at_eol_p(struct parser_params *p);
1867
1868static NODE *
1869get_nd_value(struct parser_params *p, NODE *node)
1870{
1871 switch (nd_type(node)) {
1872 case NODE_GASGN:
1873 return RNODE_GASGN(node)->nd_value;
1874 case NODE_IASGN:
1875 return RNODE_IASGN(node)->nd_value;
1876 case NODE_LASGN:
1877 return RNODE_LASGN(node)->nd_value;
1878 case NODE_DASGN:
1879 return RNODE_DASGN(node)->nd_value;
1880 case NODE_MASGN:
1881 return RNODE_MASGN(node)->nd_value;
1882 case NODE_CVASGN:
1883 return RNODE_CVASGN(node)->nd_value;
1884 case NODE_CDECL:
1885 return RNODE_CDECL(node)->nd_value;
1886 default:
1887 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1888 return 0;
1889 }
1890}
1891
1892static void
1893set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1894{
1895 switch (nd_type(node)) {
1896 case NODE_CDECL:
1897 RNODE_CDECL(node)->nd_value = rhs;
1898 break;
1899 case NODE_GASGN:
1900 RNODE_GASGN(node)->nd_value = rhs;
1901 break;
1902 case NODE_IASGN:
1903 RNODE_IASGN(node)->nd_value = rhs;
1904 break;
1905 case NODE_LASGN:
1906 RNODE_LASGN(node)->nd_value = rhs;
1907 break;
1908 case NODE_DASGN:
1909 RNODE_DASGN(node)->nd_value = rhs;
1910 break;
1911 case NODE_MASGN:
1912 RNODE_MASGN(node)->nd_value = rhs;
1913 break;
1914 case NODE_CVASGN:
1915 RNODE_CVASGN(node)->nd_value = rhs;
1916 break;
1917 default:
1918 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1919 break;
1920 }
1921}
1922
1923static ID
1924get_nd_vid(struct parser_params *p, NODE *node)
1925{
1926 switch (nd_type(node)) {
1927 case NODE_CDECL:
1928 return RNODE_CDECL(node)->nd_vid;
1929 case NODE_GASGN:
1930 return RNODE_GASGN(node)->nd_vid;
1931 case NODE_IASGN:
1932 return RNODE_IASGN(node)->nd_vid;
1933 case NODE_LASGN:
1934 return RNODE_LASGN(node)->nd_vid;
1935 case NODE_DASGN:
1936 return RNODE_DASGN(node)->nd_vid;
1937 case NODE_CVASGN:
1938 return RNODE_CVASGN(node)->nd_vid;
1939 default:
1940 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1941 return 0;
1942 }
1943}
1944
1945static NODE *
1946get_nd_args(struct parser_params *p, NODE *node)
1947{
1948 switch (nd_type(node)) {
1949 case NODE_CALL:
1950 return RNODE_CALL(node)->nd_args;
1951 case NODE_OPCALL:
1952 return RNODE_OPCALL(node)->nd_args;
1953 case NODE_FCALL:
1954 return RNODE_FCALL(node)->nd_args;
1955 case NODE_QCALL:
1956 return RNODE_QCALL(node)->nd_args;
1957 case NODE_SUPER:
1958 return RNODE_SUPER(node)->nd_args;
1959 case NODE_VCALL:
1960 case NODE_ZSUPER:
1961 case NODE_YIELD:
1962 case NODE_RETURN:
1963 case NODE_BREAK:
1964 case NODE_NEXT:
1965 return 0;
1966 default:
1967 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1968 return 0;
1969 }
1970}
1971
1972static st_index_t
1973djb2(const uint8_t *str, size_t len)
1974{
1975 st_index_t hash = 5381;
1976
1977 for (size_t i = 0; i < len; i++) {
1978 hash = ((hash << 5) + hash) + str[i];
1979 }
1980
1981 return hash;
1982}
1983
1984static st_index_t
1985parser_memhash(const void *ptr, long len)
1986{
1987 return djb2(ptr, len);
1988}
1989
1990#define PARSER_STRING_PTR(str) (str->ptr)
1991#define PARSER_STRING_LEN(str) (str->len)
1992#define PARSER_STRING_END(str) (&str->ptr[str->len])
1993#define STRING_SIZE(str) ((size_t)str->len + 1)
1994#define STRING_TERM_LEN(str) (1)
1995#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1996#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1997 REALLOC_N(str->ptr, char, (size_t)total + termlen); \
1998 str->len = total; \
1999} while (0)
2000#define STRING_SET_LEN(str, n) do { \
2001 (str)->len = (n); \
2002} while (0)
2003#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2004 ((ptrvar) = str->ptr, \
2005 (lenvar) = str->len)
2006
2007static inline int
2008parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2009{
2010 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2011}
2012
2013static rb_parser_string_t *
2014rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2015{
2016 rb_parser_string_t *str;
2017
2018 if (len < 0) {
2019 rb_bug("negative string size (or size too big): %ld", len);
2020 }
2021
2022 str = xcalloc(1, sizeof(rb_parser_string_t));
2023 str->ptr = xcalloc(len + 1, sizeof(char));
2024
2025 if (ptr) {
2026 memcpy(PARSER_STRING_PTR(str), ptr, len);
2027 }
2028 STRING_SET_LEN(str, len);
2029 STRING_TERM_FILL(str);
2030 return str;
2031}
2032
2033static rb_parser_string_t *
2034rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2035{
2036 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2037 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2038 str->enc = enc;
2039 return str;
2040}
2041
2042#ifndef RIPPER
2043rb_parser_string_t *
2044rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2045{
2046 /* Type check */
2047 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2048 RB_GC_GUARD(str);
2049 return ret;
2050}
2051
2052void
2053rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2054{
2055 if (!str) return;
2056 xfree(PARSER_STRING_PTR(str));
2057 xfree(str);
2058}
2059#endif
2060
2061static st_index_t
2062rb_parser_str_hash(rb_parser_string_t *str)
2063{
2064 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2065}
2066
2067static st_index_t
2068rb_char_p_hash(const char *c)
2069{
2070 return parser_memhash((const void *)c, strlen(c));
2071}
2072
2073static size_t
2074rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2075{
2076 return PARSER_STRING_LEN(str);
2077}
2078
2079#ifndef RIPPER
2080static char *
2081rb_parser_string_end(rb_parser_string_t *str)
2082{
2083 return &str->ptr[str->len];
2084}
2085#endif
2086
2087static void
2088rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2089{
2090 str->enc = enc;
2091}
2092
2093static rb_encoding *
2094rb_parser_str_get_encoding(rb_parser_string_t *str)
2095{
2096 return str->enc;
2097}
2098
2099#ifndef RIPPER
2100static bool
2101PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2102{
2103 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2104}
2105#endif
2106
2107static int
2108PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2109{
2110 return str->coderange;
2111}
2112
2113static void
2114PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2115{
2116 str->coderange = coderange;
2117}
2118
2119static void
2120PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2121{
2122 rb_parser_string_set_encoding(str, enc);
2123 PARSER_ENC_CODERANGE_SET(str, cr);
2124}
2125
2126static void
2127PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2128{
2129 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2130}
2131
2132static bool
2133PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2134{
2135 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2136}
2137
2138static bool
2139PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2140{
2141 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2142}
2143
2144static const char *
2145rb_parser_search_nonascii(const char *p, const char *e)
2146{
2147 const char *s = p;
2148
2149 for (; s < e; s++) {
2150 if (*s & 0x80) return s;
2151 }
2152
2153 return NULL;
2154}
2155
2156static int
2157rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2158{
2159 const char *e = ptr + len;
2160
2161 if (enc == rb_ascii8bit_encoding()) {
2162 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2163 ptr = rb_parser_search_nonascii(ptr, e);
2164 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2165 }
2166
2167 /* parser string encoding is always asciicompat */
2168 ptr = rb_parser_search_nonascii(ptr, e);
2169 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2170 for (;;) {
2171 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2172 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2173 ptr += MBCLEN_CHARFOUND_LEN(ret);
2174 if (ptr == e) break;
2175 ptr = rb_parser_search_nonascii(ptr, e);
2176 if (!ptr) break;
2177 }
2178
2179 return RB_PARSER_ENC_CODERANGE_VALID;
2180}
2181
2182static int
2183rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2184{
2185 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2186}
2187
2188static int
2189rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2190{
2191 int cr = PARSER_ENC_CODERANGE(str);
2192
2193 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2194 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2195 PARSER_ENC_CODERANGE_SET(str, cr);
2196 }
2197
2198 return cr;
2199}
2200
2201static rb_parser_string_t *
2202rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2203{
2204 if (rb_parser_str_get_encoding(str) == enc)
2205 return str;
2206 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2207 PARSER_ENC_CODERANGE_CLEAR(str);
2208 }
2209 rb_parser_string_set_encoding(str, enc);
2210 return str;
2211}
2212
2213static bool
2214rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2215{
2216 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2217}
2218
2219static rb_encoding *
2220rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2221{
2222 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2223 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2224
2225 if (enc1 == NULL || enc2 == NULL)
2226 return 0;
2227
2228 if (enc1 == enc2) {
2229 return enc1;
2230 }
2231
2232 if (PARSER_STRING_LEN(str2) == 0)
2233 return enc1;
2234 if (PARSER_STRING_LEN(str1) == 0)
2235 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2236
2237 int cr1, cr2;
2238
2239 cr1 = rb_parser_enc_str_coderange(p, str1);
2240 cr2 = rb_parser_enc_str_coderange(p, str2);
2241
2242 if (cr1 != cr2) {
2243 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2244 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2245 }
2246
2247 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2248 return enc1;
2249 }
2250
2251 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2252 return enc2;
2253 }
2254
2255 return 0;
2256}
2257
2258static void
2259rb_parser_str_modify(rb_parser_string_t *str)
2260{
2261 PARSER_ENC_CODERANGE_CLEAR(str);
2262}
2263
2264static void
2265rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2266{
2267 long capa;
2268 const int termlen = STRING_TERM_LEN(str);
2269
2270 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2271 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2272 }
2273
2274 int cr = PARSER_ENC_CODERANGE(str);
2275 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2276 /* Leave unknown. */
2277 }
2278 else if (len > PARSER_STRING_LEN(str)) {
2279 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2280 }
2281 else if (len < PARSER_STRING_LEN(str)) {
2282 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2283 /* ASCII-only string is keeping after truncated. Valid
2284 * and broken may be invalid or valid, leave unknown. */
2285 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2286 }
2287 }
2288
2289 STRING_SET_LEN(str, len);
2290 STRING_TERM_FILL(str);
2291}
2292
2293static rb_parser_string_t *
2294rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2295{
2296 rb_parser_str_modify(str);
2297 if (len == 0) return 0;
2298
2299 long total, olen, off = -1;
2300 char *sptr;
2301 const int termlen = STRING_TERM_LEN(str);
2302
2303 PARSER_STRING_GETMEM(str, sptr, olen);
2304 if (ptr >= sptr && ptr <= sptr + olen) {
2305 off = ptr - sptr;
2306 }
2307
2308 if (olen > LONG_MAX - len) {
2309 compile_error(p, "string sizes too big");
2310 return 0;
2311 }
2312 total = olen + len;
2313 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2314 sptr = PARSER_STRING_PTR(str);
2315 if (off != -1) {
2316 ptr = sptr + off;
2317 }
2318 memcpy(sptr + olen, ptr, len);
2319 STRING_SET_LEN(str, total);
2320 STRING_TERM_FILL(str);
2321
2322 return str;
2323}
2324
2325#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2326#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2327
2328static rb_parser_string_t *
2329rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2330 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2331{
2332 int str_cr, res_cr;
2333 rb_encoding *str_enc, *res_enc;
2334
2335 str_enc = rb_parser_str_get_encoding(str);
2336 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2337
2338 if (str_enc == ptr_enc) {
2339 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2340 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2341 }
2342 }
2343 else {
2344 /* parser string encoding is always asciicompat */
2345 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2346 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2347 }
2348 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2349 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2350 str_cr = rb_parser_enc_str_coderange(p, str);
2351 }
2352 }
2353 }
2354 if (ptr_cr_ret)
2355 *ptr_cr_ret = ptr_cr;
2356
2357 if (str_enc != ptr_enc &&
2358 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2359 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2360 goto incompatible;
2361 }
2362
2363 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2364 res_enc = str_enc;
2365 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2366 }
2367 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2368 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2369 res_enc = str_enc;
2370 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2371 }
2372 else {
2373 res_enc = ptr_enc;
2374 res_cr = ptr_cr;
2375 }
2376 }
2377 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2378 res_enc = str_enc;
2379 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2380 res_cr = str_cr;
2381 else
2382 res_cr = ptr_cr;
2383 }
2384 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2385 res_enc = str_enc;
2386 res_cr = str_cr;
2387 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2388 }
2389
2390 if (len < 0) {
2391 compile_error(p, "negative string size (or size too big)");
2392 }
2393 parser_str_cat(str, ptr, len);
2394 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2395 return str;
2396
2397 incompatible:
2398 compile_error(p, "incompatible character encodings: %s and %s",
2399 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2400 UNREACHABLE_RETURN(0);
2401
2402}
2403
2404static rb_parser_string_t *
2405rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2406 rb_encoding *ptr_enc)
2407{
2408 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2409}
2410
2411static rb_parser_string_t *
2412rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2413{
2414 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2415
2416 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2417 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2418
2419 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2420
2421 return str;
2422}
2423
2424static rb_parser_string_t *
2425rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2426{
2427 if (len < 0) {
2428 rb_bug("negative string size (or size too big)");
2429 }
2430
2431 long slen = PARSER_STRING_LEN(str);
2432
2433 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2434 PARSER_ENC_CODERANGE_CLEAR(str);
2435 }
2436
2437 {
2438 long capa;
2439 const int termlen = STRING_TERM_LEN(str);
2440
2441 if ((capa = slen) < len) {
2442 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2443 }
2444 else if (len == slen) return str;
2445 STRING_SET_LEN(str, len);
2446 STRING_TERM_FILL(str);
2447 }
2448 return str;
2449}
2450
2451# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2452 ((ptrvar) = str->ptr, \
2453 (lenvar) = str->len, \
2454 (encvar) = str->enc)
2455
2456static int
2457rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2458{
2459 long len1, len2;
2460 const char *ptr1, *ptr2;
2461 rb_encoding *enc1, *enc2;
2462
2463 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2464 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2465
2466 return (len1 != len2 ||
2467 enc1 != enc2 ||
2468 memcmp(ptr1, ptr2, len1) != 0);
2469}
2470
2471static void
2472rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2473{
2474 long i;
2475 if (ary->capa < len) {
2476 ary->capa = len;
2477 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2478 for (i = ary->len; i < len; i++) {
2479 ary->data[i] = 0;
2480 }
2481 }
2482}
2483
2484/*
2485 * Do not call this directly.
2486 * Use rb_parser_ary_new_capa_for_XXX() instead.
2487 */
2488static rb_parser_ary_t *
2489parser_ary_new_capa(rb_parser_t *p, long len)
2490{
2491 if (len < 0) {
2492 rb_bug("negative array size (or size too big): %ld", len);
2493 }
2494 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2495 ary->data_type = 0;
2496 ary->len = 0;
2497 ary->capa = len;
2498 if (0 < len) {
2499 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2500 }
2501 else {
2502 ary->data = NULL;
2503 }
2504 return ary;
2505}
2506
2507#ifndef RIPPER
2508static rb_parser_ary_t *
2509rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2510{
2511 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2512 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2513 return ary;
2514}
2515
2516static rb_parser_ary_t *
2517rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2518{
2519 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2520 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2521 return ary;
2522}
2523#endif
2524
2525static rb_parser_ary_t *
2526rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2527{
2528 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2529 ary->data_type = PARSER_ARY_DATA_NODE;
2530 return ary;
2531}
2532
2533/*
2534 * Do not call this directly.
2535 * Use rb_parser_ary_push_XXX() instead.
2536 */
2537static rb_parser_ary_t *
2538parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2539{
2540 if (ary->len == ary->capa) {
2541 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2542 }
2543 ary->data[ary->len++] = val;
2544 return ary;
2545}
2546
2547#ifndef RIPPER
2548static rb_parser_ary_t *
2549rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2550{
2551 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2552 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2553 }
2554 return parser_ary_push(p, ary, val);
2555}
2556
2557static rb_parser_ary_t *
2558rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2559{
2560 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2561 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2562 }
2563 return parser_ary_push(p, ary, val);
2564}
2565#endif
2566
2567static rb_parser_ary_t *
2568rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2569{
2570 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2571 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2572 }
2573 return parser_ary_push(p, ary, val);
2574}
2575
2576#ifndef RIPPER
2577static void
2578rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2579{
2580 if (!token) return;
2581 rb_parser_string_free(p, token->str);
2582 xfree(token);
2583}
2584
2585static void
2586rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2587{
2588# define foreach_ary(ptr) \
2589 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2590 ptr < end_ary_data; ptr++)
2591 switch (ary->data_type) {
2592 case PARSER_ARY_DATA_AST_TOKEN:
2593 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2594 break;
2595 case PARSER_ARY_DATA_SCRIPT_LINE:
2596 foreach_ary(data) {rb_parser_string_free(p, *data);}
2597 break;
2598 case PARSER_ARY_DATA_NODE:
2599 /* Do nothing because nodes are freed when rb_ast_t is freed */
2600 break;
2601 default:
2602 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2603 break;
2604 }
2605# undef foreach_ary
2606 xfree(ary->data);
2607 xfree(ary);
2608}
2609
2610#endif /* !RIPPER */
2611%}
2612
2613%expect 0
2614%define api.pure
2615%define parse.error verbose
2616%printer {
2617 if ((NODE *)$$ == (NODE *)-1) {
2618 rb_parser_printf(p, "NODE_SPECIAL");
2619 }
2620 else if ($$) {
2621 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2622 }
2623} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2624 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2625%printer {
2626 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2627} <id>
2628%printer {
2629 switch (nd_type(RNODE($$))) {
2630 case NODE_INTEGER:
2631 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2632 break;
2633 case NODE_FLOAT:
2634 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2635 break;
2636 case NODE_RATIONAL:
2637 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2638 break;
2639 case NODE_IMAGINARY:
2640 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2641 break;
2642 default:
2643 break;
2644 }
2645} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2646%printer {
2647 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2648} tNTH_REF
2649%printer {
2650 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2651} tBACK_REF
2652
2653%destructor {
2654 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2655} <labels>
2656
2657%lex-param {struct parser_params *p}
2658%parse-param {struct parser_params *p}
2659%initial-action
2660{
2661 RUBY_SET_YYLLOC_OF_NONE(@$);
2662};
2663%after-shift after_shift
2664%before-reduce before_reduce
2665%after-reduce after_reduce
2666%after-shift-error-token after_shift_error_token
2667%after-pop-stack after_pop_stack
2668
2669%union {
2670 NODE *node;
2671 rb_node_fcall_t *node_fcall;
2672 rb_node_args_t *node_args;
2673 rb_node_args_aux_t *node_args_aux;
2674 rb_node_opt_arg_t *node_opt_arg;
2675 rb_node_kw_arg_t *node_kw_arg;
2676 rb_node_block_pass_t *node_block_pass;
2677 rb_node_masgn_t *node_masgn;
2678 rb_node_def_temp_t *node_def_temp;
2679 rb_node_exits_t *node_exits;
2680 struct rb_locations_lambda_body_t *locations_lambda_body;
2681 ID id;
2682 int num;
2683 st_table *tbl;
2684 st_table *labels;
2685 const struct vtable *vars;
2686 struct rb_strterm_struct *strterm;
2687 struct lex_context ctxt;
2688 enum lex_state_e state;
2689}
2690
2691%token <id>
2692 keyword_class "'class'"
2693 keyword_module "'module'"
2694 keyword_def "'def'"
2695 keyword_undef "'undef'"
2696 keyword_begin "'begin'"
2697 keyword_rescue "'rescue'"
2698 keyword_ensure "'ensure'"
2699 keyword_end "'end'"
2700 keyword_if "'if'"
2701 keyword_unless "'unless'"
2702 keyword_then "'then'"
2703 keyword_elsif "'elsif'"
2704 keyword_else "'else'"
2705 keyword_case "'case'"
2706 keyword_when "'when'"
2707 keyword_while "'while'"
2708 keyword_until "'until'"
2709 keyword_for "'for'"
2710 keyword_break "'break'"
2711 keyword_next "'next'"
2712 keyword_redo "'redo'"
2713 keyword_retry "'retry'"
2714 keyword_in "'in'"
2715 keyword_do "'do'"
2716 keyword_do_cond "'do' for condition"
2717 keyword_do_block "'do' for block"
2718 keyword_do_LAMBDA "'do' for lambda"
2719 keyword_return "'return'"
2720 keyword_yield "'yield'"
2721 keyword_super "'super'"
2722 keyword_self "'self'"
2723 keyword_nil "'nil'"
2724 keyword_true "'true'"
2725 keyword_false "'false'"
2726 keyword_and "'and'"
2727 keyword_or "'or'"
2728 keyword_not "'not'"
2729 modifier_if "'if' modifier"
2730 modifier_unless "'unless' modifier"
2731 modifier_while "'while' modifier"
2732 modifier_until "'until' modifier"
2733 modifier_rescue "'rescue' modifier"
2734 keyword_alias "'alias'"
2735 keyword_defined "'defined?'"
2736 keyword_BEGIN "'BEGIN'"
2737 keyword_END "'END'"
2738 keyword__LINE__ "'__LINE__'"
2739 keyword__FILE__ "'__FILE__'"
2740 keyword__ENCODING__ "'__ENCODING__'"
2741
2742%token <id> tIDENTIFIER "local variable or method"
2743%token <id> tFID "method"
2744%token <id> tGVAR "global variable"
2745%token <id> tIVAR "instance variable"
2746%token <id> tCONSTANT "constant"
2747%token <id> tCVAR "class variable"
2748%token <id> tLABEL "label"
2749%token <node> tINTEGER "integer literal"
2750%token <node> tFLOAT "float literal"
2751%token <node> tRATIONAL "rational literal"
2752%token <node> tIMAGINARY "imaginary literal"
2753%token <node> tCHAR "char literal"
2754%token <node> tNTH_REF "numbered reference"
2755%token <node> tBACK_REF "back reference"
2756%token <node> tSTRING_CONTENT "literal content"
2757%token <num> tREGEXP_END
2758%token <num> tDUMNY_END "dummy end"
2759
2760%type <node> singleton singleton_expr strings string string1 xstring regexp
2761%type <node> string_contents xstring_contents regexp_contents string_content
2762%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2763%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2764%type <node_def_temp> defn_head defs_head k_def
2765%type <node_exits> block_open k_while k_until k_for allow_exits
2766%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2767%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2768%type <node> command command_call command_call_value method_call
2769%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2770%type <node_fcall> fcall
2771%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2772%type <node> args arg_splat call_args opt_call_args
2773%type <node> paren_args opt_paren_args
2774%type <node_args> args_tail block_args_tail
2775%type <node> command_args aref_args
2776%type <node_block_pass> opt_block_arg block_arg
2777%type <node> var_ref var_lhs
2778%type <node> command_rhs arg_rhs
2779%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2780%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args f_empty_arg
2781%type <node_args_aux> f_arg f_arg_item
2782%type <node> f_marg f_rest_marg
2783%type <node_masgn> f_margs
2784%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2785%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2786%type <id> do bv_decls opt_bv_decl bvar
2787%type <node> lambda brace_body do_body
2788%type <locations_lambda_body> lambda_body
2789%type <node_args> f_larglist f_largs largs_tail
2790%type <node> brace_block cmd_brace_block do_block lhs none fitem
2791%type <node> mlhs_head mlhs_item mlhs_node
2792%type <node_masgn> mlhs mlhs_basic mlhs_inner
2793%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2794%type <node> p_expr p_as p_alt p_expr_basic p_find
2795%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2796%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2797%type <node> p_kwargs p_kwarg p_kw
2798%type <id> keyword_variable user_variable sym operation2 operation3
2799%type <id> cname fname op f_rest_arg f_block_arg opt_comma f_norm_arg f_bad_arg
2800%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2801%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2802%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2803%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2804%type <ctxt> p_in_kwarg
2805%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2806%type <num> max_numparam
2807%type <node> numparam
2808%type <id> it_id
2809%token END_OF_INPUT 0 "end-of-input"
2810%token <id> '.'
2811
2812/* escaped chars, should be ignored otherwise */
2813%token <id> '\\' "backslash"
2814%token tSP "escaped space"
2815%token <id> '\t' "escaped horizontal tab"
2816%token <id> '\f' "escaped form feed"
2817%token <id> '\r' "escaped carriage return"
2818%token <id> '\13' "escaped vertical tab"
2819%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2820%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2821%token tPOW RUBY_TOKEN(POW) "**"
2822%token tCMP RUBY_TOKEN(CMP) "<=>"
2823%token tEQ RUBY_TOKEN(EQ) "=="
2824%token tEQQ RUBY_TOKEN(EQQ) "==="
2825%token tNEQ RUBY_TOKEN(NEQ) "!="
2826%token tGEQ RUBY_TOKEN(GEQ) ">="
2827%token tLEQ RUBY_TOKEN(LEQ) "<="
2828%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2829%token tOROP RUBY_TOKEN(OROP) "||"
2830%token tMATCH RUBY_TOKEN(MATCH) "=~"
2831%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2832%token tDOT2 RUBY_TOKEN(DOT2) ".."
2833%token tDOT3 RUBY_TOKEN(DOT3) "..."
2834%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2835%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2836%token tAREF RUBY_TOKEN(AREF) "[]"
2837%token tASET RUBY_TOKEN(ASET) "[]="
2838%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2839%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2840%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2841%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2842%token tCOLON3 ":: at EXPR_BEG"
2843%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2844%token tASSOC "=>"
2845%token tLPAREN "("
2846%token tLPAREN_ARG "( arg"
2847%token tLBRACK "["
2848%token tLBRACE "{"
2849%token tLBRACE_ARG "{ arg"
2850%token tSTAR "*"
2851%token tDSTAR "**arg"
2852%token tAMPER "&"
2853%token <num> tLAMBDA "->"
2854%token tSYMBEG "symbol literal"
2855%token tSTRING_BEG "string literal"
2856%token tXSTRING_BEG "backtick literal"
2857%token tREGEXP_BEG "regexp literal"
2858%token tWORDS_BEG "word list"
2859%token tQWORDS_BEG "verbatim word list"
2860%token tSYMBOLS_BEG "symbol list"
2861%token tQSYMBOLS_BEG "verbatim symbol list"
2862%token tSTRING_END "terminator"
2863%token tSTRING_DEND "'}'"
2864%token <state> tSTRING_DBEG "'#{'"
2865%token tSTRING_DVAR tLAMBEG tLABEL_END
2866
2867%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2868%token tHEREDOC_BEG tHEREDOC_END k__END__
2869
2870/*
2871 * precedence table
2872 */
2873
2874%nonassoc tLOWEST
2875%nonassoc tLBRACE_ARG
2876
2877%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2878%left keyword_or keyword_and
2879%right keyword_not
2880%nonassoc keyword_defined
2881%right '=' tOP_ASGN
2882%left modifier_rescue
2883%right '?' ':'
2884%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2885%left tOROP
2886%left tANDOP
2887%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2888%left '>' tGEQ '<' tLEQ
2889%left '|' '^'
2890%left '&'
2891%left tLSHFT tRSHFT
2892%left '+' '-'
2893%left '*' '/' '%'
2894%right tUMINUS_NUM tUMINUS
2895%right tPOW
2896%right '!' '~' tUPLUS
2897
2898%token tLAST_TOKEN
2899
2900/*
2901 * inlining rules
2902 */
2903%rule %inline ident_or_const
2904 : tIDENTIFIER
2905 | tCONSTANT
2906 ;
2907
2908%rule %inline user_or_keyword_variable
2909 : user_variable
2910 | keyword_variable
2911 ;
2912
2913/*
2914 * parameterizing rules
2915 */
2916%rule asgn(rhs) <node>
2917 : lhs '=' lex_ctxt rhs
2918 {
2919 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2920 /*% ripper: assign!($:1, $:4) %*/
2921 }
2922 ;
2923
2924%rule args_tail_basic(value, trailing) <node_args>
2925 : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing)
2926 {
2927 $$ = new_args_tail(p, $1, $3, $4, &@3);
2928 /*% ripper: [$:1, $:3, $:4] %*/
2929 }
2930 | f_kwarg(value) opt_f_block_arg(trailing)
2931 {
2932 $$ = new_args_tail(p, $1, 0, $2, &@1);
2933 /*% ripper: [$:1, Qnil, $:2] %*/
2934 }
2935 | f_any_kwrest opt_f_block_arg(trailing)
2936 {
2937 $$ = new_args_tail(p, 0, $1, $2, &@1);
2938 /*% ripper: [Qnil, $:1, $:2] %*/
2939 }
2940 | f_block_arg
2941 {
2942 $$ = new_args_tail(p, 0, 0, $1, &@1);
2943 /*% ripper: [Qnil, Qnil, $:1] %*/
2944 }
2945 ;
2946
2947%rule opt_f_block_arg(trailing) <id>
2948 : ',' f_block_arg
2949 {
2950 $$ = $2;
2951 /*% ripper: $:2 %*/
2952 }
2953 | trailing
2954 ;
2955
2956%rule def_endless_method(bodystmt) <node>
2957 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2958 {
2959 endless_method_name(p, $head->nd_mid, &@head);
2960 restore_defun(p, $head);
2961 ($$ = $head->nd_def)->nd_loc = @$;
2962 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2963 RNODE_DEFN($$)->nd_defn = $bodystmt;
2964 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2965 /*% ripper: def!($:head, $:args, $:$) %*/
2966 local_pop(p);
2967 }
2968 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2969 {
2970 endless_method_name(p, $head->nd_mid, &@head);
2971 restore_defun(p, $head);
2972 ($$ = $head->nd_def)->nd_loc = @$;
2973 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2974 RNODE_DEFS($$)->nd_defn = $bodystmt;
2975 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2976 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2977 local_pop(p);
2978 }
2979 ;
2980
2981%rule compstmt(stmts) <node>
2982 : stmts terms?
2983 {
2984 void_stmts(p, $$ = $stmts);
2985 }
2986 ;
2987
2988%rule f_opt(value) <node_opt_arg>
2989 : f_arg_asgn f_eq value
2990 {
2991 p->ctxt.in_argdef = 1;
2992 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2993 /*% ripper: [$:$, $:3] %*/
2994 }
2995 ;
2996
2997%rule f_opt_arg(value) <node_opt_arg>
2998 : f_opt(value)
2999 {
3000 $$ = $f_opt;
3001 /*% ripper: rb_ary_new3(1, $:1) %*/
3002 }
3003 | f_opt_arg(value) ',' f_opt(value)
3004 {
3005 $$ = opt_arg_append($f_opt_arg, $f_opt);
3006 /*% ripper: rb_ary_push($:1, $:3) %*/
3007 }
3008 ;
3009
3010%rule f_kw(value) <node_kw_arg>
3011 : f_label value
3012 {
3013 p->ctxt.in_argdef = 1;
3014 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3015 /*% ripper: [$:$, $:value] %*/
3016 }
3017 | f_label
3018 {
3019 p->ctxt.in_argdef = 1;
3020 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3021 /*% ripper: [$:$, 0] %*/
3022 }
3023 ;
3024
3025%rule f_kwarg(value) <node_kw_arg>
3026 : f_kw(value)
3027 {
3028 $$ = $f_kw;
3029 /*% ripper: rb_ary_new3(1, $:1) %*/
3030 }
3031 | f_kwarg(value) ',' f_kw(value)
3032 {
3033 $$ = kwd_append($f_kwarg, $f_kw);
3034 /*% ripper: rb_ary_push($:1, $:3) %*/
3035 }
3036 ;
3037
3038%rule mlhs_items(item) <node>
3039 : item
3040 {
3041 $$ = NEW_LIST($1, &@$);
3042 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3043 }
3044 | mlhs_items(item) ',' item
3045 {
3046 $$ = list_append(p, $1, $3);
3047 /*% ripper: mlhs_add!($:1, $:3) %*/
3048 }
3049 ;
3050
3051%rule op_asgn(rhs) <node>
3052 : var_lhs tOP_ASGN lex_ctxt rhs
3053 {
3054 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3055 /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
3056 }
3057 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3058 {
3059 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3060 /*% ripper: opassign!(aref_field!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/
3061 }
3062 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3063 {
3064 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3065 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3066 }
3067 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3068 {
3069 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3070 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3071 }
3072 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3073 {
3074 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3075 /*% ripper: opassign!(field!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3076 }
3077 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3078 {
3079 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3080 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3081 /*% ripper: opassign!(const_path_field!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3082 }
3083 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3084 {
3085 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3086 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3087 /*% ripper: opassign!(top_const_field!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3088 }
3089 | backref tOP_ASGN lex_ctxt rhs
3090 {
3091 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3092 $$ = NEW_ERROR(&@$);
3093 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:backref), $:tOP_ASGN, $:rhs)) %*/
3094 }
3095 ;
3096
3097%rule opt_args_tail(tail, trailing) <node_args>
3098 : ',' tail
3099 {
3100 $$ = $tail;
3101 /*% ripper: $:tail %*/
3102 }
3103 | trailing
3104 {
3105 $$ = new_empty_args_tail(p, &@$);
3106 /*% ripper: [Qnil, Qnil, Qnil] %*/
3107 }
3108 ;
3109
3110%rule range_expr(range) <node>
3111 : range tDOT2 range
3112 {
3113 value_expr(p, $1);
3114 value_expr(p, $3);
3115 $$ = NEW_DOT2($1, $3, &@$, &@2);
3116 /*% ripper: dot2!($:1, $:3) %*/
3117 }
3118 | range tDOT3 range
3119 {
3120 value_expr(p, $1);
3121 value_expr(p, $3);
3122 $$ = NEW_DOT3($1, $3, &@$, &@2);
3123 /*% ripper: dot3!($:1, $:3) %*/
3124 }
3125 | range tDOT2
3126 {
3127 value_expr(p, $1);
3128 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3129 /*% ripper: dot2!($:1, Qnil) %*/
3130 }
3131 | range tDOT3
3132 {
3133 value_expr(p, $1);
3134 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3135 /*% ripper: dot3!($:1, Qnil) %*/
3136 }
3137 | tBDOT2 range
3138 {
3139 value_expr(p, $2);
3140 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3141 /*% ripper: dot2!(Qnil, $:2) %*/
3142 }
3143 | tBDOT3 range
3144 {
3145 value_expr(p, $2);
3146 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3147 /*% ripper: dot3!(Qnil, $:2) %*/
3148 }
3149 ;
3150
3151%rule value_expr(value) <node>
3152 : value
3153 {
3154 value_expr(p, $1);
3155 $$ = $1;
3156 }
3157 ;
3158
3159%rule words(begin, word_list) <node>
3160 : begin ' '+ word_list tSTRING_END
3161 {
3162 $$ = make_list($word_list, &@$);
3163 /*% ripper: array!($:word_list) %*/
3164 }
3165 ;
3166
3167%%
3168program : {
3169 SET_LEX_STATE(EXPR_BEG);
3170 local_push(p, ifndef_ripper(1)+0);
3171 /* jumps are possible in the top-level loop. */
3172 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3173 }
3174 compstmt(top_stmts)
3175 {
3176 if ($2 && !compile_for_eval) {
3177 NODE *node = $2;
3178 /* last expression should not be void */
3179 if (nd_type_p(node, NODE_BLOCK)) {
3180 while (RNODE_BLOCK(node)->nd_next) {
3181 node = RNODE_BLOCK(node)->nd_next;
3182 }
3183 node = RNODE_BLOCK(node)->nd_head;
3184 }
3185 node = remove_begin(node);
3186 void_expr(p, node);
3187 }
3188 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3189 /*% ripper[final]: program!($:2) %*/
3190 local_pop(p);
3191 }
3192 ;
3193
3194top_stmts : none
3195 {
3196 $$ = NEW_BEGIN(0, &@$);
3197 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3198 }
3199 | top_stmt
3200 {
3201 $$ = newline_node($1);
3202 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3203 }
3204 | top_stmts terms top_stmt
3205 {
3206 $$ = block_append(p, $1, newline_node($3));
3207 /*% ripper: stmts_add!($:1, $:3) %*/
3208 }
3209 ;
3210
3211top_stmt : stmt
3212 {
3213 clear_block_exit(p, true);
3214 $$ = $1;
3215 }
3216 | keyword_BEGIN begin_block
3217 {
3218 $$ = $2;
3219 /*% ripper: $:2 %*/
3220 }
3221 ;
3222
3223block_open : '{' {$$ = init_block_exit(p);};
3224
3225begin_block : block_open compstmt(top_stmts) '}'
3226 {
3227 restore_block_exit(p, $block_open);
3228 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3229 NEW_BEGIN($compstmt, &@$));
3230 $$ = NEW_BEGIN(0, &@$);
3231 /*% ripper: BEGIN!($:compstmt) %*/
3232 }
3233 ;
3234
3235bodystmt : compstmt(stmts)[body]
3236 lex_ctxt[ctxt]
3237 opt_rescue
3238 k_else
3239 {
3240 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3241 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3242 }
3243 compstmt(stmts)[elsebody]
3244 {
3245 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3246 }
3247 opt_ensure
3248 {
3249 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3250 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3251 }
3252 | compstmt(stmts)[body]
3253 lex_ctxt[ctxt]
3254 opt_rescue
3255 {
3256 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3257 }
3258 opt_ensure
3259 {
3260 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3261 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3262 }
3263 ;
3264
3265stmts : none
3266 {
3267 $$ = NEW_BEGIN(0, &@$);
3268 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3269 }
3270 | stmt_or_begin
3271 {
3272 $$ = newline_node($1);
3273 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3274 }
3275 | stmts terms stmt_or_begin
3276 {
3277 $$ = block_append(p, $1, newline_node($3));
3278 /*% ripper: stmts_add!($:1, $:3) %*/
3279 }
3280 ;
3281
3282stmt_or_begin : stmt
3283 | keyword_BEGIN
3284 {
3285 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3286 }
3287 begin_block
3288 {
3289 $$ = $3;
3290 }
3291 ;
3292
3293allow_exits : {$$ = allow_block_exit(p);};
3294
3295k_END : keyword_END lex_ctxt
3296 {
3297 if (p->ctxt.in_def) {
3298 rb_warn0("END in method; use at_exit");
3299 }
3300 $$ = $2;
3301 p->ctxt.in_rescue = before_rescue;
3302 /*% ripper: $:2 %*/
3303 };
3304
3305stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3306 {
3307 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3308 /*% ripper: alias!($:new, $:old) %*/
3309 }
3310 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3311 {
3312 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3313 /*% ripper: var_alias!($:new, $:old) %*/
3314 }
3315 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
3316 {
3317 char buf[2];
3318 buf[0] = '$';
3319 buf[1] = (char)RNODE_BACK_REF($old)->nd_nth;
3320 $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw);
3321 /*% ripper: var_alias!($:new, $:old) %*/
3322 }
3323 | keyword_alias tGVAR tNTH_REF[nth]
3324 {
3325 static const char mesg[] = "can't make alias for the number variables";
3326 /*%%%*/
3327 yyerror1(&@nth, mesg);
3328 /*% %*/
3329 $$ = NEW_ERROR(&@$);
3330 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3331 }
3332 | keyword_undef[kw] undef_list[list]
3333 {
3334 nd_set_first_loc($list, @kw.beg_pos);
3335 RNODE_UNDEF($list)->keyword_loc = @kw;
3336 $$ = $list;
3337 /*% ripper: undef!($:list) %*/
3338 }
3339 | stmt[body] modifier_if[mod] expr_value[cond]
3340 {
3341 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3342 fixpos($$, $cond);
3343 /*% ripper: if_mod!($:cond, $:body) %*/
3344 }
3345 | stmt[body] modifier_unless[mod] expr_value[cond]
3346 {
3347 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3348 fixpos($$, $cond);
3349 /*% ripper: unless_mod!($:cond, $:body) %*/
3350 }
3351 | stmt[body] modifier_while[mod] expr_value[cond_expr]
3352 {
3353 clear_block_exit(p, false);
3354 if ($body && nd_type_p($body, NODE_BEGIN)) {
3355 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3356 }
3357 else {
3358 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3359 }
3360 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3361 }
3362 | stmt[body] modifier_until[mod] expr_value[cond_expr]
3363 {
3364 clear_block_exit(p, 0);
3365 if ($body && nd_type_p($body, NODE_BEGIN)) {
3366 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3367 }
3368 else {
3369 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3370 }
3371 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3372 }
3373 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3374 {
3375 p->ctxt.in_rescue = $ctxt.in_rescue;
3376 NODE *resq;
3377 YYLTYPE loc = code_loc_gen(&@mod, &@resbody);
3378 resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3379 $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$);
3380 /*% ripper: rescue_mod!($:body, $:resbody) %*/
3381 }
3382 | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace]
3383 {
3384 clear_block_exit(p, true);
3385 restore_block_exit(p, $block_open);
3386 p->ctxt = $k_end;
3387 {
3388 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$);
3389 $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace);
3390 RNODE_SCOPE(scope)->nd_parent = $$;
3391 }
3392 /*% ripper: END!($:body) %*/
3393 }
3394 | command_asgn
3395 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3396 {
3397 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3398 /*% ripper: massign!($:lhs, $:rhs) %*/
3399 }
3400 | asgn(mrhs)
3401 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3402 after_rescue[after_rescue] stmt[resbody]
3403 {
3404 p->ctxt.in_rescue = $after_rescue.in_rescue;
3405 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3406 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3407 loc.beg_pos = @mrhs_arg.beg_pos;
3408 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3409 $$ = node_assign(p, (NODE *)$lhs, $mrhs_arg, $lex_ctxt, &@$);
3410 /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/
3411 }
3412 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3413 {
3414 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3415 /*% ripper: massign!($:lhs, $:rhs) %*/
3416 }
3417 | expr
3418 | error
3419 {
3420 (void)yynerrs;
3421 $$ = NEW_ERROR(&@$);
3422 }
3423 ;
3424
3425command_asgn : asgn(command_rhs)
3426 | op_asgn(command_rhs)
3427 | def_endless_method(endless_command)
3428 ;
3429
3430endless_command : command
3431 | endless_command modifier_rescue after_rescue arg
3432 {
3433 p->ctxt.in_rescue = $3.in_rescue;
3434 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3435 /*% ripper: rescue_mod!($:1, $:4) %*/
3436 }
3437 | keyword_not '\n'? endless_command
3438 {
3439 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3440 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3441 }
3442 ;
3443
3444command_rhs : command_call_value %prec tOP_ASGN
3445 | command_call_value modifier_rescue after_rescue stmt
3446 {
3447 p->ctxt.in_rescue = $3.in_rescue;
3448 YYLTYPE loc = code_loc_gen(&@2, &@4);
3449 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3450 /*% ripper: rescue_mod!($:1, $:4) %*/
3451 }
3452 | command_asgn
3453 ;
3454
3455expr : command_call
3456 | expr[left] keyword_and[op] expr[right]
3457 {
3458 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3459 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3460 }
3461 | expr[left] keyword_or[op] expr[right]
3462 {
3463 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3464 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3465 }
3466 | keyword_not[not] '\n'? expr[arg]
3467 {
3468 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3469 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3470 }
3471 | '!'[not] command_call[arg]
3472 {
3473 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3474 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3475 }
3476 | arg tASSOC[assoc]
3477 {
3478 value_expr(p, $arg);
3479 }
3480 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3481 p_top_expr_body[body]
3482 {
3483 pop_pktbl(p, $p_pktbl);
3484 pop_pvtbl(p, $p_pvtbl);
3485 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3486 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3487 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3488 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@assoc), &@$, &NULL_LOC, &NULL_LOC);
3489 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3490 }
3491 | arg keyword_in
3492 {
3493 value_expr(p, $arg);
3494 }
3495 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3496 p_top_expr_body[body]
3497 {
3498 pop_pktbl(p, $p_pktbl);
3499 pop_pvtbl(p, $p_pvtbl);
3500 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3501 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3502 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3503 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3504 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3505 }
3506 | arg %prec tLBRACE_ARG
3507 ;
3508
3509def_name : fname
3510 {
3511 numparam_name(p, $fname);
3512 local_push(p, 0);
3513 p->ctxt.in_def = 1;
3514 p->ctxt.in_rescue = before_rescue;
3515 p->ctxt.cant_return = 0;
3516 $$ = $fname;
3517 }
3518 ;
3519
3520defn_head : k_def def_name
3521 {
3522 $$ = def_head_save(p, $k_def);
3523 $$->nd_mid = $def_name;
3524 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3525 /*% ripper: $:def_name %*/
3526 }
3527 ;
3528
3529defs_head : k_def singleton dot_or_colon
3530 {
3531 SET_LEX_STATE(EXPR_FNAME);
3532 }
3533 def_name
3534 {
3535 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3536 $$ = def_head_save(p, $k_def);
3537 $$->nd_mid = $def_name;
3538 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3539 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3540 }
3541 ;
3542
3543expr_value : value_expr(expr)
3544 | error
3545 {
3546 $$ = NEW_ERROR(&@$);
3547 }
3548 ;
3549
3550expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3551 {
3552 $$ = $2;
3553 /*% ripper: $:2 %*/
3554 }
3555 ;
3556
3557command_call : command
3558 | block_command
3559 ;
3560
3561command_call_value : value_expr(command_call)
3562 ;
3563
3564block_command : block_call
3565 | block_call call_op2 operation2 command_args
3566 {
3567 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3568 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3569 }
3570 ;
3571
3572cmd_brace_block : tLBRACE_ARG brace_body '}'
3573 {
3574 $$ = $2;
3575 set_embraced_location($$, &@1, &@3);
3576 /*% ripper: $:2 %*/
3577 }
3578 ;
3579
3580fcall : operation
3581 {
3582 $$ = NEW_FCALL($1, 0, &@$);
3583 /*% ripper: $:1 %*/
3584 }
3585 ;
3586
3587command : fcall command_args %prec tLOWEST
3588 {
3589 $1->nd_args = $2;
3590 nd_set_last_loc($1, @2.end_pos);
3591 $$ = (NODE *)$1;
3592 /*% ripper: command!($:1, $:2) %*/
3593 }
3594 | fcall command_args cmd_brace_block
3595 {
3596 block_dup_check(p, $2, $3);
3597 $1->nd_args = $2;
3598 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3599 fixpos($$, RNODE($1));
3600 nd_set_last_loc($1, @2.end_pos);
3601 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3602 }
3603 | primary_value call_op operation2 command_args %prec tLOWEST
3604 {
3605 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3606 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3607 }
3608 | primary_value call_op operation2 command_args cmd_brace_block
3609 {
3610 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3611 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3612 }
3613 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3614 {
3615 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3616 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3617 }
3618 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3619 {
3620 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3621 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3622 }
3623 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3624 {
3625 set_embraced_location($5, &@4, &@6);
3626 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3627 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3628 }
3629 | keyword_super command_args
3630 {
3631 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3632 fixpos($$, $2);
3633 /*% ripper: super!($:2) %*/
3634 }
3635 | k_yield command_args
3636 {
3637 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3638 fixpos($$, $2);
3639 /*% ripper: yield!($:2) %*/
3640 }
3641 | k_return call_args
3642 {
3643 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3644 /*% ripper: return!($:2) %*/
3645 }
3646 | keyword_break call_args
3647 {
3648 NODE *args = 0;
3649 args = ret_args(p, $2);
3650 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3651 /*% ripper: break!($:2) %*/
3652 }
3653 | keyword_next call_args
3654 {
3655 NODE *args = 0;
3656 args = ret_args(p, $2);
3657 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3658 /*% ripper: next!($:2) %*/
3659 }
3660 ;
3661
3662mlhs : mlhs_basic
3663 | tLPAREN mlhs_inner rparen
3664 {
3665 $$ = $2;
3666 /*% ripper: mlhs_paren!($:2) %*/
3667 }
3668 ;
3669
3670mlhs_inner : mlhs_basic
3671 | tLPAREN mlhs_inner rparen
3672 {
3673 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3674 /*% ripper: mlhs_paren!($:2) %*/
3675 }
3676 ;
3677
3678mlhs_basic : mlhs_head
3679 {
3680 $$ = NEW_MASGN($1, 0, &@$);
3681 /*% ripper: $:1 %*/
3682 }
3683 | mlhs_head mlhs_item
3684 {
3685 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3686 /*% ripper: mlhs_add!($:1, $:2) %*/
3687 }
3688 | mlhs_head tSTAR mlhs_node
3689 {
3690 $$ = NEW_MASGN($1, $3, &@$);
3691 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3692 }
3693 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3694 {
3695 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3696 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3697 }
3698 | mlhs_head tSTAR
3699 {
3700 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3701 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3702 }
3703 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3704 {
3705 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3706 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3707 }
3708 | tSTAR mlhs_node
3709 {
3710 $$ = NEW_MASGN(0, $2, &@$);
3711 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3712 }
3713 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3714 {
3715 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3716 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3717 }
3718 | tSTAR
3719 {
3720 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3721 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3722 }
3723 | tSTAR ',' mlhs_items(mlhs_item)
3724 {
3725 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3726 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3727 }
3728 ;
3729
3730mlhs_item : mlhs_node
3731 | tLPAREN mlhs_inner rparen
3732 {
3733 $$ = (NODE *)$2;
3734 /*% ripper: mlhs_paren!($:2) %*/
3735 }
3736 ;
3737
3738mlhs_head : mlhs_item ','
3739 {
3740 $$ = NEW_LIST($1, &@1);
3741 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3742 }
3743 | mlhs_head mlhs_item ','
3744 {
3745 $$ = list_append(p, $1, $2);
3746 /*% ripper: mlhs_add!($:1, $:2) %*/
3747 }
3748 ;
3749
3750
3751mlhs_node : user_or_keyword_variable
3752 {
3753 /*% ripper: var_field!($:1) %*/
3754 $$ = assignable(p, $1, 0, &@$);
3755 }
3756 | primary_value '[' opt_call_args rbracket
3757 {
3758 $$ = aryset(p, $1, $3, &@$);
3759 /*% ripper: aref_field!($:1, $:3) %*/
3760 }
3761 | primary_value call_op ident_or_const
3762 {
3763 anddot_multiple_assignment_check(p, &@2, $2);
3764 $$ = attrset(p, $1, $2, $3, &@$);
3765 /*% ripper: field!($:1, $:2, $:3) %*/
3766 }
3767 | primary_value tCOLON2 tIDENTIFIER
3768 {
3769 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3770 /*% ripper: const_path_field!($:1, $:3) %*/
3771 }
3772 | primary_value tCOLON2 tCONSTANT
3773 {
3774 /*% ripper: const_path_field!($:1, $:3) %*/
3775 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3776 }
3777 | tCOLON3 tCONSTANT
3778 {
3779 /*% ripper: top_const_field!($:2) %*/
3780 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3781 }
3782 | backref
3783 {
3784 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3785 $$ = NEW_ERROR(&@$);
3786 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3787 }
3788 ;
3789
3790lhs : user_or_keyword_variable
3791 {
3792 /*% ripper: var_field!($:1) %*/
3793 $$ = assignable(p, $1, 0, &@$);
3794 }
3795 | primary_value '[' opt_call_args rbracket
3796 {
3797 $$ = aryset(p, $1, $3, &@$);
3798 /*% ripper: aref_field!($:1, $:3) %*/
3799 }
3800 | primary_value call_op ident_or_const
3801 {
3802 $$ = attrset(p, $1, $2, $3, &@$);
3803 /*% ripper: field!($:1, $:2, $:3) %*/
3804 }
3805 | primary_value tCOLON2 tIDENTIFIER
3806 {
3807 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3808 /*% ripper: field!($:1, $:2, $:3) %*/
3809 }
3810 | primary_value tCOLON2 tCONSTANT
3811 {
3812 /*% ripper: const_path_field!($:1, $:3) %*/
3813 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3814 }
3815 | tCOLON3 tCONSTANT
3816 {
3817 /*% ripper: top_const_field!($:2) %*/
3818 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3819 }
3820 | backref
3821 {
3822 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3823 $$ = NEW_ERROR(&@$);
3824 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3825 }
3826 ;
3827
3828cname : tIDENTIFIER
3829 {
3830 static const char mesg[] = "class/module name must be CONSTANT";
3831 /*%%%*/
3832 yyerror1(&@1, mesg);
3833 /*% %*/
3834 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3835 }
3836 | tCONSTANT
3837 ;
3838
3839cpath : tCOLON3 cname
3840 {
3841 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3842 /*% ripper: top_const_ref!($:2) %*/
3843 }
3844 | cname
3845 {
3846 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3847 /*% ripper: const_ref!($:1) %*/
3848 }
3849 | primary_value tCOLON2 cname
3850 {
3851 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3852 /*% ripper: const_path_ref!($:1, $:3) %*/
3853 }
3854 ;
3855
3856fname : operation
3857 | op
3858 {
3859 SET_LEX_STATE(EXPR_ENDFN);
3860 $$ = $1;
3861 }
3862 | reswords
3863 ;
3864
3865fitem : fname
3866 {
3867 $$ = NEW_SYM(rb_id2str($1), &@$);
3868 /*% ripper: symbol_literal!($:1) %*/
3869 }
3870 | symbol
3871 ;
3872
3873undef_list : fitem
3874 {
3875 $$ = NEW_UNDEF($1, &@$);
3876 /*% ripper: rb_ary_new3(1, $:1) %*/
3877 }
3878 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3879 {
3880 nd_set_last_loc($1, @4.end_pos);
3881 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3882 /*% ripper: rb_ary_push($:1, $:4) %*/
3883 }
3884 ;
3885
3886op : '|' { $$ = '|'; }
3887 | '^' { $$ = '^'; }
3888 | '&' { $$ = '&'; }
3889 | tCMP { $$ = tCMP; }
3890 | tEQ { $$ = tEQ; }
3891 | tEQQ { $$ = tEQQ; }
3892 | tMATCH { $$ = tMATCH; }
3893 | tNMATCH { $$ = tNMATCH; }
3894 | '>' { $$ = '>'; }
3895 | tGEQ { $$ = tGEQ; }
3896 | '<' { $$ = '<'; }
3897 | tLEQ { $$ = tLEQ; }
3898 | tNEQ { $$ = tNEQ; }
3899 | tLSHFT { $$ = tLSHFT; }
3900 | tRSHFT { $$ = tRSHFT; }
3901 | '+' { $$ = '+'; }
3902 | '-' { $$ = '-'; }
3903 | '*' { $$ = '*'; }
3904 | tSTAR { $$ = '*'; }
3905 | '/' { $$ = '/'; }
3906 | '%' { $$ = '%'; }
3907 | tPOW { $$ = tPOW; }
3908 | tDSTAR { $$ = tDSTAR; }
3909 | '!' { $$ = '!'; }
3910 | '~' { $$ = '~'; }
3911 | tUPLUS { $$ = tUPLUS; }
3912 | tUMINUS { $$ = tUMINUS; }
3913 | tAREF { $$ = tAREF; }
3914 | tASET { $$ = tASET; }
3915 | '`' { $$ = '`'; }
3916 ;
3917
3918reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3919 | keyword_BEGIN | keyword_END
3920 | keyword_alias | keyword_and | keyword_begin
3921 | keyword_break | keyword_case | keyword_class | keyword_def
3922 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3923 | keyword_end | keyword_ensure | keyword_false
3924 | keyword_for | keyword_in | keyword_module | keyword_next
3925 | keyword_nil | keyword_not | keyword_or | keyword_redo
3926 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3927 | keyword_super | keyword_then | keyword_true | keyword_undef
3928 | keyword_when | keyword_yield | keyword_if | keyword_unless
3929 | keyword_while | keyword_until
3930 ;
3931
3932arg : asgn(arg_rhs)
3933 | op_asgn(arg_rhs)
3934 | range_expr(arg)
3935 | arg '+' arg
3936 {
3937 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3938 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3939 }
3940 | arg '-' arg
3941 {
3942 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3943 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3944 }
3945 | arg '*' arg
3946 {
3947 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3948 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3949 }
3950 | arg '/' arg
3951 {
3952 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3953 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3954 }
3955 | arg '%' arg
3956 {
3957 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3958 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3959 }
3960 | arg tPOW arg
3961 {
3962 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3963 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3964 }
3965 | tUMINUS_NUM simple_numeric tPOW arg
3966 {
3967 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3968 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3969 }
3970 | tUPLUS arg
3971 {
3972 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3973 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3974 }
3975 | tUMINUS arg
3976 {
3977 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3978 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3979 }
3980 | arg '|' arg
3981 {
3982 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3983 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3984 }
3985 | arg '^' arg
3986 {
3987 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3988 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3989 }
3990 | arg '&' arg
3991 {
3992 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3993 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3994 }
3995 | arg tCMP arg
3996 {
3997 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3998 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3999 }
4000 | rel_expr %prec tCMP
4001 | arg tEQ arg
4002 {
4003 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4004 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4005 }
4006 | arg tEQQ arg
4007 {
4008 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4009 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4010 }
4011 | arg tNEQ arg
4012 {
4013 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4014 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4015 }
4016 | arg tMATCH arg
4017 {
4018 $$ = match_op(p, $1, $3, &@2, &@$);
4019 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4020 }
4021 | arg tNMATCH arg
4022 {
4023 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4024 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4025 }
4026 | '!' arg
4027 {
4028 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4029 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4030 }
4031 | '~' arg
4032 {
4033 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4034 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4035 }
4036 | arg tLSHFT arg
4037 {
4038 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4039 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4040 }
4041 | arg tRSHFT arg
4042 {
4043 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4044 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4045 }
4046 | arg tANDOP arg
4047 {
4048 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4049 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4050 }
4051 | arg tOROP arg
4052 {
4053 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4054 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4055 }
4056 | keyword_defined '\n'? begin_defined arg
4057 {
4058 p->ctxt.in_defined = $3.in_defined;
4059 $$ = new_defined(p, $4, &@$, &@1);
4060 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4061 /*% ripper: defined!($:4) %*/
4062 }
4063 | def_endless_method(endless_arg)
4064 | ternary
4065 | primary
4066 ;
4067
4068ternary : arg '?' arg '\n'? ':' arg
4069 {
4070 value_expr(p, $1);
4071 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4072 fixpos($$, $1);
4073 /*% ripper: ifop!($:1, $:3, $:6) %*/
4074 }
4075 ;
4076
4077endless_arg : arg %prec modifier_rescue
4078 | endless_arg modifier_rescue after_rescue arg
4079 {
4080 p->ctxt.in_rescue = $3.in_rescue;
4081 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4082 /*% ripper: rescue_mod!($:1, $:4) %*/
4083 }
4084 | keyword_not '\n'? endless_arg
4085 {
4086 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4087 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4088 }
4089 ;
4090
4091relop : '>' {$$ = '>';}
4092 | '<' {$$ = '<';}
4093 | tGEQ {$$ = idGE;}
4094 | tLEQ {$$ = idLE;}
4095 ;
4096
4097rel_expr : arg relop arg %prec '>'
4098 {
4099 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4100 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4101 }
4102 | rel_expr relop arg %prec '>'
4103 {
4104 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4105 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4106 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4107 }
4108 ;
4109
4110lex_ctxt : none
4111 {
4112 $$ = p->ctxt;
4113 }
4114 ;
4115
4116begin_defined : lex_ctxt
4117 {
4118 p->ctxt.in_defined = 1;
4119 $$ = $1;
4120 }
4121 ;
4122
4123after_rescue : lex_ctxt
4124 {
4125 p->ctxt.in_rescue = after_rescue;
4126 $$ = $1;
4127 }
4128 ;
4129
4130arg_value : value_expr(arg)
4131 ;
4132
4133aref_args : none
4134 | args trailer
4135 | args ',' assocs trailer
4136 {
4137 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4138 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4139 }
4140 | assocs trailer
4141 {
4142 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4143 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4144 }
4145 ;
4146
4147arg_rhs : arg %prec tOP_ASGN
4148 {
4149 value_expr(p, $1);
4150 $$ = $1;
4151 }
4152 | arg modifier_rescue after_rescue arg
4153 {
4154 p->ctxt.in_rescue = $3.in_rescue;
4155 value_expr(p, $1);
4156 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4157 /*% ripper: rescue_mod!($:1, $:4) %*/
4158 }
4159 ;
4160
4161paren_args : '(' opt_call_args rparen
4162 {
4163 $$ = $2;
4164 /*% ripper: arg_paren!($:2) %*/
4165 }
4166 | '(' args ',' args_forward rparen
4167 {
4168 if (!check_forwarding_args(p)) {
4169 $$ = 0;
4170 }
4171 else {
4172 $$ = new_args_forward_call(p, $2, &@4, &@$);
4173 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4174 }
4175 }
4176 | '(' args_forward rparen
4177 {
4178 if (!check_forwarding_args(p)) {
4179 $$ = 0;
4180 }
4181 else {
4182 $$ = new_args_forward_call(p, 0, &@2, &@$);
4183 /*% ripper: arg_paren!($:2) %*/
4184 }
4185 }
4186 ;
4187
4188opt_paren_args : none
4189 | paren_args
4190 {
4191 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4192 }
4193 ;
4194
4195opt_call_args : none
4196 | call_args
4197 | args ','
4198 | args ',' assocs ','
4199 {
4200 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4201 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4202 }
4203 | assocs ','
4204 {
4205 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4206 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4207 }
4208 ;
4209
4210call_args : value_expr(command)
4211 {
4212 $$ = NEW_LIST($1, &@$);
4213 /*% ripper: args_add!(args_new!, $:1) %*/
4214 }
4215 | def_endless_method(endless_command)
4216 {
4217 $$ = NEW_LIST($1, &@$);
4218 /*% ripper: args_add!(args_new!, $:1) %*/
4219 }
4220 | args opt_block_arg
4221 {
4222 $$ = arg_blk_pass($1, $2);
4223 /*% ripper: args_add_block!($:1, $:2) %*/
4224 }
4225 | assocs opt_block_arg
4226 {
4227 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4228 $$ = arg_blk_pass($$, $2);
4229 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4230 }
4231 | args ',' assocs opt_block_arg
4232 {
4233 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4234 $$ = arg_blk_pass($$, $4);
4235 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4236 }
4237 | block_arg
4238 /*% ripper: args_add_block!(args_new!, $:1) %*/
4239 ;
4240
4241command_args : {
4242 /* If call_args starts with a open paren '(' or '[',
4243 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4244 * but the push must be done after CMDARG_PUSH(1).
4245 * So this code makes them consistent by first cancelling
4246 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4247 * and finally redoing CMDARG_PUSH(0).
4248 */
4249 int lookahead = 0;
4250 switch (yychar) {
4251 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4252 lookahead = 1;
4253 }
4254 if (lookahead) CMDARG_POP();
4255 CMDARG_PUSH(1);
4256 if (lookahead) CMDARG_PUSH(0);
4257 }
4258 call_args
4259 {
4260 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4261 * but the push must be done after CMDARG_POP() in the parser.
4262 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4263 * CMDARG_POP() to pop 1 pushed by command_args,
4264 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4265 */
4266 int lookahead = 0;
4267 switch (yychar) {
4268 case tLBRACE_ARG:
4269 lookahead = 1;
4270 }
4271 if (lookahead) CMDARG_POP();
4272 CMDARG_POP();
4273 if (lookahead) CMDARG_PUSH(0);
4274 $$ = $2;
4275 /*% ripper: $:2 %*/
4276 }
4277 ;
4278
4279block_arg : tAMPER arg_value
4280 {
4281 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4282 /*% ripper: $:2 %*/
4283 }
4284 | tAMPER
4285 {
4286 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4287 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4288 /*% ripper: Qnil %*/
4289 }
4290 ;
4291
4292opt_block_arg : ',' block_arg
4293 {
4294 $$ = $2;
4295 /*% ripper: $:2 %*/
4296 }
4297 | none
4298 {
4299 $$ = 0;
4300 /*% ripper: Qfalse %*/
4301 }
4302 ;
4303
4304/* value */
4305args : arg_value
4306 {
4307 $$ = NEW_LIST($arg_value, &@$);
4308 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4309 }
4310 | arg_splat
4311 {
4312 $$ = $arg_splat;
4313 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4314 }
4315 | args[non_last_args] ',' arg_value
4316 {
4317 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4318 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4319 }
4320 | args[non_last_args] ',' arg_splat
4321 {
4322 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4323 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4324 }
4325 ;
4326
4327/* value */
4328arg_splat : tSTAR arg_value
4329 {
4330 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4331 /*% ripper: $:arg_value %*/
4332 }
4333 | tSTAR /* none */
4334 {
4335 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4336 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4337 /*% ripper: Qnil %*/
4338 }
4339 ;
4340
4341/* value */
4342mrhs_arg : mrhs
4343 | arg_value
4344 ;
4345
4346/* value */
4347mrhs : args ',' arg_value
4348 {
4349 $$ = last_arg_append(p, $args, $arg_value, &@$);
4350 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4351 }
4352 | args ',' tSTAR arg_value
4353 {
4354 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4355 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4356 }
4357 | tSTAR arg_value
4358 {
4359 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4360 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4361 }
4362 ;
4363
4364%rule %inline inline_primary
4365 : literal
4366 | strings
4367 | xstring
4368 | regexp
4369 | words
4370 | qwords
4371 | symbols
4372 | qsymbols
4373 ;
4374
4375primary : inline_primary
4376 | var_ref
4377 | backref
4378 | tFID[fid]
4379 {
4380 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4381 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
4382 }
4383 | k_begin[kw]
4384 {
4385 CMDARG_PUSH(0);
4386 }
4387 bodystmt[body]
4388 k_end[k_end]
4389 {
4390 CMDARG_POP();
4391 set_line_body($body, @kw.end_pos.lineno);
4392 $$ = NEW_BEGIN($body, &@$);
4393 nd_set_line($$, @kw.end_pos.lineno);
4394 /*% ripper: begin!($:body) %*/
4395 }
4396 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4397 {
4398 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4399 $$ = $body;
4400 /*% ripper: paren!($:body) %*/
4401 }
4402 | tLPAREN compstmt(stmts)[body] ')'
4403 {
4404 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4405 $$ = NEW_BLOCK($body, &@$);
4406 /*% ripper: paren!($:body) %*/
4407 }
4408 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4409 {
4410 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4411 /*% ripper: const_path_ref!($:recv, $:name) %*/
4412 }
4413 | tCOLON3[top] tCONSTANT[name]
4414 {
4415 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4416 /*% ripper: top_const_ref!($:name) %*/
4417 }
4418 | tLBRACK aref_args[args] ']'
4419 {
4420 $$ = make_list($args, &@$);
4421 /*% ripper: array!($:args) %*/
4422 }
4423 | tLBRACE assoc_list[list] '}'
4424 {
4425 $$ = new_hash(p, $list, &@$);
4426 RNODE_HASH($$)->nd_brace = TRUE;
4427 /*% ripper: hash!($:list) %*/
4428 }
4429 | k_return[kw]
4430 {
4431 $$ = NEW_RETURN(0, &@$, &@kw);
4432 /*% ripper: return0! %*/
4433 }
4434 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4435 {
4436 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4437 /*% ripper: yield!(paren!($:args)) %*/
4438 }
4439 | k_yield[kw] '('[lpar] rparen[rpar]
4440 {
4441 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4442 /*% ripper: yield!(paren!(args_new!)) %*/
4443 }
4444 | k_yield[kw]
4445 {
4446 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4447 /*% ripper: yield0! %*/
4448 }
4449 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
4450 {
4451 p->ctxt.in_defined = $ctxt.in_defined;
4452 $$ = new_defined(p, $arg, &@$, &@kw);
4453 p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon;
4454 /*% ripper: defined!($:arg) %*/
4455 }
4456 | keyword_not[kw] '(' expr[arg] rparen
4457 {
4458 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4459 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4460 }
4461 | keyword_not[kw] '('[lpar] rparen
4462 {
4463 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4464 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4465 }
4466 | fcall[call] brace_block[block]
4467 {
4468 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4469 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4470 }
4471 | method_call
4472 | method_call[call] brace_block[block]
4473 {
4474 block_dup_check(p, get_nd_args(p, $call), $block);
4475 $$ = method_add_block(p, $call, $block, &@$);
4476 /*% ripper: method_add_block!($:call, $:block) %*/
4477 }
4478 | lambda
4479 | k_if[kw] expr_value[cond] then[then]
4480 compstmt(stmts)[body]
4481 if_tail[tail]
4482 k_end[k_end]
4483 {
4484 if ($tail && nd_type_p($tail, NODE_IF))
4485 RNODE_IF($tail)->end_keyword_loc = @k_end;
4486
4487 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4488 fixpos($$, $cond);
4489 /*% ripper: if!($:cond, $:body, $:tail) %*/
4490 }
4491 | k_unless[kw] expr_value[cond] then[then]
4492 compstmt(stmts)[body]
4493 opt_else[tail]
4494 k_end[k_end]
4495 {
4496 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4497 fixpos($$, $cond);
4498 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4499 }
4500 | k_while[kw] expr_value_do[cond]
4501 compstmt(stmts)[body]
4502 k_end[k_end]
4503 {
4504 restore_block_exit(p, $kw);
4505 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4506 fixpos($$, $cond);
4507 /*% ripper: while!($:cond, $:body) %*/
4508 }
4509 | k_until[kw] expr_value_do[cond]
4510 compstmt(stmts)[body]
4511 k_end[k_end]
4512 {
4513 restore_block_exit(p, $kw);
4514 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4515 fixpos($$, $cond);
4516 /*% ripper: until!($:cond, $:body) %*/
4517 }
4518 | k_case[k_case] expr_value[expr] terms?
4519 {
4520 $$ = p->case_labels;
4521 p->case_labels = CHECK_LITERAL_WHEN;
4522 }[labels]<labels>
4523 case_body[body]
4524 k_end[k_end]
4525 {
4526 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4527 p->case_labels = $labels;
4528 $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end);
4529 fixpos($$, $expr);
4530 /*% ripper: case!($:expr, $:body) %*/
4531 }
4532 | k_case[k_case] terms?
4533 {
4534 $$ = p->case_labels;
4535 p->case_labels = 0;
4536 }[labels]<labels>
4537 case_body[body]
4538 k_end[k_end]
4539 {
4540 if (p->case_labels) st_free_table(p->case_labels);
4541 p->case_labels = $labels;
4542 $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end);
4543 /*% ripper: case!(Qnil, $:body) %*/
4544 }
4545 | k_case[k_case] expr_value[expr] terms?
4546 p_case_body[body]
4547 k_end[k_end]
4548 {
4549 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4550 /*% ripper: case!($:expr, $:body) %*/
4551 }
4552 | k_for[k_for] for_var[for_var] keyword_in[keyword_in]
4553 {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();}
4554 compstmt(stmts)[compstmt]
4555 k_end[k_end]
4556 {
4557 restore_block_exit(p, $k_for);
4558 /*
4559 * for a, b, c in e
4560 * #=>
4561 * e.each{|*x| a, b, c = x}
4562 *
4563 * for a in e
4564 * #=>
4565 * e.each{|x| a, = x}
4566 */
4567 ID id = internal_id(p);
4568 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4569 rb_node_args_t *args;
4570 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4571 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4572 tbl->ids[0] = id; /* internal id */
4573
4574 switch (nd_type($for_var)) {
4575 case NODE_LASGN:
4576 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4577 set_nd_value(p, $for_var, internal_var);
4578 id = 0;
4579 m->nd_plen = 1;
4580 m->nd_next = $for_var;
4581 break;
4582 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4583 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4584 break;
4585 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4586 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);
4587 }
4588 /* {|*internal_id| <m> = internal_id; ... } */
4589 args = new_args(p, m, 0, id, 0, new_empty_args_tail(p, &@for_var), &@for_var);
4590 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4591 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4592 $$ = NEW_FOR($expr_value, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4593 RNODE_SCOPE(scope)->nd_parent = $$;
4594 fixpos($$, $for_var);
4595 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4596 }
4597 | k_class cpath superclass
4598 {
4599 begin_definition("class", &@k_class, &@cpath);
4600 }
4601 bodystmt
4602 k_end
4603 {
4604 YYLTYPE inheritance_operator_loc = NULL_LOC;
4605 if ($superclass) {
4606 inheritance_operator_loc = @superclass;
4607 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4608 }
4609 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4610 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4611 set_line_body($bodystmt, @superclass.end_pos.lineno);
4612 nd_set_line($$, @superclass.end_pos.lineno);
4613 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4614 local_pop(p);
4615 p->ctxt.in_class = $k_class.in_class;
4616 p->ctxt.cant_return = $k_class.cant_return;
4617 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4618 }
4619 | k_class tLSHFT expr_value
4620 {
4621 begin_definition("", &@k_class, &@tLSHFT);
4622 }
4623 term
4624 bodystmt
4625 k_end
4626 {
4627 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4628 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4629 set_line_body($bodystmt, nd_line($expr_value));
4630 fixpos($$, $expr_value);
4631 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4632 local_pop(p);
4633 p->ctxt.in_def = $k_class.in_def;
4634 p->ctxt.in_class = $k_class.in_class;
4635 p->ctxt.cant_return = $k_class.cant_return;
4636 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4637 }
4638 | k_module cpath
4639 {
4640 begin_definition("module", &@k_module, &@cpath);
4641 }
4642 bodystmt
4643 k_end
4644 {
4645 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4646 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4647 set_line_body($bodystmt, @cpath.end_pos.lineno);
4648 nd_set_line($$, @cpath.end_pos.lineno);
4649 /*% ripper: module!($:cpath, $:bodystmt) %*/
4650 local_pop(p);
4651 p->ctxt.in_class = $k_module.in_class;
4652 p->ctxt.cant_return = $k_module.cant_return;
4653 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4654 }
4655 | defn_head[head]
4656 f_arglist[args]
4657 {
4658 push_end_expect_token_locations(p, &@head.beg_pos);
4659 }
4660 bodystmt
4661 k_end
4662 {
4663 restore_defun(p, $head);
4664 ($$ = $head->nd_def)->nd_loc = @$;
4665 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4666 RNODE_DEFN($$)->nd_defn = $bodystmt;
4667 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4668 local_pop(p);
4669 }
4670 | defs_head[head]
4671 f_arglist[args]
4672 {
4673 push_end_expect_token_locations(p, &@head.beg_pos);
4674 }
4675 bodystmt
4676 k_end
4677 {
4678 restore_defun(p, $head);
4679 ($$ = $head->nd_def)->nd_loc = @$;
4680 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4681 RNODE_DEFS($$)->nd_defn = $bodystmt;
4682 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4683 local_pop(p);
4684 }
4685 | keyword_break[kw]
4686 {
4687 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4688 /*% ripper: break!(args_new!) %*/
4689 }
4690 | keyword_next[kw]
4691 {
4692 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4693 /*% ripper: next!(args_new!) %*/
4694 }
4695 | keyword_redo[kw]
4696 {
4697 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4698 /*% ripper: redo! %*/
4699 }
4700 | keyword_retry[kw]
4701 {
4702 if (!p->ctxt.in_defined) {
4703 switch (p->ctxt.in_rescue) {
4704 case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break;
4705 case after_rescue: /* ok */ break;
4706 case after_else: yyerror1(&@kw, "Invalid retry after else"); break;
4707 case after_ensure: yyerror1(&@kw, "Invalid retry after ensure"); break;
4708 }
4709 }
4710 $$ = NEW_RETRY(&@$);
4711 /*% ripper: retry! %*/
4712 }
4713 ;
4714
4715primary_value : value_expr(primary)
4716 ;
4717
4718k_begin : keyword_begin
4719 {
4720 token_info_push(p, "begin", &@$);
4721 push_end_expect_token_locations(p, &@1.beg_pos);
4722 }
4723 ;
4724
4725k_if : keyword_if
4726 {
4727 WARN_EOL("if");
4728 token_info_push(p, "if", &@$);
4729 if (p->token_info && p->token_info->nonspc &&
4730 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4731 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4732 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4733 beg += rb_strlen_lit("else");
4734 while (beg < tok && ISSPACE(*beg)) beg++;
4735 if (beg == tok) {
4736 p->token_info->nonspc = 0;
4737 }
4738 }
4739 push_end_expect_token_locations(p, &@1.beg_pos);
4740 }
4741 ;
4742
4743k_unless : keyword_unless
4744 {
4745 token_info_push(p, "unless", &@$);
4746 push_end_expect_token_locations(p, &@1.beg_pos);
4747 }
4748 ;
4749
4750k_while : keyword_while[kw] allow_exits
4751 {
4752 $$ = $allow_exits;
4753 token_info_push(p, "while", &@$);
4754 push_end_expect_token_locations(p, &@kw.beg_pos);
4755 }
4756 ;
4757
4758k_until : keyword_until[kw] allow_exits
4759 {
4760 $$ = $allow_exits;
4761 token_info_push(p, "until", &@$);
4762 push_end_expect_token_locations(p, &@kw.beg_pos);
4763 }
4764 ;
4765
4766k_case : keyword_case
4767 {
4768 token_info_push(p, "case", &@$);
4769 push_end_expect_token_locations(p, &@1.beg_pos);
4770 }
4771 ;
4772
4773k_for : keyword_for[kw] allow_exits
4774 {
4775 $$ = $allow_exits;
4776 token_info_push(p, "for", &@$);
4777 push_end_expect_token_locations(p, &@kw.beg_pos);
4778 }
4779 ;
4780
4781k_class : keyword_class
4782 {
4783 token_info_push(p, "class", &@$);
4784 $$ = p->ctxt;
4785 p->ctxt.in_rescue = before_rescue;
4786 push_end_expect_token_locations(p, &@1.beg_pos);
4787 }
4788 ;
4789
4790k_module : keyword_module
4791 {
4792 token_info_push(p, "module", &@$);
4793 $$ = p->ctxt;
4794 p->ctxt.in_rescue = before_rescue;
4795 push_end_expect_token_locations(p, &@1.beg_pos);
4796 }
4797 ;
4798
4799k_def : keyword_def
4800 {
4801 token_info_push(p, "def", &@$);
4802 $$ = NEW_DEF_TEMP(&@$);
4803 p->ctxt.in_argdef = 1;
4804 }
4805 ;
4806
4807k_do : keyword_do
4808 {
4809 token_info_push(p, "do", &@$);
4810 push_end_expect_token_locations(p, &@1.beg_pos);
4811 }
4812 ;
4813
4814k_do_block : keyword_do_block
4815 {
4816 token_info_push(p, "do", &@$);
4817 push_end_expect_token_locations(p, &@1.beg_pos);
4818 }
4819 ;
4820
4821k_rescue : keyword_rescue
4822 {
4823 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4824 $$ = p->ctxt;
4825 p->ctxt.in_rescue = after_rescue;
4826 }
4827 ;
4828
4829k_ensure : keyword_ensure
4830 {
4831 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4832 $$ = p->ctxt;
4833 }
4834 ;
4835
4836k_when : keyword_when
4837 {
4838 token_info_warn(p, "when", p->token_info, 0, &@$);
4839 }
4840 ;
4841
4842k_else : keyword_else
4843 {
4844 token_info *ptinfo_beg = p->token_info;
4845 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4846 token_info_warn(p, "else", p->token_info, same, &@$);
4847 if (same) {
4848 token_info e;
4849 e.next = ptinfo_beg->next;
4850 e.token = "else";
4851 token_info_setup(&e, p->lex.pbeg, &@$);
4852 if (!e.nonspc) *ptinfo_beg = e;
4853 }
4854 }
4855 ;
4856
4857k_elsif : keyword_elsif
4858 {
4859 WARN_EOL("elsif");
4860 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4861 }
4862 ;
4863
4864k_end : keyword_end
4865 {
4866 token_info_pop(p, "end", &@$);
4867 pop_end_expect_token_locations(p);
4868 }
4869 | tDUMNY_END
4870 {
4871 compile_error(p, "syntax error, unexpected end-of-input");
4872 }
4873 ;
4874
4875k_return : keyword_return
4876 {
4877 if (p->ctxt.cant_return && !dyna_in_block(p))
4878 yyerror1(&@1, "Invalid return in class/module body");
4879 }
4880 ;
4881
4882k_yield : keyword_yield
4883 {
4884 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4885 yyerror1(&@1, "Invalid yield");
4886 }
4887 ;
4888
4889then : term
4890 | keyword_then
4891 | term keyword_then
4892 ;
4893
4894do : term
4895 | keyword_do_cond { $$ = keyword_do_cond; }
4896 ;
4897
4898if_tail : opt_else
4899 | k_elsif expr_value then
4900 compstmt(stmts)
4901 if_tail
4902 {
4903 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4904 fixpos($$, $2);
4905 /*% ripper: elsif!($:2, $:4, $:5) %*/
4906 }
4907 ;
4908
4909opt_else : none
4910 | k_else compstmt(stmts)
4911 {
4912 $$ = $2;
4913 /*% ripper: else!($:2) %*/
4914 }
4915 ;
4916
4917for_var : lhs
4918 | mlhs
4919 ;
4920
4921f_marg : f_norm_arg
4922 {
4923 $$ = assignable(p, $1, 0, &@$);
4924 mark_lvar_used(p, $$);
4925 }
4926 | tLPAREN f_margs rparen
4927 {
4928 $$ = (NODE *)$2;
4929 /*% ripper: mlhs_paren!($:2) %*/
4930 }
4931 ;
4932
4933
4934f_margs : mlhs_items(f_marg)
4935 {
4936 $$ = NEW_MASGN($1, 0, &@$);
4937 /*% ripper: $:1 %*/
4938 }
4939 | mlhs_items(f_marg) ',' f_rest_marg
4940 {
4941 $$ = NEW_MASGN($1, $3, &@$);
4942 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4943 }
4944 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4945 {
4946 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4947 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4948 }
4949 | f_rest_marg
4950 {
4951 $$ = NEW_MASGN(0, $1, &@$);
4952 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4953 }
4954 | f_rest_marg ',' mlhs_items(f_marg)
4955 {
4956 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4957 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4958 }
4959 ;
4960
4961f_rest_marg : tSTAR f_norm_arg
4962 {
4963 /*% ripper: $:2 %*/
4964 $$ = assignable(p, $2, 0, &@$);
4965 mark_lvar_used(p, $$);
4966 }
4967 | tSTAR
4968 {
4969 $$ = NODE_SPECIAL_NO_NAME_REST;
4970 /*% ripper: Qnil %*/
4971 }
4972 ;
4973
4974f_any_kwrest : f_kwrest
4975 | f_no_kwarg
4976 {
4977 $$ = idNil;
4978 /*% ripper: ID2VAL(idNil) %*/
4979 }
4980 ;
4981
4982f_eq : {p->ctxt.in_argdef = 0;} '=';
4983
4984block_args_tail : args_tail_basic(primary_value, none)
4985 ;
4986
4987excessed_comma : ','
4988 {
4989 /* magic number for rest_id in iseq_set_arguments() */
4990 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4991 /*% ripper: excessed_comma! %*/
4992 }
4993 ;
4994
4995block_param : args-list(primary_value, opt_args_tail(block_args_tail, none))
4996 | f_arg[pre] excessed_comma
4997 {
4998 $$ = new_empty_args_tail(p, &@excessed_comma);
4999 $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$);
5000 /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/
5001 }
5002 | f_arg[pre] opt_args_tail(block_args_tail, none)[tail]
5003 {
5004 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5005 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5006 }
5007 | tail-only-args(block_args_tail)
5008 ;
5009
5010opt_block_param_def : none
5011 | block_param_def
5012 {
5013 p->command_start = TRUE;
5014 }
5015 ;
5016
5017block_param_def : '|' opt_block_param opt_bv_decl '|'
5018 {
5019 p->max_numparam = ORDINAL_PARAM;
5020 p->ctxt.in_argdef = 0;
5021 $$ = $2;
5022 /*% ripper: block_var!($:2, $:3) %*/
5023 }
5024 ;
5025
5026opt_block_param : /* none */
5027 {
5028 $$ = 0;
5029 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5030 }
5031 | block_param
5032 ;
5033
5034opt_bv_decl : '\n'?
5035 {
5036 $$ = 0;
5037 /*% ripper: Qfalse %*/
5038 }
5039 | '\n'? ';' bv_decls '\n'?
5040 {
5041 $$ = 0;
5042 /*% ripper: $:3 %*/
5043 }
5044 ;
5045
5046bv_decls : bvar
5047 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5048 | bv_decls ',' bvar
5049 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5050 ;
5051
5052bvar : tIDENTIFIER
5053 {
5054 new_bv(p, $1);
5055 /*% ripper: $:1 %*/
5056 }
5057 | f_bad_arg
5058 ;
5059
5060max_numparam : {
5061 $$ = p->max_numparam;
5062 p->max_numparam = 0;
5063 }
5064 ;
5065
5066numparam : {
5067 $$ = numparam_push(p);
5068 }
5069 ;
5070
5071it_id : {
5072 $$ = p->it_id;
5073 p->it_id = 0;
5074 }
5075 ;
5076
5077lambda : tLAMBDA[lpar]
5078 {
5079 token_info_push(p, "->", &@lpar);
5080 $$ = dyna_push(p);
5081 }[dyna]<vars>
5082 max_numparam numparam it_id allow_exits
5083 f_larglist[args]
5084 {
5085 CMDARG_PUSH(0);
5086 }
5087 lambda_body[body]
5088 {
5089 int max_numparam = p->max_numparam;
5090 ID it_id = p->it_id;
5091 p->lex.lpar_beg = $lpar;
5092 p->max_numparam = $max_numparam;
5093 p->it_id = $it_id;
5094 restore_block_exit(p, $allow_exits);
5095 CMDARG_POP();
5096 $args = args_with_numbered(p, $args, max_numparam, it_id);
5097 {
5098 YYLTYPE loc = code_loc_gen(&@lpar, &@body);
5099 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5100 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5101 nd_set_line($$, @args.end_pos.lineno);
5102 nd_set_first_loc($$, @lpar.beg_pos);
5103 xfree($body);
5104 }
5105 /*% ripper: lambda!($:args, $:body) %*/
5106 numparam_pop(p, $numparam);
5107 dyna_pop(p, $dyna);
5108 }
5109 ;
5110
5111f_larglist : '(' f_largs[args] opt_bv_decl ')'
5112 {
5113 p->ctxt.in_argdef = 0;
5114 $$ = $args;
5115 p->max_numparam = ORDINAL_PARAM;
5116 /*% ripper: paren!($:args) %*/
5117 }
5118 | f_largs[args]
5119 {
5120 p->ctxt.in_argdef = 0;
5121 if (!args_info_empty_p(&$args->nd_ainfo))
5122 p->max_numparam = ORDINAL_PARAM;
5123 $$ = $args;
5124 }
5125 ;
5126
5127lambda_body : tLAMBEG compstmt(stmts) '}'
5128 {
5129 token_info_pop(p, "}", &@3);
5130 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5131 /*% ripper: $:2 %*/
5132 }
5133 | keyword_do_LAMBDA
5134 {
5135 push_end_expect_token_locations(p, &@1.beg_pos);
5136 }
5137 bodystmt k_end
5138 {
5139 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5140 /*% ripper: $:3 %*/
5141 }
5142 ;
5143
5144do_block : k_do_block do_body k_end
5145 {
5146 $$ = $2;
5147 set_embraced_location($$, &@1, &@3);
5148 /*% ripper: $:2 %*/
5149 }
5150 ;
5151
5152block_call : command do_block
5153 {
5154 $$ = command_add_block(p, $1, $2, &@$);
5155 fixpos($$, $1);
5156 /*% ripper: method_add_block!($:1, $:2) %*/
5157 }
5158 | block_call call_op2 operation2 opt_paren_args
5159 {
5160 bool has_args = $4 != 0;
5161 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5162 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5163 /*% ripper: call!($:1, $:2, $:3) %*/
5164 if (has_args) {
5165 /*% ripper: method_add_arg!($:$, $:4) %*/
5166 }
5167 }
5168 | block_call call_op2 operation2 opt_paren_args brace_block
5169 {
5170 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5171 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5172 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5173 }
5174 | block_call call_op2 operation2 command_args do_block
5175 {
5176 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5177 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5178 }
5179 | block_call call_op2 paren_args
5180 {
5181 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5182 nd_set_line($$, @2.end_pos.lineno);
5183 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5184 }
5185 ;
5186
5187method_call : fcall paren_args
5188 {
5189 $1->nd_args = $2;
5190 $$ = (NODE *)$1;
5191 nd_set_last_loc($1, @2.end_pos);
5192 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5193 }
5194 | primary_value call_op operation2 opt_paren_args
5195 {
5196 bool has_args = $4 != 0;
5197 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5198 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5199 nd_set_line($$, @3.end_pos.lineno);
5200 /*% ripper: call!($:1, $:2, $:3) %*/
5201 if (has_args) {
5202 /*% ripper: method_add_arg!($:$, $:4) %*/
5203 }
5204 }
5205 | primary_value tCOLON2 operation2 paren_args
5206 {
5207 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5208 nd_set_line($$, @3.end_pos.lineno);
5209 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5210 }
5211 | primary_value tCOLON2 operation3
5212 {
5213 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5214 /*% ripper: call!($:1, $:2, $:3) %*/
5215 }
5216 | primary_value call_op2 paren_args
5217 {
5218 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5219 nd_set_line($$, @2.end_pos.lineno);
5220 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5221 }
5222 | keyword_super paren_args
5223 {
5224 rb_code_location_t lparen_loc = @2;
5225 rb_code_location_t rparen_loc = @2;
5226 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5227 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5228
5229 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5230 /*% ripper: super!($:2) %*/
5231 }
5232 | keyword_super
5233 {
5234 $$ = NEW_ZSUPER(&@$);
5235 /*% ripper: zsuper! %*/
5236 }
5237 | primary_value '[' opt_call_args rbracket
5238 {
5239 $$ = NEW_CALL($1, tAREF, $3, &@$);
5240 fixpos($$, $1);
5241 /*% ripper: aref!($:1, $:3) %*/
5242 }
5243 ;
5244
5245brace_block : '{' brace_body '}'
5246 {
5247 $$ = $2;
5248 set_embraced_location($$, &@1, &@3);
5249 /*% ripper: $:2 %*/
5250 }
5251 | k_do do_body k_end
5252 {
5253 $$ = $2;
5254 set_embraced_location($$, &@1, &@3);
5255 /*% ripper: $:2 %*/
5256 }
5257 ;
5258
5259brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5260 max_numparam numparam it_id allow_exits
5261 opt_block_param_def[args] compstmt(stmts)
5262 {
5263 int max_numparam = p->max_numparam;
5264 ID it_id = p->it_id;
5265 p->max_numparam = $max_numparam;
5266 p->it_id = $it_id;
5267 $args = args_with_numbered(p, $args, max_numparam, it_id);
5268 $$ = NEW_ITER($args, $compstmt, &@$);
5269 /*% ripper: brace_block!($:args, $:compstmt) %*/
5270 restore_block_exit(p, $allow_exits);
5271 numparam_pop(p, $numparam);
5272 dyna_pop(p, $dyna);
5273 }
5274 ;
5275
5276do_body : {
5277 $$ = dyna_push(p);
5278 CMDARG_PUSH(0);
5279 }[dyna]<vars>
5280 max_numparam numparam it_id allow_exits
5281 opt_block_param_def[args] bodystmt
5282 {
5283 int max_numparam = p->max_numparam;
5284 ID it_id = p->it_id;
5285 p->max_numparam = $max_numparam;
5286 p->it_id = $it_id;
5287 $args = args_with_numbered(p, $args, max_numparam, it_id);
5288 $$ = NEW_ITER($args, $bodystmt, &@$);
5289 /*% ripper: do_block!($:args, $:bodystmt) %*/
5290 CMDARG_POP();
5291 restore_block_exit(p, $allow_exits);
5292 numparam_pop(p, $numparam);
5293 dyna_pop(p, $dyna);
5294 }
5295 ;
5296
5297case_args : arg_value
5298 {
5299 check_literal_when(p, $arg_value, &@arg_value);
5300 $$ = NEW_LIST($arg_value, &@$);
5301 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5302 }
5303 | tSTAR arg_value
5304 {
5305 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5306 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5307 }
5308 | case_args[non_last_args] ',' arg_value
5309 {
5310 check_literal_when(p, $arg_value, &@arg_value);
5311 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5312 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5313 }
5314 | case_args[non_last_args] ',' tSTAR arg_value
5315 {
5316 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5317 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5318 }
5319 ;
5320
5321case_body : k_when case_args then
5322 compstmt(stmts)
5323 cases
5324 {
5325 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5326 fixpos($$, $2);
5327 /*% ripper: when!($:2, $:4, $:5) %*/
5328 }
5329 ;
5330
5331cases : opt_else
5332 | case_body
5333 ;
5334
5335p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5336p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5337
5338p_in_kwarg : {
5339 $$ = p->ctxt;
5340 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5341 p->command_start = FALSE;
5342 p->ctxt.in_kwarg = 1;
5343 p->ctxt.in_alt_pattern = 0;
5344 p->ctxt.capture_in_pattern = 0;
5345 }
5346 ;
5347
5348p_case_body : keyword_in
5349 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5350 p_top_expr[expr] then
5351 {
5352 pop_pktbl(p, $p_pktbl);
5353 pop_pvtbl(p, $p_pvtbl);
5354 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5355 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5356 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5357 }
5358 compstmt(stmts)
5359 p_cases[cases]
5360 {
5361 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5362 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5363 }
5364 ;
5365
5366p_cases : opt_else
5367 | p_case_body
5368 ;
5369
5370p_top_expr : p_top_expr_body
5371 | p_top_expr_body modifier_if expr_value
5372 {
5373 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5374 fixpos($$, $3);
5375 /*% ripper: if_mod!($:3, $:1) %*/
5376 }
5377 | p_top_expr_body modifier_unless expr_value
5378 {
5379 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5380 fixpos($$, $3);
5381 /*% ripper: unless_mod!($:3, $:1) %*/
5382 }
5383 ;
5384
5385p_top_expr_body : p_expr
5386 | p_expr ','
5387 {
5388 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5389 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5390 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5391 }
5392 | p_expr ',' p_args
5393 {
5394 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5395 nd_set_first_loc($$, @1.beg_pos);
5396 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5397 }
5398 | p_find
5399 {
5400 $$ = new_find_pattern(p, 0, $1, &@$);
5401 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5402 }
5403 | p_args_tail
5404 {
5405 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5406 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5407 }
5408 | p_kwargs
5409 {
5410 $$ = new_hash_pattern(p, 0, $1, &@$);
5411 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5412 }
5413 ;
5414
5415p_expr : p_as
5416 ;
5417
5418p_as : p_expr tASSOC p_variable
5419 {
5420 NODE *n = NEW_LIST($1, &@$);
5421 n = list_append(p, n, $3);
5422 $$ = new_hash(p, n, &@$);
5423 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5424 }
5425 | p_alt
5426 ;
5427
5428p_alt : p_alt[left] '|'[alt]
5429 {
5430 p->ctxt.in_alt_pattern = 1;
5431 }
5432 p_expr_basic[right]
5433 {
5434 if (p->ctxt.capture_in_pattern) {
5435 yyerror1(&@alt, "alternative pattern after variable capture");
5436 }
5437 p->ctxt.in_alt_pattern = 0;
5438 $$ = NEW_OR($left, $right, &@$, &@alt);
5439 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5440 }
5441 | p_expr_basic
5442 ;
5443
5444p_lparen : '(' p_pktbl
5445 {
5446 $$ = $2;
5447 /*% ripper: $:2 %*/
5448 }
5449 ;
5450
5451p_lbracket : '[' p_pktbl
5452 {
5453 $$ = $2;
5454 /*% ripper: $:2 %*/
5455 }
5456 ;
5457
5458p_expr_basic : p_value
5459 | p_variable
5460 | p_const p_lparen[p_pktbl] p_args rparen
5461 {
5462 pop_pktbl(p, $p_pktbl);
5463 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5464 nd_set_first_loc($$, @p_const.beg_pos);
5465 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5466 }
5467 | p_const p_lparen[p_pktbl] p_find rparen
5468 {
5469 pop_pktbl(p, $p_pktbl);
5470 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5471 nd_set_first_loc($$, @p_const.beg_pos);
5472 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5473 }
5474 | p_const p_lparen[p_pktbl] p_kwargs rparen
5475 {
5476 pop_pktbl(p, $p_pktbl);
5477 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5478 nd_set_first_loc($$, @p_const.beg_pos);
5479 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5480 }
5481 | p_const '(' rparen
5482 {
5483 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5484 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5485 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5486 }
5487 | p_const p_lbracket[p_pktbl] p_args rbracket
5488 {
5489 pop_pktbl(p, $p_pktbl);
5490 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5491 nd_set_first_loc($$, @p_const.beg_pos);
5492 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5493 }
5494 | p_const p_lbracket[p_pktbl] p_find rbracket
5495 {
5496 pop_pktbl(p, $p_pktbl);
5497 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5498 nd_set_first_loc($$, @p_const.beg_pos);
5499 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5500 }
5501 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5502 {
5503 pop_pktbl(p, $p_pktbl);
5504 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5505 nd_set_first_loc($$, @p_const.beg_pos);
5506 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5507 }
5508 | p_const '[' rbracket
5509 {
5510 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5511 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5512 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5513 }
5514 | tLBRACK p_args rbracket
5515 {
5516 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5517 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5518 }
5519 | tLBRACK p_find rbracket
5520 {
5521 $$ = new_find_pattern(p, 0, $p_find, &@$);
5522 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5523 }
5524 | tLBRACK rbracket
5525 {
5526 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5527 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5528 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5529 }
5530 | tLBRACE p_pktbl lex_ctxt[ctxt]
5531 {
5532 p->ctxt.in_kwarg = 0;
5533 }
5534 p_kwargs rbrace
5535 {
5536 pop_pktbl(p, $p_pktbl);
5537 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5538 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5539 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5540 }
5541 | tLBRACE rbrace
5542 {
5543 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5544 $$ = new_hash_pattern(p, 0, $$, &@$);
5545 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5546 }
5547 | tLPAREN p_pktbl p_expr rparen
5548 {
5549 pop_pktbl(p, $p_pktbl);
5550 $$ = $p_expr;
5551 /*% ripper: $:p_expr %*/
5552 }
5553 ;
5554
5555p_args : p_expr
5556 {
5557 NODE *pre_args = NEW_LIST($1, &@$);
5558 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5559 /*% ripper: [[$:1], Qnil, Qnil] %*/
5560 }
5561 | p_args_head
5562 {
5563 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5564 /*% ripper: [$:1, Qnil, Qnil] %*/
5565 }
5566 | p_args_head p_arg
5567 {
5568 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5569 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5570 }
5571 | p_args_head p_rest
5572 {
5573 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5574 /*% ripper: [$:1, $:2, Qnil] %*/
5575 }
5576 | p_args_head p_rest ',' p_args_post
5577 {
5578 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5579 /*% ripper: [$:1, $:2, $:4] %*/
5580 }
5581 | p_args_tail
5582 ;
5583
5584p_args_head : p_arg ','
5585 | p_args_head p_arg ','
5586 {
5587 $$ = list_concat($1, $2);
5588 /*% ripper: rb_ary_concat($:1, $:2) %*/
5589 }
5590 ;
5591
5592p_args_tail : p_rest
5593 {
5594 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5595 /*% ripper: [Qnil, $:1, Qnil] %*/
5596 }
5597 | p_rest ',' p_args_post
5598 {
5599 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5600 /*% ripper: [Qnil, $:1, $:3] %*/
5601 }
5602 ;
5603
5604p_find : p_rest ',' p_args_post ',' p_rest
5605 {
5606 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5607 /*% ripper: [$:1, $:3, $:5] %*/
5608 }
5609 ;
5610
5611
5612p_rest : tSTAR tIDENTIFIER
5613 {
5614 error_duplicate_pattern_variable(p, $2, &@2);
5615 /*% ripper: var_field!($:2) %*/
5616 $$ = assignable(p, $2, 0, &@$);
5617 }
5618 | tSTAR
5619 {
5620 $$ = 0;
5621 /*% ripper: var_field!(Qnil) %*/
5622 }
5623 ;
5624
5625p_args_post : p_arg
5626 | p_args_post ',' p_arg
5627 {
5628 $$ = list_concat($1, $3);
5629 /*% ripper: rb_ary_concat($:1, $:3) %*/
5630 }
5631 ;
5632
5633p_arg : p_expr
5634 {
5635 $$ = NEW_LIST($1, &@$);
5636 /*% ripper: [$:1] %*/
5637 }
5638 ;
5639
5640p_kwargs : p_kwarg ',' p_any_kwrest
5641 {
5642 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5643 /*% ripper: [$:1, $:3] %*/
5644 }
5645 | p_kwarg
5646 {
5647 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5648 /*% ripper: [$:1, Qnil] %*/
5649 }
5650 | p_kwarg ','
5651 {
5652 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5653 /*% ripper: [$:1, Qnil] %*/
5654 }
5655 | p_any_kwrest
5656 {
5657 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5658 /*% ripper: [[], $:1] %*/
5659 }
5660 ;
5661
5662p_kwarg : p_kw
5663 /*% ripper[brace]: [$:1] %*/
5664 | p_kwarg ',' p_kw
5665 {
5666 $$ = list_concat($1, $3);
5667 /*% ripper: rb_ary_push($:1, $:3) %*/
5668 }
5669 ;
5670
5671p_kw : p_kw_label p_expr
5672 {
5673 error_duplicate_pattern_key(p, $1, &@1);
5674 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5675 /*% ripper: [$:1, $:2] %*/
5676 }
5677 | p_kw_label
5678 {
5679 error_duplicate_pattern_key(p, $1, &@1);
5680 if ($1 && !is_local_id($1)) {
5681 yyerror1(&@1, "key must be valid as local variables");
5682 }
5683 error_duplicate_pattern_variable(p, $1, &@1);
5684 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5685 /*% ripper: [$:1, Qnil] %*/
5686 }
5687 ;
5688
5689p_kw_label : tLABEL
5690 | tSTRING_BEG string_contents tLABEL_END
5691 {
5692 YYLTYPE loc = code_loc_gen(&@1, &@3);
5693 if (!$2 || nd_type_p($2, NODE_STR)) {
5694 NODE *node = dsym_node(p, $2, &loc);
5695 $$ = rb_sym2id(rb_node_sym_string_val(node));
5696 }
5697 else {
5698 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5699 $$ = rb_intern_str(STR_NEW0());
5700 }
5701 /*% ripper: $:2 %*/
5702 }
5703 ;
5704
5705p_kwrest : kwrest_mark tIDENTIFIER
5706 {
5707 $$ = $2;
5708 /*% ripper: var_field!($:2) %*/
5709 }
5710 | kwrest_mark
5711 {
5712 $$ = 0;
5713 /*% ripper: Qnil %*/
5714 }
5715 ;
5716
5717p_kwnorest : kwrest_mark keyword_nil
5718 {
5719 $$ = 0;
5720 }
5721 ;
5722
5723p_any_kwrest : p_kwrest
5724 | p_kwnorest
5725 {
5726 $$ = idNil;
5727 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5728 }
5729 ;
5730
5731p_value : p_primitive
5732 | range_expr(p_primitive)
5733 | p_var_ref
5734 | p_expr_ref
5735 | p_const
5736 ;
5737
5738p_primitive : inline_primary
5739 | keyword_variable
5740 {
5741 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5742 /*% ripper: var_ref!($:1) %*/
5743 }
5744 | lambda
5745 ;
5746
5747p_variable : tIDENTIFIER
5748 {
5749 error_duplicate_pattern_variable(p, $1, &@1);
5750 /*% ripper: var_field!($:1) %*/
5751 $$ = assignable(p, $1, 0, &@$);
5752 }
5753 ;
5754
5755p_var_ref : '^' tIDENTIFIER
5756 {
5757 NODE *n = gettable(p, $2, &@$);
5758 if (!n) {
5759 n = NEW_ERROR(&@$);
5760 }
5761 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5762 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5763 }
5764 $$ = n;
5765 /*% ripper: var_ref!($:2) %*/
5766 }
5767 | '^' nonlocal_var
5768 {
5769 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5770 /*% ripper: var_ref!($:2) %*/
5771 }
5772 ;
5773
5774p_expr_ref : '^' tLPAREN expr_value rparen
5775 {
5776 $$ = NEW_BLOCK($3, &@$);
5777 /*% ripper: begin!($:3) %*/
5778 }
5779 ;
5780
5781p_const : tCOLON3 cname
5782 {
5783 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5784 /*% ripper: top_const_ref!($:2) %*/
5785 }
5786 | p_const tCOLON2 cname
5787 {
5788 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5789 /*% ripper: const_path_ref!($:1, $:3) %*/
5790 }
5791 | tCONSTANT
5792 {
5793 $$ = gettable(p, $1, &@$);
5794 /*% ripper: var_ref!($:1) %*/
5795 }
5796 ;
5797
5798opt_rescue : k_rescue exc_list exc_var then
5799 compstmt(stmts)
5800 opt_rescue
5801 {
5802 NODE *err = $3;
5803 if ($3) {
5804 err = NEW_ERRINFO(&@3);
5805 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5806 }
5807 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5808 if ($2) {
5809 fixpos($$, $2);
5810 }
5811 else if ($3) {
5812 fixpos($$, $3);
5813 }
5814 else {
5815 fixpos($$, $5);
5816 }
5817 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5818 }
5819 | none
5820 ;
5821
5822exc_list : arg_value
5823 {
5824 $$ = NEW_LIST($1, &@$);
5825 /*% ripper: rb_ary_new3(1, $:1) %*/
5826 }
5827 | mrhs
5828 {
5829 if (!($$ = splat_array($1))) $$ = $1;
5830 }
5831 | none
5832 ;
5833
5834exc_var : tASSOC lhs
5835 {
5836 $$ = $2;
5837 /*% ripper: $:2 %*/
5838 }
5839 | none
5840 ;
5841
5842opt_ensure : k_ensure stmts terms?
5843 {
5844 p->ctxt.in_rescue = $1.in_rescue;
5845 $$ = $2;
5846 void_expr(p, void_stmts(p, $$));
5847 /*% ripper: ensure!($:2) %*/
5848 }
5849 | none
5850 ;
5851
5852literal : numeric
5853 | symbol
5854 ;
5855
5856strings : string
5857 {
5858 if (!$1) {
5859 $$ = NEW_STR(STRING_NEW0(), &@$);
5860 }
5861 else {
5862 $$ = evstr2dstr(p, $1);
5863 }
5864 /*% ripper: $:1 %*/
5865 }
5866 ;
5867
5868string : tCHAR
5869 | string1
5870 | string string1
5871 {
5872 $$ = literal_concat(p, $1, $2, &@$);
5873 /*% ripper: string_concat!($:1, $:2) %*/
5874 }
5875 ;
5876
5877string1 : tSTRING_BEG string_contents tSTRING_END
5878 {
5879 $$ = heredoc_dedent(p, $2);
5880 if ($$) nd_set_loc($$, &@$);
5881 /*% ripper: $:2 %*/
5882 if (p->heredoc_indent > 0) {
5883 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5884 p->heredoc_indent = 0;
5885 }
5886 /*% ripper: string_literal!($:$) %*/
5887 }
5888 ;
5889
5890xstring : tXSTRING_BEG xstring_contents tSTRING_END
5891 {
5892 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5893 /*% ripper: $:2 %*/
5894 if (p->heredoc_indent > 0) {
5895 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5896 p->heredoc_indent = 0;
5897 }
5898 /*% ripper: xstring_literal!($:$) %*/
5899 }
5900 ;
5901
5902regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5903 {
5904 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5905 /*% ripper: regexp_literal!($:2, $:3) %*/
5906 }
5907 ;
5908
5909words : words(tWORDS_BEG, word_list)
5910 ;
5911
5912word_list : /* none */
5913 {
5914 $$ = 0;
5915 /*% ripper: words_new! %*/
5916 }
5917 | word_list word ' '+
5918 {
5919 $$ = list_append(p, $1, evstr2dstr(p, $2));
5920 /*% ripper: words_add!($:1, $:2) %*/
5921 }
5922 ;
5923
5924word : string_content
5925 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5926 | word string_content
5927 {
5928 $$ = literal_concat(p, $1, $2, &@$);
5929 /*% ripper: word_add!($:1, $:2) %*/
5930 }
5931 ;
5932
5933symbols : words(tSYMBOLS_BEG, symbol_list)
5934 ;
5935
5936symbol_list : /* none */
5937 {
5938 $$ = 0;
5939 /*% ripper: symbols_new! %*/
5940 }
5941 | symbol_list word ' '+
5942 {
5943 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5944 /*% ripper: symbols_add!($:1, $:2) %*/
5945 }
5946 ;
5947
5948qwords : words(tQWORDS_BEG, qword_list)
5949 ;
5950
5951qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5952 ;
5953
5954qword_list : /* none */
5955 {
5956 $$ = 0;
5957 /*% ripper: qwords_new! %*/
5958 }
5959 | qword_list tSTRING_CONTENT ' '+
5960 {
5961 $$ = list_append(p, $1, $2);
5962 /*% ripper: qwords_add!($:1, $:2) %*/
5963 }
5964 ;
5965
5966qsym_list : /* none */
5967 {
5968 $$ = 0;
5969 /*% ripper: qsymbols_new! %*/
5970 }
5971 | qsym_list tSTRING_CONTENT ' '+
5972 {
5973 $$ = symbol_append(p, $1, $2);
5974 /*% ripper: qsymbols_add!($:1, $:2) %*/
5975 }
5976 ;
5977
5978string_contents : /* none */
5979 {
5980 $$ = 0;
5981 /*% ripper: string_content! %*/
5982 }
5983 | string_contents string_content
5984 {
5985 $$ = literal_concat(p, $1, $2, &@$);
5986 /*% ripper: string_add!($:1, $:2) %*/
5987 }
5988 ;
5989
5990xstring_contents: /* none */
5991 {
5992 $$ = 0;
5993 /*% ripper: xstring_new! %*/
5994 }
5995 | xstring_contents string_content
5996 {
5997 $$ = literal_concat(p, $1, $2, &@$);
5998 /*% ripper: xstring_add!($:1, $:2) %*/
5999 }
6000 ;
6001
6002regexp_contents : /* none */
6003 {
6004 $$ = 0;
6005 /*% ripper: regexp_new! %*/
6006 }
6007 | regexp_contents string_content
6008 {
6009 NODE *head = $1, *tail = $2;
6010 if (!head) {
6011 $$ = tail;
6012 }
6013 else if (!tail) {
6014 $$ = head;
6015 }
6016 else {
6017 switch (nd_type(head)) {
6018 case NODE_STR:
6019 head = str2dstr(p, head);
6020 break;
6021 case NODE_DSTR:
6022 break;
6023 default:
6024 head = list_append(p, NEW_DSTR(0, &@$), head);
6025 break;
6026 }
6027 $$ = list_append(p, head, tail);
6028 }
6029 /*% ripper: regexp_add!($:1, $:2) %*/
6030 }
6031 ;
6032
6033string_content : tSTRING_CONTENT[content]
6034 /*% ripper[brace]: $:content %*/
6035 | tSTRING_DVAR[state]
6036 {
6037 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6038 $$ = p->lex.strterm;
6039 p->lex.strterm = 0;
6040 SET_LEX_STATE(EXPR_BEG);
6041 }[strterm]<strterm>
6042 string_dvar[dvar]
6043 {
6044 p->lex.strterm = $strterm;
6045 $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC);
6046 nd_set_line($$, @dvar.end_pos.lineno);
6047 /*% ripper: string_dvar!($:dvar) %*/
6048 }
6049 | tSTRING_DBEG[state]
6050 {
6051 CMDARG_PUSH(0);
6052 COND_PUSH(0);
6053 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6054 $$ = p->lex.strterm;
6055 p->lex.strterm = 0;
6056 SET_LEX_STATE(EXPR_BEG);
6057 }[term]<strterm>
6058 {
6059 $$ = p->lex.brace_nest;
6060 p->lex.brace_nest = 0;
6061 }[brace]<num>
6062 {
6063 $$ = p->lex.lpar_beg;
6064 p->lex.lpar_beg = -1;
6065 }[lpar]<num>
6066 {
6067 $$ = p->heredoc_indent;
6068 p->heredoc_indent = 0;
6069 }[indent]<num>
6070 compstmt(stmts) string_dend
6071 {
6072 COND_POP();
6073 CMDARG_POP();
6074 p->lex.strterm = $term;
6075 SET_LEX_STATE($state);
6076 p->lex.brace_nest = $brace;
6077 p->lex.lpar_beg = $lpar;
6078 p->heredoc_indent = $indent;
6079 p->heredoc_line_indent = -1;
6080 if ($compstmt) nd_unset_fl_newline($compstmt);
6081 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6082 /*% ripper: string_embexpr!($:compstmt) %*/
6083 }
6084 ;
6085
6086string_dend : tSTRING_DEND
6087 | END_OF_INPUT
6088 ;
6089
6090string_dvar : nonlocal_var
6091 {
6092 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6093 /*% ripper: var_ref!($:1) %*/
6094 }
6095 | backref
6096 ;
6097
6098symbol : ssym
6099 | dsym
6100 ;
6101
6102ssym : tSYMBEG sym
6103 {
6104 SET_LEX_STATE(EXPR_END);
6105 VALUE str = rb_id2str($2);
6106 /*
6107 * TODO:
6108 * set_yylval_noname sets invalid id to yylval.
6109 * This branch can be removed once yylval is changed to
6110 * hold lexed string.
6111 */
6112 if (!str) str = STR_NEW0();
6113 $$ = NEW_SYM(str, &@$);
6114 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6115 }
6116 ;
6117
6118sym : fname
6119 | nonlocal_var
6120 ;
6121
6122dsym : tSYMBEG string_contents tSTRING_END
6123 {
6124 SET_LEX_STATE(EXPR_END);
6125 $$ = dsym_node(p, $2, &@$);
6126 /*% ripper: dyna_symbol!($:2) %*/
6127 }
6128 ;
6129
6130numeric : simple_numeric
6131 | tUMINUS_NUM simple_numeric %prec tLOWEST
6132 {
6133 $$ = $2;
6134 negate_lit(p, $$, &@$);
6135 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6136 }
6137 ;
6138
6139simple_numeric : tINTEGER
6140 | tFLOAT
6141 | tRATIONAL
6142 | tIMAGINARY
6143 ;
6144
6145nonlocal_var : tIVAR
6146 | tGVAR
6147 | tCVAR
6148 ;
6149
6150user_variable : ident_or_const
6151 | nonlocal_var
6152 ;
6153
6154keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6155 | keyword_self {$$ = KWD2EID(self, $1);}
6156 | keyword_true {$$ = KWD2EID(true, $1);}
6157 | keyword_false {$$ = KWD2EID(false, $1);}
6158 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6159 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6160 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6161 ;
6162
6163var_ref : user_variable
6164 {
6165 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6166 if (ifdef_ripper(id_is_var(p, $1), false)) {
6167 /*% ripper: var_ref!($:1) %*/
6168 }
6169 else {
6170 /*% ripper: vcall!($:1) %*/
6171 }
6172 }
6173 | keyword_variable
6174 {
6175 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6176 /*% ripper: var_ref!($:1) %*/
6177 }
6178 ;
6179
6180var_lhs : user_or_keyword_variable
6181 {
6182 /*% ripper: var_field!($:1) %*/
6183 $$ = assignable(p, $1, 0, &@$);
6184 }
6185 ;
6186
6187backref : tNTH_REF
6188 | tBACK_REF
6189 ;
6190
6191superclass : '<'
6192 {
6193 SET_LEX_STATE(EXPR_BEG);
6194 p->command_start = TRUE;
6195 }
6196 expr_value term
6197 {
6198 $$ = $3;
6199 /*% ripper: $:3 %*/
6200 }
6201 | none
6202 ;
6203
6204f_opt_paren_args: f_paren_args
6205 | f_empty_arg
6206 {
6207 p->ctxt.in_argdef = 0;
6208 }
6209 ;
6210
6211f_empty_arg : /* none */
6212 {
6213 $$ = new_empty_args_tail(p, &@$);
6214 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6215 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6216 }
6217 ;
6218
6219f_paren_args : '(' f_args rparen
6220 {
6221 $$ = $2;
6222 /*% ripper: paren!($:2) %*/
6223 SET_LEX_STATE(EXPR_BEG);
6224 p->command_start = TRUE;
6225 p->ctxt.in_argdef = 0;
6226 }
6227 ;
6228
6229f_arglist : f_paren_args
6230 | {
6231 $$ = p->ctxt;
6232 p->ctxt.in_kwarg = 1;
6233 p->ctxt.in_argdef = 1;
6234 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6235 }<ctxt>
6236 f_args term
6237 {
6238 p->ctxt.in_kwarg = $1.in_kwarg;
6239 p->ctxt.in_argdef = 0;
6240 $$ = $2;
6241 SET_LEX_STATE(EXPR_BEG);
6242 p->command_start = TRUE;
6243 /*% ripper: $:2 %*/
6244 }
6245 ;
6246
6247args_tail : args_tail_basic(arg_value, opt_comma)
6248 | args_forward
6249 {
6250 add_forwarding_args(p);
6251 $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward);
6252 $$->nd_ainfo.forwarding = 1;
6253 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6254 }
6255 ;
6256
6257largs_tail : args_tail_basic(arg_value, none)
6258 | args_forward
6259 {
6260 yyerror1(&@args_forward, "unexpected ... in lambda argument");
6261 $$ = new_args_tail(p, 0, 0, 0, &@args_forward);
6262 $$->nd_ainfo.forwarding = 1;
6263 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6264 }
6265 ;
6266
6267%rule args-list(value, tail) <node_args>
6268 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6269 {
6270 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6271 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6272 }
6273 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6274 {
6275 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6276 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6277 }
6278 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6279 {
6280 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6281 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6282 }
6283 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6284 {
6285 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6286 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6287 }
6288 | f_arg[pre] ',' f_rest_arg[rest] tail
6289 {
6290 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6291 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6292 }
6293 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6294 {
6295 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6296 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6297 }
6298 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6299 {
6300 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6301 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6302 }
6303 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6304 {
6305 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6306 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6307 }
6308 | f_opt_arg(value)[opt] tail
6309 {
6310 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6311 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6312 }
6313 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6314 {
6315 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6316 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6317 }
6318 | f_rest_arg[rest] tail
6319 {
6320 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6321 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6322 }
6323 | f_rest_arg[rest] ',' f_arg[post] tail
6324 {
6325 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6326 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6327 }
6328 ;
6329
6330%rule tail-only-args(tail) <node_args>
6331 : tail
6332 {
6333 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6334 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6335 }
6336 ;
6337
6338%rule f_args-list(tail, trailing) <node_args>
6339 : args-list(arg_value, opt_args_tail(tail, trailing))
6340 | f_arg[pre] opt_args_tail(tail, trailing)[tail]
6341 {
6342 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6343 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6344 }
6345 | tail-only-args(tail)
6346 | f_empty_arg
6347 ;
6348
6349f_args : f_args-list(args_tail, opt_comma)
6350 ;
6351
6352f_largs : f_args-list(largs_tail, none)
6353 ;
6354
6355args_forward : tBDOT3
6356 {
6357 $$ = idFWD_KWREST;
6358 /*% ripper: args_forward! %*/
6359 }
6360 ;
6361
6362f_bad_arg : tCONSTANT
6363 {
6364 static const char mesg[] = "formal argument cannot be a constant";
6365 /*%%%*/
6366 yyerror1(&@1, mesg);
6367 /*% %*/
6368 $$ = 0;
6369 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6370 }
6371 | tIVAR
6372 {
6373 static const char mesg[] = "formal argument cannot be an instance variable";
6374 /*%%%*/
6375 yyerror1(&@1, mesg);
6376 /*% %*/
6377 $$ = 0;
6378 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6379 }
6380 | tGVAR
6381 {
6382 static const char mesg[] = "formal argument cannot be a global variable";
6383 /*%%%*/
6384 yyerror1(&@1, mesg);
6385 /*% %*/
6386 $$ = 0;
6387 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6388 }
6389 | tCVAR
6390 {
6391 static const char mesg[] = "formal argument cannot be a class variable";
6392 /*%%%*/
6393 yyerror1(&@1, mesg);
6394 /*% %*/
6395 $$ = 0;
6396 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6397 }
6398 ;
6399
6400f_norm_arg : f_bad_arg
6401 | tIDENTIFIER
6402 {
6403 VALUE e = formal_argument_error(p, $$ = $1);
6404 if (e) {
6405 /*% ripper[error]: param_error!(?e, $:1) %*/
6406 }
6407 p->max_numparam = ORDINAL_PARAM;
6408 }
6409 ;
6410
6411f_arg_asgn : f_norm_arg
6412 {
6413 arg_var(p, $1);
6414 $$ = $1;
6415 }
6416 ;
6417
6418f_arg_item : f_arg_asgn
6419 {
6420 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6421 /*% ripper: $:1 %*/
6422 }
6423 | tLPAREN f_margs rparen
6424 {
6425 ID tid = internal_id(p);
6426 YYLTYPE loc;
6427 loc.beg_pos = @2.beg_pos;
6428 loc.end_pos = @2.beg_pos;
6429 arg_var(p, tid);
6430 if (dyna_in_block(p)) {
6431 $2->nd_value = NEW_DVAR(tid, &loc);
6432 }
6433 else {
6434 $2->nd_value = NEW_LVAR(tid, &loc);
6435 }
6436 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6437 $$->nd_next = (NODE *)$2;
6438 /*% ripper: mlhs_paren!($:2) %*/
6439 }
6440 ;
6441
6442f_arg : f_arg_item
6443 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6444 | f_arg ',' f_arg_item
6445 {
6446 $$ = $1;
6447 $$->nd_plen++;
6448 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6449 rb_discard_node(p, (NODE *)$3);
6450 /*% ripper: rb_ary_push($:1, $:3) %*/
6451 }
6452 ;
6453
6454
6455f_label : tLABEL
6456 {
6457 VALUE e = formal_argument_error(p, $$ = $1);
6458 if (e) {
6459 $$ = 0;
6460 /*% ripper[error]: param_error!(?e, $:1) %*/
6461 }
6462 /*
6463 * Workaround for Prism::ParseTest#test_filepath for
6464 * "unparser/corpus/literal/def.txt"
6465 *
6466 * See the discussion on https://github.com/ruby/ruby/pull/9923
6467 */
6468 arg_var(p, ifdef_ripper(0, $1));
6469 /*% ripper: $:1 %*/
6470 p->max_numparam = ORDINAL_PARAM;
6471 p->ctxt.in_argdef = 0;
6472 }
6473 ;
6474
6475kwrest_mark : tPOW
6476 | tDSTAR
6477 ;
6478
6479f_no_kwarg : p_kwnorest
6480 {
6481 /*% ripper: nokw_param!(Qnil) %*/
6482 }
6483 ;
6484
6485f_kwrest : kwrest_mark tIDENTIFIER
6486 {
6487 arg_var(p, shadowing_lvar(p, $2));
6488 $$ = $2;
6489 /*% ripper: kwrest_param!($:2) %*/
6490 }
6491 | kwrest_mark
6492 {
6493 arg_var(p, idFWD_KWREST);
6494 $$ = idFWD_KWREST;
6495 /*% ripper: kwrest_param!(Qnil) %*/
6496 }
6497 ;
6498
6499restarg_mark : '*'
6500 | tSTAR
6501 ;
6502
6503f_rest_arg : restarg_mark tIDENTIFIER
6504 {
6505 arg_var(p, shadowing_lvar(p, $2));
6506 $$ = $2;
6507 /*% ripper: rest_param!($:2) %*/
6508 }
6509 | restarg_mark
6510 {
6511 arg_var(p, idFWD_REST);
6512 $$ = idFWD_REST;
6513 /*% ripper: rest_param!(Qnil) %*/
6514 }
6515 ;
6516
6517blkarg_mark : '&'
6518 | tAMPER
6519 ;
6520
6521f_block_arg : blkarg_mark tIDENTIFIER
6522 {
6523 arg_var(p, shadowing_lvar(p, $2));
6524 $$ = $2;
6525 /*% ripper: blockarg!($:2) %*/
6526 }
6527 | blkarg_mark keyword_nil
6528 {
6529 $$ = idNil;
6530 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6531 }
6532 | blkarg_mark
6533 {
6534 arg_var(p, idFWD_BLOCK);
6535 $$ = idFWD_BLOCK;
6536 /*% ripper: blockarg!(Qnil) %*/
6537 }
6538 ;
6539
6540opt_comma : ','?
6541 {
6542 $$ = 0;
6543 /*% ripper: Qnil %*/
6544 }
6545 ;
6546
6547
6548singleton : value_expr(singleton_expr)
6549 {
6550 NODE *expr = last_expr_node($1);
6551 switch (nd_type(expr)) {
6552 case NODE_STR:
6553 case NODE_DSTR:
6554 case NODE_XSTR:
6555 case NODE_DXSTR:
6556 case NODE_REGX:
6557 case NODE_DREGX:
6558 case NODE_SYM:
6559 case NODE_LINE:
6560 case NODE_FILE:
6561 case NODE_ENCODING:
6562 case NODE_INTEGER:
6563 case NODE_FLOAT:
6564 case NODE_RATIONAL:
6565 case NODE_IMAGINARY:
6566 case NODE_DSYM:
6567 case NODE_LIST:
6568 case NODE_ZLIST:
6569 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6570 break;
6571 default:
6572 break;
6573 }
6574 $$ = $1;
6575 }
6576 ;
6577
6578singleton_expr : var_ref
6579 | '('
6580 {
6581 SET_LEX_STATE(EXPR_BEG);
6582 p->ctxt.in_argdef = 0;
6583 }
6584 expr rparen
6585 {
6586 p->ctxt.in_argdef = 1;
6587 $$ = $3;
6588 /*% ripper: paren!($:3) %*/
6589 }
6590 ;
6591
6592assoc_list : none
6593 | assocs trailer
6594 {
6595 $$ = $1;
6596 /*% ripper: assoclist_from_args!($:1) %*/
6597 }
6598 ;
6599
6600assocs : assoc
6601 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6602 | assocs ',' assoc
6603 {
6604 NODE *assocs = $1;
6605 NODE *tail = $3;
6606 if (!assocs) {
6607 assocs = tail;
6608 }
6609 else if (tail) {
6610 NODE *n = RNODE_LIST(tail)->nd_next;
6611 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6612 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6613 /* DSTAR */
6614 tail = RNODE_HASH(n)->nd_head;
6615 }
6616 if (tail) {
6617 assocs = list_concat(assocs, tail);
6618 }
6619 }
6620 $$ = assocs;
6621 /*% ripper: rb_ary_push($:1, $:3) %*/
6622 }
6623 ;
6624
6625assoc : arg_value tASSOC arg_value
6626 {
6627 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6628 /*% ripper: assoc_new!($:1, $:3) %*/
6629 }
6630 | tLABEL arg_value
6631 {
6632 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6633 /*% ripper: assoc_new!($:1, $:2) %*/
6634 }
6635 | tLABEL
6636 {
6637 NODE *val = gettable(p, $1, &@$);
6638 if (!val) val = NEW_ERROR(&@$);
6639 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6640 /*% ripper: assoc_new!($:1, Qnil) %*/
6641 }
6642 | tSTRING_BEG string_contents tLABEL_END arg_value
6643 {
6644 YYLTYPE loc = code_loc_gen(&@1, &@3);
6645 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6646 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6647 }
6648 | tDSTAR arg_value
6649 {
6650 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6651 /*% ripper: assoc_splat!($:2) %*/
6652 }
6653 | tDSTAR
6654 {
6655 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6656 $$ = list_append(p, NEW_LIST(0, &@$),
6657 NEW_LVAR(idFWD_KWREST, &@$));
6658 /*% ripper: assoc_splat!(Qnil) %*/
6659 }
6660 ;
6661
6662%rule %inline operation : ident_or_const
6663 | tFID
6664 ;
6665
6666operation2 : operation
6667 | op
6668 ;
6669
6670operation3 : tIDENTIFIER
6671 | tFID
6672 | op
6673 ;
6674
6675dot_or_colon : '.'
6676 | tCOLON2
6677 ;
6678
6679call_op : '.'
6680 | tANDDOT
6681 ;
6682
6683call_op2 : call_op
6684 | tCOLON2
6685 ;
6686
6687rparen : '\n'? ')'
6688 ;
6689
6690rbracket : '\n'? ']'
6691 ;
6692
6693rbrace : '\n'? '}'
6694 ;
6695
6696trailer : '\n'?
6697 | ','
6698 ;
6699
6700term : ';'
6701 {
6702 yyerrok;
6703 token_flush(p);
6704 if (p->ctxt.in_defined) {
6705 p->ctxt.has_trailing_semicolon = 1;
6706 }
6707 }
6708 | '\n'
6709 {
6710 @$.end_pos = @$.beg_pos;
6711 token_flush(p);
6712 }
6713 ;
6714
6715terms : term
6716 | terms ';' {yyerrok;}
6717 ;
6718
6719none : /* none */
6720 {
6721 $$ = 0;
6722 /*% ripper: Qnil %*/
6723 }
6724 ;
6725%%
6726# undef p
6727# undef yylex
6728# undef yylval
6729# define yylval (*p->lval)
6730
6731static int regx_options(struct parser_params*);
6732static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6733static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6734static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6735static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6736
6737#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6738
6739# define set_yylval_node(x) { \
6740 YYLTYPE _cur_loc; \
6741 rb_parser_set_location(p, &_cur_loc); \
6742 yylval.node = (x); \
6743 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6744}
6745# define set_yylval_str(x) \
6746do { \
6747 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6748 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6749} while(0)
6750# define set_yylval_num(x) { \
6751 yylval.num = (x); \
6752 set_parser_s_value(x); \
6753}
6754# define set_yylval_id(x) (yylval.id = (x))
6755# define set_yylval_name(x) { \
6756 (yylval.id = (x)); \
6757 set_parser_s_value(ID2SYM(x)); \
6758}
6759# define yylval_id() (yylval.id)
6760
6761#define set_yylval_noname() set_yylval_id(keyword_nil)
6762#define has_delayed_token(p) (p->delayed.token != NULL)
6763
6764#ifndef RIPPER
6765#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6766#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6767
6768static bool
6769parser_has_token(struct parser_params *p)
6770{
6771 const char *const pcur = p->lex.pcur;
6772 const char *const ptok = p->lex.ptok;
6773 if (p->keep_tokens && (pcur < ptok)) {
6774 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6775 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6776 }
6777 return pcur > ptok;
6778}
6779
6780static const char *
6781escaped_char(int c)
6782{
6783 switch (c) {
6784 case '"': return "\\\"";
6785 case '\\': return "\\\\";
6786 case '\0': return "\\0";
6787 case '\n': return "\\n";
6788 case '\r': return "\\r";
6789 case '\t': return "\\t";
6790 case '\f': return "\\f";
6791 case '\013': return "\\v";
6792 case '\010': return "\\b";
6793 case '\007': return "\\a";
6794 case '\033': return "\\e";
6795 case '\x7f': return "\\c?";
6796 }
6797 return NULL;
6798}
6799
6800static rb_parser_string_t *
6801rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6802{
6803 rb_encoding *enc = p->enc;
6804 const char *ptr = str->ptr;
6805 const char *pend = ptr + str->len;
6806 const char *prev = ptr;
6807 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6808 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6809
6810 while (ptr < pend) {
6811 unsigned int c;
6812 const char *cc;
6813 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6814 if (!MBCLEN_CHARFOUND_P(n)) {
6815 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6816 n = rb_enc_mbminlen(enc);
6817 if (pend < ptr + n)
6818 n = (int)(pend - ptr);
6819 while (n--) {
6820 c = ((unsigned char)*ptr >> 4) & 0x0f;
6821 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6822 c = *ptr & 0x0f;
6823 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6824 parser_str_cat(result, charbuf, 4);
6825 prev = ++ptr;
6826 }
6827 continue;
6828 }
6829 n = MBCLEN_CHARFOUND_LEN(n);
6830 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6831 ptr += n;
6832 cc = escaped_char(c);
6833 if (cc) {
6834 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6835 parser_str_cat_cstr(result, cc);
6836 prev = ptr;
6837 }
6838 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6839 }
6840 else {
6841 if (ptr - n > prev) {
6842 parser_str_cat(result, prev, ptr - n - prev);
6843 prev = ptr - n;
6844 }
6845 parser_str_cat(result, prev, ptr - prev);
6846 prev = ptr;
6847 }
6848 }
6849 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6850
6851 return result;
6852}
6853
6854static void
6855parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6856{
6857 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6858 token->id = p->token_id;
6859 token->type_name = parser_token2char(p, t);
6860 token->str = str;
6861 token->loc.beg_pos = p->yylloc->beg_pos;
6862 token->loc.end_pos = p->yylloc->end_pos;
6863 rb_parser_ary_push_ast_token(p, p->tokens, token);
6864 p->token_id++;
6865
6866 if (p->debug) {
6867 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6868 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6869 line, token->id, token->type_name, str_escaped->ptr,
6870 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6871 token->loc.end_pos.lineno, token->loc.end_pos.column);
6872 rb_parser_string_free(p, str_escaped);
6873 }
6874}
6875
6876static void
6877parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6878{
6879 debug_token_line(p, "parser_dispatch_scan_event", line);
6880
6881 if (!parser_has_token(p)) return;
6882
6883 RUBY_SET_YYLLOC(*p->yylloc);
6884
6885 if (p->keep_tokens) {
6886 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6887 parser_append_tokens(p, str, t, line);
6888 }
6889
6890 token_flush(p);
6891}
6892
6893#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6894static void
6895parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6896{
6897 debug_token_line(p, "parser_dispatch_delayed_token", line);
6898
6899 if (!has_delayed_token(p)) return;
6900
6901 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6902
6903 if (p->keep_tokens) {
6904 /* p->delayed.token is freed by rb_parser_tokens_free */
6905 parser_append_tokens(p, p->delayed.token, t, line);
6906 }
6907 else {
6908 rb_parser_string_free(p, p->delayed.token);
6909 }
6910
6911 p->delayed.token = NULL;
6912}
6913#else
6914#define literal_flush(p, ptr) ((void)(ptr))
6915
6916static int
6917ripper_has_scan_event(struct parser_params *p)
6918{
6919 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6920 return p->lex.pcur > p->lex.ptok;
6921}
6922
6923static VALUE
6924ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6925{
6926 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6927 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6928 RUBY_SET_YYLLOC(*p->yylloc);
6929 token_flush(p);
6930 return rval;
6931}
6932
6933static void
6934ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6935{
6936 if (!ripper_has_scan_event(p)) return;
6937
6938 set_parser_s_value(ripper_scan_event_val(p, t));
6939}
6940#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6941
6942static void
6943ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6944{
6945 /* save and adjust the location to delayed token for callbacks */
6946 int saved_line = p->ruby_sourceline;
6947 const char *saved_tokp = p->lex.ptok;
6948 VALUE s_value, str;
6949
6950 if (!has_delayed_token(p)) return;
6951 p->ruby_sourceline = p->delayed.beg_line;
6952 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6953 str = rb_str_new_mutable_parser_string(p->delayed.token);
6954 rb_parser_string_free(p, p->delayed.token);
6955 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6956 set_parser_s_value(s_value);
6957 p->delayed.token = NULL;
6958 p->ruby_sourceline = saved_line;
6959 p->lex.ptok = saved_tokp;
6960}
6961#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6962#endif /* RIPPER */
6963
6964static inline int
6965is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6966{
6967 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6968}
6969
6970static inline bool
6971peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
6972{
6973 const char *ptr = p->lex.pcur + at;
6974 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
6975 if (memcmp(ptr, str, len)) return false;
6976 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
6977 switch (ptr[len]) {
6978 case '!': case '?': return false;
6979 }
6980 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
6981}
6982
6983static inline int
6984parser_is_identchar(struct parser_params *p)
6985{
6986 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6987}
6988
6989static inline int
6990parser_isascii(struct parser_params *p)
6991{
6992 return ISASCII(*(p->lex.pcur-1));
6993}
6994
6995static void
6996token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6997{
6998 int column = 1, nonspc = 0, i;
6999 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7000 if (*ptr == '\t') {
7001 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7002 }
7003 column++;
7004 if (*ptr != ' ' && *ptr != '\t') {
7005 nonspc = 1;
7006 }
7007 }
7008
7009 ptinfo->beg = loc->beg_pos;
7010 ptinfo->indent = column;
7011 ptinfo->nonspc = nonspc;
7012}
7013
7014static void
7015token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7016{
7017 token_info *ptinfo;
7018
7019 if (!p->token_info_enabled) return;
7020 ptinfo = ALLOC(token_info);
7021 ptinfo->token = token;
7022 ptinfo->next = p->token_info;
7023 token_info_setup(ptinfo, p->lex.pbeg, loc);
7024
7025 p->token_info = ptinfo;
7026}
7027
7028static void
7029token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7030{
7031 token_info *ptinfo_beg = p->token_info;
7032
7033 if (!ptinfo_beg) return;
7034
7035 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7036 token_info_warn(p, token, ptinfo_beg, 1, loc);
7037
7038 p->token_info = ptinfo_beg->next;
7039 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7040}
7041
7042static void
7043token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7044{
7045 token_info *ptinfo_beg = p->token_info;
7046
7047 if (!ptinfo_beg) return;
7048 p->token_info = ptinfo_beg->next;
7049
7050 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7051 ptinfo_beg->beg.column != beg_pos.column ||
7052 strcmp(ptinfo_beg->token, token)) {
7053 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7054 beg_pos.lineno, beg_pos.column, token,
7055 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7056 ptinfo_beg->token);
7057 }
7058
7059 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7060}
7061
7062static void
7063token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7064{
7065 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7066 if (!p->token_info_enabled) return;
7067 if (!ptinfo_beg) return;
7068 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7069 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7070 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7071 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7072 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7073 rb_warn3L(ptinfo_end->beg.lineno,
7074 "mismatched indentations at '%s' with '%s' at %d",
7075 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7076}
7077
7078static int
7079parser_precise_mbclen(struct parser_params *p, const char *ptr)
7080{
7081 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7082 if (!MBCLEN_CHARFOUND_P(len)) {
7083 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7084 return -1;
7085 }
7086 return len;
7087}
7088
7089#ifndef RIPPER
7090static inline void
7091parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7092{
7093 rb_parser_string_t *str;
7094 int lineno = p->ruby_sourceline;
7095 if (!yylloc) {
7096 return;
7097 }
7098 else if (yylloc->beg_pos.lineno == lineno) {
7099 str = p->lex.lastline;
7100 }
7101 else {
7102 return;
7103 }
7104 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7105}
7106
7107static int
7108parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7109{
7110#if 0
7111 YYLTYPE current;
7112
7113 if (!yylloc) {
7114 yylloc = RUBY_SET_YYLLOC(current);
7115 }
7116 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7117 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7118 yylloc = 0;
7119 }
7120#endif
7121 parser_compile_error(p, yylloc, "%s", msg);
7122 parser_show_error_line(p, yylloc);
7123 return 0;
7124}
7125
7126static int
7127parser_yyerror0(struct parser_params *p, const char *msg)
7128{
7129 YYLTYPE current;
7130 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7131}
7132
7133void
7134ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7135{
7136 VALUE mesg;
7137 const int max_line_margin = 30;
7138 const char *ptr, *ptr_end, *pt, *pb;
7139 const char *pre = "", *post = "", *pend;
7140 const char *code = "", *caret = "";
7141 const char *lim;
7142 const char *const pbeg = PARSER_STRING_PTR(str);
7143 char *buf;
7144 long len;
7145 int i;
7146
7147 if (!yylloc) return;
7148 pend = rb_parser_string_end(str);
7149 if (pend > pbeg && pend[-1] == '\n') {
7150 if (--pend > pbeg && pend[-1] == '\r') --pend;
7151 }
7152
7153 pt = pend;
7154 if (lineno == yylloc->end_pos.lineno &&
7155 (pend - pbeg) > yylloc->end_pos.column) {
7156 pt = pbeg + yylloc->end_pos.column;
7157 }
7158
7159 ptr = ptr_end = pt;
7160 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7161 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7162
7163 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7164 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7165
7166 len = ptr_end - ptr;
7167 if (len > 4) {
7168 if (ptr > pbeg) {
7169 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7170 if (ptr > pbeg) pre = "...";
7171 }
7172 if (ptr_end < pend) {
7173 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7174 if (ptr_end < pend) post = "...";
7175 }
7176 }
7177 pb = pbeg;
7178 if (lineno == yylloc->beg_pos.lineno) {
7179 pb += yylloc->beg_pos.column;
7180 if (pb > pt) pb = pt;
7181 }
7182 if (pb < ptr) pb = ptr;
7183 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7184 return;
7185 }
7186 if (RTEST(errbuf)) {
7187 mesg = rb_attr_get(errbuf, idMesg);
7188 if (char_at_end(p, mesg, '\n') != '\n')
7189 rb_str_cat_cstr(mesg, "\n");
7190 }
7191 else {
7192 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7193 }
7194 if (!errbuf && rb_stderr_tty_p()) {
7195#define CSI_BEGIN "\033["
7196#define CSI_SGR "m"
7197 rb_str_catf(mesg,
7198 CSI_BEGIN""CSI_SGR"%s" /* pre */
7199 CSI_BEGIN"1"CSI_SGR"%.*s"
7200 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7201 CSI_BEGIN";1"CSI_SGR"%.*s"
7202 CSI_BEGIN""CSI_SGR"%s" /* post */
7203 "\n",
7204 pre,
7205 (int)(pb - ptr), ptr,
7206 (int)(pt - pb), pb,
7207 (int)(ptr_end - pt), pt,
7208 post);
7209 }
7210 else {
7211 char *p2;
7212
7213 len = ptr_end - ptr;
7214 lim = pt < pend ? pt : pend;
7215 i = (int)(lim - ptr);
7216 buf = ALLOCA_N(char, i+2);
7217 code = ptr;
7218 caret = p2 = buf;
7219 if (ptr <= pb) {
7220 while (ptr < pb) {
7221 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7222 }
7223 *p2++ = '^';
7224 ptr++;
7225 }
7226 if (lim > ptr) {
7227 memset(p2, '~', (lim - ptr));
7228 p2 += (lim - ptr);
7229 }
7230 *p2 = '\0';
7231 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7232 pre, (int)len, code, post,
7233 pre, caret);
7234 }
7235 if (!errbuf) rb_write_error_str(mesg);
7236}
7237#else
7238
7239static int
7240parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7241{
7242 const char *pcur = 0, *ptok = 0;
7243 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7244 p->ruby_sourceline == yylloc->end_pos.lineno) {
7245 pcur = p->lex.pcur;
7246 ptok = p->lex.ptok;
7247 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7248 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7249 }
7250 parser_yyerror0(p, msg);
7251 if (pcur) {
7252 p->lex.ptok = ptok;
7253 p->lex.pcur = pcur;
7254 }
7255 return 0;
7256}
7257
7258static int
7259parser_yyerror0(struct parser_params *p, const char *msg)
7260{
7261 dispatch1(parse_error, STR_NEW2(msg));
7262 ripper_error(p);
7263 return 0;
7264}
7265
7266static inline void
7267parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7268{
7269}
7270#endif /* !RIPPER */
7271
7272static int
7273vtable_size(const struct vtable *tbl)
7274{
7275 if (!DVARS_TERMINAL_P(tbl)) {
7276 return tbl->pos;
7277 }
7278 else {
7279 return 0;
7280 }
7281}
7282
7283static struct vtable *
7284vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7285{
7286 struct vtable *tbl = ALLOC(struct vtable);
7287 tbl->pos = 0;
7288 tbl->capa = 8;
7289 tbl->tbl = ALLOC_N(ID, tbl->capa);
7290 tbl->prev = prev;
7291#ifndef RIPPER
7292 if (p->debug) {
7293 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7294 }
7295#endif
7296 return tbl;
7297}
7298#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7299
7300static void
7301vtable_free_gen(struct parser_params *p, int line, const char *name,
7302 struct vtable *tbl)
7303{
7304#ifndef RIPPER
7305 if (p->debug) {
7306 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7307 }
7308#endif
7309 if (!DVARS_TERMINAL_P(tbl)) {
7310 if (tbl->tbl) {
7311 ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID));
7312 }
7313 ruby_xfree_sized(tbl, sizeof(*tbl));
7314 }
7315}
7316#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7317
7318static void
7319vtable_add_gen(struct parser_params *p, int line, const char *name,
7320 struct vtable *tbl, ID id)
7321{
7322#ifndef RIPPER
7323 if (p->debug) {
7324 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7325 line, name, (void *)tbl, rb_id2name(id));
7326 }
7327#endif
7328 if (DVARS_TERMINAL_P(tbl)) {
7329 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7330 return;
7331 }
7332 if (tbl->pos == tbl->capa) {
7333 tbl->capa = tbl->capa * 2;
7334 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7335 }
7336 tbl->tbl[tbl->pos++] = id;
7337}
7338#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7339
7340static void
7341vtable_pop_gen(struct parser_params *p, int line, const char *name,
7342 struct vtable *tbl, int n)
7343{
7344 if (p->debug) {
7345 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7346 line, name, (void *)tbl, n);
7347 }
7348 if (tbl->pos < n) {
7349 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7350 return;
7351 }
7352 tbl->pos -= n;
7353}
7354#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7355
7356static int
7357vtable_included(const struct vtable * tbl, ID id)
7358{
7359 int i;
7360
7361 if (!DVARS_TERMINAL_P(tbl)) {
7362 for (i = 0; i < tbl->pos; i++) {
7363 if (tbl->tbl[i] == id) {
7364 return i+1;
7365 }
7366 }
7367 }
7368 return 0;
7369}
7370
7371static void parser_prepare(struct parser_params *p);
7372
7373static int
7374e_option_supplied(struct parser_params *p)
7375{
7376 return strcmp(p->ruby_sourcefile, "-e") == 0;
7377}
7378
7379#ifndef RIPPER
7380static NODE *parser_append_options(struct parser_params *p, NODE *node);
7381
7382static VALUE
7383yycompile0(VALUE arg)
7384{
7385 int n;
7386 NODE *tree;
7387 struct parser_params *p = (struct parser_params *)arg;
7388 int cov = FALSE;
7389
7390 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7391 cov = TRUE;
7392 }
7393
7394 if (p->debug_lines) {
7395 p->ast->body.script_lines = p->debug_lines;
7396 }
7397
7398 parser_prepare(p);
7399#define RUBY_DTRACE_PARSE_HOOK(name) \
7400 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7401 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7402 }
7403 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7404 n = yyparse(p);
7405 RUBY_DTRACE_PARSE_HOOK(END);
7406
7407 p->debug_lines = 0;
7408
7409 xfree(p->lex.strterm);
7410 p->lex.strterm = 0;
7411 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7412 if (n || p->error_p) {
7413 VALUE mesg = p->error_buffer;
7414 if (!mesg) {
7415 mesg = syntax_error_new();
7416 }
7417 if (!p->error_tolerant) {
7418 rb_set_errinfo(mesg);
7419 return FALSE;
7420 }
7421 }
7422 tree = p->eval_tree;
7423 if (!tree) {
7424 tree = NEW_NIL(&NULL_LOC);
7425 }
7426 else {
7427 rb_parser_ary_t *tokens = p->tokens;
7428 NODE *prelude;
7429 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7430 prelude = block_append(p, p->eval_tree_begin, body);
7431 RNODE_SCOPE(tree)->nd_body = prelude;
7432 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7433 p->ast->body.coverage_enabled = cov;
7434 if (p->keep_tokens) {
7435 p->ast->node_buffer->tokens = tokens;
7436 p->tokens = NULL;
7437 }
7438 }
7439 p->ast->body.root = tree;
7440 p->ast->body.line_count = p->line_count;
7441 return TRUE;
7442}
7443
7444static rb_ast_t *
7445yycompile(struct parser_params *p, VALUE fname, int line)
7446{
7447 rb_ast_t *ast;
7448 if (NIL_P(fname)) {
7449 p->ruby_sourcefile_string = Qnil;
7450 p->ruby_sourcefile = "(none)";
7451 }
7452 else {
7453 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7454 p->ruby_sourcefile = StringValueCStr(fname);
7455 }
7456 p->ruby_sourceline = line - 1;
7457
7458 p->lvtbl = NULL;
7459
7460 p->ast = ast = rb_ast_new();
7461 compile_callback(yycompile0, (VALUE)p);
7462 ast->body.source_hash = rb_source_hash_finalize(&p->source_hash);
7463 ast->body.has_source_hash = 1;
7464 p->ast = 0;
7465
7466 while (p->lvtbl) {
7467 local_pop(p);
7468 }
7469
7470 return ast;
7471}
7472#endif /* !RIPPER */
7473
7474static rb_encoding *
7475must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7476{
7477 rb_encoding *enc = rb_parser_str_get_encoding(s);
7478 if (!rb_enc_asciicompat(enc)) {
7479 rb_raise(rb_eArgError, "invalid source encoding");
7480 }
7481 return enc;
7482}
7483
7484static rb_parser_string_t *
7485lex_getline(struct parser_params *p)
7486{
7487 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7488 if (!line) return 0;
7489 p->line_count++;
7490 rb_source_hash_update(&p->source_hash, (const uint8_t *)line->ptr, (size_t)line->len);
7491 string_buffer_append(p, line);
7492 must_be_ascii_compatible(p, line);
7493 return line;
7494}
7495
7496#ifndef RIPPER
7497rb_ast_t*
7498rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7499{
7500 p->lex.gets = gets;
7501 p->lex.input = input;
7502 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7503
7504 return yycompile(p, fname, line);
7505}
7506#endif /* !RIPPER */
7507
7508#define STR_FUNC_ESCAPE 0x01
7509#define STR_FUNC_EXPAND 0x02
7510#define STR_FUNC_REGEXP 0x04
7511#define STR_FUNC_QWORDS 0x08
7512#define STR_FUNC_SYMBOL 0x10
7513#define STR_FUNC_INDENT 0x20
7514#define STR_FUNC_LABEL 0x40
7515#define STR_FUNC_LIST 0x4000
7516#define STR_FUNC_TERM 0x8000
7517
7518enum string_type {
7519 str_label = STR_FUNC_LABEL,
7520 str_squote = (0),
7521 str_dquote = (STR_FUNC_EXPAND),
7522 str_xquote = (STR_FUNC_EXPAND),
7523 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7524 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7525 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7526 str_ssym = (STR_FUNC_SYMBOL),
7527 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7528};
7529
7530static rb_parser_string_t *
7531parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7532{
7533 rb_parser_string_t *pstr;
7534
7535 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7536
7537 if (!(func & STR_FUNC_REGEXP)) {
7538 if (rb_parser_is_ascii_string(p, pstr)) {
7539 }
7540 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7541 /* everything is valid in ASCII-8BIT */
7542 enc = rb_ascii8bit_encoding();
7543 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7544 }
7545 }
7546
7547 return pstr;
7548}
7549
7550static int
7551strterm_is_heredoc(rb_strterm_t *strterm)
7552{
7553 return strterm->heredoc;
7554}
7555
7556static rb_strterm_t *
7557new_strterm(struct parser_params *p, int func, int term, int paren)
7558{
7559 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7560 strterm->u.literal.func = func;
7561 strterm->u.literal.term = term;
7562 strterm->u.literal.paren = paren;
7563 return strterm;
7564}
7565
7566static rb_strterm_t *
7567new_heredoc(struct parser_params *p)
7568{
7569 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7570 strterm->heredoc = true;
7571 return strterm;
7572}
7573
7574#define peek(p,c) peek_n(p, (c), 0)
7575#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7576#define peekc(p) peekc_n(p, 0)
7577#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7578
7579#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7580static void
7581parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7582{
7583 debug_token_line(p, "add_delayed_token", line);
7584
7585 if (tok < end) {
7586 if (has_delayed_token(p)) {
7587 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7588 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7589 int end_col = (next_line ? 0 : p->delayed.end_col);
7590 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7591 dispatch_delayed_token(p, tSTRING_CONTENT);
7592 }
7593 }
7594 if (!has_delayed_token(p)) {
7595 p->delayed.token = rb_parser_string_new(p, 0, 0);
7596 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7597 p->delayed.beg_line = p->ruby_sourceline;
7598 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7599 }
7600 parser_str_cat(p->delayed.token, tok, end - tok);
7601 p->delayed.end_line = p->ruby_sourceline;
7602 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7603 p->lex.ptok = end;
7604 }
7605}
7606
7607static void
7608set_lastline(struct parser_params *p, rb_parser_string_t *str)
7609{
7610 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7611 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7612 p->lex.lastline = str;
7613}
7614
7615static int
7616nextline(struct parser_params *p, int set_encoding)
7617{
7618 rb_parser_string_t *str = p->lex.nextline;
7619 p->lex.nextline = 0;
7620 if (!str) {
7621 if (p->eofp)
7622 return -1;
7623
7624 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7625 goto end_of_input;
7626 }
7627
7628 if (!p->lex.input || !(str = lex_getline(p))) {
7629 end_of_input:
7630 p->eofp = 1;
7631 lex_goto_eol(p);
7632 return -1;
7633 }
7634#ifndef RIPPER
7635 if (p->debug_lines) {
7636 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7637 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7638 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7639 }
7640#endif
7641 p->cr_seen = FALSE;
7642 }
7643 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7644 /* after here-document without terminator */
7645 goto end_of_input;
7646 }
7647 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7648 if (p->heredoc_end > 0) {
7649 p->ruby_sourceline = p->heredoc_end;
7650 p->heredoc_end = 0;
7651 }
7652 p->ruby_sourceline++;
7653 set_lastline(p, str);
7654 token_flush(p);
7655 return 0;
7656}
7657
7658static int
7659parser_cr(struct parser_params *p, int c)
7660{
7661 if (peek(p, '\n')) {
7662 p->lex.pcur++;
7663 c = '\n';
7664 }
7665 return c;
7666}
7667
7668static inline int
7669nextc0(struct parser_params *p, int set_encoding)
7670{
7671 int c;
7672
7673 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7674 if (nextline(p, set_encoding)) return -1;
7675 }
7676 c = (unsigned char)*p->lex.pcur++;
7677 if (UNLIKELY(c == '\r')) {
7678 c = parser_cr(p, c);
7679 }
7680
7681 return c;
7682}
7683#define nextc(p) nextc0(p, TRUE)
7684
7685static void
7686pushback(struct parser_params *p, int c)
7687{
7688 if (c == -1) return;
7689 p->eofp = 0;
7690 p->lex.pcur--;
7691 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7692 p->lex.pcur--;
7693 }
7694}
7695
7696#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7697
7698#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7699#define tok(p) (p)->tokenbuf
7700#define toklen(p) (p)->tokidx
7701
7702static int
7703looking_at_eol_p(struct parser_params *p)
7704{
7705 const char *ptr = p->lex.pcur;
7706 while (!lex_eol_ptr_p(p, ptr)) {
7707 int c = (unsigned char)*ptr++;
7708 int eol = (c == '\n' || c == '#');
7709 if (eol || !ISSPACE(c)) {
7710 return eol;
7711 }
7712 }
7713 return TRUE;
7714}
7715
7716static char*
7717newtok(struct parser_params *p)
7718{
7719 p->tokidx = 0;
7720 if (!p->tokenbuf) {
7721 p->toksiz = 60;
7722 p->tokenbuf = ALLOC_N(char, 60);
7723 }
7724 if (p->toksiz > 4096) {
7725 p->toksiz = 60;
7726 REALLOC_N(p->tokenbuf, char, 60);
7727 }
7728 return p->tokenbuf;
7729}
7730
7731static char *
7732tokspace(struct parser_params *p, int n)
7733{
7734 p->tokidx += n;
7735
7736 if (p->tokidx >= p->toksiz) {
7737 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7738 REALLOC_N(p->tokenbuf, char, p->toksiz);
7739 }
7740 return &p->tokenbuf[p->tokidx-n];
7741}
7742
7743static void
7744tokadd(struct parser_params *p, int c)
7745{
7746 p->tokenbuf[p->tokidx++] = (char)c;
7747 if (p->tokidx >= p->toksiz) {
7748 p->toksiz *= 2;
7749 REALLOC_N(p->tokenbuf, char, p->toksiz);
7750 }
7751}
7752
7753static int
7754tok_hex(struct parser_params *p, size_t *numlen)
7755{
7756 int c;
7757
7758 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7759 if (!*numlen) {
7760 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7761 yyerror0("invalid hex escape");
7762 dispatch_scan_event(p, tSTRING_CONTENT);
7763 return 0;
7764 }
7765 p->lex.pcur += *numlen;
7766 return c;
7767}
7768
7769#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7770
7771static int
7772escaped_control_code(int c)
7773{
7774 int c2 = 0;
7775 switch (c) {
7776 case ' ':
7777 c2 = 's';
7778 break;
7779 case '\n':
7780 c2 = 'n';
7781 break;
7782 case '\t':
7783 c2 = 't';
7784 break;
7785 case '\v':
7786 c2 = 'v';
7787 break;
7788 case '\r':
7789 c2 = 'r';
7790 break;
7791 case '\f':
7792 c2 = 'f';
7793 break;
7794 }
7795 return c2;
7796}
7797
7798#define WARN_SPACE_CHAR(c, prefix) \
7799 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7800
7801static int
7802tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7803 int regexp_literal, const char *begin)
7804{
7805 const int wide = !begin;
7806 size_t numlen;
7807 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7808
7809 p->lex.pcur += numlen;
7810 if (p->lex.strterm == NULL ||
7811 strterm_is_heredoc(p->lex.strterm) ||
7812 (p->lex.strterm->u.literal.func != str_regexp)) {
7813 if (!begin) begin = p->lex.pcur;
7814 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7815 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7816 yyerror0("invalid Unicode escape");
7817 dispatch_scan_event(p, tSTRING_CONTENT);
7818 return wide && numlen > 0;
7819 }
7820 if (codepoint > 0x10ffff) {
7821 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7822 yyerror0("invalid Unicode codepoint (too large)");
7823 dispatch_scan_event(p, tSTRING_CONTENT);
7824 return wide;
7825 }
7826 if ((codepoint & 0xfffff800) == 0xd800) {
7827 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7828 yyerror0("invalid Unicode codepoint");
7829 dispatch_scan_event(p, tSTRING_CONTENT);
7830 return wide;
7831 }
7832 }
7833 if (regexp_literal) {
7834 tokcopy(p, (int)numlen);
7835 }
7836 else if (codepoint >= 0x80) {
7837 rb_encoding *utf8 = rb_utf8_encoding();
7838 if (*encp && utf8 != *encp) {
7839 YYLTYPE loc = RUBY_INIT_YYLLOC();
7840 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7841 parser_show_error_line(p, &loc);
7842 return wide;
7843 }
7844 *encp = utf8;
7845 tokaddmbc(p, codepoint, *encp);
7846 }
7847 else {
7848 tokadd(p, codepoint);
7849 }
7850 return TRUE;
7851}
7852
7853static int tokadd_mbchar(struct parser_params *p, int c);
7854
7855static int
7856tokskip_mbchar(struct parser_params *p)
7857{
7858 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7859 if (len > 0) {
7860 p->lex.pcur += len - 1;
7861 }
7862 return len;
7863}
7864
7865/* return value is for ?\u3042 */
7866static void
7867tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7868 int term, int symbol_literal, int regexp_literal)
7869{
7870 /*
7871 * If `term` is not -1, then we allow multiple codepoints in \u{}
7872 * upto `term` byte, otherwise we're parsing a character literal.
7873 * And then add the codepoints to the current token.
7874 */
7875 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7876
7877 const int open_brace = '{', close_brace = '}';
7878
7879 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7880
7881 if (peek(p, open_brace)) { /* handle \u{...} form */
7882 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7883 /*
7884 * Skip parsing validation code and copy bytes as-is until term or
7885 * closing brace, in order to correctly handle extended regexps where
7886 * invalid unicode escapes are allowed in comments. The regexp parser
7887 * does its own validation and will catch any issues.
7888 */
7889 tokadd(p, open_brace);
7890 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7891 int c = peekc(p);
7892 if (c == close_brace) {
7893 tokadd(p, c);
7894 ++p->lex.pcur;
7895 break;
7896 }
7897 else if (c == term) {
7898 break;
7899 }
7900 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7901 tokadd(p, c);
7902 c = *++p->lex.pcur;
7903 }
7904 tokadd_mbchar(p, c);
7905 }
7906 }
7907 else {
7908 const char *second = NULL;
7909 int c, last = nextc(p);
7910 if (lex_eol_p(p)) goto unterminated;
7911 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7912 while (c != close_brace) {
7913 if (c == term) goto unterminated;
7914 if (second == multiple_codepoints)
7915 second = p->lex.pcur;
7916 if (regexp_literal) tokadd(p, last);
7917 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7918 break;
7919 }
7920 while (ISSPACE(c = peekc(p))) {
7921 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7922 last = c;
7923 }
7924 if (term == -1 && !second)
7925 second = multiple_codepoints;
7926 }
7927
7928 if (c != close_brace) {
7929 unterminated:
7930 flush_string_content(p, rb_utf8_encoding(), 0);
7931 yyerror0("unterminated Unicode escape");
7932 dispatch_scan_event(p, tSTRING_CONTENT);
7933 return;
7934 }
7935 if (second && second != multiple_codepoints) {
7936 const char *pcur = p->lex.pcur;
7937 p->lex.pcur = second;
7938 dispatch_scan_event(p, tSTRING_CONTENT);
7939 token_flush(p);
7940 p->lex.pcur = pcur;
7941 yyerror0(multiple_codepoints);
7942 token_flush(p);
7943 }
7944
7945 if (regexp_literal) tokadd(p, close_brace);
7946 nextc(p);
7947 }
7948 }
7949 else { /* handle \uxxxx form */
7950 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7951 token_flush(p);
7952 return;
7953 }
7954 }
7955}
7956
7957#define ESCAPE_CONTROL 1
7958#define ESCAPE_META 2
7959
7960static int
7961read_escape(struct parser_params *p, int flags, const char *begin)
7962{
7963 int c;
7964 size_t numlen;
7965
7966 switch (c = nextc(p)) {
7967 case '\\': /* Backslash */
7968 return c;
7969
7970 case 'n': /* newline */
7971 return '\n';
7972
7973 case 't': /* horizontal tab */
7974 return '\t';
7975
7976 case 'r': /* carriage-return */
7977 return '\r';
7978
7979 case 'f': /* form-feed */
7980 return '\f';
7981
7982 case 'v': /* vertical tab */
7983 return '\13';
7984
7985 case 'a': /* alarm(bell) */
7986 return '\007';
7987
7988 case 'e': /* escape */
7989 return 033;
7990
7991 case '0': case '1': case '2': case '3': /* octal constant */
7992 case '4': case '5': case '6': case '7':
7993 pushback(p, c);
7994 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
7995 p->lex.pcur += numlen;
7996 return c;
7997
7998 case 'x': /* hex constant */
7999 c = tok_hex(p, &numlen);
8000 if (numlen == 0) return 0;
8001 return c;
8002
8003 case 'b': /* backspace */
8004 return '\010';
8005
8006 case 's': /* space */
8007 return ' ';
8008
8009 case 'M':
8010 if (flags & ESCAPE_META) goto eof;
8011 if ((c = nextc(p)) != '-') {
8012 goto eof;
8013 }
8014 if ((c = nextc(p)) == '\\') {
8015 switch (peekc(p)) {
8016 case 'u': case 'U':
8017 nextc(p);
8018 goto eof;
8019 }
8020 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8021 }
8022 else if (c == -1) goto eof;
8023 else if (!ISASCII(c)) {
8024 tokskip_mbchar(p);
8025 goto eof;
8026 }
8027 else {
8028 int c2 = escaped_control_code(c);
8029 if (c2) {
8030 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8031 WARN_SPACE_CHAR(c2, "\\M-");
8032 }
8033 else {
8034 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8035 }
8036 }
8037 else if (ISCNTRL(c)) goto eof;
8038 return ((c & 0xff) | 0x80);
8039 }
8040
8041 case 'C':
8042 if ((c = nextc(p)) != '-') {
8043 goto eof;
8044 }
8045 case 'c':
8046 if (flags & ESCAPE_CONTROL) goto eof;
8047 if ((c = nextc(p))== '\\') {
8048 switch (peekc(p)) {
8049 case 'u': case 'U':
8050 nextc(p);
8051 goto eof;
8052 }
8053 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8054 }
8055 else if (c == '?')
8056 return 0177;
8057 else if (c == -1) goto eof;
8058 else if (!ISASCII(c)) {
8059 tokskip_mbchar(p);
8060 goto eof;
8061 }
8062 else {
8063 int c2 = escaped_control_code(c);
8064 if (c2) {
8065 if (ISCNTRL(c)) {
8066 if (flags & ESCAPE_META) {
8067 WARN_SPACE_CHAR(c2, "\\M-");
8068 }
8069 else {
8070 WARN_SPACE_CHAR(c2, "");
8071 }
8072 }
8073 else {
8074 if (flags & ESCAPE_META) {
8075 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8076 }
8077 else {
8078 WARN_SPACE_CHAR(c2, "\\C-");
8079 }
8080 }
8081 }
8082 else if (ISCNTRL(c)) goto eof;
8083 }
8084 return c & 0x9f;
8085
8086 eof:
8087 case -1:
8088 flush_string_content(p, p->enc, p->lex.pcur - begin);
8089 yyerror0("Invalid escape character syntax");
8090 dispatch_scan_event(p, tSTRING_CONTENT);
8091 return '\0';
8092
8093 default:
8094 if (!ISASCII(c)) {
8095 tokskip_mbchar(p);
8096 goto eof;
8097 }
8098 return c;
8099 }
8100}
8101
8102static void
8103tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8104{
8105 int len = rb_enc_codelen(c, enc);
8106 rb_enc_mbcput(c, tokspace(p, len), enc);
8107}
8108
8109static int
8110tokadd_escape(struct parser_params *p)
8111{
8112 int c;
8113 size_t numlen;
8114 const char *begin = p->lex.pcur;
8115
8116 switch (c = nextc(p)) {
8117 case '\n':
8118 return 0; /* just ignore */
8119
8120 case '0': case '1': case '2': case '3': /* octal constant */
8121 case '4': case '5': case '6': case '7':
8122 {
8123 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8124 if (numlen == 0) goto eof;
8125 p->lex.pcur += numlen;
8126 tokcopy(p, (int)numlen + 1);
8127 }
8128 return 0;
8129
8130 case 'x': /* hex constant */
8131 {
8132 tok_hex(p, &numlen);
8133 if (numlen == 0) return -1;
8134 tokcopy(p, (int)numlen + 2);
8135 }
8136 return 0;
8137
8138 eof:
8139 case -1:
8140 flush_string_content(p, p->enc, p->lex.pcur - begin);
8141 yyerror0("Invalid escape character syntax");
8142 token_flush(p);
8143 return -1;
8144
8145 default:
8146 tokadd(p, '\\');
8147 tokadd(p, c);
8148 }
8149 return 0;
8150}
8151
8152static int
8153char_to_option(int c)
8154{
8155 int val;
8156
8157 switch (c) {
8158 case 'i':
8159 val = RE_ONIG_OPTION_IGNORECASE;
8160 break;
8161 case 'x':
8162 val = RE_ONIG_OPTION_EXTEND;
8163 break;
8164 case 'm':
8165 val = RE_ONIG_OPTION_MULTILINE;
8166 break;
8167 default:
8168 val = 0;
8169 break;
8170 }
8171 return val;
8172}
8173
8174#define ARG_ENCODING_FIXED 16
8175#define ARG_ENCODING_NONE 32
8176#define ENC_ASCII8BIT 1
8177#define ENC_EUC_JP 2
8178#define ENC_Windows_31J 3
8179#define ENC_UTF8 4
8180
8181static int
8182char_to_option_kcode(int c, int *option, int *kcode)
8183{
8184 *option = 0;
8185
8186 switch (c) {
8187 case 'n':
8188 *kcode = ENC_ASCII8BIT;
8189 return (*option = ARG_ENCODING_NONE);
8190 case 'e':
8191 *kcode = ENC_EUC_JP;
8192 break;
8193 case 's':
8194 *kcode = ENC_Windows_31J;
8195 break;
8196 case 'u':
8197 *kcode = ENC_UTF8;
8198 break;
8199 default:
8200 *kcode = -1;
8201 return (*option = char_to_option(c));
8202 }
8203 *option = ARG_ENCODING_FIXED;
8204 return 1;
8205}
8206
8207static int
8208regx_options(struct parser_params *p)
8209{
8210 int kcode = 0;
8211 int kopt = 0;
8212 int options = 0;
8213 int c, opt, kc;
8214
8215 newtok(p);
8216 while (c = nextc(p), ISALPHA(c)) {
8217 if (c == 'o') {
8218 options |= RE_OPTION_ONCE;
8219 }
8220 else if (char_to_option_kcode(c, &opt, &kc)) {
8221 if (kc >= 0) {
8222 if (kc != ENC_ASCII8BIT) kcode = c;
8223 kopt = opt;
8224 }
8225 else {
8226 options |= opt;
8227 }
8228 }
8229 else {
8230 tokadd(p, c);
8231 }
8232 }
8233 options |= kopt;
8234 pushback(p, c);
8235 if (toklen(p)) {
8236 YYLTYPE loc = RUBY_INIT_YYLLOC();
8237 tokfix(p);
8238 compile_error(p, "unknown regexp option%s - %*s",
8239 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8240 parser_show_error_line(p, &loc);
8241 }
8242 return options | RE_OPTION_ENCODING(kcode);
8243}
8244
8245static int
8246tokadd_mbchar(struct parser_params *p, int c)
8247{
8248 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8249 if (len < 0) return -1;
8250 tokadd(p, c);
8251 p->lex.pcur += --len;
8252 if (len > 0) tokcopy(p, len);
8253 return c;
8254}
8255
8256static inline int
8257simple_re_meta(int c)
8258{
8259 switch (c) {
8260 case '$': case '*': case '+': case '.':
8261 case '?': case '^': case '|':
8262 case ')': case ']': case '}': case '>':
8263 return TRUE;
8264 default:
8265 return FALSE;
8266 }
8267}
8268
8269static int
8270parser_update_heredoc_indent(struct parser_params *p, int c)
8271{
8272 if (p->heredoc_line_indent == -1) {
8273 if (c == '\n') p->heredoc_line_indent = 0;
8274 }
8275 else {
8276 if (c == ' ') {
8277 p->heredoc_line_indent++;
8278 return TRUE;
8279 }
8280 else if (c == '\t') {
8281 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8282 p->heredoc_line_indent = w * TAB_WIDTH;
8283 return TRUE;
8284 }
8285 else if (c != '\n') {
8286 if (p->heredoc_indent > p->heredoc_line_indent) {
8287 p->heredoc_indent = p->heredoc_line_indent;
8288 }
8289 p->heredoc_line_indent = -1;
8290 }
8291 else {
8292 /* Whitespace only line has no indentation */
8293 p->heredoc_line_indent = 0;
8294 }
8295 }
8296 return FALSE;
8297}
8298
8299static void
8300parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8301{
8302 YYLTYPE loc = RUBY_INIT_YYLLOC();
8303 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8304 compile_error(p, "%s mixed within %s source", n1, n2);
8305 parser_show_error_line(p, &loc);
8306}
8307
8308static void
8309parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8310{
8311 const char *pos = p->lex.pcur;
8312 p->lex.pcur = beg;
8313 parser_mixed_error(p, enc1, enc2);
8314 p->lex.pcur = pos;
8315}
8316
8317static inline char
8318nibble_char_upper(unsigned int c)
8319{
8320 c &= 0xf;
8321 return c + (c < 10 ? '0' : 'A' - 10);
8322}
8323
8324static int
8325tokadd_string(struct parser_params *p,
8326 int func, int term, int paren, long *nest,
8327 rb_encoding **encp, rb_encoding **enc)
8328{
8329 int c;
8330 bool erred = false;
8331#ifdef RIPPER
8332 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8333 int top_of_line = FALSE;
8334#endif
8335
8336#define mixed_error(enc1, enc2) \
8337 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8338#define mixed_escape(beg, enc1, enc2) \
8339 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8340
8341 while ((c = nextc(p)) != -1) {
8342 if (p->heredoc_indent > 0) {
8343 parser_update_heredoc_indent(p, c);
8344 }
8345#ifdef RIPPER
8346 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8347 pushback(p, c);
8348 break;
8349 }
8350#endif
8351
8352 if (paren && c == paren) {
8353 ++*nest;
8354 }
8355 else if (c == term) {
8356 if (!nest || !*nest) {
8357 pushback(p, c);
8358 break;
8359 }
8360 --*nest;
8361 }
8362 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8363 unsigned char c2 = *p->lex.pcur;
8364 if (c2 == '$' || c2 == '@' || c2 == '{') {
8365 pushback(p, c);
8366 break;
8367 }
8368 }
8369 else if (c == '\\') {
8370 c = nextc(p);
8371 switch (c) {
8372 case '\n':
8373 if (func & STR_FUNC_QWORDS) break;
8374 if (func & STR_FUNC_EXPAND) {
8375 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8376 continue;
8377 if (c == term) {
8378 c = '\\';
8379 goto terminate;
8380 }
8381 }
8382 tokadd(p, '\\');
8383 break;
8384
8385 case '\\':
8386 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8387 break;
8388
8389 case 'u':
8390 if ((func & STR_FUNC_EXPAND) == 0) {
8391 tokadd(p, '\\');
8392 break;
8393 }
8394 tokadd_utf8(p, enc, term,
8395 func & STR_FUNC_SYMBOL,
8396 func & STR_FUNC_REGEXP);
8397 continue;
8398
8399 default:
8400 if (c == -1) return -1;
8401 if (!ISASCII(c)) {
8402 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8403 goto non_ascii;
8404 }
8405 if (func & STR_FUNC_REGEXP) {
8406 switch (c) {
8407 case 'c':
8408 case 'C':
8409 case 'M': {
8410 pushback(p, c);
8411 c = read_escape(p, 0, p->lex.pcur - 1);
8412
8413 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8414 *t++ = '\\';
8415 *t++ = 'x';
8416 *t++ = nibble_char_upper(c >> 4);
8417 *t++ = nibble_char_upper(c);
8418 continue;
8419 }
8420 }
8421
8422 if (c == term && !simple_re_meta(c)) {
8423 tokadd(p, c);
8424 continue;
8425 }
8426 pushback(p, c);
8427 if ((c = tokadd_escape(p)) < 0)
8428 return -1;
8429 if (*enc && *enc != *encp) {
8430 mixed_escape(p->lex.ptok+2, *enc, *encp);
8431 }
8432 continue;
8433 }
8434 else if (func & STR_FUNC_EXPAND) {
8435 pushback(p, c);
8436 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8437 c = read_escape(p, 0, p->lex.pcur - 1);
8438 }
8439 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8440 /* ignore backslashed spaces in %w */
8441 }
8442 else if (c != term && !(paren && c == paren)) {
8443 tokadd(p, '\\');
8444 pushback(p, c);
8445 continue;
8446 }
8447 }
8448 }
8449 else if (!parser_isascii(p)) {
8450 non_ascii:
8451 if (!*enc) {
8452 *enc = *encp;
8453 }
8454 else if (*enc != *encp) {
8455 mixed_error(*enc, *encp);
8456 continue;
8457 }
8458 if (tokadd_mbchar(p, c) == -1) return -1;
8459 continue;
8460 }
8461 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8462 pushback(p, c);
8463 break;
8464 }
8465 if (c & 0x80) {
8466 if (!*enc) {
8467 *enc = *encp;
8468 }
8469 else if (*enc != *encp) {
8470 mixed_error(*enc, *encp);
8471 continue;
8472 }
8473 }
8474 tokadd(p, c);
8475#ifdef RIPPER
8476 top_of_line = (c == '\n');
8477#endif
8478 }
8479 terminate:
8480 if (*enc) *encp = *enc;
8481 return c;
8482}
8483
8484#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8485
8486static void
8487flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8488{
8489 p->lex.pcur -= back;
8490 if (has_delayed_token(p)) {
8491 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8492 if (len > 0) {
8493 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8494 p->delayed.end_line = p->ruby_sourceline;
8495 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8496 }
8497 dispatch_delayed_token(p, tSTRING_CONTENT);
8498 p->lex.ptok = p->lex.pcur;
8499 }
8500 dispatch_scan_event(p, tSTRING_CONTENT);
8501 p->lex.pcur += back;
8502}
8503
8504/* this can be shared with ripper, since it's independent from struct
8505 * parser_params. */
8506#ifndef RIPPER
8507#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8508#define SPECIAL_PUNCT(idx) ( \
8509 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8510 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8511 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8512 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8513 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8514 BIT('0', idx))
8515const uint_least32_t ruby_global_name_punct_bits[] = {
8516 SPECIAL_PUNCT(0),
8517 SPECIAL_PUNCT(1),
8518 SPECIAL_PUNCT(2),
8519};
8520#undef BIT
8521#undef SPECIAL_PUNCT
8522#endif
8523
8524static enum yytokentype
8525parser_peek_variable_name(struct parser_params *p)
8526{
8527 int c;
8528 const char *ptr = p->lex.pcur;
8529
8530 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8531 c = *ptr++;
8532 switch (c) {
8533 case '$':
8534 if ((c = *ptr) == '-') {
8535 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8536 c = *ptr;
8537 }
8538 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8539 return tSTRING_DVAR;
8540 }
8541 break;
8542 case '@':
8543 if ((c = *ptr) == '@') {
8544 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8545 c = *ptr;
8546 }
8547 break;
8548 case '{':
8549 p->lex.pcur = ptr;
8550 p->command_start = TRUE;
8551 yylval.state = p->lex.state;
8552 return tSTRING_DBEG;
8553 default:
8554 return 0;
8555 }
8556 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8557 return tSTRING_DVAR;
8558 return 0;
8559}
8560
8561#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8562#define IS_END() IS_lex_state(EXPR_END_ANY)
8563#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8564#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8565#define IS_LABEL_POSSIBLE() (\
8566 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8567 IS_ARG())
8568#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8569#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8570
8571static inline enum yytokentype
8572parser_string_term(struct parser_params *p, int func)
8573{
8574 xfree(p->lex.strterm);
8575 p->lex.strterm = 0;
8576 if (func & STR_FUNC_REGEXP) {
8577 set_yylval_num(regx_options(p));
8578 dispatch_scan_event(p, tREGEXP_END);
8579 SET_LEX_STATE(EXPR_END);
8580 return tREGEXP_END;
8581 }
8582 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8583 nextc(p);
8584 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8585 return tLABEL_END;
8586 }
8587 SET_LEX_STATE(EXPR_END);
8588 return tSTRING_END;
8589}
8590
8591static enum yytokentype
8592parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8593{
8594 int func = quote->func;
8595 int term = quote->term;
8596 int paren = quote->paren;
8597 int c, space = 0;
8598 rb_encoding *enc = p->enc;
8599 rb_encoding *base_enc = 0;
8600 rb_parser_string_t *lit;
8601
8602 if (func & STR_FUNC_TERM) {
8603 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8604 SET_LEX_STATE(EXPR_END);
8605 xfree(p->lex.strterm);
8606 p->lex.strterm = 0;
8607 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8608 }
8609 c = nextc(p);
8610 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8611 while (c != '\n' && ISSPACE(c = nextc(p)));
8612 space = 1;
8613 }
8614 if (func & STR_FUNC_LIST) {
8615 quote->func &= ~STR_FUNC_LIST;
8616 space = 1;
8617 }
8618 if (c == term && !quote->nest) {
8619 if (func & STR_FUNC_QWORDS) {
8620 quote->func |= STR_FUNC_TERM;
8621 pushback(p, c); /* dispatch the term at tSTRING_END */
8622 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8623 return ' ';
8624 }
8625 return parser_string_term(p, func);
8626 }
8627 if (space) {
8628 if (!ISSPACE(c)) pushback(p, c);
8629 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8630 return ' ';
8631 }
8632 newtok(p);
8633 if ((func & STR_FUNC_EXPAND) && c == '#') {
8634 enum yytokentype t = parser_peek_variable_name(p);
8635 if (t) return t;
8636 tokadd(p, '#');
8637 c = nextc(p);
8638 }
8639 pushback(p, c);
8640 if (tokadd_string(p, func, term, paren, &quote->nest,
8641 &enc, &base_enc) == -1) {
8642 if (p->eofp) {
8643#ifndef RIPPER
8644# define unterminated_literal(mesg) yyerror0(mesg)
8645#else
8646# define unterminated_literal(mesg) compile_error(p, mesg)
8647#endif
8648 literal_flush(p, p->lex.pcur);
8649 if (func & STR_FUNC_QWORDS) {
8650 /* no content to add, bailing out here */
8651 unterminated_literal("unterminated list meets end of file");
8652 xfree(p->lex.strterm);
8653 p->lex.strterm = 0;
8654 return tSTRING_END;
8655 }
8656 if (func & STR_FUNC_REGEXP) {
8657 unterminated_literal("unterminated regexp meets end of file");
8658 }
8659 else {
8660 unterminated_literal("unterminated string meets end of file");
8661 }
8662 quote->func |= STR_FUNC_TERM;
8663 }
8664 }
8665
8666 tokfix(p);
8667 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8668 set_yylval_str(lit);
8669 flush_string_content(p, enc, 0);
8670
8671 return tSTRING_CONTENT;
8672}
8673
8674static enum yytokentype
8675heredoc_identifier(struct parser_params *p)
8676{
8677 /*
8678 * term_len is length of `<<"END"` except `END`,
8679 * in this case term_len is 4 (<, <, " and ").
8680 */
8681 long len, offset = p->lex.pcur - p->lex.pbeg;
8682 int c = nextc(p), term, func = 0, quote = 0;
8683 enum yytokentype token = tSTRING_BEG;
8684 int indent = 0;
8685
8686 if (c == '-') {
8687 c = nextc(p);
8688 func = STR_FUNC_INDENT;
8689 offset++;
8690 }
8691 else if (c == '~') {
8692 c = nextc(p);
8693 func = STR_FUNC_INDENT;
8694 offset++;
8695 indent = INT_MAX;
8696 }
8697 switch (c) {
8698 case '\'':
8699 func |= str_squote; goto quoted;
8700 case '"':
8701 func |= str_dquote; goto quoted;
8702 case '`':
8703 token = tXSTRING_BEG;
8704 func |= str_xquote; goto quoted;
8705
8706 quoted:
8707 quote++;
8708 offset++;
8709 term = c;
8710 len = 0;
8711 while ((c = nextc(p)) != term) {
8712 if (c == -1 || c == '\r' || c == '\n') {
8713 yyerror0("unterminated here document identifier");
8714 return -1;
8715 }
8716 }
8717 break;
8718
8719 default:
8720 if (!parser_is_identchar(p)) {
8721 pushback(p, c);
8722 if (func & STR_FUNC_INDENT) {
8723 pushback(p, indent > 0 ? '~' : '-');
8724 }
8725 return 0;
8726 }
8727 func |= str_dquote;
8728 do {
8729 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8730 if (n < 0) return 0;
8731 p->lex.pcur += --n;
8732 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8733 pushback(p, c);
8734 break;
8735 }
8736
8737 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8738 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8739 yyerror0("too long here document identifier");
8740 dispatch_scan_event(p, tHEREDOC_BEG);
8741 lex_goto_eol(p);
8742
8743 p->lex.strterm = new_heredoc(p);
8744 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8745 here->offset = offset;
8746 here->sourceline = p->ruby_sourceline;
8747 here->length = (unsigned)len;
8748 here->quote = quote;
8749 here->func = func;
8750 here->lastline = p->lex.lastline;
8751
8752 token_flush(p);
8753 p->heredoc_indent = indent;
8754 p->heredoc_line_indent = 0;
8755 return token;
8756}
8757
8758static void
8759heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8760{
8761 rb_parser_string_t *line;
8762 rb_strterm_t *term = p->lex.strterm;
8763
8764 p->lex.strterm = 0;
8765 line = here->lastline;
8766 p->lex.lastline = line;
8767 p->lex.pbeg = PARSER_STRING_PTR(line);
8768 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8769 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8770 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8771 p->heredoc_end = p->ruby_sourceline;
8772 p->ruby_sourceline = (int)here->sourceline;
8773 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8774 p->eofp = 0;
8775 xfree(term);
8776}
8777
8778static int
8779dedent_string_column(const char *str, long len, int width)
8780{
8781 int i, col = 0;
8782
8783 for (i = 0; i < len && col < width; i++) {
8784 if (str[i] == ' ') {
8785 col++;
8786 }
8787 else if (str[i] == '\t') {
8788 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8789 if (n > width) break;
8790 col = n;
8791 }
8792 else {
8793 break;
8794 }
8795 }
8796
8797 return i;
8798}
8799
8800static int
8801dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8802{
8803 char *str;
8804 long len;
8805 int i;
8806
8807 len = PARSER_STRING_LEN(string);
8808 str = PARSER_STRING_PTR(string);
8809
8810 i = dedent_string_column(str, len, width);
8811 if (!i) return 0;
8812
8813 rb_parser_str_modify(string);
8814 str = PARSER_STRING_PTR(string);
8815 if (PARSER_STRING_LEN(string) != len)
8816 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8817 MEMMOVE(str, str + i, char, len - i);
8818 rb_parser_str_set_len(p, string, len - i);
8819 return i;
8820}
8821
8822static NODE *
8823heredoc_dedent(struct parser_params *p, NODE *root)
8824{
8825 NODE *node, *str_node, *prev_node;
8826 int indent = p->heredoc_indent;
8827 rb_parser_string_t *prev_lit = 0;
8828
8829 if (indent <= 0) return root;
8830 if (!root) return root;
8831
8832 prev_node = node = str_node = root;
8833 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8834
8835 while (str_node) {
8836 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8837 if (nd_fl_newline(str_node)) {
8838 dedent_string(p, lit, indent);
8839 }
8840 if (!prev_lit) {
8841 prev_lit = lit;
8842 }
8843 else if (!literal_concat0(p, prev_lit, lit)) {
8844 return 0;
8845 }
8846 else {
8847 NODE *end = RNODE_LIST(node)->as.nd_end;
8848 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8849 if (!node) {
8850 if (nd_type_p(prev_node, NODE_DSTR))
8851 nd_set_type(prev_node, NODE_STR);
8852 break;
8853 }
8854 RNODE_LIST(node)->as.nd_end = end;
8855 goto next_str;
8856 }
8857
8858 str_node = 0;
8859 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8860 next_str:
8861 if (!nd_type_p(node, NODE_LIST)) break;
8862 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8863 enum node_type type = nd_type(str_node);
8864 if (type == NODE_STR || type == NODE_DSTR) break;
8865 prev_lit = 0;
8866 str_node = 0;
8867 }
8868 }
8869 }
8870 return root;
8871}
8872
8873static int
8874whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8875{
8876 const char *beg = p->lex.pbeg;
8877 const char *ptr = p->lex.pend;
8878
8879 if (ptr - beg < len) return FALSE;
8880 if (ptr > beg && ptr[-1] == '\n') {
8881 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8882 if (ptr - beg < len) return FALSE;
8883 }
8884 if (strncmp(eos, ptr -= len, len)) return FALSE;
8885 if (indent) {
8886 while (beg < ptr && ISSPACE(*beg)) beg++;
8887 }
8888 return beg == ptr;
8889}
8890
8891static int
8892word_match_p(struct parser_params *p, const char *word, long len)
8893{
8894 if (strncmp(p->lex.pcur, word, len)) return 0;
8895 if (lex_eol_n_p(p, len)) return 1;
8896 int c = (unsigned char)p->lex.pcur[len];
8897 if (ISSPACE(c)) return 1;
8898 switch (c) {
8899 case '\0': case '\004': case '\032': return 1;
8900 }
8901 return 0;
8902}
8903
8904#define NUM_SUFFIX_R (1<<0)
8905#define NUM_SUFFIX_I (1<<1)
8906#define NUM_SUFFIX_ALL 3
8907
8908static int
8909number_literal_suffix(struct parser_params *p, int mask)
8910{
8911 int c, result = 0;
8912 const char *lastp = p->lex.pcur;
8913
8914 while ((c = nextc(p)) != -1) {
8915 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8916 result |= (mask & NUM_SUFFIX_I);
8917 mask &= ~NUM_SUFFIX_I;
8918 /* r after i, rational of complex is disallowed */
8919 mask &= ~NUM_SUFFIX_R;
8920 continue;
8921 }
8922 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8923 result |= (mask & NUM_SUFFIX_R);
8924 mask &= ~NUM_SUFFIX_R;
8925 continue;
8926 }
8927 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8928 p->lex.pcur = lastp;
8929 return 0;
8930 }
8931 pushback(p, c);
8932 break;
8933 }
8934 return result;
8935}
8936
8937static enum yytokentype
8938set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8939{
8940 enum rb_numeric_type numeric_type = integer_literal;
8941
8942 if (type == tFLOAT) {
8943 numeric_type = float_literal;
8944 }
8945
8946 if (suffix & NUM_SUFFIX_R) {
8947 type = tRATIONAL;
8948 numeric_type = rational_literal;
8949 }
8950 if (suffix & NUM_SUFFIX_I) {
8951 type = tIMAGINARY;
8952 }
8953
8954 switch (type) {
8955 case tINTEGER:
8956 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8957 break;
8958 case tFLOAT:
8959 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8960 break;
8961 case tRATIONAL:
8962 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8963 break;
8964 case tIMAGINARY:
8965 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8966 (void)numeric_type; /* for ripper */
8967 break;
8968 default:
8969 rb_bug("unexpected token: %d", type);
8970 }
8971 SET_LEX_STATE(EXPR_END);
8972 return type;
8973}
8974
8975#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8976static void
8977parser_dispatch_heredoc_end(struct parser_params *p, int line)
8978{
8979 if (has_delayed_token(p))
8980 dispatch_delayed_token(p, tSTRING_CONTENT);
8981
8982#ifdef RIPPER
8983 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8984 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8985#else
8986 if (p->keep_tokens) {
8987 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
8988 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8989 parser_append_tokens(p, str, tHEREDOC_END, line);
8990 }
8991#endif
8992
8993 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8994 lex_goto_eol(p);
8995 token_flush(p);
8996}
8997
8998static enum yytokentype
8999here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9000{
9001 int c, func, indent = 0;
9002 const char *eos, *ptr, *ptr_end;
9003 long len;
9004 rb_parser_string_t *str = 0;
9005 rb_encoding *enc = p->enc;
9006 rb_encoding *base_enc = 0;
9007 int bol;
9008#ifdef RIPPER
9009 VALUE s_value;
9010#endif
9011
9012 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9013 len = here->length;
9014 indent = (func = here->func) & STR_FUNC_INDENT;
9015
9016 if ((c = nextc(p)) == -1) {
9017 error:
9018#ifdef RIPPER
9019 if (!has_delayed_token(p)) {
9020 dispatch_scan_event(p, tSTRING_CONTENT);
9021 }
9022 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9023 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9024 if (!(func & STR_FUNC_REGEXP)) {
9025 int cr = ENC_CODERANGE_UNKNOWN;
9026 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9027 if (cr != ENC_CODERANGE_7BIT &&
9028 rb_is_usascii_enc(p->enc) &&
9029 enc != rb_utf8_encoding()) {
9030 enc = rb_ascii8bit_encoding();
9031 }
9032 }
9033 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9034 }
9035 dispatch_delayed_token(p, tSTRING_CONTENT);
9036 }
9037 else {
9038 dispatch_delayed_token(p, tSTRING_CONTENT);
9039 dispatch_scan_event(p, tSTRING_CONTENT);
9040 }
9041 lex_goto_eol(p);
9042#endif
9043 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9044 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9045 (int)len, eos);
9046 token_flush(p);
9047 SET_LEX_STATE(EXPR_END);
9048 return tSTRING_END;
9049 }
9050 bol = was_bol(p);
9051 if (!bol) {
9052 /* not beginning of line, cannot be the terminator */
9053 }
9054 else if (p->heredoc_line_indent == -1) {
9055 /* `heredoc_line_indent == -1` means
9056 * - "after an interpolation in the same line", or
9057 * - "in a continuing line"
9058 */
9059 p->heredoc_line_indent = 0;
9060 }
9061 else if (whole_match_p(p, eos, len, indent)) {
9062 dispatch_heredoc_end(p);
9063 restore:
9064 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9065 token_flush(p);
9066 SET_LEX_STATE(EXPR_END);
9067 return tSTRING_END;
9068 }
9069
9070 if (!(func & STR_FUNC_EXPAND)) {
9071 do {
9072 ptr = PARSER_STRING_PTR(p->lex.lastline);
9073 ptr_end = p->lex.pend;
9074 if (ptr_end > ptr) {
9075 switch (ptr_end[-1]) {
9076 case '\n':
9077 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9078 ptr_end++;
9079 break;
9080 }
9081 case '\r':
9082 --ptr_end;
9083 }
9084 }
9085
9086 if (p->heredoc_indent > 0) {
9087 long i = 0;
9088 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9089 i++;
9090 p->heredoc_line_indent = 0;
9091 }
9092
9093 if (str)
9094 parser_str_cat(str, ptr, ptr_end - ptr);
9095 else
9096 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9097 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9098 lex_goto_eol(p);
9099 if (p->heredoc_indent > 0) {
9100 goto flush_str;
9101 }
9102 if (nextc(p) == -1) {
9103 if (str) {
9104 rb_parser_string_free(p, str);
9105 str = 0;
9106 }
9107 goto error;
9108 }
9109 } while (!whole_match_p(p, eos, len, indent));
9110 }
9111 else {
9112 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9113 newtok(p);
9114 if (c == '#') {
9115 enum yytokentype t = parser_peek_variable_name(p);
9116 if (p->heredoc_line_indent != -1) {
9117 if (p->heredoc_indent > p->heredoc_line_indent) {
9118 p->heredoc_indent = p->heredoc_line_indent;
9119 }
9120 p->heredoc_line_indent = -1;
9121 }
9122 if (t) return t;
9123 tokadd(p, '#');
9124 c = nextc(p);
9125 }
9126 do {
9127 pushback(p, c);
9128 enc = p->enc;
9129 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9130 if (p->eofp) goto error;
9131 goto restore;
9132 }
9133 if (c != '\n') {
9134 if (c == '\\') p->heredoc_line_indent = -1;
9135 flush:
9136 str = STR_NEW3(tok(p), toklen(p), enc, func);
9137 flush_str:
9138 set_yylval_str(str);
9139#ifndef RIPPER
9140 if (bol) nd_set_fl_newline(yylval.node);
9141#endif
9142 flush_string_content(p, enc, 0);
9143 return tSTRING_CONTENT;
9144 }
9145 tokadd(p, nextc(p));
9146 if (p->heredoc_indent > 0) {
9147 lex_goto_eol(p);
9148 goto flush;
9149 }
9150 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9151 if ((c = nextc(p)) == -1) goto error;
9152 } while (!whole_match_p(p, eos, len, indent));
9153 str = STR_NEW3(tok(p), toklen(p), enc, func);
9154 }
9155 dispatch_heredoc_end(p);
9156 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9157 token_flush(p);
9158 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9159#ifdef RIPPER
9160 /* Preserve s_value for set_yylval_str */
9161 s_value = p->s_value;
9162#endif
9163 set_yylval_str(str);
9164#ifdef RIPPER
9165 set_parser_s_value(s_value);
9166#endif
9167
9168#ifndef RIPPER
9169 if (bol) nd_set_fl_newline(yylval.node);
9170#endif
9171 return tSTRING_CONTENT;
9172}
9173
9174#include "lex.c"
9175
9176static int
9177arg_ambiguous(struct parser_params *p, char c)
9178{
9179#ifndef RIPPER
9180 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9181#else
9182 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9183#endif
9184 return TRUE;
9185}
9186
9187/* returns true value if formal argument error;
9188 * Qtrue, or error message if ripper */
9189static VALUE
9190formal_argument_error(struct parser_params *p, ID id)
9191{
9192 switch (id_type(id)) {
9193 case ID_LOCAL:
9194 break;
9195#ifndef RIPPER
9196# define ERR(mesg) (yyerror0(mesg), Qtrue)
9197#else
9198# define ERR(mesg) WARN_S(mesg)
9199#endif
9200 case ID_CONST:
9201 return ERR("formal argument cannot be a constant");
9202 case ID_INSTANCE:
9203 return ERR("formal argument cannot be an instance variable");
9204 case ID_GLOBAL:
9205 return ERR("formal argument cannot be a global variable");
9206 case ID_CLASS:
9207 return ERR("formal argument cannot be a class variable");
9208 default:
9209 return ERR("formal argument must be local variable");
9210#undef ERR
9211 }
9212 shadowing_lvar(p, id);
9213
9214 return Qfalse;
9215}
9216
9217static int
9218lvar_defined(struct parser_params *p, ID id)
9219{
9220 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9221}
9222
9223/* emacsen -*- hack */
9224static long
9225parser_encode_length(struct parser_params *p, const char *name, long len)
9226{
9227 long nlen;
9228
9229 if (len > 5 && name[nlen = len - 5] == '-') {
9230 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9231 return nlen;
9232 }
9233 if (len > 4 && name[nlen = len - 4] == '-') {
9234 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9235 return nlen;
9236 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9237 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9238 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9239 return nlen;
9240 }
9241 return len;
9242}
9243
9244static void
9245parser_set_encode(struct parser_params *p, const char *name)
9246{
9247 rb_encoding *enc;
9248 VALUE excargs[3];
9249 int idx = 0;
9250
9251 const char *wrong = 0;
9252 switch (*name) {
9253 case 'e': case 'E': wrong = "external"; break;
9254 case 'i': case 'I': wrong = "internal"; break;
9255 case 'f': case 'F': wrong = "filesystem"; break;
9256 case 'l': case 'L': wrong = "locale"; break;
9257 }
9258 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9259 idx = rb_enc_find_index(name);
9260 if (idx < 0) {
9261 unknown:
9262 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9263 error:
9264 excargs[0] = rb_eArgError;
9265 excargs[2] = rb_make_backtrace();
9266 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9267 VALUE exc = rb_make_exception(3, excargs);
9268 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9269
9270 rb_ast_free(p->ast);
9271 p->ast = NULL;
9272
9273 rb_exc_raise(exc);
9274 }
9275 enc = rb_enc_from_index(idx);
9276 if (!rb_enc_asciicompat(enc)) {
9277 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9278 goto error;
9279 }
9280 p->enc = enc;
9281#ifndef RIPPER
9282 if (p->debug_lines) {
9283 long i;
9284 for (i = 0; i < p->debug_lines->len; i++) {
9285 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9286 }
9287 }
9288#endif
9289}
9290
9291static bool
9292comment_at_top(struct parser_params *p)
9293{
9294 if (p->token_seen) return false;
9295 return (p->line_count == (p->has_shebang ? 2 : 1));
9296}
9297
9298typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9299typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9300
9301static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9302
9303static void
9304magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9305{
9306 if (!comment_at_top(p)) {
9307 return;
9308 }
9309 parser_set_encode(p, val);
9310}
9311
9312static int
9313parser_get_bool(struct parser_params *p, const char *name, const char *val)
9314{
9315 switch (*val) {
9316 case 't': case 'T':
9317 if (STRCASECMP(val, "true") == 0) {
9318 return TRUE;
9319 }
9320 break;
9321 case 'f': case 'F':
9322 if (STRCASECMP(val, "false") == 0) {
9323 return FALSE;
9324 }
9325 break;
9326 }
9327 return parser_invalid_pragma_value(p, name, val);
9328}
9329
9330static int
9331parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9332{
9333 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9334 return -1;
9335}
9336
9337static void
9338parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9339{
9340 int b = parser_get_bool(p, name, val);
9341 if (b >= 0) p->token_info_enabled = b;
9342}
9343
9344static void
9345parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9346{
9347 int b;
9348
9349 if (p->token_seen) {
9350 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9351 return;
9352 }
9353
9354 b = parser_get_bool(p, name, val);
9355 if (b < 0) return;
9356
9357 p->frozen_string_literal = b;
9358}
9359
9360static void
9361parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9362{
9363 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9364 if (*s == ' ' || *s == '\t') continue;
9365 if (*s == '#') break;
9366 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9367 return;
9368 }
9369
9370 switch (*val) {
9371 case 'n': case 'N':
9372 if (STRCASECMP(val, "none") == 0) {
9373 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9374 return;
9375 }
9376 break;
9377 case 'l': case 'L':
9378 if (STRCASECMP(val, "literal") == 0) {
9379 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9380 return;
9381 }
9382 break;
9383 case 'e': case 'E':
9384 if (STRCASECMP(val, "experimental_copy") == 0) {
9385 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9386 return;
9387 }
9388 if (STRCASECMP(val, "experimental_everything") == 0) {
9389 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9390 return;
9391 }
9392 break;
9393 }
9394 parser_invalid_pragma_value(p, name, val);
9395}
9396
9397# if WARN_PAST_SCOPE
9398static void
9399parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9400{
9401 int b = parser_get_bool(p, name, val);
9402 if (b >= 0) p->past_scope_enabled = b;
9403}
9404# endif
9405
9406struct magic_comment {
9407 const char *name;
9408 rb_magic_comment_setter_t func;
9409 rb_magic_comment_length_t length;
9410};
9411
9412static const struct magic_comment magic_comments[] = {
9413 {"coding", magic_comment_encoding, parser_encode_length},
9414 {"encoding", magic_comment_encoding, parser_encode_length},
9415 {"frozen_string_literal", parser_set_frozen_string_literal},
9416 {"shareable_constant_value", parser_set_shareable_constant_value},
9417 {"warn_indent", parser_set_token_info},
9418# if WARN_PAST_SCOPE
9419 {"warn_past_scope", parser_set_past_scope},
9420# endif
9421};
9422
9423static const char *
9424magic_comment_marker(const char *str, long len)
9425{
9426 long i = 2;
9427
9428 while (i < len) {
9429 switch (str[i]) {
9430 case '-':
9431 if (str[i-1] == '*' && str[i-2] == '-') {
9432 return str + i + 1;
9433 }
9434 i += 2;
9435 break;
9436 case '*':
9437 if (i + 1 >= len) return 0;
9438 if (str[i+1] != '-') {
9439 i += 4;
9440 }
9441 else if (str[i-1] != '-') {
9442 i += 2;
9443 }
9444 else {
9445 return str + i + 2;
9446 }
9447 break;
9448 default:
9449 i += 3;
9450 break;
9451 }
9452 }
9453 return 0;
9454}
9455
9456static int
9457parser_magic_comment(struct parser_params *p, const char *str, long len)
9458{
9459 int indicator = 0;
9460 VALUE name = 0, val = 0;
9461 const char *beg, *end, *vbeg, *vend;
9462#define str_copy(_s, _p, _n) ((_s) \
9463 ? (void)(rb_str_resize((_s), (_n)), \
9464 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9465 : (void)((_s) = STR_NEW((_p), (_n))))
9466
9467 if (len <= 7) return FALSE;
9468 if (!!(beg = magic_comment_marker(str, len))) {
9469 if (!(end = magic_comment_marker(beg, str + len - beg)))
9470 return FALSE;
9471 indicator = TRUE;
9472 str = beg;
9473 len = end - beg - 3;
9474 }
9475
9476 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9477 while (len > 0) {
9478 const struct magic_comment *mc = magic_comments;
9479 char *s;
9480 int i;
9481 long n = 0;
9482
9483 for (; len > 0 && *str; str++, --len) {
9484 switch (*str) {
9485 case '\'': case '"': case ':': case ';':
9486 continue;
9487 }
9488 if (!ISSPACE(*str)) break;
9489 }
9490 for (beg = str; len > 0; str++, --len) {
9491 switch (*str) {
9492 case '\'': case '"': case ':': case ';':
9493 break;
9494 default:
9495 if (ISSPACE(*str)) break;
9496 continue;
9497 }
9498 break;
9499 }
9500 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9501 if (!len) break;
9502 if (*str != ':') {
9503 if (!indicator) return FALSE;
9504 continue;
9505 }
9506
9507 do str++; while (--len > 0 && ISSPACE(*str));
9508 if (!len) break;
9509 const char *tok_beg = str;
9510 if (*str == '"') {
9511 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9512 if (*str == '\\') {
9513 --len;
9514 ++str;
9515 }
9516 }
9517 vend = str;
9518 if (len) {
9519 --len;
9520 ++str;
9521 }
9522 }
9523 else {
9524 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9525 vend = str;
9526 }
9527 const char *tok_end = str;
9528 if (indicator) {
9529 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9530 }
9531 else {
9532 while (len > 0 && (ISSPACE(*str))) --len, str++;
9533 if (len) return FALSE;
9534 }
9535
9536 n = end - beg;
9537 str_copy(name, beg, n);
9538 s = RSTRING_PTR(name);
9539 for (i = 0; i < n; ++i) {
9540 if (s[i] == '-') s[i] = '_';
9541 }
9542 do {
9543 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9544 n = vend - vbeg;
9545 if (mc->length) {
9546 n = (*mc->length)(p, vbeg, n);
9547 }
9548 str_copy(val, vbeg, n);
9549 p->lex.ptok = tok_beg;
9550 p->lex.pcur = tok_end;
9551 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9552 break;
9553 }
9554 } while (++mc < magic_comments + numberof(magic_comments));
9555#ifdef RIPPER
9556 str_copy(val, vbeg, vend - vbeg);
9557 dispatch2(magic_comment, name, val);
9558#endif
9559 }
9560
9561 return TRUE;
9562}
9563
9564static void
9565set_file_encoding(struct parser_params *p, const char *str, const char *send)
9566{
9567 int sep = 0;
9568 const char *beg = str;
9569 VALUE s;
9570
9571 for (;;) {
9572 if (send - str <= 6) return;
9573 switch (str[6]) {
9574 case 'C': case 'c': str += 6; continue;
9575 case 'O': case 'o': str += 5; continue;
9576 case 'D': case 'd': str += 4; continue;
9577 case 'I': case 'i': str += 3; continue;
9578 case 'N': case 'n': str += 2; continue;
9579 case 'G': case 'g': str += 1; continue;
9580 case '=': case ':':
9581 sep = 1;
9582 str += 6;
9583 break;
9584 default:
9585 str += 6;
9586 if (ISSPACE(*str)) break;
9587 continue;
9588 }
9589 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9590 sep = 0;
9591 }
9592 for (;;) {
9593 do {
9594 if (++str >= send) return;
9595 } while (ISSPACE(*str));
9596 if (sep) break;
9597 if (*str != '=' && *str != ':') return;
9598 sep = 1;
9599 str++;
9600 }
9601 beg = str;
9602 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9603 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9604 p->lex.ptok = beg;
9605 p->lex.pcur = str;
9606 parser_set_encode(p, RSTRING_PTR(s));
9607 rb_str_resize(s, 0);
9608}
9609
9610static void
9611parser_prepare(struct parser_params *p)
9612{
9613 int c = nextc0(p, FALSE);
9614 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9615 switch (c) {
9616 case '#':
9617 if (peek(p, '!')) p->has_shebang = 1;
9618 break;
9619 case 0xef: /* UTF-8 BOM marker */
9620 if (!lex_eol_n_p(p, 2) &&
9621 (unsigned char)p->lex.pcur[0] == 0xbb &&
9622 (unsigned char)p->lex.pcur[1] == 0xbf) {
9623 p->enc = rb_utf8_encoding();
9624 p->lex.pcur += 2;
9625#ifndef RIPPER
9626 if (p->debug_lines) {
9627 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9628 }
9629#endif
9630 p->lex.pbeg = p->lex.pcur;
9631 token_flush(p);
9632 return;
9633 }
9634 break;
9635 case -1: /* end of script. */
9636 return;
9637 }
9638 pushback(p, c);
9639 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9640}
9641
9642#ifndef RIPPER
9643#define ambiguous_operator(tok, op, syn) ( \
9644 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9645 rb_warning0("even though it seems like "syn""))
9646#else
9647#define ambiguous_operator(tok, op, syn) \
9648 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9649#endif
9650#define warn_balanced(tok, op, syn) ((void) \
9651 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9652 space_seen && !ISSPACE(c) && \
9653 (ambiguous_operator(tok, op, syn), 0)), \
9654 (enum yytokentype)(tok))
9655
9656static enum yytokentype
9657no_digits(struct parser_params *p)
9658{
9659 yyerror0("numeric literal without digits");
9660 if (peek(p, '_')) nextc(p);
9661 /* dummy 0, for tUMINUS_NUM at numeric */
9662 return set_number_literal(p, tINTEGER, 0, 10, 0);
9663}
9664
9665static enum yytokentype
9666parse_numeric(struct parser_params *p, int c)
9667{
9668 int is_float, seen_point, seen_e, nondigit;
9669 int suffix;
9670
9671 is_float = seen_point = seen_e = nondigit = 0;
9672 SET_LEX_STATE(EXPR_END);
9673 newtok(p);
9674 if (c == '-' || c == '+') {
9675 tokadd(p, c);
9676 c = nextc(p);
9677 }
9678 if (c == '0') {
9679 int start = toklen(p);
9680 c = nextc(p);
9681 if (c == 'x' || c == 'X') {
9682 /* hexadecimal */
9683 c = nextc(p);
9684 if (c != -1 && ISXDIGIT(c)) {
9685 do {
9686 if (c == '_') {
9687 if (nondigit) break;
9688 nondigit = c;
9689 continue;
9690 }
9691 if (!ISXDIGIT(c)) break;
9692 nondigit = 0;
9693 tokadd(p, c);
9694 } while ((c = nextc(p)) != -1);
9695 }
9696 pushback(p, c);
9697 tokfix(p);
9698 if (toklen(p) == start) {
9699 return no_digits(p);
9700 }
9701 else if (nondigit) goto trailing_uc;
9702 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9703 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9704 }
9705 if (c == 'b' || c == 'B') {
9706 /* binary */
9707 c = nextc(p);
9708 if (c == '0' || c == '1') {
9709 do {
9710 if (c == '_') {
9711 if (nondigit) break;
9712 nondigit = c;
9713 continue;
9714 }
9715 if (c != '0' && c != '1') break;
9716 nondigit = 0;
9717 tokadd(p, c);
9718 } while ((c = nextc(p)) != -1);
9719 }
9720 pushback(p, c);
9721 tokfix(p);
9722 if (toklen(p) == start) {
9723 return no_digits(p);
9724 }
9725 else if (nondigit) goto trailing_uc;
9726 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9727 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9728 }
9729 if (c == 'd' || c == 'D') {
9730 /* decimal */
9731 c = nextc(p);
9732 if (c != -1 && ISDIGIT(c)) {
9733 do {
9734 if (c == '_') {
9735 if (nondigit) break;
9736 nondigit = c;
9737 continue;
9738 }
9739 if (!ISDIGIT(c)) break;
9740 nondigit = 0;
9741 tokadd(p, c);
9742 } while ((c = nextc(p)) != -1);
9743 }
9744 pushback(p, c);
9745 tokfix(p);
9746 if (toklen(p) == start) {
9747 return no_digits(p);
9748 }
9749 else if (nondigit) goto trailing_uc;
9750 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9751 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9752 }
9753 if (c == '_') {
9754 /* 0_0 */
9755 goto octal_number;
9756 }
9757 if (c == 'o' || c == 'O') {
9758 /* prefixed octal */
9759 c = nextc(p);
9760 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9761 tokfix(p);
9762 return no_digits(p);
9763 }
9764 }
9765 if (c >= '0' && c <= '7') {
9766 /* octal */
9767 octal_number:
9768 do {
9769 if (c == '_') {
9770 if (nondigit) break;
9771 nondigit = c;
9772 continue;
9773 }
9774 if (c < '0' || c > '9') break;
9775 if (c > '7') goto invalid_octal;
9776 nondigit = 0;
9777 tokadd(p, c);
9778 } while ((c = nextc(p)) != -1);
9779 if (toklen(p) > start) {
9780 pushback(p, c);
9781 tokfix(p);
9782 if (nondigit) goto trailing_uc;
9783 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9784 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9785 }
9786 if (nondigit) {
9787 pushback(p, c);
9788 goto trailing_uc;
9789 }
9790 }
9791 if (c > '7' && c <= '9') {
9792 invalid_octal:
9793 yyerror0("Invalid octal digit");
9794 }
9795 else if (c == '.' || c == 'e' || c == 'E') {
9796 tokadd(p, '0');
9797 }
9798 else {
9799 pushback(p, c);
9800 tokfix(p);
9801 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9802 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9803 }
9804 }
9805
9806 for (;;) {
9807 switch (c) {
9808 case '0': case '1': case '2': case '3': case '4':
9809 case '5': case '6': case '7': case '8': case '9':
9810 nondigit = 0;
9811 tokadd(p, c);
9812 break;
9813
9814 case '.':
9815 if (nondigit) goto trailing_uc;
9816 if (seen_point || seen_e) {
9817 goto decode_num;
9818 }
9819 else {
9820 int c0 = nextc(p);
9821 if (c0 == -1 || !ISDIGIT(c0)) {
9822 pushback(p, c0);
9823 goto decode_num;
9824 }
9825 c = c0;
9826 }
9827 seen_point = toklen(p);
9828 tokadd(p, '.');
9829 tokadd(p, c);
9830 is_float++;
9831 nondigit = 0;
9832 break;
9833
9834 case 'e':
9835 case 'E':
9836 if (nondigit) {
9837 pushback(p, c);
9838 c = nondigit;
9839 goto decode_num;
9840 }
9841 if (seen_e) {
9842 goto decode_num;
9843 }
9844 nondigit = c;
9845 c = nextc(p);
9846 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9847 pushback(p, c);
9848 c = nondigit;
9849 nondigit = 0;
9850 goto decode_num;
9851 }
9852 tokadd(p, nondigit);
9853 seen_e++;
9854 is_float++;
9855 tokadd(p, c);
9856 nondigit = (c == '-' || c == '+') ? c : 0;
9857 break;
9858
9859 case '_': /* `_' in number just ignored */
9860 if (nondigit) goto decode_num;
9861 nondigit = c;
9862 break;
9863
9864 default:
9865 goto decode_num;
9866 }
9867 c = nextc(p);
9868 }
9869
9870 decode_num:
9871 pushback(p, c);
9872 if (nondigit) {
9873 trailing_uc:
9874 literal_flush(p, p->lex.pcur - 1);
9875 YYLTYPE loc = RUBY_INIT_YYLLOC();
9876 compile_error(p, "trailing '%c' in number", nondigit);
9877 parser_show_error_line(p, &loc);
9878 }
9879 tokfix(p);
9880 if (is_float) {
9881 enum yytokentype type = tFLOAT;
9882
9883 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9884 if (suffix & NUM_SUFFIX_R) {
9885 type = tRATIONAL;
9886 }
9887 else {
9888 errno = 0;
9889 strtod(tok(p), 0);
9890 if (errno == ERANGE) {
9891 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9892 errno = 0;
9893 }
9894 }
9895 return set_number_literal(p, type, suffix, 0, seen_point);
9896 }
9897 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9898 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9899}
9900
9901static enum yytokentype
9902parse_qmark(struct parser_params *p, int space_seen)
9903{
9904 rb_encoding *enc;
9905 register int c;
9906 rb_parser_string_t *lit;
9907 const char *start = p->lex.pcur;
9908
9909 if (IS_END()) {
9910 SET_LEX_STATE(EXPR_VALUE);
9911 return '?';
9912 }
9913 c = nextc(p);
9914 if (c == -1) {
9915 compile_error(p, "incomplete character syntax");
9916 return 0;
9917 }
9918 if (rb_enc_isspace(c, p->enc)) {
9919 if (!IS_ARG()) {
9920 int c2 = escaped_control_code(c);
9921 if (c2) {
9922 WARN_SPACE_CHAR(c2, "?");
9923 }
9924 }
9925 ternary:
9926 pushback(p, c);
9927 SET_LEX_STATE(EXPR_VALUE);
9928 return '?';
9929 }
9930 newtok(p);
9931 enc = p->enc;
9932 int w = parser_precise_mbclen(p, start);
9933 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9934 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9935 if (space_seen) {
9936 const char *ptr = start;
9937 do {
9938 int n = parser_precise_mbclen(p, ptr);
9939 if (n < 0) return -1;
9940 ptr += n;
9941 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9942 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9943 " a conditional operator, put a space after '?'",
9944 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9945 }
9946 goto ternary;
9947 }
9948 else if (c == '\\') {
9949 if (peek(p, 'u')) {
9950 nextc(p);
9951 enc = rb_utf8_encoding();
9952 tokadd_utf8(p, &enc, -1, 0, 0);
9953 }
9954 else if (!ISASCII(c = peekc(p)) && c != -1) {
9955 nextc(p);
9956 if (tokadd_mbchar(p, c) == -1) return 0;
9957 }
9958 else {
9959 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9960 tokadd(p, c);
9961 }
9962 }
9963 else {
9964 if (tokadd_mbchar(p, c) == -1) return 0;
9965 }
9966 tokfix(p);
9967 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9968 set_yylval_str(lit);
9969 SET_LEX_STATE(EXPR_END);
9970 return tCHAR;
9971}
9972
9973static enum yytokentype
9974parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9975{
9976 register int c;
9977 const char *ptok = p->lex.pcur;
9978
9979 if (IS_BEG()) {
9980 int term;
9981 int paren;
9982
9983 c = nextc(p);
9984 quotation:
9985 if (c == -1) goto unterminated;
9986 if (!ISALNUM(c)) {
9987 term = c;
9988 if (!ISASCII(c)) goto unknown;
9989 c = 'Q';
9990 }
9991 else {
9992 term = nextc(p);
9993 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9994 unknown:
9995 pushback(p, term);
9996 c = parser_precise_mbclen(p, p->lex.pcur);
9997 if (c < 0) return 0;
9998 p->lex.pcur += c;
9999 yyerror0("unknown type of %string");
10000 return 0;
10001 }
10002 }
10003 if (term == -1) {
10004 unterminated:
10005 compile_error(p, "unterminated quoted string meets end of file");
10006 return 0;
10007 }
10008 paren = term;
10009 if (term == '(') term = ')';
10010 else if (term == '[') term = ']';
10011 else if (term == '{') term = '}';
10012 else if (term == '<') term = '>';
10013 else paren = 0;
10014
10015 p->lex.ptok = ptok-1;
10016 switch (c) {
10017 case 'Q':
10018 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10019 return tSTRING_BEG;
10020
10021 case 'q':
10022 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10023 return tSTRING_BEG;
10024
10025 case 'W':
10026 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10027 return tWORDS_BEG;
10028
10029 case 'w':
10030 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10031 return tQWORDS_BEG;
10032
10033 case 'I':
10034 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10035 return tSYMBOLS_BEG;
10036
10037 case 'i':
10038 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10039 return tQSYMBOLS_BEG;
10040
10041 case 'x':
10042 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10043 return tXSTRING_BEG;
10044
10045 case 'r':
10046 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10047 return tREGEXP_BEG;
10048
10049 case 's':
10050 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10051 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10052 return tSYMBEG;
10053
10054 default:
10055 yyerror0("unknown type of %string");
10056 return 0;
10057 }
10058 }
10059 if ((c = nextc(p)) == '=') {
10060 set_yylval_id('%');
10061 SET_LEX_STATE(EXPR_BEG);
10062 return tOP_ASGN;
10063 }
10064 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10065 goto quotation;
10066 }
10067 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10068 pushback(p, c);
10069 return warn_balanced('%', "%%", "string literal");
10070}
10071
10072static int
10073tokadd_ident(struct parser_params *p, int c)
10074{
10075 do {
10076 if (tokadd_mbchar(p, c) == -1) return -1;
10077 c = nextc(p);
10078 } while (parser_is_identchar(p));
10079 pushback(p, c);
10080 return 0;
10081}
10082
10083static ID
10084tokenize_ident(struct parser_params *p)
10085{
10086 ID ident = TOK_INTERN();
10087
10088 set_yylval_name(ident);
10089
10090 return ident;
10091}
10092
10093static int
10094parse_numvar(struct parser_params *p)
10095{
10096 size_t len;
10097 int overflow;
10098 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10099 const unsigned long nth_ref_max =
10100 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10101 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10102 * turned into a Fixnum, in compile.c */
10103
10104 if (overflow || n > nth_ref_max) {
10105 /* compile_error()? */
10106 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10107 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10108 }
10109 else {
10110 return (int)n;
10111 }
10112}
10113
10114static enum yytokentype
10115parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10116{
10117 const char *ptr = p->lex.pcur;
10118 register int c;
10119
10120 SET_LEX_STATE(EXPR_END);
10121 p->lex.ptok = ptr - 1; /* from '$' */
10122 newtok(p);
10123 c = nextc(p);
10124 switch (c) {
10125 case '_': /* $_: last read line string */
10126 c = nextc(p);
10127 if (parser_is_identchar(p)) {
10128 tokadd(p, '$');
10129 tokadd(p, '_');
10130 break;
10131 }
10132 pushback(p, c);
10133 c = '_';
10134 /* fall through */
10135 case '~': /* $~: match-data */
10136 case '*': /* $*: argv */
10137 case '$': /* $$: pid */
10138 case '?': /* $?: last status */
10139 case '!': /* $!: error string */
10140 case '@': /* $@: error position */
10141 case '/': /* $/: input record separator */
10142 case '\\': /* $\: output record separator */
10143 case ';': /* $;: field separator */
10144 case ',': /* $,: output field separator */
10145 case '.': /* $.: last read line number */
10146 case '=': /* $=: ignorecase */
10147 case ':': /* $:: load path */
10148 case '<': /* $<: default input handle */
10149 case '>': /* $>: default output handle */
10150 case '\"': /* $": already loaded files */
10151 tokadd(p, '$');
10152 tokadd(p, c);
10153 goto gvar;
10154
10155 case '-':
10156 tokadd(p, '$');
10157 tokadd(p, c);
10158 c = nextc(p);
10159 if (parser_is_identchar(p)) {
10160 if (tokadd_mbchar(p, c) == -1) return 0;
10161 }
10162 else {
10163 pushback(p, c);
10164 pushback(p, '-');
10165 return '$';
10166 }
10167 gvar:
10168 tokenize_ident(p);
10169 return tGVAR;
10170
10171 case '&': /* $&: last match */
10172 case '`': /* $`: string before last match */
10173 case '\'': /* $': string after last match */
10174 case '+': /* $+: string matches last paren. */
10175 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10176 tokadd(p, '$');
10177 tokadd(p, c);
10178 goto gvar;
10179 }
10180 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10181 return tBACK_REF;
10182
10183 case '1': case '2': case '3':
10184 case '4': case '5': case '6':
10185 case '7': case '8': case '9':
10186 tokadd(p, '$');
10187 do {
10188 tokadd(p, c);
10189 c = nextc(p);
10190 } while (c != -1 && ISDIGIT(c));
10191 pushback(p, c);
10192 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10193 tokfix(p);
10194 c = parse_numvar(p);
10195 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10196 return tNTH_REF;
10197
10198 default:
10199 if (!parser_is_identchar(p)) {
10200 YYLTYPE loc = RUBY_INIT_YYLLOC();
10201 if (c == -1 || ISSPACE(c)) {
10202 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10203 }
10204 else {
10205 pushback(p, c);
10206 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10207 }
10208 parser_show_error_line(p, &loc);
10209 set_yylval_noname();
10210 return tGVAR;
10211 }
10212 /* fall through */
10213 case '0':
10214 tokadd(p, '$');
10215 }
10216
10217 if (tokadd_ident(p, c)) return 0;
10218 SET_LEX_STATE(EXPR_END);
10219 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10220 tokenize_ident(p);
10221 }
10222 else {
10223 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10224 set_yylval_noname();
10225 }
10226 return tGVAR;
10227}
10228
10229static bool
10230parser_numbered_param(struct parser_params *p, int n)
10231{
10232 if (n < 0) return false;
10233
10234 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10235 return false;
10236 }
10237 if (p->max_numparam == ORDINAL_PARAM) {
10238 compile_error(p, "ordinary parameter is defined");
10239 return false;
10240 }
10241 struct vtable *args = p->lvtbl->args;
10242 if (p->max_numparam < n) {
10243 p->max_numparam = n;
10244 }
10245 while (n > args->pos) {
10246 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10247 }
10248 return true;
10249}
10250
10251static enum yytokentype
10252parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10253{
10254 const char *ptr = p->lex.pcur;
10255 enum yytokentype result = tIVAR;
10256 register int c = nextc(p);
10257 YYLTYPE loc;
10258
10259 p->lex.ptok = ptr - 1; /* from '@' */
10260 newtok(p);
10261 tokadd(p, '@');
10262 if (c == '@') {
10263 result = tCVAR;
10264 tokadd(p, '@');
10265 c = nextc(p);
10266 }
10267 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10268 if (c == -1 || !parser_is_identchar(p)) {
10269 pushback(p, c);
10270 RUBY_SET_YYLLOC(loc);
10271 if (result == tIVAR) {
10272 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10273 }
10274 else {
10275 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10276 }
10277 parser_show_error_line(p, &loc);
10278 set_yylval_noname();
10279 SET_LEX_STATE(EXPR_END);
10280 return result;
10281 }
10282 else if (ISDIGIT(c)) {
10283 pushback(p, c);
10284 RUBY_SET_YYLLOC(loc);
10285 if (result == tIVAR) {
10286 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10287 }
10288 else {
10289 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10290 }
10291 parser_show_error_line(p, &loc);
10292 set_yylval_noname();
10293 SET_LEX_STATE(EXPR_END);
10294 return result;
10295 }
10296
10297 if (tokadd_ident(p, c)) return 0;
10298 tokenize_ident(p);
10299 return result;
10300}
10301
10302static enum yytokentype
10303parse_ident(struct parser_params *p, int c, int cmd_state)
10304{
10305 enum yytokentype result;
10306 bool is_ascii = true;
10307 const enum lex_state_e last_state = p->lex.state;
10308 ID ident;
10309 int enforce_keyword_end = 0;
10310
10311 do {
10312 if (!ISASCII(c)) is_ascii = false;
10313 if (tokadd_mbchar(p, c) == -1) return 0;
10314 c = nextc(p);
10315 } while (parser_is_identchar(p));
10316 if ((c == '!' || c == '?') && !peek(p, '=')) {
10317 result = tFID;
10318 tokadd(p, c);
10319 }
10320 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10321 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10322 result = tIDENTIFIER;
10323 tokadd(p, c);
10324 }
10325 else {
10326 result = tCONSTANT; /* assume provisionally */
10327 pushback(p, c);
10328 }
10329 tokfix(p);
10330
10331 if (IS_LABEL_POSSIBLE()) {
10332 if (IS_LABEL_SUFFIX(0)) {
10333 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10334 nextc(p);
10335 tokenize_ident(p);
10336 return tLABEL;
10337 }
10338 }
10339
10340#ifndef RIPPER
10341 if (peek_end_expect_token_locations(p)) {
10342 const rb_code_position_t *end_pos;
10343 int lineno, column;
10344 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10345
10346 end_pos = peek_end_expect_token_locations(p)->pos;
10347 lineno = end_pos->lineno;
10348 column = end_pos->column;
10349
10350 if (p->debug) {
10351 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10352 p->ruby_sourceline, beg_pos, lineno, column);
10353 }
10354
10355 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10356 const struct kwtable *kw;
10357
10358 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10359 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10360 enforce_keyword_end = 1;
10361 }
10362 }
10363 }
10364#endif
10365
10366 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10367 const struct kwtable *kw;
10368
10369 /* See if it is a reserved word. */
10370 kw = rb_reserved_word(tok(p), toklen(p));
10371 if (kw) {
10372 enum lex_state_e state = p->lex.state;
10373 if (IS_lex_state_for(state, EXPR_FNAME)) {
10374 SET_LEX_STATE(EXPR_ENDFN);
10375 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10376 return kw->id[0];
10377 }
10378 SET_LEX_STATE(kw->state);
10379 if (IS_lex_state(EXPR_BEG)) {
10380 p->command_start = TRUE;
10381 }
10382 if (kw->id[0] == keyword_do) {
10383 if (lambda_beginning_p()) {
10384 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10385 return keyword_do_LAMBDA;
10386 }
10387 if (COND_P()) return keyword_do_cond;
10388 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10389 return keyword_do_block;
10390 return keyword_do;
10391 }
10392 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10393 return kw->id[0];
10394 else {
10395 if (kw->id[0] != kw->id[1])
10396 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10397 return kw->id[1];
10398 }
10399 }
10400 }
10401
10402 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10403 if (cmd_state) {
10404 SET_LEX_STATE(EXPR_CMDARG);
10405 }
10406 else {
10407 SET_LEX_STATE(EXPR_ARG);
10408 }
10409 }
10410 else if (p->lex.state == EXPR_FNAME) {
10411 SET_LEX_STATE(EXPR_ENDFN);
10412 }
10413 else {
10414 SET_LEX_STATE(EXPR_END);
10415 }
10416
10417 ident = tokenize_ident(p);
10418 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10419 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10420 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10421 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10422 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10423 }
10424 return result;
10425}
10426
10427static void
10428warn_cr(struct parser_params *p)
10429{
10430 if (!p->cr_seen) {
10431 p->cr_seen = TRUE;
10432 /* carried over with p->lex.nextline for nextc() */
10433 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10434 }
10435}
10436
10437static enum yytokentype
10438parser_yylex(struct parser_params *p)
10439{
10440 register int c;
10441 int space_seen = 0;
10442 int cmd_state;
10443 int label;
10444 enum lex_state_e last_state;
10445 int fallthru = FALSE;
10446 int token_seen = p->token_seen;
10447
10448 if (p->lex.strterm) {
10449 if (strterm_is_heredoc(p->lex.strterm)) {
10450 token_flush(p);
10451 return here_document(p, &p->lex.strterm->u.heredoc);
10452 }
10453 else {
10454 token_flush(p);
10455 return parse_string(p, &p->lex.strterm->u.literal);
10456 }
10457 }
10458 cmd_state = p->command_start;
10459 p->command_start = FALSE;
10460 p->token_seen = TRUE;
10461#ifndef RIPPER
10462 token_flush(p);
10463#endif
10464 retry:
10465 last_state = p->lex.state;
10466 switch (c = nextc(p)) {
10467 case '\0': /* NUL */
10468 case '\004': /* ^D */
10469 case '\032': /* ^Z */
10470 case -1: /* end of script. */
10471 p->eofp = 1;
10472#ifndef RIPPER
10473 if (p->end_expect_token_locations) {
10474 pop_end_expect_token_locations(p);
10475 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10476 return tDUMNY_END;
10477 }
10478#endif
10479 /* Set location for end-of-input because dispatch_scan_event is not called. */
10480 RUBY_SET_YYLLOC(*p->yylloc);
10481 return END_OF_INPUT;
10482
10483 /* white spaces */
10484 case '\r':
10485 warn_cr(p);
10486 /* fall through */
10487 case ' ': case '\t': case '\f':
10488 case '\13': /* '\v' */
10489 space_seen = 1;
10490 while ((c = nextc(p))) {
10491 switch (c) {
10492 case '\r':
10493 warn_cr(p);
10494 /* fall through */
10495 case ' ': case '\t': case '\f':
10496 case '\13': /* '\v' */
10497 break;
10498 default:
10499 goto outofloop;
10500 }
10501 }
10502 outofloop:
10503 pushback(p, c);
10504 dispatch_scan_event(p, tSP);
10505#ifndef RIPPER
10506 token_flush(p);
10507#endif
10508 goto retry;
10509
10510 case '#': /* it's a comment */
10511 p->token_seen = token_seen;
10512 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10513 /* no magic_comment in shebang line */
10514 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10515 if (comment_at_top(p)) {
10516 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10517 }
10518 }
10519 p->lex.pcur = pcur, p->lex.ptok = ptok;
10520 lex_goto_eol(p);
10521 dispatch_scan_event(p, tCOMMENT);
10522 fallthru = TRUE;
10523 /* fall through */
10524 case '\n':
10525 p->token_seen = token_seen;
10526 rb_parser_string_t *prevline = p->lex.lastline;
10527 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10528 !IS_lex_state(EXPR_LABELED));
10529 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10530 if (!fallthru) {
10531 dispatch_scan_event(p, tIGNORED_NL);
10532 }
10533 fallthru = FALSE;
10534 if (!c && p->ctxt.in_kwarg) {
10535 goto normal_newline;
10536 }
10537 goto retry;
10538 }
10539 while (1) {
10540 switch (c = nextc(p)) {
10541 case ' ': case '\t': case '\f': case '\r':
10542 case '\13': /* '\v' */
10543 space_seen = 1;
10544 break;
10545 case '#':
10546 pushback(p, c);
10547 if (space_seen) {
10548 dispatch_scan_event(p, tSP);
10549 token_flush(p);
10550 }
10551 goto retry;
10552 case 'a':
10553 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10554 goto bol;
10555 case 'o':
10556 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10557 goto bol;
10558 case '|':
10559 if (peek(p, '|')) goto leading_logical;
10560 goto bol;
10561 case '&':
10562 if (peek(p, '&')) {
10563 leading_logical:
10564 pushback(p, c);
10565 dispatch_delayed_token(p, tIGNORED_NL);
10566 cmd_state = FALSE;
10567 goto retry;
10568 }
10569 /* fall through */
10570 case '.': {
10571 dispatch_delayed_token(p, tIGNORED_NL);
10572 if (peek(p, '.') == (c == '&')) {
10573 pushback(p, c);
10574 dispatch_scan_event(p, tSP);
10575 goto retry;
10576 }
10577 }
10578 bol:
10579 default:
10580 p->ruby_sourceline--;
10581 p->lex.nextline = p->lex.lastline;
10582 set_lastline(p, prevline);
10583 case -1: /* EOF no decrement*/
10584 if (c == -1 && space_seen) {
10585 dispatch_scan_event(p, tSP);
10586 }
10587 lex_goto_eol(p);
10588 if (c != -1) {
10589 token_flush(p);
10590 RUBY_SET_YYLLOC(*p->yylloc);
10591 }
10592 goto normal_newline;
10593 }
10594 }
10595 normal_newline:
10596 p->command_start = TRUE;
10597 SET_LEX_STATE(EXPR_BEG);
10598 return '\n';
10599
10600 case '*':
10601 if ((c = nextc(p)) == '*') {
10602 if ((c = nextc(p)) == '=') {
10603 set_yylval_id(idPow);
10604 SET_LEX_STATE(EXPR_BEG);
10605 return tOP_ASGN;
10606 }
10607 pushback(p, c);
10608 if (IS_SPCARG(c)) {
10609 rb_warning0("'**' interpreted as argument prefix");
10610 c = tDSTAR;
10611 }
10612 else if (IS_BEG()) {
10613 c = tDSTAR;
10614 }
10615 else {
10616 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10617 }
10618 }
10619 else {
10620 if (c == '=') {
10621 set_yylval_id('*');
10622 SET_LEX_STATE(EXPR_BEG);
10623 return tOP_ASGN;
10624 }
10625 pushback(p, c);
10626 if (IS_SPCARG(c)) {
10627 rb_warning0("'*' interpreted as argument prefix");
10628 c = tSTAR;
10629 }
10630 else if (IS_BEG()) {
10631 c = tSTAR;
10632 }
10633 else {
10634 c = warn_balanced('*', "*", "argument prefix");
10635 }
10636 }
10637 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10638 return c;
10639
10640 case '!':
10641 c = nextc(p);
10642 if (IS_AFTER_OPERATOR()) {
10643 SET_LEX_STATE(EXPR_ARG);
10644 if (c == '@') {
10645 return '!';
10646 }
10647 }
10648 else {
10649 SET_LEX_STATE(EXPR_BEG);
10650 }
10651 if (c == '=') {
10652 return tNEQ;
10653 }
10654 if (c == '~') {
10655 return tNMATCH;
10656 }
10657 pushback(p, c);
10658 return '!';
10659
10660 case '=':
10661 if (was_bol(p)) {
10662 /* skip embedded rd document */
10663 if (word_match_p(p, "begin", 5)) {
10664 int first_p = TRUE;
10665
10666 lex_goto_eol(p);
10667 dispatch_scan_event(p, tEMBDOC_BEG);
10668 for (;;) {
10669 lex_goto_eol(p);
10670 if (!first_p) {
10671 dispatch_scan_event(p, tEMBDOC);
10672 }
10673 first_p = FALSE;
10674 c = nextc(p);
10675 if (c == -1) {
10676 compile_error(p, "embedded document meets end of file");
10677 return END_OF_INPUT;
10678 }
10679 if (c == '=' && word_match_p(p, "end", 3)) {
10680 break;
10681 }
10682 pushback(p, c);
10683 }
10684 lex_goto_eol(p);
10685 dispatch_scan_event(p, tEMBDOC_END);
10686 goto retry;
10687 }
10688 }
10689
10690 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10691 if ((c = nextc(p)) == '=') {
10692 if ((c = nextc(p)) == '=') {
10693 return tEQQ;
10694 }
10695 pushback(p, c);
10696 return tEQ;
10697 }
10698 if (c == '~') {
10699 return tMATCH;
10700 }
10701 else if (c == '>') {
10702 return tASSOC;
10703 }
10704 pushback(p, c);
10705 return '=';
10706
10707 case '<':
10708 c = nextc(p);
10709 if (c == '<' &&
10710 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10711 !IS_END() &&
10712 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10713 enum yytokentype token = heredoc_identifier(p);
10714 if (token) return token < 0 ? 0 : token;
10715 }
10716 if (IS_AFTER_OPERATOR()) {
10717 SET_LEX_STATE(EXPR_ARG);
10718 }
10719 else {
10720 if (IS_lex_state(EXPR_CLASS))
10721 p->command_start = TRUE;
10722 SET_LEX_STATE(EXPR_BEG);
10723 }
10724 if (c == '=') {
10725 if ((c = nextc(p)) == '>') {
10726 return tCMP;
10727 }
10728 pushback(p, c);
10729 return tLEQ;
10730 }
10731 if (c == '<') {
10732 if ((c = nextc(p)) == '=') {
10733 set_yylval_id(idLTLT);
10734 SET_LEX_STATE(EXPR_BEG);
10735 return tOP_ASGN;
10736 }
10737 pushback(p, c);
10738 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10739 }
10740 pushback(p, c);
10741 return '<';
10742
10743 case '>':
10744 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10745 if ((c = nextc(p)) == '=') {
10746 return tGEQ;
10747 }
10748 if (c == '>') {
10749 if ((c = nextc(p)) == '=') {
10750 set_yylval_id(idGTGT);
10751 SET_LEX_STATE(EXPR_BEG);
10752 return tOP_ASGN;
10753 }
10754 pushback(p, c);
10755 return tRSHFT;
10756 }
10757 pushback(p, c);
10758 return '>';
10759
10760 case '"':
10761 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10762 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10763 p->lex.ptok = p->lex.pcur-1;
10764 return tSTRING_BEG;
10765
10766 case '`':
10767 if (IS_lex_state(EXPR_FNAME)) {
10768 SET_LEX_STATE(EXPR_ENDFN);
10769 return c;
10770 }
10771 if (IS_lex_state(EXPR_DOT)) {
10772 if (cmd_state)
10773 SET_LEX_STATE(EXPR_CMDARG);
10774 else
10775 SET_LEX_STATE(EXPR_ARG);
10776 return c;
10777 }
10778 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10779 return tXSTRING_BEG;
10780
10781 case '\'':
10782 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10783 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10784 p->lex.ptok = p->lex.pcur-1;
10785 return tSTRING_BEG;
10786
10787 case '?':
10788 return parse_qmark(p, space_seen);
10789
10790 case '&':
10791 if ((c = nextc(p)) == '&') {
10792 SET_LEX_STATE(EXPR_BEG);
10793 if ((c = nextc(p)) == '=') {
10794 set_yylval_id(idANDOP);
10795 SET_LEX_STATE(EXPR_BEG);
10796 return tOP_ASGN;
10797 }
10798 pushback(p, c);
10799 return tANDOP;
10800 }
10801 else if (c == '=') {
10802 set_yylval_id('&');
10803 SET_LEX_STATE(EXPR_BEG);
10804 return tOP_ASGN;
10805 }
10806 else if (c == '.') {
10807 set_yylval_id(idANDDOT);
10808 SET_LEX_STATE(EXPR_DOT);
10809 return tANDDOT;
10810 }
10811 pushback(p, c);
10812 if (IS_SPCARG(c)) {
10813 if ((c != ':') ||
10814 (c = peekc_n(p, 1)) == -1 ||
10815 !(c == '\'' || c == '"' ||
10816 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10817 rb_warning0("'&' interpreted as argument prefix");
10818 }
10819 c = tAMPER;
10820 }
10821 else if (IS_BEG()) {
10822 c = tAMPER;
10823 }
10824 else {
10825 c = warn_balanced('&', "&", "argument prefix");
10826 }
10827 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10828 return c;
10829
10830 case '|':
10831 if ((c = nextc(p)) == '|') {
10832 SET_LEX_STATE(EXPR_BEG);
10833 if ((c = nextc(p)) == '=') {
10834 set_yylval_id(idOROP);
10835 SET_LEX_STATE(EXPR_BEG);
10836 return tOP_ASGN;
10837 }
10838 pushback(p, c);
10839 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10840 c = '|';
10841 pushback(p, '|');
10842 return c;
10843 }
10844 return tOROP;
10845 }
10846 if (c == '=') {
10847 set_yylval_id('|');
10848 SET_LEX_STATE(EXPR_BEG);
10849 return tOP_ASGN;
10850 }
10851 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10852 pushback(p, c);
10853 return '|';
10854
10855 case '+':
10856 c = nextc(p);
10857 if (IS_AFTER_OPERATOR()) {
10858 SET_LEX_STATE(EXPR_ARG);
10859 if (c == '@') {
10860 return tUPLUS;
10861 }
10862 pushback(p, c);
10863 return '+';
10864 }
10865 if (c == '=') {
10866 set_yylval_id('+');
10867 SET_LEX_STATE(EXPR_BEG);
10868 return tOP_ASGN;
10869 }
10870 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10871 SET_LEX_STATE(EXPR_BEG);
10872 pushback(p, c);
10873 if (c != -1 && ISDIGIT(c)) {
10874 return parse_numeric(p, '+');
10875 }
10876 return tUPLUS;
10877 }
10878 SET_LEX_STATE(EXPR_BEG);
10879 pushback(p, c);
10880 return warn_balanced('+', "+", "unary operator");
10881
10882 case '-':
10883 c = nextc(p);
10884 if (IS_AFTER_OPERATOR()) {
10885 SET_LEX_STATE(EXPR_ARG);
10886 if (c == '@') {
10887 return tUMINUS;
10888 }
10889 pushback(p, c);
10890 return '-';
10891 }
10892 if (c == '=') {
10893 set_yylval_id('-');
10894 SET_LEX_STATE(EXPR_BEG);
10895 return tOP_ASGN;
10896 }
10897 if (c == '>') {
10898 SET_LEX_STATE(EXPR_ENDFN);
10899 yylval.num = p->lex.lpar_beg;
10900 p->lex.lpar_beg = p->lex.paren_nest;
10901 return tLAMBDA;
10902 }
10903 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10904 SET_LEX_STATE(EXPR_BEG);
10905 pushback(p, c);
10906 if (c != -1 && ISDIGIT(c)) {
10907 return tUMINUS_NUM;
10908 }
10909 return tUMINUS;
10910 }
10911 SET_LEX_STATE(EXPR_BEG);
10912 pushback(p, c);
10913 return warn_balanced('-', "-", "unary operator");
10914
10915 case '.': {
10916 int is_beg = IS_BEG();
10917 SET_LEX_STATE(EXPR_BEG);
10918 if ((c = nextc(p)) == '.') {
10919 if ((c = nextc(p)) == '.') {
10920 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10921 SET_LEX_STATE(EXPR_ENDARG);
10922 return tBDOT3;
10923 }
10924 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10925 rb_warn0("... at EOL, should be parenthesized?");
10926 }
10927 return is_beg ? tBDOT3 : tDOT3;
10928 }
10929 pushback(p, c);
10930 return is_beg ? tBDOT2 : tDOT2;
10931 }
10932 pushback(p, c);
10933 if (c != -1 && ISDIGIT(c)) {
10934 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10935 parse_numeric(p, '.');
10936 if (ISDIGIT(prev)) {
10937 yyerror0("unexpected fraction part after numeric literal");
10938 }
10939 else {
10940 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10941 }
10942 SET_LEX_STATE(EXPR_END);
10943 p->lex.ptok = p->lex.pcur;
10944 goto retry;
10945 }
10946 set_yylval_id('.');
10947 SET_LEX_STATE(EXPR_DOT);
10948 return '.';
10949 }
10950
10951 case '0': case '1': case '2': case '3': case '4':
10952 case '5': case '6': case '7': case '8': case '9':
10953 return parse_numeric(p, c);
10954
10955 case ')':
10956 COND_POP();
10957 CMDARG_POP();
10958 SET_LEX_STATE(EXPR_ENDFN);
10959 p->lex.paren_nest--;
10960 return c;
10961
10962 case ']':
10963 COND_POP();
10964 CMDARG_POP();
10965 SET_LEX_STATE(EXPR_END);
10966 p->lex.paren_nest--;
10967 return c;
10968
10969 case '}':
10970 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10971 if (!p->lex.brace_nest--) return tSTRING_DEND;
10972 COND_POP();
10973 CMDARG_POP();
10974 SET_LEX_STATE(EXPR_END);
10975 p->lex.paren_nest--;
10976 return c;
10977
10978 case ':':
10979 c = nextc(p);
10980 if (c == ':') {
10981 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10982 SET_LEX_STATE(EXPR_BEG);
10983 return tCOLON3;
10984 }
10985 set_yylval_id(idCOLON2);
10986 SET_LEX_STATE(EXPR_DOT);
10987 return tCOLON2;
10988 }
10989 if (IS_END() || ISSPACE(c) || c == '#') {
10990 pushback(p, c);
10991 c = warn_balanced(':', ":", "symbol literal");
10992 SET_LEX_STATE(EXPR_BEG);
10993 return c;
10994 }
10995 switch (c) {
10996 case '\'':
10997 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10998 break;
10999 case '"':
11000 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11001 break;
11002 default:
11003 pushback(p, c);
11004 break;
11005 }
11006 SET_LEX_STATE(EXPR_FNAME);
11007 return tSYMBEG;
11008
11009 case '/':
11010 if (IS_BEG()) {
11011 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11012 return tREGEXP_BEG;
11013 }
11014 if ((c = nextc(p)) == '=') {
11015 set_yylval_id('/');
11016 SET_LEX_STATE(EXPR_BEG);
11017 return tOP_ASGN;
11018 }
11019 pushback(p, c);
11020 if (IS_SPCARG(c)) {
11021 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11022 return tREGEXP_BEG;
11023 }
11024 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11025 return warn_balanced('/', "/", "regexp literal");
11026
11027 case '^':
11028 if ((c = nextc(p)) == '=') {
11029 set_yylval_id('^');
11030 SET_LEX_STATE(EXPR_BEG);
11031 return tOP_ASGN;
11032 }
11033 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11034 pushback(p, c);
11035 return '^';
11036
11037 case ';':
11038 SET_LEX_STATE(EXPR_BEG);
11039 p->command_start = TRUE;
11040 return ';';
11041
11042 case ',':
11043 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11044 return ',';
11045
11046 case '~':
11047 if (IS_AFTER_OPERATOR()) {
11048 if ((c = nextc(p)) != '@') {
11049 pushback(p, c);
11050 }
11051 SET_LEX_STATE(EXPR_ARG);
11052 }
11053 else {
11054 SET_LEX_STATE(EXPR_BEG);
11055 }
11056 return '~';
11057
11058 case '(':
11059 if (IS_BEG()) {
11060 c = tLPAREN;
11061 }
11062 else if (!space_seen) {
11063 /* foo( ... ) => method call, no ambiguity */
11064 }
11065 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11066 c = tLPAREN_ARG;
11067 }
11068 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11069 rb_warning0("parentheses after method name is interpreted as "
11070 "an argument list, not a decomposed argument");
11071 }
11072 p->lex.paren_nest++;
11073 COND_PUSH(0);
11074 CMDARG_PUSH(0);
11075 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11076 return c;
11077
11078 case '[':
11079 p->lex.paren_nest++;
11080 if (IS_AFTER_OPERATOR()) {
11081 if ((c = nextc(p)) == ']') {
11082 p->lex.paren_nest--;
11083 SET_LEX_STATE(EXPR_ARG);
11084 if ((c = nextc(p)) == '=') {
11085 return tASET;
11086 }
11087 pushback(p, c);
11088 return tAREF;
11089 }
11090 pushback(p, c);
11091 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11092 return '[';
11093 }
11094 else if (IS_BEG()) {
11095 c = tLBRACK;
11096 }
11097 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11098 c = tLBRACK;
11099 }
11100 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11101 COND_PUSH(0);
11102 CMDARG_PUSH(0);
11103 return c;
11104
11105 case '{':
11106 ++p->lex.brace_nest;
11107 if (lambda_beginning_p())
11108 c = tLAMBEG;
11109 else if (IS_lex_state(EXPR_LABELED))
11110 c = tLBRACE; /* hash */
11111 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11112 c = '{'; /* block (primary) */
11113 else if (IS_lex_state(EXPR_ENDARG))
11114 c = tLBRACE_ARG; /* block (expr) */
11115 else
11116 c = tLBRACE; /* hash */
11117 if (c != tLBRACE) {
11118 p->command_start = TRUE;
11119 SET_LEX_STATE(EXPR_BEG);
11120 }
11121 else {
11122 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11123 }
11124 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11125 COND_PUSH(0);
11126 CMDARG_PUSH(0);
11127 return c;
11128
11129 case '\\':
11130 c = nextc(p);
11131 if (c == '\n') {
11132 space_seen = 1;
11133 dispatch_scan_event(p, tSP);
11134 goto retry; /* skip \\n */
11135 }
11136 if (c == ' ') return tSP;
11137 if (ISSPACE(c)) return c;
11138 pushback(p, c);
11139 return '\\';
11140
11141 case '%':
11142 return parse_percent(p, space_seen, last_state);
11143
11144 case '$':
11145 return parse_gvar(p, last_state);
11146
11147 case '@':
11148 return parse_atmark(p, last_state);
11149
11150 case '_':
11151 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11152 p->ruby__end__seen = 1;
11153 p->eofp = 1;
11154#ifdef RIPPER
11155 lex_goto_eol(p);
11156 dispatch_scan_event(p, k__END__);
11157#endif
11158 return END_OF_INPUT;
11159 }
11160 newtok(p);
11161 break;
11162
11163 default:
11164 if (!parser_is_identchar(p)) {
11165 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11166 token_flush(p);
11167 goto retry;
11168 }
11169
11170 newtok(p);
11171 break;
11172 }
11173
11174 return parse_ident(p, c, cmd_state);
11175}
11176
11177static enum yytokentype
11178yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11179{
11180 enum yytokentype t;
11181
11182 p->lval = lval;
11183 lval->node = 0;
11184 p->yylloc = yylloc;
11185
11186 t = parser_yylex(p);
11187
11188 if (has_delayed_token(p))
11189 dispatch_delayed_token(p, t);
11190 else if (t != END_OF_INPUT)
11191 dispatch_scan_event(p, t);
11192
11193 return t;
11194}
11195
11196#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11197
11198static NODE*
11199node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11200{
11201 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11202
11203 rb_node_init(n, type);
11204 return n;
11205}
11206
11207static NODE *
11208nd_set_loc(NODE *nd, const YYLTYPE *loc)
11209{
11210 nd->nd_loc = *loc;
11211 nd_set_line(nd, loc->beg_pos.lineno);
11212 return nd;
11213}
11214
11215static NODE*
11216node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11217{
11218 NODE *n = node_new_internal(p, type, size, alignment);
11219
11220 nd_set_loc(n, loc);
11221 nd_set_node_id(n, parser_get_node_id(p));
11222 return n;
11223}
11224
11225#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11226
11227static rb_node_scope_t *
11228rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11229{
11230 rb_ast_id_table_t *nd_tbl;
11231 nd_tbl = local_tbl(p);
11232 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11233 n->nd_tbl = nd_tbl;
11234 n->nd_body = nd_body;
11235 n->nd_parent = nd_parent;
11236 n->nd_args = nd_args;
11237
11238 return n;
11239}
11240
11241static rb_node_scope_t *
11242rb_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)
11243{
11244 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11245 n->nd_tbl = nd_tbl;
11246 n->nd_body = nd_body;
11247 n->nd_parent = nd_parent;
11248 n->nd_args = nd_args;
11249
11250 return n;
11251}
11252
11253static rb_node_defn_t *
11254rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11255{
11256 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11257 n->nd_mid = nd_mid;
11258 n->nd_defn = nd_defn;
11259
11260 return n;
11261}
11262
11263static rb_node_defs_t *
11264rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11265{
11266 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11267 n->nd_recv = nd_recv;
11268 n->nd_mid = nd_mid;
11269 n->nd_defn = nd_defn;
11270
11271 return n;
11272}
11273
11274static rb_node_block_t *
11275rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11276{
11277 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11278 n->nd_head = nd_head;
11279 n->nd_end = (NODE *)n;
11280 n->nd_next = 0;
11281
11282 return n;
11283}
11284
11285static rb_node_for_t *
11286rb_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)
11287{
11288 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11289 n->nd_body = nd_body;
11290 n->nd_iter = nd_iter;
11291 n->for_keyword_loc = *for_keyword_loc;
11292 n->in_keyword_loc = *in_keyword_loc;
11293 n->do_keyword_loc = *do_keyword_loc;
11294 n->end_keyword_loc = *end_keyword_loc;
11295
11296 return n;
11297}
11298
11299static rb_node_for_masgn_t *
11300rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11301{
11302 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11303 n->nd_var = nd_var;
11304
11305 return n;
11306}
11307
11308static rb_node_retry_t *
11309rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11310{
11311 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11312
11313 return n;
11314}
11315
11316static rb_node_begin_t *
11317rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11318{
11319 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11320 n->nd_body = nd_body;
11321
11322 return n;
11323}
11324
11325static rb_node_rescue_t *
11326rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11327{
11328 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11329 n->nd_head = nd_head;
11330 n->nd_resq = nd_resq;
11331 n->nd_else = nd_else;
11332
11333 return n;
11334}
11335
11336static rb_node_resbody_t *
11337rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11338{
11339 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11340 n->nd_args = nd_args;
11341 n->nd_exc_var = nd_exc_var;
11342 n->nd_body = nd_body;
11343 n->nd_next = nd_next;
11344
11345 return n;
11346}
11347
11348static rb_node_ensure_t *
11349rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11350{
11351 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11352 n->nd_head = nd_head;
11353 n->nd_ensr = nd_ensr;
11354
11355 return n;
11356}
11357
11358static rb_node_and_t *
11359rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11360{
11361 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11362 n->nd_1st = nd_1st;
11363 n->nd_2nd = nd_2nd;
11364 n->operator_loc = *operator_loc;
11365
11366 return n;
11367}
11368
11369static rb_node_or_t *
11370rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11371{
11372 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11373 n->nd_1st = nd_1st;
11374 n->nd_2nd = nd_2nd;
11375 n->operator_loc = *operator_loc;
11376
11377 return n;
11378}
11379
11380static rb_node_return_t *
11381rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11382{
11383 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11384 n->nd_stts = nd_stts;
11385 n->keyword_loc = *keyword_loc;
11386 return n;
11387}
11388
11389static rb_node_yield_t *
11390rb_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)
11391{
11392 if (nd_head) no_blockarg(p, nd_head);
11393
11394 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11395 n->nd_head = nd_head;
11396 n->keyword_loc = *keyword_loc;
11397 n->lparen_loc = *lparen_loc;
11398 n->rparen_loc = *rparen_loc;
11399
11400 return n;
11401}
11402
11403static rb_node_if_t *
11404rb_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)
11405{
11406 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11407 n->nd_cond = nd_cond;
11408 n->nd_body = nd_body;
11409 n->nd_else = nd_else;
11410 n->if_keyword_loc = *if_keyword_loc;
11411 n->then_keyword_loc = *then_keyword_loc;
11412 n->end_keyword_loc = *end_keyword_loc;
11413
11414 return n;
11415}
11416
11417static rb_node_unless_t *
11418rb_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)
11419{
11420 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11421 n->nd_cond = nd_cond;
11422 n->nd_body = nd_body;
11423 n->nd_else = nd_else;
11424 n->keyword_loc = *keyword_loc;
11425 n->then_keyword_loc = *then_keyword_loc;
11426 n->end_keyword_loc = *end_keyword_loc;
11427
11428 return n;
11429}
11430
11431static rb_node_class_t *
11432rb_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)
11433{
11434 /* Keep the order of node creation */
11435 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11436 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11437 RNODE_SCOPE(scope)->nd_parent = &n->node;
11438 n->nd_cpath = nd_cpath;
11439 n->nd_body = scope;
11440 n->nd_super = nd_super;
11441 n->class_keyword_loc = *class_keyword_loc;
11442 n->inheritance_operator_loc = *inheritance_operator_loc;
11443 n->end_keyword_loc = *end_keyword_loc;
11444
11445 return n;
11446}
11447
11448static rb_node_sclass_t *
11449rb_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)
11450{
11451 /* Keep the order of node creation */
11452 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11453 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11454 RNODE_SCOPE(scope)->nd_parent = &n->node;
11455 n->nd_recv = nd_recv;
11456 n->nd_body = scope;
11457 n->class_keyword_loc = *class_keyword_loc;
11458 n->operator_loc = *operator_loc;
11459 n->end_keyword_loc = *end_keyword_loc;
11460
11461 return n;
11462}
11463
11464static rb_node_module_t *
11465rb_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)
11466{
11467 /* Keep the order of node creation */
11468 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11469 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11470 RNODE_SCOPE(scope)->nd_parent = &n->node;
11471 n->nd_cpath = nd_cpath;
11472 n->nd_body = scope;
11473 n->module_keyword_loc = *module_keyword_loc;
11474 n->end_keyword_loc = *end_keyword_loc;
11475
11476 return n;
11477}
11478
11479static rb_node_iter_t *
11480rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11481{
11482 /* Keep the order of node creation */
11483 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11484 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11485 RNODE_SCOPE(scope)->nd_parent = &n->node;
11486 n->nd_body = scope;
11487 n->nd_iter = 0;
11488
11489 return n;
11490}
11491
11492static rb_node_lambda_t *
11493rb_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)
11494{
11495 /* Keep the order of node creation */
11496 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11497 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11498 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11499 RNODE_SCOPE(scope)->nd_parent = &n->node;
11500 n->nd_body = scope;
11501 n->operator_loc = *operator_loc;
11502 n->opening_loc = *opening_loc;
11503 n->closing_loc = *closing_loc;
11504
11505 return n;
11506}
11507
11508static rb_node_case_t *
11509rb_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)
11510{
11511 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11512 n->nd_head = nd_head;
11513 n->nd_body = nd_body;
11514 n->case_keyword_loc = *case_keyword_loc;
11515 n->end_keyword_loc = *end_keyword_loc;
11516
11517 return n;
11518}
11519
11520static rb_node_case2_t *
11521rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11522{
11523 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11524 n->nd_head = 0;
11525 n->nd_body = nd_body;
11526 n->case_keyword_loc = *case_keyword_loc;
11527 n->end_keyword_loc = *end_keyword_loc;
11528
11529 return n;
11530}
11531
11532static rb_node_case3_t *
11533rb_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)
11534{
11535 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11536 n->nd_head = nd_head;
11537 n->nd_body = nd_body;
11538 n->case_keyword_loc = *case_keyword_loc;
11539 n->end_keyword_loc = *end_keyword_loc;
11540
11541 return n;
11542}
11543
11544static rb_node_when_t *
11545rb_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)
11546{
11547 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11548 n->nd_head = nd_head;
11549 n->nd_body = nd_body;
11550 n->nd_next = nd_next;
11551 n->keyword_loc = *keyword_loc;
11552 n->then_keyword_loc = *then_keyword_loc;
11553
11554 return n;
11555}
11556
11557static rb_node_in_t *
11558rb_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)
11559{
11560 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11561 n->nd_head = nd_head;
11562 n->nd_body = nd_body;
11563 n->nd_next = nd_next;
11564 n->in_keyword_loc = *in_keyword_loc;
11565 n->then_keyword_loc = *then_keyword_loc;
11566 n->operator_loc = *operator_loc;
11567
11568 return n;
11569}
11570
11571static rb_node_while_t *
11572rb_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)
11573{
11574 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11575 n->nd_cond = nd_cond;
11576 n->nd_body = nd_body;
11577 n->nd_state = nd_state;
11578 n->keyword_loc = *keyword_loc;
11579 n->closing_loc = *closing_loc;
11580
11581 return n;
11582}
11583
11584static rb_node_until_t *
11585rb_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)
11586{
11587 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11588 n->nd_cond = nd_cond;
11589 n->nd_body = nd_body;
11590 n->nd_state = nd_state;
11591 n->keyword_loc = *keyword_loc;
11592 n->closing_loc = *closing_loc;
11593
11594 return n;
11595}
11596
11597static rb_node_colon2_t *
11598rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11599{
11600 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11601 n->nd_head = nd_head;
11602 n->nd_mid = nd_mid;
11603 n->delimiter_loc = *delimiter_loc;
11604 n->name_loc = *name_loc;
11605
11606 return n;
11607}
11608
11609static rb_node_colon3_t *
11610rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11611{
11612 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11613 n->nd_mid = nd_mid;
11614 n->delimiter_loc = *delimiter_loc;
11615 n->name_loc = *name_loc;
11616
11617 return n;
11618}
11619
11620static rb_node_dot2_t *
11621rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11622{
11623 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11624 n->nd_beg = nd_beg;
11625 n->nd_end = nd_end;
11626 n->operator_loc = *operator_loc;
11627
11628 return n;
11629}
11630
11631static rb_node_dot3_t *
11632rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11633{
11634 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11635 n->nd_beg = nd_beg;
11636 n->nd_end = nd_end;
11637 n->operator_loc = *operator_loc;
11638
11639 return n;
11640}
11641
11642static rb_node_self_t *
11643rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11644{
11645 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11646 n->nd_state = 1;
11647
11648 return n;
11649}
11650
11651static rb_node_nil_t *
11652rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11653{
11654 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11655
11656 return n;
11657}
11658
11659static rb_node_true_t *
11660rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11661{
11662 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11663
11664 return n;
11665}
11666
11667static rb_node_false_t *
11668rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11669{
11670 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11671
11672 return n;
11673}
11674
11675static rb_node_super_t *
11676rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11677 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11678{
11679 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11680 n->nd_args = nd_args;
11681 n->keyword_loc = *keyword_loc;
11682 n->lparen_loc = *lparen_loc;
11683 n->rparen_loc = *rparen_loc;
11684
11685 return n;
11686}
11687
11688static rb_node_zsuper_t *
11689rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11690{
11691 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11692
11693 return n;
11694}
11695
11696static rb_node_match2_t *
11697rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11698{
11699 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11700 n->nd_recv = nd_recv;
11701 n->nd_value = nd_value;
11702 n->nd_args = 0;
11703
11704 return n;
11705}
11706
11707static rb_node_match3_t *
11708rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11709{
11710 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11711 n->nd_recv = nd_recv;
11712 n->nd_value = nd_value;
11713
11714 return n;
11715}
11716
11717static rb_node_list_t *
11718rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11719{
11720 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11721}
11722
11723static rb_node_list_t *
11724rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11725{
11726 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11727 n->nd_head = nd_head;
11728 n->as.nd_alen = nd_alen;
11729 n->nd_next = nd_next;
11730
11731 return n;
11732}
11733
11734static rb_node_zlist_t *
11735rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11736{
11737 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11738
11739 return n;
11740}
11741
11742static rb_node_hash_t *
11743rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11744{
11745 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11746 n->nd_head = nd_head;
11747 n->nd_brace = 0;
11748
11749 return n;
11750}
11751
11752static rb_node_masgn_t *
11753rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11754{
11755 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11756 n->nd_head = nd_head;
11757 n->nd_value = 0;
11758 n->nd_args = nd_args;
11759
11760 return n;
11761}
11762
11763static rb_node_gasgn_t *
11764rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11765{
11766 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11767 n->nd_vid = nd_vid;
11768 n->nd_value = nd_value;
11769
11770 return n;
11771}
11772
11773static rb_node_lasgn_t *
11774rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11775{
11776 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11777 n->nd_vid = nd_vid;
11778 n->nd_value = nd_value;
11779
11780 return n;
11781}
11782
11783static rb_node_dasgn_t *
11784rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11785{
11786 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11787 n->nd_vid = nd_vid;
11788 n->nd_value = nd_value;
11789
11790 return n;
11791}
11792
11793static rb_node_iasgn_t *
11794rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11795{
11796 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11797 n->nd_vid = nd_vid;
11798 n->nd_value = nd_value;
11799
11800 return n;
11801}
11802
11803static rb_node_cvasgn_t *
11804rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11805{
11806 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11807 n->nd_vid = nd_vid;
11808 n->nd_value = nd_value;
11809
11810 return n;
11811}
11812
11813static rb_node_op_asgn1_t *
11814rb_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)
11815{
11816 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11817 n->nd_recv = nd_recv;
11818 n->nd_mid = nd_mid;
11819 n->nd_index = index;
11820 n->nd_rvalue = rvalue;
11821 n->call_operator_loc = *call_operator_loc;
11822 n->opening_loc = *opening_loc;
11823 n->closing_loc = *closing_loc;
11824 n->binary_operator_loc = *binary_operator_loc;
11825
11826 return n;
11827}
11828
11829static rb_node_op_asgn2_t *
11830rb_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)
11831{
11832 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11833 n->nd_recv = nd_recv;
11834 n->nd_value = nd_value;
11835 n->nd_vid = nd_vid;
11836 n->nd_mid = nd_mid;
11837 n->nd_aid = nd_aid;
11838 n->call_operator_loc = *call_operator_loc;
11839 n->message_loc = *message_loc;
11840 n->binary_operator_loc = *binary_operator_loc;
11841
11842 return n;
11843}
11844
11845static rb_node_op_asgn_or_t *
11846rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11847{
11848 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11849 n->nd_head = nd_head;
11850 n->nd_value = nd_value;
11851
11852 return n;
11853}
11854
11855static rb_node_op_asgn_and_t *
11856rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11857{
11858 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11859 n->nd_head = nd_head;
11860 n->nd_value = nd_value;
11861
11862 return n;
11863}
11864
11865static rb_node_gvar_t *
11866rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11867{
11868 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11869 n->nd_vid = nd_vid;
11870
11871 return n;
11872}
11873
11874static rb_node_lvar_t *
11875rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11876{
11877 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11878 n->nd_vid = nd_vid;
11879
11880 return n;
11881}
11882
11883static rb_node_dvar_t *
11884rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11885{
11886 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11887 n->nd_vid = nd_vid;
11888
11889 return n;
11890}
11891
11892static rb_node_ivar_t *
11893rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11894{
11895 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11896 n->nd_vid = nd_vid;
11897
11898 return n;
11899}
11900
11901static rb_node_const_t *
11902rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11903{
11904 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11905 n->nd_vid = nd_vid;
11906
11907 return n;
11908}
11909
11910static rb_node_cvar_t *
11911rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11912{
11913 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11914 n->nd_vid = nd_vid;
11915
11916 return n;
11917}
11918
11919static rb_node_nth_ref_t *
11920rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11921{
11922 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11923 n->nd_nth = nd_nth;
11924
11925 return n;
11926}
11927
11928static rb_node_back_ref_t *
11929rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11930{
11931 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11932 n->nd_nth = nd_nth;
11933
11934 return n;
11935}
11936
11937static rb_node_integer_t *
11938rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11939{
11940 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11941 n->val = val;
11942 n->minus = FALSE;
11943 n->base = base;
11944
11945 return n;
11946}
11947
11948static rb_node_float_t *
11949rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11950{
11951 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11952 n->val = val;
11953 n->minus = FALSE;
11954
11955 return n;
11956}
11957
11958static rb_node_rational_t *
11959rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11960{
11961 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11962 n->val = val;
11963 n->minus = FALSE;
11964 n->base = base;
11965 n->seen_point = seen_point;
11966
11967 return n;
11968}
11969
11970static rb_node_imaginary_t *
11971rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11972{
11973 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11974 n->val = val;
11975 n->minus = FALSE;
11976 n->base = base;
11977 n->seen_point = seen_point;
11978 n->type = numeric_type;
11979
11980 return n;
11981}
11982
11983static rb_node_str_t *
11984rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11985{
11986 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11987 n->string = string;
11988
11989 return n;
11990}
11991
11992/* TODO; Use union for NODE_DSTR2 */
11993static rb_node_dstr_t *
11994rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11995{
11996 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11997 n->string = string;
11998 n->as.nd_alen = nd_alen;
11999 n->nd_next = (rb_node_list_t *)nd_next;
12000
12001 return n;
12002}
12003
12004static rb_node_dstr_t *
12005rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12006{
12007 return rb_node_dstr_new0(p, string, 1, 0, loc);
12008}
12009
12010static rb_node_xstr_t *
12011rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12012{
12013 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12014 n->string = string;
12015
12016 return n;
12017}
12018
12019static rb_node_dxstr_t *
12020rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12021{
12022 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12023 n->string = string;
12024 n->as.nd_alen = nd_alen;
12025 n->nd_next = (rb_node_list_t *)nd_next;
12026
12027 return n;
12028}
12029
12030static rb_node_sym_t *
12031rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12032{
12033 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12034 n->string = rb_str_to_parser_string(p, str);
12035
12036 return n;
12037}
12038
12039static rb_node_dsym_t *
12040rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12041{
12042 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12043 n->string = string;
12044 n->as.nd_alen = nd_alen;
12045 n->nd_next = (rb_node_list_t *)nd_next;
12046
12047 return n;
12048}
12049
12050static rb_node_evstr_t *
12051rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12052{
12053 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12054 n->nd_body = nd_body;
12055 n->opening_loc = *opening_loc;
12056 n->closing_loc = *closing_loc;
12057
12058 return n;
12059}
12060
12061static rb_node_regx_t *
12062rb_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)
12063{
12064 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12065 n->string = string;
12066 n->options = options & RE_OPTION_MASK;
12067 n->opening_loc = *opening_loc;
12068 n->content_loc = *content_loc;
12069 n->closing_loc = *closing_loc;
12070
12071 return n;
12072}
12073
12074static rb_node_call_t *
12075rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12076{
12077 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12078 n->nd_recv = nd_recv;
12079 n->nd_mid = nd_mid;
12080 n->nd_args = nd_args;
12081
12082 return n;
12083}
12084
12085static rb_node_opcall_t *
12086rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12087{
12088 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12089 n->nd_recv = nd_recv;
12090 n->nd_mid = nd_mid;
12091 n->nd_args = nd_args;
12092
12093 return n;
12094}
12095
12096static rb_node_fcall_t *
12097rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12098{
12099 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12100 n->nd_mid = nd_mid;
12101 n->nd_args = nd_args;
12102
12103 return n;
12104}
12105
12106static rb_node_qcall_t *
12107rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12108{
12109 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12110 n->nd_recv = nd_recv;
12111 n->nd_mid = nd_mid;
12112 n->nd_args = nd_args;
12113
12114 return n;
12115}
12116
12117static rb_node_vcall_t *
12118rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12119{
12120 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12121 n->nd_mid = nd_mid;
12122
12123 return n;
12124}
12125
12126static rb_node_once_t *
12127rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12128{
12129 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12130 n->nd_body = nd_body;
12131
12132 return n;
12133}
12134
12135static rb_node_args_t *
12136rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12137{
12138 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12139 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12140
12141 return n;
12142}
12143
12144static rb_node_args_aux_t *
12145rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12146{
12147 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12148 n->nd_pid = nd_pid;
12149 n->nd_plen = nd_plen;
12150 n->nd_next = 0;
12151
12152 return n;
12153}
12154
12155static rb_node_opt_arg_t *
12156rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12157{
12158 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12159 n->nd_body = nd_body;
12160 n->nd_next = 0;
12161
12162 return n;
12163}
12164
12165static rb_node_kw_arg_t *
12166rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12167{
12168 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12169 n->nd_body = nd_body;
12170 n->nd_next = 0;
12171
12172 return n;
12173}
12174
12175static rb_node_postarg_t *
12176rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12177{
12178 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12179 n->nd_1st = nd_1st;
12180 n->nd_2nd = nd_2nd;
12181
12182 return n;
12183}
12184
12185static rb_node_argscat_t *
12186rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12187{
12188 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12189 n->nd_head = nd_head;
12190 n->nd_body = nd_body;
12191
12192 return n;
12193}
12194
12195static rb_node_argspush_t *
12196rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12197{
12198 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12199 n->nd_head = nd_head;
12200 n->nd_body = nd_body;
12201
12202 return n;
12203}
12204
12205static rb_node_splat_t *
12206rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12207{
12208 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12209 n->nd_head = nd_head;
12210 n->operator_loc = *operator_loc;
12211
12212 return n;
12213}
12214
12215static rb_node_block_pass_t *
12216rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12217{
12218 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12219 n->forwarding = 0;
12220 n->nd_head = 0;
12221 n->nd_body = nd_body;
12222 n->operator_loc = *operator_loc;
12223
12224 return n;
12225}
12226
12227static rb_node_alias_t *
12228rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12229{
12230 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12231 n->nd_1st = nd_1st;
12232 n->nd_2nd = nd_2nd;
12233 n->keyword_loc = *keyword_loc;
12234
12235 return n;
12236}
12237
12238static rb_node_valias_t *
12239rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12240{
12241 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12242 n->nd_alias = nd_alias;
12243 n->nd_orig = nd_orig;
12244 n->keyword_loc = *keyword_loc;
12245
12246 return n;
12247}
12248
12249static rb_node_undef_t *
12250rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12251{
12252 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12253 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12254 n->keyword_loc = NULL_LOC;
12255 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12256
12257 return n;
12258}
12259
12260static rb_node_errinfo_t *
12261rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12262{
12263 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12264
12265 return n;
12266}
12267
12268static rb_node_defined_t *
12269rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12270{
12271 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12272 n->nd_head = nd_head;
12273 n->keyword_loc = *keyword_loc;
12274
12275 return n;
12276}
12277
12278static rb_node_postexe_t *
12279rb_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)
12280{
12281 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12282 n->nd_body = nd_body;
12283 n->keyword_loc = *keyword_loc;
12284 n->opening_loc = *opening_loc;
12285 n->closing_loc = *closing_loc;
12286
12287 return n;
12288}
12289
12290static rb_node_attrasgn_t *
12291rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12292{
12293 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12294 n->nd_recv = nd_recv;
12295 n->nd_mid = nd_mid;
12296 n->nd_args = nd_args;
12297
12298 return n;
12299}
12300
12301static rb_node_aryptn_t *
12302rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12303{
12304 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12305 n->nd_pconst = 0;
12306 n->pre_args = pre_args;
12307 n->rest_arg = rest_arg;
12308 n->post_args = post_args;
12309
12310 return n;
12311}
12312
12313static rb_node_hshptn_t *
12314rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12315{
12316 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12317 n->nd_pconst = nd_pconst;
12318 n->nd_pkwargs = nd_pkwargs;
12319 n->nd_pkwrestarg = nd_pkwrestarg;
12320
12321 return n;
12322}
12323
12324static rb_node_fndptn_t *
12325rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12326{
12327 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12328 n->nd_pconst = 0;
12329 n->pre_rest_arg = pre_rest_arg;
12330 n->args = args;
12331 n->post_rest_arg = post_rest_arg;
12332
12333 return n;
12334}
12335
12336static rb_node_line_t *
12337rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12338{
12339 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12340
12341 return n;
12342}
12343
12344static rb_node_file_t *
12345rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12346{
12347 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12348 n->path = rb_str_to_parser_string(p, str);
12349
12350 return n;
12351}
12352
12353static rb_node_encoding_t *
12354rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12355{
12356 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12357 n->enc = p->enc;
12358
12359 return n;
12360}
12361
12362static rb_node_cdecl_t *
12363rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12364{
12365 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12366 n->nd_vid = nd_vid;
12367 n->nd_value = nd_value;
12368 n->nd_else = nd_else;
12369 n->shareability = shareability;
12370
12371 return n;
12372}
12373
12374static rb_node_op_cdecl_t *
12375rb_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)
12376{
12377 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12378 n->nd_head = nd_head;
12379 n->nd_value = nd_value;
12380 n->nd_aid = nd_aid;
12381 n->shareability = shareability;
12382
12383 return n;
12384}
12385
12386static rb_node_error_t *
12387rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12388{
12389 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12390
12391 return n;
12392}
12393
12394static rb_node_break_t *
12395rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12396{
12397 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12398 n->nd_stts = nd_stts;
12399 n->nd_chain = 0;
12400 n->keyword_loc = *keyword_loc;
12401
12402 return n;
12403}
12404
12405static rb_node_next_t *
12406rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12407{
12408 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12409 n->nd_stts = nd_stts;
12410 n->nd_chain = 0;
12411 n->keyword_loc = *keyword_loc;
12412
12413 return n;
12414}
12415
12416static rb_node_redo_t *
12417rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12418{
12419 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12420 n->nd_chain = 0;
12421 n->keyword_loc = *keyword_loc;
12422
12423 return n;
12424}
12425
12426static rb_node_def_temp_t *
12427rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12428{
12429 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12430 n->save.numparam_save = 0;
12431 n->save.max_numparam = 0;
12432 n->save.ctxt = p->ctxt;
12433 n->nd_def = 0;
12434 n->nd_mid = 0;
12435
12436 return n;
12437}
12438
12439static rb_node_def_temp_t *
12440def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12441{
12442 n->save.numparam_save = numparam_push(p);
12443 n->save.max_numparam = p->max_numparam;
12444 return n;
12445}
12446
12447#ifndef RIPPER
12448static enum node_type
12449nodetype(NODE *node) /* for debug */
12450{
12451 return (enum node_type)nd_type(node);
12452}
12453
12454static int
12455nodeline(NODE *node)
12456{
12457 return nd_line(node);
12458}
12459#endif
12460
12461static NODE*
12462newline_node(NODE *node)
12463{
12464 if (node) {
12465 node = remove_begin(node);
12466 nd_set_fl_newline(node);
12467 }
12468 return node;
12469}
12470
12471static void
12472fixpos(NODE *node, NODE *orig)
12473{
12474 if (!node) return;
12475 if (!orig) return;
12476 nd_set_line(node, nd_line(orig));
12477}
12478
12479static NODE*
12480block_append(struct parser_params *p, NODE *head, NODE *tail)
12481{
12482 NODE *end, *h = head, *nd;
12483
12484 if (tail == 0) return head;
12485
12486 if (h == 0) return tail;
12487 switch (nd_type(h)) {
12488 default:
12489 h = end = NEW_BLOCK(head, &head->nd_loc);
12490 head = end;
12491 break;
12492 case NODE_BLOCK:
12493 end = RNODE_BLOCK(h)->nd_end;
12494 break;
12495 }
12496
12497 nd = RNODE_BLOCK(end)->nd_head;
12498 switch (nd_type(nd)) {
12499 case NODE_RETURN:
12500 case NODE_BREAK:
12501 case NODE_NEXT:
12502 case NODE_REDO:
12503 case NODE_RETRY:
12504 rb_warning0L(nd_line(tail), "statement not reached");
12505 break;
12506
12507 default:
12508 break;
12509 }
12510
12511 if (!nd_type_p(tail, NODE_BLOCK)) {
12512 tail = NEW_BLOCK(tail, &tail->nd_loc);
12513 }
12514 RNODE_BLOCK(end)->nd_next = tail;
12515 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12516 nd_set_last_loc(head, nd_last_loc(tail));
12517 return head;
12518}
12519
12520/* append item to the list */
12521static NODE*
12522list_append(struct parser_params *p, NODE *list, NODE *item)
12523{
12524 NODE *last;
12525
12526 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12527 if (RNODE_LIST(list)->nd_next) {
12528 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12529 }
12530 else {
12531 last = list;
12532 }
12533
12534 RNODE_LIST(list)->as.nd_alen += 1;
12535 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12536 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12537
12538 nd_set_last_loc(list, nd_last_loc(item));
12539
12540 return list;
12541}
12542
12543/* concat two lists */
12544static NODE*
12545list_concat(NODE *head, NODE *tail)
12546{
12547 NODE *last;
12548
12549 if (RNODE_LIST(head)->nd_next) {
12550 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12551 }
12552 else {
12553 last = head;
12554 }
12555
12556 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12557 RNODE_LIST(last)->nd_next = tail;
12558 if (RNODE_LIST(tail)->nd_next) {
12559 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12560 }
12561 else {
12562 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12563 }
12564
12565 nd_set_last_loc(head, nd_last_loc(tail));
12566
12567 return head;
12568}
12569
12570static int
12571literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12572{
12573 if (!tail) return 1;
12574 if (!rb_parser_enc_compatible(p, head, tail)) {
12575 compile_error(p, "string literal encodings differ (%s / %s)",
12576 rb_enc_name(rb_parser_str_get_encoding(head)),
12577 rb_enc_name(rb_parser_str_get_encoding(tail)));
12578 rb_parser_str_resize(p, head, 0);
12579 rb_parser_str_resize(p, tail, 0);
12580 return 0;
12581 }
12582 rb_parser_str_buf_append(p, head, tail);
12583 return 1;
12584}
12585
12586static rb_parser_string_t *
12587string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12588{
12589 if (htype != NODE_DSTR) return NULL;
12590 if (RNODE_DSTR(head)->nd_next) {
12591 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12592 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12593 }
12594 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12595 ASSUME(lit);
12596 return lit;
12597}
12598
12599#ifndef RIPPER
12600static rb_parser_string_t *
12601rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12602{
12603 rb_parser_string_t *copy;
12604 if (!orig) return NULL;
12605 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12606 copy->coderange = orig->coderange;
12607 copy->enc = orig->enc;
12608 return copy;
12609}
12610#endif
12611
12612/* concat two string literals */
12613static NODE *
12614literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12615{
12616 enum node_type htype;
12617 rb_parser_string_t *lit;
12618
12619 if (!head) return tail;
12620 if (!tail) return head;
12621
12622 htype = nd_type(head);
12623 if (htype == NODE_EVSTR) {
12624 head = new_dstr(p, head, loc);
12625 htype = NODE_DSTR;
12626 }
12627 if (p->heredoc_indent > 0) {
12628 switch (htype) {
12629 case NODE_STR:
12630 head = str2dstr(p, head);
12631 case NODE_DSTR:
12632 return list_append(p, head, tail);
12633 default:
12634 break;
12635 }
12636 }
12637 switch (nd_type(tail)) {
12638 case NODE_STR:
12639 if ((lit = string_literal_head(p, htype, head)) != false) {
12640 htype = NODE_STR;
12641 }
12642 else {
12643 lit = RNODE_DSTR(head)->string;
12644 }
12645 if (htype == NODE_STR) {
12646 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12647 error:
12648 rb_discard_node(p, head);
12649 rb_discard_node(p, tail);
12650 return 0;
12651 }
12652 rb_discard_node(p, tail);
12653 }
12654 else {
12655 list_append(p, head, tail);
12656 }
12657 break;
12658
12659 case NODE_DSTR:
12660 if (htype == NODE_STR) {
12661 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12662 goto error;
12663 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12664 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12665 RNODE_STR(head)->string = NULL;
12666 rb_discard_node(p, head);
12667 head = tail;
12668 }
12669 else if (!RNODE_DSTR(tail)->string) {
12670 append:
12671 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12672 if (!RNODE_DSTR(head)->nd_next) {
12673 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12674 }
12675 else if (RNODE_DSTR(tail)->nd_next) {
12676 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12677 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12678 }
12679 rb_discard_node(p, tail);
12680 }
12681 else if ((lit = string_literal_head(p, htype, head)) != false) {
12682 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12683 goto error;
12684 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12685 RNODE_DSTR(tail)->string = 0;
12686 goto append;
12687 }
12688 else {
12689 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));
12690 RNODE_DSTR(tail)->string = 0;
12691 }
12692 break;
12693
12694 case NODE_EVSTR:
12695 if (htype == NODE_STR) {
12696 head = str2dstr(p, head);
12697 RNODE_DSTR(head)->as.nd_alen = 1;
12698 }
12699 list_append(p, head, tail);
12700 break;
12701 }
12702 return head;
12703}
12704
12705static void
12706nd_copy_flag(NODE *new_node, NODE *old_node)
12707{
12708 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12709 nd_set_line(new_node, nd_line(old_node));
12710 new_node->nd_loc = old_node->nd_loc;
12711 new_node->node_id = old_node->node_id;
12712}
12713
12714static NODE *
12715str2dstr(struct parser_params *p, NODE *node)
12716{
12717 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12718 nd_copy_flag(new_node, node);
12719 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12720 RNODE_DSTR(new_node)->as.nd_alen = 0;
12721 RNODE_DSTR(new_node)->nd_next = 0;
12722 RNODE_STR(node)->string = 0;
12723
12724 return new_node;
12725}
12726
12727static NODE *
12728str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12729{
12730 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12731 nd_copy_flag(new_node, node);
12732 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12733 RNODE_REGX(new_node)->options = options;
12734 nd_set_loc(new_node, loc);
12735 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12736 RNODE_REGX(new_node)->content_loc = *content_loc;
12737 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12738 RNODE_STR(node)->string = 0;
12739
12740 return new_node;
12741}
12742
12743static NODE *
12744evstr2dstr(struct parser_params *p, NODE *node)
12745{
12746 if (nd_type_p(node, NODE_EVSTR)) {
12747 node = new_dstr(p, node, &node->nd_loc);
12748 }
12749 return node;
12750}
12751
12752static NODE *
12753new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12754{
12755 NODE *head = node;
12756
12757 if (node) {
12758 switch (nd_type(node)) {
12759 case NODE_STR:
12760 return str2dstr(p, node);
12761 case NODE_DSTR:
12762 break;
12763 case NODE_EVSTR:
12764 return node;
12765 }
12766 }
12767 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12768}
12769
12770static NODE *
12771new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12772{
12773 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12774 return list_append(p, dstr, node);
12775}
12776
12777static NODE *
12778call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12779 const YYLTYPE *op_loc, const YYLTYPE *loc)
12780{
12781 NODE *expr;
12782 value_expr(p, recv);
12783 value_expr(p, arg1);
12784 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12785 nd_set_line(expr, op_loc->beg_pos.lineno);
12786 return expr;
12787}
12788
12789static NODE *
12790call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12791{
12792 NODE *opcall;
12793 value_expr(p, recv);
12794 opcall = NEW_OPCALL(recv, id, 0, loc);
12795 nd_set_line(opcall, op_loc->beg_pos.lineno);
12796 return opcall;
12797}
12798
12799static NODE *
12800new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12801{
12802 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12803 nd_set_line(qcall, op_loc->beg_pos.lineno);
12804 return qcall;
12805}
12806
12807static NODE*
12808new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12809{
12810 NODE *ret;
12811 if (block) block_dup_check(p, args, block);
12812 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12813 if (block) ret = method_add_block(p, ret, block, loc);
12814 fixpos(ret, recv);
12815 return ret;
12816}
12817
12818static rb_locations_lambda_body_t*
12819new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12820{
12821 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12822 body->node = node;
12823 body->opening_loc = *opening_loc;
12824 body->closing_loc = *closing_loc;
12825 return body;
12826}
12827
12828static NODE *
12829command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12830{
12831 NODE **body;
12832 enum node_type type = nd_type(m);
12833 switch (type) {
12834 case NODE_YIELD:
12835 compile_error(p, "block given to yield");
12836 return m;
12837 case NODE_RETURN:
12838 body = &RNODE_RETURN(m)->nd_stts;
12839 break;
12840 case NODE_BREAK:
12841 case NODE_NEXT:
12842 body = &RNODE_EXITS(m)->nd_stts;
12843 break;
12844 case NODE_REDO: /* `redo` has no argument */
12845 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12846 return m;
12847 case NODE_RETRY: /* `retry` has no argument */
12848 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12849 return m;
12850 default:
12851 block_dup_check(p, get_nd_args(p, m), b);
12852 return method_add_block(p, m, b, loc);
12853 }
12854 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12855 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12856 *body = command_add_block(p, *body, b, &b_loc);
12857 m->nd_loc.end_pos = loc->end_pos;
12858 return m;
12859}
12860
12861#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12862
12863static NODE*
12864last_expr_once_body(NODE *node)
12865{
12866 if (!node) return 0;
12867 return nd_once_body(node);
12868}
12869
12870static NODE*
12871match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12872{
12873 NODE *n;
12874 int line = op_loc->beg_pos.lineno;
12875
12876 value_expr(p, node1);
12877 value_expr(p, node2);
12878
12879 if ((n = last_expr_once_body(node1)) != 0) {
12880 switch (nd_type(n)) {
12881 case NODE_DREGX:
12882 {
12883 NODE *match = NEW_MATCH2(node1, node2, loc);
12884 nd_set_line(match, line);
12885 return match;
12886 }
12887
12888 case NODE_REGX:
12889 {
12890 const VALUE lit = rb_node_regx_string_val(n);
12891 if (!NIL_P(lit)) {
12892 NODE *match = NEW_MATCH2(node1, node2, loc);
12893 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12894 nd_set_line(match, line);
12895 return match;
12896 }
12897 }
12898 }
12899 }
12900
12901 if ((n = last_expr_once_body(node2)) != 0) {
12902 NODE *match3;
12903
12904 switch (nd_type(n)) {
12905 case NODE_DREGX:
12906 match3 = NEW_MATCH3(node2, node1, loc);
12907 return match3;
12908 }
12909 }
12910
12911 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12912 nd_set_line(n, line);
12913 return n;
12914}
12915
12916# if WARN_PAST_SCOPE
12917static int
12918past_dvar_p(struct parser_params *p, ID id)
12919{
12920 struct vtable *past = p->lvtbl->past;
12921 while (past) {
12922 if (vtable_included(past, id)) return 1;
12923 past = past->prev;
12924 }
12925 return 0;
12926}
12927# endif
12928
12929static int
12930numparam_nested_p(struct parser_params *p)
12931{
12932 struct local_vars *local = p->lvtbl;
12933 NODE *outer = local->numparam.outer;
12934 NODE *inner = local->numparam.inner;
12935 if (outer || inner) {
12936 NODE *used = outer ? outer : inner;
12937 compile_error(p, "numbered parameter is already used in %s block\n"
12938 "%s:%d: numbered parameter is already used here",
12939 outer ? "outer" : "inner",
12940 p->ruby_sourcefile, nd_line(used));
12941 parser_show_error_line(p, &used->nd_loc);
12942 return 1;
12943 }
12944 return 0;
12945}
12946
12947static int
12948numparam_used_p(struct parser_params *p)
12949{
12950 NODE *numparam = p->lvtbl->numparam.current;
12951 if (numparam) {
12952 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12953 "%s:%d: numbered parameter is already used here",
12954 p->ruby_sourcefile, nd_line(numparam));
12955 parser_show_error_line(p, &numparam->nd_loc);
12956 return 1;
12957 }
12958 return 0;
12959}
12960
12961static int
12962it_used_p(struct parser_params *p)
12963{
12964 NODE *it = p->lvtbl->it;
12965 if (it) {
12966 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12967 "%s:%d: 'it' is already used here",
12968 p->ruby_sourcefile, nd_line(it));
12969 parser_show_error_line(p, &it->nd_loc);
12970 return 1;
12971 }
12972 return 0;
12973}
12974
12975static NODE*
12976gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12977{
12978 ID *vidp = NULL;
12979 NODE *node;
12980 switch (id) {
12981 case keyword_self:
12982 return NEW_SELF(loc);
12983 case keyword_nil:
12984 return NEW_NIL(loc);
12985 case keyword_true:
12986 return NEW_TRUE(loc);
12987 case keyword_false:
12988 return NEW_FALSE(loc);
12989 case keyword__FILE__:
12990 {
12991 VALUE file = p->ruby_sourcefile_string;
12992 if (NIL_P(file))
12993 file = rb_str_new(0, 0);
12994 node = NEW_FILE(file, loc);
12995 }
12996 return node;
12997 case keyword__LINE__:
12998 return NEW_LINE(loc);
12999 case keyword__ENCODING__:
13000 return NEW_ENCODING(loc);
13001
13002 }
13003 switch (id_type(id)) {
13004 case ID_LOCAL:
13005 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13006 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13007 if (vidp) *vidp |= LVAR_USED;
13008 node = NEW_DVAR(id, loc);
13009 return node;
13010 }
13011 if (local_id_ref(p, id, &vidp)) {
13012 if (vidp) *vidp |= LVAR_USED;
13013 node = NEW_LVAR(id, loc);
13014 return node;
13015 }
13016 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13017 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13018 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13019 node = NEW_DVAR(id, loc);
13020 struct local_vars *local = p->lvtbl;
13021 if (!local->numparam.current) local->numparam.current = node;
13022 return node;
13023 }
13024# if WARN_PAST_SCOPE
13025 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13026 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13027 }
13028# endif
13029 /* method call without arguments */
13030 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13031 if (numparam_used_p(p)) return 0;
13032 if (p->max_numparam == ORDINAL_PARAM) {
13033 compile_error(p, "ordinary parameter is defined");
13034 return 0;
13035 }
13036 if (!p->it_id) {
13037 p->it_id = idItImplicit;
13038 vtable_add(p->lvtbl->args, p->it_id);
13039 }
13040 NODE *node = NEW_DVAR(p->it_id, loc);
13041 if (!p->lvtbl->it) p->lvtbl->it = node;
13042 return node;
13043 }
13044 return NEW_VCALL(id, loc);
13045 case ID_GLOBAL:
13046 return NEW_GVAR(id, loc);
13047 case ID_INSTANCE:
13048 return NEW_IVAR(id, loc);
13049 case ID_CONST:
13050 return NEW_CONST(id, loc);
13051 case ID_CLASS:
13052 return NEW_CVAR(id, loc);
13053 }
13054 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13055 return 0;
13056}
13057
13058static rb_node_opt_arg_t *
13059opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13060{
13061 rb_node_opt_arg_t *opts = opt_list;
13062 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13063
13064 while (opts->nd_next) {
13065 opts = opts->nd_next;
13066 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13067 }
13068 opts->nd_next = opt;
13069
13070 return opt_list;
13071}
13072
13073static rb_node_kw_arg_t *
13074kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13075{
13076 if (kwlist) {
13077 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13078 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13079 }
13080 return kwlist;
13081}
13082
13083static NODE *
13084new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13085{
13086 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13087 p->ctxt.has_trailing_semicolon = 0;
13088
13089 NODE *n = expr;
13090 while (n) {
13091 if (nd_type_p(n, NODE_BEGIN)) {
13092 n = RNODE_BEGIN(n)->nd_body;
13093 }
13094 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13095 n = RNODE_BLOCK(n)->nd_head;
13096 }
13097 else {
13098 break;
13099 }
13100 }
13101
13102 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13103 NODE *block = NEW_BLOCK(expr, loc);
13104 return NEW_DEFINED(block, loc, keyword_loc);
13105 }
13106
13107 return NEW_DEFINED(n, loc, keyword_loc);
13108}
13109
13110static NODE*
13111str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13112{
13113 VALUE lit;
13114 rb_parser_string_t *str = RNODE_STR(node)->string;
13115 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13116 yyerror1(loc, "invalid symbol");
13117 lit = STR_NEW0();
13118 }
13119 else {
13120 lit = rb_str_new_parser_string(str);
13121 }
13122 return NEW_SYM(lit, loc);
13123}
13124
13125static NODE*
13126symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13127{
13128 enum node_type type = nd_type(symbol);
13129 switch (type) {
13130 case NODE_DSTR:
13131 nd_set_type(symbol, NODE_DSYM);
13132 break;
13133 case NODE_STR:
13134 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13135 break;
13136 default:
13137 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13138 }
13139 return list_append(p, symbols, symbol);
13140}
13141
13142static void
13143dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13144{
13145 if (dreg->string) {
13146 reg_fragment_setenc(p, dreg->string, options);
13147 }
13148 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13149 NODE *frag = list->nd_head;
13150 if (nd_type_p(frag, NODE_STR)) {
13151 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13152 }
13153 else if (nd_type_p(frag, NODE_DSTR)) {
13154 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13155 }
13156 }
13157}
13158
13159static NODE *
13160new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13161{
13162 if (!node) {
13163 /* Check string is valid regex */
13164 rb_parser_string_t *str = STRING_NEW0();
13165 reg_compile(p, str, options);
13166 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13167 return node;
13168 }
13169 switch (nd_type(node)) {
13170 case NODE_STR:
13171 {
13172 /* Check string is valid regex */
13173 reg_compile(p, RNODE_STR(node)->string, options);
13174 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13175 }
13176 break;
13177 default:
13178 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13179 /* fall through */
13180 case NODE_DSTR:
13181 nd_set_type(node, NODE_DREGX);
13182 nd_set_loc(node, loc);
13183 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13184 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13185 if (dreg->nd_next) {
13186 dregex_fragment_setenc(p, dreg, options);
13187 }
13188 if (options & RE_OPTION_ONCE) {
13189 node = NEW_ONCE(node, loc);
13190 }
13191 break;
13192 }
13193 return node;
13194}
13195
13196static rb_node_kw_arg_t *
13197new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13198{
13199 if (!k) return 0;
13200 return NEW_KW_ARG((k), loc);
13201}
13202
13203static NODE *
13204new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13205{
13206 if (!node) {
13207 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13208 return xstr;
13209 }
13210 switch (nd_type(node)) {
13211 case NODE_STR:
13212 nd_set_type(node, NODE_XSTR);
13213 nd_set_loc(node, loc);
13214 break;
13215 case NODE_DSTR:
13216 nd_set_type(node, NODE_DXSTR);
13217 nd_set_loc(node, loc);
13218 break;
13219 default:
13220 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13221 break;
13222 }
13223 return node;
13224}
13225
13226static const
13227struct st_hash_type literal_type = {
13228 literal_cmp,
13229 literal_hash,
13230};
13231
13232static int nd_type_st_key_enable_p(NODE *node);
13233
13234static void
13235check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13236{
13237 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13238 if (!arg || !p->case_labels) return;
13239 if (!nd_type_st_key_enable_p(arg)) return;
13240
13241 if (p->case_labels == CHECK_LITERAL_WHEN) {
13242 p->case_labels = st_init_table(&literal_type);
13243 }
13244 else {
13245 st_data_t line;
13246 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13247 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13248 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13249 return;
13250 }
13251 }
13252 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13253}
13254
13255#ifdef RIPPER
13256static int
13257id_is_var(struct parser_params *p, ID id)
13258{
13259 if (is_notop_id(id)) {
13260 switch (id & ID_SCOPE_MASK) {
13261 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13262 return 1;
13263 case ID_LOCAL:
13264 if (dyna_in_block(p)) {
13265 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13266 }
13267 if (local_id(p, id)) return 1;
13268 /* method call without arguments */
13269 return 0;
13270 }
13271 }
13272 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13273 return 0;
13274}
13275#endif
13276
13277static inline enum lex_state_e
13278parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13279{
13280 if (p->debug) {
13281 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13282 }
13283 return p->lex.state = ls;
13284}
13285
13286#ifndef RIPPER
13287static void
13288flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13289{
13290 VALUE mesg = p->debug_buffer;
13291
13292 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13293 p->debug_buffer = Qnil;
13294 rb_io_puts(1, &mesg, out);
13295 }
13296 if (!NIL_P(str) && RSTRING_LEN(str)) {
13297 rb_io_write(p->debug_output, str);
13298 }
13299}
13300
13301static const char rb_parser_lex_state_names[][8] = {
13302 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13303 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13304 "LABEL", "LABELED","FITEM",
13305};
13306
13307static VALUE
13308append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13309{
13310 int i, sep = 0;
13311 unsigned int mask = 1;
13312 static const char none[] = "NONE";
13313
13314 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13315 if ((unsigned)state & mask) {
13316 if (sep) {
13317 rb_str_cat(buf, "|", 1);
13318 }
13319 sep = 1;
13320 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13321 }
13322 }
13323 if (!sep) {
13324 rb_str_cat(buf, none, sizeof(none)-1);
13325 }
13326 return buf;
13327}
13328
13329enum lex_state_e
13330rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13331 enum lex_state_e to, int line)
13332{
13333 VALUE mesg;
13334 mesg = rb_str_new_cstr("lex_state: ");
13335 append_lex_state_name(p, from, mesg);
13336 rb_str_cat_cstr(mesg, " -> ");
13337 append_lex_state_name(p, to, mesg);
13338 rb_str_catf(mesg, " at line %d\n", line);
13339 flush_debug_buffer(p, p->debug_output, mesg);
13340 return to;
13341}
13342
13343VALUE
13344rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13345{
13346 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13347}
13348
13349static void
13350append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13351{
13352 if (stack == 0) {
13353 rb_str_cat_cstr(mesg, "0");
13354 }
13355 else {
13356 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13357 for (; mask && !(stack & mask); mask >>= 1) continue;
13358 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13359 }
13360}
13361
13362void
13363rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13364 const char *name, int line)
13365{
13366 VALUE mesg = rb_sprintf("%s: ", name);
13367 append_bitstack_value(p, stack, mesg);
13368 rb_str_catf(mesg, " at line %d\n", line);
13369 flush_debug_buffer(p, p->debug_output, mesg);
13370}
13371
13372void
13373rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13374{
13375 va_list ap;
13376 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13377
13378 va_start(ap, fmt);
13379 rb_str_vcatf(mesg, fmt, ap);
13380 va_end(ap);
13381 yyerror0(RSTRING_PTR(mesg));
13382 RB_GC_GUARD(mesg);
13383
13384 mesg = rb_str_new(0, 0);
13385 append_lex_state_name(p, p->lex.state, mesg);
13386 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13387 rb_str_resize(mesg, 0);
13388 append_bitstack_value(p, p->cond_stack, mesg);
13389 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13390 rb_str_resize(mesg, 0);
13391 append_bitstack_value(p, p->cmdarg_stack, mesg);
13392 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13393 if (p->debug_output == rb_ractor_stdout())
13394 p->debug_output = rb_ractor_stderr();
13395 p->debug = TRUE;
13396}
13397
13398static YYLTYPE *
13399rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13400{
13401 yylloc->beg_pos.lineno = sourceline;
13402 yylloc->beg_pos.column = beg_pos;
13403 yylloc->end_pos.lineno = sourceline;
13404 yylloc->end_pos.column = end_pos;
13405 return yylloc;
13406}
13407
13408YYLTYPE *
13409rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13410{
13411 int sourceline = here->sourceline;
13412 int beg_pos = (int)here->offset - here->quote
13413 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13414 int end_pos = (int)here->offset + here->length + here->quote;
13415
13416 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13417}
13418
13419YYLTYPE *
13420rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13421{
13422 yylloc->beg_pos.lineno = p->delayed.beg_line;
13423 yylloc->beg_pos.column = p->delayed.beg_col;
13424 yylloc->end_pos.lineno = p->delayed.end_line;
13425 yylloc->end_pos.column = p->delayed.end_col;
13426
13427 return yylloc;
13428}
13429
13430YYLTYPE *
13431rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13432{
13433 int sourceline = p->ruby_sourceline;
13434 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13435 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13436 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13437}
13438
13439YYLTYPE *
13440rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13441{
13442 yylloc->end_pos = yylloc->beg_pos;
13443
13444 return yylloc;
13445}
13446
13447YYLTYPE *
13448rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13449{
13450 int sourceline = p->ruby_sourceline;
13451 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13452 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13453 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13454}
13455
13456YYLTYPE *
13457rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13458{
13459 int sourceline = p->ruby_sourceline;
13460 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13461 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13462 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13463}
13464#endif /* !RIPPER */
13465
13466static int
13467assignable0(struct parser_params *p, ID id, const char **err)
13468{
13469 if (!id) return -1;
13470 switch (id) {
13471 case keyword_self:
13472 *err = "Can't change the value of self";
13473 return -1;
13474 case keyword_nil:
13475 *err = "Can't assign to nil";
13476 return -1;
13477 case keyword_true:
13478 *err = "Can't assign to true";
13479 return -1;
13480 case keyword_false:
13481 *err = "Can't assign to false";
13482 return -1;
13483 case keyword__FILE__:
13484 *err = "Can't assign to __FILE__";
13485 return -1;
13486 case keyword__LINE__:
13487 *err = "Can't assign to __LINE__";
13488 return -1;
13489 case keyword__ENCODING__:
13490 *err = "Can't assign to __ENCODING__";
13491 return -1;
13492 }
13493 switch (id_type(id)) {
13494 case ID_LOCAL:
13495 if (dyna_in_block(p)) {
13496 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13497 compile_error(p, "Can't assign to numbered parameter _%d",
13498 NUMPARAM_ID_TO_IDX(id));
13499 return -1;
13500 }
13501 if (dvar_curr(p, id)) return NODE_DASGN;
13502 if (dvar_defined(p, id)) return NODE_DASGN;
13503 if (local_id(p, id)) return NODE_LASGN;
13504 dyna_var(p, id);
13505 return NODE_DASGN;
13506 }
13507 else {
13508 if (!local_id(p, id)) local_var(p, id);
13509 return NODE_LASGN;
13510 }
13511 break;
13512 case ID_GLOBAL: return NODE_GASGN;
13513 case ID_INSTANCE: return NODE_IASGN;
13514 case ID_CONST:
13515 if (!p->ctxt.in_def) return NODE_CDECL;
13516 *err = "dynamic constant assignment";
13517 return -1;
13518 case ID_CLASS: return NODE_CVASGN;
13519 default:
13520 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13521 }
13522 return -1;
13523}
13524
13525static NODE*
13526assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13527{
13528 const char *err = 0;
13529 int node_type = assignable0(p, id, &err);
13530 switch (node_type) {
13531 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13532 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13533 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13534 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13535 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13536 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13537 }
13538/* TODO: FIXME */
13539#ifndef RIPPER
13540 if (err) yyerror1(loc, err);
13541#else
13542 if (err) set_value(assign_error(p, err, p->s_lvalue));
13543#endif
13544 return NEW_ERROR(loc);
13545}
13546
13547static int
13548is_private_local_id(struct parser_params *p, ID name)
13549{
13550 VALUE s;
13551 if (name == idUScore) return 1;
13552 if (!is_local_id(name)) return 0;
13553 s = rb_id2str(name);
13554 if (!s) return 0;
13555 return RSTRING_PTR(s)[0] == '_';
13556}
13557
13558static int
13559shadowing_lvar_0(struct parser_params *p, ID name)
13560{
13561 if (dyna_in_block(p)) {
13562 if (dvar_curr(p, name)) {
13563 if (is_private_local_id(p, name)) return 1;
13564 yyerror0("duplicated argument name");
13565 }
13566 else if (dvar_defined(p, name) || local_id(p, name)) {
13567 vtable_add(p->lvtbl->vars, name);
13568 if (p->lvtbl->used) {
13569 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13570 }
13571 return 0;
13572 }
13573 }
13574 else {
13575 if (local_id(p, name)) {
13576 if (is_private_local_id(p, name)) return 1;
13577 yyerror0("duplicated argument name");
13578 }
13579 }
13580 return 1;
13581}
13582
13583static ID
13584shadowing_lvar(struct parser_params *p, ID name)
13585{
13586 shadowing_lvar_0(p, name);
13587 return name;
13588}
13589
13590static void
13591new_bv(struct parser_params *p, ID name)
13592{
13593 if (!name) return;
13594 if (!is_local_id(name)) {
13595 compile_error(p, "invalid local variable - %"PRIsVALUE,
13596 rb_id2str(name));
13597 return;
13598 }
13599 if (!shadowing_lvar_0(p, name)) return;
13600 dyna_var(p, name);
13601 ID *vidp = 0;
13602 if (dvar_defined_ref(p, name, &vidp)) {
13603 if (vidp) *vidp |= LVAR_USED;
13604 }
13605}
13606
13607static void
13608aryset_check(struct parser_params *p, NODE *args)
13609{
13610 NODE *block = 0, *kwds = 0;
13611 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13612 block = RNODE_BLOCK_PASS(args)->nd_body;
13613 args = RNODE_BLOCK_PASS(args)->nd_head;
13614 }
13615 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13616 args = RNODE_ARGSCAT(args)->nd_body;
13617 }
13618 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13619 kwds = RNODE_ARGSPUSH(args)->nd_body;
13620 }
13621 else {
13622 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13623 next = RNODE_LIST(next)->nd_next) {
13624 kwds = RNODE_LIST(next)->nd_head;
13625 }
13626 }
13627 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13628 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13629 }
13630 if (block) {
13631 yyerror1(&block->nd_loc, "block arg given in index assignment");
13632 }
13633}
13634
13635static NODE *
13636aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13637{
13638 aryset_check(p, idx);
13639 return NEW_ATTRASGN(recv, tASET, idx, loc);
13640}
13641
13642static void
13643block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13644{
13645 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13646 compile_error(p, "both block arg and actual block given");
13647 }
13648}
13649
13650static NODE *
13651attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13652{
13653 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13654 return NEW_ATTRASGN(recv, id, 0, loc);
13655}
13656
13657static VALUE
13658rb_backref_error(struct parser_params *p, NODE *node)
13659{
13660#ifndef RIPPER
13661# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13662#else
13663# define ERR(...) rb_sprintf(__VA_ARGS__)
13664#endif
13665 switch (nd_type(node)) {
13666 case NODE_NTH_REF:
13667 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13668 case NODE_BACK_REF:
13669 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13670 }
13671#undef ERR
13672 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13673}
13674
13675static NODE *
13676arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13677{
13678 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13679 switch (nd_type(node1)) {
13680 case NODE_LIST:
13681 return list_append(p, node1, node2);
13682 case NODE_BLOCK_PASS:
13683 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13684 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13685 return node1;
13686 case NODE_ARGSPUSH:
13687 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13688 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13689 nd_set_type(node1, NODE_ARGSCAT);
13690 return node1;
13691 case NODE_ARGSCAT:
13692 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13693 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13694 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13695 return node1;
13696 }
13697 return NEW_ARGSPUSH(node1, node2, loc);
13698}
13699
13700static NODE *
13701arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13702{
13703 if (!node2) return node1;
13704 switch (nd_type(node1)) {
13705 case NODE_BLOCK_PASS:
13706 if (RNODE_BLOCK_PASS(node1)->nd_head)
13707 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13708 else
13709 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13710 return node1;
13711 case NODE_ARGSPUSH:
13712 if (!nd_type_p(node2, NODE_LIST)) break;
13713 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13714 nd_set_type(node1, NODE_ARGSCAT);
13715 return node1;
13716 case NODE_ARGSCAT:
13717 if (!nd_type_p(node2, NODE_LIST) ||
13718 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13719 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13720 return node1;
13721 }
13722 return NEW_ARGSCAT(node1, node2, loc);
13723}
13724
13725static NODE *
13726last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13727{
13728 NODE *n1;
13729 if ((n1 = splat_array(args)) != 0) {
13730 return list_append(p, n1, last_arg);
13731 }
13732 return arg_append(p, args, last_arg, loc);
13733}
13734
13735static NODE *
13736rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13737{
13738 NODE *n1;
13739 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13740 return list_concat(n1, rest_arg);
13741 }
13742 return arg_concat(p, args, rest_arg, loc);
13743}
13744
13745static NODE *
13746splat_array(NODE* node)
13747{
13748 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13749 if (nd_type_p(node, NODE_LIST)) return node;
13750 return 0;
13751}
13752
13753static void
13754mark_lvar_used(struct parser_params *p, NODE *rhs)
13755{
13756 ID *vidp = NULL;
13757 if (!rhs) return;
13758 switch (nd_type(rhs)) {
13759 case NODE_LASGN:
13760 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13761 if (vidp) *vidp |= LVAR_USED;
13762 }
13763 break;
13764 case NODE_DASGN:
13765 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13766 if (vidp) *vidp |= LVAR_USED;
13767 }
13768 break;
13769#if 0
13770 case NODE_MASGN:
13771 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13772 mark_lvar_used(p, rhs->nd_head);
13773 }
13774 break;
13775#endif
13776 }
13777}
13778
13779static int is_static_content(NODE *node);
13780
13781static NODE *
13782node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13783{
13784 if (!lhs) return 0;
13785
13786 switch (nd_type(lhs)) {
13787 case NODE_CDECL:
13788 case NODE_GASGN:
13789 case NODE_IASGN:
13790 case NODE_LASGN:
13791 case NODE_DASGN:
13792 case NODE_MASGN:
13793 case NODE_CVASGN:
13794 set_nd_value(p, lhs, rhs);
13795 nd_set_loc(lhs, loc);
13796 break;
13797
13798 case NODE_ATTRASGN:
13799 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13800 nd_set_loc(lhs, loc);
13801 break;
13802
13803 default:
13804 /* should not happen */
13805 break;
13806 }
13807
13808 return lhs;
13809}
13810
13811static NODE *
13812value_expr_check(struct parser_params *p, NODE *node)
13813{
13814 NODE *void_node = 0, *vn;
13815
13816 if (!node) {
13817 rb_warning0("empty expression");
13818 }
13819 while (node) {
13820 switch (nd_type(node)) {
13821 case NODE_ENSURE:
13822 vn = RNODE_ENSURE(node)->nd_head;
13823 node = RNODE_ENSURE(node)->nd_ensr;
13824 /* nd_ensr should not be NULL, check it out next */
13825 if (vn && (vn = value_expr_check(p, vn))) {
13826 goto found;
13827 }
13828 break;
13829
13830 case NODE_RESCUE:
13831 /* void only if all children are void */
13832 vn = RNODE_RESCUE(node)->nd_head;
13833 if (!vn || !(vn = value_expr_check(p, vn))) {
13834 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13835 }
13836 if (!void_node) void_node = vn;
13837 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13838 if (!nd_type_p(r, NODE_RESBODY)) {
13839 compile_error(p, "unexpected node");
13840 return NULL;
13841 }
13842 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13843 return NULL;
13844 }
13845 if (!void_node) void_node = vn;
13846 }
13847 node = RNODE_RESCUE(node)->nd_else;
13848 if (!node) return void_node;
13849 break;
13850
13851 case NODE_RETURN:
13852 case NODE_BREAK:
13853 case NODE_NEXT:
13854 case NODE_REDO:
13855 case NODE_RETRY:
13856 goto found;
13857
13858 case NODE_CASE:
13859 case NODE_CASE2:
13860 for (node = RNODE_CASE(node)->nd_body;
13861 node && nd_type_p(node, NODE_WHEN);
13862 node = RNODE_WHEN(node)->nd_next) {
13863 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13864 return NULL;
13865 }
13866 if (!void_node) void_node = vn;
13867 }
13868 break;
13869
13870 case NODE_CASE3:
13871 {
13872 NODE *in = RNODE_CASE3(node)->nd_body;
13873 if (!in || !nd_type_p(in, NODE_IN)) {
13874 compile_error(p, "unexpected node");
13875 return NULL;
13876 }
13877 if (!RNODE_IN(in)->nd_body) {
13878 /* single line pattern matching with "=>" operator */
13879 goto found;
13880 }
13881 do {
13882 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13883 if (!vn) return NULL;
13884 if (!void_node) void_node = vn;
13885 in = RNODE_IN(in)->nd_next;
13886 } while (in && nd_type_p(in, NODE_IN));
13887 node = in; /* else */
13888 }
13889 break;
13890
13891 case NODE_BLOCK:
13892 while (RNODE_BLOCK(node)->nd_next) {
13893 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13894 if (vn) return vn;
13895 node = RNODE_BLOCK(node)->nd_next;
13896 }
13897 node = RNODE_BLOCK(node)->nd_head;
13898 break;
13899
13900 case NODE_BEGIN:
13901 node = RNODE_BEGIN(node)->nd_body;
13902 break;
13903
13904 case NODE_IF:
13905 case NODE_UNLESS:
13906 if (!RNODE_IF(node)->nd_body) {
13907 return NULL;
13908 }
13909 else if (!RNODE_IF(node)->nd_else) {
13910 return NULL;
13911 }
13912 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13913 if (!vn) return NULL;
13914 if (!void_node) void_node = vn;
13915 node = RNODE_IF(node)->nd_else;
13916 break;
13917
13918 case NODE_AND:
13919 case NODE_OR:
13920 /* The left operand was already checked for a value when logop()
13921 * built this node, so stop here instead of re-reporting the same
13922 * void value once per operator in a chain. */
13923 return NULL;
13924
13925 case NODE_LASGN:
13926 case NODE_DASGN:
13927 case NODE_MASGN:
13928 mark_lvar_used(p, node);
13929 return NULL;
13930
13931 default:
13932 return NULL;
13933 }
13934 }
13935
13936 return NULL;
13937
13938 found:
13939 /* return the first found node */
13940 return void_node ? void_node : node;
13941}
13942
13943static int
13944value_expr(struct parser_params *p, NODE *node)
13945{
13946 NODE *void_node = value_expr_check(p, node);
13947 if (void_node) {
13948 yyerror1(&void_node->nd_loc, "void value expression");
13949 /* or "control never reach"? */
13950 return FALSE;
13951 }
13952 return TRUE;
13953}
13954
13955static void
13956void_expr(struct parser_params *p, NODE *node)
13957{
13958 const char *useless = 0;
13959
13960 if (!RTEST(ruby_verbose)) return;
13961
13962 if (!node || !(node = nd_once_body(node))) return;
13963 switch (nd_type(node)) {
13964 case NODE_OPCALL:
13965 switch (RNODE_OPCALL(node)->nd_mid) {
13966 case '+':
13967 case '-':
13968 case '*':
13969 case '/':
13970 case '%':
13971 case tPOW:
13972 case tUPLUS:
13973 case tUMINUS:
13974 case '|':
13975 case '^':
13976 case '&':
13977 case tCMP:
13978 case '>':
13979 case tGEQ:
13980 case '<':
13981 case tLEQ:
13982 case tEQ:
13983 case tNEQ:
13984 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13985 break;
13986 }
13987 break;
13988
13989 case NODE_LVAR:
13990 case NODE_DVAR:
13991 case NODE_GVAR:
13992 case NODE_IVAR:
13993 case NODE_CVAR:
13994 case NODE_NTH_REF:
13995 case NODE_BACK_REF:
13996 useless = "a variable";
13997 break;
13998 case NODE_CONST:
13999 useless = "a constant";
14000 break;
14001 case NODE_SYM:
14002 case NODE_LINE:
14003 case NODE_FILE:
14004 case NODE_ENCODING:
14005 case NODE_INTEGER:
14006 case NODE_FLOAT:
14007 case NODE_RATIONAL:
14008 case NODE_IMAGINARY:
14009 case NODE_STR:
14010 case NODE_DSTR:
14011 case NODE_REGX:
14012 case NODE_DREGX:
14013 useless = "a literal";
14014 break;
14015 case NODE_COLON2:
14016 case NODE_COLON3:
14017 useless = "::";
14018 break;
14019 case NODE_DOT2:
14020 useless = "..";
14021 break;
14022 case NODE_DOT3:
14023 useless = "...";
14024 break;
14025 case NODE_SELF:
14026 useless = "self";
14027 break;
14028 case NODE_NIL:
14029 useless = "nil";
14030 break;
14031 case NODE_TRUE:
14032 useless = "true";
14033 break;
14034 case NODE_FALSE:
14035 useless = "false";
14036 break;
14037 case NODE_DEFINED:
14038 useless = "defined?";
14039 break;
14040 }
14041
14042 if (useless) {
14043 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14044 }
14045}
14046
14047/* warns useless use of block and returns the last statement node */
14048static NODE *
14049void_stmts(struct parser_params *p, NODE *node)
14050{
14051 NODE *const n = node;
14052 if (!RTEST(ruby_verbose)) return n;
14053 if (!node) return n;
14054 if (!nd_type_p(node, NODE_BLOCK)) return n;
14055
14056 while (RNODE_BLOCK(node)->nd_next) {
14057 void_expr(p, RNODE_BLOCK(node)->nd_head);
14058 node = RNODE_BLOCK(node)->nd_next;
14059 }
14060 return RNODE_BLOCK(node)->nd_head;
14061}
14062
14063static NODE *
14064remove_begin(NODE *node)
14065{
14066 NODE **n = &node, *n1 = node;
14067 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14068 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14069 }
14070 return node;
14071}
14072
14073static void
14074reduce_nodes(struct parser_params *p, NODE **body)
14075{
14076 NODE *node = *body;
14077
14078 if (!node) {
14079 *body = NEW_NIL(&NULL_LOC);
14080 return;
14081 }
14082#define subnodes(type, n1, n2) \
14083 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14084 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14085 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14086
14087 while (node) {
14088 int newline = (int)nd_fl_newline(node);
14089 switch (nd_type(node)) {
14090 end:
14091 case NODE_NIL:
14092 *body = 0;
14093 return;
14094 case NODE_BEGIN:
14095 *body = node = RNODE_BEGIN(node)->nd_body;
14096 if (newline && node) nd_set_fl_newline(node);
14097 continue;
14098 case NODE_BLOCK:
14099 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14100 break;
14101 case NODE_IF:
14102 case NODE_UNLESS:
14103 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14104 return;
14105 case NODE_CASE:
14106 body = &RNODE_CASE(node)->nd_body;
14107 break;
14108 case NODE_WHEN:
14109 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14110 break;
14111 case NODE_ENSURE:
14112 body = &RNODE_ENSURE(node)->nd_head;
14113 break;
14114 case NODE_RESCUE:
14115 newline = 0; // RESBODY should not be a NEWLINE
14116 if (RNODE_RESCUE(node)->nd_else) {
14117 body = &RNODE_RESCUE(node)->nd_resq;
14118 break;
14119 }
14120 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14121 break;
14122 default:
14123 return;
14124 }
14125 node = *body;
14126 if (newline && node) nd_set_fl_newline(node);
14127 }
14128
14129#undef subnodes
14130}
14131
14132static int
14133is_static_content(NODE *node)
14134{
14135 if (!node) return 1;
14136 switch (nd_type(node)) {
14137 case NODE_HASH:
14138 if (!(node = RNODE_HASH(node)->nd_head)) break;
14139 case NODE_LIST:
14140 do {
14141 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14142 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14143 case NODE_SYM:
14144 case NODE_REGX:
14145 case NODE_LINE:
14146 case NODE_FILE:
14147 case NODE_ENCODING:
14148 case NODE_INTEGER:
14149 case NODE_FLOAT:
14150 case NODE_RATIONAL:
14151 case NODE_IMAGINARY:
14152 case NODE_STR:
14153 case NODE_NIL:
14154 case NODE_TRUE:
14155 case NODE_FALSE:
14156 case NODE_ZLIST:
14157 break;
14158 default:
14159 return 0;
14160 }
14161 return 1;
14162}
14163
14164static int
14165assign_in_cond(struct parser_params *p, NODE *node)
14166{
14167 switch (nd_type(node)) {
14168 case NODE_MASGN:
14169 case NODE_LASGN:
14170 case NODE_DASGN:
14171 case NODE_GASGN:
14172 case NODE_IASGN:
14173 case NODE_CVASGN:
14174 case NODE_CDECL:
14175 break;
14176
14177 default:
14178 return 0;
14179 }
14180
14181 if (!get_nd_value(p, node)) return 1;
14182 if (is_static_content(get_nd_value(p, node))) {
14183 /* reports always */
14184 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14185 }
14186 return 1;
14187}
14188
14189enum cond_type {
14190 COND_IN_OP,
14191 COND_IN_COND,
14192 COND_IN_FF
14193};
14194
14195#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14196 switch (t) { \
14197 case COND_IN_OP: break; \
14198 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14199 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14200 } \
14201} while (0)
14202
14203static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14204
14205static NODE*
14206range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14207{
14208 enum node_type type;
14209
14210 if (node == 0) return 0;
14211
14212 type = nd_type(node);
14213 value_expr(p, node);
14214 if (type == NODE_INTEGER) {
14215 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14216 ID lineno = rb_intern("$.");
14217 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14218 }
14219 return cond0(p, node, COND_IN_FF, loc, true);
14220}
14221
14222static NODE*
14223cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14224{
14225 if (node == 0) return 0;
14226 if (!(node = nd_once_body(node))) return 0;
14227 assign_in_cond(p, node);
14228
14229 switch (nd_type(node)) {
14230 case NODE_BEGIN:
14231 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14232 break;
14233
14234 case NODE_DSTR:
14235 case NODE_EVSTR:
14236 case NODE_STR:
14237 case NODE_FILE:
14238 SWITCH_BY_COND_TYPE(type, warn, "string ");
14239 break;
14240
14241 case NODE_REGX:
14242 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14243 nd_set_type(node, NODE_MATCH);
14244 break;
14245
14246 case NODE_DREGX:
14247 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14248
14249 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14250
14251 case NODE_BLOCK:
14252 {
14253 NODE *end = RNODE_BLOCK(node)->nd_end;
14254 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14255 if (top) top = node == end;
14256 *expr = cond0(p, *expr, type, loc, top);
14257 }
14258 break;
14259
14260 case NODE_AND:
14261 case NODE_OR:
14262 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14263 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14264 break;
14265
14266 case NODE_DOT2:
14267 case NODE_DOT3:
14268 if (!top) break;
14269 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14270 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14271 switch (nd_type(node)) {
14272 case NODE_DOT2:
14273 nd_set_type(node,NODE_FLIP2);
14274 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14275 (void)flip2;
14276 break;
14277 case NODE_DOT3:
14278 nd_set_type(node, NODE_FLIP3);
14279 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14280 (void)flip3;
14281 break;
14282 }
14283 break;
14284
14285 case NODE_SYM:
14286 case NODE_DSYM:
14287 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14288 break;
14289
14290 case NODE_LINE:
14291 case NODE_ENCODING:
14292 case NODE_INTEGER:
14293 case NODE_FLOAT:
14294 case NODE_RATIONAL:
14295 case NODE_IMAGINARY:
14296 SWITCH_BY_COND_TYPE(type, warning, "");
14297 break;
14298
14299 default:
14300 break;
14301 }
14302 return node;
14303}
14304
14305static NODE*
14306cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14307{
14308 if (node == 0) return 0;
14309 return cond0(p, node, COND_IN_COND, loc, true);
14310}
14311
14312static NODE*
14313method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14314{
14315 if (node == 0) return 0;
14316 return cond0(p, node, COND_IN_OP, loc, true);
14317}
14318
14319static NODE*
14320new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14321{
14322 YYLTYPE loc = {*pos, *pos};
14323 return NEW_NIL(&loc);
14324}
14325
14326static NODE*
14327new_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)
14328{
14329 if (!cc) return right;
14330 cc = cond0(p, cc, COND_IN_COND, loc, true);
14331 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14332}
14333
14334static NODE*
14335new_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)
14336{
14337 if (!cc) return right;
14338 cc = cond0(p, cc, COND_IN_COND, loc, true);
14339 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14340}
14341
14342#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))
14343
14344static NODE*
14345logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14346 const YYLTYPE *op_loc, const YYLTYPE *loc)
14347{
14348 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14349 NODE *op;
14350 value_expr(p, left);
14351 if (left && nd_type_p(left, type)) {
14352 NODE *node = left, *second;
14353 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14354 node = second;
14355 }
14356 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14357 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14358 left->nd_loc.end_pos = loc->end_pos;
14359 return left;
14360 }
14361 op = NEW_AND_OR(type, left, right, loc, op_loc);
14362 nd_set_line(op, op_loc->beg_pos.lineno);
14363 return op;
14364}
14365
14366#undef NEW_AND_OR
14367
14368static void
14369no_blockarg(struct parser_params *p, NODE *node)
14370{
14371 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14372 compile_error(p, "block argument should not be given");
14373 }
14374}
14375
14376static NODE *
14377ret_args(struct parser_params *p, NODE *node)
14378{
14379 if (node) {
14380 no_blockarg(p, node);
14381 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14382 node = RNODE_LIST(node)->nd_head;
14383 }
14384 }
14385 return node;
14386}
14387
14388static NODE*
14389negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14390{
14391 switch (nd_type(node)) {
14392 case NODE_INTEGER:
14393 RNODE_INTEGER(node)->minus = TRUE;
14394 break;
14395 case NODE_FLOAT:
14396 RNODE_FLOAT(node)->minus = TRUE;
14397 break;
14398 case NODE_RATIONAL:
14399 RNODE_RATIONAL(node)->minus = TRUE;
14400 break;
14401 case NODE_IMAGINARY:
14402 RNODE_IMAGINARY(node)->minus = TRUE;
14403 break;
14404 }
14405 node->nd_loc = *loc;
14406 return node;
14407}
14408
14409static NODE *
14410arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14411{
14412 if (node2) {
14413 if (!node1) return (NODE *)node2;
14414 node2->nd_head = node1;
14415 nd_set_first_lineno(node2, nd_first_lineno(node1));
14416 nd_set_first_column(node2, nd_first_column(node1));
14417 return (NODE *)node2;
14418 }
14419 return node1;
14420}
14421
14422static bool
14423args_info_empty_p(struct rb_args_info *args)
14424{
14425 if (args->pre_args_num) return false;
14426 if (args->post_args_num) return false;
14427 if (args->rest_arg) return false;
14428 if (args->opt_args) return false;
14429 if (args->block_arg) return false;
14430 if (args->kw_args) return false;
14431 if (args->kw_rest_arg) return false;
14432 return true;
14433}
14434
14435static rb_node_args_t *
14436new_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)
14437{
14438 struct rb_args_info *args = &tail->nd_ainfo;
14439
14440 if (args->forwarding) {
14441 if (rest_arg) {
14442 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14443 return tail;
14444 }
14445 rest_arg = idFWD_REST;
14446 }
14447
14448 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14449 args->pre_init = pre_args ? pre_args->nd_next : 0;
14450
14451 args->post_args_num = post_args ? post_args->nd_plen : 0;
14452 args->post_init = post_args ? post_args->nd_next : 0;
14453 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14454
14455 args->rest_arg = rest_arg;
14456
14457 args->opt_args = opt_args;
14458
14459 nd_set_loc(RNODE(tail), loc);
14460
14461 return tail;
14462}
14463
14464static rb_node_args_t *
14465new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14466{
14467 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14468 struct rb_args_info *args = &node->nd_ainfo;
14469 if (p->error_p) return node;
14470
14471 if (block == idNil) {
14472 block = 0;
14473 args->no_blockarg = TRUE;
14474 }
14475 args->block_arg = block;
14476 args->kw_args = kw_args;
14477
14478 if (kw_args) {
14479 /*
14480 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14481 * variable order: k1, kr1, k2, &b, internal_id, krest
14482 * #=> <reorder>
14483 * variable order: kr1, k1, k2, internal_id, krest, &b
14484 */
14485 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14486 struct vtable *vtargs = p->lvtbl->args;
14487 rb_node_kw_arg_t *kwn = kw_args;
14488
14489 if (block) block = vtargs->tbl[vtargs->pos-1];
14490 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14491 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14492 while (kwn) {
14493 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14494 --kw_vars;
14495 --required_kw_vars;
14496 kwn = kwn->nd_next;
14497 }
14498
14499 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14500 ID vid = get_nd_vid(p, kwn->nd_body);
14501 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14502 *required_kw_vars++ = vid;
14503 }
14504 else {
14505 *kw_vars++ = vid;
14506 }
14507 }
14508
14509 arg_var(p, kw_bits);
14510 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14511 if (block) arg_var(p, block);
14512
14513 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14514 }
14515 else if (kw_rest_arg == idNil) {
14516 args->no_kwarg = 1;
14517 }
14518 else if (kw_rest_arg) {
14519 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14520 }
14521
14522 return node;
14523}
14524
14525static rb_node_args_t *
14526args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14527{
14528 if (max_numparam > NO_PARAM || it_id) {
14529 if (!args) {
14530 YYLTYPE loc = RUBY_INIT_YYLLOC();
14531 args = new_empty_args_tail(p, 0);
14532 nd_set_loc(RNODE(args), &loc);
14533 }
14534 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14535 }
14536 return args;
14537}
14538
14539static NODE*
14540new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14541{
14542 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14543
14544 if (pre_arg) {
14545 NODE *pre_args = NEW_LIST(pre_arg, loc);
14546 if (RNODE_ARYPTN(aryptn)->pre_args) {
14547 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14548 }
14549 else {
14550 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14551 }
14552 }
14553 return aryptn;
14554}
14555
14556static NODE*
14557new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14558{
14559 if (has_rest) {
14560 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14561 }
14562 else {
14563 rest_arg = NULL;
14564 }
14565 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14566
14567 return node;
14568}
14569
14570static NODE*
14571new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14572{
14573 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14574
14575 return fndptn;
14576}
14577
14578static NODE*
14579new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14580{
14581 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14582 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14583 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14584
14585 return node;
14586}
14587
14588static NODE*
14589new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14590{
14591 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14592 return hshptn;
14593}
14594
14595static NODE*
14596new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14597{
14598 NODE *node, *kw_rest_arg_node;
14599
14600 if (kw_rest_arg == idNil) {
14601 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14602 }
14603 else if (kw_rest_arg) {
14604 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14605 }
14606 else {
14607 kw_rest_arg_node = NULL;
14608 }
14609
14610 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14611
14612 return node;
14613}
14614
14615static NODE*
14616dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14617{
14618 if (!node) {
14619 return NEW_SYM(STR_NEW0(), loc);
14620 }
14621
14622 switch (nd_type(node)) {
14623 case NODE_DSTR:
14624 nd_set_type(node, NODE_DSYM);
14625 nd_set_loc(node, loc);
14626 break;
14627 case NODE_STR:
14628 node = str_to_sym_node(p, node, loc);
14629 break;
14630 default:
14631 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14632 break;
14633 }
14634 return node;
14635}
14636
14637static int
14638nd_type_st_key_enable_p(NODE *node)
14639{
14640 switch (nd_type(node)) {
14641 case NODE_INTEGER:
14642 case NODE_FLOAT:
14643 case NODE_RATIONAL:
14644 case NODE_IMAGINARY:
14645 case NODE_STR:
14646 case NODE_SYM:
14647 case NODE_REGX:
14648 case NODE_LINE:
14649 case NODE_FILE:
14650 case NODE_ENCODING:
14651 return true;
14652 default:
14653 return false;
14654 }
14655}
14656
14657static VALUE
14658nd_value(struct parser_params *p, NODE *node)
14659{
14660 switch (nd_type(node)) {
14661 case NODE_STR:
14662 return rb_node_str_string_val(node);
14663 case NODE_INTEGER:
14664 return rb_node_integer_literal_val(node);
14665 case NODE_FLOAT:
14666 return rb_node_float_literal_val(node);
14667 case NODE_RATIONAL:
14668 return rb_node_rational_literal_val(node);
14669 case NODE_IMAGINARY:
14670 return rb_node_imaginary_literal_val(node);
14671 case NODE_SYM:
14672 return rb_node_sym_string_val(node);
14673 case NODE_REGX:
14674 return rb_node_regx_string_val(node);
14675 case NODE_LINE:
14676 return rb_node_line_lineno_val(node);
14677 case NODE_ENCODING:
14678 return rb_node_encoding_val(node);
14679 case NODE_FILE:
14680 return rb_node_file_path_val(node);
14681 default:
14682 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14683 UNREACHABLE_RETURN(0);
14684 }
14685}
14686
14687static void
14688warn_duplicate_keys(struct parser_params *p, NODE *hash)
14689{
14690 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14691 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14692 while (hash && RNODE_LIST(hash)->nd_next) {
14693 NODE *head = RNODE_LIST(hash)->nd_head;
14694 NODE *value = RNODE_LIST(hash)->nd_next;
14695 NODE *next = RNODE_LIST(value)->nd_next;
14696 st_data_t key;
14697 st_data_t data;
14698
14699 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14700 if (!head) {
14701 head = value;
14702 }
14703
14704 if (nd_type_st_key_enable_p(head)) {
14705 key = (st_data_t)head;
14706
14707 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14708 rb_warn2L(nd_line((NODE *)data),
14709 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14710 nd_value(p, head), WARN_I(nd_line(head)));
14711 }
14712 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14713 }
14714 hash = next;
14715 }
14716 st_free_table(p->warn_duplicate_keys_table);
14717 p->warn_duplicate_keys_table = NULL;
14718}
14719
14720static NODE *
14721new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14722{
14723 if (hash) warn_duplicate_keys(p, hash);
14724 return NEW_HASH(hash, loc);
14725}
14726
14727static void
14728error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14729{
14730 if (is_private_local_id(p, id)) {
14731 return;
14732 }
14733 if (st_is_member(p->pvtbl, id)) {
14734 yyerror1(loc, "duplicated variable name");
14735 }
14736 else if (p->ctxt.in_alt_pattern && id) {
14737 yyerror1(loc, "variable capture in alternative pattern");
14738 }
14739 else {
14740 p->ctxt.capture_in_pattern = 1;
14741 st_insert(p->pvtbl, (st_data_t)id, 0);
14742 }
14743}
14744
14745static void
14746error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14747{
14748 if (!p->pktbl) {
14749 p->pktbl = st_init_numtable();
14750 }
14751 else if (st_is_member(p->pktbl, key)) {
14752 yyerror1(loc, "duplicated key name");
14753 return;
14754 }
14755 st_insert(p->pktbl, (st_data_t)key, 0);
14756}
14757
14758static NODE *
14759new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14760{
14761 return NEW_HASH(hash, loc);
14762}
14763
14764static NODE *
14765new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14766{
14767 NODE *asgn;
14768
14769 if (lhs) {
14770 ID vid = get_nd_vid(p, lhs);
14771 YYLTYPE lhs_loc = lhs->nd_loc;
14772 if (op == tOROP) {
14773 set_nd_value(p, lhs, rhs);
14774 nd_set_loc(lhs, loc);
14775 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14776 }
14777 else if (op == tANDOP) {
14778 set_nd_value(p, lhs, rhs);
14779 nd_set_loc(lhs, loc);
14780 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14781 }
14782 else {
14783 asgn = lhs;
14784 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14785 set_nd_value(p, asgn, rhs);
14786 nd_set_loc(asgn, loc);
14787 }
14788 }
14789 else {
14790 asgn = NEW_ERROR(loc);
14791 }
14792 return asgn;
14793}
14794
14795static NODE *
14796new_ary_op_assign(struct parser_params *p, NODE *ary,
14797 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14798 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14799{
14800 NODE *asgn;
14801
14802 aryset_check(p, args);
14803 args = make_list(args, args_loc);
14804 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14805 fixpos(asgn, ary);
14806 return asgn;
14807}
14808
14809static NODE *
14810new_attr_op_assign(struct parser_params *p, NODE *lhs,
14811 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14812 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14813{
14814 NODE *asgn;
14815
14816 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14817 fixpos(asgn, lhs);
14818 return asgn;
14819}
14820
14821static NODE *
14822new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14823{
14824 NODE *asgn;
14825
14826 if (lhs) {
14827 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14828 }
14829 else {
14830 asgn = NEW_ERROR(loc);
14831 }
14832 fixpos(asgn, lhs);
14833 return asgn;
14834}
14835
14836static NODE *
14837const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14838{
14839 if (p->ctxt.in_def) {
14840#ifndef RIPPER
14841 yyerror1(loc, "dynamic constant assignment");
14842#else
14843 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14844#endif
14845 }
14846 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14847}
14848
14849#ifdef RIPPER
14850static VALUE
14851assign_error(struct parser_params *p, const char *mesg, VALUE a)
14852{
14853 a = dispatch2(assign_error, ERR_MESG(), a);
14854 ripper_error(p);
14855 return a;
14856}
14857#endif
14858
14859static NODE *
14860new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14861{
14862 NODE *result = head;
14863 if (rescue) {
14864 NODE *tmp = rescue_else ? rescue_else : rescue;
14865 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14866
14867 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14868 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14869 }
14870 if (ensure) {
14871 result = NEW_ENSURE(result, ensure, loc);
14872 }
14873 fixpos(result, head);
14874 return result;
14875}
14876
14877static void
14878warn_unused_var(struct parser_params *p, struct local_vars *local)
14879{
14880 int cnt;
14881
14882 if (!local->used) return;
14883 cnt = local->used->pos;
14884 if (cnt != local->vars->pos) {
14885 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14886 }
14887#ifndef RIPPER
14888 ID *v = local->vars->tbl;
14889 ID *u = local->used->tbl;
14890 for (int i = 0; i < cnt; ++i) {
14891 if (!v[i] || (u[i] & LVAR_USED)) continue;
14892 if (is_private_local_id(p, v[i])) continue;
14893 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14894 }
14895#endif
14896}
14897
14898static void
14899local_push(struct parser_params *p, int toplevel_scope)
14900{
14901 struct local_vars *local;
14902 int inherits_dvars = toplevel_scope && compile_for_eval;
14903 int warn_unused_vars = RTEST(ruby_verbose);
14904
14905 local = ALLOC(struct local_vars);
14906 local->prev = p->lvtbl;
14907 local->args = vtable_alloc(0);
14908 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14909#ifndef RIPPER
14910 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14911 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14912#endif
14913 local->numparam.outer = 0;
14914 local->numparam.inner = 0;
14915 local->numparam.current = 0;
14916 local->it = 0;
14917 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14918
14919# if WARN_PAST_SCOPE
14920 local->past = 0;
14921# endif
14922 CMDARG_PUSH(0);
14923 COND_PUSH(0);
14924 p->lvtbl = local;
14925}
14926
14927static void
14928vtable_chain_free(struct parser_params *p, struct vtable *table)
14929{
14930 while (!DVARS_TERMINAL_P(table)) {
14931 struct vtable *cur_table = table;
14932 table = cur_table->prev;
14933 vtable_free(cur_table);
14934 }
14935}
14936
14937static void
14938local_free(struct parser_params *p, struct local_vars *local)
14939{
14940 vtable_chain_free(p, local->used);
14941
14942# if WARN_PAST_SCOPE
14943 vtable_chain_free(p, local->past);
14944# endif
14945
14946 vtable_chain_free(p, local->args);
14947 vtable_chain_free(p, local->vars);
14948
14949 ruby_xfree_sized(local, sizeof(struct local_vars));
14950}
14951
14952static void
14953local_pop(struct parser_params *p)
14954{
14955 struct local_vars *local = p->lvtbl->prev;
14956 if (p->lvtbl->used) {
14957 warn_unused_var(p, p->lvtbl);
14958 }
14959
14960 local_free(p, p->lvtbl);
14961 p->lvtbl = local;
14962
14963 CMDARG_POP();
14964 COND_POP();
14965}
14966
14967static rb_ast_id_table_t *
14968local_tbl(struct parser_params *p)
14969{
14970 int cnt_args = vtable_size(p->lvtbl->args);
14971 int cnt_vars = vtable_size(p->lvtbl->vars);
14972 int cnt = cnt_args + cnt_vars;
14973 int i, j;
14974 rb_ast_id_table_t *tbl;
14975
14976 if (cnt <= 0) return 0;
14977 tbl = rb_ast_new_local_table(p->ast, cnt);
14978 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14979 /* remove IDs duplicated to warn shadowing */
14980 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14981 ID id = p->lvtbl->vars->tbl[i];
14982 if (!vtable_included(p->lvtbl->args, id)) {
14983 tbl->ids[j++] = id;
14984 }
14985 }
14986 if (j < cnt) {
14987 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14988 }
14989
14990 return tbl;
14991}
14992
14993static void
14994numparam_name(struct parser_params *p, ID id)
14995{
14996 if (!NUMPARAM_ID_P(id)) return;
14997 compile_error(p, "_%d is reserved for numbered parameter",
14998 NUMPARAM_ID_TO_IDX(id));
14999}
15000
15001static void
15002arg_var(struct parser_params *p, ID id)
15003{
15004 numparam_name(p, id);
15005 vtable_add(p->lvtbl->args, id);
15006}
15007
15008static void
15009local_var(struct parser_params *p, ID id)
15010{
15011 numparam_name(p, id);
15012 vtable_add(p->lvtbl->vars, id);
15013 if (p->lvtbl->used) {
15014 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15015 }
15016}
15017
15018#ifndef RIPPER
15019int
15020rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15021{
15022 return rb_local_defined(id, iseq);
15023}
15024#endif
15025
15026static int
15027local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15028{
15029 struct vtable *vars, *args, *used;
15030
15031 vars = p->lvtbl->vars;
15032 args = p->lvtbl->args;
15033 used = p->lvtbl->used;
15034
15035 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15036 vars = vars->prev;
15037 args = args->prev;
15038 if (used) used = used->prev;
15039 }
15040
15041 if (vars && vars->prev == DVARS_INHERIT) {
15042 return rb_parser_local_defined(p, id, p->parent_iseq);
15043 }
15044 else if (vtable_included(args, id)) {
15045 return 1;
15046 }
15047 else {
15048 int i = vtable_included(vars, id);
15049 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15050 return i != 0;
15051 }
15052}
15053
15054static int
15055local_id(struct parser_params *p, ID id)
15056{
15057 return local_id_ref(p, id, NULL);
15058}
15059
15060static int
15061check_forwarding_args(struct parser_params *p)
15062{
15063 if (local_id(p, idFWD_ALL)) return TRUE;
15064 compile_error(p, "unexpected ...");
15065 return FALSE;
15066}
15067
15068static void
15069add_forwarding_args(struct parser_params *p)
15070{
15071 arg_var(p, idFWD_REST);
15072 arg_var(p, idFWD_KWREST);
15073 arg_var(p, idFWD_BLOCK);
15074 arg_var(p, idFWD_ALL);
15075}
15076
15077static void
15078forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15079{
15080 bool conflict = false;
15081
15082 struct vtable *vars, *args;
15083
15084 vars = p->lvtbl->vars;
15085 args = p->lvtbl->args;
15086
15087 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15088 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15089 vars = vars->prev;
15090 args = args->prev;
15091 }
15092
15093 bool found = false;
15094 if (vars && vars->prev == DVARS_INHERIT && !found) {
15095 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15096 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15097 }
15098 else {
15099 found = (vtable_included(args, arg) &&
15100 !(all && vtable_included(args, all)));
15101 }
15102
15103 if (!found) {
15104 compile_error(p, "no anonymous %s parameter", var);
15105 }
15106 else if (conflict) {
15107 compile_error(p, "anonymous %s parameter is also used within block", var);
15108 }
15109}
15110
15111static NODE *
15112new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15113{
15114 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15115 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15116 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15117 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15118 block->forwarding = TRUE;
15119 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15120 return arg_blk_pass(args, block);
15121}
15122
15123static NODE *
15124numparam_push(struct parser_params *p)
15125{
15126 struct local_vars *local = p->lvtbl;
15127 NODE *inner = local->numparam.inner;
15128 if (!local->numparam.outer) {
15129 local->numparam.outer = local->numparam.current;
15130 }
15131 local->numparam.inner = 0;
15132 local->numparam.current = 0;
15133 local->it = 0;
15134 return inner;
15135}
15136
15137static void
15138numparam_pop(struct parser_params *p, NODE *prev_inner)
15139{
15140 struct local_vars *local = p->lvtbl;
15141 if (prev_inner) {
15142 /* prefer first one */
15143 local->numparam.inner = prev_inner;
15144 }
15145 else if (local->numparam.current) {
15146 /* current and inner are exclusive */
15147 local->numparam.inner = local->numparam.current;
15148 }
15149 if (p->max_numparam > NO_PARAM) {
15150 /* current and outer are exclusive */
15151 local->numparam.current = local->numparam.outer;
15152 local->numparam.outer = 0;
15153 }
15154 else {
15155 /* no numbered parameter */
15156 local->numparam.current = 0;
15157 }
15158 local->it = 0;
15159}
15160
15161static const struct vtable *
15162dyna_push(struct parser_params *p)
15163{
15164 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15165 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15166 if (p->lvtbl->used) {
15167 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15168 }
15169 return p->lvtbl->args;
15170}
15171
15172static void
15173dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15174{
15175 struct vtable *tmp = *vtblp;
15176 *vtblp = tmp->prev;
15177# if WARN_PAST_SCOPE
15178 if (p->past_scope_enabled) {
15179 tmp->prev = p->lvtbl->past;
15180 p->lvtbl->past = tmp;
15181 return;
15182 }
15183# endif
15184 vtable_free(tmp);
15185}
15186
15187static void
15188dyna_pop_1(struct parser_params *p)
15189{
15190 struct vtable *tmp;
15191
15192 if ((tmp = p->lvtbl->used) != 0) {
15193 warn_unused_var(p, p->lvtbl);
15194 p->lvtbl->used = p->lvtbl->used->prev;
15195 vtable_free(tmp);
15196 }
15197 dyna_pop_vtable(p, &p->lvtbl->args);
15198 dyna_pop_vtable(p, &p->lvtbl->vars);
15199}
15200
15201static void
15202dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15203{
15204 while (p->lvtbl->args != lvargs) {
15205 dyna_pop_1(p);
15206 if (!p->lvtbl->args) {
15207 struct local_vars *local = p->lvtbl->prev;
15208 ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl));
15209 p->lvtbl = local;
15210 }
15211 }
15212 dyna_pop_1(p);
15213}
15214
15215static int
15216dyna_in_block(struct parser_params *p)
15217{
15218 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15219}
15220
15221#ifndef RIPPER
15222int
15223dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15224{
15225 struct vtable *vars, *args, *used;
15226 int i;
15227
15228 args = p->lvtbl->args;
15229 vars = p->lvtbl->vars;
15230 used = p->lvtbl->used;
15231
15232 while (!DVARS_TERMINAL_P(vars)) {
15233 if (vtable_included(args, id)) {
15234 return 1;
15235 }
15236 if ((i = vtable_included(vars, id)) != 0) {
15237 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15238 return 1;
15239 }
15240 args = args->prev;
15241 vars = vars->prev;
15242 if (!vidrefp) used = 0;
15243 if (used) used = used->prev;
15244 }
15245
15246 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15247 return rb_dvar_defined(id, p->parent_iseq);
15248 }
15249
15250 return 0;
15251}
15252#endif
15253
15254static int
15255dvar_defined(struct parser_params *p, ID id)
15256{
15257 return dvar_defined_ref(p, id, NULL);
15258}
15259
15260static int
15261dvar_curr(struct parser_params *p, ID id)
15262{
15263 return (vtable_included(p->lvtbl->args, id) ||
15264 vtable_included(p->lvtbl->vars, id));
15265}
15266
15267static void
15268reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15269{
15270 compile_error(p,
15271 "regexp encoding option '%c' differs from source encoding '%s'",
15272 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15273}
15274
15275#ifndef RIPPER
15276static rb_encoding *
15277find_enc(struct parser_params* p, const char *name)
15278{
15279 int idx = rb_enc_find_index(name);
15280 if (idx < 0) {
15281 rb_bug("unknown encoding name: %s", name);
15282 }
15283
15284 return rb_enc_from_index(idx);
15285}
15286
15287static rb_encoding *
15288kcode_to_enc(struct parser_params* p, int kcode)
15289{
15290 rb_encoding *enc;
15291
15292 switch (kcode) {
15293 case ENC_ASCII8BIT:
15294 enc = rb_ascii8bit_encoding();
15295 break;
15296 case ENC_EUC_JP:
15297 enc = find_enc(p, "EUC-JP");
15298 break;
15299 case ENC_Windows_31J:
15300 enc = find_enc(p, "Windows-31J");
15301 break;
15302 case ENC_UTF8:
15303 enc = rb_utf8_encoding();
15304 break;
15305 default:
15306 enc = NULL;
15307 break;
15308 }
15309
15310 return enc;
15311}
15312
15313int
15314rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15315{
15316 int c = RE_OPTION_ENCODING_IDX(options);
15317
15318 if (c) {
15319 int opt, idx;
15320 rb_encoding *enc;
15321
15322 char_to_option_kcode(c, &opt, &idx);
15323 enc = kcode_to_enc(p, idx);
15324 if (enc != rb_parser_str_get_encoding(str) &&
15325 !rb_parser_is_ascii_string(p, str)) {
15326 goto error;
15327 }
15328 rb_parser_string_set_encoding(str, enc);
15329 }
15330 else if (RE_OPTION_ENCODING_NONE(options)) {
15331 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15332 !rb_parser_is_ascii_string(p, str)) {
15333 c = 'n';
15334 goto error;
15335 }
15336 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15337 }
15338 else if (rb_is_usascii_enc(p->enc)) {
15339 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15340 }
15341 return 0;
15342
15343 error:
15344 return c;
15345}
15346#endif
15347
15348static void
15349reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15350{
15351 int c = rb_reg_fragment_setenc(p, str, options);
15352 if (c) reg_fragment_enc_error(p, str, c);
15353}
15354
15355#ifndef UNIVERSAL_PARSER
15356typedef struct {
15357 struct parser_params* parser;
15358 rb_encoding *enc;
15359 NODE *succ_block;
15360 const YYLTYPE *loc;
15361 rb_parser_assignable_func assignable;
15362} reg_named_capture_assign_t;
15363
15364static int
15365reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15366 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15367{
15368 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15369 struct parser_params* p = arg->parser;
15370 rb_encoding *enc = arg->enc;
15371 long len = name_end - name;
15372 const char *s = (const char *)name;
15373
15374 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15375}
15376
15377static NODE *
15378reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15379{
15380 reg_named_capture_assign_t arg;
15381
15382 arg.parser = p;
15383 arg.enc = rb_enc_get(regexp);
15384 arg.succ_block = 0;
15385 arg.loc = loc;
15386 arg.assignable = assignable;
15387 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15388
15389 if (!arg.succ_block) return 0;
15390 return RNODE_BLOCK(arg.succ_block)->nd_next;
15391}
15392#endif
15393
15394#ifndef RIPPER
15395NODE *
15396rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15397{
15398 return assignable(p, id, val, loc);
15399}
15400
15401int
15402rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15403 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15404{
15405 ID var;
15406 NODE *node, *succ;
15407
15408 if (!len) return ST_CONTINUE;
15409 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15410 return ST_CONTINUE;
15411
15412 var = intern_cstr(s, len, enc);
15413 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15414 if (!lvar_defined(p, var)) return ST_CONTINUE;
15415 }
15416 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15417 succ = *succ_block;
15418 if (!succ) succ = NEW_ERROR(loc);
15419 succ = block_append(p, succ, node);
15420 *succ_block = succ;
15421 return ST_CONTINUE;
15422}
15423#endif
15424
15425static VALUE
15426parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15427{
15428 VALUE str2;
15429 reg_fragment_setenc(p, str, options);
15430 str2 = rb_str_new_parser_string(str);
15431 return rb_parser_reg_compile(p, str2, options);
15432}
15433
15434#ifndef RIPPER
15435VALUE
15436rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15437{
15438 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15439}
15440#endif
15441
15442static VALUE
15443reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15444{
15445 VALUE re;
15446 VALUE err;
15447
15448 err = rb_errinfo();
15449 re = parser_reg_compile(p, str, options);
15450 if (NIL_P(re)) {
15451 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15452 rb_set_errinfo(err);
15453 compile_error(p, "%"PRIsVALUE, m);
15454 return Qnil;
15455 }
15456 return re;
15457}
15458
15459#ifndef RIPPER
15460void
15461rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15462{
15463 p->do_print = print;
15464 p->do_loop = loop;
15465 p->do_chomp = chomp;
15466 p->do_split = split;
15467}
15468
15469static NODE *
15470parser_append_options(struct parser_params *p, NODE *node)
15471{
15472 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15473 const YYLTYPE *const LOC = &default_location;
15474
15475 if (p->do_print) {
15476 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15477 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15478 LOC);
15479 node = block_append(p, node, print);
15480 }
15481
15482 if (p->do_loop) {
15483 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15484
15485 if (p->do_split) {
15486 ID ifs = rb_intern("$;");
15487 ID fields = rb_intern("$F");
15488 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15489 NODE *split = NEW_GASGN(fields,
15490 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15491 rb_intern("split"), args, LOC),
15492 LOC);
15493 node = block_append(p, split, node);
15494 }
15495 if (p->do_chomp) {
15496 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15497 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15498 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15499 }
15500
15501 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15502 }
15503
15504 return node;
15505}
15506
15507void
15508rb_init_parse(void)
15509{
15510 /* just to suppress unused-function warnings */
15511 (void)nodetype;
15512 (void)nodeline;
15513}
15514
15515ID
15516internal_id(struct parser_params *p)
15517{
15518 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15519}
15520#endif /* !RIPPER */
15521
15522static void
15523parser_initialize(struct parser_params *p)
15524{
15525 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15526 p->command_start = TRUE;
15527 p->ruby_sourcefile_string = Qnil;
15528 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15529 string_buffer_init(p);
15530 p->node_id = 0;
15531 p->delayed.token = NULL;
15532 p->frozen_string_literal = -1; /* not specified */
15533 rb_source_hash_init(&p->source_hash);
15534#ifndef RIPPER
15535 p->error_buffer = Qfalse;
15536 p->end_expect_token_locations = NULL;
15537 p->token_id = 0;
15538 p->tokens = NULL;
15539#else
15540 p->result = Qnil;
15541 p->parsing_thread = Qnil;
15542 p->s_value = Qnil;
15543 p->s_lvalue = Qnil;
15544 p->s_value_stack = rb_ary_new();
15545#endif
15546 p->debug_buffer = Qnil;
15547 p->debug_output = rb_ractor_stdout();
15548 p->enc = rb_utf8_encoding();
15549 p->exits = 0;
15550}
15551
15552#ifdef RIPPER
15553#define rb_ruby_parser_mark ripper_parser_mark
15554#define rb_ruby_parser_free ripper_parser_free
15555#define rb_ruby_parser_memsize ripper_parser_memsize
15556#endif
15557
15558void
15559rb_ruby_parser_mark(void *ptr)
15560{
15561 struct parser_params *p = (struct parser_params*)ptr;
15562
15563 rb_gc_mark(p->ruby_sourcefile_string);
15564#ifndef RIPPER
15565 rb_gc_mark(p->error_buffer);
15566#else
15567 rb_gc_mark(p->value);
15568 rb_gc_mark(p->result);
15569 rb_gc_mark(p->parsing_thread);
15570 rb_gc_mark(p->s_value);
15571 rb_gc_mark(p->s_lvalue);
15572 rb_gc_mark(p->s_value_stack);
15573#endif
15574 rb_gc_mark(p->debug_buffer);
15575 rb_gc_mark(p->debug_output);
15576}
15577
15578void
15579rb_ruby_parser_free(void *ptr)
15580{
15581 struct parser_params *p = (struct parser_params*)ptr;
15582 struct local_vars *local, *prev;
15583
15584 if (p->ast) {
15585 rb_ast_free(p->ast);
15586 }
15587
15588 if (p->warn_duplicate_keys_table) {
15589 st_free_table(p->warn_duplicate_keys_table);
15590 }
15591
15592#ifndef RIPPER
15593 if (p->tokens) {
15594 rb_parser_ary_free(p, p->tokens);
15595 }
15596#endif
15597
15598 if (p->tokenbuf) {
15599 ruby_xfree_sized(p->tokenbuf, p->toksiz);
15600 }
15601
15602 for (local = p->lvtbl; local; local = prev) {
15603 prev = local->prev;
15604 local_free(p, local);
15605 }
15606
15607 {
15608 token_info *ptinfo;
15609 while ((ptinfo = p->token_info) != 0) {
15610 p->token_info = ptinfo->next;
15611 xfree(ptinfo);
15612 }
15613 }
15614 string_buffer_free(p);
15615
15616 if (p->pvtbl) {
15617 st_free_table(p->pvtbl);
15618 }
15619
15620 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15621 st_free_table(p->case_labels);
15622 }
15623
15624 xfree(p->lex.strterm);
15625 p->lex.strterm = 0;
15626
15627 xfree(ptr);
15628}
15629
15630size_t
15631rb_ruby_parser_memsize(const void *ptr)
15632{
15633 struct parser_params *p = (struct parser_params*)ptr;
15634 struct local_vars *local;
15635 size_t size = sizeof(*p);
15636
15637 size += p->toksiz;
15638 for (local = p->lvtbl; local; local = local->prev) {
15639 size += sizeof(*local);
15640 if (local->vars) size += local->vars->capa * sizeof(ID);
15641 }
15642 return size;
15643}
15644
15645#ifndef RIPPER
15646#undef rb_reserved_word
15647
15648const struct kwtable *
15649rb_reserved_word(const char *str, unsigned int len)
15650{
15651 return reserved_word(str, len);
15652}
15653
15654#ifdef UNIVERSAL_PARSER
15655rb_parser_t *
15656rb_ruby_parser_allocate(const rb_parser_config_t *config)
15657{
15658 /* parser_initialize expects fields to be set to 0 */
15659 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15660 p->config = config;
15661 return p;
15662}
15663
15664rb_parser_t *
15665rb_ruby_parser_new(const rb_parser_config_t *config)
15666{
15667 /* parser_initialize expects fields to be set to 0 */
15668 rb_parser_t *p = rb_ruby_parser_allocate(config);
15669 parser_initialize(p);
15670 return p;
15671}
15672#else
15673rb_parser_t *
15674rb_ruby_parser_allocate(void)
15675{
15676 /* parser_initialize expects fields to be set to 0 */
15677 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15678 return p;
15679}
15680
15681rb_parser_t *
15682rb_ruby_parser_new(void)
15683{
15684 /* parser_initialize expects fields to be set to 0 */
15685 rb_parser_t *p = rb_ruby_parser_allocate();
15686 parser_initialize(p);
15687 return p;
15688}
15689#endif
15690
15691rb_parser_t *
15692rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15693{
15694 p->error_buffer = main ? Qfalse : Qnil;
15695 p->parent_iseq = base;
15696 return p;
15697}
15698
15699void
15700rb_ruby_parser_set_script_lines(rb_parser_t *p)
15701{
15702 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15703}
15704
15705void
15706rb_ruby_parser_error_tolerant(rb_parser_t *p)
15707{
15708 p->error_tolerant = 1;
15709}
15710
15711void
15712rb_ruby_parser_keep_tokens(rb_parser_t *p)
15713{
15714 p->keep_tokens = 1;
15715 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15716}
15717
15718rb_encoding *
15719rb_ruby_parser_encoding(rb_parser_t *p)
15720{
15721 return p->enc;
15722}
15723
15724int
15725rb_ruby_parser_end_seen_p(rb_parser_t *p)
15726{
15727 return p->ruby__end__seen;
15728}
15729
15730int
15731rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15732{
15733 p->debug = flag;
15734 return flag;
15735}
15736#endif /* !RIPPER */
15737
15738#ifdef RIPPER
15739int
15740rb_ruby_parser_get_yydebug(rb_parser_t *p)
15741{
15742 return p->debug;
15743}
15744
15745void
15746rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15747{
15748 p->value = value;
15749}
15750
15751int
15752rb_ruby_parser_error_p(rb_parser_t *p)
15753{
15754 return p->error_p;
15755}
15756
15757VALUE
15758rb_ruby_parser_debug_output(rb_parser_t *p)
15759{
15760 return p->debug_output;
15761}
15762
15763void
15764rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15765{
15766 p->debug_output = output;
15767}
15768
15769VALUE
15770rb_ruby_parser_parsing_thread(rb_parser_t *p)
15771{
15772 return p->parsing_thread;
15773}
15774
15775void
15776rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15777{
15778 p->parsing_thread = parsing_thread;
15779}
15780
15781void
15782rb_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)
15783{
15784 p->lex.gets = gets;
15785 p->lex.input = input;
15786 p->eofp = 0;
15787 p->ruby_sourcefile_string = sourcefile_string;
15788 p->ruby_sourcefile = sourcefile;
15789 p->ruby_sourceline = sourceline;
15790}
15791
15792VALUE
15793rb_ruby_parser_result(rb_parser_t *p)
15794{
15795 return p->result;
15796}
15797
15798rb_encoding *
15799rb_ruby_parser_enc(rb_parser_t *p)
15800{
15801 return p->enc;
15802}
15803
15804VALUE
15805rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15806{
15807 return p->ruby_sourcefile_string;
15808}
15809
15810int
15811rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15812{
15813 return p->ruby_sourceline;
15814}
15815
15816int
15817rb_ruby_parser_lex_state(rb_parser_t *p)
15818{
15819 return p->lex.state;
15820}
15821
15822void
15823rb_ruby_ripper_parse0(rb_parser_t *p)
15824{
15825 parser_prepare(p);
15826 p->ast = rb_ast_new();
15827 ripper_yyparse((void*)p);
15828 rb_ast_free(p->ast);
15829 p->ast = 0;
15830 p->eval_tree = 0;
15831 p->eval_tree_begin = 0;
15832}
15833
15834int
15835rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15836{
15837 return dedent_string(p, string, width);
15838}
15839
15840int
15841rb_ruby_ripper_initialized_p(rb_parser_t *p)
15842{
15843 return p->lex.input != 0;
15844}
15845
15846void
15847rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15848{
15849 parser_initialize(p);
15850}
15851
15852long
15853rb_ruby_ripper_column(rb_parser_t *p)
15854{
15855 return p->lex.ptok - p->lex.pbeg;
15856}
15857
15858long
15859rb_ruby_ripper_token_len(rb_parser_t *p)
15860{
15861 return p->lex.pcur - p->lex.ptok;
15862}
15863
15864rb_parser_string_t *
15865rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15866{
15867 return p->lex.lastline;
15868}
15869
15870VALUE
15871rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15872{
15873 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15874}
15875
15876#ifdef UNIVERSAL_PARSER
15877rb_parser_t *
15878rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15879{
15880 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15881 p->config = config;
15882 return p;
15883}
15884#endif
15885
15886struct parser_params*
15887rb_ruby_ripper_parser_allocate(void)
15888{
15889 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15890}
15891#endif /* RIPPER */
15892
15893#ifndef RIPPER
15894void
15895rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15896{
15897 va_list ap;
15898 VALUE mesg = p->debug_buffer;
15899
15900 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15901 va_start(ap, fmt);
15902 rb_str_vcatf(mesg, fmt, ap);
15903 va_end(ap);
15904 if (char_at_end(p, mesg, 0) == '\n') {
15905 rb_io_write(p->debug_output, mesg);
15906 p->debug_buffer = Qnil;
15907 }
15908}
15909
15910static void
15911parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15912{
15913 va_list ap;
15914 int lineno, column;
15915
15916 if (loc) {
15917 lineno = loc->end_pos.lineno;
15918 column = loc->end_pos.column;
15919 }
15920 else {
15921 lineno = p->ruby_sourceline;
15922 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15923 }
15924
15925 rb_io_flush(p->debug_output);
15926 p->error_p = 1;
15927 va_start(ap, fmt);
15928 p->error_buffer =
15929 rb_syntax_error_append(p->error_buffer,
15930 p->ruby_sourcefile_string,
15931 lineno, column,
15932 p->enc, fmt, ap);
15933 va_end(ap);
15934}
15935
15936static size_t
15937count_char(const char *str, int c)
15938{
15939 int n = 0;
15940 while (str[n] == c) ++n;
15941 return n;
15942}
15943
15944/*
15945 * strip enclosing double-quotes, same as the default yytnamerr except
15946 * for that single-quotes matching back-quotes do not stop stripping.
15947 *
15948 * "\"`class' keyword\"" => "`class' keyword"
15949 */
15950size_t
15951rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15952{
15953 if (*yystr == '"') {
15954 size_t yyn = 0, bquote = 0;
15955 const char *yyp = yystr;
15956
15957 while (*++yyp) {
15958 switch (*yyp) {
15959 case '\'':
15960 if (!bquote) {
15961 bquote = count_char(yyp+1, '\'') + 1;
15962 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15963 yyn += bquote;
15964 yyp += bquote - 1;
15965 break;
15966 }
15967 else {
15968 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15969 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15970 yyn += bquote;
15971 yyp += bquote - 1;
15972 bquote = 0;
15973 break;
15974 }
15975 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15976 if (yyres) memcpy(yyres + yyn, yyp, 3);
15977 yyn += 3;
15978 yyp += 2;
15979 break;
15980 }
15981 goto do_not_strip_quotes;
15982 }
15983
15984 case ',':
15985 goto do_not_strip_quotes;
15986
15987 case '\\':
15988 if (*++yyp != '\\')
15989 goto do_not_strip_quotes;
15990 /* Fall through. */
15991 default:
15992 if (yyres)
15993 yyres[yyn] = *yyp;
15994 yyn++;
15995 break;
15996
15997 case '"':
15998 case '\0':
15999 if (yyres)
16000 yyres[yyn] = '\0';
16001 return yyn;
16002 }
16003 }
16004 do_not_strip_quotes: ;
16005 }
16006
16007 if (!yyres) return strlen(yystr);
16008
16009 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16010}
16011#endif
16012
16013#ifdef RIPPER
16014#define validate(x) (void)(x)
16015
16016static VALUE
16017ripper_dispatch0(struct parser_params *p, ID mid)
16018{
16019 return rb_funcall(p->value, mid, 0);
16020}
16021
16022static VALUE
16023ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16024{
16025 validate(a);
16026 return rb_funcall(p->value, mid, 1, a);
16027}
16028
16029static VALUE
16030ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16031{
16032 validate(a);
16033 validate(b);
16034 return rb_funcall(p->value, mid, 2, a, b);
16035}
16036
16037static VALUE
16038ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16039{
16040 validate(a);
16041 validate(b);
16042 validate(c);
16043 return rb_funcall(p->value, mid, 3, a, b, c);
16044}
16045
16046static VALUE
16047ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16048{
16049 validate(a);
16050 validate(b);
16051 validate(c);
16052 validate(d);
16053 return rb_funcall(p->value, mid, 4, a, b, c, d);
16054}
16055
16056static VALUE
16057ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16058{
16059 validate(a);
16060 validate(b);
16061 validate(c);
16062 validate(d);
16063 validate(e);
16064 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16065}
16066
16067static VALUE
16068ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16069{
16070 validate(a);
16071 validate(b);
16072 validate(c);
16073 validate(d);
16074 validate(e);
16075 validate(f);
16076 validate(g);
16077 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16078}
16079
16080void
16081ripper_error(struct parser_params *p)
16082{
16083 p->error_p = TRUE;
16084}
16085
16086VALUE
16087ripper_value(struct parser_params *p)
16088{
16089 (void)yystpcpy; /* may not used in newer bison */
16090
16091 return p->value;
16092}
16093
16094#endif /* RIPPER */
16095/*
16096 * Local variables:
16097 * mode: c
16098 * c-file-style: "ruby"
16099 * End:
16100 */