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) goto eof;
8051 else if (!ISASCII(c)) {
8056 int c2 = escaped_control_code(c);
8058 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8059 WARN_SPACE_CHAR(c2, "\\M-");
8062 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8065 else if (ISCNTRL(c)) goto eof;
8066 return ((c & 0xff) | 0x80);
8070 if ((c = nextc(p)) != '-') {
8074 if (flags & ESCAPE_CONTROL) goto eof;
8075 if ((c = nextc(p))== '\\') {
8081 c = read_escape(p, flags|ESCAPE_CONTROL);
8085 else if (c == -1) goto eof;
8086 else if (!ISASCII(c)) {
8091 int c2 = escaped_control_code(c);
8094 if (flags & ESCAPE_META) {
8095 WARN_SPACE_CHAR(c2, "\\M-");
8098 WARN_SPACE_CHAR(c2, "");
8102 if (flags & ESCAPE_META) {
8103 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8106 WARN_SPACE_CHAR(c2, "\\C-");
8110 else if (ISCNTRL(c)) goto eof;
8116 yyerror0("Invalid escape character syntax");
8117 dispatch_scan_event(p, tSTRING_CONTENT);
8126tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8128 int len = rb_enc_codelen(c, enc);
8129 rb_enc_mbcput(c, tokspace(p, len), enc);
8133tokadd_escape(struct parser_params *p)
8138 switch (c = nextc(p)) {
8140 return 0; /* just ignore */
8142 case '0': case '1': case '2': case '3': /* octal constant */
8143 case '4': case '5': case '6': case '7':
8145 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8146 if (numlen == 0) goto eof;
8147 p->lex.pcur += numlen;
8148 tokcopy(p, (int)numlen + 1);
8152 case 'x': /* hex constant */
8154 tok_hex(p, &numlen);
8155 if (numlen == 0) return -1;
8156 tokcopy(p, (int)numlen + 2);
8162 yyerror0("Invalid escape character syntax");
8174regx_options(struct parser_params *p)
8182 while (c = nextc(p), ISALPHA(c)) {
8184 options |= RE_OPTION_ONCE;
8186 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
8188 if (kc != rb_ascii8bit_encindex()) kcode = c;
8202 YYLTYPE loc = RUBY_INIT_YYLLOC();
8204 compile_error(p, "unknown regexp option%s - %*s",
8205 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8206 parser_show_error_line(p, &loc);
8208 return options | RE_OPTION_ENCODING(kcode);
8212tokadd_mbchar(struct parser_params *p, int c)
8214 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8215 if (len < 0) return -1;
8217 p->lex.pcur += --len;
8218 if (len > 0) tokcopy(p, len);
8223simple_re_meta(int c)
8226 case '$': case '*': case '+': case '.':
8227 case '?': case '^': case '|':
8228 case ')': case ']': case '}': case '>':
8236parser_update_heredoc_indent(struct parser_params *p, int c)
8238 if (p->heredoc_line_indent == -1) {
8239 if (c == '\n') p->heredoc_line_indent = 0;
8243 p->heredoc_line_indent++;
8246 else if (c == '\t') {
8247 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8248 p->heredoc_line_indent = w * TAB_WIDTH;
8251 else if (c != '\n') {
8252 if (p->heredoc_indent > p->heredoc_line_indent) {
8253 p->heredoc_indent = p->heredoc_line_indent;
8255 p->heredoc_line_indent = -1;
8262parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8264 YYLTYPE loc = RUBY_INIT_YYLLOC();
8265 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8266 compile_error(p, "%s mixed within %s source", n1, n2);
8267 parser_show_error_line(p, &loc);
8271parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8273 const char *pos = p->lex.pcur;
8275 parser_mixed_error(p, enc1, enc2);
8280nibble_char_upper(unsigned int c)
8283 return c + (c < 10 ? '0' : 'A' - 10);
8287tokadd_string(struct parser_params *p,
8288 int func, int term, int paren, long *nest,
8289 rb_encoding **encp, rb_encoding **enc)
8294 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8295 int top_of_line = FALSE;
8298#define mixed_error(enc1, enc2) \
8299 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8300#define mixed_escape(beg, enc1, enc2) \
8301 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8303 while ((c = nextc(p)) != -1) {
8304 if (p->heredoc_indent > 0) {
8305 parser_update_heredoc_indent(p, c);
8308 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8314 if (paren && c == paren) {
8317 else if (c == term) {
8318 if (!nest || !*nest) {
8324 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8325 unsigned char c2 = *p->lex.pcur;
8326 if (c2 == '$' || c2 == '@' || c2 == '{') {
8331 else if (c == '\\') {
8335 if (func & STR_FUNC_QWORDS) break;
8336 if (func & STR_FUNC_EXPAND) {
8337 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8348 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8352 if ((func & STR_FUNC_EXPAND) == 0) {
8356 tokadd_utf8(p, enc, term,
8357 func & STR_FUNC_SYMBOL,
8358 func & STR_FUNC_REGEXP);
8362 if (c == -1) return -1;
8364 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8367 if (func & STR_FUNC_REGEXP) {
8373 c = read_escape(p, 0);
8375 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8378 *t++ = nibble_char_upper(c >> 4);
8379 *t++ = nibble_char_upper(c);
8384 if (c == term && !simple_re_meta(c)) {
8389 if ((c = tokadd_escape(p)) < 0)
8391 if (*enc && *enc != *encp) {
8392 mixed_escape(p->lex.ptok+2, *enc, *encp);
8396 else if (func & STR_FUNC_EXPAND) {
8398 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8399 c = read_escape(p, 0);
8401 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8402 /* ignore backslashed spaces in %w */
8404 else if (c != term && !(paren && c == paren)) {
8411 else if (!parser_isascii(p)) {
8416 else if (*enc != *encp) {
8417 mixed_error(*enc, *encp);
8420 if (tokadd_mbchar(p, c) == -1) return -1;
8423 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8431 else if (*enc != *encp) {
8432 mixed_error(*enc, *encp);
8438 top_of_line = (c == '\n');
8442 if (*enc) *encp = *enc;
8446#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8450flush_string_content(struct parser_params *p, rb_encoding *enc)
8452 VALUE content = yylval.val;
8453 if (!ripper_is_node_yylval(p, content))
8454 content = ripper_new_yylval(p, 0, 0, content);
8455 if (has_delayed_token(p)) {
8456 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8458 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8460 dispatch_delayed_token(p, tSTRING_CONTENT);
8461 p->lex.ptok = p->lex.pcur;
8462 RNODE_RIPPER(content)->nd_rval = yylval.val;
8464 dispatch_scan_event(p, tSTRING_CONTENT);
8465 if (yylval.val != content)
8466 RNODE_RIPPER(content)->nd_rval = yylval.val;
8467 yylval.val = content;
8471flush_string_content(struct parser_params *p, rb_encoding *enc)
8473 if (has_delayed_token(p)) {
8474 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8476 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8477 p->delayed.end_line = p->ruby_sourceline;
8478 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8480 dispatch_delayed_token(p, tSTRING_CONTENT);
8481 p->lex.ptok = p->lex.pcur;
8483 dispatch_scan_event(p, tSTRING_CONTENT);
8487RUBY_FUNC_EXPORTED const uint_least32_t ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
8488/* this can be shared with ripper, since it's independent from struct
8491#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8492#define SPECIAL_PUNCT(idx) ( \
8493 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8494 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8495 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8496 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8497 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8499const uint_least32_t ruby_global_name_punct_bits[] = {
8508static enum yytokentype
8509parser_peek_variable_name(struct parser_params *p)
8512 const char *ptr = p->lex.pcur;
8514 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8518 if ((c = *ptr) == '-') {
8519 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8522 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8523 return tSTRING_DVAR;
8527 if ((c = *ptr) == '@') {
8528 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8534 p->command_start = TRUE;
8535 return tSTRING_DBEG;
8539 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8540 return tSTRING_DVAR;
8544#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8545#define IS_END() IS_lex_state(EXPR_END_ANY)
8546#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8547#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8548#define IS_LABEL_POSSIBLE() (\
8549 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8551#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8552#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8554static inline enum yytokentype
8555parser_string_term(struct parser_params *p, int func)
8557 xfree(p->lex.strterm);
8559 if (func & STR_FUNC_REGEXP) {
8560 set_yylval_num(regx_options(p));
8561 dispatch_scan_event(p, tREGEXP_END);
8562 SET_LEX_STATE(EXPR_END);
8565 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8567 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8570 SET_LEX_STATE(EXPR_END);
8574static enum yytokentype
8575parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8577 int func = quote->func;
8578 int term = quote->term;
8579 int paren = quote->paren;
8581 rb_encoding *enc = p->enc;
8582 rb_encoding *base_enc = 0;
8585 if (func & STR_FUNC_TERM) {
8586 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8587 SET_LEX_STATE(EXPR_END);
8588 xfree(p->lex.strterm);
8590 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8593 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8594 while (c != '\n' && ISSPACE(c = nextc(p)));
8597 if (func & STR_FUNC_LIST) {
8598 quote->func &= ~STR_FUNC_LIST;
8601 if (c == term && !quote->nest) {
8602 if (func & STR_FUNC_QWORDS) {
8603 quote->func |= STR_FUNC_TERM;
8604 pushback(p, c); /* dispatch the term at tSTRING_END */
8605 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8608 return parser_string_term(p, func);
8611 if (!ISSPACE(c)) pushback(p, c);
8612 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8616 if ((func & STR_FUNC_EXPAND) && c == '#') {
8617 enum yytokentype t = parser_peek_variable_name(p);
8623 if (tokadd_string(p, func, term, paren, "e->nest,
8624 &enc, &base_enc) == -1) {
8627# define unterminated_literal(mesg) yyerror0(mesg)
8629# define unterminated_literal(mesg) compile_error(p, mesg)
8631 literal_flush(p, p->lex.pcur);
8632 if (func & STR_FUNC_QWORDS) {
8633 /* no content to add, bailing out here */
8634 unterminated_literal("unterminated list meets end of file");
8635 xfree(p->lex.strterm);
8639 if (func & STR_FUNC_REGEXP) {
8640 unterminated_literal("unterminated regexp meets end of file");
8643 unterminated_literal("unterminated string meets end of file");
8645 quote->func |= STR_FUNC_TERM;
8650 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8651 set_yylval_str(lit);
8652 flush_string_content(p, enc);
8654 return tSTRING_CONTENT;
8657static enum yytokentype
8658heredoc_identifier(struct parser_params *p)
8661 * term_len is length of `<<"END"` except `END`,
8662 * in this case term_len is 4 (<, <, " and ").
8664 long len, offset = p->lex.pcur - p->lex.pbeg;
8665 int c = nextc(p), term, func = 0, quote = 0;
8666 enum yytokentype token = tSTRING_BEG;
8671 func = STR_FUNC_INDENT;
8674 else if (c == '~') {
8676 func = STR_FUNC_INDENT;
8682 func |= str_squote; goto quoted;
8684 func |= str_dquote; goto quoted;
8686 token = tXSTRING_BEG;
8687 func |= str_xquote; goto quoted;
8694 while ((c = nextc(p)) != term) {
8695 if (c == -1 || c == '\r' || c == '\n') {
8696 yyerror0("unterminated here document identifier");
8703 if (!parser_is_identchar(p)) {
8705 if (func & STR_FUNC_INDENT) {
8706 pushback(p, indent > 0 ? '~' : '-');
8712 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8713 if (n < 0) return 0;
8715 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8720 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8721 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8722 yyerror0("too long here document identifier");
8723 dispatch_scan_event(p, tHEREDOC_BEG);
8726 p->lex.strterm = new_heredoc(p);
8727 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8728 here->offset = offset;
8729 here->sourceline = p->ruby_sourceline;
8730 here->length = (unsigned)len;
8731 here->quote = quote;
8733 here->lastline = p->lex.lastline;
8734 rb_ast_add_mark_object(p->ast, p->lex.lastline);
8737 p->heredoc_indent = indent;
8738 p->heredoc_line_indent = 0;
8743heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8746 rb_strterm_t *term = p->lex.strterm;
8749 line = here->lastline;
8750 p->lex.lastline = line;
8751 p->lex.pbeg = RSTRING_PTR(line);
8752 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8753 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8754 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8755 p->heredoc_end = p->ruby_sourceline;
8756 p->ruby_sourceline = (int)here->sourceline;
8757 if (p->eofp) p->lex.nextline = Qnil;
8760 rb_ast_delete_mark_object(p->ast, line);
8764dedent_string(struct parser_params *p, VALUE string, int width)
8770 RSTRING_GETMEM(string, str, len);
8771 for (i = 0; i < len && col < width; i++) {
8772 if (str[i] == ' ') {
8775 else if (str[i] == '\t') {
8776 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8777 if (n > width) break;
8785 rb_str_modify(string);
8786 str = RSTRING_PTR(string);
8787 if (RSTRING_LEN(string) != len)
8788 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8789 MEMMOVE(str, str + i, char, len - i);
8790 rb_str_set_len(string, len - i);
8796heredoc_dedent(struct parser_params *p, NODE *root)
8798 NODE *node, *str_node, *prev_node;
8799 int indent = p->heredoc_indent;
8802 if (indent <= 0) return root;
8803 p->heredoc_indent = 0;
8804 if (!root) return root;
8806 prev_node = node = str_node = root;
8807 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8810 VALUE lit = RNODE_LIT(str_node)->nd_lit;
8811 if (nd_fl_newline(str_node)) {
8812 dedent_string(p, lit, indent);
8817 else if (!literal_concat0(p, prev_lit, lit)) {
8821 NODE *end = RNODE_LIST(node)->as.nd_end;
8822 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8824 if (nd_type_p(prev_node, NODE_DSTR))
8825 nd_set_type(prev_node, NODE_STR);
8828 RNODE_LIST(node)->as.nd_end = end;
8833 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8835 if (!nd_type_p(node, NODE_LIST)) break;
8836 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8837 enum node_type type = nd_type(str_node);
8838 if (type == NODE_STR || type == NODE_DSTR) break;
8848heredoc_dedent(struct parser_params *p, VALUE array)
8850 int indent = p->heredoc_indent;
8852 if (indent <= 0) return array;
8853 p->heredoc_indent = 0;
8854 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8860whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8862 const char *beg = p->lex.pbeg;
8863 const char *ptr = p->lex.pend;
8865 if (ptr - beg < len) return FALSE;
8866 if (ptr > beg && ptr[-1] == '\n') {
8867 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8868 if (ptr - beg < len) return FALSE;
8870 if (strncmp(eos, ptr -= len, len)) return FALSE;
8872 while (beg < ptr && ISSPACE(*beg)) beg++;
8878word_match_p(struct parser_params *p, const char *word, long len)
8880 if (strncmp(p->lex.pcur, word, len)) return 0;
8881 if (lex_eol_n_p(p, len)) return 1;
8882 int c = (unsigned char)p->lex.pcur[len];
8883 if (ISSPACE(c)) return 1;
8885 case '\0': case '\004': case '\032': return 1;
8890#define NUM_SUFFIX_R (1<<0)
8891#define NUM_SUFFIX_I (1<<1)
8892#define NUM_SUFFIX_ALL 3
8895number_literal_suffix(struct parser_params *p, int mask)
8898 const char *lastp = p->lex.pcur;
8900 while ((c = nextc(p)) != -1) {
8901 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8902 result |= (mask & NUM_SUFFIX_I);
8903 mask &= ~NUM_SUFFIX_I;
8904 /* r after i, rational of complex is disallowed */
8905 mask &= ~NUM_SUFFIX_R;
8908 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8909 result |= (mask & NUM_SUFFIX_R);
8910 mask &= ~NUM_SUFFIX_R;
8913 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8914 p->lex.pcur = lastp;
8915 literal_flush(p, p->lex.pcur);
8924static enum yytokentype
8925set_number_literal(struct parser_params *p, VALUE v,
8926 enum yytokentype type, int suffix)
8928 if (suffix & NUM_SUFFIX_I) {
8929 v = rb_complex_raw(INT2FIX(0), v);
8932 set_yylval_literal(v);
8933 SET_LEX_STATE(EXPR_END);
8937static enum yytokentype
8938set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8940 enum yytokentype type = tINTEGER;
8941 if (suffix & NUM_SUFFIX_R) {
8942 v = rb_rational_raw1(v);
8945 return set_number_literal(p, v, type, suffix);
8950dispatch_heredoc_end(struct parser_params *p)
8953 if (has_delayed_token(p))
8954 dispatch_delayed_token(p, tSTRING_CONTENT);
8955 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8956 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8957 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8963#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8965parser_dispatch_heredoc_end(struct parser_params *p, int line)
8967 if (has_delayed_token(p))
8968 dispatch_delayed_token(p, tSTRING_CONTENT);
8970 if (p->keep_tokens) {
8971 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8972 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8973 parser_append_tokens(p, str, tHEREDOC_END, line);
8976 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8982static enum yytokentype
8983here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8985 int c, func, indent = 0;
8986 const char *eos, *ptr, *ptr_end;
8989 rb_encoding *enc = p->enc;
8990 rb_encoding *base_enc = 0;
8993 eos = RSTRING_PTR(here->lastline) + here->offset;
8995 indent = (func = here->func) & STR_FUNC_INDENT;
8997 if ((c = nextc(p)) == -1) {
9000 if (!has_delayed_token(p)) {
9001 dispatch_scan_event(p, tSTRING_CONTENT);
9004 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9005 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
9006 int cr = ENC_CODERANGE_UNKNOWN;
9007 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9008 if (cr != ENC_CODERANGE_7BIT &&
9009 rb_is_usascii_enc(p->enc) &&
9010 enc != rb_utf8_encoding()) {
9011 enc = rb_ascii8bit_encoding();
9014 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
9016 dispatch_delayed_token(p, tSTRING_CONTENT);
9020 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9021 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9024 SET_LEX_STATE(EXPR_END);
9029 /* not beginning of line, cannot be the terminator */
9031 else if (p->heredoc_line_indent == -1) {
9032 /* `heredoc_line_indent == -1` means
9033 * - "after an interpolation in the same line", or
9034 * - "in a continuing line"
9036 p->heredoc_line_indent = 0;
9038 else if (whole_match_p(p, eos, len, indent)) {
9039 dispatch_heredoc_end(p);
9041 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9043 SET_LEX_STATE(EXPR_END);
9047 if (!(func & STR_FUNC_EXPAND)) {
9049 ptr = RSTRING_PTR(p->lex.lastline);
9050 ptr_end = p->lex.pend;
9051 if (ptr_end > ptr) {
9052 switch (ptr_end[-1]) {
9054 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9063 if (p->heredoc_indent > 0) {
9065 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9067 p->heredoc_line_indent = 0;
9071 rb_str_cat(str, ptr, ptr_end - ptr);
9073 str = STR_NEW(ptr, ptr_end - ptr);
9074 if (!lex_eol_ptr_p(p, ptr_end)) rb_str_cat(str, "\n", 1);
9076 if (p->heredoc_indent > 0) {
9079 if (nextc(p) == -1) {
9085 } while (!whole_match_p(p, eos, len, indent));
9088 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9091 enum yytokentype t = parser_peek_variable_name(p);
9092 if (p->heredoc_line_indent != -1) {
9093 if (p->heredoc_indent > p->heredoc_line_indent) {
9094 p->heredoc_indent = p->heredoc_line_indent;
9096 p->heredoc_line_indent = -1;
9105 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9106 if (p->eofp) goto error;
9110 if (c == '\\') p->heredoc_line_indent = -1;
9112 str = STR_NEW3(tok(p), toklen(p), enc, func);
9114 set_yylval_str(str);
9116 if (bol) nd_set_fl_newline(yylval.node);
9118 flush_string_content(p, enc);
9119 return tSTRING_CONTENT;
9121 tokadd(p, nextc(p));
9122 if (p->heredoc_indent > 0) {
9126 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9127 if ((c = nextc(p)) == -1) goto error;
9128 } while (!whole_match_p(p, eos, len, indent));
9129 str = STR_NEW3(tok(p), toklen(p), enc, func);
9131 dispatch_heredoc_end(p);
9133 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
9136 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9138 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9139 set_yylval_str(str);
9141 if (bol) nd_set_fl_newline(yylval.node);
9143 return tSTRING_CONTENT;
9149arg_ambiguous(struct parser_params *p, char c)
9153 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
9156 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
9159 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9166formal_argument(struct parser_params *p, ID lhs)
9168formal_argument(struct parser_params *p, VALUE lhs)
9171 ID id = get_id(lhs);
9173 switch (id_type(id)) {
9177# define ERR(mesg) yyerror0(mesg)
9179# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
9182 ERR("formal argument cannot be a constant");
9185 ERR("formal argument cannot be an instance variable");
9188 ERR("formal argument cannot be a global variable");
9191 ERR("formal argument cannot be a class variable");
9194 ERR("formal argument must be local variable");
9198 shadowing_lvar(p, id);
9203lvar_defined(struct parser_params *p, ID id)
9205 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9208/* emacsen -*- hack */
9210parser_encode_length(struct parser_params *p, const char *name, long len)
9214 if (len > 5 && name[nlen = len - 5] == '-') {
9215 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9218 if (len > 4 && name[nlen = len - 4] == '-') {
9219 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9221 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9222 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9223 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9230parser_set_encode(struct parser_params *p, const char *name)
9232 int idx = rb_enc_find_index(name);
9237 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9239 excargs[0] = rb_eArgError;
9240 excargs[2] = rb_make_backtrace();
9241 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9242 rb_exc_raise(rb_make_exception(3, excargs));
9244 enc = rb_enc_from_index(idx);
9245 if (!rb_enc_asciicompat(enc)) {
9246 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9251 if (p->debug_lines) {
9252 VALUE lines = p->debug_lines;
9253 long i, n = RARRAY_LEN(lines);
9254 for (i = 0; i < n; ++i) {
9255 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
9262comment_at_top(struct parser_params *p)
9264 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
9265 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
9266 while (ptr < ptr_end) {
9267 if (!ISSPACE(*ptr)) return 0;
9273typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9274typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9276static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9279magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9281 if (!comment_at_top(p)) {
9284 parser_set_encode(p, val);
9288parser_get_bool(struct parser_params *p, const char *name, const char *val)
9292 if (STRCASECMP(val, "true") == 0) {
9297 if (STRCASECMP(val, "false") == 0) {
9302 return parser_invalid_pragma_value(p, name, val);
9306parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9308 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9313parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9315 int b = parser_get_bool(p, name, val);
9316 if (b >= 0) p->token_info_enabled = b;
9320parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9324 if (p->token_seen) {
9325 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
9329 b = parser_get_bool(p, name, val);
9332 p->frozen_string_literal = b;
9336parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9338 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9339 if (*s == ' ' || *s == '\t') continue;
9340 if (*s == '#') break;
9341 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
9347 if (STRCASECMP(val, "none") == 0) {
9348 p->ctxt.shareable_constant_value = shareable_none;
9353 if (STRCASECMP(val, "literal") == 0) {
9354 p->ctxt.shareable_constant_value = shareable_literal;
9359 if (STRCASECMP(val, "experimental_copy") == 0) {
9360 p->ctxt.shareable_constant_value = shareable_copy;
9363 if (STRCASECMP(val, "experimental_everything") == 0) {
9364 p->ctxt.shareable_constant_value = shareable_everything;
9369 parser_invalid_pragma_value(p, name, val);
9374parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9376 int b = parser_get_bool(p, name, val);
9377 if (b >= 0) p->past_scope_enabled = b;
9381struct magic_comment {
9383 rb_magic_comment_setter_t func;
9384 rb_magic_comment_length_t length;
9387static const struct magic_comment magic_comments[] = {
9388 {"coding", magic_comment_encoding, parser_encode_length},
9389 {"encoding", magic_comment_encoding, parser_encode_length},
9390 {"frozen_string_literal", parser_set_frozen_string_literal},
9391 {"shareable_constant_value", parser_set_shareable_constant_value},
9392 {"warn_indent", parser_set_token_info},
9394 {"warn_past_scope", parser_set_past_scope},
9399magic_comment_marker(const char *str, long len)
9406 if (str[i-1] == '*' && str[i-2] == '-') {
9412 if (i + 1 >= len) return 0;
9413 if (str[i+1] != '-') {
9416 else if (str[i-1] != '-') {
9432parser_magic_comment(struct parser_params *p, const char *str, long len)
9435 VALUE name = 0, val = 0;
9436 const char *beg, *end, *vbeg, *vend;
9437#define str_copy(_s, _p, _n) ((_s) \
9438 ? (void)(rb_str_resize((_s), (_n)), \
9439 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9440 : (void)((_s) = STR_NEW((_p), (_n))))
9442 if (len <= 7) return FALSE;
9443 if (!!(beg = magic_comment_marker(str, len))) {
9444 if (!(end = magic_comment_marker(beg, str + len - beg)))
9448 len = end - beg - 3;
9451 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9453 const struct magic_comment *mc = magic_comments;
9458 for (; len > 0 && *str; str++, --len) {
9460 case '\'': case '"': case ':': case ';':
9463 if (!ISSPACE(*str)) break;
9465 for (beg = str; len > 0; str++, --len) {
9467 case '\'': case '"': case ':': case ';':
9470 if (ISSPACE(*str)) break;
9475 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9478 if (!indicator) return FALSE;
9482 do str++; while (--len > 0 && ISSPACE(*str));
9485 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9498 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9502 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9505 while (len > 0 && (ISSPACE(*str))) --len, str++;
9506 if (len) return FALSE;
9510 str_copy(name, beg, n);
9511 s = RSTRING_PTR(name);
9512 for (i = 0; i < n; ++i) {
9513 if (s[i] == '-') s[i] = '_';
9516 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9519 n = (*mc->length)(p, vbeg, n);
9521 str_copy(val, vbeg, n);
9522 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9525 } while (++mc < magic_comments + numberof(magic_comments));
9527 str_copy(val, vbeg, vend - vbeg);
9528 dispatch2(magic_comment, name, val);
9536set_file_encoding(struct parser_params *p, const char *str, const char *send)
9539 const char *beg = str;
9543 if (send - str <= 6) return;
9545 case 'C': case 'c': str += 6; continue;
9546 case 'O': case 'o': str += 5; continue;
9547 case 'D': case 'd': str += 4; continue;
9548 case 'I': case 'i': str += 3; continue;
9549 case 'N': case 'n': str += 2; continue;
9550 case 'G': case 'g': str += 1; continue;
9557 if (ISSPACE(*str)) break;
9560 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9565 if (++str >= send) return;
9566 } while (ISSPACE(*str));
9568 if (*str != '=' && *str != ':') return;
9573 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9574 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9575 parser_set_encode(p, RSTRING_PTR(s));
9576 rb_str_resize(s, 0);
9580parser_prepare(struct parser_params *p)
9582 int c = nextc0(p, FALSE);
9583 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9586 if (peek(p, '!')) p->has_shebang = 1;
9588 case 0xef: /* UTF-8 BOM marker */
9589 if (!lex_eol_n_p(p, 2) &&
9590 (unsigned char)p->lex.pcur[0] == 0xbb &&
9591 (unsigned char)p->lex.pcur[1] == 0xbf) {
9592 p->enc = rb_utf8_encoding();
9595 if (p->debug_lines) {
9596 rb_enc_associate(p->lex.lastline, p->enc);
9599 p->lex.pbeg = p->lex.pcur;
9608 p->enc = rb_enc_get(p->lex.lastline);
9612#define ambiguous_operator(tok, op, syn) ( \
9613 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
9614 rb_warning0("even though it seems like "syn""))
9616#define ambiguous_operator(tok, op, syn) \
9617 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9619#define warn_balanced(tok, op, syn) ((void) \
9620 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9621 space_seen && !ISSPACE(c) && \
9622 (ambiguous_operator(tok, op, syn), 0)), \
9623 (enum yytokentype)(tok))
9626parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9629 char *point = &str[seen_point];
9630 size_t fraclen = len-seen_point-1;
9631 memmove(point, point+1, fraclen+1);
9632 v = rb_cstr_to_inum(str, 10, FALSE);
9633 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9636static enum yytokentype
9637no_digits(struct parser_params *p)
9639 yyerror0("numeric literal without digits");
9640 if (peek(p, '_')) nextc(p);
9641 /* dummy 0, for tUMINUS_NUM at numeric */
9642 return set_integer_literal(p, INT2FIX(0), 0);
9645static enum yytokentype
9646parse_numeric(struct parser_params *p, int c)
9648 int is_float, seen_point, seen_e, nondigit;
9651 is_float = seen_point = seen_e = nondigit = 0;
9652 SET_LEX_STATE(EXPR_END);
9654 if (c == '-' || c == '+') {
9659 int start = toklen(p);
9661 if (c == 'x' || c == 'X') {
9664 if (c != -1 && ISXDIGIT(c)) {
9667 if (nondigit) break;
9671 if (!ISXDIGIT(c)) break;
9674 } while ((c = nextc(p)) != -1);
9678 if (toklen(p) == start) {
9679 return no_digits(p);
9681 else if (nondigit) goto trailing_uc;
9682 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9683 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9685 if (c == 'b' || c == 'B') {
9688 if (c == '0' || c == '1') {
9691 if (nondigit) break;
9695 if (c != '0' && c != '1') break;
9698 } while ((c = nextc(p)) != -1);
9702 if (toklen(p) == start) {
9703 return no_digits(p);
9705 else if (nondigit) goto trailing_uc;
9706 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9707 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9709 if (c == 'd' || c == 'D') {
9712 if (c != -1 && ISDIGIT(c)) {
9715 if (nondigit) break;
9719 if (!ISDIGIT(c)) break;
9722 } while ((c = nextc(p)) != -1);
9726 if (toklen(p) == start) {
9727 return no_digits(p);
9729 else if (nondigit) goto trailing_uc;
9730 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9731 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9737 if (c == 'o' || c == 'O') {
9738 /* prefixed octal */
9740 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9741 return no_digits(p);
9744 if (c >= '0' && c <= '7') {
9749 if (nondigit) break;
9753 if (c < '0' || c > '9') break;
9754 if (c > '7') goto invalid_octal;
9757 } while ((c = nextc(p)) != -1);
9758 if (toklen(p) > start) {
9761 if (nondigit) goto trailing_uc;
9762 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9763 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9770 if (c > '7' && c <= '9') {
9772 yyerror0("Invalid octal digit");
9774 else if (c == '.' || c == 'e' || c == 'E') {
9779 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9780 return set_integer_literal(p, INT2FIX(0), suffix);
9786 case '0': case '1': case '2': case '3': case '4':
9787 case '5': case '6': case '7': case '8': case '9':
9793 if (nondigit) goto trailing_uc;
9794 if (seen_point || seen_e) {
9799 if (c0 == -1 || !ISDIGIT(c0)) {
9805 seen_point = toklen(p);
9824 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9830 tokadd(p, nondigit);
9834 nondigit = (c == '-' || c == '+') ? c : 0;
9837 case '_': /* `_' in number just ignored */
9838 if (nondigit) goto decode_num;
9852 literal_flush(p, p->lex.pcur - 1);
9853 YYLTYPE loc = RUBY_INIT_YYLLOC();
9854 compile_error(p, "trailing `%c' in number", nondigit);
9855 parser_show_error_line(p, &loc);
9859 enum yytokentype type = tFLOAT;
9862 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9863 if (suffix & NUM_SUFFIX_R) {
9865 v = parse_rational(p, tok(p), toklen(p), seen_point);
9868 double d = strtod(tok(p), 0);
9869 if (errno == ERANGE) {
9870 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9875 return set_number_literal(p, v, type, suffix);
9877 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9878 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9881static enum yytokentype
9882parse_qmark(struct parser_params *p, int space_seen)
9889 SET_LEX_STATE(EXPR_VALUE);
9894 compile_error(p, "incomplete character syntax");
9897 if (rb_enc_isspace(c, p->enc)) {
9899 int c2 = escaped_control_code(c);
9901 WARN_SPACE_CHAR(c2, "?");
9906 SET_LEX_STATE(EXPR_VALUE);
9911 if (!parser_isascii(p)) {
9912 if (tokadd_mbchar(p, c) == -1) return 0;
9914 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9915 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
9917 const char *start = p->lex.pcur - 1, *ptr = start;
9919 int n = parser_precise_mbclen(p, ptr);
9920 if (n < 0) return -1;
9922 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9923 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9924 " a conditional operator, put a space after `?'",
9925 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9929 else if (c == '\\') {
9932 enc = rb_utf8_encoding();
9933 tokadd_utf8(p, &enc, -1, 0, 0);
9935 else if (!ISASCII(c = peekc(p))) {
9937 if (tokadd_mbchar(p, c) == -1) return 0;
9940 c = read_escape(p, 0);
9948 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9949 set_yylval_str(lit);
9950 SET_LEX_STATE(EXPR_END);
9954static enum yytokentype
9955parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9958 const char *ptok = p->lex.pcur;
9966 if (c == -1) goto unterminated;
9969 if (!ISASCII(c)) goto unknown;
9974 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9977 c = parser_precise_mbclen(p, p->lex.pcur);
9978 if (c < 0) return 0;
9980 yyerror0("unknown type of %string");
9986 compile_error(p, "unterminated quoted string meets end of file");
9990 if (term == '(') term = ')';
9991 else if (term == '[') term = ']';
9992 else if (term == '{') term = '}';
9993 else if (term == '<') term = '>';
9996 p->lex.ptok = ptok-1;
9999 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10000 return tSTRING_BEG;
10003 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10004 return tSTRING_BEG;
10007 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10011 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10012 return tQWORDS_BEG;
10015 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10016 return tSYMBOLS_BEG;
10019 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10020 return tQSYMBOLS_BEG;
10023 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10024 return tXSTRING_BEG;
10027 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10028 return tREGEXP_BEG;
10031 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10032 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10036 yyerror0("unknown type of %string");
10040 if ((c = nextc(p)) == '=') {
10041 set_yylval_id('%');
10042 SET_LEX_STATE(EXPR_BEG);
10045 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10048 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10050 return warn_balanced('%', "%%", "string literal");
10054tokadd_ident(struct parser_params *p, int c)
10057 if (tokadd_mbchar(p, c) == -1) return -1;
10059 } while (parser_is_identchar(p));
10065tokenize_ident(struct parser_params *p)
10067 ID ident = TOK_INTERN();
10069 set_yylval_name(ident);
10075parse_numvar(struct parser_params *p)
10079 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10080 const unsigned long nth_ref_max =
10081 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10082 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10083 * turned into a Fixnum, in compile.c */
10085 if (overflow || n > nth_ref_max) {
10086 /* compile_error()? */
10087 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10088 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10095static enum yytokentype
10096parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10098 const char *ptr = p->lex.pcur;
10101 SET_LEX_STATE(EXPR_END);
10102 p->lex.ptok = ptr - 1; /* from '$' */
10106 case '_': /* $_: last read line string */
10108 if (parser_is_identchar(p)) {
10116 case '~': /* $~: match-data */
10117 case '*': /* $*: argv */
10118 case '$': /* $$: pid */
10119 case '?': /* $?: last status */
10120 case '!': /* $!: error string */
10121 case '@': /* $@: error position */
10122 case '/': /* $/: input record separator */
10123 case '\\': /* $\: output record separator */
10124 case ';': /* $;: field separator */
10125 case ',': /* $,: output field separator */
10126 case '.': /* $.: last read line number */
10127 case '=': /* $=: ignorecase */
10128 case ':': /* $:: load path */
10129 case '<': /* $<: reading filename */
10130 case '>': /* $>: default output handle */
10131 case '\"': /* $": already loaded files */
10140 if (parser_is_identchar(p)) {
10141 if (tokadd_mbchar(p, c) == -1) return 0;
10149 set_yylval_name(TOK_INTERN());
10152 case '&': /* $&: last match */
10153 case '`': /* $`: string before last match */
10154 case '\'': /* $': string after last match */
10155 case '+': /* $+: string matches last paren. */
10156 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10161 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10164 case '1': case '2': case '3':
10165 case '4': case '5': case '6':
10166 case '7': case '8': case '9':
10171 } while (c != -1 && ISDIGIT(c));
10173 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10175 c = parse_numvar(p);
10176 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10180 if (!parser_is_identchar(p)) {
10181 YYLTYPE loc = RUBY_INIT_YYLLOC();
10182 if (c == -1 || ISSPACE(c)) {
10183 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
10187 compile_error(p, "`$%c' is not allowed as a global variable name", c);
10189 parser_show_error_line(p, &loc);
10190 set_yylval_noname();
10198 if (tokadd_ident(p, c)) return 0;
10199 SET_LEX_STATE(EXPR_END);
10200 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10204 compile_error(p, "`%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10205 set_yylval_noname();
10212parser_numbered_param(struct parser_params *p, int n)
10214 if (n < 0) return false;
10216 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10219 if (p->max_numparam == ORDINAL_PARAM) {
10220 compile_error(p, "ordinary parameter is defined");
10223 struct vtable *args = p->lvtbl->args;
10224 if (p->max_numparam < n) {
10225 p->max_numparam = n;
10227 while (n > args->pos) {
10228 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10234static enum yytokentype
10235parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10237 const char *ptr = p->lex.pcur;
10238 enum yytokentype result = tIVAR;
10239 register int c = nextc(p);
10242 p->lex.ptok = ptr - 1; /* from '@' */
10250 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10251 if (c == -1 || !parser_is_identchar(p)) {
10253 RUBY_SET_YYLLOC(loc);
10254 if (result == tIVAR) {
10255 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
10258 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
10260 parser_show_error_line(p, &loc);
10261 set_yylval_noname();
10262 SET_LEX_STATE(EXPR_END);
10265 else if (ISDIGIT(c)) {
10267 RUBY_SET_YYLLOC(loc);
10268 if (result == tIVAR) {
10269 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
10272 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
10274 parser_show_error_line(p, &loc);
10275 set_yylval_noname();
10276 SET_LEX_STATE(EXPR_END);
10280 if (tokadd_ident(p, c)) return 0;
10285static enum yytokentype
10286parse_ident(struct parser_params *p, int c, int cmd_state)
10288 enum yytokentype result;
10289 int mb = ENC_CODERANGE_7BIT;
10290 const enum lex_state_e last_state = p->lex.state;
10292 int enforce_keyword_end = 0;
10295 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
10296 if (tokadd_mbchar(p, c) == -1) return 0;
10298 } while (parser_is_identchar(p));
10299 if ((c == '!' || c == '?') && !peek(p, '=')) {
10303 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10304 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10305 result = tIDENTIFIER;
10309 result = tCONSTANT; /* assume provisionally */
10314 if (IS_LABEL_POSSIBLE()) {
10315 if (IS_LABEL_SUFFIX(0)) {
10316 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10318 set_yylval_name(TOK_INTERN());
10324 if (!NIL_P(peek_end_expect_token_locations(p))) {
10326 int lineno, column;
10327 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10329 end_loc = peek_end_expect_token_locations(p);
10330 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
10331 column = NUM2INT(rb_ary_entry(end_loc, 1));
10334 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10335 p->ruby_sourceline, beg_pos, lineno, column);
10338 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10339 const struct kwtable *kw;
10341 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10342 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10343 enforce_keyword_end = 1;
10349 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10350 const struct kwtable *kw;
10352 /* See if it is a reserved word. */
10353 kw = rb_reserved_word(tok(p), toklen(p));
10355 enum lex_state_e state = p->lex.state;
10356 if (IS_lex_state_for(state, EXPR_FNAME)) {
10357 SET_LEX_STATE(EXPR_ENDFN);
10358 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10361 SET_LEX_STATE(kw->state);
10362 if (IS_lex_state(EXPR_BEG)) {
10363 p->command_start = TRUE;
10365 if (kw->id[0] == keyword_do) {
10366 if (lambda_beginning_p()) {
10367 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10368 return keyword_do_LAMBDA;
10370 if (COND_P()) return keyword_do_cond;
10371 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10372 return keyword_do_block;
10375 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10378 if (kw->id[0] != kw->id[1])
10379 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10385 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10387 SET_LEX_STATE(EXPR_CMDARG);
10390 SET_LEX_STATE(EXPR_ARG);
10393 else if (p->lex.state == EXPR_FNAME) {
10394 SET_LEX_STATE(EXPR_ENDFN);
10397 SET_LEX_STATE(EXPR_END);
10400 ident = tokenize_ident(p);
10401 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10402 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10403 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10404 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10405 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10411warn_cr(struct parser_params *p)
10415 /* carried over with p->lex.nextline for nextc() */
10416 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10420static enum yytokentype
10421parser_yylex(struct parser_params *p)
10424 int space_seen = 0;
10427 enum lex_state_e last_state;
10428 int fallthru = FALSE;
10429 int token_seen = p->token_seen;
10431 if (p->lex.strterm) {
10432 if (strterm_is_heredoc(p->lex.strterm)) {
10434 return here_document(p, &p->lex.strterm->u.heredoc);
10438 return parse_string(p, &p->lex.strterm->u.literal);
10441 cmd_state = p->command_start;
10442 p->command_start = FALSE;
10443 p->token_seen = TRUE;
10448 last_state = p->lex.state;
10449 switch (c = nextc(p)) {
10450 case '\0': /* NUL */
10451 case '\004': /* ^D */
10452 case '\032': /* ^Z */
10453 case -1: /* end of script. */
10456 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
10457 pop_end_expect_token_locations(p);
10458 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10462 /* Set location for end-of-input because dispatch_scan_event is not called. */
10463 RUBY_SET_YYLLOC(*p->yylloc);
10464 return END_OF_INPUT;
10470 case ' ': case '\t': case '\f':
10471 case '\13': /* '\v' */
10473 while ((c = nextc(p))) {
10478 case ' ': case '\t': case '\f':
10479 case '\13': /* '\v' */
10487 dispatch_scan_event(p, tSP);
10493 case '#': /* it's a comment */
10494 p->token_seen = token_seen;
10495 /* no magic_comment in shebang line */
10496 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10497 if (comment_at_top(p)) {
10498 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10502 dispatch_scan_event(p, tCOMMENT);
10506 p->token_seen = token_seen;
10507 VALUE prevline = p->lex.lastline;
10508 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10509 !IS_lex_state(EXPR_LABELED));
10510 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10512 dispatch_scan_event(p, tIGNORED_NL);
10515 if (!c && p->ctxt.in_kwarg) {
10516 goto normal_newline;
10521 switch (c = nextc(p)) {
10522 case ' ': case '\t': case '\f': case '\r':
10523 case '\13': /* '\v' */
10529 dispatch_scan_event(p, tSP);
10535 dispatch_delayed_token(p, tIGNORED_NL);
10536 if (peek(p, '.') == (c == '&')) {
10538 dispatch_scan_event(p, tSP);
10543 p->ruby_sourceline--;
10544 p->lex.nextline = p->lex.lastline;
10545 set_lastline(p, prevline);
10546 case -1: /* EOF no decrement*/
10550 RUBY_SET_YYLLOC(*p->yylloc);
10552 goto normal_newline;
10556 p->command_start = TRUE;
10557 SET_LEX_STATE(EXPR_BEG);
10561 if ((c = nextc(p)) == '*') {
10562 if ((c = nextc(p)) == '=') {
10563 set_yylval_id(idPow);
10564 SET_LEX_STATE(EXPR_BEG);
10568 if (IS_SPCARG(c)) {
10569 rb_warning0("`**' interpreted as argument prefix");
10572 else if (IS_BEG()) {
10576 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10581 set_yylval_id('*');
10582 SET_LEX_STATE(EXPR_BEG);
10586 if (IS_SPCARG(c)) {
10587 rb_warning0("`*' interpreted as argument prefix");
10590 else if (IS_BEG()) {
10594 c = warn_balanced('*', "*", "argument prefix");
10597 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10602 if (IS_AFTER_OPERATOR()) {
10603 SET_LEX_STATE(EXPR_ARG);
10609 SET_LEX_STATE(EXPR_BEG);
10622 /* skip embedded rd document */
10623 if (word_match_p(p, "begin", 5)) {
10624 int first_p = TRUE;
10627 dispatch_scan_event(p, tEMBDOC_BEG);
10631 dispatch_scan_event(p, tEMBDOC);
10636 compile_error(p, "embedded document meets end of file");
10637 return END_OF_INPUT;
10639 if (c == '=' && word_match_p(p, "end", 3)) {
10645 dispatch_scan_event(p, tEMBDOC_END);
10650 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10651 if ((c = nextc(p)) == '=') {
10652 if ((c = nextc(p)) == '=') {
10661 else if (c == '>') {
10670 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10672 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10673 enum yytokentype token = heredoc_identifier(p);
10674 if (token) return token < 0 ? 0 : token;
10676 if (IS_AFTER_OPERATOR()) {
10677 SET_LEX_STATE(EXPR_ARG);
10680 if (IS_lex_state(EXPR_CLASS))
10681 p->command_start = TRUE;
10682 SET_LEX_STATE(EXPR_BEG);
10685 if ((c = nextc(p)) == '>') {
10692 if ((c = nextc(p)) == '=') {
10693 set_yylval_id(idLTLT);
10694 SET_LEX_STATE(EXPR_BEG);
10698 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10704 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10705 if ((c = nextc(p)) == '=') {
10709 if ((c = nextc(p)) == '=') {
10710 set_yylval_id(idGTGT);
10711 SET_LEX_STATE(EXPR_BEG);
10721 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10722 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10723 p->lex.ptok = p->lex.pcur-1;
10724 return tSTRING_BEG;
10727 if (IS_lex_state(EXPR_FNAME)) {
10728 SET_LEX_STATE(EXPR_ENDFN);
10731 if (IS_lex_state(EXPR_DOT)) {
10733 SET_LEX_STATE(EXPR_CMDARG);
10735 SET_LEX_STATE(EXPR_ARG);
10738 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10739 return tXSTRING_BEG;
10742 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10743 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10744 p->lex.ptok = p->lex.pcur-1;
10745 return tSTRING_BEG;
10748 return parse_qmark(p, space_seen);
10751 if ((c = nextc(p)) == '&') {
10752 SET_LEX_STATE(EXPR_BEG);
10753 if ((c = nextc(p)) == '=') {
10754 set_yylval_id(idANDOP);
10755 SET_LEX_STATE(EXPR_BEG);
10761 else if (c == '=') {
10762 set_yylval_id('&');
10763 SET_LEX_STATE(EXPR_BEG);
10766 else if (c == '.') {
10767 set_yylval_id(idANDDOT);
10768 SET_LEX_STATE(EXPR_DOT);
10772 if (IS_SPCARG(c)) {
10774 (c = peekc_n(p, 1)) == -1 ||
10775 !(c == '\'' || c == '"' ||
10776 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10777 rb_warning0("`&' interpreted as argument prefix");
10781 else if (IS_BEG()) {
10785 c = warn_balanced('&', "&", "argument prefix");
10787 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10791 if ((c = nextc(p)) == '|') {
10792 SET_LEX_STATE(EXPR_BEG);
10793 if ((c = nextc(p)) == '=') {
10794 set_yylval_id(idOROP);
10795 SET_LEX_STATE(EXPR_BEG);
10799 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10807 set_yylval_id('|');
10808 SET_LEX_STATE(EXPR_BEG);
10811 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10817 if (IS_AFTER_OPERATOR()) {
10818 SET_LEX_STATE(EXPR_ARG);
10826 set_yylval_id('+');
10827 SET_LEX_STATE(EXPR_BEG);
10830 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10831 SET_LEX_STATE(EXPR_BEG);
10833 if (c != -1 && ISDIGIT(c)) {
10834 return parse_numeric(p, '+');
10838 SET_LEX_STATE(EXPR_BEG);
10840 return warn_balanced('+', "+", "unary operator");
10844 if (IS_AFTER_OPERATOR()) {
10845 SET_LEX_STATE(EXPR_ARG);
10853 set_yylval_id('-');
10854 SET_LEX_STATE(EXPR_BEG);
10858 SET_LEX_STATE(EXPR_ENDFN);
10861 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10862 SET_LEX_STATE(EXPR_BEG);
10864 if (c != -1 && ISDIGIT(c)) {
10865 return tUMINUS_NUM;
10869 SET_LEX_STATE(EXPR_BEG);
10871 return warn_balanced('-', "-", "unary operator");
10874 int is_beg = IS_BEG();
10875 SET_LEX_STATE(EXPR_BEG);
10876 if ((c = nextc(p)) == '.') {
10877 if ((c = nextc(p)) == '.') {
10878 if (p->ctxt.in_argdef) {
10879 SET_LEX_STATE(EXPR_ENDARG);
10882 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10883 rb_warn0("... at EOL, should be parenthesized?");
10885 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10886 if (IS_lex_state_for(last_state, EXPR_LABEL))
10889 return is_beg ? tBDOT3 : tDOT3;
10892 return is_beg ? tBDOT2 : tDOT2;
10895 if (c != -1 && ISDIGIT(c)) {
10896 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10897 parse_numeric(p, '.');
10898 if (ISDIGIT(prev)) {
10899 yyerror0("unexpected fraction part after numeric literal");
10902 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10904 SET_LEX_STATE(EXPR_END);
10905 p->lex.ptok = p->lex.pcur;
10908 set_yylval_id('.');
10909 SET_LEX_STATE(EXPR_DOT);
10913 case '0': case '1': case '2': case '3': case '4':
10914 case '5': case '6': case '7': case '8': case '9':
10915 return parse_numeric(p, c);
10920 SET_LEX_STATE(EXPR_ENDFN);
10921 p->lex.paren_nest--;
10927 SET_LEX_STATE(EXPR_END);
10928 p->lex.paren_nest--;
10932 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10933 if (!p->lex.brace_nest--) return tSTRING_DEND;
10936 SET_LEX_STATE(EXPR_END);
10937 p->lex.paren_nest--;
10943 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10944 SET_LEX_STATE(EXPR_BEG);
10947 set_yylval_id(idCOLON2);
10948 SET_LEX_STATE(EXPR_DOT);
10951 if (IS_END() || ISSPACE(c) || c == '#') {
10953 c = warn_balanced(':', ":", "symbol literal");
10954 SET_LEX_STATE(EXPR_BEG);
10959 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10962 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10968 SET_LEX_STATE(EXPR_FNAME);
10973 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10974 return tREGEXP_BEG;
10976 if ((c = nextc(p)) == '=') {
10977 set_yylval_id('/');
10978 SET_LEX_STATE(EXPR_BEG);
10982 if (IS_SPCARG(c)) {
10983 arg_ambiguous(p, '/');
10984 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10985 return tREGEXP_BEG;
10987 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10988 return warn_balanced('/', "/", "regexp literal");
10991 if ((c = nextc(p)) == '=') {
10992 set_yylval_id('^');
10993 SET_LEX_STATE(EXPR_BEG);
10996 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11001 SET_LEX_STATE(EXPR_BEG);
11002 p->command_start = TRUE;
11006 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11010 if (IS_AFTER_OPERATOR()) {
11011 if ((c = nextc(p)) != '@') {
11014 SET_LEX_STATE(EXPR_ARG);
11017 SET_LEX_STATE(EXPR_BEG);
11025 else if (!space_seen) {
11026 /* foo( ... ) => method call, no ambiguity */
11028 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11031 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11032 rb_warning0("parentheses after method name is interpreted as "
11033 "an argument list, not a decomposed argument");
11035 p->lex.paren_nest++;
11038 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11042 p->lex.paren_nest++;
11043 if (IS_AFTER_OPERATOR()) {
11044 if ((c = nextc(p)) == ']') {
11045 p->lex.paren_nest--;
11046 SET_LEX_STATE(EXPR_ARG);
11047 if ((c = nextc(p)) == '=') {
11054 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11057 else if (IS_BEG()) {
11060 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11063 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11069 ++p->lex.brace_nest;
11070 if (lambda_beginning_p())
11072 else if (IS_lex_state(EXPR_LABELED))
11073 c = tLBRACE; /* hash */
11074 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11075 c = '{'; /* block (primary) */
11076 else if (IS_lex_state(EXPR_ENDARG))
11077 c = tLBRACE_ARG; /* block (expr) */
11079 c = tLBRACE; /* hash */
11080 if (c != tLBRACE) {
11081 p->command_start = TRUE;
11082 SET_LEX_STATE(EXPR_BEG);
11085 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11087 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11096 dispatch_scan_event(p, tSP);
11097 goto retry; /* skip \\n */
11099 if (c == ' ') return tSP;
11100 if (ISSPACE(c)) return c;
11105 return parse_percent(p, space_seen, last_state);
11108 return parse_gvar(p, last_state);
11111 return parse_atmark(p, last_state);
11114 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11115 p->ruby__end__seen = 1;
11119 dispatch_scan_event(p, k__END__);
11121 return END_OF_INPUT;
11127 if (!parser_is_identchar(p)) {
11128 compile_error(p, "Invalid char `\\x%02X' in expression", c);
11137 return parse_ident(p, c, cmd_state);
11140static enum yytokentype
11141yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11143 enum yytokentype t;
11146 lval->val = Qundef;
11147 p->yylloc = yylloc;
11149 t = parser_yylex(p);
11151 if (has_delayed_token(p))
11152 dispatch_delayed_token(p, t);
11153 else if (t != END_OF_INPUT)
11154 dispatch_scan_event(p, t);
11159#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11162node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11164 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11166 rb_node_init(n, type);
11171nd_set_loc(NODE *nd, const YYLTYPE *loc)
11174 nd_set_line(nd, loc->beg_pos.lineno);
11179node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11181 NODE *n = node_new_internal(p, type, size, alignment);
11183 nd_set_loc(n, loc);
11184 nd_set_node_id(n, parser_get_node_id(p));
11188#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11192static rb_node_scope_t *
11193rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11195 rb_ast_id_table_t *nd_tbl;
11196 nd_tbl = local_tbl(p);
11197 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11198 n->nd_tbl = nd_tbl;
11199 n->nd_body = nd_body;
11200 n->nd_args = nd_args;
11205static rb_node_scope_t *
11206rb_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)
11208 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11209 n->nd_tbl = nd_tbl;
11210 n->nd_body = nd_body;
11211 n->nd_args = nd_args;
11216static rb_node_defn_t *
11217rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11219 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11220 n->nd_mid = nd_mid;
11221 n->nd_defn = nd_defn;
11226static rb_node_defs_t *
11227rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11229 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11230 n->nd_recv = nd_recv;
11231 n->nd_mid = nd_mid;
11232 n->nd_defn = nd_defn;
11237static rb_node_block_t *
11238rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11240 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11241 n->nd_head = nd_head;
11242 n->nd_end = (NODE *)n;
11248static rb_node_for_t *
11249rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11251 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11252 n->nd_body = nd_body;
11253 n->nd_iter = nd_iter;
11258static rb_node_for_masgn_t *
11259rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11261 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11262 n->nd_var = nd_var;
11267static rb_node_retry_t *
11268rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11270 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11275static rb_node_begin_t *
11276rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11278 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11279 n->nd_body = nd_body;
11284static rb_node_rescue_t *
11285rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11287 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11288 n->nd_head = nd_head;
11289 n->nd_resq = nd_resq;
11290 n->nd_else = nd_else;
11295static rb_node_resbody_t *
11296rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc)
11298 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11299 n->nd_head = nd_head;
11300 n->nd_body = nd_body;
11301 n->nd_args = nd_args;
11306static rb_node_ensure_t *
11307rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11309 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11310 n->nd_head = nd_head;
11312 n->nd_ensr = nd_ensr;
11317static rb_node_and_t *
11318rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11320 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11321 n->nd_1st = nd_1st;
11322 n->nd_2nd = nd_2nd;
11327static rb_node_or_t *
11328rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11330 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11331 n->nd_1st = nd_1st;
11332 n->nd_2nd = nd_2nd;
11337static rb_node_return_t *
11338rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
11340 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11341 n->nd_stts = nd_stts;
11345static rb_node_yield_t *
11346rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11348 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11349 n->nd_head = nd_head;
11354static rb_node_if_t *
11355rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11357 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11358 n->nd_cond = nd_cond;
11359 n->nd_body = nd_body;
11360 n->nd_else = nd_else;
11365static rb_node_unless_t *
11366rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11368 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11369 n->nd_cond = nd_cond;
11370 n->nd_body = nd_body;
11371 n->nd_else = nd_else;
11376static rb_node_class_t *
11377rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11379 /* Keep the order of node creation */
11380 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11381 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11382 n->nd_cpath = nd_cpath;
11383 n->nd_body = scope;
11384 n->nd_super = nd_super;
11389static rb_node_sclass_t *
11390rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11392 /* Keep the order of node creation */
11393 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11394 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11395 n->nd_recv = nd_recv;
11396 n->nd_body = scope;
11401static rb_node_module_t *
11402rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11404 /* Keep the order of node creation */
11405 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11406 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11407 n->nd_cpath = nd_cpath;
11408 n->nd_body = scope;
11413static rb_node_iter_t *
11414rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11416 /* Keep the order of node creation */
11417 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11418 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11419 n->nd_body = scope;
11425static rb_node_lambda_t *
11426rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11428 /* Keep the order of node creation */
11429 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11430 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11431 n->nd_body = scope;
11436static rb_node_case_t *
11437rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11439 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11440 n->nd_head = nd_head;
11441 n->nd_body = nd_body;
11446static rb_node_case2_t *
11447rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11449 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11451 n->nd_body = nd_body;
11456static rb_node_case3_t *
11457rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11459 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11460 n->nd_head = nd_head;
11461 n->nd_body = nd_body;
11466static rb_node_when_t *
11467rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11469 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11470 n->nd_head = nd_head;
11471 n->nd_body = nd_body;
11472 n->nd_next = nd_next;
11477static rb_node_in_t *
11478rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11480 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11481 n->nd_head = nd_head;
11482 n->nd_body = nd_body;
11483 n->nd_next = nd_next;
11488static rb_node_while_t *
11489rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11491 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11492 n->nd_cond = nd_cond;
11493 n->nd_body = nd_body;
11494 n->nd_state = nd_state;
11499static rb_node_until_t *
11500rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11502 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11503 n->nd_cond = nd_cond;
11504 n->nd_body = nd_body;
11505 n->nd_state = nd_state;
11510static rb_node_colon2_t *
11511rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11513 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11514 n->nd_head = nd_head;
11515 n->nd_mid = nd_mid;
11520static rb_node_colon3_t *
11521rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11523 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11524 n->nd_mid = nd_mid;
11529static rb_node_dot2_t *
11530rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11532 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11533 n->nd_beg = nd_beg;
11534 n->nd_end = nd_end;
11539static rb_node_dot3_t *
11540rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11542 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11543 n->nd_beg = nd_beg;
11544 n->nd_end = nd_end;
11549static rb_node_self_t *
11550rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11552 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11558static rb_node_nil_t *
11559rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11561 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11566static rb_node_true_t *
11567rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11569 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11574static rb_node_false_t *
11575rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11577 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11582static rb_node_super_t *
11583rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11585 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11586 n->nd_args = nd_args;
11591static rb_node_zsuper_t *
11592rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11594 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11599static rb_node_match2_t *
11600rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11602 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11603 n->nd_recv = nd_recv;
11604 n->nd_value = nd_value;
11610static rb_node_match3_t *
11611rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11613 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11614 n->nd_recv = nd_recv;
11615 n->nd_value = nd_value;
11620/* TODO: Use union for NODE_LIST2 */
11621static rb_node_list_t *
11622rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11624 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11625 n->nd_head = nd_head;
11632static rb_node_list_t *
11633rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11635 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11636 n->nd_head = nd_head;
11637 n->as.nd_alen = nd_alen;
11638 n->nd_next = nd_next;
11643static rb_node_zlist_t *
11644rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11646 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11651static rb_node_hash_t *
11652rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11654 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11655 n->nd_head = nd_head;
11661static rb_node_masgn_t *
11662rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11664 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11665 n->nd_head = nd_head;
11667 n->nd_args = nd_args;
11672static rb_node_gasgn_t *
11673rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11675 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11676 n->nd_vid = nd_vid;
11677 n->nd_value = nd_value;
11682static rb_node_lasgn_t *
11683rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11685 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11686 n->nd_vid = nd_vid;
11687 n->nd_value = nd_value;
11692static rb_node_dasgn_t *
11693rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11695 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11696 n->nd_vid = nd_vid;
11697 n->nd_value = nd_value;
11702static rb_node_iasgn_t *
11703rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11705 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11706 n->nd_vid = nd_vid;
11707 n->nd_value = nd_value;
11712static rb_node_cvasgn_t *
11713rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11715 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11716 n->nd_vid = nd_vid;
11717 n->nd_value = nd_value;
11722static rb_node_op_asgn1_t *
11723rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc)
11725 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11726 n->nd_recv = nd_recv;
11727 n->nd_mid = nd_mid;
11728 n->nd_index = index;
11729 n->nd_rvalue = rvalue;
11734static rb_node_op_asgn2_t *
11735rb_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)
11737 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11738 n->nd_recv = nd_recv;
11739 n->nd_value = nd_value;
11740 n->nd_vid = nd_vid;
11741 n->nd_mid = nd_mid;
11742 n->nd_aid = nd_aid;
11747static rb_node_op_asgn_or_t *
11748rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11750 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11751 n->nd_head = nd_head;
11752 n->nd_value = nd_value;
11757static rb_node_op_asgn_and_t *
11758rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11760 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11761 n->nd_head = nd_head;
11762 n->nd_value = nd_value;
11767static rb_node_gvar_t *
11768rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11770 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11771 n->nd_vid = nd_vid;
11776static rb_node_lvar_t *
11777rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11779 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11780 n->nd_vid = nd_vid;
11785static rb_node_dvar_t *
11786rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11788 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11789 n->nd_vid = nd_vid;
11794static rb_node_ivar_t *
11795rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11797 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11798 n->nd_vid = nd_vid;
11803static rb_node_const_t *
11804rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11806 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11807 n->nd_vid = nd_vid;
11812static rb_node_cvar_t *
11813rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11815 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11816 n->nd_vid = nd_vid;
11821static rb_node_nth_ref_t *
11822rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11824 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11825 n->nd_nth = nd_nth;
11830static rb_node_back_ref_t *
11831rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11833 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11834 n->nd_nth = nd_nth;
11839static rb_node_lit_t *
11840rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11842 rb_node_lit_t *n = NODE_NEWNODE(NODE_LIT, rb_node_lit_t, loc);
11843 n->nd_lit = nd_lit;
11848static rb_node_str_t *
11849rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11851 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11852 n->nd_lit = nd_lit;
11857/* TODO; Use union for NODE_DSTR2 */
11858static rb_node_dstr_t *
11859rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11861 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11862 n->nd_lit = nd_lit;
11863 n->as.nd_alen = nd_alen;
11864 n->nd_next = (rb_node_list_t *)nd_next;
11869static rb_node_dstr_t *
11870rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11872 return rb_node_dstr_new0(p, nd_lit, 1, 0, loc);
11875static rb_node_xstr_t *
11876rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11878 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
11879 n->nd_lit = nd_lit;
11884static rb_node_dxstr_t *
11885rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11887 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
11888 n->nd_lit = nd_lit;
11889 n->nd_alen = nd_alen;
11890 n->nd_next = (rb_node_list_t *)nd_next;
11895static rb_node_dsym_t *
11896rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11898 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
11899 n->nd_lit = nd_lit;
11900 n->nd_alen = nd_alen;
11901 n->nd_next = (rb_node_list_t *)nd_next;
11906static rb_node_evstr_t *
11907rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11909 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
11910 n->nd_body = nd_body;
11915static rb_node_call_t *
11916rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11918 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
11919 n->nd_recv = nd_recv;
11920 n->nd_mid = nd_mid;
11921 n->nd_args = nd_args;
11926static rb_node_opcall_t *
11927rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11929 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
11930 n->nd_recv = nd_recv;
11931 n->nd_mid = nd_mid;
11932 n->nd_args = nd_args;
11937static rb_node_fcall_t *
11938rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11940 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
11941 n->nd_mid = nd_mid;
11942 n->nd_args = nd_args;
11947static rb_node_qcall_t *
11948rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11950 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
11951 n->nd_recv = nd_recv;
11952 n->nd_mid = nd_mid;
11953 n->nd_args = nd_args;
11958static rb_node_vcall_t *
11959rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11961 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
11962 n->nd_mid = nd_mid;
11967static rb_node_once_t *
11968rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11970 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
11971 n->nd_body = nd_body;
11976static rb_node_args_t *
11977rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
11979 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
11980 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
11985static rb_node_args_aux_t *
11986rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc)
11988 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
11989 n->nd_pid = nd_pid;
11990 n->nd_plen = nd_plen;
11996static rb_node_opt_arg_t *
11997rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11999 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12000 n->nd_body = nd_body;
12006static rb_node_kw_arg_t *
12007rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12009 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12010 n->nd_body = nd_body;
12016static rb_node_postarg_t *
12017rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12019 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12020 n->nd_1st = nd_1st;
12021 n->nd_2nd = nd_2nd;
12026static rb_node_argscat_t *
12027rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12029 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12030 n->nd_head = nd_head;
12031 n->nd_body = nd_body;
12036static rb_node_argspush_t *
12037rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12039 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12040 n->nd_head = nd_head;
12041 n->nd_body = nd_body;
12046static rb_node_splat_t *
12047rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12049 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12050 n->nd_head = nd_head;
12055static rb_node_block_pass_t *
12056rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12058 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12060 n->nd_body = nd_body;
12065static rb_node_alias_t *
12066rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12068 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12069 n->nd_1st = nd_1st;
12070 n->nd_2nd = nd_2nd;
12075static rb_node_valias_t *
12076rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc)
12078 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12079 n->nd_alias = nd_alias;
12080 n->nd_orig = nd_orig;
12085static rb_node_undef_t *
12086rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12088 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12089 n->nd_undef = nd_undef;
12094static rb_node_errinfo_t *
12095rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12097 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12102static rb_node_defined_t *
12103rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12105 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12106 n->nd_head = nd_head;
12111static rb_node_postexe_t *
12112rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12114 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12115 n->nd_body = nd_body;
12120static rb_node_attrasgn_t *
12121rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12123 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12124 n->nd_recv = nd_recv;
12125 n->nd_mid = nd_mid;
12126 n->nd_args = nd_args;
12131static rb_node_aryptn_t *
12132rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12134 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12136 n->pre_args = pre_args;
12137 n->rest_arg = rest_arg;
12138 n->post_args = post_args;
12143static rb_node_hshptn_t *
12144rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12146 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12147 n->nd_pconst = nd_pconst;
12148 n->nd_pkwargs = nd_pkwargs;
12149 n->nd_pkwrestarg = nd_pkwrestarg;
12154static rb_node_fndptn_t *
12155rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12157 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12159 n->pre_rest_arg = pre_rest_arg;
12161 n->post_rest_arg = post_rest_arg;
12166static rb_node_cdecl_t *
12167rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc)
12169 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12170 n->nd_vid = nd_vid;
12171 n->nd_value = nd_value;
12172 n->nd_else = nd_else;
12177static rb_node_op_cdecl_t *
12178rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, const YYLTYPE *loc)
12180 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12181 n->nd_head = nd_head;
12182 n->nd_value = nd_value;
12183 n->nd_aid = nd_aid;
12188static rb_node_error_t *
12189rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12191 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12198static rb_node_ripper_t *
12199rb_node_ripper_new(struct parser_params *p, ID nd_vid, VALUE nd_rval, VALUE nd_cval, const YYLTYPE *loc)
12201 rb_node_ripper_t *n = NODE_NEWNODE(NODE_RIPPER, rb_node_ripper_t, loc);
12202 n->nd_vid = nd_vid;
12203 n->nd_rval = nd_rval;
12204 n->nd_cval = nd_cval;
12209static rb_node_ripper_values_t *
12210rb_node_ripper_values_new(struct parser_params *p, VALUE nd_val1, VALUE nd_val2, VALUE nd_val3, const YYLTYPE *loc)
12212 rb_node_ripper_values_t *n = NODE_NEWNODE(NODE_RIPPER_VALUES, rb_node_ripper_values_t, loc);
12213 n->nd_val1 = nd_val1;
12214 n->nd_val2 = nd_val2;
12215 n->nd_val3 = nd_val3;
12222static rb_node_break_t *
12223rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12225 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12226 n->nd_stts = nd_stts;
12232static rb_node_next_t *
12233rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12235 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12236 n->nd_stts = nd_stts;
12242static rb_node_redo_t *
12243rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc)
12245 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12251static rb_node_def_temp_t *
12252rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12254 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12255 n->save.cur_arg = p->cur_arg;
12256 n->save.numparam_save = 0;
12257 n->save.max_numparam = 0;
12258 n->save.ctxt = p->ctxt;
12262 n->dot_or_colon = Qnil;
12271static rb_node_def_temp_t *
12272def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12274 n->save.numparam_save = numparam_push(p);
12275 n->save.max_numparam = p->max_numparam;
12280static enum node_type
12281nodetype(NODE *node) /* for debug */
12283 return (enum node_type)nd_type(node);
12287nodeline(NODE *node)
12289 return nd_line(node);
12293newline_node(NODE *node)
12296 node = remove_begin(node);
12297 nd_set_fl_newline(node);
12303fixpos(NODE *node, NODE *orig)
12307 nd_set_line(node, nd_line(orig));
12311parser_warning(struct parser_params *p, NODE *node, const char *mesg)
12313 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12317parser_warn(struct parser_params *p, NODE *node, const char *mesg)
12319 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12323block_append(struct parser_params *p, NODE *head, NODE *tail)
12325 NODE *end, *h = head, *nd;
12327 if (tail == 0) return head;
12329 if (h == 0) return tail;
12330 switch (nd_type(h)) {
12332 h = end = NEW_BLOCK(head, &head->nd_loc);
12336 end = RNODE_BLOCK(h)->nd_end;
12340 nd = RNODE_BLOCK(end)->nd_head;
12341 switch (nd_type(nd)) {
12347 if (RTEST(ruby_verbose)) {
12348 parser_warning(p, tail, "statement not reached");
12356 if (!nd_type_p(tail, NODE_BLOCK)) {
12357 tail = NEW_BLOCK(tail, &tail->nd_loc);
12359 RNODE_BLOCK(end)->nd_next = tail;
12360 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12361 nd_set_last_loc(head, nd_last_loc(tail));
12365/* append item to the list */
12367list_append(struct parser_params *p, NODE *list, NODE *item)
12371 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12372 if (RNODE_LIST(list)->nd_next) {
12373 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12379 RNODE_LIST(list)->as.nd_alen += 1;
12380 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12381 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12383 nd_set_last_loc(list, nd_last_loc(item));
12388/* concat two lists */
12390list_concat(NODE *head, NODE *tail)
12394 if (RNODE_LIST(head)->nd_next) {
12395 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12401 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12402 RNODE_LIST(last)->nd_next = tail;
12403 if (RNODE_LIST(tail)->nd_next) {
12404 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12407 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12410 nd_set_last_loc(head, nd_last_loc(tail));
12416literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
12418 if (NIL_P(tail)) return 1;
12419 if (!rb_enc_compatible(head, tail)) {
12420 compile_error(p, "string literal encodings differ (%s / %s)",
12421 rb_enc_name(rb_enc_get(head)),
12422 rb_enc_name(rb_enc_get(tail)));
12423 rb_str_resize(head, 0);
12424 rb_str_resize(tail, 0);
12427 rb_str_buf_append(head, tail);
12432string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12434 if (htype != NODE_DSTR) return Qfalse;
12435 if (RNODE_DSTR(head)->nd_next) {
12436 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12437 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
12439 const VALUE lit = RNODE_DSTR(head)->nd_lit;
12440 ASSUME(lit != Qfalse);
12444/* concat two string literals */
12446literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12448 enum node_type htype;
12451 if (!head) return tail;
12452 if (!tail) return head;
12454 htype = nd_type(head);
12455 if (htype == NODE_EVSTR) {
12456 head = new_dstr(p, head, loc);
12459 if (p->heredoc_indent > 0) {
12462 head = str2dstr(p, head);
12464 return list_append(p, head, tail);
12469 switch (nd_type(tail)) {
12471 if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12475 lit = RNODE_DSTR(head)->nd_lit;
12477 if (htype == NODE_STR) {
12478 if (!literal_concat0(p, lit, RNODE_STR(tail)->nd_lit)) {
12480 rb_discard_node(p, head);
12481 rb_discard_node(p, tail);
12484 rb_discard_node(p, tail);
12487 list_append(p, head, tail);
12492 if (htype == NODE_STR) {
12493 if (!literal_concat0(p, RNODE_STR(head)->nd_lit, RNODE_DSTR(tail)->nd_lit))
12495 RNODE_DSTR(tail)->nd_lit = RNODE_STR(head)->nd_lit;
12496 rb_discard_node(p, head);
12499 else if (NIL_P(RNODE_DSTR(tail)->nd_lit)) {
12501 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12502 if (!RNODE_DSTR(head)->nd_next) {
12503 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12505 else if (RNODE_DSTR(tail)->nd_next) {
12506 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12507 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12509 rb_discard_node(p, tail);
12511 else if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12512 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->nd_lit))
12514 RNODE_DSTR(tail)->nd_lit = Qnil;
12518 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));
12523 if (htype == NODE_STR) {
12524 head = str2dstr(p, head);
12525 RNODE_DSTR(head)->as.nd_alen = 1;
12527 list_append(p, head, tail);
12534nd_copy_flag(NODE *new_node, NODE *old_node)
12536 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12537 nd_set_line(new_node, nd_line(old_node));
12538 new_node->nd_loc = old_node->nd_loc;
12539 new_node->node_id = old_node->node_id;
12543str2dstr(struct parser_params *p, NODE *node)
12545 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12546 nd_copy_flag(new_node, node);
12547 RNODE_DSTR(new_node)->nd_lit = RNODE_STR(node)->nd_lit;
12548 RNODE_DSTR(new_node)->as.nd_alen = 0;
12549 RNODE_DSTR(new_node)->nd_next = 0;
12550 RNODE_STR(node)->nd_lit = 0;
12556evstr2dstr(struct parser_params *p, NODE *node)
12558 if (nd_type_p(node, NODE_EVSTR)) {
12559 node = new_dstr(p, node, &node->nd_loc);
12565new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12570 switch (nd_type(node)) {
12572 return str2dstr(p, node);
12579 return NEW_EVSTR(head, loc);
12583new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12585 VALUE lit = STR_NEW0();
12586 NODE *dstr = NEW_DSTR(lit, loc);
12587 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12588 return list_append(p, dstr, node);
12592call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12593 const YYLTYPE *op_loc, const YYLTYPE *loc)
12598 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12599 nd_set_line(expr, op_loc->beg_pos.lineno);
12604call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12608 opcall = NEW_OPCALL(recv, id, 0, loc);
12609 nd_set_line(opcall, op_loc->beg_pos.lineno);
12614new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12616 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12617 nd_set_line(qcall, op_loc->beg_pos.lineno);
12622new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12625 if (block) block_dup_check(p, args, block);
12626 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12627 if (block) ret = method_add_block(p, ret, block, loc);
12632#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12635last_expr_once_body(NODE *node)
12637 if (!node) return 0;
12638 return nd_once_body(node);
12642match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12645 int line = op_loc->beg_pos.lineno;
12650 if ((n = last_expr_once_body(node1)) != 0) {
12651 switch (nd_type(n)) {
12654 NODE *match = NEW_MATCH2(node1, node2, loc);
12655 nd_set_line(match, line);
12660 if (RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) {
12661 const VALUE lit = RNODE_LIT(n)->nd_lit;
12662 NODE *match = NEW_MATCH2(node1, node2, loc);
12663 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc);
12664 nd_set_line(match, line);
12670 if ((n = last_expr_once_body(node2)) != 0) {
12673 switch (nd_type(n)) {
12675 if (!RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) break;
12678 match3 = NEW_MATCH3(node2, node1, loc);
12683 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12684 nd_set_line(n, line);
12688# if WARN_PAST_SCOPE
12690past_dvar_p(struct parser_params *p, ID id)
12692 struct vtable *past = p->lvtbl->past;
12694 if (vtable_included(past, id)) return 1;
12702numparam_nested_p(struct parser_params *p)
12704 struct local_vars *local = p->lvtbl;
12705 NODE *outer = local->numparam.outer;
12706 NODE *inner = local->numparam.inner;
12707 if (outer || inner) {
12708 NODE *used = outer ? outer : inner;
12709 compile_error(p, "numbered parameter is already used in\n"
12710 "%s:%d: %s block here",
12711 p->ruby_sourcefile, nd_line(used),
12712 outer ? "outer" : "inner");
12713 parser_show_error_line(p, &used->nd_loc);
12720gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12726 return NEW_SELF(loc);
12728 return NEW_NIL(loc);
12730 return NEW_TRUE(loc);
12731 case keyword_false:
12732 return NEW_FALSE(loc);
12733 case keyword__FILE__:
12735 VALUE file = p->ruby_sourcefile_string;
12737 file = rb_str_new(0, 0);
12739 file = rb_str_dup(file);
12740 node = NEW_STR(file, loc);
12741 RB_OBJ_WRITTEN(p->ast, Qnil, file);
12744 case keyword__LINE__:
12745 return NEW_LIT(INT2FIX(loc->beg_pos.lineno), loc);
12746 case keyword__ENCODING__:
12747 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
12748 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12752 switch (id_type(id)) {
12754 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12755 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
12756 if (id == p->cur_arg) {
12757 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12760 if (vidp) *vidp |= LVAR_USED;
12761 node = NEW_DVAR(id, loc);
12764 if (local_id_ref(p, id, &vidp)) {
12765 if (id == p->cur_arg) {
12766 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12769 if (vidp) *vidp |= LVAR_USED;
12770 node = NEW_LVAR(id, loc);
12773 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12774 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12775 if (numparam_nested_p(p)) return 0;
12776 node = NEW_DVAR(id, loc);
12777 struct local_vars *local = p->lvtbl;
12778 if (!local->numparam.current) local->numparam.current = node;
12781# if WARN_PAST_SCOPE
12782 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
12783 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
12786 /* method call without arguments */
12787 if (dyna_in_block(p) && id == rb_intern("it")
12788 && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))
12789 && p->max_numparam != ORDINAL_PARAM) {
12790 rb_warn0("`it` calls without arguments will refer to the first block param in Ruby 3.4; use it() or self.it");
12792 return NEW_VCALL(id, loc);
12794 return NEW_GVAR(id, loc);
12796 return NEW_IVAR(id, loc);
12798 return NEW_CONST(id, loc);
12800 return NEW_CVAR(id, loc);
12802 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
12806static rb_node_opt_arg_t *
12807opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
12809 rb_node_opt_arg_t *opts = opt_list;
12810 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12812 while (opts->nd_next) {
12813 opts = opts->nd_next;
12814 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12816 opts->nd_next = opt;
12821static rb_node_kw_arg_t *
12822kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
12825 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
12826 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
12832new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
12836 if (nd_type_p(n, NODE_BEGIN)) {
12837 n = RNODE_BEGIN(n)->nd_body;
12839 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
12840 n = RNODE_BLOCK(n)->nd_head;
12846 return NEW_DEFINED(n, loc);
12850str_to_sym_check(struct parser_params *p, VALUE lit, const YYLTYPE *loc)
12852 if (rb_enc_str_coderange(lit) == ENC_CODERANGE_BROKEN) {
12853 yyerror1(loc, "invalid symbol");
12861symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
12865 enum node_type type = nd_type(symbol);
12868 nd_set_type(symbol, NODE_DSYM);
12871 nd_set_type(symbol, NODE_LIT);
12872 lit = str_to_sym_check(p, RNODE_LIT(symbol)->nd_lit, &RNODE(symbol)->nd_loc);
12873 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(symbol)->nd_lit = rb_str_intern(lit));
12876 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
12878 return list_append(p, symbols, symbol);
12882new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
12884 struct RNode_LIST *list;
12889 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
12890 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12893 switch (nd_type(node)) {
12896 VALUE src = RNODE_STR(node)->nd_lit;
12897 nd_set_type(node, NODE_LIT);
12898 nd_set_loc(node, loc);
12899 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit = reg_compile(p, src, options));
12904 node = NEW_DSTR0(lit, 1, NEW_LIST(node, loc), loc);
12905 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12908 nd_set_type(node, NODE_DREGX);
12909 nd_set_loc(node, loc);
12910 RNODE_DREGX(node)->nd_cflag = options & RE_OPTION_MASK;
12911 if (!NIL_P(RNODE_DREGX(node)->nd_lit)) reg_fragment_check(p, RNODE_DREGX(node)->nd_lit, options);
12912 for (list = RNODE_DREGX(prev = node)->nd_next; list; list = RNODE_LIST(list->nd_next)) {
12913 NODE *frag = list->nd_head;
12914 enum node_type type = nd_type(frag);
12915 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
12916 VALUE tail = RNODE_STR(frag)->nd_lit;
12917 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(RNODE_DREGX(prev)->nd_lit)) {
12918 VALUE lit = prev == node ? RNODE_DREGX(prev)->nd_lit : RNODE_LIT(RNODE_LIST(prev)->nd_head)->nd_lit;
12919 if (!literal_concat0(p, lit, tail)) {
12920 return NEW_NIL(loc); /* dummy node on error */
12922 rb_str_resize(tail, 0);
12923 RNODE_LIST(prev)->nd_next = list->nd_next;
12924 rb_discard_node(p, list->nd_head);
12925 rb_discard_node(p, (NODE *)list);
12926 list = RNODE_LIST(prev);
12929 prev = (NODE *)list;
12936 if (!RNODE_DREGX(node)->nd_next) {
12937 VALUE src = RNODE_DREGX(node)->nd_lit;
12938 VALUE re = reg_compile(p, src, options);
12939 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_DREGX(node)->nd_lit = re);
12941 if (options & RE_OPTION_ONCE) {
12942 node = NEW_ONCE(node, loc);
12949static rb_node_kw_arg_t *
12950new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
12953 return NEW_KW_ARG((k), loc);
12957new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12960 VALUE lit = STR_NEW0();
12961 NODE *xstr = NEW_XSTR(lit, loc);
12962 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12965 switch (nd_type(node)) {
12967 nd_set_type(node, NODE_XSTR);
12968 nd_set_loc(node, loc);
12971 nd_set_type(node, NODE_DXSTR);
12972 nd_set_loc(node, loc);
12975 node = NEW_DXSTR(Qnil, 1, NEW_LIST(node, loc), loc);
12982check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
12986 if (!arg || !p->case_labels) return;
12988 lit = rb_node_case_when_optimizable_literal(arg);
12989 if (UNDEF_P(lit)) return;
12990 if (nd_type_p(arg, NODE_STR)) {
12991 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(arg)->nd_lit = lit);
12994 if (NIL_P(p->case_labels)) {
12995 p->case_labels = rb_obj_hide(rb_hash_new());
12998 VALUE line = rb_hash_lookup(p->case_labels, lit);
12999 if (!NIL_P(line)) {
13000 rb_warning1("duplicated `when' clause with line %d is ignored",
13005 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
13010id_is_var(struct parser_params *p, ID id)
13012 if (is_notop_id(id)) {
13013 switch (id & ID_SCOPE_MASK) {
13014 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13017 if (dyna_in_block(p)) {
13018 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13020 if (local_id(p, id)) return 1;
13021 /* method call without arguments */
13025 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13030new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
13032 VALUE src = 0, err = 0;
13034 if (ripper_is_node_yylval(p, re)) {
13035 src = RNODE_RIPPER(re)->nd_cval;
13036 re = RNODE_RIPPER(re)->nd_rval;
13038 if (ripper_is_node_yylval(p, opt)) {
13039 options = (int)RNODE_RIPPER(opt)->nd_vid;
13040 opt = RNODE_RIPPER(opt)->nd_rval;
13042 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
13043 compile_error(p, "%"PRIsVALUE, err);
13045 return dispatch2(regexp_literal, re, opt);
13047#endif /* !RIPPER */
13049static inline enum lex_state_e
13050parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13053 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13055 return p->lex.state = ls;
13059static const char rb_parser_lex_state_names[][8] = {
13060 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13061 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13062 "LABEL", "LABELED","FITEM",
13066append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13069 unsigned int mask = 1;
13070 static const char none[] = "NONE";
13072 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13073 if ((unsigned)state & mask) {
13075 rb_str_cat(buf, "|", 1);
13078 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13082 rb_str_cat(buf, none, sizeof(none)-1);
13088flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13090 VALUE mesg = p->debug_buffer;
13092 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13093 p->debug_buffer = Qnil;
13094 rb_io_puts(1, &mesg, out);
13096 if (!NIL_P(str) && RSTRING_LEN(str)) {
13097 rb_io_write(p->debug_output, str);
13102rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13103 enum lex_state_e to, int line)
13106 mesg = rb_str_new_cstr("lex_state: ");
13107 append_lex_state_name(p, from, mesg);
13108 rb_str_cat_cstr(mesg, " -> ");
13109 append_lex_state_name(p, to, mesg);
13110 rb_str_catf(mesg, " at line %d\n", line);
13111 flush_debug_buffer(p, p->debug_output, mesg);
13116rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13118 return rb_fstring(append_lex_state_name(p, state, rb_str_new(0, 0)));
13122append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13125 rb_str_cat_cstr(mesg, "0");
13128 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13129 for (; mask && !(stack & mask); mask >>= 1) continue;
13130 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13135rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13136 const char *name, int line)
13138 VALUE mesg = rb_sprintf("%s: ", name);
13139 append_bitstack_value(p, stack, mesg);
13140 rb_str_catf(mesg, " at line %d\n", line);
13141 flush_debug_buffer(p, p->debug_output, mesg);
13145rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13148 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13151 rb_str_vcatf(mesg, fmt, ap);
13153 yyerror0(RSTRING_PTR(mesg));
13156 mesg = rb_str_new(0, 0);
13157 append_lex_state_name(p, p->lex.state, mesg);
13158 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13159 rb_str_resize(mesg, 0);
13160 append_bitstack_value(p, p->cond_stack, mesg);
13161 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13162 rb_str_resize(mesg, 0);
13163 append_bitstack_value(p, p->cmdarg_stack, mesg);
13164 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13165 if (p->debug_output == rb_ractor_stdout())
13166 p->debug_output = rb_ractor_stderr();
13171rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13173 yylloc->beg_pos.lineno = sourceline;
13174 yylloc->beg_pos.column = beg_pos;
13175 yylloc->end_pos.lineno = sourceline;
13176 yylloc->end_pos.column = end_pos;
13181rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13183 int sourceline = here->sourceline;
13184 int beg_pos = (int)here->offset - here->quote
13185 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13186 int end_pos = (int)here->offset + here->length + here->quote;
13188 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13192rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13194 yylloc->beg_pos.lineno = p->delayed.beg_line;
13195 yylloc->beg_pos.column = p->delayed.beg_col;
13196 yylloc->end_pos.lineno = p->delayed.end_line;
13197 yylloc->end_pos.column = p->delayed.end_col;
13203rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13205 int sourceline = p->ruby_sourceline;
13206 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13207 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13208 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13212rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13214 yylloc->end_pos = yylloc->beg_pos;
13220rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13222 int sourceline = p->ruby_sourceline;
13223 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13224 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13225 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13229rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13231 int sourceline = p->ruby_sourceline;
13232 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13233 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13234 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13236#endif /* !RIPPER */
13239assignable0(struct parser_params *p, ID id, const char **err)
13241 if (!id) return -1;
13244 *err = "Can't change the value of self";
13247 *err = "Can't assign to nil";
13250 *err = "Can't assign to true";
13252 case keyword_false:
13253 *err = "Can't assign to false";
13255 case keyword__FILE__:
13256 *err = "Can't assign to __FILE__";
13258 case keyword__LINE__:
13259 *err = "Can't assign to __LINE__";
13261 case keyword__ENCODING__:
13262 *err = "Can't assign to __ENCODING__";
13265 switch (id_type(id)) {
13267 if (dyna_in_block(p)) {
13268 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13269 compile_error(p, "Can't assign to numbered parameter _%d",
13270 NUMPARAM_ID_TO_IDX(id));
13273 if (dvar_curr(p, id)) return NODE_DASGN;
13274 if (dvar_defined(p, id)) return NODE_DASGN;
13275 if (local_id(p, id)) return NODE_LASGN;
13280 if (!local_id(p, id)) local_var(p, id);
13284 case ID_GLOBAL: return NODE_GASGN;
13285 case ID_INSTANCE: return NODE_IASGN;
13287 if (!p->ctxt.in_def) return NODE_CDECL;
13288 *err = "dynamic constant assignment";
13290 case ID_CLASS: return NODE_CVASGN;
13292 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13299assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13301 const char *err = 0;
13302 int node_type = assignable0(p, id, &err);
13303 switch (node_type) {
13304 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13305 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13306 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13307 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13308 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
13309 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13311 if (err) yyerror1(loc, err);
13312 return NEW_ERROR(loc);
13316assignable(struct parser_params *p, VALUE lhs)
13318 const char *err = 0;
13319 assignable0(p, get_id(lhs), &err);
13320 if (err) lhs = assign_error(p, err, lhs);
13326is_private_local_id(struct parser_params *p, ID name)
13329 if (name == idUScore) return 1;
13330 if (!is_local_id(name)) return 0;
13331 s = rb_id2str(name);
13333 return RSTRING_PTR(s)[0] == '_';
13337shadowing_lvar_0(struct parser_params *p, ID name)
13339 if (dyna_in_block(p)) {
13340 if (dvar_curr(p, name)) {
13341 if (is_private_local_id(p, name)) return 1;
13342 yyerror0("duplicated argument name");
13344 else if (dvar_defined(p, name) || local_id(p, name)) {
13345 vtable_add(p->lvtbl->vars, name);
13346 if (p->lvtbl->used) {
13347 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13353 if (local_id(p, name)) {
13354 if (is_private_local_id(p, name)) return 1;
13355 yyerror0("duplicated argument name");
13362shadowing_lvar(struct parser_params *p, ID name)
13364 shadowing_lvar_0(p, name);
13369new_bv(struct parser_params *p, ID name)
13372 if (!is_local_id(name)) {
13373 compile_error(p, "invalid local variable - %"PRIsVALUE,
13377 if (!shadowing_lvar_0(p, name)) return;
13383aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13385 return NEW_ATTRASGN(recv, tASET, idx, loc);
13389block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13391 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13392 compile_error(p, "both block arg and actual block given");
13397attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13399 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13400 return NEW_ATTRASGN(recv, id, 0, loc);
13404rb_backref_error(struct parser_params *p, NODE *node)
13406 switch (nd_type(node)) {
13408 compile_error(p, "Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13410 case NODE_BACK_REF:
13411 compile_error(p, "Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13417backref_error(struct parser_params *p, NODE *ref, VALUE expr)
13419 VALUE mesg = rb_str_new_cstr("Can't set variable ");
13420 rb_str_append(mesg, RNODE_RIPPER(ref)->nd_cval);
13421 return dispatch2(assign_error, mesg, expr);
13427arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13429 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13430 switch (nd_type(node1)) {
13432 return list_append(p, node1, node2);
13433 case NODE_BLOCK_PASS:
13434 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13435 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13437 case NODE_ARGSPUSH:
13438 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13439 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13440 nd_set_type(node1, NODE_ARGSCAT);
13443 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13444 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13445 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13448 return NEW_ARGSPUSH(node1, node2, loc);
13452arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13454 if (!node2) return node1;
13455 switch (nd_type(node1)) {
13456 case NODE_BLOCK_PASS:
13457 if (RNODE_BLOCK_PASS(node1)->nd_head)
13458 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13460 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13462 case NODE_ARGSPUSH:
13463 if (!nd_type_p(node2, NODE_LIST)) break;
13464 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13465 nd_set_type(node1, NODE_ARGSCAT);
13468 if (!nd_type_p(node2, NODE_LIST) ||
13469 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13470 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13473 return NEW_ARGSCAT(node1, node2, loc);
13477last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13480 if ((n1 = splat_array(args)) != 0) {
13481 return list_append(p, n1, last_arg);
13483 return arg_append(p, args, last_arg, loc);
13487rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13490 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13491 return list_concat(n1, rest_arg);
13493 return arg_concat(p, args, rest_arg, loc);
13497splat_array(NODE* node)
13499 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13500 if (nd_type_p(node, NODE_LIST)) return node;
13505mark_lvar_used(struct parser_params *p, NODE *rhs)
13509 switch (nd_type(rhs)) {
13511 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13512 if (vidp) *vidp |= LVAR_USED;
13516 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13517 if (vidp) *vidp |= LVAR_USED;
13522 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13523 mark_lvar_used(p, rhs->nd_head);
13531const_decl_path(struct parser_params *p, NODE *dest)
13534 if (!nd_type_p(n, NODE_CALL)) {
13535 const YYLTYPE *loc = &n->nd_loc;
13537 if (RNODE_CDECL(n)->nd_vid) {
13538 path = rb_id2str(RNODE_CDECL(n)->nd_vid);
13541 n = RNODE_CDECL(n)->nd_else;
13542 path = rb_ary_new();
13543 for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
13544 rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
13546 if (n && nd_type_p(n, NODE_CONST)) {
13548 rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid));
13550 else if (n && nd_type_p(n, NODE_COLON3)) {
13552 rb_ary_push(path, rb_str_new(0, 0));
13555 // expression::Name
13556 rb_ary_push(path, rb_str_new_cstr("..."));
13558 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
13559 path = rb_fstring(path);
13561 n = NEW_LIT(path, loc);
13562 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13568make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
13570 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13573 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
13574 NEW_LIST(value, loc), loc);
13577 return NEW_CALL(fcore, rb_intern("make_shareable"),
13578 NEW_LIST(value, loc), loc);
13583ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
13585 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13586 NODE *args = NEW_LIST(value, loc);
13587 args = list_append(p, args, const_decl_path(p, *dest));
13588 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
13591static int is_static_content(NODE *node);
13594shareable_literal_value(struct parser_params *p, NODE *node)
13596 if (!node) return Qnil;
13597 enum node_type type = nd_type(node);
13606 return RNODE_LIT(node)->nd_lit;
13612#ifndef SHAREABLE_BARE_EXPRESSION
13613#define SHAREABLE_BARE_EXPRESSION 1
13617shareable_literal_constant(struct parser_params *p, enum shareability shareable,
13618 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
13620# define shareable_literal_constant_next(n) \
13621 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
13624 if (!value) return 0;
13625 enum node_type type = nd_type(value);
13634 if (shareable == shareable_literal) {
13635 value = NEW_CALL(value, idUMinus, 0, loc);
13640 lit = rb_fstring(RNODE_STR(value)->nd_lit);
13641 nd_set_type(value, NODE_LIT);
13642 RB_OBJ_WRITE(p->ast, &RNODE_LIT(value)->nd_lit, lit);
13646 lit = rb_ary_new();
13647 OBJ_FREEZE_RAW(lit);
13648 NODE *n = NEW_LIT(lit, loc);
13649 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13653 lit = rb_ary_new();
13654 for (NODE *n = value; n; n = RNODE_LIST(n)->nd_next) {
13655 NODE *elt = RNODE_LIST(n)->nd_head;
13657 elt = shareable_literal_constant_next(elt);
13659 RNODE_LIST(n)->nd_head = elt;
13661 else if (RTEST(lit)) {
13667 VALUE e = shareable_literal_value(p, elt);
13669 rb_ary_push(lit, e);
13673 lit = Qnil; /* make shareable at runtime */
13680 if (!RNODE_HASH(value)->nd_brace) return 0;
13681 lit = rb_hash_new();
13682 for (NODE *n = RNODE_HASH(value)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
13683 NODE *key = RNODE_LIST(n)->nd_head;
13684 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
13686 key = shareable_literal_constant_next(key);
13688 RNODE_LIST(n)->nd_head = key;
13690 else if (RTEST(lit)) {
13691 rb_hash_clear(lit);
13696 val = shareable_literal_constant_next(val);
13698 RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head = val;
13700 else if (RTEST(lit)) {
13701 rb_hash_clear(lit);
13706 VALUE k = shareable_literal_value(p, key);
13707 VALUE v = shareable_literal_value(p, val);
13708 if (!UNDEF_P(k) && !UNDEF_P(v)) {
13709 rb_hash_aset(lit, k, v);
13712 rb_hash_clear(lit);
13713 lit = Qnil; /* make shareable at runtime */
13720 if (shareable == shareable_literal &&
13721 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
13722 return ensure_shareable_node(p, dest, value, loc);
13727 /* Array or Hash */
13728 if (!lit) return 0;
13730 // if shareable_literal, all elements should have been ensured
13732 value = make_shareable_node(p, value, false, loc);
13735 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
13736 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(value)->nd_lit);
13740# undef shareable_literal_constant_next
13744shareable_constant_value(struct parser_params *p, enum shareability shareable,
13745 NODE *lhs, NODE *value, const YYLTYPE *loc)
13747 if (!value) return 0;
13748 switch (shareable) {
13749 case shareable_none:
13752 case shareable_literal:
13754 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13755 if (lit) return lit;
13760 case shareable_copy:
13761 case shareable_everything:
13763 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13764 if (lit) return lit;
13765 return make_shareable_node(p, value, shareable == shareable_copy, loc);
13770 UNREACHABLE_RETURN(0);
13775node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13777 if (!lhs) return 0;
13779 switch (nd_type(lhs)) {
13781 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13790 set_nd_value(p, lhs, rhs);
13791 nd_set_loc(lhs, loc);
13794 case NODE_ATTRASGN:
13795 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13796 nd_set_loc(lhs, loc);
13800 /* should not happen */
13808value_expr_check(struct parser_params *p, NODE *node)
13810 NODE *void_node = 0, *vn;
13813 rb_warning0("empty expression");
13816 switch (nd_type(node)) {
13822 return void_node ? void_node : node;
13825 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13826 compile_error(p, "unexpected node");
13829 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13832 /* single line pattern matching with "=>" operator */
13833 return void_node ? void_node : node;
13836 while (RNODE_BLOCK(node)->nd_next) {
13837 node = RNODE_BLOCK(node)->nd_next;
13839 node = RNODE_BLOCK(node)->nd_head;
13843 node = RNODE_BEGIN(node)->nd_body;
13848 if (!RNODE_IF(node)->nd_body) {
13851 else if (!RNODE_IF(node)->nd_else) {
13854 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13855 if (!vn) return NULL;
13856 if (!void_node) void_node = vn;
13857 node = RNODE_IF(node)->nd_else;
13862 node = RNODE_AND(node)->nd_1st;
13868 mark_lvar_used(p, node);
13880value_expr_gen(struct parser_params *p, NODE *node)
13882 NODE *void_node = value_expr_check(p, node);
13884 yyerror1(&void_node->nd_loc, "void value expression");
13885 /* or "control never reach"? */
13892void_expr(struct parser_params *p, NODE *node)
13894 const char *useless = 0;
13896 if (!RTEST(ruby_verbose)) return;
13898 if (!node || !(node = nd_once_body(node))) return;
13899 switch (nd_type(node)) {
13901 switch (RNODE_OPCALL(node)->nd_mid) {
13920 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13931 case NODE_BACK_REF:
13932 useless = "a variable";
13935 useless = "a constant";
13941 useless = "a literal";
13966 useless = "defined?";
13971 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13976void_stmts(struct parser_params *p, NODE *node)
13978 NODE *const n = node;
13979 if (!RTEST(ruby_verbose)) return n;
13980 if (!node) return n;
13981 if (!nd_type_p(node, NODE_BLOCK)) return n;
13983 while (RNODE_BLOCK(node)->nd_next) {
13984 void_expr(p, RNODE_BLOCK(node)->nd_head);
13985 node = RNODE_BLOCK(node)->nd_next;
13991remove_begin(NODE *node)
13993 NODE **n = &node, *n1 = node;
13994 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
13995 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14001reduce_nodes(struct parser_params *p, NODE **body)
14003 NODE *node = *body;
14006 *body = NEW_NIL(&NULL_LOC);
14009#define subnodes(type, n1, n2) \
14010 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14011 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14012 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14015 int newline = (int)(nd_fl_newline(node));
14016 switch (nd_type(node)) {
14022 *body = node = RNODE_RETURN(node)->nd_stts;
14023 if (newline && node) nd_set_fl_newline(node);
14026 *body = node = RNODE_BEGIN(node)->nd_body;
14027 if (newline && node) nd_set_fl_newline(node);
14030 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14034 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14037 body = &RNODE_CASE(node)->nd_body;
14040 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14043 if (!subnodes(RNODE_ENSURE, nd_head, nd_resq)) goto end;
14046 newline = 0; // RESBODY should not be a NEWLINE
14047 if (RNODE_RESCUE(node)->nd_else) {
14048 body = &RNODE_RESCUE(node)->nd_resq;
14051 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14057 if (newline && node) nd_set_fl_newline(node);
14064is_static_content(NODE *node)
14066 if (!node) return 1;
14067 switch (nd_type(node)) {
14069 if (!(node = RNODE_HASH(node)->nd_head)) break;
14072 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14073 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14088assign_in_cond(struct parser_params *p, NODE *node)
14090 switch (nd_type(node)) {
14104 if (!get_nd_value(p, node)) return 1;
14105 if (is_static_content(get_nd_value(p, node))) {
14106 /* reports always */
14107 parser_warn(p, get_nd_value(p, node), "found `= literal' in conditional, should be ==");
14118#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14120 case COND_IN_OP: break; \
14121 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14122 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14126static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14129range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14131 enum node_type type;
14133 if (node == 0) return 0;
14135 type = nd_type(node);
14137 if (type == NODE_LIT && FIXNUM_P(RNODE_LIT(node)->nd_lit)) {
14138 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
14139 ID lineno = rb_intern("$.");
14140 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14142 return cond0(p, node, COND_IN_FF, loc, true);
14146cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14148 if (node == 0) return 0;
14149 if (!(node = nd_once_body(node))) return 0;
14150 assign_in_cond(p, node);
14152 switch (nd_type(node)) {
14154 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14160 SWITCH_BY_COND_TYPE(type, warn, "string ");
14164 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14166 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14170 NODE *end = RNODE_BLOCK(node)->nd_end;
14171 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14172 if (top) top = node == end;
14173 *expr = cond0(p, *expr, type, loc, top);
14179 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14180 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14186 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14187 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14188 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
14189 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
14194 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14198 if (RB_TYPE_P(RNODE_LIT(node)->nd_lit, T_REGEXP)) {
14199 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14200 nd_set_type(node, NODE_MATCH);
14202 else if (RNODE_LIT(node)->nd_lit == Qtrue ||
14203 RNODE_LIT(node)->nd_lit == Qfalse) {
14204 /* booleans are OK, e.g., while true */
14206 else if (SYMBOL_P(RNODE_LIT(node)->nd_lit)) {
14210 SWITCH_BY_COND_TYPE(type, warning, "");
14219cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14221 if (node == 0) return 0;
14222 return cond0(p, node, COND_IN_COND, loc, true);
14226method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14228 if (node == 0) return 0;
14229 return cond0(p, node, COND_IN_OP, loc, true);
14233new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14235 YYLTYPE loc = {*pos, *pos};
14236 return NEW_NIL(&loc);
14240new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14242 if (!cc) return right;
14243 cc = cond0(p, cc, COND_IN_COND, loc, true);
14244 return newline_node(NEW_IF(cc, left, right, loc));
14248new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14250 if (!cc) return right;
14251 cc = cond0(p, cc, COND_IN_COND, loc, true);
14252 return newline_node(NEW_UNLESS(cc, left, right, loc));
14255#define NEW_AND_OR(type, f, s, loc) (type == NODE_AND ? NEW_AND(f,s,loc) : NEW_OR(f,s,loc))
14258logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14259 const YYLTYPE *op_loc, const YYLTYPE *loc)
14261 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14264 if (left && nd_type_p(left, type)) {
14265 NODE *node = left, *second;
14266 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14269 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc);
14270 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14271 left->nd_loc.end_pos = loc->end_pos;
14274 op = NEW_AND_OR(type, left, right, loc);
14275 nd_set_line(op, op_loc->beg_pos.lineno);
14282no_blockarg(struct parser_params *p, NODE *node)
14284 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14285 compile_error(p, "block argument should not be given");
14290ret_args(struct parser_params *p, NODE *node)
14293 no_blockarg(p, node);
14294 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14295 node = RNODE_LIST(node)->nd_head;
14302new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14304 if (node) no_blockarg(p, node);
14306 return NEW_YIELD(node, loc);
14310negate_lit(struct parser_params *p, VALUE lit)
14312 if (FIXNUM_P(lit)) {
14313 return LONG2FIX(-FIX2LONG(lit));
14315 if (SPECIAL_CONST_P(lit)) {
14317 if (FLONUM_P(lit)) {
14318 return DBL2NUM(-RFLOAT_VALUE(lit));
14323 switch (BUILTIN_TYPE(lit)) {
14325 bignum_negate(lit);
14326 lit = rb_big_norm(lit);
14329 rational_set_num(lit, negate_lit(p, rational_get_num(lit)));
14332 rcomplex_set_real(lit, negate_lit(p, rcomplex_get_real(lit)));
14333 rcomplex_set_imag(lit, negate_lit(p, rcomplex_get_imag(lit)));
14336 lit = DBL2NUM(-RFLOAT_VALUE(lit));
14340 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
14341 rb_builtin_class_name(lit));
14348arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14351 if (!node1) return (NODE *)node2;
14352 node2->nd_head = node1;
14353 nd_set_first_lineno(node2, nd_first_lineno(node1));
14354 nd_set_first_column(node2, nd_first_column(node1));
14355 return (NODE *)node2;
14361args_info_empty_p(struct rb_args_info *args)
14363 if (args->pre_args_num) return false;
14364 if (args->post_args_num) return false;
14365 if (args->rest_arg) return false;
14366 if (args->opt_args) return false;
14367 if (args->block_arg) return false;
14368 if (args->kw_args) return false;
14369 if (args->kw_rest_arg) return false;
14373static rb_node_args_t *
14374new_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)
14376 struct rb_args_info *args = &tail->nd_ainfo;
14378 if (args->forwarding) {
14380 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14383 rest_arg = idFWD_REST;
14386 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
14387 args->pre_init = pre_args ? pre_args->nd_next : 0;
14389 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
14390 args->post_init = post_args ? post_args->nd_next : 0;
14391 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14393 args->rest_arg = rest_arg;
14395 args->opt_args = opt_args;
14397#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14398 args->ruby2_keywords = args->forwarding;
14400 args->ruby2_keywords = 0;
14403 nd_set_loc(RNODE(tail), loc);
14408static rb_node_args_t *
14409new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14411 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14412 struct rb_args_info *args = &node->nd_ainfo;
14413 if (p->error_p) return node;
14415 args->block_arg = block;
14416 args->kw_args = kw_args;
14420 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14421 * variable order: k1, kr1, k2, &b, internal_id, krest
14423 * variable order: kr1, k1, k2, internal_id, krest, &b
14425 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14426 struct vtable *vtargs = p->lvtbl->args;
14427 rb_node_kw_arg_t *kwn = kw_args;
14429 if (block) block = vtargs->tbl[vtargs->pos-1];
14430 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14431 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14433 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14435 --required_kw_vars;
14436 kwn = kwn->nd_next;
14439 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14440 ID vid = get_nd_vid(p, kwn->nd_body);
14441 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14442 *required_kw_vars++ = vid;
14449 arg_var(p, kw_bits);
14450 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14451 if (block) arg_var(p, block);
14453 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14455 else if (kw_rest_arg == idNil) {
14456 args->no_kwarg = 1;
14458 else if (kw_rest_arg) {
14459 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14465static rb_node_args_t *
14466args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam)
14468 if (max_numparam > NO_PARAM) {
14470 YYLTYPE loc = RUBY_INIT_YYLLOC();
14471 args = new_args_tail(p, 0, 0, 0, 0);
14472 nd_set_loc(RNODE(args), &loc);
14474 args->nd_ainfo.pre_args_num = max_numparam;
14480new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14482 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14485 NODE *pre_args = NEW_LIST(pre_arg, loc);
14486 if (RNODE_ARYPTN(aryptn)->pre_args) {
14487 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14490 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14497new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14500 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14505 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14511new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14513 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14519new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14521 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14522 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14523 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14529new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14531 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14536new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14538 NODE *node, *kw_rest_arg_node;
14540 if (kw_rest_arg == idNil) {
14541 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14543 else if (kw_rest_arg) {
14544 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14547 kw_rest_arg_node = NULL;
14550 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14556dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14561 return NEW_LIT(ID2SYM(idNULL), loc);
14564 switch (nd_type(node)) {
14566 nd_set_type(node, NODE_DSYM);
14567 nd_set_loc(node, loc);
14570 lit = str_to_sym_check(p, RNODE_STR(node)->nd_lit, &RNODE(node)->nd_loc);
14571 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit = ID2SYM(rb_intern_str(lit)));
14572 nd_set_type(node, NODE_LIT);
14573 nd_set_loc(node, loc);
14576 node = NEW_DSYM(Qnil, 1, NEW_LIST(node, loc), loc);
14583append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
14585 NODE *node = (NODE *)v;
14586 NODE **result = (NODE **)h;
14587 RNODE_LIST(node)->as.nd_alen = 2;
14588 RNODE_LIST(RNODE_LIST(node)->nd_next)->as.nd_end = RNODE_LIST(node)->nd_next;
14589 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next = 0;
14591 list_concat(*result, node);
14594 return ST_CONTINUE;
14598remove_duplicate_keys(struct parser_params *p, NODE *hash)
14600 struct st_hash_type literal_type = {
14605 st_table *literal_keys = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14607 NODE *last_expr = 0;
14608 rb_code_location_t loc = hash->nd_loc;
14609 while (hash && RNODE_LIST(hash)->nd_next) {
14610 NODE *head = RNODE_LIST(hash)->nd_head;
14611 NODE *value = RNODE_LIST(hash)->nd_next;
14612 NODE *next = RNODE_LIST(value)->nd_next;
14613 st_data_t key = (st_data_t)head;
14615 RNODE_LIST(value)->nd_next = 0;
14617 key = (st_data_t)value;
14619 else if (nd_type_p(head, NODE_LIT) &&
14620 st_delete(literal_keys, (key = (st_data_t)RNODE_LIT(head)->nd_lit, &key), &data)) {
14621 NODE *dup_value = (RNODE_LIST((NODE *)data))->nd_next;
14622 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
14623 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
14624 RNODE_LIT(head)->nd_lit, nd_line(head));
14625 if (dup_value == last_expr) {
14626 RNODE_LIST(value)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(value)->nd_head);
14629 RNODE_LIST(last_expr)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(last_expr)->nd_head);
14632 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
14633 last_expr = !head || nd_type_p(head, NODE_LIT) ? value : head;
14636 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
14637 st_free_table(literal_keys);
14639 if (!result) result = hash;
14640 else list_concat(result, hash);
14642 result->nd_loc = loc;
14647new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14649 if (hash) hash = remove_duplicate_keys(p, hash);
14650 return NEW_HASH(hash, loc);
14655error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14657 if (is_private_local_id(p, id)) {
14660 if (st_is_member(p->pvtbl, id)) {
14661 yyerror1(loc, "duplicated variable name");
14664 st_insert(p->pvtbl, (st_data_t)id, 0);
14669error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14672 p->pktbl = st_init_numtable();
14674 else if (st_is_member(p->pktbl, key)) {
14675 yyerror1(loc, "duplicated key name");
14678 st_insert(p->pktbl, (st_data_t)key, 0);
14683new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14685 return NEW_HASH(hash, loc);
14687#endif /* !RIPPER */
14691new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14696 ID vid = get_nd_vid(p, lhs);
14697 YYLTYPE lhs_loc = lhs->nd_loc;
14698 int shareable = ctxt.shareable_constant_value;
14700 switch (nd_type(lhs)) {
14711 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14712 set_nd_value(p, lhs, rhs);
14713 nd_set_loc(lhs, loc);
14714 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14716 else if (op == tANDOP) {
14718 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14720 set_nd_value(p, lhs, rhs);
14721 nd_set_loc(lhs, loc);
14722 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14726 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14728 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14730 set_nd_value(p, asgn, rhs);
14731 nd_set_loc(asgn, loc);
14735 asgn = NEW_ERROR(loc);
14741new_ary_op_assign(struct parser_params *p, NODE *ary,
14742 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
14746 args = make_list(args, args_loc);
14747 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc);
14753new_attr_op_assign(struct parser_params *p, NODE *lhs,
14754 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
14758 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
14764new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14769 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
14770 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
14773 asgn = NEW_ERROR(loc);
14780const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14782 if (p->ctxt.in_def) {
14783 yyerror1(loc, "dynamic constant assignment");
14785 return NEW_CDECL(0, 0, (path), loc);
14789const_decl(struct parser_params *p, VALUE path)
14791 if (p->ctxt.in_def) {
14792 path = assign_error(p, "dynamic constant assignment", path);
14798assign_error(struct parser_params *p, const char *mesg, VALUE a)
14800 a = dispatch2(assign_error, ERR_MESG(), a);
14806var_field(struct parser_params *p, VALUE a)
14808 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
14814new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14816 NODE *result = head;
14818 NODE *tmp = rescue_else ? rescue_else : rescue;
14819 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14821 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14822 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14824 else if (rescue_else) {
14825 result = block_append(p, result, rescue_else);
14828 result = NEW_ENSURE(result, ensure, loc);
14830 fixpos(result, head);
14836warn_unused_var(struct parser_params *p, struct local_vars *local)
14840 if (!local->used) return;
14841 cnt = local->used->pos;
14842 if (cnt != local->vars->pos) {
14843 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14846 ID *v = local->vars->tbl;
14847 ID *u = local->used->tbl;
14848 for (int i = 0; i < cnt; ++i) {
14849 if (!v[i] || (u[i] & LVAR_USED)) continue;
14850 if (is_private_local_id(p, v[i])) continue;
14851 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14857local_push(struct parser_params *p, int toplevel_scope)
14859 struct local_vars *local;
14860 int inherits_dvars = toplevel_scope && compile_for_eval;
14861 int warn_unused_vars = RTEST(ruby_verbose);
14863 local = ALLOC(struct local_vars);
14864 local->prev = p->lvtbl;
14865 local->args = vtable_alloc(0);
14866 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14868 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14869 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14870 local->numparam.outer = 0;
14871 local->numparam.inner = 0;
14872 local->numparam.current = 0;
14874 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14876# if WARN_PAST_SCOPE
14885vtable_chain_free(struct parser_params *p, struct vtable *table)
14887 while (!DVARS_TERMINAL_P(table)) {
14888 struct vtable *cur_table = table;
14889 table = cur_table->prev;
14890 vtable_free(cur_table);
14895local_free(struct parser_params *p, struct local_vars *local)
14897 vtable_chain_free(p, local->used);
14899# if WARN_PAST_SCOPE
14900 vtable_chain_free(p, local->past);
14903 vtable_chain_free(p, local->args);
14904 vtable_chain_free(p, local->vars);
14906 ruby_sized_xfree(local, sizeof(struct local_vars));
14910local_pop(struct parser_params *p)
14912 struct local_vars *local = p->lvtbl->prev;
14913 if (p->lvtbl->used) {
14914 warn_unused_var(p, p->lvtbl);
14917 local_free(p, p->lvtbl);
14925static rb_ast_id_table_t *
14926local_tbl(struct parser_params *p)
14928 int cnt_args = vtable_size(p->lvtbl->args);
14929 int cnt_vars = vtable_size(p->lvtbl->vars);
14930 int cnt = cnt_args + cnt_vars;
14932 rb_ast_id_table_t *tbl;
14934 if (cnt <= 0) return 0;
14935 tbl = rb_ast_new_local_table(p->ast, cnt);
14936 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14937 /* remove IDs duplicated to warn shadowing */
14938 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14939 ID id = p->lvtbl->vars->tbl[i];
14940 if (!vtable_included(p->lvtbl->args, id)) {
14941 tbl->ids[j++] = id;
14945 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14954numparam_name(struct parser_params *p, ID id)
14956 if (!NUMPARAM_ID_P(id)) return;
14957 compile_error(p, "_%d is reserved for numbered parameter",
14958 NUMPARAM_ID_TO_IDX(id));
14962arg_var(struct parser_params *p, ID id)
14964 numparam_name(p, id);
14965 vtable_add(p->lvtbl->args, id);
14969local_var(struct parser_params *p, ID id)
14971 numparam_name(p, id);
14972 vtable_add(p->lvtbl->vars, id);
14973 if (p->lvtbl->used) {
14974 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14979local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14981 struct vtable *vars, *args, *used;
14983 vars = p->lvtbl->vars;
14984 args = p->lvtbl->args;
14985 used = p->lvtbl->used;
14987 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14990 if (used) used = used->prev;
14993 if (vars && vars->prev == DVARS_INHERIT) {
14994 return rb_local_defined(id, p->parent_iseq);
14996 else if (vtable_included(args, id)) {
15000 int i = vtable_included(vars, id);
15001 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15007local_id(struct parser_params *p, ID id)
15009 return local_id_ref(p, id, NULL);
15013check_forwarding_args(struct parser_params *p)
15015 if (local_id(p, idFWD_ALL)) return TRUE;
15016 compile_error(p, "unexpected ...");
15021add_forwarding_args(struct parser_params *p)
15023 arg_var(p, idFWD_REST);
15024#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15025 arg_var(p, idFWD_KWREST);
15027 arg_var(p, idFWD_BLOCK);
15028 arg_var(p, idFWD_ALL);
15032forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15034 bool conflict = false;
15036 struct vtable *vars, *args;
15038 vars = p->lvtbl->vars;
15039 args = p->lvtbl->args;
15041 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15042 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15047 bool found = false;
15048 if (vars && vars->prev == DVARS_INHERIT && !found) {
15049 found = (rb_local_defined(arg, p->parent_iseq) &&
15050 !(all && rb_local_defined(all, p->parent_iseq)));
15053 found = (vtable_included(args, arg) &&
15054 !(all && vtable_included(args, all)));
15058 compile_error(p, "no anonymous %s parameter", var);
15060 else if (conflict) {
15061 compile_error(p, "anonymous %s parameter is also used within block", var);
15067new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15069 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15070#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15071 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15073 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
15074 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
15075#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15076 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
15078 return arg_blk_pass(args, block);
15083numparam_push(struct parser_params *p)
15086 struct local_vars *local = p->lvtbl;
15087 NODE *inner = local->numparam.inner;
15088 if (!local->numparam.outer) {
15089 local->numparam.outer = local->numparam.current;
15091 local->numparam.inner = 0;
15092 local->numparam.current = 0;
15100numparam_pop(struct parser_params *p, NODE *prev_inner)
15103 struct local_vars *local = p->lvtbl;
15105 /* prefer first one */
15106 local->numparam.inner = prev_inner;
15108 else if (local->numparam.current) {
15109 /* current and inner are exclusive */
15110 local->numparam.inner = local->numparam.current;
15112 if (p->max_numparam > NO_PARAM) {
15113 /* current and outer are exclusive */
15114 local->numparam.current = local->numparam.outer;
15115 local->numparam.outer = 0;
15118 /* no numbered parameter */
15119 local->numparam.current = 0;
15124static const struct vtable *
15125dyna_push(struct parser_params *p)
15127 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15128 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15129 if (p->lvtbl->used) {
15130 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15132 return p->lvtbl->args;
15136dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15138 struct vtable *tmp = *vtblp;
15139 *vtblp = tmp->prev;
15140# if WARN_PAST_SCOPE
15141 if (p->past_scope_enabled) {
15142 tmp->prev = p->lvtbl->past;
15143 p->lvtbl->past = tmp;
15151dyna_pop_1(struct parser_params *p)
15153 struct vtable *tmp;
15155 if ((tmp = p->lvtbl->used) != 0) {
15156 warn_unused_var(p, p->lvtbl);
15157 p->lvtbl->used = p->lvtbl->used->prev;
15160 dyna_pop_vtable(p, &p->lvtbl->args);
15161 dyna_pop_vtable(p, &p->lvtbl->vars);
15165dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15167 while (p->lvtbl->args != lvargs) {
15169 if (!p->lvtbl->args) {
15170 struct local_vars *local = p->lvtbl->prev;
15171 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15179dyna_in_block(struct parser_params *p)
15181 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15185dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15187 struct vtable *vars, *args, *used;
15190 args = p->lvtbl->args;
15191 vars = p->lvtbl->vars;
15192 used = p->lvtbl->used;
15194 while (!DVARS_TERMINAL_P(vars)) {
15195 if (vtable_included(args, id)) {
15198 if ((i = vtable_included(vars, id)) != 0) {
15199 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15204 if (!vidrefp) used = 0;
15205 if (used) used = used->prev;
15208 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15209 return rb_dvar_defined(id, p->parent_iseq);
15216dvar_defined(struct parser_params *p, ID id)
15218 return dvar_defined_ref(p, id, NULL);
15222dvar_curr(struct parser_params *p, ID id)
15224 return (vtable_included(p->lvtbl->args, id) ||
15225 vtable_included(p->lvtbl->vars, id));
15229reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
15232 "regexp encoding option '%c' differs from source encoding '%s'",
15233 c, rb_enc_name(rb_enc_get(str)));
15238rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15240 int c = RE_OPTION_ENCODING_IDX(options);
15244 rb_char_to_option_kcode(c, &opt, &idx);
15245 if (idx != ENCODING_GET(str) &&
15246 !is_ascii_string(str)) {
15249 ENCODING_SET(str, idx);
15251 else if (RE_OPTION_ENCODING_NONE(options)) {
15252 if (!ENCODING_IS_ASCII8BIT(str) &&
15253 !is_ascii_string(str)) {
15257 rb_enc_associate(str, rb_ascii8bit_encoding());
15259 else if (rb_is_usascii_enc(p->enc)) {
15260 if (!is_ascii_string(str)) {
15261 /* raise in re.c */
15262 rb_enc_associate(str, rb_usascii_encoding());
15265 rb_enc_associate(str, rb_ascii8bit_encoding());
15275reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15277 int c = rb_reg_fragment_setenc(p, str, options);
15278 if (c) reg_fragment_enc_error(p, str, c);
15282reg_fragment_check(struct parser_params* p, VALUE str, int options)
15285 reg_fragment_setenc(p, str, options);
15286 err = rb_reg_check_preprocess(str);
15288 err = rb_obj_as_string(err);
15289 compile_error(p, "%"PRIsVALUE, err);
15295#ifndef UNIVERSAL_PARSER
15297 struct parser_params* parser;
15300 const YYLTYPE *loc;
15301} reg_named_capture_assign_t;
15304reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15305 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15307 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15308 struct parser_params* p = arg->parser;
15309 rb_encoding *enc = arg->enc;
15310 long len = name_end - name;
15311 const char *s = (const char *)name;
15313 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc);
15317reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
15319 reg_named_capture_assign_t arg;
15322 arg.enc = rb_enc_get(regexp);
15323 arg.succ_block = 0;
15325 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15327 if (!arg.succ_block) return 0;
15328 return RNODE_BLOCK(arg.succ_block)->nd_next;
15333rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15334 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc)
15339 if (!len) return ST_CONTINUE;
15340 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15341 return ST_CONTINUE;
15343 var = intern_cstr(s, len, enc);
15344 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15345 if (!lvar_defined(p, var)) return ST_CONTINUE;
15347 node = node_assign(p, assignable(p, var, 0, loc), NEW_LIT(ID2SYM(var), loc), NO_LEX_CTXT, loc);
15348 succ = *succ_block;
15349 if (!succ) succ = NEW_ERROR(loc);
15350 succ = block_append(p, succ, node);
15351 *succ_block = succ;
15352 return ST_CONTINUE;
15356parser_reg_compile(struct parser_params* p, VALUE str, int options)
15358 reg_fragment_setenc(p, str, options);
15359 return rb_parser_reg_compile(p, str, options);
15363rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15365 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15369reg_compile(struct parser_params* p, VALUE str, int options)
15374 err = rb_errinfo();
15375 re = parser_reg_compile(p, str, options);
15377 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15378 rb_set_errinfo(err);
15379 compile_error(p, "%"PRIsVALUE, m);
15386parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
15388 VALUE err = rb_errinfo();
15390 str = ripper_is_node_yylval(p, str) ? RNODE_RIPPER(str)->nd_cval : str;
15391 int c = rb_reg_fragment_setenc(p, str, options);
15392 if (c) reg_fragment_enc_error(p, str, c);
15393 re = rb_parser_reg_compile(p, str, options);
15395 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
15396 rb_set_errinfo(err);
15404rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15406 p->do_print = print;
15408 p->do_chomp = chomp;
15409 p->do_split = split;
15413parser_append_options(struct parser_params *p, NODE *node)
15415 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15416 const YYLTYPE *const LOC = &default_location;
15419 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15420 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15422 node = block_append(p, node, print);
15426 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15429 ID ifs = rb_intern("$;");
15430 ID fields = rb_intern("$F");
15431 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15432 NODE *split = NEW_GASGN(fields,
15433 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15434 rb_intern("split"), args, LOC),
15436 node = block_append(p, split, node);
15439 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
15440 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15441 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15444 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
15453 /* just to suppress unused-function warnings */
15459internal_id(struct parser_params *p)
15461 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15463#endif /* !RIPPER */
15466parser_initialize(struct parser_params *p)
15468 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15469 p->command_start = TRUE;
15470 p->ruby_sourcefile_string = Qnil;
15471 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15473 p->delayed.token = Qnil;
15474 p->frozen_string_literal = -1; /* not specified */
15477 p->parsing_thread = Qnil;
15479 p->error_buffer = Qfalse;
15480 p->end_expect_token_locations = Qnil;
15484 p->debug_buffer = Qnil;
15485 p->debug_output = rb_ractor_stdout();
15486 p->enc = rb_utf8_encoding();
15491#define rb_ruby_parser_mark ripper_parser_mark
15492#define rb_ruby_parser_free ripper_parser_free
15493#define rb_ruby_parser_memsize ripper_parser_memsize
15497rb_ruby_parser_mark(void *ptr)
15499 struct parser_params *p = (struct parser_params*)ptr;
15501 rb_gc_mark(p->lex.input);
15502 rb_gc_mark(p->lex.lastline);
15503 rb_gc_mark(p->lex.nextline);
15504 rb_gc_mark(p->ruby_sourcefile_string);
15505 rb_gc_mark((VALUE)p->ast);
15506 rb_gc_mark(p->case_labels);
15507 rb_gc_mark(p->delayed.token);
15509 rb_gc_mark(p->debug_lines);
15510 rb_gc_mark(p->error_buffer);
15511 rb_gc_mark(p->end_expect_token_locations);
15512 rb_gc_mark(p->tokens);
15514 rb_gc_mark(p->value);
15515 rb_gc_mark(p->result);
15516 rb_gc_mark(p->parsing_thread);
15518 rb_gc_mark(p->debug_buffer);
15519 rb_gc_mark(p->debug_output);
15521 rb_gc_mark((VALUE)p->heap);
15526rb_ruby_parser_free(void *ptr)
15528 struct parser_params *p = (struct parser_params*)ptr;
15529 struct local_vars *local, *prev;
15530#ifdef UNIVERSAL_PARSER
15531 rb_parser_config_t *config = p->config;
15535 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15538 for (local = p->lvtbl; local; local = prev) {
15539 prev = local->prev;
15540 local_free(p, local);
15544 token_info *ptinfo;
15545 while ((ptinfo = p->token_info) != 0) {
15546 p->token_info = ptinfo->next;
15551 xfree(p->lex.strterm);
15552 p->lex.strterm = 0;
15556#ifdef UNIVERSAL_PARSER
15558 if (config->counter <= 0) {
15559 rb_ruby_parser_config_free(config);
15565rb_ruby_parser_memsize(const void *ptr)
15567 struct parser_params *p = (struct parser_params*)ptr;
15568 struct local_vars *local;
15569 size_t size = sizeof(*p);
15572 for (local = p->lvtbl; local; local = local->prev) {
15573 size += sizeof(*local);
15574 if (local->vars) size += local->vars->capa * sizeof(ID);
15579#ifdef UNIVERSAL_PARSER
15580rb_parser_config_t *
15581rb_ruby_parser_config_new(void *(*malloc)(size_t size))
15583 return (rb_parser_config_t *)malloc(sizeof(rb_parser_config_t));
15587rb_ruby_parser_config_free(rb_parser_config_t *config)
15589 config->free(config);
15593#ifndef UNIVERSAL_PARSER
15595static const rb_data_type_t parser_data_type = {
15598 rb_ruby_parser_mark,
15599 rb_ruby_parser_free,
15600 rb_ruby_parser_memsize,
15602 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
15608#undef rb_reserved_word
15610const struct kwtable *
15611rb_reserved_word(const char *str, unsigned int len)
15613 return reserved_word(str, len);
15616#ifdef UNIVERSAL_PARSER
15618rb_ruby_parser_allocate(rb_parser_config_t *config)
15620 /* parser_initialize expects fields to be set to 0 */
15621 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15622 p->config = config;
15623 p->config->counter++;
15628rb_ruby_parser_new(rb_parser_config_t *config)
15630 /* parser_initialize expects fields to be set to 0 */
15631 rb_parser_t *p = rb_ruby_parser_allocate(config);
15632 parser_initialize(p);
15638rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15640 p->error_buffer = main ? Qfalse : Qnil;
15641 p->parent_iseq = base;
15646rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines)
15648 if (!RTEST(lines)) {
15651 else if (lines == Qtrue) {
15652 lines = rb_ary_new();
15655 Check_Type(lines, T_ARRAY);
15656 rb_ary_modify(lines);
15658 p->debug_lines = lines;
15662rb_ruby_parser_error_tolerant(rb_parser_t *p)
15664 p->error_tolerant = 1;
15666 p->end_expect_token_locations = rb_ary_new();
15670rb_ruby_parser_keep_tokens(rb_parser_t *p)
15672 p->keep_tokens = 1;
15674 p->tokens = rb_ary_new();
15677#ifndef UNIVERSAL_PARSER
15679rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
15681 struct parser_params *p;
15683 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15684 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15685 return rb_ruby_parser_compile_file_path(p, fname, file, start);
15689rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
15691 struct parser_params *p;
15693 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15694 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15695 return rb_ruby_parser_compile_generic(p, lex_gets, fname, input, start);
15699rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
15701 struct parser_params *p;
15703 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15704 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15705 return rb_ruby_parser_compile_string(p, f, s, line);
15709rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
15711 struct parser_params *p;
15713 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15714 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15715 return rb_ruby_parser_compile_string_path(p, f, s, line);
15719rb_parser_encoding(VALUE vparser)
15721 struct parser_params *p;
15723 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15724 return rb_ruby_parser_encoding(p);
15728rb_parser_end_seen_p(VALUE vparser)
15730 struct parser_params *p;
15732 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15733 return RBOOL(rb_ruby_parser_end_seen_p(p));
15737rb_parser_error_tolerant(VALUE vparser)
15739 struct parser_params *p;
15741 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15742 rb_ruby_parser_error_tolerant(p);
15746rb_parser_set_script_lines(VALUE vparser, VALUE lines)
15748 struct parser_params *p;
15750 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15751 rb_ruby_parser_set_script_lines(p, lines);
15755rb_parser_keep_tokens(VALUE vparser)
15757 struct parser_params *p;
15759 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15760 rb_ruby_parser_keep_tokens(p);
15766 struct parser_params *p;
15767 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
15768 &parser_data_type, p);
15769 parser_initialize(p);
15774rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
15776 struct parser_params *p;
15778 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15779 rb_ruby_parser_set_context(p, base, main);
15784rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
15786 struct parser_params *p;
15788 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15789 rb_ruby_parser_set_options(p, print, loop, chomp, split);
15793rb_parser_set_yydebug(VALUE self, VALUE flag)
15795 struct parser_params *p;
15797 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
15798 rb_ruby_parser_set_yydebug(p, RTEST(flag));
15801#endif /* !UNIVERSAL_PARSER */
15804rb_ruby_parser_encoding(rb_parser_t *p)
15806 return rb_enc_from_encoding(p->enc);
15810rb_ruby_parser_end_seen_p(rb_parser_t *p)
15812 return p->ruby__end__seen;
15816rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15821#endif /* !RIPPER */
15825rb_ruby_parser_get_yydebug(rb_parser_t *p)
15831rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15837rb_ruby_parser_error_p(rb_parser_t *p)
15843rb_ruby_parser_debug_output(rb_parser_t *p)
15845 return p->debug_output;
15849rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15851 p->debug_output = output;
15855rb_ruby_parser_parsing_thread(rb_parser_t *p)
15857 return p->parsing_thread;
15861rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15863 p->parsing_thread = parsing_thread;
15867rb_ruby_parser_ripper_initialize(rb_parser_t *p, VALUE (*gets)(struct parser_params*,VALUE), VALUE input, VALUE sourcefile_string, const char *sourcefile, int sourceline)
15869 p->lex.gets = gets;
15870 p->lex.input = input;
15872 p->ruby_sourcefile_string = sourcefile_string;
15873 p->ruby_sourcefile = sourcefile;
15874 p->ruby_sourceline = sourceline;
15878rb_ruby_parser_result(rb_parser_t *p)
15884rb_ruby_parser_enc(rb_parser_t *p)
15890rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15892 return p->ruby_sourcefile_string;
15896rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15898 return p->ruby_sourceline;
15902rb_ruby_parser_lex_state(rb_parser_t *p)
15904 return p->lex.state;
15908rb_ruby_ripper_parse0(rb_parser_t *p)
15911 p->ast = rb_ast_new();
15912 ripper_yyparse((void*)p);
15913 rb_ast_dispose(p->ast);
15918rb_ruby_ripper_dedent_string(rb_parser_t *p, VALUE string, int width)
15920 return dedent_string(p, string, width);
15924rb_ruby_ripper_lex_get_str(rb_parser_t *p, VALUE s)
15926 return lex_get_str(p, s);
15930rb_ruby_ripper_initialized_p(rb_parser_t *p)
15932 return p->lex.input != 0;
15936rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15938 parser_initialize(p);
15942rb_ruby_ripper_column(rb_parser_t *p)
15944 return p->lex.ptok - p->lex.pbeg;
15948rb_ruby_ripper_token_len(rb_parser_t *p)
15950 return p->lex.pcur - p->lex.ptok;
15954rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15956 return p->lex.lastline;
15960rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15962 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15965struct parser_params*
15966rb_ruby_ripper_parser_allocate(void)
15968 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15974#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
15975/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
15976 * potential memory leak */
15977#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
15978#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
15979 (new)->cnt = (cnt), (ptr))
15982rb_parser_malloc(struct parser_params *p, size_t size)
15984 size_t cnt = HEAPCNT(1, size);
15985 rb_imemo_tmpbuf_t *n = NEWHEAP();
15986 void *ptr = xmalloc(size);
15988 return ADD2HEAP(n, cnt, ptr);
15992rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
15994 size_t cnt = HEAPCNT(nelem, size);
15995 rb_imemo_tmpbuf_t *n = NEWHEAP();
15996 void *ptr = xcalloc(nelem, size);
15998 return ADD2HEAP(n, cnt, ptr);
16002rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
16004 rb_imemo_tmpbuf_t *n;
16005 size_t cnt = HEAPCNT(1, size);
16007 if (ptr && (n = p->heap) != NULL) {
16009 if (n->ptr == ptr) {
16010 n->ptr = ptr = xrealloc(ptr, size);
16011 if (n->cnt) n->cnt = cnt;
16014 } while ((n = n->next) != NULL);
16017 ptr = xrealloc(ptr, size);
16018 return ADD2HEAP(n, cnt, ptr);
16022rb_parser_free(struct parser_params *p, void *ptr)
16024 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
16026 while ((n = *prev) != NULL) {
16027 if (n->ptr == ptr) {
16037rb_parser_printf(struct parser_params *p, const char *fmt, ...)
16040 VALUE mesg = p->debug_buffer;
16042 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
16044 rb_str_vcatf(mesg, fmt, ap);
16046 if (end_with_newline_p(p, mesg)) {
16047 rb_io_write(p->debug_output, mesg);
16048 p->debug_buffer = Qnil;
16053parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16056 int lineno, column;
16059 lineno = loc->end_pos.lineno;
16060 column = loc->end_pos.column;
16063 lineno = p->ruby_sourceline;
16064 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
16067 rb_io_flush(p->debug_output);
16071 rb_syntax_error_append(p->error_buffer,
16072 p->ruby_sourcefile_string,
16079count_char(const char *str, int c)
16082 while (str[n] == c) ++n;
16087 * strip enclosing double-quotes, same as the default yytnamerr except
16088 * for that single-quotes matching back-quotes do not stop stripping.
16090 * "\"`class' keyword\"" => "`class' keyword"
16092RUBY_FUNC_EXPORTED size_t
16093rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16095 if (*yystr == '"') {
16096 size_t yyn = 0, bquote = 0;
16097 const char *yyp = yystr;
16103 bquote = count_char(yyp+1, '`') + 1;
16104 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16112 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16113 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16119 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16120 if (yyres) memcpy(yyres + yyn, yyp, 3);
16125 goto do_not_strip_quotes;
16128 goto do_not_strip_quotes;
16131 if (*++yyp != '\\')
16132 goto do_not_strip_quotes;
16133 /* Fall through. */
16148 do_not_strip_quotes: ;
16151 if (!yyres) return strlen(yystr);
16153 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16161ripper_validate_object(VALUE self, VALUE x)
16163 if (x == Qfalse) return x;
16164 if (x == Qtrue) return x;
16165 if (NIL_P(x)) return x;
16167 rb_raise(rb_eArgError, "Qundef given");
16168 if (FIXNUM_P(x)) return x;
16169 if (SYMBOL_P(x)) return x;
16170 switch (BUILTIN_TYPE(x)) {
16180 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
16181 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
16183 x = ((NODE *)x)->nd_rval;
16186 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
16187 (void *)x, rb_obj_classname(x));
16189 if (!RBASIC_CLASS(x)) {
16190 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
16191 (void *)x, rb_builtin_type_name(TYPE(x)));
16197#define validate(x) ((x) = get_value(x))
16200ripper_dispatch0(struct parser_params *p, ID mid)
16202 return rb_funcall(p->value, mid, 0);
16206ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16209 return rb_funcall(p->value, mid, 1, a);
16213ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16217 return rb_funcall(p->value, mid, 2, a, b);
16221ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16226 return rb_funcall(p->value, mid, 3, a, b, c);
16230ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16236 return rb_funcall(p->value, mid, 4, a, b, c, d);
16240ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16247 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16251ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16260 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16264ripper_error(struct parser_params *p)
16270ripper_value(struct parser_params *p)
16272 (void)yystpcpy; /* may not used in newer bison */
16281 * c-file-style: "ruby"