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;}
1431static bool args_info_empty_p(struct rb_args_info *args);
1432static 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*);
1433static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1434static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1435static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1436static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1437static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1438static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1439static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1441static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1442static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1444static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*);
1445static void no_blockarg(struct parser_params*,NODE*);
1446static NODE *ret_args(struct parser_params*,NODE*);
1447static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1448static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1450static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1451static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1453static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1454static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1456static VALUE rb_backref_error(struct parser_params*,NODE*);
1457static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1459static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1460static 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);
1461static 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);
1462static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1463static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1465static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1467static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1468static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1470static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1471static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1473static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1475static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *);
1477#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1479static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1481static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1483static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1485static rb_ast_id_table_t *local_tbl(struct parser_params*);
1487static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1488static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1490static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1491static NODE *heredoc_dedent(struct parser_params*,NODE*);
1493static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1495static 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);
1498#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1499#define set_value(val) (p->s_lvalue = val)
1500static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1501static int id_is_var(struct parser_params *p, ID id);
1504RUBY_SYMBOL_EXPORT_BEGIN
1505VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1506int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1507enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1508VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1509void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1510PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1511YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1512YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1513YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1514YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1515YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1516YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1517void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1518RUBY_SYMBOL_EXPORT_END
1520static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1521static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1522static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1523static VALUE formal_argument_error(struct parser_params*, ID);
1524static ID shadowing_lvar(struct parser_params*,ID);
1525static void new_bv(struct parser_params*,ID);
1527static void local_push(struct parser_params*,int);
1528static void local_pop(struct parser_params*);
1529static void local_var(struct parser_params*, ID);
1530static void arg_var(struct parser_params*, ID);
1531static int local_id(struct parser_params *p, ID id);
1532static int local_id_ref(struct parser_params*, ID, ID **);
1533#define internal_id rb_parser_internal_id
1534ID internal_id(struct parser_params*);
1535static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1536static int check_forwarding_args(struct parser_params*);
1537static void add_forwarding_args(struct parser_params *p);
1538static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1540static const struct vtable *dyna_push(struct parser_params *);
1541static void dyna_pop(struct parser_params*, const struct vtable *);
1542static int dyna_in_block(struct parser_params*);
1543#define dyna_var(p, id) local_var(p, id)
1544static int dvar_defined(struct parser_params*, ID);
1545#define dvar_defined_ref rb_parser_dvar_defined_ref
1546int dvar_defined_ref(struct parser_params*, ID, ID**);
1547static int dvar_curr(struct parser_params*,ID);
1549static int lvar_defined(struct parser_params*, ID);
1551static NODE *numparam_push(struct parser_params *p);
1552static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1554#define METHOD_NOT '!'
1556#define idFWD_REST '*'
1557#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1558#define idFWD_BLOCK '&'
1559#define idFWD_ALL idDot3
1560#define arg_FWD_BLOCK idFWD_BLOCK
1562#define RE_ONIG_OPTION_IGNORECASE 1
1563#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1564#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1565#define RE_OPTION_ONCE (1<<16)
1566#define RE_OPTION_ENCODING_SHIFT 8
1567#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1568#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1569#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1570#define RE_OPTION_MASK 0xff
1571#define RE_OPTION_ARG_ENCODING_NONE 32
1573#define CHECK_LITERAL_WHEN (st_table *)1
1574#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1576#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1577RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1579#define TOKEN2ID(tok) ( \
1580 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1581 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1582 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1583 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1584 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1585 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1586 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1588/****** Ripper *******/
1592#include "eventids1.h"
1593#include "eventids2.h"
1595extern const struct ripper_parser_ids ripper_parser_ids;
1597static VALUE ripper_dispatch0(struct parser_params*,ID);
1598static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1599static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1600static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1601static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1602static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1603static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1604void ripper_error(struct parser_params *p);
1606#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1607#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1608#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1609#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1610#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1611#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1612#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1614#define yyparse ripper_yyparse
1617aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1619 if (!NIL_P(pre_arg)) {
1620 if (!NIL_P(pre_args)) {
1621 rb_ary_unshift(pre_args, pre_arg);
1624 pre_args = rb_ary_new_from_args(1, pre_arg);
1630#define ID2VAL(id) STATIC_ID2SYM(id)
1631#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1634#define KWD2EID(t, v) keyword_##t
1637new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc)
1639 body = remove_begin(body);
1640 reduce_nodes(p, &body);
1641 NODE *n = NEW_SCOPE(args, body, parent, loc);
1642 nd_set_line(n, loc->end_pos.lineno);
1643 set_line_body(body, loc->beg_pos.lineno);
1648rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1649 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1651 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1652 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1653 loc.beg_pos = arg_loc->beg_pos;
1654 return NEW_RESCUE(arg, rescue, 0, &loc);
1657static NODE *add_block_exit(struct parser_params *p, NODE *node);
1658static rb_node_exits_t *init_block_exit(struct parser_params *p);
1659static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1660static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1661static void clear_block_exit(struct parser_params *p, bool error);
1664next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1666 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1670restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1672 /* See: def_name action */
1673 struct lex_context ctxt = temp->save.ctxt;
1674 p->ctxt.in_def = ctxt.in_def;
1675 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1676 p->ctxt.in_rescue = ctxt.in_rescue;
1677 p->max_numparam = temp->save.max_numparam;
1678 numparam_pop(p, temp->save.numparam_save);
1679 clear_block_exit(p, true);
1683endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1685 if (is_attrset_id(mid)) {
1686 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1688 token_info_drop(p, "def", loc->beg_pos);
1691#define debug_token_line(p, name, line) do { \
1693 const char *const pcur = p->lex.pcur; \
1694 const char *const ptok = p->lex.ptok; \
1695 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1696 line, p->ruby_sourceline, \
1697 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1701#define begin_definition(k, loc_beg, loc_end) \
1703 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1704 /* singleton class */ \
1705 p->ctxt.cant_return = !p->ctxt.in_def; \
1706 p->ctxt.in_def = 0; \
1708 else if (p->ctxt.in_def) { \
1709 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1710 yyerror1(&loc, k " definition in method body"); \
1713 p->ctxt.cant_return = 1; \
1719# define ifndef_ripper(x) (x)
1720# define ifdef_ripper(r,x) (x)
1722# define ifndef_ripper(x)
1723# define ifdef_ripper(r,x) (r)
1726# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1727# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1728# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1729# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1730# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1731# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1732# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1733# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1734# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1735# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1736# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1737# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1738# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1739# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1740# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1741# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1742# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1743# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1744# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1745# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1747extern const ID id_warn, id_warning, id_gets, id_assoc;
1748# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1749# define WARN_S_L(s,l) STR_NEW(s,l)
1750# define WARN_S(s) STR_NEW2(s)
1751# define WARN_I(i) INT2NUM(i)
1752# define WARN_ID(i) rb_id2str(i)
1753# define PRIsWARN PRIsVALUE
1754# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1755# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1756# ifdef HAVE_VA_ARGS_MACRO
1757# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1759# define WARN_CALL rb_funcall
1761# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1762# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1763# ifdef HAVE_VA_ARGS_MACRO
1764# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1766# define WARNING_CALL rb_funcall
1768# define compile_error ripper_compile_error
1770# define WARN_S_L(s,l) s
1773# define WARN_ID(i) rb_id2name(i)
1774# define PRIsWARN PRIsVALUE
1775# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1776# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1777# define WARN_CALL rb_compile_warn
1778# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1779# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1780# define WARNING_CALL rb_compile_warning
1781PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1782# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1785#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1788add_block_exit(struct parser_params *p, NODE *node)
1791 compile_error(p, "unexpected null node");
1794 switch (nd_type(node)) {
1795 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1797 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1800 if (!p->ctxt.in_defined) {
1801 rb_node_exits_t *exits = p->exits;
1803 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1804 exits->nd_stts = node;
1810static rb_node_exits_t *
1811init_block_exit(struct parser_params *p)
1813 rb_node_exits_t *old = p->exits;
1814 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1815 exits->nd_chain = 0;
1816 exits->nd_stts = RNODE(exits);
1821static rb_node_exits_t *
1822allow_block_exit(struct parser_params *p)
1824 rb_node_exits_t *exits = p->exits;
1830restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1836clear_block_exit(struct parser_params *p, bool error)
1838 rb_node_exits_t *exits = p->exits;
1841 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1842 switch (nd_type(e)) {
1844 yyerror1(&e->nd_loc, "Invalid break");
1847 yyerror1(&e->nd_loc, "Invalid next");
1850 yyerror1(&e->nd_loc, "Invalid redo");
1853 yyerror1(&e->nd_loc, "unexpected node");
1854 goto end_checks; /* no nd_chain */
1859 exits->nd_stts = RNODE(exits);
1860 exits->nd_chain = 0;
1863#define WARN_EOL(tok) \
1864 (looking_at_eol_p(p) ? \
1865 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1867static int looking_at_eol_p(struct parser_params *p);
1870get_nd_value(struct parser_params *p, NODE *node)
1872 switch (nd_type(node)) {
1874 return RNODE_GASGN(node)->nd_value;
1876 return RNODE_IASGN(node)->nd_value;
1878 return RNODE_LASGN(node)->nd_value;
1880 return RNODE_DASGN(node)->nd_value;
1882 return RNODE_MASGN(node)->nd_value;
1884 return RNODE_CVASGN(node)->nd_value;
1886 return RNODE_CDECL(node)->nd_value;
1888 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1894set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1896 switch (nd_type(node)) {
1898 RNODE_CDECL(node)->nd_value = rhs;
1901 RNODE_GASGN(node)->nd_value = rhs;
1904 RNODE_IASGN(node)->nd_value = rhs;
1907 RNODE_LASGN(node)->nd_value = rhs;
1910 RNODE_DASGN(node)->nd_value = rhs;
1913 RNODE_MASGN(node)->nd_value = rhs;
1916 RNODE_CVASGN(node)->nd_value = rhs;
1919 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1925get_nd_vid(struct parser_params *p, NODE *node)
1927 switch (nd_type(node)) {
1929 return RNODE_CDECL(node)->nd_vid;
1931 return RNODE_GASGN(node)->nd_vid;
1933 return RNODE_IASGN(node)->nd_vid;
1935 return RNODE_LASGN(node)->nd_vid;
1937 return RNODE_DASGN(node)->nd_vid;
1939 return RNODE_CVASGN(node)->nd_vid;
1941 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1947get_nd_args(struct parser_params *p, NODE *node)
1949 switch (nd_type(node)) {
1951 return RNODE_CALL(node)->nd_args;
1953 return RNODE_OPCALL(node)->nd_args;
1955 return RNODE_FCALL(node)->nd_args;
1957 return RNODE_QCALL(node)->nd_args;
1959 return RNODE_SUPER(node)->nd_args;
1968 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1974djb2(const uint8_t *str, size_t len)
1976 st_index_t hash = 5381;
1978 for (size_t i = 0; i < len; i++) {
1979 hash = ((hash << 5) + hash) + str[i];
1986parser_memhash(const void *ptr, long len)
1988 return djb2(ptr, len);
1991#define PARSER_STRING_PTR(str) (str->ptr)
1992#define PARSER_STRING_LEN(str) (str->len)
1993#define PARSER_STRING_END(str) (&str->ptr[str->len])
1994#define STRING_SIZE(str) ((size_t)str->len + 1)
1995#define STRING_TERM_LEN(str) (1)
1996#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1997#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1998 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
2001#define STRING_SET_LEN(str, n) do { \
2004#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2005 ((ptrvar) = str->ptr, \
2006 (lenvar) = str->len)
2009parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2011 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2014static rb_parser_string_t *
2015rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2017 rb_parser_string_t *str;
2020 rb_bug("negative string size (or size too big): %ld", len);
2023 str = xcalloc(1, sizeof(rb_parser_string_t));
2024 str->ptr = xcalloc(len + 1, sizeof(char));
2027 memcpy(PARSER_STRING_PTR(str), ptr, len);
2029 STRING_SET_LEN(str, len);
2030 STRING_TERM_FILL(str);
2034static rb_parser_string_t *
2035rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2037 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2038 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2045rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2048 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2054rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2057 xfree(PARSER_STRING_PTR(str));
2063rb_parser_str_hash(rb_parser_string_t *str)
2065 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2069rb_char_p_hash(const char *c)
2071 return parser_memhash((const void *)c, strlen(c));
2075rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2077 return PARSER_STRING_LEN(str);
2082rb_parser_string_end(rb_parser_string_t *str)
2084 return &str->ptr[str->len];
2089rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2095rb_parser_str_get_encoding(rb_parser_string_t *str)
2102PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2104 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2109PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2111 return str->coderange;
2115PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2117 str->coderange = coderange;
2121PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2123 rb_parser_string_set_encoding(str, enc);
2124 PARSER_ENC_CODERANGE_SET(str, cr);
2128PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2130 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2134PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2136 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2140PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2142 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2146rb_parser_search_nonascii(const char *p, const char *e)
2150 for (; s < e; s++) {
2151 if (*s & 0x80) return s;
2158rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2160 const char *e = ptr + len;
2162 if (enc == rb_ascii8bit_encoding()) {
2163 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2164 ptr = rb_parser_search_nonascii(ptr, e);
2165 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2168 /* parser string encoding is always asciicompat */
2169 ptr = rb_parser_search_nonascii(ptr, e);
2170 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2172 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2173 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2174 ptr += MBCLEN_CHARFOUND_LEN(ret);
2175 if (ptr == e) break;
2176 ptr = rb_parser_search_nonascii(ptr, e);
2180 return RB_PARSER_ENC_CODERANGE_VALID;
2184rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2186 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2190rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2192 int cr = PARSER_ENC_CODERANGE(str);
2194 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2195 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2196 PARSER_ENC_CODERANGE_SET(str, cr);
2202static rb_parser_string_t *
2203rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2205 if (rb_parser_str_get_encoding(str) == enc)
2207 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2208 PARSER_ENC_CODERANGE_CLEAR(str);
2210 rb_parser_string_set_encoding(str, enc);
2215rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2217 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2221rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2223 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2224 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2226 if (enc1 == NULL || enc2 == NULL)
2233 if (PARSER_STRING_LEN(str2) == 0)
2235 if (PARSER_STRING_LEN(str1) == 0)
2236 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2240 cr1 = rb_parser_enc_str_coderange(p, str1);
2241 cr2 = rb_parser_enc_str_coderange(p, str2);
2244 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2245 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2248 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2252 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2260rb_parser_str_modify(rb_parser_string_t *str)
2262 PARSER_ENC_CODERANGE_CLEAR(str);
2266rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2269 const int termlen = STRING_TERM_LEN(str);
2271 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2272 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2275 int cr = PARSER_ENC_CODERANGE(str);
2276 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2277 /* Leave unknown. */
2279 else if (len > PARSER_STRING_LEN(str)) {
2280 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2282 else if (len < PARSER_STRING_LEN(str)) {
2283 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2284 /* ASCII-only string is keeping after truncated. Valid
2285 * and broken may be invalid or valid, leave unknown. */
2286 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2290 STRING_SET_LEN(str, len);
2291 STRING_TERM_FILL(str);
2294static rb_parser_string_t *
2295rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2297 rb_parser_str_modify(str);
2298 if (len == 0) return 0;
2300 long total, olen, off = -1;
2302 const int termlen = STRING_TERM_LEN(str);
2304 PARSER_STRING_GETMEM(str, sptr, olen);
2305 if (ptr >= sptr && ptr <= sptr + olen) {
2309 if (olen > LONG_MAX - len) {
2310 compile_error(p, "string sizes too big");
2314 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2315 sptr = PARSER_STRING_PTR(str);
2319 memcpy(sptr + olen, ptr, len);
2320 STRING_SET_LEN(str, total);
2321 STRING_TERM_FILL(str);
2326#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2327#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2329static rb_parser_string_t *
2330rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2331 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2334 rb_encoding *str_enc, *res_enc;
2336 str_enc = rb_parser_str_get_encoding(str);
2337 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2339 if (str_enc == ptr_enc) {
2340 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2341 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2345 /* parser string encoding is always asciicompat */
2346 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2347 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2349 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2350 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2351 str_cr = rb_parser_enc_str_coderange(p, str);
2356 *ptr_cr_ret = ptr_cr;
2358 if (str_enc != ptr_enc &&
2359 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2360 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2364 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2366 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2368 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2369 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2371 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2378 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2380 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2385 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2388 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2392 compile_error(p, "negative string size (or size too big)");
2394 parser_str_cat(str, ptr, len);
2395 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2399 compile_error(p, "incompatible character encodings: %s and %s",
2400 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2401 UNREACHABLE_RETURN(0);
2405static rb_parser_string_t *
2406rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2407 rb_encoding *ptr_enc)
2409 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2412static rb_parser_string_t *
2413rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2415 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2417 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2418 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2420 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2425static rb_parser_string_t *
2426rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2429 rb_bug("negative string size (or size too big)");
2432 long slen = PARSER_STRING_LEN(str);
2434 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2435 PARSER_ENC_CODERANGE_CLEAR(str);
2440 const int termlen = STRING_TERM_LEN(str);
2442 if ((capa = slen) < len) {
2443 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2445 else if (len == slen) return str;
2446 STRING_SET_LEN(str, len);
2447 STRING_TERM_FILL(str);
2452# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2453 ((ptrvar) = str->ptr, \
2454 (lenvar) = str->len, \
2455 (encvar) = str->enc)
2458rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2461 const char *ptr1, *ptr2;
2462 rb_encoding *enc1, *enc2;
2464 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2465 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2467 return (len1 != len2 ||
2469 memcmp(ptr1, ptr2, len1) != 0);
2473rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2476 if (ary->capa < len) {
2478 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2479 for (i = ary->len; i < len; i++) {
2486 * Do not call this directly.
2487 * Use rb_parser_ary_new_capa_for_XXX() instead.
2489static rb_parser_ary_t *
2490parser_ary_new_capa(rb_parser_t *p, long len)
2493 rb_bug("negative array size (or size too big): %ld", len);
2495 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2500 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2509static rb_parser_ary_t *
2510rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2512 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2513 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2517static rb_parser_ary_t *
2518rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2520 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2521 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2526static rb_parser_ary_t *
2527rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2529 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2530 ary->data_type = PARSER_ARY_DATA_NODE;
2535 * Do not call this directly.
2536 * Use rb_parser_ary_push_XXX() instead.
2538static rb_parser_ary_t *
2539parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2541 if (ary->len == ary->capa) {
2542 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2544 ary->data[ary->len++] = val;
2549static rb_parser_ary_t *
2550rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2552 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2553 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2555 return parser_ary_push(p, ary, val);
2558static rb_parser_ary_t *
2559rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2561 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2562 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2564 return parser_ary_push(p, ary, val);
2568static rb_parser_ary_t *
2569rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2571 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2572 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2574 return parser_ary_push(p, ary, val);
2579rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2582 rb_parser_string_free(p, token->str);
2587rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2589# define foreach_ary(ptr) \
2590 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2591 ptr < end_ary_data; ptr++)
2592 switch (ary->data_type) {
2593 case PARSER_ARY_DATA_AST_TOKEN:
2594 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2596 case PARSER_ARY_DATA_SCRIPT_LINE:
2597 foreach_ary(data) {rb_parser_string_free(p, *data);}
2599 case PARSER_ARY_DATA_NODE:
2600 /* Do nothing because nodes are freed when rb_ast_t is freed */
2603 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2616%define parse.error verbose
2618 if ((NODE *)$$ == (NODE *)-1) {
2619 rb_parser_printf(p, "NODE_SPECIAL");
2622 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2624} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2625 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2627 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2630 switch (nd_type(RNODE($$))) {
2632 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2635 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2638 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2640 case NODE_IMAGINARY:
2641 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2646} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2648 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2651 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2655 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2658%lex-param {struct parser_params *p}
2659%parse-param {struct parser_params *p}
2662 RUBY_SET_YYLLOC_OF_NONE(@$);
2664%after-shift after_shift
2665%before-reduce before_reduce
2666%after-reduce after_reduce
2667%after-shift-error-token after_shift_error_token
2668%after-pop-stack after_pop_stack
2672 rb_node_fcall_t *node_fcall;
2673 rb_node_args_t *node_args;
2674 rb_node_args_aux_t *node_args_aux;
2675 rb_node_opt_arg_t *node_opt_arg;
2676 rb_node_kw_arg_t *node_kw_arg;
2677 rb_node_block_pass_t *node_block_pass;
2678 rb_node_masgn_t *node_masgn;
2679 rb_node_def_temp_t *node_def_temp;
2680 rb_node_exits_t *node_exits;
2681 struct rb_locations_lambda_body_t *locations_lambda_body;
2686 const struct vtable *vars;
2687 struct rb_strterm_struct *strterm;
2688 struct lex_context ctxt;
2689 enum lex_state_e state;
2693 keyword_class "'class'"
2694 keyword_module "'module'"
2696 keyword_undef "'undef'"
2697 keyword_begin "'begin'"
2698 keyword_rescue "'rescue'"
2699 keyword_ensure "'ensure'"
2702 keyword_unless "'unless'"
2703 keyword_then "'then'"
2704 keyword_elsif "'elsif'"
2705 keyword_else "'else'"
2706 keyword_case "'case'"
2707 keyword_when "'when'"
2708 keyword_while "'while'"
2709 keyword_until "'until'"
2711 keyword_break "'break'"
2712 keyword_next "'next'"
2713 keyword_redo "'redo'"
2714 keyword_retry "'retry'"
2717 keyword_do_cond "'do' for condition"
2718 keyword_do_block "'do' for block"
2719 keyword_do_LAMBDA "'do' for lambda"
2720 keyword_return "'return'"
2721 keyword_yield "'yield'"
2722 keyword_super "'super'"
2723 keyword_self "'self'"
2725 keyword_true "'true'"
2726 keyword_false "'false'"
2730 modifier_if "'if' modifier"
2731 modifier_unless "'unless' modifier"
2732 modifier_while "'while' modifier"
2733 modifier_until "'until' modifier"
2734 modifier_rescue "'rescue' modifier"
2735 keyword_alias "'alias'"
2736 keyword_defined "'defined?'"
2737 keyword_BEGIN "'BEGIN'"
2739 keyword__LINE__ "'__LINE__'"
2740 keyword__FILE__ "'__FILE__'"
2741 keyword__ENCODING__ "'__ENCODING__'"
2743%token <id> tIDENTIFIER "local variable or method"
2744%token <id> tFID "method"
2745%token <id> tGVAR "global variable"
2746%token <id> tIVAR "instance variable"
2747%token <id> tCONSTANT "constant"
2748%token <id> tCVAR "class variable"
2749%token <id> tLABEL "label"
2750%token <node> tINTEGER "integer literal"
2751%token <node> tFLOAT "float literal"
2752%token <node> tRATIONAL "rational literal"
2753%token <node> tIMAGINARY "imaginary literal"
2754%token <node> tCHAR "char literal"
2755%token <node> tNTH_REF "numbered reference"
2756%token <node> tBACK_REF "back reference"
2757%token <node> tSTRING_CONTENT "literal content"
2758%token <num> tREGEXP_END
2759%token <num> tDUMNY_END "dummy end"
2761%type <node> singleton singleton_expr strings string string1 xstring regexp
2762%type <node> string_contents xstring_contents regexp_contents string_content
2763%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2764%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2765%type <node_def_temp> defn_head defs_head k_def
2766%type <node_exits> block_open k_while k_until k_for allow_exits
2767%type <node> top_stmts top_stmt begin_block endless_arg endless_command
2768%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary
2769%type <node> command command_call command_call_value method_call
2770%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2771%type <node_fcall> fcall
2772%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2773%type <node> args arg_splat call_args opt_call_args
2774%type <node> paren_args opt_paren_args
2775%type <node_args> args_tail block_args_tail
2776%type <node> command_args aref_args
2777%type <node_block_pass> opt_block_arg block_arg
2778%type <node> var_ref var_lhs
2779%type <node> command_rhs arg_rhs
2780%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2781%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2782%type <node_args_aux> f_arg f_arg_item
2783%type <node> f_marg f_rest_marg
2784%type <node_masgn> f_margs
2785%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2786%type <node_args> block_param opt_block_param_def block_param_def opt_block_param
2787%type <id> do bv_decls opt_bv_decl bvar
2788%type <node> lambda brace_body do_body
2789%type <locations_lambda_body> lambda_body
2790%type <node_args> f_larglist
2791%type <node> brace_block cmd_brace_block do_block lhs none fitem
2792%type <node> mlhs_head mlhs_item mlhs_node
2793%type <node_masgn> mlhs mlhs_basic mlhs_inner
2794%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2795%type <node> p_expr p_as p_alt p_expr_basic p_find
2796%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2797%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2798%type <node> p_kwargs p_kwarg p_kw
2799%type <id> keyword_variable user_variable sym operation2 operation3
2800%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2801%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2802%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2803%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2804%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2805%type <ctxt> p_in_kwarg
2806%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2807%type <num> max_numparam
2808%type <node> numparam
2810%token END_OF_INPUT 0 "end-of-input"
2813/* escaped chars, should be ignored otherwise */
2814%token <id> '\\' "backslash"
2815%token tSP "escaped space"
2816%token <id> '\t' "escaped horizontal tab"
2817%token <id> '\f' "escaped form feed"
2818%token <id> '\r' "escaped carriage return"
2819%token <id> '\13' "escaped vertical tab"
2820%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2821%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2822%token tPOW RUBY_TOKEN(POW) "**"
2823%token tCMP RUBY_TOKEN(CMP) "<=>"
2824%token tEQ RUBY_TOKEN(EQ) "=="
2825%token tEQQ RUBY_TOKEN(EQQ) "==="
2826%token tNEQ RUBY_TOKEN(NEQ) "!="
2827%token tGEQ RUBY_TOKEN(GEQ) ">="
2828%token tLEQ RUBY_TOKEN(LEQ) "<="
2829%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2830%token tOROP RUBY_TOKEN(OROP) "||"
2831%token tMATCH RUBY_TOKEN(MATCH) "=~"
2832%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2833%token tDOT2 RUBY_TOKEN(DOT2) ".."
2834%token tDOT3 RUBY_TOKEN(DOT3) "..."
2835%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2836%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2837%token tAREF RUBY_TOKEN(AREF) "[]"
2838%token tASET RUBY_TOKEN(ASET) "[]="
2839%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2840%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2841%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2842%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2843%token tCOLON3 ":: at EXPR_BEG"
2844%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2847%token tLPAREN_ARG "( arg"
2850%token tLBRACE_ARG "{ arg"
2852%token tDSTAR "**arg"
2854%token <num> tLAMBDA "->"
2855%token tSYMBEG "symbol literal"
2856%token tSTRING_BEG "string literal"
2857%token tXSTRING_BEG "backtick literal"
2858%token tREGEXP_BEG "regexp literal"
2859%token tWORDS_BEG "word list"
2860%token tQWORDS_BEG "verbatim word list"
2861%token tSYMBOLS_BEG "symbol list"
2862%token tQSYMBOLS_BEG "verbatim symbol list"
2863%token tSTRING_END "terminator"
2864%token tSTRING_DEND "'}'"
2865%token <state> tSTRING_DBEG "'#{'"
2866%token tSTRING_DVAR tLAMBEG tLABEL_END
2868%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2869%token tHEREDOC_BEG tHEREDOC_END k__END__
2876%nonassoc tLBRACE_ARG
2878%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2879%left keyword_or keyword_and
2881%nonassoc keyword_defined
2883%left modifier_rescue
2885%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2888%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2889%left '>' tGEQ '<' tLEQ
2895%right tUMINUS_NUM tUMINUS
2897%right '!' '~' tUPLUS
2904%rule %inline ident_or_const
2909%rule %inline user_or_keyword_variable
2915 * parameterizing rules
2917%rule asgn(rhs) <node>
2918 : lhs '=' lex_ctxt rhs
2920 $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$);
2921 /*% ripper: assign!($:1, $:4) %*/
2925%rule args_tail_basic(value) <node_args>
2926 : f_kwarg(value) ',' f_kwrest opt_f_block_arg
2928 $$ = new_args_tail(p, $1, $3, $4, &@3);
2929 /*% ripper: [$:1, $:3, $:4] %*/
2931 | f_kwarg(value) opt_f_block_arg
2933 $$ = new_args_tail(p, $1, 0, $2, &@1);
2934 /*% ripper: [$:1, Qnil, $:2] %*/
2936 | f_any_kwrest opt_f_block_arg
2938 $$ = new_args_tail(p, 0, $1, $2, &@1);
2939 /*% ripper: [Qnil, $:1, $:2] %*/
2943 $$ = new_args_tail(p, 0, 0, $1, &@1);
2944 /*% ripper: [Qnil, Qnil, $:1] %*/
2948%rule def_endless_method(bodystmt) <node>
2949 : defn_head[head] f_opt_paren_args[args] '=' bodystmt
2951 endless_method_name(p, $head->nd_mid, &@head);
2952 restore_defun(p, $head);
2953 ($$ = $head->nd_def)->nd_loc = @$;
2954 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2955 RNODE_DEFN($$)->nd_defn = $bodystmt;
2956 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2957 /*% ripper: def!($:head, $:args, $:$) %*/
2960 | defs_head[head] f_opt_paren_args[args] '=' bodystmt
2962 endless_method_name(p, $head->nd_mid, &@head);
2963 restore_defun(p, $head);
2964 ($$ = $head->nd_def)->nd_loc = @$;
2965 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
2966 RNODE_DEFS($$)->nd_defn = $bodystmt;
2967 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
2968 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
2973%rule compstmt(stmts) <node>
2976 void_stmts(p, $$ = $stmts);
2980%rule f_opt(value) <node_opt_arg>
2981 : f_arg_asgn f_eq value
2983 p->ctxt.in_argdef = 1;
2984 $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$);
2985 /*% ripper: [$:$, $:3] %*/
2989%rule f_opt_arg(value) <node_opt_arg>
2993 /*% ripper: rb_ary_new3(1, $:1) %*/
2995 | f_opt_arg(value) ',' f_opt(value)
2997 $$ = opt_arg_append($f_opt_arg, $f_opt);
2998 /*% ripper: rb_ary_push($:1, $:3) %*/
3002%rule f_kw(value) <node_kw_arg>
3005 p->ctxt.in_argdef = 1;
3006 $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$);
3007 /*% ripper: [$:$, $:value] %*/
3011 p->ctxt.in_argdef = 1;
3012 $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
3013 /*% ripper: [$:$, 0] %*/
3017%rule f_kwarg(value) <node_kw_arg>
3021 /*% ripper: rb_ary_new3(1, $:1) %*/
3023 | f_kwarg(value) ',' f_kw(value)
3025 $$ = kwd_append($f_kwarg, $f_kw);
3026 /*% ripper: rb_ary_push($:1, $:3) %*/
3030%rule mlhs_items(item) <node>
3033 $$ = NEW_LIST($1, &@$);
3034 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3036 | mlhs_items(item) ',' item
3038 $$ = list_append(p, $1, $3);
3039 /*% ripper: mlhs_add!($:1, $:3) %*/
3043%rule op_asgn(rhs) <node>
3044 : var_lhs tOP_ASGN lex_ctxt rhs
3046 $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3047 /*% ripper: opassign!($:1, $:2, $:4) %*/
3049 | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs
3051 $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN);
3052 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3054 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs
3056 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN);
3057 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3059 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs
3061 $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN);
3062 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3064 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs
3066 $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN);
3067 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3069 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
3071 YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3072 $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3073 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3075 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
3077 YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3078 $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3079 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3081 | backref tOP_ASGN lex_ctxt rhs
3083 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref);
3084 $$ = NEW_ERROR(&@$);
3085 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3089%rule opt_args_tail(tail) <node_args>
3097 $$ = new_args_tail(p, 0, 0, 0, &@0);
3098 /*% ripper: [Qnil, Qnil, Qnil] %*/
3102%rule range_expr(range) <node>
3107 $$ = NEW_DOT2($1, $3, &@$, &@2);
3108 /*% ripper: dot2!($:1, $:3) %*/
3114 $$ = NEW_DOT3($1, $3, &@$, &@2);
3115 /*% ripper: dot3!($:1, $:3) %*/
3120 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3121 /*% ripper: dot2!($:1, Qnil) %*/
3126 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
3127 /*% ripper: dot3!($:1, Qnil) %*/
3132 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3133 /*% ripper: dot2!(Qnil, $:2) %*/
3138 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
3139 /*% ripper: dot3!(Qnil, $:2) %*/
3143%rule value_expr(value) <node>
3151%rule words(begin, word_list) <node>
3152 : begin ' '+ word_list tSTRING_END
3154 $$ = make_list($word_list, &@$);
3155 /*% ripper: array!($:3) %*/
3161 SET_LEX_STATE(EXPR_BEG);
3162 local_push(p, ifndef_ripper(1)+0);
3163 /* jumps are possible in the top-level loop. */
3164 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
3168 if ($2 && !compile_for_eval) {
3170 /* last expression should not be void */
3171 if (nd_type_p(node, NODE_BLOCK)) {
3172 while (RNODE_BLOCK(node)->nd_next) {
3173 node = RNODE_BLOCK(node)->nd_next;
3175 node = RNODE_BLOCK(node)->nd_head;
3177 node = remove_begin(node);
3180 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$);
3181 /*% ripper[final]: program!($:2) %*/
3188 $$ = NEW_BEGIN(0, &@$);
3189 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3193 $$ = newline_node($1);
3194 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3196 | top_stmts terms top_stmt
3198 $$ = block_append(p, $1, newline_node($3));
3199 /*% ripper: stmts_add!($:1, $:3) %*/
3205 clear_block_exit(p, true);
3208 | keyword_BEGIN begin_block
3215block_open : '{' {$$ = init_block_exit(p);};
3217begin_block : block_open compstmt(top_stmts) '}'
3219 restore_block_exit(p, $block_open);
3220 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3221 NEW_BEGIN($2, &@$));
3222 $$ = NEW_BEGIN(0, &@$);
3223 /*% ripper: BEGIN!($:2) %*/
3227bodystmt : compstmt(stmts)[body]
3232 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3233 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3235 compstmt(stmts)[elsebody]
3237 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3241 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3242 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3244 | compstmt(stmts)[body]
3248 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3252 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3253 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3259 $$ = NEW_BEGIN(0, &@$);
3260 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3264 $$ = newline_node($1);
3265 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3267 | stmts terms stmt_or_begin
3269 $$ = block_append(p, $1, newline_node($3));
3270 /*% ripper: stmts_add!($:1, $:3) %*/
3277 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3285allow_exits : {$$ = allow_block_exit(p);};
3287k_END : keyword_END lex_ctxt
3290 p->ctxt.in_rescue = before_rescue;
3294stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3296 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3297 /*% ripper: alias!($:2, $:4) %*/
3299 | keyword_alias tGVAR tGVAR
3301 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3302 /*% ripper: var_alias!($:2, $:3) %*/
3304 | keyword_alias tGVAR tBACK_REF
3308 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3309 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3310 /*% ripper: var_alias!($:2, $:3) %*/
3312 | keyword_alias tGVAR tNTH_REF
3314 static const char mesg[] = "can't make alias for the number variables";
3316 yyerror1(&@3, mesg);
3318 $$ = NEW_ERROR(&@$);
3319 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3321 | keyword_undef undef_list
3323 nd_set_first_loc($2, @1.beg_pos);
3324 RNODE_UNDEF($2)->keyword_loc = @1;
3326 /*% ripper: undef!($:2) %*/
3328 | stmt modifier_if expr_value
3330 $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3332 /*% ripper: if_mod!($:3, $:1) %*/
3334 | stmt modifier_unless expr_value
3336 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3338 /*% ripper: unless_mod!($:3, $:1) %*/
3340 | stmt modifier_while expr_value
3342 clear_block_exit(p, false);
3343 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3344 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3347 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3349 /*% ripper: while_mod!($:3, $:1) %*/
3351 | stmt modifier_until expr_value
3353 clear_block_exit(p, false);
3354 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3355 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3358 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3360 /*% ripper: until_mod!($:3, $:1) %*/
3362 | stmt modifier_rescue after_rescue stmt
3364 p->ctxt.in_rescue = $3.in_rescue;
3366 YYLTYPE loc = code_loc_gen(&@2, &@4);
3367 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3368 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3369 /*% ripper: rescue_mod!($:1, $:4) %*/
3371 | k_END allow_exits '{' compstmt(stmts) '}'
3373 if (p->ctxt.in_def) {
3374 rb_warn0("END in method; use at_exit");
3376 restore_block_exit(p, $allow_exits);
3379 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, NULL /* parent */, &@$);
3380 $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
3381 RNODE_SCOPE(scope)->nd_parent = $$;
3383 /*% ripper: END!($:compstmt) %*/
3386 | mlhs '=' lex_ctxt command_call_value
3388 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3389 /*% ripper: massign!($:1, $:4) %*/
3392 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3393 after_rescue stmt[resbody]
3395 p->ctxt.in_rescue = $3.in_rescue;
3396 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3397 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3398 loc.beg_pos = @mrhs_arg.beg_pos;
3399 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3400 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3401 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3403 | mlhs '=' lex_ctxt mrhs_arg
3405 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3406 /*% ripper: massign!($:1, $:4) %*/
3412 $$ = NEW_ERROR(&@$);
3416command_asgn : asgn(command_rhs)
3417 | op_asgn(command_rhs)
3418 | def_endless_method(endless_command)
3421endless_command : command
3422 | endless_command modifier_rescue after_rescue arg
3424 p->ctxt.in_rescue = $3.in_rescue;
3425 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3426 /*% ripper: rescue_mod!($:1, $:4) %*/
3428 | keyword_not '\n'? endless_command
3430 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3431 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3435command_rhs : command_call_value %prec tOP_ASGN
3436 | command_call_value modifier_rescue after_rescue stmt
3438 p->ctxt.in_rescue = $3.in_rescue;
3439 YYLTYPE loc = code_loc_gen(&@2, &@4);
3440 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3441 /*% ripper: rescue_mod!($:1, $:4) %*/
3447 | expr keyword_and expr
3449 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3450 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3452 | expr keyword_or expr
3454 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3455 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3457 | keyword_not '\n'? expr
3459 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3460 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3464 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3465 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3469 value_expr(p, $arg);
3471 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3472 p_top_expr_body[body]
3474 pop_pktbl(p, $p_pktbl);
3475 pop_pvtbl(p, $p_pvtbl);
3476 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3477 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3478 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3479 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@2), &@$, &NULL_LOC, &NULL_LOC);
3480 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3484 value_expr(p, $arg);
3486 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3487 p_top_expr_body[body]
3489 pop_pktbl(p, $p_pktbl);
3490 pop_pvtbl(p, $p_pvtbl);
3491 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3492 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
3493 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
3494 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
3495 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3497 | arg %prec tLBRACE_ARG
3502 numparam_name(p, $fname);
3505 p->ctxt.in_rescue = before_rescue;
3506 p->ctxt.cant_return = 0;
3511defn_head : k_def def_name
3513 $$ = def_head_save(p, $k_def);
3514 $$->nd_mid = $def_name;
3515 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3516 /*% ripper: $:def_name %*/
3520defs_head : k_def singleton dot_or_colon
3522 SET_LEX_STATE(EXPR_FNAME);
3526 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3527 $$ = def_head_save(p, $k_def);
3528 $$->nd_mid = $def_name;
3529 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3530 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3534expr_value : value_expr(expr)
3537 $$ = NEW_ERROR(&@$);
3541expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3548command_call : command
3552command_call_value : value_expr(command_call)
3555block_command : block_call
3556 | block_call call_op2 operation2 command_args
3558 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3559 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3563cmd_brace_block : tLBRACE_ARG brace_body '}'
3566 set_embraced_location($$, &@1, &@3);
3573 $$ = NEW_FCALL($1, 0, &@$);
3578command : fcall command_args %prec tLOWEST
3581 nd_set_last_loc($1, @2.end_pos);
3583 /*% ripper: command!($:1, $:2) %*/
3585 | fcall command_args cmd_brace_block
3587 block_dup_check(p, $2, $3);
3589 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3590 fixpos($$, RNODE($1));
3591 nd_set_last_loc($1, @2.end_pos);
3592 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3594 | primary_value call_op operation2 command_args %prec tLOWEST
3596 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3597 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3599 | primary_value call_op operation2 command_args cmd_brace_block
3601 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3602 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3604 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3606 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3607 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3609 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3611 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3612 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3614 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3616 set_embraced_location($5, &@4, &@6);
3617 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3618 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3620 | keyword_super command_args
3622 $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3624 /*% ripper: super!($:2) %*/
3626 | k_yield command_args
3628 $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC);
3630 /*% ripper: yield!($:2) %*/
3632 | k_return call_args
3634 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3635 /*% ripper: return!($:2) %*/
3637 | keyword_break call_args
3640 args = ret_args(p, $2);
3641 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3642 /*% ripper: break!($:2) %*/
3644 | keyword_next call_args
3647 args = ret_args(p, $2);
3648 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3649 /*% ripper: next!($:2) %*/
3654 | tLPAREN mlhs_inner rparen
3657 /*% ripper: mlhs_paren!($:2) %*/
3661mlhs_inner : mlhs_basic
3662 | tLPAREN mlhs_inner rparen
3664 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3665 /*% ripper: mlhs_paren!($:2) %*/
3669mlhs_basic : mlhs_head
3671 $$ = NEW_MASGN($1, 0, &@$);
3674 | mlhs_head mlhs_item
3676 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3677 /*% ripper: mlhs_add!($:1, $:2) %*/
3679 | mlhs_head tSTAR mlhs_node
3681 $$ = NEW_MASGN($1, $3, &@$);
3682 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3684 | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3686 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3687 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3691 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3692 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3694 | mlhs_head tSTAR ',' mlhs_items(mlhs_item)
3696 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3697 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3701 $$ = NEW_MASGN(0, $2, &@$);
3702 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3704 | tSTAR mlhs_node ',' mlhs_items(mlhs_item)
3706 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3707 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3711 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3712 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3714 | tSTAR ',' mlhs_items(mlhs_item)
3716 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3717 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3721mlhs_item : mlhs_node
3722 | tLPAREN mlhs_inner rparen
3725 /*% ripper: mlhs_paren!($:2) %*/
3729mlhs_head : mlhs_item ','
3731 $$ = NEW_LIST($1, &@1);
3732 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3734 | mlhs_head mlhs_item ','
3736 $$ = list_append(p, $1, $2);
3737 /*% ripper: mlhs_add!($:1, $:2) %*/
3742mlhs_node : user_or_keyword_variable
3744 /*% ripper: var_field!($:1) %*/
3745 $$ = assignable(p, $1, 0, &@$);
3747 | primary_value '[' opt_call_args rbracket
3749 $$ = aryset(p, $1, $3, &@$);
3750 /*% ripper: aref_field!($:1, $:3) %*/
3752 | primary_value call_op ident_or_const
3754 anddot_multiple_assignment_check(p, &@2, $2);
3755 $$ = attrset(p, $1, $2, $3, &@$);
3756 /*% ripper: field!($:1, $:2, $:3) %*/
3758 | primary_value tCOLON2 tIDENTIFIER
3760 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3761 /*% ripper: const_path_field!($:1, $:3) %*/
3763 | primary_value tCOLON2 tCONSTANT
3765 /*% ripper: const_path_field!($:1, $:3) %*/
3766 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3770 /*% ripper: top_const_field!($:2) %*/
3771 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3775 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3776 $$ = NEW_ERROR(&@$);
3777 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3781lhs : user_or_keyword_variable
3783 /*% ripper: var_field!($:1) %*/
3784 $$ = assignable(p, $1, 0, &@$);
3786 | primary_value '[' opt_call_args rbracket
3788 $$ = aryset(p, $1, $3, &@$);
3789 /*% ripper: aref_field!($:1, $:3) %*/
3791 | primary_value call_op ident_or_const
3793 $$ = attrset(p, $1, $2, $3, &@$);
3794 /*% ripper: field!($:1, $:2, $:3) %*/
3796 | primary_value tCOLON2 tIDENTIFIER
3798 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3799 /*% ripper: field!($:1, $:2, $:3) %*/
3801 | primary_value tCOLON2 tCONSTANT
3803 /*% ripper: const_path_field!($:1, $:3) %*/
3804 $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
3808 /*% ripper: top_const_field!($:2) %*/
3809 $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
3813 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3814 $$ = NEW_ERROR(&@$);
3815 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3821 static const char mesg[] = "class/module name must be CONSTANT";
3823 yyerror1(&@1, mesg);
3825 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3830cpath : tCOLON3 cname
3832 $$ = NEW_COLON3($2, &@$, &@1, &@2);
3833 /*% ripper: top_const_ref!($:2) %*/
3837 $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
3838 /*% ripper: const_ref!($:1) %*/
3840 | primary_value tCOLON2 cname
3842 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
3843 /*% ripper: const_path_ref!($:1, $:3) %*/
3850 SET_LEX_STATE(EXPR_ENDFN);
3858 $$ = NEW_SYM(rb_id2str($1), &@$);
3859 /*% ripper: symbol_literal!($:1) %*/
3866 $$ = NEW_UNDEF($1, &@$);
3867 /*% ripper: rb_ary_new3(1, $:1) %*/
3869 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3871 nd_set_last_loc($1, @4.end_pos);
3872 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3873 /*% ripper: rb_ary_push($:1, $:4) %*/
3877op : '|' { $$ = '|'; }
3880 | tCMP { $$ = tCMP; }
3882 | tEQQ { $$ = tEQQ; }
3883 | tMATCH { $$ = tMATCH; }
3884 | tNMATCH { $$ = tNMATCH; }
3886 | tGEQ { $$ = tGEQ; }
3888 | tLEQ { $$ = tLEQ; }
3889 | tNEQ { $$ = tNEQ; }
3890 | tLSHFT { $$ = tLSHFT; }
3891 | tRSHFT { $$ = tRSHFT; }
3895 | tSTAR { $$ = '*'; }
3898 | tPOW { $$ = tPOW; }
3899 | tDSTAR { $$ = tDSTAR; }
3902 | tUPLUS { $$ = tUPLUS; }
3903 | tUMINUS { $$ = tUMINUS; }
3904 | tAREF { $$ = tAREF; }
3905 | tASET { $$ = tASET; }
3909reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3910 | keyword_BEGIN | keyword_END
3911 | keyword_alias | keyword_and | keyword_begin
3912 | keyword_break | keyword_case | keyword_class | keyword_def
3913 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3914 | keyword_end | keyword_ensure | keyword_false
3915 | keyword_for | keyword_in | keyword_module | keyword_next
3916 | keyword_nil | keyword_not | keyword_or | keyword_redo
3917 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3918 | keyword_super | keyword_then | keyword_true | keyword_undef
3919 | keyword_when | keyword_yield | keyword_if | keyword_unless
3920 | keyword_while | keyword_until
3928 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3929 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3933 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3934 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3938 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3939 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3943 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3944 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3948 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3949 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3953 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3954 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3956 | tUMINUS_NUM simple_numeric tPOW arg
3958 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3959 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3963 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3964 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3968 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3969 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3973 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3974 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3978 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3979 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3983 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3984 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3988 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3989 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3991 | rel_expr %prec tCMP
3994 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3995 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3999 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
4000 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4004 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4005 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4009 $$ = match_op(p, $1, $3, &@2, &@$);
4010 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4014 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4015 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4019 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4020 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4024 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4025 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4029 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4030 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4034 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4035 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4039 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4040 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4044 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4045 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4047 | keyword_defined '\n'? begin_defined arg
4049 p->ctxt.in_defined = $3.in_defined;
4050 $$ = new_defined(p, $4, &@$, &@1);
4051 p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon;
4052 /*% ripper: defined!($:4) %*/
4054 | def_endless_method(endless_arg)
4059ternary : arg '?' arg '\n'? ':' arg
4062 $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC);
4064 /*% ripper: ifop!($:1, $:3, $:6) %*/
4068endless_arg : arg %prec modifier_rescue
4069 | endless_arg modifier_rescue after_rescue arg
4071 p->ctxt.in_rescue = $3.in_rescue;
4072 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4073 /*% ripper: rescue_mod!($:1, $:4) %*/
4075 | keyword_not '\n'? endless_arg
4077 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4078 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4082relop : '>' {$$ = '>';}
4088rel_expr : arg relop arg %prec '>'
4090 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4091 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4093 | rel_expr relop arg %prec '>'
4095 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4096 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4097 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4107begin_defined : lex_ctxt
4109 p->ctxt.in_defined = 1;
4114after_rescue : lex_ctxt
4116 p->ctxt.in_rescue = after_rescue;
4121arg_value : value_expr(arg)
4126 | args ',' assocs trailer
4128 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4129 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4133 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4134 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4138arg_rhs : arg %prec tOP_ASGN
4143 | arg modifier_rescue after_rescue arg
4145 p->ctxt.in_rescue = $3.in_rescue;
4147 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4148 /*% ripper: rescue_mod!($:1, $:4) %*/
4152paren_args : '(' opt_call_args rparen
4155 /*% ripper: arg_paren!($:2) %*/
4157 | '(' args ',' args_forward rparen
4159 if (!check_forwarding_args(p)) {
4163 $$ = new_args_forward_call(p, $2, &@4, &@$);
4164 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4167 | '(' args_forward rparen
4169 if (!check_forwarding_args(p)) {
4173 $$ = new_args_forward_call(p, 0, &@2, &@$);
4174 /*% ripper: arg_paren!($:2) %*/
4179opt_paren_args : none
4182 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4189 | args ',' assocs ','
4191 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4192 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4196 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4197 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4201call_args : value_expr(command)
4203 $$ = NEW_LIST($1, &@$);
4204 /*% ripper: args_add!(args_new!, $:1) %*/
4206 | def_endless_method(endless_command)
4208 $$ = NEW_LIST($1, &@$);
4209 /*% ripper: args_add!(args_new!, $:1) %*/
4211 | args opt_block_arg
4213 $$ = arg_blk_pass($1, $2);
4214 /*% ripper: args_add_block!($:1, $:2) %*/
4216 | assocs opt_block_arg
4218 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4219 $$ = arg_blk_pass($$, $2);
4220 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4222 | args ',' assocs opt_block_arg
4224 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4225 $$ = arg_blk_pass($$, $4);
4226 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4229 /*% ripper: args_add_block!(args_new!, $:1) %*/
4233 /* If call_args starts with a open paren '(' or '[',
4234 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4235 * but the push must be done after CMDARG_PUSH(1).
4236 * So this code makes them consistent by first cancelling
4237 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4238 * and finally redoing CMDARG_PUSH(0).
4242 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4245 if (lookahead) CMDARG_POP();
4247 if (lookahead) CMDARG_PUSH(0);
4251 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4252 * but the push must be done after CMDARG_POP() in the parser.
4253 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4254 * CMDARG_POP() to pop 1 pushed by command_args,
4255 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4262 if (lookahead) CMDARG_POP();
4264 if (lookahead) CMDARG_PUSH(0);
4270block_arg : tAMPER arg_value
4272 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4277 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4278 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4279 /*% ripper: Qnil %*/
4283opt_block_arg : ',' block_arg
4291 /*% ripper: Qfalse %*/
4298 $$ = NEW_LIST($arg_value, &@$);
4299 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4304 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4306 | args[non_last_args] ',' arg_value
4308 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4309 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4311 | args[non_last_args] ',' arg_splat
4313 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4314 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4319arg_splat : tSTAR arg_value
4321 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4322 /*% ripper: $:arg_value %*/
4326 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4327 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4328 /*% ripper: Qnil %*/
4338mrhs : args ',' arg_value
4340 $$ = last_arg_append(p, $args, $arg_value, &@$);
4341 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4343 | args ',' tSTAR arg_value
4345 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4346 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4350 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4351 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4355%rule %inline inline_primary
4366primary : inline_primary
4371 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4372 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4382 set_line_body($3, @1.end_pos.lineno);
4383 $$ = NEW_BEGIN($3, &@$);
4384 nd_set_line($$, @1.end_pos.lineno);
4385 /*% ripper: begin!($:3) %*/
4387 | tLPAREN_ARG compstmt(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')'
4389 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4391 /*% ripper: paren!($:2) %*/
4393 | tLPAREN compstmt(stmts) ')'
4395 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4396 $$ = NEW_BLOCK($2, &@$);
4397 /*% ripper: paren!($:2) %*/
4399 | primary_value tCOLON2 tCONSTANT
4401 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
4402 /*% ripper: const_path_ref!($:1, $:3) %*/
4406 $$ = NEW_COLON3($2, &@$, &@1, &@2);
4407 /*% ripper: top_const_ref!($:2) %*/
4409 | tLBRACK aref_args ']'
4411 $$ = make_list($2, &@$);
4412 /*% ripper: array!($:2) %*/
4414 | tLBRACE assoc_list '}'
4416 $$ = new_hash(p, $2, &@$);
4417 RNODE_HASH($$)->nd_brace = TRUE;
4418 /*% ripper: hash!($:2) %*/
4422 $$ = NEW_RETURN(0, &@$, &@1);
4423 /*% ripper: return0! %*/
4425 | k_yield '(' call_args rparen
4427 $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4);
4428 /*% ripper: yield!(paren!($:3)) %*/
4430 | k_yield '(' rparen
4432 $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3);
4433 /*% ripper: yield!(paren!(args_new!)) %*/
4437 $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC);
4438 /*% ripper: yield0! %*/
4440 | keyword_defined '\n'? '(' begin_defined expr rparen
4442 p->ctxt.in_defined = $4.in_defined;
4443 $$ = new_defined(p, $5, &@$, &@1);
4444 p->ctxt.has_trailing_semicolon = $4.has_trailing_semicolon;
4445 /*% ripper: defined!($:5) %*/
4447 | keyword_not '(' expr rparen
4449 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4450 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4452 | keyword_not '(' rparen
4454 $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@2), &@2), METHOD_NOT, &@1, &@$);
4455 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4459 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4460 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4463 | method_call brace_block
4465 block_dup_check(p, get_nd_args(p, $1), $2);
4466 $$ = method_add_block(p, $1, $2, &@$);
4467 /*% ripper: method_add_block!($:1, $:2) %*/
4470 | k_if expr_value then
4475 if ($5 && nd_type_p($5, NODE_IF))
4476 RNODE_IF($5)->end_keyword_loc = @6;
4478 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4480 /*% ripper: if!($:2, $:4, $:5) %*/
4482 | k_unless expr_value then
4487 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4489 /*% ripper: unless!($:2, $:4, $:5) %*/
4491 | k_while expr_value_do
4495 restore_block_exit(p, $1);
4496 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4498 /*% ripper: while!($:2, $:3) %*/
4500 | k_until expr_value_do
4504 restore_block_exit(p, $1);
4505 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4507 /*% ripper: until!($:2, $:3) %*/
4509 | k_case expr_value terms?
4511 $$ = p->case_labels;
4512 p->case_labels = CHECK_LITERAL_WHEN;
4517 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4518 p->case_labels = $4;
4519 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4521 /*% ripper: case!($:2, $:5) %*/
4525 $$ = p->case_labels;
4531 if (p->case_labels) st_free_table(p->case_labels);
4532 p->case_labels = $3;
4533 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4534 /*% ripper: case!(Qnil, $:4) %*/
4536 | k_case expr_value terms?
4540 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4541 /*% ripper: case!($:2, $:4) %*/
4543 | k_for for_var keyword_in
4544 {COND_PUSH(1);} expr_value do {COND_POP();}
4548 restore_block_exit(p, $k_for);
4552 * e.each{|*x| a, b, c = x}
4556 * e.each{|x| a, = x}
4558 ID id = internal_id(p);
4559 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4560 rb_node_args_t *args;
4561 NODE *scope, *internal_var = NEW_DVAR(id, &@for_var);
4562 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4563 tbl->ids[0] = id; /* internal id */
4565 switch (nd_type($for_var)) {
4567 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4568 set_nd_value(p, $for_var, internal_var);
4571 m->nd_next = $for_var;
4573 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4574 m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var);
4576 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4577 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);
4579 /* {|*internal_id| <m> = internal_id; ... } */
4580 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@for_var), &@for_var);
4581 scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$);
4582 YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC;
4583 $$ = NEW_FOR($5, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end);
4584 RNODE_SCOPE(scope)->nd_parent = $$;
4585 fixpos($$, $for_var);
4586 /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/
4588 | k_class cpath superclass
4590 begin_definition("class", &@k_class, &@cpath);
4595 YYLTYPE inheritance_operator_loc = NULL_LOC;
4597 inheritance_operator_loc = @superclass;
4598 inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
4600 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
4601 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4602 set_line_body($bodystmt, @superclass.end_pos.lineno);
4603 nd_set_line($$, @superclass.end_pos.lineno);
4604 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4606 p->ctxt.in_class = $k_class.in_class;
4607 p->ctxt.cant_return = $k_class.cant_return;
4608 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4610 | k_class tLSHFT expr_value
4612 begin_definition("", &@k_class, &@tLSHFT);
4618 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end);
4619 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4620 set_line_body($bodystmt, nd_line($expr_value));
4621 fixpos($$, $expr_value);
4622 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4624 p->ctxt.in_def = $k_class.in_def;
4625 p->ctxt.in_class = $k_class.in_class;
4626 p->ctxt.cant_return = $k_class.cant_return;
4627 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4631 begin_definition("module", &@k_module, &@cpath);
4636 $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
4637 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4638 set_line_body($bodystmt, @cpath.end_pos.lineno);
4639 nd_set_line($$, @cpath.end_pos.lineno);
4640 /*% ripper: module!($:cpath, $:bodystmt) %*/
4642 p->ctxt.in_class = $k_module.in_class;
4643 p->ctxt.cant_return = $k_module.cant_return;
4644 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4649 push_end_expect_token_locations(p, &@head.beg_pos);
4654 restore_defun(p, $head);
4655 ($$ = $head->nd_def)->nd_loc = @$;
4656 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4657 RNODE_DEFN($$)->nd_defn = $bodystmt;
4658 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4664 push_end_expect_token_locations(p, &@head.beg_pos);
4669 restore_defun(p, $head);
4670 ($$ = $head->nd_def)->nd_loc = @$;
4671 $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$);
4672 RNODE_DEFS($$)->nd_defn = $bodystmt;
4673 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4678 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4679 /*% ripper: break!(args_new!) %*/
4683 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4684 /*% ripper: next!(args_new!) %*/
4688 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4689 /*% ripper: redo! %*/
4693 if (!p->ctxt.in_defined) {
4694 switch (p->ctxt.in_rescue) {
4695 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4696 case after_rescue: /* ok */ break;
4697 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4698 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4701 $$ = NEW_RETRY(&@$);
4702 /*% ripper: retry! %*/
4706primary_value : value_expr(primary)
4709k_begin : keyword_begin
4711 token_info_push(p, "begin", &@$);
4712 push_end_expect_token_locations(p, &@1.beg_pos);
4719 token_info_push(p, "if", &@$);
4720 if (p->token_info && p->token_info->nonspc &&
4721 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4722 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4723 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4724 beg += rb_strlen_lit("else");
4725 while (beg < tok && ISSPACE(*beg)) beg++;
4727 p->token_info->nonspc = 0;
4730 push_end_expect_token_locations(p, &@1.beg_pos);
4734k_unless : keyword_unless
4736 token_info_push(p, "unless", &@$);
4737 push_end_expect_token_locations(p, &@1.beg_pos);
4741k_while : keyword_while allow_exits
4744 token_info_push(p, "while", &@$);
4745 push_end_expect_token_locations(p, &@1.beg_pos);
4749k_until : keyword_until allow_exits
4752 token_info_push(p, "until", &@$);
4753 push_end_expect_token_locations(p, &@1.beg_pos);
4757k_case : keyword_case
4759 token_info_push(p, "case", &@$);
4760 push_end_expect_token_locations(p, &@1.beg_pos);
4764k_for : keyword_for allow_exits
4767 token_info_push(p, "for", &@$);
4768 push_end_expect_token_locations(p, &@1.beg_pos);
4772k_class : keyword_class
4774 token_info_push(p, "class", &@$);
4776 p->ctxt.in_rescue = before_rescue;
4777 push_end_expect_token_locations(p, &@1.beg_pos);
4781k_module : keyword_module
4783 token_info_push(p, "module", &@$);
4785 p->ctxt.in_rescue = before_rescue;
4786 push_end_expect_token_locations(p, &@1.beg_pos);
4792 token_info_push(p, "def", &@$);
4793 $$ = NEW_DEF_TEMP(&@$);
4794 p->ctxt.in_argdef = 1;
4800 token_info_push(p, "do", &@$);
4801 push_end_expect_token_locations(p, &@1.beg_pos);
4805k_do_block : keyword_do_block
4807 token_info_push(p, "do", &@$);
4808 push_end_expect_token_locations(p, &@1.beg_pos);
4812k_rescue : keyword_rescue
4814 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4816 p->ctxt.in_rescue = after_rescue;
4820k_ensure : keyword_ensure
4822 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4827k_when : keyword_when
4829 token_info_warn(p, "when", p->token_info, 0, &@$);
4833k_else : keyword_else
4835 token_info *ptinfo_beg = p->token_info;
4836 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4837 token_info_warn(p, "else", p->token_info, same, &@$);
4840 e.next = ptinfo_beg->next;
4842 token_info_setup(&e, p->lex.pbeg, &@$);
4843 if (!e.nonspc) *ptinfo_beg = e;
4848k_elsif : keyword_elsif
4851 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4857 token_info_pop(p, "end", &@$);
4858 pop_end_expect_token_locations(p);
4862 compile_error(p, "syntax error, unexpected end-of-input");
4866k_return : keyword_return
4868 if (p->ctxt.cant_return && !dyna_in_block(p))
4869 yyerror1(&@1, "Invalid return in class/module body");
4873k_yield : keyword_yield
4875 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4876 yyerror1(&@1, "Invalid yield");
4886 | keyword_do_cond { $$ = keyword_do_cond; }
4890 | k_elsif expr_value then
4894 $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC);
4896 /*% ripper: elsif!($:2, $:4, $:5) %*/
4901 | k_else compstmt(stmts)
4904 /*% ripper: else!($:2) %*/
4914 $$ = assignable(p, $1, 0, &@$);
4915 mark_lvar_used(p, $$);
4917 | tLPAREN f_margs rparen
4920 /*% ripper: mlhs_paren!($:2) %*/
4925f_margs : mlhs_items(f_marg)
4927 $$ = NEW_MASGN($1, 0, &@$);
4930 | mlhs_items(f_marg) ',' f_rest_marg
4932 $$ = NEW_MASGN($1, $3, &@$);
4933 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4935 | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg)
4937 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4938 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4942 $$ = NEW_MASGN(0, $1, &@$);
4943 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4945 | f_rest_marg ',' mlhs_items(f_marg)
4947 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4948 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4952f_rest_marg : tSTAR f_norm_arg
4955 $$ = assignable(p, $2, 0, &@$);
4956 mark_lvar_used(p, $$);
4960 $$ = NODE_SPECIAL_NO_NAME_REST;
4961 /*% ripper: Qnil %*/
4965f_any_kwrest : f_kwrest
4969 /*% ripper: ID2VAL(idNil) %*/
4973f_eq : {p->ctxt.in_argdef = 0;} '=';
4975block_args_tail : args_tail_basic(primary_value)
4980 /* magic number for rest_id in iseq_set_arguments() */
4981 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4982 /*% ripper: excessed_comma! %*/
4986block_param : f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
4988 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
4989 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
4991 | f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
4993 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4994 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
4996 | f_arg ',' f_opt_arg(primary_value) opt_args_tail(block_args_tail)
4998 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
4999 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
5001 | f_arg ',' f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5003 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5004 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5006 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5008 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5009 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5011 | f_arg excessed_comma
5013 $$ = new_args_tail(p, 0, 0, 0, &@2);
5014 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5015 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5017 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5019 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5020 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5022 | f_arg opt_args_tail(block_args_tail)
5024 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5025 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5027 | f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5029 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5030 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5032 | f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5034 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5035 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5037 | f_opt_arg(primary_value) opt_args_tail(block_args_tail)
5039 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5040 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5042 | f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5044 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5045 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5047 | f_rest_arg opt_args_tail(block_args_tail)
5049 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5050 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5052 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5054 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5055 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5059 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5060 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5064opt_block_param_def : none
5067 p->command_start = TRUE;
5071block_param_def : '|' opt_block_param opt_bv_decl '|'
5073 p->max_numparam = ORDINAL_PARAM;
5074 p->ctxt.in_argdef = 0;
5076 /*% ripper: block_var!($:2, $:3) %*/
5080opt_block_param : /* none */
5083 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
5091 /*% ripper: Qfalse %*/
5093 | '\n'? ';' bv_decls '\n'?
5101 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5103 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5115 $$ = p->max_numparam;
5116 p->max_numparam = 0;
5121 $$ = numparam_push(p);
5131lambda : tLAMBDA[lpar]
5133 token_info_push(p, "->", &@1);
5136 max_numparam numparam it_id allow_exits
5143 int max_numparam = p->max_numparam;
5144 ID it_id = p->it_id;
5145 p->lex.lpar_beg = $lpar;
5146 p->max_numparam = $max_numparam;
5148 restore_block_exit(p, $allow_exits);
5150 $args = args_with_numbered(p, $args, max_numparam, it_id);
5152 YYLTYPE loc = code_loc_gen(&@args, &@body);
5153 $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc);
5154 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5155 nd_set_line($$, @args.end_pos.lineno);
5156 nd_set_first_loc($$, @1.beg_pos);
5159 /*% ripper: lambda!($:args, $:body) %*/
5160 numparam_pop(p, $numparam);
5165f_larglist : '(' f_args opt_bv_decl ')'
5167 p->ctxt.in_argdef = 0;
5169 p->max_numparam = ORDINAL_PARAM;
5170 /*% ripper: paren!($:2) %*/
5174 p->ctxt.in_argdef = 0;
5175 if (!args_info_empty_p(&$1->nd_ainfo))
5176 p->max_numparam = ORDINAL_PARAM;
5181lambda_body : tLAMBEG compstmt(stmts) '}'
5183 token_info_pop(p, "}", &@3);
5184 $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3);
5189 push_end_expect_token_locations(p, &@1.beg_pos);
5193 $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4);
5198do_block : k_do_block do_body k_end
5201 set_embraced_location($$, &@1, &@3);
5206block_call : command do_block
5208 if (nd_type_p($1, NODE_YIELD)) {
5209 compile_error(p, "block given to yield");
5212 block_dup_check(p, get_nd_args(p, $1), $2);
5214 $$ = method_add_block(p, $1, $2, &@$);
5216 /*% ripper: method_add_block!($:1, $:2) %*/
5218 | block_call call_op2 operation2 opt_paren_args
5220 bool has_args = $4 != 0;
5221 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5222 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5223 /*% ripper: call!($:1, $:2, $:3) %*/
5225 /*% ripper: method_add_arg!($:$, $:4) %*/
5228 | block_call call_op2 operation2 opt_paren_args brace_block
5230 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
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 operation2 command_args do_block
5236 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5237 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5239 | block_call call_op2 paren_args
5241 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5242 nd_set_line($$, @2.end_pos.lineno);
5243 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5247method_call : fcall paren_args
5251 nd_set_last_loc($1, @2.end_pos);
5252 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5254 | primary_value call_op operation2 opt_paren_args
5256 bool has_args = $4 != 0;
5257 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5258 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5259 nd_set_line($$, @3.end_pos.lineno);
5260 /*% ripper: call!($:1, $:2, $:3) %*/
5262 /*% ripper: method_add_arg!($:$, $:4) %*/
5265 | primary_value tCOLON2 operation2 paren_args
5267 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5268 nd_set_line($$, @3.end_pos.lineno);
5269 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5271 | primary_value tCOLON2 operation3
5273 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5274 /*% ripper: call!($:1, $:2, $:3) %*/
5276 | primary_value call_op2 paren_args
5278 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5279 nd_set_line($$, @2.end_pos.lineno);
5280 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5282 | keyword_super paren_args
5284 rb_code_location_t lparen_loc = @2;
5285 rb_code_location_t rparen_loc = @2;
5286 lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1;
5287 rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1;
5289 $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc);
5290 /*% ripper: super!($:2) %*/
5294 $$ = NEW_ZSUPER(&@$);
5295 /*% ripper: zsuper! %*/
5297 | primary_value '[' opt_call_args rbracket
5299 $$ = NEW_CALL($1, tAREF, $3, &@$);
5301 /*% ripper: aref!($:1, $:3) %*/
5305brace_block : '{' brace_body '}'
5308 set_embraced_location($$, &@1, &@3);
5311 | k_do do_body k_end
5314 set_embraced_location($$, &@1, &@3);
5319brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5320 max_numparam numparam it_id allow_exits
5321 opt_block_param_def[args] compstmt(stmts)
5323 int max_numparam = p->max_numparam;
5324 ID it_id = p->it_id;
5325 p->max_numparam = $max_numparam;
5327 $args = args_with_numbered(p, $args, max_numparam, it_id);
5328 $$ = NEW_ITER($args, $compstmt, &@$);
5329 /*% ripper: brace_block!($:args, $:compstmt) %*/
5330 restore_block_exit(p, $allow_exits);
5331 numparam_pop(p, $numparam);
5340 max_numparam numparam it_id allow_exits
5341 opt_block_param_def[args] bodystmt
5343 int max_numparam = p->max_numparam;
5344 ID it_id = p->it_id;
5345 p->max_numparam = $max_numparam;
5347 $args = args_with_numbered(p, $args, max_numparam, it_id);
5348 $$ = NEW_ITER($args, $bodystmt, &@$);
5349 /*% ripper: do_block!($:args, $:bodystmt) %*/
5351 restore_block_exit(p, $allow_exits);
5352 numparam_pop(p, $numparam);
5357case_args : arg_value
5359 check_literal_when(p, $arg_value, &@arg_value);
5360 $$ = NEW_LIST($arg_value, &@$);
5361 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5365 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5366 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5368 | case_args[non_last_args] ',' arg_value
5370 check_literal_when(p, $arg_value, &@arg_value);
5371 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5372 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5374 | case_args[non_last_args] ',' tSTAR arg_value
5376 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5377 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5381case_body : k_when case_args then
5385 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5387 /*% ripper: when!($:2, $:4, $:5) %*/
5395p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5396p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5400 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5401 p->command_start = FALSE;
5402 p->ctxt.in_kwarg = 1;
5403 p->ctxt.in_alt_pattern = 0;
5404 p->ctxt.capture_in_pattern = 0;
5408p_case_body : keyword_in
5409 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5410 p_top_expr[expr] then
5412 pop_pktbl(p, $p_pktbl);
5413 pop_pvtbl(p, $p_pvtbl);
5414 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5415 p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern;
5416 p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern;
5421 $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
5422 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5430p_top_expr : p_top_expr_body
5431 | p_top_expr_body modifier_if expr_value
5433 $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5435 /*% ripper: if_mod!($:3, $:1) %*/
5437 | p_top_expr_body modifier_unless expr_value
5439 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5441 /*% ripper: unless_mod!($:3, $:1) %*/
5445p_top_expr_body : p_expr
5448 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5449 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5450 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5454 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5455 nd_set_first_loc($$, @1.beg_pos);
5456 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5460 $$ = new_find_pattern(p, 0, $1, &@$);
5461 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5465 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5466 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5470 $$ = new_hash_pattern(p, 0, $1, &@$);
5471 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5478p_as : p_expr tASSOC p_variable
5480 NODE *n = NEW_LIST($1, &@$);
5481 n = list_append(p, n, $3);
5482 $$ = new_hash(p, n, &@$);
5483 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5488p_alt : p_alt[left] '|'[alt]
5490 p->ctxt.in_alt_pattern = 1;
5494 if (p->ctxt.capture_in_pattern) {
5495 yyerror1(&@alt, "alternative pattern after variable capture");
5497 p->ctxt.in_alt_pattern = 0;
5498 $$ = NEW_OR($left, $right, &@$, &@alt);
5499 /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/
5504p_lparen : '(' p_pktbl
5511p_lbracket : '[' p_pktbl
5518p_expr_basic : p_value
5520 | p_const p_lparen[p_pktbl] p_args rparen
5522 pop_pktbl(p, $p_pktbl);
5523 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5524 nd_set_first_loc($$, @p_const.beg_pos);
5525 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5527 | p_const p_lparen[p_pktbl] p_find rparen
5529 pop_pktbl(p, $p_pktbl);
5530 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5531 nd_set_first_loc($$, @p_const.beg_pos);
5532 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5534 | p_const p_lparen[p_pktbl] p_kwargs rparen
5536 pop_pktbl(p, $p_pktbl);
5537 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5538 nd_set_first_loc($$, @p_const.beg_pos);
5539 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5541 | p_const '(' rparen
5543 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5544 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5545 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5547 | p_const p_lbracket[p_pktbl] p_args rbracket
5549 pop_pktbl(p, $p_pktbl);
5550 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5551 nd_set_first_loc($$, @p_const.beg_pos);
5552 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5554 | p_const p_lbracket[p_pktbl] p_find rbracket
5556 pop_pktbl(p, $p_pktbl);
5557 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5558 nd_set_first_loc($$, @p_const.beg_pos);
5559 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5561 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5563 pop_pktbl(p, $p_pktbl);
5564 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5565 nd_set_first_loc($$, @p_const.beg_pos);
5566 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5568 | p_const '[' rbracket
5570 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5571 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5572 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5574 | tLBRACK p_args rbracket
5576 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5577 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5579 | tLBRACK p_find rbracket
5581 $$ = new_find_pattern(p, 0, $p_find, &@$);
5582 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5586 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5587 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5588 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5590 | tLBRACE p_pktbl lex_ctxt[ctxt]
5592 p->ctxt.in_kwarg = 0;
5596 pop_pktbl(p, $p_pktbl);
5597 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5598 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5599 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5603 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5604 $$ = new_hash_pattern(p, 0, $$, &@$);
5605 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5607 | tLPAREN p_pktbl p_expr rparen
5609 pop_pktbl(p, $p_pktbl);
5611 /*% ripper: $:p_expr %*/
5617 NODE *pre_args = NEW_LIST($1, &@$);
5618 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5619 /*% ripper: [[$:1], Qnil, Qnil] %*/
5623 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5624 /*% ripper: [$:1, Qnil, Qnil] %*/
5628 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5629 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5631 | p_args_head p_rest
5633 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5634 /*% ripper: [$:1, $:2, Qnil] %*/
5636 | p_args_head p_rest ',' p_args_post
5638 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5639 /*% ripper: [$:1, $:2, $:4] %*/
5644p_args_head : p_arg ','
5645 | p_args_head p_arg ','
5647 $$ = list_concat($1, $2);
5648 /*% ripper: rb_ary_concat($:1, $:2) %*/
5654 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5655 /*% ripper: [Qnil, $:1, Qnil] %*/
5657 | p_rest ',' p_args_post
5659 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5660 /*% ripper: [Qnil, $:1, $:3] %*/
5664p_find : p_rest ',' p_args_post ',' p_rest
5666 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5667 /*% ripper: [$:1, $:3, $:5] %*/
5672p_rest : tSTAR tIDENTIFIER
5674 error_duplicate_pattern_variable(p, $2, &@2);
5675 /*% ripper: var_field!($:2) %*/
5676 $$ = assignable(p, $2, 0, &@$);
5681 /*% ripper: var_field!(Qnil) %*/
5686 | p_args_post ',' p_arg
5688 $$ = list_concat($1, $3);
5689 /*% ripper: rb_ary_concat($:1, $:3) %*/
5695 $$ = NEW_LIST($1, &@$);
5696 /*% ripper: [$:1] %*/
5700p_kwargs : p_kwarg ',' p_any_kwrest
5702 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5703 /*% ripper: [$:1, $:3] %*/
5707 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5708 /*% ripper: [$:1, Qnil] %*/
5712 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5713 /*% ripper: [$:1, Qnil] %*/
5717 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5718 /*% ripper: [[], $:1] %*/
5723 /*% ripper[brace]: [$:1] %*/
5726 $$ = list_concat($1, $3);
5727 /*% ripper: rb_ary_push($:1, $:3) %*/
5731p_kw : p_kw_label p_expr
5733 error_duplicate_pattern_key(p, $1, &@1);
5734 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5735 /*% ripper: [$:1, $:2] %*/
5739 error_duplicate_pattern_key(p, $1, &@1);
5740 if ($1 && !is_local_id($1)) {
5741 yyerror1(&@1, "key must be valid as local variables");
5743 error_duplicate_pattern_variable(p, $1, &@1);
5744 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5745 /*% ripper: [$:1, Qnil] %*/
5750 | tSTRING_BEG string_contents tLABEL_END
5752 YYLTYPE loc = code_loc_gen(&@1, &@3);
5753 if (!$2 || nd_type_p($2, NODE_STR)) {
5754 NODE *node = dsym_node(p, $2, &loc);
5755 $$ = rb_sym2id(rb_node_sym_string_val(node));
5758 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5759 $$ = rb_intern_str(STR_NEW0());
5765p_kwrest : kwrest_mark tIDENTIFIER
5768 /*% ripper: var_field!($:2) %*/
5773 /*% ripper: Qnil %*/
5777p_kwnorest : kwrest_mark keyword_nil
5783p_any_kwrest : p_kwrest
5787 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5791p_value : p_primitive
5792 | range_expr(p_primitive)
5798p_primitive : inline_primary
5801 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5802 /*% ripper: var_ref!($:1) %*/
5807p_variable : tIDENTIFIER
5809 error_duplicate_pattern_variable(p, $1, &@1);
5810 /*% ripper: var_field!($:1) %*/
5811 $$ = assignable(p, $1, 0, &@$);
5815p_var_ref : '^' tIDENTIFIER
5817 NODE *n = gettable(p, $2, &@$);
5821 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5822 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5825 /*% ripper: var_ref!($:2) %*/
5829 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5830 /*% ripper: var_ref!($:2) %*/
5834p_expr_ref : '^' tLPAREN expr_value rparen
5836 $$ = NEW_BLOCK($3, &@$);
5837 /*% ripper: begin!($:3) %*/
5841p_const : tCOLON3 cname
5843 $$ = NEW_COLON3($2, &@$, &@1, &@2);
5844 /*% ripper: top_const_ref!($:2) %*/
5846 | p_const tCOLON2 cname
5848 $$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
5849 /*% ripper: const_path_ref!($:1, $:3) %*/
5853 $$ = gettable(p, $1, &@$);
5854 /*% ripper: var_ref!($:1) %*/
5858opt_rescue : k_rescue exc_list exc_var then
5864 err = NEW_ERRINFO(&@3);
5865 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5867 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5877 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5884 $$ = NEW_LIST($1, &@$);
5885 /*% ripper: rb_ary_new3(1, $:1) %*/
5889 if (!($$ = splat_array($1))) $$ = $1;
5902opt_ensure : k_ensure stmts terms?
5904 p->ctxt.in_rescue = $1.in_rescue;
5906 void_expr(p, void_stmts(p, $$));
5907 /*% ripper: ensure!($:2) %*/
5919 $$ = NEW_STR(STRING_NEW0(), &@$);
5922 $$ = evstr2dstr(p, $1);
5932 $$ = literal_concat(p, $1, $2, &@$);
5933 /*% ripper: string_concat!($:1, $:2) %*/
5937string1 : tSTRING_BEG string_contents tSTRING_END
5939 $$ = heredoc_dedent(p, $2);
5940 if ($$) nd_set_loc($$, &@$);
5942 if (p->heredoc_indent > 0) {
5943 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5944 p->heredoc_indent = 0;
5946 /*% ripper: string_literal!($:$) %*/
5950xstring : tXSTRING_BEG xstring_contents tSTRING_END
5952 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5954 if (p->heredoc_indent > 0) {
5955 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
5956 p->heredoc_indent = 0;
5958 /*% ripper: xstring_literal!($:$) %*/
5962regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5964 $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3);
5965 /*% ripper: regexp_literal!($:2, $:3) %*/
5969words : words(tWORDS_BEG, word_list)
5972word_list : /* none */
5975 /*% ripper: words_new! %*/
5977 | word_list word ' '+
5979 $$ = list_append(p, $1, evstr2dstr(p, $2));
5980 /*% ripper: words_add!($:1, $:2) %*/
5984word : string_content
5985 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
5986 | word string_content
5988 $$ = literal_concat(p, $1, $2, &@$);
5989 /*% ripper: word_add!($:1, $:2) %*/
5993symbols : words(tSYMBOLS_BEG, symbol_list)
5996symbol_list : /* none */
5999 /*% ripper: symbols_new! %*/
6001 | symbol_list word ' '+
6003 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
6004 /*% ripper: symbols_add!($:1, $:2) %*/
6008qwords : words(tQWORDS_BEG, qword_list)
6011qsymbols : words(tQSYMBOLS_BEG, qsym_list)
6014qword_list : /* none */
6017 /*% ripper: qwords_new! %*/
6019 | qword_list tSTRING_CONTENT ' '+
6021 $$ = list_append(p, $1, $2);
6022 /*% ripper: qwords_add!($:1, $:2) %*/
6026qsym_list : /* none */
6029 /*% ripper: qsymbols_new! %*/
6031 | qsym_list tSTRING_CONTENT ' '+
6033 $$ = symbol_append(p, $1, $2);
6034 /*% ripper: qsymbols_add!($:1, $:2) %*/
6038string_contents : /* none */
6041 /*% ripper: string_content! %*/
6043 | string_contents string_content
6045 $$ = literal_concat(p, $1, $2, &@$);
6046 /*% ripper: string_add!($:1, $:2) %*/
6050xstring_contents: /* none */
6053 /*% ripper: xstring_new! %*/
6055 | xstring_contents string_content
6057 $$ = literal_concat(p, $1, $2, &@$);
6058 /*% ripper: xstring_add!($:1, $:2) %*/
6062regexp_contents : /* none */
6065 /*% ripper: regexp_new! %*/
6067 | regexp_contents string_content
6069 NODE *head = $1, *tail = $2;
6077 switch (nd_type(head)) {
6079 head = str2dstr(p, head);
6084 head = list_append(p, NEW_DSTR(0, &@$), head);
6087 $$ = list_append(p, head, tail);
6089 /*% ripper: regexp_add!($:1, $:2) %*/
6093string_content : tSTRING_CONTENT
6094 /*% ripper[brace]: $:1 %*/
6097 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6098 $$ = p->lex.strterm;
6100 SET_LEX_STATE(EXPR_BEG);
6104 p->lex.strterm = $2;
6105 $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC);
6106 nd_set_line($$, @3.end_pos.lineno);
6107 /*% ripper: string_dvar!($:3) %*/
6109 | tSTRING_DBEG[state]
6113 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6114 $$ = p->lex.strterm;
6116 SET_LEX_STATE(EXPR_BEG);
6119 $$ = p->lex.brace_nest;
6120 p->lex.brace_nest = 0;
6123 $$ = p->heredoc_indent;
6124 p->heredoc_indent = 0;
6126 compstmt(stmts) string_dend
6130 p->lex.strterm = $term;
6131 SET_LEX_STATE($state);
6132 p->lex.brace_nest = $brace;
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 if (RNODE_LIST(assocs)->nd_head) {
6645 NODE *n = RNODE_LIST(tail)->nd_next;
6646 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6647 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6649 tail = RNODE_HASH(n)->nd_head;
6653 assocs = list_concat(assocs, tail);
6657 /*% ripper: rb_ary_push($:1, $:3) %*/
6661assoc : arg_value tASSOC arg_value
6663 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6664 /*% ripper: assoc_new!($:1, $:3) %*/
6668 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6669 /*% ripper: assoc_new!($:1, $:2) %*/
6673 NODE *val = gettable(p, $1, &@$);
6674 if (!val) val = NEW_ERROR(&@$);
6675 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6676 /*% ripper: assoc_new!($:1, Qnil) %*/
6678 | tSTRING_BEG string_contents tLABEL_END arg_value
6680 YYLTYPE loc = code_loc_gen(&@1, &@3);
6681 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6682 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6686 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6687 /*% ripper: assoc_splat!($:2) %*/
6691 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6692 $$ = list_append(p, NEW_LIST(0, &@$),
6693 NEW_LVAR(idFWD_KWREST, &@$));
6694 /*% ripper: assoc_splat!(Qnil) %*/
6698%rule %inline operation : ident_or_const
6702operation2 : operation
6706operation3 : tIDENTIFIER
6740 if (p->ctxt.in_defined) {
6741 p->ctxt.has_trailing_semicolon = 1;
6746 @$.end_pos = @$.beg_pos;
6752 | terms ';' {yyerrok;}
6758 /*% ripper: Qnil %*/
6765# define yylval (*p->lval)
6767static int regx_options(struct parser_params*);
6768static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6769static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6770static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6771static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6773#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6775# define set_yylval_node(x) { \
6777 rb_parser_set_location(p, &_cur_loc); \
6778 yylval.node = (x); \
6779 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6781# define set_yylval_str(x) \
6783 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6784 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6786# define set_yylval_num(x) { \
6788 set_parser_s_value(x); \
6790# define set_yylval_id(x) (yylval.id = (x))
6791# define set_yylval_name(x) { \
6792 (yylval.id = (x)); \
6793 set_parser_s_value(ID2SYM(x)); \
6795# define yylval_id() (yylval.id)
6797#define set_yylval_noname() set_yylval_id(keyword_nil)
6798#define has_delayed_token(p) (p->delayed.token != NULL)
6801#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6802#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6805parser_has_token(struct parser_params *p)
6807 const char *const pcur = p->lex.pcur;
6808 const char *const ptok = p->lex.ptok;
6809 if (p->keep_tokens && (pcur < ptok)) {
6810 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6811 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6820 case '"': return "\\\"";
6821 case '\\': return "\\\\";
6822 case '\0': return "\\0";
6823 case '\n': return "\\n";
6824 case '\r': return "\\r";
6825 case '\t': return "\\t";
6826 case '\f': return "\\f";
6827 case '\013': return "\\v";
6828 case '\010': return "\\b";
6829 case '\007': return "\\a";
6830 case '\033': return "\\e";
6831 case '\x7f': return "\\c?";
6836static rb_parser_string_t *
6837rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6839 rb_encoding *enc = p->enc;
6840 const char *ptr = str->ptr;
6841 const char *pend = ptr + str->len;
6842 const char *prev = ptr;
6843 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6844 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6846 while (ptr < pend) {
6849 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6850 if (!MBCLEN_CHARFOUND_P(n)) {
6851 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6852 n = rb_enc_mbminlen(enc);
6854 n = (int)(pend - ptr);
6856 c = *ptr & 0xf0 >> 4;
6857 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6859 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6860 parser_str_cat(result, charbuf, 4);
6865 n = MBCLEN_CHARFOUND_LEN(n);
6866 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6868 cc = escaped_char(c);
6870 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6871 parser_str_cat_cstr(result, cc);
6874 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
6877 if (ptr - n > prev) {
6878 parser_str_cat(result, prev, ptr - n - prev);
6881 parser_str_cat(result, prev, ptr - prev);
6885 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6891parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
6893 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
6894 token->id = p->token_id;
6895 token->type_name = parser_token2char(p, t);
6897 token->loc.beg_pos = p->yylloc->beg_pos;
6898 token->loc.end_pos = p->yylloc->end_pos;
6899 rb_parser_ary_push_ast_token(p, p->tokens, token);
6903 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
6904 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
6905 line, token->id, token->type_name, str_escaped->ptr,
6906 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
6907 token->loc.end_pos.lineno, token->loc.end_pos.column);
6908 rb_parser_string_free(p, str_escaped);
6913parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6915 debug_token_line(p, "parser_dispatch_scan_event", line);
6917 if (!parser_has_token(p)) return;
6919 RUBY_SET_YYLLOC(*p->yylloc);
6921 if (p->keep_tokens) {
6922 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
6923 parser_append_tokens(p, str, t, line);
6929#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6931parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6933 debug_token_line(p, "parser_dispatch_delayed_token", line);
6935 if (!has_delayed_token(p)) return;
6937 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6939 if (p->keep_tokens) {
6940 /* p->delayed.token is freed by rb_parser_tokens_free */
6941 parser_append_tokens(p, p->delayed.token, t, line);
6944 rb_parser_string_free(p, p->delayed.token);
6947 p->delayed.token = NULL;
6950#define literal_flush(p, ptr) ((void)(ptr))
6953ripper_has_scan_event(struct parser_params *p)
6955 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6956 return p->lex.pcur > p->lex.ptok;
6960ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6962 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6963 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6964 RUBY_SET_YYLLOC(*p->yylloc);
6970ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6972 if (!ripper_has_scan_event(p)) return;
6974 set_parser_s_value(ripper_scan_event_val(p, t));
6976#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6979ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6981 /* save and adjust the location to delayed token for callbacks */
6982 int saved_line = p->ruby_sourceline;
6983 const char *saved_tokp = p->lex.ptok;
6986 if (!has_delayed_token(p)) return;
6987 p->ruby_sourceline = p->delayed.beg_line;
6988 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6989 str = rb_str_new_mutable_parser_string(p->delayed.token);
6990 rb_parser_string_free(p, p->delayed.token);
6991 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
6992 set_parser_s_value(s_value);
6993 p->delayed.token = NULL;
6994 p->ruby_sourceline = saved_line;
6995 p->lex.ptok = saved_tokp;
6997#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7001is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7003 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7007peek_word_at(struct parser_params *p, const char *str, size_t len, int at)
7009 const char *ptr = p->lex.pcur + at;
7010 if (lex_eol_ptr_n_p(p, ptr, len-1)) return false;
7011 if (memcmp(ptr, str, len)) return false;
7012 if (lex_eol_ptr_n_p(p, ptr, len)) return true;
7014 case '!': case '?': return false;
7016 return !is_identchar(p, ptr+len, p->lex.pend, p->enc);
7020parser_is_identchar(struct parser_params *p)
7022 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7026parser_isascii(struct parser_params *p)
7028 return ISASCII(*(p->lex.pcur-1));
7032token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7034 int column = 1, nonspc = 0, i;
7035 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7037 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7040 if (*ptr != ' ' && *ptr != '\t') {
7045 ptinfo->beg = loc->beg_pos;
7046 ptinfo->indent = column;
7047 ptinfo->nonspc = nonspc;
7051token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7055 if (!p->token_info_enabled) return;
7056 ptinfo = ALLOC(token_info);
7057 ptinfo->token = token;
7058 ptinfo->next = p->token_info;
7059 token_info_setup(ptinfo, p->lex.pbeg, loc);
7061 p->token_info = ptinfo;
7065token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7067 token_info *ptinfo_beg = p->token_info;
7069 if (!ptinfo_beg) return;
7071 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7072 token_info_warn(p, token, ptinfo_beg, 1, loc);
7074 p->token_info = ptinfo_beg->next;
7075 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7079token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7081 token_info *ptinfo_beg = p->token_info;
7083 if (!ptinfo_beg) return;
7084 p->token_info = ptinfo_beg->next;
7086 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7087 ptinfo_beg->beg.column != beg_pos.column ||
7088 strcmp(ptinfo_beg->token, token)) {
7089 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7090 beg_pos.lineno, beg_pos.column, token,
7091 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7095 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7099token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7101 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7102 if (!p->token_info_enabled) return;
7103 if (!ptinfo_beg) return;
7104 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7105 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7106 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7107 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7108 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7109 rb_warn3L(ptinfo_end->beg.lineno,
7110 "mismatched indentations at '%s' with '%s' at %d",
7111 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7115parser_precise_mbclen(struct parser_params *p, const char *ptr)
7117 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7118 if (!MBCLEN_CHARFOUND_P(len)) {
7119 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7127parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7129 rb_parser_string_t *str;
7130 int lineno = p->ruby_sourceline;
7134 else if (yylloc->beg_pos.lineno == lineno) {
7135 str = p->lex.lastline;
7140 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7144parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7150 yylloc = RUBY_SET_YYLLOC(current);
7152 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7153 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7157 parser_compile_error(p, yylloc, "%s", msg);
7158 parser_show_error_line(p, yylloc);
7163parser_yyerror0(struct parser_params *p, const char *msg)
7166 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7170ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7173 const int max_line_margin = 30;
7174 const char *ptr, *ptr_end, *pt, *pb;
7175 const char *pre = "", *post = "", *pend;
7176 const char *code = "", *caret = "";
7178 const char *const pbeg = PARSER_STRING_PTR(str);
7183 if (!yylloc) return;
7184 pend = rb_parser_string_end(str);
7185 if (pend > pbeg && pend[-1] == '\n') {
7186 if (--pend > pbeg && pend[-1] == '\r') --pend;
7190 if (lineno == yylloc->end_pos.lineno &&
7191 (pend - pbeg) > yylloc->end_pos.column) {
7192 pt = pbeg + yylloc->end_pos.column;
7196 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7197 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7199 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7200 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7202 len = ptr_end - ptr;
7205 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7206 if (ptr > pbeg) pre = "...";
7208 if (ptr_end < pend) {
7209 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7210 if (ptr_end < pend) post = "...";
7214 if (lineno == yylloc->beg_pos.lineno) {
7215 pb += yylloc->beg_pos.column;
7216 if (pb > pt) pb = pt;
7218 if (pb < ptr) pb = ptr;
7219 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7222 if (RTEST(errbuf)) {
7223 mesg = rb_attr_get(errbuf, idMesg);
7224 if (char_at_end(p, mesg, '\n') != '\n')
7225 rb_str_cat_cstr(mesg, "\n");
7228 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7230 if (!errbuf && rb_stderr_tty_p()) {
7231#define CSI_BEGIN "\033["
7234 CSI_BEGIN""CSI_SGR"%s" /* pre */
7235 CSI_BEGIN"1"CSI_SGR"%.*s"
7236 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7237 CSI_BEGIN";1"CSI_SGR"%.*s"
7238 CSI_BEGIN""CSI_SGR"%s" /* post */
7241 (int)(pb - ptr), ptr,
7243 (int)(ptr_end - pt), pt,
7249 len = ptr_end - ptr;
7250 lim = pt < pend ? pt : pend;
7251 i = (int)(lim - ptr);
7252 buf = ALLOCA_N(char, i+2);
7257 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7263 memset(p2, '~', (lim - ptr));
7267 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7268 pre, (int)len, code, post,
7271 if (!errbuf) rb_write_error_str(mesg);
7276parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7278 const char *pcur = 0, *ptok = 0;
7279 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7280 p->ruby_sourceline == yylloc->end_pos.lineno) {
7283 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7284 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7286 parser_yyerror0(p, msg);
7295parser_yyerror0(struct parser_params *p, const char *msg)
7297 dispatch1(parse_error, STR_NEW2(msg));
7303parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7309vtable_size(const struct vtable *tbl)
7311 if (!DVARS_TERMINAL_P(tbl)) {
7319static struct vtable *
7320vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7322 struct vtable *tbl = ALLOC(struct vtable);
7325 tbl->tbl = ALLOC_N(ID, tbl->capa);
7329 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7334#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7337vtable_free_gen(struct parser_params *p, int line, const char *name,
7342 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7345 if (!DVARS_TERMINAL_P(tbl)) {
7347 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7349 ruby_sized_xfree(tbl, sizeof(*tbl));
7352#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7355vtable_add_gen(struct parser_params *p, int line, const char *name,
7356 struct vtable *tbl, ID id)
7360 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7361 line, name, (void *)tbl, rb_id2name(id));
7364 if (DVARS_TERMINAL_P(tbl)) {
7365 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7368 if (tbl->pos == tbl->capa) {
7369 tbl->capa = tbl->capa * 2;
7370 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7372 tbl->tbl[tbl->pos++] = id;
7374#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7377vtable_pop_gen(struct parser_params *p, int line, const char *name,
7378 struct vtable *tbl, int n)
7381 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7382 line, name, (void *)tbl, n);
7385 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7390#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7393vtable_included(const struct vtable * tbl, ID id)
7397 if (!DVARS_TERMINAL_P(tbl)) {
7398 for (i = 0; i < tbl->pos; i++) {
7399 if (tbl->tbl[i] == id) {
7407static void parser_prepare(struct parser_params *p);
7410e_option_supplied(struct parser_params *p)
7412 return strcmp(p->ruby_sourcefile, "-e") == 0;
7416static NODE *parser_append_options(struct parser_params *p, NODE *node);
7419yycompile0(VALUE arg)
7423 struct parser_params *p = (struct parser_params *)arg;
7426 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7430 if (p->debug_lines) {
7431 p->ast->body.script_lines = p->debug_lines;
7435#define RUBY_DTRACE_PARSE_HOOK(name) \
7436 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7437 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7439 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7441 RUBY_DTRACE_PARSE_HOOK(END);
7445 xfree(p->lex.strterm);
7447 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7448 if (n || p->error_p) {
7449 VALUE mesg = p->error_buffer;
7451 mesg = syntax_error_new();
7453 if (!p->error_tolerant) {
7454 rb_set_errinfo(mesg);
7458 tree = p->eval_tree;
7460 tree = NEW_NIL(&NULL_LOC);
7463 rb_parser_ary_t *tokens = p->tokens;
7465 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7466 prelude = block_append(p, p->eval_tree_begin, body);
7467 RNODE_SCOPE(tree)->nd_body = prelude;
7468 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7469 p->ast->body.coverage_enabled = cov;
7470 if (p->keep_tokens) {
7471 p->ast->node_buffer->tokens = tokens;
7475 p->ast->body.root = tree;
7476 p->ast->body.line_count = p->line_count;
7481yycompile(struct parser_params *p, VALUE fname, int line)
7485 p->ruby_sourcefile_string = Qnil;
7486 p->ruby_sourcefile = "(none)";
7489 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7490 p->ruby_sourcefile = StringValueCStr(fname);
7492 p->ruby_sourceline = line - 1;
7496 p->ast = ast = rb_ast_new();
7497 compile_callback(yycompile0, (VALUE)p);
7509must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7511 rb_encoding *enc = rb_parser_str_get_encoding(s);
7512 if (!rb_enc_asciicompat(enc)) {
7513 rb_raise(rb_eArgError, "invalid source encoding");
7518static rb_parser_string_t *
7519lex_getline(struct parser_params *p)
7521 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7522 if (!line) return 0;
7524 string_buffer_append(p, line);
7525 must_be_ascii_compatible(p, line);
7531rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7534 p->lex.input = input;
7535 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7537 return yycompile(p, fname, line);
7541#define STR_FUNC_ESCAPE 0x01
7542#define STR_FUNC_EXPAND 0x02
7543#define STR_FUNC_REGEXP 0x04
7544#define STR_FUNC_QWORDS 0x08
7545#define STR_FUNC_SYMBOL 0x10
7546#define STR_FUNC_INDENT 0x20
7547#define STR_FUNC_LABEL 0x40
7548#define STR_FUNC_LIST 0x4000
7549#define STR_FUNC_TERM 0x8000
7552 str_label = STR_FUNC_LABEL,
7554 str_dquote = (STR_FUNC_EXPAND),
7555 str_xquote = (STR_FUNC_EXPAND),
7556 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7557 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7558 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7559 str_ssym = (STR_FUNC_SYMBOL),
7560 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7563static rb_parser_string_t *
7564parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7566 rb_parser_string_t *pstr;
7568 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7570 if (!(func & STR_FUNC_REGEXP)) {
7571 if (rb_parser_is_ascii_string(p, pstr)) {
7573 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7574 /* everything is valid in ASCII-8BIT */
7575 enc = rb_ascii8bit_encoding();
7576 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7584strterm_is_heredoc(rb_strterm_t *strterm)
7586 return strterm->heredoc;
7589static rb_strterm_t *
7590new_strterm(struct parser_params *p, int func, int term, int paren)
7592 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7593 strterm->u.literal.func = func;
7594 strterm->u.literal.term = term;
7595 strterm->u.literal.paren = paren;
7599static rb_strterm_t *
7600new_heredoc(struct parser_params *p)
7602 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7603 strterm->heredoc = true;
7607#define peek(p,c) peek_n(p, (c), 0)
7608#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7609#define peekc(p) peekc_n(p, 0)
7610#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7612#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7614parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7616 debug_token_line(p, "add_delayed_token", line);
7619 if (has_delayed_token(p)) {
7620 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7621 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7622 int end_col = (next_line ? 0 : p->delayed.end_col);
7623 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7624 dispatch_delayed_token(p, tSTRING_CONTENT);
7627 if (!has_delayed_token(p)) {
7628 p->delayed.token = rb_parser_string_new(p, 0, 0);
7629 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7630 p->delayed.beg_line = p->ruby_sourceline;
7631 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7633 parser_str_cat(p->delayed.token, tok, end - tok);
7634 p->delayed.end_line = p->ruby_sourceline;
7635 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7641set_lastline(struct parser_params *p, rb_parser_string_t *str)
7643 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7644 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7645 p->lex.lastline = str;
7649nextline(struct parser_params *p, int set_encoding)
7651 rb_parser_string_t *str = p->lex.nextline;
7652 p->lex.nextline = 0;
7657 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7661 if (!p->lex.input || !(str = lex_getline(p))) {
7668 if (p->debug_lines) {
7669 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7670 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7671 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7676 else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) {
7677 /* after here-document without terminator */
7680 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7681 if (p->heredoc_end > 0) {
7682 p->ruby_sourceline = p->heredoc_end;
7685 p->ruby_sourceline++;
7686 set_lastline(p, str);
7692parser_cr(struct parser_params *p, int c)
7694 if (peek(p, '\n')) {
7702nextc0(struct parser_params *p, int set_encoding)
7706 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) {
7707 if (nextline(p, set_encoding)) return -1;
7709 c = (unsigned char)*p->lex.pcur++;
7710 if (UNLIKELY(c == '\r')) {
7711 c = parser_cr(p, c);
7716#define nextc(p) nextc0(p, TRUE)
7719pushback(struct parser_params *p, int c)
7721 if (c == -1) return;
7724 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7729#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7731#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7732#define tok(p) (p)->tokenbuf
7733#define toklen(p) (p)->tokidx
7736looking_at_eol_p(struct parser_params *p)
7738 const char *ptr = p->lex.pcur;
7739 while (!lex_eol_ptr_p(p, ptr)) {
7740 int c = (unsigned char)*ptr++;
7741 int eol = (c == '\n' || c == '#');
7742 if (eol || !ISSPACE(c)) {
7750newtok(struct parser_params *p)
7755 p->tokenbuf = ALLOC_N(char, 60);
7757 if (p->toksiz > 4096) {
7759 REALLOC_N(p->tokenbuf, char, 60);
7765tokspace(struct parser_params *p, int n)
7769 if (p->tokidx >= p->toksiz) {
7770 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7771 REALLOC_N(p->tokenbuf, char, p->toksiz);
7773 return &p->tokenbuf[p->tokidx-n];
7777tokadd(struct parser_params *p, int c)
7779 p->tokenbuf[p->tokidx++] = (char)c;
7780 if (p->tokidx >= p->toksiz) {
7782 REALLOC_N(p->tokenbuf, char, p->toksiz);
7787tok_hex(struct parser_params *p, size_t *numlen)
7791 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7793 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7794 yyerror0("invalid hex escape");
7795 dispatch_scan_event(p, tSTRING_CONTENT);
7798 p->lex.pcur += *numlen;
7802#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7805escaped_control_code(int c)
7831#define WARN_SPACE_CHAR(c, prefix) \
7832 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7835tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7836 int regexp_literal, const char *begin)
7838 const int wide = !begin;
7840 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7842 p->lex.pcur += numlen;
7843 if (p->lex.strterm == NULL ||
7844 strterm_is_heredoc(p->lex.strterm) ||
7845 (p->lex.strterm->u.literal.func != str_regexp)) {
7846 if (!begin) begin = p->lex.pcur;
7847 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7848 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7849 yyerror0("invalid Unicode escape");
7850 dispatch_scan_event(p, tSTRING_CONTENT);
7851 return wide && numlen > 0;
7853 if (codepoint > 0x10ffff) {
7854 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7855 yyerror0("invalid Unicode codepoint (too large)");
7856 dispatch_scan_event(p, tSTRING_CONTENT);
7859 if ((codepoint & 0xfffff800) == 0xd800) {
7860 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7861 yyerror0("invalid Unicode codepoint");
7862 dispatch_scan_event(p, tSTRING_CONTENT);
7866 if (regexp_literal) {
7867 tokcopy(p, (int)numlen);
7869 else if (codepoint >= 0x80) {
7870 rb_encoding *utf8 = rb_utf8_encoding();
7871 if (*encp && utf8 != *encp) {
7872 YYLTYPE loc = RUBY_INIT_YYLLOC();
7873 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7874 parser_show_error_line(p, &loc);
7878 tokaddmbc(p, codepoint, *encp);
7881 tokadd(p, codepoint);
7886static int tokadd_mbchar(struct parser_params *p, int c);
7889tokskip_mbchar(struct parser_params *p)
7891 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7893 p->lex.pcur += len - 1;
7898/* return value is for ?\u3042 */
7900tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7901 int term, int symbol_literal, int regexp_literal)
7904 * If `term` is not -1, then we allow multiple codepoints in \u{}
7905 * upto `term` byte, otherwise we're parsing a character literal.
7906 * And then add the codepoints to the current token.
7908 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7910 const int open_brace = '{', close_brace = '}';
7912 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7914 if (peek(p, open_brace)) { /* handle \u{...} form */
7915 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7917 * Skip parsing validation code and copy bytes as-is until term or
7918 * closing brace, in order to correctly handle extended regexps where
7919 * invalid unicode escapes are allowed in comments. The regexp parser
7920 * does its own validation and will catch any issues.
7922 tokadd(p, open_brace);
7923 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7925 if (c == close_brace) {
7930 else if (c == term) {
7933 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7937 tokadd_mbchar(p, c);
7941 const char *second = NULL;
7942 int c, last = nextc(p);
7943 if (lex_eol_p(p)) goto unterminated;
7944 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7945 while (c != close_brace) {
7946 if (c == term) goto unterminated;
7947 if (second == multiple_codepoints)
7948 second = p->lex.pcur;
7949 if (regexp_literal) tokadd(p, last);
7950 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
7953 while (ISSPACE(c = peekc(p))) {
7954 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7957 if (term == -1 && !second)
7958 second = multiple_codepoints;
7961 if (c != close_brace) {
7963 flush_string_content(p, rb_utf8_encoding(), 0);
7964 yyerror0("unterminated Unicode escape");
7965 dispatch_scan_event(p, tSTRING_CONTENT);
7968 if (second && second != multiple_codepoints) {
7969 const char *pcur = p->lex.pcur;
7970 p->lex.pcur = second;
7971 dispatch_scan_event(p, tSTRING_CONTENT);
7974 yyerror0(multiple_codepoints);
7978 if (regexp_literal) tokadd(p, close_brace);
7982 else { /* handle \uxxxx form */
7983 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
7990#define ESCAPE_CONTROL 1
7991#define ESCAPE_META 2
7994read_escape(struct parser_params *p, int flags, const char *begin)
7999 switch (c = nextc(p)) {
8000 case '\\': /* Backslash */
8003 case 'n': /* newline */
8006 case 't': /* horizontal tab */
8009 case 'r': /* carriage-return */
8012 case 'f': /* form-feed */
8015 case 'v': /* vertical tab */
8018 case 'a': /* alarm(bell) */
8021 case 'e': /* escape */
8024 case '0': case '1': case '2': case '3': /* octal constant */
8025 case '4': case '5': case '6': case '7':
8027 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8028 p->lex.pcur += numlen;
8031 case 'x': /* hex constant */
8032 c = tok_hex(p, &numlen);
8033 if (numlen == 0) return 0;
8036 case 'b': /* backspace */
8039 case 's': /* space */
8043 if (flags & ESCAPE_META) goto eof;
8044 if ((c = nextc(p)) != '-') {
8047 if ((c = nextc(p)) == '\\') {
8053 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8055 else if (c == -1) goto eof;
8056 else if (!ISASCII(c)) {
8061 int c2 = escaped_control_code(c);
8063 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8064 WARN_SPACE_CHAR(c2, "\\M-");
8067 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8070 else if (ISCNTRL(c)) goto eof;
8071 return ((c & 0xff) | 0x80);
8075 if ((c = nextc(p)) != '-') {
8079 if (flags & ESCAPE_CONTROL) goto eof;
8080 if ((c = nextc(p))== '\\') {
8086 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8090 else if (c == -1) goto eof;
8091 else if (!ISASCII(c)) {
8096 int c2 = escaped_control_code(c);
8099 if (flags & ESCAPE_META) {
8100 WARN_SPACE_CHAR(c2, "\\M-");
8103 WARN_SPACE_CHAR(c2, "");
8107 if (flags & ESCAPE_META) {
8108 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8111 WARN_SPACE_CHAR(c2, "\\C-");
8115 else if (ISCNTRL(c)) goto eof;
8121 flush_string_content(p, p->enc, p->lex.pcur - begin);
8122 yyerror0("Invalid escape character syntax");
8123 dispatch_scan_event(p, tSTRING_CONTENT);
8136tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8138 int len = rb_enc_codelen(c, enc);
8139 rb_enc_mbcput(c, tokspace(p, len), enc);
8143tokadd_escape(struct parser_params *p)
8147 const char *begin = p->lex.pcur;
8149 switch (c = nextc(p)) {
8151 return 0; /* just ignore */
8153 case '0': case '1': case '2': case '3': /* octal constant */
8154 case '4': case '5': case '6': case '7':
8156 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8157 if (numlen == 0) goto eof;
8158 p->lex.pcur += numlen;
8159 tokcopy(p, (int)numlen + 1);
8163 case 'x': /* hex constant */
8165 tok_hex(p, &numlen);
8166 if (numlen == 0) return -1;
8167 tokcopy(p, (int)numlen + 2);
8173 flush_string_content(p, p->enc, p->lex.pcur - begin);
8174 yyerror0("Invalid escape character syntax");
8186char_to_option(int c)
8192 val = RE_ONIG_OPTION_IGNORECASE;
8195 val = RE_ONIG_OPTION_EXTEND;
8198 val = RE_ONIG_OPTION_MULTILINE;
8207#define ARG_ENCODING_FIXED 16
8208#define ARG_ENCODING_NONE 32
8209#define ENC_ASCII8BIT 1
8211#define ENC_Windows_31J 3
8215char_to_option_kcode(int c, int *option, int *kcode)
8221 *kcode = ENC_ASCII8BIT;
8222 return (*option = ARG_ENCODING_NONE);
8224 *kcode = ENC_EUC_JP;
8227 *kcode = ENC_Windows_31J;
8234 return (*option = char_to_option(c));
8236 *option = ARG_ENCODING_FIXED;
8241regx_options(struct parser_params *p)
8249 while (c = nextc(p), ISALPHA(c)) {
8251 options |= RE_OPTION_ONCE;
8253 else if (char_to_option_kcode(c, &opt, &kc)) {
8255 if (kc != ENC_ASCII8BIT) kcode = c;
8269 YYLTYPE loc = RUBY_INIT_YYLLOC();
8271 compile_error(p, "unknown regexp option%s - %*s",
8272 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8273 parser_show_error_line(p, &loc);
8275 return options | RE_OPTION_ENCODING(kcode);
8279tokadd_mbchar(struct parser_params *p, int c)
8281 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8282 if (len < 0) return -1;
8284 p->lex.pcur += --len;
8285 if (len > 0) tokcopy(p, len);
8290simple_re_meta(int c)
8293 case '$': case '*': case '+': case '.':
8294 case '?': case '^': case '|':
8295 case ')': case ']': case '}': case '>':
8303parser_update_heredoc_indent(struct parser_params *p, int c)
8305 if (p->heredoc_line_indent == -1) {
8306 if (c == '\n') p->heredoc_line_indent = 0;
8310 p->heredoc_line_indent++;
8313 else if (c == '\t') {
8314 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8315 p->heredoc_line_indent = w * TAB_WIDTH;
8318 else if (c != '\n') {
8319 if (p->heredoc_indent > p->heredoc_line_indent) {
8320 p->heredoc_indent = p->heredoc_line_indent;
8322 p->heredoc_line_indent = -1;
8325 /* Whitespace only line has no indentation */
8326 p->heredoc_line_indent = 0;
8333parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8335 YYLTYPE loc = RUBY_INIT_YYLLOC();
8336 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8337 compile_error(p, "%s mixed within %s source", n1, n2);
8338 parser_show_error_line(p, &loc);
8342parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8344 const char *pos = p->lex.pcur;
8346 parser_mixed_error(p, enc1, enc2);
8351nibble_char_upper(unsigned int c)
8354 return c + (c < 10 ? '0' : 'A' - 10);
8358tokadd_string(struct parser_params *p,
8359 int func, int term, int paren, long *nest,
8360 rb_encoding **encp, rb_encoding **enc)
8365 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8366 int top_of_line = FALSE;
8369#define mixed_error(enc1, enc2) \
8370 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8371#define mixed_escape(beg, enc1, enc2) \
8372 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8374 while ((c = nextc(p)) != -1) {
8375 if (p->heredoc_indent > 0) {
8376 parser_update_heredoc_indent(p, c);
8379 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8385 if (paren && c == paren) {
8388 else if (c == term) {
8389 if (!nest || !*nest) {
8395 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8396 unsigned char c2 = *p->lex.pcur;
8397 if (c2 == '$' || c2 == '@' || c2 == '{') {
8402 else if (c == '\\') {
8406 if (func & STR_FUNC_QWORDS) break;
8407 if (func & STR_FUNC_EXPAND) {
8408 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8419 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8423 if ((func & STR_FUNC_EXPAND) == 0) {
8427 tokadd_utf8(p, enc, term,
8428 func & STR_FUNC_SYMBOL,
8429 func & STR_FUNC_REGEXP);
8433 if (c == -1) return -1;
8435 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8438 if (func & STR_FUNC_REGEXP) {
8444 c = read_escape(p, 0, p->lex.pcur - 1);
8446 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8449 *t++ = nibble_char_upper(c >> 4);
8450 *t++ = nibble_char_upper(c);
8455 if (c == term && !simple_re_meta(c)) {
8460 if ((c = tokadd_escape(p)) < 0)
8462 if (*enc && *enc != *encp) {
8463 mixed_escape(p->lex.ptok+2, *enc, *encp);
8467 else if (func & STR_FUNC_EXPAND) {
8469 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8470 c = read_escape(p, 0, p->lex.pcur - 1);
8472 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8473 /* ignore backslashed spaces in %w */
8475 else if (c != term && !(paren && c == paren)) {
8482 else if (!parser_isascii(p)) {
8487 else if (*enc != *encp) {
8488 mixed_error(*enc, *encp);
8491 if (tokadd_mbchar(p, c) == -1) return -1;
8494 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8502 else if (*enc != *encp) {
8503 mixed_error(*enc, *encp);
8509 top_of_line = (c == '\n');
8513 if (*enc) *encp = *enc;
8517#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8520flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8522 p->lex.pcur -= back;
8523 if (has_delayed_token(p)) {
8524 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8526 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8527 p->delayed.end_line = p->ruby_sourceline;
8528 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8530 dispatch_delayed_token(p, tSTRING_CONTENT);
8531 p->lex.ptok = p->lex.pcur;
8533 dispatch_scan_event(p, tSTRING_CONTENT);
8534 p->lex.pcur += back;
8537/* this can be shared with ripper, since it's independent from struct
8540#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8541#define SPECIAL_PUNCT(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) | \
8545 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8546 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8548const uint_least32_t ruby_global_name_punct_bits[] = {
8557static enum yytokentype
8558parser_peek_variable_name(struct parser_params *p)
8561 const char *ptr = p->lex.pcur;
8563 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8567 if ((c = *ptr) == '-') {
8568 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8571 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8572 return tSTRING_DVAR;
8576 if ((c = *ptr) == '@') {
8577 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8583 p->command_start = TRUE;
8584 yylval.state = p->lex.state;
8585 return tSTRING_DBEG;
8589 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8590 return tSTRING_DVAR;
8594#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8595#define IS_END() IS_lex_state(EXPR_END_ANY)
8596#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8597#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8598#define IS_LABEL_POSSIBLE() (\
8599 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8601#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8602#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8604static inline enum yytokentype
8605parser_string_term(struct parser_params *p, int func)
8607 xfree(p->lex.strterm);
8609 if (func & STR_FUNC_REGEXP) {
8610 set_yylval_num(regx_options(p));
8611 dispatch_scan_event(p, tREGEXP_END);
8612 SET_LEX_STATE(EXPR_END);
8615 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8617 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8620 SET_LEX_STATE(EXPR_END);
8624static enum yytokentype
8625parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8627 int func = quote->func;
8628 int term = quote->term;
8629 int paren = quote->paren;
8631 rb_encoding *enc = p->enc;
8632 rb_encoding *base_enc = 0;
8633 rb_parser_string_t *lit;
8635 if (func & STR_FUNC_TERM) {
8636 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8637 SET_LEX_STATE(EXPR_END);
8638 xfree(p->lex.strterm);
8640 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8643 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8644 while (c != '\n' && ISSPACE(c = nextc(p)));
8647 if (func & STR_FUNC_LIST) {
8648 quote->func &= ~STR_FUNC_LIST;
8651 if (c == term && !quote->nest) {
8652 if (func & STR_FUNC_QWORDS) {
8653 quote->func |= STR_FUNC_TERM;
8654 pushback(p, c); /* dispatch the term at tSTRING_END */
8655 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8658 return parser_string_term(p, func);
8661 if (!ISSPACE(c)) pushback(p, c);
8662 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8666 if ((func & STR_FUNC_EXPAND) && c == '#') {
8667 enum yytokentype t = parser_peek_variable_name(p);
8673 if (tokadd_string(p, func, term, paren, "e->nest,
8674 &enc, &base_enc) == -1) {
8677# define unterminated_literal(mesg) yyerror0(mesg)
8679# define unterminated_literal(mesg) compile_error(p, mesg)
8681 literal_flush(p, p->lex.pcur);
8682 if (func & STR_FUNC_QWORDS) {
8683 /* no content to add, bailing out here */
8684 unterminated_literal("unterminated list meets end of file");
8685 xfree(p->lex.strterm);
8689 if (func & STR_FUNC_REGEXP) {
8690 unterminated_literal("unterminated regexp meets end of file");
8693 unterminated_literal("unterminated string meets end of file");
8695 quote->func |= STR_FUNC_TERM;
8700 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8701 set_yylval_str(lit);
8702 flush_string_content(p, enc, 0);
8704 return tSTRING_CONTENT;
8707static enum yytokentype
8708heredoc_identifier(struct parser_params *p)
8711 * term_len is length of `<<"END"` except `END`,
8712 * in this case term_len is 4 (<, <, " and ").
8714 long len, offset = p->lex.pcur - p->lex.pbeg;
8715 int c = nextc(p), term, func = 0, quote = 0;
8716 enum yytokentype token = tSTRING_BEG;
8721 func = STR_FUNC_INDENT;
8724 else if (c == '~') {
8726 func = STR_FUNC_INDENT;
8732 func |= str_squote; goto quoted;
8734 func |= str_dquote; goto quoted;
8736 token = tXSTRING_BEG;
8737 func |= str_xquote; goto quoted;
8744 while ((c = nextc(p)) != term) {
8745 if (c == -1 || c == '\r' || c == '\n') {
8746 yyerror0("unterminated here document identifier");
8753 if (!parser_is_identchar(p)) {
8755 if (func & STR_FUNC_INDENT) {
8756 pushback(p, indent > 0 ? '~' : '-');
8762 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8763 if (n < 0) return 0;
8765 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8770 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8771 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8772 yyerror0("too long here document identifier");
8773 dispatch_scan_event(p, tHEREDOC_BEG);
8776 p->lex.strterm = new_heredoc(p);
8777 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8778 here->offset = offset;
8779 here->sourceline = p->ruby_sourceline;
8780 here->length = (unsigned)len;
8781 here->quote = quote;
8783 here->lastline = p->lex.lastline;
8786 p->heredoc_indent = indent;
8787 p->heredoc_line_indent = 0;
8792heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8794 rb_parser_string_t *line;
8795 rb_strterm_t *term = p->lex.strterm;
8798 line = here->lastline;
8799 p->lex.lastline = line;
8800 p->lex.pbeg = PARSER_STRING_PTR(line);
8801 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8802 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8803 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8804 p->heredoc_end = p->ruby_sourceline;
8805 p->ruby_sourceline = (int)here->sourceline;
8806 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR;
8812dedent_string_column(const char *str, long len, int width)
8816 for (i = 0; i < len && col < width; i++) {
8817 if (str[i] == ' ') {
8820 else if (str[i] == '\t') {
8821 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8822 if (n > width) break;
8834dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8840 len = PARSER_STRING_LEN(string);
8841 str = PARSER_STRING_PTR(string);
8843 i = dedent_string_column(str, len, width);
8846 rb_parser_str_modify(string);
8847 str = PARSER_STRING_PTR(string);
8848 if (PARSER_STRING_LEN(string) != len)
8849 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8850 MEMMOVE(str, str + i, char, len - i);
8851 rb_parser_str_set_len(p, string, len - i);
8856heredoc_dedent(struct parser_params *p, NODE *root)
8858 NODE *node, *str_node, *prev_node;
8859 int indent = p->heredoc_indent;
8860 rb_parser_string_t *prev_lit = 0;
8862 if (indent <= 0) return root;
8863 if (!root) return root;
8865 prev_node = node = str_node = root;
8866 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8869 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8870 if (nd_fl_newline(str_node)) {
8871 dedent_string(p, lit, indent);
8876 else if (!literal_concat0(p, prev_lit, lit)) {
8880 NODE *end = RNODE_LIST(node)->as.nd_end;
8881 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8883 if (nd_type_p(prev_node, NODE_DSTR))
8884 nd_set_type(prev_node, NODE_STR);
8887 RNODE_LIST(node)->as.nd_end = end;
8892 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8894 if (!nd_type_p(node, NODE_LIST)) break;
8895 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8896 enum node_type type = nd_type(str_node);
8897 if (type == NODE_STR || type == NODE_DSTR) break;
8907whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8909 const char *beg = p->lex.pbeg;
8910 const char *ptr = p->lex.pend;
8912 if (ptr - beg < len) return FALSE;
8913 if (ptr > beg && ptr[-1] == '\n') {
8914 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8915 if (ptr - beg < len) return FALSE;
8917 if (strncmp(eos, ptr -= len, len)) return FALSE;
8919 while (beg < ptr && ISSPACE(*beg)) beg++;
8925word_match_p(struct parser_params *p, const char *word, long len)
8927 if (strncmp(p->lex.pcur, word, len)) return 0;
8928 if (lex_eol_n_p(p, len)) return 1;
8929 int c = (unsigned char)p->lex.pcur[len];
8930 if (ISSPACE(c)) return 1;
8932 case '\0': case '\004': case '\032': return 1;
8937#define NUM_SUFFIX_R (1<<0)
8938#define NUM_SUFFIX_I (1<<1)
8939#define NUM_SUFFIX_ALL 3
8942number_literal_suffix(struct parser_params *p, int mask)
8945 const char *lastp = p->lex.pcur;
8947 while ((c = nextc(p)) != -1) {
8948 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8949 result |= (mask & NUM_SUFFIX_I);
8950 mask &= ~NUM_SUFFIX_I;
8951 /* r after i, rational of complex is disallowed */
8952 mask &= ~NUM_SUFFIX_R;
8955 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8956 result |= (mask & NUM_SUFFIX_R);
8957 mask &= ~NUM_SUFFIX_R;
8960 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8961 p->lex.pcur = lastp;
8970static enum yytokentype
8971set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
8973 enum rb_numeric_type numeric_type = integer_literal;
8975 if (type == tFLOAT) {
8976 numeric_type = float_literal;
8979 if (suffix & NUM_SUFFIX_R) {
8981 numeric_type = rational_literal;
8983 if (suffix & NUM_SUFFIX_I) {
8989 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
8992 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
8995 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
8998 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
8999 (void)numeric_type; /* for ripper */
9002 rb_bug("unexpected token: %d", type);
9004 SET_LEX_STATE(EXPR_END);
9008#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9010parser_dispatch_heredoc_end(struct parser_params *p, int line)
9012 if (has_delayed_token(p))
9013 dispatch_delayed_token(p, tSTRING_CONTENT);
9016 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9017 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9019 if (p->keep_tokens) {
9020 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9021 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9022 parser_append_tokens(p, str, tHEREDOC_END, line);
9026 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9031static enum yytokentype
9032here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9034 int c, func, indent = 0;
9035 const char *eos, *ptr, *ptr_end;
9037 rb_parser_string_t *str = 0;
9038 rb_encoding *enc = p->enc;
9039 rb_encoding *base_enc = 0;
9045 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9047 indent = (func = here->func) & STR_FUNC_INDENT;
9049 if ((c = nextc(p)) == -1) {
9052 if (!has_delayed_token(p)) {
9053 dispatch_scan_event(p, tSTRING_CONTENT);
9055 else if (p->delayed.end_line + 1 == p->ruby_sourceline) {
9056 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9057 if (!(func & STR_FUNC_REGEXP)) {
9058 int cr = ENC_CODERANGE_UNKNOWN;
9059 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9060 if (cr != ENC_CODERANGE_7BIT &&
9061 rb_is_usascii_enc(p->enc) &&
9062 enc != rb_utf8_encoding()) {
9063 enc = rb_ascii8bit_encoding();
9066 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9068 dispatch_delayed_token(p, tSTRING_CONTENT);
9071 dispatch_delayed_token(p, tSTRING_CONTENT);
9072 dispatch_scan_event(p, tSTRING_CONTENT);
9076 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9077 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9080 SET_LEX_STATE(EXPR_END);
9085 /* not beginning of line, cannot be the terminator */
9087 else if (p->heredoc_line_indent == -1) {
9088 /* `heredoc_line_indent == -1` means
9089 * - "after an interpolation in the same line", or
9090 * - "in a continuing line"
9092 p->heredoc_line_indent = 0;
9094 else if (whole_match_p(p, eos, len, indent)) {
9095 dispatch_heredoc_end(p);
9097 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9099 SET_LEX_STATE(EXPR_END);
9103 if (!(func & STR_FUNC_EXPAND)) {
9105 ptr = PARSER_STRING_PTR(p->lex.lastline);
9106 ptr_end = p->lex.pend;
9107 if (ptr_end > ptr) {
9108 switch (ptr_end[-1]) {
9110 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9119 if (p->heredoc_indent > 0) {
9121 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9123 p->heredoc_line_indent = 0;
9127 parser_str_cat(str, ptr, ptr_end - ptr);
9129 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9130 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9132 if (p->heredoc_indent > 0) {
9135 if (nextc(p) == -1) {
9137 rb_parser_string_free(p, str);
9142 } while (!whole_match_p(p, eos, len, indent));
9145 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9148 enum yytokentype t = parser_peek_variable_name(p);
9149 if (p->heredoc_line_indent != -1) {
9150 if (p->heredoc_indent > p->heredoc_line_indent) {
9151 p->heredoc_indent = p->heredoc_line_indent;
9153 p->heredoc_line_indent = -1;
9162 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9163 if (p->eofp) goto error;
9167 if (c == '\\') p->heredoc_line_indent = -1;
9169 str = STR_NEW3(tok(p), toklen(p), enc, func);
9171 set_yylval_str(str);
9173 if (bol) nd_set_fl_newline(yylval.node);
9175 flush_string_content(p, enc, 0);
9176 return tSTRING_CONTENT;
9178 tokadd(p, nextc(p));
9179 if (p->heredoc_indent > 0) {
9183 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9184 if ((c = nextc(p)) == -1) goto error;
9185 } while (!whole_match_p(p, eos, len, indent));
9186 str = STR_NEW3(tok(p), toklen(p), enc, func);
9188 dispatch_heredoc_end(p);
9189 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9191 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9193 /* Preserve s_value for set_yylval_str */
9194 s_value = p->s_value;
9196 set_yylval_str(str);
9198 set_parser_s_value(s_value);
9202 if (bol) nd_set_fl_newline(yylval.node);
9204 return tSTRING_CONTENT;
9210arg_ambiguous(struct parser_params *p, char c)
9214 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9217 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9220 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9225/* returns true value if formal argument error;
9226 * Qtrue, or error message if ripper */
9228formal_argument_error(struct parser_params *p, ID id)
9230 switch (id_type(id)) {
9234# define ERR(mesg) (yyerror0(mesg), Qtrue)
9236# define ERR(mesg) WARN_S(mesg)
9239 return ERR("formal argument cannot be a constant");
9241 return ERR("formal argument cannot be an instance variable");
9243 return ERR("formal argument cannot be a global variable");
9245 return ERR("formal argument cannot be a class variable");
9247 return ERR("formal argument must be local variable");
9250 shadowing_lvar(p, id);
9256lvar_defined(struct parser_params *p, ID id)
9258 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9261/* emacsen -*- hack */
9263parser_encode_length(struct parser_params *p, const char *name, long len)
9267 if (len > 5 && name[nlen = len - 5] == '-') {
9268 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9271 if (len > 4 && name[nlen = len - 4] == '-') {
9272 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9274 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9275 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9276 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9283parser_set_encode(struct parser_params *p, const char *name)
9289 const char *wrong = 0;
9291 case 'e': case 'E': wrong = "external"; break;
9292 case 'i': case 'I': wrong = "internal"; break;
9293 case 'f': case 'F': wrong = "filesystem"; break;
9294 case 'l': case 'L': wrong = "locale"; break;
9296 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9297 idx = rb_enc_find_index(name);
9300 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9302 excargs[0] = rb_eArgError;
9303 excargs[2] = rb_make_backtrace();
9304 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9305 VALUE exc = rb_make_exception(3, excargs);
9306 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9308 rb_ast_free(p->ast);
9313 enc = rb_enc_from_index(idx);
9314 if (!rb_enc_asciicompat(enc)) {
9315 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9320 if (p->debug_lines) {
9322 for (i = 0; i < p->debug_lines->len; i++) {
9323 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9330comment_at_top(struct parser_params *p)
9332 if (p->token_seen) return false;
9333 return (p->line_count == (p->has_shebang ? 2 : 1));
9336typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9337typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9339static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9342magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9344 if (!comment_at_top(p)) {
9347 parser_set_encode(p, val);
9351parser_get_bool(struct parser_params *p, const char *name, const char *val)
9355 if (STRCASECMP(val, "true") == 0) {
9360 if (STRCASECMP(val, "false") == 0) {
9365 return parser_invalid_pragma_value(p, name, val);
9369parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9371 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9376parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9378 int b = parser_get_bool(p, name, val);
9379 if (b >= 0) p->token_info_enabled = b;
9383parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9387 if (p->token_seen) {
9388 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9392 b = parser_get_bool(p, name, val);
9395 p->frozen_string_literal = b;
9399parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9401 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9402 if (*s == ' ' || *s == '\t') continue;
9403 if (*s == '#') break;
9404 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9410 if (STRCASECMP(val, "none") == 0) {
9411 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9416 if (STRCASECMP(val, "literal") == 0) {
9417 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9422 if (STRCASECMP(val, "experimental_copy") == 0) {
9423 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9426 if (STRCASECMP(val, "experimental_everything") == 0) {
9427 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9432 parser_invalid_pragma_value(p, name, val);
9437parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9439 int b = parser_get_bool(p, name, val);
9440 if (b >= 0) p->past_scope_enabled = b;
9444struct magic_comment {
9446 rb_magic_comment_setter_t func;
9447 rb_magic_comment_length_t length;
9450static const struct magic_comment magic_comments[] = {
9451 {"coding", magic_comment_encoding, parser_encode_length},
9452 {"encoding", magic_comment_encoding, parser_encode_length},
9453 {"frozen_string_literal", parser_set_frozen_string_literal},
9454 {"shareable_constant_value", parser_set_shareable_constant_value},
9455 {"warn_indent", parser_set_token_info},
9457 {"warn_past_scope", parser_set_past_scope},
9462magic_comment_marker(const char *str, long len)
9469 if (str[i-1] == '*' && str[i-2] == '-') {
9475 if (i + 1 >= len) return 0;
9476 if (str[i+1] != '-') {
9479 else if (str[i-1] != '-') {
9495parser_magic_comment(struct parser_params *p, const char *str, long len)
9498 VALUE name = 0, val = 0;
9499 const char *beg, *end, *vbeg, *vend;
9500#define str_copy(_s, _p, _n) ((_s) \
9501 ? (void)(rb_str_resize((_s), (_n)), \
9502 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9503 : (void)((_s) = STR_NEW((_p), (_n))))
9505 if (len <= 7) return FALSE;
9506 if (!!(beg = magic_comment_marker(str, len))) {
9507 if (!(end = magic_comment_marker(beg, str + len - beg)))
9511 len = end - beg - 3;
9514 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9516 const struct magic_comment *mc = magic_comments;
9521 for (; len > 0 && *str; str++, --len) {
9523 case '\'': case '"': case ':': case ';':
9526 if (!ISSPACE(*str)) break;
9528 for (beg = str; len > 0; str++, --len) {
9530 case '\'': case '"': case ':': case ';':
9533 if (ISSPACE(*str)) break;
9538 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9541 if (!indicator) return FALSE;
9545 do str++; while (--len > 0 && ISSPACE(*str));
9547 const char *tok_beg = str;
9549 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9562 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9565 const char *tok_end = str;
9567 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9570 while (len > 0 && (ISSPACE(*str))) --len, str++;
9571 if (len) return FALSE;
9575 str_copy(name, beg, n);
9576 s = RSTRING_PTR(name);
9577 for (i = 0; i < n; ++i) {
9578 if (s[i] == '-') s[i] = '_';
9581 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9584 n = (*mc->length)(p, vbeg, n);
9586 str_copy(val, vbeg, n);
9587 p->lex.ptok = tok_beg;
9588 p->lex.pcur = tok_end;
9589 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9592 } while (++mc < magic_comments + numberof(magic_comments));
9594 str_copy(val, vbeg, vend - vbeg);
9595 dispatch2(magic_comment, name, val);
9603set_file_encoding(struct parser_params *p, const char *str, const char *send)
9606 const char *beg = str;
9610 if (send - str <= 6) return;
9612 case 'C': case 'c': str += 6; continue;
9613 case 'O': case 'o': str += 5; continue;
9614 case 'D': case 'd': str += 4; continue;
9615 case 'I': case 'i': str += 3; continue;
9616 case 'N': case 'n': str += 2; continue;
9617 case 'G': case 'g': str += 1; continue;
9624 if (ISSPACE(*str)) break;
9627 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9632 if (++str >= send) return;
9633 } while (ISSPACE(*str));
9635 if (*str != '=' && *str != ':') return;
9640 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9641 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9644 parser_set_encode(p, RSTRING_PTR(s));
9645 rb_str_resize(s, 0);
9649parser_prepare(struct parser_params *p)
9651 int c = nextc0(p, FALSE);
9652 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9655 if (peek(p, '!')) p->has_shebang = 1;
9657 case 0xef: /* UTF-8 BOM marker */
9658 if (!lex_eol_n_p(p, 2) &&
9659 (unsigned char)p->lex.pcur[0] == 0xbb &&
9660 (unsigned char)p->lex.pcur[1] == 0xbf) {
9661 p->enc = rb_utf8_encoding();
9664 if (p->debug_lines) {
9665 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9668 p->lex.pbeg = p->lex.pcur;
9673 case -1: /* end of script. */
9677 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9681#define ambiguous_operator(tok, op, syn) ( \
9682 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9683 rb_warning0("even though it seems like "syn""))
9685#define ambiguous_operator(tok, op, syn) \
9686 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9688#define warn_balanced(tok, op, syn) ((void) \
9689 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9690 space_seen && !ISSPACE(c) && \
9691 (ambiguous_operator(tok, op, syn), 0)), \
9692 (enum yytokentype)(tok))
9694static enum yytokentype
9695no_digits(struct parser_params *p)
9697 yyerror0("numeric literal without digits");
9698 if (peek(p, '_')) nextc(p);
9699 /* dummy 0, for tUMINUS_NUM at numeric */
9700 return set_number_literal(p, tINTEGER, 0, 10, 0);
9703static enum yytokentype
9704parse_numeric(struct parser_params *p, int c)
9706 int is_float, seen_point, seen_e, nondigit;
9709 is_float = seen_point = seen_e = nondigit = 0;
9710 SET_LEX_STATE(EXPR_END);
9712 if (c == '-' || c == '+') {
9717 int start = toklen(p);
9719 if (c == 'x' || c == 'X') {
9722 if (c != -1 && ISXDIGIT(c)) {
9725 if (nondigit) break;
9729 if (!ISXDIGIT(c)) break;
9732 } while ((c = nextc(p)) != -1);
9736 if (toklen(p) == start) {
9737 return no_digits(p);
9739 else if (nondigit) goto trailing_uc;
9740 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9741 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9743 if (c == 'b' || c == 'B') {
9746 if (c == '0' || c == '1') {
9749 if (nondigit) break;
9753 if (c != '0' && c != '1') break;
9756 } while ((c = nextc(p)) != -1);
9760 if (toklen(p) == start) {
9761 return no_digits(p);
9763 else if (nondigit) goto trailing_uc;
9764 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9765 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9767 if (c == 'd' || c == 'D') {
9770 if (c != -1 && ISDIGIT(c)) {
9773 if (nondigit) break;
9777 if (!ISDIGIT(c)) break;
9780 } while ((c = nextc(p)) != -1);
9784 if (toklen(p) == start) {
9785 return no_digits(p);
9787 else if (nondigit) goto trailing_uc;
9788 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9789 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9795 if (c == 'o' || c == 'O') {
9796 /* prefixed octal */
9798 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9800 return no_digits(p);
9803 if (c >= '0' && c <= '7') {
9808 if (nondigit) break;
9812 if (c < '0' || c > '9') break;
9813 if (c > '7') goto invalid_octal;
9816 } while ((c = nextc(p)) != -1);
9817 if (toklen(p) > start) {
9820 if (nondigit) goto trailing_uc;
9821 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9822 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9829 if (c > '7' && c <= '9') {
9831 yyerror0("Invalid octal digit");
9833 else if (c == '.' || c == 'e' || c == 'E') {
9839 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9840 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9846 case '0': case '1': case '2': case '3': case '4':
9847 case '5': case '6': case '7': case '8': case '9':
9853 if (nondigit) goto trailing_uc;
9854 if (seen_point || seen_e) {
9859 if (c0 == -1 || !ISDIGIT(c0)) {
9865 seen_point = toklen(p);
9884 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9890 tokadd(p, nondigit);
9894 nondigit = (c == '-' || c == '+') ? c : 0;
9897 case '_': /* `_' in number just ignored */
9898 if (nondigit) goto decode_num;
9912 literal_flush(p, p->lex.pcur - 1);
9913 YYLTYPE loc = RUBY_INIT_YYLLOC();
9914 compile_error(p, "trailing '%c' in number", nondigit);
9915 parser_show_error_line(p, &loc);
9919 enum yytokentype type = tFLOAT;
9921 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9922 if (suffix & NUM_SUFFIX_R) {
9927 if (errno == ERANGE) {
9928 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9932 return set_number_literal(p, type, suffix, 0, seen_point);
9934 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9935 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9938static enum yytokentype
9939parse_qmark(struct parser_params *p, int space_seen)
9943 rb_parser_string_t *lit;
9944 const char *start = p->lex.pcur;
9947 SET_LEX_STATE(EXPR_VALUE);
9952 compile_error(p, "incomplete character syntax");
9955 if (rb_enc_isspace(c, p->enc)) {
9957 int c2 = escaped_control_code(c);
9959 WARN_SPACE_CHAR(c2, "?");
9964 SET_LEX_STATE(EXPR_VALUE);
9969 int w = parser_precise_mbclen(p, start);
9970 if (is_identchar(p, start, p->lex.pend, p->enc) &&
9971 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
9973 const char *ptr = start;
9975 int n = parser_precise_mbclen(p, ptr);
9976 if (n < 0) return -1;
9978 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9979 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
9980 " a conditional operator, put a space after '?'",
9981 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9985 else if (c == '\\') {
9988 enc = rb_utf8_encoding();
9989 tokadd_utf8(p, &enc, -1, 0, 0);
9991 else if (!ISASCII(c = peekc(p)) && c != -1) {
9993 if (tokadd_mbchar(p, c) == -1) return 0;
9996 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10001 if (tokadd_mbchar(p, c) == -1) return 0;
10004 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10005 set_yylval_str(lit);
10006 SET_LEX_STATE(EXPR_END);
10010static enum yytokentype
10011parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10014 const char *ptok = p->lex.pcur;
10022 if (c == -1) goto unterminated;
10025 if (!ISASCII(c)) goto unknown;
10030 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10033 c = parser_precise_mbclen(p, p->lex.pcur);
10034 if (c < 0) return 0;
10036 yyerror0("unknown type of %string");
10042 compile_error(p, "unterminated quoted string meets end of file");
10046 if (term == '(') term = ')';
10047 else if (term == '[') term = ']';
10048 else if (term == '{') term = '}';
10049 else if (term == '<') term = '>';
10052 p->lex.ptok = ptok-1;
10055 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10056 return tSTRING_BEG;
10059 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10060 return tSTRING_BEG;
10063 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10067 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10068 return tQWORDS_BEG;
10071 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10072 return tSYMBOLS_BEG;
10075 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10076 return tQSYMBOLS_BEG;
10079 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10080 return tXSTRING_BEG;
10083 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10084 return tREGEXP_BEG;
10087 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10088 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10092 yyerror0("unknown type of %string");
10096 if ((c = nextc(p)) == '=') {
10097 set_yylval_id('%');
10098 SET_LEX_STATE(EXPR_BEG);
10101 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10104 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10106 return warn_balanced('%', "%%", "string literal");
10110tokadd_ident(struct parser_params *p, int c)
10113 if (tokadd_mbchar(p, c) == -1) return -1;
10115 } while (parser_is_identchar(p));
10121tokenize_ident(struct parser_params *p)
10123 ID ident = TOK_INTERN();
10125 set_yylval_name(ident);
10131parse_numvar(struct parser_params *p)
10135 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10136 const unsigned long nth_ref_max =
10137 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10138 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10139 * turned into a Fixnum, in compile.c */
10141 if (overflow || n > nth_ref_max) {
10142 /* compile_error()? */
10143 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10144 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10151static enum yytokentype
10152parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10154 const char *ptr = p->lex.pcur;
10157 SET_LEX_STATE(EXPR_END);
10158 p->lex.ptok = ptr - 1; /* from '$' */
10162 case '_': /* $_: last read line string */
10164 if (parser_is_identchar(p)) {
10172 case '~': /* $~: match-data */
10173 case '*': /* $*: argv */
10174 case '$': /* $$: pid */
10175 case '?': /* $?: last status */
10176 case '!': /* $!: error string */
10177 case '@': /* $@: error position */
10178 case '/': /* $/: input record separator */
10179 case '\\': /* $\: output record separator */
10180 case ';': /* $;: field separator */
10181 case ',': /* $,: output field separator */
10182 case '.': /* $.: last read line number */
10183 case '=': /* $=: ignorecase */
10184 case ':': /* $:: load path */
10185 case '<': /* $<: default input handle */
10186 case '>': /* $>: default output handle */
10187 case '\"': /* $": already loaded files */
10196 if (parser_is_identchar(p)) {
10197 if (tokadd_mbchar(p, c) == -1) return 0;
10208 case '&': /* $&: last match */
10209 case '`': /* $`: string before last match */
10210 case '\'': /* $': string after last match */
10211 case '+': /* $+: string matches last paren. */
10212 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10217 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10220 case '1': case '2': case '3':
10221 case '4': case '5': case '6':
10222 case '7': case '8': case '9':
10227 } while (c != -1 && ISDIGIT(c));
10229 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10231 c = parse_numvar(p);
10232 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10236 if (!parser_is_identchar(p)) {
10237 YYLTYPE loc = RUBY_INIT_YYLLOC();
10238 if (c == -1 || ISSPACE(c)) {
10239 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10243 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10245 parser_show_error_line(p, &loc);
10246 set_yylval_noname();
10254 if (tokadd_ident(p, c)) return 0;
10255 SET_LEX_STATE(EXPR_END);
10256 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10260 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10261 set_yylval_noname();
10267parser_numbered_param(struct parser_params *p, int n)
10269 if (n < 0) return false;
10271 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10274 if (p->max_numparam == ORDINAL_PARAM) {
10275 compile_error(p, "ordinary parameter is defined");
10278 struct vtable *args = p->lvtbl->args;
10279 if (p->max_numparam < n) {
10280 p->max_numparam = n;
10282 while (n > args->pos) {
10283 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10288static enum yytokentype
10289parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10291 const char *ptr = p->lex.pcur;
10292 enum yytokentype result = tIVAR;
10293 register int c = nextc(p);
10296 p->lex.ptok = ptr - 1; /* from '@' */
10304 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10305 if (c == -1 || !parser_is_identchar(p)) {
10307 RUBY_SET_YYLLOC(loc);
10308 if (result == tIVAR) {
10309 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10312 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10314 parser_show_error_line(p, &loc);
10315 set_yylval_noname();
10316 SET_LEX_STATE(EXPR_END);
10319 else if (ISDIGIT(c)) {
10321 RUBY_SET_YYLLOC(loc);
10322 if (result == tIVAR) {
10323 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10326 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10328 parser_show_error_line(p, &loc);
10329 set_yylval_noname();
10330 SET_LEX_STATE(EXPR_END);
10334 if (tokadd_ident(p, c)) return 0;
10339static enum yytokentype
10340parse_ident(struct parser_params *p, int c, int cmd_state)
10342 enum yytokentype result;
10343 bool is_ascii = true;
10344 const enum lex_state_e last_state = p->lex.state;
10346 int enforce_keyword_end = 0;
10349 if (!ISASCII(c)) is_ascii = false;
10350 if (tokadd_mbchar(p, c) == -1) return 0;
10352 } while (parser_is_identchar(p));
10353 if ((c == '!' || c == '?') && !peek(p, '=')) {
10357 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10358 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10359 result = tIDENTIFIER;
10363 result = tCONSTANT; /* assume provisionally */
10368 if (IS_LABEL_POSSIBLE()) {
10369 if (IS_LABEL_SUFFIX(0)) {
10370 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10378 if (peek_end_expect_token_locations(p)) {
10379 const rb_code_position_t *end_pos;
10380 int lineno, column;
10381 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10383 end_pos = peek_end_expect_token_locations(p)->pos;
10384 lineno = end_pos->lineno;
10385 column = end_pos->column;
10388 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10389 p->ruby_sourceline, beg_pos, lineno, column);
10392 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10393 const struct kwtable *kw;
10395 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10396 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10397 enforce_keyword_end = 1;
10403 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10404 const struct kwtable *kw;
10406 /* See if it is a reserved word. */
10407 kw = rb_reserved_word(tok(p), toklen(p));
10409 enum lex_state_e state = p->lex.state;
10410 if (IS_lex_state_for(state, EXPR_FNAME)) {
10411 SET_LEX_STATE(EXPR_ENDFN);
10412 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10415 SET_LEX_STATE(kw->state);
10416 if (IS_lex_state(EXPR_BEG)) {
10417 p->command_start = TRUE;
10419 if (kw->id[0] == keyword_do) {
10420 if (lambda_beginning_p()) {
10421 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10422 return keyword_do_LAMBDA;
10424 if (COND_P()) return keyword_do_cond;
10425 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10426 return keyword_do_block;
10429 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10432 if (kw->id[0] != kw->id[1])
10433 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10439 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10441 SET_LEX_STATE(EXPR_CMDARG);
10444 SET_LEX_STATE(EXPR_ARG);
10447 else if (p->lex.state == EXPR_FNAME) {
10448 SET_LEX_STATE(EXPR_ENDFN);
10451 SET_LEX_STATE(EXPR_END);
10454 ident = tokenize_ident(p);
10455 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10456 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10457 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10458 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10459 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10465warn_cr(struct parser_params *p)
10469 /* carried over with p->lex.nextline for nextc() */
10470 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10474static enum yytokentype
10475parser_yylex(struct parser_params *p)
10478 int space_seen = 0;
10481 enum lex_state_e last_state;
10482 int fallthru = FALSE;
10483 int token_seen = p->token_seen;
10485 if (p->lex.strterm) {
10486 if (strterm_is_heredoc(p->lex.strterm)) {
10488 return here_document(p, &p->lex.strterm->u.heredoc);
10492 return parse_string(p, &p->lex.strterm->u.literal);
10495 cmd_state = p->command_start;
10496 p->command_start = FALSE;
10497 p->token_seen = TRUE;
10502 last_state = p->lex.state;
10503 switch (c = nextc(p)) {
10504 case '\0': /* NUL */
10505 case '\004': /* ^D */
10506 case '\032': /* ^Z */
10507 case -1: /* end of script. */
10510 if (p->end_expect_token_locations) {
10511 pop_end_expect_token_locations(p);
10512 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10516 /* Set location for end-of-input because dispatch_scan_event is not called. */
10517 RUBY_SET_YYLLOC(*p->yylloc);
10518 return END_OF_INPUT;
10524 case ' ': case '\t': case '\f':
10525 case '\13': /* '\v' */
10527 while ((c = nextc(p))) {
10532 case ' ': case '\t': case '\f':
10533 case '\13': /* '\v' */
10541 dispatch_scan_event(p, tSP);
10547 case '#': /* it's a comment */
10548 p->token_seen = token_seen;
10549 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10550 /* no magic_comment in shebang line */
10551 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10552 if (comment_at_top(p)) {
10553 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10556 p->lex.pcur = pcur, p->lex.ptok = ptok;
10558 dispatch_scan_event(p, tCOMMENT);
10562 p->token_seen = token_seen;
10563 rb_parser_string_t *prevline = p->lex.lastline;
10564 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10565 !IS_lex_state(EXPR_LABELED));
10566 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10568 dispatch_scan_event(p, tIGNORED_NL);
10571 if (!c && p->ctxt.in_kwarg) {
10572 goto normal_newline;
10577 switch (c = nextc(p)) {
10578 case ' ': case '\t': case '\f': case '\r':
10579 case '\13': /* '\v' */
10585 dispatch_scan_event(p, tSP);
10590 if (peek_word_at(p, "nd", 2, 0)) goto leading_logical;
10593 if (peek_word_at(p, "r", 1, 0)) goto leading_logical;
10596 if (peek(p, '|')) goto leading_logical;
10599 if (peek(p, '&')) {
10602 dispatch_delayed_token(p, tIGNORED_NL);
10608 dispatch_delayed_token(p, tIGNORED_NL);
10609 if (peek(p, '.') == (c == '&')) {
10611 dispatch_scan_event(p, tSP);
10617 p->ruby_sourceline--;
10618 p->lex.nextline = p->lex.lastline;
10619 set_lastline(p, prevline);
10620 case -1: /* EOF no decrement*/
10621 if (c == -1 && space_seen) {
10622 dispatch_scan_event(p, tSP);
10627 RUBY_SET_YYLLOC(*p->yylloc);
10629 goto normal_newline;
10633 p->command_start = TRUE;
10634 SET_LEX_STATE(EXPR_BEG);
10638 if ((c = nextc(p)) == '*') {
10639 if ((c = nextc(p)) == '=') {
10640 set_yylval_id(idPow);
10641 SET_LEX_STATE(EXPR_BEG);
10645 if (IS_SPCARG(c)) {
10646 rb_warning0("'**' interpreted as argument prefix");
10649 else if (IS_BEG()) {
10653 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10658 set_yylval_id('*');
10659 SET_LEX_STATE(EXPR_BEG);
10663 if (IS_SPCARG(c)) {
10664 rb_warning0("'*' interpreted as argument prefix");
10667 else if (IS_BEG()) {
10671 c = warn_balanced('*', "*", "argument prefix");
10674 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10679 if (IS_AFTER_OPERATOR()) {
10680 SET_LEX_STATE(EXPR_ARG);
10686 SET_LEX_STATE(EXPR_BEG);
10699 /* skip embedded rd document */
10700 if (word_match_p(p, "begin", 5)) {
10701 int first_p = TRUE;
10704 dispatch_scan_event(p, tEMBDOC_BEG);
10708 dispatch_scan_event(p, tEMBDOC);
10713 compile_error(p, "embedded document meets end of file");
10714 return END_OF_INPUT;
10716 if (c == '=' && word_match_p(p, "end", 3)) {
10722 dispatch_scan_event(p, tEMBDOC_END);
10727 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10728 if ((c = nextc(p)) == '=') {
10729 if ((c = nextc(p)) == '=') {
10738 else if (c == '>') {
10747 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10749 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10750 enum yytokentype token = heredoc_identifier(p);
10751 if (token) return token < 0 ? 0 : token;
10753 if (IS_AFTER_OPERATOR()) {
10754 SET_LEX_STATE(EXPR_ARG);
10757 if (IS_lex_state(EXPR_CLASS))
10758 p->command_start = TRUE;
10759 SET_LEX_STATE(EXPR_BEG);
10762 if ((c = nextc(p)) == '>') {
10769 if ((c = nextc(p)) == '=') {
10770 set_yylval_id(idLTLT);
10771 SET_LEX_STATE(EXPR_BEG);
10775 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10781 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10782 if ((c = nextc(p)) == '=') {
10786 if ((c = nextc(p)) == '=') {
10787 set_yylval_id(idGTGT);
10788 SET_LEX_STATE(EXPR_BEG);
10798 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10799 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10800 p->lex.ptok = p->lex.pcur-1;
10801 return tSTRING_BEG;
10804 if (IS_lex_state(EXPR_FNAME)) {
10805 SET_LEX_STATE(EXPR_ENDFN);
10808 if (IS_lex_state(EXPR_DOT)) {
10810 SET_LEX_STATE(EXPR_CMDARG);
10812 SET_LEX_STATE(EXPR_ARG);
10815 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10816 return tXSTRING_BEG;
10819 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10820 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10821 p->lex.ptok = p->lex.pcur-1;
10822 return tSTRING_BEG;
10825 return parse_qmark(p, space_seen);
10828 if ((c = nextc(p)) == '&') {
10829 SET_LEX_STATE(EXPR_BEG);
10830 if ((c = nextc(p)) == '=') {
10831 set_yylval_id(idANDOP);
10832 SET_LEX_STATE(EXPR_BEG);
10838 else if (c == '=') {
10839 set_yylval_id('&');
10840 SET_LEX_STATE(EXPR_BEG);
10843 else if (c == '.') {
10844 set_yylval_id(idANDDOT);
10845 SET_LEX_STATE(EXPR_DOT);
10849 if (IS_SPCARG(c)) {
10851 (c = peekc_n(p, 1)) == -1 ||
10852 !(c == '\'' || c == '"' ||
10853 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10854 rb_warning0("'&' interpreted as argument prefix");
10858 else if (IS_BEG()) {
10862 c = warn_balanced('&', "&", "argument prefix");
10864 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10868 if ((c = nextc(p)) == '|') {
10869 SET_LEX_STATE(EXPR_BEG);
10870 if ((c = nextc(p)) == '=') {
10871 set_yylval_id(idOROP);
10872 SET_LEX_STATE(EXPR_BEG);
10876 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10884 set_yylval_id('|');
10885 SET_LEX_STATE(EXPR_BEG);
10888 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10894 if (IS_AFTER_OPERATOR()) {
10895 SET_LEX_STATE(EXPR_ARG);
10903 set_yylval_id('+');
10904 SET_LEX_STATE(EXPR_BEG);
10907 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10908 SET_LEX_STATE(EXPR_BEG);
10910 if (c != -1 && ISDIGIT(c)) {
10911 return parse_numeric(p, '+');
10915 SET_LEX_STATE(EXPR_BEG);
10917 return warn_balanced('+', "+", "unary operator");
10921 if (IS_AFTER_OPERATOR()) {
10922 SET_LEX_STATE(EXPR_ARG);
10930 set_yylval_id('-');
10931 SET_LEX_STATE(EXPR_BEG);
10935 SET_LEX_STATE(EXPR_ENDFN);
10936 yylval.num = p->lex.lpar_beg;
10937 p->lex.lpar_beg = p->lex.paren_nest;
10940 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10941 SET_LEX_STATE(EXPR_BEG);
10943 if (c != -1 && ISDIGIT(c)) {
10944 return tUMINUS_NUM;
10948 SET_LEX_STATE(EXPR_BEG);
10950 return warn_balanced('-', "-", "unary operator");
10953 int is_beg = IS_BEG();
10954 SET_LEX_STATE(EXPR_BEG);
10955 if ((c = nextc(p)) == '.') {
10956 if ((c = nextc(p)) == '.') {
10957 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
10958 SET_LEX_STATE(EXPR_ENDARG);
10961 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10962 rb_warn0("... at EOL, should be parenthesized?");
10964 return is_beg ? tBDOT3 : tDOT3;
10967 return is_beg ? tBDOT2 : tDOT2;
10970 if (c != -1 && ISDIGIT(c)) {
10971 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10972 parse_numeric(p, '.');
10973 if (ISDIGIT(prev)) {
10974 yyerror0("unexpected fraction part after numeric literal");
10977 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10979 SET_LEX_STATE(EXPR_END);
10980 p->lex.ptok = p->lex.pcur;
10983 set_yylval_id('.');
10984 SET_LEX_STATE(EXPR_DOT);
10988 case '0': case '1': case '2': case '3': case '4':
10989 case '5': case '6': case '7': case '8': case '9':
10990 return parse_numeric(p, c);
10995 SET_LEX_STATE(EXPR_ENDFN);
10996 p->lex.paren_nest--;
11002 SET_LEX_STATE(EXPR_END);
11003 p->lex.paren_nest--;
11007 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11008 if (!p->lex.brace_nest--) return tSTRING_DEND;
11011 SET_LEX_STATE(EXPR_END);
11012 p->lex.paren_nest--;
11018 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11019 SET_LEX_STATE(EXPR_BEG);
11022 set_yylval_id(idCOLON2);
11023 SET_LEX_STATE(EXPR_DOT);
11026 if (IS_END() || ISSPACE(c) || c == '#') {
11028 c = warn_balanced(':', ":", "symbol literal");
11029 SET_LEX_STATE(EXPR_BEG);
11034 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11037 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11043 SET_LEX_STATE(EXPR_FNAME);
11048 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11049 return tREGEXP_BEG;
11051 if ((c = nextc(p)) == '=') {
11052 set_yylval_id('/');
11053 SET_LEX_STATE(EXPR_BEG);
11057 if (IS_SPCARG(c)) {
11058 arg_ambiguous(p, '/');
11059 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11060 return tREGEXP_BEG;
11062 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11063 return warn_balanced('/', "/", "regexp literal");
11066 if ((c = nextc(p)) == '=') {
11067 set_yylval_id('^');
11068 SET_LEX_STATE(EXPR_BEG);
11071 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11076 SET_LEX_STATE(EXPR_BEG);
11077 p->command_start = TRUE;
11081 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11085 if (IS_AFTER_OPERATOR()) {
11086 if ((c = nextc(p)) != '@') {
11089 SET_LEX_STATE(EXPR_ARG);
11092 SET_LEX_STATE(EXPR_BEG);
11100 else if (!space_seen) {
11101 /* foo( ... ) => method call, no ambiguity */
11103 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11106 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11107 rb_warning0("parentheses after method name is interpreted as "
11108 "an argument list, not a decomposed argument");
11110 p->lex.paren_nest++;
11113 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11117 p->lex.paren_nest++;
11118 if (IS_AFTER_OPERATOR()) {
11119 if ((c = nextc(p)) == ']') {
11120 p->lex.paren_nest--;
11121 SET_LEX_STATE(EXPR_ARG);
11122 if ((c = nextc(p)) == '=') {
11129 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11132 else if (IS_BEG()) {
11135 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11138 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11144 ++p->lex.brace_nest;
11145 if (lambda_beginning_p())
11147 else if (IS_lex_state(EXPR_LABELED))
11148 c = tLBRACE; /* hash */
11149 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11150 c = '{'; /* block (primary) */
11151 else if (IS_lex_state(EXPR_ENDARG))
11152 c = tLBRACE_ARG; /* block (expr) */
11154 c = tLBRACE; /* hash */
11155 if (c != tLBRACE) {
11156 p->command_start = TRUE;
11157 SET_LEX_STATE(EXPR_BEG);
11160 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11162 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11171 dispatch_scan_event(p, tSP);
11172 goto retry; /* skip \\n */
11174 if (c == ' ') return tSP;
11175 if (ISSPACE(c)) return c;
11180 return parse_percent(p, space_seen, last_state);
11183 return parse_gvar(p, last_state);
11186 return parse_atmark(p, last_state);
11189 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11190 p->ruby__end__seen = 1;
11194 dispatch_scan_event(p, k__END__);
11196 return END_OF_INPUT;
11202 if (!parser_is_identchar(p)) {
11203 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11212 return parse_ident(p, c, cmd_state);
11215static enum yytokentype
11216yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11218 enum yytokentype t;
11222 p->yylloc = yylloc;
11224 t = parser_yylex(p);
11226 if (has_delayed_token(p))
11227 dispatch_delayed_token(p, t);
11228 else if (t != END_OF_INPUT)
11229 dispatch_scan_event(p, t);
11234#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11237node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11239 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11241 rb_node_init(n, type);
11246nd_set_loc(NODE *nd, const YYLTYPE *loc)
11249 nd_set_line(nd, loc->beg_pos.lineno);
11254node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11256 NODE *n = node_new_internal(p, type, size, alignment);
11258 nd_set_loc(n, loc);
11259 nd_set_node_id(n, parser_get_node_id(p));
11263#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11265static rb_node_scope_t *
11266rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc)
11268 rb_ast_id_table_t *nd_tbl;
11269 nd_tbl = local_tbl(p);
11270 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11271 n->nd_tbl = nd_tbl;
11272 n->nd_body = nd_body;
11273 n->nd_parent = nd_parent;
11274 n->nd_args = nd_args;
11279static rb_node_scope_t *
11280rb_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)
11282 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11283 n->nd_tbl = nd_tbl;
11284 n->nd_body = nd_body;
11285 n->nd_parent = nd_parent;
11286 n->nd_args = nd_args;
11291static rb_node_defn_t *
11292rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11294 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11295 n->nd_mid = nd_mid;
11296 n->nd_defn = nd_defn;
11301static rb_node_defs_t *
11302rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11304 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11305 n->nd_recv = nd_recv;
11306 n->nd_mid = nd_mid;
11307 n->nd_defn = nd_defn;
11312static rb_node_block_t *
11313rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11315 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11316 n->nd_head = nd_head;
11317 n->nd_end = (NODE *)n;
11323static rb_node_for_t *
11324rb_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)
11326 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11327 n->nd_body = nd_body;
11328 n->nd_iter = nd_iter;
11329 n->for_keyword_loc = *for_keyword_loc;
11330 n->in_keyword_loc = *in_keyword_loc;
11331 n->do_keyword_loc = *do_keyword_loc;
11332 n->end_keyword_loc = *end_keyword_loc;
11337static rb_node_for_masgn_t *
11338rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11340 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11341 n->nd_var = nd_var;
11346static rb_node_retry_t *
11347rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11349 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11354static rb_node_begin_t *
11355rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11357 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11358 n->nd_body = nd_body;
11363static rb_node_rescue_t *
11364rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11366 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11367 n->nd_head = nd_head;
11368 n->nd_resq = nd_resq;
11369 n->nd_else = nd_else;
11374static rb_node_resbody_t *
11375rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11377 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11378 n->nd_args = nd_args;
11379 n->nd_exc_var = nd_exc_var;
11380 n->nd_body = nd_body;
11381 n->nd_next = nd_next;
11386static rb_node_ensure_t *
11387rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11389 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11390 n->nd_head = nd_head;
11391 n->nd_ensr = nd_ensr;
11396static rb_node_and_t *
11397rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11399 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11400 n->nd_1st = nd_1st;
11401 n->nd_2nd = nd_2nd;
11402 n->operator_loc = *operator_loc;
11407static rb_node_or_t *
11408rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11410 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11411 n->nd_1st = nd_1st;
11412 n->nd_2nd = nd_2nd;
11413 n->operator_loc = *operator_loc;
11418static rb_node_return_t *
11419rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11421 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11422 n->nd_stts = nd_stts;
11423 n->keyword_loc = *keyword_loc;
11427static rb_node_yield_t *
11428rb_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)
11430 if (nd_head) no_blockarg(p, nd_head);
11432 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11433 n->nd_head = nd_head;
11434 n->keyword_loc = *keyword_loc;
11435 n->lparen_loc = *lparen_loc;
11436 n->rparen_loc = *rparen_loc;
11441static rb_node_if_t *
11442rb_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)
11444 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11445 n->nd_cond = nd_cond;
11446 n->nd_body = nd_body;
11447 n->nd_else = nd_else;
11448 n->if_keyword_loc = *if_keyword_loc;
11449 n->then_keyword_loc = *then_keyword_loc;
11450 n->end_keyword_loc = *end_keyword_loc;
11455static rb_node_unless_t *
11456rb_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)
11458 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11459 n->nd_cond = nd_cond;
11460 n->nd_body = nd_body;
11461 n->nd_else = nd_else;
11462 n->keyword_loc = *keyword_loc;
11463 n->then_keyword_loc = *then_keyword_loc;
11464 n->end_keyword_loc = *end_keyword_loc;
11469static rb_node_class_t *
11470rb_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)
11472 /* Keep the order of node creation */
11473 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11474 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11475 RNODE_SCOPE(scope)->nd_parent = &n->node;
11476 n->nd_cpath = nd_cpath;
11477 n->nd_body = scope;
11478 n->nd_super = nd_super;
11479 n->class_keyword_loc = *class_keyword_loc;
11480 n->inheritance_operator_loc = *inheritance_operator_loc;
11481 n->end_keyword_loc = *end_keyword_loc;
11486static rb_node_sclass_t *
11487rb_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)
11489 /* Keep the order of node creation */
11490 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11491 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11492 RNODE_SCOPE(scope)->nd_parent = &n->node;
11493 n->nd_recv = nd_recv;
11494 n->nd_body = scope;
11495 n->class_keyword_loc = *class_keyword_loc;
11496 n->operator_loc = *operator_loc;
11497 n->end_keyword_loc = *end_keyword_loc;
11502static rb_node_module_t *
11503rb_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)
11505 /* Keep the order of node creation */
11506 NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc);
11507 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11508 RNODE_SCOPE(scope)->nd_parent = &n->node;
11509 n->nd_cpath = nd_cpath;
11510 n->nd_body = scope;
11511 n->module_keyword_loc = *module_keyword_loc;
11512 n->end_keyword_loc = *end_keyword_loc;
11517static rb_node_iter_t *
11518rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11520 /* Keep the order of node creation */
11521 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11522 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11523 RNODE_SCOPE(scope)->nd_parent = &n->node;
11524 n->nd_body = scope;
11530static rb_node_lambda_t *
11531rb_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)
11533 /* Keep the order of node creation */
11534 NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc);
11535 YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc);
11536 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc);
11537 RNODE_SCOPE(scope)->nd_parent = &n->node;
11538 n->nd_body = scope;
11539 n->operator_loc = *operator_loc;
11540 n->opening_loc = *opening_loc;
11541 n->closing_loc = *closing_loc;
11546static rb_node_case_t *
11547rb_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)
11549 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11550 n->nd_head = nd_head;
11551 n->nd_body = nd_body;
11552 n->case_keyword_loc = *case_keyword_loc;
11553 n->end_keyword_loc = *end_keyword_loc;
11558static rb_node_case2_t *
11559rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11561 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11563 n->nd_body = nd_body;
11564 n->case_keyword_loc = *case_keyword_loc;
11565 n->end_keyword_loc = *end_keyword_loc;
11570static rb_node_case3_t *
11571rb_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)
11573 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11574 n->nd_head = nd_head;
11575 n->nd_body = nd_body;
11576 n->case_keyword_loc = *case_keyword_loc;
11577 n->end_keyword_loc = *end_keyword_loc;
11582static rb_node_when_t *
11583rb_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)
11585 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11586 n->nd_head = nd_head;
11587 n->nd_body = nd_body;
11588 n->nd_next = nd_next;
11589 n->keyword_loc = *keyword_loc;
11590 n->then_keyword_loc = *then_keyword_loc;
11595static rb_node_in_t *
11596rb_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)
11598 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11599 n->nd_head = nd_head;
11600 n->nd_body = nd_body;
11601 n->nd_next = nd_next;
11602 n->in_keyword_loc = *in_keyword_loc;
11603 n->then_keyword_loc = *then_keyword_loc;
11604 n->operator_loc = *operator_loc;
11609static rb_node_while_t *
11610rb_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)
11612 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11613 n->nd_cond = nd_cond;
11614 n->nd_body = nd_body;
11615 n->nd_state = nd_state;
11616 n->keyword_loc = *keyword_loc;
11617 n->closing_loc = *closing_loc;
11622static rb_node_until_t *
11623rb_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)
11625 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11626 n->nd_cond = nd_cond;
11627 n->nd_body = nd_body;
11628 n->nd_state = nd_state;
11629 n->keyword_loc = *keyword_loc;
11630 n->closing_loc = *closing_loc;
11635static rb_node_colon2_t *
11636rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11638 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11639 n->nd_head = nd_head;
11640 n->nd_mid = nd_mid;
11641 n->delimiter_loc = *delimiter_loc;
11642 n->name_loc = *name_loc;
11647static rb_node_colon3_t *
11648rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
11650 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11651 n->nd_mid = nd_mid;
11652 n->delimiter_loc = *delimiter_loc;
11653 n->name_loc = *name_loc;
11658static rb_node_dot2_t *
11659rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11661 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11662 n->nd_beg = nd_beg;
11663 n->nd_end = nd_end;
11664 n->operator_loc = *operator_loc;
11669static rb_node_dot3_t *
11670rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11672 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11673 n->nd_beg = nd_beg;
11674 n->nd_end = nd_end;
11675 n->operator_loc = *operator_loc;
11680static rb_node_self_t *
11681rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11683 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11689static rb_node_nil_t *
11690rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11692 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11697static rb_node_true_t *
11698rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11700 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11705static rb_node_false_t *
11706rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11708 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11713static rb_node_super_t *
11714rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc,
11715 const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc)
11717 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11718 n->nd_args = nd_args;
11719 n->keyword_loc = *keyword_loc;
11720 n->lparen_loc = *lparen_loc;
11721 n->rparen_loc = *rparen_loc;
11726static rb_node_zsuper_t *
11727rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11729 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11734static rb_node_match2_t *
11735rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11737 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11738 n->nd_recv = nd_recv;
11739 n->nd_value = nd_value;
11745static rb_node_match3_t *
11746rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11748 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11749 n->nd_recv = nd_recv;
11750 n->nd_value = nd_value;
11755/* TODO: Use union for NODE_LIST2 */
11756static rb_node_list_t *
11757rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11759 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11760 n->nd_head = nd_head;
11767static rb_node_list_t *
11768rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11770 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11771 n->nd_head = nd_head;
11772 n->as.nd_alen = nd_alen;
11773 n->nd_next = nd_next;
11778static rb_node_zlist_t *
11779rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11781 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11786static rb_node_hash_t *
11787rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11789 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11790 n->nd_head = nd_head;
11796static rb_node_masgn_t *
11797rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11799 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11800 n->nd_head = nd_head;
11802 n->nd_args = nd_args;
11807static rb_node_gasgn_t *
11808rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11810 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11811 n->nd_vid = nd_vid;
11812 n->nd_value = nd_value;
11817static rb_node_lasgn_t *
11818rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11820 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11821 n->nd_vid = nd_vid;
11822 n->nd_value = nd_value;
11827static rb_node_dasgn_t *
11828rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11830 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11831 n->nd_vid = nd_vid;
11832 n->nd_value = nd_value;
11837static rb_node_iasgn_t *
11838rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11840 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11841 n->nd_vid = nd_vid;
11842 n->nd_value = nd_value;
11847static rb_node_cvasgn_t *
11848rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11850 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11851 n->nd_vid = nd_vid;
11852 n->nd_value = nd_value;
11857static rb_node_op_asgn1_t *
11858rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
11860 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11861 n->nd_recv = nd_recv;
11862 n->nd_mid = nd_mid;
11863 n->nd_index = index;
11864 n->nd_rvalue = rvalue;
11865 n->call_operator_loc = *call_operator_loc;
11866 n->opening_loc = *opening_loc;
11867 n->closing_loc = *closing_loc;
11868 n->binary_operator_loc = *binary_operator_loc;
11873static rb_node_op_asgn2_t *
11874rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
11876 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11877 n->nd_recv = nd_recv;
11878 n->nd_value = nd_value;
11879 n->nd_vid = nd_vid;
11880 n->nd_mid = nd_mid;
11881 n->nd_aid = nd_aid;
11882 n->call_operator_loc = *call_operator_loc;
11883 n->message_loc = *message_loc;
11884 n->binary_operator_loc = *binary_operator_loc;
11889static rb_node_op_asgn_or_t *
11890rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11892 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11893 n->nd_head = nd_head;
11894 n->nd_value = nd_value;
11899static rb_node_op_asgn_and_t *
11900rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11902 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11903 n->nd_head = nd_head;
11904 n->nd_value = nd_value;
11909static rb_node_gvar_t *
11910rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11912 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11913 n->nd_vid = nd_vid;
11918static rb_node_lvar_t *
11919rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11921 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11922 n->nd_vid = nd_vid;
11927static rb_node_dvar_t *
11928rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11930 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11931 n->nd_vid = nd_vid;
11936static rb_node_ivar_t *
11937rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11939 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11940 n->nd_vid = nd_vid;
11945static rb_node_const_t *
11946rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11948 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11949 n->nd_vid = nd_vid;
11954static rb_node_cvar_t *
11955rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11957 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11958 n->nd_vid = nd_vid;
11963static rb_node_nth_ref_t *
11964rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11966 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11967 n->nd_nth = nd_nth;
11972static rb_node_back_ref_t *
11973rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11975 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11976 n->nd_nth = nd_nth;
11981static rb_node_integer_t *
11982rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
11984 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
11992static rb_node_float_t *
11993rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
11995 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12002static rb_node_rational_t *
12003rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12005 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12009 n->seen_point = seen_point;
12014static rb_node_imaginary_t *
12015rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12017 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12021 n->seen_point = seen_point;
12022 n->type = numeric_type;
12027static rb_node_str_t *
12028rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12030 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12031 n->string = string;
12036/* TODO; Use union for NODE_DSTR2 */
12037static rb_node_dstr_t *
12038rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12040 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12041 n->string = string;
12042 n->as.nd_alen = nd_alen;
12043 n->nd_next = (rb_node_list_t *)nd_next;
12048static rb_node_dstr_t *
12049rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12051 return rb_node_dstr_new0(p, string, 1, 0, loc);
12054static rb_node_xstr_t *
12055rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12057 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12058 n->string = string;
12063static rb_node_dxstr_t *
12064rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12066 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12067 n->string = string;
12068 n->as.nd_alen = nd_alen;
12069 n->nd_next = (rb_node_list_t *)nd_next;
12074static rb_node_sym_t *
12075rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12077 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12078 n->string = rb_str_to_parser_string(p, str);
12083static rb_node_dsym_t *
12084rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12086 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12087 n->string = string;
12088 n->as.nd_alen = nd_alen;
12089 n->nd_next = (rb_node_list_t *)nd_next;
12094static rb_node_evstr_t *
12095rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12097 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12098 n->nd_body = nd_body;
12099 n->opening_loc = *opening_loc;
12100 n->closing_loc = *closing_loc;
12105static rb_node_regx_t *
12106rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12108 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12109 n->string = string;
12110 n->options = options & RE_OPTION_MASK;
12111 n->opening_loc = *opening_loc;
12112 n->content_loc = *content_loc;
12113 n->closing_loc = *closing_loc;
12118static rb_node_call_t *
12119rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12121 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12122 n->nd_recv = nd_recv;
12123 n->nd_mid = nd_mid;
12124 n->nd_args = nd_args;
12129static rb_node_opcall_t *
12130rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12132 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12133 n->nd_recv = nd_recv;
12134 n->nd_mid = nd_mid;
12135 n->nd_args = nd_args;
12140static rb_node_fcall_t *
12141rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12143 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12144 n->nd_mid = nd_mid;
12145 n->nd_args = nd_args;
12150static rb_node_qcall_t *
12151rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12153 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12154 n->nd_recv = nd_recv;
12155 n->nd_mid = nd_mid;
12156 n->nd_args = nd_args;
12161static rb_node_vcall_t *
12162rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12164 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12165 n->nd_mid = nd_mid;
12170static rb_node_once_t *
12171rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12173 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12174 n->nd_body = nd_body;
12179static rb_node_args_t *
12180rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12182 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12183 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12188static rb_node_args_aux_t *
12189rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12191 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12192 n->nd_pid = nd_pid;
12193 n->nd_plen = nd_plen;
12199static rb_node_opt_arg_t *
12200rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12202 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12203 n->nd_body = nd_body;
12209static rb_node_kw_arg_t *
12210rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12212 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12213 n->nd_body = nd_body;
12219static rb_node_postarg_t *
12220rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12222 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12223 n->nd_1st = nd_1st;
12224 n->nd_2nd = nd_2nd;
12229static rb_node_argscat_t *
12230rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12232 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12233 n->nd_head = nd_head;
12234 n->nd_body = nd_body;
12239static rb_node_argspush_t *
12240rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12242 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12243 n->nd_head = nd_head;
12244 n->nd_body = nd_body;
12249static rb_node_splat_t *
12250rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12252 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12253 n->nd_head = nd_head;
12254 n->operator_loc = *operator_loc;
12259static rb_node_block_pass_t *
12260rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12262 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12265 n->nd_body = nd_body;
12266 n->operator_loc = *operator_loc;
12271static rb_node_alias_t *
12272rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12274 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12275 n->nd_1st = nd_1st;
12276 n->nd_2nd = nd_2nd;
12277 n->keyword_loc = *keyword_loc;
12282static rb_node_valias_t *
12283rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12285 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12286 n->nd_alias = nd_alias;
12287 n->nd_orig = nd_orig;
12288 n->keyword_loc = *keyword_loc;
12293static rb_node_undef_t *
12294rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12296 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12297 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12298 n->keyword_loc = NULL_LOC;
12299 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12304static rb_node_errinfo_t *
12305rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12307 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12312static rb_node_defined_t *
12313rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12315 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12316 n->nd_head = nd_head;
12317 n->keyword_loc = *keyword_loc;
12322static rb_node_postexe_t *
12323rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12325 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12326 n->nd_body = nd_body;
12327 n->keyword_loc = *keyword_loc;
12328 n->opening_loc = *opening_loc;
12329 n->closing_loc = *closing_loc;
12334static rb_node_attrasgn_t *
12335rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12337 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12338 n->nd_recv = nd_recv;
12339 n->nd_mid = nd_mid;
12340 n->nd_args = nd_args;
12345static rb_node_aryptn_t *
12346rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12348 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12350 n->pre_args = pre_args;
12351 n->rest_arg = rest_arg;
12352 n->post_args = post_args;
12357static rb_node_hshptn_t *
12358rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12360 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12361 n->nd_pconst = nd_pconst;
12362 n->nd_pkwargs = nd_pkwargs;
12363 n->nd_pkwrestarg = nd_pkwrestarg;
12368static rb_node_fndptn_t *
12369rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12371 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12373 n->pre_rest_arg = pre_rest_arg;
12375 n->post_rest_arg = post_rest_arg;
12380static rb_node_line_t *
12381rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12383 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12388static rb_node_file_t *
12389rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12391 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12392 n->path = rb_str_to_parser_string(p, str);
12397static rb_node_encoding_t *
12398rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12400 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12406static rb_node_cdecl_t *
12407rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12409 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12410 n->nd_vid = nd_vid;
12411 n->nd_value = nd_value;
12412 n->nd_else = nd_else;
12413 n->shareability = shareability;
12418static rb_node_op_cdecl_t *
12419rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12421 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12422 n->nd_head = nd_head;
12423 n->nd_value = nd_value;
12424 n->nd_aid = nd_aid;
12425 n->shareability = shareability;
12430static rb_node_error_t *
12431rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12433 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12438static rb_node_break_t *
12439rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12441 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12442 n->nd_stts = nd_stts;
12444 n->keyword_loc = *keyword_loc;
12449static rb_node_next_t *
12450rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12452 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12453 n->nd_stts = nd_stts;
12455 n->keyword_loc = *keyword_loc;
12460static rb_node_redo_t *
12461rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12463 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12465 n->keyword_loc = *keyword_loc;
12470static rb_node_def_temp_t *
12471rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12473 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12474 n->save.numparam_save = 0;
12475 n->save.max_numparam = 0;
12476 n->save.ctxt = p->ctxt;
12483static rb_node_def_temp_t *
12484def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12486 n->save.numparam_save = numparam_push(p);
12487 n->save.max_numparam = p->max_numparam;
12492static enum node_type
12493nodetype(NODE *node) /* for debug */
12495 return (enum node_type)nd_type(node);
12499nodeline(NODE *node)
12501 return nd_line(node);
12506newline_node(NODE *node)
12509 node = remove_begin(node);
12510 nd_set_fl_newline(node);
12516fixpos(NODE *node, NODE *orig)
12520 nd_set_line(node, nd_line(orig));
12524block_append(struct parser_params *p, NODE *head, NODE *tail)
12526 NODE *end, *h = head, *nd;
12528 if (tail == 0) return head;
12530 if (h == 0) return tail;
12531 switch (nd_type(h)) {
12533 h = end = NEW_BLOCK(head, &head->nd_loc);
12537 end = RNODE_BLOCK(h)->nd_end;
12541 nd = RNODE_BLOCK(end)->nd_head;
12542 switch (nd_type(nd)) {
12548 rb_warning0L(nd_line(tail), "statement not reached");
12555 if (!nd_type_p(tail, NODE_BLOCK)) {
12556 tail = NEW_BLOCK(tail, &tail->nd_loc);
12558 RNODE_BLOCK(end)->nd_next = tail;
12559 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12560 nd_set_last_loc(head, nd_last_loc(tail));
12564/* append item to the list */
12566list_append(struct parser_params *p, NODE *list, NODE *item)
12570 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12571 if (RNODE_LIST(list)->nd_next) {
12572 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12578 RNODE_LIST(list)->as.nd_alen += 1;
12579 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12580 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12582 nd_set_last_loc(list, nd_last_loc(item));
12587/* concat two lists */
12589list_concat(NODE *head, NODE *tail)
12593 if (RNODE_LIST(head)->nd_next) {
12594 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12600 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12601 RNODE_LIST(last)->nd_next = tail;
12602 if (RNODE_LIST(tail)->nd_next) {
12603 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12606 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12609 nd_set_last_loc(head, nd_last_loc(tail));
12615literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12617 if (!tail) return 1;
12618 if (!rb_parser_enc_compatible(p, head, tail)) {
12619 compile_error(p, "string literal encodings differ (%s / %s)",
12620 rb_enc_name(rb_parser_str_get_encoding(head)),
12621 rb_enc_name(rb_parser_str_get_encoding(tail)));
12622 rb_parser_str_resize(p, head, 0);
12623 rb_parser_str_resize(p, tail, 0);
12626 rb_parser_str_buf_append(p, head, tail);
12630static rb_parser_string_t *
12631string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12633 if (htype != NODE_DSTR) return NULL;
12634 if (RNODE_DSTR(head)->nd_next) {
12635 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12636 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12638 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12644static rb_parser_string_t *
12645rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12647 rb_parser_string_t *copy;
12648 if (!orig) return NULL;
12649 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12650 copy->coderange = orig->coderange;
12651 copy->enc = orig->enc;
12656/* concat two string literals */
12658literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12660 enum node_type htype;
12661 rb_parser_string_t *lit;
12663 if (!head) return tail;
12664 if (!tail) return head;
12666 htype = nd_type(head);
12667 if (htype == NODE_EVSTR) {
12668 head = new_dstr(p, head, loc);
12671 if (p->heredoc_indent > 0) {
12674 head = str2dstr(p, head);
12676 return list_append(p, head, tail);
12681 switch (nd_type(tail)) {
12683 if ((lit = string_literal_head(p, htype, head)) != false) {
12687 lit = RNODE_DSTR(head)->string;
12689 if (htype == NODE_STR) {
12690 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12692 rb_discard_node(p, head);
12693 rb_discard_node(p, tail);
12696 rb_discard_node(p, tail);
12699 list_append(p, head, tail);
12704 if (htype == NODE_STR) {
12705 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12707 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12708 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12709 RNODE_STR(head)->string = NULL;
12710 rb_discard_node(p, head);
12713 else if (!RNODE_DSTR(tail)->string) {
12715 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12716 if (!RNODE_DSTR(head)->nd_next) {
12717 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12719 else if (RNODE_DSTR(tail)->nd_next) {
12720 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12721 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12723 rb_discard_node(p, tail);
12725 else if ((lit = string_literal_head(p, htype, head)) != false) {
12726 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12728 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12729 RNODE_DSTR(tail)->string = 0;
12733 list_concat(head, NEW_LIST2(NEW_STR(RNODE_DSTR(tail)->string, loc), RNODE_DSTR(tail)->as.nd_alen, (NODE *)RNODE_DSTR(tail)->nd_next, loc));
12734 RNODE_DSTR(tail)->string = 0;
12739 if (htype == NODE_STR) {
12740 head = str2dstr(p, head);
12741 RNODE_DSTR(head)->as.nd_alen = 1;
12743 list_append(p, head, tail);
12750nd_copy_flag(NODE *new_node, NODE *old_node)
12752 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12753 nd_set_line(new_node, nd_line(old_node));
12754 new_node->nd_loc = old_node->nd_loc;
12755 new_node->node_id = old_node->node_id;
12759str2dstr(struct parser_params *p, NODE *node)
12761 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12762 nd_copy_flag(new_node, node);
12763 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12764 RNODE_DSTR(new_node)->as.nd_alen = 0;
12765 RNODE_DSTR(new_node)->nd_next = 0;
12766 RNODE_STR(node)->string = 0;
12772str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
12774 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12775 nd_copy_flag(new_node, node);
12776 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12777 RNODE_REGX(new_node)->options = options;
12778 nd_set_loc(new_node, loc);
12779 RNODE_REGX(new_node)->opening_loc = *opening_loc;
12780 RNODE_REGX(new_node)->content_loc = *content_loc;
12781 RNODE_REGX(new_node)->closing_loc = *closing_loc;
12782 RNODE_STR(node)->string = 0;
12788evstr2dstr(struct parser_params *p, NODE *node)
12790 if (nd_type_p(node, NODE_EVSTR)) {
12791 node = new_dstr(p, node, &node->nd_loc);
12797new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12802 switch (nd_type(node)) {
12804 return str2dstr(p, node);
12811 return NEW_EVSTR(head, loc, opening_loc, closing_loc);
12815new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12817 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12818 return list_append(p, dstr, node);
12822call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12823 const YYLTYPE *op_loc, const YYLTYPE *loc)
12826 value_expr(p, recv);
12827 value_expr(p, arg1);
12828 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12829 nd_set_line(expr, op_loc->beg_pos.lineno);
12834call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12837 value_expr(p, recv);
12838 opcall = NEW_OPCALL(recv, id, 0, loc);
12839 nd_set_line(opcall, op_loc->beg_pos.lineno);
12844new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12846 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12847 nd_set_line(qcall, op_loc->beg_pos.lineno);
12852new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12855 if (block) block_dup_check(p, args, block);
12856 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12857 if (block) ret = method_add_block(p, ret, block, loc);
12862static rb_locations_lambda_body_t*
12863new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
12865 rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t));
12867 body->opening_loc = *opening_loc;
12868 body->closing_loc = *closing_loc;
12872#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12875last_expr_once_body(NODE *node)
12877 if (!node) return 0;
12878 return nd_once_body(node);
12882match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12885 int line = op_loc->beg_pos.lineno;
12887 value_expr(p, node1);
12888 value_expr(p, node2);
12890 if ((n = last_expr_once_body(node1)) != 0) {
12891 switch (nd_type(n)) {
12894 NODE *match = NEW_MATCH2(node1, node2, loc);
12895 nd_set_line(match, line);
12901 const VALUE lit = rb_node_regx_string_val(n);
12903 NODE *match = NEW_MATCH2(node1, node2, loc);
12904 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12905 nd_set_line(match, line);
12912 if ((n = last_expr_once_body(node2)) != 0) {
12915 switch (nd_type(n)) {
12917 match3 = NEW_MATCH3(node2, node1, loc);
12922 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12923 nd_set_line(n, line);
12927# if WARN_PAST_SCOPE
12929past_dvar_p(struct parser_params *p, ID id)
12931 struct vtable *past = p->lvtbl->past;
12933 if (vtable_included(past, id)) return 1;
12941numparam_nested_p(struct parser_params *p)
12943 struct local_vars *local = p->lvtbl;
12944 NODE *outer = local->numparam.outer;
12945 NODE *inner = local->numparam.inner;
12946 if (outer || inner) {
12947 NODE *used = outer ? outer : inner;
12948 compile_error(p, "numbered parameter is already used in %s block\n"
12949 "%s:%d: numbered parameter is already used here",
12950 outer ? "outer" : "inner",
12951 p->ruby_sourcefile, nd_line(used));
12952 parser_show_error_line(p, &used->nd_loc);
12959numparam_used_p(struct parser_params *p)
12961 NODE *numparam = p->lvtbl->numparam.current;
12963 compile_error(p, "'it' is not allowed when a numbered parameter is already used\n"
12964 "%s:%d: numbered parameter is already used here",
12965 p->ruby_sourcefile, nd_line(numparam));
12966 parser_show_error_line(p, &numparam->nd_loc);
12973it_used_p(struct parser_params *p)
12975 NODE *it = p->lvtbl->it;
12977 compile_error(p, "numbered parameters are not allowed when 'it' is already used\n"
12978 "%s:%d: 'it' is already used here",
12979 p->ruby_sourcefile, nd_line(it));
12980 parser_show_error_line(p, &it->nd_loc);
12987gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12993 return NEW_SELF(loc);
12995 return NEW_NIL(loc);
12997 return NEW_TRUE(loc);
12998 case keyword_false:
12999 return NEW_FALSE(loc);
13000 case keyword__FILE__:
13002 VALUE file = p->ruby_sourcefile_string;
13004 file = rb_str_new(0, 0);
13005 node = NEW_FILE(file, loc);
13008 case keyword__LINE__:
13009 return NEW_LINE(loc);
13010 case keyword__ENCODING__:
13011 return NEW_ENCODING(loc);
13014 switch (id_type(id)) {
13016 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13017 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13018 if (vidp) *vidp |= LVAR_USED;
13019 node = NEW_DVAR(id, loc);
13022 if (local_id_ref(p, id, &vidp)) {
13023 if (vidp) *vidp |= LVAR_USED;
13024 node = NEW_LVAR(id, loc);
13027 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13028 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13029 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13030 node = NEW_DVAR(id, loc);
13031 struct local_vars *local = p->lvtbl;
13032 if (!local->numparam.current) local->numparam.current = node;
13035# if WARN_PAST_SCOPE
13036 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13037 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13040 /* method call without arguments */
13041 if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13042 if (numparam_used_p(p)) return 0;
13043 if (p->max_numparam == ORDINAL_PARAM) {
13044 compile_error(p, "ordinary parameter is defined");
13048 p->it_id = idItImplicit;
13049 vtable_add(p->lvtbl->args, p->it_id);
13051 NODE *node = NEW_DVAR(p->it_id, loc);
13052 if (!p->lvtbl->it) p->lvtbl->it = node;
13055 return NEW_VCALL(id, loc);
13057 return NEW_GVAR(id, loc);
13059 return NEW_IVAR(id, loc);
13061 return NEW_CONST(id, loc);
13063 return NEW_CVAR(id, loc);
13065 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13069static rb_node_opt_arg_t *
13070opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13072 rb_node_opt_arg_t *opts = opt_list;
13073 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13075 while (opts->nd_next) {
13076 opts = opts->nd_next;
13077 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13079 opts->nd_next = opt;
13084static rb_node_kw_arg_t *
13085kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13088 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13089 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13095new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
13097 int had_trailing_semicolon = p->ctxt.has_trailing_semicolon;
13098 p->ctxt.has_trailing_semicolon = 0;
13102 if (nd_type_p(n, NODE_BEGIN)) {
13103 n = RNODE_BEGIN(n)->nd_body;
13105 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13106 n = RNODE_BLOCK(n)->nd_head;
13113 if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) {
13114 NODE *block = NEW_BLOCK(expr, loc);
13115 return NEW_DEFINED(block, loc, keyword_loc);
13118 return NEW_DEFINED(n, loc, keyword_loc);
13122str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13125 rb_parser_string_t *str = RNODE_STR(node)->string;
13126 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13127 yyerror1(loc, "invalid symbol");
13131 lit = rb_str_new_parser_string(str);
13133 return NEW_SYM(lit, loc);
13137symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13139 enum node_type type = nd_type(symbol);
13142 nd_set_type(symbol, NODE_DSYM);
13145 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13148 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13150 return list_append(p, symbols, symbol);
13154dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options)
13156 if (dreg->string) {
13157 reg_fragment_setenc(p, dreg->string, options);
13159 for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13160 NODE *frag = list->nd_head;
13161 if (nd_type_p(frag, NODE_STR)) {
13162 reg_fragment_setenc(p, RNODE_STR(frag)->string, options);
13164 else if (nd_type_p(frag, NODE_DSTR)) {
13165 dregex_fragment_setenc(p, RNODE_DSTR(frag), options);
13171new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc)
13174 /* Check string is valid regex */
13175 rb_parser_string_t *str = STRING_NEW0();
13176 reg_compile(p, str, options);
13177 node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc);
13180 switch (nd_type(node)) {
13183 /* Check string is valid regex */
13184 reg_compile(p, RNODE_STR(node)->string, options);
13185 node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc);
13189 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13192 nd_set_type(node, NODE_DREGX);
13193 nd_set_loc(node, loc);
13194 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13195 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13196 if (dreg->nd_next) {
13197 dregex_fragment_setenc(p, dreg, options);
13199 if (options & RE_OPTION_ONCE) {
13200 node = NEW_ONCE(node, loc);
13207static rb_node_kw_arg_t *
13208new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13211 return NEW_KW_ARG((k), loc);
13215new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13218 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13221 switch (nd_type(node)) {
13223 nd_set_type(node, NODE_XSTR);
13224 nd_set_loc(node, loc);
13227 nd_set_type(node, NODE_DXSTR);
13228 nd_set_loc(node, loc);
13231 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13238struct st_hash_type literal_type = {
13243static int nd_type_st_key_enable_p(NODE *node);
13246check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13248 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13249 if (!arg || !p->case_labels) return;
13250 if (!nd_type_st_key_enable_p(arg)) return;
13252 if (p->case_labels == CHECK_LITERAL_WHEN) {
13253 p->case_labels = st_init_table(&literal_type);
13257 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13258 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13259 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13263 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13268id_is_var(struct parser_params *p, ID id)
13270 if (is_notop_id(id)) {
13271 switch (id & ID_SCOPE_MASK) {
13272 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13275 if (dyna_in_block(p)) {
13276 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13278 if (local_id(p, id)) return 1;
13279 /* method call without arguments */
13283 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13288static inline enum lex_state_e
13289parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13292 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13294 return p->lex.state = ls;
13299flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13301 VALUE mesg = p->debug_buffer;
13303 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13304 p->debug_buffer = Qnil;
13305 rb_io_puts(1, &mesg, out);
13307 if (!NIL_P(str) && RSTRING_LEN(str)) {
13308 rb_io_write(p->debug_output, str);
13312static const char rb_parser_lex_state_names[][8] = {
13313 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13314 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13315 "LABEL", "LABELED","FITEM",
13319append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13322 unsigned int mask = 1;
13323 static const char none[] = "NONE";
13325 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13326 if ((unsigned)state & mask) {
13328 rb_str_cat(buf, "|", 1);
13331 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13335 rb_str_cat(buf, none, sizeof(none)-1);
13341rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13342 enum lex_state_e to, int line)
13345 mesg = rb_str_new_cstr("lex_state: ");
13346 append_lex_state_name(p, from, mesg);
13347 rb_str_cat_cstr(mesg, " -> ");
13348 append_lex_state_name(p, to, mesg);
13349 rb_str_catf(mesg, " at line %d\n", line);
13350 flush_debug_buffer(p, p->debug_output, mesg);
13355rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13357 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13361append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13364 rb_str_cat_cstr(mesg, "0");
13367 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13368 for (; mask && !(stack & mask); mask >>= 1) continue;
13369 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13374rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13375 const char *name, int line)
13377 VALUE mesg = rb_sprintf("%s: ", name);
13378 append_bitstack_value(p, stack, mesg);
13379 rb_str_catf(mesg, " at line %d\n", line);
13380 flush_debug_buffer(p, p->debug_output, mesg);
13384rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13387 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13390 rb_str_vcatf(mesg, fmt, ap);
13392 yyerror0(RSTRING_PTR(mesg));
13395 mesg = rb_str_new(0, 0);
13396 append_lex_state_name(p, p->lex.state, mesg);
13397 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13398 rb_str_resize(mesg, 0);
13399 append_bitstack_value(p, p->cond_stack, mesg);
13400 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13401 rb_str_resize(mesg, 0);
13402 append_bitstack_value(p, p->cmdarg_stack, mesg);
13403 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13404 if (p->debug_output == rb_ractor_stdout())
13405 p->debug_output = rb_ractor_stderr();
13410rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13412 yylloc->beg_pos.lineno = sourceline;
13413 yylloc->beg_pos.column = beg_pos;
13414 yylloc->end_pos.lineno = sourceline;
13415 yylloc->end_pos.column = end_pos;
13420rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13422 int sourceline = here->sourceline;
13423 int beg_pos = (int)here->offset - here->quote
13424 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13425 int end_pos = (int)here->offset + here->length + here->quote;
13427 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13431rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13433 yylloc->beg_pos.lineno = p->delayed.beg_line;
13434 yylloc->beg_pos.column = p->delayed.beg_col;
13435 yylloc->end_pos.lineno = p->delayed.end_line;
13436 yylloc->end_pos.column = p->delayed.end_col;
13442rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13444 int sourceline = p->ruby_sourceline;
13445 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13446 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13447 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13451rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13453 yylloc->end_pos = yylloc->beg_pos;
13459rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13461 int sourceline = p->ruby_sourceline;
13462 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13463 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13464 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13468rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13470 int sourceline = p->ruby_sourceline;
13471 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13472 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13473 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13475#endif /* !RIPPER */
13478assignable0(struct parser_params *p, ID id, const char **err)
13480 if (!id) return -1;
13483 *err = "Can't change the value of self";
13486 *err = "Can't assign to nil";
13489 *err = "Can't assign to true";
13491 case keyword_false:
13492 *err = "Can't assign to false";
13494 case keyword__FILE__:
13495 *err = "Can't assign to __FILE__";
13497 case keyword__LINE__:
13498 *err = "Can't assign to __LINE__";
13500 case keyword__ENCODING__:
13501 *err = "Can't assign to __ENCODING__";
13504 switch (id_type(id)) {
13506 if (dyna_in_block(p)) {
13507 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13508 compile_error(p, "Can't assign to numbered parameter _%d",
13509 NUMPARAM_ID_TO_IDX(id));
13512 if (dvar_curr(p, id)) return NODE_DASGN;
13513 if (dvar_defined(p, id)) return NODE_DASGN;
13514 if (local_id(p, id)) return NODE_LASGN;
13519 if (!local_id(p, id)) local_var(p, id);
13523 case ID_GLOBAL: return NODE_GASGN;
13524 case ID_INSTANCE: return NODE_IASGN;
13526 if (!p->ctxt.in_def) return NODE_CDECL;
13527 *err = "dynamic constant assignment";
13529 case ID_CLASS: return NODE_CVASGN;
13531 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13537assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13539 const char *err = 0;
13540 int node_type = assignable0(p, id, &err);
13541 switch (node_type) {
13542 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13543 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13544 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13545 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13546 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13547 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13551 if (err) yyerror1(loc, err);
13553 if (err) set_value(assign_error(p, err, p->s_lvalue));
13555 return NEW_ERROR(loc);
13559is_private_local_id(struct parser_params *p, ID name)
13562 if (name == idUScore) return 1;
13563 if (!is_local_id(name)) return 0;
13564 s = rb_id2str(name);
13566 return RSTRING_PTR(s)[0] == '_';
13570shadowing_lvar_0(struct parser_params *p, ID name)
13572 if (dyna_in_block(p)) {
13573 if (dvar_curr(p, name)) {
13574 if (is_private_local_id(p, name)) return 1;
13575 yyerror0("duplicated argument name");
13577 else if (dvar_defined(p, name) || local_id(p, name)) {
13578 vtable_add(p->lvtbl->vars, name);
13579 if (p->lvtbl->used) {
13580 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13586 if (local_id(p, name)) {
13587 if (is_private_local_id(p, name)) return 1;
13588 yyerror0("duplicated argument name");
13595shadowing_lvar(struct parser_params *p, ID name)
13597 shadowing_lvar_0(p, name);
13602new_bv(struct parser_params *p, ID name)
13605 if (!is_local_id(name)) {
13606 compile_error(p, "invalid local variable - %"PRIsVALUE,
13610 if (!shadowing_lvar_0(p, name)) return;
13613 if (dvar_defined_ref(p, name, &vidp)) {
13614 if (vidp) *vidp |= LVAR_USED;
13619aryset_check(struct parser_params *p, NODE *args)
13621 NODE *block = 0, *kwds = 0;
13622 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13623 block = RNODE_BLOCK_PASS(args)->nd_body;
13624 args = RNODE_BLOCK_PASS(args)->nd_head;
13626 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13627 args = RNODE_ARGSCAT(args)->nd_body;
13629 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13630 kwds = RNODE_ARGSPUSH(args)->nd_body;
13633 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13634 next = RNODE_LIST(next)->nd_next) {
13635 kwds = RNODE_LIST(next)->nd_head;
13638 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13639 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13642 yyerror1(&block->nd_loc, "block arg given in index assignment");
13647aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13649 aryset_check(p, idx);
13650 return NEW_ATTRASGN(recv, tASET, idx, loc);
13654block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13656 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13657 compile_error(p, "both block arg and actual block given");
13662attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13664 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13665 return NEW_ATTRASGN(recv, id, 0, loc);
13669rb_backref_error(struct parser_params *p, NODE *node)
13672# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13674# define ERR(...) rb_sprintf(__VA_ARGS__)
13676 switch (nd_type(node)) {
13678 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13679 case NODE_BACK_REF:
13680 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13683 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13687arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13689 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13690 switch (nd_type(node1)) {
13692 return list_append(p, node1, node2);
13693 case NODE_BLOCK_PASS:
13694 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13695 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13697 case NODE_ARGSPUSH:
13698 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13699 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13700 nd_set_type(node1, NODE_ARGSCAT);
13703 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13704 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13705 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13708 return NEW_ARGSPUSH(node1, node2, loc);
13712arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13714 if (!node2) return node1;
13715 switch (nd_type(node1)) {
13716 case NODE_BLOCK_PASS:
13717 if (RNODE_BLOCK_PASS(node1)->nd_head)
13718 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13720 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13722 case NODE_ARGSPUSH:
13723 if (!nd_type_p(node2, NODE_LIST)) break;
13724 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13725 nd_set_type(node1, NODE_ARGSCAT);
13728 if (!nd_type_p(node2, NODE_LIST) ||
13729 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13730 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13733 return NEW_ARGSCAT(node1, node2, loc);
13737last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13740 if ((n1 = splat_array(args)) != 0) {
13741 return list_append(p, n1, last_arg);
13743 return arg_append(p, args, last_arg, loc);
13747rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13750 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13751 return list_concat(n1, rest_arg);
13753 return arg_concat(p, args, rest_arg, loc);
13757splat_array(NODE* node)
13759 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13760 if (nd_type_p(node, NODE_LIST)) return node;
13765mark_lvar_used(struct parser_params *p, NODE *rhs)
13769 switch (nd_type(rhs)) {
13771 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13772 if (vidp) *vidp |= LVAR_USED;
13776 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13777 if (vidp) *vidp |= LVAR_USED;
13782 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13783 mark_lvar_used(p, rhs->nd_head);
13790static int is_static_content(NODE *node);
13793node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13795 if (!lhs) return 0;
13797 switch (nd_type(lhs)) {
13805 set_nd_value(p, lhs, rhs);
13806 nd_set_loc(lhs, loc);
13809 case NODE_ATTRASGN:
13810 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13811 nd_set_loc(lhs, loc);
13815 /* should not happen */
13823value_expr_check(struct parser_params *p, NODE *node)
13825 NODE *void_node = 0, *vn;
13828 rb_warning0("empty expression");
13831 switch (nd_type(node)) {
13833 vn = RNODE_ENSURE(node)->nd_head;
13834 node = RNODE_ENSURE(node)->nd_ensr;
13835 /* nd_ensr should not be NULL, check it out next */
13836 if (vn && (vn = value_expr_check(p, vn))) {
13842 /* void only if all children are void */
13843 vn = RNODE_RESCUE(node)->nd_head;
13844 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13845 if (!void_node) void_node = vn;
13846 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13847 if (!nd_type_p(r, NODE_RESBODY)) {
13848 compile_error(p, "unexpected node");
13851 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13855 if (!void_node) void_node = vn;
13857 node = RNODE_RESCUE(node)->nd_else;
13858 if (!node) return void_node;
13869 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13870 compile_error(p, "unexpected node");
13873 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13876 /* single line pattern matching with "=>" operator */
13880 while (RNODE_BLOCK(node)->nd_next) {
13881 node = RNODE_BLOCK(node)->nd_next;
13883 node = RNODE_BLOCK(node)->nd_head;
13887 node = RNODE_BEGIN(node)->nd_body;
13892 if (!RNODE_IF(node)->nd_body) {
13895 else if (!RNODE_IF(node)->nd_else) {
13898 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13899 if (!vn) return NULL;
13900 if (!void_node) void_node = vn;
13901 node = RNODE_IF(node)->nd_else;
13906 node = RNODE_AND(node)->nd_1st;
13912 mark_lvar_used(p, node);
13923 /* return the first found node */
13924 return void_node ? void_node : node;
13928value_expr(struct parser_params *p, NODE *node)
13930 NODE *void_node = value_expr_check(p, node);
13932 yyerror1(&void_node->nd_loc, "void value expression");
13933 /* or "control never reach"? */
13940void_expr(struct parser_params *p, NODE *node)
13942 const char *useless = 0;
13944 if (!RTEST(ruby_verbose)) return;
13946 if (!node || !(node = nd_once_body(node))) return;
13947 switch (nd_type(node)) {
13949 switch (RNODE_OPCALL(node)->nd_mid) {
13968 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13979 case NODE_BACK_REF:
13980 useless = "a variable";
13983 useless = "a constant";
13988 case NODE_ENCODING:
13991 case NODE_RATIONAL:
13992 case NODE_IMAGINARY:
13997 useless = "a literal";
14022 useless = "defined?";
14027 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14031/* warns useless use of block and returns the last statement node */
14033void_stmts(struct parser_params *p, NODE *node)
14035 NODE *const n = node;
14036 if (!RTEST(ruby_verbose)) return n;
14037 if (!node) return n;
14038 if (!nd_type_p(node, NODE_BLOCK)) return n;
14040 while (RNODE_BLOCK(node)->nd_next) {
14041 void_expr(p, RNODE_BLOCK(node)->nd_head);
14042 node = RNODE_BLOCK(node)->nd_next;
14044 return RNODE_BLOCK(node)->nd_head;
14048remove_begin(NODE *node)
14050 NODE **n = &node, *n1 = node;
14051 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14052 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14058reduce_nodes(struct parser_params *p, NODE **body)
14060 NODE *node = *body;
14063 *body = NEW_NIL(&NULL_LOC);
14066#define subnodes(type, n1, n2) \
14067 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14068 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14069 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14072 int newline = (int)nd_fl_newline(node);
14073 switch (nd_type(node)) {
14079 *body = node = RNODE_BEGIN(node)->nd_body;
14080 if (newline && node) nd_set_fl_newline(node);
14083 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14087 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14090 body = &RNODE_CASE(node)->nd_body;
14093 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14096 body = &RNODE_ENSURE(node)->nd_head;
14099 newline = 0; // RESBODY should not be a NEWLINE
14100 if (RNODE_RESCUE(node)->nd_else) {
14101 body = &RNODE_RESCUE(node)->nd_resq;
14104 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14110 if (newline && node) nd_set_fl_newline(node);
14117is_static_content(NODE *node)
14119 if (!node) return 1;
14120 switch (nd_type(node)) {
14122 if (!(node = RNODE_HASH(node)->nd_head)) break;
14125 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14126 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14131 case NODE_ENCODING:
14134 case NODE_RATIONAL:
14135 case NODE_IMAGINARY:
14149assign_in_cond(struct parser_params *p, NODE *node)
14151 switch (nd_type(node)) {
14165 if (!get_nd_value(p, node)) return 1;
14166 if (is_static_content(get_nd_value(p, node))) {
14167 /* reports always */
14168 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14179#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14181 case COND_IN_OP: break; \
14182 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14183 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14187static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14190range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14192 enum node_type type;
14194 if (node == 0) return 0;
14196 type = nd_type(node);
14197 value_expr(p, node);
14198 if (type == NODE_INTEGER) {
14199 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14200 ID lineno = rb_intern("$.");
14201 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14203 return cond0(p, node, COND_IN_FF, loc, true);
14207cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14209 if (node == 0) return 0;
14210 if (!(node = nd_once_body(node))) return 0;
14211 assign_in_cond(p, node);
14213 switch (nd_type(node)) {
14215 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14222 SWITCH_BY_COND_TYPE(type, warn, "string ");
14226 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14227 nd_set_type(node, NODE_MATCH);
14231 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14233 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14237 NODE *end = RNODE_BLOCK(node)->nd_end;
14238 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14239 if (top) top = node == end;
14240 *expr = cond0(p, *expr, type, loc, top);
14246 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14247 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14253 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14254 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14255 switch (nd_type(node)) {
14257 nd_set_type(node,NODE_FLIP2);
14258 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14262 nd_set_type(node, NODE_FLIP3);
14263 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14271 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14275 case NODE_ENCODING:
14278 case NODE_RATIONAL:
14279 case NODE_IMAGINARY:
14280 SWITCH_BY_COND_TYPE(type, warning, "");
14290cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14292 if (node == 0) return 0;
14293 return cond0(p, node, COND_IN_COND, loc, true);
14297method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14299 if (node == 0) return 0;
14300 return cond0(p, node, COND_IN_OP, loc, true);
14304new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14306 YYLTYPE loc = {*pos, *pos};
14307 return NEW_NIL(&loc);
14311new_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)
14313 if (!cc) return right;
14314 cc = cond0(p, cc, COND_IN_COND, loc, true);
14315 return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc));
14319new_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)
14321 if (!cc) return right;
14322 cc = cond0(p, cc, COND_IN_COND, loc, true);
14323 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14326#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))
14329logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14330 const YYLTYPE *op_loc, const YYLTYPE *loc)
14332 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14334 value_expr(p, left);
14335 if (left && nd_type_p(left, type)) {
14336 NODE *node = left, *second;
14337 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14340 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14341 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14342 left->nd_loc.end_pos = loc->end_pos;
14345 op = NEW_AND_OR(type, left, right, loc, op_loc);
14346 nd_set_line(op, op_loc->beg_pos.lineno);
14353no_blockarg(struct parser_params *p, NODE *node)
14355 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14356 compile_error(p, "block argument should not be given");
14361ret_args(struct parser_params *p, NODE *node)
14364 no_blockarg(p, node);
14365 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14366 node = RNODE_LIST(node)->nd_head;
14373negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc)
14375 switch (nd_type(node)) {
14377 RNODE_INTEGER(node)->minus = TRUE;
14380 RNODE_FLOAT(node)->minus = TRUE;
14382 case NODE_RATIONAL:
14383 RNODE_RATIONAL(node)->minus = TRUE;
14385 case NODE_IMAGINARY:
14386 RNODE_IMAGINARY(node)->minus = TRUE;
14389 node->nd_loc = *loc;
14394arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14397 if (!node1) return (NODE *)node2;
14398 node2->nd_head = node1;
14399 nd_set_first_lineno(node2, nd_first_lineno(node1));
14400 nd_set_first_column(node2, nd_first_column(node1));
14401 return (NODE *)node2;
14407args_info_empty_p(struct rb_args_info *args)
14409 if (args->pre_args_num) return false;
14410 if (args->post_args_num) return false;
14411 if (args->rest_arg) return false;
14412 if (args->opt_args) return false;
14413 if (args->block_arg) return false;
14414 if (args->kw_args) return false;
14415 if (args->kw_rest_arg) return false;
14419static rb_node_args_t *
14420new_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)
14422 struct rb_args_info *args = &tail->nd_ainfo;
14424 if (args->forwarding) {
14426 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14429 rest_arg = idFWD_REST;
14432 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14433 args->pre_init = pre_args ? pre_args->nd_next : 0;
14435 args->post_args_num = post_args ? post_args->nd_plen : 0;
14436 args->post_init = post_args ? post_args->nd_next : 0;
14437 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14439 args->rest_arg = rest_arg;
14441 args->opt_args = opt_args;
14443 nd_set_loc(RNODE(tail), loc);
14448static rb_node_args_t *
14449new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14451 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14452 struct rb_args_info *args = &node->nd_ainfo;
14453 if (p->error_p) return node;
14455 args->block_arg = block;
14456 args->kw_args = kw_args;
14460 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14461 * variable order: k1, kr1, k2, &b, internal_id, krest
14463 * variable order: kr1, k1, k2, internal_id, krest, &b
14465 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14466 struct vtable *vtargs = p->lvtbl->args;
14467 rb_node_kw_arg_t *kwn = kw_args;
14469 if (block) block = vtargs->tbl[vtargs->pos-1];
14470 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14471 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14473 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14475 --required_kw_vars;
14476 kwn = kwn->nd_next;
14479 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14480 ID vid = get_nd_vid(p, kwn->nd_body);
14481 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14482 *required_kw_vars++ = vid;
14489 arg_var(p, kw_bits);
14490 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14491 if (block) arg_var(p, block);
14493 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14495 else if (kw_rest_arg == idNil) {
14496 args->no_kwarg = 1;
14498 else if (kw_rest_arg) {
14499 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14505static rb_node_args_t *
14506args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14508 if (max_numparam > NO_PARAM || it_id) {
14510 YYLTYPE loc = RUBY_INIT_YYLLOC();
14511 args = new_args_tail(p, 0, 0, 0, 0);
14512 nd_set_loc(RNODE(args), &loc);
14514 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14520new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14522 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14525 NODE *pre_args = NEW_LIST(pre_arg, loc);
14526 if (RNODE_ARYPTN(aryptn)->pre_args) {
14527 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14530 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14537new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14540 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14545 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14551new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14553 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14559new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14561 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14562 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14563 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14569new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14571 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14576new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14578 NODE *node, *kw_rest_arg_node;
14580 if (kw_rest_arg == idNil) {
14581 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14583 else if (kw_rest_arg) {
14584 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14587 kw_rest_arg_node = NULL;
14590 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14596dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14599 return NEW_SYM(STR_NEW0(), loc);
14602 switch (nd_type(node)) {
14604 nd_set_type(node, NODE_DSYM);
14605 nd_set_loc(node, loc);
14608 node = str_to_sym_node(p, node, loc);
14611 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14618nd_type_st_key_enable_p(NODE *node)
14620 switch (nd_type(node)) {
14623 case NODE_RATIONAL:
14624 case NODE_IMAGINARY:
14630 case NODE_ENCODING:
14638nd_value(struct parser_params *p, NODE *node)
14640 switch (nd_type(node)) {
14642 return rb_node_str_string_val(node);
14644 return rb_node_integer_literal_val(node);
14646 return rb_node_float_literal_val(node);
14647 case NODE_RATIONAL:
14648 return rb_node_rational_literal_val(node);
14649 case NODE_IMAGINARY:
14650 return rb_node_imaginary_literal_val(node);
14652 return rb_node_sym_string_val(node);
14654 return rb_node_regx_string_val(node);
14656 return rb_node_line_lineno_val(node);
14657 case NODE_ENCODING:
14658 return rb_node_encoding_val(node);
14660 return rb_node_file_path_val(node);
14662 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14663 UNREACHABLE_RETURN(0);
14668warn_duplicate_keys(struct parser_params *p, NODE *hash)
14670 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14671 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14672 while (hash && RNODE_LIST(hash)->nd_next) {
14673 NODE *head = RNODE_LIST(hash)->nd_head;
14674 NODE *value = RNODE_LIST(hash)->nd_next;
14675 NODE *next = RNODE_LIST(value)->nd_next;
14679 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14684 if (nd_type_st_key_enable_p(head)) {
14685 key = (st_data_t)head;
14687 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14688 rb_warn2L(nd_line((NODE *)data),
14689 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14690 nd_value(p, head), WARN_I(nd_line(head)));
14692 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14696 st_free_table(p->warn_duplicate_keys_table);
14697 p->warn_duplicate_keys_table = NULL;
14701new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14703 if (hash) warn_duplicate_keys(p, hash);
14704 return NEW_HASH(hash, loc);
14708error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14710 if (is_private_local_id(p, id)) {
14713 if (st_is_member(p->pvtbl, id)) {
14714 yyerror1(loc, "duplicated variable name");
14716 else if (p->ctxt.in_alt_pattern && id) {
14717 yyerror1(loc, "variable capture in alternative pattern");
14720 p->ctxt.capture_in_pattern = 1;
14721 st_insert(p->pvtbl, (st_data_t)id, 0);
14726error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14729 p->pktbl = st_init_numtable();
14731 else if (st_is_member(p->pktbl, key)) {
14732 yyerror1(loc, "duplicated key name");
14735 st_insert(p->pktbl, (st_data_t)key, 0);
14739new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14741 return NEW_HASH(hash, loc);
14745new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14750 ID vid = get_nd_vid(p, lhs);
14751 YYLTYPE lhs_loc = lhs->nd_loc;
14753 set_nd_value(p, lhs, rhs);
14754 nd_set_loc(lhs, loc);
14755 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14757 else if (op == tANDOP) {
14758 set_nd_value(p, lhs, rhs);
14759 nd_set_loc(lhs, loc);
14760 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14764 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14765 set_nd_value(p, asgn, rhs);
14766 nd_set_loc(asgn, loc);
14770 asgn = NEW_ERROR(loc);
14776new_ary_op_assign(struct parser_params *p, NODE *ary,
14777 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14778 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14782 aryset_check(p, args);
14783 args = make_list(args, args_loc);
14784 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14790new_attr_op_assign(struct parser_params *p, NODE *lhs,
14791 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14792 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14796 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14802new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14807 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14810 asgn = NEW_ERROR(loc);
14817const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14819 if (p->ctxt.in_def) {
14821 yyerror1(loc, "dynamic constant assignment");
14823 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14826 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14831assign_error(struct parser_params *p, const char *mesg, VALUE a)
14833 a = dispatch2(assign_error, ERR_MESG(), a);
14840new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14842 NODE *result = head;
14844 NODE *tmp = rescue_else ? rescue_else : rescue;
14845 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14847 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14848 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14851 result = NEW_ENSURE(result, ensure, loc);
14853 fixpos(result, head);
14858warn_unused_var(struct parser_params *p, struct local_vars *local)
14862 if (!local->used) return;
14863 cnt = local->used->pos;
14864 if (cnt != local->vars->pos) {
14865 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14868 ID *v = local->vars->tbl;
14869 ID *u = local->used->tbl;
14870 for (int i = 0; i < cnt; ++i) {
14871 if (!v[i] || (u[i] & LVAR_USED)) continue;
14872 if (is_private_local_id(p, v[i])) continue;
14873 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14879local_push(struct parser_params *p, int toplevel_scope)
14881 struct local_vars *local;
14882 int inherits_dvars = toplevel_scope && compile_for_eval;
14883 int warn_unused_vars = RTEST(ruby_verbose);
14885 local = ALLOC(struct local_vars);
14886 local->prev = p->lvtbl;
14887 local->args = vtable_alloc(0);
14888 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14890 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14891 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14893 local->numparam.outer = 0;
14894 local->numparam.inner = 0;
14895 local->numparam.current = 0;
14897 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14899# if WARN_PAST_SCOPE
14908vtable_chain_free(struct parser_params *p, struct vtable *table)
14910 while (!DVARS_TERMINAL_P(table)) {
14911 struct vtable *cur_table = table;
14912 table = cur_table->prev;
14913 vtable_free(cur_table);
14918local_free(struct parser_params *p, struct local_vars *local)
14920 vtable_chain_free(p, local->used);
14922# if WARN_PAST_SCOPE
14923 vtable_chain_free(p, local->past);
14926 vtable_chain_free(p, local->args);
14927 vtable_chain_free(p, local->vars);
14929 ruby_sized_xfree(local, sizeof(struct local_vars));
14933local_pop(struct parser_params *p)
14935 struct local_vars *local = p->lvtbl->prev;
14936 if (p->lvtbl->used) {
14937 warn_unused_var(p, p->lvtbl);
14940 local_free(p, p->lvtbl);
14947static rb_ast_id_table_t *
14948local_tbl(struct parser_params *p)
14950 int cnt_args = vtable_size(p->lvtbl->args);
14951 int cnt_vars = vtable_size(p->lvtbl->vars);
14952 int cnt = cnt_args + cnt_vars;
14954 rb_ast_id_table_t *tbl;
14956 if (cnt <= 0) return 0;
14957 tbl = rb_ast_new_local_table(p->ast, cnt);
14958 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14959 /* remove IDs duplicated to warn shadowing */
14960 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14961 ID id = p->lvtbl->vars->tbl[i];
14962 if (!vtable_included(p->lvtbl->args, id)) {
14963 tbl->ids[j++] = id;
14967 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14974numparam_name(struct parser_params *p, ID id)
14976 if (!NUMPARAM_ID_P(id)) return;
14977 compile_error(p, "_%d is reserved for numbered parameter",
14978 NUMPARAM_ID_TO_IDX(id));
14982arg_var(struct parser_params *p, ID id)
14984 numparam_name(p, id);
14985 vtable_add(p->lvtbl->args, id);
14989local_var(struct parser_params *p, ID id)
14991 numparam_name(p, id);
14992 vtable_add(p->lvtbl->vars, id);
14993 if (p->lvtbl->used) {
14994 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15000rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15002 return rb_local_defined(id, iseq);
15007local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15009 struct vtable *vars, *args, *used;
15011 vars = p->lvtbl->vars;
15012 args = p->lvtbl->args;
15013 used = p->lvtbl->used;
15015 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15018 if (used) used = used->prev;
15021 if (vars && vars->prev == DVARS_INHERIT) {
15022 return rb_parser_local_defined(p, id, p->parent_iseq);
15024 else if (vtable_included(args, id)) {
15028 int i = vtable_included(vars, id);
15029 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15035local_id(struct parser_params *p, ID id)
15037 return local_id_ref(p, id, NULL);
15041check_forwarding_args(struct parser_params *p)
15043 if (local_id(p, idFWD_ALL)) return TRUE;
15044 compile_error(p, "unexpected ...");
15049add_forwarding_args(struct parser_params *p)
15051 arg_var(p, idFWD_REST);
15052 arg_var(p, idFWD_KWREST);
15053 arg_var(p, idFWD_BLOCK);
15054 arg_var(p, idFWD_ALL);
15058forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15060 bool conflict = false;
15062 struct vtable *vars, *args;
15064 vars = p->lvtbl->vars;
15065 args = p->lvtbl->args;
15067 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15068 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15073 bool found = false;
15074 if (vars && vars->prev == DVARS_INHERIT && !found) {
15075 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15076 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15079 found = (vtable_included(args, arg) &&
15080 !(all && vtable_included(args, all)));
15084 compile_error(p, "no anonymous %s parameter", var);
15086 else if (conflict) {
15087 compile_error(p, "anonymous %s parameter is also used within block", var);
15092new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15094 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15095 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15096 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15097 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15098 block->forwarding = TRUE;
15099 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15100 return arg_blk_pass(args, block);
15104numparam_push(struct parser_params *p)
15106 struct local_vars *local = p->lvtbl;
15107 NODE *inner = local->numparam.inner;
15108 if (!local->numparam.outer) {
15109 local->numparam.outer = local->numparam.current;
15111 local->numparam.inner = 0;
15112 local->numparam.current = 0;
15118numparam_pop(struct parser_params *p, NODE *prev_inner)
15120 struct local_vars *local = p->lvtbl;
15122 /* prefer first one */
15123 local->numparam.inner = prev_inner;
15125 else if (local->numparam.current) {
15126 /* current and inner are exclusive */
15127 local->numparam.inner = local->numparam.current;
15129 if (p->max_numparam > NO_PARAM) {
15130 /* current and outer are exclusive */
15131 local->numparam.current = local->numparam.outer;
15132 local->numparam.outer = 0;
15135 /* no numbered parameter */
15136 local->numparam.current = 0;
15141static const struct vtable *
15142dyna_push(struct parser_params *p)
15144 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15145 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15146 if (p->lvtbl->used) {
15147 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15149 return p->lvtbl->args;
15153dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15155 struct vtable *tmp = *vtblp;
15156 *vtblp = tmp->prev;
15157# if WARN_PAST_SCOPE
15158 if (p->past_scope_enabled) {
15159 tmp->prev = p->lvtbl->past;
15160 p->lvtbl->past = tmp;
15168dyna_pop_1(struct parser_params *p)
15170 struct vtable *tmp;
15172 if ((tmp = p->lvtbl->used) != 0) {
15173 warn_unused_var(p, p->lvtbl);
15174 p->lvtbl->used = p->lvtbl->used->prev;
15177 dyna_pop_vtable(p, &p->lvtbl->args);
15178 dyna_pop_vtable(p, &p->lvtbl->vars);
15182dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15184 while (p->lvtbl->args != lvargs) {
15186 if (!p->lvtbl->args) {
15187 struct local_vars *local = p->lvtbl->prev;
15188 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15196dyna_in_block(struct parser_params *p)
15198 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15203dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15205 struct vtable *vars, *args, *used;
15208 args = p->lvtbl->args;
15209 vars = p->lvtbl->vars;
15210 used = p->lvtbl->used;
15212 while (!DVARS_TERMINAL_P(vars)) {
15213 if (vtable_included(args, id)) {
15216 if ((i = vtable_included(vars, id)) != 0) {
15217 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15222 if (!vidrefp) used = 0;
15223 if (used) used = used->prev;
15226 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15227 return rb_dvar_defined(id, p->parent_iseq);
15235dvar_defined(struct parser_params *p, ID id)
15237 return dvar_defined_ref(p, id, NULL);
15241dvar_curr(struct parser_params *p, ID id)
15243 return (vtable_included(p->lvtbl->args, id) ||
15244 vtable_included(p->lvtbl->vars, id));
15248reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15251 "regexp encoding option '%c' differs from source encoding '%s'",
15252 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15256static rb_encoding *
15257find_enc(struct parser_params* p, const char *name)
15259 int idx = rb_enc_find_index(name);
15261 rb_bug("unknown encoding name: %s", name);
15264 return rb_enc_from_index(idx);
15267static rb_encoding *
15268kcode_to_enc(struct parser_params* p, int kcode)
15273 case ENC_ASCII8BIT:
15274 enc = rb_ascii8bit_encoding();
15277 enc = find_enc(p, "EUC-JP");
15279 case ENC_Windows_31J:
15280 enc = find_enc(p, "Windows-31J");
15283 enc = rb_utf8_encoding();
15294rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15296 int c = RE_OPTION_ENCODING_IDX(options);
15302 char_to_option_kcode(c, &opt, &idx);
15303 enc = kcode_to_enc(p, idx);
15304 if (enc != rb_parser_str_get_encoding(str) &&
15305 !rb_parser_is_ascii_string(p, str)) {
15308 rb_parser_string_set_encoding(str, enc);
15310 else if (RE_OPTION_ENCODING_NONE(options)) {
15311 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15312 !rb_parser_is_ascii_string(p, str)) {
15316 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15318 else if (rb_is_usascii_enc(p->enc)) {
15319 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15329reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15331 int c = rb_reg_fragment_setenc(p, str, options);
15332 if (c) reg_fragment_enc_error(p, str, c);
15335#ifndef UNIVERSAL_PARSER
15337 struct parser_params* parser;
15340 const YYLTYPE *loc;
15341 rb_parser_assignable_func assignable;
15342} reg_named_capture_assign_t;
15345reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15346 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15348 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15349 struct parser_params* p = arg->parser;
15350 rb_encoding *enc = arg->enc;
15351 long len = name_end - name;
15352 const char *s = (const char *)name;
15354 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15358reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15360 reg_named_capture_assign_t arg;
15363 arg.enc = rb_enc_get(regexp);
15364 arg.succ_block = 0;
15366 arg.assignable = assignable;
15367 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15369 if (!arg.succ_block) return 0;
15370 return RNODE_BLOCK(arg.succ_block)->nd_next;
15376rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15378 return assignable(p, id, val, loc);
15382rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15383 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15388 if (!len) return ST_CONTINUE;
15389 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15390 return ST_CONTINUE;
15392 var = intern_cstr(s, len, enc);
15393 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15394 if (!lvar_defined(p, var)) return ST_CONTINUE;
15396 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15397 succ = *succ_block;
15398 if (!succ) succ = NEW_ERROR(loc);
15399 succ = block_append(p, succ, node);
15400 *succ_block = succ;
15401 return ST_CONTINUE;
15406parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15409 reg_fragment_setenc(p, str, options);
15410 str2 = rb_str_new_parser_string(str);
15411 return rb_parser_reg_compile(p, str2, options);
15416rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15418 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15423reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15428 err = rb_errinfo();
15429 re = parser_reg_compile(p, str, options);
15431 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15432 rb_set_errinfo(err);
15433 compile_error(p, "%"PRIsVALUE, m);
15441rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15443 p->do_print = print;
15445 p->do_chomp = chomp;
15446 p->do_split = split;
15450parser_append_options(struct parser_params *p, NODE *node)
15452 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15453 const YYLTYPE *const LOC = &default_location;
15456 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15457 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15459 node = block_append(p, node, print);
15463 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15466 ID ifs = rb_intern("$;");
15467 ID fields = rb_intern("$F");
15468 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15469 NODE *split = NEW_GASGN(fields,
15470 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15471 rb_intern("split"), args, LOC),
15473 node = block_append(p, split, node);
15476 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15477 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15478 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15481 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15490 /* just to suppress unused-function warnings */
15496internal_id(struct parser_params *p)
15498 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15500#endif /* !RIPPER */
15503parser_initialize(struct parser_params *p)
15505 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15506 p->command_start = TRUE;
15507 p->ruby_sourcefile_string = Qnil;
15508 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15509 string_buffer_init(p);
15511 p->delayed.token = NULL;
15512 p->frozen_string_literal = -1; /* not specified */
15514 p->error_buffer = Qfalse;
15515 p->end_expect_token_locations = NULL;
15520 p->parsing_thread = Qnil;
15522 p->s_lvalue = Qnil;
15523 p->s_value_stack = rb_ary_new();
15525 p->debug_buffer = Qnil;
15526 p->debug_output = rb_ractor_stdout();
15527 p->enc = rb_utf8_encoding();
15532#define rb_ruby_parser_mark ripper_parser_mark
15533#define rb_ruby_parser_free ripper_parser_free
15534#define rb_ruby_parser_memsize ripper_parser_memsize
15538rb_ruby_parser_mark(void *ptr)
15540 struct parser_params *p = (struct parser_params*)ptr;
15542 rb_gc_mark(p->ruby_sourcefile_string);
15544 rb_gc_mark(p->error_buffer);
15546 rb_gc_mark(p->value);
15547 rb_gc_mark(p->result);
15548 rb_gc_mark(p->parsing_thread);
15549 rb_gc_mark(p->s_value);
15550 rb_gc_mark(p->s_lvalue);
15551 rb_gc_mark(p->s_value_stack);
15553 rb_gc_mark(p->debug_buffer);
15554 rb_gc_mark(p->debug_output);
15558rb_ruby_parser_free(void *ptr)
15560 struct parser_params *p = (struct parser_params*)ptr;
15561 struct local_vars *local, *prev;
15564 rb_ast_free(p->ast);
15567 if (p->warn_duplicate_keys_table) {
15568 st_free_table(p->warn_duplicate_keys_table);
15573 rb_parser_ary_free(p, p->tokens);
15578 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15581 for (local = p->lvtbl; local; local = prev) {
15582 prev = local->prev;
15583 local_free(p, local);
15587 token_info *ptinfo;
15588 while ((ptinfo = p->token_info) != 0) {
15589 p->token_info = ptinfo->next;
15593 string_buffer_free(p);
15596 st_free_table(p->pvtbl);
15599 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15600 st_free_table(p->case_labels);
15603 xfree(p->lex.strterm);
15604 p->lex.strterm = 0;
15610rb_ruby_parser_memsize(const void *ptr)
15612 struct parser_params *p = (struct parser_params*)ptr;
15613 struct local_vars *local;
15614 size_t size = sizeof(*p);
15617 for (local = p->lvtbl; local; local = local->prev) {
15618 size += sizeof(*local);
15619 if (local->vars) size += local->vars->capa * sizeof(ID);
15625#undef rb_reserved_word
15627const struct kwtable *
15628rb_reserved_word(const char *str, unsigned int len)
15630 return reserved_word(str, len);
15633#ifdef UNIVERSAL_PARSER
15635rb_ruby_parser_allocate(const rb_parser_config_t *config)
15637 /* parser_initialize expects fields to be set to 0 */
15638 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15639 p->config = config;
15644rb_ruby_parser_new(const rb_parser_config_t *config)
15646 /* parser_initialize expects fields to be set to 0 */
15647 rb_parser_t *p = rb_ruby_parser_allocate(config);
15648 parser_initialize(p);
15653rb_ruby_parser_allocate(void)
15655 /* parser_initialize expects fields to be set to 0 */
15656 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15661rb_ruby_parser_new(void)
15663 /* parser_initialize expects fields to be set to 0 */
15664 rb_parser_t *p = rb_ruby_parser_allocate();
15665 parser_initialize(p);
15671rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15673 p->error_buffer = main ? Qfalse : Qnil;
15674 p->parent_iseq = base;
15679rb_ruby_parser_set_script_lines(rb_parser_t *p)
15681 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15685rb_ruby_parser_error_tolerant(rb_parser_t *p)
15687 p->error_tolerant = 1;
15691rb_ruby_parser_keep_tokens(rb_parser_t *p)
15693 p->keep_tokens = 1;
15694 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15698rb_ruby_parser_encoding(rb_parser_t *p)
15704rb_ruby_parser_end_seen_p(rb_parser_t *p)
15706 return p->ruby__end__seen;
15710rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15715#endif /* !RIPPER */
15719rb_ruby_parser_get_yydebug(rb_parser_t *p)
15725rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15731rb_ruby_parser_error_p(rb_parser_t *p)
15737rb_ruby_parser_debug_output(rb_parser_t *p)
15739 return p->debug_output;
15743rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15745 p->debug_output = output;
15749rb_ruby_parser_parsing_thread(rb_parser_t *p)
15751 return p->parsing_thread;
15755rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15757 p->parsing_thread = parsing_thread;
15761rb_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)
15763 p->lex.gets = gets;
15764 p->lex.input = input;
15766 p->ruby_sourcefile_string = sourcefile_string;
15767 p->ruby_sourcefile = sourcefile;
15768 p->ruby_sourceline = sourceline;
15772rb_ruby_parser_result(rb_parser_t *p)
15778rb_ruby_parser_enc(rb_parser_t *p)
15784rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15786 return p->ruby_sourcefile_string;
15790rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15792 return p->ruby_sourceline;
15796rb_ruby_parser_lex_state(rb_parser_t *p)
15798 return p->lex.state;
15802rb_ruby_ripper_parse0(rb_parser_t *p)
15805 p->ast = rb_ast_new();
15806 ripper_yyparse((void*)p);
15807 rb_ast_free(p->ast);
15810 p->eval_tree_begin = 0;
15814rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15816 return dedent_string(p, string, width);
15820rb_ruby_ripper_initialized_p(rb_parser_t *p)
15822 return p->lex.input != 0;
15826rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15828 parser_initialize(p);
15832rb_ruby_ripper_column(rb_parser_t *p)
15834 return p->lex.ptok - p->lex.pbeg;
15838rb_ruby_ripper_token_len(rb_parser_t *p)
15840 return p->lex.pcur - p->lex.ptok;
15843rb_parser_string_t *
15844rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15846 return p->lex.lastline;
15850rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15852 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15855#ifdef UNIVERSAL_PARSER
15857rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15859 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15860 p->config = config;
15865struct parser_params*
15866rb_ruby_ripper_parser_allocate(void)
15868 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15874rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15877 VALUE mesg = p->debug_buffer;
15879 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15881 rb_str_vcatf(mesg, fmt, ap);
15883 if (char_at_end(p, mesg, 0) == '\n') {
15884 rb_io_write(p->debug_output, mesg);
15885 p->debug_buffer = Qnil;
15890parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15893 int lineno, column;
15896 lineno = loc->end_pos.lineno;
15897 column = loc->end_pos.column;
15900 lineno = p->ruby_sourceline;
15901 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15904 rb_io_flush(p->debug_output);
15908 rb_syntax_error_append(p->error_buffer,
15909 p->ruby_sourcefile_string,
15916count_char(const char *str, int c)
15919 while (str[n] == c) ++n;
15924 * strip enclosing double-quotes, same as the default yytnamerr except
15925 * for that single-quotes matching back-quotes do not stop stripping.
15927 * "\"`class' keyword\"" => "`class' keyword"
15930rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
15932 if (*yystr == '"') {
15933 size_t yyn = 0, bquote = 0;
15934 const char *yyp = yystr;
15940 bquote = count_char(yyp+1, '\'') + 1;
15941 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
15947 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
15948 if (yyres) memcpy(yyres + yyn, yyp, bquote);
15954 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
15955 if (yyres) memcpy(yyres + yyn, yyp, 3);
15960 goto do_not_strip_quotes;
15964 goto do_not_strip_quotes;
15967 if (*++yyp != '\\')
15968 goto do_not_strip_quotes;
15969 /* Fall through. */
15983 do_not_strip_quotes: ;
15986 if (!yyres) return strlen(yystr);
15988 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
15993#define validate(x) (void)(x)
15996ripper_dispatch0(struct parser_params *p, ID mid)
15998 return rb_funcall(p->value, mid, 0);
16002ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16005 return rb_funcall(p->value, mid, 1, a);
16009ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16013 return rb_funcall(p->value, mid, 2, a, b);
16017ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16022 return rb_funcall(p->value, mid, 3, a, b, c);
16026ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16032 return rb_funcall(p->value, mid, 4, a, b, c, d);
16036ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16043 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16047ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16056 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16060ripper_error(struct parser_params *p)
16066ripper_value(struct parser_params *p)
16068 (void)yystpcpy; /* may not used in newer bison */
16077 * c-file-style: "ruby"