1/**********************************************************************
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10**********************************************************************/
17# error needs pure parser
20#define YYERROR_VERBOSE 1
21#define YYSTACK_USE_ALLOCA 0
22#define YYLTYPE rb_code_location_t
23#define YYLTYPE_IS_DECLARED 1
27# include RUBY_EXTCONF_H
30#include "ruby/internal/config.h"
34#ifdef UNIVERSAL_PARSER
36#include "internal/ruby_parser.h"
37#include "parser_node.h"
38#include "universal_parser.c"
43#define STATIC_ID2SYM p->config->static_id2sym
44#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
50#include "internal/compile.h"
51#include "internal/compilers.h"
52#include "internal/complex.h"
53#include "internal/encoding.h"
54#include "internal/error.h"
55#include "internal/hash.h"
56#include "internal/imemo.h"
57#include "internal/io.h"
58#include "internal/numeric.h"
59#include "internal/parse.h"
60#include "internal/rational.h"
61#include "internal/re.h"
62#include "internal/ruby_parser.h"
63#include "internal/symbol.h"
64#include "internal/thread.h"
65#include "internal/variable.h"
67#include "parser_node.h"
70#include "ruby/encoding.h"
71#include "ruby/regex.h"
75#include "ruby/ractor.h"
86rational_set_num(VALUE r, VALUE n)
88 RATIONAL_SET_NUM(r, n);
92rational_get_num(VALUE obj)
94 return RRATIONAL(obj)->num;
98rcomplex_set_real(VALUE cmp, VALUE r)
100 RCOMPLEX_SET_REAL(cmp, r);
104rcomplex_get_real(VALUE obj)
106 return RCOMPLEX(obj)->real;
110rcomplex_set_imag(VALUE cmp, VALUE i)
112 RCOMPLEX_SET_IMAG(cmp, i);
116rcomplex_get_imag(VALUE obj)
118 return RCOMPLEX(obj)->imag;
122hash_literal_key_p(VALUE k)
124 switch (OBJ_BUILTIN_TYPE(k)) {
133literal_cmp(VALUE val, VALUE lit)
135 if (val == lit) return 0;
136 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
137 return rb_iseq_cdhash_cmp(val, lit);
143 if (!hash_literal_key_p(a)) return (st_index_t)a;
144 return rb_iseq_cdhash_hash(a);
148syntax_error_new(void)
150 return rb_class_new_instance(0, 0, rb_eSyntaxError);
153static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
156#define compile_callback rb_suppress_tracing
157VALUE rb_io_gets_internal(VALUE io);
159VALUE rb_node_case_when_optimizable_literal(const NODE *const node);
160#endif /* !UNIVERSAL_PARSER */
165 return '\0' <= c && c <= '\x7f';
169#define ISASCII parse_isascii
174 return c == ' ' || ('\t' <= c && c <= '\r');
178#define ISSPACE parse_isspace
183 return ('\0' <= c && c < ' ') || c == '\x7f';
187#define ISCNTRL(c) parse_iscntrl(c)
192 return 'A' <= c && c <= 'Z';
198 return 'a' <= c && c <= 'z';
204 return parse_isupper(c) || parse_islower(c);
208#define ISALPHA(c) parse_isalpha(c)
213 return '0' <= c && c <= '9';
217#define ISDIGIT(c) parse_isdigit(c)
222 return parse_isalpha(c) || parse_isdigit(c);
226#define ISALNUM(c) parse_isalnum(c)
231 return parse_isdigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
235#define ISXDIGIT(c) parse_isxdigit(c)
237#include "parser_st.h"
240#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
243#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
246#include "ripper_init.h"
253 shareable_everything,
264 unsigned int in_defined: 1;
265 unsigned int in_kwarg: 1;
266 unsigned int in_argdef: 1;
267 unsigned int in_def: 1;
268 unsigned int in_class: 1;
269 BITFIELD(enum shareability, shareable_constant_value, 2);
270 BITFIELD(enum rescue_context, in_rescue, 2);
273typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
274typedef struct RNode_EXITS rb_node_exits_t;
276#if defined(__GNUC__) && !defined(__clang__)
277// Suppress "parameter passing for argument of type 'struct
278// lex_context' changed" notes. `struct lex_context` is file scope,
279// and has no ABI compatibility issue.
281RBIMPL_WARNING_IGNORED(-Wpsabi)
283// Not sure why effective even after popped.
288#define NO_LEX_CTXT (struct lex_context){0}
290#define AREF(ary, i) RARRAY_AREF(ary, i)
292#ifndef WARN_PAST_SCOPE
293# define WARN_PAST_SCOPE 0
298#define yydebug (p->debug) /* disable the global variable definition */
300#define YYMALLOC(size) rb_parser_malloc(p, (size))
301#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
302#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
303#define YYFREE(ptr) rb_parser_free(p, (ptr))
304#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
305#define YY_LOCATION_PRINT(File, loc, p) \
306 rb_parser_printf(p, "%d.%d-%d.%d", \
307 (loc).beg_pos.lineno, (loc).beg_pos.column,\
308 (loc).end_pos.lineno, (loc).end_pos.column)
309#define YYLLOC_DEFAULT(Current, Rhs, N) \
313 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
314 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
318 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
319 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
323 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
324 "nesting too deep" : (Msgid))
326#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
327 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
328#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
329 rb_parser_set_location_of_delayed_token(p, &(Current))
330#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
331 rb_parser_set_location_of_heredoc_end(p, &(Current))
332#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
333 rb_parser_set_location_of_dummy_end(p, &(Current))
334#define RUBY_SET_YYLLOC_OF_NONE(Current) \
335 rb_parser_set_location_of_none(p, &(Current))
336#define RUBY_SET_YYLLOC(Current) \
337 rb_parser_set_location(p, &(Current))
338#define RUBY_INIT_YYLLOC() \
340 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
341 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
344#define IS_lex_state_for(x, ls) ((x) & (ls))
345#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
346#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
347#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
349# define SET_LEX_STATE(ls) \
350 parser_set_lex_state(p, ls, __LINE__)
351static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
353typedef VALUE stack_type;
355static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
357# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
358# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
359# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
360# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
361# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
363/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
364 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
365#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
366#define COND_POP() BITSTACK_POP(cond_stack)
367#define COND_P() BITSTACK_SET_P(cond_stack)
368#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
370/* A flag to identify keyword_do_block; "do" keyword after command_call.
371 Example: `foo 1, 2 do`. */
372#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
373#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
374#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
375#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
391 struct local_vars *prev;
394 NODE *outer, *inner, *current;
405#define DVARS_INHERIT ((void*)1)
406#define DVARS_TOPSCOPE NULL
407#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
409typedef struct token_info {
411 rb_code_position_t beg;
414 struct token_info *next;
418 Structure of Lexer Buffer:
420 lex.pbeg lex.ptok lex.pcur lex.pend
422 |------------+------------+------------|
426struct parser_params {
427 rb_imemo_tmpbuf_t *heap;
433 rb_strterm_t *strterm;
434 VALUE (*gets)(struct parser_params*,VALUE);
444 VALUE (*call)(VALUE, int);
446 enum lex_state_e state;
447 /* track the nest level of any parens "()[]{}" */
449 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
451 /* track the nest level of only braces "{}" */
454 stack_type cond_stack;
455 stack_type cmdarg_stack;
460 int heredoc_line_indent;
462 struct local_vars *lvtbl;
466 int ruby_sourceline; /* current line no. */
467 const char *ruby_sourcefile; /* current source file */
468 VALUE ruby_sourcefile_string;
470 token_info *token_info;
472 rb_node_exits_t *exits;
492 struct lex_context ctxt;
494#ifdef UNIVERSAL_PARSER
495 rb_parser_config_t *config;
498 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
500 unsigned int command_start:1;
501 unsigned int eofp: 1;
502 unsigned int ruby__end__seen: 1;
503 unsigned int debug: 1;
504 unsigned int has_shebang: 1;
505 unsigned int token_seen: 1;
506 unsigned int token_info_enabled: 1;
508 unsigned int past_scope_enabled: 1;
510 unsigned int error_p: 1;
511 unsigned int cr_seen: 1;
516 unsigned int do_print: 1;
517 unsigned int do_loop: 1;
518 unsigned int do_chomp: 1;
519 unsigned int do_split: 1;
520 unsigned int error_tolerant: 1;
521 unsigned int keep_tokens: 1;
523 NODE *eval_tree_begin;
527 const struct rb_iseq_struct *parent_iseq;
528 /* store specific keyword locations to generate dummy end token */
529 VALUE end_expect_token_locations;
532 /* Array for term tokens */
539 VALUE parsing_thread;
543#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
544#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
545#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
547numparam_id_p(struct parser_params *p, ID id)
549 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
550 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
551 return idx > 0 && idx <= NUMPARAM_MAX;
553static void numparam_name(struct parser_params *p, ID id);
556#define intern_cstr(n,l,en) rb_intern3(n,l,en)
558#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
559#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
560#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
561#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
562#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
563#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
566end_with_newline_p(struct parser_params *p, VALUE str)
568 return RSTRING_LEN(str) > 0 && RSTRING_END(str)[-1] == '\n';
572pop_pvtbl(struct parser_params *p, st_table *tbl)
574 st_free_table(p->pvtbl);
579pop_pktbl(struct parser_params *p, st_table *tbl)
581 if (p->pktbl) st_free_table(p->pktbl);
586static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
589debug_end_expect_token_locations(struct parser_params *p, const char *name)
592 VALUE mesg = rb_sprintf("%s: ", name);
593 rb_str_catf(mesg, " %"PRIsVALUE"\n", p->end_expect_token_locations);
594 flush_debug_buffer(p, p->debug_output, mesg);
599push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
601 if(NIL_P(p->end_expect_token_locations)) return;
602 rb_ary_push(p->end_expect_token_locations, rb_ary_new_from_args(2, INT2NUM(pos->lineno), INT2NUM(pos->column)));
603 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
607pop_end_expect_token_locations(struct parser_params *p)
609 if(NIL_P(p->end_expect_token_locations)) return;
610 rb_ary_pop(p->end_expect_token_locations);
611 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
615peek_end_expect_token_locations(struct parser_params *p)
617 if(NIL_P(p->end_expect_token_locations)) return Qnil;
618 return rb_ary_last(0, 0, p->end_expect_token_locations);
622parser_token2id(struct parser_params *p, enum yytokentype tok)
625#define TOKEN2ID(tok) case tok: return rb_intern(#tok);
626#define TOKEN2ID2(tok, name) case tok: return rb_intern(name);
627 TOKEN2ID2(' ', "words_sep")
647 TOKEN2ID2('\n', "nl");
654 TOKEN2ID2('\\', "backslash");
655 TOKEN2ID(keyword_class);
656 TOKEN2ID(keyword_module);
657 TOKEN2ID(keyword_def);
658 TOKEN2ID(keyword_undef);
659 TOKEN2ID(keyword_begin);
660 TOKEN2ID(keyword_rescue);
661 TOKEN2ID(keyword_ensure);
662 TOKEN2ID(keyword_end);
663 TOKEN2ID(keyword_if);
664 TOKEN2ID(keyword_unless);
665 TOKEN2ID(keyword_then);
666 TOKEN2ID(keyword_elsif);
667 TOKEN2ID(keyword_else);
668 TOKEN2ID(keyword_case);
669 TOKEN2ID(keyword_when);
670 TOKEN2ID(keyword_while);
671 TOKEN2ID(keyword_until);
672 TOKEN2ID(keyword_for);
673 TOKEN2ID(keyword_break);
674 TOKEN2ID(keyword_next);
675 TOKEN2ID(keyword_redo);
676 TOKEN2ID(keyword_retry);
677 TOKEN2ID(keyword_in);
678 TOKEN2ID(keyword_do);
679 TOKEN2ID(keyword_do_cond);
680 TOKEN2ID(keyword_do_block);
681 TOKEN2ID(keyword_do_LAMBDA);
682 TOKEN2ID(keyword_return);
683 TOKEN2ID(keyword_yield);
684 TOKEN2ID(keyword_super);
685 TOKEN2ID(keyword_self);
686 TOKEN2ID(keyword_nil);
687 TOKEN2ID(keyword_true);
688 TOKEN2ID(keyword_false);
689 TOKEN2ID(keyword_and);
690 TOKEN2ID(keyword_or);
691 TOKEN2ID(keyword_not);
692 TOKEN2ID(modifier_if);
693 TOKEN2ID(modifier_unless);
694 TOKEN2ID(modifier_while);
695 TOKEN2ID(modifier_until);
696 TOKEN2ID(modifier_rescue);
697 TOKEN2ID(keyword_alias);
698 TOKEN2ID(keyword_defined);
699 TOKEN2ID(keyword_BEGIN);
700 TOKEN2ID(keyword_END);
701 TOKEN2ID(keyword__LINE__);
702 TOKEN2ID(keyword__FILE__);
703 TOKEN2ID(keyword__ENCODING__);
704 TOKEN2ID(tIDENTIFIER);
714 TOKEN2ID(tIMAGINARY);
718 TOKEN2ID(tSTRING_CONTENT);
719 TOKEN2ID(tREGEXP_END);
720 TOKEN2ID(tDUMNY_END);
749 TOKEN2ID(tLPAREN_ARG);
753 TOKEN2ID(tLBRACE_ARG);
759 TOKEN2ID(tSTRING_BEG);
760 TOKEN2ID(tXSTRING_BEG);
761 TOKEN2ID(tREGEXP_BEG);
762 TOKEN2ID(tWORDS_BEG);
763 TOKEN2ID(tQWORDS_BEG);
764 TOKEN2ID(tSYMBOLS_BEG);
765 TOKEN2ID(tQSYMBOLS_BEG);
766 TOKEN2ID(tSTRING_END);
767 TOKEN2ID(tSTRING_DEND);
768 TOKEN2ID(tSTRING_DBEG);
769 TOKEN2ID(tSTRING_DVAR);
771 TOKEN2ID(tLABEL_END);
772 TOKEN2ID(tIGNORED_NL);
774 TOKEN2ID(tEMBDOC_BEG);
776 TOKEN2ID(tEMBDOC_END);
777 TOKEN2ID(tHEREDOC_BEG);
778 TOKEN2ID(tHEREDOC_END);
781 TOKEN2ID(tUMINUS_NUM);
782 TOKEN2ID(tLAST_TOKEN);
787 rb_bug("parser_token2id: unknown token %d", tok);
789 UNREACHABLE_RETURN(0);
794RBIMPL_ATTR_NONNULL((1, 2, 3))
795static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
796RBIMPL_ATTR_NONNULL((1, 2))
797static int parser_yyerror0(struct parser_params*, const char*);
798#define yyerror0(msg) parser_yyerror0(p, (msg))
799#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
800#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
801#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
802#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
803#define lex_eol_p(p) lex_eol_n_p(p, 0)
804#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
805#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
806#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
808static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
809static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
810static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
811static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
812static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
815#define compile_for_eval (0)
817#define compile_for_eval (p->parent_iseq != 0)
820#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
822#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
823#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
825#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
827static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
831rb_discard_node(struct parser_params *p, NODE *n)
833 rb_ast_delete_node(p->ast, n);
839add_mark_object(struct parser_params *p, VALUE obj)
841 if (!SPECIAL_CONST_P(obj)
842 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
844 rb_ast_add_mark_object(p->ast, obj);
849static rb_node_ripper_t *rb_node_ripper_new(struct parser_params *p, ID a, VALUE b, VALUE c, const YYLTYPE *loc);
850static rb_node_ripper_values_t *rb_node_ripper_values_new(struct parser_params *p, VALUE a, VALUE b, VALUE c, const YYLTYPE *loc);
851#define NEW_RIPPER(a,b,c,loc) (VALUE)rb_node_ripper_new(p,a,b,c,loc)
852#define NEW_RIPPER_VALUES(a,b,c,loc) (VALUE)rb_node_ripper_values_new(p,a,b,c,loc)
855static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
856static 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, const YYLTYPE *loc);
857static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
858static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc);
859static rb_node_unless_t *rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc);
860static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
861static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
862static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
863static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
864static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
865static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc);
866static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc);
867static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
868static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc);
869static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
870static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
871static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
872static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
873static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc);
874static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
875static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
876static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
877static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
878static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
879static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
880static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
881static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
882static rb_node_cdecl_t *rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc);
883static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
884static 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);
885static 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);
886static 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);
887static 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);
888static rb_node_op_cdecl_t *rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, const YYLTYPE *loc);
889static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
890static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
891static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
892static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
893static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
894static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc);
895static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
896static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
897static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
898static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
899static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
900static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc);
901static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
902static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
903static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
904static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
905static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
906static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
907static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
908static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
909static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
910static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
911static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
912static rb_node_lit_t *rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc);
913static rb_node_str_t *rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc);
914static rb_node_dstr_t *rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
915static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc);
916static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc);
917static rb_node_dxstr_t *rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
918static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
919static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
920static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
921static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc);
922static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
923static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
924static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
925static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
926static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
927static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
928static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
929static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
930static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
931static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
932static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc);
933static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
934static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc);
935static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
936static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
937static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc);
938static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
939static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
940static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
941static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
942static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
943static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
944static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
945static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
946static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
947static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
948static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
949static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
950static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
951static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
952static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
953static 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);
954static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
956#define NEW_SCOPE(a,b,loc) (NODE *)rb_node_scope_new(p,a,b,loc)
957#define NEW_SCOPE2(t,a,b,loc) (NODE *)rb_node_scope_new2(p,t,a,b,loc)
958#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
959#define NEW_IF(c,t,e,loc) (NODE *)rb_node_if_new(p,c,t,e,loc)
960#define NEW_UNLESS(c,t,e,loc) (NODE *)rb_node_unless_new(p,c,t,e,loc)
961#define NEW_CASE(h,b,loc) (NODE *)rb_node_case_new(p,h,b,loc)
962#define NEW_CASE2(b,loc) (NODE *)rb_node_case2_new(p,b,loc)
963#define NEW_CASE3(h,b,loc) (NODE *)rb_node_case3_new(p,h,b,loc)
964#define NEW_WHEN(c,t,e,loc) (NODE *)rb_node_when_new(p,c,t,e,loc)
965#define NEW_IN(c,t,e,loc) (NODE *)rb_node_in_new(p,c,t,e,loc)
966#define NEW_WHILE(c,b,n,loc) (NODE *)rb_node_while_new(p,c,b,n,loc)
967#define NEW_UNTIL(c,b,n,loc) (NODE *)rb_node_until_new(p,c,b,n,loc)
968#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
969#define NEW_FOR(i,b,loc) (NODE *)rb_node_for_new(p,i,b,loc)
970#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
971#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
972#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
973#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
974#define NEW_RESBODY(a,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,ex,n,loc)
975#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
976#define NEW_AND(f,s,loc) (NODE *)rb_node_and_new(p,f,s,loc)
977#define NEW_OR(f,s,loc) (NODE *)rb_node_or_new(p,f,s,loc)
978#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
979#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
980#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
981#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
982#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
983#define NEW_CDECL(v,val,path,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,loc)
984#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
985#define NEW_OP_ASGN1(r,id,idx,rval,loc) (NODE *)rb_node_op_asgn1_new(p,r,id,idx,rval,loc)
986#define NEW_OP_ASGN2(r,t,i,o,val,loc) (NODE *)rb_node_op_asgn2_new(p,r,val,i,o,t,loc)
987#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
988#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
989#define NEW_OP_CDECL(v,op,val,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,loc)
990#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
991#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
992#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
993#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
994#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
995#define NEW_SUPER(a,loc) (NODE *)rb_node_super_new(p,a,loc)
996#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
997#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
998#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
999#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1000#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1001#define NEW_RETURN(s,loc) (NODE *)rb_node_return_new(p,s,loc)
1002#define NEW_YIELD(a,loc) (NODE *)rb_node_yield_new(p,a,loc)
1003#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1004#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1005#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1006#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1007#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1008#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1009#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1010#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1011#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1012#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1013#define NEW_LIT(l,loc) (NODE *)rb_node_lit_new(p,l,loc)
1014#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1015#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1016#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1017#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1018#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1019#define NEW_EVSTR(n,loc) (NODE *)rb_node_evstr_new(p,n,loc)
1020#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1021#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1022#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1023#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1024#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1025#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1026#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1027#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1028#define NEW_SPLAT(a,loc) (NODE *)rb_node_splat_new(p,a,loc)
1029#define NEW_BLOCK_PASS(b,loc) rb_node_block_pass_new(p,b,loc)
1030#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1031#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1032#define NEW_ALIAS(n,o,loc) (NODE *)rb_node_alias_new(p,n,o,loc)
1033#define NEW_VALIAS(n,o,loc) (NODE *)rb_node_valias_new(p,n,o,loc)
1034#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1035#define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc)
1036#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
1037#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
1038#define NEW_COLON2(c,i,loc) (NODE *)rb_node_colon2_new(p,c,i,loc)
1039#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc)
1040#define NEW_DOT2(b,e,loc) (NODE *)rb_node_dot2_new(p,b,e,loc)
1041#define NEW_DOT3(b,e,loc) (NODE *)rb_node_dot3_new(p,b,e,loc)
1042#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1043#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1044#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1045#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1046#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1047#define NEW_DEFINED(e,loc) (NODE *)rb_node_defined_new(p,e,loc)
1048#define NEW_POSTEXE(b,loc) (NODE *)rb_node_postexe_new(p,b,loc)
1049#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1050#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1051#define NEW_LAMBDA(a,b,loc) (NODE *)rb_node_lambda_new(p,a,b,loc)
1052#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1053#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1054#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1055#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1059enum internal_node_type {
1060 NODE_INTERNAL_ONLY = NODE_LAST,
1067parser_node_name(int node)
1071 return "NODE_DEF_TEMP";
1073 return "NODE_EXITS";
1075 return ruby_node_name(node);
1079/* This node is parse.y internal */
1080struct RNode_DEF_TEMP {
1083 /* for NODE_DEFN/NODE_DEFS */
1085 struct RNode *nd_def;
1096 NODE *numparam_save;
1097 struct lex_context ctxt;
1101#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1103static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc);
1104static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc);
1105static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc);
1106static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1107static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1109#define NEW_BREAK(s,loc) (NODE *)rb_node_break_new(p,s,loc)
1110#define NEW_NEXT(s,loc) (NODE *)rb_node_next_new(p,s,loc)
1111#define NEW_REDO(loc) (NODE *)rb_node_redo_new(p,loc)
1112#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1114/* Make a new internal node, which should not be appeared in the
1115 * result AST and does not have node_id and location. */
1116static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1117#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1119static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1122parser_get_node_id(struct parser_params *p)
1124 int node_id = p->node_id;
1130anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1132 if (id == tANDDOT) {
1133 yyerror1(loc, "&. inside multiple assignment destination");
1139set_line_body(NODE *body, int line)
1142 switch (nd_type(body)) {
1145 nd_set_line(body, line);
1150set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1152 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1153 nd_set_line(node, beg->end_pos.lineno);
1157last_expr_node(NODE *expr)
1160 if (nd_type_p(expr, NODE_BLOCK)) {
1161 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1163 else if (nd_type_p(expr, NODE_BEGIN)) {
1164 expr = RNODE_BEGIN(expr)->nd_body;
1173#define yyparse ruby_yyparse
1175static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1176static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1177#define new_nil(loc) NEW_NIL(loc)
1178static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1179static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
1180static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
1181static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1183static NODE *newline_node(NODE*);
1184static void fixpos(NODE*,NODE*);
1186static int value_expr_gen(struct parser_params*,NODE*);
1187static void void_expr(struct parser_params*,NODE*);
1188static NODE *remove_begin(NODE*);
1189#define value_expr(node) value_expr_gen(p, (node))
1190static NODE *void_stmts(struct parser_params*,NODE*);
1191static void reduce_nodes(struct parser_params*,NODE**);
1192static void block_dup_check(struct parser_params*,NODE*,NODE*);
1194static NODE *block_append(struct parser_params*,NODE*,NODE*);
1195static NODE *list_append(struct parser_params*,NODE*,NODE*);
1196static NODE *list_concat(NODE*,NODE*);
1197static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1198static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1199static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1200static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1201static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
1202static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1203static NODE *str2dstr(struct parser_params*,NODE*);
1204static NODE *evstr2dstr(struct parser_params*,NODE*);
1205static NODE *splat_array(NODE*);
1206static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1208static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1209static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1210static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1211static 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);
1212static 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;}
1214static bool args_info_empty_p(struct rb_args_info *args);
1215static 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*);
1216static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1217static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1218static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1219static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1220static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1221static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1222static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1224static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1225static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int);
1227static VALUE negate_lit(struct parser_params*, VALUE);
1228static NODE *ret_args(struct parser_params*,NODE*);
1229static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1230static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
1231static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1233static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1234static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1236static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1237static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1239static void rb_backref_error(struct parser_params*,NODE*);
1240static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1242static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1243static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
1244static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
1245static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1246static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1248static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1250static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1251static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1253static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1254static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1256static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
1258static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
1260#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1262static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1264static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1266static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1268static rb_ast_id_table_t *local_tbl(struct parser_params*);
1270static VALUE reg_compile(struct parser_params*, VALUE, int);
1271static void reg_fragment_setenc(struct parser_params*, VALUE, int);
1272static int reg_fragment_check(struct parser_params*, VALUE, int);
1274static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
1275static NODE *heredoc_dedent(struct parser_params*,NODE*);
1277static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1279#define get_id(id) (id)
1280#define get_value(val) (val)
1281#define get_num(num) (num)
1284static inline int ripper_is_node_yylval(struct parser_params *p, VALUE n);
1287ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
1289 if (ripper_is_node_yylval(p, c)) c = RNODE_RIPPER(c)->nd_cval;
1290 add_mark_object(p, b);
1291 add_mark_object(p, c);
1292 return NEW_RIPPER(a, b, c, &NULL_LOC);
1296ripper_new_yylval2(struct parser_params *p, VALUE a, VALUE b, VALUE c)
1298 add_mark_object(p, a);
1299 add_mark_object(p, b);
1300 add_mark_object(p, c);
1301 return NEW_RIPPER_VALUES(a, b, c, &NULL_LOC);
1305ripper_is_node_yylval(struct parser_params *p, VALUE n)
1307 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
1310#define value_expr(node) ((void)(node))
1311#define remove_begin(node) (node)
1312#define void_stmts(p,x) (x)
1313#undef rb_dvar_defined
1314#define rb_dvar_defined(id, base) 0
1315#undef rb_local_defined
1316#define rb_local_defined(id, base) 0
1317#define get_id(id) ripper_get_id(id)
1318#define get_value(val) ripper_get_value(val)
1319#define get_num(num) (int)get_id(num)
1320static VALUE assignable(struct parser_params*,VALUE);
1321static int id_is_var(struct parser_params *p, ID id);
1323#define method_cond(p,node,loc) (node)
1324#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
1325#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
1326#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
1327#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
1329#define new_nil(loc) Qnil
1331static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
1333static VALUE const_decl(struct parser_params *p, VALUE path);
1335static VALUE var_field(struct parser_params *p, VALUE a);
1336static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1338static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
1340static VALUE backref_error(struct parser_params*, NODE *, VALUE);
1343RUBY_SYMBOL_EXPORT_BEGIN
1344VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1345int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
1346enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1347VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1348void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1349PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1350YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1351YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1352YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1353YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1354YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1355YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1356RUBY_SYMBOL_EXPORT_END
1358static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1359static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1361static ID formal_argument(struct parser_params*, ID);
1363static ID formal_argument(struct parser_params*, VALUE);
1365static ID shadowing_lvar(struct parser_params*,ID);
1366static void new_bv(struct parser_params*,ID);
1368static void local_push(struct parser_params*,int);
1369static void local_pop(struct parser_params*);
1370static void local_var(struct parser_params*, ID);
1371static void arg_var(struct parser_params*, ID);
1372static int local_id(struct parser_params *p, ID id);
1373static int local_id_ref(struct parser_params*, ID, ID **);
1375static ID internal_id(struct parser_params*);
1376static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1378static int check_forwarding_args(struct parser_params*);
1379static void add_forwarding_args(struct parser_params *p);
1380static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1382static const struct vtable *dyna_push(struct parser_params *);
1383static void dyna_pop(struct parser_params*, const struct vtable *);
1384static int dyna_in_block(struct parser_params*);
1385#define dyna_var(p, id) local_var(p, id)
1386static int dvar_defined(struct parser_params*, ID);
1387static int dvar_defined_ref(struct parser_params*, ID, ID**);
1388static int dvar_curr(struct parser_params*,ID);
1390static int lvar_defined(struct parser_params*, ID);
1392static NODE *numparam_push(struct parser_params *p);
1393static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1396# define METHOD_NOT idNOT
1398# define METHOD_NOT '!'
1401#define idFWD_REST '*'
1402#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1403#define idFWD_BLOCK '&'
1404#define idFWD_ALL idDot3
1406#define arg_FWD_BLOCK Qnone
1408#define arg_FWD_BLOCK idFWD_BLOCK
1410#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS
1412#define RE_OPTION_ONCE (1<<16)
1413#define RE_OPTION_ENCODING_SHIFT 8
1414#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1415#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1416#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1417#define RE_OPTION_MASK 0xff
1418#define RE_OPTION_ARG_ENCODING_NONE 32
1420#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1421size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1423#define TOKEN2ID(tok) ( \
1424 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1425 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1426 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1427 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1428 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1429 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1430 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1432/****** Ripper *******/
1436#include "eventids1.h"
1437#include "eventids2.h"
1439extern const struct ripper_parser_ids ripper_parser_ids;
1441static VALUE ripper_dispatch0(struct parser_params*,ID);
1442static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1443static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1444static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1445static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1446static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1447static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1448void ripper_error(struct parser_params *p);
1450#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
1451#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
1452#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
1453#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
1454#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
1455#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
1456#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
1458#define yyparse ripper_yyparse
1460#define ID2VAL(id) STATIC_ID2SYM(id)
1461#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1462#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
1464#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
1465 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
1468new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
1470 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(tail);
1471 VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2, block = t->nd_val3;
1472 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, block);
1476new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
1478 return ripper_new_yylval2(p, kw_args, kw_rest_arg, block);
1482args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
1488new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
1490 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(aryptn);
1491 VALUE pre_args = t->nd_val1, rest_arg = t->nd_val2, post_args = t->nd_val3;
1493 if (!NIL_P(pre_arg)) {
1494 if (!NIL_P(pre_args)) {
1495 rb_ary_unshift(pre_args, pre_arg);
1498 pre_args = rb_ary_new_from_args(1, pre_arg);
1501 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
1505new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
1507 return ripper_new_yylval2(p, pre_args, rest_arg, post_args);
1511new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
1513 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(fndptn);
1514 VALUE pre_rest_arg = t->nd_val1, args = t->nd_val2, post_rest_arg = t->nd_val3;
1516 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
1520new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
1522 return ripper_new_yylval2(p, pre_rest_arg, args, post_rest_arg);
1525#define new_hash(p,h,l) rb_ary_new_from_args(0)
1528new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
1534new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
1536 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(hshptn);
1537 VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2;
1538 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
1542new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
1545 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
1550 return ripper_new_yylval2(p, kw_args, kw_rest_arg, Qnil);
1553#define new_defined(p,expr,loc) dispatch1(defined, (expr))
1555static VALUE heredoc_dedent(struct parser_params*,VALUE);
1558#define ID2VAL(id) (id)
1559#define TOKEN2VAL(t) ID2VAL(t)
1560#define KWD2EID(t, v) keyword_##t
1563new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1565 body = remove_begin(body);
1566 reduce_nodes(p, &body);
1567 NODE *n = NEW_SCOPE(args, body, loc);
1568 nd_set_line(n, loc->end_pos.lineno);
1569 set_line_body(body, loc->beg_pos.lineno);
1574rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1575 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1577 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1578 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1579 loc.beg_pos = arg_loc->beg_pos;
1580 return NEW_RESCUE(arg, rescue, 0, &loc);
1585static NODE *add_block_exit(struct parser_params *p, NODE *node);
1586static rb_node_exits_t *init_block_exit(struct parser_params *p);
1587static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1588static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1589static void clear_block_exit(struct parser_params *p, bool error);
1592next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1594 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1598restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1600 /* See: def_name action */
1601 struct lex_context ctxt = temp->save.ctxt;
1602 p->cur_arg = temp->save.cur_arg;
1603 p->ctxt.in_def = ctxt.in_def;
1604 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1605 p->ctxt.in_rescue = ctxt.in_rescue;
1606 p->max_numparam = temp->save.max_numparam;
1607 numparam_pop(p, temp->save.numparam_save);
1608 clear_block_exit(p, true);
1612endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1614 if (is_attrset_id(mid)) {
1615 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1617 token_info_drop(p, "def", loc->beg_pos);
1620#define debug_token_line(p, name, line) do { \
1622 const char *const pcur = p->lex.pcur; \
1623 const char *const ptok = p->lex.ptok; \
1624 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1625 line, p->ruby_sourceline, \
1626 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1630#define begin_definition(k, loc_beg, loc_end) \
1632 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1633 p->ctxt.in_def = 0; \
1635 else if (p->ctxt.in_def) { \
1636 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1637 yyerror1(&loc, k " definition in method body"); \
1645# define ifndef_ripper(x) (x)
1648# define Qnull Qundef
1649# define ifndef_ripper(x)
1652# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1653# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1654# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1655# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1656# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1657# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1658# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1659# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1660# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1661# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1662# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1663# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1664# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1665# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1666# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1667# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1668# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1669# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1670# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1671# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1673extern const ID id_warn, id_warning, id_gets, id_assoc;
1674# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1675# define WARN_S_L(s,l) STR_NEW(s,l)
1676# define WARN_S(s) STR_NEW2(s)
1677# define WARN_I(i) INT2NUM(i)
1678# define WARN_ID(i) rb_id2str(i)
1679# define WARN_IVAL(i) i
1680# define PRIsWARN "s"
1681# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1682# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1683# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1684# ifdef HAVE_VA_ARGS_MACRO
1685# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1687# define WARN_CALL rb_funcall
1689# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1690# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1691# ifdef HAVE_VA_ARGS_MACRO
1692# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1694# define WARNING_CALL rb_funcall
1696# define compile_error ripper_compile_error
1698# define WARN_S_L(s,l) s
1701# define WARN_ID(i) rb_id2name(i)
1702# define WARN_IVAL(i) NUM2INT(i)
1703# define PRIsWARN PRIsVALUE
1704# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1705# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1706# define WARN_CALL rb_compile_warn
1707# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1708# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1709# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1710# define WARNING_CALL rb_compile_warning
1711PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1712# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1718 NODE *nd_chain; /* Assume NODE_BREAK, NODE_NEXT, NODE_REDO have nd_chain here */
1722#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1725add_block_exit(struct parser_params *p, NODE *node)
1728 compile_error(p, "unexpected null node");
1731 switch (nd_type(node)) {
1732 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1734 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1737 if (!p->ctxt.in_defined) {
1738 rb_node_exits_t *exits = p->exits;
1740 RNODE_EXITS(exits->nd_end)->nd_chain = node;
1741 exits->nd_end = node;
1747static rb_node_exits_t *
1748init_block_exit(struct parser_params *p)
1750 rb_node_exits_t *old = p->exits;
1751 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1752 exits->nd_chain = 0;
1753 exits->nd_end = RNODE(exits);
1758static rb_node_exits_t *
1759allow_block_exit(struct parser_params *p)
1761 rb_node_exits_t *exits = p->exits;
1767restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1773clear_block_exit(struct parser_params *p, bool error)
1775 rb_node_exits_t *exits = p->exits;
1777 if (error && !compile_for_eval) {
1778 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1779 switch (nd_type(e)) {
1781 yyerror1(&e->nd_loc, "Invalid break");
1784 yyerror1(&e->nd_loc, "Invalid next");
1787 yyerror1(&e->nd_loc, "Invalid redo");
1790 yyerror1(&e->nd_loc, "unexpected node");
1791 goto end_checks; /* no nd_chain */
1796 exits->nd_end = RNODE(exits);
1797 exits->nd_chain = 0;
1800#define WARN_EOL(tok) \
1801 (looking_at_eol_p(p) ? \
1802 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1804static int looking_at_eol_p(struct parser_params *p);
1808get_nd_value(struct parser_params *p, NODE *node)
1810 switch (nd_type(node)) {
1812 return RNODE_GASGN(node)->nd_value;
1814 return RNODE_IASGN(node)->nd_value;
1816 return RNODE_LASGN(node)->nd_value;
1818 return RNODE_DASGN(node)->nd_value;
1820 return RNODE_MASGN(node)->nd_value;
1822 return RNODE_CVASGN(node)->nd_value;
1824 return RNODE_CDECL(node)->nd_value;
1826 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1832set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1834 switch (nd_type(node)) {
1836 RNODE_CDECL(node)->nd_value = rhs;
1839 RNODE_GASGN(node)->nd_value = rhs;
1842 RNODE_IASGN(node)->nd_value = rhs;
1845 RNODE_LASGN(node)->nd_value = rhs;
1848 RNODE_DASGN(node)->nd_value = rhs;
1851 RNODE_MASGN(node)->nd_value = rhs;
1854 RNODE_CVASGN(node)->nd_value = rhs;
1857 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1863get_nd_vid(struct parser_params *p, NODE *node)
1865 switch (nd_type(node)) {
1867 return RNODE_CDECL(node)->nd_vid;
1869 return RNODE_GASGN(node)->nd_vid;
1871 return RNODE_IASGN(node)->nd_vid;
1873 return RNODE_LASGN(node)->nd_vid;
1875 return RNODE_DASGN(node)->nd_vid;
1877 return RNODE_CVASGN(node)->nd_vid;
1879 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1885get_nd_args(struct parser_params *p, NODE *node)
1887 switch (nd_type(node)) {
1889 return RNODE_CALL(node)->nd_args;
1891 return RNODE_OPCALL(node)->nd_args;
1893 return RNODE_FCALL(node)->nd_args;
1895 return RNODE_QCALL(node)->nd_args;
1905 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1914%define parse.error verbose
1917 if ((NODE *)$$ == (NODE *)-1) {
1918 rb_parser_printf(p, "NODE_SPECIAL");
1921 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
1925} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg> <node_kw_arg> <node_block_pass>
1928 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1930 rb_parser_printf(p, "%"PRIsVALUE, RNODE_RIPPER($$)->nd_rval);
1932} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1935 rb_parser_printf(p, "%+"PRIsVALUE, RNODE_LIT($$)->nd_lit);
1937 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1939} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1942 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
1944 rb_parser_printf(p, "%"PRIsVALUE, $$);
1949 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
1951 rb_parser_printf(p, "%"PRIsVALUE, $$);
1955%lex-param {struct parser_params *p}
1956%parse-param {struct parser_params *p}
1959 RUBY_SET_YYLLOC_OF_NONE(@$);
1965 rb_node_fcall_t *node_fcall;
1966 rb_node_args_t *node_args;
1967 rb_node_args_aux_t *node_args_aux;
1968 rb_node_opt_arg_t *node_opt_arg;
1969 rb_node_kw_arg_t *node_kw_arg;
1970 rb_node_block_pass_t *node_block_pass;
1971 rb_node_masgn_t *node_masgn;
1972 rb_node_def_temp_t *node_def_temp;
1973 rb_node_exits_t *node_exits;
1977 const struct vtable *vars;
1978 struct rb_strterm_struct *strterm;
1979 struct lex_context ctxt;
1983 keyword_class "`class'"
1984 keyword_module "`module'"
1986 keyword_undef "`undef'"
1987 keyword_begin "`begin'"
1988 keyword_rescue "`rescue'"
1989 keyword_ensure "`ensure'"
1992 keyword_unless "`unless'"
1993 keyword_then "`then'"
1994 keyword_elsif "`elsif'"
1995 keyword_else "`else'"
1996 keyword_case "`case'"
1997 keyword_when "`when'"
1998 keyword_while "`while'"
1999 keyword_until "`until'"
2001 keyword_break "`break'"
2002 keyword_next "`next'"
2003 keyword_redo "`redo'"
2004 keyword_retry "`retry'"
2007 keyword_do_cond "`do' for condition"
2008 keyword_do_block "`do' for block"
2009 keyword_do_LAMBDA "`do' for lambda"
2010 keyword_return "`return'"
2011 keyword_yield "`yield'"
2012 keyword_super "`super'"
2013 keyword_self "`self'"
2015 keyword_true "`true'"
2016 keyword_false "`false'"
2020 modifier_if "`if' modifier"
2021 modifier_unless "`unless' modifier"
2022 modifier_while "`while' modifier"
2023 modifier_until "`until' modifier"
2024 modifier_rescue "`rescue' modifier"
2025 keyword_alias "`alias'"
2026 keyword_defined "`defined?'"
2027 keyword_BEGIN "`BEGIN'"
2029 keyword__LINE__ "`__LINE__'"
2030 keyword__FILE__ "`__FILE__'"
2031 keyword__ENCODING__ "`__ENCODING__'"
2033%token <id> tIDENTIFIER "local variable or method"
2034%token <id> tFID "method"
2035%token <id> tGVAR "global variable"
2036%token <id> tIVAR "instance variable"
2037%token <id> tCONSTANT "constant"
2038%token <id> tCVAR "class variable"
2039%token <id> tLABEL "label"
2040%token <node> tINTEGER "integer literal"
2041%token <node> tFLOAT "float literal"
2042%token <node> tRATIONAL "rational literal"
2043%token <node> tIMAGINARY "imaginary literal"
2044%token <node> tCHAR "char literal"
2045%token <node> tNTH_REF "numbered reference"
2046%token <node> tBACK_REF "back reference"
2047%token <node> tSTRING_CONTENT "literal content"
2048%token <num> tREGEXP_END
2049%token <num> tDUMNY_END "dummy end"
2051%type <node> singleton strings string string1 xstring regexp
2052%type <node> string_contents xstring_contents regexp_contents string_content
2053%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2054%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2055/*ripper*/ %type <node_def_temp> defn_head defs_head k_def
2056/*ripper*/ %type <node_exits> block_open k_while k_until k_for allow_exits
2057%type <node> top_compstmt top_stmts top_stmt begin_block endless_arg endless_command
2058%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
2059%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2060%type <node_fcall> fcall
2061%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2062%type <node> args arg_splat call_args opt_call_args
2063%type <node> paren_args opt_paren_args
2064%type <node_args> args_tail opt_args_tail block_args_tail opt_block_args_tail
2065%type <node> command_args aref_args
2066%type <node_block_pass> opt_block_arg block_arg
2067%type <node> var_ref var_lhs
2068%type <node> command_rhs arg_rhs
2069%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2070%type <node_opt_arg> f_block_optarg f_block_opt
2071%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2072%type <node_args_aux> f_arg f_arg_item
2073%type <node_opt_arg> f_optarg
2074%type <node> f_marg f_marg_list f_rest_marg
2075%type <node_masgn> f_margs
2076%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2077%type <node_args> block_param opt_block_param block_param_def
2078%type <node_opt_arg> f_opt
2079%type <node_kw_arg> f_kwarg f_kw f_block_kwarg f_block_kw
2080%type <node> bv_decls opt_bv_decl bvar
2081%type <node> lambda lambda_body brace_body do_body
2082%type <node_args> f_larglist
2083%type <node> brace_block cmd_brace_block do_block lhs none fitem
2084%type <node> mlhs_head mlhs_item mlhs_node mlhs_post
2085%type <node_masgn> mlhs mlhs_basic mlhs_inner
2086%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2087%type <node> p_expr p_as p_alt p_expr_basic p_find
2088%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2089%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2090%type <node> p_kwargs p_kwarg p_kw
2091%type <id> keyword_variable user_variable sym operation operation2 operation3
2092%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2093%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2094%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2095%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2096%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2097%type <ctxt> p_in_kwarg
2098%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2099/* ripper */ %type <num> max_numparam
2100/* ripper */ %type <node> numparam
2101%token END_OF_INPUT 0 "end-of-input"
2104/* escaped chars, should be ignored otherwise */
2105%token <id> '\\' "backslash"
2106%token tSP "escaped space"
2107%token <id> '\t' "escaped horizontal tab"
2108%token <id> '\f' "escaped form feed"
2109%token <id> '\r' "escaped carriage return"
2110%token <id> '\13' "escaped vertical tab"
2111%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2112%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2113%token tPOW RUBY_TOKEN(POW) "**"
2114%token tCMP RUBY_TOKEN(CMP) "<=>"
2115%token tEQ RUBY_TOKEN(EQ) "=="
2116%token tEQQ RUBY_TOKEN(EQQ) "==="
2117%token tNEQ RUBY_TOKEN(NEQ) "!="
2118%token tGEQ RUBY_TOKEN(GEQ) ">="
2119%token tLEQ RUBY_TOKEN(LEQ) "<="
2120%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2121%token tOROP RUBY_TOKEN(OROP) "||"
2122%token tMATCH RUBY_TOKEN(MATCH) "=~"
2123%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2124%token tDOT2 RUBY_TOKEN(DOT2) ".."
2125%token tDOT3 RUBY_TOKEN(DOT3) "..."
2126%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2127%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2128%token tAREF RUBY_TOKEN(AREF) "[]"
2129%token tASET RUBY_TOKEN(ASET) "[]="
2130%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2131%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2132%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2133%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2134%token tCOLON3 ":: at EXPR_BEG"
2135%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2138%token tLPAREN_ARG "( arg"
2142%token tLBRACE_ARG "{ arg"
2144%token tDSTAR "**arg"
2147%token tSYMBEG "symbol literal"
2148%token tSTRING_BEG "string literal"
2149%token tXSTRING_BEG "backtick literal"
2150%token tREGEXP_BEG "regexp literal"
2151%token tWORDS_BEG "word list"
2152%token tQWORDS_BEG "verbatim word list"
2153%token tSYMBOLS_BEG "symbol list"
2154%token tQSYMBOLS_BEG "verbatim symbol list"
2155%token tSTRING_END "terminator"
2156%token tSTRING_DEND "'}'"
2157%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
2159%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2160%token tHEREDOC_BEG tHEREDOC_END k__END__
2167%nonassoc tLBRACE_ARG
2169%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2170%left keyword_or keyword_and
2172%nonassoc keyword_defined
2174%left modifier_rescue
2176%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2179%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2180%left '>' tGEQ '<' tLEQ
2186%right tUMINUS_NUM tUMINUS
2188%right '!' '~' tUPLUS
2194 SET_LEX_STATE(EXPR_BEG);
2195 local_push(p, ifndef_ripper(1)+0);
2196 /* jumps are possible in the top-level loop. */
2197 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
2202 if ($2 && !compile_for_eval) {
2204 /* last expression should not be void */
2205 if (nd_type_p(node, NODE_BLOCK)) {
2206 while (RNODE_BLOCK(node)->nd_next) {
2207 node = RNODE_BLOCK(node)->nd_next;
2209 node = RNODE_BLOCK(node)->nd_head;
2211 node = remove_begin(node);
2214 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
2216 /*% ripper[final]: program!($2) %*/
2221top_compstmt : top_stmts opt_terms
2223 $$ = void_stmts(p, $1);
2230 $$ = NEW_BEGIN(0, &@$);
2232 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
2237 $$ = newline_node($1);
2239 /*% ripper: stmts_add!(stmts_new!, $1) %*/
2241 | top_stmts terms top_stmt
2244 $$ = block_append(p, $1, newline_node($3));
2246 /*% ripper: stmts_add!($1, $3) %*/
2252 clear_block_exit(p, true);
2255 | keyword_BEGIN begin_block
2261block_open : '{' {$$ = init_block_exit(p);};
2263begin_block : block_open top_compstmt '}'
2265 restore_block_exit(p, $block_open);
2267 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
2268 NEW_BEGIN($2, &@$));
2269 $$ = NEW_BEGIN(0, &@$);
2271 /*% ripper: BEGIN!($2) %*/
2275bodystmt : compstmt[body]
2280 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
2281 next_rescue_context(&p->ctxt, &$ctxt, after_else);
2285 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
2290 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
2292 /*% ripper: bodystmt!($body, $opt_rescue, $elsebody, $opt_ensure) %*/
2298 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
2303 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
2305 /*% ripper: bodystmt!($body, $opt_rescue, Qnil, $opt_ensure) %*/
2309compstmt : stmts opt_terms
2311 $$ = void_stmts(p, $1);
2318 $$ = NEW_BEGIN(0, &@$);
2320 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
2325 $$ = newline_node($1);
2327 /*% ripper: stmts_add!(stmts_new!, $1) %*/
2329 | stmts terms stmt_or_begin
2332 $$ = block_append(p, $1, newline_node($3));
2334 /*% ripper: stmts_add!($1, $3) %*/
2344 yyerror1(&@1, "BEGIN is permitted only at toplevel");
2352allow_exits : {$$ = allow_block_exit(p);};
2354k_END : keyword_END lex_ctxt
2357 p->ctxt.in_rescue = before_rescue;
2360stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2363 $$ = NEW_ALIAS($2, $4, &@$);
2365 /*% ripper: alias!($2, $4) %*/
2367 | keyword_alias tGVAR tGVAR
2370 $$ = NEW_VALIAS($2, $3, &@$);
2372 /*% ripper: var_alias!($2, $3) %*/
2374 | keyword_alias tGVAR tBACK_REF
2379 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
2380 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
2382 /*% ripper: var_alias!($2, $3) %*/
2384 | keyword_alias tGVAR tNTH_REF
2386 static const char mesg[] = "can't make alias for the number variables";
2388 yyerror1(&@3, mesg);
2389 $$ = NEW_ERROR(&@$);
2391 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
2393 | keyword_undef undef_list
2398 /*% ripper: undef!($2) %*/
2400 | stmt modifier_if expr_value
2403 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
2406 /*% ripper: if_mod!($3, $1) %*/
2408 | stmt modifier_unless expr_value
2411 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
2414 /*% ripper: unless_mod!($3, $1) %*/
2416 | stmt modifier_while expr_value
2418 clear_block_exit(p, false);
2420 if ($1 && nd_type_p($1, NODE_BEGIN)) {
2421 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$);
2424 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
2427 /*% ripper: while_mod!($3, $1) %*/
2429 | stmt modifier_until expr_value
2431 clear_block_exit(p, false);
2433 if ($1 && nd_type_p($1, NODE_BEGIN)) {
2434 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$);
2437 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
2440 /*% ripper: until_mod!($3, $1) %*/
2442 | stmt modifier_rescue after_rescue stmt
2444 p->ctxt.in_rescue = $3.in_rescue;
2447 YYLTYPE loc = code_loc_gen(&@2, &@4);
2448 resq = NEW_RESBODY(0, remove_begin($4), 0, &loc);
2449 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
2451 /*% ripper: rescue_mod!($1, $4) %*/
2453 | k_END allow_exits '{' compstmt '}'
2455 if (p->ctxt.in_def) {
2456 rb_warn0("END in method; use at_exit");
2458 restore_block_exit(p, $allow_exits);
2462 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
2463 $$ = NEW_POSTEXE(scope, &@$);
2466 /*% ripper: END!($compstmt) %*/
2469 | mlhs '=' lex_ctxt command_call
2473 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
2475 /*% ripper: massign!($1, $4) %*/
2477 | lhs '=' lex_ctxt mrhs
2480 $$ = node_assign(p, $1, $4, $3, &@$);
2482 /*% ripper: assign!($1, $4) %*/
2484 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
2485 after_rescue stmt[resbody]
2487 p->ctxt.in_rescue = $3.in_rescue;
2489 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
2490 $resbody = NEW_RESBODY(0, remove_begin($resbody), 0, &loc);
2491 loc.beg_pos = @mrhs_arg.beg_pos;
2492 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
2493 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
2495 /*% ripper: massign!($1, rescue_mod!($4, $7)) %*/
2497 | mlhs '=' lex_ctxt mrhs_arg
2500 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
2502 /*% ripper: massign!($1, $4) %*/
2509 $$ = NEW_ERROR(&@$);
2514command_asgn : lhs '=' lex_ctxt command_rhs
2517 $$ = node_assign(p, $1, $4, $3, &@$);
2519 /*% ripper: assign!($1, $4) %*/
2521 | var_lhs tOP_ASGN lex_ctxt command_rhs
2524 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2526 /*% ripper: opassign!($1, $2, $4) %*/
2528 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
2531 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2533 /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/
2536 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
2539 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2541 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2543 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
2546 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2548 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2550 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
2553 YYLTYPE loc = code_loc_gen(&@1, &@3);
2554 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2556 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2558 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
2561 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2563 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2565 | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
2567 endless_method_name(p, get_id($head->nd_mid), &@head);
2568 restore_defun(p, $head);
2570 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2571 ($$ = $head->nd_def)->nd_loc = @$;
2572 RNODE_DEFN($$)->nd_defn = $bodystmt;
2574 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
2575 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
2578 | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
2580 endless_method_name(p, get_id($head->nd_mid), &@head);
2581 restore_defun(p, $head);
2583 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2584 ($$ = $head->nd_def)->nd_loc = @$;
2585 RNODE_DEFS($$)->nd_defn = $bodystmt;
2587 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
2588 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
2591 | backref tOP_ASGN lex_ctxt command_rhs
2594 rb_backref_error(p, $1);
2595 $$ = NEW_ERROR(&@$);
2597 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
2601endless_command : command
2602 | endless_command modifier_rescue after_rescue arg
2604 p->ctxt.in_rescue = $3.in_rescue;
2606 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
2608 /*% ripper: rescue_mod!($1, $4) %*/
2610 | keyword_not opt_nl endless_command
2612 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2616command_rhs : command_call %prec tOP_ASGN
2621 | command_call modifier_rescue after_rescue stmt
2623 p->ctxt.in_rescue = $3.in_rescue;
2625 YYLTYPE loc = code_loc_gen(&@2, &@4);
2627 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($4), 0, &loc), 0, &@$);
2629 /*% ripper: rescue_mod!($1, $4) %*/
2635 | expr keyword_and expr
2637 $$ = logop(p, idAND, $1, $3, &@2, &@$);
2639 | expr keyword_or expr
2641 $$ = logop(p, idOR, $1, $3, &@2, &@$);
2643 | keyword_not opt_nl expr
2645 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2649 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2655 p_in_kwarg[ctxt] p_pvtbl p_pktbl
2656 p_top_expr_body[body]
2658 pop_pktbl(p, $p_pktbl);
2659 pop_pvtbl(p, $p_pvtbl);
2660 p->ctxt.in_kwarg = $ctxt.in_kwarg;
2662 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$);
2664 /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/
2670 p_in_kwarg[ctxt] p_pvtbl p_pktbl
2671 p_top_expr_body[body]
2673 pop_pktbl(p, $p_pktbl);
2674 pop_pvtbl(p, $p_pvtbl);
2675 p->ctxt.in_kwarg = $ctxt.in_kwarg;
2677 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$);
2679 /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/
2681 | arg %prec tLBRACE_ARG
2686 ID fname = get_id($1);
2687 numparam_name(p, fname);
2691 p->ctxt.in_rescue = before_rescue;
2696defn_head : k_def def_name
2698 $$ = def_head_save(p, $k_def);
2699 $$->nd_mid = $def_name;
2701 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
2703 add_mark_object(p, $def_name);
2708defs_head : k_def singleton dot_or_colon
2710 SET_LEX_STATE(EXPR_FNAME);
2711 p->ctxt.in_argdef = 1;
2715 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2716 $$ = def_head_save(p, $k_def);
2717 $$->nd_mid = $def_name;
2719 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
2721 add_mark_object(p, $def_name);
2722 $$->nd_recv = add_mark_object(p, $singleton);
2723 $$->dot_or_colon = add_mark_object(p, $dot_or_colon);
2736 $$ = NEW_ERROR(&@$);
2741expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
2747command_call : command
2751block_command : block_call
2752 | block_call call_op2 operation2 command_args
2755 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
2757 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
2761cmd_brace_block : tLBRACE_ARG brace_body '}'
2765 set_embraced_location($$, &@1, &@3);
2773 $$ = NEW_FCALL($1, 0, &@$);
2779command : fcall command_args %prec tLOWEST
2783 nd_set_last_loc($1, @2.end_pos);
2786 /*% ripper: command!($1, $2) %*/
2788 | fcall command_args cmd_brace_block
2791 block_dup_check(p, $2, $3);
2793 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
2794 fixpos($$, RNODE($1));
2795 nd_set_last_loc($1, @2.end_pos);
2797 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
2799 | primary_value call_op operation2 command_args %prec tLOWEST
2802 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
2804 /*% ripper: command_call!($1, $2, $3, $4) %*/
2806 | primary_value call_op operation2 command_args cmd_brace_block
2809 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
2811 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2813 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
2816 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
2818 /*% ripper: command_call!($1, $2, $3, $4) %*/
2820 | primary_value tCOLON2 operation2 command_args cmd_brace_block
2823 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
2825 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2827 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
2830 set_embraced_location($5, &@4, &@6);
2831 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, $5, &@3, &@$);
2833 /*% ripper: method_add_block!(command_call!($1, $2, $3, Qnull), $5) %*/
2835 | keyword_super command_args
2838 $$ = NEW_SUPER($2, &@$);
2841 /*% ripper: super!($2) %*/
2843 | k_yield command_args
2846 $$ = new_yield(p, $2, &@$);
2849 /*% ripper: yield!($2) %*/
2851 | k_return call_args
2854 $$ = NEW_RETURN(ret_args(p, $2), &@$);
2856 /*% ripper: return!($2) %*/
2858 | keyword_break call_args
2862 args = ret_args(p, $2);
2864 $<node>$ = add_block_exit(p, NEW_BREAK(args, &@$));
2865 /*% ripper: break!($2) %*/
2867 | keyword_next call_args
2871 args = ret_args(p, $2);
2873 $<node>$ = add_block_exit(p, NEW_NEXT(args, &@$));
2874 /*% ripper: next!($2) %*/
2879 | tLPAREN mlhs_inner rparen
2884 /*% ripper: mlhs_paren!($2) %*/
2888mlhs_inner : mlhs_basic
2889 | tLPAREN mlhs_inner rparen
2892 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
2894 /*% ripper: mlhs_paren!($2) %*/
2898mlhs_basic : mlhs_head
2901 $$ = NEW_MASGN($1, 0, &@$);
2905 | mlhs_head mlhs_item
2908 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
2910 /*% ripper: mlhs_add!($1, $2) %*/
2912 | mlhs_head tSTAR mlhs_node
2915 $$ = NEW_MASGN($1, $3, &@$);
2917 /*% ripper: mlhs_add_star!($1, $3) %*/
2919 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2922 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2924 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2929 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2931 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2933 | mlhs_head tSTAR ',' mlhs_post
2936 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2938 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2943 $$ = NEW_MASGN(0, $2, &@$);
2945 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2947 | tSTAR mlhs_node ',' mlhs_post
2950 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2952 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2957 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2959 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2961 | tSTAR ',' mlhs_post
2964 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2966 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2970mlhs_item : mlhs_node
2971 | tLPAREN mlhs_inner rparen
2976 /*% ripper: mlhs_paren!($2) %*/
2980mlhs_head : mlhs_item ','
2983 $$ = NEW_LIST($1, &@1);
2985 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2987 | mlhs_head mlhs_item ','
2990 $$ = list_append(p, $1, $2);
2992 /*% ripper: mlhs_add!($1, $2) %*/
2996mlhs_post : mlhs_item
2999 $$ = NEW_LIST($1, &@$);
3001 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3003 | mlhs_post ',' mlhs_item
3006 $$ = list_append(p, $1, $3);
3008 /*% ripper: mlhs_add!($1, $3) %*/
3012mlhs_node : user_variable
3015 $$ = assignable(p, $1, 0, &@$);
3017 /*% ripper: assignable(p, var_field(p, $1)) %*/
3022 $$ = assignable(p, $1, 0, &@$);
3024 /*% ripper: assignable(p, var_field(p, $1)) %*/
3026 | primary_value '[' opt_call_args rbracket
3029 $$ = aryset(p, $1, $3, &@$);
3031 /*% ripper: aref_field!($1, $3) %*/
3033 | primary_value call_op tIDENTIFIER
3035 anddot_multiple_assignment_check(p, &@2, $2);
3037 $$ = attrset(p, $1, $2, $3, &@$);
3039 /*% ripper: field!($1, $2, $3) %*/
3041 | primary_value tCOLON2 tIDENTIFIER
3044 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3046 /*% ripper: const_path_field!($1, $3) %*/
3048 | primary_value call_op tCONSTANT
3050 anddot_multiple_assignment_check(p, &@2, $2);
3052 $$ = attrset(p, $1, $2, $3, &@$);
3054 /*% ripper: field!($1, $2, $3) %*/
3056 | primary_value tCOLON2 tCONSTANT
3059 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3061 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
3066 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3068 /*% ripper: const_decl(p, top_const_field!($2)) %*/
3073 rb_backref_error(p, $1);
3074 $$ = NEW_ERROR(&@$);
3076 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
3083 $$ = assignable(p, $1, 0, &@$);
3085 /*% ripper: assignable(p, var_field(p, $1)) %*/
3090 $$ = assignable(p, $1, 0, &@$);
3092 /*% ripper: assignable(p, var_field(p, $1)) %*/
3094 | primary_value '[' opt_call_args rbracket
3097 $$ = aryset(p, $1, $3, &@$);
3099 /*% ripper: aref_field!($1, $3) %*/
3101 | primary_value call_op tIDENTIFIER
3104 $$ = attrset(p, $1, $2, $3, &@$);
3106 /*% ripper: field!($1, $2, $3) %*/
3108 | primary_value tCOLON2 tIDENTIFIER
3111 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3113 /*% ripper: field!($1, $2, $3) %*/
3115 | primary_value call_op tCONSTANT
3118 $$ = attrset(p, $1, $2, $3, &@$);
3120 /*% ripper: field!($1, $2, $3) %*/
3122 | primary_value tCOLON2 tCONSTANT
3125 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3127 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
3132 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3134 /*% ripper: const_decl(p, top_const_field!($2)) %*/
3139 rb_backref_error(p, $1);
3140 $$ = NEW_ERROR(&@$);
3142 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
3148 static const char mesg[] = "class/module name must be CONSTANT";
3150 yyerror1(&@1, mesg);
3152 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
3157cpath : tCOLON3 cname
3160 $$ = NEW_COLON3($2, &@$);
3162 /*% ripper: top_const_ref!($2) %*/
3167 $$ = NEW_COLON2(0, $1, &@$);
3169 /*% ripper: const_ref!($1) %*/
3171 | primary_value tCOLON2 cname
3174 $$ = NEW_COLON2($1, $3, &@$);
3176 /*% ripper: const_path_ref!($1, $3) %*/
3185 SET_LEX_STATE(EXPR_ENDFN);
3194 $$ = NEW_LIT(ID2SYM($1), &@$);
3196 /*% ripper: symbol_literal!($1) %*/
3204 $$ = NEW_UNDEF($1, &@$);
3206 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
3208 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3211 NODE *undef = NEW_UNDEF($4, &@4);
3212 $$ = block_append(p, $1, undef);
3214 /*% ripper: rb_ary_push($1, get_value($4)) %*/
3218op : '|' { ifndef_ripper($$ = '|'); }
3219 | '^' { ifndef_ripper($$ = '^'); }
3220 | '&' { ifndef_ripper($$ = '&'); }
3221 | tCMP { ifndef_ripper($$ = tCMP); }
3222 | tEQ { ifndef_ripper($$ = tEQ); }
3223 | tEQQ { ifndef_ripper($$ = tEQQ); }
3224 | tMATCH { ifndef_ripper($$ = tMATCH); }
3225 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
3226 | '>' { ifndef_ripper($$ = '>'); }
3227 | tGEQ { ifndef_ripper($$ = tGEQ); }
3228 | '<' { ifndef_ripper($$ = '<'); }
3229 | tLEQ { ifndef_ripper($$ = tLEQ); }
3230 | tNEQ { ifndef_ripper($$ = tNEQ); }
3231 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
3232 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
3233 | '+' { ifndef_ripper($$ = '+'); }
3234 | '-' { ifndef_ripper($$ = '-'); }
3235 | '*' { ifndef_ripper($$ = '*'); }
3236 | tSTAR { ifndef_ripper($$ = '*'); }
3237 | '/' { ifndef_ripper($$ = '/'); }
3238 | '%' { ifndef_ripper($$ = '%'); }
3239 | tPOW { ifndef_ripper($$ = tPOW); }
3240 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
3241 | '!' { ifndef_ripper($$ = '!'); }
3242 | '~' { ifndef_ripper($$ = '~'); }
3243 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
3244 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
3245 | tAREF { ifndef_ripper($$ = tAREF); }
3246 | tASET { ifndef_ripper($$ = tASET); }
3247 | '`' { ifndef_ripper($$ = '`'); }
3250reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3251 | keyword_BEGIN | keyword_END
3252 | keyword_alias | keyword_and | keyword_begin
3253 | keyword_break | keyword_case | keyword_class | keyword_def
3254 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3255 | keyword_end | keyword_ensure | keyword_false
3256 | keyword_for | keyword_in | keyword_module | keyword_next
3257 | keyword_nil | keyword_not | keyword_or | keyword_redo
3258 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3259 | keyword_super | keyword_then | keyword_true | keyword_undef
3260 | keyword_when | keyword_yield | keyword_if | keyword_unless
3261 | keyword_while | keyword_until
3264arg : lhs '=' lex_ctxt arg_rhs
3267 $$ = node_assign(p, $1, $4, $3, &@$);
3269 /*% ripper: assign!($1, $4) %*/
3271 | var_lhs tOP_ASGN lex_ctxt arg_rhs
3274 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3276 /*% ripper: opassign!($1, $2, $4) %*/
3278 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
3281 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
3283 /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/
3285 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3288 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
3290 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3292 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3295 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
3297 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3299 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3302 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
3304 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3306 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3309 YYLTYPE loc = code_loc_gen(&@1, &@3);
3310 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3312 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
3314 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3317 YYLTYPE loc = code_loc_gen(&@1, &@2);
3318 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3320 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
3322 | backref tOP_ASGN lex_ctxt arg_rhs
3325 rb_backref_error(p, $1);
3326 $$ = NEW_ERROR(&@$);
3328 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
3335 $$ = NEW_DOT2($1, $3, &@$);
3337 /*% ripper: dot2!($1, $3) %*/
3344 $$ = NEW_DOT3($1, $3, &@$);
3346 /*% ripper: dot3!($1, $3) %*/
3352 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
3354 /*% ripper: dot2!($1, Qnil) %*/
3360 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
3362 /*% ripper: dot3!($1, Qnil) %*/
3368 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
3370 /*% ripper: dot2!(Qnil, $2) %*/
3376 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
3378 /*% ripper: dot3!(Qnil, $2) %*/
3382 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3386 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3390 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3394 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3398 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3402 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3404 | tUMINUS_NUM simple_numeric tPOW arg
3406 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3410 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3414 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3418 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3422 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3426 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3430 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3432 | rel_expr %prec tCMP
3435 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3439 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3443 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3447 $$ = match_op(p, $1, $3, &@2, &@$);
3451 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
3455 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3459 $$ = call_uni_op(p, $2, '~', &@1, &@$);
3463 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
3467 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
3471 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
3475 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
3477 | keyword_defined opt_nl begin_defined arg
3479 p->ctxt.in_defined = $3.in_defined;
3480 $$ = new_defined(p, $4, &@$);
3482 | arg '?' arg opt_nl ':' arg
3486 $$ = new_if(p, $1, $3, $6, &@$);
3489 /*% ripper: ifop!($1, $3, $6) %*/
3491 | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
3493 endless_method_name(p, get_id($head->nd_mid), &@head);
3494 restore_defun(p, $head);
3496 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3497 ($$ = $head->nd_def)->nd_loc = @$;
3498 RNODE_DEFN($$)->nd_defn = $bodystmt;
3500 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
3501 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
3504 | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
3506 endless_method_name(p, get_id($head->nd_mid), &@head);
3507 restore_defun(p, $head);
3509 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3510 ($$ = $head->nd_def)->nd_loc = @$;
3511 RNODE_DEFS($$)->nd_defn = $bodystmt;
3513 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
3514 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
3523endless_arg : arg %prec modifier_rescue
3524 | endless_arg modifier_rescue after_rescue arg
3526 p->ctxt.in_rescue = $3.in_rescue;
3528 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3530 /*% ripper: rescue_mod!($1, $4) %*/
3532 | keyword_not opt_nl endless_arg
3534 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3538relop : '>' {$$ = '>';}
3544rel_expr : arg relop arg %prec '>'
3546 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
3548 | rel_expr relop arg %prec '>'
3550 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
3551 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
3561begin_defined : lex_ctxt
3563 p->ctxt.in_defined = 1;
3568after_rescue : lex_ctxt
3570 p->ctxt.in_rescue = after_rescue;
3587 | args ',' assocs trailer
3590 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3592 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3597 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
3599 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3603arg_rhs : arg %prec tOP_ASGN
3608 | arg modifier_rescue after_rescue arg
3610 p->ctxt.in_rescue = $3.in_rescue;
3613 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3615 /*% ripper: rescue_mod!($1, $4) %*/
3619paren_args : '(' opt_call_args rparen
3624 /*% ripper: arg_paren!($2) %*/
3626 | '(' args ',' args_forward rparen
3628 if (!check_forwarding_args(p)) {
3633 $$ = new_args_forward_call(p, $2, &@4, &@$);
3635 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
3638 | '(' args_forward rparen
3640 if (!check_forwarding_args(p)) {
3645 $$ = new_args_forward_call(p, 0, &@2, &@$);
3647 /*% ripper: arg_paren!($2) %*/
3652opt_paren_args : none
3662 | args ',' assocs ','
3665 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3667 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3672 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3674 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3682 $$ = NEW_LIST($1, &@$);
3684 /*% ripper: args_add!(args_new!, $1) %*/
3686 | args opt_block_arg
3689 $$ = arg_blk_pass($1, $2);
3691 /*% ripper: args_add_block!($1, $2) %*/
3693 | assocs opt_block_arg
3696 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3697 $$ = arg_blk_pass($$, $2);
3699 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
3701 | args ',' assocs opt_block_arg
3704 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3705 $$ = arg_blk_pass($$, $4);
3707 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
3710 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
3714 /* If call_args starts with a open paren '(' or '[',
3715 * look-ahead reading of the letters calls CMDARG_PUSH(0),
3716 * but the push must be done after CMDARG_PUSH(1).
3717 * So this code makes them consistent by first cancelling
3718 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
3719 * and finally redoing CMDARG_PUSH(0).
3723 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
3726 if (lookahead) CMDARG_POP();
3728 if (lookahead) CMDARG_PUSH(0);
3732 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
3733 * but the push must be done after CMDARG_POP() in the parser.
3734 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
3735 * CMDARG_POP() to pop 1 pushed by command_args,
3736 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
3743 if (lookahead) CMDARG_POP();
3745 if (lookahead) CMDARG_PUSH(0);
3750block_arg : tAMPER arg_value
3753 $$ = NEW_BLOCK_PASS($2, &@$);
3759 forwarding_arg_check(p, idFWD_BLOCK, 0, "block");
3761 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
3763 /*% ripper: Qnil %*/
3767opt_block_arg : ',' block_arg
3781 $$ = NEW_LIST($1, &@$);
3783 /*% ripper: args_add!(args_new!, $1) %*/
3788 $$ = NEW_SPLAT($arg_splat, &@$);
3790 /*% ripper: args_add_star!(args_new!, $arg_splat) %*/
3792 | args ',' arg_value
3795 $$ = last_arg_append(p, $1, $3, &@$);
3797 /*% ripper: args_add!($1, $3) %*/
3799 | args ',' arg_splat
3802 $$ = rest_arg_append(p, $1, $3, &@$);
3804 /*% ripper: args_add_star!($1, $3) %*/
3809arg_splat : tSTAR arg_value
3815 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
3817 $$ = NEW_LVAR(idFWD_REST, &@1);
3819 /*% ripper: Qnil %*/
3829mrhs : args ',' arg_value
3832 $$ = last_arg_append(p, $1, $3, &@$);
3834 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
3836 | args ',' tSTAR arg_value
3839 $$ = rest_arg_append(p, $1, $4, &@$);
3841 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
3846 $$ = NEW_SPLAT($2, &@$);
3848 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
3865 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
3867 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
3878 set_line_body($3, @1.end_pos.lineno);
3879 $$ = NEW_BEGIN($3, &@$);
3880 nd_set_line($$, @1.end_pos.lineno);
3882 /*% ripper: begin!($3) %*/
3884 | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')'
3887 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3890 /*% ripper: paren!($2) %*/
3892 | tLPAREN compstmt ')'
3895 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3896 $$ = NEW_BLOCK($2, &@$);
3898 /*% ripper: paren!($2) %*/
3900 | primary_value tCOLON2 tCONSTANT
3903 $$ = NEW_COLON2($1, $3, &@$);
3905 /*% ripper: const_path_ref!($1, $3) %*/
3910 $$ = NEW_COLON3($2, &@$);
3912 /*% ripper: top_const_ref!($2) %*/
3914 | tLBRACK aref_args ']'
3917 $$ = make_list($2, &@$);
3919 /*% ripper: array!($2) %*/
3921 | tLBRACE assoc_list '}'
3924 $$ = new_hash(p, $2, &@$);
3925 RNODE_HASH($$)->nd_brace = TRUE;
3927 /*% ripper: hash!($2) %*/
3932 $$ = NEW_RETURN(0, &@$);
3934 /*% ripper: return0! %*/
3936 | k_yield '(' call_args rparen
3939 $$ = new_yield(p, $3, &@$);
3941 /*% ripper: yield!(paren!($3)) %*/
3943 | k_yield '(' rparen
3946 $$ = NEW_YIELD(0, &@$);
3948 /*% ripper: yield!(paren!(args_new!)) %*/
3953 $$ = NEW_YIELD(0, &@$);
3955 /*% ripper: yield0! %*/
3957 | keyword_defined opt_nl '(' begin_defined expr rparen
3959 p->ctxt.in_defined = $4.in_defined;
3960 $$ = new_defined(p, $5, &@$);
3962 | keyword_not '(' expr rparen
3964 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3966 | keyword_not '(' rparen
3968 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3973 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
3975 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3978 | method_call brace_block
3981 block_dup_check(p, get_nd_args(p, $1), $2);
3982 $$ = method_add_block(p, $1, $2, &@$);
3984 /*% ripper: method_add_block!($1, $2) %*/
3987 | k_if expr_value then
3993 $$ = new_if(p, $2, $4, $5, &@$);
3996 /*% ripper: if!($2, $4, $5) %*/
3998 | k_unless expr_value then
4004 $$ = new_unless(p, $2, $4, $5, &@$);
4007 /*% ripper: unless!($2, $4, $5) %*/
4009 | k_while expr_value_do
4013 restore_block_exit(p, $1);
4015 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
4018 /*% ripper: while!($2, $3) %*/
4020 | k_until expr_value_do
4024 restore_block_exit(p, $1);
4026 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
4029 /*% ripper: until!($2, $3) %*/
4031 | k_case expr_value opt_terms
4033 $<val>$ = p->case_labels;
4034 p->case_labels = Qnil;
4039 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
4040 p->case_labels = $<val>4;
4042 $$ = NEW_CASE($2, $5, &@$);
4045 /*% ripper: case!($2, $5) %*/
4049 $<val>$ = p->case_labels;
4055 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
4056 p->case_labels = $<val>3;
4058 $$ = NEW_CASE2($4, &@$);
4060 /*% ripper: case!(Qnil, $4) %*/
4062 | k_case expr_value opt_terms
4067 $$ = NEW_CASE3($2, $4, &@$);
4069 /*% ripper: case!($2, $4) %*/
4071 | k_for for_var keyword_in expr_value_do
4075 restore_block_exit(p, $1);
4080 * e.each{|*x| a, b, c = x}
4084 * e.each{|x| a, = x}
4086 ID id = internal_id(p);
4087 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4088 rb_node_args_t *args;
4089 NODE *scope, *internal_var = NEW_DVAR(id, &@2);
4090 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4091 tbl->ids[0] = id; /* internal id */
4093 switch (nd_type($2)) {
4095 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4096 set_nd_value(p, $2, internal_var);
4101 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4102 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
4104 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4105 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
4107 /* {|*internal_id| <m> = internal_id; ... } */
4108 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
4109 scope = NEW_SCOPE2(tbl, args, $5, &@$);
4110 $$ = NEW_FOR($4, scope, &@$);
4113 /*% ripper: for!($2, $4, $5) %*/
4115 | k_class cpath superclass
4117 begin_definition("class", &@k_class, &@cpath);
4123 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4124 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4125 set_line_body($bodystmt, @superclass.end_pos.lineno);
4126 nd_set_line($$, @superclass.end_pos.lineno);
4128 /*% ripper: class!($cpath, $superclass, $bodystmt) %*/
4130 p->ctxt.in_class = $k_class.in_class;
4131 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4133 | k_class tLSHFT expr_value
4135 begin_definition("", &@k_class, &@tLSHFT);
4142 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4143 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4144 set_line_body($bodystmt, nd_line($expr_value));
4145 fixpos($$, $expr_value);
4147 /*% ripper: sclass!($expr_value, $bodystmt) %*/
4149 p->ctxt.in_def = $k_class.in_def;
4150 p->ctxt.in_class = $k_class.in_class;
4151 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4155 begin_definition("module", &@k_module, &@cpath);
4161 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4162 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4163 set_line_body($bodystmt, @cpath.end_pos.lineno);
4164 nd_set_line($$, @cpath.end_pos.lineno);
4166 /*% ripper: module!($cpath, $bodystmt) %*/
4168 p->ctxt.in_class = $k_module.in_class;
4169 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4175 push_end_expect_token_locations(p, &@head.beg_pos);
4181 restore_defun(p, $head);
4183 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4184 ($$ = $head->nd_def)->nd_loc = @$;
4185 RNODE_DEFN($$)->nd_defn = $bodystmt;
4187 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
4194 push_end_expect_token_locations(p, &@head.beg_pos);
4200 restore_defun(p, $head);
4202 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4203 ($$ = $head->nd_def)->nd_loc = @$;
4204 RNODE_DEFS($$)->nd_defn = $bodystmt;
4206 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
4211 $<node>$ = add_block_exit(p, NEW_BREAK(0, &@$));
4212 /*% ripper: break!(args_new!) %*/
4216 $<node>$ = add_block_exit(p, NEW_NEXT(0, &@$));
4217 /*% ripper: next!(args_new!) %*/
4221 $<node>$ = add_block_exit(p, NEW_REDO(&@$));
4222 /*% ripper: redo! %*/
4226 if (!p->ctxt.in_defined) {
4227 switch (p->ctxt.in_rescue) {
4228 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4229 case after_rescue: /* ok */ break;
4230 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4231 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4235 $$ = NEW_RETRY(&@$);
4237 /*% ripper: retry! %*/
4241primary_value : primary
4248k_begin : keyword_begin
4250 token_info_push(p, "begin", &@$);
4252 push_end_expect_token_locations(p, &@1.beg_pos);
4260 token_info_push(p, "if", &@$);
4261 if (p->token_info && p->token_info->nonspc &&
4262 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4263 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4264 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4265 beg += rb_strlen_lit("else");
4266 while (beg < tok && ISSPACE(*beg)) beg++;
4268 p->token_info->nonspc = 0;
4272 push_end_expect_token_locations(p, &@1.beg_pos);
4277k_unless : keyword_unless
4279 token_info_push(p, "unless", &@$);
4281 push_end_expect_token_locations(p, &@1.beg_pos);
4286k_while : keyword_while allow_exits
4289 token_info_push(p, "while", &@$);
4291 push_end_expect_token_locations(p, &@1.beg_pos);
4296k_until : keyword_until allow_exits
4299 token_info_push(p, "until", &@$);
4301 push_end_expect_token_locations(p, &@1.beg_pos);
4306k_case : keyword_case
4308 token_info_push(p, "case", &@$);
4310 push_end_expect_token_locations(p, &@1.beg_pos);
4315k_for : keyword_for allow_exits
4318 token_info_push(p, "for", &@$);
4320 push_end_expect_token_locations(p, &@1.beg_pos);
4325k_class : keyword_class
4327 token_info_push(p, "class", &@$);
4329 p->ctxt.in_rescue = before_rescue;
4331 push_end_expect_token_locations(p, &@1.beg_pos);
4336k_module : keyword_module
4338 token_info_push(p, "module", &@$);
4340 p->ctxt.in_rescue = before_rescue;
4342 push_end_expect_token_locations(p, &@1.beg_pos);
4349 token_info_push(p, "def", &@$);
4350 $$ = NEW_DEF_TEMP(&@$);
4351 p->ctxt.in_argdef = 1;
4357 token_info_push(p, "do", &@$);
4359 push_end_expect_token_locations(p, &@1.beg_pos);
4364k_do_block : keyword_do_block
4366 token_info_push(p, "do", &@$);
4368 push_end_expect_token_locations(p, &@1.beg_pos);
4373k_rescue : keyword_rescue
4375 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4377 p->ctxt.in_rescue = after_rescue;
4381k_ensure : keyword_ensure
4383 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4388k_when : keyword_when
4390 token_info_warn(p, "when", p->token_info, 0, &@$);
4394k_else : keyword_else
4396 token_info *ptinfo_beg = p->token_info;
4397 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4398 token_info_warn(p, "else", p->token_info, same, &@$);
4401 e.next = ptinfo_beg->next;
4403 token_info_setup(&e, p->lex.pbeg, &@$);
4404 if (!e.nonspc) *ptinfo_beg = e;
4409k_elsif : keyword_elsif
4412 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4418 token_info_pop(p, "end", &@$);
4420 pop_end_expect_token_locations(p);
4425 compile_error(p, "syntax error, unexpected end-of-input");
4429k_return : keyword_return
4431 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
4432 yyerror1(&@1, "Invalid return in class/module body");
4436k_yield : keyword_yield
4438 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4439 yyerror1(&@1, "Invalid yield");
4453 | k_elsif expr_value then
4458 $$ = new_if(p, $2, $4, $5, &@$);
4461 /*% ripper: elsif!($2, $4, $5) %*/
4471 /*% ripper: else!($2) %*/
4482 $$ = assignable(p, $1, 0, &@$);
4483 mark_lvar_used(p, $$);
4485 /*% ripper: assignable(p, $1) %*/
4487 | tLPAREN f_margs rparen
4492 /*% ripper: mlhs_paren!($2) %*/
4499 $$ = NEW_LIST($1, &@$);
4501 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
4503 | f_marg_list ',' f_marg
4506 $$ = list_append(p, $1, $3);
4508 /*% ripper: mlhs_add!($1, $3) %*/
4512f_margs : f_marg_list
4515 $$ = NEW_MASGN($1, 0, &@$);
4519 | f_marg_list ',' f_rest_marg
4522 $$ = NEW_MASGN($1, $3, &@$);
4524 /*% ripper: mlhs_add_star!($1, $3) %*/
4526 | f_marg_list ',' f_rest_marg ',' f_marg_list
4529 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4531 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
4536 $$ = NEW_MASGN(0, $1, &@$);
4538 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
4540 | f_rest_marg ',' f_marg_list
4543 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4545 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
4549f_rest_marg : tSTAR f_norm_arg
4552 $$ = assignable(p, $2, 0, &@$);
4553 mark_lvar_used(p, $$);
4555 /*% ripper: assignable(p, $2) %*/
4560 $$ = NODE_SPECIAL_NO_NAME_REST;
4562 /*% ripper: Qnil %*/
4566f_any_kwrest : f_kwrest
4567 | f_no_kwarg {$$ = ID2VAL(idNil);}
4570f_eq : {p->ctxt.in_argdef = 0;} '=';
4572block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
4574 $$ = new_args_tail(p, $1, $3, $4, &@3);
4576 | f_block_kwarg opt_f_block_arg
4578 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4580 | f_any_kwrest opt_f_block_arg
4582 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4586 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4590opt_block_args_tail : ',' block_args_tail
4596 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4602 /* magic number for rest_id in iseq_set_arguments() */
4604 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4606 /*% ripper: excessed_comma! %*/
4610block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
4612 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4614 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4616 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4618 | f_arg ',' f_block_optarg opt_block_args_tail
4620 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4622 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
4624 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4626 | f_arg ',' f_rest_arg opt_block_args_tail
4628 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4630 | f_arg excessed_comma
4632 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
4633 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
4635 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
4637 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4639 | f_arg opt_block_args_tail
4641 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4643 | f_block_optarg ',' f_rest_arg opt_block_args_tail
4645 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4647 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4649 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4651 | f_block_optarg opt_block_args_tail
4653 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4655 | f_block_optarg ',' f_arg opt_block_args_tail
4657 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4659 | f_rest_arg opt_block_args_tail
4661 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4663 | f_rest_arg ',' f_arg opt_block_args_tail
4665 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4669 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4673opt_block_param : none
4676 p->command_start = TRUE;
4680block_param_def : '|' opt_bv_decl '|'
4683 p->max_numparam = ORDINAL_PARAM;
4684 p->ctxt.in_argdef = 0;
4688 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
4689 /*% ripper: block_var!($$, $2) %*/
4691 | '|' block_param opt_bv_decl '|'
4694 p->max_numparam = ORDINAL_PARAM;
4695 p->ctxt.in_argdef = 0;
4699 /*% ripper: block_var!($2, $3) %*/
4708 | opt_nl ';' bv_decls opt_nl
4718 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
4720 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
4725 new_bv(p, get_id($1));
4726 /*% ripper: get_value($1) %*/
4735 $$ = p->max_numparam;
4736 p->max_numparam = 0;
4741 $$ = numparam_push(p);
4745lambda : tLAMBDA[dyna]
4747 token_info_push(p, "->", &@1);
4748 $<vars>dyna = dyna_push(p);
4749 $<num>$ = p->lex.lpar_beg;
4750 p->lex.lpar_beg = p->lex.paren_nest;
4752 max_numparam numparam allow_exits
4759 int max_numparam = p->max_numparam;
4760 p->lex.lpar_beg = $<num>lpar;
4761 p->max_numparam = $max_numparam;
4762 restore_block_exit(p, $allow_exits);
4764 $args = args_with_numbered(p, $args, max_numparam);
4767 YYLTYPE loc = code_loc_gen(&@args, &@body);
4768 $$ = NEW_LAMBDA($args, $body, &loc);
4769 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
4770 nd_set_line($$, @args.end_pos.lineno);
4771 nd_set_first_loc($$, @1.beg_pos);
4774 /*% ripper: lambda!($args, $body) %*/
4775 numparam_pop(p, $numparam);
4776 dyna_pop(p, $<vars>dyna);
4780f_larglist : '(' f_args opt_bv_decl ')'
4782 p->ctxt.in_argdef = 0;
4785 p->max_numparam = ORDINAL_PARAM;
4787 /*% ripper: paren!($2) %*/
4791 p->ctxt.in_argdef = 0;
4793 if (!args_info_empty_p(&$1->nd_ainfo))
4794 p->max_numparam = ORDINAL_PARAM;
4800lambda_body : tLAMBEG compstmt '}'
4802 token_info_pop(p, "}", &@3);
4808 push_end_expect_token_locations(p, &@1.beg_pos);
4817do_block : k_do_block do_body k_end
4821 set_embraced_location($$, &@1, &@3);
4826block_call : command do_block
4829 if (nd_type_p($1, NODE_YIELD)) {
4830 compile_error(p, "block given to yield");
4833 block_dup_check(p, get_nd_args(p, $1), $2);
4835 $$ = method_add_block(p, $1, $2, &@$);
4838 /*% ripper: method_add_block!($1, $2) %*/
4840 | block_call call_op2 operation2 opt_paren_args
4843 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4845 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4847 | block_call call_op2 operation2 opt_paren_args brace_block
4850 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4852 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
4854 | block_call call_op2 operation2 command_args do_block
4857 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4859 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
4863method_call : fcall paren_args
4868 nd_set_last_loc($1, @2.end_pos);
4870 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
4872 | primary_value call_op operation2 opt_paren_args
4875 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4876 nd_set_line($$, @3.end_pos.lineno);
4878 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4880 | primary_value tCOLON2 operation2 paren_args
4883 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
4884 nd_set_line($$, @3.end_pos.lineno);
4886 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
4888 | primary_value tCOLON2 operation3
4891 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4893 /*% ripper: call!($1, $2, $3) %*/
4895 | primary_value call_op paren_args
4898 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
4899 nd_set_line($$, @2.end_pos.lineno);
4901 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4903 | primary_value tCOLON2 paren_args
4906 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
4907 nd_set_line($$, @2.end_pos.lineno);
4909 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4911 | keyword_super paren_args
4914 $$ = NEW_SUPER($2, &@$);
4916 /*% ripper: super!($2) %*/
4921 $$ = NEW_ZSUPER(&@$);
4923 /*% ripper: zsuper! %*/
4925 | primary_value '[' opt_call_args rbracket
4928 $$ = NEW_CALL($1, tAREF, $3, &@$);
4931 /*% ripper: aref!($1, $3) %*/
4935brace_block : '{' brace_body '}'
4939 set_embraced_location($$, &@1, &@3);
4942 | k_do do_body k_end
4946 set_embraced_location($$, &@1, &@3);
4951brace_body : {$<vars>$ = dyna_push(p);}[dyna]
4952 max_numparam numparam allow_exits
4953 opt_block_param[args] compstmt
4955 int max_numparam = p->max_numparam;
4956 p->max_numparam = $max_numparam;
4957 $args = args_with_numbered(p, $args, max_numparam);
4959 $$ = NEW_ITER($args, $compstmt, &@$);
4961 /*% ripper: brace_block!($args, $compstmt) %*/
4962 restore_block_exit(p, $allow_exits);
4963 numparam_pop(p, $numparam);
4964 dyna_pop(p, $<vars>dyna);
4969 $<vars>$ = dyna_push(p);
4972 max_numparam numparam allow_exits
4973 opt_block_param[args] bodystmt
4975 int max_numparam = p->max_numparam;
4976 p->max_numparam = $max_numparam;
4977 $args = args_with_numbered(p, $args, max_numparam);
4979 $$ = NEW_ITER($args, $bodystmt, &@$);
4981 /*% ripper: do_block!($args, $bodystmt) %*/
4983 restore_block_exit(p, $allow_exits);
4984 numparam_pop(p, $numparam);
4985 dyna_pop(p, $<vars>dyna);
4989case_args : arg_value
4992 check_literal_when(p, $1, &@1);
4993 $$ = NEW_LIST($1, &@$);
4995 /*% ripper: args_add!(args_new!, $1) %*/
5000 $$ = NEW_SPLAT($2, &@$);
5002 /*% ripper: args_add_star!(args_new!, $2) %*/
5004 | case_args ',' arg_value
5007 check_literal_when(p, $3, &@3);
5008 $$ = last_arg_append(p, $1, $3, &@$);
5010 /*% ripper: args_add!($1, $3) %*/
5012 | case_args ',' tSTAR arg_value
5015 $$ = rest_arg_append(p, $1, $4, &@$);
5017 /*% ripper: args_add_star!($1, $4) %*/
5021case_body : k_when case_args then
5026 $$ = NEW_WHEN($2, $4, $5, &@$);
5029 /*% ripper: when!($2, $4, $5) %*/
5037p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5038p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5042 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5043 p->command_start = FALSE;
5044 p->ctxt.in_kwarg = 1;
5048p_case_body : keyword_in
5049 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5050 p_top_expr[expr] then
5052 pop_pktbl(p, $p_pktbl);
5053 pop_pvtbl(p, $p_pvtbl);
5054 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5060 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5062 /*% ripper: in!($expr, $compstmt, $cases) %*/
5070p_top_expr : p_top_expr_body
5071 | p_top_expr_body modifier_if expr_value
5074 $$ = new_if(p, $3, $1, 0, &@$);
5077 /*% ripper: if_mod!($3, $1) %*/
5079 | p_top_expr_body modifier_unless expr_value
5082 $$ = new_unless(p, $3, $1, 0, &@$);
5085 /*% ripper: unless_mod!($3, $1) %*/
5089p_top_expr_body : p_expr
5092 $$ = new_array_pattern_tail(p, Qnone, 1, Qnone, Qnone, &@$);
5093 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
5097 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
5099 nd_set_first_loc($$, @1.beg_pos);
5105 $$ = new_find_pattern(p, Qnone, $1, &@$);
5109 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
5113 $$ = new_hash_pattern(p, Qnone, $1, &@$);
5120p_as : p_expr tASSOC p_variable
5123 NODE *n = NEW_LIST($1, &@$);
5124 n = list_append(p, n, $3);
5125 $$ = new_hash(p, n, &@$);
5127 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
5132p_alt : p_alt '|' p_expr_basic
5135 $$ = NEW_OR($1, $3, &@$);
5137 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
5142p_lparen : '(' p_pktbl { $$ = $2;};
5143p_lbracket : '[' p_pktbl { $$ = $2;};
5145p_expr_basic : p_value
5147 | p_const p_lparen[p_pktbl] p_args rparen
5149 pop_pktbl(p, $p_pktbl);
5150 $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$);
5152 nd_set_first_loc($$, @p_const.beg_pos);
5156 | p_const p_lparen[p_pktbl] p_find rparen
5158 pop_pktbl(p, $p_pktbl);
5159 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5161 nd_set_first_loc($$, @p_const.beg_pos);
5165 | p_const p_lparen[p_pktbl] p_kwargs rparen
5167 pop_pktbl(p, $p_pktbl);
5168 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5170 nd_set_first_loc($$, @p_const.beg_pos);
5174 | p_const '(' rparen
5176 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5177 $$ = new_array_pattern(p, $p_const, Qnone, $$, &@$);
5179 | p_const p_lbracket[p_pktbl] p_args rbracket
5181 pop_pktbl(p, $p_pktbl);
5182 $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$);
5184 nd_set_first_loc($$, @p_const.beg_pos);
5188 | p_const p_lbracket[p_pktbl] p_find rbracket
5190 pop_pktbl(p, $p_pktbl);
5191 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5193 nd_set_first_loc($$, @p_const.beg_pos);
5197 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5199 pop_pktbl(p, $p_pktbl);
5200 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5202 nd_set_first_loc($$, @p_const.beg_pos);
5206 | p_const '[' rbracket
5208 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5209 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
5211 | tLBRACK p_args rbracket
5213 $$ = new_array_pattern(p, Qnone, Qnone, $p_args, &@$);
5215 | tLBRACK p_find rbracket
5217 $$ = new_find_pattern(p, Qnone, $p_find, &@$);
5221 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5222 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
5224 | tLBRACE p_pktbl lex_ctxt[ctxt]
5226 p->ctxt.in_kwarg = 0;
5230 pop_pktbl(p, $p_pktbl);
5231 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5232 $$ = new_hash_pattern(p, Qnone, $p_kwargs, &@$);
5236 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
5237 $$ = new_hash_pattern(p, Qnone, $$, &@$);
5239 | tLPAREN p_pktbl p_expr rparen
5241 pop_pktbl(p, $p_pktbl);
5249 NODE *pre_args = NEW_LIST($1, &@$);
5250 $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$);
5252 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, Qnone, Qnone, &@$);
5257 $$ = new_array_pattern_tail(p, $1, 1, Qnone, Qnone, &@$);
5262 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, Qnone, Qnone, &@$);
5264 VALUE pre_args = rb_ary_concat($1, get_value($2));
5265 $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$);
5268 | p_args_head p_rest
5270 $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$);
5272 | p_args_head p_rest ',' p_args_post
5274 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5279p_args_head : p_arg ','
5283 | p_args_head p_arg ','
5286 $$ = list_concat($1, $2);
5288 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
5294 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
5296 | p_rest ',' p_args_post
5298 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
5302p_find : p_rest ',' p_args_post ',' p_rest
5304 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5309p_rest : tSTAR tIDENTIFIER
5312 error_duplicate_pattern_variable(p, $2, &@2);
5313 $$ = assignable(p, $2, 0, &@$);
5315 /*% ripper: assignable(p, var_field(p, $2)) %*/
5322 /*% ripper: var_field(p, Qnil) %*/
5327 | p_args_post ',' p_arg
5330 $$ = list_concat($1, $3);
5332 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
5339 $$ = NEW_LIST($1, &@$);
5341 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
5345p_kwargs : p_kwarg ',' p_any_kwrest
5347 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5351 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5355 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5359 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
5364 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
5368 $$ = list_concat($1, $3);
5370 /*% ripper: rb_ary_push($1, $3) %*/
5374p_kw : p_kw_label p_expr
5376 error_duplicate_pattern_key(p, get_id($1), &@1);
5378 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5380 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
5384 error_duplicate_pattern_key(p, get_id($1), &@1);
5385 if ($1 && !is_local_id(get_id($1))) {
5386 yyerror1(&@1, "key must be valid as local variables");
5388 error_duplicate_pattern_variable(p, get_id($1), &@1);
5390 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
5392 /*% ripper: rb_ary_new_from_args(2, get_value(assignable(p, $1)), Qnil) %*/
5397 | tSTRING_BEG string_contents tLABEL_END
5399 YYLTYPE loc = code_loc_gen(&@1, &@3);
5401 if (!$2 || nd_type_p($2, NODE_STR)) {
5402 NODE *node = dsym_node(p, $2, &loc);
5403 $$ = SYM2ID(RNODE_LIT(node)->nd_lit);
5406 if (ripper_is_node_yylval(p, $2) && RNODE_RIPPER($2)->nd_cval) {
5407 VALUE label = RNODE_RIPPER($2)->nd_cval;
5408 VALUE rval = RNODE_RIPPER($2)->nd_rval;
5409 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
5410 RNODE($$)->nd_loc = loc;
5414 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5420p_kwrest : kwrest_mark tIDENTIFIER
5430p_kwnorest : kwrest_mark keyword_nil
5436p_any_kwrest : p_kwrest
5437 | p_kwnorest {$$ = ID2VAL(idNil);}
5440p_value : p_primitive
5441 | p_primitive tDOT2 p_primitive
5446 $$ = NEW_DOT2($1, $3, &@$);
5448 /*% ripper: dot2!($1, $3) %*/
5450 | p_primitive tDOT3 p_primitive
5455 $$ = NEW_DOT3($1, $3, &@$);
5457 /*% ripper: dot3!($1, $3) %*/
5463 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
5465 /*% ripper: dot2!($1, Qnil) %*/
5471 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
5473 /*% ripper: dot3!($1, Qnil) %*/
5478 | tBDOT2 p_primitive
5482 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
5484 /*% ripper: dot2!(Qnil, $2) %*/
5486 | tBDOT3 p_primitive
5490 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
5492 /*% ripper: dot3!(Qnil, $2) %*/
5496p_primitive : literal
5507 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5509 /*% ripper: var_ref!($1) %*/
5514p_variable : tIDENTIFIER
5517 error_duplicate_pattern_variable(p, $1, &@1);
5518 $$ = assignable(p, $1, 0, &@$);
5520 /*% ripper: assignable(p, var_field(p, $1)) %*/
5524p_var_ref : '^' tIDENTIFIER
5527 NODE *n = gettable(p, $2, &@$);
5528 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5529 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5533 /*% ripper: var_ref!($2) %*/
5538 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
5540 /*% ripper: var_ref!($2) %*/
5544p_expr_ref : '^' tLPAREN expr_value rparen
5547 $$ = NEW_BLOCK($3, &@$);
5549 /*% ripper: begin!($3) %*/
5553p_const : tCOLON3 cname
5556 $$ = NEW_COLON3($2, &@$);
5558 /*% ripper: top_const_ref!($2) %*/
5560 | p_const tCOLON2 cname
5563 $$ = NEW_COLON2($1, $3, &@$);
5565 /*% ripper: const_path_ref!($1, $3) %*/
5570 $$ = gettable(p, $1, &@$);
5572 /*% ripper: var_ref!($1) %*/
5576opt_rescue : k_rescue exc_list exc_var then
5583 NODE *err = NEW_ERRINFO(&@3);
5584 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5585 body = block_append(p, err, body);
5587 $$ = NEW_RESBODY($2, body, $6, &@$);
5598 /*% ripper: rescue!($2, $3, $5, $6) %*/
5606 $$ = NEW_LIST($1, &@$);
5608 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5613 if (!($$ = splat_array($1))) $$ = $1;
5627opt_ensure : k_ensure compstmt
5629 p->ctxt.in_rescue = $1.in_rescue;
5633 /*% ripper: ensure!($2) %*/
5647 node = NEW_STR(STR_NEW0(), &@$);
5648 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit);
5651 node = evstr2dstr(p, node);
5664 $$ = literal_concat(p, $1, $2, &@$);
5666 /*% ripper: string_concat!($1, $2) %*/
5670string1 : tSTRING_BEG string_contents tSTRING_END
5673 $$ = heredoc_dedent(p, $2);
5674 if ($$) nd_set_loc($$, &@$);
5676 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
5680xstring : tXSTRING_BEG xstring_contents tSTRING_END
5683 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5685 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
5689regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5691 $$ = new_regexp(p, $2, $3, &@$);
5699words : tWORDS_BEG words_sep word_list tSTRING_END
5702 $$ = make_list($3, &@$);
5704 /*% ripper: array!($3) %*/
5708word_list : /* none */
5713 /*% ripper: words_new! %*/
5715 | word_list word words_sep
5718 $$ = list_append(p, $1, evstr2dstr(p, $2));
5720 /*% ripper: words_add!($1, $2) %*/
5724word : string_content
5725 /*% ripper[brace]: word_add!(word_new!, $1) %*/
5726 | word string_content
5729 $$ = literal_concat(p, $1, $2, &@$);
5731 /*% ripper: word_add!($1, $2) %*/
5735symbols : tSYMBOLS_BEG words_sep symbol_list tSTRING_END
5738 $$ = make_list($3, &@$);
5740 /*% ripper: array!($3) %*/
5744symbol_list : /* none */
5749 /*% ripper: symbols_new! %*/
5751 | symbol_list word words_sep
5754 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5756 /*% ripper: symbols_add!($1, $2) %*/
5760qwords : tQWORDS_BEG words_sep qword_list tSTRING_END
5763 $$ = make_list($3, &@$);
5765 /*% ripper: array!($3) %*/
5769qsymbols : tQSYMBOLS_BEG words_sep qsym_list tSTRING_END
5772 $$ = make_list($3, &@$);
5774 /*% ripper: array!($3) %*/
5778qword_list : /* none */
5783 /*% ripper: qwords_new! %*/
5785 | qword_list tSTRING_CONTENT words_sep
5788 $$ = list_append(p, $1, $2);
5790 /*% ripper: qwords_add!($1, $2) %*/
5794qsym_list : /* none */
5799 /*% ripper: qsymbols_new! %*/
5801 | qsym_list tSTRING_CONTENT words_sep
5804 $$ = symbol_append(p, $1, $2);
5806 /*% ripper: qsymbols_add!($1, $2) %*/
5810string_contents : /* none */
5815 /*% ripper: string_content! %*/
5818 $$ = ripper_new_yylval(p, 0, $$, 0);
5821 | string_contents string_content
5824 $$ = literal_concat(p, $1, $2, &@$);
5826 /*% ripper: string_add!($1, $2) %*/
5829 if (ripper_is_node_yylval(p, $1) && ripper_is_node_yylval(p, $2) &&
5830 !RNODE_RIPPER($1)->nd_cval) {
5831 RNODE_RIPPER($1)->nd_cval = RNODE_RIPPER($2)->nd_cval;
5832 RNODE_RIPPER($1)->nd_rval = add_mark_object(p, $$);
5839xstring_contents: /* none */
5844 /*% ripper: xstring_new! %*/
5846 | xstring_contents string_content
5849 $$ = literal_concat(p, $1, $2, &@$);
5851 /*% ripper: xstring_add!($1, $2) %*/
5855regexp_contents: /* none */
5860 /*% ripper: regexp_new! %*/
5863 $$ = ripper_new_yylval(p, 0, $$, 0);
5866 | regexp_contents string_content
5869 NODE *head = $1, *tail = $2;
5877 switch (nd_type(head)) {
5879 head = str2dstr(p, head);
5884 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
5887 $$ = list_append(p, head, tail);
5890 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
5891 if (ripper_is_node_yylval(p, n1)) {
5892 s1 = RNODE_RIPPER(n1)->nd_cval;
5893 n1 = RNODE_RIPPER(n1)->nd_rval;
5895 if (ripper_is_node_yylval(p, n2)) {
5896 s2 = RNODE_RIPPER(n2)->nd_cval;
5897 n2 = RNODE_RIPPER(n2)->nd_rval;
5899 $$ = dispatch2(regexp_add, n1, n2);
5901 $$ = ripper_new_yylval(p, 0, $$, s2);
5907string_content : tSTRING_CONTENT
5908 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
5911 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
5912 $<strterm>$ = p->lex.strterm;
5914 SET_LEX_STATE(EXPR_BEG);
5918 p->lex.strterm = $<strterm>2;
5920 $$ = NEW_EVSTR($3, &@$);
5921 nd_set_line($$, @3.end_pos.lineno);
5923 /*% ripper: string_dvar!($3) %*/
5925 | tSTRING_DBEG[term]
5929 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
5930 $<strterm>term = p->lex.strterm;
5932 $<num>$ = p->lex.state;
5933 SET_LEX_STATE(EXPR_BEG);
5936 $<num>$ = p->lex.brace_nest;
5937 p->lex.brace_nest = 0;
5940 $<num>$ = p->heredoc_indent;
5941 p->heredoc_indent = 0;
5943 compstmt string_dend
5947 p->lex.strterm = $<strterm>term;
5948 SET_LEX_STATE($<num>state);
5949 p->lex.brace_nest = $<num>brace;
5950 p->heredoc_indent = $<num>indent;
5951 p->heredoc_line_indent = -1;
5953 if ($compstmt) nd_unset_fl_newline($compstmt);
5954 $$ = new_evstr(p, $compstmt, &@$);
5956 /*% ripper: string_embexpr!($compstmt) %*/
5960string_dend : tSTRING_DEND
5964string_dvar : nonlocal_var
5967 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5969 /*% ripper: var_ref!($1) %*/
5980 SET_LEX_STATE(EXPR_END);
5982 $$ = NEW_LIT(ID2SYM($2), &@$);
5984 /*% ripper: symbol_literal!(symbol!($2)) %*/
5992dsym : tSYMBEG string_contents tSTRING_END
5994 SET_LEX_STATE(EXPR_END);
5996 $$ = dsym_node(p, $2, &@$);
5998 /*% ripper: dyna_symbol!($2) %*/
6002numeric : simple_numeric
6003 | tUMINUS_NUM simple_numeric %prec tLOWEST
6007 RB_OBJ_WRITE(p->ast, &RNODE_LIT($$)->nd_lit, negate_lit(p, RNODE_LIT($$)->nd_lit));
6009 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
6013simple_numeric : tINTEGER
6024user_variable : tIDENTIFIER
6029keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6030 | keyword_self {$$ = KWD2EID(self, $1);}
6031 | keyword_true {$$ = KWD2EID(true, $1);}
6032 | keyword_false {$$ = KWD2EID(false, $1);}
6033 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6034 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6035 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6038var_ref : user_variable
6041 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
6043 if (id_is_var(p, get_id($1))) {
6044 $$ = dispatch1(var_ref, $1);
6047 $$ = dispatch1(vcall, $1);
6054 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6056 /*% ripper: var_ref!($1) %*/
6060var_lhs : user_variable
6063 $$ = assignable(p, $1, 0, &@$);
6065 /*% ripper: assignable(p, var_field(p, $1)) %*/
6070 $$ = assignable(p, $1, 0, &@$);
6072 /*% ripper: assignable(p, var_field(p, $1)) %*/
6082 SET_LEX_STATE(EXPR_BEG);
6083 p->command_start = TRUE;
6094 /*% ripper: Qnil %*/
6098f_opt_paren_args: f_paren_args
6101 p->ctxt.in_argdef = 0;
6102 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6103 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
6107f_paren_args : '(' f_args rparen
6112 /*% ripper: paren!($2) %*/
6113 SET_LEX_STATE(EXPR_BEG);
6114 p->command_start = TRUE;
6115 p->ctxt.in_argdef = 0;
6119f_arglist : f_paren_args
6122 p->ctxt.in_kwarg = 1;
6123 p->ctxt.in_argdef = 1;
6124 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6128 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
6129 p->ctxt.in_argdef = 0;
6131 SET_LEX_STATE(EXPR_BEG);
6132 p->command_start = TRUE;
6136args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
6138 $$ = new_args_tail(p, $1, $3, $4, &@3);
6140 | f_kwarg opt_f_block_arg
6142 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
6144 | f_any_kwrest opt_f_block_arg
6146 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
6150 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
6154 add_forwarding_args(p);
6155 $$ = new_args_tail(p, Qnone, $1, arg_FWD_BLOCK, &@1);
6157 $$->nd_ainfo.forwarding = 1;
6162opt_args_tail : ',' args_tail
6168 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6172f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
6174 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
6176 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
6178 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6180 | f_arg ',' f_optarg opt_args_tail
6182 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
6184 | f_arg ',' f_optarg ',' f_arg opt_args_tail
6186 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
6188 | f_arg ',' f_rest_arg opt_args_tail
6190 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
6192 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
6194 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
6196 | f_arg opt_args_tail
6198 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
6200 | f_optarg ',' f_rest_arg opt_args_tail
6202 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
6204 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
6206 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
6208 | f_optarg opt_args_tail
6210 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
6212 | f_optarg ',' f_arg opt_args_tail
6214 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
6216 | f_rest_arg opt_args_tail
6218 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
6220 | f_rest_arg ',' f_arg opt_args_tail
6222 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
6226 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
6230 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6231 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
6235args_forward : tBDOT3
6238#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6244 /*% ripper: args_forward! %*/
6248f_bad_arg : tCONSTANT
6250 static const char mesg[] = "formal argument cannot be a constant";
6252 yyerror1(&@1, mesg);
6255 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6259 static const char mesg[] = "formal argument cannot be an instance variable";
6261 yyerror1(&@1, mesg);
6264 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6268 static const char mesg[] = "formal argument cannot be a global variable";
6270 yyerror1(&@1, mesg);
6273 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6277 static const char mesg[] = "formal argument cannot be a class variable";
6279 yyerror1(&@1, mesg);
6282 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6286f_norm_arg : f_bad_arg
6289 formal_argument(p, $1);
6290 p->max_numparam = ORDINAL_PARAM;
6295f_arg_asgn : f_norm_arg
6304f_arg_item : f_arg_asgn
6308 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6310 /*% ripper: get_value($1) %*/
6312 | tLPAREN f_margs rparen
6315 ID tid = internal_id(p);
6317 loc.beg_pos = @2.beg_pos;
6318 loc.end_pos = @2.beg_pos;
6320 if (dyna_in_block(p)) {
6321 $2->nd_value = NEW_DVAR(tid, &loc);
6324 $2->nd_value = NEW_LVAR(tid, &loc);
6326 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6327 $$->nd_next = (NODE *)$2;
6329 /*% ripper: mlhs_paren!($2) %*/
6334 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6335 | f_arg ',' f_arg_item
6340 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6341 rb_discard_node(p, (NODE *)$3);
6343 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6350 arg_var(p, formal_argument(p, $1));
6351 p->cur_arg = get_id($1);
6352 p->max_numparam = ORDINAL_PARAM;
6353 p->ctxt.in_argdef = 0;
6358f_kw : f_label arg_value
6361 p->ctxt.in_argdef = 1;
6363 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6365 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
6370 p->ctxt.in_argdef = 1;
6372 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6374 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
6378f_block_kw : f_label primary_value
6380 p->ctxt.in_argdef = 1;
6382 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6384 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
6388 p->ctxt.in_argdef = 1;
6390 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6392 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
6396f_block_kwarg : f_block_kw
6401 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6403 | f_block_kwarg ',' f_block_kw
6406 $$ = kwd_append($1, $3);
6408 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6418 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6423 $$ = kwd_append($1, $3);
6425 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6433f_no_kwarg : p_kwnorest
6437 /*% ripper: nokw_param!(Qnil) %*/
6441f_kwrest : kwrest_mark tIDENTIFIER
6443 arg_var(p, shadowing_lvar(p, get_id($2)));
6447 /*% ripper: kwrest_param!($2) %*/
6451 arg_var(p, idFWD_KWREST);
6455 /*% ripper: kwrest_param!(Qnil) %*/
6459f_opt : f_arg_asgn f_eq arg_value
6462 p->ctxt.in_argdef = 1;
6464 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
6466 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
6470f_block_opt : f_arg_asgn f_eq primary_value
6473 p->ctxt.in_argdef = 1;
6475 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
6477 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
6481f_block_optarg : f_block_opt
6486 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6488 | f_block_optarg ',' f_block_opt
6491 $$ = opt_arg_append($1, $3);
6493 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6502 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6504 | f_optarg ',' f_opt
6507 $$ = opt_arg_append($1, $3);
6509 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6517f_rest_arg : restarg_mark tIDENTIFIER
6519 arg_var(p, shadowing_lvar(p, get_id($2)));
6523 /*% ripper: rest_param!($2) %*/
6527 arg_var(p, idFWD_REST);
6531 /*% ripper: rest_param!(Qnil) %*/
6539f_block_arg : blkarg_mark tIDENTIFIER
6541 arg_var(p, shadowing_lvar(p, get_id($2)));
6545 /*% ripper: blockarg!($2) %*/
6549 arg_var(p, idFWD_BLOCK);
6553 /*% ripper: blockarg!(Qnil) %*/
6557opt_f_block_arg : ',' f_block_arg
6572 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
6575 NODE *expr = last_expr_node($3);
6576 switch (nd_type(expr)) {
6586 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6594 /*% ripper: paren!($3) %*/
6604 /*% ripper: assoclist_from_args!($1) %*/
6609 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6619 if (RNODE_LIST(assocs)->nd_head &&
6620 !RNODE_LIST(tail)->nd_head && nd_type_p(RNODE_LIST(tail)->nd_next, NODE_LIST) &&
6621 nd_type_p(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head, NODE_HASH)) {
6623 tail = RNODE_HASH(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head)->nd_head;
6625 assocs = list_concat(assocs, tail);
6629 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6633assoc : arg_value tASSOC arg_value
6636 if (nd_type_p($1, NODE_STR)) {
6637 nd_set_type($1, NODE_LIT);
6638 RB_OBJ_WRITE(p->ast, &RNODE_LIT($1)->nd_lit, rb_fstring(RNODE_LIT($1)->nd_lit));
6640 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6642 /*% ripper: assoc_new!($1, $3) %*/
6647 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
6649 /*% ripper: assoc_new!($1, $2) %*/
6654 NODE *val = gettable(p, $1, &@$);
6655 if (!val) val = NEW_ERROR(&@$);
6656 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
6658 /*% ripper: assoc_new!($1, Qnil) %*/
6660 | tSTRING_BEG string_contents tLABEL_END arg_value
6663 YYLTYPE loc = code_loc_gen(&@1, &@3);
6664 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6666 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
6671 if (nd_type_p($2, NODE_HASH) &&
6672 !(RNODE_HASH($2)->nd_head && RNODE_LIST(RNODE_HASH($2)->nd_head)->as.nd_alen)) {
6673 static VALUE empty_hash;
6675 empty_hash = rb_obj_freeze(rb_hash_new());
6676 rb_gc_register_mark_object(empty_hash);
6678 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
6681 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6683 /*% ripper: assoc_splat!($2) %*/
6687 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6689 $$ = list_append(p, NEW_LIST(0, &@$),
6690 NEW_LVAR(idFWD_KWREST, &@$));
6692 /*% ripper: assoc_splat!(Qnil) %*/
6696operation : tIDENTIFIER
6701operation2 : operation
6705operation3 : tIDENTIFIER
6722opt_terms : /* none */
6733rbracket : opt_nl ']'
6743term : ';' {yyerrok;token_flush(p);}
6746 @$.end_pos = @$.beg_pos;
6752 | terms ';' {yyerrok;}
6764# define yylval (*p->lval)
6766static int regx_options(struct parser_params*);
6767static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6768static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6769static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6770static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6773# define set_yylval_node(x) { \
6775 rb_parser_set_location(p, &_cur_loc); \
6776 yylval.node = (x); \
6778# define set_yylval_str(x) \
6780 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6781 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6783# define set_yylval_literal(x) \
6785 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
6786 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6788# define set_yylval_num(x) (yylval.num = (x))
6789# define set_yylval_id(x) (yylval.id = (x))
6790# define set_yylval_name(x) (yylval.id = (x))
6791# define yylval_id() (yylval.id)
6794ripper_yylval_id(struct parser_params *p, ID x)
6796 return ripper_new_yylval(p, x, ID2SYM(x), 0);
6798# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
6799# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
6800# define set_yylval_id(x) (void)(x)
6801# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
6802# define set_yylval_literal(x) add_mark_object(p, (x))
6803# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
6804# define yylval_id() yylval.id
6805# define _cur_loc NULL_LOC /* dummy */
6808#define set_yylval_noname() set_yylval_id(keyword_nil)
6809#define has_delayed_token(p) (!NIL_P(p->delayed.token))
6812#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6813#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6816parser_has_token(struct parser_params *p)
6818 const char *const pcur = p->lex.pcur;
6819 const char *const ptok = p->lex.ptok;
6820 if (p->keep_tokens && (pcur < ptok)) {
6821 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6822 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6828code_loc_to_ary(struct parser_params *p, const rb_code_location_t *loc)
6830 VALUE ary = rb_ary_new_from_args(4,
6831 INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column),
6832 INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column));
6839parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line)
6844 ary = rb_ary_new2(4);
6845 token_id = p->token_id;
6846 rb_ary_push(ary, INT2FIX(token_id));
6847 rb_ary_push(ary, ID2SYM(parser_token2id(p, t)));
6848 rb_ary_push(ary, str);
6849 rb_ary_push(ary, code_loc_to_ary(p, p->yylloc));
6851 rb_ary_push(p->tokens, ary);
6855 rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary);
6860parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6862 debug_token_line(p, "parser_dispatch_scan_event", line);
6864 if (!parser_has_token(p)) return;
6866 RUBY_SET_YYLLOC(*p->yylloc);
6868 if (p->keep_tokens) {
6869 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6870 parser_append_tokens(p, str, t, line);
6876#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6878parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6880 debug_token_line(p, "parser_dispatch_delayed_token", line);
6882 if (!has_delayed_token(p)) return;
6884 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6886 if (p->keep_tokens) {
6887 parser_append_tokens(p, p->delayed.token, t, line);
6890 p->delayed.token = Qnil;
6893#define literal_flush(p, ptr) ((void)(ptr))
6895#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &RNODE_RIPPER(yylval.node)->nd_rval : &yylval.val))
6898ripper_has_scan_event(struct parser_params *p)
6900 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6901 return p->lex.pcur > p->lex.ptok;
6905ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6907 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6908 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6909 RUBY_SET_YYLLOC(*p->yylloc);
6915ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6917 if (!ripper_has_scan_event(p)) return;
6918 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
6920#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6923ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6925 /* save and adjust the location to delayed token for callbacks */
6926 int saved_line = p->ruby_sourceline;
6927 const char *saved_tokp = p->lex.ptok;
6929 if (!has_delayed_token(p)) return;
6930 p->ruby_sourceline = p->delayed.beg_line;
6931 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6932 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
6933 p->delayed.token = Qnil;
6934 p->ruby_sourceline = saved_line;
6935 p->lex.ptok = saved_tokp;
6937#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6941is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6943 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6947parser_is_identchar(struct parser_params *p)
6949 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6953parser_isascii(struct parser_params *p)
6955 return ISASCII(*(p->lex.pcur-1));
6959token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6961 int column = 1, nonspc = 0, i;
6962 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6964 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6967 if (*ptr != ' ' && *ptr != '\t') {
6972 ptinfo->beg = loc->beg_pos;
6973 ptinfo->indent = column;
6974 ptinfo->nonspc = nonspc;
6978token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6982 if (!p->token_info_enabled) return;
6983 ptinfo = ALLOC(token_info);
6984 ptinfo->token = token;
6985 ptinfo->next = p->token_info;
6986 token_info_setup(ptinfo, p->lex.pbeg, loc);
6988 p->token_info = ptinfo;
6992token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6994 token_info *ptinfo_beg = p->token_info;
6996 if (!ptinfo_beg) return;
6997 p->token_info = ptinfo_beg->next;
6999 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7000 token_info_warn(p, token, ptinfo_beg, 1, loc);
7001 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7005token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7007 token_info *ptinfo_beg = p->token_info;
7009 if (!ptinfo_beg) return;
7010 p->token_info = ptinfo_beg->next;
7012 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7013 ptinfo_beg->beg.column != beg_pos.column ||
7014 strcmp(ptinfo_beg->token, token)) {
7015 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7016 beg_pos.lineno, beg_pos.column, token,
7017 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7021 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7025token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7027 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7028 if (!p->token_info_enabled) return;
7029 if (!ptinfo_beg) return;
7030 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7031 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7032 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7033 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7034 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7035 rb_warn3L(ptinfo_end->beg.lineno,
7036 "mismatched indentations at '%s' with '%s' at %d",
7037 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7041parser_precise_mbclen(struct parser_params *p, const char *ptr)
7043 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7044 if (!MBCLEN_CHARFOUND_P(len)) {
7045 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7052static void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
7055parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7058 int lineno = p->ruby_sourceline;
7062 else if (yylloc->beg_pos.lineno == lineno) {
7063 str = p->lex.lastline;
7068 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7072parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7078 yylloc = RUBY_SET_YYLLOC(current);
7080 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7081 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7085 parser_compile_error(p, yylloc, "%s", msg);
7086 parser_show_error_line(p, yylloc);
7091parser_yyerror0(struct parser_params *p, const char *msg)
7094 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7098ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
7101 const int max_line_margin = 30;
7102 const char *ptr, *ptr_end, *pt, *pb;
7103 const char *pre = "", *post = "", *pend;
7104 const char *code = "", *caret = "";
7106 const char *const pbeg = RSTRING_PTR(str);
7111 if (!yylloc) return;
7112 pend = RSTRING_END(str);
7113 if (pend > pbeg && pend[-1] == '\n') {
7114 if (--pend > pbeg && pend[-1] == '\r') --pend;
7118 if (lineno == yylloc->end_pos.lineno &&
7119 (pend - pbeg) > yylloc->end_pos.column) {
7120 pt = pbeg + yylloc->end_pos.column;
7124 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7125 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7127 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7128 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7130 len = ptr_end - ptr;
7133 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
7134 if (ptr > pbeg) pre = "...";
7136 if (ptr_end < pend) {
7137 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
7138 if (ptr_end < pend) post = "...";
7142 if (lineno == yylloc->beg_pos.lineno) {
7143 pb += yylloc->beg_pos.column;
7144 if (pb > pt) pb = pt;
7146 if (pb < ptr) pb = ptr;
7147 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7150 if (RTEST(errbuf)) {
7151 mesg = rb_attr_get(errbuf, idMesg);
7152 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
7153 rb_str_cat_cstr(mesg, "\n");
7156 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
7158 if (!errbuf && rb_stderr_tty_p()) {
7159#define CSI_BEGIN "\033["
7162 CSI_BEGIN""CSI_SGR"%s" /* pre */
7163 CSI_BEGIN"1"CSI_SGR"%.*s"
7164 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7165 CSI_BEGIN";1"CSI_SGR"%.*s"
7166 CSI_BEGIN""CSI_SGR"%s" /* post */
7169 (int)(pb - ptr), ptr,
7171 (int)(ptr_end - pt), pt,
7177 len = ptr_end - ptr;
7178 lim = pt < pend ? pt : pend;
7179 i = (int)(lim - ptr);
7180 buf = ALLOCA_N(char, i+2);
7185 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7191 memset(p2, '~', (lim - ptr));
7195 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7196 pre, (int)len, code, post,
7199 if (!errbuf) rb_write_error_str(mesg);
7203parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7205 const char *pcur = 0, *ptok = 0;
7206 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7207 p->ruby_sourceline == yylloc->end_pos.lineno) {
7210 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7211 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7213 parser_yyerror0(p, msg);
7222parser_yyerror0(struct parser_params *p, const char *msg)
7224 dispatch1(parse_error, STR_NEW2(msg));
7230parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7237vtable_size(const struct vtable *tbl)
7239 if (!DVARS_TERMINAL_P(tbl)) {
7248static struct vtable *
7249vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7251 struct vtable *tbl = ALLOC(struct vtable);
7254 tbl->tbl = ALLOC_N(ID, tbl->capa);
7258 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7263#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7266vtable_free_gen(struct parser_params *p, int line, const char *name,
7271 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7274 if (!DVARS_TERMINAL_P(tbl)) {
7276 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7278 ruby_sized_xfree(tbl, sizeof(*tbl));
7281#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7284vtable_add_gen(struct parser_params *p, int line, const char *name,
7285 struct vtable *tbl, ID id)
7289 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7290 line, name, (void *)tbl, rb_id2name(id));
7293 if (DVARS_TERMINAL_P(tbl)) {
7294 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7297 if (tbl->pos == tbl->capa) {
7298 tbl->capa = tbl->capa * 2;
7299 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7301 tbl->tbl[tbl->pos++] = id;
7303#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7307vtable_pop_gen(struct parser_params *p, int line, const char *name,
7308 struct vtable *tbl, int n)
7311 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7312 line, name, (void *)tbl, n);
7315 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7320#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7324vtable_included(const struct vtable * tbl, ID id)
7328 if (!DVARS_TERMINAL_P(tbl)) {
7329 for (i = 0; i < tbl->pos; i++) {
7330 if (tbl->tbl[i] == id) {
7338static void parser_prepare(struct parser_params *p);
7341static NODE *parser_append_options(struct parser_params *p, NODE *node);
7344e_option_supplied(struct parser_params *p)
7346 return strcmp(p->ruby_sourcefile, "-e") == 0;
7350yycompile0(VALUE arg)
7354 struct parser_params *p = (struct parser_params *)arg;
7357 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
7358 if (p->debug_lines && p->ruby_sourceline > 0) {
7359 VALUE str = rb_default_rs;
7360 n = p->ruby_sourceline;
7362 rb_ary_push(p->debug_lines, str);
7366 if (!e_option_supplied(p)) {
7371 if (p->debug_lines) {
7372 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
7376#define RUBY_DTRACE_PARSE_HOOK(name) \
7377 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7378 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7380 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7382 RUBY_DTRACE_PARSE_HOOK(END);
7386 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7387 if (n || p->error_p) {
7388 VALUE mesg = p->error_buffer;
7390 mesg = syntax_error_new();
7392 if (!p->error_tolerant) {
7393 rb_set_errinfo(mesg);
7397 tree = p->eval_tree;
7399 tree = NEW_NIL(&NULL_LOC);
7402 VALUE tokens = p->tokens;
7404 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7405 prelude = block_append(p, p->eval_tree_begin, body);
7406 RNODE_SCOPE(tree)->nd_body = prelude;
7407 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7408 p->ast->body.coverage_enabled = cov;
7409 if (p->keep_tokens) {
7410 rb_obj_freeze(tokens);
7411 rb_ast_set_tokens(p->ast, tokens);
7414 p->ast->body.root = tree;
7415 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
7420yycompile(struct parser_params *p, VALUE fname, int line)
7424 p->ruby_sourcefile_string = Qnil;
7425 p->ruby_sourcefile = "(none)";
7428 p->ruby_sourcefile_string = rb_fstring(fname);
7429 p->ruby_sourcefile = StringValueCStr(fname);
7431 p->ruby_sourceline = line - 1;
7435 p->ast = ast = rb_ast_new();
7436 compile_callback(yycompile0, (VALUE)p);
7448must_be_ascii_compatible(struct parser_params *p, VALUE s)
7450 rb_encoding *enc = rb_enc_get(s);
7451 if (!rb_enc_asciicompat(enc)) {
7452 rb_raise(rb_eArgError, "invalid source encoding");
7458lex_get_str(struct parser_params *p, VALUE s)
7460 char *beg, *end, *start;
7463 beg = RSTRING_PTR(s);
7464 len = RSTRING_LEN(s);
7466 if (p->lex.gets_.ptr) {
7467 if (len == p->lex.gets_.ptr) return Qnil;
7468 beg += p->lex.gets_.ptr;
7469 len -= p->lex.gets_.ptr;
7471 end = memchr(beg, '\n', len);
7472 if (end) len = ++end - beg;
7473 p->lex.gets_.ptr += len;
7474 return rb_str_subseq(s, beg - start, len);
7478lex_getline(struct parser_params *p)
7480 VALUE line = (*p->lex.gets)(p, p->lex.input);
7481 if (NIL_P(line)) return line;
7482 must_be_ascii_compatible(p, line);
7483 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
7490parser_compile_string(rb_parser_t *p, VALUE fname, VALUE s, int line)
7492 p->lex.gets = lex_get_str;
7493 p->lex.gets_.ptr = 0;
7494 p->lex.input = rb_str_new_frozen(s);
7495 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7497 return yycompile(p, fname, line);
7501rb_ruby_parser_compile_string_path(rb_parser_t *p, VALUE f, VALUE s, int line)
7503 must_be_ascii_compatible(p, s);
7504 return parser_compile_string(p, f, s, line);
7508rb_ruby_parser_compile_string(rb_parser_t *p, const char *f, VALUE s, int line)
7510 return rb_ruby_parser_compile_string_path(p, rb_filesystem_str_new_cstr(f), s, line);
7514lex_io_gets(struct parser_params *p, VALUE io)
7516 return rb_io_gets_internal(io);
7520rb_ruby_parser_compile_file_path(rb_parser_t *p, VALUE fname, VALUE file, int start)
7522 p->lex.gets = lex_io_gets;
7523 p->lex.input = file;
7524 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7526 return yycompile(p, fname, start);
7530lex_generic_gets(struct parser_params *p, VALUE input)
7532 return (*p->lex.gets_.call)(input, p->line_count);
7536rb_ruby_parser_compile_generic(rb_parser_t *p, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
7538 p->lex.gets = lex_generic_gets;
7539 p->lex.gets_.call = lex_gets;
7540 p->lex.input = input;
7541 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7543 return yycompile(p, fname, start);
7547#define STR_FUNC_ESCAPE 0x01
7548#define STR_FUNC_EXPAND 0x02
7549#define STR_FUNC_REGEXP 0x04
7550#define STR_FUNC_QWORDS 0x08
7551#define STR_FUNC_SYMBOL 0x10
7552#define STR_FUNC_INDENT 0x20
7553#define STR_FUNC_LABEL 0x40
7554#define STR_FUNC_LIST 0x4000
7555#define STR_FUNC_TERM 0x8000
7558 str_label = STR_FUNC_LABEL,
7560 str_dquote = (STR_FUNC_EXPAND),
7561 str_xquote = (STR_FUNC_EXPAND),
7562 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7563 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7564 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7565 str_ssym = (STR_FUNC_SYMBOL),
7566 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7570parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7574 str = rb_enc_str_new(ptr, len, enc);
7575 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7576 if (is_ascii_string(str)) {
7578 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7579 rb_enc_associate(str, rb_ascii8bit_encoding());
7587strterm_is_heredoc(rb_strterm_t *strterm)
7589 return strterm->flags & STRTERM_HEREDOC;
7592static rb_strterm_t *
7593new_strterm(struct parser_params *p, int func, int term, int paren)
7595 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7596 strterm->u.literal.func = func;
7597 strterm->u.literal.term = term;
7598 strterm->u.literal.paren = paren;
7602static rb_strterm_t *
7603new_heredoc(struct parser_params *p)
7605 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7606 strterm->flags |= STRTERM_HEREDOC;
7610#define peek(p,c) peek_n(p, (c), 0)
7611#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7612#define peekc(p) peekc_n(p, 0)
7613#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7616add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7619 debug_token_line(p, "add_delayed_token", line);
7623 if (has_delayed_token(p)) {
7624 bool next_line = end_with_newline_p(p, p->delayed.token);
7625 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7626 int end_col = (next_line ? 0 : p->delayed.end_col);
7627 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7628 dispatch_delayed_token(p, tSTRING_CONTENT);
7631 if (!has_delayed_token(p)) {
7632 p->delayed.token = rb_str_buf_new(end - tok);
7633 rb_enc_associate(p->delayed.token, p->enc);
7634 p->delayed.beg_line = p->ruby_sourceline;
7635 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7637 rb_str_buf_cat(p->delayed.token, tok, end - tok);
7638 p->delayed.end_line = p->ruby_sourceline;
7639 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7645set_lastline(struct parser_params *p, VALUE v)
7647 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
7648 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
7649 p->lex.lastline = v;
7653nextline(struct parser_params *p, int set_encoding)
7655 VALUE v = p->lex.nextline;
7656 p->lex.nextline = 0;
7661 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7665 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
7672 if (p->debug_lines) {
7673 if (set_encoding) rb_enc_associate(v, p->enc);
7674 rb_ary_push(p->debug_lines, v);
7679 else if (NIL_P(v)) {
7680 /* after here-document without terminator */
7683 add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__);
7684 if (p->heredoc_end > 0) {
7685 p->ruby_sourceline = p->heredoc_end;
7688 p->ruby_sourceline++;
7695parser_cr(struct parser_params *p, int c)
7697 if (peek(p, '\n')) {
7705nextc0(struct parser_params *p, int set_encoding)
7709 if (UNLIKELY(lex_eol_p(p) || p->eofp || RTEST(p->lex.nextline))) {
7710 if (nextline(p, set_encoding)) return -1;
7712 c = (unsigned char)*p->lex.pcur++;
7713 if (UNLIKELY(c == '\r')) {
7714 c = parser_cr(p, c);
7719#define nextc(p) nextc0(p, TRUE)
7722pushback(struct parser_params *p, int c)
7724 if (c == -1) return;
7727 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7732#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7734#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7735#define tok(p) (p)->tokenbuf
7736#define toklen(p) (p)->tokidx
7739looking_at_eol_p(struct parser_params *p)
7741 const char *ptr = p->lex.pcur;
7742 while (!lex_eol_ptr_p(p, ptr)) {
7743 int c = (unsigned char)*ptr++;
7744 int eol = (c == '\n' || c == '#');
7745 if (eol || !ISSPACE(c)) {
7753newtok(struct parser_params *p)
7758 p->tokenbuf = ALLOC_N(char, 60);
7760 if (p->toksiz > 4096) {
7762 REALLOC_N(p->tokenbuf, char, 60);
7768tokspace(struct parser_params *p, int n)
7772 if (p->tokidx >= p->toksiz) {
7773 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7774 REALLOC_N(p->tokenbuf, char, p->toksiz);
7776 return &p->tokenbuf[p->tokidx-n];
7780tokadd(struct parser_params *p, int c)
7782 p->tokenbuf[p->tokidx++] = (char)c;
7783 if (p->tokidx >= p->toksiz) {
7785 REALLOC_N(p->tokenbuf, char, p->toksiz);
7790tok_hex(struct parser_params *p, size_t *numlen)
7794 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7796 yyerror0("invalid hex escape");
7797 dispatch_scan_event(p, tSTRING_CONTENT);
7800 p->lex.pcur += *numlen;
7804#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7807escaped_control_code(int c)
7833#define WARN_SPACE_CHAR(c, prefix) \
7834 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
7837tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7838 int regexp_literal, int wide)
7841 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 (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7847 literal_flush(p, p->lex.pcur);
7848 yyerror0("invalid Unicode escape");
7849 return wide && numlen > 0;
7851 if (codepoint > 0x10ffff) {
7852 literal_flush(p, p->lex.pcur);
7853 yyerror0("invalid Unicode codepoint (too large)");
7856 if ((codepoint & 0xfffff800) == 0xd800) {
7857 literal_flush(p, p->lex.pcur);
7858 yyerror0("invalid Unicode codepoint");
7862 if (regexp_literal) {
7863 tokcopy(p, (int)numlen);
7865 else if (codepoint >= 0x80) {
7866 rb_encoding *utf8 = rb_utf8_encoding();
7867 if (*encp && utf8 != *encp) {
7868 YYLTYPE loc = RUBY_INIT_YYLLOC();
7869 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7870 parser_show_error_line(p, &loc);
7874 tokaddmbc(p, codepoint, *encp);
7877 tokadd(p, codepoint);
7882static int tokadd_mbchar(struct parser_params *p, int c);
7885tokskip_mbchar(struct parser_params *p)
7887 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7889 p->lex.pcur += len - 1;
7894/* return value is for ?\u3042 */
7896tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7897 int term, int symbol_literal, int regexp_literal)
7900 * If `term` is not -1, then we allow multiple codepoints in \u{}
7901 * upto `term` byte, otherwise we're parsing a character literal.
7902 * And then add the codepoints to the current token.
7904 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7906 const int open_brace = '{', close_brace = '}';
7908 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7910 if (peek(p, open_brace)) { /* handle \u{...} form */
7911 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7913 * Skip parsing validation code and copy bytes as-is until term or
7914 * closing brace, in order to correctly handle extended regexps where
7915 * invalid unicode escapes are allowed in comments. The regexp parser
7916 * does its own validation and will catch any issues.
7918 tokadd(p, open_brace);
7919 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7921 if (c == close_brace) {
7926 else if (c == term) {
7929 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7933 tokadd_mbchar(p, c);
7937 const char *second = NULL;
7938 int c, last = nextc(p);
7939 if (lex_eol_p(p)) goto unterminated;
7940 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7941 while (c != close_brace) {
7942 if (c == term) goto unterminated;
7943 if (second == multiple_codepoints)
7944 second = p->lex.pcur;
7945 if (regexp_literal) tokadd(p, last);
7946 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7949 while (ISSPACE(c = peekc(p))) {
7950 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7953 if (term == -1 && !second)
7954 second = multiple_codepoints;
7957 if (c != close_brace) {
7960 yyerror0("unterminated Unicode escape");
7963 if (second && second != multiple_codepoints) {
7964 const char *pcur = p->lex.pcur;
7965 p->lex.pcur = second;
7966 dispatch_scan_event(p, tSTRING_CONTENT);
7969 yyerror0(multiple_codepoints);
7973 if (regexp_literal) tokadd(p, close_brace);
7977 else { /* handle \uxxxx form */
7978 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7985#define ESCAPE_CONTROL 1
7986#define ESCAPE_META 2
7989read_escape(struct parser_params *p, int flags)
7994 switch (c = nextc(p)) {
7995 case '\\': /* Backslash */
7998 case 'n': /* newline */
8001 case 't': /* horizontal tab */
8004 case 'r': /* carriage-return */
8007 case 'f': /* form-feed */
8010 case 'v': /* vertical tab */
8013 case 'a': /* alarm(bell) */
8016 case 'e': /* escape */
8019 case '0': case '1': case '2': case '3': /* octal constant */
8020 case '4': case '5': case '6': case '7':
8022 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8023 p->lex.pcur += numlen;
8026 case 'x': /* hex constant */
8027 c = tok_hex(p, &numlen);
8028 if (numlen == 0) return 0;
8031 case 'b': /* backspace */
8034 case 's': /* space */
8038 if (flags & ESCAPE_META) goto eof;
8039 if ((c = nextc(p)) != '-') {
8042 if ((c = nextc(p)) == '\\') {
8048 return read_escape(p, flags|ESCAPE_META) | 0x80;
8050 else if (c == -1 || !ISASCII(c)) goto eof;
8052 int c2 = escaped_control_code(c);
8054 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8055 WARN_SPACE_CHAR(c2, "\\M-");
8058 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8061 else if (ISCNTRL(c)) goto eof;
8062 return ((c & 0xff) | 0x80);
8066 if ((c = nextc(p)) != '-') {
8070 if (flags & ESCAPE_CONTROL) goto eof;
8071 if ((c = nextc(p))== '\\') {
8077 c = read_escape(p, flags|ESCAPE_CONTROL);
8081 else if (c == -1) goto eof;
8082 else if (!ISASCII(c)) {
8087 int c2 = escaped_control_code(c);
8090 if (flags & ESCAPE_META) {
8091 WARN_SPACE_CHAR(c2, "\\M-");
8094 WARN_SPACE_CHAR(c2, "");
8098 if (flags & ESCAPE_META) {
8099 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8102 WARN_SPACE_CHAR(c2, "\\C-");
8106 else if (ISCNTRL(c)) goto eof;
8112 yyerror0("Invalid escape character syntax");
8113 dispatch_scan_event(p, tSTRING_CONTENT);
8122tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8124 int len = rb_enc_codelen(c, enc);
8125 rb_enc_mbcput(c, tokspace(p, len), enc);
8129tokadd_escape(struct parser_params *p)
8134 switch (c = nextc(p)) {
8136 return 0; /* just ignore */
8138 case '0': case '1': case '2': case '3': /* octal constant */
8139 case '4': case '5': case '6': case '7':
8141 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8142 if (numlen == 0) goto eof;
8143 p->lex.pcur += numlen;
8144 tokcopy(p, (int)numlen + 1);
8148 case 'x': /* hex constant */
8150 tok_hex(p, &numlen);
8151 if (numlen == 0) return -1;
8152 tokcopy(p, (int)numlen + 2);
8158 yyerror0("Invalid escape character syntax");
8170regx_options(struct parser_params *p)
8178 while (c = nextc(p), ISALPHA(c)) {
8180 options |= RE_OPTION_ONCE;
8182 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
8184 if (kc != rb_ascii8bit_encindex()) kcode = c;
8198 YYLTYPE loc = RUBY_INIT_YYLLOC();
8200 compile_error(p, "unknown regexp option%s - %*s",
8201 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8202 parser_show_error_line(p, &loc);
8204 return options | RE_OPTION_ENCODING(kcode);
8208tokadd_mbchar(struct parser_params *p, int c)
8210 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8211 if (len < 0) return -1;
8213 p->lex.pcur += --len;
8214 if (len > 0) tokcopy(p, len);
8219simple_re_meta(int c)
8222 case '$': case '*': case '+': case '.':
8223 case '?': case '^': case '|':
8224 case ')': case ']': case '}': case '>':
8232parser_update_heredoc_indent(struct parser_params *p, int c)
8234 if (p->heredoc_line_indent == -1) {
8235 if (c == '\n') p->heredoc_line_indent = 0;
8239 p->heredoc_line_indent++;
8242 else if (c == '\t') {
8243 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8244 p->heredoc_line_indent = w * TAB_WIDTH;
8247 else if (c != '\n') {
8248 if (p->heredoc_indent > p->heredoc_line_indent) {
8249 p->heredoc_indent = p->heredoc_line_indent;
8251 p->heredoc_line_indent = -1;
8258parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8260 YYLTYPE loc = RUBY_INIT_YYLLOC();
8261 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8262 compile_error(p, "%s mixed within %s source", n1, n2);
8263 parser_show_error_line(p, &loc);
8267parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8269 const char *pos = p->lex.pcur;
8271 parser_mixed_error(p, enc1, enc2);
8276nibble_char_upper(unsigned int c)
8279 return c + (c < 10 ? '0' : 'A' - 10);
8283tokadd_string(struct parser_params *p,
8284 int func, int term, int paren, long *nest,
8285 rb_encoding **encp, rb_encoding **enc)
8290 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8291 int top_of_line = FALSE;
8294#define mixed_error(enc1, enc2) \
8295 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8296#define mixed_escape(beg, enc1, enc2) \
8297 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8299 while ((c = nextc(p)) != -1) {
8300 if (p->heredoc_indent > 0) {
8301 parser_update_heredoc_indent(p, c);
8304 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8310 if (paren && c == paren) {
8313 else if (c == term) {
8314 if (!nest || !*nest) {
8320 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8321 unsigned char c2 = *p->lex.pcur;
8322 if (c2 == '$' || c2 == '@' || c2 == '{') {
8327 else if (c == '\\') {
8331 if (func & STR_FUNC_QWORDS) break;
8332 if (func & STR_FUNC_EXPAND) {
8333 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8344 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8348 if ((func & STR_FUNC_EXPAND) == 0) {
8352 tokadd_utf8(p, enc, term,
8353 func & STR_FUNC_SYMBOL,
8354 func & STR_FUNC_REGEXP);
8358 if (c == -1) return -1;
8360 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8363 if (func & STR_FUNC_REGEXP) {
8369 c = read_escape(p, 0);
8371 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8374 *t++ = nibble_char_upper(c >> 4);
8375 *t++ = nibble_char_upper(c);
8380 if (c == term && !simple_re_meta(c)) {
8385 if ((c = tokadd_escape(p)) < 0)
8387 if (*enc && *enc != *encp) {
8388 mixed_escape(p->lex.ptok+2, *enc, *encp);
8392 else if (func & STR_FUNC_EXPAND) {
8394 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8395 c = read_escape(p, 0);
8397 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8398 /* ignore backslashed spaces in %w */
8400 else if (c != term && !(paren && c == paren)) {
8407 else if (!parser_isascii(p)) {
8412 else if (*enc != *encp) {
8413 mixed_error(*enc, *encp);
8416 if (tokadd_mbchar(p, c) == -1) return -1;
8419 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8427 else if (*enc != *encp) {
8428 mixed_error(*enc, *encp);
8434 top_of_line = (c == '\n');
8438 if (*enc) *encp = *enc;
8442#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8446flush_string_content(struct parser_params *p, rb_encoding *enc)
8448 VALUE content = yylval.val;
8449 if (!ripper_is_node_yylval(p, content))
8450 content = ripper_new_yylval(p, 0, 0, content);
8451 if (has_delayed_token(p)) {
8452 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8454 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8456 dispatch_delayed_token(p, tSTRING_CONTENT);
8457 p->lex.ptok = p->lex.pcur;
8458 RNODE_RIPPER(content)->nd_rval = yylval.val;
8460 dispatch_scan_event(p, tSTRING_CONTENT);
8461 if (yylval.val != content)
8462 RNODE_RIPPER(content)->nd_rval = yylval.val;
8463 yylval.val = content;
8467flush_string_content(struct parser_params *p, rb_encoding *enc)
8469 if (has_delayed_token(p)) {
8470 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8472 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8473 p->delayed.end_line = p->ruby_sourceline;
8474 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8476 dispatch_delayed_token(p, tSTRING_CONTENT);
8477 p->lex.ptok = p->lex.pcur;
8479 dispatch_scan_event(p, tSTRING_CONTENT);
8483RUBY_FUNC_EXPORTED const uint_least32_t ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
8484/* this can be shared with ripper, since it's independent from struct
8487#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8488#define SPECIAL_PUNCT(idx) ( \
8489 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8490 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8491 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8492 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8493 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8495const uint_least32_t ruby_global_name_punct_bits[] = {
8504static enum yytokentype
8505parser_peek_variable_name(struct parser_params *p)
8508 const char *ptr = p->lex.pcur;
8510 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8514 if ((c = *ptr) == '-') {
8515 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8518 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8519 return tSTRING_DVAR;
8523 if ((c = *ptr) == '@') {
8524 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8530 p->command_start = TRUE;
8531 return tSTRING_DBEG;
8535 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8536 return tSTRING_DVAR;
8540#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8541#define IS_END() IS_lex_state(EXPR_END_ANY)
8542#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8543#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8544#define IS_LABEL_POSSIBLE() (\
8545 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8547#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8548#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8550static inline enum yytokentype
8551parser_string_term(struct parser_params *p, int func)
8553 xfree(p->lex.strterm);
8555 if (func & STR_FUNC_REGEXP) {
8556 set_yylval_num(regx_options(p));
8557 dispatch_scan_event(p, tREGEXP_END);
8558 SET_LEX_STATE(EXPR_END);
8561 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8563 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8566 SET_LEX_STATE(EXPR_END);
8570static enum yytokentype
8571parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8573 int func = quote->func;
8574 int term = quote->term;
8575 int paren = quote->paren;
8577 rb_encoding *enc = p->enc;
8578 rb_encoding *base_enc = 0;
8581 if (func & STR_FUNC_TERM) {
8582 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8583 SET_LEX_STATE(EXPR_END);
8584 xfree(p->lex.strterm);
8586 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8589 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8590 while (c != '\n' && ISSPACE(c = nextc(p)));
8593 if (func & STR_FUNC_LIST) {
8594 quote->func &= ~STR_FUNC_LIST;
8597 if (c == term && !quote->nest) {
8598 if (func & STR_FUNC_QWORDS) {
8599 quote->func |= STR_FUNC_TERM;
8600 pushback(p, c); /* dispatch the term at tSTRING_END */
8601 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8604 return parser_string_term(p, func);
8607 if (!ISSPACE(c)) pushback(p, c);
8608 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8612 if ((func & STR_FUNC_EXPAND) && c == '#') {
8613 enum yytokentype t = parser_peek_variable_name(p);
8619 if (tokadd_string(p, func, term, paren, "e->nest,
8620 &enc, &base_enc) == -1) {
8623# define unterminated_literal(mesg) yyerror0(mesg)
8625# define unterminated_literal(mesg) compile_error(p, mesg)
8627 literal_flush(p, p->lex.pcur);
8628 if (func & STR_FUNC_QWORDS) {
8629 /* no content to add, bailing out here */
8630 unterminated_literal("unterminated list meets end of file");
8631 xfree(p->lex.strterm);
8635 if (func & STR_FUNC_REGEXP) {
8636 unterminated_literal("unterminated regexp meets end of file");
8639 unterminated_literal("unterminated string meets end of file");
8641 quote->func |= STR_FUNC_TERM;
8646 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8647 set_yylval_str(lit);
8648 flush_string_content(p, enc);
8650 return tSTRING_CONTENT;
8653static enum yytokentype
8654heredoc_identifier(struct parser_params *p)
8657 * term_len is length of `<<"END"` except `END`,
8658 * in this case term_len is 4 (<, <, " and ").
8660 long len, offset = p->lex.pcur - p->lex.pbeg;
8661 int c = nextc(p), term, func = 0, quote = 0;
8662 enum yytokentype token = tSTRING_BEG;
8667 func = STR_FUNC_INDENT;
8670 else if (c == '~') {
8672 func = STR_FUNC_INDENT;
8678 func |= str_squote; goto quoted;
8680 func |= str_dquote; goto quoted;
8682 token = tXSTRING_BEG;
8683 func |= str_xquote; goto quoted;
8690 while ((c = nextc(p)) != term) {
8691 if (c == -1 || c == '\r' || c == '\n') {
8692 yyerror0("unterminated here document identifier");
8699 if (!parser_is_identchar(p)) {
8701 if (func & STR_FUNC_INDENT) {
8702 pushback(p, indent > 0 ? '~' : '-');
8708 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8709 if (n < 0) return 0;
8711 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8716 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8717 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8718 yyerror0("too long here document identifier");
8719 dispatch_scan_event(p, tHEREDOC_BEG);
8722 p->lex.strterm = new_heredoc(p);
8723 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8724 here->offset = offset;
8725 here->sourceline = p->ruby_sourceline;
8726 here->length = (unsigned)len;
8727 here->quote = quote;
8729 here->lastline = p->lex.lastline;
8730 rb_ast_add_mark_object(p->ast, p->lex.lastline);
8733 p->heredoc_indent = indent;
8734 p->heredoc_line_indent = 0;
8739heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8742 rb_strterm_t *term = p->lex.strterm;
8745 line = here->lastline;
8746 p->lex.lastline = line;
8747 p->lex.pbeg = RSTRING_PTR(line);
8748 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8749 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8750 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8751 p->heredoc_end = p->ruby_sourceline;
8752 p->ruby_sourceline = (int)here->sourceline;
8753 if (p->eofp) p->lex.nextline = Qnil;
8756 rb_ast_delete_mark_object(p->ast, line);
8760dedent_string(struct parser_params *p, VALUE string, int width)
8766 RSTRING_GETMEM(string, str, len);
8767 for (i = 0; i < len && col < width; i++) {
8768 if (str[i] == ' ') {
8771 else if (str[i] == '\t') {
8772 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8773 if (n > width) break;
8781 rb_str_modify(string);
8782 str = RSTRING_PTR(string);
8783 if (RSTRING_LEN(string) != len)
8784 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8785 MEMMOVE(str, str + i, char, len - i);
8786 rb_str_set_len(string, len - i);
8792heredoc_dedent(struct parser_params *p, NODE *root)
8794 NODE *node, *str_node, *prev_node;
8795 int indent = p->heredoc_indent;
8798 if (indent <= 0) return root;
8799 p->heredoc_indent = 0;
8800 if (!root) return root;
8802 prev_node = node = str_node = root;
8803 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8806 VALUE lit = RNODE_LIT(str_node)->nd_lit;
8807 if (nd_fl_newline(str_node)) {
8808 dedent_string(p, lit, indent);
8813 else if (!literal_concat0(p, prev_lit, lit)) {
8817 NODE *end = RNODE_LIST(node)->as.nd_end;
8818 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8820 if (nd_type_p(prev_node, NODE_DSTR))
8821 nd_set_type(prev_node, NODE_STR);
8824 RNODE_LIST(node)->as.nd_end = end;
8829 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8831 if (!nd_type_p(node, NODE_LIST)) break;
8832 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8833 enum node_type type = nd_type(str_node);
8834 if (type == NODE_STR || type == NODE_DSTR) break;
8844heredoc_dedent(struct parser_params *p, VALUE array)
8846 int indent = p->heredoc_indent;
8848 if (indent <= 0) return array;
8849 p->heredoc_indent = 0;
8850 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8856whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8858 const char *beg = p->lex.pbeg;
8859 const char *ptr = p->lex.pend;
8861 if (ptr - beg < len) return FALSE;
8862 if (ptr > beg && ptr[-1] == '\n') {
8863 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8864 if (ptr - beg < len) return FALSE;
8866 if (strncmp(eos, ptr -= len, len)) return FALSE;
8868 while (beg < ptr && ISSPACE(*beg)) beg++;
8874word_match_p(struct parser_params *p, const char *word, long len)
8876 if (strncmp(p->lex.pcur, word, len)) return 0;
8877 if (lex_eol_n_p(p, len)) return 1;
8878 int c = (unsigned char)p->lex.pcur[len];
8879 if (ISSPACE(c)) return 1;
8881 case '\0': case '\004': case '\032': return 1;
8886#define NUM_SUFFIX_R (1<<0)
8887#define NUM_SUFFIX_I (1<<1)
8888#define NUM_SUFFIX_ALL 3
8891number_literal_suffix(struct parser_params *p, int mask)
8894 const char *lastp = p->lex.pcur;
8896 while ((c = nextc(p)) != -1) {
8897 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8898 result |= (mask & NUM_SUFFIX_I);
8899 mask &= ~NUM_SUFFIX_I;
8900 /* r after i, rational of complex is disallowed */
8901 mask &= ~NUM_SUFFIX_R;
8904 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8905 result |= (mask & NUM_SUFFIX_R);
8906 mask &= ~NUM_SUFFIX_R;
8909 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8910 p->lex.pcur = lastp;
8911 literal_flush(p, p->lex.pcur);
8920static enum yytokentype
8921set_number_literal(struct parser_params *p, VALUE v,
8922 enum yytokentype type, int suffix)
8924 if (suffix & NUM_SUFFIX_I) {
8925 v = rb_complex_raw(INT2FIX(0), v);
8928 set_yylval_literal(v);
8929 SET_LEX_STATE(EXPR_END);
8933static enum yytokentype
8934set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8936 enum yytokentype type = tINTEGER;
8937 if (suffix & NUM_SUFFIX_R) {
8938 v = rb_rational_raw1(v);
8941 return set_number_literal(p, v, type, suffix);
8946dispatch_heredoc_end(struct parser_params *p)
8949 if (has_delayed_token(p))
8950 dispatch_delayed_token(p, tSTRING_CONTENT);
8951 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8952 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8953 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8959#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8961parser_dispatch_heredoc_end(struct parser_params *p, int line)
8963 if (has_delayed_token(p))
8964 dispatch_delayed_token(p, tSTRING_CONTENT);
8966 if (p->keep_tokens) {
8967 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8968 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8969 parser_append_tokens(p, str, tHEREDOC_END, line);
8972 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8978static enum yytokentype
8979here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8981 int c, func, indent = 0;
8982 const char *eos, *ptr, *ptr_end;
8985 rb_encoding *enc = p->enc;
8986 rb_encoding *base_enc = 0;
8989 eos = RSTRING_PTR(here->lastline) + here->offset;
8991 indent = (func = here->func) & STR_FUNC_INDENT;
8993 if ((c = nextc(p)) == -1) {
8996 if (!has_delayed_token(p)) {
8997 dispatch_scan_event(p, tSTRING_CONTENT);
9000 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9001 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
9002 int cr = ENC_CODERANGE_UNKNOWN;
9003 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9004 if (cr != ENC_CODERANGE_7BIT &&
9005 rb_is_usascii_enc(p->enc) &&
9006 enc != rb_utf8_encoding()) {
9007 enc = rb_ascii8bit_encoding();
9010 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
9012 dispatch_delayed_token(p, tSTRING_CONTENT);
9016 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9017 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9020 SET_LEX_STATE(EXPR_END);
9025 /* not beginning of line, cannot be the terminator */
9027 else if (p->heredoc_line_indent == -1) {
9028 /* `heredoc_line_indent == -1` means
9029 * - "after an interpolation in the same line", or
9030 * - "in a continuing line"
9032 p->heredoc_line_indent = 0;
9034 else if (whole_match_p(p, eos, len, indent)) {
9035 dispatch_heredoc_end(p);
9037 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9039 SET_LEX_STATE(EXPR_END);
9043 if (!(func & STR_FUNC_EXPAND)) {
9045 ptr = RSTRING_PTR(p->lex.lastline);
9046 ptr_end = p->lex.pend;
9047 if (ptr_end > ptr) {
9048 switch (ptr_end[-1]) {
9050 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9059 if (p->heredoc_indent > 0) {
9061 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9063 p->heredoc_line_indent = 0;
9067 rb_str_cat(str, ptr, ptr_end - ptr);
9069 str = STR_NEW(ptr, ptr_end - ptr);
9070 if (!lex_eol_ptr_p(p, ptr_end)) rb_str_cat(str, "\n", 1);
9072 if (p->heredoc_indent > 0) {
9075 if (nextc(p) == -1) {
9081 } while (!whole_match_p(p, eos, len, indent));
9084 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9087 enum yytokentype t = parser_peek_variable_name(p);
9088 if (p->heredoc_line_indent != -1) {
9089 if (p->heredoc_indent > p->heredoc_line_indent) {
9090 p->heredoc_indent = p->heredoc_line_indent;
9092 p->heredoc_line_indent = -1;
9101 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9102 if (p->eofp) goto error;
9106 if (c == '\\') p->heredoc_line_indent = -1;
9108 str = STR_NEW3(tok(p), toklen(p), enc, func);
9110 set_yylval_str(str);
9112 if (bol) nd_set_fl_newline(yylval.node);
9114 flush_string_content(p, enc);
9115 return tSTRING_CONTENT;
9117 tokadd(p, nextc(p));
9118 if (p->heredoc_indent > 0) {
9122 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9123 if ((c = nextc(p)) == -1) goto error;
9124 } while (!whole_match_p(p, eos, len, indent));
9125 str = STR_NEW3(tok(p), toklen(p), enc, func);
9127 dispatch_heredoc_end(p);
9129 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
9132 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9134 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9135 set_yylval_str(str);
9137 if (bol) nd_set_fl_newline(yylval.node);
9139 return tSTRING_CONTENT;
9145arg_ambiguous(struct parser_params *p, char c)
9149 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
9152 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
9155 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9162formal_argument(struct parser_params *p, ID lhs)
9164formal_argument(struct parser_params *p, VALUE lhs)
9167 ID id = get_id(lhs);
9169 switch (id_type(id)) {
9173# define ERR(mesg) yyerror0(mesg)
9175# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
9178 ERR("formal argument cannot be a constant");
9181 ERR("formal argument cannot be an instance variable");
9184 ERR("formal argument cannot be a global variable");
9187 ERR("formal argument cannot be a class variable");
9190 ERR("formal argument must be local variable");
9194 shadowing_lvar(p, id);
9199lvar_defined(struct parser_params *p, ID id)
9201 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9204/* emacsen -*- hack */
9206parser_encode_length(struct parser_params *p, const char *name, long len)
9210 if (len > 5 && name[nlen = len - 5] == '-') {
9211 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9214 if (len > 4 && name[nlen = len - 4] == '-') {
9215 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9217 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9218 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9219 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9226parser_set_encode(struct parser_params *p, const char *name)
9228 int idx = rb_enc_find_index(name);
9233 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9235 excargs[0] = rb_eArgError;
9236 excargs[2] = rb_make_backtrace();
9237 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9238 rb_exc_raise(rb_make_exception(3, excargs));
9240 enc = rb_enc_from_index(idx);
9241 if (!rb_enc_asciicompat(enc)) {
9242 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9247 if (p->debug_lines) {
9248 VALUE lines = p->debug_lines;
9249 long i, n = RARRAY_LEN(lines);
9250 for (i = 0; i < n; ++i) {
9251 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
9258comment_at_top(struct parser_params *p)
9260 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
9261 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
9262 while (ptr < ptr_end) {
9263 if (!ISSPACE(*ptr)) return 0;
9269typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9270typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9272static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9275magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9277 if (!comment_at_top(p)) {
9280 parser_set_encode(p, val);
9284parser_get_bool(struct parser_params *p, const char *name, const char *val)
9288 if (STRCASECMP(val, "true") == 0) {
9293 if (STRCASECMP(val, "false") == 0) {
9298 return parser_invalid_pragma_value(p, name, val);
9302parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9304 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9309parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9311 int b = parser_get_bool(p, name, val);
9312 if (b >= 0) p->token_info_enabled = b;
9316parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9320 if (p->token_seen) {
9321 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
9325 b = parser_get_bool(p, name, val);
9328 p->frozen_string_literal = b;
9332parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9334 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9335 if (*s == ' ' || *s == '\t') continue;
9336 if (*s == '#') break;
9337 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
9343 if (STRCASECMP(val, "none") == 0) {
9344 p->ctxt.shareable_constant_value = shareable_none;
9349 if (STRCASECMP(val, "literal") == 0) {
9350 p->ctxt.shareable_constant_value = shareable_literal;
9355 if (STRCASECMP(val, "experimental_copy") == 0) {
9356 p->ctxt.shareable_constant_value = shareable_copy;
9359 if (STRCASECMP(val, "experimental_everything") == 0) {
9360 p->ctxt.shareable_constant_value = shareable_everything;
9365 parser_invalid_pragma_value(p, name, val);
9370parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9372 int b = parser_get_bool(p, name, val);
9373 if (b >= 0) p->past_scope_enabled = b;
9377struct magic_comment {
9379 rb_magic_comment_setter_t func;
9380 rb_magic_comment_length_t length;
9383static const struct magic_comment magic_comments[] = {
9384 {"coding", magic_comment_encoding, parser_encode_length},
9385 {"encoding", magic_comment_encoding, parser_encode_length},
9386 {"frozen_string_literal", parser_set_frozen_string_literal},
9387 {"shareable_constant_value", parser_set_shareable_constant_value},
9388 {"warn_indent", parser_set_token_info},
9390 {"warn_past_scope", parser_set_past_scope},
9395magic_comment_marker(const char *str, long len)
9402 if (str[i-1] == '*' && str[i-2] == '-') {
9408 if (i + 1 >= len) return 0;
9409 if (str[i+1] != '-') {
9412 else if (str[i-1] != '-') {
9428parser_magic_comment(struct parser_params *p, const char *str, long len)
9431 VALUE name = 0, val = 0;
9432 const char *beg, *end, *vbeg, *vend;
9433#define str_copy(_s, _p, _n) ((_s) \
9434 ? (void)(rb_str_resize((_s), (_n)), \
9435 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9436 : (void)((_s) = STR_NEW((_p), (_n))))
9438 if (len <= 7) return FALSE;
9439 if (!!(beg = magic_comment_marker(str, len))) {
9440 if (!(end = magic_comment_marker(beg, str + len - beg)))
9444 len = end - beg - 3;
9447 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9449 const struct magic_comment *mc = magic_comments;
9454 for (; len > 0 && *str; str++, --len) {
9456 case '\'': case '"': case ':': case ';':
9459 if (!ISSPACE(*str)) break;
9461 for (beg = str; len > 0; str++, --len) {
9463 case '\'': case '"': case ':': case ';':
9466 if (ISSPACE(*str)) break;
9471 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9474 if (!indicator) return FALSE;
9478 do str++; while (--len > 0 && ISSPACE(*str));
9481 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9494 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9498 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9501 while (len > 0 && (ISSPACE(*str))) --len, str++;
9502 if (len) return FALSE;
9506 str_copy(name, beg, n);
9507 s = RSTRING_PTR(name);
9508 for (i = 0; i < n; ++i) {
9509 if (s[i] == '-') s[i] = '_';
9512 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9515 n = (*mc->length)(p, vbeg, n);
9517 str_copy(val, vbeg, n);
9518 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9521 } while (++mc < magic_comments + numberof(magic_comments));
9523 str_copy(val, vbeg, vend - vbeg);
9524 dispatch2(magic_comment, name, val);
9532set_file_encoding(struct parser_params *p, const char *str, const char *send)
9535 const char *beg = str;
9539 if (send - str <= 6) return;
9541 case 'C': case 'c': str += 6; continue;
9542 case 'O': case 'o': str += 5; continue;
9543 case 'D': case 'd': str += 4; continue;
9544 case 'I': case 'i': str += 3; continue;
9545 case 'N': case 'n': str += 2; continue;
9546 case 'G': case 'g': str += 1; continue;
9553 if (ISSPACE(*str)) break;
9556 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9561 if (++str >= send) return;
9562 } while (ISSPACE(*str));
9564 if (*str != '=' && *str != ':') return;
9569 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9570 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9571 parser_set_encode(p, RSTRING_PTR(s));
9572 rb_str_resize(s, 0);
9576parser_prepare(struct parser_params *p)
9578 int c = nextc0(p, FALSE);
9579 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9582 if (peek(p, '!')) p->has_shebang = 1;
9584 case 0xef: /* UTF-8 BOM marker */
9585 if (!lex_eol_n_p(p, 2) &&
9586 (unsigned char)p->lex.pcur[0] == 0xbb &&
9587 (unsigned char)p->lex.pcur[1] == 0xbf) {
9588 p->enc = rb_utf8_encoding();
9591 if (p->debug_lines) {
9592 rb_enc_associate(p->lex.lastline, p->enc);
9595 p->lex.pbeg = p->lex.pcur;
9604 p->enc = rb_enc_get(p->lex.lastline);
9608#define ambiguous_operator(tok, op, syn) ( \
9609 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
9610 rb_warning0("even though it seems like "syn""))
9612#define ambiguous_operator(tok, op, syn) \
9613 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9615#define warn_balanced(tok, op, syn) ((void) \
9616 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9617 space_seen && !ISSPACE(c) && \
9618 (ambiguous_operator(tok, op, syn), 0)), \
9619 (enum yytokentype)(tok))
9622parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9625 char *point = &str[seen_point];
9626 size_t fraclen = len-seen_point-1;
9627 memmove(point, point+1, fraclen+1);
9628 v = rb_cstr_to_inum(str, 10, FALSE);
9629 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9632static enum yytokentype
9633no_digits(struct parser_params *p)
9635 yyerror0("numeric literal without digits");
9636 if (peek(p, '_')) nextc(p);
9637 /* dummy 0, for tUMINUS_NUM at numeric */
9638 return set_integer_literal(p, INT2FIX(0), 0);
9641static enum yytokentype
9642parse_numeric(struct parser_params *p, int c)
9644 int is_float, seen_point, seen_e, nondigit;
9647 is_float = seen_point = seen_e = nondigit = 0;
9648 SET_LEX_STATE(EXPR_END);
9650 if (c == '-' || c == '+') {
9655 int start = toklen(p);
9657 if (c == 'x' || c == 'X') {
9660 if (c != -1 && ISXDIGIT(c)) {
9663 if (nondigit) break;
9667 if (!ISXDIGIT(c)) break;
9670 } while ((c = nextc(p)) != -1);
9674 if (toklen(p) == start) {
9675 return no_digits(p);
9677 else if (nondigit) goto trailing_uc;
9678 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9679 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9681 if (c == 'b' || c == 'B') {
9684 if (c == '0' || c == '1') {
9687 if (nondigit) break;
9691 if (c != '0' && c != '1') break;
9694 } while ((c = nextc(p)) != -1);
9698 if (toklen(p) == start) {
9699 return no_digits(p);
9701 else if (nondigit) goto trailing_uc;
9702 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9703 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9705 if (c == 'd' || c == 'D') {
9708 if (c != -1 && ISDIGIT(c)) {
9711 if (nondigit) break;
9715 if (!ISDIGIT(c)) break;
9718 } while ((c = nextc(p)) != -1);
9722 if (toklen(p) == start) {
9723 return no_digits(p);
9725 else if (nondigit) goto trailing_uc;
9726 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9727 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9733 if (c == 'o' || c == 'O') {
9734 /* prefixed octal */
9736 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9737 return no_digits(p);
9740 if (c >= '0' && c <= '7') {
9745 if (nondigit) break;
9749 if (c < '0' || c > '9') break;
9750 if (c > '7') goto invalid_octal;
9753 } while ((c = nextc(p)) != -1);
9754 if (toklen(p) > start) {
9757 if (nondigit) goto trailing_uc;
9758 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9759 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9766 if (c > '7' && c <= '9') {
9768 yyerror0("Invalid octal digit");
9770 else if (c == '.' || c == 'e' || c == 'E') {
9775 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9776 return set_integer_literal(p, INT2FIX(0), suffix);
9782 case '0': case '1': case '2': case '3': case '4':
9783 case '5': case '6': case '7': case '8': case '9':
9789 if (nondigit) goto trailing_uc;
9790 if (seen_point || seen_e) {
9795 if (c0 == -1 || !ISDIGIT(c0)) {
9801 seen_point = toklen(p);
9820 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9826 tokadd(p, nondigit);
9830 nondigit = (c == '-' || c == '+') ? c : 0;
9833 case '_': /* `_' in number just ignored */
9834 if (nondigit) goto decode_num;
9848 literal_flush(p, p->lex.pcur - 1);
9849 YYLTYPE loc = RUBY_INIT_YYLLOC();
9850 compile_error(p, "trailing `%c' in number", nondigit);
9851 parser_show_error_line(p, &loc);
9855 enum yytokentype type = tFLOAT;
9858 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9859 if (suffix & NUM_SUFFIX_R) {
9861 v = parse_rational(p, tok(p), toklen(p), seen_point);
9864 double d = strtod(tok(p), 0);
9865 if (errno == ERANGE) {
9866 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9871 return set_number_literal(p, v, type, suffix);
9873 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9874 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9877static enum yytokentype
9878parse_qmark(struct parser_params *p, int space_seen)
9885 SET_LEX_STATE(EXPR_VALUE);
9890 compile_error(p, "incomplete character syntax");
9893 if (rb_enc_isspace(c, p->enc)) {
9895 int c2 = escaped_control_code(c);
9897 WARN_SPACE_CHAR(c2, "?");
9902 SET_LEX_STATE(EXPR_VALUE);
9907 if (!parser_isascii(p)) {
9908 if (tokadd_mbchar(p, c) == -1) return 0;
9910 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9911 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
9913 const char *start = p->lex.pcur - 1, *ptr = start;
9915 int n = parser_precise_mbclen(p, ptr);
9916 if (n < 0) return -1;
9918 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9919 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9920 " a conditional operator, put a space after `?'",
9921 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9925 else if (c == '\\') {
9928 enc = rb_utf8_encoding();
9929 tokadd_utf8(p, &enc, -1, 0, 0);
9931 else if (!ISASCII(c = peekc(p))) {
9933 if (tokadd_mbchar(p, c) == -1) return 0;
9936 c = read_escape(p, 0);
9944 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9945 set_yylval_str(lit);
9946 SET_LEX_STATE(EXPR_END);
9950static enum yytokentype
9951parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9954 const char *ptok = p->lex.pcur;
9962 if (c == -1) goto unterminated;
9965 if (!ISASCII(c)) goto unknown;
9970 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9973 c = parser_precise_mbclen(p, p->lex.pcur);
9974 if (c < 0) return 0;
9976 yyerror0("unknown type of %string");
9982 compile_error(p, "unterminated quoted string meets end of file");
9986 if (term == '(') term = ')';
9987 else if (term == '[') term = ']';
9988 else if (term == '{') term = '}';
9989 else if (term == '<') term = '>';
9992 p->lex.ptok = ptok-1;
9995 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9999 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10000 return tSTRING_BEG;
10003 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10007 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10008 return tQWORDS_BEG;
10011 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10012 return tSYMBOLS_BEG;
10015 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10016 return tQSYMBOLS_BEG;
10019 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10020 return tXSTRING_BEG;
10023 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10024 return tREGEXP_BEG;
10027 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10028 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10032 yyerror0("unknown type of %string");
10036 if ((c = nextc(p)) == '=') {
10037 set_yylval_id('%');
10038 SET_LEX_STATE(EXPR_BEG);
10041 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10044 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10046 return warn_balanced('%', "%%", "string literal");
10050tokadd_ident(struct parser_params *p, int c)
10053 if (tokadd_mbchar(p, c) == -1) return -1;
10055 } while (parser_is_identchar(p));
10061tokenize_ident(struct parser_params *p)
10063 ID ident = TOK_INTERN();
10065 set_yylval_name(ident);
10071parse_numvar(struct parser_params *p)
10075 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10076 const unsigned long nth_ref_max =
10077 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10078 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10079 * turned into a Fixnum, in compile.c */
10081 if (overflow || n > nth_ref_max) {
10082 /* compile_error()? */
10083 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10084 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10091static enum yytokentype
10092parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10094 const char *ptr = p->lex.pcur;
10097 SET_LEX_STATE(EXPR_END);
10098 p->lex.ptok = ptr - 1; /* from '$' */
10102 case '_': /* $_: last read line string */
10104 if (parser_is_identchar(p)) {
10112 case '~': /* $~: match-data */
10113 case '*': /* $*: argv */
10114 case '$': /* $$: pid */
10115 case '?': /* $?: last status */
10116 case '!': /* $!: error string */
10117 case '@': /* $@: error position */
10118 case '/': /* $/: input record separator */
10119 case '\\': /* $\: output record separator */
10120 case ';': /* $;: field separator */
10121 case ',': /* $,: output field separator */
10122 case '.': /* $.: last read line number */
10123 case '=': /* $=: ignorecase */
10124 case ':': /* $:: load path */
10125 case '<': /* $<: reading filename */
10126 case '>': /* $>: default output handle */
10127 case '\"': /* $": already loaded files */
10136 if (parser_is_identchar(p)) {
10137 if (tokadd_mbchar(p, c) == -1) return 0;
10145 set_yylval_name(TOK_INTERN());
10148 case '&': /* $&: last match */
10149 case '`': /* $`: string before last match */
10150 case '\'': /* $': string after last match */
10151 case '+': /* $+: string matches last paren. */
10152 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10157 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10160 case '1': case '2': case '3':
10161 case '4': case '5': case '6':
10162 case '7': case '8': case '9':
10167 } while (c != -1 && ISDIGIT(c));
10169 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10171 c = parse_numvar(p);
10172 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10176 if (!parser_is_identchar(p)) {
10177 YYLTYPE loc = RUBY_INIT_YYLLOC();
10178 if (c == -1 || ISSPACE(c)) {
10179 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
10183 compile_error(p, "`$%c' is not allowed as a global variable name", c);
10185 parser_show_error_line(p, &loc);
10186 set_yylval_noname();
10194 if (tokadd_ident(p, c)) return 0;
10195 SET_LEX_STATE(EXPR_END);
10196 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10200 compile_error(p, "`%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10201 set_yylval_noname();
10208parser_numbered_param(struct parser_params *p, int n)
10210 if (n < 0) return false;
10212 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10215 if (p->max_numparam == ORDINAL_PARAM) {
10216 compile_error(p, "ordinary parameter is defined");
10219 struct vtable *args = p->lvtbl->args;
10220 if (p->max_numparam < n) {
10221 p->max_numparam = n;
10223 while (n > args->pos) {
10224 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10230static enum yytokentype
10231parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10233 const char *ptr = p->lex.pcur;
10234 enum yytokentype result = tIVAR;
10235 register int c = nextc(p);
10238 p->lex.ptok = ptr - 1; /* from '@' */
10246 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10247 if (c == -1 || !parser_is_identchar(p)) {
10249 RUBY_SET_YYLLOC(loc);
10250 if (result == tIVAR) {
10251 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
10254 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
10256 parser_show_error_line(p, &loc);
10257 set_yylval_noname();
10258 SET_LEX_STATE(EXPR_END);
10261 else if (ISDIGIT(c)) {
10263 RUBY_SET_YYLLOC(loc);
10264 if (result == tIVAR) {
10265 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
10268 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
10270 parser_show_error_line(p, &loc);
10271 set_yylval_noname();
10272 SET_LEX_STATE(EXPR_END);
10276 if (tokadd_ident(p, c)) return 0;
10281static enum yytokentype
10282parse_ident(struct parser_params *p, int c, int cmd_state)
10284 enum yytokentype result;
10285 int mb = ENC_CODERANGE_7BIT;
10286 const enum lex_state_e last_state = p->lex.state;
10288 int enforce_keyword_end = 0;
10291 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
10292 if (tokadd_mbchar(p, c) == -1) return 0;
10294 } while (parser_is_identchar(p));
10295 if ((c == '!' || c == '?') && !peek(p, '=')) {
10299 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10300 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10301 result = tIDENTIFIER;
10305 result = tCONSTANT; /* assume provisionally */
10310 if (IS_LABEL_POSSIBLE()) {
10311 if (IS_LABEL_SUFFIX(0)) {
10312 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10314 set_yylval_name(TOK_INTERN());
10320 if (!NIL_P(peek_end_expect_token_locations(p))) {
10322 int lineno, column;
10323 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10325 end_loc = peek_end_expect_token_locations(p);
10326 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
10327 column = NUM2INT(rb_ary_entry(end_loc, 1));
10330 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10331 p->ruby_sourceline, beg_pos, lineno, column);
10334 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10335 const struct kwtable *kw;
10337 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10338 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10339 enforce_keyword_end = 1;
10345 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10346 const struct kwtable *kw;
10348 /* See if it is a reserved word. */
10349 kw = rb_reserved_word(tok(p), toklen(p));
10351 enum lex_state_e state = p->lex.state;
10352 if (IS_lex_state_for(state, EXPR_FNAME)) {
10353 SET_LEX_STATE(EXPR_ENDFN);
10354 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10357 SET_LEX_STATE(kw->state);
10358 if (IS_lex_state(EXPR_BEG)) {
10359 p->command_start = TRUE;
10361 if (kw->id[0] == keyword_do) {
10362 if (lambda_beginning_p()) {
10363 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10364 return keyword_do_LAMBDA;
10366 if (COND_P()) return keyword_do_cond;
10367 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10368 return keyword_do_block;
10371 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10374 if (kw->id[0] != kw->id[1])
10375 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10381 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10383 SET_LEX_STATE(EXPR_CMDARG);
10386 SET_LEX_STATE(EXPR_ARG);
10389 else if (p->lex.state == EXPR_FNAME) {
10390 SET_LEX_STATE(EXPR_ENDFN);
10393 SET_LEX_STATE(EXPR_END);
10396 ident = tokenize_ident(p);
10397 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10398 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10399 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10400 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10401 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10407warn_cr(struct parser_params *p)
10411 /* carried over with p->lex.nextline for nextc() */
10412 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10416static enum yytokentype
10417parser_yylex(struct parser_params *p)
10420 int space_seen = 0;
10423 enum lex_state_e last_state;
10424 int fallthru = FALSE;
10425 int token_seen = p->token_seen;
10427 if (p->lex.strterm) {
10428 if (strterm_is_heredoc(p->lex.strterm)) {
10430 return here_document(p, &p->lex.strterm->u.heredoc);
10434 return parse_string(p, &p->lex.strterm->u.literal);
10437 cmd_state = p->command_start;
10438 p->command_start = FALSE;
10439 p->token_seen = TRUE;
10444 last_state = p->lex.state;
10445 switch (c = nextc(p)) {
10446 case '\0': /* NUL */
10447 case '\004': /* ^D */
10448 case '\032': /* ^Z */
10449 case -1: /* end of script. */
10452 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
10453 pop_end_expect_token_locations(p);
10454 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10458 /* Set location for end-of-input because dispatch_scan_event is not called. */
10459 RUBY_SET_YYLLOC(*p->yylloc);
10460 return END_OF_INPUT;
10466 case ' ': case '\t': case '\f':
10467 case '\13': /* '\v' */
10469 while ((c = nextc(p))) {
10474 case ' ': case '\t': case '\f':
10475 case '\13': /* '\v' */
10483 dispatch_scan_event(p, tSP);
10489 case '#': /* it's a comment */
10490 p->token_seen = token_seen;
10491 /* no magic_comment in shebang line */
10492 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10493 if (comment_at_top(p)) {
10494 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10498 dispatch_scan_event(p, tCOMMENT);
10502 p->token_seen = token_seen;
10503 VALUE prevline = p->lex.lastline;
10504 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10505 !IS_lex_state(EXPR_LABELED));
10506 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10508 dispatch_scan_event(p, tIGNORED_NL);
10511 if (!c && p->ctxt.in_kwarg) {
10512 goto normal_newline;
10517 switch (c = nextc(p)) {
10518 case ' ': case '\t': case '\f': case '\r':
10519 case '\13': /* '\v' */
10525 dispatch_scan_event(p, tSP);
10531 dispatch_delayed_token(p, tIGNORED_NL);
10532 if (peek(p, '.') == (c == '&')) {
10534 dispatch_scan_event(p, tSP);
10539 p->ruby_sourceline--;
10540 p->lex.nextline = p->lex.lastline;
10541 set_lastline(p, prevline);
10542 case -1: /* EOF no decrement*/
10546 RUBY_SET_YYLLOC(*p->yylloc);
10548 goto normal_newline;
10552 p->command_start = TRUE;
10553 SET_LEX_STATE(EXPR_BEG);
10557 if ((c = nextc(p)) == '*') {
10558 if ((c = nextc(p)) == '=') {
10559 set_yylval_id(idPow);
10560 SET_LEX_STATE(EXPR_BEG);
10564 if (IS_SPCARG(c)) {
10565 rb_warning0("`**' interpreted as argument prefix");
10568 else if (IS_BEG()) {
10572 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10577 set_yylval_id('*');
10578 SET_LEX_STATE(EXPR_BEG);
10582 if (IS_SPCARG(c)) {
10583 rb_warning0("`*' interpreted as argument prefix");
10586 else if (IS_BEG()) {
10590 c = warn_balanced('*', "*", "argument prefix");
10593 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10598 if (IS_AFTER_OPERATOR()) {
10599 SET_LEX_STATE(EXPR_ARG);
10605 SET_LEX_STATE(EXPR_BEG);
10618 /* skip embedded rd document */
10619 if (word_match_p(p, "begin", 5)) {
10620 int first_p = TRUE;
10623 dispatch_scan_event(p, tEMBDOC_BEG);
10627 dispatch_scan_event(p, tEMBDOC);
10632 compile_error(p, "embedded document meets end of file");
10633 return END_OF_INPUT;
10635 if (c == '=' && word_match_p(p, "end", 3)) {
10641 dispatch_scan_event(p, tEMBDOC_END);
10646 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10647 if ((c = nextc(p)) == '=') {
10648 if ((c = nextc(p)) == '=') {
10657 else if (c == '>') {
10666 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10668 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10669 enum yytokentype token = heredoc_identifier(p);
10670 if (token) return token < 0 ? 0 : token;
10672 if (IS_AFTER_OPERATOR()) {
10673 SET_LEX_STATE(EXPR_ARG);
10676 if (IS_lex_state(EXPR_CLASS))
10677 p->command_start = TRUE;
10678 SET_LEX_STATE(EXPR_BEG);
10681 if ((c = nextc(p)) == '>') {
10688 if ((c = nextc(p)) == '=') {
10689 set_yylval_id(idLTLT);
10690 SET_LEX_STATE(EXPR_BEG);
10694 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10700 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10701 if ((c = nextc(p)) == '=') {
10705 if ((c = nextc(p)) == '=') {
10706 set_yylval_id(idGTGT);
10707 SET_LEX_STATE(EXPR_BEG);
10717 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10718 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10719 p->lex.ptok = p->lex.pcur-1;
10720 return tSTRING_BEG;
10723 if (IS_lex_state(EXPR_FNAME)) {
10724 SET_LEX_STATE(EXPR_ENDFN);
10727 if (IS_lex_state(EXPR_DOT)) {
10729 SET_LEX_STATE(EXPR_CMDARG);
10731 SET_LEX_STATE(EXPR_ARG);
10734 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10735 return tXSTRING_BEG;
10738 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10739 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10740 p->lex.ptok = p->lex.pcur-1;
10741 return tSTRING_BEG;
10744 return parse_qmark(p, space_seen);
10747 if ((c = nextc(p)) == '&') {
10748 SET_LEX_STATE(EXPR_BEG);
10749 if ((c = nextc(p)) == '=') {
10750 set_yylval_id(idANDOP);
10751 SET_LEX_STATE(EXPR_BEG);
10757 else if (c == '=') {
10758 set_yylval_id('&');
10759 SET_LEX_STATE(EXPR_BEG);
10762 else if (c == '.') {
10763 set_yylval_id(idANDDOT);
10764 SET_LEX_STATE(EXPR_DOT);
10768 if (IS_SPCARG(c)) {
10770 (c = peekc_n(p, 1)) == -1 ||
10771 !(c == '\'' || c == '"' ||
10772 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10773 rb_warning0("`&' interpreted as argument prefix");
10777 else if (IS_BEG()) {
10781 c = warn_balanced('&', "&", "argument prefix");
10783 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10787 if ((c = nextc(p)) == '|') {
10788 SET_LEX_STATE(EXPR_BEG);
10789 if ((c = nextc(p)) == '=') {
10790 set_yylval_id(idOROP);
10791 SET_LEX_STATE(EXPR_BEG);
10795 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10803 set_yylval_id('|');
10804 SET_LEX_STATE(EXPR_BEG);
10807 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10813 if (IS_AFTER_OPERATOR()) {
10814 SET_LEX_STATE(EXPR_ARG);
10822 set_yylval_id('+');
10823 SET_LEX_STATE(EXPR_BEG);
10826 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10827 SET_LEX_STATE(EXPR_BEG);
10829 if (c != -1 && ISDIGIT(c)) {
10830 return parse_numeric(p, '+');
10834 SET_LEX_STATE(EXPR_BEG);
10836 return warn_balanced('+', "+", "unary operator");
10840 if (IS_AFTER_OPERATOR()) {
10841 SET_LEX_STATE(EXPR_ARG);
10849 set_yylval_id('-');
10850 SET_LEX_STATE(EXPR_BEG);
10854 SET_LEX_STATE(EXPR_ENDFN);
10857 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10858 SET_LEX_STATE(EXPR_BEG);
10860 if (c != -1 && ISDIGIT(c)) {
10861 return tUMINUS_NUM;
10865 SET_LEX_STATE(EXPR_BEG);
10867 return warn_balanced('-', "-", "unary operator");
10870 int is_beg = IS_BEG();
10871 SET_LEX_STATE(EXPR_BEG);
10872 if ((c = nextc(p)) == '.') {
10873 if ((c = nextc(p)) == '.') {
10874 if (p->ctxt.in_argdef) {
10875 SET_LEX_STATE(EXPR_ENDARG);
10878 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10879 rb_warn0("... at EOL, should be parenthesized?");
10881 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10882 if (IS_lex_state_for(last_state, EXPR_LABEL))
10885 return is_beg ? tBDOT3 : tDOT3;
10888 return is_beg ? tBDOT2 : tDOT2;
10891 if (c != -1 && ISDIGIT(c)) {
10892 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10893 parse_numeric(p, '.');
10894 if (ISDIGIT(prev)) {
10895 yyerror0("unexpected fraction part after numeric literal");
10898 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10900 SET_LEX_STATE(EXPR_END);
10901 p->lex.ptok = p->lex.pcur;
10904 set_yylval_id('.');
10905 SET_LEX_STATE(EXPR_DOT);
10909 case '0': case '1': case '2': case '3': case '4':
10910 case '5': case '6': case '7': case '8': case '9':
10911 return parse_numeric(p, c);
10916 SET_LEX_STATE(EXPR_ENDFN);
10917 p->lex.paren_nest--;
10923 SET_LEX_STATE(EXPR_END);
10924 p->lex.paren_nest--;
10928 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10929 if (!p->lex.brace_nest--) return tSTRING_DEND;
10932 SET_LEX_STATE(EXPR_END);
10933 p->lex.paren_nest--;
10939 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10940 SET_LEX_STATE(EXPR_BEG);
10943 set_yylval_id(idCOLON2);
10944 SET_LEX_STATE(EXPR_DOT);
10947 if (IS_END() || ISSPACE(c) || c == '#') {
10949 c = warn_balanced(':', ":", "symbol literal");
10950 SET_LEX_STATE(EXPR_BEG);
10955 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10958 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10964 SET_LEX_STATE(EXPR_FNAME);
10969 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10970 return tREGEXP_BEG;
10972 if ((c = nextc(p)) == '=') {
10973 set_yylval_id('/');
10974 SET_LEX_STATE(EXPR_BEG);
10978 if (IS_SPCARG(c)) {
10979 arg_ambiguous(p, '/');
10980 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10981 return tREGEXP_BEG;
10983 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10984 return warn_balanced('/', "/", "regexp literal");
10987 if ((c = nextc(p)) == '=') {
10988 set_yylval_id('^');
10989 SET_LEX_STATE(EXPR_BEG);
10992 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10997 SET_LEX_STATE(EXPR_BEG);
10998 p->command_start = TRUE;
11002 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11006 if (IS_AFTER_OPERATOR()) {
11007 if ((c = nextc(p)) != '@') {
11010 SET_LEX_STATE(EXPR_ARG);
11013 SET_LEX_STATE(EXPR_BEG);
11021 else if (!space_seen) {
11022 /* foo( ... ) => method call, no ambiguity */
11024 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11027 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11028 rb_warning0("parentheses after method name is interpreted as "
11029 "an argument list, not a decomposed argument");
11031 p->lex.paren_nest++;
11034 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11038 p->lex.paren_nest++;
11039 if (IS_AFTER_OPERATOR()) {
11040 if ((c = nextc(p)) == ']') {
11041 p->lex.paren_nest--;
11042 SET_LEX_STATE(EXPR_ARG);
11043 if ((c = nextc(p)) == '=') {
11050 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11053 else if (IS_BEG()) {
11056 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11059 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11065 ++p->lex.brace_nest;
11066 if (lambda_beginning_p())
11068 else if (IS_lex_state(EXPR_LABELED))
11069 c = tLBRACE; /* hash */
11070 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11071 c = '{'; /* block (primary) */
11072 else if (IS_lex_state(EXPR_ENDARG))
11073 c = tLBRACE_ARG; /* block (expr) */
11075 c = tLBRACE; /* hash */
11076 if (c != tLBRACE) {
11077 p->command_start = TRUE;
11078 SET_LEX_STATE(EXPR_BEG);
11081 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11083 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11092 dispatch_scan_event(p, tSP);
11093 goto retry; /* skip \\n */
11095 if (c == ' ') return tSP;
11096 if (ISSPACE(c)) return c;
11101 return parse_percent(p, space_seen, last_state);
11104 return parse_gvar(p, last_state);
11107 return parse_atmark(p, last_state);
11110 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11111 p->ruby__end__seen = 1;
11115 dispatch_scan_event(p, k__END__);
11117 return END_OF_INPUT;
11123 if (!parser_is_identchar(p)) {
11124 compile_error(p, "Invalid char `\\x%02X' in expression", c);
11133 return parse_ident(p, c, cmd_state);
11136static enum yytokentype
11137yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11139 enum yytokentype t;
11142 lval->val = Qundef;
11143 p->yylloc = yylloc;
11145 t = parser_yylex(p);
11147 if (has_delayed_token(p))
11148 dispatch_delayed_token(p, t);
11149 else if (t != END_OF_INPUT)
11150 dispatch_scan_event(p, t);
11155#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11158node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11160 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11162 rb_node_init(n, type);
11167nd_set_loc(NODE *nd, const YYLTYPE *loc)
11170 nd_set_line(nd, loc->beg_pos.lineno);
11175node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11177 NODE *n = node_new_internal(p, type, size, alignment);
11179 nd_set_loc(n, loc);
11180 nd_set_node_id(n, parser_get_node_id(p));
11184#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11188static rb_node_scope_t *
11189rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11191 rb_ast_id_table_t *nd_tbl;
11192 nd_tbl = local_tbl(p);
11193 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11194 n->nd_tbl = nd_tbl;
11195 n->nd_body = nd_body;
11196 n->nd_args = nd_args;
11201static rb_node_scope_t *
11202rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11204 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11205 n->nd_tbl = nd_tbl;
11206 n->nd_body = nd_body;
11207 n->nd_args = nd_args;
11212static rb_node_defn_t *
11213rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11215 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11216 n->nd_mid = nd_mid;
11217 n->nd_defn = nd_defn;
11222static rb_node_defs_t *
11223rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11225 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11226 n->nd_recv = nd_recv;
11227 n->nd_mid = nd_mid;
11228 n->nd_defn = nd_defn;
11233static rb_node_block_t *
11234rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11236 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11237 n->nd_head = nd_head;
11238 n->nd_end = (NODE *)n;
11244static rb_node_for_t *
11245rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11247 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11248 n->nd_body = nd_body;
11249 n->nd_iter = nd_iter;
11254static rb_node_for_masgn_t *
11255rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11257 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11258 n->nd_var = nd_var;
11263static rb_node_retry_t *
11264rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11266 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11271static rb_node_begin_t *
11272rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11274 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11275 n->nd_body = nd_body;
11280static rb_node_rescue_t *
11281rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11283 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11284 n->nd_head = nd_head;
11285 n->nd_resq = nd_resq;
11286 n->nd_else = nd_else;
11291static rb_node_resbody_t *
11292rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc)
11294 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11295 n->nd_head = nd_head;
11296 n->nd_body = nd_body;
11297 n->nd_args = nd_args;
11302static rb_node_ensure_t *
11303rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11305 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11306 n->nd_head = nd_head;
11308 n->nd_ensr = nd_ensr;
11313static rb_node_and_t *
11314rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11316 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11317 n->nd_1st = nd_1st;
11318 n->nd_2nd = nd_2nd;
11323static rb_node_or_t *
11324rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11326 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11327 n->nd_1st = nd_1st;
11328 n->nd_2nd = nd_2nd;
11333static rb_node_return_t *
11334rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
11336 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11337 n->nd_stts = nd_stts;
11341static rb_node_yield_t *
11342rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11344 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11345 n->nd_head = nd_head;
11350static rb_node_if_t *
11351rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11353 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11354 n->nd_cond = nd_cond;
11355 n->nd_body = nd_body;
11356 n->nd_else = nd_else;
11361static rb_node_unless_t *
11362rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11364 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11365 n->nd_cond = nd_cond;
11366 n->nd_body = nd_body;
11367 n->nd_else = nd_else;
11372static rb_node_class_t *
11373rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11375 /* Keep the order of node creation */
11376 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11377 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11378 n->nd_cpath = nd_cpath;
11379 n->nd_body = scope;
11380 n->nd_super = nd_super;
11385static rb_node_sclass_t *
11386rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11388 /* Keep the order of node creation */
11389 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11390 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11391 n->nd_recv = nd_recv;
11392 n->nd_body = scope;
11397static rb_node_module_t *
11398rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11400 /* Keep the order of node creation */
11401 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11402 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11403 n->nd_cpath = nd_cpath;
11404 n->nd_body = scope;
11409static rb_node_iter_t *
11410rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11412 /* Keep the order of node creation */
11413 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11414 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11415 n->nd_body = scope;
11421static rb_node_lambda_t *
11422rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11424 /* Keep the order of node creation */
11425 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11426 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11427 n->nd_body = scope;
11432static rb_node_case_t *
11433rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11435 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11436 n->nd_head = nd_head;
11437 n->nd_body = nd_body;
11442static rb_node_case2_t *
11443rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11445 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11447 n->nd_body = nd_body;
11452static rb_node_case3_t *
11453rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11455 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11456 n->nd_head = nd_head;
11457 n->nd_body = nd_body;
11462static rb_node_when_t *
11463rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11465 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11466 n->nd_head = nd_head;
11467 n->nd_body = nd_body;
11468 n->nd_next = nd_next;
11473static rb_node_in_t *
11474rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11476 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11477 n->nd_head = nd_head;
11478 n->nd_body = nd_body;
11479 n->nd_next = nd_next;
11484static rb_node_while_t *
11485rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11487 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11488 n->nd_cond = nd_cond;
11489 n->nd_body = nd_body;
11490 n->nd_state = nd_state;
11495static rb_node_until_t *
11496rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11498 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11499 n->nd_cond = nd_cond;
11500 n->nd_body = nd_body;
11501 n->nd_state = nd_state;
11506static rb_node_colon2_t *
11507rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11509 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11510 n->nd_head = nd_head;
11511 n->nd_mid = nd_mid;
11516static rb_node_colon3_t *
11517rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11519 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11520 n->nd_mid = nd_mid;
11525static rb_node_dot2_t *
11526rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11528 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11529 n->nd_beg = nd_beg;
11530 n->nd_end = nd_end;
11535static rb_node_dot3_t *
11536rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11538 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11539 n->nd_beg = nd_beg;
11540 n->nd_end = nd_end;
11545static rb_node_self_t *
11546rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11548 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11554static rb_node_nil_t *
11555rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11557 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11562static rb_node_true_t *
11563rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11565 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11570static rb_node_false_t *
11571rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11573 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11578static rb_node_super_t *
11579rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11581 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11582 n->nd_args = nd_args;
11587static rb_node_zsuper_t *
11588rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11590 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11595static rb_node_match2_t *
11596rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11598 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11599 n->nd_recv = nd_recv;
11600 n->nd_value = nd_value;
11606static rb_node_match3_t *
11607rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11609 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11610 n->nd_recv = nd_recv;
11611 n->nd_value = nd_value;
11616/* TODO: Use union for NODE_LIST2 */
11617static rb_node_list_t *
11618rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11620 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11621 n->nd_head = nd_head;
11628static rb_node_list_t *
11629rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11631 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11632 n->nd_head = nd_head;
11633 n->as.nd_alen = nd_alen;
11634 n->nd_next = nd_next;
11639static rb_node_zlist_t *
11640rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11642 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11647static rb_node_hash_t *
11648rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11650 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11651 n->nd_head = nd_head;
11657static rb_node_masgn_t *
11658rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11660 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11661 n->nd_head = nd_head;
11663 n->nd_args = nd_args;
11668static rb_node_gasgn_t *
11669rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11671 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11672 n->nd_vid = nd_vid;
11673 n->nd_value = nd_value;
11678static rb_node_lasgn_t *
11679rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11681 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11682 n->nd_vid = nd_vid;
11683 n->nd_value = nd_value;
11688static rb_node_dasgn_t *
11689rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11691 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11692 n->nd_vid = nd_vid;
11693 n->nd_value = nd_value;
11698static rb_node_iasgn_t *
11699rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11701 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11702 n->nd_vid = nd_vid;
11703 n->nd_value = nd_value;
11708static rb_node_cvasgn_t *
11709rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11711 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11712 n->nd_vid = nd_vid;
11713 n->nd_value = nd_value;
11718static rb_node_op_asgn1_t *
11719rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc)
11721 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11722 n->nd_recv = nd_recv;
11723 n->nd_mid = nd_mid;
11724 n->nd_index = index;
11725 n->nd_rvalue = rvalue;
11730static rb_node_op_asgn2_t *
11731rb_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)
11733 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11734 n->nd_recv = nd_recv;
11735 n->nd_value = nd_value;
11736 n->nd_vid = nd_vid;
11737 n->nd_mid = nd_mid;
11738 n->nd_aid = nd_aid;
11743static rb_node_op_asgn_or_t *
11744rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11746 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11747 n->nd_head = nd_head;
11748 n->nd_value = nd_value;
11753static rb_node_op_asgn_and_t *
11754rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11756 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11757 n->nd_head = nd_head;
11758 n->nd_value = nd_value;
11763static rb_node_gvar_t *
11764rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11766 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11767 n->nd_vid = nd_vid;
11772static rb_node_lvar_t *
11773rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11775 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11776 n->nd_vid = nd_vid;
11781static rb_node_dvar_t *
11782rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11784 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11785 n->nd_vid = nd_vid;
11790static rb_node_ivar_t *
11791rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11793 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11794 n->nd_vid = nd_vid;
11799static rb_node_const_t *
11800rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11802 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11803 n->nd_vid = nd_vid;
11808static rb_node_cvar_t *
11809rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11811 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11812 n->nd_vid = nd_vid;
11817static rb_node_nth_ref_t *
11818rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11820 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11821 n->nd_nth = nd_nth;
11826static rb_node_back_ref_t *
11827rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11829 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11830 n->nd_nth = nd_nth;
11835static rb_node_lit_t *
11836rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11838 rb_node_lit_t *n = NODE_NEWNODE(NODE_LIT, rb_node_lit_t, loc);
11839 n->nd_lit = nd_lit;
11844static rb_node_str_t *
11845rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11847 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11848 n->nd_lit = nd_lit;
11853/* TODO; Use union for NODE_DSTR2 */
11854static rb_node_dstr_t *
11855rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11857 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11858 n->nd_lit = nd_lit;
11859 n->as.nd_alen = nd_alen;
11860 n->nd_next = (rb_node_list_t *)nd_next;
11865static rb_node_dstr_t *
11866rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11868 return rb_node_dstr_new0(p, nd_lit, 1, 0, loc);
11871static rb_node_xstr_t *
11872rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11874 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
11875 n->nd_lit = nd_lit;
11880static rb_node_dxstr_t *
11881rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11883 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
11884 n->nd_lit = nd_lit;
11885 n->nd_alen = nd_alen;
11886 n->nd_next = (rb_node_list_t *)nd_next;
11891static rb_node_dsym_t *
11892rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11894 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
11895 n->nd_lit = nd_lit;
11896 n->nd_alen = nd_alen;
11897 n->nd_next = (rb_node_list_t *)nd_next;
11902static rb_node_evstr_t *
11903rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11905 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
11906 n->nd_body = nd_body;
11911static rb_node_call_t *
11912rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11914 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
11915 n->nd_recv = nd_recv;
11916 n->nd_mid = nd_mid;
11917 n->nd_args = nd_args;
11922static rb_node_opcall_t *
11923rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11925 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
11926 n->nd_recv = nd_recv;
11927 n->nd_mid = nd_mid;
11928 n->nd_args = nd_args;
11933static rb_node_fcall_t *
11934rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11936 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
11937 n->nd_mid = nd_mid;
11938 n->nd_args = nd_args;
11943static rb_node_qcall_t *
11944rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11946 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
11947 n->nd_recv = nd_recv;
11948 n->nd_mid = nd_mid;
11949 n->nd_args = nd_args;
11954static rb_node_vcall_t *
11955rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11957 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
11958 n->nd_mid = nd_mid;
11963static rb_node_once_t *
11964rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11966 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
11967 n->nd_body = nd_body;
11972static rb_node_args_t *
11973rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
11975 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
11976 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
11981static rb_node_args_aux_t *
11982rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc)
11984 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
11985 n->nd_pid = nd_pid;
11986 n->nd_plen = nd_plen;
11992static rb_node_opt_arg_t *
11993rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11995 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
11996 n->nd_body = nd_body;
12002static rb_node_kw_arg_t *
12003rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12005 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12006 n->nd_body = nd_body;
12012static rb_node_postarg_t *
12013rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12015 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12016 n->nd_1st = nd_1st;
12017 n->nd_2nd = nd_2nd;
12022static rb_node_argscat_t *
12023rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12025 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12026 n->nd_head = nd_head;
12027 n->nd_body = nd_body;
12032static rb_node_argspush_t *
12033rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12035 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12036 n->nd_head = nd_head;
12037 n->nd_body = nd_body;
12042static rb_node_splat_t *
12043rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12045 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12046 n->nd_head = nd_head;
12051static rb_node_block_pass_t *
12052rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12054 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12056 n->nd_body = nd_body;
12061static rb_node_alias_t *
12062rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12064 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12065 n->nd_1st = nd_1st;
12066 n->nd_2nd = nd_2nd;
12071static rb_node_valias_t *
12072rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc)
12074 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12075 n->nd_alias = nd_alias;
12076 n->nd_orig = nd_orig;
12081static rb_node_undef_t *
12082rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12084 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12085 n->nd_undef = nd_undef;
12090static rb_node_errinfo_t *
12091rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12093 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12098static rb_node_defined_t *
12099rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12101 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12102 n->nd_head = nd_head;
12107static rb_node_postexe_t *
12108rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12110 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12111 n->nd_body = nd_body;
12116static rb_node_attrasgn_t *
12117rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12119 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12120 n->nd_recv = nd_recv;
12121 n->nd_mid = nd_mid;
12122 n->nd_args = nd_args;
12127static rb_node_aryptn_t *
12128rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12130 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12132 n->pre_args = pre_args;
12133 n->rest_arg = rest_arg;
12134 n->post_args = post_args;
12139static rb_node_hshptn_t *
12140rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12142 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12143 n->nd_pconst = nd_pconst;
12144 n->nd_pkwargs = nd_pkwargs;
12145 n->nd_pkwrestarg = nd_pkwrestarg;
12150static rb_node_fndptn_t *
12151rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12153 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12155 n->pre_rest_arg = pre_rest_arg;
12157 n->post_rest_arg = post_rest_arg;
12162static rb_node_cdecl_t *
12163rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc)
12165 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12166 n->nd_vid = nd_vid;
12167 n->nd_value = nd_value;
12168 n->nd_else = nd_else;
12173static rb_node_op_cdecl_t *
12174rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, const YYLTYPE *loc)
12176 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12177 n->nd_head = nd_head;
12178 n->nd_value = nd_value;
12179 n->nd_aid = nd_aid;
12184static rb_node_error_t *
12185rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12187 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12194static rb_node_ripper_t *
12195rb_node_ripper_new(struct parser_params *p, ID nd_vid, VALUE nd_rval, VALUE nd_cval, const YYLTYPE *loc)
12197 rb_node_ripper_t *n = NODE_NEWNODE(NODE_RIPPER, rb_node_ripper_t, loc);
12198 n->nd_vid = nd_vid;
12199 n->nd_rval = nd_rval;
12200 n->nd_cval = nd_cval;
12205static rb_node_ripper_values_t *
12206rb_node_ripper_values_new(struct parser_params *p, VALUE nd_val1, VALUE nd_val2, VALUE nd_val3, const YYLTYPE *loc)
12208 rb_node_ripper_values_t *n = NODE_NEWNODE(NODE_RIPPER_VALUES, rb_node_ripper_values_t, loc);
12209 n->nd_val1 = nd_val1;
12210 n->nd_val2 = nd_val2;
12211 n->nd_val3 = nd_val3;
12218static rb_node_break_t *
12219rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12221 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12222 n->nd_stts = nd_stts;
12228static rb_node_next_t *
12229rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12231 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12232 n->nd_stts = nd_stts;
12238static rb_node_redo_t *
12239rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc)
12241 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12247static rb_node_def_temp_t *
12248rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12250 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12251 n->save.cur_arg = p->cur_arg;
12252 n->save.numparam_save = 0;
12253 n->save.max_numparam = 0;
12254 n->save.ctxt = p->ctxt;
12258 n->dot_or_colon = Qnil;
12267static rb_node_def_temp_t *
12268def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12270 n->save.numparam_save = numparam_push(p);
12271 n->save.max_numparam = p->max_numparam;
12276static enum node_type
12277nodetype(NODE *node) /* for debug */
12279 return (enum node_type)nd_type(node);
12283nodeline(NODE *node)
12285 return nd_line(node);
12289newline_node(NODE *node)
12292 node = remove_begin(node);
12293 nd_set_fl_newline(node);
12299fixpos(NODE *node, NODE *orig)
12303 nd_set_line(node, nd_line(orig));
12307parser_warning(struct parser_params *p, NODE *node, const char *mesg)
12309 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12313parser_warn(struct parser_params *p, NODE *node, const char *mesg)
12315 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12319block_append(struct parser_params *p, NODE *head, NODE *tail)
12321 NODE *end, *h = head, *nd;
12323 if (tail == 0) return head;
12325 if (h == 0) return tail;
12326 switch (nd_type(h)) {
12328 h = end = NEW_BLOCK(head, &head->nd_loc);
12332 end = RNODE_BLOCK(h)->nd_end;
12336 nd = RNODE_BLOCK(end)->nd_head;
12337 switch (nd_type(nd)) {
12343 if (RTEST(ruby_verbose)) {
12344 parser_warning(p, tail, "statement not reached");
12352 if (!nd_type_p(tail, NODE_BLOCK)) {
12353 tail = NEW_BLOCK(tail, &tail->nd_loc);
12355 RNODE_BLOCK(end)->nd_next = tail;
12356 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12357 nd_set_last_loc(head, nd_last_loc(tail));
12361/* append item to the list */
12363list_append(struct parser_params *p, NODE *list, NODE *item)
12367 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12368 if (RNODE_LIST(list)->nd_next) {
12369 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12375 RNODE_LIST(list)->as.nd_alen += 1;
12376 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12377 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12379 nd_set_last_loc(list, nd_last_loc(item));
12384/* concat two lists */
12386list_concat(NODE *head, NODE *tail)
12390 if (RNODE_LIST(head)->nd_next) {
12391 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12397 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12398 RNODE_LIST(last)->nd_next = tail;
12399 if (RNODE_LIST(tail)->nd_next) {
12400 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12403 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12406 nd_set_last_loc(head, nd_last_loc(tail));
12412literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
12414 if (NIL_P(tail)) return 1;
12415 if (!rb_enc_compatible(head, tail)) {
12416 compile_error(p, "string literal encodings differ (%s / %s)",
12417 rb_enc_name(rb_enc_get(head)),
12418 rb_enc_name(rb_enc_get(tail)));
12419 rb_str_resize(head, 0);
12420 rb_str_resize(tail, 0);
12423 rb_str_buf_append(head, tail);
12428string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12430 if (htype != NODE_DSTR) return Qfalse;
12431 if (RNODE_DSTR(head)->nd_next) {
12432 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12433 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
12435 const VALUE lit = RNODE_DSTR(head)->nd_lit;
12436 ASSUME(lit != Qfalse);
12440/* concat two string literals */
12442literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12444 enum node_type htype;
12447 if (!head) return tail;
12448 if (!tail) return head;
12450 htype = nd_type(head);
12451 if (htype == NODE_EVSTR) {
12452 head = new_dstr(p, head, loc);
12455 if (p->heredoc_indent > 0) {
12458 head = str2dstr(p, head);
12460 return list_append(p, head, tail);
12465 switch (nd_type(tail)) {
12467 if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12471 lit = RNODE_DSTR(head)->nd_lit;
12473 if (htype == NODE_STR) {
12474 if (!literal_concat0(p, lit, RNODE_STR(tail)->nd_lit)) {
12476 rb_discard_node(p, head);
12477 rb_discard_node(p, tail);
12480 rb_discard_node(p, tail);
12483 list_append(p, head, tail);
12488 if (htype == NODE_STR) {
12489 if (!literal_concat0(p, RNODE_STR(head)->nd_lit, RNODE_DSTR(tail)->nd_lit))
12491 RNODE_DSTR(tail)->nd_lit = RNODE_STR(head)->nd_lit;
12492 rb_discard_node(p, head);
12495 else if (NIL_P(RNODE_DSTR(tail)->nd_lit)) {
12497 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12498 if (!RNODE_DSTR(head)->nd_next) {
12499 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12501 else if (RNODE_DSTR(tail)->nd_next) {
12502 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12503 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12505 rb_discard_node(p, tail);
12507 else if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12508 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->nd_lit))
12510 RNODE_DSTR(tail)->nd_lit = Qnil;
12514 list_concat(head, NEW_LIST2(NEW_STR(RNODE_DSTR(tail)->nd_lit, loc), RNODE_DSTR(tail)->as.nd_alen, (NODE *)RNODE_DSTR(tail)->nd_next, loc));
12519 if (htype == NODE_STR) {
12520 head = str2dstr(p, head);
12521 RNODE_DSTR(head)->as.nd_alen = 1;
12523 list_append(p, head, tail);
12530nd_copy_flag(NODE *new_node, NODE *old_node)
12532 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12533 nd_set_line(new_node, nd_line(old_node));
12534 new_node->nd_loc = old_node->nd_loc;
12535 new_node->node_id = old_node->node_id;
12539str2dstr(struct parser_params *p, NODE *node)
12541 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12542 nd_copy_flag(new_node, node);
12543 RNODE_DSTR(new_node)->nd_lit = RNODE_STR(node)->nd_lit;
12544 RNODE_DSTR(new_node)->as.nd_alen = 0;
12545 RNODE_DSTR(new_node)->nd_next = 0;
12546 RNODE_STR(node)->nd_lit = 0;
12552evstr2dstr(struct parser_params *p, NODE *node)
12554 if (nd_type_p(node, NODE_EVSTR)) {
12555 node = new_dstr(p, node, &node->nd_loc);
12561new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12566 switch (nd_type(node)) {
12568 return str2dstr(p, node);
12575 return NEW_EVSTR(head, loc);
12579new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12581 VALUE lit = STR_NEW0();
12582 NODE *dstr = NEW_DSTR(lit, loc);
12583 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12584 return list_append(p, dstr, node);
12588call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12589 const YYLTYPE *op_loc, const YYLTYPE *loc)
12594 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12595 nd_set_line(expr, op_loc->beg_pos.lineno);
12600call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12604 opcall = NEW_OPCALL(recv, id, 0, loc);
12605 nd_set_line(opcall, op_loc->beg_pos.lineno);
12610new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12612 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12613 nd_set_line(qcall, op_loc->beg_pos.lineno);
12618new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12621 if (block) block_dup_check(p, args, block);
12622 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12623 if (block) ret = method_add_block(p, ret, block, loc);
12628#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12631last_expr_once_body(NODE *node)
12633 if (!node) return 0;
12634 return nd_once_body(node);
12638match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12641 int line = op_loc->beg_pos.lineno;
12646 if ((n = last_expr_once_body(node1)) != 0) {
12647 switch (nd_type(n)) {
12650 NODE *match = NEW_MATCH2(node1, node2, loc);
12651 nd_set_line(match, line);
12656 if (RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) {
12657 const VALUE lit = RNODE_LIT(n)->nd_lit;
12658 NODE *match = NEW_MATCH2(node1, node2, loc);
12659 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc);
12660 nd_set_line(match, line);
12666 if ((n = last_expr_once_body(node2)) != 0) {
12669 switch (nd_type(n)) {
12671 if (!RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) break;
12674 match3 = NEW_MATCH3(node2, node1, loc);
12679 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12680 nd_set_line(n, line);
12684# if WARN_PAST_SCOPE
12686past_dvar_p(struct parser_params *p, ID id)
12688 struct vtable *past = p->lvtbl->past;
12690 if (vtable_included(past, id)) return 1;
12698numparam_nested_p(struct parser_params *p)
12700 struct local_vars *local = p->lvtbl;
12701 NODE *outer = local->numparam.outer;
12702 NODE *inner = local->numparam.inner;
12703 if (outer || inner) {
12704 NODE *used = outer ? outer : inner;
12705 compile_error(p, "numbered parameter is already used in\n"
12706 "%s:%d: %s block here",
12707 p->ruby_sourcefile, nd_line(used),
12708 outer ? "outer" : "inner");
12709 parser_show_error_line(p, &used->nd_loc);
12716gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12722 return NEW_SELF(loc);
12724 return NEW_NIL(loc);
12726 return NEW_TRUE(loc);
12727 case keyword_false:
12728 return NEW_FALSE(loc);
12729 case keyword__FILE__:
12731 VALUE file = p->ruby_sourcefile_string;
12733 file = rb_str_new(0, 0);
12735 file = rb_str_dup(file);
12736 node = NEW_STR(file, loc);
12737 RB_OBJ_WRITTEN(p->ast, Qnil, file);
12740 case keyword__LINE__:
12741 return NEW_LIT(INT2FIX(loc->beg_pos.lineno), loc);
12742 case keyword__ENCODING__:
12743 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
12744 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12748 switch (id_type(id)) {
12750 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12751 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
12752 if (id == p->cur_arg) {
12753 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12756 if (vidp) *vidp |= LVAR_USED;
12757 node = NEW_DVAR(id, loc);
12760 if (local_id_ref(p, id, &vidp)) {
12761 if (id == p->cur_arg) {
12762 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12765 if (vidp) *vidp |= LVAR_USED;
12766 node = NEW_LVAR(id, loc);
12769 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12770 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12771 if (numparam_nested_p(p)) return 0;
12772 node = NEW_DVAR(id, loc);
12773 struct local_vars *local = p->lvtbl;
12774 if (!local->numparam.current) local->numparam.current = node;
12777# if WARN_PAST_SCOPE
12778 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
12779 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
12782 /* method call without arguments */
12783 if (dyna_in_block(p) && id == rb_intern("it")
12784 && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))
12785 && p->max_numparam != ORDINAL_PARAM) {
12786 rb_warn0("`it` calls without arguments will refer to the first block param in Ruby 3.4; use it() or self.it");
12788 return NEW_VCALL(id, loc);
12790 return NEW_GVAR(id, loc);
12792 return NEW_IVAR(id, loc);
12794 return NEW_CONST(id, loc);
12796 return NEW_CVAR(id, loc);
12798 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
12802static rb_node_opt_arg_t *
12803opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
12805 rb_node_opt_arg_t *opts = opt_list;
12806 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12808 while (opts->nd_next) {
12809 opts = opts->nd_next;
12810 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12812 opts->nd_next = opt;
12817static rb_node_kw_arg_t *
12818kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
12821 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
12822 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
12828new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
12832 if (nd_type_p(n, NODE_BEGIN)) {
12833 n = RNODE_BEGIN(n)->nd_body;
12835 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
12836 n = RNODE_BLOCK(n)->nd_head;
12842 return NEW_DEFINED(n, loc);
12846symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
12848 enum node_type type = nd_type(symbol);
12851 nd_set_type(symbol, NODE_DSYM);
12854 nd_set_type(symbol, NODE_LIT);
12855 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(symbol)->nd_lit = rb_str_intern(RNODE_LIT(symbol)->nd_lit));
12858 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
12860 return list_append(p, symbols, symbol);
12864new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
12866 struct RNode_LIST *list;
12871 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
12872 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12875 switch (nd_type(node)) {
12878 VALUE src = RNODE_STR(node)->nd_lit;
12879 nd_set_type(node, NODE_LIT);
12880 nd_set_loc(node, loc);
12881 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit = reg_compile(p, src, options));
12886 node = NEW_DSTR0(lit, 1, NEW_LIST(node, loc), loc);
12887 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12890 nd_set_type(node, NODE_DREGX);
12891 nd_set_loc(node, loc);
12892 RNODE_DREGX(node)->nd_cflag = options & RE_OPTION_MASK;
12893 if (!NIL_P(RNODE_DREGX(node)->nd_lit)) reg_fragment_check(p, RNODE_DREGX(node)->nd_lit, options);
12894 for (list = RNODE_DREGX(prev = node)->nd_next; list; list = RNODE_LIST(list->nd_next)) {
12895 NODE *frag = list->nd_head;
12896 enum node_type type = nd_type(frag);
12897 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
12898 VALUE tail = RNODE_STR(frag)->nd_lit;
12899 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(RNODE_DREGX(prev)->nd_lit)) {
12900 VALUE lit = prev == node ? RNODE_DREGX(prev)->nd_lit : RNODE_LIT(RNODE_LIST(prev)->nd_head)->nd_lit;
12901 if (!literal_concat0(p, lit, tail)) {
12902 return NEW_NIL(loc); /* dummy node on error */
12904 rb_str_resize(tail, 0);
12905 RNODE_LIST(prev)->nd_next = list->nd_next;
12906 rb_discard_node(p, list->nd_head);
12907 rb_discard_node(p, (NODE *)list);
12908 list = RNODE_LIST(prev);
12911 prev = (NODE *)list;
12918 if (!RNODE_DREGX(node)->nd_next) {
12919 VALUE src = RNODE_DREGX(node)->nd_lit;
12920 VALUE re = reg_compile(p, src, options);
12921 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_DREGX(node)->nd_lit = re);
12923 if (options & RE_OPTION_ONCE) {
12924 node = NEW_ONCE(node, loc);
12931static rb_node_kw_arg_t *
12932new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
12935 return NEW_KW_ARG((k), loc);
12939new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12942 VALUE lit = STR_NEW0();
12943 NODE *xstr = NEW_XSTR(lit, loc);
12944 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12947 switch (nd_type(node)) {
12949 nd_set_type(node, NODE_XSTR);
12950 nd_set_loc(node, loc);
12953 nd_set_type(node, NODE_DXSTR);
12954 nd_set_loc(node, loc);
12957 node = NEW_DXSTR(Qnil, 1, NEW_LIST(node, loc), loc);
12964check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
12968 if (!arg || !p->case_labels) return;
12970 lit = rb_node_case_when_optimizable_literal(arg);
12971 if (UNDEF_P(lit)) return;
12972 if (nd_type_p(arg, NODE_STR)) {
12973 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(arg)->nd_lit = lit);
12976 if (NIL_P(p->case_labels)) {
12977 p->case_labels = rb_obj_hide(rb_hash_new());
12980 VALUE line = rb_hash_lookup(p->case_labels, lit);
12981 if (!NIL_P(line)) {
12982 rb_warning1("duplicated `when' clause with line %d is ignored",
12987 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
12992id_is_var(struct parser_params *p, ID id)
12994 if (is_notop_id(id)) {
12995 switch (id & ID_SCOPE_MASK) {
12996 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
12999 if (dyna_in_block(p)) {
13000 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13002 if (local_id(p, id)) return 1;
13003 /* method call without arguments */
13007 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13012new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
13014 VALUE src = 0, err = 0;
13016 if (ripper_is_node_yylval(p, re)) {
13017 src = RNODE_RIPPER(re)->nd_cval;
13018 re = RNODE_RIPPER(re)->nd_rval;
13020 if (ripper_is_node_yylval(p, opt)) {
13021 options = (int)RNODE_RIPPER(opt)->nd_vid;
13022 opt = RNODE_RIPPER(opt)->nd_rval;
13024 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
13025 compile_error(p, "%"PRIsVALUE, err);
13027 return dispatch2(regexp_literal, re, opt);
13029#endif /* !RIPPER */
13031static inline enum lex_state_e
13032parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13035 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13037 return p->lex.state = ls;
13041static const char rb_parser_lex_state_names[][8] = {
13042 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13043 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13044 "LABEL", "LABELED","FITEM",
13048append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13051 unsigned int mask = 1;
13052 static const char none[] = "NONE";
13054 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13055 if ((unsigned)state & mask) {
13057 rb_str_cat(buf, "|", 1);
13060 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13064 rb_str_cat(buf, none, sizeof(none)-1);
13070flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13072 VALUE mesg = p->debug_buffer;
13074 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13075 p->debug_buffer = Qnil;
13076 rb_io_puts(1, &mesg, out);
13078 if (!NIL_P(str) && RSTRING_LEN(str)) {
13079 rb_io_write(p->debug_output, str);
13084rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13085 enum lex_state_e to, int line)
13088 mesg = rb_str_new_cstr("lex_state: ");
13089 append_lex_state_name(p, from, mesg);
13090 rb_str_cat_cstr(mesg, " -> ");
13091 append_lex_state_name(p, to, mesg);
13092 rb_str_catf(mesg, " at line %d\n", line);
13093 flush_debug_buffer(p, p->debug_output, mesg);
13098rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13100 return rb_fstring(append_lex_state_name(p, state, rb_str_new(0, 0)));
13104append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13107 rb_str_cat_cstr(mesg, "0");
13110 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13111 for (; mask && !(stack & mask); mask >>= 1) continue;
13112 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13117rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13118 const char *name, int line)
13120 VALUE mesg = rb_sprintf("%s: ", name);
13121 append_bitstack_value(p, stack, mesg);
13122 rb_str_catf(mesg, " at line %d\n", line);
13123 flush_debug_buffer(p, p->debug_output, mesg);
13127rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13130 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13133 rb_str_vcatf(mesg, fmt, ap);
13135 yyerror0(RSTRING_PTR(mesg));
13138 mesg = rb_str_new(0, 0);
13139 append_lex_state_name(p, p->lex.state, mesg);
13140 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13141 rb_str_resize(mesg, 0);
13142 append_bitstack_value(p, p->cond_stack, mesg);
13143 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13144 rb_str_resize(mesg, 0);
13145 append_bitstack_value(p, p->cmdarg_stack, mesg);
13146 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13147 if (p->debug_output == rb_ractor_stdout())
13148 p->debug_output = rb_ractor_stderr();
13153rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13155 yylloc->beg_pos.lineno = sourceline;
13156 yylloc->beg_pos.column = beg_pos;
13157 yylloc->end_pos.lineno = sourceline;
13158 yylloc->end_pos.column = end_pos;
13163rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13165 int sourceline = here->sourceline;
13166 int beg_pos = (int)here->offset - here->quote
13167 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13168 int end_pos = (int)here->offset + here->length + here->quote;
13170 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13174rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13176 yylloc->beg_pos.lineno = p->delayed.beg_line;
13177 yylloc->beg_pos.column = p->delayed.beg_col;
13178 yylloc->end_pos.lineno = p->delayed.end_line;
13179 yylloc->end_pos.column = p->delayed.end_col;
13185rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13187 int sourceline = p->ruby_sourceline;
13188 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13189 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13190 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13194rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13196 yylloc->end_pos = yylloc->beg_pos;
13202rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13204 int sourceline = p->ruby_sourceline;
13205 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13206 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13207 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13211rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13213 int sourceline = p->ruby_sourceline;
13214 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13215 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13216 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13218#endif /* !RIPPER */
13221assignable0(struct parser_params *p, ID id, const char **err)
13223 if (!id) return -1;
13226 *err = "Can't change the value of self";
13229 *err = "Can't assign to nil";
13232 *err = "Can't assign to true";
13234 case keyword_false:
13235 *err = "Can't assign to false";
13237 case keyword__FILE__:
13238 *err = "Can't assign to __FILE__";
13240 case keyword__LINE__:
13241 *err = "Can't assign to __LINE__";
13243 case keyword__ENCODING__:
13244 *err = "Can't assign to __ENCODING__";
13247 switch (id_type(id)) {
13249 if (dyna_in_block(p)) {
13250 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13251 compile_error(p, "Can't assign to numbered parameter _%d",
13252 NUMPARAM_ID_TO_IDX(id));
13255 if (dvar_curr(p, id)) return NODE_DASGN;
13256 if (dvar_defined(p, id)) return NODE_DASGN;
13257 if (local_id(p, id)) return NODE_LASGN;
13262 if (!local_id(p, id)) local_var(p, id);
13266 case ID_GLOBAL: return NODE_GASGN;
13267 case ID_INSTANCE: return NODE_IASGN;
13269 if (!p->ctxt.in_def) return NODE_CDECL;
13270 *err = "dynamic constant assignment";
13272 case ID_CLASS: return NODE_CVASGN;
13274 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13281assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13283 const char *err = 0;
13284 int node_type = assignable0(p, id, &err);
13285 switch (node_type) {
13286 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13287 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13288 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13289 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13290 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
13291 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13293 if (err) yyerror1(loc, err);
13294 return NEW_ERROR(loc);
13298assignable(struct parser_params *p, VALUE lhs)
13300 const char *err = 0;
13301 assignable0(p, get_id(lhs), &err);
13302 if (err) lhs = assign_error(p, err, lhs);
13308is_private_local_id(struct parser_params *p, ID name)
13311 if (name == idUScore) return 1;
13312 if (!is_local_id(name)) return 0;
13313 s = rb_id2str(name);
13315 return RSTRING_PTR(s)[0] == '_';
13319shadowing_lvar_0(struct parser_params *p, ID name)
13321 if (dyna_in_block(p)) {
13322 if (dvar_curr(p, name)) {
13323 if (is_private_local_id(p, name)) return 1;
13324 yyerror0("duplicated argument name");
13326 else if (dvar_defined(p, name) || local_id(p, name)) {
13327 vtable_add(p->lvtbl->vars, name);
13328 if (p->lvtbl->used) {
13329 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13335 if (local_id(p, name)) {
13336 if (is_private_local_id(p, name)) return 1;
13337 yyerror0("duplicated argument name");
13344shadowing_lvar(struct parser_params *p, ID name)
13346 shadowing_lvar_0(p, name);
13351new_bv(struct parser_params *p, ID name)
13354 if (!is_local_id(name)) {
13355 compile_error(p, "invalid local variable - %"PRIsVALUE,
13359 if (!shadowing_lvar_0(p, name)) return;
13365aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13367 return NEW_ATTRASGN(recv, tASET, idx, loc);
13371block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13373 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13374 compile_error(p, "both block arg and actual block given");
13379attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13381 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13382 return NEW_ATTRASGN(recv, id, 0, loc);
13386rb_backref_error(struct parser_params *p, NODE *node)
13388 switch (nd_type(node)) {
13390 compile_error(p, "Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13392 case NODE_BACK_REF:
13393 compile_error(p, "Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13399backref_error(struct parser_params *p, NODE *ref, VALUE expr)
13401 VALUE mesg = rb_str_new_cstr("Can't set variable ");
13402 rb_str_append(mesg, RNODE_RIPPER(ref)->nd_cval);
13403 return dispatch2(assign_error, mesg, expr);
13409arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13411 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13412 switch (nd_type(node1)) {
13414 return list_append(p, node1, node2);
13415 case NODE_BLOCK_PASS:
13416 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13417 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13419 case NODE_ARGSPUSH:
13420 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13421 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13422 nd_set_type(node1, NODE_ARGSCAT);
13425 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13426 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13427 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13430 return NEW_ARGSPUSH(node1, node2, loc);
13434arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13436 if (!node2) return node1;
13437 switch (nd_type(node1)) {
13438 case NODE_BLOCK_PASS:
13439 if (RNODE_BLOCK_PASS(node1)->nd_head)
13440 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13442 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13444 case NODE_ARGSPUSH:
13445 if (!nd_type_p(node2, NODE_LIST)) break;
13446 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13447 nd_set_type(node1, NODE_ARGSCAT);
13450 if (!nd_type_p(node2, NODE_LIST) ||
13451 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13452 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13455 return NEW_ARGSCAT(node1, node2, loc);
13459last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13462 if ((n1 = splat_array(args)) != 0) {
13463 return list_append(p, n1, last_arg);
13465 return arg_append(p, args, last_arg, loc);
13469rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13472 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13473 return list_concat(n1, rest_arg);
13475 return arg_concat(p, args, rest_arg, loc);
13479splat_array(NODE* node)
13481 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13482 if (nd_type_p(node, NODE_LIST)) return node;
13487mark_lvar_used(struct parser_params *p, NODE *rhs)
13491 switch (nd_type(rhs)) {
13493 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13494 if (vidp) *vidp |= LVAR_USED;
13498 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13499 if (vidp) *vidp |= LVAR_USED;
13504 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13505 mark_lvar_used(p, rhs->nd_head);
13513const_decl_path(struct parser_params *p, NODE **dest)
13516 if (!nd_type_p(n, NODE_CALL)) {
13517 const YYLTYPE *loc = &n->nd_loc;
13519 if (RNODE_CDECL(n)->nd_vid) {
13520 path = rb_id2str(RNODE_CDECL(n)->nd_vid);
13523 n = RNODE_CDECL(n)->nd_else;
13524 path = rb_ary_new();
13525 for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
13526 rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
13528 if (n && nd_type_p(n, NODE_CONST)) {
13530 rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid));
13532 else if (n && nd_type_p(n, NODE_COLON3)) {
13534 rb_ary_push(path, rb_str_new(0, 0));
13537 // expression::Name
13538 rb_ary_push(path, rb_str_new_cstr("..."));
13540 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
13541 path = rb_fstring(path);
13543 *dest = n = NEW_LIT(path, loc);
13544 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13550make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
13552 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13555 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
13556 NEW_LIST(value, loc), loc);
13559 return NEW_CALL(fcore, rb_intern("make_shareable"),
13560 NEW_LIST(value, loc), loc);
13565ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
13567 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13568 NODE *args = NEW_LIST(value, loc);
13569 args = list_append(p, args, const_decl_path(p, dest));
13570 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
13573static int is_static_content(NODE *node);
13576shareable_literal_value(struct parser_params *p, NODE *node)
13578 if (!node) return Qnil;
13579 enum node_type type = nd_type(node);
13588 return RNODE_LIT(node)->nd_lit;
13594#ifndef SHAREABLE_BARE_EXPRESSION
13595#define SHAREABLE_BARE_EXPRESSION 1
13599shareable_literal_constant(struct parser_params *p, enum shareability shareable,
13600 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
13602# define shareable_literal_constant_next(n) \
13603 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
13606 if (!value) return 0;
13607 enum node_type type = nd_type(value);
13616 if (shareable == shareable_literal) {
13617 value = NEW_CALL(value, idUMinus, 0, loc);
13622 lit = rb_fstring(RNODE_STR(value)->nd_lit);
13623 nd_set_type(value, NODE_LIT);
13624 RB_OBJ_WRITE(p->ast, &RNODE_LIT(value)->nd_lit, lit);
13628 lit = rb_ary_new();
13629 OBJ_FREEZE_RAW(lit);
13630 NODE *n = NEW_LIT(lit, loc);
13631 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13635 lit = rb_ary_new();
13636 for (NODE *n = value; n; n = RNODE_LIST(n)->nd_next) {
13637 NODE *elt = RNODE_LIST(n)->nd_head;
13639 elt = shareable_literal_constant_next(elt);
13641 RNODE_LIST(n)->nd_head = elt;
13643 else if (RTEST(lit)) {
13649 VALUE e = shareable_literal_value(p, elt);
13651 rb_ary_push(lit, e);
13655 lit = Qnil; /* make shareable at runtime */
13662 if (!RNODE_HASH(value)->nd_brace) return 0;
13663 lit = rb_hash_new();
13664 for (NODE *n = RNODE_HASH(value)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
13665 NODE *key = RNODE_LIST(n)->nd_head;
13666 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
13668 key = shareable_literal_constant_next(key);
13670 RNODE_LIST(n)->nd_head = key;
13672 else if (RTEST(lit)) {
13673 rb_hash_clear(lit);
13678 val = shareable_literal_constant_next(val);
13680 RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head = val;
13682 else if (RTEST(lit)) {
13683 rb_hash_clear(lit);
13688 VALUE k = shareable_literal_value(p, key);
13689 VALUE v = shareable_literal_value(p, val);
13690 if (!UNDEF_P(k) && !UNDEF_P(v)) {
13691 rb_hash_aset(lit, k, v);
13694 rb_hash_clear(lit);
13695 lit = Qnil; /* make shareable at runtime */
13702 if (shareable == shareable_literal &&
13703 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
13704 return ensure_shareable_node(p, dest, value, loc);
13709 /* Array or Hash */
13710 if (!lit) return 0;
13712 // if shareable_literal, all elements should have been ensured
13714 value = make_shareable_node(p, value, false, loc);
13717 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
13718 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(value)->nd_lit);
13722# undef shareable_literal_constant_next
13726shareable_constant_value(struct parser_params *p, enum shareability shareable,
13727 NODE *lhs, NODE *value, const YYLTYPE *loc)
13729 if (!value) return 0;
13730 switch (shareable) {
13731 case shareable_none:
13734 case shareable_literal:
13736 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13737 if (lit) return lit;
13742 case shareable_copy:
13743 case shareable_everything:
13745 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13746 if (lit) return lit;
13747 return make_shareable_node(p, value, shareable == shareable_copy, loc);
13752 UNREACHABLE_RETURN(0);
13757node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13759 if (!lhs) return 0;
13761 switch (nd_type(lhs)) {
13763 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13772 set_nd_value(p, lhs, rhs);
13773 nd_set_loc(lhs, loc);
13776 case NODE_ATTRASGN:
13777 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13778 nd_set_loc(lhs, loc);
13782 /* should not happen */
13790value_expr_check(struct parser_params *p, NODE *node)
13792 NODE *void_node = 0, *vn;
13795 rb_warning0("empty expression");
13798 switch (nd_type(node)) {
13804 return void_node ? void_node : node;
13807 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13808 compile_error(p, "unexpected node");
13811 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13814 /* single line pattern matching with "=>" operator */
13815 return void_node ? void_node : node;
13818 while (RNODE_BLOCK(node)->nd_next) {
13819 node = RNODE_BLOCK(node)->nd_next;
13821 node = RNODE_BLOCK(node)->nd_head;
13825 node = RNODE_BEGIN(node)->nd_body;
13830 if (!RNODE_IF(node)->nd_body) {
13833 else if (!RNODE_IF(node)->nd_else) {
13836 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13837 if (!vn) return NULL;
13838 if (!void_node) void_node = vn;
13839 node = RNODE_IF(node)->nd_else;
13844 node = RNODE_AND(node)->nd_1st;
13850 mark_lvar_used(p, node);
13862value_expr_gen(struct parser_params *p, NODE *node)
13864 NODE *void_node = value_expr_check(p, node);
13866 yyerror1(&void_node->nd_loc, "void value expression");
13867 /* or "control never reach"? */
13874void_expr(struct parser_params *p, NODE *node)
13876 const char *useless = 0;
13878 if (!RTEST(ruby_verbose)) return;
13880 if (!node || !(node = nd_once_body(node))) return;
13881 switch (nd_type(node)) {
13883 switch (RNODE_OPCALL(node)->nd_mid) {
13902 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13913 case NODE_BACK_REF:
13914 useless = "a variable";
13917 useless = "a constant";
13923 useless = "a literal";
13948 useless = "defined?";
13953 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13958void_stmts(struct parser_params *p, NODE *node)
13960 NODE *const n = node;
13961 if (!RTEST(ruby_verbose)) return n;
13962 if (!node) return n;
13963 if (!nd_type_p(node, NODE_BLOCK)) return n;
13965 while (RNODE_BLOCK(node)->nd_next) {
13966 void_expr(p, RNODE_BLOCK(node)->nd_head);
13967 node = RNODE_BLOCK(node)->nd_next;
13973remove_begin(NODE *node)
13975 NODE **n = &node, *n1 = node;
13976 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
13977 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13983reduce_nodes(struct parser_params *p, NODE **body)
13985 NODE *node = *body;
13988 *body = NEW_NIL(&NULL_LOC);
13991#define subnodes(type, n1, n2) \
13992 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
13993 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
13994 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
13997 int newline = (int)(nd_fl_newline(node));
13998 switch (nd_type(node)) {
14004 *body = node = RNODE_RETURN(node)->nd_stts;
14005 if (newline && node) nd_set_fl_newline(node);
14008 *body = node = RNODE_BEGIN(node)->nd_body;
14009 if (newline && node) nd_set_fl_newline(node);
14012 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14016 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14019 body = &RNODE_CASE(node)->nd_body;
14022 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14025 if (!subnodes(RNODE_ENSURE, nd_head, nd_resq)) goto end;
14028 newline = 0; // RESBODY should not be a NEWLINE
14029 if (RNODE_RESCUE(node)->nd_else) {
14030 body = &RNODE_RESCUE(node)->nd_resq;
14033 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14039 if (newline && node) nd_set_fl_newline(node);
14046is_static_content(NODE *node)
14048 if (!node) return 1;
14049 switch (nd_type(node)) {
14051 if (!(node = RNODE_HASH(node)->nd_head)) break;
14054 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14055 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14070assign_in_cond(struct parser_params *p, NODE *node)
14072 switch (nd_type(node)) {
14086 if (!get_nd_value(p, node)) return 1;
14087 if (is_static_content(get_nd_value(p, node))) {
14088 /* reports always */
14089 parser_warn(p, get_nd_value(p, node), "found `= literal' in conditional, should be ==");
14100#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14102 case COND_IN_OP: break; \
14103 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14104 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14108static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14111range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14113 enum node_type type;
14115 if (node == 0) return 0;
14117 type = nd_type(node);
14119 if (type == NODE_LIT && FIXNUM_P(RNODE_LIT(node)->nd_lit)) {
14120 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
14121 ID lineno = rb_intern("$.");
14122 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14124 return cond0(p, node, COND_IN_FF, loc, true);
14128cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14130 if (node == 0) return 0;
14131 if (!(node = nd_once_body(node))) return 0;
14132 assign_in_cond(p, node);
14134 switch (nd_type(node)) {
14136 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14142 SWITCH_BY_COND_TYPE(type, warn, "string ");
14146 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14148 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14152 NODE *end = RNODE_BLOCK(node)->nd_end;
14153 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14154 if (top) top = node == end;
14155 *expr = cond0(p, *expr, type, loc, top);
14161 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14162 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14168 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14169 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14170 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
14171 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
14176 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14180 if (RB_TYPE_P(RNODE_LIT(node)->nd_lit, T_REGEXP)) {
14181 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14182 nd_set_type(node, NODE_MATCH);
14184 else if (RNODE_LIT(node)->nd_lit == Qtrue ||
14185 RNODE_LIT(node)->nd_lit == Qfalse) {
14186 /* booleans are OK, e.g., while true */
14188 else if (SYMBOL_P(RNODE_LIT(node)->nd_lit)) {
14192 SWITCH_BY_COND_TYPE(type, warning, "");
14201cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14203 if (node == 0) return 0;
14204 return cond0(p, node, COND_IN_COND, loc, true);
14208method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14210 if (node == 0) return 0;
14211 return cond0(p, node, COND_IN_OP, loc, true);
14215new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14217 YYLTYPE loc = {*pos, *pos};
14218 return NEW_NIL(&loc);
14222new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14224 if (!cc) return right;
14225 cc = cond0(p, cc, COND_IN_COND, loc, true);
14226 return newline_node(NEW_IF(cc, left, right, loc));
14230new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14232 if (!cc) return right;
14233 cc = cond0(p, cc, COND_IN_COND, loc, true);
14234 return newline_node(NEW_UNLESS(cc, left, right, loc));
14237#define NEW_AND_OR(type, f, s, loc) (type == NODE_AND ? NEW_AND(f,s,loc) : NEW_OR(f,s,loc))
14240logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14241 const YYLTYPE *op_loc, const YYLTYPE *loc)
14243 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14246 if (left && nd_type_p(left, type)) {
14247 NODE *node = left, *second;
14248 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14251 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc);
14252 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14253 left->nd_loc.end_pos = loc->end_pos;
14256 op = NEW_AND_OR(type, left, right, loc);
14257 nd_set_line(op, op_loc->beg_pos.lineno);
14264no_blockarg(struct parser_params *p, NODE *node)
14266 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14267 compile_error(p, "block argument should not be given");
14272ret_args(struct parser_params *p, NODE *node)
14275 no_blockarg(p, node);
14276 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14277 node = RNODE_LIST(node)->nd_head;
14284new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14286 if (node) no_blockarg(p, node);
14288 return NEW_YIELD(node, loc);
14292negate_lit(struct parser_params *p, VALUE lit)
14294 if (FIXNUM_P(lit)) {
14295 return LONG2FIX(-FIX2LONG(lit));
14297 if (SPECIAL_CONST_P(lit)) {
14299 if (FLONUM_P(lit)) {
14300 return DBL2NUM(-RFLOAT_VALUE(lit));
14305 switch (BUILTIN_TYPE(lit)) {
14307 bignum_negate(lit);
14308 lit = rb_big_norm(lit);
14311 rational_set_num(lit, negate_lit(p, rational_get_num(lit)));
14314 rcomplex_set_real(lit, negate_lit(p, rcomplex_get_real(lit)));
14315 rcomplex_set_imag(lit, negate_lit(p, rcomplex_get_imag(lit)));
14318 lit = DBL2NUM(-RFLOAT_VALUE(lit));
14322 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
14323 rb_builtin_class_name(lit));
14330arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14333 if (!node1) return (NODE *)node2;
14334 node2->nd_head = node1;
14335 nd_set_first_lineno(node2, nd_first_lineno(node1));
14336 nd_set_first_column(node2, nd_first_column(node1));
14337 return (NODE *)node2;
14343args_info_empty_p(struct rb_args_info *args)
14345 if (args->pre_args_num) return false;
14346 if (args->post_args_num) return false;
14347 if (args->rest_arg) return false;
14348 if (args->opt_args) return false;
14349 if (args->block_arg) return false;
14350 if (args->kw_args) return false;
14351 if (args->kw_rest_arg) return false;
14355static rb_node_args_t *
14356new_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)
14358 struct rb_args_info *args = &tail->nd_ainfo;
14360 if (args->forwarding) {
14362 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14365 rest_arg = idFWD_REST;
14368 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
14369 args->pre_init = pre_args ? pre_args->nd_next : 0;
14371 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
14372 args->post_init = post_args ? post_args->nd_next : 0;
14373 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14375 args->rest_arg = rest_arg;
14377 args->opt_args = opt_args;
14379#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14380 args->ruby2_keywords = args->forwarding;
14382 args->ruby2_keywords = 0;
14385 nd_set_loc(RNODE(tail), loc);
14390static rb_node_args_t *
14391new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14393 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14394 struct rb_args_info *args = &node->nd_ainfo;
14395 if (p->error_p) return node;
14397 args->block_arg = block;
14398 args->kw_args = kw_args;
14402 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14403 * variable order: k1, kr1, k2, &b, internal_id, krest
14405 * variable order: kr1, k1, k2, internal_id, krest, &b
14407 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14408 struct vtable *vtargs = p->lvtbl->args;
14409 rb_node_kw_arg_t *kwn = kw_args;
14411 if (block) block = vtargs->tbl[vtargs->pos-1];
14412 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14413 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14415 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14417 --required_kw_vars;
14418 kwn = kwn->nd_next;
14421 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14422 ID vid = get_nd_vid(p, kwn->nd_body);
14423 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14424 *required_kw_vars++ = vid;
14431 arg_var(p, kw_bits);
14432 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14433 if (block) arg_var(p, block);
14435 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14437 else if (kw_rest_arg == idNil) {
14438 args->no_kwarg = 1;
14440 else if (kw_rest_arg) {
14441 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14447static rb_node_args_t *
14448args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam)
14450 if (max_numparam > NO_PARAM) {
14452 YYLTYPE loc = RUBY_INIT_YYLLOC();
14453 args = new_args_tail(p, 0, 0, 0, 0);
14454 nd_set_loc(RNODE(args), &loc);
14456 args->nd_ainfo.pre_args_num = max_numparam;
14462new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14464 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14467 NODE *pre_args = NEW_LIST(pre_arg, loc);
14468 if (RNODE_ARYPTN(aryptn)->pre_args) {
14469 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14472 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14479new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14482 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14487 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14493new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14495 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14501new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14503 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14504 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14505 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14511new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14513 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14518new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14520 NODE *node, *kw_rest_arg_node;
14522 if (kw_rest_arg == idNil) {
14523 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14525 else if (kw_rest_arg) {
14526 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14529 kw_rest_arg_node = NULL;
14532 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14538dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14543 return NEW_LIT(ID2SYM(idNULL), loc);
14546 switch (nd_type(node)) {
14548 nd_set_type(node, NODE_DSYM);
14549 nd_set_loc(node, loc);
14552 lit = RNODE_STR(node)->nd_lit;
14553 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit = ID2SYM(rb_intern_str(lit)));
14554 nd_set_type(node, NODE_LIT);
14555 nd_set_loc(node, loc);
14558 node = NEW_DSYM(Qnil, 1, NEW_LIST(node, loc), loc);
14565append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
14567 NODE *node = (NODE *)v;
14568 NODE **result = (NODE **)h;
14569 RNODE_LIST(node)->as.nd_alen = 2;
14570 RNODE_LIST(RNODE_LIST(node)->nd_next)->as.nd_end = RNODE_LIST(node)->nd_next;
14571 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next = 0;
14573 list_concat(*result, node);
14576 return ST_CONTINUE;
14580remove_duplicate_keys(struct parser_params *p, NODE *hash)
14582 struct st_hash_type literal_type = {
14587 st_table *literal_keys = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14589 NODE *last_expr = 0;
14590 rb_code_location_t loc = hash->nd_loc;
14591 while (hash && RNODE_LIST(hash)->nd_next) {
14592 NODE *head = RNODE_LIST(hash)->nd_head;
14593 NODE *value = RNODE_LIST(hash)->nd_next;
14594 NODE *next = RNODE_LIST(value)->nd_next;
14595 st_data_t key = (st_data_t)head;
14597 RNODE_LIST(value)->nd_next = 0;
14599 key = (st_data_t)value;
14601 else if (nd_type_p(head, NODE_LIT) &&
14602 st_delete(literal_keys, (key = (st_data_t)RNODE_LIT(head)->nd_lit, &key), &data)) {
14603 NODE *dup_value = (RNODE_LIST((NODE *)data))->nd_next;
14604 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
14605 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
14606 RNODE_LIT(head)->nd_lit, nd_line(head));
14607 if (dup_value == last_expr) {
14608 RNODE_LIST(value)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(value)->nd_head);
14611 RNODE_LIST(last_expr)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(last_expr)->nd_head);
14614 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
14615 last_expr = !head || nd_type_p(head, NODE_LIT) ? value : head;
14618 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
14619 st_free_table(literal_keys);
14621 if (!result) result = hash;
14622 else list_concat(result, hash);
14624 result->nd_loc = loc;
14629new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14631 if (hash) hash = remove_duplicate_keys(p, hash);
14632 return NEW_HASH(hash, loc);
14637error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14639 if (is_private_local_id(p, id)) {
14642 if (st_is_member(p->pvtbl, id)) {
14643 yyerror1(loc, "duplicated variable name");
14646 st_insert(p->pvtbl, (st_data_t)id, 0);
14651error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14654 p->pktbl = st_init_numtable();
14656 else if (st_is_member(p->pktbl, key)) {
14657 yyerror1(loc, "duplicated key name");
14660 st_insert(p->pktbl, (st_data_t)key, 0);
14665new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14667 return NEW_HASH(hash, loc);
14669#endif /* !RIPPER */
14673new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14678 ID vid = get_nd_vid(p, lhs);
14679 YYLTYPE lhs_loc = lhs->nd_loc;
14680 int shareable = ctxt.shareable_constant_value;
14682 switch (nd_type(lhs)) {
14693 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14694 set_nd_value(p, lhs, rhs);
14695 nd_set_loc(lhs, loc);
14696 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14698 else if (op == tANDOP) {
14700 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14702 set_nd_value(p, lhs, rhs);
14703 nd_set_loc(lhs, loc);
14704 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14708 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14710 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14712 set_nd_value(p, asgn, rhs);
14713 nd_set_loc(asgn, loc);
14717 asgn = NEW_ERROR(loc);
14723new_ary_op_assign(struct parser_params *p, NODE *ary,
14724 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
14728 args = make_list(args, args_loc);
14729 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc);
14735new_attr_op_assign(struct parser_params *p, NODE *lhs,
14736 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
14740 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
14746new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14751 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
14752 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
14755 asgn = NEW_ERROR(loc);
14762const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14764 if (p->ctxt.in_def) {
14765 yyerror1(loc, "dynamic constant assignment");
14767 return NEW_CDECL(0, 0, (path), loc);
14771const_decl(struct parser_params *p, VALUE path)
14773 if (p->ctxt.in_def) {
14774 path = assign_error(p, "dynamic constant assignment", path);
14780assign_error(struct parser_params *p, const char *mesg, VALUE a)
14782 a = dispatch2(assign_error, ERR_MESG(), a);
14788var_field(struct parser_params *p, VALUE a)
14790 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
14796new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14798 NODE *result = head;
14800 NODE *tmp = rescue_else ? rescue_else : rescue;
14801 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14803 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14804 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14806 else if (rescue_else) {
14807 result = block_append(p, result, rescue_else);
14810 result = NEW_ENSURE(result, ensure, loc);
14812 fixpos(result, head);
14818warn_unused_var(struct parser_params *p, struct local_vars *local)
14822 if (!local->used) return;
14823 cnt = local->used->pos;
14824 if (cnt != local->vars->pos) {
14825 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14828 ID *v = local->vars->tbl;
14829 ID *u = local->used->tbl;
14830 for (int i = 0; i < cnt; ++i) {
14831 if (!v[i] || (u[i] & LVAR_USED)) continue;
14832 if (is_private_local_id(p, v[i])) continue;
14833 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14839local_push(struct parser_params *p, int toplevel_scope)
14841 struct local_vars *local;
14842 int inherits_dvars = toplevel_scope && compile_for_eval;
14843 int warn_unused_vars = RTEST(ruby_verbose);
14845 local = ALLOC(struct local_vars);
14846 local->prev = p->lvtbl;
14847 local->args = vtable_alloc(0);
14848 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14850 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14851 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14852 local->numparam.outer = 0;
14853 local->numparam.inner = 0;
14854 local->numparam.current = 0;
14856 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14858# if WARN_PAST_SCOPE
14867vtable_chain_free(struct parser_params *p, struct vtable *table)
14869 while (!DVARS_TERMINAL_P(table)) {
14870 struct vtable *cur_table = table;
14871 table = cur_table->prev;
14872 vtable_free(cur_table);
14877local_free(struct parser_params *p, struct local_vars *local)
14879 vtable_chain_free(p, local->used);
14881# if WARN_PAST_SCOPE
14882 vtable_chain_free(p, local->past);
14885 vtable_chain_free(p, local->args);
14886 vtable_chain_free(p, local->vars);
14888 ruby_sized_xfree(local, sizeof(struct local_vars));
14892local_pop(struct parser_params *p)
14894 struct local_vars *local = p->lvtbl->prev;
14895 if (p->lvtbl->used) {
14896 warn_unused_var(p, p->lvtbl);
14899 local_free(p, p->lvtbl);
14907static rb_ast_id_table_t *
14908local_tbl(struct parser_params *p)
14910 int cnt_args = vtable_size(p->lvtbl->args);
14911 int cnt_vars = vtable_size(p->lvtbl->vars);
14912 int cnt = cnt_args + cnt_vars;
14914 rb_ast_id_table_t *tbl;
14916 if (cnt <= 0) return 0;
14917 tbl = rb_ast_new_local_table(p->ast, cnt);
14918 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14919 /* remove IDs duplicated to warn shadowing */
14920 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14921 ID id = p->lvtbl->vars->tbl[i];
14922 if (!vtable_included(p->lvtbl->args, id)) {
14923 tbl->ids[j++] = id;
14927 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14936numparam_name(struct parser_params *p, ID id)
14938 if (!NUMPARAM_ID_P(id)) return;
14939 compile_error(p, "_%d is reserved for numbered parameter",
14940 NUMPARAM_ID_TO_IDX(id));
14944arg_var(struct parser_params *p, ID id)
14946 numparam_name(p, id);
14947 vtable_add(p->lvtbl->args, id);
14951local_var(struct parser_params *p, ID id)
14953 numparam_name(p, id);
14954 vtable_add(p->lvtbl->vars, id);
14955 if (p->lvtbl->used) {
14956 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14961local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14963 struct vtable *vars, *args, *used;
14965 vars = p->lvtbl->vars;
14966 args = p->lvtbl->args;
14967 used = p->lvtbl->used;
14969 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14972 if (used) used = used->prev;
14975 if (vars && vars->prev == DVARS_INHERIT) {
14976 return rb_local_defined(id, p->parent_iseq);
14978 else if (vtable_included(args, id)) {
14982 int i = vtable_included(vars, id);
14983 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
14989local_id(struct parser_params *p, ID id)
14991 return local_id_ref(p, id, NULL);
14995check_forwarding_args(struct parser_params *p)
14997 if (local_id(p, idFWD_ALL)) return TRUE;
14998 compile_error(p, "unexpected ...");
15003add_forwarding_args(struct parser_params *p)
15005 arg_var(p, idFWD_REST);
15006#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15007 arg_var(p, idFWD_KWREST);
15009 arg_var(p, idFWD_BLOCK);
15010 arg_var(p, idFWD_ALL);
15014forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15016 bool conflict = false;
15018 struct vtable *vars, *args;
15020 vars = p->lvtbl->vars;
15021 args = p->lvtbl->args;
15023 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15024 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15029 bool found = false;
15030 if (vars && vars->prev == DVARS_INHERIT && !found) {
15031 found = (rb_local_defined(arg, p->parent_iseq) &&
15032 !(all && rb_local_defined(all, p->parent_iseq)));
15035 found = (vtable_included(args, arg) &&
15036 !(all && vtable_included(args, all)));
15040 compile_error(p, "no anonymous %s parameter", var);
15042 else if (conflict) {
15043 compile_error(p, "anonymous %s parameter is also used within block", var);
15049new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15051 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15052#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15053 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15055 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
15056 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
15057#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15058 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
15060 return arg_blk_pass(args, block);
15065numparam_push(struct parser_params *p)
15068 struct local_vars *local = p->lvtbl;
15069 NODE *inner = local->numparam.inner;
15070 if (!local->numparam.outer) {
15071 local->numparam.outer = local->numparam.current;
15073 local->numparam.inner = 0;
15074 local->numparam.current = 0;
15082numparam_pop(struct parser_params *p, NODE *prev_inner)
15085 struct local_vars *local = p->lvtbl;
15087 /* prefer first one */
15088 local->numparam.inner = prev_inner;
15090 else if (local->numparam.current) {
15091 /* current and inner are exclusive */
15092 local->numparam.inner = local->numparam.current;
15094 if (p->max_numparam > NO_PARAM) {
15095 /* current and outer are exclusive */
15096 local->numparam.current = local->numparam.outer;
15097 local->numparam.outer = 0;
15100 /* no numbered parameter */
15101 local->numparam.current = 0;
15106static const struct vtable *
15107dyna_push(struct parser_params *p)
15109 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15110 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15111 if (p->lvtbl->used) {
15112 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15114 return p->lvtbl->args;
15118dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15120 struct vtable *tmp = *vtblp;
15121 *vtblp = tmp->prev;
15122# if WARN_PAST_SCOPE
15123 if (p->past_scope_enabled) {
15124 tmp->prev = p->lvtbl->past;
15125 p->lvtbl->past = tmp;
15133dyna_pop_1(struct parser_params *p)
15135 struct vtable *tmp;
15137 if ((tmp = p->lvtbl->used) != 0) {
15138 warn_unused_var(p, p->lvtbl);
15139 p->lvtbl->used = p->lvtbl->used->prev;
15142 dyna_pop_vtable(p, &p->lvtbl->args);
15143 dyna_pop_vtable(p, &p->lvtbl->vars);
15147dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15149 while (p->lvtbl->args != lvargs) {
15151 if (!p->lvtbl->args) {
15152 struct local_vars *local = p->lvtbl->prev;
15153 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15161dyna_in_block(struct parser_params *p)
15163 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15167dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15169 struct vtable *vars, *args, *used;
15172 args = p->lvtbl->args;
15173 vars = p->lvtbl->vars;
15174 used = p->lvtbl->used;
15176 while (!DVARS_TERMINAL_P(vars)) {
15177 if (vtable_included(args, id)) {
15180 if ((i = vtable_included(vars, id)) != 0) {
15181 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15186 if (!vidrefp) used = 0;
15187 if (used) used = used->prev;
15190 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15191 return rb_dvar_defined(id, p->parent_iseq);
15198dvar_defined(struct parser_params *p, ID id)
15200 return dvar_defined_ref(p, id, NULL);
15204dvar_curr(struct parser_params *p, ID id)
15206 return (vtable_included(p->lvtbl->args, id) ||
15207 vtable_included(p->lvtbl->vars, id));
15211reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
15214 "regexp encoding option '%c' differs from source encoding '%s'",
15215 c, rb_enc_name(rb_enc_get(str)));
15220rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15222 int c = RE_OPTION_ENCODING_IDX(options);
15226 rb_char_to_option_kcode(c, &opt, &idx);
15227 if (idx != ENCODING_GET(str) &&
15228 !is_ascii_string(str)) {
15231 ENCODING_SET(str, idx);
15233 else if (RE_OPTION_ENCODING_NONE(options)) {
15234 if (!ENCODING_IS_ASCII8BIT(str) &&
15235 !is_ascii_string(str)) {
15239 rb_enc_associate(str, rb_ascii8bit_encoding());
15241 else if (rb_is_usascii_enc(p->enc)) {
15242 if (!is_ascii_string(str)) {
15243 /* raise in re.c */
15244 rb_enc_associate(str, rb_usascii_encoding());
15247 rb_enc_associate(str, rb_ascii8bit_encoding());
15257reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15259 int c = rb_reg_fragment_setenc(p, str, options);
15260 if (c) reg_fragment_enc_error(p, str, c);
15264reg_fragment_check(struct parser_params* p, VALUE str, int options)
15267 reg_fragment_setenc(p, str, options);
15268 err = rb_reg_check_preprocess(str);
15270 err = rb_obj_as_string(err);
15271 compile_error(p, "%"PRIsVALUE, err);
15277#ifndef UNIVERSAL_PARSER
15279 struct parser_params* parser;
15282 const YYLTYPE *loc;
15283} reg_named_capture_assign_t;
15286reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15287 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15289 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15290 struct parser_params* p = arg->parser;
15291 rb_encoding *enc = arg->enc;
15292 long len = name_end - name;
15293 const char *s = (const char *)name;
15295 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc);
15299reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
15301 reg_named_capture_assign_t arg;
15304 arg.enc = rb_enc_get(regexp);
15305 arg.succ_block = 0;
15307 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15309 if (!arg.succ_block) return 0;
15310 return RNODE_BLOCK(arg.succ_block)->nd_next;
15315rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15316 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc)
15321 if (!len) return ST_CONTINUE;
15322 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15323 return ST_CONTINUE;
15325 var = intern_cstr(s, len, enc);
15326 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15327 if (!lvar_defined(p, var)) return ST_CONTINUE;
15329 node = node_assign(p, assignable(p, var, 0, loc), NEW_LIT(ID2SYM(var), loc), NO_LEX_CTXT, loc);
15330 succ = *succ_block;
15331 if (!succ) succ = NEW_ERROR(loc);
15332 succ = block_append(p, succ, node);
15333 *succ_block = succ;
15334 return ST_CONTINUE;
15338parser_reg_compile(struct parser_params* p, VALUE str, int options)
15340 reg_fragment_setenc(p, str, options);
15341 return rb_parser_reg_compile(p, str, options);
15345rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15347 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15351reg_compile(struct parser_params* p, VALUE str, int options)
15356 err = rb_errinfo();
15357 re = parser_reg_compile(p, str, options);
15359 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15360 rb_set_errinfo(err);
15361 compile_error(p, "%"PRIsVALUE, m);
15368parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
15370 VALUE err = rb_errinfo();
15372 str = ripper_is_node_yylval(p, str) ? RNODE_RIPPER(str)->nd_cval : str;
15373 int c = rb_reg_fragment_setenc(p, str, options);
15374 if (c) reg_fragment_enc_error(p, str, c);
15375 re = rb_parser_reg_compile(p, str, options);
15377 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
15378 rb_set_errinfo(err);
15386rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15388 p->do_print = print;
15390 p->do_chomp = chomp;
15391 p->do_split = split;
15395parser_append_options(struct parser_params *p, NODE *node)
15397 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15398 const YYLTYPE *const LOC = &default_location;
15401 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15402 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15404 node = block_append(p, node, print);
15408 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15411 ID ifs = rb_intern("$;");
15412 ID fields = rb_intern("$F");
15413 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15414 NODE *split = NEW_GASGN(fields,
15415 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15416 rb_intern("split"), args, LOC),
15418 node = block_append(p, split, node);
15421 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
15422 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15423 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15426 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
15435 /* just to suppress unused-function warnings */
15441internal_id(struct parser_params *p)
15443 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15445#endif /* !RIPPER */
15448parser_initialize(struct parser_params *p)
15450 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15451 p->command_start = TRUE;
15452 p->ruby_sourcefile_string = Qnil;
15453 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15455 p->delayed.token = Qnil;
15456 p->frozen_string_literal = -1; /* not specified */
15459 p->parsing_thread = Qnil;
15461 p->error_buffer = Qfalse;
15462 p->end_expect_token_locations = Qnil;
15466 p->debug_buffer = Qnil;
15467 p->debug_output = rb_ractor_stdout();
15468 p->enc = rb_utf8_encoding();
15473#define rb_ruby_parser_mark ripper_parser_mark
15474#define rb_ruby_parser_free ripper_parser_free
15475#define rb_ruby_parser_memsize ripper_parser_memsize
15479rb_ruby_parser_mark(void *ptr)
15481 struct parser_params *p = (struct parser_params*)ptr;
15483 rb_gc_mark(p->lex.input);
15484 rb_gc_mark(p->lex.lastline);
15485 rb_gc_mark(p->lex.nextline);
15486 rb_gc_mark(p->ruby_sourcefile_string);
15487 rb_gc_mark((VALUE)p->ast);
15488 rb_gc_mark(p->case_labels);
15489 rb_gc_mark(p->delayed.token);
15491 rb_gc_mark(p->debug_lines);
15492 rb_gc_mark(p->error_buffer);
15493 rb_gc_mark(p->end_expect_token_locations);
15494 rb_gc_mark(p->tokens);
15496 rb_gc_mark(p->value);
15497 rb_gc_mark(p->result);
15498 rb_gc_mark(p->parsing_thread);
15500 rb_gc_mark(p->debug_buffer);
15501 rb_gc_mark(p->debug_output);
15503 rb_gc_mark((VALUE)p->heap);
15508rb_ruby_parser_free(void *ptr)
15510 struct parser_params *p = (struct parser_params*)ptr;
15511 struct local_vars *local, *prev;
15512#ifdef UNIVERSAL_PARSER
15513 rb_parser_config_t *config = p->config;
15517 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15520 for (local = p->lvtbl; local; local = prev) {
15521 prev = local->prev;
15522 local_free(p, local);
15526 token_info *ptinfo;
15527 while ((ptinfo = p->token_info) != 0) {
15528 p->token_info = ptinfo->next;
15534#ifdef UNIVERSAL_PARSER
15536 if (config->counter <= 0) {
15537 rb_ruby_parser_config_free(config);
15543rb_ruby_parser_memsize(const void *ptr)
15545 struct parser_params *p = (struct parser_params*)ptr;
15546 struct local_vars *local;
15547 size_t size = sizeof(*p);
15550 for (local = p->lvtbl; local; local = local->prev) {
15551 size += sizeof(*local);
15552 if (local->vars) size += local->vars->capa * sizeof(ID);
15557#ifdef UNIVERSAL_PARSER
15558rb_parser_config_t *
15559rb_ruby_parser_config_new(void *(*malloc)(size_t size))
15561 return (rb_parser_config_t *)malloc(sizeof(rb_parser_config_t));
15565rb_ruby_parser_config_free(rb_parser_config_t *config)
15567 config->free(config);
15571#ifndef UNIVERSAL_PARSER
15573static const rb_data_type_t parser_data_type = {
15576 rb_ruby_parser_mark,
15577 rb_ruby_parser_free,
15578 rb_ruby_parser_memsize,
15580 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
15586#undef rb_reserved_word
15588const struct kwtable *
15589rb_reserved_word(const char *str, unsigned int len)
15591 return reserved_word(str, len);
15594#ifdef UNIVERSAL_PARSER
15596rb_ruby_parser_allocate(rb_parser_config_t *config)
15598 /* parser_initialize expects fields to be set to 0 */
15599 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15600 p->config = config;
15601 p->config->counter++;
15606rb_ruby_parser_new(rb_parser_config_t *config)
15608 /* parser_initialize expects fields to be set to 0 */
15609 rb_parser_t *p = rb_ruby_parser_allocate(config);
15610 parser_initialize(p);
15616rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15618 p->error_buffer = main ? Qfalse : Qnil;
15619 p->parent_iseq = base;
15624rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines)
15626 if (!RTEST(lines)) {
15629 else if (lines == Qtrue) {
15630 lines = rb_ary_new();
15633 Check_Type(lines, T_ARRAY);
15634 rb_ary_modify(lines);
15636 p->debug_lines = lines;
15640rb_ruby_parser_error_tolerant(rb_parser_t *p)
15642 p->error_tolerant = 1;
15644 p->end_expect_token_locations = rb_ary_new();
15648rb_ruby_parser_keep_tokens(rb_parser_t *p)
15650 p->keep_tokens = 1;
15652 p->tokens = rb_ary_new();
15655#ifndef UNIVERSAL_PARSER
15657rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
15659 struct parser_params *p;
15661 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15662 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15663 return rb_ruby_parser_compile_file_path(p, fname, file, start);
15667rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
15669 struct parser_params *p;
15671 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15672 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15673 return rb_ruby_parser_compile_generic(p, lex_gets, fname, input, start);
15677rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
15679 struct parser_params *p;
15681 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15682 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15683 return rb_ruby_parser_compile_string(p, f, s, line);
15687rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
15689 struct parser_params *p;
15691 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15692 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15693 return rb_ruby_parser_compile_string_path(p, f, s, line);
15697rb_parser_encoding(VALUE vparser)
15699 struct parser_params *p;
15701 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15702 return rb_ruby_parser_encoding(p);
15706rb_parser_end_seen_p(VALUE vparser)
15708 struct parser_params *p;
15710 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15711 return RBOOL(rb_ruby_parser_end_seen_p(p));
15715rb_parser_error_tolerant(VALUE vparser)
15717 struct parser_params *p;
15719 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15720 rb_ruby_parser_error_tolerant(p);
15724rb_parser_set_script_lines(VALUE vparser, VALUE lines)
15726 struct parser_params *p;
15728 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15729 rb_ruby_parser_set_script_lines(p, lines);
15733rb_parser_keep_tokens(VALUE vparser)
15735 struct parser_params *p;
15737 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15738 rb_ruby_parser_keep_tokens(p);
15744 struct parser_params *p;
15745 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
15746 &parser_data_type, p);
15747 parser_initialize(p);
15752rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
15754 struct parser_params *p;
15756 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15757 rb_ruby_parser_set_context(p, base, main);
15762rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
15764 struct parser_params *p;
15766 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15767 rb_ruby_parser_set_options(p, print, loop, chomp, split);
15771rb_parser_set_yydebug(VALUE self, VALUE flag)
15773 struct parser_params *p;
15775 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
15776 rb_ruby_parser_set_yydebug(p, RTEST(flag));
15779#endif /* !UNIVERSAL_PARSER */
15782rb_ruby_parser_encoding(rb_parser_t *p)
15784 return rb_enc_from_encoding(p->enc);
15788rb_ruby_parser_end_seen_p(rb_parser_t *p)
15790 return p->ruby__end__seen;
15794rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15799#endif /* !RIPPER */
15803rb_ruby_parser_get_yydebug(rb_parser_t *p)
15809rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15815rb_ruby_parser_error_p(rb_parser_t *p)
15821rb_ruby_parser_debug_output(rb_parser_t *p)
15823 return p->debug_output;
15827rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15829 p->debug_output = output;
15833rb_ruby_parser_parsing_thread(rb_parser_t *p)
15835 return p->parsing_thread;
15839rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15841 p->parsing_thread = parsing_thread;
15845rb_ruby_parser_ripper_initialize(rb_parser_t *p, VALUE (*gets)(struct parser_params*,VALUE), VALUE input, VALUE sourcefile_string, const char *sourcefile, int sourceline)
15847 p->lex.gets = gets;
15848 p->lex.input = input;
15850 p->ruby_sourcefile_string = sourcefile_string;
15851 p->ruby_sourcefile = sourcefile;
15852 p->ruby_sourceline = sourceline;
15856rb_ruby_parser_result(rb_parser_t *p)
15862rb_ruby_parser_enc(rb_parser_t *p)
15868rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15870 return p->ruby_sourcefile_string;
15874rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15876 return p->ruby_sourceline;
15880rb_ruby_parser_lex_state(rb_parser_t *p)
15882 return p->lex.state;
15886rb_ruby_ripper_parse0(rb_parser_t *p)
15889 p->ast = rb_ast_new();
15890 ripper_yyparse((void*)p);
15891 rb_ast_dispose(p->ast);
15896rb_ruby_ripper_dedent_string(rb_parser_t *p, VALUE string, int width)
15898 return dedent_string(p, string, width);
15902rb_ruby_ripper_lex_get_str(rb_parser_t *p, VALUE s)
15904 return lex_get_str(p, s);
15908rb_ruby_ripper_initialized_p(rb_parser_t *p)
15910 return p->lex.input != 0;
15914rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15916 parser_initialize(p);
15920rb_ruby_ripper_column(rb_parser_t *p)
15922 return p->lex.ptok - p->lex.pbeg;
15926rb_ruby_ripper_token_len(rb_parser_t *p)
15928 return p->lex.pcur - p->lex.ptok;
15932rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15934 return p->lex.lastline;
15938rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15940 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15943struct parser_params*
15944rb_ruby_ripper_parser_allocate(void)
15946 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15952#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
15953/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
15954 * potential memory leak */
15955#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
15956#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
15957 (new)->cnt = (cnt), (ptr))
15960rb_parser_malloc(struct parser_params *p, size_t size)
15962 size_t cnt = HEAPCNT(1, size);
15963 rb_imemo_tmpbuf_t *n = NEWHEAP();
15964 void *ptr = xmalloc(size);
15966 return ADD2HEAP(n, cnt, ptr);
15970rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
15972 size_t cnt = HEAPCNT(nelem, size);
15973 rb_imemo_tmpbuf_t *n = NEWHEAP();
15974 void *ptr = xcalloc(nelem, size);
15976 return ADD2HEAP(n, cnt, ptr);
15980rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
15982 rb_imemo_tmpbuf_t *n;
15983 size_t cnt = HEAPCNT(1, size);
15985 if (ptr && (n = p->heap) != NULL) {
15987 if (n->ptr == ptr) {
15988 n->ptr = ptr = xrealloc(ptr, size);
15989 if (n->cnt) n->cnt = cnt;
15992 } while ((n = n->next) != NULL);
15995 ptr = xrealloc(ptr, size);
15996 return ADD2HEAP(n, cnt, ptr);
16000rb_parser_free(struct parser_params *p, void *ptr)
16002 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
16004 while ((n = *prev) != NULL) {
16005 if (n->ptr == ptr) {
16015rb_parser_printf(struct parser_params *p, const char *fmt, ...)
16018 VALUE mesg = p->debug_buffer;
16020 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
16022 rb_str_vcatf(mesg, fmt, ap);
16024 if (end_with_newline_p(p, mesg)) {
16025 rb_io_write(p->debug_output, mesg);
16026 p->debug_buffer = Qnil;
16031parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16034 int lineno, column;
16037 lineno = loc->end_pos.lineno;
16038 column = loc->end_pos.column;
16041 lineno = p->ruby_sourceline;
16042 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
16045 rb_io_flush(p->debug_output);
16049 rb_syntax_error_append(p->error_buffer,
16050 p->ruby_sourcefile_string,
16057count_char(const char *str, int c)
16060 while (str[n] == c) ++n;
16065 * strip enclosing double-quotes, same as the default yytnamerr except
16066 * for that single-quotes matching back-quotes do not stop stripping.
16068 * "\"`class' keyword\"" => "`class' keyword"
16070RUBY_FUNC_EXPORTED size_t
16071rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16073 if (*yystr == '"') {
16074 size_t yyn = 0, bquote = 0;
16075 const char *yyp = yystr;
16081 bquote = count_char(yyp+1, '`') + 1;
16082 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16090 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16091 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16097 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16098 if (yyres) memcpy(yyres + yyn, yyp, 3);
16103 goto do_not_strip_quotes;
16106 goto do_not_strip_quotes;
16109 if (*++yyp != '\\')
16110 goto do_not_strip_quotes;
16111 /* Fall through. */
16126 do_not_strip_quotes: ;
16129 if (!yyres) return strlen(yystr);
16131 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16139ripper_validate_object(VALUE self, VALUE x)
16141 if (x == Qfalse) return x;
16142 if (x == Qtrue) return x;
16143 if (NIL_P(x)) return x;
16145 rb_raise(rb_eArgError, "Qundef given");
16146 if (FIXNUM_P(x)) return x;
16147 if (SYMBOL_P(x)) return x;
16148 switch (BUILTIN_TYPE(x)) {
16158 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
16159 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
16161 x = ((NODE *)x)->nd_rval;
16164 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
16165 (void *)x, rb_obj_classname(x));
16167 if (!RBASIC_CLASS(x)) {
16168 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
16169 (void *)x, rb_builtin_type_name(TYPE(x)));
16175#define validate(x) ((x) = get_value(x))
16178ripper_dispatch0(struct parser_params *p, ID mid)
16180 return rb_funcall(p->value, mid, 0);
16184ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16187 return rb_funcall(p->value, mid, 1, a);
16191ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16195 return rb_funcall(p->value, mid, 2, a, b);
16199ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16204 return rb_funcall(p->value, mid, 3, a, b, c);
16208ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16214 return rb_funcall(p->value, mid, 4, a, b, c, d);
16218ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16225 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16229ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16238 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16242ripper_error(struct parser_params *p)
16248ripper_value(struct parser_params *p)
16250 (void)yystpcpy; /* may not used in newer bison */
16259 * c-file-style: "ruby"