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"
30#ifdef UNIVERSAL_PARSER
32#include "internal/ruby_parser.h"
33#include "parser_node.h"
34#include "universal_parser.c"
37#define STATIC_ID2SYM p->config->static_id2sym
38#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
44#include "internal/compile.h"
45#include "internal/compilers.h"
46#include "internal/complex.h"
47#include "internal/encoding.h"
48#include "internal/error.h"
49#include "internal/hash.h"
50#include "internal/io.h"
51#include "internal/numeric.h"
52#include "internal/parse.h"
53#include "internal/rational.h"
54#include "internal/re.h"
55#include "internal/ruby_parser.h"
56#include "internal/symbol.h"
57#include "internal/thread.h"
58#include "internal/variable.h"
60#include "parser_node.h"
63#include "ruby/encoding.h"
64#include "ruby/regex.h"
68#include "ruby/ractor.h"
75 return rb_class_new_instance(0, 0, rb_eSyntaxError);
79static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
81#define compile_callback rb_suppress_tracing
82#endif /* !UNIVERSAL_PARSER */
84#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
85#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
87static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
90static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
94node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
96 return (n1->minus != n2->minus ||
97 n1->base != n2->base ||
98 strcmp(n1->val, n2->val));
102node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
104 return (n1->minus != n2->minus ||
105 strcmp(n1->val, n2->val));
109node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
111 return (n1->minus != n2->minus ||
112 n1->base != n2->base ||
113 n1->seen_point != n2->seen_point ||
114 strcmp(n1->val, n2->val));
118node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
120 return (n1->minus != n2->minus ||
121 n1->base != n2->base ||
122 n1->seen_point != n2->seen_point ||
123 n1->type != n2->type ||
124 strcmp(n1->val, n2->val));
128rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
130 return (n1->options != n2->options ||
131 rb_parser_string_hash_cmp(n1->string, n2->string));
134static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
135static st_index_t rb_char_p_hash(const char *c);
138literal_cmp(st_data_t val, st_data_t lit)
140 if (val == lit) return 0;
142 NODE *node_val = RNODE(val);
143 NODE *node_lit = RNODE(lit);
144 enum node_type type_val = nd_type(node_val);
145 enum node_type type_lit = nd_type(node_lit);
147 if (type_val != type_lit) {
153 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
155 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
157 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
159 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
161 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
163 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
165 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
167 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
169 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
171 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
173#ifdef UNIVERSAL_PARSER
176 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
182literal_hash(st_data_t a)
184 NODE *node = (NODE *)a;
185 enum node_type type = nd_type(node);
189 return rb_char_p_hash(RNODE_INTEGER(node)->val);
191 return rb_char_p_hash(RNODE_FLOAT(node)->val);
193 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
195 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
197 return rb_parser_str_hash(RNODE_STR(node)->string);
199 return rb_parser_str_hash(RNODE_SYM(node)->string);
201 return rb_parser_str_hash(RNODE_REGX(node)->string);
203 return (st_index_t)node->nd_loc.beg_pos.lineno;
205 return rb_parser_str_hash(RNODE_FILE(node)->path);
207 return (st_index_t)RNODE_ENCODING(node)->enc;
209#ifdef UNIVERSAL_PARSER
212 rb_bug("unexpected node: %s", ruby_node_name(type));
220 return '\0' <= c && c <= '\x7f';
224#define ISASCII parse_isascii
229 return c == ' ' || ('\t' <= c && c <= '\r');
233#define ISSPACE parse_isspace
238 return ('\0' <= c && c < ' ') || c == '\x7f';
242#define ISCNTRL(c) parse_iscntrl(c)
247 return 'A' <= c && c <= 'Z';
253 return 'a' <= c && c <= 'z';
259 return parse_isupper(c) || parse_islower(c);
263#define ISALPHA(c) parse_isalpha(c)
268 return '0' <= c && c <= '9';
272#define ISDIGIT(c) parse_isdigit(c)
277 return ISALPHA(c) || ISDIGIT(c);
281#define ISALNUM(c) parse_isalnum(c)
286 return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
290#define ISXDIGIT(c) parse_isxdigit(c)
292#include "parser_st.h"
295#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
298#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
301#include "ripper_init.h"
312 unsigned int in_defined: 1;
313 unsigned int in_kwarg: 1;
314 unsigned int in_argdef: 1;
315 unsigned int in_def: 1;
316 unsigned int in_class: 1;
317 unsigned int has_trailing_semicolon: 1;
318 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
319 BITFIELD(enum rescue_context, in_rescue, 2);
320 unsigned int cant_return: 1;
321 unsigned int in_alt_pattern: 1;
322 unsigned int capture_in_pattern: 1;
325typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
327#if defined(__GNUC__) && !defined(__clang__)
328// Suppress "parameter passing for argument of type 'struct
329// lex_context' changed" notes. `struct lex_context` is file scope,
330// and has no ABI compatibility issue.
332RBIMPL_WARNING_IGNORED(-Wpsabi)
334// Not sure why effective even after popped.
339#define NO_LEX_CTXT (struct lex_context){0}
341#ifndef WARN_PAST_SCOPE
342# define WARN_PAST_SCOPE 0
347#define yydebug (p->debug) /* disable the global variable definition */
349#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
350#define YY_LOCATION_PRINT(File, loc, p) \
351 rb_parser_printf(p, "%d.%d-%d.%d", \
352 (loc).beg_pos.lineno, (loc).beg_pos.column,\
353 (loc).end_pos.lineno, (loc).end_pos.column)
354#define YYLLOC_DEFAULT(Current, Rhs, N) \
358 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
359 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
363 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
364 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
368 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
369 "nesting too deep" : (Msgid))
371#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
372 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
373#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
374 rb_parser_set_location_of_delayed_token(p, &(Current))
375#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
376 rb_parser_set_location_of_heredoc_end(p, &(Current))
377#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
378 rb_parser_set_location_of_dummy_end(p, &(Current))
379#define RUBY_SET_YYLLOC_OF_NONE(Current) \
380 rb_parser_set_location_of_none(p, &(Current))
381#define RUBY_SET_YYLLOC(Current) \
382 rb_parser_set_location(p, &(Current))
383#define RUBY_INIT_YYLLOC() \
385 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
386 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
389#define IS_lex_state_for(x, ls) ((x) & (ls))
390#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
391#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
392#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
394# define SET_LEX_STATE(ls) \
395 parser_set_lex_state(p, ls, __LINE__)
396static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
398typedef VALUE stack_type;
400static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
402# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
403# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
404# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
405# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
406# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
408/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
409 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
410#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
411#define COND_POP() BITSTACK_POP(cond_stack)
412#define COND_P() BITSTACK_SET_P(cond_stack)
413#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
415/* A flag to identify keyword_do_block; "do" keyword after command_call.
416 Example: `foo 1, 2 do`. */
417#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
418#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
419#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
420#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
436 struct local_vars *prev;
438 NODE *outer, *inner, *current;
443typedef struct rb_locations_lambda_body_t {
447} rb_locations_lambda_body_t;
455#define DVARS_INHERIT ((void*)1)
456#define DVARS_TOPSCOPE NULL
457#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
459typedef struct token_info {
461 rb_code_position_t beg;
464 struct token_info *next;
467typedef struct end_expect_token_locations {
468 const rb_code_position_t *pos;
469 struct end_expect_token_locations *prev;
470} end_expect_token_locations_t;
472typedef struct parser_string_buffer_elem {
473 struct parser_string_buffer_elem *next;
474 long len; /* Total length of allocated buf */
475 long used; /* Current usage of buf */
476 rb_parser_string_t *buf[FLEX_ARY_LEN];
477} parser_string_buffer_elem_t;
479typedef struct parser_string_buffer {
480 parser_string_buffer_elem_t *head;
481 parser_string_buffer_elem_t *last;
482} parser_string_buffer_t;
484#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1)
487 Structure of Lexer Buffer:
489 lex.pbeg lex.ptok lex.pcur lex.pend
491 |------------+------------+------------|
495struct parser_params {
500 rb_strterm_t *strterm;
501 rb_parser_lex_gets_func *gets;
502 rb_parser_input_data input;
503 parser_string_buffer_t string_buffer;
504 rb_parser_string_t *lastline;
505 rb_parser_string_t *nextline;
510 enum lex_state_e state;
511 /* track the nest level of any parens "()[]{}" */
513 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
515 /* track the nest level of only braces "{}" */
518 stack_type cond_stack;
519 stack_type cmdarg_stack;
524 int heredoc_line_indent;
526 struct local_vars *lvtbl;
530 int ruby_sourceline; /* current line no. */
531 const char *ruby_sourcefile; /* current source file */
532 VALUE ruby_sourcefile_string;
534 token_info *token_info;
535 st_table *case_labels;
536 rb_node_exits_t *exits;
542 rb_parser_string_t *token;
552 st_table *warn_duplicate_keys_table;
557 struct lex_context ctxt;
559 NODE *eval_tree_begin;
561 const struct rb_iseq_struct *parent_iseq;
563#ifdef UNIVERSAL_PARSER
564 const rb_parser_config_t *config;
567 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
569 unsigned int command_start:1;
570 unsigned int eofp: 1;
571 unsigned int ruby__end__seen: 1;
572 unsigned int debug: 1;
573 unsigned int has_shebang: 1;
574 unsigned int token_seen: 1;
575 unsigned int token_info_enabled: 1;
577 unsigned int past_scope_enabled: 1;
579 unsigned int error_p: 1;
580 unsigned int cr_seen: 1;
585 unsigned int do_print: 1;
586 unsigned int do_loop: 1;
587 unsigned int do_chomp: 1;
588 unsigned int do_split: 1;
589 unsigned int error_tolerant: 1;
590 unsigned int keep_tokens: 1;
593 rb_parser_ary_t *debug_lines;
595 * Store specific keyword locations to generate dummy end token.
596 * Refer to the tail of list element.
598 end_expect_token_locations_t *end_expect_token_locations;
601 /* Array for term tokens */
602 rb_parser_ary_t *tokens;
608 VALUE parsing_thread;
609 VALUE s_value; /* Token VALUE */
610 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
615#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
616#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
617#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
619numparam_id_p(struct parser_params *p, ID id)
621 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
622 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
623 return idx > 0 && idx <= NUMPARAM_MAX;
625static void numparam_name(struct parser_params *p, ID id);
629after_shift(struct parser_params *p)
632 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
634 rb_ary_push(p->s_value_stack, p->s_value);
639before_reduce(int len, struct parser_params *p)
641 // Initialize $$ with $1.
642 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
646after_reduce(int len, struct parser_params *p)
648 for (int i = 0; i < len; i++) {
649 VALUE tos = rb_ary_pop(p->s_value_stack);
651 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
655 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
657 rb_ary_push(p->s_value_stack, p->s_lvalue);
662after_shift_error_token(struct parser_params *p)
665 rb_parser_printf(p, "after-shift-error-token:\n");
667 rb_ary_push(p->s_value_stack, Qnil);
671after_pop_stack(int len, struct parser_params *p)
673 for (int i = 0; i < len; i++) {
674 VALUE tos = rb_ary_pop(p->s_value_stack);
676 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
682after_shift(struct parser_params *p)
687before_reduce(int len, struct parser_params *p)
692after_reduce(int len, struct parser_params *p)
697after_shift_error_token(struct parser_params *p)
702after_pop_stack(int len, struct parser_params *p)
707#define intern_cstr(n,l,en) rb_intern3(n,l,en)
709#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
711#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
712#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
713#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
714#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
715#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
716#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
720char_at_end(struct parser_params *p, VALUE str, int when_empty)
722 long len = RSTRING_LEN(str);
723 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
728pop_pvtbl(struct parser_params *p, st_table *tbl)
730 st_free_table(p->pvtbl);
735pop_pktbl(struct parser_params *p, st_table *tbl)
737 if (p->pktbl) st_free_table(p->pktbl);
741#define STRING_BUF_DEFAULT_LEN 16
744string_buffer_init(struct parser_params *p)
746 parser_string_buffer_t *buf = &p->lex.string_buffer;
747 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
749 buf->head = buf->last = xmalloc(size);
750 buf->head->len = STRING_BUF_DEFAULT_LEN;
752 buf->head->next = NULL;
756string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
758 parser_string_buffer_t *buf = &p->lex.string_buffer;
760 if (buf->head->used >= buf->head->len) {
761 parser_string_buffer_elem_t *elem;
762 long n = buf->head->len * 2;
763 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
765 elem = xmalloc(size);
769 buf->last->next = elem;
772 buf->last->buf[buf->last->used++] = str;
776string_buffer_free(struct parser_params *p)
778 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
781 parser_string_buffer_elem_t *next_elem = elem->next;
783 for (long i = 0; i < elem->used; i++) {
784 rb_parser_string_free(p, elem->buf[i]);
793static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
796debug_end_expect_token_locations(struct parser_params *p, const char *name)
799 VALUE mesg = rb_sprintf("%s: [", name);
801 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
803 rb_str_cat_cstr(mesg, ", ");
804 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
807 rb_str_cat_cstr(mesg, "]\n");
808 flush_debug_buffer(p, p->debug_output, mesg);
813push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
815 if(!p->error_tolerant) return;
817 end_expect_token_locations_t *locations;
818 locations = ALLOC(end_expect_token_locations_t);
819 locations->pos = pos;
820 locations->prev = p->end_expect_token_locations;
821 p->end_expect_token_locations = locations;
823 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
827pop_end_expect_token_locations(struct parser_params *p)
829 if(!p->end_expect_token_locations) return;
831 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
832 ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
833 p->end_expect_token_locations = locations;
835 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
838static end_expect_token_locations_t *
839peek_end_expect_token_locations(struct parser_params *p)
841 return p->end_expect_token_locations;
845parser_token2char(struct parser_params *p, enum yytokentype tok)
848#define TOKEN2CHAR(tok) case tok: return (#tok);
849#define TOKEN2CHAR2(tok, name) case tok: return (name);
850 TOKEN2CHAR2(' ', "word_sep");
851 TOKEN2CHAR2('!', "!")
852 TOKEN2CHAR2('%', "%");
853 TOKEN2CHAR2('&', "&");
854 TOKEN2CHAR2('*', "*");
855 TOKEN2CHAR2('+', "+");
856 TOKEN2CHAR2('-', "-");
857 TOKEN2CHAR2('/', "/");
858 TOKEN2CHAR2('<', "<");
859 TOKEN2CHAR2('=', "=");
860 TOKEN2CHAR2('>', ">");
861 TOKEN2CHAR2('?', "?");
862 TOKEN2CHAR2('^', "^");
863 TOKEN2CHAR2('|', "|");
864 TOKEN2CHAR2('~', "~");
865 TOKEN2CHAR2(':', ":");
866 TOKEN2CHAR2(',', ",");
867 TOKEN2CHAR2('.', ".");
868 TOKEN2CHAR2(';', ";");
869 TOKEN2CHAR2('`', "`");
870 TOKEN2CHAR2('\n', "nl");
871 TOKEN2CHAR2('{', "\"{\"");
872 TOKEN2CHAR2('}', "\"}\"");
873 TOKEN2CHAR2('[', "\"[\"");
874 TOKEN2CHAR2(']', "\"]\"");
875 TOKEN2CHAR2('(', "\"(\"");
876 TOKEN2CHAR2(')', "\")\"");
877 TOKEN2CHAR2('\\', "backslash");
878 TOKEN2CHAR(keyword_class);
879 TOKEN2CHAR(keyword_module);
880 TOKEN2CHAR(keyword_def);
881 TOKEN2CHAR(keyword_undef);
882 TOKEN2CHAR(keyword_begin);
883 TOKEN2CHAR(keyword_rescue);
884 TOKEN2CHAR(keyword_ensure);
885 TOKEN2CHAR(keyword_end);
886 TOKEN2CHAR(keyword_if);
887 TOKEN2CHAR(keyword_unless);
888 TOKEN2CHAR(keyword_then);
889 TOKEN2CHAR(keyword_elsif);
890 TOKEN2CHAR(keyword_else);
891 TOKEN2CHAR(keyword_case);
892 TOKEN2CHAR(keyword_when);
893 TOKEN2CHAR(keyword_while);
894 TOKEN2CHAR(keyword_until);
895 TOKEN2CHAR(keyword_for);
896 TOKEN2CHAR(keyword_break);
897 TOKEN2CHAR(keyword_next);
898 TOKEN2CHAR(keyword_redo);
899 TOKEN2CHAR(keyword_retry);
900 TOKEN2CHAR(keyword_in);
901 TOKEN2CHAR(keyword_do);
902 TOKEN2CHAR(keyword_do_cond);
903 TOKEN2CHAR(keyword_do_block);
904 TOKEN2CHAR(keyword_do_LAMBDA);
905 TOKEN2CHAR(keyword_return);
906 TOKEN2CHAR(keyword_yield);
907 TOKEN2CHAR(keyword_super);
908 TOKEN2CHAR(keyword_self);
909 TOKEN2CHAR(keyword_nil);
910 TOKEN2CHAR(keyword_true);
911 TOKEN2CHAR(keyword_false);
912 TOKEN2CHAR(keyword_and);
913 TOKEN2CHAR(keyword_or);
914 TOKEN2CHAR(keyword_not);
915 TOKEN2CHAR(modifier_if);
916 TOKEN2CHAR(modifier_unless);
917 TOKEN2CHAR(modifier_while);
918 TOKEN2CHAR(modifier_until);
919 TOKEN2CHAR(modifier_rescue);
920 TOKEN2CHAR(keyword_alias);
921 TOKEN2CHAR(keyword_defined);
922 TOKEN2CHAR(keyword_BEGIN);
923 TOKEN2CHAR(keyword_END);
924 TOKEN2CHAR(keyword__LINE__);
925 TOKEN2CHAR(keyword__FILE__);
926 TOKEN2CHAR(keyword__ENCODING__);
927 TOKEN2CHAR(tIDENTIFIER);
931 TOKEN2CHAR(tCONSTANT);
934 TOKEN2CHAR(tINTEGER);
936 TOKEN2CHAR(tRATIONAL);
937 TOKEN2CHAR(tIMAGINARY);
939 TOKEN2CHAR(tNTH_REF);
940 TOKEN2CHAR(tBACK_REF);
941 TOKEN2CHAR(tSTRING_CONTENT);
942 TOKEN2CHAR(tREGEXP_END);
943 TOKEN2CHAR(tDUMNY_END);
969 TOKEN2CHAR(tOP_ASGN);
972 TOKEN2CHAR(tLPAREN_ARG);
975 TOKEN2CHAR(tLBRACE_ARG);
981 TOKEN2CHAR(tSTRING_BEG);
982 TOKEN2CHAR(tXSTRING_BEG);
983 TOKEN2CHAR(tREGEXP_BEG);
984 TOKEN2CHAR(tWORDS_BEG);
985 TOKEN2CHAR(tQWORDS_BEG);
986 TOKEN2CHAR(tSYMBOLS_BEG);
987 TOKEN2CHAR(tQSYMBOLS_BEG);
988 TOKEN2CHAR(tSTRING_END);
989 TOKEN2CHAR(tSTRING_DEND);
990 TOKEN2CHAR(tSTRING_DBEG);
991 TOKEN2CHAR(tSTRING_DVAR);
993 TOKEN2CHAR(tLABEL_END);
994 TOKEN2CHAR(tIGNORED_NL);
995 TOKEN2CHAR(tCOMMENT);
996 TOKEN2CHAR(tEMBDOC_BEG);
998 TOKEN2CHAR(tEMBDOC_END);
999 TOKEN2CHAR(tHEREDOC_BEG);
1000 TOKEN2CHAR(tHEREDOC_END);
1001 TOKEN2CHAR(k__END__);
1002 TOKEN2CHAR(tLOWEST);
1003 TOKEN2CHAR(tUMINUS_NUM);
1004 TOKEN2CHAR(tLAST_TOKEN);
1009 rb_bug("parser_token2id: unknown token %d", tok);
1011 UNREACHABLE_RETURN(0);
1015push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1020pop_end_expect_token_locations(struct parser_params *p)
1025RBIMPL_ATTR_NONNULL((1, 2, 3))
1026static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1027RBIMPL_ATTR_NONNULL((1, 2))
1028static int parser_yyerror0(struct parser_params*, const char*);
1029#define yyerror0(msg) parser_yyerror0(p, (msg))
1030#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1031#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1032#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1033#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1034#define lex_eol_p(p) lex_eol_n_p(p, 0)
1035#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1036#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1037#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1039static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1040static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1041static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1042static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1043static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1046#define compile_for_eval (0)
1048#define compile_for_eval (p->parent_iseq != 0)
1051#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1053#define CALL_Q_P(q) ((q) == tANDDOT)
1054#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1056#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1058static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1061rb_discard_node(struct parser_params *p, NODE *n)
1063 rb_ast_delete_node(p->ast, n);
1066static 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);
1067static 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);
1068static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1069static 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);
1070static 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);
1071static 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);
1072static 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);
1073static 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);
1074static 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);
1075static 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);
1076static 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);
1077static 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);
1078static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1079static 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);
1080static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1081static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1082static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1083static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1084static 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);
1085static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1086static 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);
1087static 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);
1088static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1089static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1090static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1091static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1092static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1093static 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);
1094static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1095static 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);
1096static 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);
1097static 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);
1098static 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);
1099static 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);
1100static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1101static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1102static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1103static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1104static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1105static 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);
1106static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1107static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1108static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1109static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1110static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1111static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1112static 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);
1113static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1114static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1115static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1116static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1117static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1118static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1119static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1120static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1121static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1122static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1123static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1124static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1125static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1126static 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);
1127static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1128static 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);
1129static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1130static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1131static 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);
1132static 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);
1133static 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);
1134static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1135static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1136static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1137static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1138static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1139static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1140static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1141static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1142static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1143static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1144static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1145static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1146static 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);
1147static 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);
1148static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1149static 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);
1150static 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);
1151static 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);
1152static 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);
1153static 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);
1154static 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);
1155static 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);
1156static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1157static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1158static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1159static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1160static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1161static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1162static 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);
1163static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1164static 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);
1165static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1166static 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);
1167static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1168static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1169static 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);
1170static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1171static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1172static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1174#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc)
1175#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc)
1176#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1177#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)
1178#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)
1179#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1180#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1181#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1182#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)
1183#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)
1184#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)
1185#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)
1186#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1187#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)
1188#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1189#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1190#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1191#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1192#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1193#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1194#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1195#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1196#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1197#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1198#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1199#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1200#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1201#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1202#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1203#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)
1204#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)
1205#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1206#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1207#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1208#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1209#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1210#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1211#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1212#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1213#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)
1214#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1215#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1216#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1217#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1218#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1219#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1220#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)
1221#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1222#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1223#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1224#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1225#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1226#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1227#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1228#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1229#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1230#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1231#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1232#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1233#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1234#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1235#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1236#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1237#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1238#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1239#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1240#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc)
1241#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)
1242#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1243#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1244#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1245#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1246#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1247#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1248#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1249#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1250#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1251#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1252#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1253#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1254#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1255#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1256#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1257#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)
1258#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
1259#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)
1260#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1261#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
1262#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
1263#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
1264#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1265#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1266#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1267#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1268#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1269#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc)
1270#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)
1271#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1272#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1273#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1274#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)
1275#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1276#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1277#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1278#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1279#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1280#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1281#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1283enum internal_node_type {
1284 NODE_INTERNAL_ONLY = NODE_LAST,
1291parser_node_name(int node)
1295 return "NODE_DEF_TEMP";
1297 return "NODE_EXITS";
1299 return ruby_node_name(node);
1303/* This node is parse.y internal */
1304struct RNode_DEF_TEMP {
1307 /* for NODE_DEFN/NODE_DEFS */
1309 struct RNode *nd_def;
1314 NODE *numparam_save;
1315 struct lex_context ctxt;
1319#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1321static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1322static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1323static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1324static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1325static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1327#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1328#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1329#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1330#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1332/* Make a new internal node, which should not be appeared in the
1333 * result AST and does not have node_id and location. */
1334static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1335#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1337static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1340parser_get_node_id(struct parser_params *p)
1342 int node_id = p->node_id;
1348anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1350 if (id == tANDDOT) {
1351 yyerror1(loc, "&. inside multiple assignment destination");
1356set_line_body(NODE *body, int line)
1359 switch (nd_type(body)) {
1362 nd_set_line(body, line);
1367set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1369 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1370 nd_set_line(node, beg->end_pos.lineno);
1374last_expr_node(NODE *expr)
1377 if (nd_type_p(expr, NODE_BLOCK)) {
1378 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1380 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1381 expr = RNODE_BEGIN(expr)->nd_body;
1391#define yyparse ruby_yyparse
1394static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1395static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1396static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1397static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1398static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1399static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1401static NODE *newline_node(NODE*);
1402static void fixpos(NODE*,NODE*);
1404static int value_expr(struct parser_params*,NODE*);
1405static void void_expr(struct parser_params*,NODE*);
1406static NODE *remove_begin(NODE*);
1407static NODE *void_stmts(struct parser_params*,NODE*);
1408static void reduce_nodes(struct parser_params*,NODE**);
1409static void block_dup_check(struct parser_params*,NODE*,NODE*);
1411static NODE *block_append(struct parser_params*,NODE*,NODE*);
1412static NODE *list_append(struct parser_params*,NODE*,NODE*);
1413static NODE *list_concat(NODE*,NODE*);
1414static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1415static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1416static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1417static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1418static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1419static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1420static NODE *str2dstr(struct parser_params*,NODE*);
1421static NODE *evstr2dstr(struct parser_params*,NODE*);
1422static NODE *splat_array(NODE*);
1423static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1425static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1426static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1427static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1428static 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);
1429static 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;}
1430static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
1432static bool args_info_empty_p(struct rb_args_info *args);
1433static 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*);
1434static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1435static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1436static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1437static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1438static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1439static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1440static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1442static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1443static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1445static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1446static void no_blockarg(struct parser_params*,NODE*);
1447static NODE *ret_args(struct parser_params*,NODE*);
1448static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1449static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1451static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1452static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1454static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1455static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1457static VALUE rb_backref_error(struct parser_params*,NODE*);
1458static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1460static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1461static 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);
1462static 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);
1463static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1464static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1466static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1468static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1469static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1471static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1472static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1474static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1476static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1478#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1480static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1482static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1484static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1486static rb_ast_id_table_t *local_tbl(struct parser_params*);
1488static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1489static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1491static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1492static NODE *heredoc_dedent(struct parser_params*,NODE*);
1494static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1496static 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);
1499#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1500#define set_value(val) (p->s_lvalue = val)
1501static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1502static int id_is_var(struct parser_params *p, ID id);
1505RUBY_SYMBOL_EXPORT_BEGIN
1506VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1507int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1508enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1509VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1510void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1511PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1512YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1513YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1514YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1515YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1516YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1517YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1518void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1519RUBY_SYMBOL_EXPORT_END
1521static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1522static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1523static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1524static VALUE formal_argument_error(struct parser_params*, ID);
1525static ID shadowing_lvar(struct parser_params*,ID);
1526static void new_bv(struct parser_params*,ID);
1528static void local_push(struct parser_params*,int);
1529static void local_pop(struct parser_params*);
1530static void local_var(struct parser_params*, ID);
1531static void arg_var(struct parser_params*, ID);
1532static int local_id(struct parser_params *p, ID id);
1533static int local_id_ref(struct parser_params*, ID, ID **);
1534#define internal_id rb_parser_internal_id
1535ID internal_id(struct parser_params*);
1536static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1537static int check_forwarding_args(struct parser_params*);
1538static void add_forwarding_args(struct parser_params *p);
1539static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1541static const struct vtable *dyna_push(struct parser_params *);
1542static void dyna_pop(struct parser_params*, const struct vtable *);
1543static int dyna_in_block(struct parser_params*);
1544#define dyna_var(p, id) local_var(p, id)
1545static int dvar_defined(struct parser_params*, ID);
1546#define dvar_defined_ref rb_parser_dvar_defined_ref
1547int dvar_defined_ref(struct parser_params*, ID, ID**);
1548static int dvar_curr(struct parser_params*,ID);
1550static int lvar_defined(struct parser_params*, ID);
1552static NODE *numparam_push(struct parser_params *p);
1553static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1555#define METHOD_NOT '!'
1557#define idFWD_REST '*'
1558#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1559#define idFWD_BLOCK '&'
1560#define idFWD_ALL idDot3
1561#define arg_FWD_BLOCK idFWD_BLOCK
1563#define RE_ONIG_OPTION_IGNORECASE 1
1564#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1565#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1566#define RE_OPTION_ONCE (1<<16)
1567#define RE_OPTION_ENCODING_SHIFT 8
1568#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1569#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1570#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1571#define RE_OPTION_MASK 0xff
1572#define RE_OPTION_ARG_ENCODING_NONE 32
1574#define CHECK_LITERAL_WHEN (st_table *)1
1575#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1577#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1578RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1580#define TOKEN2ID(tok) ( \
1581 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1582 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1583 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1584 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1585 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1586 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1587 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1589/****** Ripper *******/
1593#include "eventids1.h"
1594#include "eventids2.h"
1596extern const struct ripper_parser_ids ripper_parser_ids;
1598static VALUE ripper_dispatch0(struct parser_params*,ID);
1599static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1600static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1601static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1602static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1603static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1604static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1605void ripper_error(struct parser_params *p);
1607#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1608#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1609#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1610#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1611#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1612#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1613#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1615#define yyparse ripper_yyparse
1618aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1620 if (!NIL_P(pre_arg)) {
1621 if (!NIL_P(pre_args)) {
1622 rb_ary_unshift(pre_args, pre_arg);
1625 pre_args = rb_ary_new_from_args(1, pre_arg);
1631#define ID2VAL(id) STATIC_ID2SYM(id)
1632#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1635#define KWD2EID(t, v) keyword_##t
1638new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1640 body = remove_begin(body);
1641 reduce_nodes(p, &body);
1642 NODE *n = NEW_SCOPE(args, body, parent, loc);
1643 nd_set_line(n, loc->end_pos.lineno);
1644 set_line_body(body, loc->beg_pos.lineno);
1649rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1650 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1652 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1653 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1654 loc.beg_pos = arg_loc->beg_pos;
1655 return NEW_RESCUE(arg, rescue, 0, &loc);
1658static NODE *add_block_exit(struct parser_params *p, NODE *node);
1659static rb_node_exits_t *init_block_exit(struct parser_params *p);
1660static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1661static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1662static void clear_block_exit(struct parser_params *p, bool error);
1665next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1667 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1671restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1673 /* See: def_name action */
1674 struct lex_context ctxt = temp->save.ctxt;
1675 p->ctxt.in_def = ctxt.in_def;
1676 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1677 p->ctxt.in_rescue = ctxt.in_rescue;
1678 p->max_numparam = temp->save.max_numparam;
1679 numparam_pop(p, temp->save.numparam_save);
1680 clear_block_exit(p, true);
1684endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1686 if (is_attrset_id(mid)) {
1687 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1689 token_info_drop(p, "def", loc->beg_pos);
1692#define debug_token_line(p, name, line) do { \
1694 const char *const pcur = p->lex.pcur; \
1695 const char *const ptok = p->lex.ptok; \
1696 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1697 line, p->ruby_sourceline, \
1698 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1702#define begin_definition(k, loc_beg, loc_end) \
1704 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1705 /* singleton class */ \
1706 p->ctxt.cant_return = !p->ctxt.in_def; \
1707 p->ctxt.in_def = 0; \
1709 else if (p->ctxt.in_def) { \
1710 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1711 yyerror1(&loc, k " definition in method body"); \
1714 p->ctxt.cant_return = 1; \
1720# define ifndef_ripper(x) (x)
1721# define ifdef_ripper(r,x) (x)
1723# define ifndef_ripper(x)
1724# define ifdef_ripper(r,x) (r)
1727# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1728# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1729# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1730# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1731# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1732# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1733# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1734# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1735# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1736# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1737# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1738# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1739# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1740# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1741# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1742# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1743# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1744# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1745# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1746# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1748extern const ID id_warn, id_warning, id_gets, id_assoc;
1749# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1750# define WARN_S_L(s,l) STR_NEW(s,l)
1751# define WARN_S(s) STR_NEW2(s)
1752# define WARN_I(i) INT2NUM(i)
1753# define WARN_ID(i) rb_id2str(i)
1754# define PRIsWARN PRIsVALUE
1755# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1756# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1757# ifdef HAVE_VA_ARGS_MACRO
1758# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1760# define WARN_CALL rb_funcall
1762# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1763# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1764# ifdef HAVE_VA_ARGS_MACRO
1765# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1767# define WARNING_CALL rb_funcall
1769# define compile_error ripper_compile_error
1771# define WARN_S_L(s,l) s
1774# define WARN_ID(i) rb_id2name(i)
1775# define PRIsWARN PRIsVALUE
1776# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1777# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1778# define WARN_CALL rb_compile_warn
1779# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1780# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1781# define WARNING_CALL rb_compile_warning
1782PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1783# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1786#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1789add_block_exit(struct parser_params *p, NODE *node)
1792 compile_error(p, "unexpected null node");
1795 switch (nd_type(node)) {
1796 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1798 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1801 if (!p->ctxt.in_defined) {
1802 rb_node_exits_t *exits = p->exits;
1804 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1805 exits->nd_stts = node;
1811static rb_node_exits_t *
1812init_block_exit(struct parser_params *p)
1814 rb_node_exits_t *old = p->exits;
1815 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1816 exits->nd_chain = 0;
1817 exits->nd_stts = RNODE(exits);
1822static rb_node_exits_t *
1823allow_block_exit(struct parser_params *p)
1825 rb_node_exits_t *exits = p->exits;
1831restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1837clear_block_exit(struct parser_params *p, bool error)
1839 rb_node_exits_t *exits = p->exits;
1842 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1843 switch (nd_type(e)) {
1845 yyerror1(&e->nd_loc, "Invalid break");
1848 yyerror1(&e->nd_loc, "Invalid next");
1851 yyerror1(&e->nd_loc, "Invalid redo");
1854 yyerror1(&e->nd_loc, "unexpected node");
1855 goto end_checks; /* no nd_chain */
1860 exits->nd_stts = RNODE(exits);
1861 exits->nd_chain = 0;
1864#define WARN_EOL(tok) \
1865 (looking_at_eol_p(p) ? \
1866 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1868static int looking_at_eol_p(struct parser_params *p);
1871get_nd_value(struct parser_params *p, NODE *node)
1873 switch (nd_type(node)) {
1875 return RNODE_GASGN(node)->nd_value;
1877 return RNODE_IASGN(node)->nd_value;
1879 return RNODE_LASGN(node)->nd_value;
1881 return RNODE_DASGN(node)->nd_value;
1883 return RNODE_MASGN(node)->nd_value;
1885 return RNODE_CVASGN(node)->nd_value;
1887 return RNODE_CDECL(node)->nd_value;
1889 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1895set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1897 switch (nd_type(node)) {
1899 RNODE_CDECL(node)->nd_value = rhs;
1902 RNODE_GASGN(node)->nd_value = rhs;
1905 RNODE_IASGN(node)->nd_value = rhs;
1908 RNODE_LASGN(node)->nd_value = rhs;
1911 RNODE_DASGN(node)->nd_value = rhs;
1914 RNODE_MASGN(node)->nd_value = rhs;
1917 RNODE_CVASGN(node)->nd_value = rhs;
1920 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1926get_nd_vid(struct parser_params *p, NODE *node)
1928 switch (nd_type(node)) {
1930 return RNODE_CDECL(node)->nd_vid;
1932 return RNODE_GASGN(node)->nd_vid;
1934 return RNODE_IASGN(node)->nd_vid;
1936 return RNODE_LASGN(node)->nd_vid;
1938 return RNODE_DASGN(node)->nd_vid;
1940 return RNODE_CVASGN(node)->nd_vid;
1942 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1948get_nd_args(struct parser_params *p, NODE *node)
1950 switch (nd_type(node)) {
1952 return RNODE_CALL(node)->nd_args;
1954 return RNODE_OPCALL(node)->nd_args;
1956 return RNODE_FCALL(node)->nd_args;
1958 return RNODE_QCALL(node)->nd_args;
1960 return RNODE_SUPER(node)->nd_args;
1969 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1975djb2(const uint8_t *str, size_t len)
1977 st_index_t hash = 5381;
1979 for (size_t i = 0; i < len; i++) {
1980 hash = ((hash << 5) + hash) + str[i];
1987parser_memhash(const void *ptr, long len)
1989 return djb2(ptr, len);
1992#define PARSER_STRING_PTR(str) (str->ptr)
1993#define PARSER_STRING_LEN(str) (str->len)
1994#define PARSER_STRING_END(str) (&str->ptr[str->len])
1995#define STRING_SIZE(str) ((size_t)str->len + 1)
1996#define STRING_TERM_LEN(str) (1)
1997#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1998#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1999 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
2002#define STRING_SET_LEN(str, n) do { \
2005#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2006 ((ptrvar) = str->ptr, \
2007 (lenvar) = str->len)
2010parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2012 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2015static rb_parser_string_t *
2016rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2018 rb_parser_string_t *str;
2021 rb_bug("negative string size (or size too big): %ld", len);
2024 str = xcalloc(1, sizeof(rb_parser_string_t));
2025 str->ptr = xcalloc(len + 1, sizeof(char));
2028 memcpy(PARSER_STRING_PTR(str), ptr, len);
2030 STRING_SET_LEN(str, len);
2031 STRING_TERM_FILL(str);
2035static rb_parser_string_t *
2036rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2038 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2039 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2046rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2049 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2055rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2058 xfree(PARSER_STRING_PTR(str));
2064rb_parser_str_hash(rb_parser_string_t *str)
2066 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2070rb_char_p_hash(const char *c)
2072 return parser_memhash((const void *)c, strlen(c));
2076rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2078 return PARSER_STRING_LEN(str);
2083rb_parser_string_end(rb_parser_string_t *str)
2085 return &str->ptr[str->len];
2090rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2096rb_parser_str_get_encoding(rb_parser_string_t *str)
2103PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2105 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2110PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2112 return str->coderange;
2116PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2118 str->coderange = coderange;
2122PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2124 rb_parser_string_set_encoding(str, enc);
2125 PARSER_ENC_CODERANGE_SET(str, cr);
2129PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2131 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2135PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2137 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2141PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2143 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2147rb_parser_search_nonascii(const char *p, const char *e)
2151 for (; s < e; s++) {
2152 if (*s & 0x80) return s;
2159rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2161 const char *e = ptr + len;
2163 if (enc == rb_ascii8bit_encoding()) {
2164 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2165 ptr = rb_parser_search_nonascii(ptr, e);
2166 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2169 /* parser string encoding is always asciicompat */
2170 ptr = rb_parser_search_nonascii(ptr, e);
2171 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2173 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2174 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2175 ptr += MBCLEN_CHARFOUND_LEN(ret);
2176 if (ptr == e) break;
2177 ptr = rb_parser_search_nonascii(ptr, e);
2181 return RB_PARSER_ENC_CODERANGE_VALID;
2185rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2187 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2191rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2193 int cr = PARSER_ENC_CODERANGE(str);
2195 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2196 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2197 PARSER_ENC_CODERANGE_SET(str, cr);
2203static rb_parser_string_t *
2204rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2206 if (rb_parser_str_get_encoding(str) == enc)
2208 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2209 PARSER_ENC_CODERANGE_CLEAR(str);
2211 rb_parser_string_set_encoding(str, enc);
2216rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2218 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2222rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2224 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2225 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2227 if (enc1 == NULL || enc2 == NULL)
2234 if (PARSER_STRING_LEN(str2) == 0)
2236 if (PARSER_STRING_LEN(str1) == 0)
2237 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2241 cr1 = rb_parser_enc_str_coderange(p, str1);
2242 cr2 = rb_parser_enc_str_coderange(p, str2);
2245 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2246 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2249 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2253 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2261rb_parser_str_modify(rb_parser_string_t *str)
2263 PARSER_ENC_CODERANGE_CLEAR(str);
2267rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2270 const int termlen = STRING_TERM_LEN(str);
2272 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2273 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2276 int cr = PARSER_ENC_CODERANGE(str);
2277 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2278 /* Leave unknown. */
2280 else if (len > PARSER_STRING_LEN(str)) {
2281 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2283 else if (len < PARSER_STRING_LEN(str)) {
2284 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2285 /* ASCII-only string is keeping after truncated. Valid
2286 * and broken may be invalid or valid, leave unknown. */
2287 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2291 STRING_SET_LEN(str, len);
2292 STRING_TERM_FILL(str);
2295static rb_parser_string_t *
2296rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2298 rb_parser_str_modify(str);
2299 if (len == 0) return 0;
2301 long total, olen, off = -1;
2303 const int termlen = STRING_TERM_LEN(str);
2305 PARSER_STRING_GETMEM(str, sptr, olen);
2306 if (ptr >= sptr && ptr <= sptr + olen) {
2310 if (olen > LONG_MAX - len) {
2311 compile_error(p, "string sizes too big");
2315 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2316 sptr = PARSER_STRING_PTR(str);
2320 memcpy(sptr + olen, ptr, len);
2321 STRING_SET_LEN(str, total);
2322 STRING_TERM_FILL(str);
2327#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2328#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2330static rb_parser_string_t *
2331rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2332 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2335 rb_encoding *str_enc, *res_enc;
2337 str_enc = rb_parser_str_get_encoding(str);
2338 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2340 if (str_enc == ptr_enc) {
2341 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2342 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2346 /* parser string encoding is always asciicompat */
2347 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2348 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2350 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2351 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2352 str_cr = rb_parser_enc_str_coderange(p, str);
2357 *ptr_cr_ret = ptr_cr;
2359 if (str_enc != ptr_enc &&
2360 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2361 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2365 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2367 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2369 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2370 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2372 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2379 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2381 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2386 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2389 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2393 compile_error(p, "negative string size (or size too big)");
2395 parser_str_cat(str, ptr, len);
2396 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2400 compile_error(p, "incompatible character encodings: %s and %s",
2401 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2402 UNREACHABLE_RETURN(0);
2406static rb_parser_string_t *
2407rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2408 rb_encoding *ptr_enc)
2410 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2413static rb_parser_string_t *
2414rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2416 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2418 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2419 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2421 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2426static rb_parser_string_t *
2427rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2430 rb_bug("negative string size (or size too big)");
2433 long slen = PARSER_STRING_LEN(str);
2435 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2436 PARSER_ENC_CODERANGE_CLEAR(str);
2441 const int termlen = STRING_TERM_LEN(str);
2443 if ((capa = slen) < len) {
2444 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2446 else if (len == slen) return str;
2447 STRING_SET_LEN(str, len);
2448 STRING_TERM_FILL(str);
2453# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2454 ((ptrvar) = str->ptr, \
2455 (lenvar) = str->len, \
2456 (encvar) = str->enc)
2459rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2462 const char *ptr1, *ptr2;
2463 rb_encoding *enc1, *enc2;
2465 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2466 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2468 return (len1 != len2 ||
2470 memcmp(ptr1, ptr2, len1) != 0);
2474rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2477 if (ary->capa < len) {
2479 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2480 for (i = ary->len; i < len; i++) {
2487 * Do not call this directly.
2488 * Use rb_parser_ary_new_capa_for_XXX() instead.
2490static rb_parser_ary_t *
2491parser_ary_new_capa(rb_parser_t *p, long len)
2494 rb_bug("negative array size (or size too big): %ld", len);
2496 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2501 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2510static rb_parser_ary_t *
2511rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2513 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2514 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2518static rb_parser_ary_t *
2519rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2521 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2522 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2527static rb_parser_ary_t *
2528rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2530 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2531 ary->data_type = PARSER_ARY_DATA_NODE;
2536 * Do not call this directly.
2537 * Use rb_parser_ary_push_XXX() instead.
2539static rb_parser_ary_t *
2540parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2542 if (ary->len == ary->capa) {
2543 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2545 ary->data[ary->len++] = val;
2550static rb_parser_ary_t *
2551rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2553 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2554 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2556 return parser_ary_push(p, ary, val);
2559static rb_parser_ary_t *
2560rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2562 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2563 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2565 return parser_ary_push(p, ary, val);
2569static rb_parser_ary_t *
2570rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2572 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2573 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2575 return parser_ary_push(p, ary, val);
2580rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2583 rb_parser_string_free(p, token->str);
2588rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2590# define foreach_ary(ptr) \
2591 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2592 ptr < end_ary_data; ptr++)
2593 switch (ary->data_type) {
2594 case PARSER_ARY_DATA_AST_TOKEN:
2595 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2597 case PARSER_ARY_DATA_SCRIPT_LINE:
2598 foreach_ary(data) {rb_parser_string_free(p, *data);}
2600 case PARSER_ARY_DATA_NODE:
2601 /* Do nothing because nodes are freed when rb_ast_t is freed */
2604 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2617%define parse.error verbose
2619 if ((NODE *)$$ == (NODE *)-1) {
2620 rb_parser_printf(p, "NODE_SPECIAL");
2623 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2625} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2626 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2628 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2631 switch (nd_type(RNODE($$))) {
2633 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2636 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2639 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2641 case NODE_IMAGINARY:
2642 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2647} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2649 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2652 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2656 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2659%lex-param {struct parser_params *p}
2660%parse-param {struct parser_params *p}
2663 RUBY_SET_YYLLOC_OF_NONE(@$);
2665%after-shift after_shift
2666%before-reduce before_reduce
2667%after-reduce after_reduce
2668%after-shift-error-token after_shift_error_token
2669%after-pop-stack after_pop_stack
2673 rb_node_fcall_t *node_fcall;
2674 rb_node_args_t *node_args;
2675 rb_node_args_aux_t *node_args_aux;
2676 rb_node_opt_arg_t *node_opt_arg;
2677 rb_node_kw_arg_t *node_kw_arg;
2678 rb_node_block_pass_t *node_block_pass;
2679 rb_node_masgn_t *node_masgn;
2680 rb_node_def_temp_t *node_def_temp;
2681 rb_node_exits_t *node_exits;
2682 struct rb_locations_lambda_body_t *locations_lambda_body;
2687 const struct vtable *vars;
2688 struct rb_strterm_struct *strterm;
2689 struct lex_context ctxt;
2690 enum lex_state_e state;
2694 keyword_class "'class'"
2695 keyword_module "'module'"
2697 keyword_undef "'undef'"
2698 keyword_begin "'begin'"
2699 keyword_rescue "'rescue'"
2700 keyword_ensure "'ensure'"
2703 keyword_unless "'unless'"
2704 keyword_then "'then'"
2705 keyword_elsif "'elsif'"
2706 keyword_else "'else'"
2707 keyword_case "'case'"
2708 keyword_when "'when'"
2709 keyword_while "'while'"
2710 keyword_until "'until'"
2712 keyword_break "'break'"
2713 keyword_next "'next'"
2714 keyword_redo "'redo'"
2715 keyword_retry "'retry'"
2718 keyword_do_cond "'do' for condition"
2719 keyword_do_block "'do' for block"
2720 keyword_do_LAMBDA "'do' for lambda"
2721 keyword_return "'return'"
2722 keyword_yield "'yield'"
2723 keyword_super "'super'"
2724 keyword_self "'self'"
2726 keyword_true "'true'"
2727 keyword_false "'false'"
2731 modifier_if "'if' modifier"
2732 modifier_unless "'unless' modifier"
2733 modifier_while "'while' modifier"
2734 modifier_until "'until' modifier"
2735 modifier_rescue "'rescue' modifier"
2736 keyword_alias "'alias'"
2737 keyword_defined "'defined?'"
2738 keyword_BEGIN "'BEGIN'"
2740 keyword__LINE__ "'__LINE__'"
2741 keyword__FILE__ "'__FILE__'"
2742 keyword__ENCODING__ "'__ENCODING__'"
2744%token <id> tIDENTIFIER "local variable or method"
2745%token <id> tFID "method"
2746%token <id> tGVAR "global variable"
2747%token <id> tIVAR "instance variable"
2748%token <id> tCONSTANT "constant"
2749%token <id> tCVAR "class variable"
2750%token <id> tLABEL "label"
2751%token <node> tINTEGER "integer literal"
2752%token <node> tFLOAT "float literal"
2753%token <node> tRATIONAL "rational literal"
2754%token <node> tIMAGINARY "imaginary literal"
2755%token <node> tCHAR "char literal"
2756%token <node> tNTH_REF "numbered reference"
2757%token <node> tBACK_REF "back reference"
2758%token <node> tSTRING_CONTENT "literal content"
2759%token <num> tREGEXP_END
2760%token <num> tDUMNY_END "dummy end"
2762%type <node> singleton singleton_expr strings string string1 xstring regexp
2763%type <node> string_contents xstring_contents regexp_contents string_content
2764%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2765%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2766%type <node_def_temp> defn_head defs_head k_def
2767%type <node_exits> block_open k_while k_until k_for allow_exits
2768%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2769%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2770%type <node> command command_call command_call_value method_call
2771%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2772%type <node_fcall> fcall
2773%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2774%type <node> args arg_splat call_args opt_call_args
2775%type <node> paren_args opt_paren_args
2776%type <node_args> args_tail block_args_tail
2777%type <node> command_args aref_args
2778%type <node_block_pass> opt_block_arg block_arg
2779%type <node> var_ref var_lhs
2780%type <node> command_rhs arg_rhs
2781%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2782%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2783%type <node_args_aux> f_arg f_arg_item
2784%type <node> f_marg f_rest_marg
2785%type <node_masgn> f_margs
2786%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2787%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2788%type <id> do bv_decls opt_bv_decl bvar
2789%type <node> lambda brace_body do_body
2790%type <locations_lambda_body> lambda_body
2791%type <node_args> f_larglist
2792%type <node> brace_block cmd_brace_block do_block lhs none fitem
2793%type <node> mlhs_head mlhs_item mlhs_node
2794%type <node_masgn> mlhs mlhs_basic mlhs_inner
2795%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2796%type <node> p_expr p_as p_alt p_expr_basic p_find
2797%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2798%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2799%type <node> p_kwargs p_kwarg p_kw
2800%type <id> keyword_variable user_variable sym operation2 operation3
2801%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2802%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2803%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2804%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2805%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2806%type <ctxt> p_in_kwarg
2807%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2808%type <num> max_numparam
2809%type <node> numparam
2811%token END_OF_INPUT 0 "end-of-input"
2814/* escaped chars, should be ignored otherwise */
2815%token <id> '\\' "backslash"
2816%token tSP "escaped space"
2817%token <id> '\t' "escaped horizontal tab"
2818%token <id> '\f' "escaped form feed"
2819%token <id> '\r' "escaped carriage return"
2820%token <id> '\13' "escaped vertical tab"
2821%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2822%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2823%token tPOW RUBY_TOKEN(POW) "**"
2824%token tCMP RUBY_TOKEN(CMP) "<=>"
2825%token tEQ RUBY_TOKEN(EQ) "=="
2826%token tEQQ RUBY_TOKEN(EQQ) "==="
2827%token tNEQ RUBY_TOKEN(NEQ) "!="
2828%token tGEQ RUBY_TOKEN(GEQ) ">="
2829%token tLEQ RUBY_TOKEN(LEQ) "<="
2830%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2831%token tOROP RUBY_TOKEN(OROP) "||"
2832%token tMATCH RUBY_TOKEN(MATCH) "=~"
2833%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2834%token tDOT2 RUBY_TOKEN(DOT2) ".."
2835%token tDOT3 RUBY_TOKEN(DOT3) "..."
2836%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2837%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2838%token tAREF RUBY_TOKEN(AREF) "[]"
2839%token tASET RUBY_TOKEN(ASET) "[]="
2840%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2841%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2842%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2843%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2844%token tCOLON3 ":: at EXPR_BEG"
2845%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2848%token tLPAREN_ARG "( arg"
2851%token tLBRACE_ARG "{ arg"
2853%token tDSTAR "**arg"
2855%token <num> tLAMBDA "->"
2856%token tSYMBEG "symbol literal"
2857%token tSTRING_BEG "string literal"
2858%token tXSTRING_BEG "backtick literal"
2859%token tREGEXP_BEG "regexp literal"
2860%token tWORDS_BEG "word list"
2861%token tQWORDS_BEG "verbatim word list"
2862%token tSYMBOLS_BEG "symbol list"
2863%token tQSYMBOLS_BEG "verbatim symbol list"
2864%token tSTRING_END "terminator"
2865%token tSTRING_DEND "'}'"
2866%token <state> tSTRING_DBEG "'#{'"
2867%token tSTRING_DVAR tLAMBEG tLABEL_END
2869%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2870%token tHEREDOC_BEG tHEREDOC_END k__END__
2877%nonassoc tLBRACE_ARG
2879%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2880%left keyword_or keyword_and
2882%nonassoc keyword_defined
2884%left modifier_rescue
2886%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2889%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2890%left '>' tGEQ '<' tLEQ
2896%right tUMINUS_NUM tUMINUS
2898%right '!' '~' tUPLUS
2905%rule %inline ident_or_const
2910%rule %inline user_or_keyword_variable
2916 * parameterizing rules
2918%rule asgn(rhs) <node>
2919 : lhs '=' lex_ctxt rhs
2921 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2922 /*% ripper: assign!($:1, $:4) %*/
2926%rule args_tail_basic(value) <node_args>
2927 : f_kwarg(value) ',' f_kwrest opt_f_block_arg
2929 $$ = new_args_tail(p, $1, $3, $4, &@3);
2930 /*% ripper: [$:1, $:3, $:4] %*/
2932 | f_kwarg(value) opt_f_block_arg
2934 $$ = new_args_tail(p, $1, 0, $2, &@1);
2935 /*% ripper: [$:1, Qnil, $:2] %*/
2937 | f_any_kwrest opt_f_block_arg
2939 $$ = new_args_tail(p, 0, $1, $2, &@1);
2940 /*% ripper: [Qnil, $:1, $:2] %*/
2944 $$ = new_args_tail(p, 0, 0, $1, &@1);
2945 /*% ripper: [Qnil, Qnil, $:1] %*/
2949%rule def_endless_method(bodystmt) <node>
2950 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2952 endless_method_name(p, $head->nd_mid, &@head);
2953 restore_defun(p, $head);
2954 ($$ = $head->nd_def)->nd_loc = @$;
2955 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2956 RNODE_DEFN($$)->nd_defn = $bodystmt;
2957 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2958 /*% ripper: def!($:head, $:args, $:$) %*/
2961 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2963 endless_method_name(p, $head->nd_mid, &@head);
2964 restore_defun(p, $head);
2965 ($$ = $head->nd_def)->nd_loc = @$;
2966 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2967 RNODE_DEFS($$)->nd_defn = $bodystmt;
2968 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2969 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2974%rule compstmt(stmts) <node>
2977 void_stmts(p, $$ = $stmts);
2981%rule f_opt(value) <node_opt_arg>
2982 : f_arg_asgn f_eq value
2984 p->ctxt.in_argdef = 1;
2985 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2986 /*% ripper: [$:$, $:3] %*/
2990%rule f_opt_arg(value) <node_opt_arg>
2994 /*% ripper: rb_ary_new3(1, $:1) %*/
2996 | f_opt_arg(value) ',' f_opt(value)
2998 $$ = opt_arg_append($f_opt_arg, $f_opt);
2999 /*% ripper: rb_ary_push($:1, $:3) %*/
3003%rule f_kw(value) <node_kw_arg>
3006 p->ctxt.in_argdef = 1;
3007 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3008 /*% ripper: [$:$, $:value] %*/
3012 p->ctxt.in_argdef = 1;
3013 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3014 /*% ripper: [$:$, 0] %*/
3018%rule f_kwarg(value) <node_kw_arg>
3022 /*% ripper: rb_ary_new3(1, $:1) %*/
3024 | f_kwarg(value) ',' f_kw(value)
3026 $$ = kwd_append($f_kwarg, $f_kw);
3027 /*% ripper: rb_ary_push($:1, $:3) %*/
3031%rule mlhs_items(item) <node>
3034 $$ = NEW_LIST($1, &@$);
3035 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3037 | mlhs_items(item) ',' item
3039 $$ = list_append(p, $1, $3);
3040 /*% ripper: mlhs_add!($:1, $:3) %*/
3044%rule op_asgn(rhs) <node>
3045 : var_lhs tOP_ASGN lex_ctxt rhs
3047 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3048 /*% ripper: opassign!($:1, $:2, $:4) %*/
3050 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3052 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3053 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3055 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3057 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3058 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3060 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3062 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3063 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3065 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3067 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3068 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3070 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3072 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3073 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3074 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3076 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3078 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3079 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3080 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3082 | backref tOP_ASGN lex_ctxt rhs
3084 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3085 $$ = NEW_ERROR(&@$);
3086 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3090%rule opt_args_tail(tail) <node_args>
3098 $$ = new_args_tail(p, 0, 0, 0, &@0);
3099 /*% ripper: [Qnil, Qnil, Qnil] %*/
3103%rule range_expr(range) <node>
3108 $$ = NEW_DOT2($1, $3, &@$, &@2);
3109 /*% ripper: dot2!($:1, $:3) %*/
3115 $$ = NEW_DOT3($1, $3, &@$, &@2);
3116 /*% ripper: dot3!($:1, $:3) %*/
3121 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3122 /*% ripper: dot2!($:1, Qnil) %*/
3127 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3128 /*% ripper: dot3!($:1, Qnil) %*/
3133 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3134 /*% ripper: dot2!(Qnil, $:2) %*/
3139 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3140 /*% ripper: dot3!(Qnil, $:2) %*/
3144%rule value_expr(value) <node>
3152%rule words(begin, word_list) <node>
3153 : begin ' '+ word_list tSTRING_END
3155 $$ = make_list($word_list, &@$);
3156 /*% ripper: array!($:3) %*/
3162 SET_LEX_STATE(EXPR_BEG);
3163 local_push(p, ifndef_ripper(1)+0);
3164 /* jumps are possible in the top-level loop. */
3165 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3169 if ($2 && !compile_for_eval) {
3171 /* last expression should not be void */
3172 if (nd_type_p(node, NODE_BLOCK)) {
3173 while (RNODE_BLOCK(node)->nd_next) {
3174 node = RNODE_BLOCK(node)->nd_next;
3176 node = RNODE_BLOCK(node)->nd_head;
3178 node = remove_begin(node);
3181 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3182 /*% ripper[final]: program!($:2) %*/
3189 $$ = NEW_BEGIN(0, &@$);
3190 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3194 $$ = newline_node($1);
3195 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3197 | top_stmts terms top_stmt
3199 $$ = block_append(p, $1, newline_node($3));
3200 /*% ripper: stmts_add!($:1, $:3) %*/
3206 clear_block_exit(p, true);
3209 | keyword_BEGIN begin_block
3216block_open : '{' {$$ = init_block_exit(p);};
3218begin_block : block_open compstmt(top_stmts) '}'
3220 restore_block_exit(p, $block_open);
3221 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3222 NEW_BEGIN($2, &@$));
3223 $$ = NEW_BEGIN(0, &@$);
3224 /*% ripper: BEGIN!($:2) %*/
3228bodystmt : compstmt(stmts)[body]
3233 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3234 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3236 compstmt(stmts)[elsebody]
3238 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3242 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3243 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3245 | compstmt(stmts)[body]
3249 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3253 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3254 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3260 $$ = NEW_BEGIN(0, &@$);
3261 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3265 $$ = newline_node($1);
3266 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3268 | stmts terms stmt_or_begin
3270 $$ = block_append(p, $1, newline_node($3));
3271 /*% ripper: stmts_add!($:1, $:3) %*/
3278 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3286allow_exits : {$$ = allow_block_exit(p);};
3288k_END : keyword_END lex_ctxt
3291 p->ctxt.in_rescue = before_rescue;
3295stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3297 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3298 /*% ripper: alias!($:2, $:4) %*/
3300 | keyword_alias tGVAR tGVAR
3302 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3303 /*% ripper: var_alias!($:2, $:3) %*/
3305 | keyword_alias tGVAR tBACK_REF
3309 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3310 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3311 /*% ripper: var_alias!($:2, $:3) %*/
3313 | keyword_alias tGVAR tNTH_REF
3315 static const char mesg[] = "can't make alias for the number variables";
3317 yyerror1(&@3, mesg);
3319 $$ = NEW_ERROR(&@$);
3320 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3322 | keyword_undef undef_list
3324 nd_set_first_loc($2, @1.beg_pos);
3325 RNODE_UNDEF($2)->keyword_loc = @1;
3327 /*% ripper: undef!($:2) %*/
3329 | stmt modifier_if expr_value
3331 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3333 /*% ripper: if_mod!($:3, $:1) %*/
3335 | stmt modifier_unless expr_value
3337 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3339 /*% ripper: unless_mod!($:3, $:1) %*/
3341 | stmt modifier_while expr_value
3343 clear_block_exit(p, false);
3344 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3345 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3348 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3350 /*% ripper: while_mod!($:3, $:1) %*/
3352 | stmt modifier_until expr_value
3354 clear_block_exit(p, false);
3355 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3356 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3359 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3361 /*% ripper: until_mod!($:3, $:1) %*/
3363 | stmt modifier_rescue after_rescue stmt
3365 p->ctxt.in_rescue = $3.in_rescue;
3367 YYLTYPE loc = code_loc_gen(&@2, &@4);
3368 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3369 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3370 /*% ripper: rescue_mod!($:1, $:4) %*/
3372 | k_END allow_exits '{' compstmt(stmts) '}'
3374 if (p->ctxt.in_def) {
3375 rb_warn0("END in method; use at_exit");
3377 restore_block_exit(p, $allow_exits);
3380 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, NULL /* parent */, &@$);
3381 $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
3382 RNODE_SCOPE(scope)->nd_parent = $$;
3384 /*% ripper: END!($:compstmt) %*/
3387 | mlhs '=' lex_ctxt command_call_value
3389 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3390 /*% ripper: massign!($:1, $:4) %*/
3393 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3394 after_rescue stmt[resbody]
3396 p->ctxt.in_rescue = $3.in_rescue;
3397 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3398 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3399 loc.beg_pos = @mrhs_arg.beg_pos;
3400 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3401 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3402 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3404 | mlhs '=' lex_ctxt mrhs_arg
3406 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3407 /*% ripper: massign!($:1, $:4) %*/
3413 $$ = NEW_ERROR(&@$);
3417command_asgn : asgn(command_rhs)
3418 | op_asgn(command_rhs)
3419 | def_endless_method(endless_command)
3422endless_command : command
3423 | endless_command modifier_rescue after_rescue arg
3425 p->ctxt.in_rescue = $3.in_rescue;
3426 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3427 /*% ripper: rescue_mod!($:1, $:4) %*/
3429 | keyword_not '\n'? endless_command
3431 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3432 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3436command_rhs : command_call_value %prec tOP_ASGN
3437 | command_call_value modifier_rescue after_rescue stmt
3439 p->ctxt.in_rescue = $3.in_rescue;
3440 YYLTYPE loc = code_loc_gen(&@2, &@4);
3441 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3442 /*% ripper: rescue_mod!($:1, $:4) %*/
3448 | expr keyword_and expr
3450 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3451 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3453 | expr keyword_or expr
3455 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3456 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3458 | keyword_not '\n'? expr
3460 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3461 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3465 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3466 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3470 value_expr(p, $arg);
3472 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3473 p_top_expr_body[body]
3475 pop_pktbl(p, $p_pktbl);
3476 pop_pvtbl(p, $p_pvtbl);
3477 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3478 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3479 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3480 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@2), &@$, &NULL_LOC, &NULL_LOC);
3481 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3485 value_expr(p, $arg);
3487 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3488 p_top_expr_body[body]
3490 pop_pktbl(p, $p_pktbl);
3491 pop_pvtbl(p, $p_pvtbl);
3492 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3493 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3494 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3495 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3496 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3498 | arg %prec tLBRACE_ARG
3503 numparam_name(p, $fname);
3506 p->ctxt.in_rescue = before_rescue;
3507 p->ctxt.cant_return = 0;
3512defn_head : k_def def_name
3514 $$ = def_head_save(p, $k_def);
3515 $$->nd_mid = $def_name;
3516 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3517 /*% ripper: $:def_name %*/
3521defs_head : k_def singleton dot_or_colon
3523 SET_LEX_STATE(EXPR_FNAME);
3527 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3528 $$ = def_head_save(p, $k_def);
3529 $$->nd_mid = $def_name;
3530 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3531 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3535expr_value : value_expr(expr)
3538 $$ = NEW_ERROR(&@$);
3542expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3549command_call : command
3553command_call_value : value_expr(command_call)
3556block_command : block_call
3557 | block_call call_op2 operation2 command_args
3559 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3560 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3564cmd_brace_block : tLBRACE_ARG brace_body '}'
3567 set_embraced_location($$, &@1, &@3);
3574 $$ = NEW_FCALL($1, 0, &@$);
3579command : fcall command_args %prec tLOWEST
3582 nd_set_last_loc($1, @2.end_pos);
3584 /*% ripper: command!($:1, $:2) %*/
3586 | fcall command_args cmd_brace_block
3588 block_dup_check(p, $2, $3);
3590 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3591 fixpos($$, RNODE($1));
3592 nd_set_last_loc($1, @2.end_pos);
3593 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3595 | primary_value call_op operation2 command_args %prec tLOWEST
3597 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3598 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3600 | primary_value call_op operation2 command_args cmd_brace_block
3602 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3603 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3605 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3607 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3608 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3610 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3612 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3613 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3615 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3617 set_embraced_location($5, &@4, &@6);
3618 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3619 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3621 | keyword_super command_args
3623 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3625 /*% ripper: super!($:2) %*/
3627 | k_yield command_args
3629 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3631 /*% ripper: yield!($:2) %*/
3633 | k_return call_args
3635 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3636 /*% ripper: return!($:2) %*/
3638 | keyword_break call_args
3641 args = ret_args(p, $2);
3642 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3643 /*% ripper: break!($:2) %*/
3645 | keyword_next call_args
3648 args = ret_args(p, $2);
3649 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3650 /*% ripper: next!($:2) %*/
3655 | tLPAREN mlhs_inner rparen
3658 /*% ripper: mlhs_paren!($:2) %*/
3662mlhs_inner : mlhs_basic
3663 | tLPAREN mlhs_inner rparen
3665 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3666 /*% ripper: mlhs_paren!($:2) %*/
3670mlhs_basic : mlhs_head
3672 $$ = NEW_MASGN($1, 0, &@$);
3675 | mlhs_head mlhs_item
3677 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3678 /*% ripper: mlhs_add!($:1, $:2) %*/
3680 | mlhs_head tSTAR mlhs_node
3682 $$ = NEW_MASGN($1, $3, &@$);
3683 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3685 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3687 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3688 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3692 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3693 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3695 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3697 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3698 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3702 $$ = NEW_MASGN(0, $2, &@$);
3703 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3705 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3707 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3708 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3712 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3713 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3715 | tSTAR ',' mlhs_items(mlhs_item)
3717 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3718 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3722mlhs_item : mlhs_node
3723 | tLPAREN mlhs_inner rparen
3726 /*% ripper: mlhs_paren!($:2) %*/
3730mlhs_head : mlhs_item ','
3732 $$ = NEW_LIST($1, &@1);
3733 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3735 | mlhs_head mlhs_item ','
3737 $$ = list_append(p, $1, $2);
3738 /*% ripper: mlhs_add!($:1, $:2) %*/
3743mlhs_node : user_or_keyword_variable
3745 /*% ripper: var_field!($:1) %*/
3746 $$ = assignable(p, $1, 0, &@$);
3748 | primary_value '[' opt_call_args rbracket
3750 $$ = aryset(p, $1, $3, &@$);
3751 /*% ripper: aref_field!($:1, $:3) %*/
3753 | primary_value call_op ident_or_const
3755 anddot_multiple_assignment_check(p, &@2, $2);
3756 $$ = attrset(p, $1, $2, $3, &@$);
3757 /*% ripper: field!($:1, $:2, $:3) %*/
3759 | primary_value tCOLON2 tIDENTIFIER
3761 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3762 /*% ripper: const_path_field!($:1, $:3) %*/
3764 | primary_value tCOLON2 tCONSTANT
3766 /*% ripper: const_path_field!($:1, $:3) %*/
3767 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3771 /*% ripper: top_const_field!($:2) %*/
3772 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3776 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3777 $$ = NEW_ERROR(&@$);
3778 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3782lhs : user_or_keyword_variable
3784 /*% ripper: var_field!($:1) %*/
3785 $$ = assignable(p, $1, 0, &@$);
3787 | primary_value '[' opt_call_args rbracket
3789 $$ = aryset(p, $1, $3, &@$);
3790 /*% ripper: aref_field!($:1, $:3) %*/
3792 | primary_value call_op ident_or_const
3794 $$ = attrset(p, $1, $2, $3, &@$);
3795 /*% ripper: field!($:1, $:2, $:3) %*/
3797 | primary_value tCOLON2 tIDENTIFIER
3799 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3800 /*% ripper: field!($:1, $:2, $:3) %*/
3802 | primary_value tCOLON2 tCONSTANT
3804 /*% ripper: const_path_field!($:1, $:3) %*/
3805 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3809 /*% ripper: top_const_field!($:2) %*/
3810 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3814 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3815 $$ = NEW_ERROR(&@$);
3816 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3822 static const char mesg[] = "class/module name must be CONSTANT";
3824 yyerror1(&@1, mesg);
3826 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3831cpath : tCOLON3 cname
3833 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3834 /*% ripper: top_const_ref!($:2) %*/
3838 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3839 /*% ripper: const_ref!($:1) %*/
3841 | primary_value tCOLON2 cname
3843 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3844 /*% ripper: const_path_ref!($:1, $:3) %*/
3851 SET_LEX_STATE(EXPR_ENDFN);
3859 $$ = NEW_SYM(rb_id2str($1), &@$);
3860 /*% ripper: symbol_literal!($:1) %*/
3867 $$ = NEW_UNDEF($1, &@$);
3868 /*% ripper: rb_ary_new3(1, $:1) %*/
3870 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3872 nd_set_last_loc($1, @4.end_pos);
3873 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3874 /*% ripper: rb_ary_push($:1, $:4) %*/
3878op : '|' { $$ = '|'; }
3881 | tCMP { $$ = tCMP; }
3883 | tEQQ { $$ = tEQQ; }
3884 | tMATCH { $$ = tMATCH; }
3885 | tNMATCH { $$ = tNMATCH; }
3887 | tGEQ { $$ = tGEQ; }
3889 | tLEQ { $$ = tLEQ; }
3890 | tNEQ { $$ = tNEQ; }
3891 | tLSHFT { $$ = tLSHFT; }
3892 | tRSHFT { $$ = tRSHFT; }
3896 | tSTAR { $$ = '*'; }
3899 | tPOW { $$ = tPOW; }
3900 | tDSTAR { $$ = tDSTAR; }
3903 | tUPLUS { $$ = tUPLUS; }
3904 | tUMINUS { $$ = tUMINUS; }
3905 | tAREF { $$ = tAREF; }
3906 | tASET { $$ = tASET; }
3910reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3911 | keyword_BEGIN | keyword_END
3912 | keyword_alias | keyword_and | keyword_begin
3913 | keyword_break | keyword_case | keyword_class | keyword_def
3914 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3915 | keyword_end | keyword_ensure | keyword_false
3916 | keyword_for | keyword_in | keyword_module | keyword_next
3917 | keyword_nil | keyword_not | keyword_or | keyword_redo
3918 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3919 | keyword_super | keyword_then | keyword_true | keyword_undef
3920 | keyword_when | keyword_yield | keyword_if | keyword_unless
3921 | keyword_while | keyword_until
3929 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3930 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3934 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3935 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3939 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3940 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3944 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3945 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3949 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3950 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3954 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3955 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3957 | tUMINUS_NUM simple_numeric tPOW arg
3959 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3960 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3964 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3965 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3969 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3970 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3974 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3975 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3979 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3980 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3984 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3985 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3989 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3990 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3992 | rel_expr %prec tCMP
3995 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3996 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
4000 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4001 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4005 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4006 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4010 $$ = match_op(p, $1, $3, &@2, &@$);
4011 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4015 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4016 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4020 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4021 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4025 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4026 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4030 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4031 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4035 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4036 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4040 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4041 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4045 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4046 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4048 | keyword_defined '\n'? begin_defined arg
4050 p->ctxt.in_defined = $3.in_defined;
4051 $$ = new_defined(p, $4, &@$, &@1);
4052 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4053 /*% ripper: defined!($:4) %*/
4055 | def_endless_method(endless_arg)
4060ternary : arg '?' arg '\n'? ':' arg
4063 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4065 /*% ripper: ifop!($:1, $:3, $:6) %*/
4069endless_arg : arg %prec modifier_rescue
4070 | endless_arg modifier_rescue after_rescue arg
4072 p->ctxt.in_rescue = $3.in_rescue;
4073 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4074 /*% ripper: rescue_mod!($:1, $:4) %*/
4076 | keyword_not '\n'? endless_arg
4078 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4079 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4083relop : '>' {$$ = '>';}
4089rel_expr : arg relop arg %prec '>'
4091 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4092 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4094 | rel_expr relop arg %prec '>'
4096 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4097 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4098 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4108begin_defined : lex_ctxt
4110 p->ctxt.in_defined = 1;
4115after_rescue : lex_ctxt
4117 p->ctxt.in_rescue = after_rescue;
4122arg_value : value_expr(arg)
4127 | args ',' assocs trailer
4129 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4130 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4134 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4135 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4139arg_rhs : arg %prec tOP_ASGN
4144 | arg modifier_rescue after_rescue arg
4146 p->ctxt.in_rescue = $3.in_rescue;
4148 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4149 /*% ripper: rescue_mod!($:1, $:4) %*/
4153paren_args : '(' opt_call_args rparen
4156 /*% ripper: arg_paren!($:2) %*/
4158 | '(' args ',' args_forward rparen
4160 if (!check_forwarding_args(p)) {
4164 $$ = new_args_forward_call(p, $2, &@4, &@$);
4165 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4168 | '(' args_forward rparen
4170 if (!check_forwarding_args(p)) {
4174 $$ = new_args_forward_call(p, 0, &@2, &@$);
4175 /*% ripper: arg_paren!($:2) %*/
4180opt_paren_args : none
4183 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4190 | args ',' assocs ','
4192 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4193 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4197 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4198 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4202call_args : value_expr(command)
4204 $$ = NEW_LIST($1, &@$);
4205 /*% ripper: args_add!(args_new!, $:1) %*/
4207 | def_endless_method(endless_command)
4209 $$ = NEW_LIST($1, &@$);
4210 /*% ripper: args_add!(args_new!, $:1) %*/
4212 | args opt_block_arg
4214 $$ = arg_blk_pass($1, $2);
4215 /*% ripper: args_add_block!($:1, $:2) %*/
4217 | assocs opt_block_arg
4219 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4220 $$ = arg_blk_pass($$, $2);
4221 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4223 | args ',' assocs opt_block_arg
4225 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4226 $$ = arg_blk_pass($$, $4);
4227 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4230 /*% ripper: args_add_block!(args_new!, $:1) %*/
4234 /* If call_args starts with a open paren '(' or '[',
4235 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4236 * but the push must be done after CMDARG_PUSH(1).
4237 * So this code makes them consistent by first cancelling
4238 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4239 * and finally redoing CMDARG_PUSH(0).
4243 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4246 if (lookahead) CMDARG_POP();
4248 if (lookahead) CMDARG_PUSH(0);
4252 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4253 * but the push must be done after CMDARG_POP() in the parser.
4254 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4255 * CMDARG_POP() to pop 1 pushed by command_args,
4256 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4263 if (lookahead) CMDARG_POP();
4265 if (lookahead) CMDARG_PUSH(0);
4271block_arg : tAMPER arg_value
4273 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4278 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4279 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4280 /*% ripper: Qnil %*/
4284opt_block_arg : ',' block_arg
4292 /*% ripper: Qfalse %*/
4299 $$ = NEW_LIST($arg_value, &@$);
4300 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4305 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4307 | args[non_last_args] ',' arg_value
4309 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4310 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4312 | args[non_last_args] ',' arg_splat
4314 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4315 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4320arg_splat : tSTAR arg_value
4322 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4323 /*% ripper: $:arg_value %*/
4327 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4328 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4329 /*% ripper: Qnil %*/
4339mrhs : args ',' arg_value
4341 $$ = last_arg_append(p, $args, $arg_value, &@$);
4342 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4344 | args ',' tSTAR arg_value
4346 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4347 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4351 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4352 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4356%rule %inline inline_primary
4367primary : inline_primary
4372 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4373 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4383 set_line_body($3, @1.end_pos.lineno);
4384 $$ = NEW_BEGIN($3, &@$);
4385 nd_set_line($$, @1.end_pos.lineno);
4386 /*% ripper: begin!($:3) %*/
4388 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4390 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4392 /*% ripper: paren!($:2) %*/
4394 | tLPAREN compstmt(stmts) ')'
4396 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4397 $$ = NEW_BLOCK($2, &@$);
4398 /*% ripper: paren!($:2) %*/
4400 | primary_value tCOLON2 tCONSTANT
4402 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
4403 /*% ripper: const_path_ref!($:1, $:3) %*/
4407 $$ = NEW_COLON3($2, &@$, &@1, &@2);
4408 /*% ripper: top_const_ref!($:2) %*/
4410 | tLBRACK aref_args ']'
4412 $$ = make_list($2, &@$);
4413 /*% ripper: array!($:2) %*/
4415 | tLBRACE assoc_list '}'
4417 $$ = new_hash(p, $2, &@$);
4418 RNODE_HASH($$)->nd_brace = TRUE;
4419 /*% ripper: hash!($:2) %*/
4423 $$ = NEW_RETURN(0, &@$, &@1);
4424 /*% ripper: return0! %*/
4426 | k_yield '(' call_args rparen
4428 $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4);
4429 /*% ripper: yield!(paren!($:3)) %*/
4431 | k_yield '(' rparen
4433 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4434 /*% ripper: yield!(paren!(args_new!)) %*/
4438 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4439 /*% ripper: yield0! %*/
4441 | keyword_defined '\n'? '(' begin_defined expr rparen
4443 p->ctxt.in_defined = $4.in_defined;
4444 $$ = new_defined(p, $5, &@$, &@1);
4445 p->ctxt.has_trailing_semicolon = $4.has_trailing_semicolon;
4446 /*% ripper: defined!($:5) %*/
4448 | keyword_not '(' expr rparen
4450 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4451 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4453 | keyword_not '(' rparen
4455 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@2), &@2), METHOD_NOT, &@1, &@$);
4456 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4460 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4461 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4464 | method_call brace_block
4466 block_dup_check(p, get_nd_args(p, $1), $2);
4467 $$ = method_add_block(p, $1, $2, &@$);
4468 /*% ripper: method_add_block!($:1, $:2) %*/
4471 | k_if expr_value then
4476 if ($5 && nd_type_p($5, NODE_IF))
4477 RNODE_IF($5)->end_keyword_loc = @6;
4479 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4481 /*% ripper: if!($:2, $:4, $:5) %*/
4483 | k_unless expr_value then
4488 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4490 /*% ripper: unless!($:2, $:4, $:5) %*/
4492 | k_while expr_value_do
4496 restore_block_exit(p, $1);
4497 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4499 /*% ripper: while!($:2, $:3) %*/
4501 | k_until expr_value_do
4505 restore_block_exit(p, $1);
4506 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4508 /*% ripper: until!($:2, $:3) %*/
4510 | k_case expr_value terms?
4512 $$ = p->case_labels;
4513 p->case_labels = CHECK_LITERAL_WHEN;
4518 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4519 p->case_labels = $4;
4520 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4522 /*% ripper: case!($:2, $:5) %*/
4526 $$ = p->case_labels;
4532 if (p->case_labels) st_free_table(p->case_labels);
4533 p->case_labels = $3;
4534 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4535 /*% ripper: case!(Qnil, $:4) %*/
4537 | k_case expr_value terms?
4541 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4542 /*% ripper: case!($:2, $:4) %*/
4544 | k_for for_var keyword_in
4545 {COND_PUSH(1);} expr_value do {COND_POP();}
4549 restore_block_exit(p, $k_for);
4553 * e.each{|*x| a, b, c = x}
4557 * e.each{|x| a, = x}
4559 ID id = internal_id(p);
4560 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4561 rb_node_args_t *args;
4562 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4563 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4564 tbl->ids[0] = id; /* internal id */
4566 switch (nd_type($for_var)) {
4568 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4569 set_nd_value(p, $for_var, internal_var);
4572 m->nd_next = $for_var;
4574 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4575 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4577 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4578 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);
4580 /* {|*internal_id| <m> = internal_id; ... } */
4581 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4582 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4583 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4584 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4585 RNODE_SCOPE(scope)->nd_parent = $$;
4586 fixpos($$, $for_var);
4587 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4589 | k_class cpath superclass
4591 begin_definition("class", &@k_class, &@cpath);
4596 YYLTYPE inheritance_operator_loc = NULL_LOC;
4598 inheritance_operator_loc = @superclass;
4599 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4601 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4602 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4603 set_line_body($bodystmt, @superclass.end_pos.lineno);
4604 nd_set_line($$, @superclass.end_pos.lineno);
4605 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4607 p->ctxt.in_class = $k_class.in_class;
4608 p->ctxt.cant_return = $k_class.cant_return;
4609 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4611 | k_class tLSHFT expr_value
4613 begin_definition("", &@k_class, &@tLSHFT);
4619 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4620 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4621 set_line_body($bodystmt, nd_line($expr_value));
4622 fixpos($$, $expr_value);
4623 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4625 p->ctxt.in_def = $k_class.in_def;
4626 p->ctxt.in_class = $k_class.in_class;
4627 p->ctxt.cant_return = $k_class.cant_return;
4628 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4632 begin_definition("module", &@k_module, &@cpath);
4637 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4638 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4639 set_line_body($bodystmt, @cpath.end_pos.lineno);
4640 nd_set_line($$, @cpath.end_pos.lineno);
4641 /*% ripper: module!($:cpath, $:bodystmt) %*/
4643 p->ctxt.in_class = $k_module.in_class;
4644 p->ctxt.cant_return = $k_module.cant_return;
4645 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4650 push_end_expect_token_locations(p, &@head.beg_pos);
4655 restore_defun(p, $head);
4656 ($$ = $head->nd_def)->nd_loc = @$;
4657 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4658 RNODE_DEFN($$)->nd_defn = $bodystmt;
4659 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4665 push_end_expect_token_locations(p, &@head.beg_pos);
4670 restore_defun(p, $head);
4671 ($$ = $head->nd_def)->nd_loc = @$;
4672 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4673 RNODE_DEFS($$)->nd_defn = $bodystmt;
4674 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4679 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4680 /*% ripper: break!(args_new!) %*/
4684 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4685 /*% ripper: next!(args_new!) %*/
4689 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4690 /*% ripper: redo! %*/
4694 if (!p->ctxt.in_defined) {
4695 switch (p->ctxt.in_rescue) {
4696 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4697 case after_rescue: /* ok */ break;
4698 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4699 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4702 $$ = NEW_RETRY(&@$);
4703 /*% ripper: retry! %*/
4707primary_value : value_expr(primary)
4710k_begin : keyword_begin
4712 token_info_push(p, "begin", &@$);
4713 push_end_expect_token_locations(p, &@1.beg_pos);
4720 token_info_push(p, "if", &@$);
4721 if (p->token_info && p->token_info->nonspc &&
4722 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4723 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4724 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4725 beg += rb_strlen_lit("else");
4726 while (beg < tok && ISSPACE(*beg)) beg++;
4728 p->token_info->nonspc = 0;
4731 push_end_expect_token_locations(p, &@1.beg_pos);
4735k_unless : keyword_unless
4737 token_info_push(p, "unless", &@$);
4738 push_end_expect_token_locations(p, &@1.beg_pos);
4742k_while : keyword_while allow_exits
4745 token_info_push(p, "while", &@$);
4746 push_end_expect_token_locations(p, &@1.beg_pos);
4750k_until : keyword_until allow_exits
4753 token_info_push(p, "until", &@$);
4754 push_end_expect_token_locations(p, &@1.beg_pos);
4758k_case : keyword_case
4760 token_info_push(p, "case", &@$);
4761 push_end_expect_token_locations(p, &@1.beg_pos);
4765k_for : keyword_for allow_exits
4768 token_info_push(p, "for", &@$);
4769 push_end_expect_token_locations(p, &@1.beg_pos);
4773k_class : keyword_class
4775 token_info_push(p, "class", &@$);
4777 p->ctxt.in_rescue = before_rescue;
4778 push_end_expect_token_locations(p, &@1.beg_pos);
4782k_module : keyword_module
4784 token_info_push(p, "module", &@$);
4786 p->ctxt.in_rescue = before_rescue;
4787 push_end_expect_token_locations(p, &@1.beg_pos);
4793 token_info_push(p, "def", &@$);
4794 $$ = NEW_DEF_TEMP(&@$);
4795 p->ctxt.in_argdef = 1;
4801 token_info_push(p, "do", &@$);
4802 push_end_expect_token_locations(p, &@1.beg_pos);
4806k_do_block : keyword_do_block
4808 token_info_push(p, "do", &@$);
4809 push_end_expect_token_locations(p, &@1.beg_pos);
4813k_rescue : keyword_rescue
4815 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4817 p->ctxt.in_rescue = after_rescue;
4821k_ensure : keyword_ensure
4823 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4828k_when : keyword_when
4830 token_info_warn(p, "when", p->token_info, 0, &@$);
4834k_else : keyword_else
4836 token_info *ptinfo_beg = p->token_info;
4837 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4838 token_info_warn(p, "else", p->token_info, same, &@$);
4841 e.next = ptinfo_beg->next;
4843 token_info_setup(&e, p->lex.pbeg, &@$);
4844 if (!e.nonspc) *ptinfo_beg = e;
4849k_elsif : keyword_elsif
4852 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4858 token_info_pop(p, "end", &@$);
4859 pop_end_expect_token_locations(p);
4863 compile_error(p, "syntax error, unexpected end-of-input");
4867k_return : keyword_return
4869 if (p->ctxt.cant_return && !dyna_in_block(p))
4870 yyerror1(&@1, "Invalid return in class/module body");
4874k_yield : keyword_yield
4876 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4877 yyerror1(&@1, "Invalid yield");
4887 | keyword_do_cond { $$ = keyword_do_cond; }
4891 | k_elsif expr_value then
4895 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4897 /*% ripper: elsif!($:2, $:4, $:5) %*/
4902 | k_else compstmt(stmts)
4905 /*% ripper: else!($:2) %*/
4915 $$ = assignable(p, $1, 0, &@$);
4916 mark_lvar_used(p, $$);
4918 | tLPAREN f_margs rparen
4921 /*% ripper: mlhs_paren!($:2) %*/
4926f_margs : mlhs_items(f_marg)
4928 $$ = NEW_MASGN($1, 0, &@$);
4931 | mlhs_items(f_marg) ',' f_rest_marg
4933 $$ = NEW_MASGN($1, $3, &@$);
4934 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4936 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4938 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4939 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4943 $$ = NEW_MASGN(0, $1, &@$);
4944 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4946 | f_rest_marg ',' mlhs_items(f_marg)
4948 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4949 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4953f_rest_marg : tSTAR f_norm_arg
4956 $$ = assignable(p, $2, 0, &@$);
4957 mark_lvar_used(p, $$);
4961 $$ = NODE_SPECIAL_NO_NAME_REST;
4962 /*% ripper: Qnil %*/
4966f_any_kwrest : f_kwrest
4970 /*% ripper: ID2VAL(idNil) %*/
4974f_eq : {p->ctxt.in_argdef = 0;} '=';
4976block_args_tail : args_tail_basic(primary_value)
4981 /* magic number for rest_id in iseq_set_arguments() */
4982 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4983 /*% ripper: excessed_comma! %*/
4987block_param : f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
4989 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
4990 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
4992 | f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4994 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4995 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
4997 | f_arg ',' f_opt_arg(primary_value) opt_args_tail(block_args_tail)
4999 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
5000 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
5002 | f_arg ',' f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5004 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5005 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5007 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5009 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5010 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5012 | f_arg excessed_comma
5014 $$ = new_args_tail(p, 0, 0, 0, &@2);
5015 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5016 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5018 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5020 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5021 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5023 | f_arg opt_args_tail(block_args_tail)
5025 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5026 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5028 | f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5030 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5031 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5033 | f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5035 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5036 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5038 | f_opt_arg(primary_value) opt_args_tail(block_args_tail)
5040 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5041 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5043 | f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5045 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5046 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5048 | f_rest_arg opt_args_tail(block_args_tail)
5050 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5051 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5053 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5055 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5056 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5060 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5061 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5065opt_block_param_def : none
5068 p->command_start = TRUE;
5072block_param_def : '|' opt_block_param opt_bv_decl '|'
5074 p->max_numparam = ORDINAL_PARAM;
5075 p->ctxt.in_argdef = 0;
5077 /*% ripper: block_var!($:2, $:3) %*/
5081opt_block_param : /* none */
5084 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5092 /*% ripper: Qfalse %*/
5094 | '\n'? ';' bv_decls '\n'?
5102 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5104 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5116 $$ = p->max_numparam;
5117 p->max_numparam = 0;
5122 $$ = numparam_push(p);
5132lambda : tLAMBDA[lpar]
5134 token_info_push(p, "->", &@1);
5137 max_numparam numparam it_id allow_exits
5144 int max_numparam = p->max_numparam;
5145 ID it_id = p->it_id;
5146 p->lex.lpar_beg = $lpar;
5147 p->max_numparam = $max_numparam;
5149 restore_block_exit(p, $allow_exits);
5151 $args = args_with_numbered(p, $args, max_numparam, it_id);
5153 YYLTYPE loc = code_loc_gen(&@args, &@body);
5154 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5155 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5156 nd_set_line($$, @args.end_pos.lineno);
5157 nd_set_first_loc($$, @1.beg_pos);
5160 /*% ripper: lambda!($:args, $:body) %*/
5161 numparam_pop(p, $numparam);
5166f_larglist : '(' f_args opt_bv_decl ')'
5168 p->ctxt.in_argdef = 0;
5170 p->max_numparam = ORDINAL_PARAM;
5171 /*% ripper: paren!($:2) %*/
5175 p->ctxt.in_argdef = 0;
5176 if (!args_info_empty_p(&$1->nd_ainfo))
5177 p->max_numparam = ORDINAL_PARAM;
5182lambda_body : tLAMBEG compstmt(stmts) '}'
5184 token_info_pop(p, "}", &@3);
5185 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5190 push_end_expect_token_locations(p, &@1.beg_pos);
5194 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5199do_block : k_do_block do_body k_end
5202 set_embraced_location($$, &@1, &@3);
5207block_call : command do_block
5209 $$ = command_add_block(p, $1, $2, &@$);
5211 /*% ripper: method_add_block!($:1, $:2) %*/
5213 | block_call call_op2 operation2 opt_paren_args
5215 bool has_args = $4 != 0;
5216 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5217 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5218 /*% ripper: call!($:1, $:2, $:3) %*/
5220 /*% ripper: method_add_arg!($:$, $:4) %*/
5223 | block_call call_op2 operation2 opt_paren_args brace_block
5225 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5226 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5227 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5229 | block_call call_op2 operation2 command_args do_block
5231 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5232 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5234 | block_call call_op2 paren_args
5236 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5237 nd_set_line($$, @2.end_pos.lineno);
5238 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5242method_call : fcall paren_args
5246 nd_set_last_loc($1, @2.end_pos);
5247 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5249 | primary_value call_op operation2 opt_paren_args
5251 bool has_args = $4 != 0;
5252 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5253 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5254 nd_set_line($$, @3.end_pos.lineno);
5255 /*% ripper: call!($:1, $:2, $:3) %*/
5257 /*% ripper: method_add_arg!($:$, $:4) %*/
5260 | primary_value tCOLON2 operation2 paren_args
5262 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5263 nd_set_line($$, @3.end_pos.lineno);
5264 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5266 | primary_value tCOLON2 operation3
5268 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5269 /*% ripper: call!($:1, $:2, $:3) %*/
5271 | primary_value call_op2 paren_args
5273 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5274 nd_set_line($$, @2.end_pos.lineno);
5275 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5277 | keyword_super paren_args
5279 rb_code_location_t lparen_loc = @2;
5280 rb_code_location_t rparen_loc = @2;
5281 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5282 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5284 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5285 /*% ripper: super!($:2) %*/
5289 $$ = NEW_ZSUPER(&@$);
5290 /*% ripper: zsuper! %*/
5292 | primary_value '[' opt_call_args rbracket
5294 $$ = NEW_CALL($1, tAREF, $3, &@$);
5296 /*% ripper: aref!($:1, $:3) %*/
5300brace_block : '{' brace_body '}'
5303 set_embraced_location($$, &@1, &@3);
5306 | k_do do_body k_end
5309 set_embraced_location($$, &@1, &@3);
5314brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5315 max_numparam numparam it_id allow_exits
5316 opt_block_param_def[args] compstmt(stmts)
5318 int max_numparam = p->max_numparam;
5319 ID it_id = p->it_id;
5320 p->max_numparam = $max_numparam;
5322 $args = args_with_numbered(p, $args, max_numparam, it_id);
5323 $$ = NEW_ITER($args, $compstmt, &@$);
5324 /*% ripper: brace_block!($:args, $:compstmt) %*/
5325 restore_block_exit(p, $allow_exits);
5326 numparam_pop(p, $numparam);
5335 max_numparam numparam it_id allow_exits
5336 opt_block_param_def[args] bodystmt
5338 int max_numparam = p->max_numparam;
5339 ID it_id = p->it_id;
5340 p->max_numparam = $max_numparam;
5342 $args = args_with_numbered(p, $args, max_numparam, it_id);
5343 $$ = NEW_ITER($args, $bodystmt, &@$);
5344 /*% ripper: do_block!($:args, $:bodystmt) %*/
5346 restore_block_exit(p, $allow_exits);
5347 numparam_pop(p, $numparam);
5352case_args : arg_value
5354 check_literal_when(p, $arg_value, &@arg_value);
5355 $$ = NEW_LIST($arg_value, &@$);
5356 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5360 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5361 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5363 | case_args[non_last_args] ',' arg_value
5365 check_literal_when(p, $arg_value, &@arg_value);
5366 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5367 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5369 | case_args[non_last_args] ',' tSTAR arg_value
5371 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5372 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5376case_body : k_when case_args then
5380 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5382 /*% ripper: when!($:2, $:4, $:5) %*/
5390p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5391p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5395 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5396 p->command_start = FALSE;
5397 p->ctxt.in_kwarg = 1;
5398 p->ctxt.in_alt_pattern = 0;
5399 p->ctxt.capture_in_pattern = 0;
5403p_case_body : keyword_in
5404 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5405 p_top_expr[expr] then
5407 pop_pktbl(p, $p_pktbl);
5408 pop_pvtbl(p, $p_pvtbl);
5409 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5410 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5411 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5416 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5417 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5425p_top_expr : p_top_expr_body
5426 | p_top_expr_body modifier_if expr_value
5428 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5430 /*% ripper: if_mod!($:3, $:1) %*/
5432 | p_top_expr_body modifier_unless expr_value
5434 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5436 /*% ripper: unless_mod!($:3, $:1) %*/
5440p_top_expr_body : p_expr
5443 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5444 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5445 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5449 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5450 nd_set_first_loc($$, @1.beg_pos);
5451 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5455 $$ = new_find_pattern(p, 0, $1, &@$);
5456 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5460 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5461 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5465 $$ = new_hash_pattern(p, 0, $1, &@$);
5466 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5473p_as : p_expr tASSOC p_variable
5475 NODE *n = NEW_LIST($1, &@$);
5476 n = list_append(p, n, $3);
5477 $$ = new_hash(p, n, &@$);
5478 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5483p_alt : p_alt[left] '|'[alt]
5485 p->ctxt.in_alt_pattern = 1;
5489 if (p->ctxt.capture_in_pattern) {
5490 yyerror1(&@alt, "alternative pattern after variable capture");
5492 p->ctxt.in_alt_pattern = 0;
5493 $$ = NEW_OR($left, $right, &@$, &@alt);
5494 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5499p_lparen : '(' p_pktbl
5506p_lbracket : '[' p_pktbl
5513p_expr_basic : p_value
5515 | p_const p_lparen[p_pktbl] p_args rparen
5517 pop_pktbl(p, $p_pktbl);
5518 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5519 nd_set_first_loc($$, @p_const.beg_pos);
5520 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5522 | p_const p_lparen[p_pktbl] p_find rparen
5524 pop_pktbl(p, $p_pktbl);
5525 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5526 nd_set_first_loc($$, @p_const.beg_pos);
5527 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5529 | p_const p_lparen[p_pktbl] p_kwargs rparen
5531 pop_pktbl(p, $p_pktbl);
5532 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5533 nd_set_first_loc($$, @p_const.beg_pos);
5534 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5536 | p_const '(' rparen
5538 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5539 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5540 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5542 | p_const p_lbracket[p_pktbl] p_args rbracket
5544 pop_pktbl(p, $p_pktbl);
5545 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5546 nd_set_first_loc($$, @p_const.beg_pos);
5547 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5549 | p_const p_lbracket[p_pktbl] p_find rbracket
5551 pop_pktbl(p, $p_pktbl);
5552 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5553 nd_set_first_loc($$, @p_const.beg_pos);
5554 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5556 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5558 pop_pktbl(p, $p_pktbl);
5559 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5560 nd_set_first_loc($$, @p_const.beg_pos);
5561 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5563 | p_const '[' rbracket
5565 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5566 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5567 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5569 | tLBRACK p_args rbracket
5571 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5572 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5574 | tLBRACK p_find rbracket
5576 $$ = new_find_pattern(p, 0, $p_find, &@$);
5577 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5581 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5582 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5583 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5585 | tLBRACE p_pktbl lex_ctxt[ctxt]
5587 p->ctxt.in_kwarg = 0;
5591 pop_pktbl(p, $p_pktbl);
5592 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5593 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5594 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5598 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5599 $$ = new_hash_pattern(p, 0, $$, &@$);
5600 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5602 | tLPAREN p_pktbl p_expr rparen
5604 pop_pktbl(p, $p_pktbl);
5606 /*% ripper: $:p_expr %*/
5612 NODE *pre_args = NEW_LIST($1, &@$);
5613 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5614 /*% ripper: [[$:1], Qnil, Qnil] %*/
5618 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5619 /*% ripper: [$:1, Qnil, Qnil] %*/
5623 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5624 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5626 | p_args_head p_rest
5628 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5629 /*% ripper: [$:1, $:2, Qnil] %*/
5631 | p_args_head p_rest ',' p_args_post
5633 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5634 /*% ripper: [$:1, $:2, $:4] %*/
5639p_args_head : p_arg ','
5640 | p_args_head p_arg ','
5642 $$ = list_concat($1, $2);
5643 /*% ripper: rb_ary_concat($:1, $:2) %*/
5649 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5650 /*% ripper: [Qnil, $:1, Qnil] %*/
5652 | p_rest ',' p_args_post
5654 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5655 /*% ripper: [Qnil, $:1, $:3] %*/
5659p_find : p_rest ',' p_args_post ',' p_rest
5661 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5662 /*% ripper: [$:1, $:3, $:5] %*/
5667p_rest : tSTAR tIDENTIFIER
5669 error_duplicate_pattern_variable(p, $2, &@2);
5670 /*% ripper: var_field!($:2) %*/
5671 $$ = assignable(p, $2, 0, &@$);
5676 /*% ripper: var_field!(Qnil) %*/
5681 | p_args_post ',' p_arg
5683 $$ = list_concat($1, $3);
5684 /*% ripper: rb_ary_concat($:1, $:3) %*/
5690 $$ = NEW_LIST($1, &@$);
5691 /*% ripper: [$:1] %*/
5695p_kwargs : p_kwarg ',' p_any_kwrest
5697 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5698 /*% ripper: [$:1, $:3] %*/
5702 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5703 /*% ripper: [$:1, Qnil] %*/
5707 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5708 /*% ripper: [$:1, Qnil] %*/
5712 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5713 /*% ripper: [[], $:1] %*/
5718 /*% ripper[brace]: [$:1] %*/
5721 $$ = list_concat($1, $3);
5722 /*% ripper: rb_ary_push($:1, $:3) %*/
5726p_kw : p_kw_label p_expr
5728 error_duplicate_pattern_key(p, $1, &@1);
5729 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5730 /*% ripper: [$:1, $:2] %*/
5734 error_duplicate_pattern_key(p, $1, &@1);
5735 if ($1 && !is_local_id($1)) {
5736 yyerror1(&@1, "key must be valid as local variables");
5738 error_duplicate_pattern_variable(p, $1, &@1);
5739 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5740 /*% ripper: [$:1, Qnil] %*/
5745 | tSTRING_BEG string_contents tLABEL_END
5747 YYLTYPE loc = code_loc_gen(&@1, &@3);
5748 if (!$2 || nd_type_p($2, NODE_STR)) {
5749 NODE *node = dsym_node(p, $2, &loc);
5750 $$ = rb_sym2id(rb_node_sym_string_val(node));
5753 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5754 $$ = rb_intern_str(STR_NEW0());
5760p_kwrest : kwrest_mark tIDENTIFIER
5763 /*% ripper: var_field!($:2) %*/
5768 /*% ripper: Qnil %*/
5772p_kwnorest : kwrest_mark keyword_nil
5778p_any_kwrest : p_kwrest
5782 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5786p_value : p_primitive
5787 | range_expr(p_primitive)
5793p_primitive : inline_primary
5796 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5797 /*% ripper: var_ref!($:1) %*/
5802p_variable : tIDENTIFIER
5804 error_duplicate_pattern_variable(p, $1, &@1);
5805 /*% ripper: var_field!($:1) %*/
5806 $$ = assignable(p, $1, 0, &@$);
5810p_var_ref : '^' tIDENTIFIER
5812 NODE *n = gettable(p, $2, &@$);
5816 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5817 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5820 /*% ripper: var_ref!($:2) %*/
5824 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5825 /*% ripper: var_ref!($:2) %*/
5829p_expr_ref : '^' tLPAREN expr_value rparen
5831 $$ = NEW_BLOCK($3, &@$);
5832 /*% ripper: begin!($:3) %*/
5836p_const : tCOLON3 cname
5838 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5839 /*% ripper: top_const_ref!($:2) %*/
5841 | p_const tCOLON2 cname
5843 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5844 /*% ripper: const_path_ref!($:1, $:3) %*/
5848 $$ = gettable(p, $1, &@$);
5849 /*% ripper: var_ref!($:1) %*/
5853opt_rescue : k_rescue exc_list exc_var then
5859 err = NEW_ERRINFO(&@3);
5860 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5862 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5872 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5879 $$ = NEW_LIST($1, &@$);
5880 /*% ripper: rb_ary_new3(1, $:1) %*/
5884 if (!($$ = splat_array($1))) $$ = $1;
5897opt_ensure : k_ensure stmts terms?
5899 p->ctxt.in_rescue = $1.in_rescue;
5901 void_expr(p, void_stmts(p, $$));
5902 /*% ripper: ensure!($:2) %*/
5914 $$ = NEW_STR(STRING_NEW0(), &@$);
5917 $$ = evstr2dstr(p, $1);
5927 $$ = literal_concat(p, $1, $2, &@$);
5928 /*% ripper: string_concat!($:1, $:2) %*/
5932string1 : tSTRING_BEG string_contents tSTRING_END
5934 $$ = heredoc_dedent(p, $2);
5935 if ($$) nd_set_loc($$, &@$);
5937 if (p->heredoc_indent > 0) {
5938 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5939 p->heredoc_indent = 0;
5941 /*% ripper: string_literal!($:$) %*/
5945xstring : tXSTRING_BEG xstring_contents tSTRING_END
5947 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5949 if (p->heredoc_indent > 0) {
5950 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5951 p->heredoc_indent = 0;
5953 /*% ripper: xstring_literal!($:$) %*/
5957regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5959 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5960 /*% ripper: regexp_literal!($:2, $:3) %*/
5964words : words(tWORDS_BEG, word_list)
5967word_list : /* none */
5970 /*% ripper: words_new! %*/
5972 | word_list word ' '+
5974 $$ = list_append(p, $1, evstr2dstr(p, $2));
5975 /*% ripper: words_add!($:1, $:2) %*/
5979word : string_content
5980 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5981 | word string_content
5983 $$ = literal_concat(p, $1, $2, &@$);
5984 /*% ripper: word_add!($:1, $:2) %*/
5988symbols : words(tSYMBOLS_BEG, symbol_list)
5991symbol_list : /* none */
5994 /*% ripper: symbols_new! %*/
5996 | symbol_list word ' '+
5998 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5999 /*% ripper: symbols_add!($:1, $:2) %*/
6003qwords : words(tQWORDS_BEG, qword_list)
6006qsymbols : words(tQSYMBOLS_BEG, qsym_list)
6009qword_list : /* none */
6012 /*% ripper: qwords_new! %*/
6014 | qword_list tSTRING_CONTENT ' '+
6016 $$ = list_append(p, $1, $2);
6017 /*% ripper: qwords_add!($:1, $:2) %*/
6021qsym_list : /* none */
6024 /*% ripper: qsymbols_new! %*/
6026 | qsym_list tSTRING_CONTENT ' '+
6028 $$ = symbol_append(p, $1, $2);
6029 /*% ripper: qsymbols_add!($:1, $:2) %*/
6033string_contents : /* none */
6036 /*% ripper: string_content! %*/
6038 | string_contents string_content
6040 $$ = literal_concat(p, $1, $2, &@$);
6041 /*% ripper: string_add!($:1, $:2) %*/
6045xstring_contents: /* none */
6048 /*% ripper: xstring_new! %*/
6050 | xstring_contents string_content
6052 $$ = literal_concat(p, $1, $2, &@$);
6053 /*% ripper: xstring_add!($:1, $:2) %*/
6057regexp_contents : /* none */
6060 /*% ripper: regexp_new! %*/
6062 | regexp_contents string_content
6064 NODE *head = $1, *tail = $2;
6072 switch (nd_type(head)) {
6074 head = str2dstr(p, head);
6079 head = list_append(p, NEW_DSTR(0, &@$), head);
6082 $$ = list_append(p, head, tail);
6084 /*% ripper: regexp_add!($:1, $:2) %*/
6088string_content : tSTRING_CONTENT
6089 /*% ripper[brace]: $:1 %*/
6092 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6093 $$ = p->lex.strterm;
6095 SET_LEX_STATE(EXPR_BEG);
6099 p->lex.strterm = $2;
6100 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6101 nd_set_line($$, @3.end_pos.lineno);
6102 /*% ripper: string_dvar!($:3) %*/
6104 | tSTRING_DBEG[state]
6108 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6109 $$ = p->lex.strterm;
6111 SET_LEX_STATE(EXPR_BEG);
6114 $$ = p->lex.brace_nest;
6115 p->lex.brace_nest = 0;
6118 $$ = p->lex.lpar_beg;
6119 p->lex.lpar_beg = -1;
6122 $$ = p->heredoc_indent;
6123 p->heredoc_indent = 0;
6125 compstmt(stmts) string_dend
6129 p->lex.strterm = $term;
6130 SET_LEX_STATE($state);
6131 p->lex.brace_nest = $brace;
6132 p->lex.lpar_beg = $lpar;
6133 p->heredoc_indent = $indent;
6134 p->heredoc_line_indent = -1;
6135 if ($compstmt) nd_unset_fl_newline($compstmt);
6136 $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend);
6137 /*% ripper: string_embexpr!($:compstmt) %*/
6141string_dend : tSTRING_DEND
6145string_dvar : nonlocal_var
6147 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6148 /*% ripper: var_ref!($:1) %*/
6159 SET_LEX_STATE(EXPR_END);
6160 VALUE str = rb_id2str($2);
6163 * set_yylval_noname sets invalid id to yylval.
6164 * This branch can be removed once yylval is changed to
6165 * hold lexed string.
6167 if (!str) str = STR_NEW0();
6168 $$ = NEW_SYM(str, &@$);
6169 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6177dsym : tSYMBEG string_contents tSTRING_END
6179 SET_LEX_STATE(EXPR_END);
6180 $$ = dsym_node(p, $2, &@$);
6181 /*% ripper: dyna_symbol!($:2) %*/
6185numeric : simple_numeric
6186 | tUMINUS_NUM simple_numeric %prec tLOWEST
6189 negate_lit(p, $$, &@$);
6190 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6194simple_numeric : tINTEGER
6205user_variable : ident_or_const
6209keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6210 | keyword_self {$$ = KWD2EID(self, $1);}
6211 | keyword_true {$$ = KWD2EID(true, $1);}
6212 | keyword_false {$$ = KWD2EID(false, $1);}
6213 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6214 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6215 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6218var_ref : user_variable
6220 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6221 if (ifdef_ripper(id_is_var(p, $1), false)) {
6222 /*% ripper: var_ref!($:1) %*/
6225 /*% ripper: vcall!($:1) %*/
6230 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6231 /*% ripper: var_ref!($:1) %*/
6235var_lhs : user_or_keyword_variable
6237 /*% ripper: var_field!($:1) %*/
6238 $$ = assignable(p, $1, 0, &@$);
6248 SET_LEX_STATE(EXPR_BEG);
6249 p->command_start = TRUE;
6259f_opt_paren_args: f_paren_args
6262 p->ctxt.in_argdef = 0;
6263 $$ = new_args_tail(p, 0, 0, 0, &@0);
6264 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6265 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6269f_paren_args : '(' f_args rparen
6272 /*% ripper: paren!($:2) %*/
6273 SET_LEX_STATE(EXPR_BEG);
6274 p->command_start = TRUE;
6275 p->ctxt.in_argdef = 0;
6279f_arglist : f_paren_args
6282 p->ctxt.in_kwarg = 1;
6283 p->ctxt.in_argdef = 1;
6284 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6288 p->ctxt.in_kwarg = $1.in_kwarg;
6289 p->ctxt.in_argdef = 0;
6291 SET_LEX_STATE(EXPR_BEG);
6292 p->command_start = TRUE;
6297args_tail : args_tail_basic(arg_value)
6300 ID fwd = $args_forward;
6301 if (lambda_beginning_p() ||
6302 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6303 yyerror0("unexpected ... in lambda argument");
6307 add_forwarding_args(p);
6309 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6310 $$->nd_ainfo.forwarding = 1;
6311 /*% ripper: [Qnil, $:1, Qnil] %*/
6315f_args : f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6317 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6318 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6320 | f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6322 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6323 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6325 | f_arg ',' f_opt_arg(arg_value) opt_args_tail(args_tail)
6327 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6328 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6330 | f_arg ',' f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6332 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6333 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6335 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6337 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6338 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6340 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6342 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6343 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6345 | f_arg opt_args_tail(args_tail)
6347 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6348 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6350 | f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6352 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6353 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6355 | f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6357 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6358 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6360 | f_opt_arg(arg_value) opt_args_tail(args_tail)
6362 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6363 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6365 | f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail)
6367 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6368 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6370 | f_rest_arg opt_args_tail(args_tail)
6372 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6373 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6375 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6377 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6378 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6382 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6383 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6387 $$ = new_args_tail(p, 0, 0, 0, &@0);
6388 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6389 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6393args_forward : tBDOT3
6396 /*% ripper: args_forward! %*/
6400f_bad_arg : tCONSTANT
6402 static const char mesg[] = "formal argument cannot be a constant";
6404 yyerror1(&@1, mesg);
6407 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6411 static const char mesg[] = "formal argument cannot be an instance variable";
6413 yyerror1(&@1, mesg);
6416 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6420 static const char mesg[] = "formal argument cannot be a global variable";
6422 yyerror1(&@1, mesg);
6425 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6429 static const char mesg[] = "formal argument cannot be a class variable";
6431 yyerror1(&@1, mesg);
6434 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6438f_norm_arg : f_bad_arg
6441 VALUE e = formal_argument_error(p, $$ = $1);
6443 /*% ripper[error]: param_error!(?e, $:1) %*/
6445 p->max_numparam = ORDINAL_PARAM;
6449f_arg_asgn : f_norm_arg
6456f_arg_item : f_arg_asgn
6458 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6461 | tLPAREN f_margs rparen
6463 ID tid = internal_id(p);
6465 loc.beg_pos = @2.beg_pos;
6466 loc.end_pos = @2.beg_pos;
6468 if (dyna_in_block(p)) {
6469 $2->nd_value = NEW_DVAR(tid, &loc);
6472 $2->nd_value = NEW_LVAR(tid, &loc);
6474 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6475 $$->nd_next = (NODE *)$2;
6476 /*% ripper: mlhs_paren!($:2) %*/
6481 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6482 | f_arg ',' f_arg_item
6486 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6487 rb_discard_node(p, (NODE *)$3);
6488 /*% ripper: rb_ary_push($:1, $:3) %*/
6495 VALUE e = formal_argument_error(p, $$ = $1);
6498 /*% ripper[error]: param_error!(?e, $:1) %*/
6501 * Workaround for Prism::ParseTest#test_filepath for
6502 * "unparser/corpus/literal/def.txt"
6504 * See the discussion on https://github.com/ruby/ruby/pull/9923
6506 arg_var(p, ifdef_ripper(0, $1));
6508 p->max_numparam = ORDINAL_PARAM;
6509 p->ctxt.in_argdef = 0;
6517f_no_kwarg : p_kwnorest
6519 /*% ripper: nokw_param!(Qnil) %*/
6523f_kwrest : kwrest_mark tIDENTIFIER
6525 arg_var(p, shadowing_lvar(p, $2));
6527 /*% ripper: kwrest_param!($:2) %*/
6531 arg_var(p, idFWD_KWREST);
6533 /*% ripper: kwrest_param!(Qnil) %*/
6541f_rest_arg : restarg_mark tIDENTIFIER
6543 arg_var(p, shadowing_lvar(p, $2));
6545 /*% ripper: rest_param!($:2) %*/
6549 arg_var(p, idFWD_REST);
6551 /*% ripper: rest_param!(Qnil) %*/
6559f_block_arg : blkarg_mark tIDENTIFIER
6561 arg_var(p, shadowing_lvar(p, $2));
6563 /*% ripper: blockarg!($:2) %*/
6567 arg_var(p, idFWD_BLOCK);
6569 /*% ripper: blockarg!(Qnil) %*/
6573opt_f_block_arg : ',' f_block_arg
6582singleton : value_expr(singleton_expr)
6584 NODE *expr = last_expr_node($1);
6585 switch (nd_type(expr)) {
6599 case NODE_IMAGINARY:
6603 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6612singleton_expr : var_ref
6615 SET_LEX_STATE(EXPR_BEG);
6616 p->ctxt.in_argdef = 0;
6620 p->ctxt.in_argdef = 1;
6622 /*% ripper: paren!($:3) %*/
6630 /*% ripper: assoclist_from_args!($:1) %*/
6635 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6644 NODE *n = RNODE_LIST(tail)->nd_next;
6645 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6646 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6648 tail = RNODE_HASH(n)->nd_head;
6651 assocs = list_concat(assocs, tail);
6655 /*% ripper: rb_ary_push($:1, $:3) %*/
6659assoc : arg_value tASSOC arg_value
6661 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6662 /*% ripper: assoc_new!($:1, $:3) %*/
6666 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6667 /*% ripper: assoc_new!($:1, $:2) %*/
6671 NODE *val = gettable(p, $1, &@$);
6672 if (!val) val = NEW_ERROR(&@$);
6673 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6674 /*% ripper: assoc_new!($:1, Qnil) %*/
6676 | tSTRING_BEG string_contents tLABEL_END arg_value
6678 YYLTYPE loc = code_loc_gen(&@1, &@3);
6679 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6680 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6684 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6685 /*% ripper: assoc_splat!($:2) %*/
6689 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6690 $$ = list_append(p, NEW_LIST(0, &@$),
6691 NEW_LVAR(idFWD_KWREST, &@$));
6692 /*% ripper: assoc_splat!(Qnil) %*/
6696%rule %inline operation : ident_or_const
6700operation2 : operation
6704operation3 : tIDENTIFIER
6738 if (p->ctxt.in_defined) {
6739 p->ctxt.has_trailing_semicolon = 1;
6744 @$.end_pos = @$.beg_pos;
6750 | terms ';' {yyerrok;}
6756 /*% ripper: Qnil %*/
6763# define yylval (*p->lval)
6765static int regx_options(struct parser_params*);
6766static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6767static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6768static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6769static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6771#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6773# define set_yylval_node(x) { \
6775 rb_parser_set_location(p, &_cur_loc); \
6776 yylval.node = (x); \
6777 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6779# define set_yylval_str(x) \
6781 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6782 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6784# define set_yylval_num(x) { \
6786 set_parser_s_value(x); \
6788# define set_yylval_id(x) (yylval.id = (x))
6789# define set_yylval_name(x) { \
6790 (yylval.id = (x)); \
6791 set_parser_s_value(ID2SYM(x)); \
6793# define yylval_id() (yylval.id)
6795#define set_yylval_noname() set_yylval_id(keyword_nil)
6796#define has_delayed_token(p) (p->delayed.token != NULL)
6799#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6800#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6803parser_has_token(struct parser_params *p)
6805 const char *const pcur = p->lex.pcur;
6806 const char *const ptok = p->lex.ptok;
6807 if (p->keep_tokens && (pcur < ptok)) {
6808 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6809 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6818 case '"': return "\\\"";
6819 case '\\': return "\\\\";
6820 case '\0': return "\\0";
6821 case '\n': return "\\n";
6822 case '\r': return "\\r";
6823 case '\t': return "\\t";
6824 case '\f': return "\\f";
6825 case '\013': return "\\v";
6826 case '\010': return "\\b";
6827 case '\007': return "\\a";
6828 case '\033': return "\\e";
6829 case '\x7f': return "\\c?";
6834static rb_parser_string_t *
6835rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6837 rb_encoding *enc = p->enc;
6838 const char *ptr = str->ptr;
6839 const char *pend = ptr + str->len;
6840 const char *prev = ptr;
6841 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6842 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6844 while (ptr < pend) {
6847 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6848 if (!MBCLEN_CHARFOUND_P(n)) {
6849 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6850 n = rb_enc_mbminlen(enc);
6852 n = (int)(pend - ptr);
6854 c = *ptr & 0xf0 >> 4;
6855 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6857 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6858 parser_str_cat(result, charbuf, 4);
6863 n = MBCLEN_CHARFOUND_LEN(n);
6864 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6866 cc = escaped_char(c);
6868 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6869 parser_str_cat_cstr(result, cc);
6872 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6875 if (ptr - n > prev) {
6876 parser_str_cat(result, prev, ptr - n - prev);
6879 parser_str_cat(result, prev, ptr - prev);
6883 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6889parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6891 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6892 token->id = p->token_id;
6893 token->type_name = parser_token2char(p, t);
6895 token->loc.beg_pos = p->yylloc->beg_pos;
6896 token->loc.end_pos = p->yylloc->end_pos;
6897 rb_parser_ary_push_ast_token(p, p->tokens, token);
6901 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6902 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6903 line, token->id, token->type_name, str_escaped->ptr,
6904 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6905 token->loc.end_pos.lineno, token->loc.end_pos.column);
6906 rb_parser_string_free(p, str_escaped);
6911parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6913 debug_token_line(p, "parser_dispatch_scan_event", line);
6915 if (!parser_has_token(p)) return;
6917 RUBY_SET_YYLLOC(*p->yylloc);
6919 if (p->keep_tokens) {
6920 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6921 parser_append_tokens(p, str, t, line);
6927#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6929parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6931 debug_token_line(p, "parser_dispatch_delayed_token", line);
6933 if (!has_delayed_token(p)) return;
6935 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6937 if (p->keep_tokens) {
6938 /* p->delayed.token is freed by rb_parser_tokens_free */
6939 parser_append_tokens(p, p->delayed.token, t, line);
6942 rb_parser_string_free(p, p->delayed.token);
6945 p->delayed.token = NULL;
6948#define literal_flush(p, ptr) ((void)(ptr))
6951ripper_has_scan_event(struct parser_params *p)
6953 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6954 return p->lex.pcur > p->lex.ptok;
6958ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6960 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6961 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6962 RUBY_SET_YYLLOC(*p->yylloc);
6968ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6970 if (!ripper_has_scan_event(p)) return;
6972 set_parser_s_value(ripper_scan_event_val(p, t));
6974#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6977ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6979 /* save and adjust the location to delayed token for callbacks */
6980 int saved_line = p->ruby_sourceline;
6981 const char *saved_tokp = p->lex.ptok;
6984 if (!has_delayed_token(p)) return;
6985 p->ruby_sourceline = p->delayed.beg_line;
6986 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6987 str = rb_str_new_mutable_parser_string(p->delayed.token);
6988 rb_parser_string_free(p, p->delayed.token);
6989 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6990 set_parser_s_value(s_value);
6991 p->delayed.token = NULL;
6992 p->ruby_sourceline = saved_line;
6993 p->lex.ptok = saved_tokp;
6995#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6999is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7001 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7005peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
7007 const char *ptr = p->lex.pcur + at;
7008 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
7009 if (memcmp(ptr, str, len)) return false;
7010 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
7012 case '!': case '?': return false;
7014 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
7018parser_is_identchar(struct parser_params *p)
7020 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7024parser_isascii(struct parser_params *p)
7026 return ISASCII(*(p->lex.pcur-1));
7030token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7032 int column = 1, nonspc = 0, i;
7033 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7035 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7038 if (*ptr != ' ' && *ptr != '\t') {
7043 ptinfo->beg = loc->beg_pos;
7044 ptinfo->indent = column;
7045 ptinfo->nonspc = nonspc;
7049token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7053 if (!p->token_info_enabled) return;
7054 ptinfo = ALLOC(token_info);
7055 ptinfo->token = token;
7056 ptinfo->next = p->token_info;
7057 token_info_setup(ptinfo, p->lex.pbeg, loc);
7059 p->token_info = ptinfo;
7063token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7065 token_info *ptinfo_beg = p->token_info;
7067 if (!ptinfo_beg) return;
7069 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7070 token_info_warn(p, token, ptinfo_beg, 1, loc);
7072 p->token_info = ptinfo_beg->next;
7073 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7077token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7079 token_info *ptinfo_beg = p->token_info;
7081 if (!ptinfo_beg) return;
7082 p->token_info = ptinfo_beg->next;
7084 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7085 ptinfo_beg->beg.column != beg_pos.column ||
7086 strcmp(ptinfo_beg->token, token)) {
7087 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7088 beg_pos.lineno, beg_pos.column, token,
7089 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7093 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7097token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7099 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7100 if (!p->token_info_enabled) return;
7101 if (!ptinfo_beg) return;
7102 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7103 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7104 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7105 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7106 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7107 rb_warn3L(ptinfo_end->beg.lineno,
7108 "mismatched indentations at '%s' with '%s' at %d",
7109 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7113parser_precise_mbclen(struct parser_params *p, const char *ptr)
7115 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7116 if (!MBCLEN_CHARFOUND_P(len)) {
7117 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7125parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7127 rb_parser_string_t *str;
7128 int lineno = p->ruby_sourceline;
7132 else if (yylloc->beg_pos.lineno == lineno) {
7133 str = p->lex.lastline;
7138 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7142parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7148 yylloc = RUBY_SET_YYLLOC(current);
7150 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7151 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7155 parser_compile_error(p, yylloc, "%s", msg);
7156 parser_show_error_line(p, yylloc);
7161parser_yyerror0(struct parser_params *p, const char *msg)
7164 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7168ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7171 const int max_line_margin = 30;
7172 const char *ptr, *ptr_end, *pt, *pb;
7173 const char *pre = "", *post = "", *pend;
7174 const char *code = "", *caret = "";
7176 const char *const pbeg = PARSER_STRING_PTR(str);
7181 if (!yylloc) return;
7182 pend = rb_parser_string_end(str);
7183 if (pend > pbeg && pend[-1] == '\n') {
7184 if (--pend > pbeg && pend[-1] == '\r') --pend;
7188 if (lineno == yylloc->end_pos.lineno &&
7189 (pend - pbeg) > yylloc->end_pos.column) {
7190 pt = pbeg + yylloc->end_pos.column;
7194 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7195 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7197 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7198 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7200 len = ptr_end - ptr;
7203 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7204 if (ptr > pbeg) pre = "...";
7206 if (ptr_end < pend) {
7207 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7208 if (ptr_end < pend) post = "...";
7212 if (lineno == yylloc->beg_pos.lineno) {
7213 pb += yylloc->beg_pos.column;
7214 if (pb > pt) pb = pt;
7216 if (pb < ptr) pb = ptr;
7217 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7220 if (RTEST(errbuf)) {
7221 mesg = rb_attr_get(errbuf, idMesg);
7222 if (char_at_end(p, mesg, '\n') != '\n')
7223 rb_str_cat_cstr(mesg, "\n");
7226 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7228 if (!errbuf && rb_stderr_tty_p()) {
7229#define CSI_BEGIN "\033["
7232 CSI_BEGIN""CSI_SGR"%s" /* pre */
7233 CSI_BEGIN"1"CSI_SGR"%.*s"
7234 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7235 CSI_BEGIN";1"CSI_SGR"%.*s"
7236 CSI_BEGIN""CSI_SGR"%s" /* post */
7239 (int)(pb - ptr), ptr,
7241 (int)(ptr_end - pt), pt,
7247 len = ptr_end - ptr;
7248 lim = pt < pend ? pt : pend;
7249 i = (int)(lim - ptr);
7250 buf = ALLOCA_N(char, i+2);
7255 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7261 memset(p2, '~', (lim - ptr));
7265 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7266 pre, (int)len, code, post,
7269 if (!errbuf) rb_write_error_str(mesg);
7274parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7276 const char *pcur = 0, *ptok = 0;
7277 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7278 p->ruby_sourceline == yylloc->end_pos.lineno) {
7281 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7282 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7284 parser_yyerror0(p, msg);
7293parser_yyerror0(struct parser_params *p, const char *msg)
7295 dispatch1(parse_error, STR_NEW2(msg));
7301parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7307vtable_size(const struct vtable *tbl)
7309 if (!DVARS_TERMINAL_P(tbl)) {
7317static struct vtable *
7318vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7320 struct vtable *tbl = ALLOC(struct vtable);
7323 tbl->tbl = ALLOC_N(ID, tbl->capa);
7327 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7332#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7335vtable_free_gen(struct parser_params *p, int line, const char *name,
7340 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7343 if (!DVARS_TERMINAL_P(tbl)) {
7345 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7347 ruby_sized_xfree(tbl, sizeof(*tbl));
7350#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7353vtable_add_gen(struct parser_params *p, int line, const char *name,
7354 struct vtable *tbl, ID id)
7358 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7359 line, name, (void *)tbl, rb_id2name(id));
7362 if (DVARS_TERMINAL_P(tbl)) {
7363 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7366 if (tbl->pos == tbl->capa) {
7367 tbl->capa = tbl->capa * 2;
7368 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7370 tbl->tbl[tbl->pos++] = id;
7372#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7375vtable_pop_gen(struct parser_params *p, int line, const char *name,
7376 struct vtable *tbl, int n)
7379 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7380 line, name, (void *)tbl, n);
7383 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7388#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7391vtable_included(const struct vtable * tbl, ID id)
7395 if (!DVARS_TERMINAL_P(tbl)) {
7396 for (i = 0; i < tbl->pos; i++) {
7397 if (tbl->tbl[i] == id) {
7405static void parser_prepare(struct parser_params *p);
7408e_option_supplied(struct parser_params *p)
7410 return strcmp(p->ruby_sourcefile, "-e") == 0;
7414static NODE *parser_append_options(struct parser_params *p, NODE *node);
7417yycompile0(VALUE arg)
7421 struct parser_params *p = (struct parser_params *)arg;
7424 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7428 if (p->debug_lines) {
7429 p->ast->body.script_lines = p->debug_lines;
7433#define RUBY_DTRACE_PARSE_HOOK(name) \
7434 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7435 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7437 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7439 RUBY_DTRACE_PARSE_HOOK(END);
7443 xfree(p->lex.strterm);
7445 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7446 if (n || p->error_p) {
7447 VALUE mesg = p->error_buffer;
7449 mesg = syntax_error_new();
7451 if (!p->error_tolerant) {
7452 rb_set_errinfo(mesg);
7456 tree = p->eval_tree;
7458 tree = NEW_NIL(&NULL_LOC);
7461 rb_parser_ary_t *tokens = p->tokens;
7463 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7464 prelude = block_append(p, p->eval_tree_begin, body);
7465 RNODE_SCOPE(tree)->nd_body = prelude;
7466 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7467 p->ast->body.coverage_enabled = cov;
7468 if (p->keep_tokens) {
7469 p->ast->node_buffer->tokens = tokens;
7473 p->ast->body.root = tree;
7474 p->ast->body.line_count = p->line_count;
7479yycompile(struct parser_params *p, VALUE fname, int line)
7483 p->ruby_sourcefile_string = Qnil;
7484 p->ruby_sourcefile = "(none)";
7487 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7488 p->ruby_sourcefile = StringValueCStr(fname);
7490 p->ruby_sourceline = line - 1;
7494 p->ast = ast = rb_ast_new();
7495 compile_callback(yycompile0, (VALUE)p);
7507must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7509 rb_encoding *enc = rb_parser_str_get_encoding(s);
7510 if (!rb_enc_asciicompat(enc)) {
7511 rb_raise(rb_eArgError, "invalid source encoding");
7516static rb_parser_string_t *
7517lex_getline(struct parser_params *p)
7519 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7520 if (!line) return 0;
7522 string_buffer_append(p, line);
7523 must_be_ascii_compatible(p, line);
7529rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7532 p->lex.input = input;
7533 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7535 return yycompile(p, fname, line);
7539#define STR_FUNC_ESCAPE 0x01
7540#define STR_FUNC_EXPAND 0x02
7541#define STR_FUNC_REGEXP 0x04
7542#define STR_FUNC_QWORDS 0x08
7543#define STR_FUNC_SYMBOL 0x10
7544#define STR_FUNC_INDENT 0x20
7545#define STR_FUNC_LABEL 0x40
7546#define STR_FUNC_LIST 0x4000
7547#define STR_FUNC_TERM 0x8000
7550 str_label = STR_FUNC_LABEL,
7552 str_dquote = (STR_FUNC_EXPAND),
7553 str_xquote = (STR_FUNC_EXPAND),
7554 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7555 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7556 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7557 str_ssym = (STR_FUNC_SYMBOL),
7558 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7561static rb_parser_string_t *
7562parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7564 rb_parser_string_t *pstr;
7566 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7568 if (!(func & STR_FUNC_REGEXP)) {
7569 if (rb_parser_is_ascii_string(p, pstr)) {
7571 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7572 /* everything is valid in ASCII-8BIT */
7573 enc = rb_ascii8bit_encoding();
7574 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7582strterm_is_heredoc(rb_strterm_t *strterm)
7584 return strterm->heredoc;
7587static rb_strterm_t *
7588new_strterm(struct parser_params *p, int func, int term, int paren)
7590 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7591 strterm->u.literal.func = func;
7592 strterm->u.literal.term = term;
7593 strterm->u.literal.paren = paren;
7597static rb_strterm_t *
7598new_heredoc(struct parser_params *p)
7600 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7601 strterm->heredoc = true;
7605#define peek(p,c) peek_n(p, (c), 0)
7606#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7607#define peekc(p) peekc_n(p, 0)
7608#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7610#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7612parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7614 debug_token_line(p, "add_delayed_token", line);
7617 if (has_delayed_token(p)) {
7618 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7619 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7620 int end_col = (next_line ? 0 : p->delayed.end_col);
7621 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7622 dispatch_delayed_token(p, tSTRING_CONTENT);
7625 if (!has_delayed_token(p)) {
7626 p->delayed.token = rb_parser_string_new(p, 0, 0);
7627 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7628 p->delayed.beg_line = p->ruby_sourceline;
7629 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7631 parser_str_cat(p->delayed.token, tok, end - tok);
7632 p->delayed.end_line = p->ruby_sourceline;
7633 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7639set_lastline(struct parser_params *p, rb_parser_string_t *str)
7641 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7642 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7643 p->lex.lastline = str;
7647nextline(struct parser_params *p, int set_encoding)
7649 rb_parser_string_t *str = p->lex.nextline;
7650 p->lex.nextline = 0;
7655 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7659 if (!p->lex.input || !(str = lex_getline(p))) {
7666 if (p->debug_lines) {
7667 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7668 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7669 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7674 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7675 /* after here-document without terminator */
7678 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7679 if (p->heredoc_end > 0) {
7680 p->ruby_sourceline = p->heredoc_end;
7683 p->ruby_sourceline++;
7684 set_lastline(p, str);
7690parser_cr(struct parser_params *p, int c)
7692 if (peek(p, '\n')) {
7700nextc0(struct parser_params *p, int set_encoding)
7704 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7705 if (nextline(p, set_encoding)) return -1;
7707 c = (unsigned char)*p->lex.pcur++;
7708 if (UNLIKELY(c == '\r')) {
7709 c = parser_cr(p, c);
7714#define nextc(p) nextc0(p, TRUE)
7717pushback(struct parser_params *p, int c)
7719 if (c == -1) return;
7722 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7727#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7729#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7730#define tok(p) (p)->tokenbuf
7731#define toklen(p) (p)->tokidx
7734looking_at_eol_p(struct parser_params *p)
7736 const char *ptr = p->lex.pcur;
7737 while (!lex_eol_ptr_p(p, ptr)) {
7738 int c = (unsigned char)*ptr++;
7739 int eol = (c == '\n' || c == '#');
7740 if (eol || !ISSPACE(c)) {
7748newtok(struct parser_params *p)
7753 p->tokenbuf = ALLOC_N(char, 60);
7755 if (p->toksiz > 4096) {
7757 REALLOC_N(p->tokenbuf, char, 60);
7763tokspace(struct parser_params *p, int n)
7767 if (p->tokidx >= p->toksiz) {
7768 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7769 REALLOC_N(p->tokenbuf, char, p->toksiz);
7771 return &p->tokenbuf[p->tokidx-n];
7775tokadd(struct parser_params *p, int c)
7777 p->tokenbuf[p->tokidx++] = (char)c;
7778 if (p->tokidx >= p->toksiz) {
7780 REALLOC_N(p->tokenbuf, char, p->toksiz);
7785tok_hex(struct parser_params *p, size_t *numlen)
7789 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7791 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7792 yyerror0("invalid hex escape");
7793 dispatch_scan_event(p, tSTRING_CONTENT);
7796 p->lex.pcur += *numlen;
7800#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7803escaped_control_code(int c)
7829#define WARN_SPACE_CHAR(c, prefix) \
7830 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7833tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7834 int regexp_literal, const char *begin)
7836 const int wide = !begin;
7838 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7840 p->lex.pcur += numlen;
7841 if (p->lex.strterm == NULL ||
7842 strterm_is_heredoc(p->lex.strterm) ||
7843 (p->lex.strterm->u.literal.func != str_regexp)) {
7844 if (!begin) begin = p->lex.pcur;
7845 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7846 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7847 yyerror0("invalid Unicode escape");
7848 dispatch_scan_event(p, tSTRING_CONTENT);
7849 return wide && numlen > 0;
7851 if (codepoint > 0x10ffff) {
7852 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7853 yyerror0("invalid Unicode codepoint (too large)");
7854 dispatch_scan_event(p, tSTRING_CONTENT);
7857 if ((codepoint & 0xfffff800) == 0xd800) {
7858 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7859 yyerror0("invalid Unicode codepoint");
7860 dispatch_scan_event(p, tSTRING_CONTENT);
7864 if (regexp_literal) {
7865 tokcopy(p, (int)numlen);
7867 else if (codepoint >= 0x80) {
7868 rb_encoding *utf8 = rb_utf8_encoding();
7869 if (*encp && utf8 != *encp) {
7870 YYLTYPE loc = RUBY_INIT_YYLLOC();
7871 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7872 parser_show_error_line(p, &loc);
7876 tokaddmbc(p, codepoint, *encp);
7879 tokadd(p, codepoint);
7884static int tokadd_mbchar(struct parser_params *p, int c);
7887tokskip_mbchar(struct parser_params *p)
7889 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7891 p->lex.pcur += len - 1;
7896/* return value is for ?\u3042 */
7898tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7899 int term, int symbol_literal, int regexp_literal)
7902 * If `term` is not -1, then we allow multiple codepoints in \u{}
7903 * upto `term` byte, otherwise we're parsing a character literal.
7904 * And then add the codepoints to the current token.
7906 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7908 const int open_brace = '{', close_brace = '}';
7910 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7912 if (peek(p, open_brace)) { /* handle \u{...} form */
7913 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7915 * Skip parsing validation code and copy bytes as-is until term or
7916 * closing brace, in order to correctly handle extended regexps where
7917 * invalid unicode escapes are allowed in comments. The regexp parser
7918 * does its own validation and will catch any issues.
7920 tokadd(p, open_brace);
7921 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7923 if (c == close_brace) {
7928 else if (c == term) {
7931 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7935 tokadd_mbchar(p, c);
7939 const char *second = NULL;
7940 int c, last = nextc(p);
7941 if (lex_eol_p(p)) goto unterminated;
7942 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7943 while (c != close_brace) {
7944 if (c == term) goto unterminated;
7945 if (second == multiple_codepoints)
7946 second = p->lex.pcur;
7947 if (regexp_literal) tokadd(p, last);
7948 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7951 while (ISSPACE(c = peekc(p))) {
7952 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7955 if (term == -1 && !second)
7956 second = multiple_codepoints;
7959 if (c != close_brace) {
7961 flush_string_content(p, rb_utf8_encoding(), 0);
7962 yyerror0("unterminated Unicode escape");
7963 dispatch_scan_event(p, tSTRING_CONTENT);
7966 if (second && second != multiple_codepoints) {
7967 const char *pcur = p->lex.pcur;
7968 p->lex.pcur = second;
7969 dispatch_scan_event(p, tSTRING_CONTENT);
7972 yyerror0(multiple_codepoints);
7976 if (regexp_literal) tokadd(p, close_brace);
7980 else { /* handle \uxxxx form */
7981 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7988#define ESCAPE_CONTROL 1
7989#define ESCAPE_META 2
7992read_escape(struct parser_params *p, int flags, const char *begin)
7997 switch (c = nextc(p)) {
7998 case '\\': /* Backslash */
8001 case 'n': /* newline */
8004 case 't': /* horizontal tab */
8007 case 'r': /* carriage-return */
8010 case 'f': /* form-feed */
8013 case 'v': /* vertical tab */
8016 case 'a': /* alarm(bell) */
8019 case 'e': /* escape */
8022 case '0': case '1': case '2': case '3': /* octal constant */
8023 case '4': case '5': case '6': case '7':
8025 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8026 p->lex.pcur += numlen;
8029 case 'x': /* hex constant */
8030 c = tok_hex(p, &numlen);
8031 if (numlen == 0) return 0;
8034 case 'b': /* backspace */
8037 case 's': /* space */
8041 if (flags & ESCAPE_META) goto eof;
8042 if ((c = nextc(p)) != '-') {
8045 if ((c = nextc(p)) == '\\') {
8051 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8053 else if (c == -1) goto eof;
8054 else if (!ISASCII(c)) {
8059 int c2 = escaped_control_code(c);
8061 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8062 WARN_SPACE_CHAR(c2, "\\M-");
8065 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8068 else if (ISCNTRL(c)) goto eof;
8069 return ((c & 0xff) | 0x80);
8073 if ((c = nextc(p)) != '-') {
8077 if (flags & ESCAPE_CONTROL) goto eof;
8078 if ((c = nextc(p))== '\\') {
8084 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8088 else if (c == -1) goto eof;
8089 else if (!ISASCII(c)) {
8094 int c2 = escaped_control_code(c);
8097 if (flags & ESCAPE_META) {
8098 WARN_SPACE_CHAR(c2, "\\M-");
8101 WARN_SPACE_CHAR(c2, "");
8105 if (flags & ESCAPE_META) {
8106 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8109 WARN_SPACE_CHAR(c2, "\\C-");
8113 else if (ISCNTRL(c)) goto eof;
8119 flush_string_content(p, p->enc, p->lex.pcur - begin);
8120 yyerror0("Invalid escape character syntax");
8121 dispatch_scan_event(p, tSTRING_CONTENT);
8134tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8136 int len = rb_enc_codelen(c, enc);
8137 rb_enc_mbcput(c, tokspace(p, len), enc);
8141tokadd_escape(struct parser_params *p)
8145 const char *begin = p->lex.pcur;
8147 switch (c = nextc(p)) {
8149 return 0; /* just ignore */
8151 case '0': case '1': case '2': case '3': /* octal constant */
8152 case '4': case '5': case '6': case '7':
8154 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8155 if (numlen == 0) goto eof;
8156 p->lex.pcur += numlen;
8157 tokcopy(p, (int)numlen + 1);
8161 case 'x': /* hex constant */
8163 tok_hex(p, &numlen);
8164 if (numlen == 0) return -1;
8165 tokcopy(p, (int)numlen + 2);
8171 flush_string_content(p, p->enc, p->lex.pcur - begin);
8172 yyerror0("Invalid escape character syntax");
8184char_to_option(int c)
8190 val = RE_ONIG_OPTION_IGNORECASE;
8193 val = RE_ONIG_OPTION_EXTEND;
8196 val = RE_ONIG_OPTION_MULTILINE;
8205#define ARG_ENCODING_FIXED 16
8206#define ARG_ENCODING_NONE 32
8207#define ENC_ASCII8BIT 1
8209#define ENC_Windows_31J 3
8213char_to_option_kcode(int c, int *option, int *kcode)
8219 *kcode = ENC_ASCII8BIT;
8220 return (*option = ARG_ENCODING_NONE);
8222 *kcode = ENC_EUC_JP;
8225 *kcode = ENC_Windows_31J;
8232 return (*option = char_to_option(c));
8234 *option = ARG_ENCODING_FIXED;
8239regx_options(struct parser_params *p)
8247 while (c = nextc(p), ISALPHA(c)) {
8249 options |= RE_OPTION_ONCE;
8251 else if (char_to_option_kcode(c, &opt, &kc)) {
8253 if (kc != ENC_ASCII8BIT) kcode = c;
8267 YYLTYPE loc = RUBY_INIT_YYLLOC();
8269 compile_error(p, "unknown regexp option%s - %*s",
8270 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8271 parser_show_error_line(p, &loc);
8273 return options | RE_OPTION_ENCODING(kcode);
8277tokadd_mbchar(struct parser_params *p, int c)
8279 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8280 if (len < 0) return -1;
8282 p->lex.pcur += --len;
8283 if (len > 0) tokcopy(p, len);
8288simple_re_meta(int c)
8291 case '$': case '*': case '+': case '.':
8292 case '?': case '^': case '|':
8293 case ')': case ']': case '}': case '>':
8301parser_update_heredoc_indent(struct parser_params *p, int c)
8303 if (p->heredoc_line_indent == -1) {
8304 if (c == '\n') p->heredoc_line_indent = 0;
8308 p->heredoc_line_indent++;
8311 else if (c == '\t') {
8312 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8313 p->heredoc_line_indent = w * TAB_WIDTH;
8316 else if (c != '\n') {
8317 if (p->heredoc_indent > p->heredoc_line_indent) {
8318 p->heredoc_indent = p->heredoc_line_indent;
8320 p->heredoc_line_indent = -1;
8323 /* Whitespace only line has no indentation */
8324 p->heredoc_line_indent = 0;
8331parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8333 YYLTYPE loc = RUBY_INIT_YYLLOC();
8334 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8335 compile_error(p, "%s mixed within %s source", n1, n2);
8336 parser_show_error_line(p, &loc);
8340parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8342 const char *pos = p->lex.pcur;
8344 parser_mixed_error(p, enc1, enc2);
8349nibble_char_upper(unsigned int c)
8352 return c + (c < 10 ? '0' : 'A' - 10);
8356tokadd_string(struct parser_params *p,
8357 int func, int term, int paren, long *nest,
8358 rb_encoding **encp, rb_encoding **enc)
8363 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8364 int top_of_line = FALSE;
8367#define mixed_error(enc1, enc2) \
8368 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8369#define mixed_escape(beg, enc1, enc2) \
8370 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8372 while ((c = nextc(p)) != -1) {
8373 if (p->heredoc_indent > 0) {
8374 parser_update_heredoc_indent(p, c);
8377 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8383 if (paren && c == paren) {
8386 else if (c == term) {
8387 if (!nest || !*nest) {
8393 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8394 unsigned char c2 = *p->lex.pcur;
8395 if (c2 == '$' || c2 == '@' || c2 == '{') {
8400 else if (c == '\\') {
8404 if (func & STR_FUNC_QWORDS) break;
8405 if (func & STR_FUNC_EXPAND) {
8406 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8417 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8421 if ((func & STR_FUNC_EXPAND) == 0) {
8425 tokadd_utf8(p, enc, term,
8426 func & STR_FUNC_SYMBOL,
8427 func & STR_FUNC_REGEXP);
8431 if (c == -1) return -1;
8433 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8436 if (func & STR_FUNC_REGEXP) {
8442 c = read_escape(p, 0, p->lex.pcur - 1);
8444 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8447 *t++ = nibble_char_upper(c >> 4);
8448 *t++ = nibble_char_upper(c);
8453 if (c == term && !simple_re_meta(c)) {
8458 if ((c = tokadd_escape(p)) < 0)
8460 if (*enc && *enc != *encp) {
8461 mixed_escape(p->lex.ptok+2, *enc, *encp);
8465 else if (func & STR_FUNC_EXPAND) {
8467 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8468 c = read_escape(p, 0, p->lex.pcur - 1);
8470 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8471 /* ignore backslashed spaces in %w */
8473 else if (c != term && !(paren && c == paren)) {
8480 else if (!parser_isascii(p)) {
8485 else if (*enc != *encp) {
8486 mixed_error(*enc, *encp);
8489 if (tokadd_mbchar(p, c) == -1) return -1;
8492 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8500 else if (*enc != *encp) {
8501 mixed_error(*enc, *encp);
8507 top_of_line = (c == '\n');
8511 if (*enc) *encp = *enc;
8515#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8518flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8520 p->lex.pcur -= back;
8521 if (has_delayed_token(p)) {
8522 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8524 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8525 p->delayed.end_line = p->ruby_sourceline;
8526 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8528 dispatch_delayed_token(p, tSTRING_CONTENT);
8529 p->lex.ptok = p->lex.pcur;
8531 dispatch_scan_event(p, tSTRING_CONTENT);
8532 p->lex.pcur += back;
8535/* this can be shared with ripper, since it's independent from struct
8538#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8539#define SPECIAL_PUNCT(idx) ( \
8540 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8541 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8542 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8543 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8544 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8546const uint_least32_t ruby_global_name_punct_bits[] = {
8555static enum yytokentype
8556parser_peek_variable_name(struct parser_params *p)
8559 const char *ptr = p->lex.pcur;
8561 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8565 if ((c = *ptr) == '-') {
8566 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8569 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8570 return tSTRING_DVAR;
8574 if ((c = *ptr) == '@') {
8575 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8581 p->command_start = TRUE;
8582 yylval.state = p->lex.state;
8583 return tSTRING_DBEG;
8587 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8588 return tSTRING_DVAR;
8592#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8593#define IS_END() IS_lex_state(EXPR_END_ANY)
8594#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8595#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8596#define IS_LABEL_POSSIBLE() (\
8597 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8599#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8600#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8602static inline enum yytokentype
8603parser_string_term(struct parser_params *p, int func)
8605 xfree(p->lex.strterm);
8607 if (func & STR_FUNC_REGEXP) {
8608 set_yylval_num(regx_options(p));
8609 dispatch_scan_event(p, tREGEXP_END);
8610 SET_LEX_STATE(EXPR_END);
8613 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8615 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8618 SET_LEX_STATE(EXPR_END);
8622static enum yytokentype
8623parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8625 int func = quote->func;
8626 int term = quote->term;
8627 int paren = quote->paren;
8629 rb_encoding *enc = p->enc;
8630 rb_encoding *base_enc = 0;
8631 rb_parser_string_t *lit;
8633 if (func & STR_FUNC_TERM) {
8634 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8635 SET_LEX_STATE(EXPR_END);
8636 xfree(p->lex.strterm);
8638 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8641 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8642 while (c != '\n' && ISSPACE(c = nextc(p)));
8645 if (func & STR_FUNC_LIST) {
8646 quote->func &= ~STR_FUNC_LIST;
8649 if (c == term && !quote->nest) {
8650 if (func & STR_FUNC_QWORDS) {
8651 quote->func |= STR_FUNC_TERM;
8652 pushback(p, c); /* dispatch the term at tSTRING_END */
8653 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8656 return parser_string_term(p, func);
8659 if (!ISSPACE(c)) pushback(p, c);
8660 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8664 if ((func & STR_FUNC_EXPAND) && c == '#') {
8665 enum yytokentype t = parser_peek_variable_name(p);
8671 if (tokadd_string(p, func, term, paren, "e->nest,
8672 &enc, &base_enc) == -1) {
8675# define unterminated_literal(mesg) yyerror0(mesg)
8677# define unterminated_literal(mesg) compile_error(p, mesg)
8679 literal_flush(p, p->lex.pcur);
8680 if (func & STR_FUNC_QWORDS) {
8681 /* no content to add, bailing out here */
8682 unterminated_literal("unterminated list meets end of file");
8683 xfree(p->lex.strterm);
8687 if (func & STR_FUNC_REGEXP) {
8688 unterminated_literal("unterminated regexp meets end of file");
8691 unterminated_literal("unterminated string meets end of file");
8693 quote->func |= STR_FUNC_TERM;
8698 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8699 set_yylval_str(lit);
8700 flush_string_content(p, enc, 0);
8702 return tSTRING_CONTENT;
8705static enum yytokentype
8706heredoc_identifier(struct parser_params *p)
8709 * term_len is length of `<<"END"` except `END`,
8710 * in this case term_len is 4 (<, <, " and ").
8712 long len, offset = p->lex.pcur - p->lex.pbeg;
8713 int c = nextc(p), term, func = 0, quote = 0;
8714 enum yytokentype token = tSTRING_BEG;
8719 func = STR_FUNC_INDENT;
8722 else if (c == '~') {
8724 func = STR_FUNC_INDENT;
8730 func |= str_squote; goto quoted;
8732 func |= str_dquote; goto quoted;
8734 token = tXSTRING_BEG;
8735 func |= str_xquote; goto quoted;
8742 while ((c = nextc(p)) != term) {
8743 if (c == -1 || c == '\r' || c == '\n') {
8744 yyerror0("unterminated here document identifier");
8751 if (!parser_is_identchar(p)) {
8753 if (func & STR_FUNC_INDENT) {
8754 pushback(p, indent > 0 ? '~' : '-');
8760 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8761 if (n < 0) return 0;
8763 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8768 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8769 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8770 yyerror0("too long here document identifier");
8771 dispatch_scan_event(p, tHEREDOC_BEG);
8774 p->lex.strterm = new_heredoc(p);
8775 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8776 here->offset = offset;
8777 here->sourceline = p->ruby_sourceline;
8778 here->length = (unsigned)len;
8779 here->quote = quote;
8781 here->lastline = p->lex.lastline;
8784 p->heredoc_indent = indent;
8785 p->heredoc_line_indent = 0;
8790heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8792 rb_parser_string_t *line;
8793 rb_strterm_t *term = p->lex.strterm;
8796 line = here->lastline;
8797 p->lex.lastline = line;
8798 p->lex.pbeg = PARSER_STRING_PTR(line);
8799 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8800 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8801 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8802 p->heredoc_end = p->ruby_sourceline;
8803 p->ruby_sourceline = (int)here->sourceline;
8804 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8810dedent_string_column(const char *str, long len, int width)
8814 for (i = 0; i < len && col < width; i++) {
8815 if (str[i] == ' ') {
8818 else if (str[i] == '\t') {
8819 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8820 if (n > width) break;
8832dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8838 len = PARSER_STRING_LEN(string);
8839 str = PARSER_STRING_PTR(string);
8841 i = dedent_string_column(str, len, width);
8844 rb_parser_str_modify(string);
8845 str = PARSER_STRING_PTR(string);
8846 if (PARSER_STRING_LEN(string) != len)
8847 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8848 MEMMOVE(str, str + i, char, len - i);
8849 rb_parser_str_set_len(p, string, len - i);
8854heredoc_dedent(struct parser_params *p, NODE *root)
8856 NODE *node, *str_node, *prev_node;
8857 int indent = p->heredoc_indent;
8858 rb_parser_string_t *prev_lit = 0;
8860 if (indent <= 0) return root;
8861 if (!root) return root;
8863 prev_node = node = str_node = root;
8864 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8867 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8868 if (nd_fl_newline(str_node)) {
8869 dedent_string(p, lit, indent);
8874 else if (!literal_concat0(p, prev_lit, lit)) {
8878 NODE *end = RNODE_LIST(node)->as.nd_end;
8879 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8881 if (nd_type_p(prev_node, NODE_DSTR))
8882 nd_set_type(prev_node, NODE_STR);
8885 RNODE_LIST(node)->as.nd_end = end;
8890 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8892 if (!nd_type_p(node, NODE_LIST)) break;
8893 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8894 enum node_type type = nd_type(str_node);
8895 if (type == NODE_STR || type == NODE_DSTR) break;
8905whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8907 const char *beg = p->lex.pbeg;
8908 const char *ptr = p->lex.pend;
8910 if (ptr - beg < len) return FALSE;
8911 if (ptr > beg && ptr[-1] == '\n') {
8912 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8913 if (ptr - beg < len) return FALSE;
8915 if (strncmp(eos, ptr -= len, len)) return FALSE;
8917 while (beg < ptr && ISSPACE(*beg)) beg++;
8923word_match_p(struct parser_params *p, const char *word, long len)
8925 if (strncmp(p->lex.pcur, word, len)) return 0;
8926 if (lex_eol_n_p(p, len)) return 1;
8927 int c = (unsigned char)p->lex.pcur[len];
8928 if (ISSPACE(c)) return 1;
8930 case '\0': case '\004': case '\032': return 1;
8935#define NUM_SUFFIX_R (1<<0)
8936#define NUM_SUFFIX_I (1<<1)
8937#define NUM_SUFFIX_ALL 3
8940number_literal_suffix(struct parser_params *p, int mask)
8943 const char *lastp = p->lex.pcur;
8945 while ((c = nextc(p)) != -1) {
8946 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8947 result |= (mask & NUM_SUFFIX_I);
8948 mask &= ~NUM_SUFFIX_I;
8949 /* r after i, rational of complex is disallowed */
8950 mask &= ~NUM_SUFFIX_R;
8953 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8954 result |= (mask & NUM_SUFFIX_R);
8955 mask &= ~NUM_SUFFIX_R;
8958 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8959 p->lex.pcur = lastp;
8968static enum yytokentype
8969set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8971 enum rb_numeric_type numeric_type = integer_literal;
8973 if (type == tFLOAT) {
8974 numeric_type = float_literal;
8977 if (suffix & NUM_SUFFIX_R) {
8979 numeric_type = rational_literal;
8981 if (suffix & NUM_SUFFIX_I) {
8987 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8990 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8993 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8996 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8997 (void)numeric_type; /* for ripper */
9000 rb_bug("unexpected token: %d", type);
9002 SET_LEX_STATE(EXPR_END);
9006#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9008parser_dispatch_heredoc_end(struct parser_params *p, int line)
9010 if (has_delayed_token(p))
9011 dispatch_delayed_token(p, tSTRING_CONTENT);
9014 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9015 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9017 if (p->keep_tokens) {
9018 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9019 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9020 parser_append_tokens(p, str, tHEREDOC_END, line);
9024 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9029static enum yytokentype
9030here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9032 int c, func, indent = 0;
9033 const char *eos, *ptr, *ptr_end;
9035 rb_parser_string_t *str = 0;
9036 rb_encoding *enc = p->enc;
9037 rb_encoding *base_enc = 0;
9043 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9045 indent = (func = here->func) & STR_FUNC_INDENT;
9047 if ((c = nextc(p)) == -1) {
9050 if (!has_delayed_token(p)) {
9051 dispatch_scan_event(p, tSTRING_CONTENT);
9053 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9054 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9055 if (!(func & STR_FUNC_REGEXP)) {
9056 int cr = ENC_CODERANGE_UNKNOWN;
9057 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9058 if (cr != ENC_CODERANGE_7BIT &&
9059 rb_is_usascii_enc(p->enc) &&
9060 enc != rb_utf8_encoding()) {
9061 enc = rb_ascii8bit_encoding();
9064 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9066 dispatch_delayed_token(p, tSTRING_CONTENT);
9069 dispatch_delayed_token(p, tSTRING_CONTENT);
9070 dispatch_scan_event(p, tSTRING_CONTENT);
9074 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9075 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9078 SET_LEX_STATE(EXPR_END);
9083 /* not beginning of line, cannot be the terminator */
9085 else if (p->heredoc_line_indent == -1) {
9086 /* `heredoc_line_indent == -1` means
9087 * - "after an interpolation in the same line", or
9088 * - "in a continuing line"
9090 p->heredoc_line_indent = 0;
9092 else if (whole_match_p(p, eos, len, indent)) {
9093 dispatch_heredoc_end(p);
9095 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9097 SET_LEX_STATE(EXPR_END);
9101 if (!(func & STR_FUNC_EXPAND)) {
9103 ptr = PARSER_STRING_PTR(p->lex.lastline);
9104 ptr_end = p->lex.pend;
9105 if (ptr_end > ptr) {
9106 switch (ptr_end[-1]) {
9108 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9117 if (p->heredoc_indent > 0) {
9119 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9121 p->heredoc_line_indent = 0;
9125 parser_str_cat(str, ptr, ptr_end - ptr);
9127 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9128 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9130 if (p->heredoc_indent > 0) {
9133 if (nextc(p) == -1) {
9135 rb_parser_string_free(p, str);
9140 } while (!whole_match_p(p, eos, len, indent));
9143 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9146 enum yytokentype t = parser_peek_variable_name(p);
9147 if (p->heredoc_line_indent != -1) {
9148 if (p->heredoc_indent > p->heredoc_line_indent) {
9149 p->heredoc_indent = p->heredoc_line_indent;
9151 p->heredoc_line_indent = -1;
9160 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9161 if (p->eofp) goto error;
9165 if (c == '\\') p->heredoc_line_indent = -1;
9167 str = STR_NEW3(tok(p), toklen(p), enc, func);
9169 set_yylval_str(str);
9171 if (bol) nd_set_fl_newline(yylval.node);
9173 flush_string_content(p, enc, 0);
9174 return tSTRING_CONTENT;
9176 tokadd(p, nextc(p));
9177 if (p->heredoc_indent > 0) {
9181 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9182 if ((c = nextc(p)) == -1) goto error;
9183 } while (!whole_match_p(p, eos, len, indent));
9184 str = STR_NEW3(tok(p), toklen(p), enc, func);
9186 dispatch_heredoc_end(p);
9187 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9189 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9191 /* Preserve s_value for set_yylval_str */
9192 s_value = p->s_value;
9194 set_yylval_str(str);
9196 set_parser_s_value(s_value);
9200 if (bol) nd_set_fl_newline(yylval.node);
9202 return tSTRING_CONTENT;
9208arg_ambiguous(struct parser_params *p, char c)
9212 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9215 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9218 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9223/* returns true value if formal argument error;
9224 * Qtrue, or error message if ripper */
9226formal_argument_error(struct parser_params *p, ID id)
9228 switch (id_type(id)) {
9232# define ERR(mesg) (yyerror0(mesg), Qtrue)
9234# define ERR(mesg) WARN_S(mesg)
9237 return ERR("formal argument cannot be a constant");
9239 return ERR("formal argument cannot be an instance variable");
9241 return ERR("formal argument cannot be a global variable");
9243 return ERR("formal argument cannot be a class variable");
9245 return ERR("formal argument must be local variable");
9248 shadowing_lvar(p, id);
9254lvar_defined(struct parser_params *p, ID id)
9256 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9259/* emacsen -*- hack */
9261parser_encode_length(struct parser_params *p, const char *name, long len)
9265 if (len > 5 && name[nlen = len - 5] == '-') {
9266 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9269 if (len > 4 && name[nlen = len - 4] == '-') {
9270 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9272 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9273 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9274 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9281parser_set_encode(struct parser_params *p, const char *name)
9287 const char *wrong = 0;
9289 case 'e': case 'E': wrong = "external"; break;
9290 case 'i': case 'I': wrong = "internal"; break;
9291 case 'f': case 'F': wrong = "filesystem"; break;
9292 case 'l': case 'L': wrong = "locale"; break;
9294 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9295 idx = rb_enc_find_index(name);
9298 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9300 excargs[0] = rb_eArgError;
9301 excargs[2] = rb_make_backtrace();
9302 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9303 VALUE exc = rb_make_exception(3, excargs);
9304 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9306 rb_ast_free(p->ast);
9311 enc = rb_enc_from_index(idx);
9312 if (!rb_enc_asciicompat(enc)) {
9313 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9318 if (p->debug_lines) {
9320 for (i = 0; i < p->debug_lines->len; i++) {
9321 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9328comment_at_top(struct parser_params *p)
9330 if (p->token_seen) return false;
9331 return (p->line_count == (p->has_shebang ? 2 : 1));
9334typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9335typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9337static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9340magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9342 if (!comment_at_top(p)) {
9345 parser_set_encode(p, val);
9349parser_get_bool(struct parser_params *p, const char *name, const char *val)
9353 if (STRCASECMP(val, "true") == 0) {
9358 if (STRCASECMP(val, "false") == 0) {
9363 return parser_invalid_pragma_value(p, name, val);
9367parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9369 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9374parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9376 int b = parser_get_bool(p, name, val);
9377 if (b >= 0) p->token_info_enabled = b;
9381parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9385 if (p->token_seen) {
9386 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9390 b = parser_get_bool(p, name, val);
9393 p->frozen_string_literal = b;
9397parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9399 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9400 if (*s == ' ' || *s == '\t') continue;
9401 if (*s == '#') break;
9402 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9408 if (STRCASECMP(val, "none") == 0) {
9409 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9414 if (STRCASECMP(val, "literal") == 0) {
9415 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9420 if (STRCASECMP(val, "experimental_copy") == 0) {
9421 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9424 if (STRCASECMP(val, "experimental_everything") == 0) {
9425 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9430 parser_invalid_pragma_value(p, name, val);
9435parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9437 int b = parser_get_bool(p, name, val);
9438 if (b >= 0) p->past_scope_enabled = b;
9442struct magic_comment {
9444 rb_magic_comment_setter_t func;
9445 rb_magic_comment_length_t length;
9448static const struct magic_comment magic_comments[] = {
9449 {"coding", magic_comment_encoding, parser_encode_length},
9450 {"encoding", magic_comment_encoding, parser_encode_length},
9451 {"frozen_string_literal", parser_set_frozen_string_literal},
9452 {"shareable_constant_value", parser_set_shareable_constant_value},
9453 {"warn_indent", parser_set_token_info},
9455 {"warn_past_scope", parser_set_past_scope},
9460magic_comment_marker(const char *str, long len)
9467 if (str[i-1] == '*' && str[i-2] == '-') {
9473 if (i + 1 >= len) return 0;
9474 if (str[i+1] != '-') {
9477 else if (str[i-1] != '-') {
9493parser_magic_comment(struct parser_params *p, const char *str, long len)
9496 VALUE name = 0, val = 0;
9497 const char *beg, *end, *vbeg, *vend;
9498#define str_copy(_s, _p, _n) ((_s) \
9499 ? (void)(rb_str_resize((_s), (_n)), \
9500 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9501 : (void)((_s) = STR_NEW((_p), (_n))))
9503 if (len <= 7) return FALSE;
9504 if (!!(beg = magic_comment_marker(str, len))) {
9505 if (!(end = magic_comment_marker(beg, str + len - beg)))
9509 len = end - beg - 3;
9512 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9514 const struct magic_comment *mc = magic_comments;
9519 for (; len > 0 && *str; str++, --len) {
9521 case '\'': case '"': case ':': case ';':
9524 if (!ISSPACE(*str)) break;
9526 for (beg = str; len > 0; str++, --len) {
9528 case '\'': case '"': case ':': case ';':
9531 if (ISSPACE(*str)) break;
9536 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9539 if (!indicator) return FALSE;
9543 do str++; while (--len > 0 && ISSPACE(*str));
9545 const char *tok_beg = str;
9547 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9560 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9563 const char *tok_end = str;
9565 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9568 while (len > 0 && (ISSPACE(*str))) --len, str++;
9569 if (len) return FALSE;
9573 str_copy(name, beg, n);
9574 s = RSTRING_PTR(name);
9575 for (i = 0; i < n; ++i) {
9576 if (s[i] == '-') s[i] = '_';
9579 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9582 n = (*mc->length)(p, vbeg, n);
9584 str_copy(val, vbeg, n);
9585 p->lex.ptok = tok_beg;
9586 p->lex.pcur = tok_end;
9587 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9590 } while (++mc < magic_comments + numberof(magic_comments));
9592 str_copy(val, vbeg, vend - vbeg);
9593 dispatch2(magic_comment, name, val);
9601set_file_encoding(struct parser_params *p, const char *str, const char *send)
9604 const char *beg = str;
9608 if (send - str <= 6) return;
9610 case 'C': case 'c': str += 6; continue;
9611 case 'O': case 'o': str += 5; continue;
9612 case 'D': case 'd': str += 4; continue;
9613 case 'I': case 'i': str += 3; continue;
9614 case 'N': case 'n': str += 2; continue;
9615 case 'G': case 'g': str += 1; continue;
9622 if (ISSPACE(*str)) break;
9625 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9630 if (++str >= send) return;
9631 } while (ISSPACE(*str));
9633 if (*str != '=' && *str != ':') return;
9638 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9639 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9642 parser_set_encode(p, RSTRING_PTR(s));
9643 rb_str_resize(s, 0);
9647parser_prepare(struct parser_params *p)
9649 int c = nextc0(p, FALSE);
9650 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9653 if (peek(p, '!')) p->has_shebang = 1;
9655 case 0xef: /* UTF-8 BOM marker */
9656 if (!lex_eol_n_p(p, 2) &&
9657 (unsigned char)p->lex.pcur[0] == 0xbb &&
9658 (unsigned char)p->lex.pcur[1] == 0xbf) {
9659 p->enc = rb_utf8_encoding();
9662 if (p->debug_lines) {
9663 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9666 p->lex.pbeg = p->lex.pcur;
9671 case -1: /* end of script. */
9675 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9679#define ambiguous_operator(tok, op, syn) ( \
9680 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9681 rb_warning0("even though it seems like "syn""))
9683#define ambiguous_operator(tok, op, syn) \
9684 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9686#define warn_balanced(tok, op, syn) ((void) \
9687 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9688 space_seen && !ISSPACE(c) && \
9689 (ambiguous_operator(tok, op, syn), 0)), \
9690 (enum yytokentype)(tok))
9692static enum yytokentype
9693no_digits(struct parser_params *p)
9695 yyerror0("numeric literal without digits");
9696 if (peek(p, '_')) nextc(p);
9697 /* dummy 0, for tUMINUS_NUM at numeric */
9698 return set_number_literal(p, tINTEGER, 0, 10, 0);
9701static enum yytokentype
9702parse_numeric(struct parser_params *p, int c)
9704 int is_float, seen_point, seen_e, nondigit;
9707 is_float = seen_point = seen_e = nondigit = 0;
9708 SET_LEX_STATE(EXPR_END);
9710 if (c == '-' || c == '+') {
9715 int start = toklen(p);
9717 if (c == 'x' || c == 'X') {
9720 if (c != -1 && ISXDIGIT(c)) {
9723 if (nondigit) break;
9727 if (!ISXDIGIT(c)) break;
9730 } while ((c = nextc(p)) != -1);
9734 if (toklen(p) == start) {
9735 return no_digits(p);
9737 else if (nondigit) goto trailing_uc;
9738 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9739 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9741 if (c == 'b' || c == 'B') {
9744 if (c == '0' || c == '1') {
9747 if (nondigit) break;
9751 if (c != '0' && c != '1') break;
9754 } while ((c = nextc(p)) != -1);
9758 if (toklen(p) == start) {
9759 return no_digits(p);
9761 else if (nondigit) goto trailing_uc;
9762 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9763 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9765 if (c == 'd' || c == 'D') {
9768 if (c != -1 && ISDIGIT(c)) {
9771 if (nondigit) break;
9775 if (!ISDIGIT(c)) break;
9778 } while ((c = nextc(p)) != -1);
9782 if (toklen(p) == start) {
9783 return no_digits(p);
9785 else if (nondigit) goto trailing_uc;
9786 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9787 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9793 if (c == 'o' || c == 'O') {
9794 /* prefixed octal */
9796 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9798 return no_digits(p);
9801 if (c >= '0' && c <= '7') {
9806 if (nondigit) break;
9810 if (c < '0' || c > '9') break;
9811 if (c > '7') goto invalid_octal;
9814 } while ((c = nextc(p)) != -1);
9815 if (toklen(p) > start) {
9818 if (nondigit) goto trailing_uc;
9819 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9820 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9827 if (c > '7' && c <= '9') {
9829 yyerror0("Invalid octal digit");
9831 else if (c == '.' || c == 'e' || c == 'E') {
9837 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9838 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9844 case '0': case '1': case '2': case '3': case '4':
9845 case '5': case '6': case '7': case '8': case '9':
9851 if (nondigit) goto trailing_uc;
9852 if (seen_point || seen_e) {
9857 if (c0 == -1 || !ISDIGIT(c0)) {
9863 seen_point = toklen(p);
9882 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9888 tokadd(p, nondigit);
9892 nondigit = (c == '-' || c == '+') ? c : 0;
9895 case '_': /* `_' in number just ignored */
9896 if (nondigit) goto decode_num;
9910 literal_flush(p, p->lex.pcur - 1);
9911 YYLTYPE loc = RUBY_INIT_YYLLOC();
9912 compile_error(p, "trailing '%c' in number", nondigit);
9913 parser_show_error_line(p, &loc);
9917 enum yytokentype type = tFLOAT;
9919 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9920 if (suffix & NUM_SUFFIX_R) {
9925 if (errno == ERANGE) {
9926 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9930 return set_number_literal(p, type, suffix, 0, seen_point);
9932 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9933 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9936static enum yytokentype
9937parse_qmark(struct parser_params *p, int space_seen)
9941 rb_parser_string_t *lit;
9942 const char *start = p->lex.pcur;
9945 SET_LEX_STATE(EXPR_VALUE);
9950 compile_error(p, "incomplete character syntax");
9953 if (rb_enc_isspace(c, p->enc)) {
9955 int c2 = escaped_control_code(c);
9957 WARN_SPACE_CHAR(c2, "?");
9962 SET_LEX_STATE(EXPR_VALUE);
9967 int w = parser_precise_mbclen(p, start);
9968 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9969 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9971 const char *ptr = start;
9973 int n = parser_precise_mbclen(p, ptr);
9974 if (n < 0) return -1;
9976 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9977 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9978 " a conditional operator, put a space after '?'",
9979 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9983 else if (c == '\\') {
9986 enc = rb_utf8_encoding();
9987 tokadd_utf8(p, &enc, -1, 0, 0);
9989 else if (!ISASCII(c = peekc(p)) && c != -1) {
9991 if (tokadd_mbchar(p, c) == -1) return 0;
9994 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
9999 if (tokadd_mbchar(p, c) == -1) return 0;
10002 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10003 set_yylval_str(lit);
10004 SET_LEX_STATE(EXPR_END);
10008static enum yytokentype
10009parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10012 const char *ptok = p->lex.pcur;
10020 if (c == -1) goto unterminated;
10023 if (!ISASCII(c)) goto unknown;
10028 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10031 c = parser_precise_mbclen(p, p->lex.pcur);
10032 if (c < 0) return 0;
10034 yyerror0("unknown type of %string");
10040 compile_error(p, "unterminated quoted string meets end of file");
10044 if (term == '(') term = ')';
10045 else if (term == '[') term = ']';
10046 else if (term == '{') term = '}';
10047 else if (term == '<') term = '>';
10050 p->lex.ptok = ptok-1;
10053 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10054 return tSTRING_BEG;
10057 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10058 return tSTRING_BEG;
10061 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10065 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10066 return tQWORDS_BEG;
10069 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10070 return tSYMBOLS_BEG;
10073 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10074 return tQSYMBOLS_BEG;
10077 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10078 return tXSTRING_BEG;
10081 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10082 return tREGEXP_BEG;
10085 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10086 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10090 yyerror0("unknown type of %string");
10094 if ((c = nextc(p)) == '=') {
10095 set_yylval_id('%');
10096 SET_LEX_STATE(EXPR_BEG);
10099 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10102 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10104 return warn_balanced('%', "%%", "string literal");
10108tokadd_ident(struct parser_params *p, int c)
10111 if (tokadd_mbchar(p, c) == -1) return -1;
10113 } while (parser_is_identchar(p));
10119tokenize_ident(struct parser_params *p)
10121 ID ident = TOK_INTERN();
10123 set_yylval_name(ident);
10129parse_numvar(struct parser_params *p)
10133 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10134 const unsigned long nth_ref_max =
10135 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10136 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10137 * turned into a Fixnum, in compile.c */
10139 if (overflow || n > nth_ref_max) {
10140 /* compile_error()? */
10141 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10142 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10149static enum yytokentype
10150parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10152 const char *ptr = p->lex.pcur;
10155 SET_LEX_STATE(EXPR_END);
10156 p->lex.ptok = ptr - 1; /* from '$' */
10160 case '_': /* $_: last read line string */
10162 if (parser_is_identchar(p)) {
10170 case '~': /* $~: match-data */
10171 case '*': /* $*: argv */
10172 case '$': /* $$: pid */
10173 case '?': /* $?: last status */
10174 case '!': /* $!: error string */
10175 case '@': /* $@: error position */
10176 case '/': /* $/: input record separator */
10177 case '\\': /* $\: output record separator */
10178 case ';': /* $;: field separator */
10179 case ',': /* $,: output field separator */
10180 case '.': /* $.: last read line number */
10181 case '=': /* $=: ignorecase */
10182 case ':': /* $:: load path */
10183 case '<': /* $<: default input handle */
10184 case '>': /* $>: default output handle */
10185 case '\"': /* $": already loaded files */
10194 if (parser_is_identchar(p)) {
10195 if (tokadd_mbchar(p, c) == -1) return 0;
10206 case '&': /* $&: last match */
10207 case '`': /* $`: string before last match */
10208 case '\'': /* $': string after last match */
10209 case '+': /* $+: string matches last paren. */
10210 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10215 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10218 case '1': case '2': case '3':
10219 case '4': case '5': case '6':
10220 case '7': case '8': case '9':
10225 } while (c != -1 && ISDIGIT(c));
10227 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10229 c = parse_numvar(p);
10230 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10234 if (!parser_is_identchar(p)) {
10235 YYLTYPE loc = RUBY_INIT_YYLLOC();
10236 if (c == -1 || ISSPACE(c)) {
10237 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10241 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10243 parser_show_error_line(p, &loc);
10244 set_yylval_noname();
10252 if (tokadd_ident(p, c)) return 0;
10253 SET_LEX_STATE(EXPR_END);
10254 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10258 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10259 set_yylval_noname();
10265parser_numbered_param(struct parser_params *p, int n)
10267 if (n < 0) return false;
10269 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10272 if (p->max_numparam == ORDINAL_PARAM) {
10273 compile_error(p, "ordinary parameter is defined");
10276 struct vtable *args = p->lvtbl->args;
10277 if (p->max_numparam < n) {
10278 p->max_numparam = n;
10280 while (n > args->pos) {
10281 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10286static enum yytokentype
10287parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10289 const char *ptr = p->lex.pcur;
10290 enum yytokentype result = tIVAR;
10291 register int c = nextc(p);
10294 p->lex.ptok = ptr - 1; /* from '@' */
10302 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10303 if (c == -1 || !parser_is_identchar(p)) {
10305 RUBY_SET_YYLLOC(loc);
10306 if (result == tIVAR) {
10307 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10310 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10312 parser_show_error_line(p, &loc);
10313 set_yylval_noname();
10314 SET_LEX_STATE(EXPR_END);
10317 else if (ISDIGIT(c)) {
10319 RUBY_SET_YYLLOC(loc);
10320 if (result == tIVAR) {
10321 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10324 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10326 parser_show_error_line(p, &loc);
10327 set_yylval_noname();
10328 SET_LEX_STATE(EXPR_END);
10332 if (tokadd_ident(p, c)) return 0;
10337static enum yytokentype
10338parse_ident(struct parser_params *p, int c, int cmd_state)
10340 enum yytokentype result;
10341 bool is_ascii = true;
10342 const enum lex_state_e last_state = p->lex.state;
10344 int enforce_keyword_end = 0;
10347 if (!ISASCII(c)) is_ascii = false;
10348 if (tokadd_mbchar(p, c) == -1) return 0;
10350 } while (parser_is_identchar(p));
10351 if ((c == '!' || c == '?') && !peek(p, '=')) {
10355 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10356 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10357 result = tIDENTIFIER;
10361 result = tCONSTANT; /* assume provisionally */
10366 if (IS_LABEL_POSSIBLE()) {
10367 if (IS_LABEL_SUFFIX(0)) {
10368 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10376 if (peek_end_expect_token_locations(p)) {
10377 const rb_code_position_t *end_pos;
10378 int lineno, column;
10379 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10381 end_pos = peek_end_expect_token_locations(p)->pos;
10382 lineno = end_pos->lineno;
10383 column = end_pos->column;
10386 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10387 p->ruby_sourceline, beg_pos, lineno, column);
10390 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10391 const struct kwtable *kw;
10393 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10394 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10395 enforce_keyword_end = 1;
10401 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10402 const struct kwtable *kw;
10404 /* See if it is a reserved word. */
10405 kw = rb_reserved_word(tok(p), toklen(p));
10407 enum lex_state_e state = p->lex.state;
10408 if (IS_lex_state_for(state, EXPR_FNAME)) {
10409 SET_LEX_STATE(EXPR_ENDFN);
10410 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10413 SET_LEX_STATE(kw->state);
10414 if (IS_lex_state(EXPR_BEG)) {
10415 p->command_start = TRUE;
10417 if (kw->id[0] == keyword_do) {
10418 if (lambda_beginning_p()) {
10419 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10420 return keyword_do_LAMBDA;
10422 if (COND_P()) return keyword_do_cond;
10423 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10424 return keyword_do_block;
10427 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10430 if (kw->id[0] != kw->id[1])
10431 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10437 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10439 SET_LEX_STATE(EXPR_CMDARG);
10442 SET_LEX_STATE(EXPR_ARG);
10445 else if (p->lex.state == EXPR_FNAME) {
10446 SET_LEX_STATE(EXPR_ENDFN);
10449 SET_LEX_STATE(EXPR_END);
10452 ident = tokenize_ident(p);
10453 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10454 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10455 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10456 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10457 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10463warn_cr(struct parser_params *p)
10467 /* carried over with p->lex.nextline for nextc() */
10468 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10472static enum yytokentype
10473parser_yylex(struct parser_params *p)
10476 int space_seen = 0;
10479 enum lex_state_e last_state;
10480 int fallthru = FALSE;
10481 int token_seen = p->token_seen;
10483 if (p->lex.strterm) {
10484 if (strterm_is_heredoc(p->lex.strterm)) {
10486 return here_document(p, &p->lex.strterm->u.heredoc);
10490 return parse_string(p, &p->lex.strterm->u.literal);
10493 cmd_state = p->command_start;
10494 p->command_start = FALSE;
10495 p->token_seen = TRUE;
10500 last_state = p->lex.state;
10501 switch (c = nextc(p)) {
10502 case '\0': /* NUL */
10503 case '\004': /* ^D */
10504 case '\032': /* ^Z */
10505 case -1: /* end of script. */
10508 if (p->end_expect_token_locations) {
10509 pop_end_expect_token_locations(p);
10510 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10514 /* Set location for end-of-input because dispatch_scan_event is not called. */
10515 RUBY_SET_YYLLOC(*p->yylloc);
10516 return END_OF_INPUT;
10522 case ' ': case '\t': case '\f':
10523 case '\13': /* '\v' */
10525 while ((c = nextc(p))) {
10530 case ' ': case '\t': case '\f':
10531 case '\13': /* '\v' */
10539 dispatch_scan_event(p, tSP);
10545 case '#': /* it's a comment */
10546 p->token_seen = token_seen;
10547 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10548 /* no magic_comment in shebang line */
10549 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10550 if (comment_at_top(p)) {
10551 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10554 p->lex.pcur = pcur, p->lex.ptok = ptok;
10556 dispatch_scan_event(p, tCOMMENT);
10560 p->token_seen = token_seen;
10561 rb_parser_string_t *prevline = p->lex.lastline;
10562 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10563 !IS_lex_state(EXPR_LABELED));
10564 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10566 dispatch_scan_event(p, tIGNORED_NL);
10569 if (!c && p->ctxt.in_kwarg) {
10570 goto normal_newline;
10575 switch (c = nextc(p)) {
10576 case ' ': case '\t': case '\f': case '\r':
10577 case '\13': /* '\v' */
10583 dispatch_scan_event(p, tSP);
10588 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10591 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10594 if (peek(p, '|')) goto leading_logical;
10597 if (peek(p, '&')) {
10600 dispatch_delayed_token(p, tIGNORED_NL);
10606 dispatch_delayed_token(p, tIGNORED_NL);
10607 if (peek(p, '.') == (c == '&')) {
10609 dispatch_scan_event(p, tSP);
10615 p->ruby_sourceline--;
10616 p->lex.nextline = p->lex.lastline;
10617 set_lastline(p, prevline);
10618 case -1: /* EOF no decrement*/
10619 if (c == -1 && space_seen) {
10620 dispatch_scan_event(p, tSP);
10625 RUBY_SET_YYLLOC(*p->yylloc);
10627 goto normal_newline;
10631 p->command_start = TRUE;
10632 SET_LEX_STATE(EXPR_BEG);
10636 if ((c = nextc(p)) == '*') {
10637 if ((c = nextc(p)) == '=') {
10638 set_yylval_id(idPow);
10639 SET_LEX_STATE(EXPR_BEG);
10643 if (IS_SPCARG(c)) {
10644 rb_warning0("'**' interpreted as argument prefix");
10647 else if (IS_BEG()) {
10651 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10656 set_yylval_id('*');
10657 SET_LEX_STATE(EXPR_BEG);
10661 if (IS_SPCARG(c)) {
10662 rb_warning0("'*' interpreted as argument prefix");
10665 else if (IS_BEG()) {
10669 c = warn_balanced('*', "*", "argument prefix");
10672 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10677 if (IS_AFTER_OPERATOR()) {
10678 SET_LEX_STATE(EXPR_ARG);
10684 SET_LEX_STATE(EXPR_BEG);
10697 /* skip embedded rd document */
10698 if (word_match_p(p, "begin", 5)) {
10699 int first_p = TRUE;
10702 dispatch_scan_event(p, tEMBDOC_BEG);
10706 dispatch_scan_event(p, tEMBDOC);
10711 compile_error(p, "embedded document meets end of file");
10712 return END_OF_INPUT;
10714 if (c == '=' && word_match_p(p, "end", 3)) {
10720 dispatch_scan_event(p, tEMBDOC_END);
10725 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10726 if ((c = nextc(p)) == '=') {
10727 if ((c = nextc(p)) == '=') {
10736 else if (c == '>') {
10745 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10747 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10748 enum yytokentype token = heredoc_identifier(p);
10749 if (token) return token < 0 ? 0 : token;
10751 if (IS_AFTER_OPERATOR()) {
10752 SET_LEX_STATE(EXPR_ARG);
10755 if (IS_lex_state(EXPR_CLASS))
10756 p->command_start = TRUE;
10757 SET_LEX_STATE(EXPR_BEG);
10760 if ((c = nextc(p)) == '>') {
10767 if ((c = nextc(p)) == '=') {
10768 set_yylval_id(idLTLT);
10769 SET_LEX_STATE(EXPR_BEG);
10773 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10779 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10780 if ((c = nextc(p)) == '=') {
10784 if ((c = nextc(p)) == '=') {
10785 set_yylval_id(idGTGT);
10786 SET_LEX_STATE(EXPR_BEG);
10796 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10797 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10798 p->lex.ptok = p->lex.pcur-1;
10799 return tSTRING_BEG;
10802 if (IS_lex_state(EXPR_FNAME)) {
10803 SET_LEX_STATE(EXPR_ENDFN);
10806 if (IS_lex_state(EXPR_DOT)) {
10808 SET_LEX_STATE(EXPR_CMDARG);
10810 SET_LEX_STATE(EXPR_ARG);
10813 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10814 return tXSTRING_BEG;
10817 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10818 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10819 p->lex.ptok = p->lex.pcur-1;
10820 return tSTRING_BEG;
10823 return parse_qmark(p, space_seen);
10826 if ((c = nextc(p)) == '&') {
10827 SET_LEX_STATE(EXPR_BEG);
10828 if ((c = nextc(p)) == '=') {
10829 set_yylval_id(idANDOP);
10830 SET_LEX_STATE(EXPR_BEG);
10836 else if (c == '=') {
10837 set_yylval_id('&');
10838 SET_LEX_STATE(EXPR_BEG);
10841 else if (c == '.') {
10842 set_yylval_id(idANDDOT);
10843 SET_LEX_STATE(EXPR_DOT);
10847 if (IS_SPCARG(c)) {
10849 (c = peekc_n(p, 1)) == -1 ||
10850 !(c == '\'' || c == '"' ||
10851 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10852 rb_warning0("'&' interpreted as argument prefix");
10856 else if (IS_BEG()) {
10860 c = warn_balanced('&', "&", "argument prefix");
10862 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10866 if ((c = nextc(p)) == '|') {
10867 SET_LEX_STATE(EXPR_BEG);
10868 if ((c = nextc(p)) == '=') {
10869 set_yylval_id(idOROP);
10870 SET_LEX_STATE(EXPR_BEG);
10874 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10882 set_yylval_id('|');
10883 SET_LEX_STATE(EXPR_BEG);
10886 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10892 if (IS_AFTER_OPERATOR()) {
10893 SET_LEX_STATE(EXPR_ARG);
10901 set_yylval_id('+');
10902 SET_LEX_STATE(EXPR_BEG);
10905 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10906 SET_LEX_STATE(EXPR_BEG);
10908 if (c != -1 && ISDIGIT(c)) {
10909 return parse_numeric(p, '+');
10913 SET_LEX_STATE(EXPR_BEG);
10915 return warn_balanced('+', "+", "unary operator");
10919 if (IS_AFTER_OPERATOR()) {
10920 SET_LEX_STATE(EXPR_ARG);
10928 set_yylval_id('-');
10929 SET_LEX_STATE(EXPR_BEG);
10933 SET_LEX_STATE(EXPR_ENDFN);
10934 yylval.num = p->lex.lpar_beg;
10935 p->lex.lpar_beg = p->lex.paren_nest;
10938 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10939 SET_LEX_STATE(EXPR_BEG);
10941 if (c != -1 && ISDIGIT(c)) {
10942 return tUMINUS_NUM;
10946 SET_LEX_STATE(EXPR_BEG);
10948 return warn_balanced('-', "-", "unary operator");
10951 int is_beg = IS_BEG();
10952 SET_LEX_STATE(EXPR_BEG);
10953 if ((c = nextc(p)) == '.') {
10954 if ((c = nextc(p)) == '.') {
10955 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10956 SET_LEX_STATE(EXPR_ENDARG);
10959 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10960 rb_warn0("... at EOL, should be parenthesized?");
10962 return is_beg ? tBDOT3 : tDOT3;
10965 return is_beg ? tBDOT2 : tDOT2;
10968 if (c != -1 && ISDIGIT(c)) {
10969 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10970 parse_numeric(p, '.');
10971 if (ISDIGIT(prev)) {
10972 yyerror0("unexpected fraction part after numeric literal");
10975 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10977 SET_LEX_STATE(EXPR_END);
10978 p->lex.ptok = p->lex.pcur;
10981 set_yylval_id('.');
10982 SET_LEX_STATE(EXPR_DOT);
10986 case '0': case '1': case '2': case '3': case '4':
10987 case '5': case '6': case '7': case '8': case '9':
10988 return parse_numeric(p, c);
10993 SET_LEX_STATE(EXPR_ENDFN);
10994 p->lex.paren_nest--;
11000 SET_LEX_STATE(EXPR_END);
11001 p->lex.paren_nest--;
11005 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11006 if (!p->lex.brace_nest--) return tSTRING_DEND;
11009 SET_LEX_STATE(EXPR_END);
11010 p->lex.paren_nest--;
11016 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11017 SET_LEX_STATE(EXPR_BEG);
11020 set_yylval_id(idCOLON2);
11021 SET_LEX_STATE(EXPR_DOT);
11024 if (IS_END() || ISSPACE(c) || c == '#') {
11026 c = warn_balanced(':', ":", "symbol literal");
11027 SET_LEX_STATE(EXPR_BEG);
11032 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11035 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11041 SET_LEX_STATE(EXPR_FNAME);
11046 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11047 return tREGEXP_BEG;
11049 if ((c = nextc(p)) == '=') {
11050 set_yylval_id('/');
11051 SET_LEX_STATE(EXPR_BEG);
11055 if (IS_SPCARG(c)) {
11056 arg_ambiguous(p, '/');
11057 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11058 return tREGEXP_BEG;
11060 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11061 return warn_balanced('/', "/", "regexp literal");
11064 if ((c = nextc(p)) == '=') {
11065 set_yylval_id('^');
11066 SET_LEX_STATE(EXPR_BEG);
11069 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11074 SET_LEX_STATE(EXPR_BEG);
11075 p->command_start = TRUE;
11079 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11083 if (IS_AFTER_OPERATOR()) {
11084 if ((c = nextc(p)) != '@') {
11087 SET_LEX_STATE(EXPR_ARG);
11090 SET_LEX_STATE(EXPR_BEG);
11098 else if (!space_seen) {
11099 /* foo( ... ) => method call, no ambiguity */
11101 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11104 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11105 rb_warning0("parentheses after method name is interpreted as "
11106 "an argument list, not a decomposed argument");
11108 p->lex.paren_nest++;
11111 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11115 p->lex.paren_nest++;
11116 if (IS_AFTER_OPERATOR()) {
11117 if ((c = nextc(p)) == ']') {
11118 p->lex.paren_nest--;
11119 SET_LEX_STATE(EXPR_ARG);
11120 if ((c = nextc(p)) == '=') {
11127 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11130 else if (IS_BEG()) {
11133 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11136 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11142 ++p->lex.brace_nest;
11143 if (lambda_beginning_p())
11145 else if (IS_lex_state(EXPR_LABELED))
11146 c = tLBRACE; /* hash */
11147 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11148 c = '{'; /* block (primary) */
11149 else if (IS_lex_state(EXPR_ENDARG))
11150 c = tLBRACE_ARG; /* block (expr) */
11152 c = tLBRACE; /* hash */
11153 if (c != tLBRACE) {
11154 p->command_start = TRUE;
11155 SET_LEX_STATE(EXPR_BEG);
11158 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11160 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11169 dispatch_scan_event(p, tSP);
11170 goto retry; /* skip \\n */
11172 if (c == ' ') return tSP;
11173 if (ISSPACE(c)) return c;
11178 return parse_percent(p, space_seen, last_state);
11181 return parse_gvar(p, last_state);
11184 return parse_atmark(p, last_state);
11187 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11188 p->ruby__end__seen = 1;
11192 dispatch_scan_event(p, k__END__);
11194 return END_OF_INPUT;
11200 if (!parser_is_identchar(p)) {
11201 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11210 return parse_ident(p, c, cmd_state);
11213static enum yytokentype
11214yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11216 enum yytokentype t;
11220 p->yylloc = yylloc;
11222 t = parser_yylex(p);
11224 if (has_delayed_token(p))
11225 dispatch_delayed_token(p, t);
11226 else if (t != END_OF_INPUT)
11227 dispatch_scan_event(p, t);
11232#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11235node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11237 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11239 rb_node_init(n, type);
11244nd_set_loc(NODE *nd, const YYLTYPE *loc)
11247 nd_set_line(nd, loc->beg_pos.lineno);
11252node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11254 NODE *n = node_new_internal(p, type, size, alignment);
11256 nd_set_loc(n, loc);
11257 nd_set_node_id(n, parser_get_node_id(p));
11261#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11263static rb_node_scope_t *
11264rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11266 rb_ast_id_table_t *nd_tbl;
11267 nd_tbl = local_tbl(p);
11268 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11269 n->nd_tbl = nd_tbl;
11270 n->nd_body = nd_body;
11271 n->nd_parent = nd_parent;
11272 n->nd_args = nd_args;
11277static rb_node_scope_t *
11278rb_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)
11280 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11281 n->nd_tbl = nd_tbl;
11282 n->nd_body = nd_body;
11283 n->nd_parent = nd_parent;
11284 n->nd_args = nd_args;
11289static rb_node_defn_t *
11290rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11292 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11293 n->nd_mid = nd_mid;
11294 n->nd_defn = nd_defn;
11299static rb_node_defs_t *
11300rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11302 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11303 n->nd_recv = nd_recv;
11304 n->nd_mid = nd_mid;
11305 n->nd_defn = nd_defn;
11310static rb_node_block_t *
11311rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11313 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11314 n->nd_head = nd_head;
11315 n->nd_end = (NODE *)n;
11321static rb_node_for_t *
11322rb_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)
11324 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11325 n->nd_body = nd_body;
11326 n->nd_iter = nd_iter;
11327 n->for_keyword_loc = *for_keyword_loc;
11328 n->in_keyword_loc = *in_keyword_loc;
11329 n->do_keyword_loc = *do_keyword_loc;
11330 n->end_keyword_loc = *end_keyword_loc;
11335static rb_node_for_masgn_t *
11336rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11338 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11339 n->nd_var = nd_var;
11344static rb_node_retry_t *
11345rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11347 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11352static rb_node_begin_t *
11353rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11355 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11356 n->nd_body = nd_body;
11361static rb_node_rescue_t *
11362rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11364 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11365 n->nd_head = nd_head;
11366 n->nd_resq = nd_resq;
11367 n->nd_else = nd_else;
11372static rb_node_resbody_t *
11373rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11375 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11376 n->nd_args = nd_args;
11377 n->nd_exc_var = nd_exc_var;
11378 n->nd_body = nd_body;
11379 n->nd_next = nd_next;
11384static rb_node_ensure_t *
11385rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11387 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11388 n->nd_head = nd_head;
11389 n->nd_ensr = nd_ensr;
11394static rb_node_and_t *
11395rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11397 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11398 n->nd_1st = nd_1st;
11399 n->nd_2nd = nd_2nd;
11400 n->operator_loc = *operator_loc;
11405static rb_node_or_t *
11406rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11408 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11409 n->nd_1st = nd_1st;
11410 n->nd_2nd = nd_2nd;
11411 n->operator_loc = *operator_loc;
11416static rb_node_return_t *
11417rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11419 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11420 n->nd_stts = nd_stts;
11421 n->keyword_loc = *keyword_loc;
11425static rb_node_yield_t *
11426rb_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)
11428 if (nd_head) no_blockarg(p, nd_head);
11430 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11431 n->nd_head = nd_head;
11432 n->keyword_loc = *keyword_loc;
11433 n->lparen_loc = *lparen_loc;
11434 n->rparen_loc = *rparen_loc;
11439static rb_node_if_t *
11440rb_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)
11442 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11443 n->nd_cond = nd_cond;
11444 n->nd_body = nd_body;
11445 n->nd_else = nd_else;
11446 n->if_keyword_loc = *if_keyword_loc;
11447 n->then_keyword_loc = *then_keyword_loc;
11448 n->end_keyword_loc = *end_keyword_loc;
11453static rb_node_unless_t *
11454rb_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)
11456 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11457 n->nd_cond = nd_cond;
11458 n->nd_body = nd_body;
11459 n->nd_else = nd_else;
11460 n->keyword_loc = *keyword_loc;
11461 n->then_keyword_loc = *then_keyword_loc;
11462 n->end_keyword_loc = *end_keyword_loc;
11467static rb_node_class_t *
11468rb_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)
11470 /* Keep the order of node creation */
11471 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11472 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11473 RNODE_SCOPE(scope)->nd_parent = &n->node;
11474 n->nd_cpath = nd_cpath;
11475 n->nd_body = scope;
11476 n->nd_super = nd_super;
11477 n->class_keyword_loc = *class_keyword_loc;
11478 n->inheritance_operator_loc = *inheritance_operator_loc;
11479 n->end_keyword_loc = *end_keyword_loc;
11484static rb_node_sclass_t *
11485rb_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)
11487 /* Keep the order of node creation */
11488 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11489 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11490 RNODE_SCOPE(scope)->nd_parent = &n->node;
11491 n->nd_recv = nd_recv;
11492 n->nd_body = scope;
11493 n->class_keyword_loc = *class_keyword_loc;
11494 n->operator_loc = *operator_loc;
11495 n->end_keyword_loc = *end_keyword_loc;
11500static rb_node_module_t *
11501rb_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)
11503 /* Keep the order of node creation */
11504 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11505 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11506 RNODE_SCOPE(scope)->nd_parent = &n->node;
11507 n->nd_cpath = nd_cpath;
11508 n->nd_body = scope;
11509 n->module_keyword_loc = *module_keyword_loc;
11510 n->end_keyword_loc = *end_keyword_loc;
11515static rb_node_iter_t *
11516rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11518 /* Keep the order of node creation */
11519 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11520 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11521 RNODE_SCOPE(scope)->nd_parent = &n->node;
11522 n->nd_body = scope;
11528static rb_node_lambda_t *
11529rb_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)
11531 /* Keep the order of node creation */
11532 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11533 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11534 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11535 RNODE_SCOPE(scope)->nd_parent = &n->node;
11536 n->nd_body = scope;
11537 n->operator_loc = *operator_loc;
11538 n->opening_loc = *opening_loc;
11539 n->closing_loc = *closing_loc;
11544static rb_node_case_t *
11545rb_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)
11547 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11548 n->nd_head = nd_head;
11549 n->nd_body = nd_body;
11550 n->case_keyword_loc = *case_keyword_loc;
11551 n->end_keyword_loc = *end_keyword_loc;
11556static rb_node_case2_t *
11557rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11559 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11561 n->nd_body = nd_body;
11562 n->case_keyword_loc = *case_keyword_loc;
11563 n->end_keyword_loc = *end_keyword_loc;
11568static rb_node_case3_t *
11569rb_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)
11571 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11572 n->nd_head = nd_head;
11573 n->nd_body = nd_body;
11574 n->case_keyword_loc = *case_keyword_loc;
11575 n->end_keyword_loc = *end_keyword_loc;
11580static rb_node_when_t *
11581rb_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)
11583 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11584 n->nd_head = nd_head;
11585 n->nd_body = nd_body;
11586 n->nd_next = nd_next;
11587 n->keyword_loc = *keyword_loc;
11588 n->then_keyword_loc = *then_keyword_loc;
11593static rb_node_in_t *
11594rb_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)
11596 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11597 n->nd_head = nd_head;
11598 n->nd_body = nd_body;
11599 n->nd_next = nd_next;
11600 n->in_keyword_loc = *in_keyword_loc;
11601 n->then_keyword_loc = *then_keyword_loc;
11602 n->operator_loc = *operator_loc;
11607static rb_node_while_t *
11608rb_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)
11610 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11611 n->nd_cond = nd_cond;
11612 n->nd_body = nd_body;
11613 n->nd_state = nd_state;
11614 n->keyword_loc = *keyword_loc;
11615 n->closing_loc = *closing_loc;
11620static rb_node_until_t *
11621rb_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)
11623 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11624 n->nd_cond = nd_cond;
11625 n->nd_body = nd_body;
11626 n->nd_state = nd_state;
11627 n->keyword_loc = *keyword_loc;
11628 n->closing_loc = *closing_loc;
11633static rb_node_colon2_t *
11634rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11636 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11637 n->nd_head = nd_head;
11638 n->nd_mid = nd_mid;
11639 n->delimiter_loc = *delimiter_loc;
11640 n->name_loc = *name_loc;
11645static rb_node_colon3_t *
11646rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11648 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11649 n->nd_mid = nd_mid;
11650 n->delimiter_loc = *delimiter_loc;
11651 n->name_loc = *name_loc;
11656static rb_node_dot2_t *
11657rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11659 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11660 n->nd_beg = nd_beg;
11661 n->nd_end = nd_end;
11662 n->operator_loc = *operator_loc;
11667static rb_node_dot3_t *
11668rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11670 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11671 n->nd_beg = nd_beg;
11672 n->nd_end = nd_end;
11673 n->operator_loc = *operator_loc;
11678static rb_node_self_t *
11679rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11681 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11687static rb_node_nil_t *
11688rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11690 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11695static rb_node_true_t *
11696rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11698 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11703static rb_node_false_t *
11704rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11706 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11711static rb_node_super_t *
11712rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11713 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11715 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11716 n->nd_args = nd_args;
11717 n->keyword_loc = *keyword_loc;
11718 n->lparen_loc = *lparen_loc;
11719 n->rparen_loc = *rparen_loc;
11724static rb_node_zsuper_t *
11725rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11727 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11732static rb_node_match2_t *
11733rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11735 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11736 n->nd_recv = nd_recv;
11737 n->nd_value = nd_value;
11743static rb_node_match3_t *
11744rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11746 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11747 n->nd_recv = nd_recv;
11748 n->nd_value = nd_value;
11753/* TODO: Use union for NODE_LIST2 */
11754static rb_node_list_t *
11755rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11757 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11758 n->nd_head = nd_head;
11765static rb_node_list_t *
11766rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11768 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11769 n->nd_head = nd_head;
11770 n->as.nd_alen = nd_alen;
11771 n->nd_next = nd_next;
11776static rb_node_zlist_t *
11777rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11779 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11784static rb_node_hash_t *
11785rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11787 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11788 n->nd_head = nd_head;
11794static rb_node_masgn_t *
11795rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11797 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11798 n->nd_head = nd_head;
11800 n->nd_args = nd_args;
11805static rb_node_gasgn_t *
11806rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11808 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11809 n->nd_vid = nd_vid;
11810 n->nd_value = nd_value;
11815static rb_node_lasgn_t *
11816rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11818 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11819 n->nd_vid = nd_vid;
11820 n->nd_value = nd_value;
11825static rb_node_dasgn_t *
11826rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11828 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11829 n->nd_vid = nd_vid;
11830 n->nd_value = nd_value;
11835static rb_node_iasgn_t *
11836rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11838 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11839 n->nd_vid = nd_vid;
11840 n->nd_value = nd_value;
11845static rb_node_cvasgn_t *
11846rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11848 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11849 n->nd_vid = nd_vid;
11850 n->nd_value = nd_value;
11855static rb_node_op_asgn1_t *
11856rb_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)
11858 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11859 n->nd_recv = nd_recv;
11860 n->nd_mid = nd_mid;
11861 n->nd_index = index;
11862 n->nd_rvalue = rvalue;
11863 n->call_operator_loc = *call_operator_loc;
11864 n->opening_loc = *opening_loc;
11865 n->closing_loc = *closing_loc;
11866 n->binary_operator_loc = *binary_operator_loc;
11871static rb_node_op_asgn2_t *
11872rb_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)
11874 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11875 n->nd_recv = nd_recv;
11876 n->nd_value = nd_value;
11877 n->nd_vid = nd_vid;
11878 n->nd_mid = nd_mid;
11879 n->nd_aid = nd_aid;
11880 n->call_operator_loc = *call_operator_loc;
11881 n->message_loc = *message_loc;
11882 n->binary_operator_loc = *binary_operator_loc;
11887static rb_node_op_asgn_or_t *
11888rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11890 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11891 n->nd_head = nd_head;
11892 n->nd_value = nd_value;
11897static rb_node_op_asgn_and_t *
11898rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11900 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11901 n->nd_head = nd_head;
11902 n->nd_value = nd_value;
11907static rb_node_gvar_t *
11908rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11910 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11911 n->nd_vid = nd_vid;
11916static rb_node_lvar_t *
11917rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11919 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11920 n->nd_vid = nd_vid;
11925static rb_node_dvar_t *
11926rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11928 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11929 n->nd_vid = nd_vid;
11934static rb_node_ivar_t *
11935rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11937 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11938 n->nd_vid = nd_vid;
11943static rb_node_const_t *
11944rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11946 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11947 n->nd_vid = nd_vid;
11952static rb_node_cvar_t *
11953rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11955 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11956 n->nd_vid = nd_vid;
11961static rb_node_nth_ref_t *
11962rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11964 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11965 n->nd_nth = nd_nth;
11970static rb_node_back_ref_t *
11971rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11973 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11974 n->nd_nth = nd_nth;
11979static rb_node_integer_t *
11980rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11982 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11990static rb_node_float_t *
11991rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11993 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12000static rb_node_rational_t *
12001rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12003 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12007 n->seen_point = seen_point;
12012static rb_node_imaginary_t *
12013rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12015 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12019 n->seen_point = seen_point;
12020 n->type = numeric_type;
12025static rb_node_str_t *
12026rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12028 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12029 n->string = string;
12034/* TODO; Use union for NODE_DSTR2 */
12035static rb_node_dstr_t *
12036rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12038 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12039 n->string = string;
12040 n->as.nd_alen = nd_alen;
12041 n->nd_next = (rb_node_list_t *)nd_next;
12046static rb_node_dstr_t *
12047rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12049 return rb_node_dstr_new0(p, string, 1, 0, loc);
12052static rb_node_xstr_t *
12053rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12055 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12056 n->string = string;
12061static rb_node_dxstr_t *
12062rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12064 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12065 n->string = string;
12066 n->as.nd_alen = nd_alen;
12067 n->nd_next = (rb_node_list_t *)nd_next;
12072static rb_node_sym_t *
12073rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12075 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12076 n->string = rb_str_to_parser_string(p, str);
12081static rb_node_dsym_t *
12082rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12084 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12085 n->string = string;
12086 n->as.nd_alen = nd_alen;
12087 n->nd_next = (rb_node_list_t *)nd_next;
12092static rb_node_evstr_t *
12093rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12095 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12096 n->nd_body = nd_body;
12097 n->opening_loc = *opening_loc;
12098 n->closing_loc = *closing_loc;
12103static rb_node_regx_t *
12104rb_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)
12106 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12107 n->string = string;
12108 n->options = options & RE_OPTION_MASK;
12109 n->opening_loc = *opening_loc;
12110 n->content_loc = *content_loc;
12111 n->closing_loc = *closing_loc;
12116static rb_node_call_t *
12117rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12119 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12120 n->nd_recv = nd_recv;
12121 n->nd_mid = nd_mid;
12122 n->nd_args = nd_args;
12127static rb_node_opcall_t *
12128rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12130 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12131 n->nd_recv = nd_recv;
12132 n->nd_mid = nd_mid;
12133 n->nd_args = nd_args;
12138static rb_node_fcall_t *
12139rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12141 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12142 n->nd_mid = nd_mid;
12143 n->nd_args = nd_args;
12148static rb_node_qcall_t *
12149rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12151 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12152 n->nd_recv = nd_recv;
12153 n->nd_mid = nd_mid;
12154 n->nd_args = nd_args;
12159static rb_node_vcall_t *
12160rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12162 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12163 n->nd_mid = nd_mid;
12168static rb_node_once_t *
12169rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12171 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12172 n->nd_body = nd_body;
12177static rb_node_args_t *
12178rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12180 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12181 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12186static rb_node_args_aux_t *
12187rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12189 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12190 n->nd_pid = nd_pid;
12191 n->nd_plen = nd_plen;
12197static rb_node_opt_arg_t *
12198rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12200 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12201 n->nd_body = nd_body;
12207static rb_node_kw_arg_t *
12208rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12210 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12211 n->nd_body = nd_body;
12217static rb_node_postarg_t *
12218rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12220 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12221 n->nd_1st = nd_1st;
12222 n->nd_2nd = nd_2nd;
12227static rb_node_argscat_t *
12228rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12230 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12231 n->nd_head = nd_head;
12232 n->nd_body = nd_body;
12237static rb_node_argspush_t *
12238rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12240 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12241 n->nd_head = nd_head;
12242 n->nd_body = nd_body;
12247static rb_node_splat_t *
12248rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12250 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12251 n->nd_head = nd_head;
12252 n->operator_loc = *operator_loc;
12257static rb_node_block_pass_t *
12258rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12260 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12263 n->nd_body = nd_body;
12264 n->operator_loc = *operator_loc;
12269static rb_node_alias_t *
12270rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12272 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12273 n->nd_1st = nd_1st;
12274 n->nd_2nd = nd_2nd;
12275 n->keyword_loc = *keyword_loc;
12280static rb_node_valias_t *
12281rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12283 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12284 n->nd_alias = nd_alias;
12285 n->nd_orig = nd_orig;
12286 n->keyword_loc = *keyword_loc;
12291static rb_node_undef_t *
12292rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12294 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12295 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12296 n->keyword_loc = NULL_LOC;
12297 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12302static rb_node_errinfo_t *
12303rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12305 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12310static rb_node_defined_t *
12311rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12313 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12314 n->nd_head = nd_head;
12315 n->keyword_loc = *keyword_loc;
12320static rb_node_postexe_t *
12321rb_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)
12323 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12324 n->nd_body = nd_body;
12325 n->keyword_loc = *keyword_loc;
12326 n->opening_loc = *opening_loc;
12327 n->closing_loc = *closing_loc;
12332static rb_node_attrasgn_t *
12333rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12335 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12336 n->nd_recv = nd_recv;
12337 n->nd_mid = nd_mid;
12338 n->nd_args = nd_args;
12343static rb_node_aryptn_t *
12344rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12346 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12348 n->pre_args = pre_args;
12349 n->rest_arg = rest_arg;
12350 n->post_args = post_args;
12355static rb_node_hshptn_t *
12356rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12358 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12359 n->nd_pconst = nd_pconst;
12360 n->nd_pkwargs = nd_pkwargs;
12361 n->nd_pkwrestarg = nd_pkwrestarg;
12366static rb_node_fndptn_t *
12367rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12369 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12371 n->pre_rest_arg = pre_rest_arg;
12373 n->post_rest_arg = post_rest_arg;
12378static rb_node_line_t *
12379rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12381 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12386static rb_node_file_t *
12387rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12389 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12390 n->path = rb_str_to_parser_string(p, str);
12395static rb_node_encoding_t *
12396rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12398 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12404static rb_node_cdecl_t *
12405rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12407 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12408 n->nd_vid = nd_vid;
12409 n->nd_value = nd_value;
12410 n->nd_else = nd_else;
12411 n->shareability = shareability;
12416static rb_node_op_cdecl_t *
12417rb_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)
12419 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12420 n->nd_head = nd_head;
12421 n->nd_value = nd_value;
12422 n->nd_aid = nd_aid;
12423 n->shareability = shareability;
12428static rb_node_error_t *
12429rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12431 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12436static rb_node_break_t *
12437rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12439 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12440 n->nd_stts = nd_stts;
12442 n->keyword_loc = *keyword_loc;
12447static rb_node_next_t *
12448rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12450 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12451 n->nd_stts = nd_stts;
12453 n->keyword_loc = *keyword_loc;
12458static rb_node_redo_t *
12459rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12461 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12463 n->keyword_loc = *keyword_loc;
12468static rb_node_def_temp_t *
12469rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12471 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12472 n->save.numparam_save = 0;
12473 n->save.max_numparam = 0;
12474 n->save.ctxt = p->ctxt;
12481static rb_node_def_temp_t *
12482def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12484 n->save.numparam_save = numparam_push(p);
12485 n->save.max_numparam = p->max_numparam;
12490static enum node_type
12491nodetype(NODE *node) /* for debug */
12493 return (enum node_type)nd_type(node);
12497nodeline(NODE *node)
12499 return nd_line(node);
12504newline_node(NODE *node)
12507 node = remove_begin(node);
12508 nd_set_fl_newline(node);
12514fixpos(NODE *node, NODE *orig)
12518 nd_set_line(node, nd_line(orig));
12522block_append(struct parser_params *p, NODE *head, NODE *tail)
12524 NODE *end, *h = head, *nd;
12526 if (tail == 0) return head;
12528 if (h == 0) return tail;
12529 switch (nd_type(h)) {
12531 h = end = NEW_BLOCK(head, &head->nd_loc);
12535 end = RNODE_BLOCK(h)->nd_end;
12539 nd = RNODE_BLOCK(end)->nd_head;
12540 switch (nd_type(nd)) {
12546 rb_warning0L(nd_line(tail), "statement not reached");
12553 if (!nd_type_p(tail, NODE_BLOCK)) {
12554 tail = NEW_BLOCK(tail, &tail->nd_loc);
12556 RNODE_BLOCK(end)->nd_next = tail;
12557 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12558 nd_set_last_loc(head, nd_last_loc(tail));
12562/* append item to the list */
12564list_append(struct parser_params *p, NODE *list, NODE *item)
12568 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12569 if (RNODE_LIST(list)->nd_next) {
12570 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12576 RNODE_LIST(list)->as.nd_alen += 1;
12577 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12578 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12580 nd_set_last_loc(list, nd_last_loc(item));
12585/* concat two lists */
12587list_concat(NODE *head, NODE *tail)
12591 if (RNODE_LIST(head)->nd_next) {
12592 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12598 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12599 RNODE_LIST(last)->nd_next = tail;
12600 if (RNODE_LIST(tail)->nd_next) {
12601 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12604 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12607 nd_set_last_loc(head, nd_last_loc(tail));
12613literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12615 if (!tail) return 1;
12616 if (!rb_parser_enc_compatible(p, head, tail)) {
12617 compile_error(p, "string literal encodings differ (%s / %s)",
12618 rb_enc_name(rb_parser_str_get_encoding(head)),
12619 rb_enc_name(rb_parser_str_get_encoding(tail)));
12620 rb_parser_str_resize(p, head, 0);
12621 rb_parser_str_resize(p, tail, 0);
12624 rb_parser_str_buf_append(p, head, tail);
12628static rb_parser_string_t *
12629string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12631 if (htype != NODE_DSTR) return NULL;
12632 if (RNODE_DSTR(head)->nd_next) {
12633 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12634 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12636 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12642static rb_parser_string_t *
12643rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12645 rb_parser_string_t *copy;
12646 if (!orig) return NULL;
12647 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12648 copy->coderange = orig->coderange;
12649 copy->enc = orig->enc;
12654/* concat two string literals */
12656literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12658 enum node_type htype;
12659 rb_parser_string_t *lit;
12661 if (!head) return tail;
12662 if (!tail) return head;
12664 htype = nd_type(head);
12665 if (htype == NODE_EVSTR) {
12666 head = new_dstr(p, head, loc);
12669 if (p->heredoc_indent > 0) {
12672 head = str2dstr(p, head);
12674 return list_append(p, head, tail);
12679 switch (nd_type(tail)) {
12681 if ((lit = string_literal_head(p, htype, head)) != false) {
12685 lit = RNODE_DSTR(head)->string;
12687 if (htype == NODE_STR) {
12688 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12690 rb_discard_node(p, head);
12691 rb_discard_node(p, tail);
12694 rb_discard_node(p, tail);
12697 list_append(p, head, tail);
12702 if (htype == NODE_STR) {
12703 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12705 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12706 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12707 RNODE_STR(head)->string = NULL;
12708 rb_discard_node(p, head);
12711 else if (!RNODE_DSTR(tail)->string) {
12713 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12714 if (!RNODE_DSTR(head)->nd_next) {
12715 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12717 else if (RNODE_DSTR(tail)->nd_next) {
12718 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12719 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12721 rb_discard_node(p, tail);
12723 else if ((lit = string_literal_head(p, htype, head)) != false) {
12724 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12726 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12727 RNODE_DSTR(tail)->string = 0;
12731 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));
12732 RNODE_DSTR(tail)->string = 0;
12737 if (htype == NODE_STR) {
12738 head = str2dstr(p, head);
12739 RNODE_DSTR(head)->as.nd_alen = 1;
12741 list_append(p, head, tail);
12748nd_copy_flag(NODE *new_node, NODE *old_node)
12750 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12751 nd_set_line(new_node, nd_line(old_node));
12752 new_node->nd_loc = old_node->nd_loc;
12753 new_node->node_id = old_node->node_id;
12757str2dstr(struct parser_params *p, NODE *node)
12759 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12760 nd_copy_flag(new_node, node);
12761 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12762 RNODE_DSTR(new_node)->as.nd_alen = 0;
12763 RNODE_DSTR(new_node)->nd_next = 0;
12764 RNODE_STR(node)->string = 0;
12770str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12772 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12773 nd_copy_flag(new_node, node);
12774 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12775 RNODE_REGX(new_node)->options = options;
12776 nd_set_loc(new_node, loc);
12777 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12778 RNODE_REGX(new_node)->content_loc = *content_loc;
12779 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12780 RNODE_STR(node)->string = 0;
12786evstr2dstr(struct parser_params *p, NODE *node)
12788 if (nd_type_p(node, NODE_EVSTR)) {
12789 node = new_dstr(p, node, &node->nd_loc);
12795new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12800 switch (nd_type(node)) {
12802 return str2dstr(p, node);
12809 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12813new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12815 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12816 return list_append(p, dstr, node);
12820call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12821 const YYLTYPE *op_loc, const YYLTYPE *loc)
12824 value_expr(p, recv);
12825 value_expr(p, arg1);
12826 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12827 nd_set_line(expr, op_loc->beg_pos.lineno);
12832call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12835 value_expr(p, recv);
12836 opcall = NEW_OPCALL(recv, id, 0, loc);
12837 nd_set_line(opcall, op_loc->beg_pos.lineno);
12842new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12844 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12845 nd_set_line(qcall, op_loc->beg_pos.lineno);
12850new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12853 if (block) block_dup_check(p, args, block);
12854 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12855 if (block) ret = method_add_block(p, ret, block, loc);
12860static rb_locations_lambda_body_t*
12861new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12863 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12865 body->opening_loc = *opening_loc;
12866 body->closing_loc = *closing_loc;
12871command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12874 enum node_type type = nd_type(m);
12877 compile_error(p, "block given to yield");
12880 body = &RNODE_RETURN(m)->nd_stts;
12884 body = &RNODE_EXITS(m)->nd_stts;
12886 case NODE_REDO: /* `redo` has no argument */
12887 compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12889 case NODE_RETRY: /* `retry` has no argument */
12890 compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12893 block_dup_check(p, get_nd_args(p, m), b);
12894 return method_add_block(p, m, b, loc);
12896 RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12897 YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12898 *body = command_add_block(p, *body, b, &b_loc);
12899 m->nd_loc.end_pos = loc->end_pos;
12903#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12906last_expr_once_body(NODE *node)
12908 if (!node) return 0;
12909 return nd_once_body(node);
12913match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12916 int line = op_loc->beg_pos.lineno;
12918 value_expr(p, node1);
12919 value_expr(p, node2);
12921 if ((n = last_expr_once_body(node1)) != 0) {
12922 switch (nd_type(n)) {
12925 NODE *match = NEW_MATCH2(node1, node2, loc);
12926 nd_set_line(match, line);
12932 const VALUE lit = rb_node_regx_string_val(n);
12934 NODE *match = NEW_MATCH2(node1, node2, loc);
12935 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12936 nd_set_line(match, line);
12943 if ((n = last_expr_once_body(node2)) != 0) {
12946 switch (nd_type(n)) {
12948 match3 = NEW_MATCH3(node2, node1, loc);
12953 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12954 nd_set_line(n, line);
12958# if WARN_PAST_SCOPE
12960past_dvar_p(struct parser_params *p, ID id)
12962 struct vtable *past = p->lvtbl->past;
12964 if (vtable_included(past, id)) return 1;
12972numparam_nested_p(struct parser_params *p)
12974 struct local_vars *local = p->lvtbl;
12975 NODE *outer = local->numparam.outer;
12976 NODE *inner = local->numparam.inner;
12977 if (outer || inner) {
12978 NODE *used = outer ? outer : inner;
12979 compile_error(p, "numbered parameter is already used in %s block\n"
12980 "%s:%d: numbered parameter is already used here",
12981 outer ? "outer" : "inner",
12982 p->ruby_sourcefile, nd_line(used));
12983 parser_show_error_line(p, &used->nd_loc);
12990numparam_used_p(struct parser_params *p)
12992 NODE *numparam = p->lvtbl->numparam.current;
12994 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12995 "%s:%d: numbered parameter is already used here",
12996 p->ruby_sourcefile, nd_line(numparam));
12997 parser_show_error_line(p, &numparam->nd_loc);
13004it_used_p(struct parser_params *p)
13006 NODE *it = p->lvtbl->it;
13008 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
13009 "%s:%d: 'it' is already used here",
13010 p->ruby_sourcefile, nd_line(it));
13011 parser_show_error_line(p, &it->nd_loc);
13018gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13024 return NEW_SELF(loc);
13026 return NEW_NIL(loc);
13028 return NEW_TRUE(loc);
13029 case keyword_false:
13030 return NEW_FALSE(loc);
13031 case keyword__FILE__:
13033 VALUE file = p->ruby_sourcefile_string;
13035 file = rb_str_new(0, 0);
13036 node = NEW_FILE(file, loc);
13039 case keyword__LINE__:
13040 return NEW_LINE(loc);
13041 case keyword__ENCODING__:
13042 return NEW_ENCODING(loc);
13045 switch (id_type(id)) {
13047 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13048 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13049 if (vidp) *vidp |= LVAR_USED;
13050 node = NEW_DVAR(id, loc);
13053 if (local_id_ref(p, id, &vidp)) {
13054 if (vidp) *vidp |= LVAR_USED;
13055 node = NEW_LVAR(id, loc);
13058 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13059 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13060 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13061 node = NEW_DVAR(id, loc);
13062 struct local_vars *local = p->lvtbl;
13063 if (!local->numparam.current) local->numparam.current = node;
13066# if WARN_PAST_SCOPE
13067 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13068 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13071 /* method call without arguments */
13072 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13073 if (numparam_used_p(p)) return 0;
13074 if (p->max_numparam == ORDINAL_PARAM) {
13075 compile_error(p, "ordinary parameter is defined");
13079 p->it_id = idItImplicit;
13080 vtable_add(p->lvtbl->args, p->it_id);
13082 NODE *node = NEW_DVAR(p->it_id, loc);
13083 if (!p->lvtbl->it) p->lvtbl->it = node;
13086 return NEW_VCALL(id, loc);
13088 return NEW_GVAR(id, loc);
13090 return NEW_IVAR(id, loc);
13092 return NEW_CONST(id, loc);
13094 return NEW_CVAR(id, loc);
13096 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13100static rb_node_opt_arg_t *
13101opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13103 rb_node_opt_arg_t *opts = opt_list;
13104 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13106 while (opts->nd_next) {
13107 opts = opts->nd_next;
13108 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13110 opts->nd_next = opt;
13115static rb_node_kw_arg_t *
13116kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13119 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13120 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13126new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13128 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13129 p->ctxt.has_trailing_semicolon = 0;
13133 if (nd_type_p(n, NODE_BEGIN)) {
13134 n = RNODE_BEGIN(n)->nd_body;
13136 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13137 n = RNODE_BLOCK(n)->nd_head;
13144 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13145 NODE *block = NEW_BLOCK(expr, loc);
13146 return NEW_DEFINED(block, loc, keyword_loc);
13149 return NEW_DEFINED(n, loc, keyword_loc);
13153str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13156 rb_parser_string_t *str = RNODE_STR(node)->string;
13157 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13158 yyerror1(loc, "invalid symbol");
13162 lit = rb_str_new_parser_string(str);
13164 return NEW_SYM(lit, loc);
13168symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13170 enum node_type type = nd_type(symbol);
13173 nd_set_type(symbol, NODE_DSYM);
13176 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13179 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13181 return list_append(p, symbols, symbol);
13185dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13187 if (dreg->string) {
13188 reg_fragment_setenc(p, dreg->string, options);
13190 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13191 NODE *frag = list->nd_head;
13192 if (nd_type_p(frag, NODE_STR)) {
13193 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13195 else if (nd_type_p(frag, NODE_DSTR)) {
13196 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13202new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13205 /* Check string is valid regex */
13206 rb_parser_string_t *str = STRING_NEW0();
13207 reg_compile(p, str, options);
13208 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13211 switch (nd_type(node)) {
13214 /* Check string is valid regex */
13215 reg_compile(p, RNODE_STR(node)->string, options);
13216 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13220 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13223 nd_set_type(node, NODE_DREGX);
13224 nd_set_loc(node, loc);
13225 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13226 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13227 if (dreg->nd_next) {
13228 dregex_fragment_setenc(p, dreg, options);
13230 if (options & RE_OPTION_ONCE) {
13231 node = NEW_ONCE(node, loc);
13238static rb_node_kw_arg_t *
13239new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13242 return NEW_KW_ARG((k), loc);
13246new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13249 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13252 switch (nd_type(node)) {
13254 nd_set_type(node, NODE_XSTR);
13255 nd_set_loc(node, loc);
13258 nd_set_type(node, NODE_DXSTR);
13259 nd_set_loc(node, loc);
13262 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13269struct st_hash_type literal_type = {
13274static int nd_type_st_key_enable_p(NODE *node);
13277check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13279 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13280 if (!arg || !p->case_labels) return;
13281 if (!nd_type_st_key_enable_p(arg)) return;
13283 if (p->case_labels == CHECK_LITERAL_WHEN) {
13284 p->case_labels = st_init_table(&literal_type);
13288 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13289 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13290 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13294 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13299id_is_var(struct parser_params *p, ID id)
13301 if (is_notop_id(id)) {
13302 switch (id & ID_SCOPE_MASK) {
13303 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13306 if (dyna_in_block(p)) {
13307 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13309 if (local_id(p, id)) return 1;
13310 /* method call without arguments */
13314 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13319static inline enum lex_state_e
13320parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13323 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13325 return p->lex.state = ls;
13330flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13332 VALUE mesg = p->debug_buffer;
13334 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13335 p->debug_buffer = Qnil;
13336 rb_io_puts(1, &mesg, out);
13338 if (!NIL_P(str) && RSTRING_LEN(str)) {
13339 rb_io_write(p->debug_output, str);
13343static const char rb_parser_lex_state_names[][8] = {
13344 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13345 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13346 "LABEL", "LABELED","FITEM",
13350append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13353 unsigned int mask = 1;
13354 static const char none[] = "NONE";
13356 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13357 if ((unsigned)state & mask) {
13359 rb_str_cat(buf, "|", 1);
13362 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13366 rb_str_cat(buf, none, sizeof(none)-1);
13372rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13373 enum lex_state_e to, int line)
13376 mesg = rb_str_new_cstr("lex_state: ");
13377 append_lex_state_name(p, from, mesg);
13378 rb_str_cat_cstr(mesg, " -> ");
13379 append_lex_state_name(p, to, mesg);
13380 rb_str_catf(mesg, " at line %d\n", line);
13381 flush_debug_buffer(p, p->debug_output, mesg);
13386rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13388 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13392append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13395 rb_str_cat_cstr(mesg, "0");
13398 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13399 for (; mask && !(stack & mask); mask >>= 1) continue;
13400 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13405rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13406 const char *name, int line)
13408 VALUE mesg = rb_sprintf("%s: ", name);
13409 append_bitstack_value(p, stack, mesg);
13410 rb_str_catf(mesg, " at line %d\n", line);
13411 flush_debug_buffer(p, p->debug_output, mesg);
13415rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13418 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13421 rb_str_vcatf(mesg, fmt, ap);
13423 yyerror0(RSTRING_PTR(mesg));
13426 mesg = rb_str_new(0, 0);
13427 append_lex_state_name(p, p->lex.state, mesg);
13428 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13429 rb_str_resize(mesg, 0);
13430 append_bitstack_value(p, p->cond_stack, mesg);
13431 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13432 rb_str_resize(mesg, 0);
13433 append_bitstack_value(p, p->cmdarg_stack, mesg);
13434 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13435 if (p->debug_output == rb_ractor_stdout())
13436 p->debug_output = rb_ractor_stderr();
13441rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13443 yylloc->beg_pos.lineno = sourceline;
13444 yylloc->beg_pos.column = beg_pos;
13445 yylloc->end_pos.lineno = sourceline;
13446 yylloc->end_pos.column = end_pos;
13451rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13453 int sourceline = here->sourceline;
13454 int beg_pos = (int)here->offset - here->quote
13455 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13456 int end_pos = (int)here->offset + here->length + here->quote;
13458 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13462rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13464 yylloc->beg_pos.lineno = p->delayed.beg_line;
13465 yylloc->beg_pos.column = p->delayed.beg_col;
13466 yylloc->end_pos.lineno = p->delayed.end_line;
13467 yylloc->end_pos.column = p->delayed.end_col;
13473rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13475 int sourceline = p->ruby_sourceline;
13476 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13477 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13478 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13482rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13484 yylloc->end_pos = yylloc->beg_pos;
13490rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13492 int sourceline = p->ruby_sourceline;
13493 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13494 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13495 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13499rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13501 int sourceline = p->ruby_sourceline;
13502 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13503 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13504 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13506#endif /* !RIPPER */
13509assignable0(struct parser_params *p, ID id, const char **err)
13511 if (!id) return -1;
13514 *err = "Can't change the value of self";
13517 *err = "Can't assign to nil";
13520 *err = "Can't assign to true";
13522 case keyword_false:
13523 *err = "Can't assign to false";
13525 case keyword__FILE__:
13526 *err = "Can't assign to __FILE__";
13528 case keyword__LINE__:
13529 *err = "Can't assign to __LINE__";
13531 case keyword__ENCODING__:
13532 *err = "Can't assign to __ENCODING__";
13535 switch (id_type(id)) {
13537 if (dyna_in_block(p)) {
13538 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13539 compile_error(p, "Can't assign to numbered parameter _%d",
13540 NUMPARAM_ID_TO_IDX(id));
13543 if (dvar_curr(p, id)) return NODE_DASGN;
13544 if (dvar_defined(p, id)) return NODE_DASGN;
13545 if (local_id(p, id)) return NODE_LASGN;
13550 if (!local_id(p, id)) local_var(p, id);
13554 case ID_GLOBAL: return NODE_GASGN;
13555 case ID_INSTANCE: return NODE_IASGN;
13557 if (!p->ctxt.in_def) return NODE_CDECL;
13558 *err = "dynamic constant assignment";
13560 case ID_CLASS: return NODE_CVASGN;
13562 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13568assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13570 const char *err = 0;
13571 int node_type = assignable0(p, id, &err);
13572 switch (node_type) {
13573 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13574 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13575 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13576 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13577 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13578 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13582 if (err) yyerror1(loc, err);
13584 if (err) set_value(assign_error(p, err, p->s_lvalue));
13586 return NEW_ERROR(loc);
13590is_private_local_id(struct parser_params *p, ID name)
13593 if (name == idUScore) return 1;
13594 if (!is_local_id(name)) return 0;
13595 s = rb_id2str(name);
13597 return RSTRING_PTR(s)[0] == '_';
13601shadowing_lvar_0(struct parser_params *p, ID name)
13603 if (dyna_in_block(p)) {
13604 if (dvar_curr(p, name)) {
13605 if (is_private_local_id(p, name)) return 1;
13606 yyerror0("duplicated argument name");
13608 else if (dvar_defined(p, name) || local_id(p, name)) {
13609 vtable_add(p->lvtbl->vars, name);
13610 if (p->lvtbl->used) {
13611 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13617 if (local_id(p, name)) {
13618 if (is_private_local_id(p, name)) return 1;
13619 yyerror0("duplicated argument name");
13626shadowing_lvar(struct parser_params *p, ID name)
13628 shadowing_lvar_0(p, name);
13633new_bv(struct parser_params *p, ID name)
13636 if (!is_local_id(name)) {
13637 compile_error(p, "invalid local variable - %"PRIsVALUE,
13641 if (!shadowing_lvar_0(p, name)) return;
13644 if (dvar_defined_ref(p, name, &vidp)) {
13645 if (vidp) *vidp |= LVAR_USED;
13650aryset_check(struct parser_params *p, NODE *args)
13652 NODE *block = 0, *kwds = 0;
13653 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13654 block = RNODE_BLOCK_PASS(args)->nd_body;
13655 args = RNODE_BLOCK_PASS(args)->nd_head;
13657 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13658 args = RNODE_ARGSCAT(args)->nd_body;
13660 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13661 kwds = RNODE_ARGSPUSH(args)->nd_body;
13664 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13665 next = RNODE_LIST(next)->nd_next) {
13666 kwds = RNODE_LIST(next)->nd_head;
13669 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13670 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13673 yyerror1(&block->nd_loc, "block arg given in index assignment");
13678aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13680 aryset_check(p, idx);
13681 return NEW_ATTRASGN(recv, tASET, idx, loc);
13685block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13687 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13688 compile_error(p, "both block arg and actual block given");
13693attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13695 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13696 return NEW_ATTRASGN(recv, id, 0, loc);
13700rb_backref_error(struct parser_params *p, NODE *node)
13703# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13705# define ERR(...) rb_sprintf(__VA_ARGS__)
13707 switch (nd_type(node)) {
13709 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13710 case NODE_BACK_REF:
13711 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13714 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13718arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13720 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13721 switch (nd_type(node1)) {
13723 return list_append(p, node1, node2);
13724 case NODE_BLOCK_PASS:
13725 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13726 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13728 case NODE_ARGSPUSH:
13729 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13730 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13731 nd_set_type(node1, NODE_ARGSCAT);
13734 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13735 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13736 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13739 return NEW_ARGSPUSH(node1, node2, loc);
13743arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13745 if (!node2) return node1;
13746 switch (nd_type(node1)) {
13747 case NODE_BLOCK_PASS:
13748 if (RNODE_BLOCK_PASS(node1)->nd_head)
13749 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13751 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13753 case NODE_ARGSPUSH:
13754 if (!nd_type_p(node2, NODE_LIST)) break;
13755 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13756 nd_set_type(node1, NODE_ARGSCAT);
13759 if (!nd_type_p(node2, NODE_LIST) ||
13760 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13761 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13764 return NEW_ARGSCAT(node1, node2, loc);
13768last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13771 if ((n1 = splat_array(args)) != 0) {
13772 return list_append(p, n1, last_arg);
13774 return arg_append(p, args, last_arg, loc);
13778rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13781 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13782 return list_concat(n1, rest_arg);
13784 return arg_concat(p, args, rest_arg, loc);
13788splat_array(NODE* node)
13790 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13791 if (nd_type_p(node, NODE_LIST)) return node;
13796mark_lvar_used(struct parser_params *p, NODE *rhs)
13800 switch (nd_type(rhs)) {
13802 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13803 if (vidp) *vidp |= LVAR_USED;
13807 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13808 if (vidp) *vidp |= LVAR_USED;
13813 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13814 mark_lvar_used(p, rhs->nd_head);
13821static int is_static_content(NODE *node);
13824node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13826 if (!lhs) return 0;
13828 switch (nd_type(lhs)) {
13836 set_nd_value(p, lhs, rhs);
13837 nd_set_loc(lhs, loc);
13840 case NODE_ATTRASGN:
13841 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13842 nd_set_loc(lhs, loc);
13846 /* should not happen */
13854value_expr_check(struct parser_params *p, NODE *node)
13856 NODE *void_node = 0, *vn;
13859 rb_warning0("empty expression");
13862 switch (nd_type(node)) {
13864 vn = RNODE_ENSURE(node)->nd_head;
13865 node = RNODE_ENSURE(node)->nd_ensr;
13866 /* nd_ensr should not be NULL, check it out next */
13867 if (vn && (vn = value_expr_check(p, vn))) {
13873 /* void only if all children are void */
13874 vn = RNODE_RESCUE(node)->nd_head;
13875 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13876 if (!void_node) void_node = vn;
13877 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13878 if (!nd_type_p(r, NODE_RESBODY)) {
13879 compile_error(p, "unexpected node");
13882 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13886 if (!void_node) void_node = vn;
13888 node = RNODE_RESCUE(node)->nd_else;
13889 if (!node) return void_node;
13900 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13901 compile_error(p, "unexpected node");
13904 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13907 /* single line pattern matching with "=>" operator */
13911 while (RNODE_BLOCK(node)->nd_next) {
13912 node = RNODE_BLOCK(node)->nd_next;
13914 node = RNODE_BLOCK(node)->nd_head;
13918 node = RNODE_BEGIN(node)->nd_body;
13923 if (!RNODE_IF(node)->nd_body) {
13926 else if (!RNODE_IF(node)->nd_else) {
13929 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13930 if (!vn) return NULL;
13931 if (!void_node) void_node = vn;
13932 node = RNODE_IF(node)->nd_else;
13937 node = RNODE_AND(node)->nd_1st;
13943 mark_lvar_used(p, node);
13954 /* return the first found node */
13955 return void_node ? void_node : node;
13959value_expr(struct parser_params *p, NODE *node)
13961 NODE *void_node = value_expr_check(p, node);
13963 yyerror1(&void_node->nd_loc, "void value expression");
13964 /* or "control never reach"? */
13971void_expr(struct parser_params *p, NODE *node)
13973 const char *useless = 0;
13975 if (!RTEST(ruby_verbose)) return;
13977 if (!node || !(node = nd_once_body(node))) return;
13978 switch (nd_type(node)) {
13980 switch (RNODE_OPCALL(node)->nd_mid) {
13999 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
14010 case NODE_BACK_REF:
14011 useless = "a variable";
14014 useless = "a constant";
14019 case NODE_ENCODING:
14022 case NODE_RATIONAL:
14023 case NODE_IMAGINARY:
14028 useless = "a literal";
14053 useless = "defined?";
14058 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14062/* warns useless use of block and returns the last statement node */
14064void_stmts(struct parser_params *p, NODE *node)
14066 NODE *const n = node;
14067 if (!RTEST(ruby_verbose)) return n;
14068 if (!node) return n;
14069 if (!nd_type_p(node, NODE_BLOCK)) return n;
14071 while (RNODE_BLOCK(node)->nd_next) {
14072 void_expr(p, RNODE_BLOCK(node)->nd_head);
14073 node = RNODE_BLOCK(node)->nd_next;
14075 return RNODE_BLOCK(node)->nd_head;
14079remove_begin(NODE *node)
14081 NODE **n = &node, *n1 = node;
14082 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14083 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14089reduce_nodes(struct parser_params *p, NODE **body)
14091 NODE *node = *body;
14094 *body = NEW_NIL(&NULL_LOC);
14097#define subnodes(type, n1, n2) \
14098 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14099 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14100 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14103 int newline = (int)nd_fl_newline(node);
14104 switch (nd_type(node)) {
14110 *body = node = RNODE_BEGIN(node)->nd_body;
14111 if (newline && node) nd_set_fl_newline(node);
14114 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14118 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14121 body = &RNODE_CASE(node)->nd_body;
14124 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14127 body = &RNODE_ENSURE(node)->nd_head;
14130 newline = 0; // RESBODY should not be a NEWLINE
14131 if (RNODE_RESCUE(node)->nd_else) {
14132 body = &RNODE_RESCUE(node)->nd_resq;
14135 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14141 if (newline && node) nd_set_fl_newline(node);
14148is_static_content(NODE *node)
14150 if (!node) return 1;
14151 switch (nd_type(node)) {
14153 if (!(node = RNODE_HASH(node)->nd_head)) break;
14156 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14157 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14162 case NODE_ENCODING:
14165 case NODE_RATIONAL:
14166 case NODE_IMAGINARY:
14180assign_in_cond(struct parser_params *p, NODE *node)
14182 switch (nd_type(node)) {
14196 if (!get_nd_value(p, node)) return 1;
14197 if (is_static_content(get_nd_value(p, node))) {
14198 /* reports always */
14199 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14210#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14212 case COND_IN_OP: break; \
14213 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14214 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14218static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14221range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14223 enum node_type type;
14225 if (node == 0) return 0;
14227 type = nd_type(node);
14228 value_expr(p, node);
14229 if (type == NODE_INTEGER) {
14230 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14231 ID lineno = rb_intern("$.");
14232 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14234 return cond0(p, node, COND_IN_FF, loc, true);
14238cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14240 if (node == 0) return 0;
14241 if (!(node = nd_once_body(node))) return 0;
14242 assign_in_cond(p, node);
14244 switch (nd_type(node)) {
14246 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14253 SWITCH_BY_COND_TYPE(type, warn, "string ");
14257 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14258 nd_set_type(node, NODE_MATCH);
14262 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14264 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14268 NODE *end = RNODE_BLOCK(node)->nd_end;
14269 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14270 if (top) top = node == end;
14271 *expr = cond0(p, *expr, type, loc, top);
14277 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14278 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14284 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14285 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14286 switch (nd_type(node)) {
14288 nd_set_type(node,NODE_FLIP2);
14289 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14293 nd_set_type(node, NODE_FLIP3);
14294 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14302 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14306 case NODE_ENCODING:
14309 case NODE_RATIONAL:
14310 case NODE_IMAGINARY:
14311 SWITCH_BY_COND_TYPE(type, warning, "");
14321cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14323 if (node == 0) return 0;
14324 return cond0(p, node, COND_IN_COND, loc, true);
14328method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14330 if (node == 0) return 0;
14331 return cond0(p, node, COND_IN_OP, loc, true);
14335new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14337 YYLTYPE loc = {*pos, *pos};
14338 return NEW_NIL(&loc);
14342new_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)
14344 if (!cc) return right;
14345 cc = cond0(p, cc, COND_IN_COND, loc, true);
14346 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14350new_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)
14352 if (!cc) return right;
14353 cc = cond0(p, cc, COND_IN_COND, loc, true);
14354 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14357#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))
14360logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14361 const YYLTYPE *op_loc, const YYLTYPE *loc)
14363 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14365 value_expr(p, left);
14366 if (left && nd_type_p(left, type)) {
14367 NODE *node = left, *second;
14368 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14371 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14372 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14373 left->nd_loc.end_pos = loc->end_pos;
14376 op = NEW_AND_OR(type, left, right, loc, op_loc);
14377 nd_set_line(op, op_loc->beg_pos.lineno);
14384no_blockarg(struct parser_params *p, NODE *node)
14386 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14387 compile_error(p, "block argument should not be given");
14392ret_args(struct parser_params *p, NODE *node)
14395 no_blockarg(p, node);
14396 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14397 node = RNODE_LIST(node)->nd_head;
14404negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14406 switch (nd_type(node)) {
14408 RNODE_INTEGER(node)->minus = TRUE;
14411 RNODE_FLOAT(node)->minus = TRUE;
14413 case NODE_RATIONAL:
14414 RNODE_RATIONAL(node)->minus = TRUE;
14416 case NODE_IMAGINARY:
14417 RNODE_IMAGINARY(node)->minus = TRUE;
14420 node->nd_loc = *loc;
14425arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14428 if (!node1) return (NODE *)node2;
14429 node2->nd_head = node1;
14430 nd_set_first_lineno(node2, nd_first_lineno(node1));
14431 nd_set_first_column(node2, nd_first_column(node1));
14432 return (NODE *)node2;
14438args_info_empty_p(struct rb_args_info *args)
14440 if (args->pre_args_num) return false;
14441 if (args->post_args_num) return false;
14442 if (args->rest_arg) return false;
14443 if (args->opt_args) return false;
14444 if (args->block_arg) return false;
14445 if (args->kw_args) return false;
14446 if (args->kw_rest_arg) return false;
14450static rb_node_args_t *
14451new_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)
14453 struct rb_args_info *args = &tail->nd_ainfo;
14455 if (args->forwarding) {
14457 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14460 rest_arg = idFWD_REST;
14463 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14464 args->pre_init = pre_args ? pre_args->nd_next : 0;
14466 args->post_args_num = post_args ? post_args->nd_plen : 0;
14467 args->post_init = post_args ? post_args->nd_next : 0;
14468 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14470 args->rest_arg = rest_arg;
14472 args->opt_args = opt_args;
14474 nd_set_loc(RNODE(tail), loc);
14479static rb_node_args_t *
14480new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14482 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14483 struct rb_args_info *args = &node->nd_ainfo;
14484 if (p->error_p) return node;
14486 args->block_arg = block;
14487 args->kw_args = kw_args;
14491 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14492 * variable order: k1, kr1, k2, &b, internal_id, krest
14494 * variable order: kr1, k1, k2, internal_id, krest, &b
14496 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14497 struct vtable *vtargs = p->lvtbl->args;
14498 rb_node_kw_arg_t *kwn = kw_args;
14500 if (block) block = vtargs->tbl[vtargs->pos-1];
14501 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14502 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14504 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14506 --required_kw_vars;
14507 kwn = kwn->nd_next;
14510 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14511 ID vid = get_nd_vid(p, kwn->nd_body);
14512 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14513 *required_kw_vars++ = vid;
14520 arg_var(p, kw_bits);
14521 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14522 if (block) arg_var(p, block);
14524 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14526 else if (kw_rest_arg == idNil) {
14527 args->no_kwarg = 1;
14529 else if (kw_rest_arg) {
14530 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14536static rb_node_args_t *
14537args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14539 if (max_numparam > NO_PARAM || it_id) {
14541 YYLTYPE loc = RUBY_INIT_YYLLOC();
14542 args = new_args_tail(p, 0, 0, 0, 0);
14543 nd_set_loc(RNODE(args), &loc);
14545 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14551new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14553 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14556 NODE *pre_args = NEW_LIST(pre_arg, loc);
14557 if (RNODE_ARYPTN(aryptn)->pre_args) {
14558 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14561 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14568new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14571 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14576 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14582new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14584 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14590new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14592 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14593 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14594 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14600new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14602 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14607new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14609 NODE *node, *kw_rest_arg_node;
14611 if (kw_rest_arg == idNil) {
14612 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14614 else if (kw_rest_arg) {
14615 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14618 kw_rest_arg_node = NULL;
14621 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14627dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14630 return NEW_SYM(STR_NEW0(), loc);
14633 switch (nd_type(node)) {
14635 nd_set_type(node, NODE_DSYM);
14636 nd_set_loc(node, loc);
14639 node = str_to_sym_node(p, node, loc);
14642 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14649nd_type_st_key_enable_p(NODE *node)
14651 switch (nd_type(node)) {
14654 case NODE_RATIONAL:
14655 case NODE_IMAGINARY:
14661 case NODE_ENCODING:
14669nd_value(struct parser_params *p, NODE *node)
14671 switch (nd_type(node)) {
14673 return rb_node_str_string_val(node);
14675 return rb_node_integer_literal_val(node);
14677 return rb_node_float_literal_val(node);
14678 case NODE_RATIONAL:
14679 return rb_node_rational_literal_val(node);
14680 case NODE_IMAGINARY:
14681 return rb_node_imaginary_literal_val(node);
14683 return rb_node_sym_string_val(node);
14685 return rb_node_regx_string_val(node);
14687 return rb_node_line_lineno_val(node);
14688 case NODE_ENCODING:
14689 return rb_node_encoding_val(node);
14691 return rb_node_file_path_val(node);
14693 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14694 UNREACHABLE_RETURN(0);
14699warn_duplicate_keys(struct parser_params *p, NODE *hash)
14701 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14702 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14703 while (hash && RNODE_LIST(hash)->nd_next) {
14704 NODE *head = RNODE_LIST(hash)->nd_head;
14705 NODE *value = RNODE_LIST(hash)->nd_next;
14706 NODE *next = RNODE_LIST(value)->nd_next;
14710 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14715 if (nd_type_st_key_enable_p(head)) {
14716 key = (st_data_t)head;
14718 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14719 rb_warn2L(nd_line((NODE *)data),
14720 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14721 nd_value(p, head), WARN_I(nd_line(head)));
14723 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14727 st_free_table(p->warn_duplicate_keys_table);
14728 p->warn_duplicate_keys_table = NULL;
14732new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14734 if (hash) warn_duplicate_keys(p, hash);
14735 return NEW_HASH(hash, loc);
14739error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14741 if (is_private_local_id(p, id)) {
14744 if (st_is_member(p->pvtbl, id)) {
14745 yyerror1(loc, "duplicated variable name");
14747 else if (p->ctxt.in_alt_pattern && id) {
14748 yyerror1(loc, "variable capture in alternative pattern");
14751 p->ctxt.capture_in_pattern = 1;
14752 st_insert(p->pvtbl, (st_data_t)id, 0);
14757error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14760 p->pktbl = st_init_numtable();
14762 else if (st_is_member(p->pktbl, key)) {
14763 yyerror1(loc, "duplicated key name");
14766 st_insert(p->pktbl, (st_data_t)key, 0);
14770new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14772 return NEW_HASH(hash, loc);
14776new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14781 ID vid = get_nd_vid(p, lhs);
14782 YYLTYPE lhs_loc = lhs->nd_loc;
14784 set_nd_value(p, lhs, rhs);
14785 nd_set_loc(lhs, loc);
14786 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14788 else if (op == tANDOP) {
14789 set_nd_value(p, lhs, rhs);
14790 nd_set_loc(lhs, loc);
14791 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14795 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14796 set_nd_value(p, asgn, rhs);
14797 nd_set_loc(asgn, loc);
14801 asgn = NEW_ERROR(loc);
14807new_ary_op_assign(struct parser_params *p, NODE *ary,
14808 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14809 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14813 aryset_check(p, args);
14814 args = make_list(args, args_loc);
14815 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14821new_attr_op_assign(struct parser_params *p, NODE *lhs,
14822 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14823 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14827 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14833new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14838 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14841 asgn = NEW_ERROR(loc);
14848const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14850 if (p->ctxt.in_def) {
14852 yyerror1(loc, "dynamic constant assignment");
14854 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14857 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14862assign_error(struct parser_params *p, const char *mesg, VALUE a)
14864 a = dispatch2(assign_error, ERR_MESG(), a);
14871new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14873 NODE *result = head;
14875 NODE *tmp = rescue_else ? rescue_else : rescue;
14876 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14878 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14879 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14882 result = NEW_ENSURE(result, ensure, loc);
14884 fixpos(result, head);
14889warn_unused_var(struct parser_params *p, struct local_vars *local)
14893 if (!local->used) return;
14894 cnt = local->used->pos;
14895 if (cnt != local->vars->pos) {
14896 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14899 ID *v = local->vars->tbl;
14900 ID *u = local->used->tbl;
14901 for (int i = 0; i < cnt; ++i) {
14902 if (!v[i] || (u[i] & LVAR_USED)) continue;
14903 if (is_private_local_id(p, v[i])) continue;
14904 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14910local_push(struct parser_params *p, int toplevel_scope)
14912 struct local_vars *local;
14913 int inherits_dvars = toplevel_scope && compile_for_eval;
14914 int warn_unused_vars = RTEST(ruby_verbose);
14916 local = ALLOC(struct local_vars);
14917 local->prev = p->lvtbl;
14918 local->args = vtable_alloc(0);
14919 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14921 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14922 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14924 local->numparam.outer = 0;
14925 local->numparam.inner = 0;
14926 local->numparam.current = 0;
14928 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14930# if WARN_PAST_SCOPE
14939vtable_chain_free(struct parser_params *p, struct vtable *table)
14941 while (!DVARS_TERMINAL_P(table)) {
14942 struct vtable *cur_table = table;
14943 table = cur_table->prev;
14944 vtable_free(cur_table);
14949local_free(struct parser_params *p, struct local_vars *local)
14951 vtable_chain_free(p, local->used);
14953# if WARN_PAST_SCOPE
14954 vtable_chain_free(p, local->past);
14957 vtable_chain_free(p, local->args);
14958 vtable_chain_free(p, local->vars);
14960 ruby_sized_xfree(local, sizeof(struct local_vars));
14964local_pop(struct parser_params *p)
14966 struct local_vars *local = p->lvtbl->prev;
14967 if (p->lvtbl->used) {
14968 warn_unused_var(p, p->lvtbl);
14971 local_free(p, p->lvtbl);
14978static rb_ast_id_table_t *
14979local_tbl(struct parser_params *p)
14981 int cnt_args = vtable_size(p->lvtbl->args);
14982 int cnt_vars = vtable_size(p->lvtbl->vars);
14983 int cnt = cnt_args + cnt_vars;
14985 rb_ast_id_table_t *tbl;
14987 if (cnt <= 0) return 0;
14988 tbl = rb_ast_new_local_table(p->ast, cnt);
14989 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14990 /* remove IDs duplicated to warn shadowing */
14991 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14992 ID id = p->lvtbl->vars->tbl[i];
14993 if (!vtable_included(p->lvtbl->args, id)) {
14994 tbl->ids[j++] = id;
14998 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15005numparam_name(struct parser_params *p, ID id)
15007 if (!NUMPARAM_ID_P(id)) return;
15008 compile_error(p, "_%d is reserved for numbered parameter",
15009 NUMPARAM_ID_TO_IDX(id));
15013arg_var(struct parser_params *p, ID id)
15015 numparam_name(p, id);
15016 vtable_add(p->lvtbl->args, id);
15020local_var(struct parser_params *p, ID id)
15022 numparam_name(p, id);
15023 vtable_add(p->lvtbl->vars, id);
15024 if (p->lvtbl->used) {
15025 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15031rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15033 return rb_local_defined(id, iseq);
15038local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15040 struct vtable *vars, *args, *used;
15042 vars = p->lvtbl->vars;
15043 args = p->lvtbl->args;
15044 used = p->lvtbl->used;
15046 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15049 if (used) used = used->prev;
15052 if (vars && vars->prev == DVARS_INHERIT) {
15053 return rb_parser_local_defined(p, id, p->parent_iseq);
15055 else if (vtable_included(args, id)) {
15059 int i = vtable_included(vars, id);
15060 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15066local_id(struct parser_params *p, ID id)
15068 return local_id_ref(p, id, NULL);
15072check_forwarding_args(struct parser_params *p)
15074 if (local_id(p, idFWD_ALL)) return TRUE;
15075 compile_error(p, "unexpected ...");
15080add_forwarding_args(struct parser_params *p)
15082 arg_var(p, idFWD_REST);
15083 arg_var(p, idFWD_KWREST);
15084 arg_var(p, idFWD_BLOCK);
15085 arg_var(p, idFWD_ALL);
15089forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15091 bool conflict = false;
15093 struct vtable *vars, *args;
15095 vars = p->lvtbl->vars;
15096 args = p->lvtbl->args;
15098 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15099 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15104 bool found = false;
15105 if (vars && vars->prev == DVARS_INHERIT && !found) {
15106 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15107 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15110 found = (vtable_included(args, arg) &&
15111 !(all && vtable_included(args, all)));
15115 compile_error(p, "no anonymous %s parameter", var);
15117 else if (conflict) {
15118 compile_error(p, "anonymous %s parameter is also used within block", var);
15123new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15125 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15126 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15127 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15128 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15129 block->forwarding = TRUE;
15130 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15131 return arg_blk_pass(args, block);
15135numparam_push(struct parser_params *p)
15137 struct local_vars *local = p->lvtbl;
15138 NODE *inner = local->numparam.inner;
15139 if (!local->numparam.outer) {
15140 local->numparam.outer = local->numparam.current;
15142 local->numparam.inner = 0;
15143 local->numparam.current = 0;
15149numparam_pop(struct parser_params *p, NODE *prev_inner)
15151 struct local_vars *local = p->lvtbl;
15153 /* prefer first one */
15154 local->numparam.inner = prev_inner;
15156 else if (local->numparam.current) {
15157 /* current and inner are exclusive */
15158 local->numparam.inner = local->numparam.current;
15160 if (p->max_numparam > NO_PARAM) {
15161 /* current and outer are exclusive */
15162 local->numparam.current = local->numparam.outer;
15163 local->numparam.outer = 0;
15166 /* no numbered parameter */
15167 local->numparam.current = 0;
15172static const struct vtable *
15173dyna_push(struct parser_params *p)
15175 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15176 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15177 if (p->lvtbl->used) {
15178 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15180 return p->lvtbl->args;
15184dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15186 struct vtable *tmp = *vtblp;
15187 *vtblp = tmp->prev;
15188# if WARN_PAST_SCOPE
15189 if (p->past_scope_enabled) {
15190 tmp->prev = p->lvtbl->past;
15191 p->lvtbl->past = tmp;
15199dyna_pop_1(struct parser_params *p)
15201 struct vtable *tmp;
15203 if ((tmp = p->lvtbl->used) != 0) {
15204 warn_unused_var(p, p->lvtbl);
15205 p->lvtbl->used = p->lvtbl->used->prev;
15208 dyna_pop_vtable(p, &p->lvtbl->args);
15209 dyna_pop_vtable(p, &p->lvtbl->vars);
15213dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15215 while (p->lvtbl->args != lvargs) {
15217 if (!p->lvtbl->args) {
15218 struct local_vars *local = p->lvtbl->prev;
15219 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15227dyna_in_block(struct parser_params *p)
15229 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15234dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15236 struct vtable *vars, *args, *used;
15239 args = p->lvtbl->args;
15240 vars = p->lvtbl->vars;
15241 used = p->lvtbl->used;
15243 while (!DVARS_TERMINAL_P(vars)) {
15244 if (vtable_included(args, id)) {
15247 if ((i = vtable_included(vars, id)) != 0) {
15248 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15253 if (!vidrefp) used = 0;
15254 if (used) used = used->prev;
15257 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15258 return rb_dvar_defined(id, p->parent_iseq);
15266dvar_defined(struct parser_params *p, ID id)
15268 return dvar_defined_ref(p, id, NULL);
15272dvar_curr(struct parser_params *p, ID id)
15274 return (vtable_included(p->lvtbl->args, id) ||
15275 vtable_included(p->lvtbl->vars, id));
15279reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15282 "regexp encoding option '%c' differs from source encoding '%s'",
15283 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15287static rb_encoding *
15288find_enc(struct parser_params* p, const char *name)
15290 int idx = rb_enc_find_index(name);
15292 rb_bug("unknown encoding name: %s", name);
15295 return rb_enc_from_index(idx);
15298static rb_encoding *
15299kcode_to_enc(struct parser_params* p, int kcode)
15304 case ENC_ASCII8BIT:
15305 enc = rb_ascii8bit_encoding();
15308 enc = find_enc(p, "EUC-JP");
15310 case ENC_Windows_31J:
15311 enc = find_enc(p, "Windows-31J");
15314 enc = rb_utf8_encoding();
15325rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15327 int c = RE_OPTION_ENCODING_IDX(options);
15333 char_to_option_kcode(c, &opt, &idx);
15334 enc = kcode_to_enc(p, idx);
15335 if (enc != rb_parser_str_get_encoding(str) &&
15336 !rb_parser_is_ascii_string(p, str)) {
15339 rb_parser_string_set_encoding(str, enc);
15341 else if (RE_OPTION_ENCODING_NONE(options)) {
15342 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15343 !rb_parser_is_ascii_string(p, str)) {
15347 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15349 else if (rb_is_usascii_enc(p->enc)) {
15350 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15360reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15362 int c = rb_reg_fragment_setenc(p, str, options);
15363 if (c) reg_fragment_enc_error(p, str, c);
15366#ifndef UNIVERSAL_PARSER
15368 struct parser_params* parser;
15371 const YYLTYPE *loc;
15372 rb_parser_assignable_func assignable;
15373} reg_named_capture_assign_t;
15376reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15377 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15379 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15380 struct parser_params* p = arg->parser;
15381 rb_encoding *enc = arg->enc;
15382 long len = name_end - name;
15383 const char *s = (const char *)name;
15385 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15389reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15391 reg_named_capture_assign_t arg;
15394 arg.enc = rb_enc_get(regexp);
15395 arg.succ_block = 0;
15397 arg.assignable = assignable;
15398 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15400 if (!arg.succ_block) return 0;
15401 return RNODE_BLOCK(arg.succ_block)->nd_next;
15407rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15409 return assignable(p, id, val, loc);
15413rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15414 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15419 if (!len) return ST_CONTINUE;
15420 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15421 return ST_CONTINUE;
15423 var = intern_cstr(s, len, enc);
15424 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15425 if (!lvar_defined(p, var)) return ST_CONTINUE;
15427 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15428 succ = *succ_block;
15429 if (!succ) succ = NEW_ERROR(loc);
15430 succ = block_append(p, succ, node);
15431 *succ_block = succ;
15432 return ST_CONTINUE;
15437parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15440 reg_fragment_setenc(p, str, options);
15441 str2 = rb_str_new_parser_string(str);
15442 return rb_parser_reg_compile(p, str2, options);
15447rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15449 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15454reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15459 err = rb_errinfo();
15460 re = parser_reg_compile(p, str, options);
15462 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15463 rb_set_errinfo(err);
15464 compile_error(p, "%"PRIsVALUE, m);
15472rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15474 p->do_print = print;
15476 p->do_chomp = chomp;
15477 p->do_split = split;
15481parser_append_options(struct parser_params *p, NODE *node)
15483 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15484 const YYLTYPE *const LOC = &default_location;
15487 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15488 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15490 node = block_append(p, node, print);
15494 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15497 ID ifs = rb_intern("$;");
15498 ID fields = rb_intern("$F");
15499 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15500 NODE *split = NEW_GASGN(fields,
15501 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15502 rb_intern("split"), args, LOC),
15504 node = block_append(p, split, node);
15507 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15508 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15509 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15512 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15521 /* just to suppress unused-function warnings */
15527internal_id(struct parser_params *p)
15529 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15531#endif /* !RIPPER */
15534parser_initialize(struct parser_params *p)
15536 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15537 p->command_start = TRUE;
15538 p->ruby_sourcefile_string = Qnil;
15539 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15540 string_buffer_init(p);
15542 p->delayed.token = NULL;
15543 p->frozen_string_literal = -1; /* not specified */
15545 p->error_buffer = Qfalse;
15546 p->end_expect_token_locations = NULL;
15551 p->parsing_thread = Qnil;
15553 p->s_lvalue = Qnil;
15554 p->s_value_stack = rb_ary_new();
15556 p->debug_buffer = Qnil;
15557 p->debug_output = rb_ractor_stdout();
15558 p->enc = rb_utf8_encoding();
15563#define rb_ruby_parser_mark ripper_parser_mark
15564#define rb_ruby_parser_free ripper_parser_free
15565#define rb_ruby_parser_memsize ripper_parser_memsize
15569rb_ruby_parser_mark(void *ptr)
15571 struct parser_params *p = (struct parser_params*)ptr;
15573 rb_gc_mark(p->ruby_sourcefile_string);
15575 rb_gc_mark(p->error_buffer);
15577 rb_gc_mark(p->value);
15578 rb_gc_mark(p->result);
15579 rb_gc_mark(p->parsing_thread);
15580 rb_gc_mark(p->s_value);
15581 rb_gc_mark(p->s_lvalue);
15582 rb_gc_mark(p->s_value_stack);
15584 rb_gc_mark(p->debug_buffer);
15585 rb_gc_mark(p->debug_output);
15589rb_ruby_parser_free(void *ptr)
15591 struct parser_params *p = (struct parser_params*)ptr;
15592 struct local_vars *local, *prev;
15595 rb_ast_free(p->ast);
15598 if (p->warn_duplicate_keys_table) {
15599 st_free_table(p->warn_duplicate_keys_table);
15604 rb_parser_ary_free(p, p->tokens);
15609 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15612 for (local = p->lvtbl; local; local = prev) {
15613 prev = local->prev;
15614 local_free(p, local);
15618 token_info *ptinfo;
15619 while ((ptinfo = p->token_info) != 0) {
15620 p->token_info = ptinfo->next;
15624 string_buffer_free(p);
15627 st_free_table(p->pvtbl);
15630 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15631 st_free_table(p->case_labels);
15634 xfree(p->lex.strterm);
15635 p->lex.strterm = 0;
15641rb_ruby_parser_memsize(const void *ptr)
15643 struct parser_params *p = (struct parser_params*)ptr;
15644 struct local_vars *local;
15645 size_t size = sizeof(*p);
15648 for (local = p->lvtbl; local; local = local->prev) {
15649 size += sizeof(*local);
15650 if (local->vars) size += local->vars->capa * sizeof(ID);
15656#undef rb_reserved_word
15658const struct kwtable *
15659rb_reserved_word(const char *str, unsigned int len)
15661 return reserved_word(str, len);
15664#ifdef UNIVERSAL_PARSER
15666rb_ruby_parser_allocate(const rb_parser_config_t *config)
15668 /* parser_initialize expects fields to be set to 0 */
15669 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15670 p->config = config;
15675rb_ruby_parser_new(const rb_parser_config_t *config)
15677 /* parser_initialize expects fields to be set to 0 */
15678 rb_parser_t *p = rb_ruby_parser_allocate(config);
15679 parser_initialize(p);
15684rb_ruby_parser_allocate(void)
15686 /* parser_initialize expects fields to be set to 0 */
15687 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15692rb_ruby_parser_new(void)
15694 /* parser_initialize expects fields to be set to 0 */
15695 rb_parser_t *p = rb_ruby_parser_allocate();
15696 parser_initialize(p);
15702rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15704 p->error_buffer = main ? Qfalse : Qnil;
15705 p->parent_iseq = base;
15710rb_ruby_parser_set_script_lines(rb_parser_t *p)
15712 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15716rb_ruby_parser_error_tolerant(rb_parser_t *p)
15718 p->error_tolerant = 1;
15722rb_ruby_parser_keep_tokens(rb_parser_t *p)
15724 p->keep_tokens = 1;
15725 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15729rb_ruby_parser_encoding(rb_parser_t *p)
15735rb_ruby_parser_end_seen_p(rb_parser_t *p)
15737 return p->ruby__end__seen;
15741rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15746#endif /* !RIPPER */
15750rb_ruby_parser_get_yydebug(rb_parser_t *p)
15756rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15762rb_ruby_parser_error_p(rb_parser_t *p)
15768rb_ruby_parser_debug_output(rb_parser_t *p)
15770 return p->debug_output;
15774rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15776 p->debug_output = output;
15780rb_ruby_parser_parsing_thread(rb_parser_t *p)
15782 return p->parsing_thread;
15786rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15788 p->parsing_thread = parsing_thread;
15792rb_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)
15794 p->lex.gets = gets;
15795 p->lex.input = input;
15797 p->ruby_sourcefile_string = sourcefile_string;
15798 p->ruby_sourcefile = sourcefile;
15799 p->ruby_sourceline = sourceline;
15803rb_ruby_parser_result(rb_parser_t *p)
15809rb_ruby_parser_enc(rb_parser_t *p)
15815rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15817 return p->ruby_sourcefile_string;
15821rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15823 return p->ruby_sourceline;
15827rb_ruby_parser_lex_state(rb_parser_t *p)
15829 return p->lex.state;
15833rb_ruby_ripper_parse0(rb_parser_t *p)
15836 p->ast = rb_ast_new();
15837 ripper_yyparse((void*)p);
15838 rb_ast_free(p->ast);
15841 p->eval_tree_begin = 0;
15845rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15847 return dedent_string(p, string, width);
15851rb_ruby_ripper_initialized_p(rb_parser_t *p)
15853 return p->lex.input != 0;
15857rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15859 parser_initialize(p);
15863rb_ruby_ripper_column(rb_parser_t *p)
15865 return p->lex.ptok - p->lex.pbeg;
15869rb_ruby_ripper_token_len(rb_parser_t *p)
15871 return p->lex.pcur - p->lex.ptok;
15874rb_parser_string_t *
15875rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15877 return p->lex.lastline;
15881rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15883 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15886#ifdef UNIVERSAL_PARSER
15888rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15890 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15891 p->config = config;
15896struct parser_params*
15897rb_ruby_ripper_parser_allocate(void)
15899 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15905rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15908 VALUE mesg = p->debug_buffer;
15910 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15912 rb_str_vcatf(mesg, fmt, ap);
15914 if (char_at_end(p, mesg, 0) == '\n') {
15915 rb_io_write(p->debug_output, mesg);
15916 p->debug_buffer = Qnil;
15921parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15924 int lineno, column;
15927 lineno = loc->end_pos.lineno;
15928 column = loc->end_pos.column;
15931 lineno = p->ruby_sourceline;
15932 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15935 rb_io_flush(p->debug_output);
15939 rb_syntax_error_append(p->error_buffer,
15940 p->ruby_sourcefile_string,
15947count_char(const char *str, int c)
15950 while (str[n] == c) ++n;
15955 * strip enclosing double-quotes, same as the default yytnamerr except
15956 * for that single-quotes matching back-quotes do not stop stripping.
15958 * "\"`class' keyword\"" => "`class' keyword"
15961rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15963 if (*yystr == '"') {
15964 size_t yyn = 0, bquote = 0;
15965 const char *yyp = yystr;
15971 bquote = count_char(yyp+1, '\'') + 1;
15972 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15978 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15979 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15985 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15986 if (yyres) memcpy(yyres + yyn, yyp, 3);
15991 goto do_not_strip_quotes;
15995 goto do_not_strip_quotes;
15998 if (*++yyp != '\\')
15999 goto do_not_strip_quotes;
16000 /* Fall through. */
16014 do_not_strip_quotes: ;
16017 if (!yyres) return strlen(yystr);
16019 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16024#define validate(x) (void)(x)
16027ripper_dispatch0(struct parser_params *p, ID mid)
16029 return rb_funcall(p->value, mid, 0);
16033ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16036 return rb_funcall(p->value, mid, 1, a);
16040ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16044 return rb_funcall(p->value, mid, 2, a, b);
16048ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16053 return rb_funcall(p->value, mid, 3, a, b, c);
16057ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16063 return rb_funcall(p->value, mid, 4, a, b, c, d);
16067ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16074 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16078ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16087 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16091ripper_error(struct parser_params *p)
16097ripper_value(struct parser_params *p)
16099 (void)yystpcpy; /* may not used in newer bison */
16108 * c-file-style: "ruby"