1/**********************************************************************
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10**********************************************************************/
15# error needs pure parser
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
23# include RUBY_EXTCONF_H
26#include "ruby/internal/config.h"
27#include "internal/thread.h"
31#ifdef UNIVERSAL_PARSER
33#include "internal/ruby_parser.h"
34#include "parser_node.h"
35#include "universal_parser.c"
38#define STATIC_ID2SYM p->config->static_id2sym
39#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
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"
62#include "parser_node.h"
65#include "ruby/encoding.h"
66#include "ruby/regex.h"
70#include "ruby/ractor.h"
77 return rb_class_new_instance(0, 0, rb_eSyntaxError);
81static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
83#define compile_callback rb_suppress_tracing
84#endif /* !UNIVERSAL_PARSER */
86#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
87#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
89static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
92static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
96node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
98 return (n1->minus != n2->minus ||
99 n1->base != n2->base ||
100 strcmp(n1->val, n2->val));
104node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
106 return (n1->minus != n2->minus ||
107 strcmp(n1->val, n2->val));
111node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
113 return (n1->minus != n2->minus ||
114 n1->base != n2->base ||
115 n1->seen_point != n2->seen_point ||
116 strcmp(n1->val, n2->val));
120node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
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));
130rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
132 return (n1->options != n2->options ||
133 rb_parser_string_hash_cmp(n1->string, n2->string));
136static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
137static st_index_t rb_char_p_hash(const char *c);
140literal_cmp(st_data_t val, st_data_t lit)
142 if (val == lit) return 0;
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);
149 if (type_val != type_lit) {
155 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
157 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
159 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
161 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
163 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
165 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
167 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
169 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
171 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
173 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
175#ifdef UNIVERSAL_PARSER
178 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
184literal_hash(st_data_t a)
186 NODE *node = (NODE *)a;
187 enum node_type type = nd_type(node);
191 return rb_char_p_hash(RNODE_INTEGER(node)->val);
193 return rb_char_p_hash(RNODE_FLOAT(node)->val);
195 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
197 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
199 return rb_parser_str_hash(RNODE_STR(node)->string);
201 return rb_parser_str_hash(RNODE_SYM(node)->string);
203 return rb_parser_str_hash(RNODE_REGX(node)->string);
205 return (st_index_t)node->nd_loc.beg_pos.lineno;
207 return rb_parser_str_hash(RNODE_FILE(node)->path);
209 return (st_index_t)RNODE_ENCODING(node)->enc;
211#ifdef UNIVERSAL_PARSER
214 rb_bug("unexpected node: %s", ruby_node_name(type));
222 return '\0' <= c && c <= '\x7f';
226#define ISASCII parse_isascii
231 return c == ' ' || ('\t' <= c && c <= '\r');
235#define ISSPACE parse_isspace
240 return ('\0' <= c && c < ' ') || c == '\x7f';
244#define ISCNTRL(c) parse_iscntrl(c)
249 return 'A' <= c && c <= 'Z';
255 return 'a' <= c && c <= 'z';
261 return parse_isupper(c) || parse_islower(c);
265#define ISALPHA(c) parse_isalpha(c)
270 return '0' <= c && c <= '9';
274#define ISDIGIT(c) parse_isdigit(c)
279 return ISALPHA(c) || ISDIGIT(c);
283#define ISALNUM(c) parse_isalnum(c)
288 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
292#define ISXDIGIT(c) parse_isxdigit(c)
294#include "parser_st.h"
297#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
300#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
303#include "ripper_init.h"
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;
327typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
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.
334RBIMPL_WARNING_IGNORED(-Wpsabi)
336// Not sure why effective even after popped.
341#define NO_LEX_CTXT (struct lex_context){0}
343#ifndef WARN_PAST_SCOPE
344# define WARN_PAST_SCOPE 0
349#define yydebug (p->debug) /* disable the global variable definition */
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) \
360 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
361 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
365 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
366 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
370 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
371 "nesting too deep" : (Msgid))
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() \
387 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
388 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
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))
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);
400typedef VALUE stack_type;
402static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
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)"))
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))
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))
438 struct local_vars *prev;
440 NODE *outer, *inner, *current;
445typedef struct rb_locations_lambda_body_t {
449} rb_locations_lambda_body_t;
457#define DVARS_INHERIT ((void*)1)
458#define DVARS_TOPSCOPE NULL
459#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
461typedef struct token_info {
463 rb_code_position_t beg;
466 struct token_info *next;
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;
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;
481typedef struct parser_string_buffer {
482 parser_string_buffer_elem_t *head;
483 parser_string_buffer_elem_t *last;
484} parser_string_buffer_t;
486#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
489 Structure of Lexer Buffer:
491 lex.pbeg lex.ptok lex.pcur lex.pend
493 |------------+------------+------------|
497struct parser_params {
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;
512 enum lex_state_e state;
513 /* track the nest level of any parens "()[]{}" */
515 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
517 /* track the nest level of only braces "{}" */
520 stack_type cond_stack;
521 stack_type cmdarg_stack;
526 int heredoc_line_indent;
528 struct local_vars *lvtbl;
532 int ruby_sourceline; /* current line no. */
533 const char *ruby_sourcefile; /* current source file */
534 VALUE ruby_sourcefile_string;
536 token_info *token_info;
537 st_table *case_labels;
538 rb_node_exits_t *exits;
544 rb_parser_string_t *token;
554 st_table *warn_duplicate_keys_table;
559 struct lex_context ctxt;
561 NODE *eval_tree_begin;
563 const struct rb_iseq_struct *parent_iseq;
565#ifdef UNIVERSAL_PARSER
566 const rb_parser_config_t *config;
569 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
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;
579 unsigned int past_scope_enabled: 1;
581 unsigned int error_p: 1;
582 unsigned int cr_seen: 1;
584 /* Streaming hash state of the source bytes read so far. */
585 rb_source_hash_state_t source_hash;
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;
598 rb_parser_ary_t *debug_lines;
600 * Store specific keyword locations to generate dummy end token.
601 * Refer to the tail of list element.
603 end_expect_token_locations_t *end_expect_token_locations;
606 /* Array for term tokens */
607 rb_parser_ary_t *tokens;
613 VALUE parsing_thread;
614 VALUE s_value; /* Token VALUE */
615 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
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)))
624numparam_id_p(struct parser_params *p, ID id)
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;
630static void numparam_name(struct parser_params *p, ID id);
634after_shift(struct parser_params *p)
637 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
639 rb_ary_push(p->s_value_stack, p->s_value);
644before_reduce(int len, struct parser_params *p)
646 // Initialize $$ with $1.
647 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
651after_reduce(int len, struct parser_params *p)
653 for (int i = 0; i < len; i++) {
654 VALUE tos = rb_ary_pop(p->s_value_stack);
656 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
660 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
662 rb_ary_push(p->s_value_stack, p->s_lvalue);
667after_shift_error_token(struct parser_params *p)
670 rb_parser_printf(p, "after-shift-error-token:\n");
672 rb_ary_push(p->s_value_stack, Qnil);
676after_pop_stack(int len, struct parser_params *p)
678 for (int i = 0; i < len; i++) {
679 VALUE tos = rb_ary_pop(p->s_value_stack);
681 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
687after_shift(struct parser_params *p)
692before_reduce(int len, struct parser_params *p)
697after_reduce(int len, struct parser_params *p)
702after_shift_error_token(struct parser_params *p)
707after_pop_stack(int len, struct parser_params *p)
712#define intern_cstr(n,l,en) rb_intern3(n,l,en)
714#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
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))
725char_at_end(struct parser_params *p, VALUE str, int when_empty)
727 long len = RSTRING_LEN(str);
728 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
733pop_pvtbl(struct parser_params *p, st_table *tbl)
735 st_free_table(p->pvtbl);
740pop_pktbl(struct parser_params *p, st_table *tbl)
742 if (p->pktbl) st_free_table(p->pktbl);
746#define STRING_BUF_DEFAULT_LEN 16
749string_buffer_init(struct parser_params *p)
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;
754 buf->head = buf->last = xmalloc(size);
755 buf->head->len = STRING_BUF_DEFAULT_LEN;
757 buf->head->next = NULL;
761string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
763 parser_string_buffer_t *buf = &p->lex.string_buffer;
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;
770 elem = xmalloc(size);
774 buf->last->next = elem;
777 buf->last->buf[buf->last->used++] = str;
781string_buffer_free(struct parser_params *p)
783 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
786 parser_string_buffer_elem_t *next_elem = elem->next;
788 for (long i = 0; i < elem->used; i++) {
789 rb_parser_string_free(p, elem->buf[i]);
798static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
801debug_end_expect_token_locations(struct parser_params *p, const char *name)
804 VALUE mesg = rb_sprintf("%s: [", name);
806 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
808 rb_str_cat_cstr(mesg, ", ");
809 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
812 rb_str_cat_cstr(mesg, "]\n");
813 flush_debug_buffer(p, p->debug_output, mesg);
818push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
820 if(!p->error_tolerant) return;
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;
828 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
832pop_end_expect_token_locations(struct parser_params *p)
834 if(!p->end_expect_token_locations) return;
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;
840 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
843static end_expect_token_locations_t *
844peek_end_expect_token_locations(struct parser_params *p)
846 return p->end_expect_token_locations;
850parser_token2char(struct parser_params *p, enum yytokentype 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);
936 TOKEN2CHAR(tCONSTANT);
939 TOKEN2CHAR(tINTEGER);
941 TOKEN2CHAR(tRATIONAL);
942 TOKEN2CHAR(tIMAGINARY);
944 TOKEN2CHAR(tNTH_REF);
945 TOKEN2CHAR(tBACK_REF);
946 TOKEN2CHAR(tSTRING_CONTENT);
947 TOKEN2CHAR(tREGEXP_END);
948 TOKEN2CHAR(tDUMNY_END);
974 TOKEN2CHAR(tOP_ASGN);
977 TOKEN2CHAR(tLPAREN_ARG);
980 TOKEN2CHAR(tLBRACE_ARG);
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);
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);
1014 rb_bug("parser_token2id: unknown token %d", tok);
1016 UNREACHABLE_RETURN(0);
1020push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1025pop_end_expect_token_locations(struct parser_params *p)
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)
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);
1051#define compile_for_eval (0)
1053#define compile_for_eval (p->parent_iseq != 0)
1056#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
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))
1061#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1063static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1066rb_discard_node(struct parser_params *p, NODE *n)
1068 rb_ast_delete_node(p->ast, n);
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);
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)
1288enum internal_node_type {
1289 NODE_INTERNAL_ONLY = NODE_LAST,
1296parser_node_name(int node)
1300 return "NODE_DEF_TEMP";
1302 return "NODE_EXITS";
1304 return ruby_node_name(node);
1308/* This node is parse.y internal */
1309struct RNode_DEF_TEMP {
1312 /* for NODE_DEFN/NODE_DEFS */
1314 struct RNode *nd_def;
1319 NODE *numparam_save;
1320 struct lex_context ctxt;
1324#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
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);
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)
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))
1342static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1345parser_get_node_id(struct parser_params *p)
1347 int node_id = p->node_id;
1353anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1355 if (id == tANDDOT) {
1356 yyerror1(loc, "&. inside multiple assignment destination");
1361set_line_body(NODE *body, int line)
1364 switch (nd_type(body)) {
1367 nd_set_line(body, line);
1372set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1374 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1375 nd_set_line(node, beg->end_pos.lineno);
1379last_expr_node(NODE *expr)
1382 if (nd_type_p(expr, NODE_BLOCK)) {
1383 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1385 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1386 expr = RNODE_BEGIN(expr)->nd_body;
1396#define yyparse ruby_yyparse
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*);
1406static NODE *newline_node(NODE*);
1407static void fixpos(NODE*,NODE*);
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*);
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);
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);
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);
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);
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*);
1457static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1458static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1460static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1461static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1463static VALUE rb_backref_error(struct parser_params*,NODE*);
1464static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
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);
1472static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
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*);
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);
1480static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1482static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1484#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1486static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1488static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1490static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1492static rb_ast_id_table_t *local_tbl(struct parser_params*);
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);
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*);
1500static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
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);
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);
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
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);
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);
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);
1556static int lvar_defined(struct parser_params*, ID);
1558static NODE *numparam_push(struct parser_params *p);
1559static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1561#define METHOD_NOT '!'
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
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
1580#define CHECK_LITERAL_WHEN (st_table *)1
1581#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
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);
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)))))
1595/****** Ripper *******/
1599#include "eventids1.h"
1600#include "eventids2.h"
1602extern const struct ripper_parser_ids ripper_parser_ids;
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);
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))
1621#define yyparse ripper_yyparse
1624aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1626 if (!NIL_P(pre_arg)) {
1627 if (!NIL_P(pre_args)) {
1628 rb_ary_unshift(pre_args, pre_arg);
1631 pre_args = rb_ary_new_from_args(1, pre_arg);
1637#define ID2VAL(id) STATIC_ID2SYM(id)
1638#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1641#define KWD2EID(t, v) keyword_##t
1644new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
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);
1655rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1656 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
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);
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);
1671next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1673 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1677restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
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);
1690endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1692 if (is_attrset_id(mid)) {
1693 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1695 token_info_drop(p, "def", loc->beg_pos);
1698#define debug_token_line(p, name, line) do { \
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); \
1708#define begin_definition(k, loc_beg, loc_end) \
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; \
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"); \
1720 p->ctxt.cant_return = 1; \
1726# define ifndef_ripper(x) (x)
1727# define ifdef_ripper(r,x) (x)
1729# define ifndef_ripper(x)
1730# define ifdef_ripper(r,x) (r)
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))
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
1769# define WARN_S_L(s,l) s
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__)
1784#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1787add_block_exit(struct parser_params *p, NODE *node)
1790 compile_error(p, "unexpected null node");
1793 switch (nd_type(node)) {
1794 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1796 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1799 if (!p->ctxt.in_defined) {
1800 rb_node_exits_t *exits = p->exits;
1802 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1803 exits->nd_stts = node;
1809static rb_node_exits_t *
1810init_block_exit(struct parser_params *p)
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);
1820static rb_node_exits_t *
1821allow_block_exit(struct parser_params *p)
1823 rb_node_exits_t *exits = p->exits;
1829restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1835clear_block_exit(struct parser_params *p, bool error)
1837 rb_node_exits_t *exits = p->exits;
1840 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1841 switch (nd_type(e)) {
1843 yyerror1(&e->nd_loc, "Invalid break");
1846 yyerror1(&e->nd_loc, "Invalid next");
1849 yyerror1(&e->nd_loc, "Invalid redo");
1852 yyerror1(&e->nd_loc, "unexpected node");
1853 goto end_checks; /* no nd_chain */
1858 exits->nd_stts = RNODE(exits);
1859 exits->nd_chain = 0;
1862#define WARN_EOL(tok) \
1863 (looking_at_eol_p(p) ? \
1864 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1866static int looking_at_eol_p(struct parser_params *p);
1869get_nd_value(struct parser_params *p, NODE *node)
1871 switch (nd_type(node)) {
1873 return RNODE_GASGN(node)->nd_value;
1875 return RNODE_IASGN(node)->nd_value;
1877 return RNODE_LASGN(node)->nd_value;
1879 return RNODE_DASGN(node)->nd_value;
1881 return RNODE_MASGN(node)->nd_value;
1883 return RNODE_CVASGN(node)->nd_value;
1885 return RNODE_CDECL(node)->nd_value;
1887 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1893set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1895 switch (nd_type(node)) {
1897 RNODE_CDECL(node)->nd_value = rhs;
1900 RNODE_GASGN(node)->nd_value = rhs;
1903 RNODE_IASGN(node)->nd_value = rhs;
1906 RNODE_LASGN(node)->nd_value = rhs;
1909 RNODE_DASGN(node)->nd_value = rhs;
1912 RNODE_MASGN(node)->nd_value = rhs;
1915 RNODE_CVASGN(node)->nd_value = rhs;
1918 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1924get_nd_vid(struct parser_params *p, NODE *node)
1926 switch (nd_type(node)) {
1928 return RNODE_CDECL(node)->nd_vid;
1930 return RNODE_GASGN(node)->nd_vid;
1932 return RNODE_IASGN(node)->nd_vid;
1934 return RNODE_LASGN(node)->nd_vid;
1936 return RNODE_DASGN(node)->nd_vid;
1938 return RNODE_CVASGN(node)->nd_vid;
1940 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1946get_nd_args(struct parser_params *p, NODE *node)
1948 switch (nd_type(node)) {
1950 return RNODE_CALL(node)->nd_args;
1952 return RNODE_OPCALL(node)->nd_args;
1954 return RNODE_FCALL(node)->nd_args;
1956 return RNODE_QCALL(node)->nd_args;
1958 return RNODE_SUPER(node)->nd_args;
1967 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1973djb2(const uint8_t *str, size_t len)
1975 st_index_t hash = 5381;
1977 for (size_t i = 0; i < len; i++) {
1978 hash = ((hash << 5) + hash) + str[i];
1985parser_memhash(const void *ptr, long len)
1987 return djb2(ptr, len);
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); \
2000#define STRING_SET_LEN(str, n) do { \
2003#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2004 ((ptrvar) = str->ptr, \
2005 (lenvar) = str->len)
2008parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2010 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2013static rb_parser_string_t *
2014rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2016 rb_parser_string_t *str;
2019 rb_bug("negative string size (or size too big): %ld", len);
2022 str = xcalloc(1, sizeof(rb_parser_string_t));
2023 str->ptr = xcalloc(len + 1, sizeof(char));
2026 memcpy(PARSER_STRING_PTR(str), ptr, len);
2028 STRING_SET_LEN(str, len);
2029 STRING_TERM_FILL(str);
2033static rb_parser_string_t *
2034rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2036 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2037 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2044rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2047 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2053rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2056 xfree(PARSER_STRING_PTR(str));
2062rb_parser_str_hash(rb_parser_string_t *str)
2064 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2068rb_char_p_hash(const char *c)
2070 return parser_memhash((const void *)c, strlen(c));
2074rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2076 return PARSER_STRING_LEN(str);
2081rb_parser_string_end(rb_parser_string_t *str)
2083 return &str->ptr[str->len];
2088rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2094rb_parser_str_get_encoding(rb_parser_string_t *str)
2101PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2103 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2108PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2110 return str->coderange;
2114PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2116 str->coderange = coderange;
2120PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2122 rb_parser_string_set_encoding(str, enc);
2123 PARSER_ENC_CODERANGE_SET(str, cr);
2127PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2129 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2133PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2135 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2139PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2141 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2145rb_parser_search_nonascii(const char *p, const char *e)
2149 for (; s < e; s++) {
2150 if (*s & 0x80) return s;
2157rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2159 const char *e = ptr + len;
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;
2167 /* parser string encoding is always asciicompat */
2168 ptr = rb_parser_search_nonascii(ptr, e);
2169 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
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);
2179 return RB_PARSER_ENC_CODERANGE_VALID;
2183rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2185 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2189rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2191 int cr = PARSER_ENC_CODERANGE(str);
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);
2201static rb_parser_string_t *
2202rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2204 if (rb_parser_str_get_encoding(str) == enc)
2206 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2207 PARSER_ENC_CODERANGE_CLEAR(str);
2209 rb_parser_string_set_encoding(str, enc);
2214rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2216 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2220rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2222 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2223 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2225 if (enc1 == NULL || enc2 == NULL)
2232 if (PARSER_STRING_LEN(str2) == 0)
2234 if (PARSER_STRING_LEN(str1) == 0)
2235 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2239 cr1 = rb_parser_enc_str_coderange(p, str1);
2240 cr2 = rb_parser_enc_str_coderange(p, str2);
2243 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2244 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2247 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2251 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2259rb_parser_str_modify(rb_parser_string_t *str)
2261 PARSER_ENC_CODERANGE_CLEAR(str);
2265rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2268 const int termlen = STRING_TERM_LEN(str);
2270 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2271 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2274 int cr = PARSER_ENC_CODERANGE(str);
2275 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2276 /* Leave unknown. */
2278 else if (len > PARSER_STRING_LEN(str)) {
2279 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
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);
2289 STRING_SET_LEN(str, len);
2290 STRING_TERM_FILL(str);
2293static rb_parser_string_t *
2294rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2296 rb_parser_str_modify(str);
2297 if (len == 0) return 0;
2299 long total, olen, off = -1;
2301 const int termlen = STRING_TERM_LEN(str);
2303 PARSER_STRING_GETMEM(str, sptr, olen);
2304 if (ptr >= sptr && ptr <= sptr + olen) {
2308 if (olen > LONG_MAX - len) {
2309 compile_error(p, "string sizes too big");
2313 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2314 sptr = PARSER_STRING_PTR(str);
2318 memcpy(sptr + olen, ptr, len);
2319 STRING_SET_LEN(str, total);
2320 STRING_TERM_FILL(str);
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))
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)
2333 rb_encoding *str_enc, *res_enc;
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;
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);
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);
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);
2355 *ptr_cr_ret = ptr_cr;
2357 if (str_enc != ptr_enc &&
2358 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2359 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2363 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2365 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2367 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2368 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2370 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2377 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2379 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2384 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2387 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2391 compile_error(p, "negative string size (or size too big)");
2393 parser_str_cat(str, ptr, len);
2394 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
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);
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)
2408 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2411static rb_parser_string_t *
2412rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2414 int str2_cr = rb_parser_enc_str_coderange(p, str2);
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);
2419 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2424static rb_parser_string_t *
2425rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2428 rb_bug("negative string size (or size too big)");
2431 long slen = PARSER_STRING_LEN(str);
2433 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2434 PARSER_ENC_CODERANGE_CLEAR(str);
2439 const int termlen = STRING_TERM_LEN(str);
2441 if ((capa = slen) < len) {
2442 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2444 else if (len == slen) return str;
2445 STRING_SET_LEN(str, len);
2446 STRING_TERM_FILL(str);
2451# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2452 ((ptrvar) = str->ptr, \
2453 (lenvar) = str->len, \
2454 (encvar) = str->enc)
2457rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2460 const char *ptr1, *ptr2;
2461 rb_encoding *enc1, *enc2;
2463 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2464 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2466 return (len1 != len2 ||
2468 memcmp(ptr1, ptr2, len1) != 0);
2472rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2475 if (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++) {
2485 * Do not call this directly.
2486 * Use rb_parser_ary_new_capa_for_XXX() instead.
2488static rb_parser_ary_t *
2489parser_ary_new_capa(rb_parser_t *p, long len)
2492 rb_bug("negative array size (or size too big): %ld", len);
2494 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2499 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2508static rb_parser_ary_t *
2509rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2511 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2512 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2516static rb_parser_ary_t *
2517rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2519 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2520 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2525static rb_parser_ary_t *
2526rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2528 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2529 ary->data_type = PARSER_ARY_DATA_NODE;
2534 * Do not call this directly.
2535 * Use rb_parser_ary_push_XXX() instead.
2537static rb_parser_ary_t *
2538parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2540 if (ary->len == ary->capa) {
2541 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2543 ary->data[ary->len++] = val;
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)
2551 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2552 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2554 return parser_ary_push(p, ary, val);
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)
2560 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2561 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2563 return parser_ary_push(p, ary, val);
2567static rb_parser_ary_t *
2568rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2570 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2571 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2573 return parser_ary_push(p, ary, val);
2578rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2581 rb_parser_string_free(p, token->str);
2586rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
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);}
2595 case PARSER_ARY_DATA_SCRIPT_LINE:
2596 foreach_ary(data) {rb_parser_string_free(p, *data);}
2598 case PARSER_ARY_DATA_NODE:
2599 /* Do nothing because nodes are freed when rb_ast_t is freed */
2602 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2615%define parse.error verbose
2617 if ((NODE *)$$ == (NODE *)-1) {
2618 rb_parser_printf(p, "NODE_SPECIAL");
2621 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
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>
2626 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2629 switch (nd_type(RNODE($$))) {
2631 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2634 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2637 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2639 case NODE_IMAGINARY:
2640 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2645} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2647 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2650 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2654 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2657%lex-param {struct parser_params *p}
2658%parse-param {struct parser_params *p}
2661 RUBY_SET_YYLLOC_OF_NONE(@$);
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
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;
2685 const struct vtable *vars;
2686 struct rb_strterm_struct *strterm;
2687 struct lex_context ctxt;
2688 enum lex_state_e state;
2692 keyword_class "'class'"
2693 keyword_module "'module'"
2695 keyword_undef "'undef'"
2696 keyword_begin "'begin'"
2697 keyword_rescue "'rescue'"
2698 keyword_ensure "'ensure'"
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'"
2710 keyword_break "'break'"
2711 keyword_next "'next'"
2712 keyword_redo "'redo'"
2713 keyword_retry "'retry'"
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'"
2724 keyword_true "'true'"
2725 keyword_false "'false'"
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'"
2738 keyword__LINE__ "'__LINE__'"
2739 keyword__FILE__ "'__FILE__'"
2740 keyword__ENCODING__ "'__ENCODING__'"
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"
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
2809%token END_OF_INPUT 0 "end-of-input"
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. */
2846%token tLPAREN_ARG "( arg"
2849%token tLBRACE_ARG "{ arg"
2851%token tDSTAR "**arg"
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
2867%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2868%token tHEREDOC_BEG tHEREDOC_END k__END__
2875%nonassoc tLBRACE_ARG
2877%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2878%left keyword_or keyword_and
2880%nonassoc keyword_defined
2882%left modifier_rescue
2884%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2887%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2888%left '>' tGEQ '<' tLEQ
2894%right tUMINUS_NUM tUMINUS
2896%right '!' '~' tUPLUS
2903%rule %inline ident_or_const
2908%rule %inline user_or_keyword_variable
2914 * parameterizing rules
2916%rule asgn(rhs) <node>
2917 : lhs '=' lex_ctxt rhs
2919 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2920 /*% ripper: assign!($:1, $:4) %*/
2924%rule args_tail_basic(value, trailing) <node_args>
2925 : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing)
2927 $$ = new_args_tail(p, $1, $3, $4, &@3);
2928 /*% ripper: [$:1, $:3, $:4] %*/
2930 | f_kwarg(value) opt_f_block_arg(trailing)
2932 $$ = new_args_tail(p, $1, 0, $2, &@1);
2933 /*% ripper: [$:1, Qnil, $:2] %*/
2935 | f_any_kwrest opt_f_block_arg(trailing)
2937 $$ = new_args_tail(p, 0, $1, $2, &@1);
2938 /*% ripper: [Qnil, $:1, $:2] %*/
2942 $$ = new_args_tail(p, 0, 0, $1, &@1);
2943 /*% ripper: [Qnil, Qnil, $:1] %*/
2947%rule opt_f_block_arg(trailing) <id>
2956%rule def_endless_method(bodystmt) <node>
2957 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
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, $:$) %*/
2968 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
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, $:$) %*/
2981%rule compstmt(stmts) <node>
2984 void_stmts(p, $$ = $stmts);
2988%rule f_opt(value) <node_opt_arg>
2989 : f_arg_asgn f_eq value
2991 p->ctxt.in_argdef = 1;
2992 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2993 /*% ripper: [$:$, $:3] %*/
2997%rule f_opt_arg(value) <node_opt_arg>
3001 /*% ripper: rb_ary_new3(1, $:1) %*/
3003 | f_opt_arg(value) ',' f_opt(value)
3005 $$ = opt_arg_append($f_opt_arg, $f_opt);
3006 /*% ripper: rb_ary_push($:1, $:3) %*/
3010%rule f_kw(value) <node_kw_arg>
3013 p->ctxt.in_argdef = 1;
3014 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3015 /*% ripper: [$:$, $:value] %*/
3019 p->ctxt.in_argdef = 1;
3020 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3021 /*% ripper: [$:$, 0] %*/
3025%rule f_kwarg(value) <node_kw_arg>
3029 /*% ripper: rb_ary_new3(1, $:1) %*/
3031 | f_kwarg(value) ',' f_kw(value)
3033 $$ = kwd_append($f_kwarg, $f_kw);
3034 /*% ripper: rb_ary_push($:1, $:3) %*/
3038%rule mlhs_items(item) <node>
3041 $$ = NEW_LIST($1, &@$);
3042 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3044 | mlhs_items(item) ',' item
3046 $$ = list_append(p, $1, $3);
3047 /*% ripper: mlhs_add!($:1, $:3) %*/
3051%rule op_asgn(rhs) <node>
3052 : var_lhs tOP_ASGN lex_ctxt rhs
3054 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3055 /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
3057 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
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) %*/
3062 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
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) %*/
3067 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
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) %*/
3072 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
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) %*/
3077 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
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) %*/
3083 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
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) %*/
3089 | backref tOP_ASGN lex_ctxt rhs
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)) %*/
3097%rule opt_args_tail(tail, trailing) <node_args>
3101 /*% ripper: $:tail %*/
3105 $$ = new_empty_args_tail(p, &@$);
3106 /*% ripper: [Qnil, Qnil, Qnil] %*/
3110%rule range_expr(range) <node>
3115 $$ = NEW_DOT2($1, $3, &@$, &@2);
3116 /*% ripper: dot2!($:1, $:3) %*/
3122 $$ = NEW_DOT3($1, $3, &@$, &@2);
3123 /*% ripper: dot3!($:1, $:3) %*/
3128 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3129 /*% ripper: dot2!($:1, Qnil) %*/
3134 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3135 /*% ripper: dot3!($:1, Qnil) %*/
3140 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3141 /*% ripper: dot2!(Qnil, $:2) %*/
3146 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3147 /*% ripper: dot3!(Qnil, $:2) %*/
3151%rule value_expr(value) <node>
3159%rule words(begin, word_list) <node>
3160 : begin ' '+ word_list tSTRING_END
3162 $$ = make_list($word_list, &@$);
3163 /*% ripper: array!($:word_list) %*/
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);
3176 if ($2 && !compile_for_eval) {
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;
3183 node = RNODE_BLOCK(node)->nd_head;
3185 node = remove_begin(node);
3188 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3189 /*% ripper[final]: program!($:2) %*/
3196 $$ = NEW_BEGIN(0, &@$);
3197 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3201 $$ = newline_node($1);
3202 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3204 | top_stmts terms top_stmt
3206 $$ = block_append(p, $1, newline_node($3));
3207 /*% ripper: stmts_add!($:1, $:3) %*/
3213 clear_block_exit(p, true);
3216 | keyword_BEGIN begin_block
3223block_open : '{' {$$ = init_block_exit(p);};
3225begin_block : block_open compstmt(top_stmts) '}'
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) %*/
3235bodystmt : compstmt(stmts)[body]
3240 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3241 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3243 compstmt(stmts)[elsebody]
3245 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3249 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3250 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3252 | compstmt(stmts)[body]
3256 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3260 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3261 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3267 $$ = NEW_BEGIN(0, &@$);
3268 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3272 $$ = newline_node($1);
3273 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3275 | stmts terms stmt_or_begin
3277 $$ = block_append(p, $1, newline_node($3));
3278 /*% ripper: stmts_add!($:1, $:3) %*/
3285 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3293allow_exits : {$$ = allow_block_exit(p);};
3295k_END : keyword_END lex_ctxt
3297 if (p->ctxt.in_def) {
3298 rb_warn0("END in method; use at_exit");
3301 p->ctxt.in_rescue = before_rescue;
3305stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3307 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3308 /*% ripper: alias!($:new, $:old) %*/
3310 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3312 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3313 /*% ripper: var_alias!($:new, $:old) %*/
3315 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
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) %*/
3323 | keyword_alias tGVAR tNTH_REF[nth]
3325 static const char mesg[] = "can't make alias for the number variables";
3327 yyerror1(&@nth, mesg);
3329 $$ = NEW_ERROR(&@$);
3330 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3332 | keyword_undef[kw] undef_list[list]
3334 nd_set_first_loc($list, @kw.beg_pos);
3335 RNODE_UNDEF($list)->keyword_loc = @kw;
3337 /*% ripper: undef!($:list) %*/
3339 | stmt[body] modifier_if[mod] expr_value[cond]
3341 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3343 /*% ripper: if_mod!($:cond, $:body) %*/
3345 | stmt[body] modifier_unless[mod] expr_value[cond]
3347 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3349 /*% ripper: unless_mod!($:cond, $:body) %*/
3351 | stmt[body] modifier_while[mod] expr_value[cond_expr]
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);
3358 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3360 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3362 | stmt[body] modifier_until[mod] expr_value[cond_expr]
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);
3369 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3371 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3373 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3375 p->ctxt.in_rescue = $ctxt.in_rescue;
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) %*/
3382 | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace]
3384 clear_block_exit(p, true);
3385 restore_block_exit(p, $block_open);
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 = $$;
3392 /*% ripper: END!($:body) %*/
3395 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3397 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3398 /*% ripper: massign!($:lhs, $:rhs) %*/
3401 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3402 after_rescue[after_rescue] stmt[resbody]
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)) %*/
3412 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3414 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3415 /*% ripper: massign!($:lhs, $:rhs) %*/
3421 $$ = NEW_ERROR(&@$);
3425command_asgn : asgn(command_rhs)
3426 | op_asgn(command_rhs)
3427 | def_endless_method(endless_command)
3430endless_command : command
3431 | endless_command modifier_rescue after_rescue arg
3433 p->ctxt.in_rescue = $3.in_rescue;
3434 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3435 /*% ripper: rescue_mod!($:1, $:4) %*/
3437 | keyword_not '\n'? endless_command
3439 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3440 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3444command_rhs : command_call_value %prec tOP_ASGN
3445 | command_call_value modifier_rescue after_rescue stmt
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) %*/
3456 | expr[left] keyword_and[op] expr[right]
3458 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3459 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3461 | expr[left] keyword_or[op] expr[right]
3463 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3464 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3466 | keyword_not[not] '\n'? expr[arg]
3468 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3469 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3471 | '!'[not] command_call[arg]
3473 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3474 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3478 value_expr(p, $arg);
3480 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3481 p_top_expr_body[body]
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)) %*/
3493 value_expr(p, $arg);
3495 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3496 p_top_expr_body[body]
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)) %*/
3506 | arg %prec tLBRACE_ARG
3511 numparam_name(p, $fname);
3514 p->ctxt.in_rescue = before_rescue;
3515 p->ctxt.cant_return = 0;
3520defn_head : k_def def_name
3522 $$ = def_head_save(p, $k_def);
3523 $$->nd_mid = $def_name;
3524 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3525 /*% ripper: $:def_name %*/
3529defs_head : k_def singleton dot_or_colon
3531 SET_LEX_STATE(EXPR_FNAME);
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] %*/
3543expr_value : value_expr(expr)
3546 $$ = NEW_ERROR(&@$);
3550expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3557command_call : command
3561command_call_value : value_expr(command_call)
3564block_command : block_call
3565 | block_call call_op2 operation2 command_args
3567 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3568 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3572cmd_brace_block : tLBRACE_ARG brace_body '}'
3575 set_embraced_location($$, &@1, &@3);
3582 $$ = NEW_FCALL($1, 0, &@$);
3587command : fcall command_args %prec tLOWEST
3590 nd_set_last_loc($1, @2.end_pos);
3592 /*% ripper: command!($:1, $:2) %*/
3594 | fcall command_args cmd_brace_block
3596 block_dup_check(p, $2, $3);
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) %*/
3603 | primary_value call_op operation2 command_args %prec tLOWEST
3605 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3606 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3608 | primary_value call_op operation2 command_args cmd_brace_block
3610 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3611 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3613 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3615 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3616 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3618 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3620 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3621 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3623 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
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) %*/
3629 | keyword_super command_args
3631 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3633 /*% ripper: super!($:2) %*/
3635 | k_yield command_args
3637 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3639 /*% ripper: yield!($:2) %*/
3641 | k_return call_args
3643 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3644 /*% ripper: return!($:2) %*/
3646 | keyword_break call_args
3649 args = ret_args(p, $2);
3650 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3651 /*% ripper: break!($:2) %*/
3653 | keyword_next call_args
3656 args = ret_args(p, $2);
3657 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3658 /*% ripper: next!($:2) %*/
3663 | tLPAREN mlhs_inner rparen
3666 /*% ripper: mlhs_paren!($:2) %*/
3670mlhs_inner : mlhs_basic
3671 | tLPAREN mlhs_inner rparen
3673 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3674 /*% ripper: mlhs_paren!($:2) %*/
3678mlhs_basic : mlhs_head
3680 $$ = NEW_MASGN($1, 0, &@$);
3683 | mlhs_head mlhs_item
3685 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3686 /*% ripper: mlhs_add!($:1, $:2) %*/
3688 | mlhs_head tSTAR mlhs_node
3690 $$ = NEW_MASGN($1, $3, &@$);
3691 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3693 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3695 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3696 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3700 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3701 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3703 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3705 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3706 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3710 $$ = NEW_MASGN(0, $2, &@$);
3711 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3713 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3715 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3716 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3720 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3721 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3723 | tSTAR ',' mlhs_items(mlhs_item)
3725 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3726 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3730mlhs_item : mlhs_node
3731 | tLPAREN mlhs_inner rparen
3734 /*% ripper: mlhs_paren!($:2) %*/
3738mlhs_head : mlhs_item ','
3740 $$ = NEW_LIST($1, &@1);
3741 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3743 | mlhs_head mlhs_item ','
3745 $$ = list_append(p, $1, $2);
3746 /*% ripper: mlhs_add!($:1, $:2) %*/
3751mlhs_node : user_or_keyword_variable
3753 /*% ripper: var_field!($:1) %*/
3754 $$ = assignable(p, $1, 0, &@$);
3756 | primary_value '[' opt_call_args rbracket
3758 $$ = aryset(p, $1, $3, &@$);
3759 /*% ripper: aref_field!($:1, $:3) %*/
3761 | primary_value call_op ident_or_const
3763 anddot_multiple_assignment_check(p, &@2, $2);
3764 $$ = attrset(p, $1, $2, $3, &@$);
3765 /*% ripper: field!($:1, $:2, $:3) %*/
3767 | primary_value tCOLON2 tIDENTIFIER
3769 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3770 /*% ripper: const_path_field!($:1, $:3) %*/
3772 | primary_value tCOLON2 tCONSTANT
3774 /*% ripper: const_path_field!($:1, $:3) %*/
3775 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3779 /*% ripper: top_const_field!($:2) %*/
3780 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3784 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3785 $$ = NEW_ERROR(&@$);
3786 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3790lhs : user_or_keyword_variable
3792 /*% ripper: var_field!($:1) %*/
3793 $$ = assignable(p, $1, 0, &@$);
3795 | primary_value '[' opt_call_args rbracket
3797 $$ = aryset(p, $1, $3, &@$);
3798 /*% ripper: aref_field!($:1, $:3) %*/
3800 | primary_value call_op ident_or_const
3802 $$ = attrset(p, $1, $2, $3, &@$);
3803 /*% ripper: field!($:1, $:2, $:3) %*/
3805 | primary_value tCOLON2 tIDENTIFIER
3807 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3808 /*% ripper: field!($:1, $:2, $:3) %*/
3810 | primary_value tCOLON2 tCONSTANT
3812 /*% ripper: const_path_field!($:1, $:3) %*/
3813 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3817 /*% ripper: top_const_field!($:2) %*/
3818 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3822 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3823 $$ = NEW_ERROR(&@$);
3824 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3830 static const char mesg[] = "class/module name must be CONSTANT";
3832 yyerror1(&@1, mesg);
3834 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3839cpath : tCOLON3 cname
3841 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3842 /*% ripper: top_const_ref!($:2) %*/
3846 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3847 /*% ripper: const_ref!($:1) %*/
3849 | primary_value tCOLON2 cname
3851 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3852 /*% ripper: const_path_ref!($:1, $:3) %*/
3859 SET_LEX_STATE(EXPR_ENDFN);
3867 $$ = NEW_SYM(rb_id2str($1), &@$);
3868 /*% ripper: symbol_literal!($:1) %*/
3875 $$ = NEW_UNDEF($1, &@$);
3876 /*% ripper: rb_ary_new3(1, $:1) %*/
3878 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
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) %*/
3886op : '|' { $$ = '|'; }
3889 | tCMP { $$ = tCMP; }
3891 | tEQQ { $$ = tEQQ; }
3892 | tMATCH { $$ = tMATCH; }
3893 | tNMATCH { $$ = tNMATCH; }
3895 | tGEQ { $$ = tGEQ; }
3897 | tLEQ { $$ = tLEQ; }
3898 | tNEQ { $$ = tNEQ; }
3899 | tLSHFT { $$ = tLSHFT; }
3900 | tRSHFT { $$ = tRSHFT; }
3904 | tSTAR { $$ = '*'; }
3907 | tPOW { $$ = tPOW; }
3908 | tDSTAR { $$ = tDSTAR; }
3911 | tUPLUS { $$ = tUPLUS; }
3912 | tUMINUS { $$ = tUMINUS; }
3913 | tAREF { $$ = tAREF; }
3914 | tASET { $$ = tASET; }
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
3937 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3938 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3942 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3943 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3947 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3948 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3952 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3953 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3957 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3958 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3962 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3963 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3965 | tUMINUS_NUM simple_numeric tPOW arg
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)) %*/
3972 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3973 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3977 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3978 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3982 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3983 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3987 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3988 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3992 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3993 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3997 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3998 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
4000 | rel_expr %prec tCMP
4003 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4004 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4008 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4009 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4013 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4014 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4018 $$ = match_op(p, $1, $3, &@2, &@$);
4019 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4023 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4024 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4028 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4029 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4033 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4034 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4038 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4039 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4043 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4044 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4048 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4049 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4053 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4054 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4056 | keyword_defined '\n'? begin_defined arg
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) %*/
4063 | def_endless_method(endless_arg)
4068ternary : arg '?' arg '\n'? ':' arg
4071 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4073 /*% ripper: ifop!($:1, $:3, $:6) %*/
4077endless_arg : arg %prec modifier_rescue
4078 | endless_arg modifier_rescue after_rescue arg
4080 p->ctxt.in_rescue = $3.in_rescue;
4081 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4082 /*% ripper: rescue_mod!($:1, $:4) %*/
4084 | keyword_not '\n'? endless_arg
4086 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4087 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4091relop : '>' {$$ = '>';}
4097rel_expr : arg relop arg %prec '>'
4099 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4100 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4102 | rel_expr relop arg %prec '>'
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) %*/
4116begin_defined : lex_ctxt
4118 p->ctxt.in_defined = 1;
4123after_rescue : lex_ctxt
4125 p->ctxt.in_rescue = after_rescue;
4130arg_value : value_expr(arg)
4135 | args ',' assocs trailer
4137 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4138 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4142 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4143 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4147arg_rhs : arg %prec tOP_ASGN
4152 | arg modifier_rescue after_rescue arg
4154 p->ctxt.in_rescue = $3.in_rescue;
4156 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4157 /*% ripper: rescue_mod!($:1, $:4) %*/
4161paren_args : '(' opt_call_args rparen
4164 /*% ripper: arg_paren!($:2) %*/
4166 | '(' args ',' args_forward rparen
4168 if (!check_forwarding_args(p)) {
4172 $$ = new_args_forward_call(p, $2, &@4, &@$);
4173 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4176 | '(' args_forward rparen
4178 if (!check_forwarding_args(p)) {
4182 $$ = new_args_forward_call(p, 0, &@2, &@$);
4183 /*% ripper: arg_paren!($:2) %*/
4188opt_paren_args : none
4191 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4198 | args ',' assocs ','
4200 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4201 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4205 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4206 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4210call_args : value_expr(command)
4212 $$ = NEW_LIST($1, &@$);
4213 /*% ripper: args_add!(args_new!, $:1) %*/
4215 | def_endless_method(endless_command)
4217 $$ = NEW_LIST($1, &@$);
4218 /*% ripper: args_add!(args_new!, $:1) %*/
4220 | args opt_block_arg
4222 $$ = arg_blk_pass($1, $2);
4223 /*% ripper: args_add_block!($:1, $:2) %*/
4225 | assocs opt_block_arg
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) %*/
4231 | args ',' assocs opt_block_arg
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) %*/
4238 /*% ripper: args_add_block!(args_new!, $:1) %*/
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).
4251 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4254 if (lookahead) CMDARG_POP();
4256 if (lookahead) CMDARG_PUSH(0);
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.
4271 if (lookahead) CMDARG_POP();
4273 if (lookahead) CMDARG_PUSH(0);
4279block_arg : tAMPER arg_value
4281 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4286 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4287 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4288 /*% ripper: Qnil %*/
4292opt_block_arg : ',' block_arg
4300 /*% ripper: Qfalse %*/
4307 $$ = NEW_LIST($arg_value, &@$);
4308 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4313 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4315 | args[non_last_args] ',' arg_value
4317 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4318 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4320 | args[non_last_args] ',' arg_splat
4322 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4323 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4328arg_splat : tSTAR arg_value
4330 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4331 /*% ripper: $:arg_value %*/
4335 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4336 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4337 /*% ripper: Qnil %*/
4347mrhs : args ',' arg_value
4349 $$ = last_arg_append(p, $args, $arg_value, &@$);
4350 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4352 | args ',' tSTAR arg_value
4354 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4355 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4359 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4360 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4364%rule %inline inline_primary
4375primary : inline_primary
4380 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4381 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
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) %*/
4396 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4398 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4400 /*% ripper: paren!($:body) %*/
4402 | tLPAREN compstmt(stmts)[body] ')'
4404 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4405 $$ = NEW_BLOCK($body, &@$);
4406 /*% ripper: paren!($:body) %*/
4408 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4410 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4411 /*% ripper: const_path_ref!($:recv, $:name) %*/
4413 | tCOLON3[top] tCONSTANT[name]
4415 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4416 /*% ripper: top_const_ref!($:name) %*/
4418 | tLBRACK aref_args[args] ']'
4420 $$ = make_list($args, &@$);
4421 /*% ripper: array!($:args) %*/
4423 | tLBRACE assoc_list[list] '}'
4425 $$ = new_hash(p, $list, &@$);
4426 RNODE_HASH($$)->nd_brace = TRUE;
4427 /*% ripper: hash!($:list) %*/
4431 $$ = NEW_RETURN(0, &@$, &@kw);
4432 /*% ripper: return0! %*/
4434 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4436 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4437 /*% ripper: yield!(paren!($:args)) %*/
4439 | k_yield[kw] '('[lpar] rparen[rpar]
4441 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4442 /*% ripper: yield!(paren!(args_new!)) %*/
4446 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4447 /*% ripper: yield0! %*/
4449 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
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) %*/
4456 | keyword_not[kw] '(' expr[arg] rparen
4458 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4459 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4461 | keyword_not[kw] '('[lpar] rparen
4463 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4464 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4466 | fcall[call] brace_block[block]
4468 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4469 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4472 | method_call[call] brace_block[block]
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) %*/
4479 | k_if[kw] expr_value[cond] then[then]
4480 compstmt(stmts)[body]
4484 if ($tail && nd_type_p($tail, NODE_IF))
4485 RNODE_IF($tail)->end_keyword_loc = @k_end;
4487 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4489 /*% ripper: if!($:cond, $:body, $:tail) %*/
4491 | k_unless[kw] expr_value[cond] then[then]
4492 compstmt(stmts)[body]
4496 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4498 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4500 | k_while[kw] expr_value_do[cond]
4501 compstmt(stmts)[body]
4504 restore_block_exit(p, $kw);
4505 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4507 /*% ripper: while!($:cond, $:body) %*/
4509 | k_until[kw] expr_value_do[cond]
4510 compstmt(stmts)[body]
4513 restore_block_exit(p, $kw);
4514 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4516 /*% ripper: until!($:cond, $:body) %*/
4518 | k_case[k_case] expr_value[expr] terms?
4520 $$ = p->case_labels;
4521 p->case_labels = CHECK_LITERAL_WHEN;
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);
4530 /*% ripper: case!($:expr, $:body) %*/
4532 | k_case[k_case] terms?
4534 $$ = p->case_labels;
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) %*/
4545 | k_case[k_case] expr_value[expr] terms?
4549 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4550 /*% ripper: case!($:expr, $:body) %*/
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]
4557 restore_block_exit(p, $k_for);
4561 * e.each{|*x| a, b, c = x}
4565 * e.each{|x| a, = x}
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 */
4574 switch (nd_type($for_var)) {
4576 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4577 set_nd_value(p, $for_var, internal_var);
4580 m->nd_next = $for_var;
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);
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);
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) %*/
4597 | k_class cpath superclass
4599 begin_definition("class", &@k_class, &@cpath);
4604 YYLTYPE inheritance_operator_loc = NULL_LOC;
4606 inheritance_operator_loc = @superclass;
4607 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
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) %*/
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;
4619 | k_class tLSHFT expr_value
4621 begin_definition("", &@k_class, &@tLSHFT);
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) %*/
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;
4640 begin_definition("module", &@k_module, &@cpath);
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) %*/
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;
4658 push_end_expect_token_locations(p, &@head.beg_pos);
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) %*/
4673 push_end_expect_token_locations(p, &@head.beg_pos);
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) %*/
4687 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4688 /*% ripper: break!(args_new!) %*/
4692 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4693 /*% ripper: next!(args_new!) %*/
4697 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4698 /*% ripper: redo! %*/
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;
4710 $$ = NEW_RETRY(&@$);
4711 /*% ripper: retry! %*/
4715primary_value : value_expr(primary)
4718k_begin : keyword_begin
4720 token_info_push(p, "begin", &@$);
4721 push_end_expect_token_locations(p, &@1.beg_pos);
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++;
4736 p->token_info->nonspc = 0;
4739 push_end_expect_token_locations(p, &@1.beg_pos);
4743k_unless : keyword_unless
4745 token_info_push(p, "unless", &@$);
4746 push_end_expect_token_locations(p, &@1.beg_pos);
4750k_while : keyword_while[kw] allow_exits
4753 token_info_push(p, "while", &@$);
4754 push_end_expect_token_locations(p, &@kw.beg_pos);
4758k_until : keyword_until[kw] allow_exits
4761 token_info_push(p, "until", &@$);
4762 push_end_expect_token_locations(p, &@kw.beg_pos);
4766k_case : keyword_case
4768 token_info_push(p, "case", &@$);
4769 push_end_expect_token_locations(p, &@1.beg_pos);
4773k_for : keyword_for[kw] allow_exits
4776 token_info_push(p, "for", &@$);
4777 push_end_expect_token_locations(p, &@kw.beg_pos);
4781k_class : keyword_class
4783 token_info_push(p, "class", &@$);
4785 p->ctxt.in_rescue = before_rescue;
4786 push_end_expect_token_locations(p, &@1.beg_pos);
4790k_module : keyword_module
4792 token_info_push(p, "module", &@$);
4794 p->ctxt.in_rescue = before_rescue;
4795 push_end_expect_token_locations(p, &@1.beg_pos);
4801 token_info_push(p, "def", &@$);
4802 $$ = NEW_DEF_TEMP(&@$);
4803 p->ctxt.in_argdef = 1;
4809 token_info_push(p, "do", &@$);
4810 push_end_expect_token_locations(p, &@1.beg_pos);
4814k_do_block : keyword_do_block
4816 token_info_push(p, "do", &@$);
4817 push_end_expect_token_locations(p, &@1.beg_pos);
4821k_rescue : keyword_rescue
4823 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4825 p->ctxt.in_rescue = after_rescue;
4829k_ensure : keyword_ensure
4831 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4836k_when : keyword_when
4838 token_info_warn(p, "when", p->token_info, 0, &@$);
4842k_else : keyword_else
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, &@$);
4849 e.next = ptinfo_beg->next;
4851 token_info_setup(&e, p->lex.pbeg, &@$);
4852 if (!e.nonspc) *ptinfo_beg = e;
4857k_elsif : keyword_elsif
4860 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4866 token_info_pop(p, "end", &@$);
4867 pop_end_expect_token_locations(p);
4871 compile_error(p, "syntax error, unexpected end-of-input");
4875k_return : keyword_return
4877 if (p->ctxt.cant_return && !dyna_in_block(p))
4878 yyerror1(&@1, "Invalid return in class/module body");
4882k_yield : keyword_yield
4884 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4885 yyerror1(&@1, "Invalid yield");
4895 | keyword_do_cond { $$ = keyword_do_cond; }
4899 | k_elsif expr_value then
4903 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4905 /*% ripper: elsif!($:2, $:4, $:5) %*/
4910 | k_else compstmt(stmts)
4913 /*% ripper: else!($:2) %*/
4923 $$ = assignable(p, $1, 0, &@$);
4924 mark_lvar_used(p, $$);
4926 | tLPAREN f_margs rparen
4929 /*% ripper: mlhs_paren!($:2) %*/
4934f_margs : mlhs_items(f_marg)
4936 $$ = NEW_MASGN($1, 0, &@$);
4939 | mlhs_items(f_marg) ',' f_rest_marg
4941 $$ = NEW_MASGN($1, $3, &@$);
4942 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4944 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4946 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4947 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4951 $$ = NEW_MASGN(0, $1, &@$);
4952 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4954 | f_rest_marg ',' mlhs_items(f_marg)
4956 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4957 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4961f_rest_marg : tSTAR f_norm_arg
4964 $$ = assignable(p, $2, 0, &@$);
4965 mark_lvar_used(p, $$);
4969 $$ = NODE_SPECIAL_NO_NAME_REST;
4970 /*% ripper: Qnil %*/
4974f_any_kwrest : f_kwrest
4978 /*% ripper: ID2VAL(idNil) %*/
4982f_eq : {p->ctxt.in_argdef = 0;} '=';
4984block_args_tail : args_tail_basic(primary_value, none)
4989 /* magic number for rest_id in iseq_set_arguments() */
4990 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4991 /*% ripper: excessed_comma! %*/
4995block_param : args-list(primary_value, opt_args_tail(block_args_tail, none))
4996 | f_arg[pre] excessed_comma
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) %*/
5002 | f_arg[pre] opt_args_tail(block_args_tail, none)[tail]
5004 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5005 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5007 | tail-only-args(block_args_tail)
5010opt_block_param_def : none
5013 p->command_start = TRUE;
5017block_param_def : '|' opt_block_param opt_bv_decl '|'
5019 p->max_numparam = ORDINAL_PARAM;
5020 p->ctxt.in_argdef = 0;
5022 /*% ripper: block_var!($:2, $:3) %*/
5026opt_block_param : /* none */
5029 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5037 /*% ripper: Qfalse %*/
5039 | '\n'? ';' bv_decls '\n'?
5047 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5049 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5061 $$ = p->max_numparam;
5062 p->max_numparam = 0;
5067 $$ = numparam_push(p);
5077lambda : tLAMBDA[lpar]
5079 token_info_push(p, "->", &@lpar);
5082 max_numparam numparam it_id allow_exits
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;
5094 restore_block_exit(p, $allow_exits);
5096 $args = args_with_numbered(p, $args, max_numparam, it_id);
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);
5105 /*% ripper: lambda!($:args, $:body) %*/
5106 numparam_pop(p, $numparam);
5111f_larglist : '(' f_largs[args] opt_bv_decl ')'
5113 p->ctxt.in_argdef = 0;
5115 p->max_numparam = ORDINAL_PARAM;
5116 /*% ripper: paren!($:args) %*/
5120 p->ctxt.in_argdef = 0;
5121 if (!args_info_empty_p(&$args->nd_ainfo))
5122 p->max_numparam = ORDINAL_PARAM;
5127lambda_body : tLAMBEG compstmt(stmts) '}'
5129 token_info_pop(p, "}", &@3);
5130 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5135 push_end_expect_token_locations(p, &@1.beg_pos);
5139 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5144do_block : k_do_block do_body k_end
5147 set_embraced_location($$, &@1, &@3);
5152block_call : command do_block
5154 $$ = command_add_block(p, $1, $2, &@$);
5156 /*% ripper: method_add_block!($:1, $:2) %*/
5158 | block_call call_op2 operation2 opt_paren_args
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) %*/
5165 /*% ripper: method_add_arg!($:$, $:4) %*/
5168 | block_call call_op2 operation2 opt_paren_args brace_block
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) %*/
5174 | block_call call_op2 operation2 command_args do_block
5176 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5177 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5179 | block_call call_op2 paren_args
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) %*/
5187method_call : fcall paren_args
5191 nd_set_last_loc($1, @2.end_pos);
5192 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5194 | primary_value call_op operation2 opt_paren_args
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) %*/
5202 /*% ripper: method_add_arg!($:$, $:4) %*/
5205 | primary_value tCOLON2 operation2 paren_args
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) %*/
5211 | primary_value tCOLON2 operation3
5213 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5214 /*% ripper: call!($:1, $:2, $:3) %*/
5216 | primary_value call_op2 paren_args
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) %*/
5222 | keyword_super paren_args
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;
5229 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5230 /*% ripper: super!($:2) %*/
5234 $$ = NEW_ZSUPER(&@$);
5235 /*% ripper: zsuper! %*/
5237 | primary_value '[' opt_call_args rbracket
5239 $$ = NEW_CALL($1, tAREF, $3, &@$);
5241 /*% ripper: aref!($:1, $:3) %*/
5245brace_block : '{' brace_body '}'
5248 set_embraced_location($$, &@1, &@3);
5251 | k_do do_body k_end
5254 set_embraced_location($$, &@1, &@3);
5259brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5260 max_numparam numparam it_id allow_exits
5261 opt_block_param_def[args] compstmt(stmts)
5263 int max_numparam = p->max_numparam;
5264 ID it_id = p->it_id;
5265 p->max_numparam = $max_numparam;
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);
5280 max_numparam numparam it_id allow_exits
5281 opt_block_param_def[args] bodystmt
5283 int max_numparam = p->max_numparam;
5284 ID it_id = p->it_id;
5285 p->max_numparam = $max_numparam;
5287 $args = args_with_numbered(p, $args, max_numparam, it_id);
5288 $$ = NEW_ITER($args, $bodystmt, &@$);
5289 /*% ripper: do_block!($:args, $:bodystmt) %*/
5291 restore_block_exit(p, $allow_exits);
5292 numparam_pop(p, $numparam);
5297case_args : arg_value
5299 check_literal_when(p, $arg_value, &@arg_value);
5300 $$ = NEW_LIST($arg_value, &@$);
5301 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5305 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5306 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5308 | case_args[non_last_args] ',' arg_value
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) %*/
5314 | case_args[non_last_args] ',' tSTAR arg_value
5316 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5317 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5321case_body : k_when case_args then
5325 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5327 /*% ripper: when!($:2, $:4, $:5) %*/
5335p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5336p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
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;
5348p_case_body : keyword_in
5349 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5350 p_top_expr[expr] then
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;
5361 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5362 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5370p_top_expr : p_top_expr_body
5371 | p_top_expr_body modifier_if expr_value
5373 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5375 /*% ripper: if_mod!($:3, $:1) %*/
5377 | p_top_expr_body modifier_unless expr_value
5379 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5381 /*% ripper: unless_mod!($:3, $:1) %*/
5385p_top_expr_body : p_expr
5388 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5389 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5390 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
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]) %*/
5400 $$ = new_find_pattern(p, 0, $1, &@$);
5401 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5405 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5406 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5410 $$ = new_hash_pattern(p, 0, $1, &@$);
5411 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5418p_as : p_expr tASSOC p_variable
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) %*/
5428p_alt : p_alt[left] '|'[alt]
5430 p->ctxt.in_alt_pattern = 1;
5434 if (p->ctxt.capture_in_pattern) {
5435 yyerror1(&@alt, "alternative pattern after variable capture");
5437 p->ctxt.in_alt_pattern = 0;
5438 $$ = NEW_OR($left, $right, &@$, &@alt);
5439 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5444p_lparen : '(' p_pktbl
5451p_lbracket : '[' p_pktbl
5458p_expr_basic : p_value
5460 | p_const p_lparen[p_pktbl] p_args rparen
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]) %*/
5467 | p_const p_lparen[p_pktbl] p_find rparen
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]) %*/
5474 | p_const p_lparen[p_pktbl] p_kwargs rparen
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]) %*/
5481 | p_const '(' rparen
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) %*/
5487 | p_const p_lbracket[p_pktbl] p_args rbracket
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]) %*/
5494 | p_const p_lbracket[p_pktbl] p_find rbracket
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]) %*/
5501 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
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]) %*/
5508 | p_const '[' rbracket
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) %*/
5514 | tLBRACK p_args rbracket
5516 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5517 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5519 | tLBRACK p_find rbracket
5521 $$ = new_find_pattern(p, 0, $p_find, &@$);
5522 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5526 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5527 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5528 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5530 | tLBRACE p_pktbl lex_ctxt[ctxt]
5532 p->ctxt.in_kwarg = 0;
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]) %*/
5543 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5544 $$ = new_hash_pattern(p, 0, $$, &@$);
5545 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5547 | tLPAREN p_pktbl p_expr rparen
5549 pop_pktbl(p, $p_pktbl);
5551 /*% ripper: $:p_expr %*/
5557 NODE *pre_args = NEW_LIST($1, &@$);
5558 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5559 /*% ripper: [[$:1], Qnil, Qnil] %*/
5563 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5564 /*% ripper: [$:1, Qnil, Qnil] %*/
5568 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5569 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5571 | p_args_head p_rest
5573 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5574 /*% ripper: [$:1, $:2, Qnil] %*/
5576 | p_args_head p_rest ',' p_args_post
5578 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5579 /*% ripper: [$:1, $:2, $:4] %*/
5584p_args_head : p_arg ','
5585 | p_args_head p_arg ','
5587 $$ = list_concat($1, $2);
5588 /*% ripper: rb_ary_concat($:1, $:2) %*/
5594 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5595 /*% ripper: [Qnil, $:1, Qnil] %*/
5597 | p_rest ',' p_args_post
5599 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5600 /*% ripper: [Qnil, $:1, $:3] %*/
5604p_find : p_rest ',' p_args_post ',' p_rest
5606 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5607 /*% ripper: [$:1, $:3, $:5] %*/
5612p_rest : tSTAR tIDENTIFIER
5614 error_duplicate_pattern_variable(p, $2, &@2);
5615 /*% ripper: var_field!($:2) %*/
5616 $$ = assignable(p, $2, 0, &@$);
5621 /*% ripper: var_field!(Qnil) %*/
5626 | p_args_post ',' p_arg
5628 $$ = list_concat($1, $3);
5629 /*% ripper: rb_ary_concat($:1, $:3) %*/
5635 $$ = NEW_LIST($1, &@$);
5636 /*% ripper: [$:1] %*/
5640p_kwargs : p_kwarg ',' p_any_kwrest
5642 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5643 /*% ripper: [$:1, $:3] %*/
5647 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5648 /*% ripper: [$:1, Qnil] %*/
5652 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5653 /*% ripper: [$:1, Qnil] %*/
5657 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5658 /*% ripper: [[], $:1] %*/
5663 /*% ripper[brace]: [$:1] %*/
5666 $$ = list_concat($1, $3);
5667 /*% ripper: rb_ary_push($:1, $:3) %*/
5671p_kw : p_kw_label p_expr
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] %*/
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");
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] %*/
5690 | tSTRING_BEG string_contents tLABEL_END
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));
5698 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5699 $$ = rb_intern_str(STR_NEW0());
5705p_kwrest : kwrest_mark tIDENTIFIER
5708 /*% ripper: var_field!($:2) %*/
5713 /*% ripper: Qnil %*/
5717p_kwnorest : kwrest_mark keyword_nil
5723p_any_kwrest : p_kwrest
5727 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5731p_value : p_primitive
5732 | range_expr(p_primitive)
5738p_primitive : inline_primary
5741 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5742 /*% ripper: var_ref!($:1) %*/
5747p_variable : tIDENTIFIER
5749 error_duplicate_pattern_variable(p, $1, &@1);
5750 /*% ripper: var_field!($:1) %*/
5751 $$ = assignable(p, $1, 0, &@$);
5755p_var_ref : '^' tIDENTIFIER
5757 NODE *n = gettable(p, $2, &@$);
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));
5765 /*% ripper: var_ref!($:2) %*/
5769 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5770 /*% ripper: var_ref!($:2) %*/
5774p_expr_ref : '^' tLPAREN expr_value rparen
5776 $$ = NEW_BLOCK($3, &@$);
5777 /*% ripper: begin!($:3) %*/
5781p_const : tCOLON3 cname
5783 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5784 /*% ripper: top_const_ref!($:2) %*/
5786 | p_const tCOLON2 cname
5788 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5789 /*% ripper: const_path_ref!($:1, $:3) %*/
5793 $$ = gettable(p, $1, &@$);
5794 /*% ripper: var_ref!($:1) %*/
5798opt_rescue : k_rescue exc_list exc_var then
5804 err = NEW_ERRINFO(&@3);
5805 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5807 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5817 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5824 $$ = NEW_LIST($1, &@$);
5825 /*% ripper: rb_ary_new3(1, $:1) %*/
5829 if (!($$ = splat_array($1))) $$ = $1;
5842opt_ensure : k_ensure stmts terms?
5844 p->ctxt.in_rescue = $1.in_rescue;
5846 void_expr(p, void_stmts(p, $$));
5847 /*% ripper: ensure!($:2) %*/
5859 $$ = NEW_STR(STRING_NEW0(), &@$);
5862 $$ = evstr2dstr(p, $1);
5872 $$ = literal_concat(p, $1, $2, &@$);
5873 /*% ripper: string_concat!($:1, $:2) %*/
5877string1 : tSTRING_BEG string_contents tSTRING_END
5879 $$ = heredoc_dedent(p, $2);
5880 if ($$) nd_set_loc($$, &@$);
5882 if (p->heredoc_indent > 0) {
5883 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5884 p->heredoc_indent = 0;
5886 /*% ripper: string_literal!($:$) %*/
5890xstring : tXSTRING_BEG xstring_contents tSTRING_END
5892 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5894 if (p->heredoc_indent > 0) {
5895 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5896 p->heredoc_indent = 0;
5898 /*% ripper: xstring_literal!($:$) %*/
5902regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5904 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5905 /*% ripper: regexp_literal!($:2, $:3) %*/
5909words : words(tWORDS_BEG, word_list)
5912word_list : /* none */
5915 /*% ripper: words_new! %*/
5917 | word_list word ' '+
5919 $$ = list_append(p, $1, evstr2dstr(p, $2));
5920 /*% ripper: words_add!($:1, $:2) %*/
5924word : string_content
5925 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5926 | word string_content
5928 $$ = literal_concat(p, $1, $2, &@$);
5929 /*% ripper: word_add!($:1, $:2) %*/
5933symbols : words(tSYMBOLS_BEG, symbol_list)
5936symbol_list : /* none */
5939 /*% ripper: symbols_new! %*/
5941 | symbol_list word ' '+
5943 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5944 /*% ripper: symbols_add!($:1, $:2) %*/
5948qwords : words(tQWORDS_BEG, qword_list)
5951qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5954qword_list : /* none */
5957 /*% ripper: qwords_new! %*/
5959 | qword_list tSTRING_CONTENT ' '+
5961 $$ = list_append(p, $1, $2);
5962 /*% ripper: qwords_add!($:1, $:2) %*/
5966qsym_list : /* none */
5969 /*% ripper: qsymbols_new! %*/
5971 | qsym_list tSTRING_CONTENT ' '+
5973 $$ = symbol_append(p, $1, $2);
5974 /*% ripper: qsymbols_add!($:1, $:2) %*/
5978string_contents : /* none */
5981 /*% ripper: string_content! %*/
5983 | string_contents string_content
5985 $$ = literal_concat(p, $1, $2, &@$);
5986 /*% ripper: string_add!($:1, $:2) %*/
5990xstring_contents: /* none */
5993 /*% ripper: xstring_new! %*/
5995 | xstring_contents string_content
5997 $$ = literal_concat(p, $1, $2, &@$);
5998 /*% ripper: xstring_add!($:1, $:2) %*/
6002regexp_contents : /* none */
6005 /*% ripper: regexp_new! %*/
6007 | regexp_contents string_content
6009 NODE *head = $1, *tail = $2;
6017 switch (nd_type(head)) {
6019 head = str2dstr(p, head);
6024 head = list_append(p, NEW_DSTR(0, &@$), head);
6027 $$ = list_append(p, head, tail);
6029 /*% ripper: regexp_add!($:1, $:2) %*/
6033string_content : tSTRING_CONTENT[content]
6034 /*% ripper[brace]: $:content %*/
6035 | tSTRING_DVAR[state]
6037 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6038 $$ = p->lex.strterm;
6040 SET_LEX_STATE(EXPR_BEG);
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) %*/
6049 | tSTRING_DBEG[state]
6053 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6054 $$ = p->lex.strterm;
6056 SET_LEX_STATE(EXPR_BEG);
6059 $$ = p->lex.brace_nest;
6060 p->lex.brace_nest = 0;
6063 $$ = p->lex.lpar_beg;
6064 p->lex.lpar_beg = -1;
6067 $$ = p->heredoc_indent;
6068 p->heredoc_indent = 0;
6070 compstmt(stmts) string_dend
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) %*/
6086string_dend : tSTRING_DEND
6090string_dvar : nonlocal_var
6092 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6093 /*% ripper: var_ref!($:1) %*/
6104 SET_LEX_STATE(EXPR_END);
6105 VALUE str = rb_id2str($2);
6108 * set_yylval_noname sets invalid id to yylval.
6109 * This branch can be removed once yylval is changed to
6110 * hold lexed string.
6112 if (!str) str = STR_NEW0();
6113 $$ = NEW_SYM(str, &@$);
6114 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6122dsym : tSYMBEG string_contents tSTRING_END
6124 SET_LEX_STATE(EXPR_END);
6125 $$ = dsym_node(p, $2, &@$);
6126 /*% ripper: dyna_symbol!($:2) %*/
6130numeric : simple_numeric
6131 | tUMINUS_NUM simple_numeric %prec tLOWEST
6134 negate_lit(p, $$, &@$);
6135 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6139simple_numeric : tINTEGER
6150user_variable : ident_or_const
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);}
6163var_ref : user_variable
6165 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6166 if (ifdef_ripper(id_is_var(p, $1), false)) {
6167 /*% ripper: var_ref!($:1) %*/
6170 /*% ripper: vcall!($:1) %*/
6175 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6176 /*% ripper: var_ref!($:1) %*/
6180var_lhs : user_or_keyword_variable
6182 /*% ripper: var_field!($:1) %*/
6183 $$ = assignable(p, $1, 0, &@$);
6193 SET_LEX_STATE(EXPR_BEG);
6194 p->command_start = TRUE;
6204f_opt_paren_args: f_paren_args
6207 p->ctxt.in_argdef = 0;
6211f_empty_arg : /* none */
6213 $$ = new_empty_args_tail(p, &@$);
6214 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6215 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6219f_paren_args : '(' f_args rparen
6222 /*% ripper: paren!($:2) %*/
6223 SET_LEX_STATE(EXPR_BEG);
6224 p->command_start = TRUE;
6225 p->ctxt.in_argdef = 0;
6229f_arglist : f_paren_args
6232 p->ctxt.in_kwarg = 1;
6233 p->ctxt.in_argdef = 1;
6234 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6238 p->ctxt.in_kwarg = $1.in_kwarg;
6239 p->ctxt.in_argdef = 0;
6241 SET_LEX_STATE(EXPR_BEG);
6242 p->command_start = TRUE;
6247args_tail : args_tail_basic(arg_value, opt_comma)
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] %*/
6257largs_tail : args_tail_basic(arg_value, none)
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] %*/
6267%rule args-list(value, tail) <node_args>
6268 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6270 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6271 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6273 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6275 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6276 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6278 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6280 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6281 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6283 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6285 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6286 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6288 | f_arg[pre] ',' f_rest_arg[rest] tail
6290 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6291 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6293 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6295 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6296 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6298 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6300 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6301 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6303 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6305 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6306 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6308 | f_opt_arg(value)[opt] tail
6310 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6311 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6313 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6315 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6316 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6318 | f_rest_arg[rest] tail
6320 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6321 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6323 | f_rest_arg[rest] ',' f_arg[post] tail
6325 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6326 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6330%rule tail-only-args(tail) <node_args>
6333 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6334 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
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]
6342 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6343 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6345 | tail-only-args(tail)
6349f_args : f_args-list(args_tail, opt_comma)
6352f_largs : f_args-list(largs_tail, none)
6355args_forward : tBDOT3
6358 /*% ripper: args_forward! %*/
6362f_bad_arg : tCONSTANT
6364 static const char mesg[] = "formal argument cannot be a constant";
6366 yyerror1(&@1, mesg);
6369 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6373 static const char mesg[] = "formal argument cannot be an instance variable";
6375 yyerror1(&@1, mesg);
6378 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6382 static const char mesg[] = "formal argument cannot be a global variable";
6384 yyerror1(&@1, mesg);
6387 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6391 static const char mesg[] = "formal argument cannot be a class variable";
6393 yyerror1(&@1, mesg);
6396 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6400f_norm_arg : f_bad_arg
6403 VALUE e = formal_argument_error(p, $$ = $1);
6405 /*% ripper[error]: param_error!(?e, $:1) %*/
6407 p->max_numparam = ORDINAL_PARAM;
6411f_arg_asgn : f_norm_arg
6418f_arg_item : f_arg_asgn
6420 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6423 | tLPAREN f_margs rparen
6425 ID tid = internal_id(p);
6427 loc.beg_pos = @2.beg_pos;
6428 loc.end_pos = @2.beg_pos;
6430 if (dyna_in_block(p)) {
6431 $2->nd_value = NEW_DVAR(tid, &loc);
6434 $2->nd_value = NEW_LVAR(tid, &loc);
6436 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6437 $$->nd_next = (NODE *)$2;
6438 /*% ripper: mlhs_paren!($:2) %*/
6443 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6444 | f_arg ',' f_arg_item
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) %*/
6457 VALUE e = formal_argument_error(p, $$ = $1);
6460 /*% ripper[error]: param_error!(?e, $:1) %*/
6463 * Workaround for Prism::ParseTest#test_filepath for
6464 * "unparser/corpus/literal/def.txt"
6466 * See the discussion on https://github.com/ruby/ruby/pull/9923
6468 arg_var(p, ifdef_ripper(0, $1));
6470 p->max_numparam = ORDINAL_PARAM;
6471 p->ctxt.in_argdef = 0;
6479f_no_kwarg : p_kwnorest
6481 /*% ripper: nokw_param!(Qnil) %*/
6485f_kwrest : kwrest_mark tIDENTIFIER
6487 arg_var(p, shadowing_lvar(p, $2));
6489 /*% ripper: kwrest_param!($:2) %*/
6493 arg_var(p, idFWD_KWREST);
6495 /*% ripper: kwrest_param!(Qnil) %*/
6503f_rest_arg : restarg_mark tIDENTIFIER
6505 arg_var(p, shadowing_lvar(p, $2));
6507 /*% ripper: rest_param!($:2) %*/
6511 arg_var(p, idFWD_REST);
6513 /*% ripper: rest_param!(Qnil) %*/
6521f_block_arg : blkarg_mark tIDENTIFIER
6523 arg_var(p, shadowing_lvar(p, $2));
6525 /*% ripper: blockarg!($:2) %*/
6527 | blkarg_mark keyword_nil
6530 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6534 arg_var(p, idFWD_BLOCK);
6536 /*% ripper: blockarg!(Qnil) %*/
6543 /*% ripper: Qnil %*/
6548singleton : value_expr(singleton_expr)
6550 NODE *expr = last_expr_node($1);
6551 switch (nd_type(expr)) {
6565 case NODE_IMAGINARY:
6569 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6578singleton_expr : var_ref
6581 SET_LEX_STATE(EXPR_BEG);
6582 p->ctxt.in_argdef = 0;
6586 p->ctxt.in_argdef = 1;
6588 /*% ripper: paren!($:3) %*/
6596 /*% ripper: assoclist_from_args!($:1) %*/
6601 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6610 if (RNODE_LIST(assocs)->nd_head) {
6611 NODE *n = RNODE_LIST(tail)->nd_next;
6612 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6613 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6615 tail = RNODE_HASH(n)->nd_head;
6619 assocs = list_concat(assocs, tail);
6623 /*% ripper: rb_ary_push($:1, $:3) %*/
6627assoc : arg_value tASSOC arg_value
6629 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6630 /*% ripper: assoc_new!($:1, $:3) %*/
6634 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6635 /*% ripper: assoc_new!($:1, $:2) %*/
6639 NODE *val = gettable(p, $1, &@$);
6640 if (!val) val = NEW_ERROR(&@$);
6641 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6642 /*% ripper: assoc_new!($:1, Qnil) %*/
6644 | tSTRING_BEG string_contents tLABEL_END arg_value
6646 YYLTYPE loc = code_loc_gen(&@1, &@3);
6647 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6648 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6652 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6653 /*% ripper: assoc_splat!($:2) %*/
6657 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6658 $$ = list_append(p, NEW_LIST(0, &@$),
6659 NEW_LVAR(idFWD_KWREST, &@$));
6660 /*% ripper: assoc_splat!(Qnil) %*/
6664%rule %inline operation : ident_or_const
6668operation2 : operation
6672operation3 : tIDENTIFIER
6706 if (p->ctxt.in_defined) {
6707 p->ctxt.has_trailing_semicolon = 1;
6712 @$.end_pos = @$.beg_pos;
6718 | terms ';' {yyerrok;}
6724 /*% ripper: Qnil %*/
6731# define yylval (*p->lval)
6733static int regx_options(struct parser_params*);
6734static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6735static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6736static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6737static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6739#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6741# define set_yylval_node(x) { \
6743 rb_parser_set_location(p, &_cur_loc); \
6744 yylval.node = (x); \
6745 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6747# define set_yylval_str(x) \
6749 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6750 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6752# define set_yylval_num(x) { \
6754 set_parser_s_value(x); \
6756# define set_yylval_id(x) (yylval.id = (x))
6757# define set_yylval_name(x) { \
6758 (yylval.id = (x)); \
6759 set_parser_s_value(ID2SYM(x)); \
6761# define yylval_id() (yylval.id)
6763#define set_yylval_noname() set_yylval_id(keyword_nil)
6764#define has_delayed_token(p) (p->delayed.token != NULL)
6767#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6768#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6771parser_has_token(struct parser_params *p)
6773 const char *const pcur = p->lex.pcur;
6774 const char *const ptok = p->lex.ptok;
6775 if (p->keep_tokens && (pcur < ptok)) {
6776 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6777 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6786 case '"': return "\\\"";
6787 case '\\': return "\\\\";
6788 case '\0': return "\\0";
6789 case '\n': return "\\n";
6790 case '\r': return "\\r";
6791 case '\t': return "\\t";
6792 case '\f': return "\\f";
6793 case '\013': return "\\v";
6794 case '\010': return "\\b";
6795 case '\007': return "\\a";
6796 case '\033': return "\\e";
6797 case '\x7f': return "\\c?";
6802static rb_parser_string_t *
6803rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6805 rb_encoding *enc = p->enc;
6806 const char *ptr = str->ptr;
6807 const char *pend = ptr + str->len;
6808 const char *prev = ptr;
6809 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6810 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6812 while (ptr < pend) {
6815 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6816 if (!MBCLEN_CHARFOUND_P(n)) {
6817 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6818 n = rb_enc_mbminlen(enc);
6820 n = (int)(pend - ptr);
6822 c = *ptr & 0xf0 >> 4;
6823 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6825 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6826 parser_str_cat(result, charbuf, 4);
6831 n = MBCLEN_CHARFOUND_LEN(n);
6832 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6834 cc = escaped_char(c);
6836 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6837 parser_str_cat_cstr(result, cc);
6840 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6843 if (ptr - n > prev) {
6844 parser_str_cat(result, prev, ptr - n - prev);
6847 parser_str_cat(result, prev, ptr - prev);
6851 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6857parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6859 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6860 token->id = p->token_id;
6861 token->type_name = parser_token2char(p, t);
6863 token->loc.beg_pos = p->yylloc->beg_pos;
6864 token->loc.end_pos = p->yylloc->end_pos;
6865 rb_parser_ary_push_ast_token(p, p->tokens, token);
6869 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6870 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6871 line, token->id, token->type_name, str_escaped->ptr,
6872 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6873 token->loc.end_pos.lineno, token->loc.end_pos.column);
6874 rb_parser_string_free(p, str_escaped);
6879parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6881 debug_token_line(p, "parser_dispatch_scan_event", line);
6883 if (!parser_has_token(p)) return;
6885 RUBY_SET_YYLLOC(*p->yylloc);
6887 if (p->keep_tokens) {
6888 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6889 parser_append_tokens(p, str, t, line);
6895#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6897parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6899 debug_token_line(p, "parser_dispatch_delayed_token", line);
6901 if (!has_delayed_token(p)) return;
6903 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6905 if (p->keep_tokens) {
6906 /* p->delayed.token is freed by rb_parser_tokens_free */
6907 parser_append_tokens(p, p->delayed.token, t, line);
6910 rb_parser_string_free(p, p->delayed.token);
6913 p->delayed.token = NULL;
6916#define literal_flush(p, ptr) ((void)(ptr))
6919ripper_has_scan_event(struct parser_params *p)
6921 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6922 return p->lex.pcur > p->lex.ptok;
6926ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6928 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6929 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6930 RUBY_SET_YYLLOC(*p->yylloc);
6936ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6938 if (!ripper_has_scan_event(p)) return;
6940 set_parser_s_value(ripper_scan_event_val(p, t));
6942#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6945ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6947 /* save and adjust the location to delayed token for callbacks */
6948 int saved_line = p->ruby_sourceline;
6949 const char *saved_tokp = p->lex.ptok;
6952 if (!has_delayed_token(p)) return;
6953 p->ruby_sourceline = p->delayed.beg_line;
6954 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6955 str = rb_str_new_mutable_parser_string(p->delayed.token);
6956 rb_parser_string_free(p, p->delayed.token);
6957 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6958 set_parser_s_value(s_value);
6959 p->delayed.token = NULL;
6960 p->ruby_sourceline = saved_line;
6961 p->lex.ptok = saved_tokp;
6963#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6967is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6969 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6973peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
6975 const char *ptr = p->lex.pcur + at;
6976 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
6977 if (memcmp(ptr, str, len)) return false;
6978 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
6980 case '!': case '?': return false;
6982 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
6986parser_is_identchar(struct parser_params *p)
6988 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6992parser_isascii(struct parser_params *p)
6994 return ISASCII(*(p->lex.pcur-1));
6998token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7000 int column = 1, nonspc = 0, i;
7001 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7003 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7006 if (*ptr != ' ' && *ptr != '\t') {
7011 ptinfo->beg = loc->beg_pos;
7012 ptinfo->indent = column;
7013 ptinfo->nonspc = nonspc;
7017token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7021 if (!p->token_info_enabled) return;
7022 ptinfo = ALLOC(token_info);
7023 ptinfo->token = token;
7024 ptinfo->next = p->token_info;
7025 token_info_setup(ptinfo, p->lex.pbeg, loc);
7027 p->token_info = ptinfo;
7031token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7033 token_info *ptinfo_beg = p->token_info;
7035 if (!ptinfo_beg) return;
7037 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7038 token_info_warn(p, token, ptinfo_beg, 1, loc);
7040 p->token_info = ptinfo_beg->next;
7041 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7045token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7047 token_info *ptinfo_beg = p->token_info;
7049 if (!ptinfo_beg) return;
7050 p->token_info = ptinfo_beg->next;
7052 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7053 ptinfo_beg->beg.column != beg_pos.column ||
7054 strcmp(ptinfo_beg->token, token)) {
7055 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7056 beg_pos.lineno, beg_pos.column, token,
7057 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7061 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7065token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7067 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7068 if (!p->token_info_enabled) return;
7069 if (!ptinfo_beg) return;
7070 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7071 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7072 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7073 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7074 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7075 rb_warn3L(ptinfo_end->beg.lineno,
7076 "mismatched indentations at '%s' with '%s' at %d",
7077 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7081parser_precise_mbclen(struct parser_params *p, const char *ptr)
7083 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7084 if (!MBCLEN_CHARFOUND_P(len)) {
7085 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7093parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7095 rb_parser_string_t *str;
7096 int lineno = p->ruby_sourceline;
7100 else if (yylloc->beg_pos.lineno == lineno) {
7101 str = p->lex.lastline;
7106 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7110parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7116 yylloc = RUBY_SET_YYLLOC(current);
7118 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7119 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7123 parser_compile_error(p, yylloc, "%s", msg);
7124 parser_show_error_line(p, yylloc);
7129parser_yyerror0(struct parser_params *p, const char *msg)
7132 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7136ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7139 const int max_line_margin = 30;
7140 const char *ptr, *ptr_end, *pt, *pb;
7141 const char *pre = "", *post = "", *pend;
7142 const char *code = "", *caret = "";
7144 const char *const pbeg = PARSER_STRING_PTR(str);
7149 if (!yylloc) return;
7150 pend = rb_parser_string_end(str);
7151 if (pend > pbeg && pend[-1] == '\n') {
7152 if (--pend > pbeg && pend[-1] == '\r') --pend;
7156 if (lineno == yylloc->end_pos.lineno &&
7157 (pend - pbeg) > yylloc->end_pos.column) {
7158 pt = pbeg + yylloc->end_pos.column;
7162 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7163 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7165 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7166 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7168 len = ptr_end - ptr;
7171 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7172 if (ptr > pbeg) pre = "...";
7174 if (ptr_end < pend) {
7175 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7176 if (ptr_end < pend) post = "...";
7180 if (lineno == yylloc->beg_pos.lineno) {
7181 pb += yylloc->beg_pos.column;
7182 if (pb > pt) pb = pt;
7184 if (pb < ptr) pb = ptr;
7185 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7188 if (RTEST(errbuf)) {
7189 mesg = rb_attr_get(errbuf, idMesg);
7190 if (char_at_end(p, mesg, '\n') != '\n')
7191 rb_str_cat_cstr(mesg, "\n");
7194 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7196 if (!errbuf && rb_stderr_tty_p()) {
7197#define CSI_BEGIN "\033["
7200 CSI_BEGIN""CSI_SGR"%s" /* pre */
7201 CSI_BEGIN"1"CSI_SGR"%.*s"
7202 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7203 CSI_BEGIN";1"CSI_SGR"%.*s"
7204 CSI_BEGIN""CSI_SGR"%s" /* post */
7207 (int)(pb - ptr), ptr,
7209 (int)(ptr_end - pt), pt,
7215 len = ptr_end - ptr;
7216 lim = pt < pend ? pt : pend;
7217 i = (int)(lim - ptr);
7218 buf = ALLOCA_N(char, i+2);
7223 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7229 memset(p2, '~', (lim - ptr));
7233 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7234 pre, (int)len, code, post,
7237 if (!errbuf) rb_write_error_str(mesg);
7242parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7244 const char *pcur = 0, *ptok = 0;
7245 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7246 p->ruby_sourceline == yylloc->end_pos.lineno) {
7249 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7250 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7252 parser_yyerror0(p, msg);
7261parser_yyerror0(struct parser_params *p, const char *msg)
7263 dispatch1(parse_error, STR_NEW2(msg));
7269parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7275vtable_size(const struct vtable *tbl)
7277 if (!DVARS_TERMINAL_P(tbl)) {
7285static struct vtable *
7286vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7288 struct vtable *tbl = ALLOC(struct vtable);
7291 tbl->tbl = ALLOC_N(ID, tbl->capa);
7295 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7300#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7303vtable_free_gen(struct parser_params *p, int line, const char *name,
7308 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7311 if (!DVARS_TERMINAL_P(tbl)) {
7313 ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID));
7315 ruby_xfree_sized(tbl, sizeof(*tbl));
7318#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7321vtable_add_gen(struct parser_params *p, int line, const char *name,
7322 struct vtable *tbl, ID id)
7326 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7327 line, name, (void *)tbl, rb_id2name(id));
7330 if (DVARS_TERMINAL_P(tbl)) {
7331 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7334 if (tbl->pos == tbl->capa) {
7335 tbl->capa = tbl->capa * 2;
7336 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7338 tbl->tbl[tbl->pos++] = id;
7340#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7343vtable_pop_gen(struct parser_params *p, int line, const char *name,
7344 struct vtable *tbl, int n)
7347 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7348 line, name, (void *)tbl, n);
7351 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7356#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7359vtable_included(const struct vtable * tbl, ID id)
7363 if (!DVARS_TERMINAL_P(tbl)) {
7364 for (i = 0; i < tbl->pos; i++) {
7365 if (tbl->tbl[i] == id) {
7373static void parser_prepare(struct parser_params *p);
7376e_option_supplied(struct parser_params *p)
7378 return strcmp(p->ruby_sourcefile, "-e") == 0;
7382static NODE *parser_append_options(struct parser_params *p, NODE *node);
7385yycompile0(VALUE arg)
7389 struct parser_params *p = (struct parser_params *)arg;
7392 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7396 if (p->debug_lines) {
7397 p->ast->body.script_lines = p->debug_lines;
7401#define RUBY_DTRACE_PARSE_HOOK(name) \
7402 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7403 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7405 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7407 RUBY_DTRACE_PARSE_HOOK(END);
7411 xfree(p->lex.strterm);
7413 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7414 if (n || p->error_p) {
7415 VALUE mesg = p->error_buffer;
7417 mesg = syntax_error_new();
7419 if (!p->error_tolerant) {
7420 rb_set_errinfo(mesg);
7424 tree = p->eval_tree;
7426 tree = NEW_NIL(&NULL_LOC);
7429 rb_parser_ary_t *tokens = p->tokens;
7431 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7432 prelude = block_append(p, p->eval_tree_begin, body);
7433 RNODE_SCOPE(tree)->nd_body = prelude;
7434 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7435 p->ast->body.coverage_enabled = cov;
7436 if (p->keep_tokens) {
7437 p->ast->node_buffer->tokens = tokens;
7441 p->ast->body.root = tree;
7442 p->ast->body.line_count = p->line_count;
7447yycompile(struct parser_params *p, VALUE fname, int line)
7451 p->ruby_sourcefile_string = Qnil;
7452 p->ruby_sourcefile = "(none)";
7455 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7456 p->ruby_sourcefile = StringValueCStr(fname);
7458 p->ruby_sourceline = line - 1;
7462 p->ast = ast = rb_ast_new();
7463 compile_callback(yycompile0, (VALUE)p);
7464 ast->body.source_hash = rb_source_hash_finalize(&p->source_hash);
7465 ast->body.has_source_hash = 1;
7477must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7479 rb_encoding *enc = rb_parser_str_get_encoding(s);
7480 if (!rb_enc_asciicompat(enc)) {
7481 rb_raise(rb_eArgError, "invalid source encoding");
7486static rb_parser_string_t *
7487lex_getline(struct parser_params *p)
7489 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7490 if (!line) return 0;
7492 rb_source_hash_update(&p->source_hash, (const uint8_t *)line->ptr, (size_t)line->len);
7493 string_buffer_append(p, line);
7494 must_be_ascii_compatible(p, line);
7500rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7503 p->lex.input = input;
7504 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7506 return yycompile(p, fname, line);
7510#define STR_FUNC_ESCAPE 0x01
7511#define STR_FUNC_EXPAND 0x02
7512#define STR_FUNC_REGEXP 0x04
7513#define STR_FUNC_QWORDS 0x08
7514#define STR_FUNC_SYMBOL 0x10
7515#define STR_FUNC_INDENT 0x20
7516#define STR_FUNC_LABEL 0x40
7517#define STR_FUNC_LIST 0x4000
7518#define STR_FUNC_TERM 0x8000
7521 str_label = STR_FUNC_LABEL,
7523 str_dquote = (STR_FUNC_EXPAND),
7524 str_xquote = (STR_FUNC_EXPAND),
7525 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7526 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7527 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7528 str_ssym = (STR_FUNC_SYMBOL),
7529 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7532static rb_parser_string_t *
7533parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7535 rb_parser_string_t *pstr;
7537 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7539 if (!(func & STR_FUNC_REGEXP)) {
7540 if (rb_parser_is_ascii_string(p, pstr)) {
7542 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7543 /* everything is valid in ASCII-8BIT */
7544 enc = rb_ascii8bit_encoding();
7545 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7553strterm_is_heredoc(rb_strterm_t *strterm)
7555 return strterm->heredoc;
7558static rb_strterm_t *
7559new_strterm(struct parser_params *p, int func, int term, int paren)
7561 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7562 strterm->u.literal.func = func;
7563 strterm->u.literal.term = term;
7564 strterm->u.literal.paren = paren;
7568static rb_strterm_t *
7569new_heredoc(struct parser_params *p)
7571 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7572 strterm->heredoc = true;
7576#define peek(p,c) peek_n(p, (c), 0)
7577#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7578#define peekc(p) peekc_n(p, 0)
7579#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7581#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7583parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7585 debug_token_line(p, "add_delayed_token", line);
7588 if (has_delayed_token(p)) {
7589 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7590 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7591 int end_col = (next_line ? 0 : p->delayed.end_col);
7592 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7593 dispatch_delayed_token(p, tSTRING_CONTENT);
7596 if (!has_delayed_token(p)) {
7597 p->delayed.token = rb_parser_string_new(p, 0, 0);
7598 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7599 p->delayed.beg_line = p->ruby_sourceline;
7600 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7602 parser_str_cat(p->delayed.token, tok, end - tok);
7603 p->delayed.end_line = p->ruby_sourceline;
7604 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7610set_lastline(struct parser_params *p, rb_parser_string_t *str)
7612 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7613 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7614 p->lex.lastline = str;
7618nextline(struct parser_params *p, int set_encoding)
7620 rb_parser_string_t *str = p->lex.nextline;
7621 p->lex.nextline = 0;
7626 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7630 if (!p->lex.input || !(str = lex_getline(p))) {
7637 if (p->debug_lines) {
7638 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7639 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7640 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7645 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7646 /* after here-document without terminator */
7649 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7650 if (p->heredoc_end > 0) {
7651 p->ruby_sourceline = p->heredoc_end;
7654 p->ruby_sourceline++;
7655 set_lastline(p, str);
7661parser_cr(struct parser_params *p, int c)
7663 if (peek(p, '\n')) {
7671nextc0(struct parser_params *p, int set_encoding)
7675 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7676 if (nextline(p, set_encoding)) return -1;
7678 c = (unsigned char)*p->lex.pcur++;
7679 if (UNLIKELY(c == '\r')) {
7680 c = parser_cr(p, c);
7685#define nextc(p) nextc0(p, TRUE)
7688pushback(struct parser_params *p, int c)
7690 if (c == -1) return;
7693 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7698#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7700#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7701#define tok(p) (p)->tokenbuf
7702#define toklen(p) (p)->tokidx
7705looking_at_eol_p(struct parser_params *p)
7707 const char *ptr = p->lex.pcur;
7708 while (!lex_eol_ptr_p(p, ptr)) {
7709 int c = (unsigned char)*ptr++;
7710 int eol = (c == '\n' || c == '#');
7711 if (eol || !ISSPACE(c)) {
7719newtok(struct parser_params *p)
7724 p->tokenbuf = ALLOC_N(char, 60);
7726 if (p->toksiz > 4096) {
7728 REALLOC_N(p->tokenbuf, char, 60);
7734tokspace(struct parser_params *p, int n)
7738 if (p->tokidx >= p->toksiz) {
7739 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7740 REALLOC_N(p->tokenbuf, char, p->toksiz);
7742 return &p->tokenbuf[p->tokidx-n];
7746tokadd(struct parser_params *p, int c)
7748 p->tokenbuf[p->tokidx++] = (char)c;
7749 if (p->tokidx >= p->toksiz) {
7751 REALLOC_N(p->tokenbuf, char, p->toksiz);
7756tok_hex(struct parser_params *p, size_t *numlen)
7760 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7762 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7763 yyerror0("invalid hex escape");
7764 dispatch_scan_event(p, tSTRING_CONTENT);
7767 p->lex.pcur += *numlen;
7771#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7774escaped_control_code(int c)
7800#define WARN_SPACE_CHAR(c, prefix) \
7801 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7804tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7805 int regexp_literal, const char *begin)
7807 const int wide = !begin;
7809 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7811 p->lex.pcur += numlen;
7812 if (p->lex.strterm == NULL ||
7813 strterm_is_heredoc(p->lex.strterm) ||
7814 (p->lex.strterm->u.literal.func != str_regexp)) {
7815 if (!begin) begin = p->lex.pcur;
7816 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7817 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7818 yyerror0("invalid Unicode escape");
7819 dispatch_scan_event(p, tSTRING_CONTENT);
7820 return wide && numlen > 0;
7822 if (codepoint > 0x10ffff) {
7823 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7824 yyerror0("invalid Unicode codepoint (too large)");
7825 dispatch_scan_event(p, tSTRING_CONTENT);
7828 if ((codepoint & 0xfffff800) == 0xd800) {
7829 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7830 yyerror0("invalid Unicode codepoint");
7831 dispatch_scan_event(p, tSTRING_CONTENT);
7835 if (regexp_literal) {
7836 tokcopy(p, (int)numlen);
7838 else if (codepoint >= 0x80) {
7839 rb_encoding *utf8 = rb_utf8_encoding();
7840 if (*encp && utf8 != *encp) {
7841 YYLTYPE loc = RUBY_INIT_YYLLOC();
7842 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7843 parser_show_error_line(p, &loc);
7847 tokaddmbc(p, codepoint, *encp);
7850 tokadd(p, codepoint);
7855static int tokadd_mbchar(struct parser_params *p, int c);
7858tokskip_mbchar(struct parser_params *p)
7860 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7862 p->lex.pcur += len - 1;
7867/* return value is for ?\u3042 */
7869tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7870 int term, int symbol_literal, int regexp_literal)
7873 * If `term` is not -1, then we allow multiple codepoints in \u{}
7874 * upto `term` byte, otherwise we're parsing a character literal.
7875 * And then add the codepoints to the current token.
7877 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7879 const int open_brace = '{', close_brace = '}';
7881 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7883 if (peek(p, open_brace)) { /* handle \u{...} form */
7884 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7886 * Skip parsing validation code and copy bytes as-is until term or
7887 * closing brace, in order to correctly handle extended regexps where
7888 * invalid unicode escapes are allowed in comments. The regexp parser
7889 * does its own validation and will catch any issues.
7891 tokadd(p, open_brace);
7892 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7894 if (c == close_brace) {
7899 else if (c == term) {
7902 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7906 tokadd_mbchar(p, c);
7910 const char *second = NULL;
7911 int c, last = nextc(p);
7912 if (lex_eol_p(p)) goto unterminated;
7913 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7914 while (c != close_brace) {
7915 if (c == term) goto unterminated;
7916 if (second == multiple_codepoints)
7917 second = p->lex.pcur;
7918 if (regexp_literal) tokadd(p, last);
7919 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7922 while (ISSPACE(c = peekc(p))) {
7923 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7926 if (term == -1 && !second)
7927 second = multiple_codepoints;
7930 if (c != close_brace) {
7932 flush_string_content(p, rb_utf8_encoding(), 0);
7933 yyerror0("unterminated Unicode escape");
7934 dispatch_scan_event(p, tSTRING_CONTENT);
7937 if (second && second != multiple_codepoints) {
7938 const char *pcur = p->lex.pcur;
7939 p->lex.pcur = second;
7940 dispatch_scan_event(p, tSTRING_CONTENT);
7943 yyerror0(multiple_codepoints);
7947 if (regexp_literal) tokadd(p, close_brace);
7951 else { /* handle \uxxxx form */
7952 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7959#define ESCAPE_CONTROL 1
7960#define ESCAPE_META 2
7963read_escape(struct parser_params *p, int flags, const char *begin)
7968 switch (c = nextc(p)) {
7969 case '\\': /* Backslash */
7972 case 'n': /* newline */
7975 case 't': /* horizontal tab */
7978 case 'r': /* carriage-return */
7981 case 'f': /* form-feed */
7984 case 'v': /* vertical tab */
7987 case 'a': /* alarm(bell) */
7990 case 'e': /* escape */
7993 case '0': case '1': case '2': case '3': /* octal constant */
7994 case '4': case '5': case '6': case '7':
7996 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
7997 p->lex.pcur += numlen;
8000 case 'x': /* hex constant */
8001 c = tok_hex(p, &numlen);
8002 if (numlen == 0) return 0;
8005 case 'b': /* backspace */
8008 case 's': /* space */
8012 if (flags & ESCAPE_META) goto eof;
8013 if ((c = nextc(p)) != '-') {
8016 if ((c = nextc(p)) == '\\') {
8022 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8024 else if (c == -1) goto eof;
8025 else if (!ISASCII(c)) {
8030 int c2 = escaped_control_code(c);
8032 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8033 WARN_SPACE_CHAR(c2, "\\M-");
8036 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8039 else if (ISCNTRL(c)) goto eof;
8040 return ((c & 0xff) | 0x80);
8044 if ((c = nextc(p)) != '-') {
8048 if (flags & ESCAPE_CONTROL) goto eof;
8049 if ((c = nextc(p))== '\\') {
8055 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8059 else if (c == -1) goto eof;
8060 else if (!ISASCII(c)) {
8065 int c2 = escaped_control_code(c);
8068 if (flags & ESCAPE_META) {
8069 WARN_SPACE_CHAR(c2, "\\M-");
8072 WARN_SPACE_CHAR(c2, "");
8076 if (flags & ESCAPE_META) {
8077 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8080 WARN_SPACE_CHAR(c2, "\\C-");
8084 else if (ISCNTRL(c)) goto eof;
8090 flush_string_content(p, p->enc, p->lex.pcur - begin);
8091 yyerror0("Invalid escape character syntax");
8092 dispatch_scan_event(p, tSTRING_CONTENT);
8105tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8107 int len = rb_enc_codelen(c, enc);
8108 rb_enc_mbcput(c, tokspace(p, len), enc);
8112tokadd_escape(struct parser_params *p)
8116 const char *begin = p->lex.pcur;
8118 switch (c = nextc(p)) {
8120 return 0; /* just ignore */
8122 case '0': case '1': case '2': case '3': /* octal constant */
8123 case '4': case '5': case '6': case '7':
8125 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8126 if (numlen == 0) goto eof;
8127 p->lex.pcur += numlen;
8128 tokcopy(p, (int)numlen + 1);
8132 case 'x': /* hex constant */
8134 tok_hex(p, &numlen);
8135 if (numlen == 0) return -1;
8136 tokcopy(p, (int)numlen + 2);
8142 flush_string_content(p, p->enc, p->lex.pcur - begin);
8143 yyerror0("Invalid escape character syntax");
8155char_to_option(int c)
8161 val = RE_ONIG_OPTION_IGNORECASE;
8164 val = RE_ONIG_OPTION_EXTEND;
8167 val = RE_ONIG_OPTION_MULTILINE;
8176#define ARG_ENCODING_FIXED 16
8177#define ARG_ENCODING_NONE 32
8178#define ENC_ASCII8BIT 1
8180#define ENC_Windows_31J 3
8184char_to_option_kcode(int c, int *option, int *kcode)
8190 *kcode = ENC_ASCII8BIT;
8191 return (*option = ARG_ENCODING_NONE);
8193 *kcode = ENC_EUC_JP;
8196 *kcode = ENC_Windows_31J;
8203 return (*option = char_to_option(c));
8205 *option = ARG_ENCODING_FIXED;
8210regx_options(struct parser_params *p)
8218 while (c = nextc(p), ISALPHA(c)) {
8220 options |= RE_OPTION_ONCE;
8222 else if (char_to_option_kcode(c, &opt, &kc)) {
8224 if (kc != ENC_ASCII8BIT) kcode = c;
8238 YYLTYPE loc = RUBY_INIT_YYLLOC();
8240 compile_error(p, "unknown regexp option%s - %*s",
8241 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8242 parser_show_error_line(p, &loc);
8244 return options | RE_OPTION_ENCODING(kcode);
8248tokadd_mbchar(struct parser_params *p, int c)
8250 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8251 if (len < 0) return -1;
8253 p->lex.pcur += --len;
8254 if (len > 0) tokcopy(p, len);
8259simple_re_meta(int c)
8262 case '$': case '*': case '+': case '.':
8263 case '?': case '^': case '|':
8264 case ')': case ']': case '}': case '>':
8272parser_update_heredoc_indent(struct parser_params *p, int c)
8274 if (p->heredoc_line_indent == -1) {
8275 if (c == '\n') p->heredoc_line_indent = 0;
8279 p->heredoc_line_indent++;
8282 else if (c == '\t') {
8283 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8284 p->heredoc_line_indent = w * TAB_WIDTH;
8287 else if (c != '\n') {
8288 if (p->heredoc_indent > p->heredoc_line_indent) {
8289 p->heredoc_indent = p->heredoc_line_indent;
8291 p->heredoc_line_indent = -1;
8294 /* Whitespace only line has no indentation */
8295 p->heredoc_line_indent = 0;
8302parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8304 YYLTYPE loc = RUBY_INIT_YYLLOC();
8305 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8306 compile_error(p, "%s mixed within %s source", n1, n2);
8307 parser_show_error_line(p, &loc);
8311parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8313 const char *pos = p->lex.pcur;
8315 parser_mixed_error(p, enc1, enc2);
8320nibble_char_upper(unsigned int c)
8323 return c + (c < 10 ? '0' : 'A' - 10);
8327tokadd_string(struct parser_params *p,
8328 int func, int term, int paren, long *nest,
8329 rb_encoding **encp, rb_encoding **enc)
8334 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8335 int top_of_line = FALSE;
8338#define mixed_error(enc1, enc2) \
8339 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8340#define mixed_escape(beg, enc1, enc2) \
8341 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8343 while ((c = nextc(p)) != -1) {
8344 if (p->heredoc_indent > 0) {
8345 parser_update_heredoc_indent(p, c);
8348 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8354 if (paren && c == paren) {
8357 else if (c == term) {
8358 if (!nest || !*nest) {
8364 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8365 unsigned char c2 = *p->lex.pcur;
8366 if (c2 == '$' || c2 == '@' || c2 == '{') {
8371 else if (c == '\\') {
8375 if (func & STR_FUNC_QWORDS) break;
8376 if (func & STR_FUNC_EXPAND) {
8377 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8388 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8392 if ((func & STR_FUNC_EXPAND) == 0) {
8396 tokadd_utf8(p, enc, term,
8397 func & STR_FUNC_SYMBOL,
8398 func & STR_FUNC_REGEXP);
8402 if (c == -1) return -1;
8404 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8407 if (func & STR_FUNC_REGEXP) {
8413 c = read_escape(p, 0, p->lex.pcur - 1);
8415 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8418 *t++ = nibble_char_upper(c >> 4);
8419 *t++ = nibble_char_upper(c);
8424 if (c == term && !simple_re_meta(c)) {
8429 if ((c = tokadd_escape(p)) < 0)
8431 if (*enc && *enc != *encp) {
8432 mixed_escape(p->lex.ptok+2, *enc, *encp);
8436 else if (func & STR_FUNC_EXPAND) {
8438 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8439 c = read_escape(p, 0, p->lex.pcur - 1);
8441 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8442 /* ignore backslashed spaces in %w */
8444 else if (c != term && !(paren && c == paren)) {
8451 else if (!parser_isascii(p)) {
8456 else if (*enc != *encp) {
8457 mixed_error(*enc, *encp);
8460 if (tokadd_mbchar(p, c) == -1) return -1;
8463 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8471 else if (*enc != *encp) {
8472 mixed_error(*enc, *encp);
8478 top_of_line = (c == '\n');
8482 if (*enc) *encp = *enc;
8486#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8489flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8491 p->lex.pcur -= back;
8492 if (has_delayed_token(p)) {
8493 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8495 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8496 p->delayed.end_line = p->ruby_sourceline;
8497 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8499 dispatch_delayed_token(p, tSTRING_CONTENT);
8500 p->lex.ptok = p->lex.pcur;
8502 dispatch_scan_event(p, tSTRING_CONTENT);
8503 p->lex.pcur += back;
8506/* this can be shared with ripper, since it's independent from struct
8509#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8510#define SPECIAL_PUNCT(idx) ( \
8511 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8512 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8513 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8514 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8515 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8517const uint_least32_t ruby_global_name_punct_bits[] = {
8526static enum yytokentype
8527parser_peek_variable_name(struct parser_params *p)
8530 const char *ptr = p->lex.pcur;
8532 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8536 if ((c = *ptr) == '-') {
8537 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8540 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8541 return tSTRING_DVAR;
8545 if ((c = *ptr) == '@') {
8546 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8552 p->command_start = TRUE;
8553 yylval.state = p->lex.state;
8554 return tSTRING_DBEG;
8558 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8559 return tSTRING_DVAR;
8563#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8564#define IS_END() IS_lex_state(EXPR_END_ANY)
8565#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8566#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8567#define IS_LABEL_POSSIBLE() (\
8568 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8570#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8571#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8573static inline enum yytokentype
8574parser_string_term(struct parser_params *p, int func)
8576 xfree(p->lex.strterm);
8578 if (func & STR_FUNC_REGEXP) {
8579 set_yylval_num(regx_options(p));
8580 dispatch_scan_event(p, tREGEXP_END);
8581 SET_LEX_STATE(EXPR_END);
8584 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8586 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8589 SET_LEX_STATE(EXPR_END);
8593static enum yytokentype
8594parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8596 int func = quote->func;
8597 int term = quote->term;
8598 int paren = quote->paren;
8600 rb_encoding *enc = p->enc;
8601 rb_encoding *base_enc = 0;
8602 rb_parser_string_t *lit;
8604 if (func & STR_FUNC_TERM) {
8605 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8606 SET_LEX_STATE(EXPR_END);
8607 xfree(p->lex.strterm);
8609 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8612 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8613 while (c != '\n' && ISSPACE(c = nextc(p)));
8616 if (func & STR_FUNC_LIST) {
8617 quote->func &= ~STR_FUNC_LIST;
8620 if (c == term && !quote->nest) {
8621 if (func & STR_FUNC_QWORDS) {
8622 quote->func |= STR_FUNC_TERM;
8623 pushback(p, c); /* dispatch the term at tSTRING_END */
8624 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8627 return parser_string_term(p, func);
8630 if (!ISSPACE(c)) pushback(p, c);
8631 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8635 if ((func & STR_FUNC_EXPAND) && c == '#') {
8636 enum yytokentype t = parser_peek_variable_name(p);
8642 if (tokadd_string(p, func, term, paren, "e->nest,
8643 &enc, &base_enc) == -1) {
8646# define unterminated_literal(mesg) yyerror0(mesg)
8648# define unterminated_literal(mesg) compile_error(p, mesg)
8650 literal_flush(p, p->lex.pcur);
8651 if (func & STR_FUNC_QWORDS) {
8652 /* no content to add, bailing out here */
8653 unterminated_literal("unterminated list meets end of file");
8654 xfree(p->lex.strterm);
8658 if (func & STR_FUNC_REGEXP) {
8659 unterminated_literal("unterminated regexp meets end of file");
8662 unterminated_literal("unterminated string meets end of file");
8664 quote->func |= STR_FUNC_TERM;
8669 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8670 set_yylval_str(lit);
8671 flush_string_content(p, enc, 0);
8673 return tSTRING_CONTENT;
8676static enum yytokentype
8677heredoc_identifier(struct parser_params *p)
8680 * term_len is length of `<<"END"` except `END`,
8681 * in this case term_len is 4 (<, <, " and ").
8683 long len, offset = p->lex.pcur - p->lex.pbeg;
8684 int c = nextc(p), term, func = 0, quote = 0;
8685 enum yytokentype token = tSTRING_BEG;
8690 func = STR_FUNC_INDENT;
8693 else if (c == '~') {
8695 func = STR_FUNC_INDENT;
8701 func |= str_squote; goto quoted;
8703 func |= str_dquote; goto quoted;
8705 token = tXSTRING_BEG;
8706 func |= str_xquote; goto quoted;
8713 while ((c = nextc(p)) != term) {
8714 if (c == -1 || c == '\r' || c == '\n') {
8715 yyerror0("unterminated here document identifier");
8722 if (!parser_is_identchar(p)) {
8724 if (func & STR_FUNC_INDENT) {
8725 pushback(p, indent > 0 ? '~' : '-');
8731 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8732 if (n < 0) return 0;
8734 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8739 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8740 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8741 yyerror0("too long here document identifier");
8742 dispatch_scan_event(p, tHEREDOC_BEG);
8745 p->lex.strterm = new_heredoc(p);
8746 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8747 here->offset = offset;
8748 here->sourceline = p->ruby_sourceline;
8749 here->length = (unsigned)len;
8750 here->quote = quote;
8752 here->lastline = p->lex.lastline;
8755 p->heredoc_indent = indent;
8756 p->heredoc_line_indent = 0;
8761heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8763 rb_parser_string_t *line;
8764 rb_strterm_t *term = p->lex.strterm;
8767 line = here->lastline;
8768 p->lex.lastline = line;
8769 p->lex.pbeg = PARSER_STRING_PTR(line);
8770 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8771 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8772 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8773 p->heredoc_end = p->ruby_sourceline;
8774 p->ruby_sourceline = (int)here->sourceline;
8775 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8781dedent_string_column(const char *str, long len, int width)
8785 for (i = 0; i < len && col < width; i++) {
8786 if (str[i] == ' ') {
8789 else if (str[i] == '\t') {
8790 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8791 if (n > width) break;
8803dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8809 len = PARSER_STRING_LEN(string);
8810 str = PARSER_STRING_PTR(string);
8812 i = dedent_string_column(str, len, width);
8815 rb_parser_str_modify(string);
8816 str = PARSER_STRING_PTR(string);
8817 if (PARSER_STRING_LEN(string) != len)
8818 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8819 MEMMOVE(str, str + i, char, len - i);
8820 rb_parser_str_set_len(p, string, len - i);
8825heredoc_dedent(struct parser_params *p, NODE *root)
8827 NODE *node, *str_node, *prev_node;
8828 int indent = p->heredoc_indent;
8829 rb_parser_string_t *prev_lit = 0;
8831 if (indent <= 0) return root;
8832 if (!root) return root;
8834 prev_node = node = str_node = root;
8835 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8838 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8839 if (nd_fl_newline(str_node)) {
8840 dedent_string(p, lit, indent);
8845 else if (!literal_concat0(p, prev_lit, lit)) {
8849 NODE *end = RNODE_LIST(node)->as.nd_end;
8850 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8852 if (nd_type_p(prev_node, NODE_DSTR))
8853 nd_set_type(prev_node, NODE_STR);
8856 RNODE_LIST(node)->as.nd_end = end;
8861 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8863 if (!nd_type_p(node, NODE_LIST)) break;
8864 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8865 enum node_type type = nd_type(str_node);
8866 if (type == NODE_STR || type == NODE_DSTR) break;
8876whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8878 const char *beg = p->lex.pbeg;
8879 const char *ptr = p->lex.pend;
8881 if (ptr - beg < len) return FALSE;
8882 if (ptr > beg && ptr[-1] == '\n') {
8883 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8884 if (ptr - beg < len) return FALSE;
8886 if (strncmp(eos, ptr -= len, len)) return FALSE;
8888 while (beg < ptr && ISSPACE(*beg)) beg++;
8894word_match_p(struct parser_params *p, const char *word, long len)
8896 if (strncmp(p->lex.pcur, word, len)) return 0;
8897 if (lex_eol_n_p(p, len)) return 1;
8898 int c = (unsigned char)p->lex.pcur[len];
8899 if (ISSPACE(c)) return 1;
8901 case '\0': case '\004': case '\032': return 1;
8906#define NUM_SUFFIX_R (1<<0)
8907#define NUM_SUFFIX_I (1<<1)
8908#define NUM_SUFFIX_ALL 3
8911number_literal_suffix(struct parser_params *p, int mask)
8914 const char *lastp = p->lex.pcur;
8916 while ((c = nextc(p)) != -1) {
8917 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8918 result |= (mask & NUM_SUFFIX_I);
8919 mask &= ~NUM_SUFFIX_I;
8920 /* r after i, rational of complex is disallowed */
8921 mask &= ~NUM_SUFFIX_R;
8924 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8925 result |= (mask & NUM_SUFFIX_R);
8926 mask &= ~NUM_SUFFIX_R;
8929 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8930 p->lex.pcur = lastp;
8939static enum yytokentype
8940set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8942 enum rb_numeric_type numeric_type = integer_literal;
8944 if (type == tFLOAT) {
8945 numeric_type = float_literal;
8948 if (suffix & NUM_SUFFIX_R) {
8950 numeric_type = rational_literal;
8952 if (suffix & NUM_SUFFIX_I) {
8958 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8961 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8964 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8967 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8968 (void)numeric_type; /* for ripper */
8971 rb_bug("unexpected token: %d", type);
8973 SET_LEX_STATE(EXPR_END);
8977#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8979parser_dispatch_heredoc_end(struct parser_params *p, int line)
8981 if (has_delayed_token(p))
8982 dispatch_delayed_token(p, tSTRING_CONTENT);
8985 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8986 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8988 if (p->keep_tokens) {
8989 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
8990 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8991 parser_append_tokens(p, str, tHEREDOC_END, line);
8995 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9000static enum yytokentype
9001here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9003 int c, func, indent = 0;
9004 const char *eos, *ptr, *ptr_end;
9006 rb_parser_string_t *str = 0;
9007 rb_encoding *enc = p->enc;
9008 rb_encoding *base_enc = 0;
9014 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9016 indent = (func = here->func) & STR_FUNC_INDENT;
9018 if ((c = nextc(p)) == -1) {
9021 if (!has_delayed_token(p)) {
9022 dispatch_scan_event(p, tSTRING_CONTENT);
9024 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9025 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9026 if (!(func & STR_FUNC_REGEXP)) {
9027 int cr = ENC_CODERANGE_UNKNOWN;
9028 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9029 if (cr != ENC_CODERANGE_7BIT &&
9030 rb_is_usascii_enc(p->enc) &&
9031 enc != rb_utf8_encoding()) {
9032 enc = rb_ascii8bit_encoding();
9035 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9037 dispatch_delayed_token(p, tSTRING_CONTENT);
9040 dispatch_delayed_token(p, tSTRING_CONTENT);
9041 dispatch_scan_event(p, tSTRING_CONTENT);
9045 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9046 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9049 SET_LEX_STATE(EXPR_END);
9054 /* not beginning of line, cannot be the terminator */
9056 else if (p->heredoc_line_indent == -1) {
9057 /* `heredoc_line_indent == -1` means
9058 * - "after an interpolation in the same line", or
9059 * - "in a continuing line"
9061 p->heredoc_line_indent = 0;
9063 else if (whole_match_p(p, eos, len, indent)) {
9064 dispatch_heredoc_end(p);
9066 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9068 SET_LEX_STATE(EXPR_END);
9072 if (!(func & STR_FUNC_EXPAND)) {
9074 ptr = PARSER_STRING_PTR(p->lex.lastline);
9075 ptr_end = p->lex.pend;
9076 if (ptr_end > ptr) {
9077 switch (ptr_end[-1]) {
9079 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9088 if (p->heredoc_indent > 0) {
9090 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9092 p->heredoc_line_indent = 0;
9096 parser_str_cat(str, ptr, ptr_end - ptr);
9098 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9099 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9101 if (p->heredoc_indent > 0) {
9104 if (nextc(p) == -1) {
9106 rb_parser_string_free(p, str);
9111 } while (!whole_match_p(p, eos, len, indent));
9114 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9117 enum yytokentype t = parser_peek_variable_name(p);
9118 if (p->heredoc_line_indent != -1) {
9119 if (p->heredoc_indent > p->heredoc_line_indent) {
9120 p->heredoc_indent = p->heredoc_line_indent;
9122 p->heredoc_line_indent = -1;
9131 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9132 if (p->eofp) goto error;
9136 if (c == '\\') p->heredoc_line_indent = -1;
9138 str = STR_NEW3(tok(p), toklen(p), enc, func);
9140 set_yylval_str(str);
9142 if (bol) nd_set_fl_newline(yylval.node);
9144 flush_string_content(p, enc, 0);
9145 return tSTRING_CONTENT;
9147 tokadd(p, nextc(p));
9148 if (p->heredoc_indent > 0) {
9152 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9153 if ((c = nextc(p)) == -1) goto error;
9154 } while (!whole_match_p(p, eos, len, indent));
9155 str = STR_NEW3(tok(p), toklen(p), enc, func);
9157 dispatch_heredoc_end(p);
9158 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9160 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9162 /* Preserve s_value for set_yylval_str */
9163 s_value = p->s_value;
9165 set_yylval_str(str);
9167 set_parser_s_value(s_value);
9171 if (bol) nd_set_fl_newline(yylval.node);
9173 return tSTRING_CONTENT;
9179arg_ambiguous(struct parser_params *p, char c)
9182 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9184 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9189/* returns true value if formal argument error;
9190 * Qtrue, or error message if ripper */
9192formal_argument_error(struct parser_params *p, ID id)
9194 switch (id_type(id)) {
9198# define ERR(mesg) (yyerror0(mesg), Qtrue)
9200# define ERR(mesg) WARN_S(mesg)
9203 return ERR("formal argument cannot be a constant");
9205 return ERR("formal argument cannot be an instance variable");
9207 return ERR("formal argument cannot be a global variable");
9209 return ERR("formal argument cannot be a class variable");
9211 return ERR("formal argument must be local variable");
9214 shadowing_lvar(p, id);
9220lvar_defined(struct parser_params *p, ID id)
9222 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9225/* emacsen -*- hack */
9227parser_encode_length(struct parser_params *p, const char *name, long len)
9231 if (len > 5 && name[nlen = len - 5] == '-') {
9232 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9235 if (len > 4 && name[nlen = len - 4] == '-') {
9236 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9238 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9239 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9240 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9247parser_set_encode(struct parser_params *p, const char *name)
9253 const char *wrong = 0;
9255 case 'e': case 'E': wrong = "external"; break;
9256 case 'i': case 'I': wrong = "internal"; break;
9257 case 'f': case 'F': wrong = "filesystem"; break;
9258 case 'l': case 'L': wrong = "locale"; break;
9260 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9261 idx = rb_enc_find_index(name);
9264 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9266 excargs[0] = rb_eArgError;
9267 excargs[2] = rb_make_backtrace();
9268 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9269 VALUE exc = rb_make_exception(3, excargs);
9270 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9272 rb_ast_free(p->ast);
9277 enc = rb_enc_from_index(idx);
9278 if (!rb_enc_asciicompat(enc)) {
9279 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9284 if (p->debug_lines) {
9286 for (i = 0; i < p->debug_lines->len; i++) {
9287 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9294comment_at_top(struct parser_params *p)
9296 if (p->token_seen) return false;
9297 return (p->line_count == (p->has_shebang ? 2 : 1));
9300typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9301typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9303static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9306magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9308 if (!comment_at_top(p)) {
9311 parser_set_encode(p, val);
9315parser_get_bool(struct parser_params *p, const char *name, const char *val)
9319 if (STRCASECMP(val, "true") == 0) {
9324 if (STRCASECMP(val, "false") == 0) {
9329 return parser_invalid_pragma_value(p, name, val);
9333parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9335 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9340parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9342 int b = parser_get_bool(p, name, val);
9343 if (b >= 0) p->token_info_enabled = b;
9347parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9351 if (p->token_seen) {
9352 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9356 b = parser_get_bool(p, name, val);
9359 p->frozen_string_literal = b;
9363parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9365 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9366 if (*s == ' ' || *s == '\t') continue;
9367 if (*s == '#') break;
9368 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9374 if (STRCASECMP(val, "none") == 0) {
9375 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9380 if (STRCASECMP(val, "literal") == 0) {
9381 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9386 if (STRCASECMP(val, "experimental_copy") == 0) {
9387 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9390 if (STRCASECMP(val, "experimental_everything") == 0) {
9391 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9396 parser_invalid_pragma_value(p, name, val);
9401parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9403 int b = parser_get_bool(p, name, val);
9404 if (b >= 0) p->past_scope_enabled = b;
9408struct magic_comment {
9410 rb_magic_comment_setter_t func;
9411 rb_magic_comment_length_t length;
9414static const struct magic_comment magic_comments[] = {
9415 {"coding", magic_comment_encoding, parser_encode_length},
9416 {"encoding", magic_comment_encoding, parser_encode_length},
9417 {"frozen_string_literal", parser_set_frozen_string_literal},
9418 {"shareable_constant_value", parser_set_shareable_constant_value},
9419 {"warn_indent", parser_set_token_info},
9421 {"warn_past_scope", parser_set_past_scope},
9426magic_comment_marker(const char *str, long len)
9433 if (str[i-1] == '*' && str[i-2] == '-') {
9439 if (i + 1 >= len) return 0;
9440 if (str[i+1] != '-') {
9443 else if (str[i-1] != '-') {
9459parser_magic_comment(struct parser_params *p, const char *str, long len)
9462 VALUE name = 0, val = 0;
9463 const char *beg, *end, *vbeg, *vend;
9464#define str_copy(_s, _p, _n) ((_s) \
9465 ? (void)(rb_str_resize((_s), (_n)), \
9466 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9467 : (void)((_s) = STR_NEW((_p), (_n))))
9469 if (len <= 7) return FALSE;
9470 if (!!(beg = magic_comment_marker(str, len))) {
9471 if (!(end = magic_comment_marker(beg, str + len - beg)))
9475 len = end - beg - 3;
9478 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9480 const struct magic_comment *mc = magic_comments;
9485 for (; len > 0 && *str; str++, --len) {
9487 case '\'': case '"': case ':': case ';':
9490 if (!ISSPACE(*str)) break;
9492 for (beg = str; len > 0; str++, --len) {
9494 case '\'': case '"': case ':': case ';':
9497 if (ISSPACE(*str)) break;
9502 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9505 if (!indicator) return FALSE;
9509 do str++; while (--len > 0 && ISSPACE(*str));
9511 const char *tok_beg = str;
9513 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9526 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9529 const char *tok_end = str;
9531 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9534 while (len > 0 && (ISSPACE(*str))) --len, str++;
9535 if (len) return FALSE;
9539 str_copy(name, beg, n);
9540 s = RSTRING_PTR(name);
9541 for (i = 0; i < n; ++i) {
9542 if (s[i] == '-') s[i] = '_';
9545 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9548 n = (*mc->length)(p, vbeg, n);
9550 str_copy(val, vbeg, n);
9551 p->lex.ptok = tok_beg;
9552 p->lex.pcur = tok_end;
9553 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9556 } while (++mc < magic_comments + numberof(magic_comments));
9558 str_copy(val, vbeg, vend - vbeg);
9559 dispatch2(magic_comment, name, val);
9567set_file_encoding(struct parser_params *p, const char *str, const char *send)
9570 const char *beg = str;
9574 if (send - str <= 6) return;
9576 case 'C': case 'c': str += 6; continue;
9577 case 'O': case 'o': str += 5; continue;
9578 case 'D': case 'd': str += 4; continue;
9579 case 'I': case 'i': str += 3; continue;
9580 case 'N': case 'n': str += 2; continue;
9581 case 'G': case 'g': str += 1; continue;
9588 if (ISSPACE(*str)) break;
9591 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9596 if (++str >= send) return;
9597 } while (ISSPACE(*str));
9599 if (*str != '=' && *str != ':') return;
9604 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9605 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9608 parser_set_encode(p, RSTRING_PTR(s));
9609 rb_str_resize(s, 0);
9613parser_prepare(struct parser_params *p)
9615 int c = nextc0(p, FALSE);
9616 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9619 if (peek(p, '!')) p->has_shebang = 1;
9621 case 0xef: /* UTF-8 BOM marker */
9622 if (!lex_eol_n_p(p, 2) &&
9623 (unsigned char)p->lex.pcur[0] == 0xbb &&
9624 (unsigned char)p->lex.pcur[1] == 0xbf) {
9625 p->enc = rb_utf8_encoding();
9628 if (p->debug_lines) {
9629 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9632 p->lex.pbeg = p->lex.pcur;
9637 case -1: /* end of script. */
9641 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9645#define ambiguous_operator(tok, op, syn) ( \
9646 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9647 rb_warning0("even though it seems like "syn""))
9649#define ambiguous_operator(tok, op, syn) \
9650 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9652#define warn_balanced(tok, op, syn) ((void) \
9653 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9654 space_seen && !ISSPACE(c) && \
9655 (ambiguous_operator(tok, op, syn), 0)), \
9656 (enum yytokentype)(tok))
9658static enum yytokentype
9659no_digits(struct parser_params *p)
9661 yyerror0("numeric literal without digits");
9662 if (peek(p, '_')) nextc(p);
9663 /* dummy 0, for tUMINUS_NUM at numeric */
9664 return set_number_literal(p, tINTEGER, 0, 10, 0);
9667static enum yytokentype
9668parse_numeric(struct parser_params *p, int c)
9670 int is_float, seen_point, seen_e, nondigit;
9673 is_float = seen_point = seen_e = nondigit = 0;
9674 SET_LEX_STATE(EXPR_END);
9676 if (c == '-' || c == '+') {
9681 int start = toklen(p);
9683 if (c == 'x' || c == 'X') {
9686 if (c != -1 && ISXDIGIT(c)) {
9689 if (nondigit) break;
9693 if (!ISXDIGIT(c)) break;
9696 } while ((c = nextc(p)) != -1);
9700 if (toklen(p) == start) {
9701 return no_digits(p);
9703 else if (nondigit) goto trailing_uc;
9704 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9705 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9707 if (c == 'b' || c == 'B') {
9710 if (c == '0' || c == '1') {
9713 if (nondigit) break;
9717 if (c != '0' && c != '1') break;
9720 } while ((c = nextc(p)) != -1);
9724 if (toklen(p) == start) {
9725 return no_digits(p);
9727 else if (nondigit) goto trailing_uc;
9728 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9729 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9731 if (c == 'd' || c == 'D') {
9734 if (c != -1 && ISDIGIT(c)) {
9737 if (nondigit) break;
9741 if (!ISDIGIT(c)) break;
9744 } while ((c = nextc(p)) != -1);
9748 if (toklen(p) == start) {
9749 return no_digits(p);
9751 else if (nondigit) goto trailing_uc;
9752 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9753 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9759 if (c == 'o' || c == 'O') {
9760 /* prefixed octal */
9762 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9764 return no_digits(p);
9767 if (c >= '0' && c <= '7') {
9772 if (nondigit) break;
9776 if (c < '0' || c > '9') break;
9777 if (c > '7') goto invalid_octal;
9780 } while ((c = nextc(p)) != -1);
9781 if (toklen(p) > start) {
9784 if (nondigit) goto trailing_uc;
9785 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9786 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9793 if (c > '7' && c <= '9') {
9795 yyerror0("Invalid octal digit");
9797 else if (c == '.' || c == 'e' || c == 'E') {
9803 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9804 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9810 case '0': case '1': case '2': case '3': case '4':
9811 case '5': case '6': case '7': case '8': case '9':
9817 if (nondigit) goto trailing_uc;
9818 if (seen_point || seen_e) {
9823 if (c0 == -1 || !ISDIGIT(c0)) {
9829 seen_point = toklen(p);
9848 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9854 tokadd(p, nondigit);
9858 nondigit = (c == '-' || c == '+') ? c : 0;
9861 case '_': /* `_' in number just ignored */
9862 if (nondigit) goto decode_num;
9876 literal_flush(p, p->lex.pcur - 1);
9877 YYLTYPE loc = RUBY_INIT_YYLLOC();
9878 compile_error(p, "trailing '%c' in number", nondigit);
9879 parser_show_error_line(p, &loc);
9883 enum yytokentype type = tFLOAT;
9885 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9886 if (suffix & NUM_SUFFIX_R) {
9891 if (errno == ERANGE) {
9892 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9896 return set_number_literal(p, type, suffix, 0, seen_point);
9898 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9899 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9902static enum yytokentype
9903parse_qmark(struct parser_params *p, int space_seen)
9907 rb_parser_string_t *lit;
9908 const char *start = p->lex.pcur;
9911 SET_LEX_STATE(EXPR_VALUE);
9916 compile_error(p, "incomplete character syntax");
9919 if (rb_enc_isspace(c, p->enc)) {
9921 int c2 = escaped_control_code(c);
9923 WARN_SPACE_CHAR(c2, "?");
9928 SET_LEX_STATE(EXPR_VALUE);
9933 int w = parser_precise_mbclen(p, start);
9934 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9935 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9937 const char *ptr = start;
9939 int n = parser_precise_mbclen(p, ptr);
9940 if (n < 0) return -1;
9942 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9943 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9944 " a conditional operator, put a space after '?'",
9945 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9949 else if (c == '\\') {
9952 enc = rb_utf8_encoding();
9953 tokadd_utf8(p, &enc, -1, 0, 0);
9955 else if (!ISASCII(c = peekc(p)) && c != -1) {
9957 if (tokadd_mbchar(p, c) == -1) return 0;
9960 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9965 if (tokadd_mbchar(p, c) == -1) return 0;
9968 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9969 set_yylval_str(lit);
9970 SET_LEX_STATE(EXPR_END);
9974static enum yytokentype
9975parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9978 const char *ptok = p->lex.pcur;
9986 if (c == -1) goto unterminated;
9989 if (!ISASCII(c)) goto unknown;
9994 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9997 c = parser_precise_mbclen(p, p->lex.pcur);
9998 if (c < 0) return 0;
10000 yyerror0("unknown type of %string");
10006 compile_error(p, "unterminated quoted string meets end of file");
10010 if (term == '(') term = ')';
10011 else if (term == '[') term = ']';
10012 else if (term == '{') term = '}';
10013 else if (term == '<') term = '>';
10016 p->lex.ptok = ptok-1;
10019 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10020 return tSTRING_BEG;
10023 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10024 return tSTRING_BEG;
10027 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10031 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10032 return tQWORDS_BEG;
10035 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10036 return tSYMBOLS_BEG;
10039 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10040 return tQSYMBOLS_BEG;
10043 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10044 return tXSTRING_BEG;
10047 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10048 return tREGEXP_BEG;
10051 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10052 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10056 yyerror0("unknown type of %string");
10060 if ((c = nextc(p)) == '=') {
10061 set_yylval_id('%');
10062 SET_LEX_STATE(EXPR_BEG);
10065 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10068 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10070 return warn_balanced('%', "%%", "string literal");
10074tokadd_ident(struct parser_params *p, int c)
10077 if (tokadd_mbchar(p, c) == -1) return -1;
10079 } while (parser_is_identchar(p));
10085tokenize_ident(struct parser_params *p)
10087 ID ident = TOK_INTERN();
10089 set_yylval_name(ident);
10095parse_numvar(struct parser_params *p)
10099 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10100 const unsigned long nth_ref_max =
10101 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10102 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10103 * turned into a Fixnum, in compile.c */
10105 if (overflow || n > nth_ref_max) {
10106 /* compile_error()? */
10107 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10108 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10115static enum yytokentype
10116parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10118 const char *ptr = p->lex.pcur;
10121 SET_LEX_STATE(EXPR_END);
10122 p->lex.ptok = ptr - 1; /* from '$' */
10126 case '_': /* $_: last read line string */
10128 if (parser_is_identchar(p)) {
10136 case '~': /* $~: match-data */
10137 case '*': /* $*: argv */
10138 case '$': /* $$: pid */
10139 case '?': /* $?: last status */
10140 case '!': /* $!: error string */
10141 case '@': /* $@: error position */
10142 case '/': /* $/: input record separator */
10143 case '\\': /* $\: output record separator */
10144 case ';': /* $;: field separator */
10145 case ',': /* $,: output field separator */
10146 case '.': /* $.: last read line number */
10147 case '=': /* $=: ignorecase */
10148 case ':': /* $:: load path */
10149 case '<': /* $<: default input handle */
10150 case '>': /* $>: default output handle */
10151 case '\"': /* $": already loaded files */
10160 if (parser_is_identchar(p)) {
10161 if (tokadd_mbchar(p, c) == -1) return 0;
10172 case '&': /* $&: last match */
10173 case '`': /* $`: string before last match */
10174 case '\'': /* $': string after last match */
10175 case '+': /* $+: string matches last paren. */
10176 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10181 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10184 case '1': case '2': case '3':
10185 case '4': case '5': case '6':
10186 case '7': case '8': case '9':
10191 } while (c != -1 && ISDIGIT(c));
10193 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10195 c = parse_numvar(p);
10196 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10200 if (!parser_is_identchar(p)) {
10201 YYLTYPE loc = RUBY_INIT_YYLLOC();
10202 if (c == -1 || ISSPACE(c)) {
10203 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10207 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10209 parser_show_error_line(p, &loc);
10210 set_yylval_noname();
10218 if (tokadd_ident(p, c)) return 0;
10219 SET_LEX_STATE(EXPR_END);
10220 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10224 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10225 set_yylval_noname();
10231parser_numbered_param(struct parser_params *p, int n)
10233 if (n < 0) return false;
10235 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10238 if (p->max_numparam == ORDINAL_PARAM) {
10239 compile_error(p, "ordinary parameter is defined");
10242 struct vtable *args = p->lvtbl->args;
10243 if (p->max_numparam < n) {
10244 p->max_numparam = n;
10246 while (n > args->pos) {
10247 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10252static enum yytokentype
10253parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10255 const char *ptr = p->lex.pcur;
10256 enum yytokentype result = tIVAR;
10257 register int c = nextc(p);
10260 p->lex.ptok = ptr - 1; /* from '@' */
10268 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10269 if (c == -1 || !parser_is_identchar(p)) {
10271 RUBY_SET_YYLLOC(loc);
10272 if (result == tIVAR) {
10273 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10276 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10278 parser_show_error_line(p, &loc);
10279 set_yylval_noname();
10280 SET_LEX_STATE(EXPR_END);
10283 else if (ISDIGIT(c)) {
10285 RUBY_SET_YYLLOC(loc);
10286 if (result == tIVAR) {
10287 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10290 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10292 parser_show_error_line(p, &loc);
10293 set_yylval_noname();
10294 SET_LEX_STATE(EXPR_END);
10298 if (tokadd_ident(p, c)) return 0;
10303static enum yytokentype
10304parse_ident(struct parser_params *p, int c, int cmd_state)
10306 enum yytokentype result;
10307 bool is_ascii = true;
10308 const enum lex_state_e last_state = p->lex.state;
10310 int enforce_keyword_end = 0;
10313 if (!ISASCII(c)) is_ascii = false;
10314 if (tokadd_mbchar(p, c) == -1) return 0;
10316 } while (parser_is_identchar(p));
10317 if ((c == '!' || c == '?') && !peek(p, '=')) {
10321 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10322 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10323 result = tIDENTIFIER;
10327 result = tCONSTANT; /* assume provisionally */
10332 if (IS_LABEL_POSSIBLE()) {
10333 if (IS_LABEL_SUFFIX(0)) {
10334 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10342 if (peek_end_expect_token_locations(p)) {
10343 const rb_code_position_t *end_pos;
10344 int lineno, column;
10345 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10347 end_pos = peek_end_expect_token_locations(p)->pos;
10348 lineno = end_pos->lineno;
10349 column = end_pos->column;
10352 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10353 p->ruby_sourceline, beg_pos, lineno, column);
10356 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10357 const struct kwtable *kw;
10359 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10360 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10361 enforce_keyword_end = 1;
10367 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10368 const struct kwtable *kw;
10370 /* See if it is a reserved word. */
10371 kw = rb_reserved_word(tok(p), toklen(p));
10373 enum lex_state_e state = p->lex.state;
10374 if (IS_lex_state_for(state, EXPR_FNAME)) {
10375 SET_LEX_STATE(EXPR_ENDFN);
10376 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10379 SET_LEX_STATE(kw->state);
10380 if (IS_lex_state(EXPR_BEG)) {
10381 p->command_start = TRUE;
10383 if (kw->id[0] == keyword_do) {
10384 if (lambda_beginning_p()) {
10385 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10386 return keyword_do_LAMBDA;
10388 if (COND_P()) return keyword_do_cond;
10389 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10390 return keyword_do_block;
10393 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10396 if (kw->id[0] != kw->id[1])
10397 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10403 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10405 SET_LEX_STATE(EXPR_CMDARG);
10408 SET_LEX_STATE(EXPR_ARG);
10411 else if (p->lex.state == EXPR_FNAME) {
10412 SET_LEX_STATE(EXPR_ENDFN);
10415 SET_LEX_STATE(EXPR_END);
10418 ident = tokenize_ident(p);
10419 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10420 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10421 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10422 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10423 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10429warn_cr(struct parser_params *p)
10433 /* carried over with p->lex.nextline for nextc() */
10434 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10438static enum yytokentype
10439parser_yylex(struct parser_params *p)
10442 int space_seen = 0;
10445 enum lex_state_e last_state;
10446 int fallthru = FALSE;
10447 int token_seen = p->token_seen;
10449 if (p->lex.strterm) {
10450 if (strterm_is_heredoc(p->lex.strterm)) {
10452 return here_document(p, &p->lex.strterm->u.heredoc);
10456 return parse_string(p, &p->lex.strterm->u.literal);
10459 cmd_state = p->command_start;
10460 p->command_start = FALSE;
10461 p->token_seen = TRUE;
10466 last_state = p->lex.state;
10467 switch (c = nextc(p)) {
10468 case '\0': /* NUL */
10469 case '\004': /* ^D */
10470 case '\032': /* ^Z */
10471 case -1: /* end of script. */
10474 if (p->end_expect_token_locations) {
10475 pop_end_expect_token_locations(p);
10476 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10480 /* Set location for end-of-input because dispatch_scan_event is not called. */
10481 RUBY_SET_YYLLOC(*p->yylloc);
10482 return END_OF_INPUT;
10488 case ' ': case '\t': case '\f':
10489 case '\13': /* '\v' */
10491 while ((c = nextc(p))) {
10496 case ' ': case '\t': case '\f':
10497 case '\13': /* '\v' */
10505 dispatch_scan_event(p, tSP);
10511 case '#': /* it's a comment */
10512 p->token_seen = token_seen;
10513 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10514 /* no magic_comment in shebang line */
10515 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10516 if (comment_at_top(p)) {
10517 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10520 p->lex.pcur = pcur, p->lex.ptok = ptok;
10522 dispatch_scan_event(p, tCOMMENT);
10526 p->token_seen = token_seen;
10527 rb_parser_string_t *prevline = p->lex.lastline;
10528 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10529 !IS_lex_state(EXPR_LABELED));
10530 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10532 dispatch_scan_event(p, tIGNORED_NL);
10535 if (!c && p->ctxt.in_kwarg) {
10536 goto normal_newline;
10541 switch (c = nextc(p)) {
10542 case ' ': case '\t': case '\f': case '\r':
10543 case '\13': /* '\v' */
10549 dispatch_scan_event(p, tSP);
10554 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10557 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10560 if (peek(p, '|')) goto leading_logical;
10563 if (peek(p, '&')) {
10566 dispatch_delayed_token(p, tIGNORED_NL);
10572 dispatch_delayed_token(p, tIGNORED_NL);
10573 if (peek(p, '.') == (c == '&')) {
10575 dispatch_scan_event(p, tSP);
10581 p->ruby_sourceline--;
10582 p->lex.nextline = p->lex.lastline;
10583 set_lastline(p, prevline);
10584 case -1: /* EOF no decrement*/
10585 if (c == -1 && space_seen) {
10586 dispatch_scan_event(p, tSP);
10591 RUBY_SET_YYLLOC(*p->yylloc);
10593 goto normal_newline;
10597 p->command_start = TRUE;
10598 SET_LEX_STATE(EXPR_BEG);
10602 if ((c = nextc(p)) == '*') {
10603 if ((c = nextc(p)) == '=') {
10604 set_yylval_id(idPow);
10605 SET_LEX_STATE(EXPR_BEG);
10609 if (IS_SPCARG(c)) {
10610 rb_warning0("'**' interpreted as argument prefix");
10613 else if (IS_BEG()) {
10617 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10622 set_yylval_id('*');
10623 SET_LEX_STATE(EXPR_BEG);
10627 if (IS_SPCARG(c)) {
10628 rb_warning0("'*' interpreted as argument prefix");
10631 else if (IS_BEG()) {
10635 c = warn_balanced('*', "*", "argument prefix");
10638 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10643 if (IS_AFTER_OPERATOR()) {
10644 SET_LEX_STATE(EXPR_ARG);
10650 SET_LEX_STATE(EXPR_BEG);
10663 /* skip embedded rd document */
10664 if (word_match_p(p, "begin", 5)) {
10665 int first_p = TRUE;
10668 dispatch_scan_event(p, tEMBDOC_BEG);
10672 dispatch_scan_event(p, tEMBDOC);
10677 compile_error(p, "embedded document meets end of file");
10678 return END_OF_INPUT;
10680 if (c == '=' && word_match_p(p, "end", 3)) {
10686 dispatch_scan_event(p, tEMBDOC_END);
10691 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10692 if ((c = nextc(p)) == '=') {
10693 if ((c = nextc(p)) == '=') {
10702 else if (c == '>') {
10711 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10713 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10714 enum yytokentype token = heredoc_identifier(p);
10715 if (token) return token < 0 ? 0 : token;
10717 if (IS_AFTER_OPERATOR()) {
10718 SET_LEX_STATE(EXPR_ARG);
10721 if (IS_lex_state(EXPR_CLASS))
10722 p->command_start = TRUE;
10723 SET_LEX_STATE(EXPR_BEG);
10726 if ((c = nextc(p)) == '>') {
10733 if ((c = nextc(p)) == '=') {
10734 set_yylval_id(idLTLT);
10735 SET_LEX_STATE(EXPR_BEG);
10739 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10745 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10746 if ((c = nextc(p)) == '=') {
10750 if ((c = nextc(p)) == '=') {
10751 set_yylval_id(idGTGT);
10752 SET_LEX_STATE(EXPR_BEG);
10762 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10763 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10764 p->lex.ptok = p->lex.pcur-1;
10765 return tSTRING_BEG;
10768 if (IS_lex_state(EXPR_FNAME)) {
10769 SET_LEX_STATE(EXPR_ENDFN);
10772 if (IS_lex_state(EXPR_DOT)) {
10774 SET_LEX_STATE(EXPR_CMDARG);
10776 SET_LEX_STATE(EXPR_ARG);
10779 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10780 return tXSTRING_BEG;
10783 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10784 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10785 p->lex.ptok = p->lex.pcur-1;
10786 return tSTRING_BEG;
10789 return parse_qmark(p, space_seen);
10792 if ((c = nextc(p)) == '&') {
10793 SET_LEX_STATE(EXPR_BEG);
10794 if ((c = nextc(p)) == '=') {
10795 set_yylval_id(idANDOP);
10796 SET_LEX_STATE(EXPR_BEG);
10802 else if (c == '=') {
10803 set_yylval_id('&');
10804 SET_LEX_STATE(EXPR_BEG);
10807 else if (c == '.') {
10808 set_yylval_id(idANDDOT);
10809 SET_LEX_STATE(EXPR_DOT);
10813 if (IS_SPCARG(c)) {
10815 (c = peekc_n(p, 1)) == -1 ||
10816 !(c == '\'' || c == '"' ||
10817 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10818 rb_warning0("'&' interpreted as argument prefix");
10822 else if (IS_BEG()) {
10826 c = warn_balanced('&', "&", "argument prefix");
10828 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10832 if ((c = nextc(p)) == '|') {
10833 SET_LEX_STATE(EXPR_BEG);
10834 if ((c = nextc(p)) == '=') {
10835 set_yylval_id(idOROP);
10836 SET_LEX_STATE(EXPR_BEG);
10840 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10848 set_yylval_id('|');
10849 SET_LEX_STATE(EXPR_BEG);
10852 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10858 if (IS_AFTER_OPERATOR()) {
10859 SET_LEX_STATE(EXPR_ARG);
10867 set_yylval_id('+');
10868 SET_LEX_STATE(EXPR_BEG);
10871 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10872 SET_LEX_STATE(EXPR_BEG);
10874 if (c != -1 && ISDIGIT(c)) {
10875 return parse_numeric(p, '+');
10879 SET_LEX_STATE(EXPR_BEG);
10881 return warn_balanced('+', "+", "unary operator");
10885 if (IS_AFTER_OPERATOR()) {
10886 SET_LEX_STATE(EXPR_ARG);
10894 set_yylval_id('-');
10895 SET_LEX_STATE(EXPR_BEG);
10899 SET_LEX_STATE(EXPR_ENDFN);
10900 yylval.num = p->lex.lpar_beg;
10901 p->lex.lpar_beg = p->lex.paren_nest;
10904 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10905 SET_LEX_STATE(EXPR_BEG);
10907 if (c != -1 && ISDIGIT(c)) {
10908 return tUMINUS_NUM;
10912 SET_LEX_STATE(EXPR_BEG);
10914 return warn_balanced('-', "-", "unary operator");
10917 int is_beg = IS_BEG();
10918 SET_LEX_STATE(EXPR_BEG);
10919 if ((c = nextc(p)) == '.') {
10920 if ((c = nextc(p)) == '.') {
10921 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10922 SET_LEX_STATE(EXPR_ENDARG);
10925 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10926 rb_warn0("... at EOL, should be parenthesized?");
10928 return is_beg ? tBDOT3 : tDOT3;
10931 return is_beg ? tBDOT2 : tDOT2;
10934 if (c != -1 && ISDIGIT(c)) {
10935 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10936 parse_numeric(p, '.');
10937 if (ISDIGIT(prev)) {
10938 yyerror0("unexpected fraction part after numeric literal");
10941 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10943 SET_LEX_STATE(EXPR_END);
10944 p->lex.ptok = p->lex.pcur;
10947 set_yylval_id('.');
10948 SET_LEX_STATE(EXPR_DOT);
10952 case '0': case '1': case '2': case '3': case '4':
10953 case '5': case '6': case '7': case '8': case '9':
10954 return parse_numeric(p, c);
10959 SET_LEX_STATE(EXPR_ENDFN);
10960 p->lex.paren_nest--;
10966 SET_LEX_STATE(EXPR_END);
10967 p->lex.paren_nest--;
10971 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10972 if (!p->lex.brace_nest--) return tSTRING_DEND;
10975 SET_LEX_STATE(EXPR_END);
10976 p->lex.paren_nest--;
10982 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10983 SET_LEX_STATE(EXPR_BEG);
10986 set_yylval_id(idCOLON2);
10987 SET_LEX_STATE(EXPR_DOT);
10990 if (IS_END() || ISSPACE(c) || c == '#') {
10992 c = warn_balanced(':', ":", "symbol literal");
10993 SET_LEX_STATE(EXPR_BEG);
10998 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11001 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11007 SET_LEX_STATE(EXPR_FNAME);
11012 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11013 return tREGEXP_BEG;
11015 if ((c = nextc(p)) == '=') {
11016 set_yylval_id('/');
11017 SET_LEX_STATE(EXPR_BEG);
11021 if (IS_SPCARG(c)) {
11022 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11023 return tREGEXP_BEG;
11025 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11026 return warn_balanced('/', "/", "regexp literal");
11029 if ((c = nextc(p)) == '=') {
11030 set_yylval_id('^');
11031 SET_LEX_STATE(EXPR_BEG);
11034 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11039 SET_LEX_STATE(EXPR_BEG);
11040 p->command_start = TRUE;
11044 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11048 if (IS_AFTER_OPERATOR()) {
11049 if ((c = nextc(p)) != '@') {
11052 SET_LEX_STATE(EXPR_ARG);
11055 SET_LEX_STATE(EXPR_BEG);
11063 else if (!space_seen) {
11064 /* foo( ... ) => method call, no ambiguity */
11066 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11069 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11070 rb_warning0("parentheses after method name is interpreted as "
11071 "an argument list, not a decomposed argument");
11073 p->lex.paren_nest++;
11076 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11080 p->lex.paren_nest++;
11081 if (IS_AFTER_OPERATOR()) {
11082 if ((c = nextc(p)) == ']') {
11083 p->lex.paren_nest--;
11084 SET_LEX_STATE(EXPR_ARG);
11085 if ((c = nextc(p)) == '=') {
11092 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11095 else if (IS_BEG()) {
11098 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11101 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11107 ++p->lex.brace_nest;
11108 if (lambda_beginning_p())
11110 else if (IS_lex_state(EXPR_LABELED))
11111 c = tLBRACE; /* hash */
11112 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11113 c = '{'; /* block (primary) */
11114 else if (IS_lex_state(EXPR_ENDARG))
11115 c = tLBRACE_ARG; /* block (expr) */
11117 c = tLBRACE; /* hash */
11118 if (c != tLBRACE) {
11119 p->command_start = TRUE;
11120 SET_LEX_STATE(EXPR_BEG);
11123 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11125 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11134 dispatch_scan_event(p, tSP);
11135 goto retry; /* skip \\n */
11137 if (c == ' ') return tSP;
11138 if (ISSPACE(c)) return c;
11143 return parse_percent(p, space_seen, last_state);
11146 return parse_gvar(p, last_state);
11149 return parse_atmark(p, last_state);
11152 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11153 p->ruby__end__seen = 1;
11157 dispatch_scan_event(p, k__END__);
11159 return END_OF_INPUT;
11165 if (!parser_is_identchar(p)) {
11166 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11175 return parse_ident(p, c, cmd_state);
11178static enum yytokentype
11179yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11181 enum yytokentype t;
11185 p->yylloc = yylloc;
11187 t = parser_yylex(p);
11189 if (has_delayed_token(p))
11190 dispatch_delayed_token(p, t);
11191 else if (t != END_OF_INPUT)
11192 dispatch_scan_event(p, t);
11197#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11200node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11202 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11204 rb_node_init(n, type);
11209nd_set_loc(NODE *nd, const YYLTYPE *loc)
11212 nd_set_line(nd, loc->beg_pos.lineno);
11217node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11219 NODE *n = node_new_internal(p, type, size, alignment);
11221 nd_set_loc(n, loc);
11222 nd_set_node_id(n, parser_get_node_id(p));
11226#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11228static rb_node_scope_t *
11229rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11231 rb_ast_id_table_t *nd_tbl;
11232 nd_tbl = local_tbl(p);
11233 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11234 n->nd_tbl = nd_tbl;
11235 n->nd_body = nd_body;
11236 n->nd_parent = nd_parent;
11237 n->nd_args = nd_args;
11242static rb_node_scope_t *
11243rb_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)
11245 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11246 n->nd_tbl = nd_tbl;
11247 n->nd_body = nd_body;
11248 n->nd_parent = nd_parent;
11249 n->nd_args = nd_args;
11254static rb_node_defn_t *
11255rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11257 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11258 n->nd_mid = nd_mid;
11259 n->nd_defn = nd_defn;
11264static rb_node_defs_t *
11265rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11267 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11268 n->nd_recv = nd_recv;
11269 n->nd_mid = nd_mid;
11270 n->nd_defn = nd_defn;
11275static rb_node_block_t *
11276rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11278 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11279 n->nd_head = nd_head;
11280 n->nd_end = (NODE *)n;
11286static rb_node_for_t *
11287rb_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)
11289 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11290 n->nd_body = nd_body;
11291 n->nd_iter = nd_iter;
11292 n->for_keyword_loc = *for_keyword_loc;
11293 n->in_keyword_loc = *in_keyword_loc;
11294 n->do_keyword_loc = *do_keyword_loc;
11295 n->end_keyword_loc = *end_keyword_loc;
11300static rb_node_for_masgn_t *
11301rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11303 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11304 n->nd_var = nd_var;
11309static rb_node_retry_t *
11310rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11312 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11317static rb_node_begin_t *
11318rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11320 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11321 n->nd_body = nd_body;
11326static rb_node_rescue_t *
11327rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11329 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11330 n->nd_head = nd_head;
11331 n->nd_resq = nd_resq;
11332 n->nd_else = nd_else;
11337static rb_node_resbody_t *
11338rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11340 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11341 n->nd_args = nd_args;
11342 n->nd_exc_var = nd_exc_var;
11343 n->nd_body = nd_body;
11344 n->nd_next = nd_next;
11349static rb_node_ensure_t *
11350rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11352 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11353 n->nd_head = nd_head;
11354 n->nd_ensr = nd_ensr;
11359static rb_node_and_t *
11360rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11362 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11363 n->nd_1st = nd_1st;
11364 n->nd_2nd = nd_2nd;
11365 n->operator_loc = *operator_loc;
11370static rb_node_or_t *
11371rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11373 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11374 n->nd_1st = nd_1st;
11375 n->nd_2nd = nd_2nd;
11376 n->operator_loc = *operator_loc;
11381static rb_node_return_t *
11382rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11384 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11385 n->nd_stts = nd_stts;
11386 n->keyword_loc = *keyword_loc;
11390static rb_node_yield_t *
11391rb_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)
11393 if (nd_head) no_blockarg(p, nd_head);
11395 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11396 n->nd_head = nd_head;
11397 n->keyword_loc = *keyword_loc;
11398 n->lparen_loc = *lparen_loc;
11399 n->rparen_loc = *rparen_loc;
11404static rb_node_if_t *
11405rb_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)
11407 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11408 n->nd_cond = nd_cond;
11409 n->nd_body = nd_body;
11410 n->nd_else = nd_else;
11411 n->if_keyword_loc = *if_keyword_loc;
11412 n->then_keyword_loc = *then_keyword_loc;
11413 n->end_keyword_loc = *end_keyword_loc;
11418static rb_node_unless_t *
11419rb_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)
11421 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11422 n->nd_cond = nd_cond;
11423 n->nd_body = nd_body;
11424 n->nd_else = nd_else;
11425 n->keyword_loc = *keyword_loc;
11426 n->then_keyword_loc = *then_keyword_loc;
11427 n->end_keyword_loc = *end_keyword_loc;
11432static rb_node_class_t *
11433rb_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)
11435 /* Keep the order of node creation */
11436 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11437 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11438 RNODE_SCOPE(scope)->nd_parent = &n->node;
11439 n->nd_cpath = nd_cpath;
11440 n->nd_body = scope;
11441 n->nd_super = nd_super;
11442 n->class_keyword_loc = *class_keyword_loc;
11443 n->inheritance_operator_loc = *inheritance_operator_loc;
11444 n->end_keyword_loc = *end_keyword_loc;
11449static rb_node_sclass_t *
11450rb_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)
11452 /* Keep the order of node creation */
11453 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11454 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11455 RNODE_SCOPE(scope)->nd_parent = &n->node;
11456 n->nd_recv = nd_recv;
11457 n->nd_body = scope;
11458 n->class_keyword_loc = *class_keyword_loc;
11459 n->operator_loc = *operator_loc;
11460 n->end_keyword_loc = *end_keyword_loc;
11465static rb_node_module_t *
11466rb_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)
11468 /* Keep the order of node creation */
11469 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11470 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11471 RNODE_SCOPE(scope)->nd_parent = &n->node;
11472 n->nd_cpath = nd_cpath;
11473 n->nd_body = scope;
11474 n->module_keyword_loc = *module_keyword_loc;
11475 n->end_keyword_loc = *end_keyword_loc;
11480static rb_node_iter_t *
11481rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11483 /* Keep the order of node creation */
11484 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11485 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11486 RNODE_SCOPE(scope)->nd_parent = &n->node;
11487 n->nd_body = scope;
11493static rb_node_lambda_t *
11494rb_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)
11496 /* Keep the order of node creation */
11497 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11498 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11499 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11500 RNODE_SCOPE(scope)->nd_parent = &n->node;
11501 n->nd_body = scope;
11502 n->operator_loc = *operator_loc;
11503 n->opening_loc = *opening_loc;
11504 n->closing_loc = *closing_loc;
11509static rb_node_case_t *
11510rb_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)
11512 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11513 n->nd_head = nd_head;
11514 n->nd_body = nd_body;
11515 n->case_keyword_loc = *case_keyword_loc;
11516 n->end_keyword_loc = *end_keyword_loc;
11521static rb_node_case2_t *
11522rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11524 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11526 n->nd_body = nd_body;
11527 n->case_keyword_loc = *case_keyword_loc;
11528 n->end_keyword_loc = *end_keyword_loc;
11533static rb_node_case3_t *
11534rb_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)
11536 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11537 n->nd_head = nd_head;
11538 n->nd_body = nd_body;
11539 n->case_keyword_loc = *case_keyword_loc;
11540 n->end_keyword_loc = *end_keyword_loc;
11545static rb_node_when_t *
11546rb_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)
11548 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11549 n->nd_head = nd_head;
11550 n->nd_body = nd_body;
11551 n->nd_next = nd_next;
11552 n->keyword_loc = *keyword_loc;
11553 n->then_keyword_loc = *then_keyword_loc;
11558static rb_node_in_t *
11559rb_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)
11561 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11562 n->nd_head = nd_head;
11563 n->nd_body = nd_body;
11564 n->nd_next = nd_next;
11565 n->in_keyword_loc = *in_keyword_loc;
11566 n->then_keyword_loc = *then_keyword_loc;
11567 n->operator_loc = *operator_loc;
11572static rb_node_while_t *
11573rb_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)
11575 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11576 n->nd_cond = nd_cond;
11577 n->nd_body = nd_body;
11578 n->nd_state = nd_state;
11579 n->keyword_loc = *keyword_loc;
11580 n->closing_loc = *closing_loc;
11585static rb_node_until_t *
11586rb_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)
11588 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11589 n->nd_cond = nd_cond;
11590 n->nd_body = nd_body;
11591 n->nd_state = nd_state;
11592 n->keyword_loc = *keyword_loc;
11593 n->closing_loc = *closing_loc;
11598static rb_node_colon2_t *
11599rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11601 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11602 n->nd_head = nd_head;
11603 n->nd_mid = nd_mid;
11604 n->delimiter_loc = *delimiter_loc;
11605 n->name_loc = *name_loc;
11610static rb_node_colon3_t *
11611rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11613 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11614 n->nd_mid = nd_mid;
11615 n->delimiter_loc = *delimiter_loc;
11616 n->name_loc = *name_loc;
11621static rb_node_dot2_t *
11622rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11624 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11625 n->nd_beg = nd_beg;
11626 n->nd_end = nd_end;
11627 n->operator_loc = *operator_loc;
11632static rb_node_dot3_t *
11633rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11635 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11636 n->nd_beg = nd_beg;
11637 n->nd_end = nd_end;
11638 n->operator_loc = *operator_loc;
11643static rb_node_self_t *
11644rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11646 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11652static rb_node_nil_t *
11653rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11655 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11660static rb_node_true_t *
11661rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11663 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11668static rb_node_false_t *
11669rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11671 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11676static rb_node_super_t *
11677rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11678 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11680 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11681 n->nd_args = nd_args;
11682 n->keyword_loc = *keyword_loc;
11683 n->lparen_loc = *lparen_loc;
11684 n->rparen_loc = *rparen_loc;
11689static rb_node_zsuper_t *
11690rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11692 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11697static rb_node_match2_t *
11698rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11700 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11701 n->nd_recv = nd_recv;
11702 n->nd_value = nd_value;
11708static rb_node_match3_t *
11709rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11711 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11712 n->nd_recv = nd_recv;
11713 n->nd_value = nd_value;
11718static rb_node_list_t *
11719rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11721 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11724static rb_node_list_t *
11725rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11727 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11728 n->nd_head = nd_head;
11729 n->as.nd_alen = nd_alen;
11730 n->nd_next = nd_next;
11735static rb_node_zlist_t *
11736rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11738 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11743static rb_node_hash_t *
11744rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11746 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11747 n->nd_head = nd_head;
11753static rb_node_masgn_t *
11754rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11756 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11757 n->nd_head = nd_head;
11759 n->nd_args = nd_args;
11764static rb_node_gasgn_t *
11765rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11767 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11768 n->nd_vid = nd_vid;
11769 n->nd_value = nd_value;
11774static rb_node_lasgn_t *
11775rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11777 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11778 n->nd_vid = nd_vid;
11779 n->nd_value = nd_value;
11784static rb_node_dasgn_t *
11785rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11787 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11788 n->nd_vid = nd_vid;
11789 n->nd_value = nd_value;
11794static rb_node_iasgn_t *
11795rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11797 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11798 n->nd_vid = nd_vid;
11799 n->nd_value = nd_value;
11804static rb_node_cvasgn_t *
11805rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11807 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11808 n->nd_vid = nd_vid;
11809 n->nd_value = nd_value;
11814static rb_node_op_asgn1_t *
11815rb_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)
11817 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11818 n->nd_recv = nd_recv;
11819 n->nd_mid = nd_mid;
11820 n->nd_index = index;
11821 n->nd_rvalue = rvalue;
11822 n->call_operator_loc = *call_operator_loc;
11823 n->opening_loc = *opening_loc;
11824 n->closing_loc = *closing_loc;
11825 n->binary_operator_loc = *binary_operator_loc;
11830static rb_node_op_asgn2_t *
11831rb_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)
11833 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11834 n->nd_recv = nd_recv;
11835 n->nd_value = nd_value;
11836 n->nd_vid = nd_vid;
11837 n->nd_mid = nd_mid;
11838 n->nd_aid = nd_aid;
11839 n->call_operator_loc = *call_operator_loc;
11840 n->message_loc = *message_loc;
11841 n->binary_operator_loc = *binary_operator_loc;
11846static rb_node_op_asgn_or_t *
11847rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11849 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11850 n->nd_head = nd_head;
11851 n->nd_value = nd_value;
11856static rb_node_op_asgn_and_t *
11857rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11859 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11860 n->nd_head = nd_head;
11861 n->nd_value = nd_value;
11866static rb_node_gvar_t *
11867rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11869 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11870 n->nd_vid = nd_vid;
11875static rb_node_lvar_t *
11876rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11878 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11879 n->nd_vid = nd_vid;
11884static rb_node_dvar_t *
11885rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11887 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11888 n->nd_vid = nd_vid;
11893static rb_node_ivar_t *
11894rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11896 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11897 n->nd_vid = nd_vid;
11902static rb_node_const_t *
11903rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11905 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11906 n->nd_vid = nd_vid;
11911static rb_node_cvar_t *
11912rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11914 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11915 n->nd_vid = nd_vid;
11920static rb_node_nth_ref_t *
11921rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11923 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11924 n->nd_nth = nd_nth;
11929static rb_node_back_ref_t *
11930rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11932 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11933 n->nd_nth = nd_nth;
11938static rb_node_integer_t *
11939rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11941 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11949static rb_node_float_t *
11950rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11952 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
11959static rb_node_rational_t *
11960rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
11962 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
11966 n->seen_point = seen_point;
11971static rb_node_imaginary_t *
11972rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
11974 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
11978 n->seen_point = seen_point;
11979 n->type = numeric_type;
11984static rb_node_str_t *
11985rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
11987 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11988 n->string = string;
11993/* TODO; Use union for NODE_DSTR2 */
11994static rb_node_dstr_t *
11995rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11997 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11998 n->string = string;
11999 n->as.nd_alen = nd_alen;
12000 n->nd_next = (rb_node_list_t *)nd_next;
12005static rb_node_dstr_t *
12006rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12008 return rb_node_dstr_new0(p, string, 1, 0, loc);
12011static rb_node_xstr_t *
12012rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12014 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12015 n->string = string;
12020static rb_node_dxstr_t *
12021rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12023 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12024 n->string = string;
12025 n->as.nd_alen = nd_alen;
12026 n->nd_next = (rb_node_list_t *)nd_next;
12031static rb_node_sym_t *
12032rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12034 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12035 n->string = rb_str_to_parser_string(p, str);
12040static rb_node_dsym_t *
12041rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12043 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12044 n->string = string;
12045 n->as.nd_alen = nd_alen;
12046 n->nd_next = (rb_node_list_t *)nd_next;
12051static rb_node_evstr_t *
12052rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12054 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12055 n->nd_body = nd_body;
12056 n->opening_loc = *opening_loc;
12057 n->closing_loc = *closing_loc;
12062static rb_node_regx_t *
12063rb_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)
12065 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12066 n->string = string;
12067 n->options = options & RE_OPTION_MASK;
12068 n->opening_loc = *opening_loc;
12069 n->content_loc = *content_loc;
12070 n->closing_loc = *closing_loc;
12075static rb_node_call_t *
12076rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12078 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12079 n->nd_recv = nd_recv;
12080 n->nd_mid = nd_mid;
12081 n->nd_args = nd_args;
12086static rb_node_opcall_t *
12087rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12089 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12090 n->nd_recv = nd_recv;
12091 n->nd_mid = nd_mid;
12092 n->nd_args = nd_args;
12097static rb_node_fcall_t *
12098rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12100 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12101 n->nd_mid = nd_mid;
12102 n->nd_args = nd_args;
12107static rb_node_qcall_t *
12108rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12110 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12111 n->nd_recv = nd_recv;
12112 n->nd_mid = nd_mid;
12113 n->nd_args = nd_args;
12118static rb_node_vcall_t *
12119rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12121 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12122 n->nd_mid = nd_mid;
12127static rb_node_once_t *
12128rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12130 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12131 n->nd_body = nd_body;
12136static rb_node_args_t *
12137rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12139 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12140 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12145static rb_node_args_aux_t *
12146rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12148 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12149 n->nd_pid = nd_pid;
12150 n->nd_plen = nd_plen;
12156static rb_node_opt_arg_t *
12157rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12159 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12160 n->nd_body = nd_body;
12166static rb_node_kw_arg_t *
12167rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12169 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12170 n->nd_body = nd_body;
12176static rb_node_postarg_t *
12177rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12179 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12180 n->nd_1st = nd_1st;
12181 n->nd_2nd = nd_2nd;
12186static rb_node_argscat_t *
12187rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12189 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12190 n->nd_head = nd_head;
12191 n->nd_body = nd_body;
12196static rb_node_argspush_t *
12197rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12199 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12200 n->nd_head = nd_head;
12201 n->nd_body = nd_body;
12206static rb_node_splat_t *
12207rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12209 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12210 n->nd_head = nd_head;
12211 n->operator_loc = *operator_loc;
12216static rb_node_block_pass_t *
12217rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12219 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12222 n->nd_body = nd_body;
12223 n->operator_loc = *operator_loc;
12228static rb_node_alias_t *
12229rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12231 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12232 n->nd_1st = nd_1st;
12233 n->nd_2nd = nd_2nd;
12234 n->keyword_loc = *keyword_loc;
12239static rb_node_valias_t *
12240rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12242 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12243 n->nd_alias = nd_alias;
12244 n->nd_orig = nd_orig;
12245 n->keyword_loc = *keyword_loc;
12250static rb_node_undef_t *
12251rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12253 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12254 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12255 n->keyword_loc = NULL_LOC;
12256 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12261static rb_node_errinfo_t *
12262rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12264 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12269static rb_node_defined_t *
12270rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12272 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12273 n->nd_head = nd_head;
12274 n->keyword_loc = *keyword_loc;
12279static rb_node_postexe_t *
12280rb_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)
12282 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12283 n->nd_body = nd_body;
12284 n->keyword_loc = *keyword_loc;
12285 n->opening_loc = *opening_loc;
12286 n->closing_loc = *closing_loc;
12291static rb_node_attrasgn_t *
12292rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12294 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12295 n->nd_recv = nd_recv;
12296 n->nd_mid = nd_mid;
12297 n->nd_args = nd_args;
12302static rb_node_aryptn_t *
12303rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12305 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12307 n->pre_args = pre_args;
12308 n->rest_arg = rest_arg;
12309 n->post_args = post_args;
12314static rb_node_hshptn_t *
12315rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12317 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12318 n->nd_pconst = nd_pconst;
12319 n->nd_pkwargs = nd_pkwargs;
12320 n->nd_pkwrestarg = nd_pkwrestarg;
12325static rb_node_fndptn_t *
12326rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12328 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12330 n->pre_rest_arg = pre_rest_arg;
12332 n->post_rest_arg = post_rest_arg;
12337static rb_node_line_t *
12338rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12340 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12345static rb_node_file_t *
12346rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12348 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12349 n->path = rb_str_to_parser_string(p, str);
12354static rb_node_encoding_t *
12355rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12357 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12363static rb_node_cdecl_t *
12364rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12366 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12367 n->nd_vid = nd_vid;
12368 n->nd_value = nd_value;
12369 n->nd_else = nd_else;
12370 n->shareability = shareability;
12375static rb_node_op_cdecl_t *
12376rb_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)
12378 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12379 n->nd_head = nd_head;
12380 n->nd_value = nd_value;
12381 n->nd_aid = nd_aid;
12382 n->shareability = shareability;
12387static rb_node_error_t *
12388rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12390 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12395static rb_node_break_t *
12396rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12398 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12399 n->nd_stts = nd_stts;
12401 n->keyword_loc = *keyword_loc;
12406static rb_node_next_t *
12407rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12409 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12410 n->nd_stts = nd_stts;
12412 n->keyword_loc = *keyword_loc;
12417static rb_node_redo_t *
12418rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12420 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12422 n->keyword_loc = *keyword_loc;
12427static rb_node_def_temp_t *
12428rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12430 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12431 n->save.numparam_save = 0;
12432 n->save.max_numparam = 0;
12433 n->save.ctxt = p->ctxt;
12440static rb_node_def_temp_t *
12441def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12443 n->save.numparam_save = numparam_push(p);
12444 n->save.max_numparam = p->max_numparam;
12449static enum node_type
12450nodetype(NODE *node) /* for debug */
12452 return (enum node_type)nd_type(node);
12456nodeline(NODE *node)
12458 return nd_line(node);
12463newline_node(NODE *node)
12466 node = remove_begin(node);
12467 nd_set_fl_newline(node);
12473fixpos(NODE *node, NODE *orig)
12477 nd_set_line(node, nd_line(orig));
12481block_append(struct parser_params *p, NODE *head, NODE *tail)
12483 NODE *end, *h = head, *nd;
12485 if (tail == 0) return head;
12487 if (h == 0) return tail;
12488 switch (nd_type(h)) {
12490 h = end = NEW_BLOCK(head, &head->nd_loc);
12494 end = RNODE_BLOCK(h)->nd_end;
12498 nd = RNODE_BLOCK(end)->nd_head;
12499 switch (nd_type(nd)) {
12505 rb_warning0L(nd_line(tail), "statement not reached");
12512 if (!nd_type_p(tail, NODE_BLOCK)) {
12513 tail = NEW_BLOCK(tail, &tail->nd_loc);
12515 RNODE_BLOCK(end)->nd_next = tail;
12516 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12517 nd_set_last_loc(head, nd_last_loc(tail));
12521/* append item to the list */
12523list_append(struct parser_params *p, NODE *list, NODE *item)
12527 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12528 if (RNODE_LIST(list)->nd_next) {
12529 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12535 RNODE_LIST(list)->as.nd_alen += 1;
12536 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12537 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12539 nd_set_last_loc(list, nd_last_loc(item));
12544/* concat two lists */
12546list_concat(NODE *head, NODE *tail)
12550 if (RNODE_LIST(head)->nd_next) {
12551 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12557 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12558 RNODE_LIST(last)->nd_next = tail;
12559 if (RNODE_LIST(tail)->nd_next) {
12560 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12563 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12566 nd_set_last_loc(head, nd_last_loc(tail));
12572literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12574 if (!tail) return 1;
12575 if (!rb_parser_enc_compatible(p, head, tail)) {
12576 compile_error(p, "string literal encodings differ (%s / %s)",
12577 rb_enc_name(rb_parser_str_get_encoding(head)),
12578 rb_enc_name(rb_parser_str_get_encoding(tail)));
12579 rb_parser_str_resize(p, head, 0);
12580 rb_parser_str_resize(p, tail, 0);
12583 rb_parser_str_buf_append(p, head, tail);
12587static rb_parser_string_t *
12588string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12590 if (htype != NODE_DSTR) return NULL;
12591 if (RNODE_DSTR(head)->nd_next) {
12592 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12593 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12595 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12601static rb_parser_string_t *
12602rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12604 rb_parser_string_t *copy;
12605 if (!orig) return NULL;
12606 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12607 copy->coderange = orig->coderange;
12608 copy->enc = orig->enc;
12613/* concat two string literals */
12615literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12617 enum node_type htype;
12618 rb_parser_string_t *lit;
12620 if (!head) return tail;
12621 if (!tail) return head;
12623 htype = nd_type(head);
12624 if (htype == NODE_EVSTR) {
12625 head = new_dstr(p, head, loc);
12628 if (p->heredoc_indent > 0) {
12631 head = str2dstr(p, head);
12633 return list_append(p, head, tail);
12638 switch (nd_type(tail)) {
12640 if ((lit = string_literal_head(p, htype, head)) != false) {
12644 lit = RNODE_DSTR(head)->string;
12646 if (htype == NODE_STR) {
12647 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12649 rb_discard_node(p, head);
12650 rb_discard_node(p, tail);
12653 rb_discard_node(p, tail);
12656 list_append(p, head, tail);
12661 if (htype == NODE_STR) {
12662 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12664 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12665 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12666 RNODE_STR(head)->string = NULL;
12667 rb_discard_node(p, head);
12670 else if (!RNODE_DSTR(tail)->string) {
12672 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12673 if (!RNODE_DSTR(head)->nd_next) {
12674 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12676 else if (RNODE_DSTR(tail)->nd_next) {
12677 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12678 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12680 rb_discard_node(p, tail);
12682 else if ((lit = string_literal_head(p, htype, head)) != false) {
12683 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12685 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12686 RNODE_DSTR(tail)->string = 0;
12690 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));
12691 RNODE_DSTR(tail)->string = 0;
12696 if (htype == NODE_STR) {
12697 head = str2dstr(p, head);
12698 RNODE_DSTR(head)->as.nd_alen = 1;
12700 list_append(p, head, tail);
12707nd_copy_flag(NODE *new_node, NODE *old_node)
12709 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12710 nd_set_line(new_node, nd_line(old_node));
12711 new_node->nd_loc = old_node->nd_loc;
12712 new_node->node_id = old_node->node_id;
12716str2dstr(struct parser_params *p, NODE *node)
12718 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12719 nd_copy_flag(new_node, node);
12720 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12721 RNODE_DSTR(new_node)->as.nd_alen = 0;
12722 RNODE_DSTR(new_node)->nd_next = 0;
12723 RNODE_STR(node)->string = 0;
12729str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12731 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12732 nd_copy_flag(new_node, node);
12733 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12734 RNODE_REGX(new_node)->options = options;
12735 nd_set_loc(new_node, loc);
12736 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12737 RNODE_REGX(new_node)->content_loc = *content_loc;
12738 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12739 RNODE_STR(node)->string = 0;
12745evstr2dstr(struct parser_params *p, NODE *node)
12747 if (nd_type_p(node, NODE_EVSTR)) {
12748 node = new_dstr(p, node, &node->nd_loc);
12754new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12759 switch (nd_type(node)) {
12761 return str2dstr(p, node);
12768 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12772new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12774 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12775 return list_append(p, dstr, node);
12779call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12780 const YYLTYPE *op_loc, const YYLTYPE *loc)
12783 value_expr(p, recv);
12784 value_expr(p, arg1);
12785 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12786 nd_set_line(expr, op_loc->beg_pos.lineno);
12791call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12794 value_expr(p, recv);
12795 opcall = NEW_OPCALL(recv, id, 0, loc);
12796 nd_set_line(opcall, op_loc->beg_pos.lineno);
12801new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12803 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12804 nd_set_line(qcall, op_loc->beg_pos.lineno);
12809new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12812 if (block) block_dup_check(p, args, block);
12813 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12814 if (block) ret = method_add_block(p, ret, block, loc);
12819static rb_locations_lambda_body_t*
12820new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12822 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12824 body->opening_loc = *opening_loc;
12825 body->closing_loc = *closing_loc;
12830command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12833 enum node_type type = nd_type(m);
12836 compile_error(p, "block given to yield");
12839 body = &RNODE_RETURN(m)->nd_stts;
12843 body = &RNODE_EXITS(m)->nd_stts;
12845 case NODE_REDO: /* `redo` has no argument */
12846 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12848 case NODE_RETRY: /* `retry` has no argument */
12849 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12852 block_dup_check(p, get_nd_args(p, m), b);
12853 return method_add_block(p, m, b, loc);
12855 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12856 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12857 *body = command_add_block(p, *body, b, &b_loc);
12858 m->nd_loc.end_pos = loc->end_pos;
12862#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12865last_expr_once_body(NODE *node)
12867 if (!node) return 0;
12868 return nd_once_body(node);
12872match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12875 int line = op_loc->beg_pos.lineno;
12877 value_expr(p, node1);
12878 value_expr(p, node2);
12880 if ((n = last_expr_once_body(node1)) != 0) {
12881 switch (nd_type(n)) {
12884 NODE *match = NEW_MATCH2(node1, node2, loc);
12885 nd_set_line(match, line);
12891 const VALUE lit = rb_node_regx_string_val(n);
12893 NODE *match = NEW_MATCH2(node1, node2, loc);
12894 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12895 nd_set_line(match, line);
12902 if ((n = last_expr_once_body(node2)) != 0) {
12905 switch (nd_type(n)) {
12907 match3 = NEW_MATCH3(node2, node1, loc);
12912 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12913 nd_set_line(n, line);
12917# if WARN_PAST_SCOPE
12919past_dvar_p(struct parser_params *p, ID id)
12921 struct vtable *past = p->lvtbl->past;
12923 if (vtable_included(past, id)) return 1;
12931numparam_nested_p(struct parser_params *p)
12933 struct local_vars *local = p->lvtbl;
12934 NODE *outer = local->numparam.outer;
12935 NODE *inner = local->numparam.inner;
12936 if (outer || inner) {
12937 NODE *used = outer ? outer : inner;
12938 compile_error(p, "numbered parameter is already used in %s block\n"
12939 "%s:%d: numbered parameter is already used here",
12940 outer ? "outer" : "inner",
12941 p->ruby_sourcefile, nd_line(used));
12942 parser_show_error_line(p, &used->nd_loc);
12949numparam_used_p(struct parser_params *p)
12951 NODE *numparam = p->lvtbl->numparam.current;
12953 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12954 "%s:%d: numbered parameter is already used here",
12955 p->ruby_sourcefile, nd_line(numparam));
12956 parser_show_error_line(p, &numparam->nd_loc);
12963it_used_p(struct parser_params *p)
12965 NODE *it = p->lvtbl->it;
12967 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12968 "%s:%d: 'it' is already used here",
12969 p->ruby_sourcefile, nd_line(it));
12970 parser_show_error_line(p, &it->nd_loc);
12977gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12983 return NEW_SELF(loc);
12985 return NEW_NIL(loc);
12987 return NEW_TRUE(loc);
12988 case keyword_false:
12989 return NEW_FALSE(loc);
12990 case keyword__FILE__:
12992 VALUE file = p->ruby_sourcefile_string;
12994 file = rb_str_new(0, 0);
12995 node = NEW_FILE(file, loc);
12998 case keyword__LINE__:
12999 return NEW_LINE(loc);
13000 case keyword__ENCODING__:
13001 return NEW_ENCODING(loc);
13004 switch (id_type(id)) {
13006 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13007 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13008 if (vidp) *vidp |= LVAR_USED;
13009 node = NEW_DVAR(id, loc);
13012 if (local_id_ref(p, id, &vidp)) {
13013 if (vidp) *vidp |= LVAR_USED;
13014 node = NEW_LVAR(id, loc);
13017 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13018 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13019 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13020 node = NEW_DVAR(id, loc);
13021 struct local_vars *local = p->lvtbl;
13022 if (!local->numparam.current) local->numparam.current = node;
13025# if WARN_PAST_SCOPE
13026 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13027 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13030 /* method call without arguments */
13031 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13032 if (numparam_used_p(p)) return 0;
13033 if (p->max_numparam == ORDINAL_PARAM) {
13034 compile_error(p, "ordinary parameter is defined");
13038 p->it_id = idItImplicit;
13039 vtable_add(p->lvtbl->args, p->it_id);
13041 NODE *node = NEW_DVAR(p->it_id, loc);
13042 if (!p->lvtbl->it) p->lvtbl->it = node;
13045 return NEW_VCALL(id, loc);
13047 return NEW_GVAR(id, loc);
13049 return NEW_IVAR(id, loc);
13051 return NEW_CONST(id, loc);
13053 return NEW_CVAR(id, loc);
13055 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13059static rb_node_opt_arg_t *
13060opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13062 rb_node_opt_arg_t *opts = opt_list;
13063 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13065 while (opts->nd_next) {
13066 opts = opts->nd_next;
13067 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13069 opts->nd_next = opt;
13074static rb_node_kw_arg_t *
13075kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13078 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13079 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13085new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13087 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13088 p->ctxt.has_trailing_semicolon = 0;
13092 if (nd_type_p(n, NODE_BEGIN)) {
13093 n = RNODE_BEGIN(n)->nd_body;
13095 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13096 n = RNODE_BLOCK(n)->nd_head;
13103 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13104 NODE *block = NEW_BLOCK(expr, loc);
13105 return NEW_DEFINED(block, loc, keyword_loc);
13108 return NEW_DEFINED(n, loc, keyword_loc);
13112str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13115 rb_parser_string_t *str = RNODE_STR(node)->string;
13116 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13117 yyerror1(loc, "invalid symbol");
13121 lit = rb_str_new_parser_string(str);
13123 return NEW_SYM(lit, loc);
13127symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13129 enum node_type type = nd_type(symbol);
13132 nd_set_type(symbol, NODE_DSYM);
13135 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13138 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13140 return list_append(p, symbols, symbol);
13144dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13146 if (dreg->string) {
13147 reg_fragment_setenc(p, dreg->string, options);
13149 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13150 NODE *frag = list->nd_head;
13151 if (nd_type_p(frag, NODE_STR)) {
13152 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13154 else if (nd_type_p(frag, NODE_DSTR)) {
13155 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13161new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13164 /* Check string is valid regex */
13165 rb_parser_string_t *str = STRING_NEW0();
13166 reg_compile(p, str, options);
13167 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13170 switch (nd_type(node)) {
13173 /* Check string is valid regex */
13174 reg_compile(p, RNODE_STR(node)->string, options);
13175 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13179 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13182 nd_set_type(node, NODE_DREGX);
13183 nd_set_loc(node, loc);
13184 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13185 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13186 if (dreg->nd_next) {
13187 dregex_fragment_setenc(p, dreg, options);
13189 if (options & RE_OPTION_ONCE) {
13190 node = NEW_ONCE(node, loc);
13197static rb_node_kw_arg_t *
13198new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13201 return NEW_KW_ARG((k), loc);
13205new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13208 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13211 switch (nd_type(node)) {
13213 nd_set_type(node, NODE_XSTR);
13214 nd_set_loc(node, loc);
13217 nd_set_type(node, NODE_DXSTR);
13218 nd_set_loc(node, loc);
13221 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13228struct st_hash_type literal_type = {
13233static int nd_type_st_key_enable_p(NODE *node);
13236check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13238 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13239 if (!arg || !p->case_labels) return;
13240 if (!nd_type_st_key_enable_p(arg)) return;
13242 if (p->case_labels == CHECK_LITERAL_WHEN) {
13243 p->case_labels = st_init_table(&literal_type);
13247 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13248 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13249 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13253 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13258id_is_var(struct parser_params *p, ID id)
13260 if (is_notop_id(id)) {
13261 switch (id & ID_SCOPE_MASK) {
13262 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13265 if (dyna_in_block(p)) {
13266 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13268 if (local_id(p, id)) return 1;
13269 /* method call without arguments */
13273 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13278static inline enum lex_state_e
13279parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13282 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13284 return p->lex.state = ls;
13289flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13291 VALUE mesg = p->debug_buffer;
13293 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13294 p->debug_buffer = Qnil;
13295 rb_io_puts(1, &mesg, out);
13297 if (!NIL_P(str) && RSTRING_LEN(str)) {
13298 rb_io_write(p->debug_output, str);
13302static const char rb_parser_lex_state_names[][8] = {
13303 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13304 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13305 "LABEL", "LABELED","FITEM",
13309append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13312 unsigned int mask = 1;
13313 static const char none[] = "NONE";
13315 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13316 if ((unsigned)state & mask) {
13318 rb_str_cat(buf, "|", 1);
13321 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13325 rb_str_cat(buf, none, sizeof(none)-1);
13331rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13332 enum lex_state_e to, int line)
13335 mesg = rb_str_new_cstr("lex_state: ");
13336 append_lex_state_name(p, from, mesg);
13337 rb_str_cat_cstr(mesg, " -> ");
13338 append_lex_state_name(p, to, mesg);
13339 rb_str_catf(mesg, " at line %d\n", line);
13340 flush_debug_buffer(p, p->debug_output, mesg);
13345rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13347 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13351append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13354 rb_str_cat_cstr(mesg, "0");
13357 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13358 for (; mask && !(stack & mask); mask >>= 1) continue;
13359 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13364rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13365 const char *name, int line)
13367 VALUE mesg = rb_sprintf("%s: ", name);
13368 append_bitstack_value(p, stack, mesg);
13369 rb_str_catf(mesg, " at line %d\n", line);
13370 flush_debug_buffer(p, p->debug_output, mesg);
13374rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13377 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13380 rb_str_vcatf(mesg, fmt, ap);
13382 yyerror0(RSTRING_PTR(mesg));
13385 mesg = rb_str_new(0, 0);
13386 append_lex_state_name(p, p->lex.state, mesg);
13387 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13388 rb_str_resize(mesg, 0);
13389 append_bitstack_value(p, p->cond_stack, mesg);
13390 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13391 rb_str_resize(mesg, 0);
13392 append_bitstack_value(p, p->cmdarg_stack, mesg);
13393 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13394 if (p->debug_output == rb_ractor_stdout())
13395 p->debug_output = rb_ractor_stderr();
13400rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13402 yylloc->beg_pos.lineno = sourceline;
13403 yylloc->beg_pos.column = beg_pos;
13404 yylloc->end_pos.lineno = sourceline;
13405 yylloc->end_pos.column = end_pos;
13410rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13412 int sourceline = here->sourceline;
13413 int beg_pos = (int)here->offset - here->quote
13414 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13415 int end_pos = (int)here->offset + here->length + here->quote;
13417 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13421rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13423 yylloc->beg_pos.lineno = p->delayed.beg_line;
13424 yylloc->beg_pos.column = p->delayed.beg_col;
13425 yylloc->end_pos.lineno = p->delayed.end_line;
13426 yylloc->end_pos.column = p->delayed.end_col;
13432rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13434 int sourceline = p->ruby_sourceline;
13435 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13436 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13437 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13441rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13443 yylloc->end_pos = yylloc->beg_pos;
13449rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13451 int sourceline = p->ruby_sourceline;
13452 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13453 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13454 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13458rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13460 int sourceline = p->ruby_sourceline;
13461 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13462 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13463 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13465#endif /* !RIPPER */
13468assignable0(struct parser_params *p, ID id, const char **err)
13470 if (!id) return -1;
13473 *err = "Can't change the value of self";
13476 *err = "Can't assign to nil";
13479 *err = "Can't assign to true";
13481 case keyword_false:
13482 *err = "Can't assign to false";
13484 case keyword__FILE__:
13485 *err = "Can't assign to __FILE__";
13487 case keyword__LINE__:
13488 *err = "Can't assign to __LINE__";
13490 case keyword__ENCODING__:
13491 *err = "Can't assign to __ENCODING__";
13494 switch (id_type(id)) {
13496 if (dyna_in_block(p)) {
13497 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13498 compile_error(p, "Can't assign to numbered parameter _%d",
13499 NUMPARAM_ID_TO_IDX(id));
13502 if (dvar_curr(p, id)) return NODE_DASGN;
13503 if (dvar_defined(p, id)) return NODE_DASGN;
13504 if (local_id(p, id)) return NODE_LASGN;
13509 if (!local_id(p, id)) local_var(p, id);
13513 case ID_GLOBAL: return NODE_GASGN;
13514 case ID_INSTANCE: return NODE_IASGN;
13516 if (!p->ctxt.in_def) return NODE_CDECL;
13517 *err = "dynamic constant assignment";
13519 case ID_CLASS: return NODE_CVASGN;
13521 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13527assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13529 const char *err = 0;
13530 int node_type = assignable0(p, id, &err);
13531 switch (node_type) {
13532 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13533 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13534 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13535 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13536 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13537 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13541 if (err) yyerror1(loc, err);
13543 if (err) set_value(assign_error(p, err, p->s_lvalue));
13545 return NEW_ERROR(loc);
13549is_private_local_id(struct parser_params *p, ID name)
13552 if (name == idUScore) return 1;
13553 if (!is_local_id(name)) return 0;
13554 s = rb_id2str(name);
13556 return RSTRING_PTR(s)[0] == '_';
13560shadowing_lvar_0(struct parser_params *p, ID name)
13562 if (dyna_in_block(p)) {
13563 if (dvar_curr(p, name)) {
13564 if (is_private_local_id(p, name)) return 1;
13565 yyerror0("duplicated argument name");
13567 else if (dvar_defined(p, name) || local_id(p, name)) {
13568 vtable_add(p->lvtbl->vars, name);
13569 if (p->lvtbl->used) {
13570 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13576 if (local_id(p, name)) {
13577 if (is_private_local_id(p, name)) return 1;
13578 yyerror0("duplicated argument name");
13585shadowing_lvar(struct parser_params *p, ID name)
13587 shadowing_lvar_0(p, name);
13592new_bv(struct parser_params *p, ID name)
13595 if (!is_local_id(name)) {
13596 compile_error(p, "invalid local variable - %"PRIsVALUE,
13600 if (!shadowing_lvar_0(p, name)) return;
13603 if (dvar_defined_ref(p, name, &vidp)) {
13604 if (vidp) *vidp |= LVAR_USED;
13609aryset_check(struct parser_params *p, NODE *args)
13611 NODE *block = 0, *kwds = 0;
13612 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13613 block = RNODE_BLOCK_PASS(args)->nd_body;
13614 args = RNODE_BLOCK_PASS(args)->nd_head;
13616 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13617 args = RNODE_ARGSCAT(args)->nd_body;
13619 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13620 kwds = RNODE_ARGSPUSH(args)->nd_body;
13623 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13624 next = RNODE_LIST(next)->nd_next) {
13625 kwds = RNODE_LIST(next)->nd_head;
13628 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13629 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13632 yyerror1(&block->nd_loc, "block arg given in index assignment");
13637aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13639 aryset_check(p, idx);
13640 return NEW_ATTRASGN(recv, tASET, idx, loc);
13644block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13646 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13647 compile_error(p, "both block arg and actual block given");
13652attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13654 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13655 return NEW_ATTRASGN(recv, id, 0, loc);
13659rb_backref_error(struct parser_params *p, NODE *node)
13662# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13664# define ERR(...) rb_sprintf(__VA_ARGS__)
13666 switch (nd_type(node)) {
13668 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13669 case NODE_BACK_REF:
13670 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13673 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13677arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13679 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13680 switch (nd_type(node1)) {
13682 return list_append(p, node1, node2);
13683 case NODE_BLOCK_PASS:
13684 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13685 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13687 case NODE_ARGSPUSH:
13688 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13689 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13690 nd_set_type(node1, NODE_ARGSCAT);
13693 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13694 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13695 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13698 return NEW_ARGSPUSH(node1, node2, loc);
13702arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13704 if (!node2) return node1;
13705 switch (nd_type(node1)) {
13706 case NODE_BLOCK_PASS:
13707 if (RNODE_BLOCK_PASS(node1)->nd_head)
13708 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13710 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13712 case NODE_ARGSPUSH:
13713 if (!nd_type_p(node2, NODE_LIST)) break;
13714 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13715 nd_set_type(node1, NODE_ARGSCAT);
13718 if (!nd_type_p(node2, NODE_LIST) ||
13719 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13720 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13723 return NEW_ARGSCAT(node1, node2, loc);
13727last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13730 if ((n1 = splat_array(args)) != 0) {
13731 return list_append(p, n1, last_arg);
13733 return arg_append(p, args, last_arg, loc);
13737rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13740 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13741 return list_concat(n1, rest_arg);
13743 return arg_concat(p, args, rest_arg, loc);
13747splat_array(NODE* node)
13749 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13750 if (nd_type_p(node, NODE_LIST)) return node;
13755mark_lvar_used(struct parser_params *p, NODE *rhs)
13759 switch (nd_type(rhs)) {
13761 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13762 if (vidp) *vidp |= LVAR_USED;
13766 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13767 if (vidp) *vidp |= LVAR_USED;
13772 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13773 mark_lvar_used(p, rhs->nd_head);
13780static int is_static_content(NODE *node);
13783node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13785 if (!lhs) return 0;
13787 switch (nd_type(lhs)) {
13795 set_nd_value(p, lhs, rhs);
13796 nd_set_loc(lhs, loc);
13799 case NODE_ATTRASGN:
13800 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13801 nd_set_loc(lhs, loc);
13805 /* should not happen */
13813value_expr_check(struct parser_params *p, NODE *node)
13815 NODE *void_node = 0, *vn;
13818 rb_warning0("empty expression");
13821 switch (nd_type(node)) {
13823 vn = RNODE_ENSURE(node)->nd_head;
13824 node = RNODE_ENSURE(node)->nd_ensr;
13825 /* nd_ensr should not be NULL, check it out next */
13826 if (vn && (vn = value_expr_check(p, vn))) {
13832 /* void only if all children are void */
13833 vn = RNODE_RESCUE(node)->nd_head;
13834 if (!vn || !(vn = value_expr_check(p, vn))) {
13835 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13837 if (!void_node) void_node = vn;
13838 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13839 if (!nd_type_p(r, NODE_RESBODY)) {
13840 compile_error(p, "unexpected node");
13843 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13846 if (!void_node) void_node = vn;
13848 node = RNODE_RESCUE(node)->nd_else;
13849 if (!node) return void_node;
13861 for (node = RNODE_CASE(node)->nd_body;
13862 node && nd_type_p(node, NODE_WHEN);
13863 node = RNODE_WHEN(node)->nd_next) {
13864 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13867 if (!void_node) void_node = vn;
13873 NODE *in = RNODE_CASE3(node)->nd_body;
13874 if (!in || !nd_type_p(in, NODE_IN)) {
13875 compile_error(p, "unexpected node");
13878 if (!RNODE_IN(in)->nd_body) {
13879 /* single line pattern matching with "=>" operator */
13883 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13884 if (!vn) return NULL;
13885 if (!void_node) void_node = vn;
13886 in = RNODE_IN(in)->nd_next;
13887 } while (in && nd_type_p(in, NODE_IN));
13888 node = in; /* else */
13893 while (RNODE_BLOCK(node)->nd_next) {
13894 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13896 node = RNODE_BLOCK(node)->nd_next;
13898 node = RNODE_BLOCK(node)->nd_head;
13902 node = RNODE_BEGIN(node)->nd_body;
13907 if (!RNODE_IF(node)->nd_body) {
13910 else if (!RNODE_IF(node)->nd_else) {
13913 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13914 if (!vn) return NULL;
13915 if (!void_node) void_node = vn;
13916 node = RNODE_IF(node)->nd_else;
13921 node = RNODE_AND(node)->nd_1st;
13927 mark_lvar_used(p, node);
13938 /* return the first found node */
13939 return void_node ? void_node : node;
13943value_expr(struct parser_params *p, NODE *node)
13945 NODE *void_node = value_expr_check(p, node);
13947 yyerror1(&void_node->nd_loc, "void value expression");
13948 /* or "control never reach"? */
13955void_expr(struct parser_params *p, NODE *node)
13957 const char *useless = 0;
13959 if (!RTEST(ruby_verbose)) return;
13961 if (!node || !(node = nd_once_body(node))) return;
13962 switch (nd_type(node)) {
13964 switch (RNODE_OPCALL(node)->nd_mid) {
13983 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13994 case NODE_BACK_REF:
13995 useless = "a variable";
13998 useless = "a constant";
14003 case NODE_ENCODING:
14006 case NODE_RATIONAL:
14007 case NODE_IMAGINARY:
14012 useless = "a literal";
14037 useless = "defined?";
14042 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14046/* warns useless use of block and returns the last statement node */
14048void_stmts(struct parser_params *p, NODE *node)
14050 NODE *const n = node;
14051 if (!RTEST(ruby_verbose)) return n;
14052 if (!node) return n;
14053 if (!nd_type_p(node, NODE_BLOCK)) return n;
14055 while (RNODE_BLOCK(node)->nd_next) {
14056 void_expr(p, RNODE_BLOCK(node)->nd_head);
14057 node = RNODE_BLOCK(node)->nd_next;
14059 return RNODE_BLOCK(node)->nd_head;
14063remove_begin(NODE *node)
14065 NODE **n = &node, *n1 = node;
14066 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14067 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14073reduce_nodes(struct parser_params *p, NODE **body)
14075 NODE *node = *body;
14078 *body = NEW_NIL(&NULL_LOC);
14081#define subnodes(type, n1, n2) \
14082 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14083 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14084 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14087 int newline = (int)nd_fl_newline(node);
14088 switch (nd_type(node)) {
14094 *body = node = RNODE_BEGIN(node)->nd_body;
14095 if (newline && node) nd_set_fl_newline(node);
14098 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14102 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14105 body = &RNODE_CASE(node)->nd_body;
14108 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14111 body = &RNODE_ENSURE(node)->nd_head;
14114 newline = 0; // RESBODY should not be a NEWLINE
14115 if (RNODE_RESCUE(node)->nd_else) {
14116 body = &RNODE_RESCUE(node)->nd_resq;
14119 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14125 if (newline && node) nd_set_fl_newline(node);
14132is_static_content(NODE *node)
14134 if (!node) return 1;
14135 switch (nd_type(node)) {
14137 if (!(node = RNODE_HASH(node)->nd_head)) break;
14140 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14141 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14146 case NODE_ENCODING:
14149 case NODE_RATIONAL:
14150 case NODE_IMAGINARY:
14164assign_in_cond(struct parser_params *p, NODE *node)
14166 switch (nd_type(node)) {
14180 if (!get_nd_value(p, node)) return 1;
14181 if (is_static_content(get_nd_value(p, node))) {
14182 /* reports always */
14183 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14194#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14196 case COND_IN_OP: break; \
14197 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14198 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14202static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14205range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14207 enum node_type type;
14209 if (node == 0) return 0;
14211 type = nd_type(node);
14212 value_expr(p, node);
14213 if (type == NODE_INTEGER) {
14214 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14215 ID lineno = rb_intern("$.");
14216 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14218 return cond0(p, node, COND_IN_FF, loc, true);
14222cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14224 if (node == 0) return 0;
14225 if (!(node = nd_once_body(node))) return 0;
14226 assign_in_cond(p, node);
14228 switch (nd_type(node)) {
14230 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14237 SWITCH_BY_COND_TYPE(type, warn, "string ");
14241 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14242 nd_set_type(node, NODE_MATCH);
14246 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14248 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14252 NODE *end = RNODE_BLOCK(node)->nd_end;
14253 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14254 if (top) top = node == end;
14255 *expr = cond0(p, *expr, type, loc, top);
14261 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14262 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14268 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14269 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14270 switch (nd_type(node)) {
14272 nd_set_type(node,NODE_FLIP2);
14273 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14277 nd_set_type(node, NODE_FLIP3);
14278 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14286 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14290 case NODE_ENCODING:
14293 case NODE_RATIONAL:
14294 case NODE_IMAGINARY:
14295 SWITCH_BY_COND_TYPE(type, warning, "");
14305cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14307 if (node == 0) return 0;
14308 return cond0(p, node, COND_IN_COND, loc, true);
14312method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14314 if (node == 0) return 0;
14315 return cond0(p, node, COND_IN_OP, loc, true);
14319new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14321 YYLTYPE loc = {*pos, *pos};
14322 return NEW_NIL(&loc);
14326new_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 if (!cc) return right;
14329 cc = cond0(p, cc, COND_IN_COND, loc, true);
14330 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14334new_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 if (!cc) return right;
14337 cc = cond0(p, cc, COND_IN_COND, loc, true);
14338 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14341#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))
14344logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14345 const YYLTYPE *op_loc, const YYLTYPE *loc)
14347 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14349 value_expr(p, left);
14350 if (left && nd_type_p(left, type)) {
14351 NODE *node = left, *second;
14352 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14355 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14356 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14357 left->nd_loc.end_pos = loc->end_pos;
14360 op = NEW_AND_OR(type, left, right, loc, op_loc);
14361 nd_set_line(op, op_loc->beg_pos.lineno);
14368no_blockarg(struct parser_params *p, NODE *node)
14370 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14371 compile_error(p, "block argument should not be given");
14376ret_args(struct parser_params *p, NODE *node)
14379 no_blockarg(p, node);
14380 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14381 node = RNODE_LIST(node)->nd_head;
14388negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14390 switch (nd_type(node)) {
14392 RNODE_INTEGER(node)->minus = TRUE;
14395 RNODE_FLOAT(node)->minus = TRUE;
14397 case NODE_RATIONAL:
14398 RNODE_RATIONAL(node)->minus = TRUE;
14400 case NODE_IMAGINARY:
14401 RNODE_IMAGINARY(node)->minus = TRUE;
14404 node->nd_loc = *loc;
14409arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14412 if (!node1) return (NODE *)node2;
14413 node2->nd_head = node1;
14414 nd_set_first_lineno(node2, nd_first_lineno(node1));
14415 nd_set_first_column(node2, nd_first_column(node1));
14416 return (NODE *)node2;
14422args_info_empty_p(struct rb_args_info *args)
14424 if (args->pre_args_num) return false;
14425 if (args->post_args_num) return false;
14426 if (args->rest_arg) return false;
14427 if (args->opt_args) return false;
14428 if (args->block_arg) return false;
14429 if (args->kw_args) return false;
14430 if (args->kw_rest_arg) return false;
14434static rb_node_args_t *
14435new_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 struct rb_args_info *args = &tail->nd_ainfo;
14439 if (args->forwarding) {
14441 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14444 rest_arg = idFWD_REST;
14447 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14448 args->pre_init = pre_args ? pre_args->nd_next : 0;
14450 args->post_args_num = post_args ? post_args->nd_plen : 0;
14451 args->post_init = post_args ? post_args->nd_next : 0;
14452 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14454 args->rest_arg = rest_arg;
14456 args->opt_args = opt_args;
14458 nd_set_loc(RNODE(tail), loc);
14463static rb_node_args_t *
14464new_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 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14467 struct rb_args_info *args = &node->nd_ainfo;
14468 if (p->error_p) return node;
14470 if (block == idNil) {
14472 args->no_blockarg = TRUE;
14474 args->block_arg = block;
14475 args->kw_args = kw_args;
14479 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14480 * variable order: k1, kr1, k2, &b, internal_id, krest
14482 * variable order: kr1, k1, k2, internal_id, krest, &b
14484 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14485 struct vtable *vtargs = p->lvtbl->args;
14486 rb_node_kw_arg_t *kwn = kw_args;
14488 if (block) block = vtargs->tbl[vtargs->pos-1];
14489 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14490 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14492 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14494 --required_kw_vars;
14495 kwn = kwn->nd_next;
14498 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14499 ID vid = get_nd_vid(p, kwn->nd_body);
14500 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14501 *required_kw_vars++ = vid;
14508 arg_var(p, kw_bits);
14509 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14510 if (block) arg_var(p, block);
14512 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14514 else if (kw_rest_arg == idNil) {
14515 args->no_kwarg = 1;
14517 else if (kw_rest_arg) {
14518 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14524static rb_node_args_t *
14525args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14527 if (max_numparam > NO_PARAM || it_id) {
14529 YYLTYPE loc = RUBY_INIT_YYLLOC();
14530 args = new_empty_args_tail(p, 0);
14531 nd_set_loc(RNODE(args), &loc);
14533 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14539new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14541 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14544 NODE *pre_args = NEW_LIST(pre_arg, loc);
14545 if (RNODE_ARYPTN(aryptn)->pre_args) {
14546 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14549 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14556new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14559 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14564 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14570new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14572 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14578new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14580 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14581 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14582 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14588new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14590 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14595new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14597 NODE *node, *kw_rest_arg_node;
14599 if (kw_rest_arg == idNil) {
14600 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14602 else if (kw_rest_arg) {
14603 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14606 kw_rest_arg_node = NULL;
14609 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14615dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14618 return NEW_SYM(STR_NEW0(), loc);
14621 switch (nd_type(node)) {
14623 nd_set_type(node, NODE_DSYM);
14624 nd_set_loc(node, loc);
14627 node = str_to_sym_node(p, node, loc);
14630 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14637nd_type_st_key_enable_p(NODE *node)
14639 switch (nd_type(node)) {
14642 case NODE_RATIONAL:
14643 case NODE_IMAGINARY:
14649 case NODE_ENCODING:
14657nd_value(struct parser_params *p, NODE *node)
14659 switch (nd_type(node)) {
14661 return rb_node_str_string_val(node);
14663 return rb_node_integer_literal_val(node);
14665 return rb_node_float_literal_val(node);
14666 case NODE_RATIONAL:
14667 return rb_node_rational_literal_val(node);
14668 case NODE_IMAGINARY:
14669 return rb_node_imaginary_literal_val(node);
14671 return rb_node_sym_string_val(node);
14673 return rb_node_regx_string_val(node);
14675 return rb_node_line_lineno_val(node);
14676 case NODE_ENCODING:
14677 return rb_node_encoding_val(node);
14679 return rb_node_file_path_val(node);
14681 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14682 UNREACHABLE_RETURN(0);
14687warn_duplicate_keys(struct parser_params *p, NODE *hash)
14689 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14690 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14691 while (hash && RNODE_LIST(hash)->nd_next) {
14692 NODE *head = RNODE_LIST(hash)->nd_head;
14693 NODE *value = RNODE_LIST(hash)->nd_next;
14694 NODE *next = RNODE_LIST(value)->nd_next;
14698 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14703 if (nd_type_st_key_enable_p(head)) {
14704 key = (st_data_t)head;
14706 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14707 rb_warn2L(nd_line((NODE *)data),
14708 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14709 nd_value(p, head), WARN_I(nd_line(head)));
14711 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14715 st_free_table(p->warn_duplicate_keys_table);
14716 p->warn_duplicate_keys_table = NULL;
14720new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14722 if (hash) warn_duplicate_keys(p, hash);
14723 return NEW_HASH(hash, loc);
14727error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14729 if (is_private_local_id(p, id)) {
14732 if (st_is_member(p->pvtbl, id)) {
14733 yyerror1(loc, "duplicated variable name");
14735 else if (p->ctxt.in_alt_pattern && id) {
14736 yyerror1(loc, "variable capture in alternative pattern");
14739 p->ctxt.capture_in_pattern = 1;
14740 st_insert(p->pvtbl, (st_data_t)id, 0);
14745error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14748 p->pktbl = st_init_numtable();
14750 else if (st_is_member(p->pktbl, key)) {
14751 yyerror1(loc, "duplicated key name");
14754 st_insert(p->pktbl, (st_data_t)key, 0);
14758new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14760 return NEW_HASH(hash, loc);
14764new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14769 ID vid = get_nd_vid(p, lhs);
14770 YYLTYPE lhs_loc = lhs->nd_loc;
14772 set_nd_value(p, lhs, rhs);
14773 nd_set_loc(lhs, loc);
14774 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14776 else if (op == tANDOP) {
14777 set_nd_value(p, lhs, rhs);
14778 nd_set_loc(lhs, loc);
14779 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14783 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14784 set_nd_value(p, asgn, rhs);
14785 nd_set_loc(asgn, loc);
14789 asgn = NEW_ERROR(loc);
14795new_ary_op_assign(struct parser_params *p, NODE *ary,
14796 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14797 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14801 aryset_check(p, args);
14802 args = make_list(args, args_loc);
14803 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14809new_attr_op_assign(struct parser_params *p, NODE *lhs,
14810 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14811 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14815 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14821new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14826 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14829 asgn = NEW_ERROR(loc);
14836const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14838 if (p->ctxt.in_def) {
14840 yyerror1(loc, "dynamic constant assignment");
14842 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14845 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14850assign_error(struct parser_params *p, const char *mesg, VALUE a)
14852 a = dispatch2(assign_error, ERR_MESG(), a);
14859new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14861 NODE *result = head;
14863 NODE *tmp = rescue_else ? rescue_else : rescue;
14864 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14866 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14867 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14870 result = NEW_ENSURE(result, ensure, loc);
14872 fixpos(result, head);
14877warn_unused_var(struct parser_params *p, struct local_vars *local)
14881 if (!local->used) return;
14882 cnt = local->used->pos;
14883 if (cnt != local->vars->pos) {
14884 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14887 ID *v = local->vars->tbl;
14888 ID *u = local->used->tbl;
14889 for (int i = 0; i < cnt; ++i) {
14890 if (!v[i] || (u[i] & LVAR_USED)) continue;
14891 if (is_private_local_id(p, v[i])) continue;
14892 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14898local_push(struct parser_params *p, int toplevel_scope)
14900 struct local_vars *local;
14901 int inherits_dvars = toplevel_scope && compile_for_eval;
14902 int warn_unused_vars = RTEST(ruby_verbose);
14904 local = ALLOC(struct local_vars);
14905 local->prev = p->lvtbl;
14906 local->args = vtable_alloc(0);
14907 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14909 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14910 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14912 local->numparam.outer = 0;
14913 local->numparam.inner = 0;
14914 local->numparam.current = 0;
14916 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14918# if WARN_PAST_SCOPE
14927vtable_chain_free(struct parser_params *p, struct vtable *table)
14929 while (!DVARS_TERMINAL_P(table)) {
14930 struct vtable *cur_table = table;
14931 table = cur_table->prev;
14932 vtable_free(cur_table);
14937local_free(struct parser_params *p, struct local_vars *local)
14939 vtable_chain_free(p, local->used);
14941# if WARN_PAST_SCOPE
14942 vtable_chain_free(p, local->past);
14945 vtable_chain_free(p, local->args);
14946 vtable_chain_free(p, local->vars);
14948 ruby_xfree_sized(local, sizeof(struct local_vars));
14952local_pop(struct parser_params *p)
14954 struct local_vars *local = p->lvtbl->prev;
14955 if (p->lvtbl->used) {
14956 warn_unused_var(p, p->lvtbl);
14959 local_free(p, p->lvtbl);
14966static rb_ast_id_table_t *
14967local_tbl(struct parser_params *p)
14969 int cnt_args = vtable_size(p->lvtbl->args);
14970 int cnt_vars = vtable_size(p->lvtbl->vars);
14971 int cnt = cnt_args + cnt_vars;
14973 rb_ast_id_table_t *tbl;
14975 if (cnt <= 0) return 0;
14976 tbl = rb_ast_new_local_table(p->ast, cnt);
14977 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14978 /* remove IDs duplicated to warn shadowing */
14979 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14980 ID id = p->lvtbl->vars->tbl[i];
14981 if (!vtable_included(p->lvtbl->args, id)) {
14982 tbl->ids[j++] = id;
14986 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14993numparam_name(struct parser_params *p, ID id)
14995 if (!NUMPARAM_ID_P(id)) return;
14996 compile_error(p, "_%d is reserved for numbered parameter",
14997 NUMPARAM_ID_TO_IDX(id));
15001arg_var(struct parser_params *p, ID id)
15003 numparam_name(p, id);
15004 vtable_add(p->lvtbl->args, id);
15008local_var(struct parser_params *p, ID id)
15010 numparam_name(p, id);
15011 vtable_add(p->lvtbl->vars, id);
15012 if (p->lvtbl->used) {
15013 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15019rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15021 return rb_local_defined(id, iseq);
15026local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15028 struct vtable *vars, *args, *used;
15030 vars = p->lvtbl->vars;
15031 args = p->lvtbl->args;
15032 used = p->lvtbl->used;
15034 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15037 if (used) used = used->prev;
15040 if (vars && vars->prev == DVARS_INHERIT) {
15041 return rb_parser_local_defined(p, id, p->parent_iseq);
15043 else if (vtable_included(args, id)) {
15047 int i = vtable_included(vars, id);
15048 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15054local_id(struct parser_params *p, ID id)
15056 return local_id_ref(p, id, NULL);
15060check_forwarding_args(struct parser_params *p)
15062 if (local_id(p, idFWD_ALL)) return TRUE;
15063 compile_error(p, "unexpected ...");
15068add_forwarding_args(struct parser_params *p)
15070 arg_var(p, idFWD_REST);
15071 arg_var(p, idFWD_KWREST);
15072 arg_var(p, idFWD_BLOCK);
15073 arg_var(p, idFWD_ALL);
15077forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15079 bool conflict = false;
15081 struct vtable *vars, *args;
15083 vars = p->lvtbl->vars;
15084 args = p->lvtbl->args;
15086 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15087 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15092 bool found = false;
15093 if (vars && vars->prev == DVARS_INHERIT && !found) {
15094 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15095 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15098 found = (vtable_included(args, arg) &&
15099 !(all && vtable_included(args, all)));
15103 compile_error(p, "no anonymous %s parameter", var);
15105 else if (conflict) {
15106 compile_error(p, "anonymous %s parameter is also used within block", var);
15111new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15113 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15114 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15115 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15116 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15117 block->forwarding = TRUE;
15118 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15119 return arg_blk_pass(args, block);
15123numparam_push(struct parser_params *p)
15125 struct local_vars *local = p->lvtbl;
15126 NODE *inner = local->numparam.inner;
15127 if (!local->numparam.outer) {
15128 local->numparam.outer = local->numparam.current;
15130 local->numparam.inner = 0;
15131 local->numparam.current = 0;
15137numparam_pop(struct parser_params *p, NODE *prev_inner)
15139 struct local_vars *local = p->lvtbl;
15141 /* prefer first one */
15142 local->numparam.inner = prev_inner;
15144 else if (local->numparam.current) {
15145 /* current and inner are exclusive */
15146 local->numparam.inner = local->numparam.current;
15148 if (p->max_numparam > NO_PARAM) {
15149 /* current and outer are exclusive */
15150 local->numparam.current = local->numparam.outer;
15151 local->numparam.outer = 0;
15154 /* no numbered parameter */
15155 local->numparam.current = 0;
15160static const struct vtable *
15161dyna_push(struct parser_params *p)
15163 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15164 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15165 if (p->lvtbl->used) {
15166 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15168 return p->lvtbl->args;
15172dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15174 struct vtable *tmp = *vtblp;
15175 *vtblp = tmp->prev;
15176# if WARN_PAST_SCOPE
15177 if (p->past_scope_enabled) {
15178 tmp->prev = p->lvtbl->past;
15179 p->lvtbl->past = tmp;
15187dyna_pop_1(struct parser_params *p)
15189 struct vtable *tmp;
15191 if ((tmp = p->lvtbl->used) != 0) {
15192 warn_unused_var(p, p->lvtbl);
15193 p->lvtbl->used = p->lvtbl->used->prev;
15196 dyna_pop_vtable(p, &p->lvtbl->args);
15197 dyna_pop_vtable(p, &p->lvtbl->vars);
15201dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15203 while (p->lvtbl->args != lvargs) {
15205 if (!p->lvtbl->args) {
15206 struct local_vars *local = p->lvtbl->prev;
15207 ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl));
15215dyna_in_block(struct parser_params *p)
15217 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15222dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15224 struct vtable *vars, *args, *used;
15227 args = p->lvtbl->args;
15228 vars = p->lvtbl->vars;
15229 used = p->lvtbl->used;
15231 while (!DVARS_TERMINAL_P(vars)) {
15232 if (vtable_included(args, id)) {
15235 if ((i = vtable_included(vars, id)) != 0) {
15236 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15241 if (!vidrefp) used = 0;
15242 if (used) used = used->prev;
15245 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15246 return rb_dvar_defined(id, p->parent_iseq);
15254dvar_defined(struct parser_params *p, ID id)
15256 return dvar_defined_ref(p, id, NULL);
15260dvar_curr(struct parser_params *p, ID id)
15262 return (vtable_included(p->lvtbl->args, id) ||
15263 vtable_included(p->lvtbl->vars, id));
15267reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15270 "regexp encoding option '%c' differs from source encoding '%s'",
15271 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15275static rb_encoding *
15276find_enc(struct parser_params* p, const char *name)
15278 int idx = rb_enc_find_index(name);
15280 rb_bug("unknown encoding name: %s", name);
15283 return rb_enc_from_index(idx);
15286static rb_encoding *
15287kcode_to_enc(struct parser_params* p, int kcode)
15292 case ENC_ASCII8BIT:
15293 enc = rb_ascii8bit_encoding();
15296 enc = find_enc(p, "EUC-JP");
15298 case ENC_Windows_31J:
15299 enc = find_enc(p, "Windows-31J");
15302 enc = rb_utf8_encoding();
15313rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15315 int c = RE_OPTION_ENCODING_IDX(options);
15321 char_to_option_kcode(c, &opt, &idx);
15322 enc = kcode_to_enc(p, idx);
15323 if (enc != rb_parser_str_get_encoding(str) &&
15324 !rb_parser_is_ascii_string(p, str)) {
15327 rb_parser_string_set_encoding(str, enc);
15329 else if (RE_OPTION_ENCODING_NONE(options)) {
15330 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15331 !rb_parser_is_ascii_string(p, str)) {
15335 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15337 else if (rb_is_usascii_enc(p->enc)) {
15338 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15348reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15350 int c = rb_reg_fragment_setenc(p, str, options);
15351 if (c) reg_fragment_enc_error(p, str, c);
15354#ifndef UNIVERSAL_PARSER
15356 struct parser_params* parser;
15359 const YYLTYPE *loc;
15360 rb_parser_assignable_func assignable;
15361} reg_named_capture_assign_t;
15364reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15365 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15367 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15368 struct parser_params* p = arg->parser;
15369 rb_encoding *enc = arg->enc;
15370 long len = name_end - name;
15371 const char *s = (const char *)name;
15373 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15377reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15379 reg_named_capture_assign_t arg;
15382 arg.enc = rb_enc_get(regexp);
15383 arg.succ_block = 0;
15385 arg.assignable = assignable;
15386 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15388 if (!arg.succ_block) return 0;
15389 return RNODE_BLOCK(arg.succ_block)->nd_next;
15395rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15397 return assignable(p, id, val, loc);
15401rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15402 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15407 if (!len) return ST_CONTINUE;
15408 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15409 return ST_CONTINUE;
15411 var = intern_cstr(s, len, enc);
15412 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15413 if (!lvar_defined(p, var)) return ST_CONTINUE;
15415 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15416 succ = *succ_block;
15417 if (!succ) succ = NEW_ERROR(loc);
15418 succ = block_append(p, succ, node);
15419 *succ_block = succ;
15420 return ST_CONTINUE;
15425parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15428 reg_fragment_setenc(p, str, options);
15429 str2 = rb_str_new_parser_string(str);
15430 return rb_parser_reg_compile(p, str2, options);
15435rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15437 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15442reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15447 err = rb_errinfo();
15448 re = parser_reg_compile(p, str, options);
15450 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15451 rb_set_errinfo(err);
15452 compile_error(p, "%"PRIsVALUE, m);
15460rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15462 p->do_print = print;
15464 p->do_chomp = chomp;
15465 p->do_split = split;
15469parser_append_options(struct parser_params *p, NODE *node)
15471 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15472 const YYLTYPE *const LOC = &default_location;
15475 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15476 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15478 node = block_append(p, node, print);
15482 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15485 ID ifs = rb_intern("$;");
15486 ID fields = rb_intern("$F");
15487 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15488 NODE *split = NEW_GASGN(fields,
15489 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15490 rb_intern("split"), args, LOC),
15492 node = block_append(p, split, node);
15495 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15496 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15497 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15500 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15509 /* just to suppress unused-function warnings */
15515internal_id(struct parser_params *p)
15517 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15519#endif /* !RIPPER */
15522parser_initialize(struct parser_params *p)
15524 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15525 p->command_start = TRUE;
15526 p->ruby_sourcefile_string = Qnil;
15527 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15528 string_buffer_init(p);
15530 p->delayed.token = NULL;
15531 p->frozen_string_literal = -1; /* not specified */
15532 rb_source_hash_init(&p->source_hash);
15534 p->error_buffer = Qfalse;
15535 p->end_expect_token_locations = NULL;
15540 p->parsing_thread = Qnil;
15542 p->s_lvalue = Qnil;
15543 p->s_value_stack = rb_ary_new();
15545 p->debug_buffer = Qnil;
15546 p->debug_output = rb_ractor_stdout();
15547 p->enc = rb_utf8_encoding();
15552#define rb_ruby_parser_mark ripper_parser_mark
15553#define rb_ruby_parser_free ripper_parser_free
15554#define rb_ruby_parser_memsize ripper_parser_memsize
15558rb_ruby_parser_mark(void *ptr)
15560 struct parser_params *p = (struct parser_params*)ptr;
15562 rb_gc_mark(p->ruby_sourcefile_string);
15564 rb_gc_mark(p->error_buffer);
15566 rb_gc_mark(p->value);
15567 rb_gc_mark(p->result);
15568 rb_gc_mark(p->parsing_thread);
15569 rb_gc_mark(p->s_value);
15570 rb_gc_mark(p->s_lvalue);
15571 rb_gc_mark(p->s_value_stack);
15573 rb_gc_mark(p->debug_buffer);
15574 rb_gc_mark(p->debug_output);
15578rb_ruby_parser_free(void *ptr)
15580 struct parser_params *p = (struct parser_params*)ptr;
15581 struct local_vars *local, *prev;
15584 rb_ast_free(p->ast);
15587 if (p->warn_duplicate_keys_table) {
15588 st_free_table(p->warn_duplicate_keys_table);
15593 rb_parser_ary_free(p, p->tokens);
15598 ruby_xfree_sized(p->tokenbuf, p->toksiz);
15601 for (local = p->lvtbl; local; local = prev) {
15602 prev = local->prev;
15603 local_free(p, local);
15607 token_info *ptinfo;
15608 while ((ptinfo = p->token_info) != 0) {
15609 p->token_info = ptinfo->next;
15613 string_buffer_free(p);
15616 st_free_table(p->pvtbl);
15619 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15620 st_free_table(p->case_labels);
15623 xfree(p->lex.strterm);
15624 p->lex.strterm = 0;
15630rb_ruby_parser_memsize(const void *ptr)
15632 struct parser_params *p = (struct parser_params*)ptr;
15633 struct local_vars *local;
15634 size_t size = sizeof(*p);
15637 for (local = p->lvtbl; local; local = local->prev) {
15638 size += sizeof(*local);
15639 if (local->vars) size += local->vars->capa * sizeof(ID);
15645#undef rb_reserved_word
15647const struct kwtable *
15648rb_reserved_word(const char *str, unsigned int len)
15650 return reserved_word(str, len);
15653#ifdef UNIVERSAL_PARSER
15655rb_ruby_parser_allocate(const rb_parser_config_t *config)
15657 /* parser_initialize expects fields to be set to 0 */
15658 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15659 p->config = config;
15664rb_ruby_parser_new(const rb_parser_config_t *config)
15666 /* parser_initialize expects fields to be set to 0 */
15667 rb_parser_t *p = rb_ruby_parser_allocate(config);
15668 parser_initialize(p);
15673rb_ruby_parser_allocate(void)
15675 /* parser_initialize expects fields to be set to 0 */
15676 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15681rb_ruby_parser_new(void)
15683 /* parser_initialize expects fields to be set to 0 */
15684 rb_parser_t *p = rb_ruby_parser_allocate();
15685 parser_initialize(p);
15691rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15693 p->error_buffer = main ? Qfalse : Qnil;
15694 p->parent_iseq = base;
15699rb_ruby_parser_set_script_lines(rb_parser_t *p)
15701 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15705rb_ruby_parser_error_tolerant(rb_parser_t *p)
15707 p->error_tolerant = 1;
15711rb_ruby_parser_keep_tokens(rb_parser_t *p)
15713 p->keep_tokens = 1;
15714 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15718rb_ruby_parser_encoding(rb_parser_t *p)
15724rb_ruby_parser_end_seen_p(rb_parser_t *p)
15726 return p->ruby__end__seen;
15730rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15735#endif /* !RIPPER */
15739rb_ruby_parser_get_yydebug(rb_parser_t *p)
15745rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15751rb_ruby_parser_error_p(rb_parser_t *p)
15757rb_ruby_parser_debug_output(rb_parser_t *p)
15759 return p->debug_output;
15763rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15765 p->debug_output = output;
15769rb_ruby_parser_parsing_thread(rb_parser_t *p)
15771 return p->parsing_thread;
15775rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15777 p->parsing_thread = parsing_thread;
15781rb_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 p->lex.gets = gets;
15784 p->lex.input = input;
15786 p->ruby_sourcefile_string = sourcefile_string;
15787 p->ruby_sourcefile = sourcefile;
15788 p->ruby_sourceline = sourceline;
15792rb_ruby_parser_result(rb_parser_t *p)
15798rb_ruby_parser_enc(rb_parser_t *p)
15804rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15806 return p->ruby_sourcefile_string;
15810rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15812 return p->ruby_sourceline;
15816rb_ruby_parser_lex_state(rb_parser_t *p)
15818 return p->lex.state;
15822rb_ruby_ripper_parse0(rb_parser_t *p)
15825 p->ast = rb_ast_new();
15826 ripper_yyparse((void*)p);
15827 rb_ast_free(p->ast);
15830 p->eval_tree_begin = 0;
15834rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15836 return dedent_string(p, string, width);
15840rb_ruby_ripper_initialized_p(rb_parser_t *p)
15842 return p->lex.input != 0;
15846rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15848 parser_initialize(p);
15852rb_ruby_ripper_column(rb_parser_t *p)
15854 return p->lex.ptok - p->lex.pbeg;
15858rb_ruby_ripper_token_len(rb_parser_t *p)
15860 return p->lex.pcur - p->lex.ptok;
15863rb_parser_string_t *
15864rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15866 return p->lex.lastline;
15870rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15872 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15875#ifdef UNIVERSAL_PARSER
15877rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15879 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15880 p->config = config;
15885struct parser_params*
15886rb_ruby_ripper_parser_allocate(void)
15888 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15894rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15897 VALUE mesg = p->debug_buffer;
15899 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15901 rb_str_vcatf(mesg, fmt, ap);
15903 if (char_at_end(p, mesg, 0) == '\n') {
15904 rb_io_write(p->debug_output, mesg);
15905 p->debug_buffer = Qnil;
15910parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15913 int lineno, column;
15916 lineno = loc->end_pos.lineno;
15917 column = loc->end_pos.column;
15920 lineno = p->ruby_sourceline;
15921 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15924 rb_io_flush(p->debug_output);
15928 rb_syntax_error_append(p->error_buffer,
15929 p->ruby_sourcefile_string,
15936count_char(const char *str, int c)
15939 while (str[n] == c) ++n;
15944 * strip enclosing double-quotes, same as the default yytnamerr except
15945 * for that single-quotes matching back-quotes do not stop stripping.
15947 * "\"`class' keyword\"" => "`class' keyword"
15950rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15952 if (*yystr == '"') {
15953 size_t yyn = 0, bquote = 0;
15954 const char *yyp = yystr;
15960 bquote = count_char(yyp+1, '\'') + 1;
15961 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15967 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15968 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15974 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15975 if (yyres) memcpy(yyres + yyn, yyp, 3);
15980 goto do_not_strip_quotes;
15984 goto do_not_strip_quotes;
15987 if (*++yyp != '\\')
15988 goto do_not_strip_quotes;
15989 /* Fall through. */
16003 do_not_strip_quotes: ;
16006 if (!yyres) return strlen(yystr);
16008 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16013#define validate(x) (void)(x)
16016ripper_dispatch0(struct parser_params *p, ID mid)
16018 return rb_funcall(p->value, mid, 0);
16022ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16025 return rb_funcall(p->value, mid, 1, a);
16029ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16033 return rb_funcall(p->value, mid, 2, a, b);
16037ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16042 return rb_funcall(p->value, mid, 3, a, b, c);
16046ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16052 return rb_funcall(p->value, mid, 4, a, b, c, d);
16056ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16063 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16067ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16076 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16080ripper_error(struct parser_params *p)
16086ripper_value(struct parser_params *p)
16088 (void)yystpcpy; /* may not used in newer bison */
16097 * c-file-style: "ruby"