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, parser_column(p->lex.ptok - p->lex.pbeg)}, \
388 {p->ruby_sourceline, parser_column(p->lex.pcur - p->lex.pbeg)}, \
391/* Saturate instead of wrapping to a column before the line. */
393parser_column(rb_len_t pos)
395#if SIZEOF_INT < SIZEOF_RB_LEN_T
396 if (pos > INT_MAX) return INT_MAX;
401#define IS_lex_state_for(x, ls) ((x) & (ls))
402#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
403#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
404#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
406# define SET_LEX_STATE(ls) \
407 parser_set_lex_state(p, ls, __LINE__)
408static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
410typedef VALUE stack_type;
412static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
414# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
415# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
416# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
417# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
418# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
420/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
421 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
422#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
423#define COND_POP() BITSTACK_POP(cond_stack)
424#define COND_P() BITSTACK_SET_P(cond_stack)
425#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
427/* A flag to identify keyword_do_block; "do" keyword after command_call.
428 Example: `foo 1, 2 do`. */
429#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
430#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
431#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
432#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
448 struct local_vars *prev;
450 NODE *outer, *inner, *current;
455typedef struct rb_locations_lambda_body_t {
459} rb_locations_lambda_body_t;
467#define DVARS_INHERIT ((void*)1)
468#define DVARS_TOPSCOPE NULL
469#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
471typedef struct token_info {
473 rb_code_position_t beg;
476 struct token_info *next;
479typedef struct end_expect_token_locations {
480 const rb_code_position_t *pos;
481 struct end_expect_token_locations *prev;
482} end_expect_token_locations_t;
484typedef struct parser_string_buffer_elem {
485 struct parser_string_buffer_elem *next;
486 long len; /* Total length of allocated buf */
487 long used; /* Current usage of buf */
488 rb_parser_string_t *buf[FLEX_ARY_LEN];
489} parser_string_buffer_elem_t;
491typedef struct parser_string_buffer {
492 parser_string_buffer_elem_t *head;
493 parser_string_buffer_elem_t *last;
494} parser_string_buffer_t;
496#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
499 Structure of Lexer Buffer:
501 lex.pbeg lex.ptok lex.pcur lex.pend
503 |------------+------------+------------|
507struct parser_params {
512 rb_strterm_t *strterm;
513 rb_parser_lex_gets_func *gets;
514 rb_parser_input_data input;
515 parser_string_buffer_t string_buffer;
516 rb_parser_string_t *lastline;
517 rb_parser_string_t *nextline;
522 enum lex_state_e state;
523 /* track the nest level of any parens "()[]{}" */
525 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
527 /* track the nest level of only braces "{}" */
530 /* Cache the tail of the and/or chain most recently built by logop(), so a
531 * left-associative chain does not rescan its right branch on every
532 * operator. head is the chain's top node, tail the node whose nd_2nd is the
533 * next insertion point. */
538 stack_type cond_stack;
539 stack_type cmdarg_stack;
544 int heredoc_line_indent;
546 struct local_vars *lvtbl;
550 int ruby_sourceline; /* current line no. */
551 const char *ruby_sourcefile; /* current source file */
552 VALUE ruby_sourcefile_string;
554 token_info *token_info;
555 st_table *case_labels;
556 rb_node_exits_t *exits;
562 rb_parser_string_t *token;
572 st_table *warn_duplicate_keys_table;
577 struct lex_context ctxt;
579 NODE *eval_tree_begin;
581 const struct rb_iseq_struct *parent_iseq;
583#ifdef UNIVERSAL_PARSER
584 const rb_parser_config_t *config;
587 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
589 unsigned int command_start:1;
590 unsigned int eofp: 1;
591 unsigned int ruby__end__seen: 1;
592 unsigned int debug: 1;
593 unsigned int has_shebang: 1;
594 unsigned int token_seen: 1;
595 unsigned int token_info_enabled: 1;
597 unsigned int past_scope_enabled: 1;
599 unsigned int error_p: 1;
600 unsigned int cr_seen: 1;
602 /* Streaming hash state of the source bytes read so far. */
603 rb_source_hash_state_t source_hash;
608 unsigned int do_print: 1;
609 unsigned int do_loop: 1;
610 unsigned int do_chomp: 1;
611 unsigned int do_split: 1;
612 unsigned int error_tolerant: 1;
613 unsigned int keep_tokens: 1;
616 rb_parser_ary_t *debug_lines;
618 * Store specific keyword locations to generate dummy end token.
619 * Refer to the tail of list element.
621 end_expect_token_locations_t *end_expect_token_locations;
624 /* Array for term tokens */
625 rb_parser_ary_t *tokens;
631 VALUE parsing_thread;
632 VALUE s_value; /* Token VALUE */
633 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
638#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
639#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
640#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
642numparam_id_p(struct parser_params *p, ID id)
644 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
645 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
646 return idx > 0 && idx <= NUMPARAM_MAX;
648static void numparam_name(struct parser_params *p, ID id);
652after_shift(struct parser_params *p)
655 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
657 rb_ary_push(p->s_value_stack, p->s_value);
662before_reduce(int len, struct parser_params *p)
664 // Initialize $$ with $1.
665 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
669after_reduce(int len, struct parser_params *p)
671 for (int i = 0; i < len; i++) {
672 VALUE tos = rb_ary_pop(p->s_value_stack);
674 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
678 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
680 rb_ary_push(p->s_value_stack, p->s_lvalue);
685after_shift_error_token(struct parser_params *p)
688 rb_parser_printf(p, "after-shift-error-token:\n");
690 rb_ary_push(p->s_value_stack, Qnil);
694after_pop_stack(int len, struct parser_params *p)
696 for (int i = 0; i < len; i++) {
697 VALUE tos = rb_ary_pop(p->s_value_stack);
699 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
705after_shift(struct parser_params *p)
710before_reduce(int len, struct parser_params *p)
715after_reduce(int len, struct parser_params *p)
720after_shift_error_token(struct parser_params *p)
725after_pop_stack(int len, struct parser_params *p)
730#define intern_cstr(n,l,en) rb_intern3(n,l,en)
732#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
734#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
735#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
736#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
737#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
738#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
739#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
743char_at_end(struct parser_params *p, VALUE str, int when_empty)
745 rb_len_t len = RSTRING_LEN(str);
746 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
751pop_pvtbl(struct parser_params *p, st_table *tbl)
753 st_free_table(p->pvtbl);
758pop_pktbl(struct parser_params *p, st_table *tbl)
760 if (p->pktbl) st_free_table(p->pktbl);
764#define STRING_BUF_DEFAULT_LEN 16
767string_buffer_init(struct parser_params *p)
769 parser_string_buffer_t *buf = &p->lex.string_buffer;
770 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
772 buf->head = buf->last = xmalloc(size);
773 buf->head->len = STRING_BUF_DEFAULT_LEN;
775 buf->head->next = NULL;
779string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
781 parser_string_buffer_t *buf = &p->lex.string_buffer;
783 if (buf->head->used >= buf->head->len) {
784 parser_string_buffer_elem_t *elem;
785 long n = buf->head->len * 2;
786 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
788 elem = xmalloc(size);
792 buf->last->next = elem;
795 buf->last->buf[buf->last->used++] = str;
799string_buffer_free(struct parser_params *p)
801 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
804 parser_string_buffer_elem_t *next_elem = elem->next;
806 for (long i = 0; i < elem->used; i++) {
807 rb_parser_string_free(p, elem->buf[i]);
816static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
819debug_end_expect_token_locations(struct parser_params *p, const char *name)
822 VALUE mesg = rb_sprintf("%s: [", name);
824 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
826 rb_str_cat_cstr(mesg, ", ");
827 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
830 rb_str_cat_cstr(mesg, "]\n");
831 flush_debug_buffer(p, p->debug_output, mesg);
836push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
838 if(!p->error_tolerant) return;
840 end_expect_token_locations_t *locations;
841 locations = ALLOC(end_expect_token_locations_t);
842 locations->pos = pos;
843 locations->prev = p->end_expect_token_locations;
844 p->end_expect_token_locations = locations;
846 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
850pop_end_expect_token_locations(struct parser_params *p)
852 if(!p->end_expect_token_locations) return;
854 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
855 ruby_xfree_sized(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
856 p->end_expect_token_locations = locations;
858 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
861static end_expect_token_locations_t *
862peek_end_expect_token_locations(struct parser_params *p)
864 return p->end_expect_token_locations;
868parser_token2char(struct parser_params *p, enum yytokentype tok)
871#define TOKEN2CHAR(tok) case tok: return (#tok);
872#define TOKEN2CHAR2(tok, name) case tok: return (name);
873 TOKEN2CHAR2(' ', "word_sep");
874 TOKEN2CHAR2('!', "!")
875 TOKEN2CHAR2('%', "%");
876 TOKEN2CHAR2('&', "&");
877 TOKEN2CHAR2('*', "*");
878 TOKEN2CHAR2('+', "+");
879 TOKEN2CHAR2('-', "-");
880 TOKEN2CHAR2('/', "/");
881 TOKEN2CHAR2('<', "<");
882 TOKEN2CHAR2('=', "=");
883 TOKEN2CHAR2('>', ">");
884 TOKEN2CHAR2('?', "?");
885 TOKEN2CHAR2('^', "^");
886 TOKEN2CHAR2('|', "|");
887 TOKEN2CHAR2('~', "~");
888 TOKEN2CHAR2(':', ":");
889 TOKEN2CHAR2(',', ",");
890 TOKEN2CHAR2('.', ".");
891 TOKEN2CHAR2(';', ";");
892 TOKEN2CHAR2('`', "`");
893 TOKEN2CHAR2('\n', "nl");
894 TOKEN2CHAR2('{', "\"{\"");
895 TOKEN2CHAR2('}', "\"}\"");
896 TOKEN2CHAR2('[', "\"[\"");
897 TOKEN2CHAR2(']', "\"]\"");
898 TOKEN2CHAR2('(', "\"(\"");
899 TOKEN2CHAR2(')', "\")\"");
900 TOKEN2CHAR2('\\', "backslash");
901 TOKEN2CHAR(keyword_class);
902 TOKEN2CHAR(keyword_module);
903 TOKEN2CHAR(keyword_def);
904 TOKEN2CHAR(keyword_undef);
905 TOKEN2CHAR(keyword_begin);
906 TOKEN2CHAR(keyword_rescue);
907 TOKEN2CHAR(keyword_ensure);
908 TOKEN2CHAR(keyword_end);
909 TOKEN2CHAR(keyword_if);
910 TOKEN2CHAR(keyword_unless);
911 TOKEN2CHAR(keyword_then);
912 TOKEN2CHAR(keyword_elsif);
913 TOKEN2CHAR(keyword_else);
914 TOKEN2CHAR(keyword_case);
915 TOKEN2CHAR(keyword_when);
916 TOKEN2CHAR(keyword_while);
917 TOKEN2CHAR(keyword_until);
918 TOKEN2CHAR(keyword_for);
919 TOKEN2CHAR(keyword_break);
920 TOKEN2CHAR(keyword_next);
921 TOKEN2CHAR(keyword_redo);
922 TOKEN2CHAR(keyword_retry);
923 TOKEN2CHAR(keyword_in);
924 TOKEN2CHAR(keyword_do);
925 TOKEN2CHAR(keyword_do_cond);
926 TOKEN2CHAR(keyword_do_block);
927 TOKEN2CHAR(keyword_do_LAMBDA);
928 TOKEN2CHAR(keyword_return);
929 TOKEN2CHAR(keyword_yield);
930 TOKEN2CHAR(keyword_super);
931 TOKEN2CHAR(keyword_self);
932 TOKEN2CHAR(keyword_nil);
933 TOKEN2CHAR(keyword_true);
934 TOKEN2CHAR(keyword_false);
935 TOKEN2CHAR(keyword_and);
936 TOKEN2CHAR(keyword_or);
937 TOKEN2CHAR(keyword_not);
938 TOKEN2CHAR(modifier_if);
939 TOKEN2CHAR(modifier_unless);
940 TOKEN2CHAR(modifier_while);
941 TOKEN2CHAR(modifier_until);
942 TOKEN2CHAR(modifier_rescue);
943 TOKEN2CHAR(keyword_alias);
944 TOKEN2CHAR(keyword_defined);
945 TOKEN2CHAR(keyword_BEGIN);
946 TOKEN2CHAR(keyword_END);
947 TOKEN2CHAR(keyword__LINE__);
948 TOKEN2CHAR(keyword__FILE__);
949 TOKEN2CHAR(keyword__ENCODING__);
950 TOKEN2CHAR(tIDENTIFIER);
954 TOKEN2CHAR(tCONSTANT);
957 TOKEN2CHAR(tINTEGER);
959 TOKEN2CHAR(tRATIONAL);
960 TOKEN2CHAR(tIMAGINARY);
962 TOKEN2CHAR(tNTH_REF);
963 TOKEN2CHAR(tBACK_REF);
964 TOKEN2CHAR(tSTRING_CONTENT);
965 TOKEN2CHAR(tREGEXP_END);
966 TOKEN2CHAR(tDUMNY_END);
992 TOKEN2CHAR(tOP_ASGN);
995 TOKEN2CHAR(tLPAREN_ARG);
998 TOKEN2CHAR(tLBRACE_ARG);
1002 TOKEN2CHAR(tLAMBDA);
1003 TOKEN2CHAR(tSYMBEG);
1004 TOKEN2CHAR(tSTRING_BEG);
1005 TOKEN2CHAR(tXSTRING_BEG);
1006 TOKEN2CHAR(tREGEXP_BEG);
1007 TOKEN2CHAR(tWORDS_BEG);
1008 TOKEN2CHAR(tQWORDS_BEG);
1009 TOKEN2CHAR(tSYMBOLS_BEG);
1010 TOKEN2CHAR(tQSYMBOLS_BEG);
1011 TOKEN2CHAR(tSTRING_END);
1012 TOKEN2CHAR(tSTRING_DEND);
1013 TOKEN2CHAR(tSTRING_DBEG);
1014 TOKEN2CHAR(tSTRING_DVAR);
1015 TOKEN2CHAR(tLAMBEG);
1016 TOKEN2CHAR(tLABEL_END);
1017 TOKEN2CHAR(tIGNORED_NL);
1018 TOKEN2CHAR(tCOMMENT);
1019 TOKEN2CHAR(tEMBDOC_BEG);
1020 TOKEN2CHAR(tEMBDOC);
1021 TOKEN2CHAR(tEMBDOC_END);
1022 TOKEN2CHAR(tHEREDOC_BEG);
1023 TOKEN2CHAR(tHEREDOC_END);
1024 TOKEN2CHAR(k__END__);
1025 TOKEN2CHAR(tLOWEST);
1026 TOKEN2CHAR(tUMINUS_NUM);
1027 TOKEN2CHAR(tLAST_TOKEN);
1032 rb_bug("parser_token2id: unknown token %d", tok);
1034 UNREACHABLE_RETURN(0);
1038push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1043pop_end_expect_token_locations(struct parser_params *p)
1048RBIMPL_ATTR_NONNULL((1, 2, 3))
1049static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1050RBIMPL_ATTR_NONNULL((1, 2))
1051static int parser_yyerror0(struct parser_params*, const char*);
1052#define yyerror0(msg) parser_yyerror0(p, (msg))
1053#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1054#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1055#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1056#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1057#define lex_eol_p(p) lex_eol_n_p(p, 0)
1058#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1059#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1060#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1062static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1063static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1064static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1065static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1066static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1069#define compile_for_eval (0)
1071#define compile_for_eval (p->parent_iseq != 0)
1074#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1076#define CALL_Q_P(q) ((q) == tANDDOT)
1077#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1079#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1081static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1084rb_discard_node(struct parser_params *p, NODE *n)
1086 rb_ast_delete_node(p->ast, n);
1089static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc);
1090static rb_node_scope_t *rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc);
1091static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1092static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc);
1093static rb_node_unless_t *rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc);
1094static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1095static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1096static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1097static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc);
1098static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc);
1099static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
1100static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
1101static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1102static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *for_keyword_loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *do_keyword_loc, const YYLTYPE *end_keyword_loc);
1103static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1104static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1105static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1106static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1107static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1108static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1109static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1110static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1111static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1112static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1113static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1114static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1115static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1116static rb_node_cdecl_t *rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc);
1117static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1118static rb_node_op_asgn1_t *rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc);
1119static rb_node_op_asgn2_t *rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc);
1120static rb_node_op_asgn_or_t *rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc);
1121static rb_node_op_asgn_and_t *rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc);
1122static rb_node_op_cdecl_t *rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc);
1123static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1124static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1125static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1126static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1127static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1128static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc);
1129static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1130static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1131static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1132static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1133static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1134static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1135static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc);
1136static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1137static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1138static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1139static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1140static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1141static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1142static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1143static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1144static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1145static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1146static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1147static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1148static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1149static rb_node_imaginary_t * rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type, const YYLTYPE *loc);
1150static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1151static rb_node_dstr_t *rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1152static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1153static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1154static rb_node_dxstr_t *rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1155static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1156static rb_node_regx_t *rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc);
1157static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1158static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1159static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1160static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1161static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1162static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1163static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1164static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1165static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1166static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1167static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1168static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1169static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1170static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1171static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1172static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc);
1173static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc);
1174static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *operator_loc, const YYLTYPE *end_keyword_loc);
1175static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
1176static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
1177static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1178static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1179static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1180static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1181static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1182static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1183static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1184static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1185static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1186static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1187static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1188static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1189static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1190static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1191static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1192static rb_node_fndptn_t *rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1193static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1194static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1195static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1197#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1198#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1199#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1200#define NEW_IF(c,t,e,loc,ik_loc,tk_loc,ek_loc) (NODE *)rb_node_if_new(p,c,t,e,loc,ik_loc,tk_loc,ek_loc)
1201#define NEW_UNLESS(c,t,e,loc,k_loc,t_loc,e_loc) (NODE *)rb_node_unless_new(p,c,t,e,loc,k_loc,t_loc,e_loc)
1202#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1203#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1204#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1205#define NEW_WHEN(c,t,e,loc,k_loc,t_loc) (NODE *)rb_node_when_new(p,c,t,e,loc,k_loc,t_loc)
1206#define NEW_IN(c,t,e,loc,ik_loc,tk_loc,o_loc) (NODE *)rb_node_in_new(p,c,t,e,loc,ik_loc,tk_loc,o_loc)
1207#define NEW_WHILE(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_while_new(p,c,b,n,loc,k_loc,c_loc)
1208#define NEW_UNTIL(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_until_new(p,c,b,n,loc,k_loc,c_loc)
1209#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1210#define NEW_FOR(i,b,loc,f_loc,i_loc,d_loc,e_loc) (NODE *)rb_node_for_new(p,i,b,loc,f_loc,i_loc,d_loc,e_loc)
1211#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1212#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1213#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1214#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1215#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1216#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1217#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1218#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1219#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1220#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1221#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1222#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1223#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1224#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1225#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1226#define NEW_OP_ASGN1(r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc) (NODE *)rb_node_op_asgn1_new(p,r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc)
1227#define NEW_OP_ASGN2(r,t,i,o,val,loc,c_op_loc,m_loc,b_op_loc) (NODE *)rb_node_op_asgn2_new(p,r,val,i,o,t,loc,c_op_loc,m_loc,b_op_loc)
1228#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1229#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1230#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1231#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1232#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1233#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1234#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1235#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1236#define NEW_SUPER(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_super_new(p,a,loc,k_loc,l_loc,r_loc)
1237#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1238#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1239#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1240#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1241#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1242#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1243#define NEW_YIELD(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_yield_new(p,a,loc,k_loc,l_loc,r_loc)
1244#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1245#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1246#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1247#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1248#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1249#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1250#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1251#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1252#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1253#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1254#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1255#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1256#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1257#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1258#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1259#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1260#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1261#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1262#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1263#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1264#define NEW_REGX(str,opts,loc,o_loc,ct_loc,c_loc) (NODE *)rb_node_regx_new(p,str,opts,loc,o_loc,ct_loc,c_loc)
1265#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1266#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1267#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1268#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1269#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1270#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1271#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1272#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1273#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1274#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1275#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1276#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1277#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1278#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1279#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1280#define NEW_CLASS(n,b,s,loc,ck_loc,io_loc,ek_loc) (NODE *)rb_node_class_new(p,n,b,s,loc,ck_loc,io_loc,ek_loc)
1281#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1282#define NEW_SCLASS(r,b,loc,ck_loc,op_loc,ek_loc) (NODE *)rb_node_sclass_new(p,r,b,loc,ck_loc,op_loc,ek_loc)
1283#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1284#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1285#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1286#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1287#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1288#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1289#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1290#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1291#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1292#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1293#define NEW_POSTEXE(b,loc,k_loc,o_loc,c_loc) (NODE *)rb_node_postexe_new(p,b,loc,k_loc,o_loc,c_loc)
1294#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1295#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1296#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1297#define NEW_LAMBDA(a,b,loc,op_loc,o_loc,c_loc) (NODE *)rb_node_lambda_new(p,a,b,loc,op_loc,o_loc,c_loc)
1298#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1299#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1300#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1301#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1302#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1303#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1304#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1306enum internal_node_type {
1307 NODE_INTERNAL_ONLY = NODE_LAST,
1314parser_node_name(int node)
1318 return "NODE_DEF_TEMP";
1320 return "NODE_EXITS";
1322 return ruby_node_name(node);
1326/* This node is parse.y internal */
1327struct RNode_DEF_TEMP {
1330 /* for NODE_DEFN/NODE_DEFS */
1332 struct RNode *nd_def;
1337 NODE *numparam_save;
1338 struct lex_context ctxt;
1342#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1344static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1345static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1346static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1347static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1348static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1350#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1351#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1352#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1353#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1355/* Make a new internal node, which should not be appeared in the
1356 * result AST and does not have node_id and location. */
1357static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1358#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1360static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1363parser_get_node_id(struct parser_params *p)
1365 int node_id = p->node_id;
1371anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1373 if (id == tANDDOT) {
1374 yyerror1(loc, "&. inside multiple assignment destination");
1379set_line_body(NODE *body, int line)
1382 switch (nd_type(body)) {
1385 nd_set_line(body, line);
1390set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1392 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1393 nd_set_line(node, beg->end_pos.lineno);
1397last_expr_node(NODE *expr)
1400 if (nd_type_p(expr, NODE_BLOCK)) {
1401 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1403 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1404 expr = RNODE_BEGIN(expr)->nd_body;
1414#define yyparse ruby_yyparse
1417static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1418static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1419static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1420static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1421static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1422static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1424static NODE *newline_node(NODE*);
1425static void fixpos(NODE*,NODE*);
1427static int value_expr(struct parser_params*,NODE*);
1428static void void_expr(struct parser_params*,NODE*);
1429static NODE *remove_begin(NODE*);
1430static NODE *void_stmts(struct parser_params*,NODE*);
1431static void reduce_nodes(struct parser_params*,NODE**);
1432static void block_dup_check(struct parser_params*,NODE*,NODE*);
1434static NODE *block_append(struct parser_params*,NODE*,NODE*);
1435static NODE *list_append(struct parser_params*,NODE*,NODE*);
1436static NODE *list_concat(NODE*,NODE*);
1437static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1438static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1439static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1440static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1441static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1442static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1443static NODE *str2dstr(struct parser_params*,NODE*);
1444static NODE *evstr2dstr(struct parser_params*,NODE*);
1445static NODE *splat_array(NODE*);
1446static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1448static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1449static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1450static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1451static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
1452static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {RNODE_ITER(b)->nd_iter = m; b->nd_loc = *loc; return b;}
1453static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1455static bool args_info_empty_p(struct rb_args_info *args);
1456static rb_node_args_t *new_args(struct parser_params*,rb_node_args_aux_t*,rb_node_opt_arg_t*,ID,rb_node_args_aux_t*,rb_node_args_t*,const YYLTYPE*);
1457static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1458#define new_empty_args_tail(p, loc) new_args_tail(p, 0, 0, 0, loc)
1459static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1460static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1461static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1462static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1463static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1464static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1466static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1467static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1469static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1470static void no_blockarg(struct parser_params*,NODE*);
1471static NODE *ret_args(struct parser_params*,NODE*);
1472static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1473static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1475static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1476static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1478static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1479static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1481static VALUE rb_backref_error(struct parser_params*,NODE*);
1482static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1484static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1485static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc);
1486static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc);
1487static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1488static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1490static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1492static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1493static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1495static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1496static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1498static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1500static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1502#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1504static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1506static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1508static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1510static rb_ast_id_table_t *local_tbl(struct parser_params*);
1512static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1513static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1515static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1516static NODE *heredoc_dedent(struct parser_params*,NODE*);
1518static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1520static rb_locations_lambda_body_t* new_locations_lambda_body(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
1523#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1524#define set_value(val) (p->s_lvalue = val)
1525static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1526static int id_is_var(struct parser_params *p, ID id);
1529RUBY_SYMBOL_EXPORT_BEGIN
1530VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1531int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1532enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1533VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1534void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1535PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1536YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1537YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1538YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1539YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1540YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1541YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1542void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1543RUBY_SYMBOL_EXPORT_END
1545static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1546static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1547static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1548static VALUE formal_argument_error(struct parser_params*, ID);
1549static ID shadowing_lvar(struct parser_params*,ID);
1550static void new_bv(struct parser_params*,ID);
1552static void local_push(struct parser_params*,int);
1553static void local_pop(struct parser_params*);
1554static void local_var(struct parser_params*, ID);
1555static void arg_var(struct parser_params*, ID);
1556static int local_id(struct parser_params *p, ID id);
1557static int local_id_ref(struct parser_params*, ID, ID **);
1558#define internal_id rb_parser_internal_id
1559ID internal_id(struct parser_params*);
1560static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1561static int check_forwarding_args(struct parser_params*);
1562static void add_forwarding_args(struct parser_params *p);
1563static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1565static const struct vtable *dyna_push(struct parser_params *);
1566static void dyna_pop(struct parser_params*, const struct vtable *);
1567static int dyna_in_block(struct parser_params*);
1568#define dyna_var(p, id) local_var(p, id)
1569static int dvar_defined(struct parser_params*, ID);
1570#define dvar_defined_ref rb_parser_dvar_defined_ref
1571int dvar_defined_ref(struct parser_params*, ID, ID**);
1572static int dvar_curr(struct parser_params*,ID);
1574static int lvar_defined(struct parser_params*, ID);
1576static NODE *numparam_push(struct parser_params *p);
1577static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1579#define METHOD_NOT '!'
1581#define idFWD_REST '*'
1582#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1583#define idFWD_BLOCK '&'
1584#define idFWD_ALL idDot3
1585#define arg_FWD_BLOCK idFWD_BLOCK
1587#define RE_ONIG_OPTION_IGNORECASE 1
1588#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1589#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1590#define RE_OPTION_ONCE (1<<16)
1591#define RE_OPTION_ENCODING_SHIFT 8
1592#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1593#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1594#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1595#define RE_OPTION_MASK 0xff
1596#define RE_OPTION_ARG_ENCODING_NONE 32
1598#define CHECK_LITERAL_WHEN (st_table *)1
1599#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1601#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1602RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1604#define TOKEN2ID(tok) ( \
1605 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1606 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1607 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1608 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1609 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1610 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1611 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1613/****** Ripper *******/
1617#include "eventids1.h"
1618#include "eventids2.h"
1620extern const struct ripper_parser_ids ripper_parser_ids;
1622static VALUE ripper_dispatch0(struct parser_params*,ID);
1623static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1624static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1625static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1626static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1627static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1628static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1629void ripper_error(struct parser_params *p);
1631#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1632#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1633#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1634#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1635#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1636#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1637#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1639#define yyparse ripper_yyparse
1642aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1644 if (!NIL_P(pre_arg)) {
1645 if (!NIL_P(pre_args)) {
1646 rb_ary_unshift(pre_args, pre_arg);
1649 pre_args = rb_ary_new_from_args(1, pre_arg);
1655#define ID2VAL(id) STATIC_ID2SYM(id)
1656#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1659#define KWD2EID(t, v) keyword_##t
1662new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1664 body = remove_begin(body);
1665 reduce_nodes(p, &body);
1666 NODE *n = NEW_SCOPE(args, body, parent, loc);
1667 nd_set_line(n, loc->end_pos.lineno);
1668 set_line_body(body, loc->beg_pos.lineno);
1673rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1674 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1676 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1677 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1678 loc.beg_pos = arg_loc->beg_pos;
1679 return NEW_RESCUE(arg, rescue, 0, &loc);
1682static NODE *add_block_exit(struct parser_params *p, NODE *node);
1683static rb_node_exits_t *init_block_exit(struct parser_params *p);
1684static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1685static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1686static void clear_block_exit(struct parser_params *p, bool error);
1689next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1691 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1695restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1697 /* See: def_name action */
1698 struct lex_context ctxt = temp->save.ctxt;
1699 p->ctxt.in_def = ctxt.in_def;
1700 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1701 p->ctxt.in_rescue = ctxt.in_rescue;
1702 p->max_numparam = temp->save.max_numparam;
1703 numparam_pop(p, temp->save.numparam_save);
1704 clear_block_exit(p, true);
1708endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1710 if (is_attrset_id(mid)) {
1711 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1713 token_info_drop(p, "def", loc->beg_pos);
1716#define debug_token_line(p, name, line) do { \
1718 const char *const pcur = p->lex.pcur; \
1719 const char *const ptok = p->lex.ptok; \
1720 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1721 line, p->ruby_sourceline, \
1722 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1726#define begin_definition(k, loc_beg, loc_end) \
1728 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1729 /* singleton class */ \
1730 p->ctxt.cant_return = !p->ctxt.in_def; \
1731 p->ctxt.in_def = 0; \
1733 else if (p->ctxt.in_def) { \
1734 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1735 yyerror1(&loc, k " definition in method body"); \
1738 p->ctxt.cant_return = 1; \
1744# define ifndef_ripper(x) (x)
1745# define ifdef_ripper(r,x) (x)
1747# define ifndef_ripper(x)
1748# define ifdef_ripper(r,x) (r)
1751# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1752# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1753# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1754# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1755# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1756# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1757# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1758# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1759# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1760# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1761# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1762# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1763# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1764# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1765# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1766# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1767# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1768# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1769# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1770# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1772extern const ID id_warn, id_warning, id_gets, id_assoc;
1773# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1774# define WARN_S_L(s,l) STR_NEW(s,l)
1775# define WARN_S(s) STR_NEW2(s)
1776# define WARN_I(i) INT2NUM(i)
1777# define WARN_ID(i) rb_id2str(i)
1778# define PRIsWARN PRIsVALUE
1779# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1780# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1781# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1782# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1783# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1784# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1785# define compile_error ripper_compile_error
1787# define WARN_S_L(s,l) s
1790# define WARN_ID(i) rb_id2name(i)
1791# define PRIsWARN PRIsVALUE
1792# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1793# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1794# define WARN_CALL rb_compile_warn
1795# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1796# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1797# define WARNING_CALL rb_compile_warning
1798PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1799# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1802#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1805add_block_exit(struct parser_params *p, NODE *node)
1808 compile_error(p, "unexpected null node");
1811 switch (nd_type(node)) {
1812 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1814 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1817 if (!p->ctxt.in_defined) {
1818 rb_node_exits_t *exits = p->exits;
1820 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1821 exits->nd_stts = node;
1827static rb_node_exits_t *
1828init_block_exit(struct parser_params *p)
1830 rb_node_exits_t *old = p->exits;
1831 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1832 exits->nd_chain = 0;
1833 exits->nd_stts = RNODE(exits);
1838static rb_node_exits_t *
1839allow_block_exit(struct parser_params *p)
1841 rb_node_exits_t *exits = p->exits;
1847restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1853clear_block_exit(struct parser_params *p, bool error)
1855 rb_node_exits_t *exits = p->exits;
1858 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1859 switch (nd_type(e)) {
1861 yyerror1(&e->nd_loc, "Invalid break");
1864 yyerror1(&e->nd_loc, "Invalid next");
1867 yyerror1(&e->nd_loc, "Invalid redo");
1870 yyerror1(&e->nd_loc, "unexpected node");
1871 goto end_checks; /* no nd_chain */
1876 exits->nd_stts = RNODE(exits);
1877 exits->nd_chain = 0;
1880#define WARN_EOL(tok) \
1881 (looking_at_eol_p(p) ? \
1882 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1884static int looking_at_eol_p(struct parser_params *p);
1887get_nd_value(struct parser_params *p, NODE *node)
1889 switch (nd_type(node)) {
1891 return RNODE_GASGN(node)->nd_value;
1893 return RNODE_IASGN(node)->nd_value;
1895 return RNODE_LASGN(node)->nd_value;
1897 return RNODE_DASGN(node)->nd_value;
1899 return RNODE_MASGN(node)->nd_value;
1901 return RNODE_CVASGN(node)->nd_value;
1903 return RNODE_CDECL(node)->nd_value;
1905 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1911set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1913 switch (nd_type(node)) {
1915 RNODE_CDECL(node)->nd_value = rhs;
1918 RNODE_GASGN(node)->nd_value = rhs;
1921 RNODE_IASGN(node)->nd_value = rhs;
1924 RNODE_LASGN(node)->nd_value = rhs;
1927 RNODE_DASGN(node)->nd_value = rhs;
1930 RNODE_MASGN(node)->nd_value = rhs;
1933 RNODE_CVASGN(node)->nd_value = rhs;
1936 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1942get_nd_vid(struct parser_params *p, NODE *node)
1944 switch (nd_type(node)) {
1946 return RNODE_CDECL(node)->nd_vid;
1948 return RNODE_GASGN(node)->nd_vid;
1950 return RNODE_IASGN(node)->nd_vid;
1952 return RNODE_LASGN(node)->nd_vid;
1954 return RNODE_DASGN(node)->nd_vid;
1956 return RNODE_CVASGN(node)->nd_vid;
1958 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1964get_nd_args(struct parser_params *p, NODE *node)
1966 switch (nd_type(node)) {
1968 return RNODE_CALL(node)->nd_args;
1970 return RNODE_OPCALL(node)->nd_args;
1972 return RNODE_FCALL(node)->nd_args;
1974 return RNODE_QCALL(node)->nd_args;
1976 return RNODE_SUPER(node)->nd_args;
1985 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1991djb2(const uint8_t *str, size_t len)
1993 st_index_t hash = 5381;
1995 for (size_t i = 0; i < len; i++) {
1996 hash = ((hash << 5) + hash) + str[i];
2003parser_memhash(const void *ptr, rb_len_t len)
2005 return djb2(ptr, len);
2008#define PARSER_STRING_PTR(str) (str->ptr)
2009#define PARSER_STRING_LEN(str) (str->len)
2010#define PARSER_STRING_END(str) (&str->ptr[str->len])
2011#define STRING_SIZE(str) ((size_t)str->len + 1)
2012#define STRING_TERM_LEN(str) (1)
2013#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
2014#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
2015 REALLOC_N(str->ptr, char, (size_t)total + termlen); \
2018#define STRING_SET_LEN(str, n) do { \
2021#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2022 ((ptrvar) = str->ptr, \
2023 (lenvar) = str->len)
2026parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2028 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2031static rb_parser_string_t *
2032rb_parser_string_new(rb_parser_t *p, const char *ptr, rb_len_t len)
2034 rb_parser_string_t *str;
2037 rb_bug("negative string size (or size too big): %"PRIdLEN, len);
2040 str = xcalloc(1, sizeof(rb_parser_string_t));
2041 str->ptr = xcalloc(len + 1, sizeof(char));
2044 memcpy(PARSER_STRING_PTR(str), ptr, len);
2046 STRING_SET_LEN(str, len);
2047 STRING_TERM_FILL(str);
2051static rb_parser_string_t *
2052rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, rb_len_t len, rb_encoding *enc)
2054 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2055 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2062rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2065 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2071rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2074 xfree(PARSER_STRING_PTR(str));
2080rb_parser_str_hash(rb_parser_string_t *str)
2082 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2086rb_char_p_hash(const char *c)
2088 return parser_memhash((const void *)c, strlen(c));
2092rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2094 return PARSER_STRING_LEN(str);
2099rb_parser_string_end(rb_parser_string_t *str)
2101 return &str->ptr[str->len];
2106rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2112rb_parser_str_get_encoding(rb_parser_string_t *str)
2119PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2121 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2126PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2128 return str->coderange;
2132PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2134 str->coderange = coderange;
2138PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2140 rb_parser_string_set_encoding(str, enc);
2141 PARSER_ENC_CODERANGE_SET(str, cr);
2145PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2147 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2151PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2153 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2157PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2159 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2163rb_parser_search_nonascii(const char *p, const char *e)
2167 for (; s < e; s++) {
2168 if (*s & 0x80) return s;
2175rb_parser_coderange_scan(struct parser_params *p, const char *ptr, rb_len_t len, rb_encoding *enc)
2177 const char *e = ptr + len;
2179 if (enc == rb_ascii8bit_encoding()) {
2180 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2181 ptr = rb_parser_search_nonascii(ptr, e);
2182 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2185 /* parser string encoding is always asciicompat */
2186 ptr = rb_parser_search_nonascii(ptr, e);
2187 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2189 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2190 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2191 ptr += MBCLEN_CHARFOUND_LEN(ret);
2192 if (ptr == e) break;
2193 ptr = rb_parser_search_nonascii(ptr, e);
2197 return RB_PARSER_ENC_CODERANGE_VALID;
2201rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2203 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2207rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2209 int cr = PARSER_ENC_CODERANGE(str);
2211 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2212 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2213 PARSER_ENC_CODERANGE_SET(str, cr);
2219static rb_parser_string_t *
2220rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2222 if (rb_parser_str_get_encoding(str) == enc)
2224 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2225 PARSER_ENC_CODERANGE_CLEAR(str);
2227 rb_parser_string_set_encoding(str, enc);
2232rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2234 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2238rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2240 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2241 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2243 if (enc1 == NULL || enc2 == NULL)
2250 if (PARSER_STRING_LEN(str2) == 0)
2252 if (PARSER_STRING_LEN(str1) == 0)
2253 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2257 cr1 = rb_parser_enc_str_coderange(p, str1);
2258 cr2 = rb_parser_enc_str_coderange(p, str2);
2261 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2262 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2265 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2269 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2277rb_parser_str_modify(rb_parser_string_t *str)
2279 PARSER_ENC_CODERANGE_CLEAR(str);
2283rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, rb_len_t len)
2286 const int termlen = STRING_TERM_LEN(str);
2288 if (len > (capa = (rb_len_t)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2289 rb_bug("probable buffer overflow: %"PRIdLEN" for %"PRIdLEN, len, capa);
2292 int cr = PARSER_ENC_CODERANGE(str);
2293 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2294 /* Leave unknown. */
2296 else if (len > PARSER_STRING_LEN(str)) {
2297 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2299 else if (len < PARSER_STRING_LEN(str)) {
2300 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2301 /* ASCII-only string is keeping after truncated. Valid
2302 * and broken may be invalid or valid, leave unknown. */
2303 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2307 STRING_SET_LEN(str, len);
2308 STRING_TERM_FILL(str);
2311static rb_parser_string_t *
2312rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len)
2314 rb_parser_str_modify(str);
2315 if (len == 0) return 0;
2317 rb_len_t total, olen, off = -1;
2319 const int termlen = STRING_TERM_LEN(str);
2321 PARSER_STRING_GETMEM(str, sptr, olen);
2322 if (ptr >= sptr && ptr <= sptr + olen) {
2326 if (olen > RB_LEN_MAX - len) {
2327 compile_error(p, "string sizes too big");
2331 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2332 sptr = PARSER_STRING_PTR(str);
2336 memcpy(sptr + olen, ptr, len);
2337 STRING_SET_LEN(str, total);
2338 STRING_TERM_FILL(str);
2343#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2344#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2346static rb_parser_string_t *
2347rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len,
2348 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2351 rb_encoding *str_enc, *res_enc;
2353 str_enc = rb_parser_str_get_encoding(str);
2354 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2356 if (str_enc == ptr_enc) {
2357 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2358 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2362 /* parser string encoding is always asciicompat */
2363 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2364 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2366 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2367 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2368 str_cr = rb_parser_enc_str_coderange(p, str);
2373 *ptr_cr_ret = ptr_cr;
2375 if (str_enc != ptr_enc &&
2376 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2377 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2381 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2383 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2385 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2386 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2388 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2395 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2397 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2402 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2405 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2409 compile_error(p, "negative string size (or size too big)");
2411 parser_str_cat(str, ptr, len);
2412 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2416 compile_error(p, "incompatible character encodings: %s and %s",
2417 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2418 UNREACHABLE_RETURN(0);
2422static rb_parser_string_t *
2423rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, rb_len_t len,
2424 rb_encoding *ptr_enc)
2426 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2429static rb_parser_string_t *
2430rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2432 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2434 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2435 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2437 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2442static rb_parser_string_t *
2443rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, rb_len_t len)
2446 rb_bug("negative string size (or size too big)");
2449 rb_len_t slen = PARSER_STRING_LEN(str);
2451 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2452 PARSER_ENC_CODERANGE_CLEAR(str);
2457 const int termlen = STRING_TERM_LEN(str);
2459 if ((capa = slen) < len) {
2460 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2462 else if (len == slen) return str;
2463 STRING_SET_LEN(str, len);
2464 STRING_TERM_FILL(str);
2469# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2470 ((ptrvar) = str->ptr, \
2471 (lenvar) = str->len, \
2472 (encvar) = str->enc)
2475rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2477 rb_len_t len1, len2;
2478 const char *ptr1, *ptr2;
2479 rb_encoding *enc1, *enc2;
2481 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2482 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2484 return (len1 != len2 ||
2486 memcmp(ptr1, ptr2, len1) != 0);
2490rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2493 if (ary->capa < len) {
2495 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2496 for (i = ary->len; i < len; i++) {
2503 * Do not call this directly.
2504 * Use rb_parser_ary_new_capa_for_XXX() instead.
2506static rb_parser_ary_t *
2507parser_ary_new_capa(rb_parser_t *p, long len)
2510 rb_bug("negative array size (or size too big): %ld", len);
2512 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2517 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2526static rb_parser_ary_t *
2527rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2529 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2530 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2534static rb_parser_ary_t *
2535rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2537 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2538 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2543static rb_parser_ary_t *
2544rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2546 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2547 ary->data_type = PARSER_ARY_DATA_NODE;
2552 * Do not call this directly.
2553 * Use rb_parser_ary_push_XXX() instead.
2555static rb_parser_ary_t *
2556parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2558 if (ary->len == ary->capa) {
2559 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2561 ary->data[ary->len++] = val;
2566static rb_parser_ary_t *
2567rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2569 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2570 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2572 return parser_ary_push(p, ary, val);
2575static rb_parser_ary_t *
2576rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2578 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2579 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2581 return parser_ary_push(p, ary, val);
2585static rb_parser_ary_t *
2586rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2588 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2589 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2591 return parser_ary_push(p, ary, val);
2596rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2599 rb_parser_string_free(p, token->str);
2604rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2606# define foreach_ary(ptr) \
2607 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2608 ptr < end_ary_data; ptr++)
2609 switch (ary->data_type) {
2610 case PARSER_ARY_DATA_AST_TOKEN:
2611 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2613 case PARSER_ARY_DATA_SCRIPT_LINE:
2614 foreach_ary(data) {rb_parser_string_free(p, *data);}
2616 case PARSER_ARY_DATA_NODE:
2617 /* Do nothing because nodes are freed when rb_ast_t is freed */
2620 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2633%define parse.error verbose
2635 if ((NODE *)$$ == (NODE *)-1) {
2636 rb_parser_printf(p, "NODE_SPECIAL");
2639 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2641} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2642 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2644 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2647 switch (nd_type(RNODE($$))) {
2649 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2652 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2655 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2657 case NODE_IMAGINARY:
2658 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2663} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2665 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2668 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2672 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2675%lex-param {struct parser_params *p}
2676%parse-param {struct parser_params *p}
2679 RUBY_SET_YYLLOC_OF_NONE(@$);
2681%after-shift after_shift
2682%before-reduce before_reduce
2683%after-reduce after_reduce
2684%after-shift-error-token after_shift_error_token
2685%after-pop-stack after_pop_stack
2689 rb_node_fcall_t *node_fcall;
2690 rb_node_args_t *node_args;
2691 rb_node_args_aux_t *node_args_aux;
2692 rb_node_opt_arg_t *node_opt_arg;
2693 rb_node_kw_arg_t *node_kw_arg;
2694 rb_node_block_pass_t *node_block_pass;
2695 rb_node_masgn_t *node_masgn;
2696 rb_node_def_temp_t *node_def_temp;
2697 rb_node_exits_t *node_exits;
2698 struct rb_locations_lambda_body_t *locations_lambda_body;
2703 const struct vtable *vars;
2704 struct rb_strterm_struct *strterm;
2705 struct lex_context ctxt;
2706 enum lex_state_e state;
2710 keyword_class "'class'"
2711 keyword_module "'module'"
2713 keyword_undef "'undef'"
2714 keyword_begin "'begin'"
2715 keyword_rescue "'rescue'"
2716 keyword_ensure "'ensure'"
2719 keyword_unless "'unless'"
2720 keyword_then "'then'"
2721 keyword_elsif "'elsif'"
2722 keyword_else "'else'"
2723 keyword_case "'case'"
2724 keyword_when "'when'"
2725 keyword_while "'while'"
2726 keyword_until "'until'"
2728 keyword_break "'break'"
2729 keyword_next "'next'"
2730 keyword_redo "'redo'"
2731 keyword_retry "'retry'"
2734 keyword_do_cond "'do' for condition"
2735 keyword_do_block "'do' for block"
2736 keyword_do_LAMBDA "'do' for lambda"
2737 keyword_return "'return'"
2738 keyword_yield "'yield'"
2739 keyword_super "'super'"
2740 keyword_self "'self'"
2742 keyword_true "'true'"
2743 keyword_false "'false'"
2747 modifier_if "'if' modifier"
2748 modifier_unless "'unless' modifier"
2749 modifier_while "'while' modifier"
2750 modifier_until "'until' modifier"
2751 modifier_rescue "'rescue' modifier"
2752 keyword_alias "'alias'"
2753 keyword_defined "'defined?'"
2754 keyword_BEGIN "'BEGIN'"
2756 keyword__LINE__ "'__LINE__'"
2757 keyword__FILE__ "'__FILE__'"
2758 keyword__ENCODING__ "'__ENCODING__'"
2760%token <id> tIDENTIFIER "local variable or method"
2761%token <id> tFID "method"
2762%token <id> tGVAR "global variable"
2763%token <id> tIVAR "instance variable"
2764%token <id> tCONSTANT "constant"
2765%token <id> tCVAR "class variable"
2766%token <id> tLABEL "label"
2767%token <node> tINTEGER "integer literal"
2768%token <node> tFLOAT "float literal"
2769%token <node> tRATIONAL "rational literal"
2770%token <node> tIMAGINARY "imaginary literal"
2771%token <node> tCHAR "char literal"
2772%token <node> tNTH_REF "numbered reference"
2773%token <node> tBACK_REF "back reference"
2774%token <node> tSTRING_CONTENT "literal content"
2775%token <num> tREGEXP_END
2776%token <num> tDUMNY_END "dummy end"
2778%type <node> singleton singleton_expr strings string string1 xstring regexp
2779%type <node> string_contents xstring_contents regexp_contents string_content
2780%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2781%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2782%type <node_def_temp> defn_head defs_head k_def
2783%type <node_exits> block_open k_while k_until k_for allow_exits
2784%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2785%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2786%type <node> command command_call command_call_value method_call
2787%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2788%type <node_fcall> fcall
2789%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2790%type <node> args arg_splat call_args opt_call_args
2791%type <node> paren_args opt_paren_args
2792%type <node_args> args_tail block_args_tail
2793%type <node> command_args aref_args
2794%type <node_block_pass> opt_block_arg block_arg
2795%type <node> var_ref var_lhs
2796%type <node> command_rhs arg_rhs
2797%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2798%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args f_empty_arg
2799%type <node_args_aux> f_arg f_arg_item
2800%type <node> f_marg f_rest_marg
2801%type <node_masgn> f_margs
2802%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2803%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2804%type <id> do bv_decls opt_bv_decl bvar
2805%type <node> lambda brace_body do_body
2806%type <locations_lambda_body> lambda_body
2807%type <node_args> f_larglist f_largs largs_tail
2808%type <node> brace_block cmd_brace_block do_block lhs none fitem
2809%type <node> mlhs_head mlhs_item mlhs_node
2810%type <node_masgn> mlhs mlhs_basic mlhs_inner
2811%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2812%type <node> p_expr p_as p_alt p_expr_basic p_find
2813%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2814%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2815%type <node> p_kwargs p_kwarg p_kw
2816%type <id> keyword_variable user_variable sym operation2 operation3
2817%type <id> cname fname op f_rest_arg f_block_arg opt_comma f_norm_arg f_bad_arg
2818%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2819%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2820%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2821%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2822%type <ctxt> p_in_kwarg
2823%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2824%type <num> max_numparam
2825%type <node> numparam
2827%token END_OF_INPUT 0 "end-of-input"
2830/* escaped chars, should be ignored otherwise */
2831%token <id> '\\' "backslash"
2832%token tSP "escaped space"
2833%token <id> '\t' "escaped horizontal tab"
2834%token <id> '\f' "escaped form feed"
2835%token <id> '\r' "escaped carriage return"
2836%token <id> '\13' "escaped vertical tab"
2837%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2838%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2839%token tPOW RUBY_TOKEN(POW) "**"
2840%token tCMP RUBY_TOKEN(CMP) "<=>"
2841%token tEQ RUBY_TOKEN(EQ) "=="
2842%token tEQQ RUBY_TOKEN(EQQ) "==="
2843%token tNEQ RUBY_TOKEN(NEQ) "!="
2844%token tGEQ RUBY_TOKEN(GEQ) ">="
2845%token tLEQ RUBY_TOKEN(LEQ) "<="
2846%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2847%token tOROP RUBY_TOKEN(OROP) "||"
2848%token tMATCH RUBY_TOKEN(MATCH) "=~"
2849%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2850%token tDOT2 RUBY_TOKEN(DOT2) ".."
2851%token tDOT3 RUBY_TOKEN(DOT3) "..."
2852%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2853%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2854%token tAREF RUBY_TOKEN(AREF) "[]"
2855%token tASET RUBY_TOKEN(ASET) "[]="
2856%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2857%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2858%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2859%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2860%token tCOLON3 ":: at EXPR_BEG"
2861%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2864%token tLPAREN_ARG "( arg"
2867%token tLBRACE_ARG "{ arg"
2869%token tDSTAR "**arg"
2871%token <num> tLAMBDA "->"
2872%token tSYMBEG "symbol literal"
2873%token tSTRING_BEG "string literal"
2874%token tXSTRING_BEG "backtick literal"
2875%token tREGEXP_BEG "regexp literal"
2876%token tWORDS_BEG "word list"
2877%token tQWORDS_BEG "verbatim word list"
2878%token tSYMBOLS_BEG "symbol list"
2879%token tQSYMBOLS_BEG "verbatim symbol list"
2880%token tSTRING_END "terminator"
2881%token tSTRING_DEND "'}'"
2882%token <state> tSTRING_DBEG "'#{'"
2883%token tSTRING_DVAR tLAMBEG tLABEL_END
2885%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2886%token tHEREDOC_BEG tHEREDOC_END k__END__
2893%nonassoc tLBRACE_ARG
2895%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2896%left keyword_or keyword_and
2898%nonassoc keyword_defined
2900%left modifier_rescue
2902%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2905%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2906%left '>' tGEQ '<' tLEQ
2912%right tUMINUS_NUM tUMINUS
2914%right '!' '~' tUPLUS
2921%rule %inline ident_or_const
2926%rule %inline user_or_keyword_variable
2932 * parameterizing rules
2934%rule asgn(rhs) <node>
2935 : lhs '=' lex_ctxt rhs
2937 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2938 /*% ripper: assign!($:1, $:4) %*/
2942%rule args_tail_basic(value, trailing) <node_args>
2943 : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing)
2945 $$ = new_args_tail(p, $1, $3, $4, &@3);
2946 /*% ripper: [$:1, $:3, $:4] %*/
2948 | f_kwarg(value) opt_f_block_arg(trailing)
2950 $$ = new_args_tail(p, $1, 0, $2, &@1);
2951 /*% ripper: [$:1, Qnil, $:2] %*/
2953 | f_any_kwrest opt_f_block_arg(trailing)
2955 $$ = new_args_tail(p, 0, $1, $2, &@1);
2956 /*% ripper: [Qnil, $:1, $:2] %*/
2960 $$ = new_args_tail(p, 0, 0, $1, &@1);
2961 /*% ripper: [Qnil, Qnil, $:1] %*/
2965%rule opt_f_block_arg(trailing) <id>
2974%rule def_endless_method(bodystmt) <node>
2975 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2977 endless_method_name(p, $head->nd_mid, &@head);
2978 restore_defun(p, $head);
2979 ($$ = $head->nd_def)->nd_loc = @$;
2980 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2981 RNODE_DEFN($$)->nd_defn = $bodystmt;
2982 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2983 /*% ripper: def!($:head, $:args, $:$) %*/
2986 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2988 endless_method_name(p, $head->nd_mid, &@head);
2989 restore_defun(p, $head);
2990 ($$ = $head->nd_def)->nd_loc = @$;
2991 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2992 RNODE_DEFS($$)->nd_defn = $bodystmt;
2993 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2994 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2999%rule compstmt(stmts) <node>
3002 void_stmts(p, $$ = $stmts);
3006%rule f_opt(value) <node_opt_arg>
3007 : f_arg_asgn f_eq value
3009 p->ctxt.in_argdef = 1;
3010 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
3011 /*% ripper: [$:$, $:3] %*/
3015%rule f_opt_arg(value) <node_opt_arg>
3019 /*% ripper: rb_ary_new3(1, $:1) %*/
3021 | f_opt_arg(value) ',' f_opt(value)
3023 $$ = opt_arg_append($f_opt_arg, $f_opt);
3024 /*% ripper: rb_ary_push($:1, $:3) %*/
3028%rule f_kw(value) <node_kw_arg>
3031 p->ctxt.in_argdef = 1;
3032 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3033 /*% ripper: [$:$, $:value] %*/
3037 p->ctxt.in_argdef = 1;
3038 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3039 /*% ripper: [$:$, 0] %*/
3043%rule f_kwarg(value) <node_kw_arg>
3047 /*% ripper: rb_ary_new3(1, $:1) %*/
3049 | f_kwarg(value) ',' f_kw(value)
3051 $$ = kwd_append($f_kwarg, $f_kw);
3052 /*% ripper: rb_ary_push($:1, $:3) %*/
3056%rule mlhs_items(item) <node>
3059 $$ = NEW_LIST($1, &@$);
3060 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3062 | mlhs_items(item) ',' item
3064 $$ = list_append(p, $1, $3);
3065 /*% ripper: mlhs_add!($:1, $:3) %*/
3069%rule op_asgn(rhs) <node>
3070 : var_lhs tOP_ASGN lex_ctxt rhs
3072 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3073 /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/
3075 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3077 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3078 /*% ripper: opassign!(aref_field!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/
3080 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3082 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3083 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3085 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3087 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3088 /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3090 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3092 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3093 /*% ripper: opassign!(field!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/
3095 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3097 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3098 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3099 /*% ripper: opassign!(const_path_field!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3101 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3103 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3104 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3105 /*% ripper: opassign!(top_const_field!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/
3107 | backref tOP_ASGN lex_ctxt rhs
3109 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3110 $$ = NEW_ERROR(&@$);
3111 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:backref), $:tOP_ASGN, $:rhs)) %*/
3115%rule opt_args_tail(tail, trailing) <node_args>
3119 /*% ripper: $:tail %*/
3123 $$ = new_empty_args_tail(p, &@$);
3124 /*% ripper: [Qnil, Qnil, Qnil] %*/
3128%rule range_expr(range) <node>
3133 $$ = NEW_DOT2($1, $3, &@$, &@2);
3134 /*% ripper: dot2!($:1, $:3) %*/
3140 $$ = NEW_DOT3($1, $3, &@$, &@2);
3141 /*% ripper: dot3!($:1, $:3) %*/
3146 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3147 /*% ripper: dot2!($:1, Qnil) %*/
3152 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3153 /*% ripper: dot3!($:1, Qnil) %*/
3158 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3159 /*% ripper: dot2!(Qnil, $:2) %*/
3164 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3165 /*% ripper: dot3!(Qnil, $:2) %*/
3169%rule value_expr(value) <node>
3177%rule words(begin, word_list) <node>
3178 : begin ' '+ word_list tSTRING_END
3180 $$ = make_list($word_list, &@$);
3181 /*% ripper: array!($:word_list) %*/
3187 SET_LEX_STATE(EXPR_BEG);
3188 local_push(p, ifndef_ripper(1)+0);
3189 /* jumps are possible in the top-level loop. */
3190 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3194 if ($2 && !compile_for_eval) {
3196 /* last expression should not be void */
3197 if (nd_type_p(node, NODE_BLOCK)) {
3198 while (RNODE_BLOCK(node)->nd_next) {
3199 node = RNODE_BLOCK(node)->nd_next;
3201 node = RNODE_BLOCK(node)->nd_head;
3203 node = remove_begin(node);
3206 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3207 /*% ripper[final]: program!($:2) %*/
3214 $$ = NEW_BEGIN(0, &@$);
3215 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3219 $$ = newline_node($1);
3220 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3222 | top_stmts terms top_stmt
3224 $$ = block_append(p, $1, newline_node($3));
3225 /*% ripper: stmts_add!($:1, $:3) %*/
3231 clear_block_exit(p, true);
3234 | keyword_BEGIN begin_block
3241block_open : '{' {$$ = init_block_exit(p);};
3243begin_block : block_open compstmt(top_stmts) '}'
3245 restore_block_exit(p, $block_open);
3246 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3247 NEW_BEGIN($compstmt, &@$));
3248 $$ = NEW_BEGIN(0, &@$);
3249 /*% ripper: BEGIN!($:compstmt) %*/
3253bodystmt : compstmt(stmts)[body]
3258 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3259 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3261 compstmt(stmts)[elsebody]
3263 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3267 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3268 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3270 | compstmt(stmts)[body]
3274 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3278 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3279 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3285 $$ = NEW_BEGIN(0, &@$);
3286 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3290 $$ = newline_node($1);
3291 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3293 | stmts terms stmt_or_begin
3295 $$ = block_append(p, $1, newline_node($3));
3296 /*% ripper: stmts_add!($:1, $:3) %*/
3303 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3311allow_exits : {$$ = allow_block_exit(p);};
3313k_END : keyword_END lex_ctxt
3315 if (p->ctxt.in_def) {
3316 rb_warn0("END in method; use at_exit");
3319 p->ctxt.in_rescue = before_rescue;
3323stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old]
3325 $$ = NEW_ALIAS($new, $old, &@$, &@kw);
3326 /*% ripper: alias!($:new, $:old) %*/
3328 | keyword_alias[kw] tGVAR[new] tGVAR[old]
3330 $$ = NEW_VALIAS($new, $old, &@$, &@kw);
3331 /*% ripper: var_alias!($:new, $:old) %*/
3333 | keyword_alias[kw] tGVAR[new] tBACK_REF[old]
3337 buf[1] = (char)RNODE_BACK_REF($old)->nd_nth;
3338 $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw);
3339 /*% ripper: var_alias!($:new, $:old) %*/
3341 | keyword_alias tGVAR tNTH_REF[nth]
3343 static const char mesg[] = "can't make alias for the number variables";
3345 yyerror1(&@nth, mesg);
3347 $$ = NEW_ERROR(&@$);
3348 /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/
3350 | keyword_undef[kw] undef_list[list]
3352 nd_set_first_loc($list, @kw.beg_pos);
3353 RNODE_UNDEF($list)->keyword_loc = @kw;
3355 /*% ripper: undef!($:list) %*/
3357 | stmt[body] modifier_if[mod] expr_value[cond]
3359 $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3361 /*% ripper: if_mod!($:cond, $:body) %*/
3363 | stmt[body] modifier_unless[mod] expr_value[cond]
3365 $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC);
3367 /*% ripper: unless_mod!($:cond, $:body) %*/
3369 | stmt[body] modifier_while[mod] expr_value[cond_expr]
3371 clear_block_exit(p, false);
3372 if ($body && nd_type_p($body, NODE_BEGIN)) {
3373 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3376 $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3378 /*% ripper: while_mod!($:cond_expr, $:body) %*/
3380 | stmt[body] modifier_until[mod] expr_value[cond_expr]
3382 clear_block_exit(p, 0);
3383 if ($body && nd_type_p($body, NODE_BEGIN)) {
3384 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC);
3387 $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC);
3389 /*% ripper: until_mod!($:cond_expr, $:body) %*/
3391 | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody]
3393 p->ctxt.in_rescue = $ctxt.in_rescue;
3395 YYLTYPE loc = code_loc_gen(&@mod, &@resbody);
3396 resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3397 $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$);
3398 /*% ripper: rescue_mod!($:body, $:resbody) %*/
3400 | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace]
3402 clear_block_exit(p, true);
3403 restore_block_exit(p, $block_open);
3406 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$);
3407 $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace);
3408 RNODE_SCOPE(scope)->nd_parent = $$;
3410 /*% ripper: END!($:body) %*/
3413 | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs]
3415 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3416 /*% ripper: massign!($:lhs, $:rhs) %*/
3419 | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue]
3420 after_rescue[after_rescue] stmt[resbody]
3422 p->ctxt.in_rescue = $after_rescue.in_rescue;
3423 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3424 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3425 loc.beg_pos = @mrhs_arg.beg_pos;
3426 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3427 $$ = node_assign(p, (NODE *)$lhs, $mrhs_arg, $lex_ctxt, &@$);
3428 /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/
3430 | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs]
3432 $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$);
3433 /*% ripper: massign!($:lhs, $:rhs) %*/
3439 $$ = NEW_ERROR(&@$);
3443command_asgn : asgn(command_rhs)
3444 | op_asgn(command_rhs)
3445 | def_endless_method(endless_command)
3448endless_command : command
3449 | endless_command modifier_rescue after_rescue arg
3451 p->ctxt.in_rescue = $3.in_rescue;
3452 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3453 /*% ripper: rescue_mod!($:1, $:4) %*/
3455 | keyword_not '\n'? endless_command
3457 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3458 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3462command_rhs : command_call_value %prec tOP_ASGN
3463 | command_call_value modifier_rescue after_rescue stmt
3465 p->ctxt.in_rescue = $3.in_rescue;
3466 YYLTYPE loc = code_loc_gen(&@2, &@4);
3467 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3468 /*% ripper: rescue_mod!($:1, $:4) %*/
3474 | expr[left] keyword_and[op] expr[right]
3476 $$ = logop(p, idAND, $left, $right, &@op, &@$);
3477 /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/
3479 | expr[left] keyword_or[op] expr[right]
3481 $$ = logop(p, idOR, $left, $right, &@op, &@$);
3482 /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/
3484 | keyword_not[not] '\n'? expr[arg]
3486 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$);
3487 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
3489 | '!'[not] command_call[arg]
3491 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$);
3492 /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/
3496 value_expr(p, $arg);
3498 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3499 p_top_expr_body[body]
3501 pop_pktbl(p, $p_pktbl);
3502 pop_pvtbl(p, $p_pvtbl);
3503 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3504 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3505 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3506 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@assoc), &@$, &NULL_LOC, &NULL_LOC);
3507 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3511 value_expr(p, $arg);
3513 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3514 p_top_expr_body[body]
3516 pop_pktbl(p, $p_pktbl);
3517 pop_pvtbl(p, $p_pvtbl);
3518 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3519 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3520 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3521 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3522 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3524 | arg %prec tLBRACE_ARG
3529 numparam_name(p, $fname);
3532 p->ctxt.in_rescue = before_rescue;
3533 p->ctxt.cant_return = 0;
3538defn_head : k_def def_name
3540 $$ = def_head_save(p, $k_def);
3541 $$->nd_mid = $def_name;
3542 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3543 /*% ripper: $:def_name %*/
3547defs_head : k_def singleton dot_or_colon
3549 SET_LEX_STATE(EXPR_FNAME);
3553 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3554 $$ = def_head_save(p, $k_def);
3555 $$->nd_mid = $def_name;
3556 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3557 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3561expr_value : value_expr(expr)
3564 $$ = NEW_ERROR(&@$);
3568expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3575command_call : command
3579command_call_value : value_expr(command_call)
3582block_command : block_call
3583 | block_call call_op2 operation2 command_args
3585 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3586 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3590cmd_brace_block : tLBRACE_ARG brace_body '}'
3593 set_embraced_location($$, &@1, &@3);
3600 $$ = NEW_FCALL($1, 0, &@$);
3605command : fcall command_args %prec tLOWEST
3608 nd_set_last_loc($1, @2.end_pos);
3610 /*% ripper: command!($:1, $:2) %*/
3612 | fcall command_args cmd_brace_block
3614 block_dup_check(p, $2, $3);
3616 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3617 fixpos($$, RNODE($1));
3618 nd_set_last_loc($1, @2.end_pos);
3619 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3621 | primary_value call_op operation2 command_args %prec tLOWEST
3623 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3624 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3626 | primary_value call_op operation2 command_args cmd_brace_block
3628 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3629 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3631 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3633 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3634 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3636 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3638 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3639 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3641 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3643 set_embraced_location($5, &@4, &@6);
3644 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3645 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3647 | keyword_super command_args
3649 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3651 /*% ripper: super!($:2) %*/
3653 | k_yield command_args
3655 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3657 /*% ripper: yield!($:2) %*/
3659 | k_return_arg call_args
3662 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3663 /*% ripper: return!($:2) %*/
3665 | k_break_arg call_args
3669 args = ret_args(p, $2);
3670 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3671 /*% ripper: break!($:2) %*/
3673 | k_next_arg call_args
3677 args = ret_args(p, $2);
3678 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3679 /*% ripper: next!($:2) %*/
3684 | tLPAREN mlhs_inner rparen
3687 /*% ripper: mlhs_paren!($:2) %*/
3691mlhs_inner : mlhs_basic
3692 | tLPAREN mlhs_inner rparen
3694 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3695 /*% ripper: mlhs_paren!($:2) %*/
3699mlhs_basic : mlhs_head
3701 $$ = NEW_MASGN($1, 0, &@$);
3704 | mlhs_head mlhs_item
3706 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3707 /*% ripper: mlhs_add!($:1, $:2) %*/
3709 | mlhs_head tSTAR mlhs_node
3711 $$ = NEW_MASGN($1, $3, &@$);
3712 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3714 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3716 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3717 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3721 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3722 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3724 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3726 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3727 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3731 $$ = NEW_MASGN(0, $2, &@$);
3732 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3734 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3736 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3737 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3741 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3742 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3744 | tSTAR ',' mlhs_items(mlhs_item)
3746 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3747 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3751mlhs_item : mlhs_node
3752 | tLPAREN mlhs_inner rparen
3755 /*% ripper: mlhs_paren!($:2) %*/
3759mlhs_head : mlhs_item ','
3761 $$ = NEW_LIST($1, &@1);
3762 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3764 | mlhs_head mlhs_item ','
3766 $$ = list_append(p, $1, $2);
3767 /*% ripper: mlhs_add!($:1, $:2) %*/
3772mlhs_node : user_or_keyword_variable
3774 /*% ripper: var_field!($:1) %*/
3775 $$ = assignable(p, $1, 0, &@$);
3777 | primary_value '[' opt_call_args rbracket
3779 $$ = aryset(p, $1, $3, &@$);
3780 /*% ripper: aref_field!($:1, $:3) %*/
3782 | primary_value call_op ident_or_const
3784 anddot_multiple_assignment_check(p, &@2, $2);
3785 $$ = attrset(p, $1, $2, $3, &@$);
3786 /*% ripper: field!($:1, $:2, $:3) %*/
3788 | primary_value tCOLON2 tIDENTIFIER
3790 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3791 /*% ripper: const_path_field!($:1, $:3) %*/
3793 | primary_value tCOLON2 tCONSTANT
3795 /*% ripper: const_path_field!($:1, $:3) %*/
3796 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3800 /*% ripper: top_const_field!($:2) %*/
3801 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3805 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3806 $$ = NEW_ERROR(&@$);
3807 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3811lhs : user_or_keyword_variable
3813 /*% ripper: var_field!($:1) %*/
3814 $$ = assignable(p, $1, 0, &@$);
3816 | primary_value '[' opt_call_args rbracket
3818 $$ = aryset(p, $1, $3, &@$);
3819 /*% ripper: aref_field!($:1, $:3) %*/
3821 | primary_value call_op ident_or_const
3823 $$ = attrset(p, $1, $2, $3, &@$);
3824 /*% ripper: field!($:1, $:2, $:3) %*/
3826 | primary_value tCOLON2 tIDENTIFIER
3828 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3829 /*% ripper: field!($:1, $:2, $:3) %*/
3831 | primary_value tCOLON2 tCONSTANT
3833 /*% ripper: const_path_field!($:1, $:3) %*/
3834 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3838 /*% ripper: top_const_field!($:2) %*/
3839 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3843 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3844 $$ = NEW_ERROR(&@$);
3845 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3851 static const char mesg[] = "class/module name must be CONSTANT";
3853 yyerror1(&@1, mesg);
3855 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3860cpath : tCOLON3 cname
3862 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3863 /*% ripper: top_const_ref!($:2) %*/
3867 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3868 /*% ripper: const_ref!($:1) %*/
3870 | primary_value tCOLON2 cname
3872 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3873 /*% ripper: const_path_ref!($:1, $:3) %*/
3880 SET_LEX_STATE(EXPR_ENDFN);
3888 $$ = NEW_SYM(rb_id2str($1), &@$);
3889 /*% ripper: symbol_literal!($:1) %*/
3896 $$ = NEW_UNDEF($1, &@$);
3897 /*% ripper: rb_ary_new3(1, $:1) %*/
3899 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3901 nd_set_last_loc($1, @4.end_pos);
3902 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3903 /*% ripper: rb_ary_push($:1, $:4) %*/
3907op : '|' { $$ = '|'; }
3910 | tCMP { $$ = tCMP; }
3912 | tEQQ { $$ = tEQQ; }
3913 | tMATCH { $$ = tMATCH; }
3914 | tNMATCH { $$ = tNMATCH; }
3916 | tGEQ { $$ = tGEQ; }
3918 | tLEQ { $$ = tLEQ; }
3919 | tNEQ { $$ = tNEQ; }
3920 | tLSHFT { $$ = tLSHFT; }
3921 | tRSHFT { $$ = tRSHFT; }
3925 | tSTAR { $$ = '*'; }
3928 | tPOW { $$ = tPOW; }
3929 | tDSTAR { $$ = tDSTAR; }
3932 | tUPLUS { $$ = tUPLUS; }
3933 | tUMINUS { $$ = tUMINUS; }
3934 | tAREF { $$ = tAREF; }
3935 | tASET { $$ = tASET; }
3939reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3940 | keyword_BEGIN | keyword_END
3941 | keyword_alias | keyword_and | keyword_begin
3942 | keyword_break | keyword_case | keyword_class | keyword_def
3943 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3944 | keyword_end | keyword_ensure | keyword_false
3945 | keyword_for | keyword_in | keyword_module | keyword_next
3946 | keyword_nil | keyword_not | keyword_or | keyword_redo
3947 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3948 | keyword_super | keyword_then | keyword_true | keyword_undef
3949 | keyword_when | keyword_yield | keyword_if | keyword_unless
3950 | keyword_while | keyword_until
3958 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3959 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3963 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3964 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3968 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3969 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3973 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3974 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3978 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3979 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3983 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3984 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3986 | tUMINUS_NUM simple_numeric tPOW arg
3988 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3989 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3993 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3994 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3998 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3999 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
4003 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
4004 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
4008 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
4009 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
4013 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
4014 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
4018 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
4019 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
4021 | rel_expr %prec tCMP
4024 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
4025 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4029 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4030 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4034 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4035 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4039 $$ = match_op(p, $1, $3, &@2, &@$);
4040 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4044 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4045 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4049 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4050 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4054 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4055 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4059 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4060 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4064 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4065 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4069 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4070 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4074 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4075 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4077 | keyword_defined '\n'? begin_defined arg
4079 p->ctxt.in_defined = $3.in_defined;
4080 $$ = new_defined(p, $4, &@$, &@1);
4081 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4082 /*% ripper: defined!($:4) %*/
4084 | def_endless_method(endless_arg)
4089ternary : arg '?' arg '\n'? ':' arg
4092 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4094 /*% ripper: ifop!($:1, $:3, $:6) %*/
4098endless_arg : arg %prec modifier_rescue
4099 | endless_arg modifier_rescue after_rescue arg
4101 p->ctxt.in_rescue = $3.in_rescue;
4102 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4103 /*% ripper: rescue_mod!($:1, $:4) %*/
4105 | keyword_not '\n'? endless_arg
4107 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4108 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4112relop : '>' {$$ = '>';}
4118rel_expr : arg relop arg %prec '>'
4120 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4121 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4123 | rel_expr relop arg %prec '>'
4125 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4126 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4127 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4137begin_defined : lex_ctxt
4139 p->ctxt.in_defined = 1;
4144after_rescue : lex_ctxt
4146 p->ctxt.in_rescue = after_rescue;
4151arg_value : value_expr(arg)
4156 | args ',' assocs trailer
4158 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4159 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4163 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4164 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4168arg_rhs : arg %prec tOP_ASGN
4173 | arg modifier_rescue after_rescue arg
4175 p->ctxt.in_rescue = $3.in_rescue;
4177 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4178 /*% ripper: rescue_mod!($:1, $:4) %*/
4182paren_args : '(' opt_call_args rparen
4185 /*% ripper: arg_paren!($:2) %*/
4187 | '(' args ',' args_forward rparen
4189 if (!check_forwarding_args(p)) {
4193 $$ = new_args_forward_call(p, $2, &@4, &@$);
4194 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4197 | '(' args_forward rparen
4199 if (!check_forwarding_args(p)) {
4203 $$ = new_args_forward_call(p, 0, &@2, &@$);
4204 /*% ripper: arg_paren!($:2) %*/
4209opt_paren_args : none
4212 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4219 | args ',' assocs ','
4221 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4222 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4226 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4227 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4231call_args : value_expr(command)
4233 $$ = NEW_LIST($1, &@$);
4234 /*% ripper: args_add!(args_new!, $:1) %*/
4236 | def_endless_method(endless_command)
4238 $$ = NEW_LIST($1, &@$);
4239 /*% ripper: args_add!(args_new!, $:1) %*/
4241 | args opt_block_arg
4243 $$ = arg_blk_pass($1, $2);
4244 /*% ripper: args_add_block!($:1, $:2) %*/
4246 | assocs opt_block_arg
4248 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4249 $$ = arg_blk_pass($$, $2);
4250 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4252 | args ',' assocs opt_block_arg
4254 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4255 $$ = arg_blk_pass($$, $4);
4256 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4259 /*% ripper: args_add_block!(args_new!, $:1) %*/
4263 /* If call_args starts with a open paren '(' or '[',
4264 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4265 * but the push must be done after CMDARG_PUSH(1).
4266 * So this code makes them consistent by first cancelling
4267 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4268 * and finally redoing CMDARG_PUSH(0).
4272 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4275 if (lookahead) CMDARG_POP();
4277 if (lookahead) CMDARG_PUSH(0);
4281 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4282 * but the push must be done after CMDARG_POP() in the parser.
4283 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4284 * CMDARG_POP() to pop 1 pushed by command_args,
4285 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4292 if (lookahead) CMDARG_POP();
4294 if (lookahead) CMDARG_PUSH(0);
4300block_arg : tAMPER arg_value
4302 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4307 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4308 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4309 /*% ripper: Qnil %*/
4313opt_block_arg : ',' block_arg
4321 /*% ripper: Qfalse %*/
4328 $$ = NEW_LIST($arg_value, &@$);
4329 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4334 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4336 | args[non_last_args] ',' arg_value
4338 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4339 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4341 | args[non_last_args] ',' arg_splat
4343 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4344 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4349arg_splat : tSTAR arg_value
4351 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4352 /*% ripper: $:arg_value %*/
4356 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4357 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4358 /*% ripper: Qnil %*/
4368mrhs : args ',' arg_value
4370 $$ = last_arg_append(p, $args, $arg_value, &@$);
4371 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4373 | args ',' tSTAR arg_value
4375 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4376 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4380 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4381 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4385%rule %inline inline_primary
4396primary : inline_primary
4401 $$ = (NODE *)NEW_FCALL($fid, 0, &@$);
4402 /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/
4412 set_line_body($body, @kw.end_pos.lineno);
4413 $$ = NEW_BEGIN($body, &@$);
4414 nd_set_line($$, @kw.end_pos.lineno);
4415 /*% ripper: begin!($:body) %*/
4417 | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')'
4419 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4421 /*% ripper: paren!($:body) %*/
4423 | tLPAREN compstmt(stmts)[body] ')'
4425 if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0;
4426 $$ = NEW_BLOCK($body, &@$);
4427 /*% ripper: paren!($:body) %*/
4429 | primary_value[recv] tCOLON2[op] tCONSTANT[name]
4431 $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name);
4432 /*% ripper: const_path_ref!($:recv, $:name) %*/
4434 | tCOLON3[top] tCONSTANT[name]
4436 $$ = NEW_COLON3($name, &@$, &@top, &@name);
4437 /*% ripper: top_const_ref!($:name) %*/
4439 | tLBRACK aref_args[args] ']'
4441 $$ = make_list($args, &@$);
4442 /*% ripper: array!($:args) %*/
4444 | tLBRACE assoc_list[list] '}'
4446 $$ = new_hash(p, $list, &@$);
4447 RNODE_HASH($$)->nd_brace = TRUE;
4448 /*% ripper: hash!($:list) %*/
4452 $$ = NEW_RETURN(0, &@$, &@kw);
4453 /*% ripper: return0! %*/
4455 | k_yield[kw] '('[lpar] call_args[args] rparen[rpar]
4457 $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar);
4458 /*% ripper: yield!(paren!($:args)) %*/
4460 | k_yield[kw] '('[lpar] rparen[rpar]
4462 $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar);
4463 /*% ripper: yield!(paren!(args_new!)) %*/
4467 $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC);
4468 /*% ripper: yield0! %*/
4470 | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen
4472 p->ctxt.in_defined = $ctxt.in_defined;
4473 $$ = new_defined(p, $arg, &@$, &@kw);
4474 p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon;
4475 /*% ripper: defined!($:arg) %*/
4477 | keyword_not[kw] '(' expr[arg] rparen
4479 $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$);
4480 /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/
4482 | keyword_not[kw] '('[lpar] rparen
4484 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$);
4485 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4487 | fcall[call] brace_block[block]
4489 $$ = method_add_block(p, (NODE *)$call, $block, &@$);
4490 /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/
4493 | method_call[call] brace_block[block]
4495 block_dup_check(p, get_nd_args(p, $call), $block);
4496 $$ = method_add_block(p, $call, $block, &@$);
4497 /*% ripper: method_add_block!($:call, $:block) %*/
4500 | k_if[kw] expr_value[cond] then[then]
4501 compstmt(stmts)[body]
4505 if ($tail && nd_type_p($tail, NODE_IF))
4506 RNODE_IF($tail)->end_keyword_loc = @k_end;
4508 $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4510 /*% ripper: if!($:cond, $:body, $:tail) %*/
4512 | k_unless[kw] expr_value[cond] then[then]
4513 compstmt(stmts)[body]
4517 $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end);
4519 /*% ripper: unless!($:cond, $:body, $:tail) %*/
4521 | k_while[kw] expr_value_do[cond]
4522 compstmt(stmts)[body]
4525 restore_block_exit(p, $kw);
4526 $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4528 /*% ripper: while!($:cond, $:body) %*/
4530 | k_until[kw] expr_value_do[cond]
4531 compstmt(stmts)[body]
4534 restore_block_exit(p, $kw);
4535 $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end);
4537 /*% ripper: until!($:cond, $:body) %*/
4539 | k_case[k_case] expr_value[expr] terms?
4541 $$ = p->case_labels;
4542 p->case_labels = CHECK_LITERAL_WHEN;
4547 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4548 p->case_labels = $labels;
4549 $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end);
4551 /*% ripper: case!($:expr, $:body) %*/
4553 | k_case[k_case] terms?
4555 $$ = p->case_labels;
4561 if (p->case_labels) st_free_table(p->case_labels);
4562 p->case_labels = $labels;
4563 $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end);
4564 /*% ripper: case!(Qnil, $:body) %*/
4566 | k_case[k_case] expr_value[expr] terms?
4570 $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end);
4571 /*% ripper: case!($:expr, $:body) %*/
4573 | k_for[k_for] for_var[for_var] keyword_in[keyword_in]
4574 {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();}
4575 compstmt(stmts)[compstmt]
4578 restore_block_exit(p, $k_for);
4582 * e.each{|*x| a, b, c = x}
4586 * e.each{|x| a, = x}
4588 ID id = internal_id(p);
4589 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4590 rb_node_args_t *args;
4591 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4592 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4593 tbl->ids[0] = id; /* internal id */
4595 switch (nd_type($for_var)) {
4597 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4598 set_nd_value(p, $for_var, internal_var);
4601 m->nd_next = $for_var;
4603 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4604 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4606 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4607 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);
4609 /* {|*internal_id| <m> = internal_id; ... } */
4610 args = new_args(p, m, 0, id, 0, new_empty_args_tail(p, &@for_var), &@for_var);
4611 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4612 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4613 $$ = NEW_FOR($expr_value, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4614 RNODE_SCOPE(scope)->nd_parent = $$;
4615 fixpos($$, $for_var);
4616 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4618 | k_class cpath superclass
4620 begin_definition("class", &@k_class, &@cpath);
4625 YYLTYPE inheritance_operator_loc = NULL_LOC;
4627 inheritance_operator_loc = @superclass;
4628 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4630 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4631 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4632 set_line_body($bodystmt, @superclass.end_pos.lineno);
4633 nd_set_line($$, @superclass.end_pos.lineno);
4634 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4636 p->ctxt.in_class = $k_class.in_class;
4637 p->ctxt.cant_return = $k_class.cant_return;
4638 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4640 | k_class tLSHFT expr_value
4642 begin_definition("", &@k_class, &@tLSHFT);
4648 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4649 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4650 set_line_body($bodystmt, nd_line($expr_value));
4651 fixpos($$, $expr_value);
4652 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4654 p->ctxt.in_def = $k_class.in_def;
4655 p->ctxt.in_class = $k_class.in_class;
4656 p->ctxt.cant_return = $k_class.cant_return;
4657 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4661 begin_definition("module", &@k_module, &@cpath);
4666 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4667 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4668 set_line_body($bodystmt, @cpath.end_pos.lineno);
4669 nd_set_line($$, @cpath.end_pos.lineno);
4670 /*% ripper: module!($:cpath, $:bodystmt) %*/
4672 p->ctxt.in_class = $k_module.in_class;
4673 p->ctxt.cant_return = $k_module.cant_return;
4674 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4679 push_end_expect_token_locations(p, &@head.beg_pos);
4684 restore_defun(p, $head);
4685 ($$ = $head->nd_def)->nd_loc = @$;
4686 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4687 RNODE_DEFN($$)->nd_defn = $bodystmt;
4688 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4694 push_end_expect_token_locations(p, &@head.beg_pos);
4699 restore_defun(p, $head);
4700 ($$ = $head->nd_def)->nd_loc = @$;
4701 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4702 RNODE_DEFS($$)->nd_defn = $bodystmt;
4703 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4708 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw));
4709 /*% ripper: break!(args_new!) %*/
4713 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw));
4714 /*% ripper: next!(args_new!) %*/
4718 $$ = add_block_exit(p, NEW_REDO(&@$, &@kw));
4719 /*% ripper: redo! %*/
4723 if (!p->ctxt.in_defined) {
4724 switch (p->ctxt.in_rescue) {
4725 case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break;
4726 case after_rescue: /* ok */ break;
4727 case after_else: yyerror1(&@kw, "Invalid retry after else"); break;
4728 case after_ensure: yyerror1(&@kw, "Invalid retry after ensure"); break;
4731 $$ = NEW_RETRY(&@$);
4732 /*% ripper: retry! %*/
4736primary_value : value_expr(primary)
4739k_begin : keyword_begin
4741 token_info_push(p, "begin", &@$);
4742 push_end_expect_token_locations(p, &@1.beg_pos);
4749 token_info_push(p, "if", &@$);
4750 if (p->token_info && p->token_info->nonspc &&
4751 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4752 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4753 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4754 beg += rb_strlen_lit("else");
4755 while (beg < tok && ISSPACE(*beg)) beg++;
4757 p->token_info->nonspc = 0;
4760 push_end_expect_token_locations(p, &@1.beg_pos);
4764k_unless : keyword_unless
4766 token_info_push(p, "unless", &@$);
4767 push_end_expect_token_locations(p, &@1.beg_pos);
4771k_while : keyword_while[kw] allow_exits
4774 token_info_push(p, "while", &@$);
4775 push_end_expect_token_locations(p, &@kw.beg_pos);
4779k_until : keyword_until[kw] allow_exits
4782 token_info_push(p, "until", &@$);
4783 push_end_expect_token_locations(p, &@kw.beg_pos);
4787k_case : keyword_case
4789 token_info_push(p, "case", &@$);
4790 push_end_expect_token_locations(p, &@1.beg_pos);
4794k_for : keyword_for[kw] allow_exits
4797 token_info_push(p, "for", &@$);
4798 push_end_expect_token_locations(p, &@kw.beg_pos);
4802k_class : keyword_class
4804 token_info_push(p, "class", &@$);
4806 p->ctxt.in_rescue = before_rescue;
4807 push_end_expect_token_locations(p, &@1.beg_pos);
4811k_module : keyword_module
4813 token_info_push(p, "module", &@$);
4815 p->ctxt.in_rescue = before_rescue;
4816 push_end_expect_token_locations(p, &@1.beg_pos);
4822 token_info_push(p, "def", &@$);
4823 $$ = NEW_DEF_TEMP(&@$);
4824 p->ctxt.in_argdef = 1;
4830 token_info_push(p, "do", &@$);
4831 push_end_expect_token_locations(p, &@1.beg_pos);
4835k_do_block : keyword_do_block
4837 token_info_push(p, "do", &@$);
4838 push_end_expect_token_locations(p, &@1.beg_pos);
4842k_rescue : keyword_rescue
4844 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4846 p->ctxt.in_rescue = after_rescue;
4850k_ensure : keyword_ensure
4852 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4857k_when : keyword_when
4859 token_info_warn(p, "when", p->token_info, 0, &@$);
4863k_else : keyword_else
4865 token_info *ptinfo_beg = p->token_info;
4866 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4867 token_info_warn(p, "else", p->token_info, same, &@$);
4870 e.next = ptinfo_beg->next;
4872 token_info_setup(&e, p->lex.pbeg, &@$);
4873 if (!e.nonspc) *ptinfo_beg = e;
4878k_elsif : keyword_elsif
4881 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4887 token_info_pop(p, "end", &@$);
4888 pop_end_expect_token_locations(p);
4892 compile_error(p, "syntax error, unexpected end-of-input");
4896k_return : keyword_return
4898 if (p->ctxt.cant_return && !dyna_in_block(p))
4899 yyerror1(&@1, "Invalid return in class/module body");
4903k_return_arg : k_return
4909k_break_arg : keyword_break
4915k_next_arg : keyword_next
4921k_yield : keyword_yield
4923 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4924 yyerror1(&@1, "Invalid yield");
4934 | keyword_do_cond { $$ = keyword_do_cond; }
4938 | k_elsif expr_value then
4942 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4944 /*% ripper: elsif!($:2, $:4, $:5) %*/
4949 | k_else compstmt(stmts)
4952 /*% ripper: else!($:2) %*/
4962 $$ = assignable(p, $1, 0, &@$);
4963 mark_lvar_used(p, $$);
4965 | tLPAREN f_margs rparen
4968 /*% ripper: mlhs_paren!($:2) %*/
4973f_margs : mlhs_items(f_marg)
4975 $$ = NEW_MASGN($1, 0, &@$);
4978 | mlhs_items(f_marg) ',' f_rest_marg
4980 $$ = NEW_MASGN($1, $3, &@$);
4981 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4983 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4985 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4986 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4990 $$ = NEW_MASGN(0, $1, &@$);
4991 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4993 | f_rest_marg ',' mlhs_items(f_marg)
4995 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4996 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
5000f_rest_marg : tSTAR f_norm_arg
5003 $$ = assignable(p, $2, 0, &@$);
5004 mark_lvar_used(p, $$);
5008 $$ = NODE_SPECIAL_NO_NAME_REST;
5009 /*% ripper: Qnil %*/
5013f_any_kwrest : f_kwrest
5017 /*% ripper: ID2VAL(idNil) %*/
5021f_eq : {p->ctxt.in_argdef = 0;} '=';
5023block_args_tail : args_tail_basic(primary_value, none)
5028 /* magic number for rest_id in iseq_set_arguments() */
5029 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5030 /*% ripper: excessed_comma! %*/
5034block_param : args-list(primary_value, opt_args_tail(block_args_tail, none))
5035 | f_arg[pre] excessed_comma
5037 $$ = new_empty_args_tail(p, &@excessed_comma);
5038 $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$);
5039 /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/
5041 | f_arg[pre] opt_args_tail(block_args_tail, none)[tail]
5043 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
5044 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
5046 | tail-only-args(block_args_tail)
5049opt_block_param_def : none
5052 p->command_start = TRUE;
5056block_param_def : '|' opt_block_param opt_bv_decl '|'
5058 p->max_numparam = ORDINAL_PARAM;
5059 p->ctxt.in_argdef = 0;
5061 /*% ripper: block_var!($:2, $:3) %*/
5065opt_block_param : /* none */
5068 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5076 /*% ripper: Qfalse %*/
5078 | '\n'? ';' bv_decls '\n'?
5086 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5088 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5100 $$ = p->max_numparam;
5101 p->max_numparam = 0;
5106 $$ = numparam_push(p);
5116lambda : tLAMBDA[lpar]
5118 token_info_push(p, "->", &@lpar);
5121 max_numparam numparam it_id allow_exits
5128 int max_numparam = p->max_numparam;
5129 ID it_id = p->it_id;
5130 p->lex.lpar_beg = $lpar;
5131 p->max_numparam = $max_numparam;
5133 restore_block_exit(p, $allow_exits);
5135 $args = args_with_numbered(p, $args, max_numparam, it_id);
5137 YYLTYPE loc = code_loc_gen(&@lpar, &@body);
5138 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5139 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5140 nd_set_line($$, @args.end_pos.lineno);
5141 nd_set_first_loc($$, @lpar.beg_pos);
5144 /*% ripper: lambda!($:args, $:body) %*/
5145 numparam_pop(p, $numparam);
5150f_larglist : '(' f_largs[args] opt_bv_decl ')'
5152 p->ctxt.in_argdef = 0;
5154 p->max_numparam = ORDINAL_PARAM;
5155 /*% ripper: paren!($:args) %*/
5159 p->ctxt.in_argdef = 0;
5160 if (!args_info_empty_p(&$args->nd_ainfo))
5161 p->max_numparam = ORDINAL_PARAM;
5166lambda_body : tLAMBEG compstmt(stmts) '}'
5168 token_info_pop(p, "}", &@3);
5169 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5174 push_end_expect_token_locations(p, &@1.beg_pos);
5178 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5183do_block : k_do_block do_body k_end
5186 set_embraced_location($$, &@1, &@3);
5191block_call : command do_block
5193 $$ = command_add_block(p, $1, $2, &@$);
5195 /*% ripper: method_add_block!($:1, $:2) %*/
5197 | block_call call_op2 operation2 opt_paren_args
5199 bool has_args = $4 != 0;
5200 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5201 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5202 /*% ripper: call!($:1, $:2, $:3) %*/
5204 /*% ripper: method_add_arg!($:$, $:4) %*/
5207 | block_call call_op2 operation2 opt_paren_args brace_block
5209 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5210 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5211 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5213 | block_call call_op2 operation2 command_args do_block
5215 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5216 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5218 | block_call call_op2 paren_args
5220 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5221 nd_set_line($$, @2.end_pos.lineno);
5222 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5226method_call : fcall paren_args
5230 nd_set_last_loc($1, @2.end_pos);
5231 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5233 | primary_value call_op operation2 opt_paren_args
5235 bool has_args = $4 != 0;
5236 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5237 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5238 nd_set_line($$, @3.end_pos.lineno);
5239 /*% ripper: call!($:1, $:2, $:3) %*/
5241 /*% ripper: method_add_arg!($:$, $:4) %*/
5244 | primary_value tCOLON2 operation2 paren_args
5246 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5247 nd_set_line($$, @3.end_pos.lineno);
5248 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5250 | primary_value tCOLON2 operation3
5252 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5253 /*% ripper: call!($:1, $:2, $:3) %*/
5255 | primary_value call_op2 paren_args
5257 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5258 nd_set_line($$, @2.end_pos.lineno);
5259 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5261 | keyword_super paren_args
5263 rb_code_location_t lparen_loc = @2;
5264 rb_code_location_t rparen_loc = @2;
5265 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5266 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5268 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5269 /*% ripper: super!($:2) %*/
5273 $$ = NEW_ZSUPER(&@$);
5274 /*% ripper: zsuper! %*/
5276 | primary_value '[' opt_call_args rbracket
5278 $$ = NEW_CALL($1, tAREF, $3, &@$);
5280 /*% ripper: aref!($:1, $:3) %*/
5284brace_block : '{' brace_body '}'
5287 set_embraced_location($$, &@1, &@3);
5290 | k_do do_body k_end
5293 set_embraced_location($$, &@1, &@3);
5298brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5299 max_numparam numparam it_id allow_exits
5300 opt_block_param_def[args] compstmt(stmts)
5302 int max_numparam = p->max_numparam;
5303 ID it_id = p->it_id;
5304 p->max_numparam = $max_numparam;
5306 $args = args_with_numbered(p, $args, max_numparam, it_id);
5307 $$ = NEW_ITER($args, $compstmt, &@$);
5308 /*% ripper: brace_block!($:args, $:compstmt) %*/
5309 restore_block_exit(p, $allow_exits);
5310 numparam_pop(p, $numparam);
5319 max_numparam numparam it_id allow_exits
5320 opt_block_param_def[args] bodystmt
5322 int max_numparam = p->max_numparam;
5323 ID it_id = p->it_id;
5324 p->max_numparam = $max_numparam;
5326 $args = args_with_numbered(p, $args, max_numparam, it_id);
5327 $$ = NEW_ITER($args, $bodystmt, &@$);
5328 /*% ripper: do_block!($:args, $:bodystmt) %*/
5330 restore_block_exit(p, $allow_exits);
5331 numparam_pop(p, $numparam);
5336case_args : arg_value
5338 check_literal_when(p, $arg_value, &@arg_value);
5339 $$ = NEW_LIST($arg_value, &@$);
5340 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5344 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5345 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5347 | case_args[non_last_args] ',' arg_value
5349 check_literal_when(p, $arg_value, &@arg_value);
5350 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5351 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5353 | case_args[non_last_args] ',' tSTAR arg_value
5355 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5356 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5360case_body : k_when case_args then
5364 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5366 /*% ripper: when!($:2, $:4, $:5) %*/
5374p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5375p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5379 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5380 p->command_start = FALSE;
5381 p->ctxt.in_kwarg = 1;
5382 p->ctxt.in_alt_pattern = 0;
5383 p->ctxt.capture_in_pattern = 0;
5387p_case_body : keyword_in
5388 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5389 p_top_expr[expr] then
5391 pop_pktbl(p, $p_pktbl);
5392 pop_pvtbl(p, $p_pvtbl);
5393 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5394 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5395 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5400 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5401 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5409p_top_expr : p_top_expr_body
5410 | p_top_expr_body modifier_if expr_value
5412 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5414 /*% ripper: if_mod!($:3, $:1) %*/
5416 | p_top_expr_body modifier_unless expr_value
5418 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5420 /*% ripper: unless_mod!($:3, $:1) %*/
5424p_top_expr_body : p_expr
5427 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5428 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5429 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5433 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5434 nd_set_first_loc($$, @1.beg_pos);
5435 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5439 $$ = new_find_pattern(p, 0, $1, &@$);
5440 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5444 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5445 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5449 $$ = new_hash_pattern(p, 0, $1, &@$);
5450 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5457p_as : p_expr tASSOC p_variable
5459 NODE *n = NEW_LIST($1, &@$);
5460 n = list_append(p, n, $3);
5461 $$ = new_hash(p, n, &@$);
5462 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5467p_alt : p_alt[left] '|'[alt]
5469 p->ctxt.in_alt_pattern = 1;
5473 if (p->ctxt.capture_in_pattern) {
5474 yyerror1(&@alt, "alternative pattern after variable capture");
5476 p->ctxt.in_alt_pattern = 0;
5477 $$ = NEW_OR($left, $right, &@$, &@alt);
5478 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5483p_lparen : '(' p_pktbl
5490p_lbracket : '[' p_pktbl
5497p_expr_basic : p_value
5499 | p_const p_lparen[p_pktbl] p_args rparen
5501 pop_pktbl(p, $p_pktbl);
5502 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5503 nd_set_first_loc($$, @p_const.beg_pos);
5504 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5506 | p_const p_lparen[p_pktbl] p_find rparen
5508 pop_pktbl(p, $p_pktbl);
5509 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5510 nd_set_first_loc($$, @p_const.beg_pos);
5511 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5513 | p_const p_lparen[p_pktbl] p_kwargs rparen
5515 pop_pktbl(p, $p_pktbl);
5516 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5517 nd_set_first_loc($$, @p_const.beg_pos);
5518 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5520 | p_const '(' rparen
5522 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5523 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5524 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5526 | p_const p_lbracket[p_pktbl] p_args rbracket
5528 pop_pktbl(p, $p_pktbl);
5529 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5530 nd_set_first_loc($$, @p_const.beg_pos);
5531 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5533 | p_const p_lbracket[p_pktbl] p_find rbracket
5535 pop_pktbl(p, $p_pktbl);
5536 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5537 nd_set_first_loc($$, @p_const.beg_pos);
5538 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5540 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5542 pop_pktbl(p, $p_pktbl);
5543 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5544 nd_set_first_loc($$, @p_const.beg_pos);
5545 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5547 | p_const '[' rbracket
5549 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5550 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5551 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5553 | tLBRACK p_args rbracket
5555 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5556 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5558 | tLBRACK p_find rbracket
5560 $$ = new_find_pattern(p, 0, $p_find, &@$);
5561 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5565 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5566 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5567 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5569 | tLBRACE p_pktbl lex_ctxt[ctxt]
5571 p->ctxt.in_kwarg = 0;
5575 pop_pktbl(p, $p_pktbl);
5576 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5577 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5578 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5582 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5583 $$ = new_hash_pattern(p, 0, $$, &@$);
5584 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5586 | tLPAREN p_pktbl p_expr rparen
5588 pop_pktbl(p, $p_pktbl);
5590 /*% ripper: $:p_expr %*/
5596 NODE *pre_args = NEW_LIST($1, &@$);
5597 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5598 /*% ripper: [[$:1], Qnil, Qnil] %*/
5602 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5603 /*% ripper: [$:1, Qnil, Qnil] %*/
5607 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5608 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5610 | p_args_head p_rest
5612 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5613 /*% ripper: [$:1, $:2, Qnil] %*/
5615 | p_args_head p_rest ',' p_args_post
5617 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5618 /*% ripper: [$:1, $:2, $:4] %*/
5623p_args_head : p_arg ','
5624 | p_args_head p_arg ','
5626 $$ = list_concat($1, $2);
5627 /*% ripper: rb_ary_concat($:1, $:2) %*/
5633 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5634 /*% ripper: [Qnil, $:1, Qnil] %*/
5636 | p_rest ',' p_args_post
5638 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5639 /*% ripper: [Qnil, $:1, $:3] %*/
5643p_find : p_rest ',' p_args_post ',' p_rest
5645 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5646 /*% ripper: [$:1, $:3, $:5] %*/
5651p_rest : tSTAR tIDENTIFIER
5653 error_duplicate_pattern_variable(p, $2, &@2);
5654 /*% ripper: var_field!($:2) %*/
5655 $$ = assignable(p, $2, 0, &@$);
5660 /*% ripper: var_field!(Qnil) %*/
5665 | p_args_post ',' p_arg
5667 $$ = list_concat($1, $3);
5668 /*% ripper: rb_ary_concat($:1, $:3) %*/
5674 $$ = NEW_LIST($1, &@$);
5675 /*% ripper: [$:1] %*/
5679p_kwargs : p_kwarg ',' p_any_kwrest
5681 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5682 /*% ripper: [$:1, $:3] %*/
5686 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5687 /*% ripper: [$:1, Qnil] %*/
5691 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5692 /*% ripper: [$:1, Qnil] %*/
5696 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5697 /*% ripper: [[], $:1] %*/
5702 /*% ripper[brace]: [$:1] %*/
5705 $$ = list_concat($1, $3);
5706 /*% ripper: rb_ary_push($:1, $:3) %*/
5710p_kw : p_kw_label p_expr
5712 error_duplicate_pattern_key(p, $1, &@1);
5713 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5714 /*% ripper: [$:1, $:2] %*/
5718 error_duplicate_pattern_key(p, $1, &@1);
5719 if ($1 && !is_local_id($1)) {
5720 yyerror1(&@1, "key must be valid as local variables");
5722 error_duplicate_pattern_variable(p, $1, &@1);
5723 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5724 /*% ripper: [$:1, Qnil] %*/
5729 | tSTRING_BEG string_contents tLABEL_END
5731 YYLTYPE loc = code_loc_gen(&@1, &@3);
5732 if (!$2 || nd_type_p($2, NODE_STR)) {
5733 NODE *node = dsym_node(p, $2, &loc);
5734 $$ = rb_sym2id(rb_node_sym_string_val(node));
5737 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5738 $$ = rb_intern_str(STR_NEW0());
5744p_kwrest : kwrest_mark tIDENTIFIER
5747 /*% ripper: var_field!($:2) %*/
5752 /*% ripper: Qnil %*/
5756p_kwnorest : kwrest_mark keyword_nil
5762p_any_kwrest : p_kwrest
5766 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5770p_value : p_primitive
5771 | range_expr(p_primitive)
5777p_primitive : inline_primary
5780 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5781 /*% ripper: var_ref!($:1) %*/
5786p_variable : tIDENTIFIER
5788 error_duplicate_pattern_variable(p, $1, &@1);
5789 /*% ripper: var_field!($:1) %*/
5790 $$ = assignable(p, $1, 0, &@$);
5794p_var_ref : '^' tIDENTIFIER
5796 NODE *n = gettable(p, $2, &@$);
5800 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5801 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5804 /*% ripper: var_ref!($:2) %*/
5808 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5809 /*% ripper: var_ref!($:2) %*/
5813p_expr_ref : '^' tLPAREN expr_value rparen
5815 $$ = NEW_BLOCK($3, &@$);
5816 /*% ripper: begin!($:3) %*/
5820p_const : tCOLON3 cname
5822 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5823 /*% ripper: top_const_ref!($:2) %*/
5825 | p_const tCOLON2 cname
5827 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5828 /*% ripper: const_path_ref!($:1, $:3) %*/
5832 $$ = gettable(p, $1, &@$);
5833 /*% ripper: var_ref!($:1) %*/
5837opt_rescue : k_rescue exc_list exc_var then
5843 err = NEW_ERRINFO(&@3);
5844 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5846 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5856 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5863 $$ = NEW_LIST($1, &@$);
5864 /*% ripper: rb_ary_new3(1, $:1) %*/
5868 if (!($$ = splat_array($1))) $$ = $1;
5881opt_ensure : k_ensure stmts terms?
5883 p->ctxt.in_rescue = $1.in_rescue;
5885 void_expr(p, void_stmts(p, $$));
5886 /*% ripper: ensure!($:2) %*/
5898 $$ = NEW_STR(STRING_NEW0(), &@$);
5901 $$ = evstr2dstr(p, $1);
5911 $$ = literal_concat(p, $1, $2, &@$);
5912 /*% ripper: string_concat!($:1, $:2) %*/
5916string1 : tSTRING_BEG string_contents tSTRING_END
5918 $$ = heredoc_dedent(p, $2);
5919 if ($$) nd_set_loc($$, &@$);
5921 if (p->heredoc_indent > 0) {
5922 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5923 p->heredoc_indent = 0;
5925 /*% ripper: string_literal!($:$) %*/
5929xstring : tXSTRING_BEG xstring_contents tSTRING_END
5931 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5933 if (p->heredoc_indent > 0) {
5934 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5935 p->heredoc_indent = 0;
5937 /*% ripper: xstring_literal!($:$) %*/
5941regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5943 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5944 /*% ripper: regexp_literal!($:2, $:3) %*/
5948words : words(tWORDS_BEG, word_list)
5951word_list : /* none */
5954 /*% ripper: words_new! %*/
5956 | word_list word ' '+
5958 $$ = list_append(p, $1, evstr2dstr(p, $2));
5959 /*% ripper: words_add!($:1, $:2) %*/
5963word : string_content
5964 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5965 | word string_content
5967 $$ = literal_concat(p, $1, $2, &@$);
5968 /*% ripper: word_add!($:1, $:2) %*/
5972symbols : words(tSYMBOLS_BEG, symbol_list)
5975symbol_list : /* none */
5978 /*% ripper: symbols_new! %*/
5980 | symbol_list word ' '+
5982 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5983 /*% ripper: symbols_add!($:1, $:2) %*/
5987qwords : words(tQWORDS_BEG, qword_list)
5990qsymbols : words(tQSYMBOLS_BEG, qsym_list)
5993qword_list : /* none */
5996 /*% ripper: qwords_new! %*/
5998 | qword_list tSTRING_CONTENT ' '+
6000 $$ = list_append(p, $1, $2);
6001 /*% ripper: qwords_add!($:1, $:2) %*/
6005qsym_list : /* none */
6008 /*% ripper: qsymbols_new! %*/
6010 | qsym_list tSTRING_CONTENT ' '+
6012 $$ = symbol_append(p, $1, $2);
6013 /*% ripper: qsymbols_add!($:1, $:2) %*/
6017string_contents : /* none */
6020 /*% ripper: string_content! %*/
6022 | string_contents string_content
6024 $$ = literal_concat(p, $1, $2, &@$);
6025 /*% ripper: string_add!($:1, $:2) %*/
6029xstring_contents: /* none */
6032 /*% ripper: xstring_new! %*/
6034 | xstring_contents string_content
6036 $$ = literal_concat(p, $1, $2, &@$);
6037 /*% ripper: xstring_add!($:1, $:2) %*/
6041regexp_contents : /* none */
6044 /*% ripper: regexp_new! %*/
6046 | regexp_contents string_content
6048 NODE *head = $1, *tail = $2;
6056 switch (nd_type(head)) {
6058 head = str2dstr(p, head);
6063 head = list_append(p, NEW_DSTR(0, &@$), head);
6066 $$ = list_append(p, head, tail);
6068 /*% ripper: regexp_add!($:1, $:2) %*/
6072string_content : tSTRING_CONTENT[content]
6073 /*% ripper[brace]: $:content %*/
6074 | tSTRING_DVAR[state]
6076 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6077 $$ = p->lex.strterm;
6079 SET_LEX_STATE(EXPR_BEG);
6083 p->lex.strterm = $strterm;
6084 $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC);
6085 nd_set_line($$, @dvar.end_pos.lineno);
6086 /*% ripper: string_dvar!($:dvar) %*/
6088 | tSTRING_DBEG[state]
6092 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6093 $$ = p->lex.strterm;
6095 SET_LEX_STATE(EXPR_BEG);
6098 $$ = p->lex.brace_nest;
6099 p->lex.brace_nest = 0;
6102 $$ = p->lex.lpar_beg;
6103 p->lex.lpar_beg = -1;
6106 $$ = p->heredoc_indent;
6107 p->heredoc_indent = 0;
6109 compstmt(stmts) string_dend
6113 p->lex.strterm = $term;
6114 SET_LEX_STATE($state);
6115 p->lex.brace_nest = $brace;
6116 p->lex.lpar_beg = $lpar;
6117 p->heredoc_indent = $indent;
6118 p->heredoc_line_indent = -1;
6119 if ($compstmt) nd_unset_fl_newline($compstmt);
6120 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6121 /*% ripper: string_embexpr!($:compstmt) %*/
6125string_dend : tSTRING_DEND
6129string_dvar : nonlocal_var
6131 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6132 /*% ripper: var_ref!($:1) %*/
6143 SET_LEX_STATE(EXPR_END);
6144 VALUE str = rb_id2str($2);
6147 * set_yylval_noname sets invalid id to yylval.
6148 * This branch can be removed once yylval is changed to
6149 * hold lexed string.
6151 if (!str) str = STR_NEW0();
6152 $$ = NEW_SYM(str, &@$);
6153 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6161dsym : tSYMBEG string_contents tSTRING_END
6163 SET_LEX_STATE(EXPR_END);
6164 $$ = dsym_node(p, $2, &@$);
6165 /*% ripper: dyna_symbol!($:2) %*/
6169numeric : simple_numeric
6170 | tUMINUS_NUM simple_numeric %prec tLOWEST
6173 negate_lit(p, $$, &@$);
6174 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6178simple_numeric : tINTEGER
6189user_variable : ident_or_const
6193keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6194 | keyword_self {$$ = KWD2EID(self, $1);}
6195 | keyword_true {$$ = KWD2EID(true, $1);}
6196 | keyword_false {$$ = KWD2EID(false, $1);}
6197 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6198 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6199 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6202var_ref : user_variable
6204 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6205 if (ifdef_ripper(id_is_var(p, $1), false)) {
6206 /*% ripper: var_ref!($:1) %*/
6209 /*% ripper: vcall!($:1) %*/
6214 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6215 /*% ripper: var_ref!($:1) %*/
6219var_lhs : user_or_keyword_variable
6221 /*% ripper: var_field!($:1) %*/
6222 $$ = assignable(p, $1, 0, &@$);
6232 SET_LEX_STATE(EXPR_BEG);
6233 p->command_start = TRUE;
6243f_opt_paren_args: f_paren_args
6246 p->ctxt.in_argdef = 0;
6250f_empty_arg : /* none */
6252 $$ = new_empty_args_tail(p, &@$);
6253 $$ = new_args(p, 0, 0, 0, 0, $$, &@$);
6254 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6258f_paren_args : '(' f_args rparen
6261 /*% ripper: paren!($:2) %*/
6262 SET_LEX_STATE(EXPR_BEG);
6263 p->command_start = TRUE;
6264 p->ctxt.in_argdef = 0;
6268f_arglist : f_paren_args
6271 p->ctxt.in_kwarg = 1;
6272 p->ctxt.in_argdef = 1;
6273 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6277 p->ctxt.in_kwarg = $1.in_kwarg;
6278 p->ctxt.in_argdef = 0;
6280 SET_LEX_STATE(EXPR_BEG);
6281 p->command_start = TRUE;
6286args_tail : args_tail_basic(arg_value, opt_comma)
6289 add_forwarding_args(p);
6290 $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward);
6291 $$->nd_ainfo.forwarding = 1;
6292 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6296largs_tail : args_tail_basic(arg_value, none)
6299 yyerror1(&@args_forward, "unexpected ... in lambda argument");
6300 $$ = new_args_tail(p, 0, 0, 0, &@args_forward);
6301 $$->nd_ainfo.forwarding = 1;
6302 /*% ripper: [Qnil, $:args_forward, Qnil] %*/
6306%rule args-list(value, tail) <node_args>
6307 : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6309 $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$);
6310 /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6312 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6314 $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$);
6315 /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6317 | f_arg[pre] ',' f_opt_arg(value)[opt] tail
6319 $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$);
6320 /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6322 | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail
6324 $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$);
6325 /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6327 | f_arg[pre] ',' f_rest_arg[rest] tail
6329 $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$);
6330 /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6332 | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail
6334 $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$);
6335 /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6337 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail
6339 $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$);
6340 /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/
6342 | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail
6344 $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$);
6345 /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/
6347 | f_opt_arg(value)[opt] tail
6349 $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$);
6350 /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/
6352 | f_opt_arg(value)[opt] ',' f_arg[post] tail
6354 $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$);
6355 /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/
6357 | f_rest_arg[rest] tail
6359 $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$);
6360 /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/
6362 | f_rest_arg[rest] ',' f_arg[post] tail
6364 $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$);
6365 /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/
6369%rule tail-only-args(tail) <node_args>
6372 $$ = new_args(p, 0, 0, 0, 0, $tail, &@$);
6373 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6377%rule f_args-list(tail, trailing) <node_args>
6378 : args-list(arg_value, opt_args_tail(tail, trailing))
6379 | f_arg[pre] opt_args_tail(tail, trailing)[tail]
6381 $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$);
6382 /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/
6384 | tail-only-args(tail)
6388f_args : f_args-list(args_tail, opt_comma)
6391f_largs : f_args-list(largs_tail, none)
6394args_forward : tBDOT3
6397 /*% ripper: args_forward! %*/
6401f_bad_arg : tCONSTANT
6403 static const char mesg[] = "formal argument cannot be a constant";
6405 yyerror1(&@1, mesg);
6408 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6412 static const char mesg[] = "formal argument cannot be an instance variable";
6414 yyerror1(&@1, mesg);
6417 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6421 static const char mesg[] = "formal argument cannot be a global variable";
6423 yyerror1(&@1, mesg);
6426 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6430 static const char mesg[] = "formal argument cannot be a class variable";
6432 yyerror1(&@1, mesg);
6435 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6439f_norm_arg : f_bad_arg
6442 VALUE e = formal_argument_error(p, $$ = $1);
6444 /*% ripper[error]: param_error!(?e, $:1) %*/
6446 p->max_numparam = ORDINAL_PARAM;
6450f_arg_asgn : f_norm_arg
6457f_arg_item : f_arg_asgn
6459 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6462 | tLPAREN f_margs rparen
6464 ID tid = internal_id(p);
6466 loc.beg_pos = @2.beg_pos;
6467 loc.end_pos = @2.beg_pos;
6469 if (dyna_in_block(p)) {
6470 $2->nd_value = NEW_DVAR(tid, &loc);
6473 $2->nd_value = NEW_LVAR(tid, &loc);
6475 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6476 $$->nd_next = (NODE *)$2;
6477 /*% ripper: mlhs_paren!($:2) %*/
6482 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6483 | f_arg ',' f_arg_item
6487 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6488 rb_discard_node(p, (NODE *)$3);
6489 /*% ripper: rb_ary_push($:1, $:3) %*/
6496 VALUE e = formal_argument_error(p, $$ = $1);
6499 /*% ripper[error]: param_error!(?e, $:1) %*/
6502 * Workaround for Prism::ParseTest#test_filepath for
6503 * "unparser/corpus/literal/def.txt"
6505 * See the discussion on https://github.com/ruby/ruby/pull/9923
6507 arg_var(p, ifdef_ripper(0, $1));
6509 p->max_numparam = ORDINAL_PARAM;
6510 p->ctxt.in_argdef = 0;
6518f_no_kwarg : p_kwnorest
6520 /*% ripper: nokw_param!(Qnil) %*/
6524f_kwrest : kwrest_mark tIDENTIFIER
6526 arg_var(p, shadowing_lvar(p, $2));
6528 /*% ripper: kwrest_param!($:2) %*/
6532 arg_var(p, idFWD_KWREST);
6534 /*% ripper: kwrest_param!(Qnil) %*/
6542f_rest_arg : restarg_mark tIDENTIFIER
6544 arg_var(p, shadowing_lvar(p, $2));
6546 /*% ripper: rest_param!($:2) %*/
6550 arg_var(p, idFWD_REST);
6552 /*% ripper: rest_param!(Qnil) %*/
6560f_block_arg : blkarg_mark tIDENTIFIER
6562 arg_var(p, shadowing_lvar(p, $2));
6564 /*% ripper: blockarg!($:2) %*/
6566 | blkarg_mark keyword_nil
6569 /*% ripper: blockarg!(ID2VAL(idNil)) %*/
6573 arg_var(p, idFWD_BLOCK);
6575 /*% ripper: blockarg!(Qnil) %*/
6582 /*% ripper: Qnil %*/
6587singleton : value_expr(singleton_expr)
6589 NODE *expr = last_expr_node($1);
6590 switch (nd_type(expr)) {
6604 case NODE_IMAGINARY:
6608 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6617singleton_expr : var_ref
6620 SET_LEX_STATE(EXPR_BEG);
6621 p->ctxt.in_argdef = 0;
6625 p->ctxt.in_argdef = 1;
6627 /*% ripper: paren!($:3) %*/
6635 /*% ripper: assoclist_from_args!($:1) %*/
6640 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6649 NODE *n = RNODE_LIST(tail)->nd_next;
6650 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6651 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6653 tail = RNODE_HASH(n)->nd_head;
6656 assocs = list_concat(assocs, tail);
6660 /*% ripper: rb_ary_push($:1, $:3) %*/
6664assoc : arg_value tASSOC arg_value
6666 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6667 /*% ripper: assoc_new!($:1, $:3) %*/
6671 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6672 /*% ripper: assoc_new!($:1, $:2) %*/
6676 NODE *val = gettable(p, $1, &@$);
6677 if (!val) val = NEW_ERROR(&@$);
6678 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6679 /*% ripper: assoc_new!($:1, Qnil) %*/
6681 | tSTRING_BEG string_contents tLABEL_END arg_value
6683 YYLTYPE loc = code_loc_gen(&@1, &@3);
6684 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6685 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6689 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6690 /*% ripper: assoc_splat!($:2) %*/
6694 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6695 $$ = list_append(p, NEW_LIST(0, &@$),
6696 NEW_LVAR(idFWD_KWREST, &@$));
6697 /*% ripper: assoc_splat!(Qnil) %*/
6701%rule %inline operation : ident_or_const
6705operation2 : operation
6709operation3 : tIDENTIFIER
6743 if (p->ctxt.in_defined) {
6744 p->ctxt.has_trailing_semicolon = 1;
6749 @$.end_pos = @$.beg_pos;
6755 | terms ';' {yyerrok;}
6761 /*% ripper: Qnil %*/
6768# define yylval (*p->lval)
6770static int regx_options(struct parser_params*);
6771static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6772static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6773static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6774static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6776#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6778# define set_yylval_node(x) { \
6780 rb_parser_set_location(p, &_cur_loc); \
6781 yylval.node = (x); \
6782 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6784# define set_yylval_str(x) \
6786 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6787 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6789# define set_yylval_num(x) { \
6791 set_parser_s_value(x); \
6793# define set_yylval_id(x) (yylval.id = (x))
6794# define set_yylval_name(x) { \
6795 (yylval.id = (x)); \
6796 set_parser_s_value(ID2SYM(x)); \
6798# define yylval_id() (yylval.id)
6800#define set_yylval_noname() set_yylval_id(keyword_nil)
6801#define has_delayed_token(p) (p->delayed.token != NULL)
6804#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6805#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6808parser_has_token(struct parser_params *p)
6810 const char *const pcur = p->lex.pcur;
6811 const char *const ptok = p->lex.ptok;
6812 if (p->keep_tokens && (pcur < ptok)) {
6813 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6814 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6823 case '"': return "\\\"";
6824 case '\\': return "\\\\";
6825 case '\0': return "\\0";
6826 case '\n': return "\\n";
6827 case '\r': return "\\r";
6828 case '\t': return "\\t";
6829 case '\f': return "\\f";
6830 case '\013': return "\\v";
6831 case '\010': return "\\b";
6832 case '\007': return "\\a";
6833 case '\033': return "\\e";
6834 case '\x7f': return "\\c?";
6839static rb_parser_string_t *
6840rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6842 rb_encoding *enc = p->enc;
6843 const char *ptr = str->ptr;
6844 const char *pend = ptr + str->len;
6845 const char *prev = ptr;
6846 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6847 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6849 while (ptr < pend) {
6852 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6853 if (!MBCLEN_CHARFOUND_P(n)) {
6854 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6855 n = rb_enc_mbminlen(enc);
6857 n = (int)(pend - ptr);
6859 c = ((unsigned char)*ptr >> 4) & 0x0f;
6860 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6862 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6863 parser_str_cat(result, charbuf, 4);
6868 n = MBCLEN_CHARFOUND_LEN(n);
6869 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6871 cc = escaped_char(c);
6873 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6874 parser_str_cat_cstr(result, cc);
6877 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6880 if (ptr - n > prev) {
6881 parser_str_cat(result, prev, ptr - n - prev);
6884 parser_str_cat(result, prev, ptr - prev);
6888 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6894parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6896 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6897 token->id = p->token_id;
6898 token->type_name = parser_token2char(p, t);
6900 token->loc.beg_pos = p->yylloc->beg_pos;
6901 token->loc.end_pos = p->yylloc->end_pos;
6902 rb_parser_ary_push_ast_token(p, p->tokens, token);
6906 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6907 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6908 line, token->id, token->type_name, str_escaped->ptr,
6909 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6910 token->loc.end_pos.lineno, token->loc.end_pos.column);
6911 rb_parser_string_free(p, str_escaped);
6916parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6918 debug_token_line(p, "parser_dispatch_scan_event", line);
6920 if (!parser_has_token(p)) return;
6922 RUBY_SET_YYLLOC(*p->yylloc);
6924 if (p->keep_tokens) {
6925 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6926 parser_append_tokens(p, str, t, line);
6932#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6934parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6936 debug_token_line(p, "parser_dispatch_delayed_token", line);
6938 if (!has_delayed_token(p)) return;
6940 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6942 if (p->keep_tokens) {
6943 /* p->delayed.token is freed by rb_parser_tokens_free */
6944 parser_append_tokens(p, p->delayed.token, t, line);
6947 rb_parser_string_free(p, p->delayed.token);
6950 p->delayed.token = NULL;
6953#define literal_flush(p, ptr) ((void)(ptr))
6956ripper_has_scan_event(struct parser_params *p)
6958 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6959 return p->lex.pcur > p->lex.ptok;
6963ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6965 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6966 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6967 RUBY_SET_YYLLOC(*p->yylloc);
6973ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6975 if (!ripper_has_scan_event(p)) return;
6977 set_parser_s_value(ripper_scan_event_val(p, t));
6979#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6982ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6984 /* save and adjust the location to delayed token for callbacks */
6985 int saved_line = p->ruby_sourceline;
6986 const char *saved_tokp = p->lex.ptok;
6989 if (!has_delayed_token(p)) return;
6990 p->ruby_sourceline = p->delayed.beg_line;
6991 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6992 str = rb_str_new_mutable_parser_string(p->delayed.token);
6993 rb_parser_string_free(p, p->delayed.token);
6994 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6995 set_parser_s_value(s_value);
6996 p->delayed.token = NULL;
6997 p->ruby_sourceline = saved_line;
6998 p->lex.ptok = saved_tokp;
7000#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7004is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7006 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7010peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
7012 const char *ptr = p->lex.pcur + at;
7013 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
7014 if (memcmp(ptr, str, len)) return false;
7015 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
7017 case '!': case '?': return false;
7019 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
7023parser_is_identchar(struct parser_params *p)
7025 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7029parser_isascii(struct parser_params *p)
7031 return ISASCII(*(p->lex.pcur-1));
7035token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7037 int column = 1, nonspc = 0, i;
7038 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7040 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7043 if (*ptr != ' ' && *ptr != '\t') {
7048 ptinfo->beg = loc->beg_pos;
7049 ptinfo->indent = column;
7050 ptinfo->nonspc = nonspc;
7054token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7058 if (!p->token_info_enabled) return;
7059 ptinfo = ALLOC(token_info);
7060 ptinfo->token = token;
7061 ptinfo->next = p->token_info;
7062 token_info_setup(ptinfo, p->lex.pbeg, loc);
7064 p->token_info = ptinfo;
7068token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7070 token_info *ptinfo_beg = p->token_info;
7072 if (!ptinfo_beg) return;
7074 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7075 token_info_warn(p, token, ptinfo_beg, 1, loc);
7077 p->token_info = ptinfo_beg->next;
7078 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7082token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7084 token_info *ptinfo_beg = p->token_info;
7086 if (!ptinfo_beg) return;
7087 p->token_info = ptinfo_beg->next;
7089 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7090 ptinfo_beg->beg.column != beg_pos.column ||
7091 strcmp(ptinfo_beg->token, token)) {
7092 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7093 beg_pos.lineno, beg_pos.column, token,
7094 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7098 ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg));
7102token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7104 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7105 if (!p->token_info_enabled) return;
7106 if (!ptinfo_beg) return;
7107 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7108 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7109 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7110 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7111 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7112 rb_warn3L(ptinfo_end->beg.lineno,
7113 "mismatched indentations at '%s' with '%s' at %d",
7114 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7118parser_precise_mbclen(struct parser_params *p, const char *ptr)
7120 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7121 if (!MBCLEN_CHARFOUND_P(len)) {
7122 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7130parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7132 rb_parser_string_t *str;
7133 int lineno = p->ruby_sourceline;
7137 else if (yylloc->beg_pos.lineno == lineno) {
7138 str = p->lex.lastline;
7143 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7147parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7153 yylloc = RUBY_SET_YYLLOC(current);
7155 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7156 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7160 parser_compile_error(p, yylloc, "%s", msg);
7161 parser_show_error_line(p, yylloc);
7166parser_yyerror0(struct parser_params *p, const char *msg)
7169 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7173ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7176 const int max_line_margin = 30;
7177 const char *ptr, *ptr_end, *pt, *pb;
7178 const char *pre = "", *post = "", *pend;
7179 const char *code = "", *caret = "";
7181 const char *const pbeg = PARSER_STRING_PTR(str);
7186 if (!yylloc) return;
7187 pend = rb_parser_string_end(str);
7188 if (pend > pbeg && pend[-1] == '\n') {
7189 if (--pend > pbeg && pend[-1] == '\r') --pend;
7193 if (lineno == yylloc->end_pos.lineno &&
7194 (pend - pbeg) > yylloc->end_pos.column) {
7195 pt = pbeg + yylloc->end_pos.column;
7199 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7200 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7202 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7203 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7205 len = ptr_end - ptr;
7208 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7209 if (ptr > pbeg) pre = "...";
7211 if (ptr_end < pend) {
7212 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7213 if (ptr_end < pend) post = "...";
7217 if (lineno == yylloc->beg_pos.lineno) {
7218 pb += yylloc->beg_pos.column;
7219 if (pb > pt) pb = pt;
7221 if (pb < ptr) pb = ptr;
7222 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7225 if (RTEST(errbuf)) {
7226 mesg = rb_attr_get(errbuf, idMesg);
7227 if (char_at_end(p, mesg, '\n') != '\n')
7228 rb_str_cat_cstr(mesg, "\n");
7231 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7233 if (!errbuf && rb_stderr_tty_p()) {
7234#define CSI_BEGIN "\033["
7237 CSI_BEGIN""CSI_SGR"%s" /* pre */
7238 CSI_BEGIN"1"CSI_SGR"%.*s"
7239 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7240 CSI_BEGIN";1"CSI_SGR"%.*s"
7241 CSI_BEGIN""CSI_SGR"%s" /* post */
7244 (int)(pb - ptr), ptr,
7246 (int)(ptr_end - pt), pt,
7252 len = ptr_end - ptr;
7253 lim = pt < pend ? pt : pend;
7254 i = (int)(lim - ptr);
7255 buf = ALLOCA_N(char, i+2);
7260 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7266 memset(p2, '~', (lim - ptr));
7270 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7271 pre, (int)len, code, post,
7274 if (!errbuf) rb_write_error_str(mesg);
7279parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7281 const char *pcur = 0, *ptok = 0;
7282 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7283 p->ruby_sourceline == yylloc->end_pos.lineno) {
7286 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7287 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7289 parser_yyerror0(p, msg);
7298parser_yyerror0(struct parser_params *p, const char *msg)
7300 dispatch1(parse_error, STR_NEW2(msg));
7306parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7312vtable_size(const struct vtable *tbl)
7314 if (!DVARS_TERMINAL_P(tbl)) {
7322static struct vtable *
7323vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7325 struct vtable *tbl = ALLOC(struct vtable);
7328 tbl->tbl = ALLOC_N(ID, tbl->capa);
7332 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7337#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7340vtable_free_gen(struct parser_params *p, int line, const char *name,
7345 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7348 if (!DVARS_TERMINAL_P(tbl)) {
7350 ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID));
7352 ruby_xfree_sized(tbl, sizeof(*tbl));
7355#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7358vtable_add_gen(struct parser_params *p, int line, const char *name,
7359 struct vtable *tbl, ID id)
7363 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7364 line, name, (void *)tbl, rb_id2name(id));
7367 if (DVARS_TERMINAL_P(tbl)) {
7368 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7371 if (tbl->pos == tbl->capa) {
7372 tbl->capa = tbl->capa * 2;
7373 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7375 tbl->tbl[tbl->pos++] = id;
7377#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7380vtable_pop_gen(struct parser_params *p, int line, const char *name,
7381 struct vtable *tbl, int n)
7384 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7385 line, name, (void *)tbl, n);
7388 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7393#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7396vtable_included(const struct vtable * tbl, ID id)
7400 if (!DVARS_TERMINAL_P(tbl)) {
7401 for (i = 0; i < tbl->pos; i++) {
7402 if (tbl->tbl[i] == id) {
7410static void parser_prepare(struct parser_params *p);
7413e_option_supplied(struct parser_params *p)
7415 return strcmp(p->ruby_sourcefile, "-e") == 0;
7419static NODE *parser_append_options(struct parser_params *p, NODE *node);
7422yycompile0(VALUE arg)
7426 struct parser_params *p = (struct parser_params *)arg;
7429 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7433 if (p->debug_lines) {
7434 p->ast->body.script_lines = p->debug_lines;
7438#define RUBY_DTRACE_PARSE_HOOK(name) \
7439 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7440 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7442 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7444 RUBY_DTRACE_PARSE_HOOK(END);
7448 xfree(p->lex.strterm);
7450 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7451 if (n || p->error_p) {
7452 VALUE mesg = p->error_buffer;
7454 mesg = syntax_error_new();
7456 if (!p->error_tolerant) {
7457 rb_set_errinfo(mesg);
7461 tree = p->eval_tree;
7463 tree = NEW_NIL(&NULL_LOC);
7466 rb_parser_ary_t *tokens = p->tokens;
7468 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7469 prelude = block_append(p, p->eval_tree_begin, body);
7470 RNODE_SCOPE(tree)->nd_body = prelude;
7471 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7472 p->ast->body.coverage_enabled = cov;
7473 if (p->keep_tokens) {
7474 p->ast->node_buffer->tokens = tokens;
7478 p->ast->body.root = tree;
7479 p->ast->body.line_count = p->line_count;
7484yycompile(struct parser_params *p, VALUE fname, int line)
7488 p->ruby_sourcefile_string = Qnil;
7489 p->ruby_sourcefile = "(none)";
7492 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7493 p->ruby_sourcefile = StringValueCStr(fname);
7495 p->ruby_sourceline = line - 1;
7499 p->ast = ast = rb_ast_new();
7500 compile_callback(yycompile0, (VALUE)p);
7501 ast->body.source_hash = rb_source_hash_finalize(&p->source_hash);
7502 ast->body.has_source_hash = 1;
7514must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7516 rb_encoding *enc = rb_parser_str_get_encoding(s);
7517 if (!rb_enc_asciicompat(enc)) {
7518 rb_raise(rb_eArgError, "invalid source encoding");
7523static rb_parser_string_t *
7524lex_getline(struct parser_params *p)
7526 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7527 if (!line) return 0;
7529 rb_source_hash_update(&p->source_hash, (const uint8_t *)line->ptr, (size_t)line->len);
7530 string_buffer_append(p, line);
7531 must_be_ascii_compatible(p, line);
7537rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7540 p->lex.input = input;
7541 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7543 return yycompile(p, fname, line);
7547#define STR_FUNC_ESCAPE 0x01
7548#define STR_FUNC_EXPAND 0x02
7549#define STR_FUNC_REGEXP 0x04
7550#define STR_FUNC_QWORDS 0x08
7551#define STR_FUNC_SYMBOL 0x10
7552#define STR_FUNC_INDENT 0x20
7553#define STR_FUNC_LABEL 0x40
7554#define STR_FUNC_LIST 0x4000
7555#define STR_FUNC_TERM 0x8000
7558 str_label = STR_FUNC_LABEL,
7560 str_dquote = (STR_FUNC_EXPAND),
7561 str_xquote = (STR_FUNC_EXPAND),
7562 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7563 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7564 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7565 str_ssym = (STR_FUNC_SYMBOL),
7566 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7569static rb_parser_string_t *
7570parser_str_new(struct parser_params *p, const char *ptr, rb_len_t len, rb_encoding *enc, int func, rb_encoding *enc0)
7572 rb_parser_string_t *pstr;
7574 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7576 if (!(func & STR_FUNC_REGEXP)) {
7577 if (rb_parser_is_ascii_string(p, pstr)) {
7579 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7580 /* everything is valid in ASCII-8BIT */
7581 enc = rb_ascii8bit_encoding();
7582 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7590strterm_is_heredoc(rb_strterm_t *strterm)
7592 return strterm->heredoc;
7595static rb_strterm_t *
7596new_strterm(struct parser_params *p, int func, int term, int paren)
7598 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7599 strterm->u.literal.func = func;
7600 strterm->u.literal.term = term;
7601 strterm->u.literal.paren = paren;
7605static rb_strterm_t *
7606new_heredoc(struct parser_params *p)
7608 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7609 strterm->heredoc = true;
7613#define peek(p,c) peek_n(p, (c), 0)
7614#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7615#define peekc(p) peekc_n(p, 0)
7616#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7618#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7620parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7622 debug_token_line(p, "add_delayed_token", line);
7625 if (has_delayed_token(p)) {
7626 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7627 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7628 int end_col = (next_line ? 0 : p->delayed.end_col);
7629 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7630 dispatch_delayed_token(p, tSTRING_CONTENT);
7633 if (!has_delayed_token(p)) {
7634 p->delayed.token = rb_parser_string_new(p, 0, 0);
7635 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7636 p->delayed.beg_line = p->ruby_sourceline;
7637 p->delayed.beg_col = rb_len2int(tok - p->lex.pbeg);
7639 parser_str_cat(p->delayed.token, tok, end - tok);
7640 p->delayed.end_line = p->ruby_sourceline;
7641 p->delayed.end_col = rb_len2int(end - p->lex.pbeg);
7647set_lastline(struct parser_params *p, rb_parser_string_t *str)
7649 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7650 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7651 p->lex.lastline = str;
7655nextline(struct parser_params *p, int set_encoding)
7657 rb_parser_string_t *str = p->lex.nextline;
7658 p->lex.nextline = 0;
7663 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7667 if (!p->lex.input || !(str = lex_getline(p))) {
7674 if (p->debug_lines) {
7675 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7676 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7677 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7682 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7683 /* after here-document without terminator */
7686 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7687 if (p->heredoc_end > 0) {
7688 p->ruby_sourceline = p->heredoc_end;
7691 p->ruby_sourceline++;
7692 set_lastline(p, str);
7698parser_cr(struct parser_params *p, int c)
7700 if (peek(p, '\n')) {
7708nextc0(struct parser_params *p, int set_encoding)
7712 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7713 if (nextline(p, set_encoding)) return -1;
7715 c = (unsigned char)*p->lex.pcur++;
7716 if (UNLIKELY(c == '\r')) {
7717 c = parser_cr(p, c);
7722#define nextc(p) nextc0(p, TRUE)
7725pushback(struct parser_params *p, int c)
7727 if (c == -1) return;
7730 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7735#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7737#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7738#define tok(p) (p)->tokenbuf
7739#define toklen(p) (p)->tokidx
7742looking_at_eol_p(struct parser_params *p)
7744 const char *ptr = p->lex.pcur;
7745 while (!lex_eol_ptr_p(p, ptr)) {
7746 int c = (unsigned char)*ptr++;
7747 int eol = (c == '\n' || c == '#');
7748 if (eol || !ISSPACE(c)) {
7756newtok(struct parser_params *p)
7761 p->tokenbuf = ALLOC_N(char, 60);
7763 if (p->toksiz > 4096) {
7765 REALLOC_N(p->tokenbuf, char, 60);
7771tokspace(struct parser_params *p, int n)
7775 if (p->tokidx >= p->toksiz) {
7776 do {p->toksiz *= 2;} while (p->toksiz <= p->tokidx);
7777 REALLOC_N(p->tokenbuf, char, p->toksiz);
7779 return &p->tokenbuf[p->tokidx-n];
7783tokadd(struct parser_params *p, int c)
7785 p->tokenbuf[p->tokidx++] = (char)c;
7786 if (p->tokidx >= p->toksiz) {
7788 REALLOC_N(p->tokenbuf, char, p->toksiz);
7793tok_hex(struct parser_params *p, size_t *numlen)
7797 c = (int)ruby_scan_digits(p->lex.pcur, 2, 16, numlen, &c);
7799 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7800 yyerror0("invalid hex escape");
7801 dispatch_scan_event(p, tSTRING_CONTENT);
7804 p->lex.pcur += *numlen;
7808#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7811escaped_control_code(int c)
7837#define WARN_SPACE_CHAR(c, prefix) \
7838 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7841tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7842 int regexp_literal, const char *begin)
7844 const int wide = !begin;
7845 size_t numlen = wide ? p->lex.pend - p->lex.pcur : 4;
7846 int ov, codepoint = (int)ruby_scan_digits(p->lex.pcur, numlen, 16, &numlen, &ov);
7848 p->lex.pcur += numlen;
7849 if (p->lex.strterm == NULL ||
7850 strterm_is_heredoc(p->lex.strterm) ||
7851 (p->lex.strterm->u.literal.func != str_regexp)) {
7852 if (!begin) begin = p->lex.pcur;
7853 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7854 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7855 yyerror0("invalid Unicode escape");
7856 dispatch_scan_event(p, tSTRING_CONTENT);
7857 return wide && numlen > 0;
7859 if (codepoint > 0x10ffff) {
7860 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7861 yyerror0("invalid Unicode codepoint (too large)");
7862 dispatch_scan_event(p, tSTRING_CONTENT);
7865 if ((codepoint & 0xfffff800) == 0xd800) {
7866 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7867 yyerror0("invalid Unicode codepoint");
7868 dispatch_scan_event(p, tSTRING_CONTENT);
7872 if (regexp_literal) {
7873 tokcopy(p, (int)numlen);
7875 else if (codepoint >= 0x80) {
7876 rb_encoding *utf8 = rb_utf8_encoding();
7877 if (*encp && utf8 != *encp) {
7878 YYLTYPE loc = RUBY_INIT_YYLLOC();
7879 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7880 parser_show_error_line(p, &loc);
7884 tokaddmbc(p, codepoint, *encp);
7887 tokadd(p, codepoint);
7892static int tokadd_mbchar(struct parser_params *p, int c);
7895tokskip_mbchar(struct parser_params *p)
7897 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7899 p->lex.pcur += len - 1;
7904/* return value is for ?\u3042 */
7906tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7907 int term, int symbol_literal, int regexp_literal)
7910 * If `term` is not -1, then we allow multiple codepoints in \u{}
7911 * upto `term` byte, otherwise we're parsing a character literal.
7912 * And then add the codepoints to the current token.
7914 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7916 const int open_brace = '{', close_brace = '}';
7918 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7920 if (peek(p, open_brace)) { /* handle \u{...} form */
7921 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7923 * Skip parsing validation code and copy bytes as-is until term or
7924 * closing brace, in order to correctly handle extended regexps where
7925 * invalid unicode escapes are allowed in comments. The regexp parser
7926 * does its own validation and will catch any issues.
7928 tokadd(p, open_brace);
7930 while (!lex_eol_p(p)) {
7932 if (c == close_brace) {
7937 else if (c == term) {
7940 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7945 tokadd_mbchar(p, c);
7949 const char *second = NULL;
7950 int c, last = nextc(p);
7951 if (lex_eol_p(p)) goto unterminated;
7952 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7953 while (c != close_brace) {
7954 if (c == term) goto unterminated;
7955 if (second == multiple_codepoints)
7956 second = p->lex.pcur;
7957 if (regexp_literal) tokadd(p, last);
7958 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7961 while (ISSPACE(c = peekc(p))) {
7962 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7965 if (term == -1 && !second)
7966 second = multiple_codepoints;
7969 if (c != close_brace) {
7971 flush_string_content(p, rb_utf8_encoding(), 0);
7972 yyerror0("unterminated Unicode escape");
7973 dispatch_scan_event(p, tSTRING_CONTENT);
7976 if (second && second != multiple_codepoints) {
7977 const char *pcur = p->lex.pcur;
7978 p->lex.pcur = second;
7979 dispatch_scan_event(p, tSTRING_CONTENT);
7982 yyerror0(multiple_codepoints);
7986 if (regexp_literal) tokadd(p, close_brace);
7990 else { /* handle \uxxxx form */
7991 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7998#define ESCAPE_CONTROL 1
7999#define ESCAPE_META 2
8002read_escape(struct parser_params *p, int flags, const char *begin)
8007 switch (c = nextc(p)) {
8008 case '\\': /* Backslash */
8011 case 'n': /* newline */
8014 case 't': /* horizontal tab */
8017 case 'r': /* carriage-return */
8020 case 'f': /* form-feed */
8023 case 'v': /* vertical tab */
8026 case 'a': /* alarm(bell) */
8029 case 'e': /* escape */
8032 case '0': case '1': case '2': case '3': /* octal constant */
8033 case '4': case '5': case '6': case '7':
8036 c = (int)ruby_scan_digits(p->lex.pcur, 3, 8, &numlen, &ov);
8037 p->lex.pcur += numlen;
8040 case 'x': /* hex constant */
8041 c = tok_hex(p, &numlen);
8042 if (numlen == 0) return 0;
8045 case 'b': /* backspace */
8048 case 's': /* space */
8052 if (flags & ESCAPE_META) goto eof;
8053 if ((c = nextc(p)) != '-') {
8056 if ((c = nextc(p)) == '\\') {
8062 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8064 else if (c == -1) goto eof;
8065 else if (!ISASCII(c)) {
8070 int c2 = escaped_control_code(c);
8072 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8073 WARN_SPACE_CHAR(c2, "\\M-");
8076 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8079 else if (ISCNTRL(c)) goto eof;
8080 return ((c & 0xff) | 0x80);
8084 if ((c = nextc(p)) != '-') {
8088 if (flags & ESCAPE_CONTROL) goto eof;
8089 if ((c = nextc(p))== '\\') {
8095 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8099 else if (c == -1) goto eof;
8100 else if (!ISASCII(c)) {
8105 int c2 = escaped_control_code(c);
8108 if (flags & ESCAPE_META) {
8109 WARN_SPACE_CHAR(c2, "\\M-");
8112 WARN_SPACE_CHAR(c2, "");
8116 if (flags & ESCAPE_META) {
8117 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8120 WARN_SPACE_CHAR(c2, "\\C-");
8124 else if (ISCNTRL(c)) goto eof;
8130 flush_string_content(p, p->enc, p->lex.pcur - begin);
8131 yyerror0("Invalid escape character syntax");
8132 dispatch_scan_event(p, tSTRING_CONTENT);
8145tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8147 int len = rb_enc_codelen(c, enc);
8148 rb_enc_mbcput(c, tokspace(p, len), enc);
8152tokadd_escape(struct parser_params *p)
8156 const char *begin = p->lex.pcur;
8158 switch (c = nextc(p)) {
8160 return 0; /* just ignore */
8162 case '0': case '1': case '2': case '3': /* octal constant */
8163 case '4': case '5': case '6': case '7':
8166 ruby_scan_digits(--p->lex.pcur, 3, 8, &numlen, &ov);
8167 if (numlen == 0) goto eof;
8168 p->lex.pcur += numlen;
8169 tokcopy(p, (int)numlen + 1);
8173 case 'x': /* hex constant */
8175 tok_hex(p, &numlen);
8176 if (numlen == 0) return -1;
8177 tokcopy(p, (int)numlen + 2);
8183 flush_string_content(p, p->enc, p->lex.pcur - begin);
8184 yyerror0("Invalid escape character syntax");
8196char_to_option(int c)
8202 val = RE_ONIG_OPTION_IGNORECASE;
8205 val = RE_ONIG_OPTION_EXTEND;
8208 val = RE_ONIG_OPTION_MULTILINE;
8217#define ARG_ENCODING_FIXED 16
8218#define ARG_ENCODING_NONE 32
8219#define ENC_ASCII8BIT 1
8221#define ENC_Windows_31J 3
8225char_to_option_kcode(int c, int *option, int *kcode)
8231 *kcode = ENC_ASCII8BIT;
8232 return (*option = ARG_ENCODING_NONE);
8234 *kcode = ENC_EUC_JP;
8237 *kcode = ENC_Windows_31J;
8244 return (*option = char_to_option(c));
8246 *option = ARG_ENCODING_FIXED;
8251regx_options(struct parser_params *p)
8259 while (c = nextc(p), ISALPHA(c)) {
8261 options |= RE_OPTION_ONCE;
8263 else if (char_to_option_kcode(c, &opt, &kc)) {
8265 if (kc != ENC_ASCII8BIT) kcode = c;
8279 YYLTYPE loc = RUBY_INIT_YYLLOC();
8281 compile_error(p, "unknown regexp option%s - %*s",
8282 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8283 parser_show_error_line(p, &loc);
8285 return options | RE_OPTION_ENCODING(kcode);
8289tokadd_mbchar(struct parser_params *p, int c)
8291 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8292 if (len < 0) return -1;
8294 p->lex.pcur += --len;
8295 if (len > 0) tokcopy(p, len);
8300simple_re_meta(int c)
8303 case '$': case '*': case '+': case '.':
8304 case '?': case '^': case '|':
8305 case ')': case ']': case '}': case '>':
8313parser_update_heredoc_indent(struct parser_params *p, int c)
8315 if (p->heredoc_line_indent == -1) {
8316 if (c == '\n') p->heredoc_line_indent = 0;
8320 p->heredoc_line_indent++;
8323 else if (c == '\t') {
8324 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8325 p->heredoc_line_indent = w * TAB_WIDTH;
8328 else if (c != '\n') {
8329 if (p->heredoc_indent > p->heredoc_line_indent) {
8330 p->heredoc_indent = p->heredoc_line_indent;
8332 p->heredoc_line_indent = -1;
8335 /* Whitespace only line has no indentation */
8336 p->heredoc_line_indent = 0;
8343parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8345 YYLTYPE loc = RUBY_INIT_YYLLOC();
8346 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8347 compile_error(p, "%s mixed within %s source", n1, n2);
8348 parser_show_error_line(p, &loc);
8352parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8354 const char *pos = p->lex.pcur;
8356 parser_mixed_error(p, enc1, enc2);
8361nibble_char_upper(unsigned int c)
8364 return c + (c < 10 ? '0' : 'A' - 10);
8368tokadd_string(struct parser_params *p,
8369 int func, int term, int paren, long *nest,
8370 rb_encoding **encp, rb_encoding **enc)
8375 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8376 int top_of_line = FALSE;
8379#define mixed_error(enc1, enc2) \
8380 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8381#define mixed_escape(beg, enc1, enc2) \
8382 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8384 while ((c = nextc(p)) != -1) {
8385 if (p->heredoc_indent > 0) {
8386 parser_update_heredoc_indent(p, c);
8389 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8395 if (paren && c == paren) {
8398 else if (c == term) {
8399 if (!nest || !*nest) {
8405 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8406 unsigned char c2 = *p->lex.pcur;
8407 if (c2 == '$' || c2 == '@' || c2 == '{') {
8412 else if (c == '\\') {
8416 if (func & STR_FUNC_QWORDS) break;
8417 if (func & STR_FUNC_EXPAND) {
8418 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8429 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8433 if ((func & STR_FUNC_EXPAND) == 0) {
8437 tokadd_utf8(p, enc, term,
8438 func & STR_FUNC_SYMBOL,
8439 func & STR_FUNC_REGEXP);
8443 if (c == -1) return -1;
8445 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8448 if (func & STR_FUNC_REGEXP) {
8454 c = read_escape(p, 0, p->lex.pcur - 1);
8456 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8459 *t++ = nibble_char_upper(c >> 4);
8460 *t++ = nibble_char_upper(c);
8465 if (c == term && !simple_re_meta(c)) {
8470 if ((c = tokadd_escape(p)) < 0)
8472 if (*enc && *enc != *encp) {
8473 mixed_escape(p->lex.ptok+2, *enc, *encp);
8477 else if (func & STR_FUNC_EXPAND) {
8479 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8480 c = read_escape(p, 0, p->lex.pcur - 1);
8482 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8483 /* ignore backslashed spaces in %w */
8485 else if (c != term && !(paren && c == paren)) {
8492 else if (!parser_isascii(p)) {
8497 else if (*enc != *encp) {
8498 mixed_error(*enc, *encp);
8501 if (tokadd_mbchar(p, c) == -1) return -1;
8504 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8512 else if (*enc != *encp) {
8513 mixed_error(*enc, *encp);
8519 top_of_line = (c == '\n');
8523 if (*enc) *encp = *enc;
8527#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8530flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8532 p->lex.pcur -= back;
8533 if (has_delayed_token(p)) {
8534 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8536 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8537 p->delayed.end_line = p->ruby_sourceline;
8538 p->delayed.end_col = rb_len2int(p->lex.pcur - p->lex.pbeg);
8540 dispatch_delayed_token(p, tSTRING_CONTENT);
8541 p->lex.ptok = p->lex.pcur;
8543 dispatch_scan_event(p, tSTRING_CONTENT);
8544 p->lex.pcur += back;
8547/* this can be shared with ripper, since it's independent from struct
8550#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8551#define SPECIAL_PUNCT(idx) ( \
8552 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8553 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8554 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8555 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8556 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8558const uint_least32_t ruby_global_name_punct_bits[] = {
8567static enum yytokentype
8568parser_peek_variable_name(struct parser_params *p)
8571 const char *ptr = p->lex.pcur;
8573 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8577 if ((c = *ptr) == '-') {
8578 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8581 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8582 return tSTRING_DVAR;
8586 if ((c = *ptr) == '@') {
8587 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8593 p->command_start = TRUE;
8594 yylval.state = p->lex.state;
8595 return tSTRING_DBEG;
8599 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8600 return tSTRING_DVAR;
8604#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8605#define IS_END() IS_lex_state(EXPR_END_ANY)
8606#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8607#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8608#define IS_LABEL_POSSIBLE() (\
8609 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8611#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8612#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8614static inline enum yytokentype
8615parser_string_term(struct parser_params *p, int func)
8617 xfree(p->lex.strterm);
8619 if (func & STR_FUNC_REGEXP) {
8620 set_yylval_num(regx_options(p));
8621 dispatch_scan_event(p, tREGEXP_END);
8622 SET_LEX_STATE(EXPR_END);
8625 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8627 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8630 SET_LEX_STATE(EXPR_END);
8634static enum yytokentype
8635parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8637 int func = quote->func;
8638 int term = quote->term;
8639 int paren = quote->paren;
8641 rb_encoding *enc = p->enc;
8642 rb_encoding *base_enc = 0;
8643 rb_parser_string_t *lit;
8645 if (func & STR_FUNC_TERM) {
8646 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8647 SET_LEX_STATE(EXPR_END);
8648 xfree(p->lex.strterm);
8650 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8653 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8654 while (c != '\n' && ISSPACE(c = nextc(p)));
8657 if (func & STR_FUNC_LIST) {
8658 quote->func &= ~STR_FUNC_LIST;
8661 if (c == term && !quote->nest) {
8662 if (func & STR_FUNC_QWORDS) {
8663 quote->func |= STR_FUNC_TERM;
8664 pushback(p, c); /* dispatch the term at tSTRING_END */
8665 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8668 return parser_string_term(p, func);
8671 if (!ISSPACE(c)) pushback(p, c);
8672 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8676 if ((func & STR_FUNC_EXPAND) && c == '#') {
8677 enum yytokentype t = parser_peek_variable_name(p);
8683 if (tokadd_string(p, func, term, paren, "e->nest,
8684 &enc, &base_enc) == -1) {
8687# define unterminated_literal(mesg) yyerror0(mesg)
8689# define unterminated_literal(mesg) compile_error(p, mesg)
8691 literal_flush(p, p->lex.pcur);
8692 if (func & STR_FUNC_QWORDS) {
8693 /* no content to add, bailing out here */
8694 unterminated_literal("unterminated list meets end of file");
8695 xfree(p->lex.strterm);
8699 if (func & STR_FUNC_REGEXP) {
8700 unterminated_literal("unterminated regexp meets end of file");
8703 unterminated_literal("unterminated string meets end of file");
8705 quote->func |= STR_FUNC_TERM;
8710 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8711 set_yylval_str(lit);
8712 flush_string_content(p, enc, 0);
8714 return tSTRING_CONTENT;
8717static enum yytokentype
8718heredoc_identifier(struct parser_params *p)
8721 * term_len is length of `<<"END"` except `END`,
8722 * in this case term_len is 4 (<, <, " and ").
8724 rb_len_t len, offset = p->lex.pcur - p->lex.pbeg;
8725 int c = nextc(p), term, func = 0, quote = 0;
8726 enum yytokentype token = tSTRING_BEG;
8731 func = STR_FUNC_INDENT;
8734 else if (c == '~') {
8736 func = STR_FUNC_INDENT;
8742 func |= str_squote; goto quoted;
8744 func |= str_dquote; goto quoted;
8746 token = tXSTRING_BEG;
8747 func |= str_xquote; goto quoted;
8754 while ((c = nextc(p)) != term) {
8755 if (c == -1 || c == '\r' || c == '\n') {
8756 yyerror0("unterminated here document identifier");
8763 if (!parser_is_identchar(p)) {
8765 if (func & STR_FUNC_INDENT) {
8766 pushback(p, indent > 0 ? '~' : '-');
8772 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8773 if (n < 0) return 0;
8775 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8780 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8781 if ((rb_ulen_t)len >= HERETERM_LENGTH_MAX)
8782 yyerror0("too long here document identifier");
8783 dispatch_scan_event(p, tHEREDOC_BEG);
8786 p->lex.strterm = new_heredoc(p);
8787 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8788 here->offset = offset;
8789 here->sourceline = p->ruby_sourceline;
8790 here->length = (unsigned)len;
8791 here->quote = quote;
8793 here->lastline = p->lex.lastline;
8796 p->heredoc_indent = indent;
8797 p->heredoc_line_indent = 0;
8802heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8804 rb_parser_string_t *line;
8805 rb_strterm_t *term = p->lex.strterm;
8808 line = here->lastline;
8809 p->lex.lastline = line;
8810 p->lex.pbeg = PARSER_STRING_PTR(line);
8811 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8812 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8813 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8814 p->heredoc_end = p->ruby_sourceline;
8815 p->ruby_sourceline = (int)here->sourceline;
8816 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8822dedent_string_column(const char *str, rb_len_t len, int width)
8826 for (i = 0; i < len && col < width; i++) {
8827 if (str[i] == ' ') {
8830 else if (str[i] == '\t') {
8831 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8832 if (n > width) break;
8844dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8850 len = PARSER_STRING_LEN(string);
8851 str = PARSER_STRING_PTR(string);
8853 i = dedent_string_column(str, len, width);
8856 rb_parser_str_modify(string);
8857 str = PARSER_STRING_PTR(string);
8858 if (PARSER_STRING_LEN(string) != len)
8859 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8860 MEMMOVE(str, str + i, char, len - i);
8861 rb_parser_str_set_len(p, string, len - i);
8866heredoc_dedent(struct parser_params *p, NODE *root)
8868 NODE *node, *str_node, *prev_node;
8869 int indent = p->heredoc_indent;
8870 rb_parser_string_t *prev_lit = 0;
8872 if (indent <= 0) return root;
8873 if (!root) return root;
8875 prev_node = node = str_node = root;
8876 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8879 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8880 if (nd_fl_newline(str_node)) {
8881 dedent_string(p, lit, indent);
8886 else if (!literal_concat0(p, prev_lit, lit)) {
8890 NODE *end = RNODE_LIST(node)->as.nd_end;
8891 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8893 if (nd_type_p(prev_node, NODE_DSTR))
8894 nd_set_type(prev_node, NODE_STR);
8897 RNODE_LIST(node)->as.nd_end = end;
8902 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8904 if (!nd_type_p(node, NODE_LIST)) break;
8905 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8906 enum node_type type = nd_type(str_node);
8907 if (type == NODE_STR || type == NODE_DSTR) break;
8917whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8919 const char *beg = p->lex.pbeg;
8920 const char *ptr = p->lex.pend;
8922 if (ptr - beg < len) return FALSE;
8923 if (ptr > beg && ptr[-1] == '\n') {
8924 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8925 if (ptr - beg < len) return FALSE;
8927 if (strncmp(eos, ptr -= len, len)) return FALSE;
8929 while (beg < ptr && ISSPACE(*beg)) beg++;
8935word_match_p(struct parser_params *p, const char *word, long len)
8937 if (strncmp(p->lex.pcur, word, len)) return 0;
8938 if (lex_eol_n_p(p, len)) return 1;
8939 int c = (unsigned char)p->lex.pcur[len];
8940 if (ISSPACE(c)) return 1;
8942 case '\0': case '\004': case '\032': return 1;
8947#define NUM_SUFFIX_R (1<<0)
8948#define NUM_SUFFIX_I (1<<1)
8949#define NUM_SUFFIX_ALL 3
8952number_literal_suffix(struct parser_params *p, int mask)
8955 const char *lastp = p->lex.pcur;
8957 while ((c = nextc(p)) != -1) {
8958 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8959 result |= (mask & NUM_SUFFIX_I);
8960 mask &= ~NUM_SUFFIX_I;
8961 /* r after i, rational of complex is disallowed */
8962 mask &= ~NUM_SUFFIX_R;
8965 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8966 result |= (mask & NUM_SUFFIX_R);
8967 mask &= ~NUM_SUFFIX_R;
8970 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8971 p->lex.pcur = lastp;
8980static enum yytokentype
8981set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8983 enum rb_numeric_type numeric_type = integer_literal;
8985 if (type == tFLOAT) {
8986 numeric_type = float_literal;
8989 if (suffix & NUM_SUFFIX_R) {
8991 numeric_type = rational_literal;
8993 if (suffix & NUM_SUFFIX_I) {
8999 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
9002 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
9005 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9008 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9009 (void)numeric_type; /* for ripper */
9012 rb_bug("unexpected token: %d", type);
9014 SET_LEX_STATE(EXPR_END);
9018#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9020parser_dispatch_heredoc_end(struct parser_params *p, int line)
9022 if (has_delayed_token(p))
9023 dispatch_delayed_token(p, tSTRING_CONTENT);
9026 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9027 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9029 if (p->keep_tokens) {
9030 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9031 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9032 parser_append_tokens(p, str, tHEREDOC_END, line);
9036 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9041static enum yytokentype
9042here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9044 int c, func, indent = 0;
9045 const char *eos, *ptr, *ptr_end;
9047 rb_parser_string_t *str = 0;
9048 rb_encoding *enc = p->enc;
9049 rb_encoding *base_enc = 0;
9055 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9057 indent = (func = here->func) & STR_FUNC_INDENT;
9059 if ((c = nextc(p)) == -1) {
9062 if (!has_delayed_token(p)) {
9063 dispatch_scan_event(p, tSTRING_CONTENT);
9065 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9066 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9067 if (!(func & STR_FUNC_REGEXP)) {
9068 int cr = ENC_CODERANGE_UNKNOWN;
9069 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9070 if (cr != ENC_CODERANGE_7BIT &&
9071 rb_is_usascii_enc(p->enc) &&
9072 enc != rb_utf8_encoding()) {
9073 enc = rb_ascii8bit_encoding();
9076 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9078 dispatch_delayed_token(p, tSTRING_CONTENT);
9081 dispatch_delayed_token(p, tSTRING_CONTENT);
9082 dispatch_scan_event(p, tSTRING_CONTENT);
9086 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9087 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9090 SET_LEX_STATE(EXPR_END);
9095 /* not beginning of line, cannot be the terminator */
9097 else if (p->heredoc_line_indent == -1) {
9098 /* `heredoc_line_indent == -1` means
9099 * - "after an interpolation in the same line", or
9100 * - "in a continuing line"
9102 p->heredoc_line_indent = 0;
9104 else if (whole_match_p(p, eos, len, indent)) {
9105 dispatch_heredoc_end(p);
9107 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9109 SET_LEX_STATE(EXPR_END);
9113 if (!(func & STR_FUNC_EXPAND)) {
9115 ptr = PARSER_STRING_PTR(p->lex.lastline);
9116 ptr_end = p->lex.pend;
9117 if (ptr_end > ptr) {
9118 switch (ptr_end[-1]) {
9120 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9129 if (p->heredoc_indent > 0) {
9131 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9133 p->heredoc_line_indent = 0;
9137 parser_str_cat(str, ptr, ptr_end - ptr);
9139 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9140 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9142 if (p->heredoc_indent > 0) {
9145 if (nextc(p) == -1) {
9147 rb_parser_string_free(p, str);
9152 } while (!whole_match_p(p, eos, len, indent));
9155 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9158 enum yytokentype t = parser_peek_variable_name(p);
9159 if (p->heredoc_line_indent != -1) {
9160 if (p->heredoc_indent > p->heredoc_line_indent) {
9161 p->heredoc_indent = p->heredoc_line_indent;
9163 p->heredoc_line_indent = -1;
9172 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9173 if (p->eofp) goto error;
9177 if (c == '\\') p->heredoc_line_indent = -1;
9179 str = STR_NEW3(tok(p), toklen(p), enc, func);
9181 set_yylval_str(str);
9183 if (bol) nd_set_fl_newline(yylval.node);
9185 flush_string_content(p, enc, 0);
9186 return tSTRING_CONTENT;
9188 tokadd(p, nextc(p));
9189 if (p->heredoc_indent > 0) {
9193 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9194 if ((c = nextc(p)) == -1) goto error;
9195 } while (!whole_match_p(p, eos, len, indent));
9196 str = STR_NEW3(tok(p), toklen(p), enc, func);
9198 dispatch_heredoc_end(p);
9199 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9201 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9203 /* Preserve s_value for set_yylval_str */
9204 s_value = p->s_value;
9206 set_yylval_str(str);
9208 set_parser_s_value(s_value);
9212 if (bol) nd_set_fl_newline(yylval.node);
9214 return tSTRING_CONTENT;
9220arg_ambiguous(struct parser_params *p, char c)
9223 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9225 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9230/* returns true value if formal argument error;
9231 * Qtrue, or error message if ripper */
9233formal_argument_error(struct parser_params *p, ID id)
9235 switch (id_type(id)) {
9239# define ERR(mesg) (yyerror0(mesg), Qtrue)
9241# define ERR(mesg) WARN_S(mesg)
9244 return ERR("formal argument cannot be a constant");
9246 return ERR("formal argument cannot be an instance variable");
9248 return ERR("formal argument cannot be a global variable");
9250 return ERR("formal argument cannot be a class variable");
9252 return ERR("formal argument must be local variable");
9255 shadowing_lvar(p, id);
9261lvar_defined(struct parser_params *p, ID id)
9263 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9266/* emacsen -*- hack */
9268parser_encode_length(struct parser_params *p, const char *name, long len)
9272 if (len > 5 && name[nlen = len - 5] == '-') {
9273 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9276 if (len > 4 && name[nlen = len - 4] == '-') {
9277 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9279 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9280 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9281 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9288parser_set_encode(struct parser_params *p, const char *name)
9294 const char *wrong = 0;
9296 case 'e': case 'E': wrong = "external"; break;
9297 case 'i': case 'I': wrong = "internal"; break;
9298 case 'f': case 'F': wrong = "filesystem"; break;
9299 case 'l': case 'L': wrong = "locale"; break;
9301 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9302 idx = rb_enc_find_index(name);
9305 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9307 excargs[0] = rb_eArgError;
9308 excargs[2] = rb_make_backtrace();
9309 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9310 VALUE exc = rb_make_exception(3, excargs);
9311 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9313 rb_ast_free(p->ast);
9318 enc = rb_enc_from_index(idx);
9319 if (!rb_enc_asciicompat(enc)) {
9320 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9325 if (p->debug_lines) {
9327 for (i = 0; i < p->debug_lines->len; i++) {
9328 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9335comment_at_top(struct parser_params *p)
9337 if (p->token_seen) return false;
9338 return (p->line_count == (p->has_shebang ? 2 : 1));
9341typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9342typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9344static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9347magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9349 if (!comment_at_top(p)) {
9352 parser_set_encode(p, val);
9356parser_get_bool(struct parser_params *p, const char *name, const char *val)
9360 if (STRCASECMP(val, "true") == 0) {
9365 if (STRCASECMP(val, "false") == 0) {
9370 return parser_invalid_pragma_value(p, name, val);
9374parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9376 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9381parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9383 int b = parser_get_bool(p, name, val);
9384 if (b >= 0) p->token_info_enabled = b;
9388parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9392 if (p->token_seen) {
9393 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9397 b = parser_get_bool(p, name, val);
9400 p->frozen_string_literal = b;
9404parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9406 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9407 if (*s == ' ' || *s == '\t') continue;
9408 if (*s == '#') break;
9409 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9415 if (STRCASECMP(val, "none") == 0) {
9416 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9421 if (STRCASECMP(val, "literal") == 0) {
9422 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9427 if (STRCASECMP(val, "experimental_copy") == 0) {
9428 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9431 if (STRCASECMP(val, "experimental_everything") == 0) {
9432 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9437 parser_invalid_pragma_value(p, name, val);
9442parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9444 int b = parser_get_bool(p, name, val);
9445 if (b >= 0) p->past_scope_enabled = b;
9449struct magic_comment {
9451 rb_magic_comment_setter_t func;
9452 rb_magic_comment_length_t length;
9455static const struct magic_comment magic_comments[] = {
9456 {"coding", magic_comment_encoding, parser_encode_length},
9457 {"encoding", magic_comment_encoding, parser_encode_length},
9458 {"frozen_string_literal", parser_set_frozen_string_literal},
9459 {"shareable_constant_value", parser_set_shareable_constant_value},
9460 {"warn_indent", parser_set_token_info},
9462 {"warn_past_scope", parser_set_past_scope},
9467magic_comment_marker(const char *str, long len)
9474 if (str[i-1] == '*' && str[i-2] == '-') {
9480 if (i + 1 >= len) return 0;
9481 if (str[i+1] != '-') {
9484 else if (str[i-1] != '-') {
9500parser_magic_comment(struct parser_params *p, const char *str, long len)
9503 VALUE name = 0, val = 0;
9504 const char *beg, *end, *vbeg, *vend;
9505#define str_copy(_s, _p, _n) ((_s) \
9506 ? (void)(rb_str_resize((_s), (_n)), \
9507 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9508 : (void)((_s) = STR_NEW((_p), (_n))))
9510 if (len <= 7) return FALSE;
9511 if (!!(beg = magic_comment_marker(str, len))) {
9512 if (!(end = magic_comment_marker(beg, str + len - beg)))
9516 len = end - beg - 3;
9519 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9521 const struct magic_comment *mc = magic_comments;
9526 for (; len > 0 && *str; str++, --len) {
9528 case '\'': case '"': case ':': case ';':
9531 if (!ISSPACE(*str)) break;
9533 for (beg = str; len > 0; str++, --len) {
9535 case '\'': case '"': case ':': case ';':
9538 if (ISSPACE(*str)) break;
9543 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9546 if (!indicator) return FALSE;
9550 do str++; while (--len > 0 && ISSPACE(*str));
9552 const char *tok_beg = str;
9554 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9567 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9570 const char *tok_end = str;
9572 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9575 while (len > 0 && (ISSPACE(*str))) --len, str++;
9576 if (len) return FALSE;
9580 str_copy(name, beg, n);
9581 s = RSTRING_PTR(name);
9582 for (i = 0; i < n; ++i) {
9583 if (s[i] == '-') s[i] = '_';
9586 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9589 n = (*mc->length)(p, vbeg, n);
9591 str_copy(val, vbeg, n);
9592 p->lex.ptok = tok_beg;
9593 p->lex.pcur = tok_end;
9594 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9597 } while (++mc < magic_comments + numberof(magic_comments));
9599 str_copy(val, vbeg, vend - vbeg);
9600 dispatch2(magic_comment, name, val);
9608set_file_encoding(struct parser_params *p, const char *str, const char *send)
9611 const char *beg = str;
9615 if (send - str <= 6) return;
9617 case 'C': case 'c': str += 6; continue;
9618 case 'O': case 'o': str += 5; continue;
9619 case 'D': case 'd': str += 4; continue;
9620 case 'I': case 'i': str += 3; continue;
9621 case 'N': case 'n': str += 2; continue;
9622 case 'G': case 'g': str += 1; continue;
9629 if (ISSPACE(*str)) break;
9632 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9637 if (++str >= send) return;
9638 } while (ISSPACE(*str));
9640 if (*str != '=' && *str != ':') return;
9645 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9646 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9649 parser_set_encode(p, RSTRING_PTR(s));
9650 rb_str_resize(s, 0);
9654parser_prepare(struct parser_params *p)
9656 int c = nextc0(p, FALSE);
9657 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9660 if (peek(p, '!')) p->has_shebang = 1;
9662 case 0xef: /* UTF-8 BOM marker */
9663 if (!lex_eol_n_p(p, 2) &&
9664 (unsigned char)p->lex.pcur[0] == 0xbb &&
9665 (unsigned char)p->lex.pcur[1] == 0xbf) {
9666 p->enc = rb_utf8_encoding();
9669 if (p->debug_lines) {
9670 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9673 p->lex.pbeg = p->lex.pcur;
9678 case -1: /* end of script. */
9682 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9686#define ambiguous_operator(tok, op, syn) ( \
9687 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9688 rb_warning0("even though it seems like "syn""))
9690#define ambiguous_operator(tok, op, syn) \
9691 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9693#define warn_balanced(tok, op, syn) ((void) \
9694 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9695 space_seen && !ISSPACE(c) && \
9696 (ambiguous_operator(tok, op, syn), 0)), \
9697 (enum yytokentype)(tok))
9699static enum yytokentype
9700no_digits(struct parser_params *p)
9702 yyerror0("numeric literal without digits");
9703 if (peek(p, '_')) nextc(p);
9704 /* dummy 0, for tUMINUS_NUM at numeric */
9705 return set_number_literal(p, tINTEGER, 0, 10, 0);
9708static enum yytokentype
9709parse_numeric(struct parser_params *p, int c)
9711 int is_float, seen_point, seen_e, nondigit;
9714 is_float = seen_point = seen_e = nondigit = 0;
9715 SET_LEX_STATE(EXPR_END);
9717 if (c == '-' || c == '+') {
9722 int start = toklen(p);
9724 if (c == 'x' || c == 'X') {
9727 if (c != -1 && ISXDIGIT(c)) {
9730 if (nondigit) break;
9734 if (!ISXDIGIT(c)) break;
9737 } while ((c = nextc(p)) != -1);
9741 if (toklen(p) == start) {
9742 return no_digits(p);
9744 else if (nondigit) goto trailing_uc;
9745 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9746 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9748 if (c == 'b' || c == 'B') {
9751 if (c == '0' || c == '1') {
9754 if (nondigit) break;
9758 if (c != '0' && c != '1') break;
9761 } while ((c = nextc(p)) != -1);
9765 if (toklen(p) == start) {
9766 return no_digits(p);
9768 else if (nondigit) goto trailing_uc;
9769 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9770 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9772 if (c == 'd' || c == 'D') {
9775 if (c != -1 && ISDIGIT(c)) {
9778 if (nondigit) break;
9782 if (!ISDIGIT(c)) break;
9785 } while ((c = nextc(p)) != -1);
9789 if (toklen(p) == start) {
9790 return no_digits(p);
9792 else if (nondigit) goto trailing_uc;
9793 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9794 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9800 if (c == 'o' || c == 'O') {
9801 /* prefixed octal */
9803 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9805 return no_digits(p);
9808 if (c >= '0' && c <= '7') {
9813 if (nondigit) break;
9817 if (c < '0' || c > '9') break;
9818 if (c > '7') goto invalid_octal;
9821 } while ((c = nextc(p)) != -1);
9822 if (toklen(p) > start) {
9825 if (nondigit) goto trailing_uc;
9826 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9827 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9834 if (c > '7' && c <= '9') {
9836 yyerror0("Invalid octal digit");
9838 else if (c == '.' || c == 'e' || c == 'E') {
9844 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9845 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9851 case '0': case '1': case '2': case '3': case '4':
9852 case '5': case '6': case '7': case '8': case '9':
9858 if (nondigit) goto trailing_uc;
9859 if (seen_point || seen_e) {
9864 if (c0 == -1 || !ISDIGIT(c0)) {
9870 seen_point = toklen(p);
9889 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9895 tokadd(p, nondigit);
9899 nondigit = (c == '-' || c == '+') ? c : 0;
9902 case '_': /* `_' in number just ignored */
9903 if (nondigit) goto decode_num;
9917 literal_flush(p, p->lex.pcur - 1);
9918 YYLTYPE loc = RUBY_INIT_YYLLOC();
9919 compile_error(p, "trailing '%c' in number", nondigit);
9920 parser_show_error_line(p, &loc);
9924 enum yytokentype type = tFLOAT;
9926 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9927 if (suffix & NUM_SUFFIX_R) {
9933 if (errno == ERANGE) {
9934 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9938 return set_number_literal(p, type, suffix, 0, seen_point);
9940 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9941 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9944static enum yytokentype
9945parse_qmark(struct parser_params *p, int space_seen)
9949 rb_parser_string_t *lit;
9950 const char *start = p->lex.pcur;
9953 SET_LEX_STATE(EXPR_VALUE);
9958 compile_error(p, "incomplete character syntax");
9961 if (rb_enc_isspace(c, p->enc)) {
9963 int c2 = escaped_control_code(c);
9965 WARN_SPACE_CHAR(c2, "?");
9970 SET_LEX_STATE(EXPR_VALUE);
9975 int w = parser_precise_mbclen(p, start);
9976 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9977 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9979 const char *ptr = start;
9981 int n = parser_precise_mbclen(p, ptr);
9982 if (n < 0) return -1;
9984 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9985 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9986 " a conditional operator, put a space after '?'",
9987 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9991 else if (c == '\\') {
9994 enc = rb_utf8_encoding();
9995 tokadd_utf8(p, &enc, -1, 0, 0);
9997 else if (!ISASCII(c = peekc(p)) && c != -1) {
9999 if (tokadd_mbchar(p, c) == -1) return 0;
10002 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10007 if (tokadd_mbchar(p, c) == -1) return 0;
10010 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10011 set_yylval_str(lit);
10012 SET_LEX_STATE(EXPR_END);
10016static enum yytokentype
10017parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10020 const char *ptok = p->lex.pcur;
10028 if (c == -1) goto unterminated;
10031 if (!ISASCII(c)) goto unknown;
10036 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10039 c = parser_precise_mbclen(p, p->lex.pcur);
10040 if (c < 0) return 0;
10042 yyerror0("unknown type of %string");
10048 compile_error(p, "unterminated quoted string meets end of file");
10052 if (term == '(') term = ')';
10053 else if (term == '[') term = ']';
10054 else if (term == '{') term = '}';
10055 else if (term == '<') term = '>';
10058 p->lex.ptok = ptok-1;
10061 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10062 return tSTRING_BEG;
10065 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10066 return tSTRING_BEG;
10069 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10073 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10074 return tQWORDS_BEG;
10077 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10078 return tSYMBOLS_BEG;
10081 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10082 return tQSYMBOLS_BEG;
10085 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10086 return tXSTRING_BEG;
10089 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10090 return tREGEXP_BEG;
10093 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10094 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10098 yyerror0("unknown type of %string");
10102 if ((c = nextc(p)) == '=') {
10103 set_yylval_id('%');
10104 SET_LEX_STATE(EXPR_BEG);
10107 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10110 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10112 return warn_balanced('%', "%%", "string literal");
10116tokadd_ident(struct parser_params *p, int c)
10119 if (tokadd_mbchar(p, c) == -1) return -1;
10121 } while (parser_is_identchar(p));
10127tokenize_ident(struct parser_params *p)
10129 ID ident = TOK_INTERN();
10131 set_yylval_name(ident);
10137parse_numvar(struct parser_params *p)
10141 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10142 const unsigned long nth_ref_max =
10143 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10144 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10145 * turned into a Fixnum, in compile.c */
10147 if (overflow || n > nth_ref_max) {
10148 /* compile_error()? */
10149 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10150 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10157static enum yytokentype
10158parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10160 const char *ptr = p->lex.pcur;
10163 SET_LEX_STATE(EXPR_END);
10164 p->lex.ptok = ptr - 1; /* from '$' */
10168 case '_': /* $_: last read line string */
10170 if (parser_is_identchar(p)) {
10178 case '~': /* $~: match-data */
10179 case '*': /* $*: argv */
10180 case '$': /* $$: pid */
10181 case '?': /* $?: last status */
10182 case '!': /* $!: error string */
10183 case '@': /* $@: error position */
10184 case '/': /* $/: input record separator */
10185 case '\\': /* $\: output record separator */
10186 case ';': /* $;: field separator */
10187 case ',': /* $,: output field separator */
10188 case '.': /* $.: last read line number */
10189 case '=': /* $=: ignorecase */
10190 case ':': /* $:: load path */
10191 case '<': /* $<: default input handle */
10192 case '>': /* $>: default output handle */
10193 case '\"': /* $": already loaded files */
10202 if (parser_is_identchar(p)) {
10203 if (tokadd_mbchar(p, c) == -1) return 0;
10214 case '&': /* $&: last match */
10215 case '`': /* $`: string before last match */
10216 case '\'': /* $': string after last match */
10217 case '+': /* $+: string matches last paren. */
10218 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10223 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10226 case '1': case '2': case '3':
10227 case '4': case '5': case '6':
10228 case '7': case '8': case '9':
10233 } while (c != -1 && ISDIGIT(c));
10235 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10237 c = parse_numvar(p);
10238 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10242 if (!parser_is_identchar(p)) {
10243 YYLTYPE loc = RUBY_INIT_YYLLOC();
10244 if (c == -1 || ISSPACE(c)) {
10245 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10249 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10251 parser_show_error_line(p, &loc);
10252 set_yylval_noname();
10260 if (tokadd_ident(p, c)) return 0;
10261 SET_LEX_STATE(EXPR_END);
10262 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10266 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10267 set_yylval_noname();
10273parser_numbered_param(struct parser_params *p, int n)
10275 if (n < 0) return false;
10277 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10280 if (p->max_numparam == ORDINAL_PARAM) {
10281 compile_error(p, "ordinary parameter is defined");
10284 struct vtable *args = p->lvtbl->args;
10285 if (p->max_numparam < n) {
10286 p->max_numparam = n;
10288 while (n > args->pos) {
10289 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10294static enum yytokentype
10295parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10297 const char *ptr = p->lex.pcur;
10298 enum yytokentype result = tIVAR;
10299 register int c = nextc(p);
10302 p->lex.ptok = ptr - 1; /* from '@' */
10310 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10311 if (c == -1 || !parser_is_identchar(p)) {
10313 RUBY_SET_YYLLOC(loc);
10314 if (result == tIVAR) {
10315 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10318 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10320 parser_show_error_line(p, &loc);
10321 set_yylval_noname();
10322 SET_LEX_STATE(EXPR_END);
10325 else if (ISDIGIT(c)) {
10327 RUBY_SET_YYLLOC(loc);
10328 if (result == tIVAR) {
10329 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10332 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10334 parser_show_error_line(p, &loc);
10335 set_yylval_noname();
10336 SET_LEX_STATE(EXPR_END);
10340 if (tokadd_ident(p, c)) return 0;
10345static enum yytokentype
10346parse_ident(struct parser_params *p, int c, int cmd_state)
10348 enum yytokentype result;
10349 bool is_ascii = true;
10350 const enum lex_state_e last_state = p->lex.state;
10352 int enforce_keyword_end = 0;
10355 if (!ISASCII(c)) is_ascii = false;
10356 if (tokadd_mbchar(p, c) == -1) return 0;
10358 } while (parser_is_identchar(p));
10359 if ((c == '!' || c == '?') && !peek(p, '=')) {
10363 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10364 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10365 result = tIDENTIFIER;
10369 result = tCONSTANT; /* assume provisionally */
10374 if (IS_LABEL_POSSIBLE()) {
10375 if (IS_LABEL_SUFFIX(0)) {
10376 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10384 if (peek_end_expect_token_locations(p)) {
10385 const rb_code_position_t *end_pos;
10386 int lineno, column;
10387 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
10389 end_pos = peek_end_expect_token_locations(p)->pos;
10390 lineno = end_pos->lineno;
10391 column = end_pos->column;
10394 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10395 p->ruby_sourceline, beg_pos, lineno, column);
10398 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10399 const struct kwtable *kw;
10401 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10402 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10403 enforce_keyword_end = 1;
10409 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10410 const struct kwtable *kw;
10412 /* See if it is a reserved word. */
10413 kw = rb_reserved_word(tok(p), toklen(p));
10415 enum lex_state_e state = p->lex.state;
10416 if (IS_lex_state_for(state, EXPR_FNAME)) {
10417 SET_LEX_STATE(EXPR_ENDFN);
10418 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10421 SET_LEX_STATE(kw->state);
10422 if (IS_lex_state(EXPR_BEG)) {
10423 p->command_start = TRUE;
10425 if (kw->id[0] == keyword_do) {
10426 if (lambda_beginning_p()) {
10427 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10428 return keyword_do_LAMBDA;
10430 if (COND_P()) return keyword_do_cond;
10431 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10432 return keyword_do_block;
10435 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10438 if (kw->id[0] != kw->id[1])
10439 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10445 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10447 SET_LEX_STATE(EXPR_CMDARG);
10450 SET_LEX_STATE(EXPR_ARG);
10453 else if (p->lex.state == EXPR_FNAME) {
10454 SET_LEX_STATE(EXPR_ENDFN);
10457 SET_LEX_STATE(EXPR_END);
10460 ident = tokenize_ident(p);
10461 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10462 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10463 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10464 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10465 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10471warn_cr(struct parser_params *p)
10475 /* carried over with p->lex.nextline for nextc() */
10476 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10480static enum yytokentype
10481parser_yylex(struct parser_params *p)
10484 int space_seen = 0;
10487 enum lex_state_e last_state;
10488 int fallthru = FALSE;
10489 int token_seen = p->token_seen;
10491 if (p->lex.strterm) {
10492 if (strterm_is_heredoc(p->lex.strterm)) {
10494 return here_document(p, &p->lex.strterm->u.heredoc);
10498 return parse_string(p, &p->lex.strterm->u.literal);
10501 cmd_state = p->command_start;
10502 p->command_start = FALSE;
10503 p->token_seen = TRUE;
10508 last_state = p->lex.state;
10509 switch (c = nextc(p)) {
10510 case '\0': /* NUL */
10511 case '\004': /* ^D */
10512 case '\032': /* ^Z */
10513 case -1: /* end of script. */
10516 if (p->end_expect_token_locations) {
10517 pop_end_expect_token_locations(p);
10518 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10522 /* Set location for end-of-input because dispatch_scan_event is not called. */
10523 RUBY_SET_YYLLOC(*p->yylloc);
10524 return END_OF_INPUT;
10530 case ' ': case '\t': case '\f':
10531 case '\13': /* '\v' */
10533 while ((c = nextc(p))) {
10538 case ' ': case '\t': case '\f':
10539 case '\13': /* '\v' */
10547 dispatch_scan_event(p, tSP);
10553 case '#': /* it's a comment */
10554 p->token_seen = token_seen;
10555 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10556 /* no magic_comment in shebang line */
10557 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10558 if (comment_at_top(p)) {
10559 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10562 p->lex.pcur = pcur, p->lex.ptok = ptok;
10564 dispatch_scan_event(p, tCOMMENT);
10568 p->token_seen = token_seen;
10569 rb_parser_string_t *prevline = p->lex.lastline;
10570 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10571 !IS_lex_state(EXPR_LABELED));
10572 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10574 dispatch_scan_event(p, tIGNORED_NL);
10577 if (!c && p->ctxt.in_kwarg) {
10578 goto normal_newline;
10583 switch (c = nextc(p)) {
10584 case ' ': case '\t': case '\f': case '\r':
10585 case '\13': /* '\v' */
10591 dispatch_scan_event(p, tSP);
10596 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10599 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10602 if (peek(p, '|')) goto leading_logical;
10605 if (peek(p, '&')) {
10608 dispatch_delayed_token(p, tIGNORED_NL);
10614 dispatch_delayed_token(p, tIGNORED_NL);
10615 if (peek(p, '.') == (c == '&')) {
10617 dispatch_scan_event(p, tSP);
10623 p->ruby_sourceline--;
10624 p->lex.nextline = p->lex.lastline;
10625 set_lastline(p, prevline);
10626 case -1: /* EOF no decrement*/
10627 if (c == -1 && space_seen) {
10628 dispatch_scan_event(p, tSP);
10633 RUBY_SET_YYLLOC(*p->yylloc);
10635 goto normal_newline;
10639 p->command_start = TRUE;
10640 SET_LEX_STATE(EXPR_BEG);
10644 if ((c = nextc(p)) == '*') {
10645 if ((c = nextc(p)) == '=') {
10646 set_yylval_id(idPow);
10647 SET_LEX_STATE(EXPR_BEG);
10651 if (IS_SPCARG(c)) {
10652 rb_warning0("'**' interpreted as argument prefix");
10655 else if (IS_BEG()) {
10659 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10664 set_yylval_id('*');
10665 SET_LEX_STATE(EXPR_BEG);
10669 if (IS_SPCARG(c)) {
10670 rb_warning0("'*' interpreted as argument prefix");
10673 else if (IS_BEG()) {
10677 c = warn_balanced('*', "*", "argument prefix");
10680 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10685 if (IS_AFTER_OPERATOR()) {
10686 SET_LEX_STATE(EXPR_ARG);
10692 SET_LEX_STATE(EXPR_BEG);
10705 /* skip embedded rd document */
10706 if (word_match_p(p, "begin", 5)) {
10707 int first_p = TRUE;
10710 dispatch_scan_event(p, tEMBDOC_BEG);
10714 dispatch_scan_event(p, tEMBDOC);
10719 compile_error(p, "embedded document meets end of file");
10720 return END_OF_INPUT;
10722 if (c == '=' && word_match_p(p, "end", 3)) {
10728 dispatch_scan_event(p, tEMBDOC_END);
10733 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10734 if ((c = nextc(p)) == '=') {
10735 if ((c = nextc(p)) == '=') {
10744 else if (c == '>') {
10753 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10755 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10756 enum yytokentype token = heredoc_identifier(p);
10757 if (token) return token < 0 ? 0 : token;
10759 if (IS_AFTER_OPERATOR()) {
10760 SET_LEX_STATE(EXPR_ARG);
10763 if (IS_lex_state(EXPR_CLASS))
10764 p->command_start = TRUE;
10765 SET_LEX_STATE(EXPR_BEG);
10768 if ((c = nextc(p)) == '>') {
10775 if ((c = nextc(p)) == '=') {
10776 set_yylval_id(idLTLT);
10777 SET_LEX_STATE(EXPR_BEG);
10781 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10787 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10788 if ((c = nextc(p)) == '=') {
10792 if ((c = nextc(p)) == '=') {
10793 set_yylval_id(idGTGT);
10794 SET_LEX_STATE(EXPR_BEG);
10804 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10805 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10806 p->lex.ptok = p->lex.pcur-1;
10807 return tSTRING_BEG;
10810 if (IS_lex_state(EXPR_FNAME)) {
10811 SET_LEX_STATE(EXPR_ENDFN);
10814 if (IS_lex_state(EXPR_DOT)) {
10816 SET_LEX_STATE(EXPR_CMDARG);
10818 SET_LEX_STATE(EXPR_ARG);
10821 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10822 return tXSTRING_BEG;
10825 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10826 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10827 p->lex.ptok = p->lex.pcur-1;
10828 return tSTRING_BEG;
10831 return parse_qmark(p, space_seen);
10834 if ((c = nextc(p)) == '&') {
10835 SET_LEX_STATE(EXPR_BEG);
10836 if ((c = nextc(p)) == '=') {
10837 set_yylval_id(idANDOP);
10838 SET_LEX_STATE(EXPR_BEG);
10844 else if (c == '=') {
10845 set_yylval_id('&');
10846 SET_LEX_STATE(EXPR_BEG);
10849 else if (c == '.') {
10850 set_yylval_id(idANDDOT);
10851 SET_LEX_STATE(EXPR_DOT);
10855 if (IS_SPCARG(c)) {
10857 (c = peekc_n(p, 1)) == -1 ||
10858 !(c == '\'' || c == '"' ||
10859 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10860 rb_warning0("'&' interpreted as argument prefix");
10864 else if (IS_BEG()) {
10868 c = warn_balanced('&', "&", "argument prefix");
10870 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10874 if ((c = nextc(p)) == '|') {
10875 SET_LEX_STATE(EXPR_BEG);
10876 if ((c = nextc(p)) == '=') {
10877 set_yylval_id(idOROP);
10878 SET_LEX_STATE(EXPR_BEG);
10882 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10890 set_yylval_id('|');
10891 SET_LEX_STATE(EXPR_BEG);
10894 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10900 if (IS_AFTER_OPERATOR()) {
10901 SET_LEX_STATE(EXPR_ARG);
10909 set_yylval_id('+');
10910 SET_LEX_STATE(EXPR_BEG);
10913 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10914 SET_LEX_STATE(EXPR_BEG);
10916 if (c != -1 && ISDIGIT(c)) {
10917 return parse_numeric(p, '+');
10921 SET_LEX_STATE(EXPR_BEG);
10923 return warn_balanced('+', "+", "unary operator");
10927 if (IS_AFTER_OPERATOR()) {
10928 SET_LEX_STATE(EXPR_ARG);
10936 set_yylval_id('-');
10937 SET_LEX_STATE(EXPR_BEG);
10941 SET_LEX_STATE(EXPR_ENDFN);
10942 yylval.num = p->lex.lpar_beg;
10943 p->lex.lpar_beg = p->lex.paren_nest;
10946 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10947 SET_LEX_STATE(EXPR_BEG);
10949 if (c != -1 && ISDIGIT(c)) {
10950 return tUMINUS_NUM;
10954 SET_LEX_STATE(EXPR_BEG);
10956 return warn_balanced('-', "-", "unary operator");
10959 int is_beg = IS_BEG();
10960 SET_LEX_STATE(EXPR_BEG);
10961 if ((c = nextc(p)) == '.') {
10962 if ((c = nextc(p)) == '.') {
10963 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10964 SET_LEX_STATE(EXPR_ENDARG);
10967 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10968 rb_warn0("... at EOL, should be parenthesized?");
10970 return is_beg ? tBDOT3 : tDOT3;
10973 return is_beg ? tBDOT2 : tDOT2;
10976 if (c != -1 && ISDIGIT(c)) {
10977 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10978 parse_numeric(p, '.');
10979 if (ISDIGIT(prev)) {
10980 yyerror0("unexpected fraction part after numeric literal");
10983 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10985 set_yylval_id(idNULL);
10986 SET_LEX_STATE(EXPR_END);
10987 p->lex.ptok = p->lex.pcur;
10990 set_yylval_id('.');
10991 SET_LEX_STATE(EXPR_DOT);
10995 case '0': case '1': case '2': case '3': case '4':
10996 case '5': case '6': case '7': case '8': case '9':
10997 return parse_numeric(p, c);
11002 SET_LEX_STATE(EXPR_ENDFN);
11003 p->lex.paren_nest--;
11009 SET_LEX_STATE(EXPR_END);
11010 p->lex.paren_nest--;
11014 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11015 if (!p->lex.brace_nest--) return tSTRING_DEND;
11018 SET_LEX_STATE(EXPR_END);
11019 p->lex.paren_nest--;
11025 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11026 SET_LEX_STATE(EXPR_BEG);
11029 set_yylval_id(idCOLON2);
11030 SET_LEX_STATE(EXPR_DOT);
11033 if (IS_END() || ISSPACE(c) || c == '#') {
11035 c = warn_balanced(':', ":", "symbol literal");
11036 SET_LEX_STATE(EXPR_BEG);
11041 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11044 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11050 SET_LEX_STATE(EXPR_FNAME);
11055 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11056 return tREGEXP_BEG;
11058 if ((c = nextc(p)) == '=') {
11059 set_yylval_id('/');
11060 SET_LEX_STATE(EXPR_BEG);
11064 if (IS_SPCARG(c)) {
11065 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11066 return tREGEXP_BEG;
11068 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11069 return warn_balanced('/', "/", "regexp literal");
11072 if ((c = nextc(p)) == '=') {
11073 set_yylval_id('^');
11074 SET_LEX_STATE(EXPR_BEG);
11077 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11082 SET_LEX_STATE(EXPR_BEG);
11083 p->command_start = TRUE;
11087 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11091 if (IS_AFTER_OPERATOR()) {
11092 if ((c = nextc(p)) != '@') {
11095 SET_LEX_STATE(EXPR_ARG);
11098 SET_LEX_STATE(EXPR_BEG);
11106 else if (!space_seen) {
11107 /* foo( ... ) => method call, no ambiguity */
11109 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11112 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11113 rb_warning0("parentheses after method name is interpreted as "
11114 "an argument list, not a decomposed argument");
11116 p->lex.paren_nest++;
11119 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11123 p->lex.paren_nest++;
11124 if (IS_AFTER_OPERATOR()) {
11125 if ((c = nextc(p)) == ']') {
11126 p->lex.paren_nest--;
11127 SET_LEX_STATE(EXPR_ARG);
11128 if ((c = nextc(p)) == '=') {
11135 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11138 else if (IS_BEG()) {
11141 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11144 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11150 ++p->lex.brace_nest;
11151 if (lambda_beginning_p())
11153 else if (IS_lex_state(EXPR_LABELED))
11154 c = tLBRACE; /* hash */
11155 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11156 c = '{'; /* block (primary) */
11157 else if (IS_lex_state(EXPR_ENDARG))
11158 c = tLBRACE_ARG; /* block (expr) */
11160 c = tLBRACE; /* hash */
11161 if (c != tLBRACE) {
11162 p->command_start = TRUE;
11163 SET_LEX_STATE(EXPR_BEG);
11166 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11168 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11177 dispatch_scan_event(p, tSP);
11178 goto retry; /* skip \\n */
11180 if (c == ' ') return tSP;
11181 if (ISSPACE(c)) return c;
11186 return parse_percent(p, space_seen, last_state);
11189 return parse_gvar(p, last_state);
11192 return parse_atmark(p, last_state);
11195 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11196 p->ruby__end__seen = 1;
11200 dispatch_scan_event(p, k__END__);
11202 return END_OF_INPUT;
11208 if (!parser_is_identchar(p)) {
11209 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11218 return parse_ident(p, c, cmd_state);
11221static enum yytokentype
11222yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11224 enum yytokentype t;
11228 p->yylloc = yylloc;
11230 t = parser_yylex(p);
11232 if (has_delayed_token(p))
11233 dispatch_delayed_token(p, t);
11234 else if (t != END_OF_INPUT)
11235 dispatch_scan_event(p, t);
11240#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11243node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11245 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11247 rb_node_init(n, type);
11252nd_set_loc(NODE *nd, const YYLTYPE *loc)
11255 nd_set_line(nd, loc->beg_pos.lineno);
11260node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11262 NODE *n = node_new_internal(p, type, size, alignment);
11264 nd_set_loc(n, loc);
11265 nd_set_node_id(n, parser_get_node_id(p));
11269#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11271static rb_node_scope_t *
11272rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11274 rb_ast_id_table_t *nd_tbl;
11275 nd_tbl = local_tbl(p);
11276 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11277 n->nd_tbl = nd_tbl;
11278 n->nd_body = nd_body;
11279 n->nd_parent = nd_parent;
11280 n->nd_args = nd_args;
11285static rb_node_scope_t *
11286rb_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)
11288 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11289 n->nd_tbl = nd_tbl;
11290 n->nd_body = nd_body;
11291 n->nd_parent = nd_parent;
11292 n->nd_args = nd_args;
11297static rb_node_defn_t *
11298rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11300 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11301 n->nd_mid = nd_mid;
11302 n->nd_defn = nd_defn;
11307static rb_node_defs_t *
11308rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11310 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11311 n->nd_recv = nd_recv;
11312 n->nd_mid = nd_mid;
11313 n->nd_defn = nd_defn;
11318static rb_node_block_t *
11319rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11321 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11322 n->nd_head = nd_head;
11323 n->nd_end = (NODE *)n;
11329static rb_node_for_t *
11330rb_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)
11332 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11333 n->nd_body = nd_body;
11334 n->nd_iter = nd_iter;
11335 n->for_keyword_loc = *for_keyword_loc;
11336 n->in_keyword_loc = *in_keyword_loc;
11337 n->do_keyword_loc = *do_keyword_loc;
11338 n->end_keyword_loc = *end_keyword_loc;
11343static rb_node_for_masgn_t *
11344rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11346 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11347 n->nd_var = nd_var;
11352static rb_node_retry_t *
11353rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11355 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11360static rb_node_begin_t *
11361rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11363 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11364 n->nd_body = nd_body;
11369static rb_node_rescue_t *
11370rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11372 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11373 n->nd_head = nd_head;
11374 n->nd_resq = nd_resq;
11375 n->nd_else = nd_else;
11380static rb_node_resbody_t *
11381rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11383 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11384 n->nd_args = nd_args;
11385 n->nd_exc_var = nd_exc_var;
11386 n->nd_body = nd_body;
11387 n->nd_next = nd_next;
11392static rb_node_ensure_t *
11393rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11395 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11396 n->nd_head = nd_head;
11397 n->nd_ensr = nd_ensr;
11402static rb_node_and_t *
11403rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11405 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11406 n->nd_1st = nd_1st;
11407 n->nd_2nd = nd_2nd;
11408 n->operator_loc = *operator_loc;
11413static rb_node_or_t *
11414rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11416 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11417 n->nd_1st = nd_1st;
11418 n->nd_2nd = nd_2nd;
11419 n->operator_loc = *operator_loc;
11424static rb_node_return_t *
11425rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11427 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11428 n->nd_stts = nd_stts;
11429 n->keyword_loc = *keyword_loc;
11433static rb_node_yield_t *
11434rb_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)
11436 if (nd_head) no_blockarg(p, nd_head);
11438 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11439 n->nd_head = nd_head;
11440 n->keyword_loc = *keyword_loc;
11441 n->lparen_loc = *lparen_loc;
11442 n->rparen_loc = *rparen_loc;
11447static rb_node_if_t *
11448rb_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)
11450 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11451 n->nd_cond = nd_cond;
11452 n->nd_body = nd_body;
11453 n->nd_else = nd_else;
11454 n->if_keyword_loc = *if_keyword_loc;
11455 n->then_keyword_loc = *then_keyword_loc;
11456 n->end_keyword_loc = *end_keyword_loc;
11461static rb_node_unless_t *
11462rb_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)
11464 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11465 n->nd_cond = nd_cond;
11466 n->nd_body = nd_body;
11467 n->nd_else = nd_else;
11468 n->keyword_loc = *keyword_loc;
11469 n->then_keyword_loc = *then_keyword_loc;
11470 n->end_keyword_loc = *end_keyword_loc;
11475static rb_node_class_t *
11476rb_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)
11478 /* Keep the order of node creation */
11479 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11480 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11481 RNODE_SCOPE(scope)->nd_parent = &n->node;
11482 n->nd_cpath = nd_cpath;
11483 n->nd_body = scope;
11484 n->nd_super = nd_super;
11485 n->class_keyword_loc = *class_keyword_loc;
11486 n->inheritance_operator_loc = *inheritance_operator_loc;
11487 n->end_keyword_loc = *end_keyword_loc;
11492static rb_node_sclass_t *
11493rb_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)
11495 /* Keep the order of node creation */
11496 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11497 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11498 RNODE_SCOPE(scope)->nd_parent = &n->node;
11499 n->nd_recv = nd_recv;
11500 n->nd_body = scope;
11501 n->class_keyword_loc = *class_keyword_loc;
11502 n->operator_loc = *operator_loc;
11503 n->end_keyword_loc = *end_keyword_loc;
11508static rb_node_module_t *
11509rb_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)
11511 /* Keep the order of node creation */
11512 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11513 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11514 RNODE_SCOPE(scope)->nd_parent = &n->node;
11515 n->nd_cpath = nd_cpath;
11516 n->nd_body = scope;
11517 n->module_keyword_loc = *module_keyword_loc;
11518 n->end_keyword_loc = *end_keyword_loc;
11523static rb_node_iter_t *
11524rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11526 /* Keep the order of node creation */
11527 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11528 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11529 RNODE_SCOPE(scope)->nd_parent = &n->node;
11530 n->nd_body = scope;
11536static rb_node_lambda_t *
11537rb_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)
11539 /* Keep the order of node creation */
11540 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11541 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11542 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11543 RNODE_SCOPE(scope)->nd_parent = &n->node;
11544 n->nd_body = scope;
11545 n->operator_loc = *operator_loc;
11546 n->opening_loc = *opening_loc;
11547 n->closing_loc = *closing_loc;
11552static rb_node_case_t *
11553rb_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)
11555 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11556 n->nd_head = nd_head;
11557 n->nd_body = nd_body;
11558 n->case_keyword_loc = *case_keyword_loc;
11559 n->end_keyword_loc = *end_keyword_loc;
11564static rb_node_case2_t *
11565rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11567 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11569 n->nd_body = nd_body;
11570 n->case_keyword_loc = *case_keyword_loc;
11571 n->end_keyword_loc = *end_keyword_loc;
11576static rb_node_case3_t *
11577rb_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)
11579 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11580 n->nd_head = nd_head;
11581 n->nd_body = nd_body;
11582 n->case_keyword_loc = *case_keyword_loc;
11583 n->end_keyword_loc = *end_keyword_loc;
11588static rb_node_when_t *
11589rb_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)
11591 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11592 n->nd_head = nd_head;
11593 n->nd_body = nd_body;
11594 n->nd_next = nd_next;
11595 n->keyword_loc = *keyword_loc;
11596 n->then_keyword_loc = *then_keyword_loc;
11601static rb_node_in_t *
11602rb_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)
11604 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11605 n->nd_head = nd_head;
11606 n->nd_body = nd_body;
11607 n->nd_next = nd_next;
11608 n->in_keyword_loc = *in_keyword_loc;
11609 n->then_keyword_loc = *then_keyword_loc;
11610 n->operator_loc = *operator_loc;
11615static rb_node_while_t *
11616rb_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)
11618 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11619 n->nd_cond = nd_cond;
11620 n->nd_body = nd_body;
11621 n->nd_state = nd_state;
11622 n->keyword_loc = *keyword_loc;
11623 n->closing_loc = *closing_loc;
11628static rb_node_until_t *
11629rb_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)
11631 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11632 n->nd_cond = nd_cond;
11633 n->nd_body = nd_body;
11634 n->nd_state = nd_state;
11635 n->keyword_loc = *keyword_loc;
11636 n->closing_loc = *closing_loc;
11641static rb_node_colon2_t *
11642rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11644 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11645 n->nd_head = nd_head;
11646 n->nd_mid = nd_mid;
11647 n->delimiter_loc = *delimiter_loc;
11648 n->name_loc = *name_loc;
11653static rb_node_colon3_t *
11654rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11656 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11657 n->nd_mid = nd_mid;
11658 n->delimiter_loc = *delimiter_loc;
11659 n->name_loc = *name_loc;
11664static rb_node_dot2_t *
11665rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11667 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11668 n->nd_beg = nd_beg;
11669 n->nd_end = nd_end;
11670 n->operator_loc = *operator_loc;
11675static rb_node_dot3_t *
11676rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11678 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11679 n->nd_beg = nd_beg;
11680 n->nd_end = nd_end;
11681 n->operator_loc = *operator_loc;
11686static rb_node_self_t *
11687rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11689 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11695static rb_node_nil_t *
11696rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11698 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11703static rb_node_true_t *
11704rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11706 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11711static rb_node_false_t *
11712rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11714 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11719static rb_node_super_t *
11720rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11721 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11723 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11724 n->nd_args = nd_args;
11725 n->keyword_loc = *keyword_loc;
11726 n->lparen_loc = *lparen_loc;
11727 n->rparen_loc = *rparen_loc;
11732static rb_node_zsuper_t *
11733rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11735 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11740static rb_node_match2_t *
11741rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11743 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11744 n->nd_recv = nd_recv;
11745 n->nd_value = nd_value;
11751static rb_node_match3_t *
11752rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11754 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11755 n->nd_recv = nd_recv;
11756 n->nd_value = nd_value;
11761static rb_node_list_t *
11762rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11764 return rb_node_list_new2(p, nd_head, 1, 0, loc);
11767static rb_node_list_t *
11768rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11770 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11771 n->nd_head = nd_head;
11772 n->as.nd_alen = nd_alen;
11773 n->nd_next = nd_next;
11778static rb_node_zlist_t *
11779rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11781 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11786static rb_node_hash_t *
11787rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11789 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11790 n->nd_head = nd_head;
11796static rb_node_masgn_t *
11797rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11799 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11800 n->nd_head = nd_head;
11802 n->nd_args = nd_args;
11807static rb_node_gasgn_t *
11808rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11810 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11811 n->nd_vid = nd_vid;
11812 n->nd_value = nd_value;
11817static rb_node_lasgn_t *
11818rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11820 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11821 n->nd_vid = nd_vid;
11822 n->nd_value = nd_value;
11827static rb_node_dasgn_t *
11828rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11830 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11831 n->nd_vid = nd_vid;
11832 n->nd_value = nd_value;
11837static rb_node_iasgn_t *
11838rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11840 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11841 n->nd_vid = nd_vid;
11842 n->nd_value = nd_value;
11847static rb_node_cvasgn_t *
11848rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11850 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11851 n->nd_vid = nd_vid;
11852 n->nd_value = nd_value;
11857static rb_node_op_asgn1_t *
11858rb_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)
11860 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11861 n->nd_recv = nd_recv;
11862 n->nd_mid = nd_mid;
11863 n->nd_index = index;
11864 n->nd_rvalue = rvalue;
11865 n->call_operator_loc = *call_operator_loc;
11866 n->opening_loc = *opening_loc;
11867 n->closing_loc = *closing_loc;
11868 n->binary_operator_loc = *binary_operator_loc;
11873static rb_node_op_asgn2_t *
11874rb_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)
11876 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11877 n->nd_recv = nd_recv;
11878 n->nd_value = nd_value;
11879 n->nd_vid = nd_vid;
11880 n->nd_mid = nd_mid;
11881 n->nd_aid = nd_aid;
11882 n->call_operator_loc = *call_operator_loc;
11883 n->message_loc = *message_loc;
11884 n->binary_operator_loc = *binary_operator_loc;
11889static rb_node_op_asgn_or_t *
11890rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11892 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11893 n->nd_head = nd_head;
11894 n->nd_value = nd_value;
11899static rb_node_op_asgn_and_t *
11900rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11902 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11903 n->nd_head = nd_head;
11904 n->nd_value = nd_value;
11909static rb_node_gvar_t *
11910rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11912 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11913 n->nd_vid = nd_vid;
11918static rb_node_lvar_t *
11919rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11921 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11922 n->nd_vid = nd_vid;
11927static rb_node_dvar_t *
11928rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11930 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11931 n->nd_vid = nd_vid;
11936static rb_node_ivar_t *
11937rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11939 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11940 n->nd_vid = nd_vid;
11945static rb_node_const_t *
11946rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11948 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11949 n->nd_vid = nd_vid;
11954static rb_node_cvar_t *
11955rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11957 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11958 n->nd_vid = nd_vid;
11963static rb_node_nth_ref_t *
11964rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11966 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11967 n->nd_nth = nd_nth;
11972static rb_node_back_ref_t *
11973rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11975 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11976 n->nd_nth = nd_nth;
11981static rb_node_integer_t *
11982rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11984 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11992static rb_node_float_t *
11993rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11995 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12002static rb_node_rational_t *
12003rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12005 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12009 n->seen_point = seen_point;
12014static rb_node_imaginary_t *
12015rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12017 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12021 n->seen_point = seen_point;
12022 n->type = numeric_type;
12027static rb_node_str_t *
12028rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12030 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12031 n->string = string;
12036/* TODO; Use union for NODE_DSTR2 */
12037static rb_node_dstr_t *
12038rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12040 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12041 n->string = string;
12042 n->as.nd_alen = nd_alen;
12043 n->nd_next = (rb_node_list_t *)nd_next;
12048static rb_node_dstr_t *
12049rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12051 return rb_node_dstr_new0(p, string, 1, 0, loc);
12054static rb_node_xstr_t *
12055rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12057 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12058 n->string = string;
12063static rb_node_dxstr_t *
12064rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12066 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12067 n->string = string;
12068 n->as.nd_alen = nd_alen;
12069 n->nd_next = (rb_node_list_t *)nd_next;
12074static rb_node_sym_t *
12075rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12077 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12078 n->string = rb_str_to_parser_string(p, str);
12083static rb_node_dsym_t *
12084rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12086 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12087 n->string = string;
12088 n->as.nd_alen = nd_alen;
12089 n->nd_next = (rb_node_list_t *)nd_next;
12094static rb_node_evstr_t *
12095rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12097 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12098 n->nd_body = nd_body;
12099 n->opening_loc = *opening_loc;
12100 n->closing_loc = *closing_loc;
12105static rb_node_regx_t *
12106rb_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)
12108 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12109 n->string = string;
12110 n->options = options & RE_OPTION_MASK;
12111 n->opening_loc = *opening_loc;
12112 n->content_loc = *content_loc;
12113 n->closing_loc = *closing_loc;
12118static rb_node_call_t *
12119rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12121 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12122 n->nd_recv = nd_recv;
12123 n->nd_mid = nd_mid;
12124 n->nd_args = nd_args;
12129static rb_node_opcall_t *
12130rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12132 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12133 n->nd_recv = nd_recv;
12134 n->nd_mid = nd_mid;
12135 n->nd_args = nd_args;
12140static rb_node_fcall_t *
12141rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12143 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12144 n->nd_mid = nd_mid;
12145 n->nd_args = nd_args;
12150static rb_node_qcall_t *
12151rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12153 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12154 n->nd_recv = nd_recv;
12155 n->nd_mid = nd_mid;
12156 n->nd_args = nd_args;
12161static rb_node_vcall_t *
12162rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12164 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12165 n->nd_mid = nd_mid;
12170static rb_node_once_t *
12171rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12173 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12174 n->nd_body = nd_body;
12179static rb_node_args_t *
12180rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12182 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12183 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12188static rb_node_args_aux_t *
12189rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12191 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12192 n->nd_pid = nd_pid;
12193 n->nd_plen = nd_plen;
12199static rb_node_opt_arg_t *
12200rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12202 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12203 n->nd_body = nd_body;
12209static rb_node_kw_arg_t *
12210rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12212 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12213 n->nd_body = nd_body;
12219static rb_node_postarg_t *
12220rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12222 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12223 n->nd_1st = nd_1st;
12224 n->nd_2nd = nd_2nd;
12229static rb_node_argscat_t *
12230rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12232 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12233 n->nd_head = nd_head;
12234 n->nd_body = nd_body;
12239static rb_node_argspush_t *
12240rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12242 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12243 n->nd_head = nd_head;
12244 n->nd_body = nd_body;
12249static rb_node_splat_t *
12250rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12252 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12253 n->nd_head = nd_head;
12254 n->operator_loc = *operator_loc;
12259static rb_node_block_pass_t *
12260rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12262 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12265 n->nd_body = nd_body;
12266 n->operator_loc = *operator_loc;
12271static rb_node_alias_t *
12272rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12274 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12275 n->nd_1st = nd_1st;
12276 n->nd_2nd = nd_2nd;
12277 n->keyword_loc = *keyword_loc;
12282static rb_node_valias_t *
12283rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12285 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12286 n->nd_alias = nd_alias;
12287 n->nd_orig = nd_orig;
12288 n->keyword_loc = *keyword_loc;
12293static rb_node_undef_t *
12294rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12296 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12297 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12298 n->keyword_loc = NULL_LOC;
12299 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12304static rb_node_errinfo_t *
12305rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12307 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12312static rb_node_defined_t *
12313rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12315 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12316 n->nd_head = nd_head;
12317 n->keyword_loc = *keyword_loc;
12322static rb_node_postexe_t *
12323rb_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)
12325 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12326 n->nd_body = nd_body;
12327 n->keyword_loc = *keyword_loc;
12328 n->opening_loc = *opening_loc;
12329 n->closing_loc = *closing_loc;
12334static rb_node_attrasgn_t *
12335rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12337 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12338 n->nd_recv = nd_recv;
12339 n->nd_mid = nd_mid;
12340 n->nd_args = nd_args;
12345static rb_node_aryptn_t *
12346rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12348 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12350 n->pre_args = pre_args;
12351 n->rest_arg = rest_arg;
12352 n->post_args = post_args;
12357static rb_node_hshptn_t *
12358rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12360 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12361 n->nd_pconst = nd_pconst;
12362 n->nd_pkwargs = nd_pkwargs;
12363 n->nd_pkwrestarg = nd_pkwrestarg;
12368static rb_node_fndptn_t *
12369rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12371 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12373 n->pre_rest_arg = pre_rest_arg;
12375 n->post_rest_arg = post_rest_arg;
12380static rb_node_line_t *
12381rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12383 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12388static rb_node_file_t *
12389rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12391 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12392 n->path = rb_str_to_parser_string(p, str);
12397static rb_node_encoding_t *
12398rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12400 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12406static rb_node_cdecl_t *
12407rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12409 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12410 n->nd_vid = nd_vid;
12411 n->nd_value = nd_value;
12412 n->nd_else = nd_else;
12413 n->shareability = shareability;
12418static rb_node_op_cdecl_t *
12419rb_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)
12421 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12422 n->nd_head = nd_head;
12423 n->nd_value = nd_value;
12424 n->nd_aid = nd_aid;
12425 n->shareability = shareability;
12430static rb_node_error_t *
12431rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12433 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12438static rb_node_break_t *
12439rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12441 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12442 n->nd_stts = nd_stts;
12444 n->keyword_loc = *keyword_loc;
12449static rb_node_next_t *
12450rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12452 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12453 n->nd_stts = nd_stts;
12455 n->keyword_loc = *keyword_loc;
12460static rb_node_redo_t *
12461rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12463 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12465 n->keyword_loc = *keyword_loc;
12470static rb_node_def_temp_t *
12471rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12473 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12474 n->save.numparam_save = 0;
12475 n->save.max_numparam = 0;
12476 n->save.ctxt = p->ctxt;
12483static rb_node_def_temp_t *
12484def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12486 n->save.numparam_save = numparam_push(p);
12487 n->save.max_numparam = p->max_numparam;
12492static enum node_type
12493nodetype(NODE *node) /* for debug */
12495 return (enum node_type)nd_type(node);
12499nodeline(NODE *node)
12501 return nd_line(node);
12506newline_node(NODE *node)
12509 node = remove_begin(node);
12510 nd_set_fl_newline(node);
12516fixpos(NODE *node, NODE *orig)
12520 nd_set_line(node, nd_line(orig));
12524block_append(struct parser_params *p, NODE *head, NODE *tail)
12526 NODE *end, *h = head, *nd;
12528 if (tail == 0) return head;
12530 if (h == 0) return tail;
12531 switch (nd_type(h)) {
12533 h = end = NEW_BLOCK(head, &head->nd_loc);
12537 end = RNODE_BLOCK(h)->nd_end;
12541 nd = RNODE_BLOCK(end)->nd_head;
12542 switch (nd_type(nd)) {
12548 rb_warning0L(nd_line(tail), "statement not reached");
12555 if (!nd_type_p(tail, NODE_BLOCK)) {
12556 tail = NEW_BLOCK(tail, &tail->nd_loc);
12558 RNODE_BLOCK(end)->nd_next = tail;
12559 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12560 nd_set_last_loc(head, nd_last_loc(tail));
12564/* append item to the list */
12566list_append(struct parser_params *p, NODE *list, NODE *item)
12570 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12571 if (RNODE_LIST(list)->nd_next) {
12572 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12578 RNODE_LIST(list)->as.nd_alen += 1;
12579 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12580 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12582 nd_set_last_loc(list, nd_last_loc(item));
12587/* concat two lists */
12589list_concat(NODE *head, NODE *tail)
12593 if (RNODE_LIST(head)->nd_next) {
12594 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12600 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12601 RNODE_LIST(last)->nd_next = tail;
12602 if (RNODE_LIST(tail)->nd_next) {
12603 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12606 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12609 nd_set_last_loc(head, nd_last_loc(tail));
12615literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12617 if (!tail) return 1;
12618 if (!rb_parser_enc_compatible(p, head, tail)) {
12619 compile_error(p, "string literal encodings differ (%s / %s)",
12620 rb_enc_name(rb_parser_str_get_encoding(head)),
12621 rb_enc_name(rb_parser_str_get_encoding(tail)));
12622 rb_parser_str_resize(p, head, 0);
12623 rb_parser_str_resize(p, tail, 0);
12626 rb_parser_str_buf_append(p, head, tail);
12630static rb_parser_string_t *
12631string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12633 if (htype != NODE_DSTR) return NULL;
12634 if (RNODE_DSTR(head)->nd_next) {
12635 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12636 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12638 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12644static rb_parser_string_t *
12645rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12647 rb_parser_string_t *copy;
12648 if (!orig) return NULL;
12649 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12650 copy->coderange = orig->coderange;
12651 copy->enc = orig->enc;
12656/* concat two string literals */
12658literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12660 enum node_type htype;
12661 rb_parser_string_t *lit;
12663 if (!head) return tail;
12664 if (!tail) return head;
12666 htype = nd_type(head);
12667 if (htype == NODE_EVSTR) {
12668 head = new_dstr(p, head, loc);
12671 if (p->heredoc_indent > 0) {
12674 head = str2dstr(p, head);
12676 return list_append(p, head, tail);
12681 switch (nd_type(tail)) {
12683 if ((lit = string_literal_head(p, htype, head)) != false) {
12687 lit = RNODE_DSTR(head)->string;
12689 if (htype == NODE_STR) {
12690 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12692 rb_discard_node(p, head);
12693 rb_discard_node(p, tail);
12696 rb_discard_node(p, tail);
12699 list_append(p, head, tail);
12704 if (htype == NODE_STR) {
12705 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12707 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12708 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12709 RNODE_STR(head)->string = NULL;
12710 rb_discard_node(p, head);
12713 else if (!RNODE_DSTR(tail)->string) {
12715 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12716 if (!RNODE_DSTR(head)->nd_next) {
12717 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12719 else if (RNODE_DSTR(tail)->nd_next) {
12720 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12721 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12723 rb_discard_node(p, tail);
12725 else if ((lit = string_literal_head(p, htype, head)) != false) {
12726 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12728 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12729 RNODE_DSTR(tail)->string = 0;
12733 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));
12734 RNODE_DSTR(tail)->string = 0;
12739 if (htype == NODE_STR) {
12740 head = str2dstr(p, head);
12741 RNODE_DSTR(head)->as.nd_alen = 1;
12743 list_append(p, head, tail);
12750nd_copy_flag(NODE *new_node, NODE *old_node)
12752 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12753 nd_set_line(new_node, nd_line(old_node));
12754 new_node->nd_loc = old_node->nd_loc;
12755 new_node->node_id = old_node->node_id;
12759str2dstr(struct parser_params *p, NODE *node)
12761 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12762 nd_copy_flag(new_node, node);
12763 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12764 RNODE_DSTR(new_node)->as.nd_alen = 0;
12765 RNODE_DSTR(new_node)->nd_next = 0;
12766 RNODE_STR(node)->string = 0;
12772str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12774 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12775 nd_copy_flag(new_node, node);
12776 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12777 RNODE_REGX(new_node)->options = options;
12778 nd_set_loc(new_node, loc);
12779 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12780 RNODE_REGX(new_node)->content_loc = *content_loc;
12781 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12782 RNODE_STR(node)->string = 0;
12788evstr2dstr(struct parser_params *p, NODE *node)
12790 if (nd_type_p(node, NODE_EVSTR)) {
12791 node = new_dstr(p, node, &node->nd_loc);
12797new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12802 switch (nd_type(node)) {
12804 return str2dstr(p, node);
12811 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12815new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12817 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12818 return list_append(p, dstr, node);
12822call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12823 const YYLTYPE *op_loc, const YYLTYPE *loc)
12826 value_expr(p, recv);
12827 value_expr(p, arg1);
12828 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12829 nd_set_line(expr, op_loc->beg_pos.lineno);
12834call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12837 value_expr(p, recv);
12838 opcall = NEW_OPCALL(recv, id, 0, loc);
12839 nd_set_line(opcall, op_loc->beg_pos.lineno);
12844new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12846 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12847 nd_set_line(qcall, op_loc->beg_pos.lineno);
12852new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12855 if (block) block_dup_check(p, args, block);
12856 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12857 if (block) ret = method_add_block(p, ret, block, loc);
12862static rb_locations_lambda_body_t*
12863new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12865 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12867 body->opening_loc = *opening_loc;
12868 body->closing_loc = *closing_loc;
12873command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12876 enum node_type type = nd_type(m);
12879 compile_error(p, "block given to yield");
12882 body = &RNODE_RETURN(m)->nd_stts;
12886 body = &RNODE_EXITS(m)->nd_stts;
12888 case NODE_REDO: /* `redo` has no argument */
12889 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12891 case NODE_RETRY: /* `retry` has no argument */
12892 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12895 block_dup_check(p, get_nd_args(p, m), b);
12896 return method_add_block(p, m, b, loc);
12898 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12899 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12900 *body = command_add_block(p, *body, b, &b_loc);
12901 m->nd_loc.end_pos = loc->end_pos;
12905#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12908last_expr_once_body(NODE *node)
12910 if (!node) return 0;
12911 return nd_once_body(node);
12915match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12918 int line = op_loc->beg_pos.lineno;
12920 value_expr(p, node1);
12921 value_expr(p, node2);
12923 if ((n = last_expr_once_body(node1)) != 0) {
12924 switch (nd_type(n)) {
12927 NODE *match = NEW_MATCH2(node1, node2, loc);
12928 nd_set_line(match, line);
12934 const VALUE lit = rb_node_regx_string_val(n);
12936 NODE *match = NEW_MATCH2(node1, node2, loc);
12937 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12938 nd_set_line(match, line);
12945 if ((n = last_expr_once_body(node2)) != 0) {
12948 switch (nd_type(n)) {
12950 match3 = NEW_MATCH3(node2, node1, loc);
12955 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12956 nd_set_line(n, line);
12960# if WARN_PAST_SCOPE
12962past_dvar_p(struct parser_params *p, ID id)
12964 struct vtable *past = p->lvtbl->past;
12966 if (vtable_included(past, id)) return 1;
12974numparam_nested_p(struct parser_params *p)
12976 struct local_vars *local = p->lvtbl;
12977 NODE *outer = local->numparam.outer;
12978 NODE *inner = local->numparam.inner;
12979 if (outer || inner) {
12980 NODE *used = outer ? outer : inner;
12981 compile_error(p, "numbered parameter is already used in %s block\n"
12982 "%s:%d: numbered parameter is already used here",
12983 outer ? "outer" : "inner",
12984 p->ruby_sourcefile, nd_line(used));
12985 parser_show_error_line(p, &used->nd_loc);
12992numparam_used_p(struct parser_params *p)
12994 NODE *numparam = p->lvtbl->numparam.current;
12996 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12997 "%s:%d: numbered parameter is already used here",
12998 p->ruby_sourcefile, nd_line(numparam));
12999 parser_show_error_line(p, &numparam->nd_loc);
13006it_used_p(struct parser_params *p)
13008 NODE *it = p->lvtbl->it;
13010 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
13011 "%s:%d: 'it' is already used here",
13012 p->ruby_sourcefile, nd_line(it));
13013 parser_show_error_line(p, &it->nd_loc);
13020gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13026 return NEW_SELF(loc);
13028 return NEW_NIL(loc);
13030 return NEW_TRUE(loc);
13031 case keyword_false:
13032 return NEW_FALSE(loc);
13033 case keyword__FILE__:
13035 VALUE file = p->ruby_sourcefile_string;
13037 file = rb_str_new(0, 0);
13038 node = NEW_FILE(file, loc);
13041 case keyword__LINE__:
13042 return NEW_LINE(loc);
13043 case keyword__ENCODING__:
13044 return NEW_ENCODING(loc);
13047 switch (id_type(id)) {
13049 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13050 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13051 if (vidp) *vidp |= LVAR_USED;
13052 node = NEW_DVAR(id, loc);
13055 if (local_id_ref(p, id, &vidp)) {
13056 if (vidp) *vidp |= LVAR_USED;
13057 node = NEW_LVAR(id, loc);
13060 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13061 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13062 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13063 node = NEW_DVAR(id, loc);
13064 struct local_vars *local = p->lvtbl;
13065 if (!local->numparam.current) local->numparam.current = node;
13068# if WARN_PAST_SCOPE
13069 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13070 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13073 /* method call without arguments */
13074 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13075 if (numparam_used_p(p)) return 0;
13076 if (p->max_numparam == ORDINAL_PARAM) {
13077 compile_error(p, "ordinary parameter is defined");
13081 p->it_id = idItImplicit;
13082 vtable_add(p->lvtbl->args, p->it_id);
13084 NODE *node = NEW_DVAR(p->it_id, loc);
13085 if (!p->lvtbl->it) p->lvtbl->it = node;
13088 return NEW_VCALL(id, loc);
13090 return NEW_GVAR(id, loc);
13092 return NEW_IVAR(id, loc);
13094 return NEW_CONST(id, loc);
13096 return NEW_CVAR(id, loc);
13098 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13102static rb_node_opt_arg_t *
13103opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13105 rb_node_opt_arg_t *opts = opt_list;
13106 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13108 while (opts->nd_next) {
13109 opts = opts->nd_next;
13110 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13112 opts->nd_next = opt;
13117static rb_node_kw_arg_t *
13118kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13121 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13122 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13128new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13130 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13131 p->ctxt.has_trailing_semicolon = 0;
13135 if (nd_type_p(n, NODE_BEGIN)) {
13136 n = RNODE_BEGIN(n)->nd_body;
13138 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13139 n = RNODE_BLOCK(n)->nd_head;
13146 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13147 NODE *block = NEW_BLOCK(expr, loc);
13148 return NEW_DEFINED(block, loc, keyword_loc);
13151 return NEW_DEFINED(n, loc, keyword_loc);
13155str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13158 rb_parser_string_t *str = RNODE_STR(node)->string;
13159 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13160 yyerror1(loc, "invalid symbol");
13164 lit = rb_str_new_parser_string(str);
13166 return NEW_SYM(lit, loc);
13170symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13172 enum node_type type = nd_type(symbol);
13175 nd_set_type(symbol, NODE_DSYM);
13178 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13181 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13183 return list_append(p, symbols, symbol);
13187dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13189 if (dreg->string) {
13190 reg_fragment_setenc(p, dreg->string, options);
13192 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13193 NODE *frag = list->nd_head;
13194 if (nd_type_p(frag, NODE_STR)) {
13195 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13197 else if (nd_type_p(frag, NODE_DSTR)) {
13198 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13204new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13207 /* Check string is valid regex */
13208 rb_parser_string_t *str = STRING_NEW0();
13209 reg_compile(p, str, options);
13210 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13213 switch (nd_type(node)) {
13216 /* Check string is valid regex */
13217 reg_compile(p, RNODE_STR(node)->string, options);
13218 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13222 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13225 nd_set_type(node, NODE_DREGX);
13226 nd_set_loc(node, loc);
13227 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13228 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13229 if (dreg->nd_next) {
13230 dregex_fragment_setenc(p, dreg, options);
13232 if (options & RE_OPTION_ONCE) {
13233 node = NEW_ONCE(node, loc);
13240static rb_node_kw_arg_t *
13241new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13244 return NEW_KW_ARG((k), loc);
13248new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13251 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13254 switch (nd_type(node)) {
13256 nd_set_type(node, NODE_XSTR);
13257 nd_set_loc(node, loc);
13260 nd_set_type(node, NODE_DXSTR);
13261 nd_set_loc(node, loc);
13264 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13271struct st_hash_type literal_type = {
13276static int nd_type_st_key_enable_p(NODE *node);
13279check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13281 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13282 if (!arg || !p->case_labels) return;
13283 if (!nd_type_st_key_enable_p(arg)) return;
13285 if (p->case_labels == CHECK_LITERAL_WHEN) {
13286 p->case_labels = st_init_table(&literal_type);
13290 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13291 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13292 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13296 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13301id_is_var(struct parser_params *p, ID id)
13303 if (is_notop_id(id)) {
13304 switch (id & ID_SCOPE_MASK) {
13305 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13308 if (dyna_in_block(p)) {
13309 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13311 if (local_id(p, id)) return 1;
13312 /* method call without arguments */
13316 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13321static inline enum lex_state_e
13322parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13325 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13327 return p->lex.state = ls;
13332flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13334 VALUE mesg = p->debug_buffer;
13336 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13337 p->debug_buffer = Qnil;
13338 rb_io_puts(1, &mesg, out);
13340 if (!NIL_P(str) && RSTRING_LEN(str)) {
13341 rb_io_write(p->debug_output, str);
13345static const char rb_parser_lex_state_names[][8] = {
13346 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13347 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13348 "LABEL", "LABELED","FITEM",
13352append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13355 unsigned int mask = 1;
13356 static const char none[] = "NONE";
13358 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13359 if ((unsigned)state & mask) {
13361 rb_str_cat(buf, "|", 1);
13364 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13368 rb_str_cat(buf, none, sizeof(none)-1);
13374rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13375 enum lex_state_e to, int line)
13378 mesg = rb_str_new_cstr("lex_state: ");
13379 append_lex_state_name(p, from, mesg);
13380 rb_str_cat_cstr(mesg, " -> ");
13381 append_lex_state_name(p, to, mesg);
13382 rb_str_catf(mesg, " at line %d\n", line);
13383 flush_debug_buffer(p, p->debug_output, mesg);
13388rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13390 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13394append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13397 rb_str_cat_cstr(mesg, "0");
13400 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13401 for (; mask && !(stack & mask); mask >>= 1) continue;
13402 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13407rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13408 const char *name, int line)
13410 VALUE mesg = rb_sprintf("%s: ", name);
13411 append_bitstack_value(p, stack, mesg);
13412 rb_str_catf(mesg, " at line %d\n", line);
13413 flush_debug_buffer(p, p->debug_output, mesg);
13417rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13420 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13423 rb_str_vcatf(mesg, fmt, ap);
13425 yyerror0(RSTRING_PTR(mesg));
13428 mesg = rb_str_new(0, 0);
13429 append_lex_state_name(p, p->lex.state, mesg);
13430 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13431 rb_str_resize(mesg, 0);
13432 append_bitstack_value(p, p->cond_stack, mesg);
13433 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13434 rb_str_resize(mesg, 0);
13435 append_bitstack_value(p, p->cmdarg_stack, mesg);
13436 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13437 if (p->debug_output == rb_ractor_stdout())
13438 p->debug_output = rb_ractor_stderr();
13443rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13445 yylloc->beg_pos.lineno = sourceline;
13446 yylloc->beg_pos.column = beg_pos;
13447 yylloc->end_pos.lineno = sourceline;
13448 yylloc->end_pos.column = end_pos;
13453rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13455 int sourceline = here->sourceline;
13456 int beg_pos = parser_column(here->offset - here->quote
13457 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT)));
13458 int end_pos = parser_column(here->offset + here->length + here->quote);
13460 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13464rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13466 yylloc->beg_pos.lineno = p->delayed.beg_line;
13467 yylloc->beg_pos.column = p->delayed.beg_col;
13468 yylloc->end_pos.lineno = p->delayed.end_line;
13469 yylloc->end_pos.column = p->delayed.end_col;
13475rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13477 int sourceline = p->ruby_sourceline;
13478 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13479 int end_pos = parser_column(p->lex.pend - p->lex.pbeg);
13480 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13484rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13486 yylloc->end_pos = yylloc->beg_pos;
13492rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13494 int sourceline = p->ruby_sourceline;
13495 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13496 int end_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13497 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13501rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13503 int sourceline = p->ruby_sourceline;
13504 int beg_pos = parser_column(p->lex.ptok - p->lex.pbeg);
13505 int end_pos = parser_column(p->lex.pcur - p->lex.pbeg);
13506 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13508#endif /* !RIPPER */
13511assignable0(struct parser_params *p, ID id, const char **err)
13513 if (!id) return -1;
13516 *err = "Can't change the value of self";
13519 *err = "Can't assign to nil";
13522 *err = "Can't assign to true";
13524 case keyword_false:
13525 *err = "Can't assign to false";
13527 case keyword__FILE__:
13528 *err = "Can't assign to __FILE__";
13530 case keyword__LINE__:
13531 *err = "Can't assign to __LINE__";
13533 case keyword__ENCODING__:
13534 *err = "Can't assign to __ENCODING__";
13537 switch (id_type(id)) {
13539 if (dyna_in_block(p)) {
13540 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13541 compile_error(p, "Can't assign to numbered parameter _%d",
13542 NUMPARAM_ID_TO_IDX(id));
13545 if (dvar_curr(p, id)) return NODE_DASGN;
13546 if (dvar_defined(p, id)) return NODE_DASGN;
13547 if (local_id(p, id)) return NODE_LASGN;
13552 if (!local_id(p, id)) local_var(p, id);
13556 case ID_GLOBAL: return NODE_GASGN;
13557 case ID_INSTANCE: return NODE_IASGN;
13559 if (!p->ctxt.in_def) return NODE_CDECL;
13560 *err = "dynamic constant assignment";
13562 case ID_CLASS: return NODE_CVASGN;
13564 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13570assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13572 const char *err = 0;
13573 int node_type = assignable0(p, id, &err);
13574 switch (node_type) {
13575 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13576 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13577 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13578 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13579 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13580 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13584 if (err) yyerror1(loc, err);
13586 if (err) set_value(assign_error(p, err, p->s_lvalue));
13588 return NEW_ERROR(loc);
13592is_private_local_id(struct parser_params *p, ID name)
13595 if (name == idUScore) return 1;
13596 if (!is_local_id(name)) return 0;
13597 s = rb_id2str(name);
13599 return RSTRING_PTR(s)[0] == '_';
13603shadowing_lvar_0(struct parser_params *p, ID name)
13605 if (dyna_in_block(p)) {
13606 if (dvar_curr(p, name)) {
13607 if (is_private_local_id(p, name)) return 1;
13608 yyerror0("duplicated argument name");
13610 else if (dvar_defined(p, name) || local_id(p, name)) {
13611 vtable_add(p->lvtbl->vars, name);
13612 if (p->lvtbl->used) {
13613 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13619 if (local_id(p, name)) {
13620 if (is_private_local_id(p, name)) return 1;
13621 yyerror0("duplicated argument name");
13628shadowing_lvar(struct parser_params *p, ID name)
13630 shadowing_lvar_0(p, name);
13635new_bv(struct parser_params *p, ID name)
13638 if (!is_local_id(name)) {
13639 compile_error(p, "invalid local variable - %"PRIsVALUE,
13643 if (!shadowing_lvar_0(p, name)) return;
13646 if (dvar_defined_ref(p, name, &vidp)) {
13647 if (vidp) *vidp |= LVAR_USED;
13652aryset_check(struct parser_params *p, NODE *args)
13654 NODE *block = 0, *kwds = 0;
13655 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13656 block = RNODE_BLOCK_PASS(args)->nd_body;
13657 args = RNODE_BLOCK_PASS(args)->nd_head;
13659 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13660 args = RNODE_ARGSCAT(args)->nd_body;
13662 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13663 kwds = RNODE_ARGSPUSH(args)->nd_body;
13666 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13667 next = RNODE_LIST(next)->nd_next) {
13668 kwds = RNODE_LIST(next)->nd_head;
13671 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13672 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13675 yyerror1(&block->nd_loc, "block arg given in index assignment");
13680aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13682 aryset_check(p, idx);
13683 return NEW_ATTRASGN(recv, tASET, idx, loc);
13687block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13689 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13690 compile_error(p, "both block arg and actual block given");
13695attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13697 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13698 return NEW_ATTRASGN(recv, id, 0, loc);
13702rb_backref_error(struct parser_params *p, NODE *node)
13705# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13707# define ERR(...) rb_sprintf(__VA_ARGS__)
13709 switch (nd_type(node)) {
13711 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13712 case NODE_BACK_REF:
13713 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13716 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13720arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13722 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13723 switch (nd_type(node1)) {
13725 return list_append(p, node1, node2);
13726 case NODE_BLOCK_PASS:
13727 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13728 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13730 case NODE_ARGSPUSH:
13731 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13732 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13733 nd_set_type(node1, NODE_ARGSCAT);
13736 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13737 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13738 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13741 return NEW_ARGSPUSH(node1, node2, loc);
13745arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13747 if (!node2) return node1;
13748 switch (nd_type(node1)) {
13749 case NODE_BLOCK_PASS:
13750 if (RNODE_BLOCK_PASS(node1)->nd_head)
13751 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13753 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13755 case NODE_ARGSPUSH:
13756 if (!nd_type_p(node2, NODE_LIST)) break;
13757 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13758 nd_set_type(node1, NODE_ARGSCAT);
13761 if (!nd_type_p(node2, NODE_LIST) ||
13762 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13763 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13766 return NEW_ARGSCAT(node1, node2, loc);
13770last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13773 if ((n1 = splat_array(args)) != 0) {
13774 return list_append(p, n1, last_arg);
13776 return arg_append(p, args, last_arg, loc);
13780rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13783 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13784 return list_concat(n1, rest_arg);
13786 return arg_concat(p, args, rest_arg, loc);
13790splat_array(NODE* node)
13792 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13793 if (nd_type_p(node, NODE_LIST)) return node;
13798mark_lvar_used(struct parser_params *p, NODE *rhs)
13802 switch (nd_type(rhs)) {
13804 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13805 if (vidp) *vidp |= LVAR_USED;
13809 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13810 if (vidp) *vidp |= LVAR_USED;
13815 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13816 mark_lvar_used(p, rhs->nd_head);
13823static int is_static_content(NODE *node);
13826node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13828 if (!lhs) return 0;
13830 switch (nd_type(lhs)) {
13838 set_nd_value(p, lhs, rhs);
13839 nd_set_loc(lhs, loc);
13842 case NODE_ATTRASGN:
13843 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13844 nd_set_loc(lhs, loc);
13848 /* should not happen */
13856value_expr_check(struct parser_params *p, NODE *node)
13858 NODE *void_node = 0, *vn;
13861 rb_warning0("empty expression");
13864 switch (nd_type(node)) {
13866 vn = RNODE_ENSURE(node)->nd_head;
13867 node = RNODE_ENSURE(node)->nd_ensr;
13868 /* nd_ensr should not be NULL, check it out next */
13869 if (vn && (vn = value_expr_check(p, vn))) {
13875 /* void only if all children are void */
13876 vn = RNODE_RESCUE(node)->nd_head;
13877 if (!vn || !(vn = value_expr_check(p, vn))) {
13878 if (!RNODE_RESCUE(node)->nd_else) return NULL;
13880 if (!void_node) void_node = vn;
13881 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13882 if (!nd_type_p(r, NODE_RESBODY)) {
13883 compile_error(p, "unexpected node");
13886 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13889 if (!void_node) void_node = vn;
13891 node = RNODE_RESCUE(node)->nd_else;
13892 if (!node) return void_node;
13904 for (node = RNODE_CASE(node)->nd_body;
13905 node && nd_type_p(node, NODE_WHEN);
13906 node = RNODE_WHEN(node)->nd_next) {
13907 if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) {
13910 if (!void_node) void_node = vn;
13916 NODE *in = RNODE_CASE3(node)->nd_body;
13917 if (!in || !nd_type_p(in, NODE_IN)) {
13918 compile_error(p, "unexpected node");
13921 if (!RNODE_IN(in)->nd_body) {
13922 /* single line pattern matching with "=>" operator */
13926 vn = value_expr_check(p, RNODE_IN(in)->nd_body);
13927 if (!vn) return NULL;
13928 if (!void_node) void_node = vn;
13929 in = RNODE_IN(in)->nd_next;
13930 } while (in && nd_type_p(in, NODE_IN));
13931 node = in; /* else */
13936 while (RNODE_BLOCK(node)->nd_next) {
13937 vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head);
13939 node = RNODE_BLOCK(node)->nd_next;
13941 node = RNODE_BLOCK(node)->nd_head;
13945 node = RNODE_BEGIN(node)->nd_body;
13950 if (!RNODE_IF(node)->nd_body) {
13953 else if (!RNODE_IF(node)->nd_else) {
13956 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13957 if (!vn) return NULL;
13958 if (!void_node) void_node = vn;
13959 node = RNODE_IF(node)->nd_else;
13964 /* The left operand was already checked for a value when logop()
13965 * built this node, so stop here instead of re-reporting the same
13966 * void value once per operator in a chain. */
13972 mark_lvar_used(p, node);
13983 /* return the first found node */
13984 return void_node ? void_node : node;
13988value_expr(struct parser_params *p, NODE *node)
13990 NODE *void_node = value_expr_check(p, node);
13992 yyerror1(&void_node->nd_loc, "void value expression");
13993 /* or "control never reach"? */
14000void_expr(struct parser_params *p, NODE *node)
14002 const char *useless = 0;
14004 if (!RTEST(ruby_verbose)) return;
14006 if (!node || !(node = nd_once_body(node))) return;
14007 switch (nd_type(node)) {
14009 switch (RNODE_OPCALL(node)->nd_mid) {
14028 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
14039 case NODE_BACK_REF:
14040 useless = "a variable";
14043 useless = "a constant";
14048 case NODE_ENCODING:
14051 case NODE_RATIONAL:
14052 case NODE_IMAGINARY:
14057 useless = "a literal";
14082 useless = "defined?";
14087 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14091/* warns useless use of block and returns the last statement node */
14093void_stmts(struct parser_params *p, NODE *node)
14095 NODE *const n = node;
14096 if (!RTEST(ruby_verbose)) return n;
14097 if (!node) return n;
14098 if (!nd_type_p(node, NODE_BLOCK)) return n;
14100 while (RNODE_BLOCK(node)->nd_next) {
14101 void_expr(p, RNODE_BLOCK(node)->nd_head);
14102 node = RNODE_BLOCK(node)->nd_next;
14104 return RNODE_BLOCK(node)->nd_head;
14108remove_begin(NODE *node)
14110 NODE **n = &node, *n1 = node;
14111 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14112 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14118reduce_nodes(struct parser_params *p, NODE **body)
14120 NODE *node = *body;
14123 *body = NEW_NIL(&NULL_LOC);
14126#define subnodes(type, n1, n2) \
14127 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14128 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14129 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14132 int newline = (int)nd_fl_newline(node);
14133 switch (nd_type(node)) {
14135 // Keep an explicit nil in a method's tail (value) position when it
14136 // is on its own line, so it still emits a :line event and records
14137 // line coverage for that line. [Bug #22302]
14138 if (newline) return;
14143 *body = node = RNODE_BEGIN(node)->nd_body;
14144 if (newline && node) nd_set_fl_newline(node);
14147 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14151 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14154 body = &RNODE_CASE(node)->nd_body;
14157 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14160 body = &RNODE_ENSURE(node)->nd_head;
14163 newline = 0; // RESBODY should not be a NEWLINE
14164 if (RNODE_RESCUE(node)->nd_else) {
14165 body = &RNODE_RESCUE(node)->nd_resq;
14168 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14174 if (newline && node) nd_set_fl_newline(node);
14181is_static_content(NODE *node)
14183 if (!node) return 1;
14184 switch (nd_type(node)) {
14186 if (!(node = RNODE_HASH(node)->nd_head)) break;
14189 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14190 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14195 case NODE_ENCODING:
14198 case NODE_RATIONAL:
14199 case NODE_IMAGINARY:
14213assign_in_cond(struct parser_params *p, NODE *node)
14215 switch (nd_type(node)) {
14229 if (!get_nd_value(p, node)) return 1;
14230 if (is_static_content(get_nd_value(p, node))) {
14231 /* reports always */
14232 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14243#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14245 case COND_IN_OP: break; \
14246 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14247 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14251static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14254range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14256 enum node_type type;
14258 if (node == 0) return 0;
14260 type = nd_type(node);
14261 value_expr(p, node);
14262 if (type == NODE_INTEGER) {
14263 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14264 ID lineno = rb_intern("$.");
14265 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14267 return cond0(p, node, COND_IN_FF, loc, true);
14271cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14273 if (node == 0) return 0;
14274 if (!(node = nd_once_body(node))) return 0;
14275 assign_in_cond(p, node);
14277 switch (nd_type(node)) {
14279 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14286 SWITCH_BY_COND_TYPE(type, warn, "string ");
14290 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14291 nd_set_type(node, NODE_MATCH);
14295 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14297 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14301 NODE *end = RNODE_BLOCK(node)->nd_end;
14302 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14303 if (top) top = node == end;
14304 *expr = cond0(p, *expr, type, loc, top);
14310 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14311 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14317 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14318 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14319 switch (nd_type(node)) {
14321 nd_set_type(node,NODE_FLIP2);
14322 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14326 nd_set_type(node, NODE_FLIP3);
14327 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14335 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14339 case NODE_ENCODING:
14342 case NODE_RATIONAL:
14343 case NODE_IMAGINARY:
14344 SWITCH_BY_COND_TYPE(type, warning, "");
14354cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14356 if (node == 0) return 0;
14357 return cond0(p, node, COND_IN_COND, loc, true);
14361method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14363 if (node == 0) return 0;
14364 return cond0(p, node, COND_IN_OP, loc, true);
14368new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14370 YYLTYPE loc = {*pos, *pos};
14371 return NEW_NIL(&loc);
14375new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc)
14377 if (!cc) return right;
14378 cc = cond0(p, cc, COND_IN_COND, loc, true);
14379 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14383new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc)
14385 if (!cc) return right;
14386 cc = cond0(p, cc, COND_IN_COND, loc, true);
14387 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14390#define NEW_AND_OR(type, f, s, loc, op_loc) (type == NODE_AND ? NEW_AND(f,s,loc,op_loc) : NEW_OR(f,s,loc,op_loc))
14392/* A cached logop tail is usable only if it is still a node of the chain's type
14393 * whose nd_2nd is the insertion point, i.e. not itself another node of that
14394 * type. This rejects a stale cache left over from an earlier parse. */
14396logop_valid_tail(NODE *tail, enum node_type type)
14399 return tail && nd_type_p(tail, type) &&
14400 ((second = RNODE_AND(tail)->nd_2nd) == 0 || !nd_type_p(second, type));
14404logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14405 const YYLTYPE *op_loc, const YYLTYPE *loc)
14407 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14409 /* Snapshot the cache from the previous logop() call before overwriting it. */
14410 NODE *prev_head = p->logop.head;
14411 NODE *prev_tail = p->logop.tail;
14412 NODE *node, *second;
14413 value_expr(p, left);
14414 if (left && nd_type_p(left, type)) {
14415 /* The insertion point is the far end of left's right branch.
14416 * Reusing the cached tail keeps a chain such as `a && a && ... && a`
14417 * linear instead of rescanning the whole branch on every operator. The
14418 * cache is validated (its nd_2nd is not another node of the same type)
14419 * so a stale entry falls back to the scan. */
14420 if (left == prev_head && logop_valid_tail(prev_tail, type)) {
14425 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14429 second = RNODE_AND(node)->nd_2nd;
14430 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14431 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14432 left->nd_loc.end_pos = loc->end_pos;
14433 p->logop.head = left;
14434 p->logop.tail = RNODE_AND(node)->nd_2nd;
14437 op = NEW_AND_OR(type, left, right, loc, op_loc);
14438 nd_set_line(op, op_loc->beg_pos.lineno);
14439 /* Record where the next operator will extend op, mirroring the scan the
14440 * chained branch above would perform: if right is itself a chain of the
14441 * same type (from parentheses), its far end; otherwise op itself. */
14442 p->logop.head = op;
14443 if (right == prev_head && logop_valid_tail(prev_tail, type)) {
14444 p->logop.tail = prev_tail;
14446 else if (right && nd_type_p(right, type)) {
14448 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14451 p->logop.tail = node;
14454 p->logop.tail = op;
14462no_blockarg(struct parser_params *p, NODE *node)
14464 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14465 compile_error(p, "block argument should not be given");
14470ret_args(struct parser_params *p, NODE *node)
14473 no_blockarg(p, node);
14474 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14475 node = RNODE_LIST(node)->nd_head;
14482negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14484 switch (nd_type(node)) {
14486 RNODE_INTEGER(node)->minus = TRUE;
14489 RNODE_FLOAT(node)->minus = TRUE;
14491 case NODE_RATIONAL:
14492 RNODE_RATIONAL(node)->minus = TRUE;
14494 case NODE_IMAGINARY:
14495 RNODE_IMAGINARY(node)->minus = TRUE;
14498 node->nd_loc = *loc;
14503arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14506 if (!node1) return (NODE *)node2;
14507 node2->nd_head = node1;
14508 nd_set_first_lineno(node2, nd_first_lineno(node1));
14509 nd_set_first_column(node2, nd_first_column(node1));
14510 return (NODE *)node2;
14516args_info_empty_p(struct rb_args_info *args)
14518 if (args->pre_args_num) return false;
14519 if (args->post_args_num) return false;
14520 if (args->rest_arg) return false;
14521 if (args->opt_args) return false;
14522 if (args->block_arg) return false;
14523 if (args->kw_args) return false;
14524 if (args->kw_rest_arg) return false;
14528static rb_node_args_t *
14529new_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)
14531 struct rb_args_info *args = &tail->nd_ainfo;
14533 if (args->forwarding) {
14535 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14538 rest_arg = idFWD_REST;
14541 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14542 args->pre_init = pre_args ? pre_args->nd_next : 0;
14544 args->post_args_num = post_args ? post_args->nd_plen : 0;
14545 args->post_init = post_args ? post_args->nd_next : 0;
14546 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14548 args->rest_arg = rest_arg;
14550 args->opt_args = opt_args;
14552 nd_set_loc(RNODE(tail), loc);
14557static rb_node_args_t *
14558new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14560 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14561 struct rb_args_info *args = &node->nd_ainfo;
14562 if (p->error_p) return node;
14564 if (block == idNil) {
14566 args->no_blockarg = TRUE;
14568 args->block_arg = block;
14569 args->kw_args = kw_args;
14573 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14574 * variable order: k1, kr1, k2, &b, internal_id, krest
14576 * variable order: kr1, k1, k2, internal_id, krest, &b
14578 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14579 struct vtable *vtargs = p->lvtbl->args;
14580 rb_node_kw_arg_t *kwn = kw_args;
14582 if (block) block = vtargs->tbl[vtargs->pos-1];
14583 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14584 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14586 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14588 --required_kw_vars;
14589 kwn = kwn->nd_next;
14592 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14593 ID vid = get_nd_vid(p, kwn->nd_body);
14594 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14595 *required_kw_vars++ = vid;
14602 arg_var(p, kw_bits);
14603 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14604 if (block) arg_var(p, block);
14606 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14608 else if (kw_rest_arg == idNil) {
14609 args->no_kwarg = 1;
14611 else if (kw_rest_arg) {
14612 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14618static rb_node_args_t *
14619args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14621 if (max_numparam > NO_PARAM || it_id) {
14623 YYLTYPE loc = RUBY_INIT_YYLLOC();
14624 args = new_empty_args_tail(p, 0);
14625 nd_set_loc(RNODE(args), &loc);
14627 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14633new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14635 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14638 NODE *pre_args = NEW_LIST(pre_arg, loc);
14639 if (RNODE_ARYPTN(aryptn)->pre_args) {
14640 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14643 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14650new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14653 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14658 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14664new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14666 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14672new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14674 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14675 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14676 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14682new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14684 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14689new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14691 NODE *node, *kw_rest_arg_node;
14693 if (kw_rest_arg == idNil) {
14694 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14696 else if (kw_rest_arg) {
14697 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14700 kw_rest_arg_node = NULL;
14703 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14709dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14712 return NEW_SYM(STR_NEW0(), loc);
14715 switch (nd_type(node)) {
14717 nd_set_type(node, NODE_DSYM);
14718 nd_set_loc(node, loc);
14721 node = str_to_sym_node(p, node, loc);
14724 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14731nd_type_st_key_enable_p(NODE *node)
14733 switch (nd_type(node)) {
14736 case NODE_RATIONAL:
14737 case NODE_IMAGINARY:
14743 case NODE_ENCODING:
14751nd_value(struct parser_params *p, NODE *node)
14753 switch (nd_type(node)) {
14755 return rb_node_str_string_val(node);
14757 return rb_node_integer_literal_val(node);
14759 return rb_node_float_literal_val(node);
14760 case NODE_RATIONAL:
14761 return rb_node_rational_literal_val(node);
14762 case NODE_IMAGINARY:
14763 return rb_node_imaginary_literal_val(node);
14765 return rb_node_sym_string_val(node);
14767 return rb_node_regx_string_val(node);
14769 return rb_node_line_lineno_val(node);
14770 case NODE_ENCODING:
14771 return rb_node_encoding_val(node);
14773 return rb_node_file_path_val(node);
14775 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14776 UNREACHABLE_RETURN(0);
14781warn_duplicate_keys(struct parser_params *p, NODE *hash)
14783 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14784 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14785 while (hash && RNODE_LIST(hash)->nd_next) {
14786 NODE *head = RNODE_LIST(hash)->nd_head;
14787 NODE *value = RNODE_LIST(hash)->nd_next;
14788 NODE *next = RNODE_LIST(value)->nd_next;
14792 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14797 if (nd_type_st_key_enable_p(head)) {
14798 key = (st_data_t)head;
14800 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14801 rb_warn2L(nd_line((NODE *)data),
14802 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14803 nd_value(p, head), WARN_I(nd_line(head)));
14805 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14809 st_free_table(p->warn_duplicate_keys_table);
14810 p->warn_duplicate_keys_table = NULL;
14814new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14816 if (hash) warn_duplicate_keys(p, hash);
14817 return NEW_HASH(hash, loc);
14821error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14823 if (is_private_local_id(p, id)) {
14826 if (st_is_member(p->pvtbl, id)) {
14827 yyerror1(loc, "duplicated variable name");
14829 else if (p->ctxt.in_alt_pattern && id) {
14830 yyerror1(loc, "variable capture in alternative pattern");
14833 p->ctxt.capture_in_pattern = 1;
14834 st_insert(p->pvtbl, (st_data_t)id, 0);
14839error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14842 p->pktbl = st_init_numtable();
14844 else if (st_is_member(p->pktbl, key)) {
14845 yyerror1(loc, "duplicated key name");
14848 st_insert(p->pktbl, (st_data_t)key, 0);
14852new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14854 return NEW_HASH(hash, loc);
14858new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14863 ID vid = get_nd_vid(p, lhs);
14864 YYLTYPE lhs_loc = lhs->nd_loc;
14865 /* A constant read can raise NameError. Prism has no separate read node
14866 * for `Const op= value` and reports the whole expression, so give the
14867 * NODE_CONST the location of the whole operator assignment as well,
14868 * for consistent Thread::Backtrace::Location#source_range between both
14870 const YYLTYPE *read_loc = nd_type_p(lhs, NODE_CDECL) ? loc : &lhs_loc;
14872 set_nd_value(p, lhs, rhs);
14873 nd_set_loc(lhs, loc);
14874 asgn = NEW_OP_ASGN_OR(gettable(p, vid, read_loc), lhs, loc);
14876 else if (op == tANDOP) {
14877 set_nd_value(p, lhs, rhs);
14878 nd_set_loc(lhs, loc);
14879 asgn = NEW_OP_ASGN_AND(gettable(p, vid, read_loc), lhs, loc);
14883 rhs = NEW_CALL(gettable(p, vid, read_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14884 set_nd_value(p, asgn, rhs);
14885 nd_set_loc(asgn, loc);
14889 asgn = NEW_ERROR(loc);
14895new_ary_op_assign(struct parser_params *p, NODE *ary,
14896 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14897 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14901 aryset_check(p, args);
14902 args = make_list(args, args_loc);
14903 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14909new_attr_op_assign(struct parser_params *p, NODE *lhs,
14910 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14911 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14915 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14921new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14926 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14929 asgn = NEW_ERROR(loc);
14936const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14938 if (p->ctxt.in_def) {
14940 yyerror1(loc, "dynamic constant assignment");
14942 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14945 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14950assign_error(struct parser_params *p, const char *mesg, VALUE a)
14952 a = dispatch2(assign_error, ERR_MESG(), a);
14959new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14961 NODE *result = head;
14963 NODE *tmp = rescue_else ? rescue_else : rescue;
14964 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14966 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14967 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14970 result = NEW_ENSURE(result, ensure, loc);
14972 fixpos(result, head);
14977warn_unused_var(struct parser_params *p, struct local_vars *local)
14981 if (!local->used) return;
14982 cnt = local->used->pos;
14983 if (cnt != local->vars->pos) {
14984 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14987 ID *v = local->vars->tbl;
14988 ID *u = local->used->tbl;
14989 for (int i = 0; i < cnt; ++i) {
14990 if (!v[i] || (u[i] & LVAR_USED)) continue;
14991 if (is_private_local_id(p, v[i])) continue;
14992 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14998local_push(struct parser_params *p, int toplevel_scope)
15000 struct local_vars *local;
15001 int inherits_dvars = toplevel_scope && compile_for_eval;
15002 int warn_unused_vars = RTEST(ruby_verbose);
15004 local = ALLOC(struct local_vars);
15005 local->prev = p->lvtbl;
15006 local->args = vtable_alloc(0);
15007 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
15009 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
15010 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
15012 local->numparam.outer = 0;
15013 local->numparam.inner = 0;
15014 local->numparam.current = 0;
15016 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
15018# if WARN_PAST_SCOPE
15027vtable_chain_free(struct parser_params *p, struct vtable *table)
15029 while (!DVARS_TERMINAL_P(table)) {
15030 struct vtable *cur_table = table;
15031 table = cur_table->prev;
15032 vtable_free(cur_table);
15037local_free(struct parser_params *p, struct local_vars *local)
15039 vtable_chain_free(p, local->used);
15041# if WARN_PAST_SCOPE
15042 vtable_chain_free(p, local->past);
15045 vtable_chain_free(p, local->args);
15046 vtable_chain_free(p, local->vars);
15048 ruby_xfree_sized(local, sizeof(struct local_vars));
15052local_pop(struct parser_params *p)
15054 struct local_vars *local = p->lvtbl->prev;
15055 if (p->lvtbl->used) {
15056 warn_unused_var(p, p->lvtbl);
15059 local_free(p, p->lvtbl);
15066static rb_ast_id_table_t *
15067local_tbl(struct parser_params *p)
15069 int cnt_args = vtable_size(p->lvtbl->args);
15070 int cnt_vars = vtable_size(p->lvtbl->vars);
15071 int cnt = cnt_args + cnt_vars;
15073 rb_ast_id_table_t *tbl;
15075 if (cnt <= 0) return 0;
15076 tbl = rb_ast_new_local_table(p->ast, cnt);
15077 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
15078 /* remove IDs duplicated to warn shadowing */
15079 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15080 ID id = p->lvtbl->vars->tbl[i];
15081 if (!vtable_included(p->lvtbl->args, id)) {
15082 tbl->ids[j++] = id;
15086 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15093numparam_name(struct parser_params *p, ID id)
15095 if (!NUMPARAM_ID_P(id)) return;
15096 compile_error(p, "_%d is reserved for numbered parameter",
15097 NUMPARAM_ID_TO_IDX(id));
15101arg_var(struct parser_params *p, ID id)
15103 numparam_name(p, id);
15104 vtable_add(p->lvtbl->args, id);
15108local_var(struct parser_params *p, ID id)
15110 numparam_name(p, id);
15111 vtable_add(p->lvtbl->vars, id);
15112 if (p->lvtbl->used) {
15113 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15119rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15121 return rb_local_defined(id, iseq);
15126local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15128 struct vtable *vars, *args, *used;
15130 vars = p->lvtbl->vars;
15131 args = p->lvtbl->args;
15132 used = p->lvtbl->used;
15134 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15137 if (used) used = used->prev;
15140 if (vars && vars->prev == DVARS_INHERIT) {
15141 return rb_parser_local_defined(p, id, p->parent_iseq);
15143 else if (vtable_included(args, id)) {
15147 int i = vtable_included(vars, id);
15148 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15154local_id(struct parser_params *p, ID id)
15156 return local_id_ref(p, id, NULL);
15160check_forwarding_args(struct parser_params *p)
15162 if (local_id(p, idFWD_ALL)) return TRUE;
15163 compile_error(p, "unexpected ...");
15168add_forwarding_args(struct parser_params *p)
15170 arg_var(p, idFWD_REST);
15171 arg_var(p, idFWD_KWREST);
15172 arg_var(p, idFWD_BLOCK);
15173 arg_var(p, idFWD_ALL);
15177forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15179 bool conflict = false;
15181 struct vtable *vars, *args;
15183 vars = p->lvtbl->vars;
15184 args = p->lvtbl->args;
15186 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15187 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15192 bool found = false;
15193 if (vars && vars->prev == DVARS_INHERIT && !found) {
15194 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15195 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15198 found = (vtable_included(args, arg) &&
15199 !(all && vtable_included(args, all)));
15203 compile_error(p, "no anonymous %s parameter", var);
15205 else if (conflict) {
15206 compile_error(p, "anonymous %s parameter is also used within block", var);
15211new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15213 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15214 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15215 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15216 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15217 block->forwarding = TRUE;
15218 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15219 return arg_blk_pass(args, block);
15223numparam_push(struct parser_params *p)
15225 struct local_vars *local = p->lvtbl;
15226 NODE *inner = local->numparam.inner;
15227 if (!local->numparam.outer) {
15228 local->numparam.outer = local->numparam.current;
15230 local->numparam.inner = 0;
15231 local->numparam.current = 0;
15237numparam_pop(struct parser_params *p, NODE *prev_inner)
15239 struct local_vars *local = p->lvtbl;
15241 /* prefer first one */
15242 local->numparam.inner = prev_inner;
15244 else if (local->numparam.current) {
15245 /* current and inner are exclusive */
15246 local->numparam.inner = local->numparam.current;
15248 if (p->max_numparam > NO_PARAM) {
15249 /* current and outer are exclusive */
15250 local->numparam.current = local->numparam.outer;
15251 local->numparam.outer = 0;
15254 /* no numbered parameter */
15255 local->numparam.current = 0;
15260static const struct vtable *
15261dyna_push(struct parser_params *p)
15263 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15264 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15265 if (p->lvtbl->used) {
15266 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15268 return p->lvtbl->args;
15272dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15274 struct vtable *tmp = *vtblp;
15275 *vtblp = tmp->prev;
15276# if WARN_PAST_SCOPE
15277 if (p->past_scope_enabled) {
15278 tmp->prev = p->lvtbl->past;
15279 p->lvtbl->past = tmp;
15287dyna_pop_1(struct parser_params *p)
15289 struct vtable *tmp;
15291 if ((tmp = p->lvtbl->used) != 0) {
15292 warn_unused_var(p, p->lvtbl);
15293 p->lvtbl->used = p->lvtbl->used->prev;
15296 dyna_pop_vtable(p, &p->lvtbl->args);
15297 dyna_pop_vtable(p, &p->lvtbl->vars);
15301dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15303 while (p->lvtbl->args != lvargs) {
15305 if (!p->lvtbl->args) {
15306 struct local_vars *local = p->lvtbl->prev;
15307 ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl));
15315dyna_in_block(struct parser_params *p)
15317 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15322dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15324 struct vtable *vars, *args, *used;
15327 args = p->lvtbl->args;
15328 vars = p->lvtbl->vars;
15329 used = p->lvtbl->used;
15331 while (!DVARS_TERMINAL_P(vars)) {
15332 if (vtable_included(args, id)) {
15335 if ((i = vtable_included(vars, id)) != 0) {
15336 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15341 if (!vidrefp) used = 0;
15342 if (used) used = used->prev;
15345 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15346 return rb_dvar_defined(id, p->parent_iseq);
15354dvar_defined(struct parser_params *p, ID id)
15356 return dvar_defined_ref(p, id, NULL);
15360dvar_curr(struct parser_params *p, ID id)
15362 return (vtable_included(p->lvtbl->args, id) ||
15363 vtable_included(p->lvtbl->vars, id));
15367reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15370 "regexp encoding option '%c' differs from source encoding '%s'",
15371 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15375static rb_encoding *
15376find_enc(struct parser_params* p, const char *name)
15378 int idx = rb_enc_find_index(name);
15380 rb_bug("unknown encoding name: %s", name);
15383 return rb_enc_from_index(idx);
15386static rb_encoding *
15387kcode_to_enc(struct parser_params* p, int kcode)
15392 case ENC_ASCII8BIT:
15393 enc = rb_ascii8bit_encoding();
15396 enc = find_enc(p, "EUC-JP");
15398 case ENC_Windows_31J:
15399 enc = find_enc(p, "Windows-31J");
15402 enc = rb_utf8_encoding();
15413rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15415 int c = RE_OPTION_ENCODING_IDX(options);
15421 char_to_option_kcode(c, &opt, &idx);
15422 enc = kcode_to_enc(p, idx);
15423 if (enc != rb_parser_str_get_encoding(str) &&
15424 !rb_parser_is_ascii_string(p, str)) {
15427 rb_parser_string_set_encoding(str, enc);
15429 else if (RE_OPTION_ENCODING_NONE(options)) {
15430 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15431 !rb_parser_is_ascii_string(p, str)) {
15435 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15437 else if (rb_is_usascii_enc(p->enc)) {
15438 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15448reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15450 int c = rb_reg_fragment_setenc(p, str, options);
15451 if (c) reg_fragment_enc_error(p, str, c);
15454#ifndef UNIVERSAL_PARSER
15456 struct parser_params* parser;
15459 const YYLTYPE *loc;
15460 rb_parser_assignable_func assignable;
15461} reg_named_capture_assign_t;
15464reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15465 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15467 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15468 struct parser_params* p = arg->parser;
15469 rb_encoding *enc = arg->enc;
15470 long len = name_end - name;
15471 const char *s = (const char *)name;
15473 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15477reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15479 reg_named_capture_assign_t arg;
15482 arg.enc = rb_enc_get(regexp);
15483 arg.succ_block = 0;
15485 arg.assignable = assignable;
15486 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15488 if (!arg.succ_block) return 0;
15489 return RNODE_BLOCK(arg.succ_block)->nd_next;
15495rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15497 return assignable(p, id, val, loc);
15501rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15502 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15507 if (!len) return ST_CONTINUE;
15508 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15509 return ST_CONTINUE;
15511 var = intern_cstr(s, len, enc);
15512 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15513 if (!lvar_defined(p, var)) return ST_CONTINUE;
15515 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15516 succ = *succ_block;
15517 if (!succ) succ = NEW_ERROR(loc);
15518 succ = block_append(p, succ, node);
15519 *succ_block = succ;
15520 return ST_CONTINUE;
15525parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15528 reg_fragment_setenc(p, str, options);
15529 str2 = rb_str_new_parser_string(str);
15530 return rb_parser_reg_compile(p, str2, options);
15535rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15537 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15542reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15547 err = rb_errinfo();
15548 re = parser_reg_compile(p, str, options);
15550 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15551 rb_set_errinfo(err);
15552 compile_error(p, "%"PRIsVALUE, m);
15560rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15562 p->do_print = print;
15564 p->do_chomp = chomp;
15565 p->do_split = split;
15569parser_append_options(struct parser_params *p, NODE *node)
15571 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15572 const YYLTYPE *const LOC = &default_location;
15575 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15576 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15578 node = block_append(p, node, print);
15582 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15585 ID ifs = rb_intern("$;");
15586 ID fields = rb_intern("$F");
15587 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15588 NODE *split = NEW_GASGN(fields,
15589 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15590 rb_intern("split"), args, LOC),
15592 node = block_append(p, split, node);
15595 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15596 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15597 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15600 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15609 /* just to suppress unused-function warnings */
15615internal_id(struct parser_params *p)
15617 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15619#endif /* !RIPPER */
15622parser_initialize(struct parser_params *p)
15624 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15625 p->command_start = TRUE;
15626 p->ruby_sourcefile_string = Qnil;
15627 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15628 string_buffer_init(p);
15630 p->delayed.token = NULL;
15631 p->frozen_string_literal = -1; /* not specified */
15632 rb_source_hash_init(&p->source_hash);
15634 p->error_buffer = Qfalse;
15635 p->end_expect_token_locations = NULL;
15640 p->parsing_thread = Qnil;
15642 p->s_lvalue = Qnil;
15643 p->s_value_stack = rb_ary_new();
15645 p->debug_buffer = Qnil;
15646 p->debug_output = rb_ractor_stdout();
15647 p->enc = rb_utf8_encoding();
15652#define rb_ruby_parser_mark ripper_parser_mark
15653#define rb_ruby_parser_free ripper_parser_free
15654#define rb_ruby_parser_memsize ripper_parser_memsize
15658rb_ruby_parser_mark(void *ptr)
15660 struct parser_params *p = (struct parser_params*)ptr;
15662 rb_gc_mark(p->ruby_sourcefile_string);
15664 rb_gc_mark(p->error_buffer);
15666 rb_gc_mark(p->value);
15667 rb_gc_mark(p->result);
15668 rb_gc_mark(p->parsing_thread);
15669 rb_gc_mark(p->s_value);
15670 rb_gc_mark(p->s_lvalue);
15671 rb_gc_mark(p->s_value_stack);
15673 rb_gc_mark(p->debug_buffer);
15674 rb_gc_mark(p->debug_output);
15678rb_ruby_parser_free(void *ptr)
15680 struct parser_params *p = (struct parser_params*)ptr;
15681 struct local_vars *local, *prev;
15684 rb_ast_free(p->ast);
15687 if (p->warn_duplicate_keys_table) {
15688 st_free_table(p->warn_duplicate_keys_table);
15693 rb_parser_ary_free(p, p->tokens);
15698 ruby_xfree_sized(p->tokenbuf, p->toksiz);
15701 for (local = p->lvtbl; local; local = prev) {
15702 prev = local->prev;
15703 local_free(p, local);
15707 token_info *ptinfo;
15708 while ((ptinfo = p->token_info) != 0) {
15709 p->token_info = ptinfo->next;
15713 string_buffer_free(p);
15716 st_free_table(p->pvtbl);
15719 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15720 st_free_table(p->case_labels);
15723 xfree(p->lex.strterm);
15724 p->lex.strterm = 0;
15730rb_ruby_parser_memsize(const void *ptr)
15732 struct parser_params *p = (struct parser_params*)ptr;
15733 struct local_vars *local;
15734 size_t size = sizeof(*p);
15737 for (local = p->lvtbl; local; local = local->prev) {
15738 size += sizeof(*local);
15739 if (local->vars) size += local->vars->capa * sizeof(ID);
15745#undef rb_reserved_word
15747const struct kwtable *
15748rb_reserved_word(const char *str, unsigned int len)
15750 return reserved_word(str, len);
15753#ifdef UNIVERSAL_PARSER
15755rb_ruby_parser_allocate(const rb_parser_config_t *config)
15757 /* parser_initialize expects fields to be set to 0 */
15758 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15759 p->config = config;
15764rb_ruby_parser_new(const rb_parser_config_t *config)
15766 /* parser_initialize expects fields to be set to 0 */
15767 rb_parser_t *p = rb_ruby_parser_allocate(config);
15768 parser_initialize(p);
15773rb_ruby_parser_allocate(void)
15775 /* parser_initialize expects fields to be set to 0 */
15776 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15781rb_ruby_parser_new(void)
15783 /* parser_initialize expects fields to be set to 0 */
15784 rb_parser_t *p = rb_ruby_parser_allocate();
15785 parser_initialize(p);
15791rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15793 p->error_buffer = main ? Qfalse : Qnil;
15794 p->parent_iseq = base;
15799rb_ruby_parser_set_script_lines(rb_parser_t *p)
15801 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15805rb_ruby_parser_error_tolerant(rb_parser_t *p)
15807 p->error_tolerant = 1;
15811rb_ruby_parser_keep_tokens(rb_parser_t *p)
15813 p->keep_tokens = 1;
15814 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15818rb_ruby_parser_encoding(rb_parser_t *p)
15824rb_ruby_parser_end_seen_p(rb_parser_t *p)
15826 return p->ruby__end__seen;
15830rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15835#endif /* !RIPPER */
15839rb_ruby_parser_get_yydebug(rb_parser_t *p)
15845rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15851rb_ruby_parser_error_p(rb_parser_t *p)
15857rb_ruby_parser_debug_output(rb_parser_t *p)
15859 return p->debug_output;
15863rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15865 p->debug_output = output;
15869rb_ruby_parser_parsing_thread(rb_parser_t *p)
15871 return p->parsing_thread;
15875rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15877 p->parsing_thread = parsing_thread;
15881rb_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)
15883 p->lex.gets = gets;
15884 p->lex.input = input;
15886 p->ruby_sourcefile_string = sourcefile_string;
15887 p->ruby_sourcefile = sourcefile;
15888 p->ruby_sourceline = sourceline;
15892rb_ruby_parser_result(rb_parser_t *p)
15898rb_ruby_parser_enc(rb_parser_t *p)
15904rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15906 return p->ruby_sourcefile_string;
15910rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15912 return p->ruby_sourceline;
15916rb_ruby_parser_lex_state(rb_parser_t *p)
15918 return p->lex.state;
15922rb_ruby_ripper_parse0(rb_parser_t *p)
15925 p->ast = rb_ast_new();
15926 ripper_yyparse((void*)p);
15927 rb_ast_free(p->ast);
15930 p->eval_tree_begin = 0;
15934rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15936 return dedent_string(p, string, width);
15940rb_ruby_ripper_initialized_p(rb_parser_t *p)
15942 return p->lex.input != 0;
15946rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15948 parser_initialize(p);
15952rb_ruby_ripper_column(rb_parser_t *p)
15954 return p->lex.ptok - p->lex.pbeg;
15958rb_ruby_ripper_token_len(rb_parser_t *p)
15960 return p->lex.pcur - p->lex.ptok;
15963rb_parser_string_t *
15964rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15966 return p->lex.lastline;
15970rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15972 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15975#ifdef UNIVERSAL_PARSER
15977rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15979 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15980 p->config = config;
15985struct parser_params*
15986rb_ruby_ripper_parser_allocate(void)
15988 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15994rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15997 VALUE mesg = p->debug_buffer;
15999 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
16001 rb_str_vcatf(mesg, fmt, ap);
16003 if (char_at_end(p, mesg, 0) == '\n') {
16004 rb_io_write(p->debug_output, mesg);
16005 p->debug_buffer = Qnil;
16010parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16013 int lineno, column;
16016 lineno = loc->end_pos.lineno;
16017 column = loc->end_pos.column;
16020 lineno = p->ruby_sourceline;
16021 column = rb_len2int(p->lex.pcur - p->lex.pbeg);
16024 rb_io_flush(p->debug_output);
16028 rb_syntax_error_append(p->error_buffer,
16029 p->ruby_sourcefile_string,
16036count_char(const char *str, int c)
16039 while (str[n] == c) ++n;
16044 * strip enclosing double-quotes, same as the default yytnamerr except
16045 * for that single-quotes matching back-quotes do not stop stripping.
16047 * "\"`class' keyword\"" => "`class' keyword"
16050rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16052 if (*yystr == '"') {
16053 size_t yyn = 0, bquote = 0;
16054 const char *yyp = yystr;
16060 bquote = count_char(yyp+1, '\'') + 1;
16061 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16067 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16068 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16074 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16075 if (yyres) memcpy(yyres + yyn, yyp, 3);
16080 goto do_not_strip_quotes;
16084 goto do_not_strip_quotes;
16087 if (*++yyp != '\\')
16088 goto do_not_strip_quotes;
16089 /* Fall through. */
16103 do_not_strip_quotes: ;
16106 if (!yyres) return strlen(yystr);
16108 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16113#define validate(x) (void)(x)
16116ripper_dispatch0(struct parser_params *p, ID mid)
16118 return rb_funcall(p->value, mid, 0);
16122ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16125 return rb_funcall(p->value, mid, 1, a);
16129ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16133 return rb_funcall(p->value, mid, 2, a, b);
16137ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16142 return rb_funcall(p->value, mid, 3, a, b, c);
16146ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16152 return rb_funcall(p->value, mid, 4, a, b, c, d);
16156ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16163 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16167ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16176 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16180ripper_error(struct parser_params *p)
16186ripper_value(struct parser_params *p)
16188 (void)yystpcpy; /* may not used in newer bison */
16197 * c-file-style: "ruby"