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*);
1189static NODE *remove_begin_all(NODE*);
1190#define value_expr(node) value_expr_gen(p, (node))
1191static NODE *void_stmts(struct parser_params*,NODE*);
1192static void reduce_nodes(struct parser_params*,NODE**);
1193static void block_dup_check(struct parser_params*,NODE*,NODE*);
1195static NODE *block_append(struct parser_params*,NODE*,NODE*);
1196static NODE *list_append(struct parser_params*,NODE*,NODE*);
1197static NODE *list_concat(NODE*,NODE*);
1198static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1199static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1200static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1201static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1202static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
1203static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1204static NODE *str2dstr(struct parser_params*,NODE*);
1205static NODE *evstr2dstr(struct parser_params*,NODE*);
1206static NODE *splat_array(NODE*);
1207static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1209static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1210static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1211static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1212static 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);
1213static 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;}
1215static bool args_info_empty_p(struct rb_args_info *args);
1216static 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*);
1217static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1218static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1219static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1220static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1221static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1222static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1223static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1225static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1226static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int);
1228static VALUE negate_lit(struct parser_params*, VALUE);
1229static NODE *ret_args(struct parser_params*,NODE*);
1230static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1231static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
1232static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1234static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1235static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1237static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1238static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1240static void rb_backref_error(struct parser_params*,NODE*);
1241static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1243static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1244static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
1245static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
1246static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1247static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1249static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1251static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1252static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1254static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1255static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1257static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
1259static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
1261#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1263static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1265static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1267static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1269static rb_ast_id_table_t *local_tbl(struct parser_params*);
1271static VALUE reg_compile(struct parser_params*, VALUE, int);
1272static void reg_fragment_setenc(struct parser_params*, VALUE, int);
1273static int reg_fragment_check(struct parser_params*, VALUE, int);
1275static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
1276static NODE *heredoc_dedent(struct parser_params*,NODE*);
1278static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1280#define get_id(id) (id)
1281#define get_value(val) (val)
1282#define get_num(num) (num)
1285static inline int ripper_is_node_yylval(struct parser_params *p, VALUE n);
1288ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
1290 if (ripper_is_node_yylval(p, c)) c = RNODE_RIPPER(c)->nd_cval;
1291 add_mark_object(p, b);
1292 add_mark_object(p, c);
1293 return NEW_RIPPER(a, b, c, &NULL_LOC);
1297ripper_new_yylval2(struct parser_params *p, VALUE a, VALUE b, VALUE c)
1299 add_mark_object(p, a);
1300 add_mark_object(p, b);
1301 add_mark_object(p, c);
1302 return NEW_RIPPER_VALUES(a, b, c, &NULL_LOC);
1306ripper_is_node_yylval(struct parser_params *p, VALUE n)
1308 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
1311#define value_expr(node) ((void)(node))
1312#define remove_begin(node) (node)
1313#define void_stmts(p,x) (x)
1314#undef rb_dvar_defined
1315#define rb_dvar_defined(id, base) 0
1316#undef rb_local_defined
1317#define rb_local_defined(id, base) 0
1318#define get_id(id) ripper_get_id(id)
1319#define get_value(val) ripper_get_value(val)
1320#define get_num(num) (int)get_id(num)
1321static VALUE assignable(struct parser_params*,VALUE);
1322static int id_is_var(struct parser_params *p, ID id);
1324#define method_cond(p,node,loc) (node)
1325#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
1326#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
1327#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
1328#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
1330#define new_nil(loc) Qnil
1332static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
1334static VALUE const_decl(struct parser_params *p, VALUE path);
1336static VALUE var_field(struct parser_params *p, VALUE a);
1337static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1339static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
1341static VALUE backref_error(struct parser_params*, NODE *, VALUE);
1344RUBY_SYMBOL_EXPORT_BEGIN
1345VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1346int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
1347enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1348VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1349void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1350PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1351YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1352YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1353YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1354YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1355YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1356YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1357RUBY_SYMBOL_EXPORT_END
1359static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1360static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1362static ID formal_argument(struct parser_params*, ID);
1364static ID formal_argument(struct parser_params*, VALUE);
1366static ID shadowing_lvar(struct parser_params*,ID);
1367static void new_bv(struct parser_params*,ID);
1369static void local_push(struct parser_params*,int);
1370static void local_pop(struct parser_params*);
1371static void local_var(struct parser_params*, ID);
1372static void arg_var(struct parser_params*, ID);
1373static int local_id(struct parser_params *p, ID id);
1374static int local_id_ref(struct parser_params*, ID, ID **);
1376static ID internal_id(struct parser_params*);
1377static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1379static int check_forwarding_args(struct parser_params*);
1380static void add_forwarding_args(struct parser_params *p);
1381static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1383static const struct vtable *dyna_push(struct parser_params *);
1384static void dyna_pop(struct parser_params*, const struct vtable *);
1385static int dyna_in_block(struct parser_params*);
1386#define dyna_var(p, id) local_var(p, id)
1387static int dvar_defined(struct parser_params*, ID);
1388static int dvar_defined_ref(struct parser_params*, ID, ID**);
1389static int dvar_curr(struct parser_params*,ID);
1391static int lvar_defined(struct parser_params*, ID);
1393static NODE *numparam_push(struct parser_params *p);
1394static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1397# define METHOD_NOT idNOT
1399# define METHOD_NOT '!'
1402#define idFWD_REST '*'
1403#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1404#define idFWD_BLOCK '&'
1405#define idFWD_ALL idDot3
1407#define arg_FWD_BLOCK Qnone
1409#define arg_FWD_BLOCK idFWD_BLOCK
1411#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS
1413#define RE_OPTION_ONCE (1<<16)
1414#define RE_OPTION_ENCODING_SHIFT 8
1415#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1416#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1417#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1418#define RE_OPTION_MASK 0xff
1419#define RE_OPTION_ARG_ENCODING_NONE 32
1421#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1422size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1424#define TOKEN2ID(tok) ( \
1425 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1426 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1427 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1428 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1429 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1430 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1431 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1433/****** Ripper *******/
1437#include "eventids1.h"
1438#include "eventids2.h"
1440extern const struct ripper_parser_ids ripper_parser_ids;
1442static VALUE ripper_dispatch0(struct parser_params*,ID);
1443static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1444static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1445static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1446static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1447static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1448static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1449void ripper_error(struct parser_params *p);
1451#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
1452#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
1453#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
1454#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
1455#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
1456#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
1457#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))
1459#define yyparse ripper_yyparse
1461#define ID2VAL(id) STATIC_ID2SYM(id)
1462#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1463#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
1465#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
1466 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
1469new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
1471 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(tail);
1472 VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2, block = t->nd_val3;
1473 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, block);
1477new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
1479 return ripper_new_yylval2(p, kw_args, kw_rest_arg, block);
1483args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
1489new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
1491 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(aryptn);
1492 VALUE pre_args = t->nd_val1, rest_arg = t->nd_val2, post_args = t->nd_val3;
1494 if (!NIL_P(pre_arg)) {
1495 if (!NIL_P(pre_args)) {
1496 rb_ary_unshift(pre_args, pre_arg);
1499 pre_args = rb_ary_new_from_args(1, pre_arg);
1502 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
1506new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
1508 return ripper_new_yylval2(p, pre_args, rest_arg, post_args);
1512new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
1514 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(fndptn);
1515 VALUE pre_rest_arg = t->nd_val1, args = t->nd_val2, post_rest_arg = t->nd_val3;
1517 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
1521new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
1523 return ripper_new_yylval2(p, pre_rest_arg, args, post_rest_arg);
1526#define new_hash(p,h,l) rb_ary_new_from_args(0)
1529new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
1535new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
1537 struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(hshptn);
1538 VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2;
1539 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
1543new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
1546 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
1551 return ripper_new_yylval2(p, kw_args, kw_rest_arg, Qnil);
1554#define new_defined(p,expr,loc) dispatch1(defined, (expr))
1556static VALUE heredoc_dedent(struct parser_params*,VALUE);
1559#define ID2VAL(id) (id)
1560#define TOKEN2VAL(t) ID2VAL(t)
1561#define KWD2EID(t, v) keyword_##t
1564new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1566 body = remove_begin(body);
1567 reduce_nodes(p, &body);
1568 NODE *n = NEW_SCOPE(args, body, loc);
1569 nd_set_line(n, loc->end_pos.lineno);
1570 set_line_body(body, loc->beg_pos.lineno);
1575rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1576 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1578 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1579 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1580 loc.beg_pos = arg_loc->beg_pos;
1581 return NEW_RESCUE(arg, rescue, 0, &loc);
1586static NODE *add_block_exit(struct parser_params *p, NODE *node);
1587static rb_node_exits_t *init_block_exit(struct parser_params *p);
1588static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1589static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1590static void clear_block_exit(struct parser_params *p, bool error);
1593next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1595 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1599restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1601 /* See: def_name action */
1602 struct lex_context ctxt = temp->save.ctxt;
1603 p->cur_arg = temp->save.cur_arg;
1604 p->ctxt.in_def = ctxt.in_def;
1605 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1606 p->ctxt.in_rescue = ctxt.in_rescue;
1607 p->max_numparam = temp->save.max_numparam;
1608 numparam_pop(p, temp->save.numparam_save);
1609 clear_block_exit(p, true);
1613endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1615 if (is_attrset_id(mid)) {
1616 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1618 token_info_drop(p, "def", loc->beg_pos);
1621#define debug_token_line(p, name, line) do { \
1623 const char *const pcur = p->lex.pcur; \
1624 const char *const ptok = p->lex.ptok; \
1625 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1626 line, p->ruby_sourceline, \
1627 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1631#define begin_definition(k, loc_beg, loc_end) \
1633 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1634 p->ctxt.in_def = 0; \
1636 else if (p->ctxt.in_def) { \
1637 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1638 yyerror1(&loc, k " definition in method body"); \
1646# define ifndef_ripper(x) (x)
1649# define Qnull Qundef
1650# define ifndef_ripper(x)
1653# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1654# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1655# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1656# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1657# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1658# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1659# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1660# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1661# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1662# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1663# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1664# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1665# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1666# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1667# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1668# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1669# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1670# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1671# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1672# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1674extern const ID id_warn, id_warning, id_gets, id_assoc;
1675# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1676# define WARN_S_L(s,l) STR_NEW(s,l)
1677# define WARN_S(s) STR_NEW2(s)
1678# define WARN_I(i) INT2NUM(i)
1679# define WARN_ID(i) rb_id2str(i)
1680# define WARN_IVAL(i) i
1681# define PRIsWARN "s"
1682# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1683# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1684# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1685# ifdef HAVE_VA_ARGS_MACRO
1686# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1688# define WARN_CALL rb_funcall
1690# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1691# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1692# ifdef HAVE_VA_ARGS_MACRO
1693# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1695# define WARNING_CALL rb_funcall
1697# define compile_error ripper_compile_error
1699# define WARN_S_L(s,l) s
1702# define WARN_ID(i) rb_id2name(i)
1703# define WARN_IVAL(i) NUM2INT(i)
1704# define PRIsWARN PRIsVALUE
1705# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1706# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1707# define WARN_CALL rb_compile_warn
1708# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1709# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1710# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1711# define WARNING_CALL rb_compile_warning
1712PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1713# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1719 NODE *nd_chain; /* Assume NODE_BREAK, NODE_NEXT, NODE_REDO have nd_chain here */
1723#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1726add_block_exit(struct parser_params *p, NODE *node)
1729 compile_error(p, "unexpected null node");
1732 switch (nd_type(node)) {
1733 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1735 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1738 if (!p->ctxt.in_defined) {
1739 rb_node_exits_t *exits = p->exits;
1741 RNODE_EXITS(exits->nd_end)->nd_chain = node;
1742 exits->nd_end = node;
1748static rb_node_exits_t *
1749init_block_exit(struct parser_params *p)
1751 rb_node_exits_t *old = p->exits;
1752 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1753 exits->nd_chain = 0;
1754 exits->nd_end = RNODE(exits);
1759static rb_node_exits_t *
1760allow_block_exit(struct parser_params *p)
1762 rb_node_exits_t *exits = p->exits;
1768restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1774clear_block_exit(struct parser_params *p, bool error)
1776 rb_node_exits_t *exits = p->exits;
1778 if (error && !compile_for_eval) {
1779 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1780 switch (nd_type(e)) {
1782 yyerror1(&e->nd_loc, "Invalid break");
1785 yyerror1(&e->nd_loc, "Invalid next");
1788 yyerror1(&e->nd_loc, "Invalid redo");
1791 yyerror1(&e->nd_loc, "unexpected node");
1792 goto end_checks; /* no nd_chain */
1797 exits->nd_end = RNODE(exits);
1798 exits->nd_chain = 0;
1801#define WARN_EOL(tok) \
1802 (looking_at_eol_p(p) ? \
1803 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1805static int looking_at_eol_p(struct parser_params *p);
1809get_nd_value(struct parser_params *p, NODE *node)
1811 switch (nd_type(node)) {
1813 return RNODE_GASGN(node)->nd_value;
1815 return RNODE_IASGN(node)->nd_value;
1817 return RNODE_LASGN(node)->nd_value;
1819 return RNODE_DASGN(node)->nd_value;
1821 return RNODE_MASGN(node)->nd_value;
1823 return RNODE_CVASGN(node)->nd_value;
1825 return RNODE_CDECL(node)->nd_value;
1827 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1833set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1835 switch (nd_type(node)) {
1837 RNODE_CDECL(node)->nd_value = rhs;
1840 RNODE_GASGN(node)->nd_value = rhs;
1843 RNODE_IASGN(node)->nd_value = rhs;
1846 RNODE_LASGN(node)->nd_value = rhs;
1849 RNODE_DASGN(node)->nd_value = rhs;
1852 RNODE_MASGN(node)->nd_value = rhs;
1855 RNODE_CVASGN(node)->nd_value = rhs;
1858 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1864get_nd_vid(struct parser_params *p, NODE *node)
1866 switch (nd_type(node)) {
1868 return RNODE_CDECL(node)->nd_vid;
1870 return RNODE_GASGN(node)->nd_vid;
1872 return RNODE_IASGN(node)->nd_vid;
1874 return RNODE_LASGN(node)->nd_vid;
1876 return RNODE_DASGN(node)->nd_vid;
1878 return RNODE_CVASGN(node)->nd_vid;
1880 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1886get_nd_args(struct parser_params *p, NODE *node)
1888 switch (nd_type(node)) {
1890 return RNODE_CALL(node)->nd_args;
1892 return RNODE_OPCALL(node)->nd_args;
1894 return RNODE_FCALL(node)->nd_args;
1896 return RNODE_QCALL(node)->nd_args;
1906 compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node)));
1915%define parse.error verbose
1918 if ((NODE *)$$ == (NODE *)-1) {
1919 rb_parser_printf(p, "NODE_SPECIAL");
1922 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
1926} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg> <node_kw_arg> <node_block_pass>
1929 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1931 rb_parser_printf(p, "%"PRIsVALUE, RNODE_RIPPER($$)->nd_rval);
1933} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1936 rb_parser_printf(p, "%+"PRIsVALUE, RNODE_LIT($$)->nd_lit);
1938 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1940} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1943 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
1945 rb_parser_printf(p, "%"PRIsVALUE, $$);
1950 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
1952 rb_parser_printf(p, "%"PRIsVALUE, $$);
1956%lex-param {struct parser_params *p}
1957%parse-param {struct parser_params *p}
1960 RUBY_SET_YYLLOC_OF_NONE(@$);
1966 rb_node_fcall_t *node_fcall;
1967 rb_node_args_t *node_args;
1968 rb_node_args_aux_t *node_args_aux;
1969 rb_node_opt_arg_t *node_opt_arg;
1970 rb_node_kw_arg_t *node_kw_arg;
1971 rb_node_block_pass_t *node_block_pass;
1972 rb_node_masgn_t *node_masgn;
1973 rb_node_def_temp_t *node_def_temp;
1974 rb_node_exits_t *node_exits;
1978 const struct vtable *vars;
1979 struct rb_strterm_struct *strterm;
1980 struct lex_context ctxt;
1984 keyword_class "`class'"
1985 keyword_module "`module'"
1987 keyword_undef "`undef'"
1988 keyword_begin "`begin'"
1989 keyword_rescue "`rescue'"
1990 keyword_ensure "`ensure'"
1993 keyword_unless "`unless'"
1994 keyword_then "`then'"
1995 keyword_elsif "`elsif'"
1996 keyword_else "`else'"
1997 keyword_case "`case'"
1998 keyword_when "`when'"
1999 keyword_while "`while'"
2000 keyword_until "`until'"
2002 keyword_break "`break'"
2003 keyword_next "`next'"
2004 keyword_redo "`redo'"
2005 keyword_retry "`retry'"
2008 keyword_do_cond "`do' for condition"
2009 keyword_do_block "`do' for block"
2010 keyword_do_LAMBDA "`do' for lambda"
2011 keyword_return "`return'"
2012 keyword_yield "`yield'"
2013 keyword_super "`super'"
2014 keyword_self "`self'"
2016 keyword_true "`true'"
2017 keyword_false "`false'"
2021 modifier_if "`if' modifier"
2022 modifier_unless "`unless' modifier"
2023 modifier_while "`while' modifier"
2024 modifier_until "`until' modifier"
2025 modifier_rescue "`rescue' modifier"
2026 keyword_alias "`alias'"
2027 keyword_defined "`defined?'"
2028 keyword_BEGIN "`BEGIN'"
2030 keyword__LINE__ "`__LINE__'"
2031 keyword__FILE__ "`__FILE__'"
2032 keyword__ENCODING__ "`__ENCODING__'"
2034%token <id> tIDENTIFIER "local variable or method"
2035%token <id> tFID "method"
2036%token <id> tGVAR "global variable"
2037%token <id> tIVAR "instance variable"
2038%token <id> tCONSTANT "constant"
2039%token <id> tCVAR "class variable"
2040%token <id> tLABEL "label"
2041%token <node> tINTEGER "integer literal"
2042%token <node> tFLOAT "float literal"
2043%token <node> tRATIONAL "rational literal"
2044%token <node> tIMAGINARY "imaginary literal"
2045%token <node> tCHAR "char literal"
2046%token <node> tNTH_REF "numbered reference"
2047%token <node> tBACK_REF "back reference"
2048%token <node> tSTRING_CONTENT "literal content"
2049%token <num> tREGEXP_END
2050%token <num> tDUMNY_END "dummy end"
2052%type <node> singleton strings string string1 xstring regexp
2053%type <node> string_contents xstring_contents regexp_contents string_content
2054%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2055%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2056/*ripper*/ %type <node_def_temp> defn_head defs_head k_def
2057/*ripper*/ %type <node_exits> block_open k_while k_until k_for allow_exits
2058%type <node> top_compstmt top_stmts top_stmt begin_block endless_arg endless_command
2059%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
2060%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2061%type <node_fcall> fcall
2062%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2063%type <node> args arg_splat call_args opt_call_args
2064%type <node> paren_args opt_paren_args
2065%type <node_args> args_tail opt_args_tail block_args_tail opt_block_args_tail
2066%type <node> command_args aref_args
2067%type <node_block_pass> opt_block_arg block_arg
2068%type <node> var_ref var_lhs
2069%type <node> command_rhs arg_rhs
2070%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2071%type <node_opt_arg> f_block_optarg f_block_opt
2072%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2073%type <node_args_aux> f_arg f_arg_item
2074%type <node_opt_arg> f_optarg
2075%type <node> f_marg f_marg_list f_rest_marg
2076%type <node_masgn> f_margs
2077%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2078%type <node_args> block_param opt_block_param block_param_def
2079%type <node_opt_arg> f_opt
2080%type <node_kw_arg> f_kwarg f_kw f_block_kwarg f_block_kw
2081%type <node> bv_decls opt_bv_decl bvar
2082%type <node> lambda lambda_body brace_body do_body
2083%type <node_args> f_larglist
2084%type <node> brace_block cmd_brace_block do_block lhs none fitem
2085%type <node> mlhs_head mlhs_item mlhs_node mlhs_post
2086%type <node_masgn> mlhs mlhs_basic mlhs_inner
2087%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2088%type <node> p_expr p_as p_alt p_expr_basic p_find
2089%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2090%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2091%type <node> p_kwargs p_kwarg p_kw
2092%type <id> keyword_variable user_variable sym operation operation2 operation3
2093%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2094%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2095%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2096%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2097%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2098%type <ctxt> p_in_kwarg
2099%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2100/* ripper */ %type <num> max_numparam
2101/* ripper */ %type <node> numparam
2102%token END_OF_INPUT 0 "end-of-input"
2105/* escaped chars, should be ignored otherwise */
2106%token <id> '\\' "backslash"
2107%token tSP "escaped space"
2108%token <id> '\t' "escaped horizontal tab"
2109%token <id> '\f' "escaped form feed"
2110%token <id> '\r' "escaped carriage return"
2111%token <id> '\13' "escaped vertical tab"
2112%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2113%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2114%token tPOW RUBY_TOKEN(POW) "**"
2115%token tCMP RUBY_TOKEN(CMP) "<=>"
2116%token tEQ RUBY_TOKEN(EQ) "=="
2117%token tEQQ RUBY_TOKEN(EQQ) "==="
2118%token tNEQ RUBY_TOKEN(NEQ) "!="
2119%token tGEQ RUBY_TOKEN(GEQ) ">="
2120%token tLEQ RUBY_TOKEN(LEQ) "<="
2121%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2122%token tOROP RUBY_TOKEN(OROP) "||"
2123%token tMATCH RUBY_TOKEN(MATCH) "=~"
2124%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2125%token tDOT2 RUBY_TOKEN(DOT2) ".."
2126%token tDOT3 RUBY_TOKEN(DOT3) "..."
2127%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2128%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2129%token tAREF RUBY_TOKEN(AREF) "[]"
2130%token tASET RUBY_TOKEN(ASET) "[]="
2131%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2132%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2133%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2134%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2135%token tCOLON3 ":: at EXPR_BEG"
2136%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2139%token tLPAREN_ARG "( arg"
2143%token tLBRACE_ARG "{ arg"
2145%token tDSTAR "**arg"
2148%token tSYMBEG "symbol literal"
2149%token tSTRING_BEG "string literal"
2150%token tXSTRING_BEG "backtick literal"
2151%token tREGEXP_BEG "regexp literal"
2152%token tWORDS_BEG "word list"
2153%token tQWORDS_BEG "verbatim word list"
2154%token tSYMBOLS_BEG "symbol list"
2155%token tQSYMBOLS_BEG "verbatim symbol list"
2156%token tSTRING_END "terminator"
2157%token tSTRING_DEND "'}'"
2158%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
2160%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2161%token tHEREDOC_BEG tHEREDOC_END k__END__
2168%nonassoc tLBRACE_ARG
2170%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2171%left keyword_or keyword_and
2173%nonassoc keyword_defined
2175%left modifier_rescue
2177%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2180%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2181%left '>' tGEQ '<' tLEQ
2187%right tUMINUS_NUM tUMINUS
2189%right '!' '~' tUPLUS
2195 SET_LEX_STATE(EXPR_BEG);
2196 local_push(p, ifndef_ripper(1)+0);
2197 /* jumps are possible in the top-level loop. */
2198 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
2203 if ($2 && !compile_for_eval) {
2205 /* last expression should not be void */
2206 if (nd_type_p(node, NODE_BLOCK)) {
2207 while (RNODE_BLOCK(node)->nd_next) {
2208 node = RNODE_BLOCK(node)->nd_next;
2210 node = RNODE_BLOCK(node)->nd_head;
2212 node = remove_begin(node);
2215 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
2217 /*% ripper[final]: program!($2) %*/
2222top_compstmt : top_stmts opt_terms
2224 $$ = void_stmts(p, $1);
2231 $$ = NEW_BEGIN(0, &@$);
2233 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
2238 $$ = newline_node($1);
2240 /*% ripper: stmts_add!(stmts_new!, $1) %*/
2242 | top_stmts terms top_stmt
2245 $$ = block_append(p, $1, newline_node($3));
2247 /*% ripper: stmts_add!($1, $3) %*/
2253 clear_block_exit(p, true);
2256 | keyword_BEGIN begin_block
2262block_open : '{' {$$ = init_block_exit(p);};
2264begin_block : block_open top_compstmt '}'
2266 restore_block_exit(p, $block_open);
2268 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
2269 NEW_BEGIN($2, &@$));
2270 $$ = NEW_BEGIN(0, &@$);
2272 /*% ripper: BEGIN!($2) %*/
2276bodystmt : compstmt[body]
2281 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
2282 next_rescue_context(&p->ctxt, &$ctxt, after_else);
2286 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
2291 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
2293 /*% ripper: bodystmt!($body, $opt_rescue, $elsebody, $opt_ensure) %*/
2299 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
2304 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
2306 /*% ripper: bodystmt!($body, $opt_rescue, Qnil, $opt_ensure) %*/
2310compstmt : stmts opt_terms
2312 $$ = void_stmts(p, $1);
2319 $$ = NEW_BEGIN(0, &@$);
2321 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
2326 $$ = newline_node($1);
2328 /*% ripper: stmts_add!(stmts_new!, $1) %*/
2330 | stmts terms stmt_or_begin
2333 $$ = block_append(p, $1, newline_node($3));
2335 /*% ripper: stmts_add!($1, $3) %*/
2345 yyerror1(&@1, "BEGIN is permitted only at toplevel");
2353allow_exits : {$$ = allow_block_exit(p);};
2355k_END : keyword_END lex_ctxt
2358 p->ctxt.in_rescue = before_rescue;
2361stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2364 $$ = NEW_ALIAS($2, $4, &@$);
2366 /*% ripper: alias!($2, $4) %*/
2368 | keyword_alias tGVAR tGVAR
2371 $$ = NEW_VALIAS($2, $3, &@$);
2373 /*% ripper: var_alias!($2, $3) %*/
2375 | keyword_alias tGVAR tBACK_REF
2380 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
2381 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
2383 /*% ripper: var_alias!($2, $3) %*/
2385 | keyword_alias tGVAR tNTH_REF
2387 static const char mesg[] = "can't make alias for the number variables";
2389 yyerror1(&@3, mesg);
2390 $$ = NEW_BEGIN(0, &@$);
2392 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
2394 | keyword_undef undef_list
2399 /*% ripper: undef!($2) %*/
2401 | stmt modifier_if expr_value
2404 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
2407 /*% ripper: if_mod!($3, $1) %*/
2409 | stmt modifier_unless expr_value
2412 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
2415 /*% ripper: unless_mod!($3, $1) %*/
2417 | stmt modifier_while expr_value
2419 clear_block_exit(p, false);
2421 if ($1 && nd_type_p($1, NODE_BEGIN)) {
2422 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$);
2425 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
2428 /*% ripper: while_mod!($3, $1) %*/
2430 | stmt modifier_until expr_value
2432 clear_block_exit(p, false);
2434 if ($1 && nd_type_p($1, NODE_BEGIN)) {
2435 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$);
2438 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
2441 /*% ripper: until_mod!($3, $1) %*/
2443 | stmt modifier_rescue after_rescue stmt
2445 p->ctxt.in_rescue = $3.in_rescue;
2448 YYLTYPE loc = code_loc_gen(&@2, &@4);
2449 resq = NEW_RESBODY(0, remove_begin($4), 0, &loc);
2450 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
2452 /*% ripper: rescue_mod!($1, $4) %*/
2454 | k_END allow_exits '{' compstmt '}'
2456 if (p->ctxt.in_def) {
2457 rb_warn0("END in method; use at_exit");
2459 restore_block_exit(p, $allow_exits);
2463 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
2464 $$ = NEW_POSTEXE(scope, &@$);
2467 /*% ripper: END!($compstmt) %*/
2470 | mlhs '=' lex_ctxt command_call
2474 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
2476 /*% ripper: massign!($1, $4) %*/
2478 | lhs '=' lex_ctxt mrhs
2481 $$ = node_assign(p, $1, $4, $3, &@$);
2483 /*% ripper: assign!($1, $4) %*/
2485 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
2486 after_rescue stmt[resbody]
2488 p->ctxt.in_rescue = $3.in_rescue;
2490 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
2491 $resbody = NEW_RESBODY(0, remove_begin($resbody), 0, &loc);
2492 loc.beg_pos = @mrhs_arg.beg_pos;
2493 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
2494 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
2496 /*% ripper: massign!($1, rescue_mod!($4, $7)) %*/
2498 | mlhs '=' lex_ctxt mrhs_arg
2501 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
2503 /*% ripper: massign!($1, $4) %*/
2510 $$ = NEW_ERROR(&@$);
2515command_asgn : lhs '=' lex_ctxt command_rhs
2518 $$ = node_assign(p, $1, $4, $3, &@$);
2520 /*% ripper: assign!($1, $4) %*/
2522 | var_lhs tOP_ASGN lex_ctxt command_rhs
2525 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2527 /*% ripper: opassign!($1, $2, $4) %*/
2529 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
2532 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2534 /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/
2537 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
2540 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2542 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2544 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
2547 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2549 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2551 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
2554 YYLTYPE loc = code_loc_gen(&@1, &@3);
2555 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2557 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2559 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
2562 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2564 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2566 | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
2568 endless_method_name(p, get_id($head->nd_mid), &@head);
2569 restore_defun(p, $head);
2571 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2572 ($$ = $head->nd_def)->nd_loc = @$;
2573 RNODE_DEFN($$)->nd_defn = $bodystmt;
2575 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
2576 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
2579 | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
2581 endless_method_name(p, get_id($head->nd_mid), &@head);
2582 restore_defun(p, $head);
2584 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
2585 ($$ = $head->nd_def)->nd_loc = @$;
2586 RNODE_DEFS($$)->nd_defn = $bodystmt;
2588 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
2589 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
2592 | backref tOP_ASGN lex_ctxt command_rhs
2595 rb_backref_error(p, $1);
2596 $$ = NEW_BEGIN(0, &@$);
2598 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
2602endless_command : command
2603 | endless_command modifier_rescue after_rescue arg
2605 p->ctxt.in_rescue = $3.in_rescue;
2607 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
2609 /*% ripper: rescue_mod!($1, $4) %*/
2611 | keyword_not opt_nl endless_command
2613 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2617command_rhs : command_call %prec tOP_ASGN
2622 | command_call modifier_rescue after_rescue stmt
2624 p->ctxt.in_rescue = $3.in_rescue;
2626 YYLTYPE loc = code_loc_gen(&@2, &@4);
2628 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($4), 0, &loc), 0, &@$);
2630 /*% ripper: rescue_mod!($1, $4) %*/
2636 | expr keyword_and expr
2638 $$ = logop(p, idAND, $1, $3, &@2, &@$);
2640 | expr keyword_or expr
2642 $$ = logop(p, idOR, $1, $3, &@2, &@$);
2644 | keyword_not opt_nl expr
2646 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2650 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2656 p_in_kwarg[ctxt] p_pvtbl p_pktbl
2657 p_top_expr_body[body]
2659 pop_pktbl(p, $p_pktbl);
2660 pop_pvtbl(p, $p_pvtbl);
2661 p->ctxt.in_kwarg = $ctxt.in_kwarg;
2663 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$);
2665 /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/
2671 p_in_kwarg[ctxt] p_pvtbl p_pktbl
2672 p_top_expr_body[body]
2674 pop_pktbl(p, $p_pktbl);
2675 pop_pvtbl(p, $p_pvtbl);
2676 p->ctxt.in_kwarg = $ctxt.in_kwarg;
2678 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$);
2680 /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/
2682 | arg %prec tLBRACE_ARG
2687 ID fname = get_id($1);
2688 numparam_name(p, fname);
2692 p->ctxt.in_rescue = before_rescue;
2697defn_head : k_def def_name
2699 $$ = def_head_save(p, $k_def);
2700 $$->nd_mid = $def_name;
2702 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
2704 add_mark_object(p, $def_name);
2709defs_head : k_def singleton dot_or_colon
2711 SET_LEX_STATE(EXPR_FNAME);
2712 p->ctxt.in_argdef = 1;
2716 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2717 $$ = def_head_save(p, $k_def);
2718 $$->nd_mid = $def_name;
2720 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
2722 add_mark_object(p, $def_name);
2723 $$->nd_recv = add_mark_object(p, $singleton);
2724 $$->dot_or_colon = add_mark_object(p, $dot_or_colon);
2737 $$ = NEW_ERROR(&@$);
2742expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
2748command_call : command
2752block_command : block_call
2753 | block_call call_op2 operation2 command_args
2756 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
2758 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
2762cmd_brace_block : tLBRACE_ARG brace_body '}'
2766 set_embraced_location($$, &@1, &@3);
2774 $$ = NEW_FCALL($1, 0, &@$);
2780command : fcall command_args %prec tLOWEST
2784 nd_set_last_loc($1, @2.end_pos);
2787 /*% ripper: command!($1, $2) %*/
2789 | fcall command_args cmd_brace_block
2792 block_dup_check(p, $2, $3);
2794 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
2795 fixpos($$, RNODE($1));
2796 nd_set_last_loc($1, @2.end_pos);
2798 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
2800 | primary_value call_op operation2 command_args %prec tLOWEST
2803 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
2805 /*% ripper: command_call!($1, $2, $3, $4) %*/
2807 | primary_value call_op operation2 command_args cmd_brace_block
2810 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
2812 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2814 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
2817 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
2819 /*% ripper: command_call!($1, $2, $3, $4) %*/
2821 | primary_value tCOLON2 operation2 command_args cmd_brace_block
2824 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
2826 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2828 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
2831 set_embraced_location($5, &@4, &@6);
2832 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, $5, &@3, &@$);
2834 /*% ripper: method_add_block!(command_call!($1, $2, $3, Qnull), $5) %*/
2836 | keyword_super command_args
2839 $$ = NEW_SUPER($2, &@$);
2842 /*% ripper: super!($2) %*/
2844 | k_yield command_args
2847 $$ = new_yield(p, $2, &@$);
2850 /*% ripper: yield!($2) %*/
2852 | k_return call_args
2855 $$ = NEW_RETURN(ret_args(p, $2), &@$);
2857 /*% ripper: return!($2) %*/
2859 | keyword_break call_args
2863 args = ret_args(p, $2);
2865 $<node>$ = add_block_exit(p, NEW_BREAK(args, &@$));
2866 /*% ripper: break!($2) %*/
2868 | keyword_next call_args
2872 args = ret_args(p, $2);
2874 $<node>$ = add_block_exit(p, NEW_NEXT(args, &@$));
2875 /*% ripper: next!($2) %*/
2880 | tLPAREN mlhs_inner rparen
2885 /*% ripper: mlhs_paren!($2) %*/
2889mlhs_inner : mlhs_basic
2890 | tLPAREN mlhs_inner rparen
2893 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
2895 /*% ripper: mlhs_paren!($2) %*/
2899mlhs_basic : mlhs_head
2902 $$ = NEW_MASGN($1, 0, &@$);
2906 | mlhs_head mlhs_item
2909 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
2911 /*% ripper: mlhs_add!($1, $2) %*/
2913 | mlhs_head tSTAR mlhs_node
2916 $$ = NEW_MASGN($1, $3, &@$);
2918 /*% ripper: mlhs_add_star!($1, $3) %*/
2920 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2923 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2925 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2930 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2932 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2934 | mlhs_head tSTAR ',' mlhs_post
2937 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2939 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2944 $$ = NEW_MASGN(0, $2, &@$);
2946 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2948 | tSTAR mlhs_node ',' mlhs_post
2951 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2953 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2958 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2960 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2962 | tSTAR ',' mlhs_post
2965 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2967 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2971mlhs_item : mlhs_node
2972 | tLPAREN mlhs_inner rparen
2977 /*% ripper: mlhs_paren!($2) %*/
2981mlhs_head : mlhs_item ','
2984 $$ = NEW_LIST($1, &@1);
2986 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2988 | mlhs_head mlhs_item ','
2991 $$ = list_append(p, $1, $2);
2993 /*% ripper: mlhs_add!($1, $2) %*/
2997mlhs_post : mlhs_item
3000 $$ = NEW_LIST($1, &@$);
3002 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3004 | mlhs_post ',' mlhs_item
3007 $$ = list_append(p, $1, $3);
3009 /*% ripper: mlhs_add!($1, $3) %*/
3013mlhs_node : user_variable
3016 $$ = assignable(p, $1, 0, &@$);
3018 /*% ripper: assignable(p, var_field(p, $1)) %*/
3023 $$ = assignable(p, $1, 0, &@$);
3025 /*% ripper: assignable(p, var_field(p, $1)) %*/
3027 | primary_value '[' opt_call_args rbracket
3030 $$ = aryset(p, $1, $3, &@$);
3032 /*% ripper: aref_field!($1, $3) %*/
3034 | primary_value call_op tIDENTIFIER
3036 anddot_multiple_assignment_check(p, &@2, $2);
3038 $$ = attrset(p, $1, $2, $3, &@$);
3040 /*% ripper: field!($1, $2, $3) %*/
3042 | primary_value tCOLON2 tIDENTIFIER
3045 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3047 /*% ripper: const_path_field!($1, $3) %*/
3049 | primary_value call_op tCONSTANT
3051 anddot_multiple_assignment_check(p, &@2, $2);
3053 $$ = attrset(p, $1, $2, $3, &@$);
3055 /*% ripper: field!($1, $2, $3) %*/
3057 | primary_value tCOLON2 tCONSTANT
3060 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3062 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
3067 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3069 /*% ripper: const_decl(p, top_const_field!($2)) %*/
3074 rb_backref_error(p, $1);
3075 $$ = NEW_BEGIN(0, &@$);
3077 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
3084 $$ = assignable(p, $1, 0, &@$);
3086 /*% ripper: assignable(p, var_field(p, $1)) %*/
3091 $$ = assignable(p, $1, 0, &@$);
3093 /*% ripper: assignable(p, var_field(p, $1)) %*/
3095 | primary_value '[' opt_call_args rbracket
3098 $$ = aryset(p, $1, $3, &@$);
3100 /*% ripper: aref_field!($1, $3) %*/
3102 | primary_value call_op tIDENTIFIER
3105 $$ = attrset(p, $1, $2, $3, &@$);
3107 /*% ripper: field!($1, $2, $3) %*/
3109 | primary_value tCOLON2 tIDENTIFIER
3112 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3114 /*% ripper: field!($1, $2, $3) %*/
3116 | primary_value call_op tCONSTANT
3119 $$ = attrset(p, $1, $2, $3, &@$);
3121 /*% ripper: field!($1, $2, $3) %*/
3123 | primary_value tCOLON2 tCONSTANT
3126 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3128 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
3133 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3135 /*% ripper: const_decl(p, top_const_field!($2)) %*/
3140 rb_backref_error(p, $1);
3141 $$ = NEW_BEGIN(0, &@$);
3143 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
3149 static const char mesg[] = "class/module name must be CONSTANT";
3151 yyerror1(&@1, mesg);
3153 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
3158cpath : tCOLON3 cname
3161 $$ = NEW_COLON3($2, &@$);
3163 /*% ripper: top_const_ref!($2) %*/
3168 $$ = NEW_COLON2(0, $1, &@$);
3170 /*% ripper: const_ref!($1) %*/
3172 | primary_value tCOLON2 cname
3175 $$ = NEW_COLON2($1, $3, &@$);
3177 /*% ripper: const_path_ref!($1, $3) %*/
3186 SET_LEX_STATE(EXPR_ENDFN);
3195 $$ = NEW_LIT(ID2SYM($1), &@$);
3197 /*% ripper: symbol_literal!($1) %*/
3205 $$ = NEW_UNDEF($1, &@$);
3207 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
3209 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3212 NODE *undef = NEW_UNDEF($4, &@4);
3213 $$ = block_append(p, $1, undef);
3215 /*% ripper: rb_ary_push($1, get_value($4)) %*/
3219op : '|' { ifndef_ripper($$ = '|'); }
3220 | '^' { ifndef_ripper($$ = '^'); }
3221 | '&' { ifndef_ripper($$ = '&'); }
3222 | tCMP { ifndef_ripper($$ = tCMP); }
3223 | tEQ { ifndef_ripper($$ = tEQ); }
3224 | tEQQ { ifndef_ripper($$ = tEQQ); }
3225 | tMATCH { ifndef_ripper($$ = tMATCH); }
3226 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
3227 | '>' { ifndef_ripper($$ = '>'); }
3228 | tGEQ { ifndef_ripper($$ = tGEQ); }
3229 | '<' { ifndef_ripper($$ = '<'); }
3230 | tLEQ { ifndef_ripper($$ = tLEQ); }
3231 | tNEQ { ifndef_ripper($$ = tNEQ); }
3232 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
3233 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
3234 | '+' { ifndef_ripper($$ = '+'); }
3235 | '-' { ifndef_ripper($$ = '-'); }
3236 | '*' { ifndef_ripper($$ = '*'); }
3237 | tSTAR { ifndef_ripper($$ = '*'); }
3238 | '/' { ifndef_ripper($$ = '/'); }
3239 | '%' { ifndef_ripper($$ = '%'); }
3240 | tPOW { ifndef_ripper($$ = tPOW); }
3241 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
3242 | '!' { ifndef_ripper($$ = '!'); }
3243 | '~' { ifndef_ripper($$ = '~'); }
3244 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
3245 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
3246 | tAREF { ifndef_ripper($$ = tAREF); }
3247 | tASET { ifndef_ripper($$ = tASET); }
3248 | '`' { ifndef_ripper($$ = '`'); }
3251reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3252 | keyword_BEGIN | keyword_END
3253 | keyword_alias | keyword_and | keyword_begin
3254 | keyword_break | keyword_case | keyword_class | keyword_def
3255 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3256 | keyword_end | keyword_ensure | keyword_false
3257 | keyword_for | keyword_in | keyword_module | keyword_next
3258 | keyword_nil | keyword_not | keyword_or | keyword_redo
3259 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3260 | keyword_super | keyword_then | keyword_true | keyword_undef
3261 | keyword_when | keyword_yield | keyword_if | keyword_unless
3262 | keyword_while | keyword_until
3265arg : lhs '=' lex_ctxt arg_rhs
3268 $$ = node_assign(p, $1, $4, $3, &@$);
3270 /*% ripper: assign!($1, $4) %*/
3272 | var_lhs tOP_ASGN lex_ctxt arg_rhs
3275 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3277 /*% ripper: opassign!($1, $2, $4) %*/
3279 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
3282 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
3284 /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/
3286 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3289 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
3291 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3293 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3296 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
3298 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3300 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3303 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
3305 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
3307 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3310 YYLTYPE loc = code_loc_gen(&@1, &@3);
3311 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3313 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
3315 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3318 YYLTYPE loc = code_loc_gen(&@1, &@2);
3319 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3321 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
3323 | backref tOP_ASGN lex_ctxt arg_rhs
3326 rb_backref_error(p, $1);
3327 $$ = NEW_BEGIN(0, &@$);
3329 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
3336 $$ = NEW_DOT2($1, $3, &@$);
3338 /*% ripper: dot2!($1, $3) %*/
3345 $$ = NEW_DOT3($1, $3, &@$);
3347 /*% ripper: dot3!($1, $3) %*/
3353 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
3355 /*% ripper: dot2!($1, Qnil) %*/
3361 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
3363 /*% ripper: dot3!($1, Qnil) %*/
3369 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
3371 /*% ripper: dot2!(Qnil, $2) %*/
3377 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
3379 /*% ripper: dot3!(Qnil, $2) %*/
3383 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3387 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3391 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3395 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3399 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3403 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3405 | tUMINUS_NUM simple_numeric tPOW arg
3407 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3411 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3415 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3419 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3423 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3427 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3431 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3433 | rel_expr %prec tCMP
3436 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3440 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3444 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
3448 $$ = match_op(p, $1, $3, &@2, &@$);
3452 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
3456 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3460 $$ = call_uni_op(p, $2, '~', &@1, &@$);
3464 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
3468 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
3472 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
3476 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
3478 | keyword_defined opt_nl begin_defined arg
3480 p->ctxt.in_defined = $3.in_defined;
3481 $$ = new_defined(p, $4, &@$);
3483 | arg '?' arg opt_nl ':' arg
3487 $$ = new_if(p, $1, $3, $6, &@$);
3490 /*% ripper: ifop!($1, $3, $6) %*/
3492 | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
3494 endless_method_name(p, get_id($head->nd_mid), &@head);
3495 restore_defun(p, $head);
3497 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3498 ($$ = $head->nd_def)->nd_loc = @$;
3499 RNODE_DEFN($$)->nd_defn = $bodystmt;
3501 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
3502 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
3505 | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
3507 endless_method_name(p, get_id($head->nd_mid), &@head);
3508 restore_defun(p, $head);
3510 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3511 ($$ = $head->nd_def)->nd_loc = @$;
3512 RNODE_DEFS($$)->nd_defn = $bodystmt;
3514 /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/
3515 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
3524endless_arg : arg %prec modifier_rescue
3525 | endless_arg modifier_rescue after_rescue arg
3527 p->ctxt.in_rescue = $3.in_rescue;
3529 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3531 /*% ripper: rescue_mod!($1, $4) %*/
3533 | keyword_not opt_nl endless_arg
3535 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3539relop : '>' {$$ = '>';}
3545rel_expr : arg relop arg %prec '>'
3547 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
3549 | rel_expr relop arg %prec '>'
3551 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
3552 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
3562begin_defined : lex_ctxt
3564 p->ctxt.in_defined = 1;
3569after_rescue : lex_ctxt
3571 p->ctxt.in_rescue = after_rescue;
3588 | args ',' assocs trailer
3591 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3593 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3598 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
3600 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3604arg_rhs : arg %prec tOP_ASGN
3609 | arg modifier_rescue after_rescue arg
3611 p->ctxt.in_rescue = $3.in_rescue;
3614 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3616 /*% ripper: rescue_mod!($1, $4) %*/
3620paren_args : '(' opt_call_args rparen
3625 /*% ripper: arg_paren!($2) %*/
3627 | '(' args ',' args_forward rparen
3629 if (!check_forwarding_args(p)) {
3634 $$ = new_args_forward_call(p, $2, &@4, &@$);
3636 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
3639 | '(' args_forward rparen
3641 if (!check_forwarding_args(p)) {
3646 $$ = new_args_forward_call(p, 0, &@2, &@$);
3648 /*% ripper: arg_paren!($2) %*/
3653opt_paren_args : none
3663 | args ',' assocs ','
3666 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3668 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3673 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3675 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3683 $$ = NEW_LIST($1, &@$);
3685 /*% ripper: args_add!(args_new!, $1) %*/
3687 | args opt_block_arg
3690 $$ = arg_blk_pass($1, $2);
3692 /*% ripper: args_add_block!($1, $2) %*/
3694 | assocs opt_block_arg
3697 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3698 $$ = arg_blk_pass($$, $2);
3700 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
3702 | args ',' assocs opt_block_arg
3705 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3706 $$ = arg_blk_pass($$, $4);
3708 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
3711 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
3715 /* If call_args starts with a open paren '(' or '[',
3716 * look-ahead reading of the letters calls CMDARG_PUSH(0),
3717 * but the push must be done after CMDARG_PUSH(1).
3718 * So this code makes them consistent by first cancelling
3719 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
3720 * and finally redoing CMDARG_PUSH(0).
3724 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
3727 if (lookahead) CMDARG_POP();
3729 if (lookahead) CMDARG_PUSH(0);
3733 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
3734 * but the push must be done after CMDARG_POP() in the parser.
3735 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
3736 * CMDARG_POP() to pop 1 pushed by command_args,
3737 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
3744 if (lookahead) CMDARG_POP();
3746 if (lookahead) CMDARG_PUSH(0);
3751block_arg : tAMPER arg_value
3754 $$ = NEW_BLOCK_PASS($2, &@$);
3760 forwarding_arg_check(p, idFWD_BLOCK, 0, "block");
3762 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
3764 /*% ripper: Qnil %*/
3768opt_block_arg : ',' block_arg
3782 $$ = NEW_LIST($1, &@$);
3784 /*% ripper: args_add!(args_new!, $1) %*/
3789 $$ = NEW_SPLAT($arg_splat, &@$);
3791 /*% ripper: args_add_star!(args_new!, $arg_splat) %*/
3793 | args ',' arg_value
3796 $$ = last_arg_append(p, $1, $3, &@$);
3798 /*% ripper: args_add!($1, $3) %*/
3800 | args ',' arg_splat
3803 $$ = rest_arg_append(p, $args, $arg_splat, &@$);
3805 /*% ripper: args_add_star!($args, $arg_splat) %*/
3810arg_splat : tSTAR arg_value
3816 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
3818 $$ = NEW_LVAR(idFWD_REST, &@1);
3820 /*% ripper: Qnil %*/
3830mrhs : args ',' arg_value
3833 $$ = last_arg_append(p, $1, $3, &@$);
3835 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
3837 | args ',' tSTAR arg_value
3840 $$ = rest_arg_append(p, $1, $4, &@$);
3842 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
3847 $$ = NEW_SPLAT($2, &@$);
3849 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
3866 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
3868 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
3879 set_line_body($3, @1.end_pos.lineno);
3880 $$ = NEW_BEGIN($3, &@$);
3881 nd_set_line($$, @1.end_pos.lineno);
3883 /*% ripper: begin!($3) %*/
3885 | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')'
3888 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3891 /*% ripper: paren!($2) %*/
3893 | tLPAREN compstmt ')'
3896 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
3897 $$ = NEW_BEGIN($2, &@$);
3899 /*% ripper: paren!($2) %*/
3901 | primary_value tCOLON2 tCONSTANT
3904 $$ = NEW_COLON2($1, $3, &@$);
3906 /*% ripper: const_path_ref!($1, $3) %*/
3911 $$ = NEW_COLON3($2, &@$);
3913 /*% ripper: top_const_ref!($2) %*/
3915 | tLBRACK aref_args ']'
3918 $$ = make_list($2, &@$);
3920 /*% ripper: array!($2) %*/
3922 | tLBRACE assoc_list '}'
3925 $$ = new_hash(p, $2, &@$);
3926 RNODE_HASH($$)->nd_brace = TRUE;
3928 /*% ripper: hash!($2) %*/
3933 $$ = NEW_RETURN(0, &@$);
3935 /*% ripper: return0! %*/
3937 | k_yield '(' call_args rparen
3940 $$ = new_yield(p, $3, &@$);
3942 /*% ripper: yield!(paren!($3)) %*/
3944 | k_yield '(' rparen
3947 $$ = NEW_YIELD(0, &@$);
3949 /*% ripper: yield!(paren!(args_new!)) %*/
3954 $$ = NEW_YIELD(0, &@$);
3956 /*% ripper: yield0! %*/
3958 | keyword_defined opt_nl '(' begin_defined expr rparen
3960 p->ctxt.in_defined = $4.in_defined;
3961 $$ = new_defined(p, $5, &@$);
3963 | keyword_not '(' expr rparen
3965 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3967 | keyword_not '(' rparen
3969 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3974 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
3976 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3979 | method_call brace_block
3982 block_dup_check(p, get_nd_args(p, $1), $2);
3983 $$ = method_add_block(p, $1, $2, &@$);
3985 /*% ripper: method_add_block!($1, $2) %*/
3988 | k_if expr_value then
3994 $$ = new_if(p, $2, $4, $5, &@$);
3997 /*% ripper: if!($2, $4, $5) %*/
3999 | k_unless expr_value then
4005 $$ = new_unless(p, $2, $4, $5, &@$);
4008 /*% ripper: unless!($2, $4, $5) %*/
4010 | k_while expr_value_do
4014 restore_block_exit(p, $1);
4016 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
4019 /*% ripper: while!($2, $3) %*/
4021 | k_until expr_value_do
4025 restore_block_exit(p, $1);
4027 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
4030 /*% ripper: until!($2, $3) %*/
4032 | k_case expr_value opt_terms
4034 $<val>$ = p->case_labels;
4035 p->case_labels = Qnil;
4040 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
4041 p->case_labels = $<val>4;
4043 $$ = NEW_CASE($2, $5, &@$);
4046 /*% ripper: case!($2, $5) %*/
4050 $<val>$ = p->case_labels;
4056 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
4057 p->case_labels = $<val>3;
4059 $$ = NEW_CASE2($4, &@$);
4061 /*% ripper: case!(Qnil, $4) %*/
4063 | k_case expr_value opt_terms
4068 $$ = NEW_CASE3($2, $4, &@$);
4070 /*% ripper: case!($2, $4) %*/
4072 | k_for for_var keyword_in expr_value_do
4076 restore_block_exit(p, $1);
4081 * e.each{|*x| a, b, c = x}
4085 * e.each{|x| a, = x}
4087 ID id = internal_id(p);
4088 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4089 rb_node_args_t *args;
4090 NODE *scope, *internal_var = NEW_DVAR(id, &@2);
4091 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4092 tbl->ids[0] = id; /* internal id */
4094 switch (nd_type($2)) {
4096 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4097 set_nd_value(p, $2, internal_var);
4102 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4103 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
4105 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4106 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
4108 /* {|*internal_id| <m> = internal_id; ... } */
4109 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
4110 scope = NEW_SCOPE2(tbl, args, $5, &@$);
4111 $$ = NEW_FOR($4, scope, &@$);
4114 /*% ripper: for!($2, $4, $5) %*/
4116 | k_class cpath superclass
4118 begin_definition("class", &@k_class, &@cpath);
4124 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4125 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4126 set_line_body($bodystmt, @superclass.end_pos.lineno);
4127 nd_set_line($$, @superclass.end_pos.lineno);
4129 /*% ripper: class!($cpath, $superclass, $bodystmt) %*/
4131 p->ctxt.in_class = $k_class.in_class;
4132 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4134 | k_class tLSHFT expr_value
4136 begin_definition("", &@k_class, &@tLSHFT);
4143 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4144 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4145 set_line_body($bodystmt, nd_line($expr_value));
4146 fixpos($$, $expr_value);
4148 /*% ripper: sclass!($expr_value, $bodystmt) %*/
4150 p->ctxt.in_def = $k_class.in_def;
4151 p->ctxt.in_class = $k_class.in_class;
4152 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4156 begin_definition("module", &@k_module, &@cpath);
4162 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4163 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4164 set_line_body($bodystmt, @cpath.end_pos.lineno);
4165 nd_set_line($$, @cpath.end_pos.lineno);
4167 /*% ripper: module!($cpath, $bodystmt) %*/
4169 p->ctxt.in_class = $k_module.in_class;
4170 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4176 push_end_expect_token_locations(p, &@head.beg_pos);
4182 restore_defun(p, $head);
4184 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4185 ($$ = $head->nd_def)->nd_loc = @$;
4186 RNODE_DEFN($$)->nd_defn = $bodystmt;
4188 /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/
4195 push_end_expect_token_locations(p, &@head.beg_pos);
4201 restore_defun(p, $head);
4203 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4204 ($$ = $head->nd_def)->nd_loc = @$;
4205 RNODE_DEFS($$)->nd_defn = $bodystmt;
4207 /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/
4212 $<node>$ = add_block_exit(p, NEW_BREAK(0, &@$));
4213 /*% ripper: break!(args_new!) %*/
4217 $<node>$ = add_block_exit(p, NEW_NEXT(0, &@$));
4218 /*% ripper: next!(args_new!) %*/
4222 $<node>$ = add_block_exit(p, NEW_REDO(&@$));
4223 /*% ripper: redo! %*/
4227 if (!p->ctxt.in_defined) {
4228 switch (p->ctxt.in_rescue) {
4229 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4230 case after_rescue: /* ok */ break;
4231 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4232 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4236 $$ = NEW_RETRY(&@$);
4238 /*% ripper: retry! %*/
4242primary_value : primary
4249k_begin : keyword_begin
4251 token_info_push(p, "begin", &@$);
4253 push_end_expect_token_locations(p, &@1.beg_pos);
4261 token_info_push(p, "if", &@$);
4262 if (p->token_info && p->token_info->nonspc &&
4263 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4264 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4265 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4266 beg += rb_strlen_lit("else");
4267 while (beg < tok && ISSPACE(*beg)) beg++;
4269 p->token_info->nonspc = 0;
4273 push_end_expect_token_locations(p, &@1.beg_pos);
4278k_unless : keyword_unless
4280 token_info_push(p, "unless", &@$);
4282 push_end_expect_token_locations(p, &@1.beg_pos);
4287k_while : keyword_while allow_exits
4290 token_info_push(p, "while", &@$);
4292 push_end_expect_token_locations(p, &@1.beg_pos);
4297k_until : keyword_until allow_exits
4300 token_info_push(p, "until", &@$);
4302 push_end_expect_token_locations(p, &@1.beg_pos);
4307k_case : keyword_case
4309 token_info_push(p, "case", &@$);
4311 push_end_expect_token_locations(p, &@1.beg_pos);
4316k_for : keyword_for allow_exits
4319 token_info_push(p, "for", &@$);
4321 push_end_expect_token_locations(p, &@1.beg_pos);
4326k_class : keyword_class
4328 token_info_push(p, "class", &@$);
4330 p->ctxt.in_rescue = before_rescue;
4332 push_end_expect_token_locations(p, &@1.beg_pos);
4337k_module : keyword_module
4339 token_info_push(p, "module", &@$);
4341 p->ctxt.in_rescue = before_rescue;
4343 push_end_expect_token_locations(p, &@1.beg_pos);
4350 token_info_push(p, "def", &@$);
4351 $$ = NEW_DEF_TEMP(&@$);
4352 p->ctxt.in_argdef = 1;
4358 token_info_push(p, "do", &@$);
4360 push_end_expect_token_locations(p, &@1.beg_pos);
4365k_do_block : keyword_do_block
4367 token_info_push(p, "do", &@$);
4369 push_end_expect_token_locations(p, &@1.beg_pos);
4374k_rescue : keyword_rescue
4376 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4378 p->ctxt.in_rescue = after_rescue;
4382k_ensure : keyword_ensure
4384 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4389k_when : keyword_when
4391 token_info_warn(p, "when", p->token_info, 0, &@$);
4395k_else : keyword_else
4397 token_info *ptinfo_beg = p->token_info;
4398 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4399 token_info_warn(p, "else", p->token_info, same, &@$);
4402 e.next = ptinfo_beg->next;
4404 token_info_setup(&e, p->lex.pbeg, &@$);
4405 if (!e.nonspc) *ptinfo_beg = e;
4410k_elsif : keyword_elsif
4413 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4419 token_info_pop(p, "end", &@$);
4421 pop_end_expect_token_locations(p);
4426 compile_error(p, "syntax error, unexpected end-of-input");
4430k_return : keyword_return
4432 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
4433 yyerror1(&@1, "Invalid return in class/module body");
4437k_yield : keyword_yield
4439 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4440 yyerror1(&@1, "Invalid yield");
4454 | k_elsif expr_value then
4459 $$ = new_if(p, $2, $4, $5, &@$);
4462 /*% ripper: elsif!($2, $4, $5) %*/
4472 /*% ripper: else!($2) %*/
4483 $$ = assignable(p, $1, 0, &@$);
4484 mark_lvar_used(p, $$);
4486 /*% ripper: assignable(p, $1) %*/
4488 | tLPAREN f_margs rparen
4493 /*% ripper: mlhs_paren!($2) %*/
4500 $$ = NEW_LIST($1, &@$);
4502 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
4504 | f_marg_list ',' f_marg
4507 $$ = list_append(p, $1, $3);
4509 /*% ripper: mlhs_add!($1, $3) %*/
4513f_margs : f_marg_list
4516 $$ = NEW_MASGN($1, 0, &@$);
4520 | f_marg_list ',' f_rest_marg
4523 $$ = NEW_MASGN($1, $3, &@$);
4525 /*% ripper: mlhs_add_star!($1, $3) %*/
4527 | f_marg_list ',' f_rest_marg ',' f_marg_list
4530 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4532 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
4537 $$ = NEW_MASGN(0, $1, &@$);
4539 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
4541 | f_rest_marg ',' f_marg_list
4544 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4546 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
4550f_rest_marg : tSTAR f_norm_arg
4553 $$ = assignable(p, $2, 0, &@$);
4554 mark_lvar_used(p, $$);
4556 /*% ripper: assignable(p, $2) %*/
4561 $$ = NODE_SPECIAL_NO_NAME_REST;
4563 /*% ripper: Qnil %*/
4567f_any_kwrest : f_kwrest
4568 | f_no_kwarg {$$ = ID2VAL(idNil);}
4571f_eq : {p->ctxt.in_argdef = 0;} '=';
4573block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
4575 $$ = new_args_tail(p, $1, $3, $4, &@3);
4577 | f_block_kwarg opt_f_block_arg
4579 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4581 | f_any_kwrest opt_f_block_arg
4583 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4587 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4591opt_block_args_tail : ',' block_args_tail
4597 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4603 /* magic number for rest_id in iseq_set_arguments() */
4605 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
4607 /*% ripper: excessed_comma! %*/
4611block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
4613 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4615 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4617 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4619 | f_arg ',' f_block_optarg opt_block_args_tail
4621 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4623 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
4625 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4627 | f_arg ',' f_rest_arg opt_block_args_tail
4629 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4631 | f_arg excessed_comma
4633 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
4634 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
4636 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
4638 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4640 | f_arg opt_block_args_tail
4642 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4644 | f_block_optarg ',' f_rest_arg opt_block_args_tail
4646 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4648 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4650 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4652 | f_block_optarg opt_block_args_tail
4654 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4656 | f_block_optarg ',' f_arg opt_block_args_tail
4658 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4660 | f_rest_arg opt_block_args_tail
4662 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4664 | f_rest_arg ',' f_arg opt_block_args_tail
4666 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4670 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4674opt_block_param : none
4677 p->command_start = TRUE;
4681block_param_def : '|' opt_bv_decl '|'
4684 p->max_numparam = ORDINAL_PARAM;
4685 p->ctxt.in_argdef = 0;
4689 /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/
4690 /*% ripper: block_var!($$, $2) %*/
4692 | '|' block_param opt_bv_decl '|'
4695 p->max_numparam = ORDINAL_PARAM;
4696 p->ctxt.in_argdef = 0;
4700 /*% ripper: block_var!($2, $3) %*/
4709 | opt_nl ';' bv_decls opt_nl
4719 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
4721 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
4726 new_bv(p, get_id($1));
4727 /*% ripper: get_value($1) %*/
4736 $$ = p->max_numparam;
4737 p->max_numparam = 0;
4742 $$ = numparam_push(p);
4746lambda : tLAMBDA[dyna]
4748 token_info_push(p, "->", &@1);
4749 $<vars>dyna = dyna_push(p);
4750 $<num>$ = p->lex.lpar_beg;
4751 p->lex.lpar_beg = p->lex.paren_nest;
4753 max_numparam numparam allow_exits
4760 int max_numparam = p->max_numparam;
4761 p->lex.lpar_beg = $<num>lpar;
4762 p->max_numparam = $max_numparam;
4763 restore_block_exit(p, $allow_exits);
4765 $args = args_with_numbered(p, $args, max_numparam);
4768 YYLTYPE loc = code_loc_gen(&@args, &@body);
4769 $$ = NEW_LAMBDA($args, $body, &loc);
4770 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
4771 nd_set_line($$, @args.end_pos.lineno);
4772 nd_set_first_loc($$, @1.beg_pos);
4775 /*% ripper: lambda!($args, $body) %*/
4776 numparam_pop(p, $numparam);
4777 dyna_pop(p, $<vars>dyna);
4781f_larglist : '(' f_args opt_bv_decl ')'
4783 p->ctxt.in_argdef = 0;
4786 p->max_numparam = ORDINAL_PARAM;
4788 /*% ripper: paren!($2) %*/
4792 p->ctxt.in_argdef = 0;
4794 if (!args_info_empty_p(&$1->nd_ainfo))
4795 p->max_numparam = ORDINAL_PARAM;
4801lambda_body : tLAMBEG compstmt '}'
4803 token_info_pop(p, "}", &@3);
4809 push_end_expect_token_locations(p, &@1.beg_pos);
4818do_block : k_do_block do_body k_end
4822 set_embraced_location($$, &@1, &@3);
4827block_call : command do_block
4830 if (nd_type_p($1, NODE_YIELD)) {
4831 compile_error(p, "block given to yield");
4834 block_dup_check(p, get_nd_args(p, $1), $2);
4836 $$ = method_add_block(p, $1, $2, &@$);
4839 /*% ripper: method_add_block!($1, $2) %*/
4841 | block_call call_op2 operation2 opt_paren_args
4844 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4846 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4848 | block_call call_op2 operation2 opt_paren_args brace_block
4851 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4853 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
4855 | block_call call_op2 operation2 command_args do_block
4858 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4860 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
4864method_call : fcall paren_args
4869 nd_set_last_loc($1, @2.end_pos);
4871 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
4873 | primary_value call_op operation2 opt_paren_args
4876 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4877 nd_set_line($$, @3.end_pos.lineno);
4879 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4881 | primary_value tCOLON2 operation2 paren_args
4884 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
4885 nd_set_line($$, @3.end_pos.lineno);
4887 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
4889 | primary_value tCOLON2 operation3
4892 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4894 /*% ripper: call!($1, $2, $3) %*/
4896 | primary_value call_op paren_args
4899 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
4900 nd_set_line($$, @2.end_pos.lineno);
4902 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4904 | primary_value tCOLON2 paren_args
4907 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
4908 nd_set_line($$, @2.end_pos.lineno);
4910 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4912 | keyword_super paren_args
4915 $$ = NEW_SUPER($2, &@$);
4917 /*% ripper: super!($2) %*/
4922 $$ = NEW_ZSUPER(&@$);
4924 /*% ripper: zsuper! %*/
4926 | primary_value '[' opt_call_args rbracket
4929 $$ = NEW_CALL($1, tAREF, $3, &@$);
4932 /*% ripper: aref!($1, $3) %*/
4936brace_block : '{' brace_body '}'
4940 set_embraced_location($$, &@1, &@3);
4943 | k_do do_body k_end
4947 set_embraced_location($$, &@1, &@3);
4952brace_body : {$<vars>$ = dyna_push(p);}[dyna]
4953 max_numparam numparam allow_exits
4954 opt_block_param[args] compstmt
4956 int max_numparam = p->max_numparam;
4957 p->max_numparam = $max_numparam;
4958 $args = args_with_numbered(p, $args, max_numparam);
4960 $$ = NEW_ITER($args, $compstmt, &@$);
4962 /*% ripper: brace_block!($args, $compstmt) %*/
4963 restore_block_exit(p, $allow_exits);
4964 numparam_pop(p, $numparam);
4965 dyna_pop(p, $<vars>dyna);
4970 $<vars>$ = dyna_push(p);
4973 max_numparam numparam allow_exits
4974 opt_block_param[args] bodystmt
4976 int max_numparam = p->max_numparam;
4977 p->max_numparam = $max_numparam;
4978 $args = args_with_numbered(p, $args, max_numparam);
4980 $$ = NEW_ITER($args, $bodystmt, &@$);
4982 /*% ripper: do_block!($args, $bodystmt) %*/
4984 restore_block_exit(p, $allow_exits);
4985 numparam_pop(p, $numparam);
4986 dyna_pop(p, $<vars>dyna);
4990case_args : arg_value
4993 check_literal_when(p, $1, &@1);
4994 $$ = NEW_LIST($1, &@$);
4996 /*% ripper: args_add!(args_new!, $1) %*/
5001 $$ = NEW_SPLAT($2, &@$);
5003 /*% ripper: args_add_star!(args_new!, $2) %*/
5005 | case_args ',' arg_value
5008 check_literal_when(p, $3, &@3);
5009 $$ = last_arg_append(p, $1, $3, &@$);
5011 /*% ripper: args_add!($1, $3) %*/
5013 | case_args ',' tSTAR arg_value
5016 $$ = rest_arg_append(p, $1, $4, &@$);
5018 /*% ripper: args_add_star!($1, $4) %*/
5022case_body : k_when case_args then
5027 $$ = NEW_WHEN($2, $4, $5, &@$);
5030 /*% ripper: when!($2, $4, $5) %*/
5038p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5039p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5043 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5044 p->command_start = FALSE;
5045 p->ctxt.in_kwarg = 1;
5049p_case_body : keyword_in
5050 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5051 p_top_expr[expr] then
5053 pop_pktbl(p, $p_pktbl);
5054 pop_pvtbl(p, $p_pvtbl);
5055 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5061 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5063 /*% ripper: in!($expr, $compstmt, $cases) %*/
5071p_top_expr : p_top_expr_body
5072 | p_top_expr_body modifier_if expr_value
5075 $$ = new_if(p, $3, $1, 0, &@$);
5078 /*% ripper: if_mod!($3, $1) %*/
5080 | p_top_expr_body modifier_unless expr_value
5083 $$ = new_unless(p, $3, $1, 0, &@$);
5086 /*% ripper: unless_mod!($3, $1) %*/
5090p_top_expr_body : p_expr
5093 $$ = new_array_pattern_tail(p, Qnone, 1, Qnone, Qnone, &@$);
5094 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
5098 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
5100 nd_set_first_loc($$, @1.beg_pos);
5106 $$ = new_find_pattern(p, Qnone, $1, &@$);
5110 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
5114 $$ = new_hash_pattern(p, Qnone, $1, &@$);
5121p_as : p_expr tASSOC p_variable
5124 NODE *n = NEW_LIST($1, &@$);
5125 n = list_append(p, n, $3);
5126 $$ = new_hash(p, n, &@$);
5128 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
5133p_alt : p_alt '|' p_expr_basic
5136 $$ = NEW_OR($1, $3, &@$);
5138 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
5143p_lparen : '(' p_pktbl { $$ = $2;};
5144p_lbracket : '[' p_pktbl { $$ = $2;};
5146p_expr_basic : p_value
5148 | p_const p_lparen[p_pktbl] p_args rparen
5150 pop_pktbl(p, $p_pktbl);
5151 $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$);
5153 nd_set_first_loc($$, @p_const.beg_pos);
5157 | p_const p_lparen[p_pktbl] p_find rparen
5159 pop_pktbl(p, $p_pktbl);
5160 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5162 nd_set_first_loc($$, @p_const.beg_pos);
5166 | p_const p_lparen[p_pktbl] p_kwargs rparen
5168 pop_pktbl(p, $p_pktbl);
5169 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5171 nd_set_first_loc($$, @p_const.beg_pos);
5175 | p_const '(' rparen
5177 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5178 $$ = new_array_pattern(p, $p_const, Qnone, $$, &@$);
5180 | p_const p_lbracket[p_pktbl] p_args rbracket
5182 pop_pktbl(p, $p_pktbl);
5183 $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$);
5185 nd_set_first_loc($$, @p_const.beg_pos);
5189 | p_const p_lbracket[p_pktbl] p_find rbracket
5191 pop_pktbl(p, $p_pktbl);
5192 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5194 nd_set_first_loc($$, @p_const.beg_pos);
5198 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5200 pop_pktbl(p, $p_pktbl);
5201 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5203 nd_set_first_loc($$, @p_const.beg_pos);
5207 | p_const '[' rbracket
5209 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5210 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
5212 | tLBRACK p_args rbracket
5214 $$ = new_array_pattern(p, Qnone, Qnone, $p_args, &@$);
5216 | tLBRACK p_find rbracket
5218 $$ = new_find_pattern(p, Qnone, $p_find, &@$);
5222 $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$);
5223 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
5225 | tLBRACE p_pktbl lex_ctxt[ctxt]
5227 p->ctxt.in_kwarg = 0;
5231 pop_pktbl(p, $p_pktbl);
5232 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5233 $$ = new_hash_pattern(p, Qnone, $p_kwargs, &@$);
5237 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
5238 $$ = new_hash_pattern(p, Qnone, $$, &@$);
5240 | tLPAREN p_pktbl p_expr rparen
5242 pop_pktbl(p, $p_pktbl);
5250 NODE *pre_args = NEW_LIST($1, &@$);
5251 $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$);
5253 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, Qnone, Qnone, &@$);
5258 $$ = new_array_pattern_tail(p, $1, 1, Qnone, Qnone, &@$);
5263 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, Qnone, Qnone, &@$);
5265 VALUE pre_args = rb_ary_concat($1, get_value($2));
5266 $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$);
5269 | p_args_head p_rest
5271 $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$);
5273 | p_args_head p_rest ',' p_args_post
5275 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5280p_args_head : p_arg ','
5284 | p_args_head p_arg ','
5287 $$ = list_concat($1, $2);
5289 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
5295 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
5297 | p_rest ',' p_args_post
5299 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
5303p_find : p_rest ',' p_args_post ',' p_rest
5305 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5310p_rest : tSTAR tIDENTIFIER
5313 error_duplicate_pattern_variable(p, $2, &@2);
5314 $$ = assignable(p, $2, 0, &@$);
5316 /*% ripper: assignable(p, var_field(p, $2)) %*/
5323 /*% ripper: var_field(p, Qnil) %*/
5328 | p_args_post ',' p_arg
5331 $$ = list_concat($1, $3);
5333 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
5340 $$ = NEW_LIST($1, &@$);
5342 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
5346p_kwargs : p_kwarg ',' p_any_kwrest
5348 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5352 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5356 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5360 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
5365 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
5369 $$ = list_concat($1, $3);
5371 /*% ripper: rb_ary_push($1, $3) %*/
5375p_kw : p_kw_label p_expr
5377 error_duplicate_pattern_key(p, get_id($1), &@1);
5379 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5381 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
5385 error_duplicate_pattern_key(p, get_id($1), &@1);
5386 if ($1 && !is_local_id(get_id($1))) {
5387 yyerror1(&@1, "key must be valid as local variables");
5389 error_duplicate_pattern_variable(p, get_id($1), &@1);
5391 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
5393 /*% ripper: rb_ary_new_from_args(2, get_value(assignable(p, $1)), Qnil) %*/
5398 | tSTRING_BEG string_contents tLABEL_END
5400 YYLTYPE loc = code_loc_gen(&@1, &@3);
5402 if (!$2 || nd_type_p($2, NODE_STR)) {
5403 NODE *node = dsym_node(p, $2, &loc);
5404 $$ = SYM2ID(RNODE_LIT(node)->nd_lit);
5407 if (ripper_is_node_yylval(p, $2) && RNODE_RIPPER($2)->nd_cval) {
5408 VALUE label = RNODE_RIPPER($2)->nd_cval;
5409 VALUE rval = RNODE_RIPPER($2)->nd_rval;
5410 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
5411 RNODE($$)->nd_loc = loc;
5415 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5421p_kwrest : kwrest_mark tIDENTIFIER
5431p_kwnorest : kwrest_mark keyword_nil
5437p_any_kwrest : p_kwrest
5438 | p_kwnorest {$$ = ID2VAL(idNil);}
5441p_value : p_primitive
5442 | p_primitive tDOT2 p_primitive
5447 $$ = NEW_DOT2($1, $3, &@$);
5449 /*% ripper: dot2!($1, $3) %*/
5451 | p_primitive tDOT3 p_primitive
5456 $$ = NEW_DOT3($1, $3, &@$);
5458 /*% ripper: dot3!($1, $3) %*/
5464 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
5466 /*% ripper: dot2!($1, Qnil) %*/
5472 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
5474 /*% ripper: dot3!($1, Qnil) %*/
5479 | tBDOT2 p_primitive
5483 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
5485 /*% ripper: dot2!(Qnil, $2) %*/
5487 | tBDOT3 p_primitive
5491 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
5493 /*% ripper: dot3!(Qnil, $2) %*/
5497p_primitive : literal
5508 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5510 /*% ripper: var_ref!($1) %*/
5515p_variable : tIDENTIFIER
5518 error_duplicate_pattern_variable(p, $1, &@1);
5519 $$ = assignable(p, $1, 0, &@$);
5521 /*% ripper: assignable(p, var_field(p, $1)) %*/
5525p_var_ref : '^' tIDENTIFIER
5528 NODE *n = gettable(p, $2, &@$);
5529 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5530 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5534 /*% ripper: var_ref!($2) %*/
5539 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
5541 /*% ripper: var_ref!($2) %*/
5545p_expr_ref : '^' tLPAREN expr_value rparen
5548 $$ = NEW_BEGIN($3, &@$);
5550 /*% ripper: begin!($3) %*/
5554p_const : tCOLON3 cname
5557 $$ = NEW_COLON3($2, &@$);
5559 /*% ripper: top_const_ref!($2) %*/
5561 | p_const tCOLON2 cname
5564 $$ = NEW_COLON2($1, $3, &@$);
5566 /*% ripper: const_path_ref!($1, $3) %*/
5571 $$ = gettable(p, $1, &@$);
5573 /*% ripper: var_ref!($1) %*/
5577opt_rescue : k_rescue exc_list exc_var then
5584 NODE *err = NEW_ERRINFO(&@3);
5585 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5586 body = block_append(p, err, body);
5588 $$ = NEW_RESBODY($2, body, $6, &@$);
5599 /*% ripper: rescue!($2, $3, $5, $6) %*/
5607 $$ = NEW_LIST($1, &@$);
5609 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5614 if (!($$ = splat_array($1))) $$ = $1;
5628opt_ensure : k_ensure compstmt
5630 p->ctxt.in_rescue = $1.in_rescue;
5634 /*% ripper: ensure!($2) %*/
5648 node = NEW_STR(STR_NEW0(), &@$);
5649 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit);
5652 node = evstr2dstr(p, node);
5665 $$ = literal_concat(p, $1, $2, &@$);
5667 /*% ripper: string_concat!($1, $2) %*/
5671string1 : tSTRING_BEG string_contents tSTRING_END
5674 $$ = heredoc_dedent(p, $2);
5675 if ($$) nd_set_loc($$, &@$);
5677 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
5681xstring : tXSTRING_BEG xstring_contents tSTRING_END
5684 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5686 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
5690regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5692 $$ = new_regexp(p, $2, $3, &@$);
5700words : tWORDS_BEG words_sep word_list tSTRING_END
5703 $$ = make_list($3, &@$);
5705 /*% ripper: array!($3) %*/
5709word_list : /* none */
5714 /*% ripper: words_new! %*/
5716 | word_list word words_sep
5719 $$ = list_append(p, $1, evstr2dstr(p, $2));
5721 /*% ripper: words_add!($1, $2) %*/
5725word : string_content
5726 /*% ripper[brace]: word_add!(word_new!, $1) %*/
5727 | word string_content
5730 $$ = literal_concat(p, $1, $2, &@$);
5732 /*% ripper: word_add!($1, $2) %*/
5736symbols : tSYMBOLS_BEG words_sep symbol_list tSTRING_END
5739 $$ = make_list($3, &@$);
5741 /*% ripper: array!($3) %*/
5745symbol_list : /* none */
5750 /*% ripper: symbols_new! %*/
5752 | symbol_list word words_sep
5755 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5757 /*% ripper: symbols_add!($1, $2) %*/
5761qwords : tQWORDS_BEG words_sep qword_list tSTRING_END
5764 $$ = make_list($3, &@$);
5766 /*% ripper: array!($3) %*/
5770qsymbols : tQSYMBOLS_BEG words_sep qsym_list tSTRING_END
5773 $$ = make_list($3, &@$);
5775 /*% ripper: array!($3) %*/
5779qword_list : /* none */
5784 /*% ripper: qwords_new! %*/
5786 | qword_list tSTRING_CONTENT words_sep
5789 $$ = list_append(p, $1, $2);
5791 /*% ripper: qwords_add!($1, $2) %*/
5795qsym_list : /* none */
5800 /*% ripper: qsymbols_new! %*/
5802 | qsym_list tSTRING_CONTENT words_sep
5805 $$ = symbol_append(p, $1, $2);
5807 /*% ripper: qsymbols_add!($1, $2) %*/
5811string_contents : /* none */
5816 /*% ripper: string_content! %*/
5819 $$ = ripper_new_yylval(p, 0, $$, 0);
5822 | string_contents string_content
5825 $$ = literal_concat(p, $1, $2, &@$);
5827 /*% ripper: string_add!($1, $2) %*/
5830 if (ripper_is_node_yylval(p, $1) && ripper_is_node_yylval(p, $2) &&
5831 !RNODE_RIPPER($1)->nd_cval) {
5832 RNODE_RIPPER($1)->nd_cval = RNODE_RIPPER($2)->nd_cval;
5833 RNODE_RIPPER($1)->nd_rval = add_mark_object(p, $$);
5840xstring_contents: /* none */
5845 /*% ripper: xstring_new! %*/
5847 | xstring_contents string_content
5850 $$ = literal_concat(p, $1, $2, &@$);
5852 /*% ripper: xstring_add!($1, $2) %*/
5856regexp_contents: /* none */
5861 /*% ripper: regexp_new! %*/
5864 $$ = ripper_new_yylval(p, 0, $$, 0);
5867 | regexp_contents string_content
5870 NODE *head = $1, *tail = $2;
5878 switch (nd_type(head)) {
5880 head = str2dstr(p, head);
5885 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
5888 $$ = list_append(p, head, tail);
5891 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
5892 if (ripper_is_node_yylval(p, n1)) {
5893 s1 = RNODE_RIPPER(n1)->nd_cval;
5894 n1 = RNODE_RIPPER(n1)->nd_rval;
5896 if (ripper_is_node_yylval(p, n2)) {
5897 s2 = RNODE_RIPPER(n2)->nd_cval;
5898 n2 = RNODE_RIPPER(n2)->nd_rval;
5900 $$ = dispatch2(regexp_add, n1, n2);
5902 $$ = ripper_new_yylval(p, 0, $$, s2);
5908string_content : tSTRING_CONTENT
5909 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
5912 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
5913 $<strterm>$ = p->lex.strterm;
5915 SET_LEX_STATE(EXPR_BEG);
5919 p->lex.strterm = $<strterm>2;
5921 $$ = NEW_EVSTR($3, &@$);
5922 nd_set_line($$, @3.end_pos.lineno);
5924 /*% ripper: string_dvar!($3) %*/
5926 | tSTRING_DBEG[term]
5930 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
5931 $<strterm>term = p->lex.strterm;
5933 $<num>$ = p->lex.state;
5934 SET_LEX_STATE(EXPR_BEG);
5937 $<num>$ = p->lex.brace_nest;
5938 p->lex.brace_nest = 0;
5941 $<num>$ = p->heredoc_indent;
5942 p->heredoc_indent = 0;
5944 compstmt string_dend
5948 p->lex.strterm = $<strterm>term;
5949 SET_LEX_STATE($<num>state);
5950 p->lex.brace_nest = $<num>brace;
5951 p->heredoc_indent = $<num>indent;
5952 p->heredoc_line_indent = -1;
5954 if ($compstmt) nd_unset_fl_newline($compstmt);
5955 $$ = new_evstr(p, $compstmt, &@$);
5957 /*% ripper: string_embexpr!($compstmt) %*/
5961string_dend : tSTRING_DEND
5965string_dvar : nonlocal_var
5968 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5970 /*% ripper: var_ref!($1) %*/
5981 SET_LEX_STATE(EXPR_END);
5983 $$ = NEW_LIT(ID2SYM($2), &@$);
5985 /*% ripper: symbol_literal!(symbol!($2)) %*/
5993dsym : tSYMBEG string_contents tSTRING_END
5995 SET_LEX_STATE(EXPR_END);
5997 $$ = dsym_node(p, $2, &@$);
5999 /*% ripper: dyna_symbol!($2) %*/
6003numeric : simple_numeric
6004 | tUMINUS_NUM simple_numeric %prec tLOWEST
6008 RB_OBJ_WRITE(p->ast, &RNODE_LIT($$)->nd_lit, negate_lit(p, RNODE_LIT($$)->nd_lit));
6010 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
6014simple_numeric : tINTEGER
6025user_variable : tIDENTIFIER
6030keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6031 | keyword_self {$$ = KWD2EID(self, $1);}
6032 | keyword_true {$$ = KWD2EID(true, $1);}
6033 | keyword_false {$$ = KWD2EID(false, $1);}
6034 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6035 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6036 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6039var_ref : user_variable
6042 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
6044 if (id_is_var(p, get_id($1))) {
6045 $$ = dispatch1(var_ref, $1);
6048 $$ = dispatch1(vcall, $1);
6055 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
6057 /*% ripper: var_ref!($1) %*/
6061var_lhs : user_variable
6064 $$ = assignable(p, $1, 0, &@$);
6066 /*% ripper: assignable(p, var_field(p, $1)) %*/
6071 $$ = assignable(p, $1, 0, &@$);
6073 /*% ripper: assignable(p, var_field(p, $1)) %*/
6083 SET_LEX_STATE(EXPR_BEG);
6084 p->command_start = TRUE;
6095 /*% ripper: Qnil %*/
6099f_opt_paren_args: f_paren_args
6102 p->ctxt.in_argdef = 0;
6103 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6104 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
6108f_paren_args : '(' f_args rparen
6113 /*% ripper: paren!($2) %*/
6114 SET_LEX_STATE(EXPR_BEG);
6115 p->command_start = TRUE;
6116 p->ctxt.in_argdef = 0;
6120f_arglist : f_paren_args
6123 p->ctxt.in_kwarg = 1;
6124 p->ctxt.in_argdef = 1;
6125 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6129 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
6130 p->ctxt.in_argdef = 0;
6132 SET_LEX_STATE(EXPR_BEG);
6133 p->command_start = TRUE;
6137args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
6139 $$ = new_args_tail(p, $1, $3, $4, &@3);
6141 | f_kwarg opt_f_block_arg
6143 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
6145 | f_any_kwrest opt_f_block_arg
6147 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
6151 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
6155 add_forwarding_args(p);
6156 $$ = new_args_tail(p, Qnone, $1, arg_FWD_BLOCK, &@1);
6158 $$->nd_ainfo.forwarding = 1;
6163opt_args_tail : ',' args_tail
6169 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6173f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
6175 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
6177 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
6179 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6181 | f_arg ',' f_optarg opt_args_tail
6183 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
6185 | f_arg ',' f_optarg ',' f_arg opt_args_tail
6187 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
6189 | f_arg ',' f_rest_arg opt_args_tail
6191 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
6193 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
6195 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
6197 | f_arg opt_args_tail
6199 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
6201 | f_optarg ',' f_rest_arg opt_args_tail
6203 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
6205 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
6207 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
6209 | f_optarg opt_args_tail
6211 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
6213 | f_optarg ',' f_arg opt_args_tail
6215 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
6217 | f_rest_arg opt_args_tail
6219 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
6221 | f_rest_arg ',' f_arg opt_args_tail
6223 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
6227 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
6231 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
6232 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
6236args_forward : tBDOT3
6239#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6245 /*% ripper: args_forward! %*/
6249f_bad_arg : tCONSTANT
6251 static const char mesg[] = "formal argument cannot be a constant";
6253 yyerror1(&@1, mesg);
6256 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6260 static const char mesg[] = "formal argument cannot be an instance variable";
6262 yyerror1(&@1, mesg);
6265 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6269 static const char mesg[] = "formal argument cannot be a global variable";
6271 yyerror1(&@1, mesg);
6274 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6278 static const char mesg[] = "formal argument cannot be a class variable";
6280 yyerror1(&@1, mesg);
6283 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
6287f_norm_arg : f_bad_arg
6290 formal_argument(p, $1);
6291 p->max_numparam = ORDINAL_PARAM;
6296f_arg_asgn : f_norm_arg
6305f_arg_item : f_arg_asgn
6309 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6311 /*% ripper: get_value($1) %*/
6313 | tLPAREN f_margs rparen
6316 ID tid = internal_id(p);
6318 loc.beg_pos = @2.beg_pos;
6319 loc.end_pos = @2.beg_pos;
6321 if (dyna_in_block(p)) {
6322 $2->nd_value = NEW_DVAR(tid, &loc);
6325 $2->nd_value = NEW_LVAR(tid, &loc);
6327 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6328 $$->nd_next = (NODE *)$2;
6330 /*% ripper: mlhs_paren!($2) %*/
6335 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6336 | f_arg ',' f_arg_item
6341 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6342 rb_discard_node(p, (NODE *)$3);
6344 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6351 arg_var(p, formal_argument(p, $1));
6352 p->cur_arg = get_id($1);
6353 p->max_numparam = ORDINAL_PARAM;
6354 p->ctxt.in_argdef = 0;
6359f_kw : f_label arg_value
6362 p->ctxt.in_argdef = 1;
6364 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6366 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
6371 p->ctxt.in_argdef = 1;
6373 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6375 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
6379f_block_kw : f_label primary_value
6381 p->ctxt.in_argdef = 1;
6383 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6385 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
6389 p->ctxt.in_argdef = 1;
6391 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6393 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
6397f_block_kwarg : f_block_kw
6402 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6404 | f_block_kwarg ',' f_block_kw
6407 $$ = kwd_append($1, $3);
6409 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6419 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6424 $$ = kwd_append($1, $3);
6426 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6434f_no_kwarg : p_kwnorest
6438 /*% ripper: nokw_param!(Qnil) %*/
6442f_kwrest : kwrest_mark tIDENTIFIER
6444 arg_var(p, shadowing_lvar(p, get_id($2)));
6448 /*% ripper: kwrest_param!($2) %*/
6452 arg_var(p, idFWD_KWREST);
6456 /*% ripper: kwrest_param!(Qnil) %*/
6460f_opt : f_arg_asgn f_eq arg_value
6463 p->ctxt.in_argdef = 1;
6465 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
6467 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
6471f_block_opt : f_arg_asgn f_eq primary_value
6474 p->ctxt.in_argdef = 1;
6476 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
6478 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
6482f_block_optarg : f_block_opt
6487 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6489 | f_block_optarg ',' f_block_opt
6492 $$ = opt_arg_append($1, $3);
6494 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6503 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
6505 | f_optarg ',' f_opt
6508 $$ = opt_arg_append($1, $3);
6510 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6518f_rest_arg : restarg_mark tIDENTIFIER
6520 arg_var(p, shadowing_lvar(p, get_id($2)));
6524 /*% ripper: rest_param!($2) %*/
6528 arg_var(p, idFWD_REST);
6532 /*% ripper: rest_param!(Qnil) %*/
6540f_block_arg : blkarg_mark tIDENTIFIER
6542 arg_var(p, shadowing_lvar(p, get_id($2)));
6546 /*% ripper: blockarg!($2) %*/
6550 arg_var(p, idFWD_BLOCK);
6554 /*% ripper: blockarg!(Qnil) %*/
6558opt_f_block_arg : ',' f_block_arg
6573 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
6576 NODE *expr = last_expr_node($3);
6577 switch (nd_type(expr)) {
6587 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6595 /*% ripper: paren!($3) %*/
6605 /*% ripper: assoclist_from_args!($1) %*/
6610 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6620 if (RNODE_LIST(assocs)->nd_head &&
6621 !RNODE_LIST(tail)->nd_head && nd_type_p(RNODE_LIST(tail)->nd_next, NODE_LIST) &&
6622 nd_type_p(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head, NODE_HASH)) {
6624 tail = RNODE_HASH(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head)->nd_head;
6626 assocs = list_concat(assocs, tail);
6630 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6634assoc : arg_value tASSOC arg_value
6637 if (nd_type_p($1, NODE_STR)) {
6638 nd_set_type($1, NODE_LIT);
6639 RB_OBJ_WRITE(p->ast, &RNODE_LIT($1)->nd_lit, rb_fstring(RNODE_LIT($1)->nd_lit));
6641 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6643 /*% ripper: assoc_new!($1, $3) %*/
6648 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
6650 /*% ripper: assoc_new!($1, $2) %*/
6655 NODE *val = gettable(p, $1, &@$);
6656 if (!val) val = NEW_BEGIN(0, &@$);
6657 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
6659 /*% ripper: assoc_new!($1, Qnil) %*/
6661 | tSTRING_BEG string_contents tLABEL_END arg_value
6664 YYLTYPE loc = code_loc_gen(&@1, &@3);
6665 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6667 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
6672 if (nd_type_p($2, NODE_HASH) &&
6673 !(RNODE_HASH($2)->nd_head && RNODE_LIST(RNODE_HASH($2)->nd_head)->as.nd_alen)) {
6674 static VALUE empty_hash;
6676 empty_hash = rb_obj_freeze(rb_hash_new());
6677 rb_gc_register_mark_object(empty_hash);
6679 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
6682 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6684 /*% ripper: assoc_splat!($2) %*/
6688 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6690 $$ = list_append(p, NEW_LIST(0, &@$),
6691 NEW_LVAR(idFWD_KWREST, &@$));
6693 /*% ripper: assoc_splat!(Qnil) %*/
6697operation : tIDENTIFIER
6702operation2 : operation
6706operation3 : tIDENTIFIER
6723opt_terms : /* none */
6734rbracket : opt_nl ']'
6744term : ';' {yyerrok;token_flush(p);}
6747 @$.end_pos = @$.beg_pos;
6753 | terms ';' {yyerrok;}
6765# define yylval (*p->lval)
6767static int regx_options(struct parser_params*);
6768static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6769static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6770static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6771static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6774# define set_yylval_node(x) { \
6776 rb_parser_set_location(p, &_cur_loc); \
6777 yylval.node = (x); \
6779# define set_yylval_str(x) \
6781 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6782 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6784# define set_yylval_literal(x) \
6786 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
6787 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6789# define set_yylval_num(x) (yylval.num = (x))
6790# define set_yylval_id(x) (yylval.id = (x))
6791# define set_yylval_name(x) (yylval.id = (x))
6792# define yylval_id() (yylval.id)
6795ripper_yylval_id(struct parser_params *p, ID x)
6797 return ripper_new_yylval(p, x, ID2SYM(x), 0);
6799# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
6800# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
6801# define set_yylval_id(x) (void)(x)
6802# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
6803# define set_yylval_literal(x) add_mark_object(p, (x))
6804# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
6805# define yylval_id() yylval.id
6806# define _cur_loc NULL_LOC /* dummy */
6809#define set_yylval_noname() set_yylval_id(keyword_nil)
6810#define has_delayed_token(p) (!NIL_P(p->delayed.token))
6813#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6814#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6817parser_has_token(struct parser_params *p)
6819 const char *const pcur = p->lex.pcur;
6820 const char *const ptok = p->lex.ptok;
6821 if (p->keep_tokens && (pcur < ptok)) {
6822 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6823 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6829code_loc_to_ary(struct parser_params *p, const rb_code_location_t *loc)
6831 VALUE ary = rb_ary_new_from_args(4,
6832 INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column),
6833 INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column));
6840parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line)
6845 ary = rb_ary_new2(4);
6846 token_id = p->token_id;
6847 rb_ary_push(ary, INT2FIX(token_id));
6848 rb_ary_push(ary, ID2SYM(parser_token2id(p, t)));
6849 rb_ary_push(ary, str);
6850 rb_ary_push(ary, code_loc_to_ary(p, p->yylloc));
6852 rb_ary_push(p->tokens, ary);
6856 rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary);
6861parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6863 debug_token_line(p, "parser_dispatch_scan_event", line);
6865 if (!parser_has_token(p)) return;
6867 RUBY_SET_YYLLOC(*p->yylloc);
6869 if (p->keep_tokens) {
6870 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6871 parser_append_tokens(p, str, t, line);
6877#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6879parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6881 debug_token_line(p, "parser_dispatch_delayed_token", line);
6883 if (!has_delayed_token(p)) return;
6885 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6887 if (p->keep_tokens) {
6888 parser_append_tokens(p, p->delayed.token, t, line);
6891 p->delayed.token = Qnil;
6894#define literal_flush(p, ptr) ((void)(ptr))
6896#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &RNODE_RIPPER(yylval.node)->nd_rval : &yylval.val))
6899ripper_has_scan_event(struct parser_params *p)
6901 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6902 return p->lex.pcur > p->lex.ptok;
6906ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6908 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6909 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6910 RUBY_SET_YYLLOC(*p->yylloc);
6916ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6918 if (!ripper_has_scan_event(p)) return;
6919 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
6921#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6924ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6926 /* save and adjust the location to delayed token for callbacks */
6927 int saved_line = p->ruby_sourceline;
6928 const char *saved_tokp = p->lex.ptok;
6930 if (!has_delayed_token(p)) return;
6931 p->ruby_sourceline = p->delayed.beg_line;
6932 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6933 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
6934 p->delayed.token = Qnil;
6935 p->ruby_sourceline = saved_line;
6936 p->lex.ptok = saved_tokp;
6938#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6942is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6944 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6948parser_is_identchar(struct parser_params *p)
6950 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
6954parser_isascii(struct parser_params *p)
6956 return ISASCII(*(p->lex.pcur-1));
6960token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6962 int column = 1, nonspc = 0, i;
6963 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6965 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6968 if (*ptr != ' ' && *ptr != '\t') {
6973 ptinfo->beg = loc->beg_pos;
6974 ptinfo->indent = column;
6975 ptinfo->nonspc = nonspc;
6979token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6983 if (!p->token_info_enabled) return;
6984 ptinfo = ALLOC(token_info);
6985 ptinfo->token = token;
6986 ptinfo->next = p->token_info;
6987 token_info_setup(ptinfo, p->lex.pbeg, loc);
6989 p->token_info = ptinfo;
6993token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6995 token_info *ptinfo_beg = p->token_info;
6997 if (!ptinfo_beg) return;
6998 p->token_info = ptinfo_beg->next;
7000 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7001 token_info_warn(p, token, ptinfo_beg, 1, loc);
7002 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7006token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7008 token_info *ptinfo_beg = p->token_info;
7010 if (!ptinfo_beg) return;
7011 p->token_info = ptinfo_beg->next;
7013 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7014 ptinfo_beg->beg.column != beg_pos.column ||
7015 strcmp(ptinfo_beg->token, token)) {
7016 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7017 beg_pos.lineno, beg_pos.column, token,
7018 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7022 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7026token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7028 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7029 if (!p->token_info_enabled) return;
7030 if (!ptinfo_beg) return;
7031 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7032 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7033 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7034 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7035 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7036 rb_warn3L(ptinfo_end->beg.lineno,
7037 "mismatched indentations at '%s' with '%s' at %d",
7038 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7042parser_precise_mbclen(struct parser_params *p, const char *ptr)
7044 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7045 if (!MBCLEN_CHARFOUND_P(len)) {
7046 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7053static void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
7056parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7059 int lineno = p->ruby_sourceline;
7063 else if (yylloc->beg_pos.lineno == lineno) {
7064 str = p->lex.lastline;
7069 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7073parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7079 yylloc = RUBY_SET_YYLLOC(current);
7081 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7082 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7086 parser_compile_error(p, yylloc, "%s", msg);
7087 parser_show_error_line(p, yylloc);
7092parser_yyerror0(struct parser_params *p, const char *msg)
7095 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7099ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
7102 const int max_line_margin = 30;
7103 const char *ptr, *ptr_end, *pt, *pb;
7104 const char *pre = "", *post = "", *pend;
7105 const char *code = "", *caret = "";
7107 const char *const pbeg = RSTRING_PTR(str);
7112 if (!yylloc) return;
7113 pend = RSTRING_END(str);
7114 if (pend > pbeg && pend[-1] == '\n') {
7115 if (--pend > pbeg && pend[-1] == '\r') --pend;
7119 if (lineno == yylloc->end_pos.lineno &&
7120 (pend - pbeg) > yylloc->end_pos.column) {
7121 pt = pbeg + yylloc->end_pos.column;
7125 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7126 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7128 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7129 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7131 len = ptr_end - ptr;
7134 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
7135 if (ptr > pbeg) pre = "...";
7137 if (ptr_end < pend) {
7138 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
7139 if (ptr_end < pend) post = "...";
7143 if (lineno == yylloc->beg_pos.lineno) {
7144 pb += yylloc->beg_pos.column;
7145 if (pb > pt) pb = pt;
7147 if (pb < ptr) pb = ptr;
7148 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7151 if (RTEST(errbuf)) {
7152 mesg = rb_attr_get(errbuf, idMesg);
7153 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
7154 rb_str_cat_cstr(mesg, "\n");
7157 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
7159 if (!errbuf && rb_stderr_tty_p()) {
7160#define CSI_BEGIN "\033["
7163 CSI_BEGIN""CSI_SGR"%s" /* pre */
7164 CSI_BEGIN"1"CSI_SGR"%.*s"
7165 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7166 CSI_BEGIN";1"CSI_SGR"%.*s"
7167 CSI_BEGIN""CSI_SGR"%s" /* post */
7170 (int)(pb - ptr), ptr,
7172 (int)(ptr_end - pt), pt,
7178 len = ptr_end - ptr;
7179 lim = pt < pend ? pt : pend;
7180 i = (int)(lim - ptr);
7181 buf = ALLOCA_N(char, i+2);
7186 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7192 memset(p2, '~', (lim - ptr));
7196 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7197 pre, (int)len, code, post,
7200 if (!errbuf) rb_write_error_str(mesg);
7204parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7206 const char *pcur = 0, *ptok = 0;
7207 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7208 p->ruby_sourceline == yylloc->end_pos.lineno) {
7211 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7212 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7214 parser_yyerror0(p, msg);
7223parser_yyerror0(struct parser_params *p, const char *msg)
7225 dispatch1(parse_error, STR_NEW2(msg));
7231parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7238vtable_size(const struct vtable *tbl)
7240 if (!DVARS_TERMINAL_P(tbl)) {
7249static struct vtable *
7250vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7252 struct vtable *tbl = ALLOC(struct vtable);
7255 tbl->tbl = ALLOC_N(ID, tbl->capa);
7259 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7264#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7267vtable_free_gen(struct parser_params *p, int line, const char *name,
7272 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7275 if (!DVARS_TERMINAL_P(tbl)) {
7277 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7279 ruby_sized_xfree(tbl, sizeof(*tbl));
7282#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7285vtable_add_gen(struct parser_params *p, int line, const char *name,
7286 struct vtable *tbl, ID id)
7290 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7291 line, name, (void *)tbl, rb_id2name(id));
7294 if (DVARS_TERMINAL_P(tbl)) {
7295 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7298 if (tbl->pos == tbl->capa) {
7299 tbl->capa = tbl->capa * 2;
7300 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7302 tbl->tbl[tbl->pos++] = id;
7304#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7308vtable_pop_gen(struct parser_params *p, int line, const char *name,
7309 struct vtable *tbl, int n)
7312 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7313 line, name, (void *)tbl, n);
7316 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7321#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7325vtable_included(const struct vtable * tbl, ID id)
7329 if (!DVARS_TERMINAL_P(tbl)) {
7330 for (i = 0; i < tbl->pos; i++) {
7331 if (tbl->tbl[i] == id) {
7339static void parser_prepare(struct parser_params *p);
7342static NODE *parser_append_options(struct parser_params *p, NODE *node);
7345e_option_supplied(struct parser_params *p)
7347 return strcmp(p->ruby_sourcefile, "-e") == 0;
7351yycompile0(VALUE arg)
7355 struct parser_params *p = (struct parser_params *)arg;
7358 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
7359 if (p->debug_lines && p->ruby_sourceline > 0) {
7360 VALUE str = rb_default_rs;
7361 n = p->ruby_sourceline;
7363 rb_ary_push(p->debug_lines, str);
7367 if (!e_option_supplied(p)) {
7372 if (p->debug_lines) {
7373 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
7377#define RUBY_DTRACE_PARSE_HOOK(name) \
7378 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7379 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7381 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7383 RUBY_DTRACE_PARSE_HOOK(END);
7387 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7388 if (n || p->error_p) {
7389 VALUE mesg = p->error_buffer;
7391 mesg = syntax_error_new();
7393 if (!p->error_tolerant) {
7394 rb_set_errinfo(mesg);
7398 tree = p->eval_tree;
7400 tree = NEW_NIL(&NULL_LOC);
7403 VALUE tokens = p->tokens;
7405 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7406 prelude = block_append(p, p->eval_tree_begin, body);
7407 RNODE_SCOPE(tree)->nd_body = prelude;
7408 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7409 p->ast->body.coverage_enabled = cov;
7410 if (p->keep_tokens) {
7411 rb_obj_freeze(tokens);
7412 rb_ast_set_tokens(p->ast, tokens);
7415 p->ast->body.root = tree;
7416 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
7421yycompile(struct parser_params *p, VALUE fname, int line)
7425 p->ruby_sourcefile_string = Qnil;
7426 p->ruby_sourcefile = "(none)";
7429 p->ruby_sourcefile_string = rb_fstring(fname);
7430 p->ruby_sourcefile = StringValueCStr(fname);
7432 p->ruby_sourceline = line - 1;
7436 p->ast = ast = rb_ast_new();
7437 compile_callback(yycompile0, (VALUE)p);
7449must_be_ascii_compatible(struct parser_params *p, VALUE s)
7451 rb_encoding *enc = rb_enc_get(s);
7452 if (!rb_enc_asciicompat(enc)) {
7453 rb_raise(rb_eArgError, "invalid source encoding");
7459lex_get_str(struct parser_params *p, VALUE s)
7461 char *beg, *end, *start;
7464 beg = RSTRING_PTR(s);
7465 len = RSTRING_LEN(s);
7467 if (p->lex.gets_.ptr) {
7468 if (len == p->lex.gets_.ptr) return Qnil;
7469 beg += p->lex.gets_.ptr;
7470 len -= p->lex.gets_.ptr;
7472 end = memchr(beg, '\n', len);
7473 if (end) len = ++end - beg;
7474 p->lex.gets_.ptr += len;
7475 return rb_str_subseq(s, beg - start, len);
7479lex_getline(struct parser_params *p)
7481 VALUE line = (*p->lex.gets)(p, p->lex.input);
7482 if (NIL_P(line)) return line;
7483 must_be_ascii_compatible(p, line);
7484 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
7491parser_compile_string(rb_parser_t *p, VALUE fname, VALUE s, int line)
7493 p->lex.gets = lex_get_str;
7494 p->lex.gets_.ptr = 0;
7495 p->lex.input = rb_str_new_frozen(s);
7496 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7498 return yycompile(p, fname, line);
7502rb_ruby_parser_compile_string_path(rb_parser_t *p, VALUE f, VALUE s, int line)
7504 must_be_ascii_compatible(p, s);
7505 return parser_compile_string(p, f, s, line);
7509rb_ruby_parser_compile_string(rb_parser_t *p, const char *f, VALUE s, int line)
7511 return rb_ruby_parser_compile_string_path(p, rb_filesystem_str_new_cstr(f), s, line);
7515lex_io_gets(struct parser_params *p, VALUE io)
7517 return rb_io_gets_internal(io);
7521rb_ruby_parser_compile_file_path(rb_parser_t *p, VALUE fname, VALUE file, int start)
7523 p->lex.gets = lex_io_gets;
7524 p->lex.input = file;
7525 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7527 return yycompile(p, fname, start);
7531lex_generic_gets(struct parser_params *p, VALUE input)
7533 return (*p->lex.gets_.call)(input, p->line_count);
7537rb_ruby_parser_compile_generic(rb_parser_t *p, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
7539 p->lex.gets = lex_generic_gets;
7540 p->lex.gets_.call = lex_gets;
7541 p->lex.input = input;
7542 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7544 return yycompile(p, fname, start);
7548#define STR_FUNC_ESCAPE 0x01
7549#define STR_FUNC_EXPAND 0x02
7550#define STR_FUNC_REGEXP 0x04
7551#define STR_FUNC_QWORDS 0x08
7552#define STR_FUNC_SYMBOL 0x10
7553#define STR_FUNC_INDENT 0x20
7554#define STR_FUNC_LABEL 0x40
7555#define STR_FUNC_LIST 0x4000
7556#define STR_FUNC_TERM 0x8000
7559 str_label = STR_FUNC_LABEL,
7561 str_dquote = (STR_FUNC_EXPAND),
7562 str_xquote = (STR_FUNC_EXPAND),
7563 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7564 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7565 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7566 str_ssym = (STR_FUNC_SYMBOL),
7567 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7571parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7575 str = rb_enc_str_new(ptr, len, enc);
7576 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7577 if (is_ascii_string(str)) {
7579 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7580 rb_enc_associate(str, rb_ascii8bit_encoding());
7588strterm_is_heredoc(rb_strterm_t *strterm)
7590 return strterm->flags & STRTERM_HEREDOC;
7593static rb_strterm_t *
7594new_strterm(struct parser_params *p, int func, int term, int paren)
7596 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7597 strterm->u.literal.func = func;
7598 strterm->u.literal.term = term;
7599 strterm->u.literal.paren = paren;
7603static rb_strterm_t *
7604new_heredoc(struct parser_params *p)
7606 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7607 strterm->flags |= STRTERM_HEREDOC;
7611#define peek(p,c) peek_n(p, (c), 0)
7612#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7613#define peekc(p) peekc_n(p, 0)
7614#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7617add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7620 debug_token_line(p, "add_delayed_token", line);
7624 if (has_delayed_token(p)) {
7625 bool next_line = end_with_newline_p(p, p->delayed.token);
7626 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7627 int end_col = (next_line ? 0 : p->delayed.end_col);
7628 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7629 dispatch_delayed_token(p, tSTRING_CONTENT);
7632 if (!has_delayed_token(p)) {
7633 p->delayed.token = rb_str_buf_new(end - tok);
7634 rb_enc_associate(p->delayed.token, p->enc);
7635 p->delayed.beg_line = p->ruby_sourceline;
7636 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7638 rb_str_buf_cat(p->delayed.token, tok, end - tok);
7639 p->delayed.end_line = p->ruby_sourceline;
7640 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7646set_lastline(struct parser_params *p, VALUE v)
7648 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
7649 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
7650 p->lex.lastline = v;
7654nextline(struct parser_params *p, int set_encoding)
7656 VALUE v = p->lex.nextline;
7657 p->lex.nextline = 0;
7662 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7666 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
7673 if (p->debug_lines) {
7674 if (set_encoding) rb_enc_associate(v, p->enc);
7675 rb_ary_push(p->debug_lines, v);
7680 else if (NIL_P(v)) {
7681 /* after here-document without terminator */
7684 add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__);
7685 if (p->heredoc_end > 0) {
7686 p->ruby_sourceline = p->heredoc_end;
7689 p->ruby_sourceline++;
7696parser_cr(struct parser_params *p, int c)
7698 if (peek(p, '\n')) {
7706nextc0(struct parser_params *p, int set_encoding)
7710 if (UNLIKELY(lex_eol_p(p) || p->eofp || RTEST(p->lex.nextline))) {
7711 if (nextline(p, set_encoding)) return -1;
7713 c = (unsigned char)*p->lex.pcur++;
7714 if (UNLIKELY(c == '\r')) {
7715 c = parser_cr(p, c);
7720#define nextc(p) nextc0(p, TRUE)
7723pushback(struct parser_params *p, int c)
7725 if (c == -1) return;
7728 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7733#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7735#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7736#define tok(p) (p)->tokenbuf
7737#define toklen(p) (p)->tokidx
7740looking_at_eol_p(struct parser_params *p)
7742 const char *ptr = p->lex.pcur;
7743 while (!lex_eol_ptr_p(p, ptr)) {
7744 int c = (unsigned char)*ptr++;
7745 int eol = (c == '\n' || c == '#');
7746 if (eol || !ISSPACE(c)) {
7754newtok(struct parser_params *p)
7759 p->tokenbuf = ALLOC_N(char, 60);
7761 if (p->toksiz > 4096) {
7763 REALLOC_N(p->tokenbuf, char, 60);
7769tokspace(struct parser_params *p, int n)
7773 if (p->tokidx >= p->toksiz) {
7774 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7775 REALLOC_N(p->tokenbuf, char, p->toksiz);
7777 return &p->tokenbuf[p->tokidx-n];
7781tokadd(struct parser_params *p, int c)
7783 p->tokenbuf[p->tokidx++] = (char)c;
7784 if (p->tokidx >= p->toksiz) {
7786 REALLOC_N(p->tokenbuf, char, p->toksiz);
7791tok_hex(struct parser_params *p, size_t *numlen)
7795 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7797 yyerror0("invalid hex escape");
7798 dispatch_scan_event(p, tSTRING_CONTENT);
7801 p->lex.pcur += *numlen;
7805#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7808escaped_control_code(int c)
7834#define WARN_SPACE_CHAR(c, prefix) \
7835 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
7838tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7839 int regexp_literal, int wide)
7842 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7843 p->lex.pcur += numlen;
7844 if (p->lex.strterm == NULL ||
7845 strterm_is_heredoc(p->lex.strterm) ||
7846 (p->lex.strterm->u.literal.func != str_regexp)) {
7847 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7848 literal_flush(p, p->lex.pcur);
7849 yyerror0("invalid Unicode escape");
7850 return wide && numlen > 0;
7852 if (codepoint > 0x10ffff) {
7853 literal_flush(p, p->lex.pcur);
7854 yyerror0("invalid Unicode codepoint (too large)");
7857 if ((codepoint & 0xfffff800) == 0xd800) {
7858 literal_flush(p, p->lex.pcur);
7859 yyerror0("invalid Unicode codepoint");
7863 if (regexp_literal) {
7864 tokcopy(p, (int)numlen);
7866 else if (codepoint >= 0x80) {
7867 rb_encoding *utf8 = rb_utf8_encoding();
7868 if (*encp && utf8 != *encp) {
7869 YYLTYPE loc = RUBY_INIT_YYLLOC();
7870 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7871 parser_show_error_line(p, &loc);
7875 tokaddmbc(p, codepoint, *encp);
7878 tokadd(p, codepoint);
7883static int tokadd_mbchar(struct parser_params *p, int c);
7886tokskip_mbchar(struct parser_params *p)
7888 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7890 p->lex.pcur += len - 1;
7895/* return value is for ?\u3042 */
7897tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7898 int term, int symbol_literal, int regexp_literal)
7901 * If `term` is not -1, then we allow multiple codepoints in \u{}
7902 * upto `term` byte, otherwise we're parsing a character literal.
7903 * And then add the codepoints to the current token.
7905 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7907 const int open_brace = '{', close_brace = '}';
7909 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7911 if (peek(p, open_brace)) { /* handle \u{...} form */
7912 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
7914 * Skip parsing validation code and copy bytes as-is until term or
7915 * closing brace, in order to correctly handle extended regexps where
7916 * invalid unicode escapes are allowed in comments. The regexp parser
7917 * does its own validation and will catch any issues.
7919 tokadd(p, open_brace);
7920 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
7922 if (c == close_brace) {
7927 else if (c == term) {
7930 if (c == '\\' && !lex_eol_n_p(p, 1)) {
7934 tokadd_mbchar(p, c);
7938 const char *second = NULL;
7939 int c, last = nextc(p);
7940 if (lex_eol_p(p)) goto unterminated;
7941 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
7942 while (c != close_brace) {
7943 if (c == term) goto unterminated;
7944 if (second == multiple_codepoints)
7945 second = p->lex.pcur;
7946 if (regexp_literal) tokadd(p, last);
7947 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7950 while (ISSPACE(c = peekc(p))) {
7951 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
7954 if (term == -1 && !second)
7955 second = multiple_codepoints;
7958 if (c != close_brace) {
7961 yyerror0("unterminated Unicode escape");
7964 if (second && second != multiple_codepoints) {
7965 const char *pcur = p->lex.pcur;
7966 p->lex.pcur = second;
7967 dispatch_scan_event(p, tSTRING_CONTENT);
7970 yyerror0(multiple_codepoints);
7974 if (regexp_literal) tokadd(p, close_brace);
7978 else { /* handle \uxxxx form */
7979 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7986#define ESCAPE_CONTROL 1
7987#define ESCAPE_META 2
7990read_escape(struct parser_params *p, int flags)
7995 switch (c = nextc(p)) {
7996 case '\\': /* Backslash */
7999 case 'n': /* newline */
8002 case 't': /* horizontal tab */
8005 case 'r': /* carriage-return */
8008 case 'f': /* form-feed */
8011 case 'v': /* vertical tab */
8014 case 'a': /* alarm(bell) */
8017 case 'e': /* escape */
8020 case '0': case '1': case '2': case '3': /* octal constant */
8021 case '4': case '5': case '6': case '7':
8023 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8024 p->lex.pcur += numlen;
8027 case 'x': /* hex constant */
8028 c = tok_hex(p, &numlen);
8029 if (numlen == 0) return 0;
8032 case 'b': /* backspace */
8035 case 's': /* space */
8039 if (flags & ESCAPE_META) goto eof;
8040 if ((c = nextc(p)) != '-') {
8043 if ((c = nextc(p)) == '\\') {
8049 return read_escape(p, flags|ESCAPE_META) | 0x80;
8051 else if (c == -1 || !ISASCII(c)) goto eof;
8053 int c2 = escaped_control_code(c);
8055 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8056 WARN_SPACE_CHAR(c2, "\\M-");
8059 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8062 else if (ISCNTRL(c)) goto eof;
8063 return ((c & 0xff) | 0x80);
8067 if ((c = nextc(p)) != '-') {
8071 if (flags & ESCAPE_CONTROL) goto eof;
8072 if ((c = nextc(p))== '\\') {
8078 c = read_escape(p, flags|ESCAPE_CONTROL);
8082 else if (c == -1) goto eof;
8083 else if (!ISASCII(c)) {
8088 int c2 = escaped_control_code(c);
8091 if (flags & ESCAPE_META) {
8092 WARN_SPACE_CHAR(c2, "\\M-");
8095 WARN_SPACE_CHAR(c2, "");
8099 if (flags & ESCAPE_META) {
8100 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8103 WARN_SPACE_CHAR(c2, "\\C-");
8107 else if (ISCNTRL(c)) goto eof;
8113 yyerror0("Invalid escape character syntax");
8114 dispatch_scan_event(p, tSTRING_CONTENT);
8123tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8125 int len = rb_enc_codelen(c, enc);
8126 rb_enc_mbcput(c, tokspace(p, len), enc);
8130tokadd_escape(struct parser_params *p)
8135 switch (c = nextc(p)) {
8137 return 0; /* just ignore */
8139 case '0': case '1': case '2': case '3': /* octal constant */
8140 case '4': case '5': case '6': case '7':
8142 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8143 if (numlen == 0) goto eof;
8144 p->lex.pcur += numlen;
8145 tokcopy(p, (int)numlen + 1);
8149 case 'x': /* hex constant */
8151 tok_hex(p, &numlen);
8152 if (numlen == 0) return -1;
8153 tokcopy(p, (int)numlen + 2);
8159 yyerror0("Invalid escape character syntax");
8171regx_options(struct parser_params *p)
8179 while (c = nextc(p), ISALPHA(c)) {
8181 options |= RE_OPTION_ONCE;
8183 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
8185 if (kc != rb_ascii8bit_encindex()) kcode = c;
8199 YYLTYPE loc = RUBY_INIT_YYLLOC();
8201 compile_error(p, "unknown regexp option%s - %*s",
8202 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8203 parser_show_error_line(p, &loc);
8205 return options | RE_OPTION_ENCODING(kcode);
8209tokadd_mbchar(struct parser_params *p, int c)
8211 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8212 if (len < 0) return -1;
8214 p->lex.pcur += --len;
8215 if (len > 0) tokcopy(p, len);
8220simple_re_meta(int c)
8223 case '$': case '*': case '+': case '.':
8224 case '?': case '^': case '|':
8225 case ')': case ']': case '}': case '>':
8233parser_update_heredoc_indent(struct parser_params *p, int c)
8235 if (p->heredoc_line_indent == -1) {
8236 if (c == '\n') p->heredoc_line_indent = 0;
8240 p->heredoc_line_indent++;
8243 else if (c == '\t') {
8244 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8245 p->heredoc_line_indent = w * TAB_WIDTH;
8248 else if (c != '\n') {
8249 if (p->heredoc_indent > p->heredoc_line_indent) {
8250 p->heredoc_indent = p->heredoc_line_indent;
8252 p->heredoc_line_indent = -1;
8259parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8261 YYLTYPE loc = RUBY_INIT_YYLLOC();
8262 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8263 compile_error(p, "%s mixed within %s source", n1, n2);
8264 parser_show_error_line(p, &loc);
8268parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8270 const char *pos = p->lex.pcur;
8272 parser_mixed_error(p, enc1, enc2);
8277nibble_char_upper(unsigned int c)
8280 return c + (c < 10 ? '0' : 'A' - 10);
8284tokadd_string(struct parser_params *p,
8285 int func, int term, int paren, long *nest,
8286 rb_encoding **encp, rb_encoding **enc)
8291 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8292 int top_of_line = FALSE;
8295#define mixed_error(enc1, enc2) \
8296 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8297#define mixed_escape(beg, enc1, enc2) \
8298 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8300 while ((c = nextc(p)) != -1) {
8301 if (p->heredoc_indent > 0) {
8302 parser_update_heredoc_indent(p, c);
8305 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8311 if (paren && c == paren) {
8314 else if (c == term) {
8315 if (!nest || !*nest) {
8321 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8322 unsigned char c2 = *p->lex.pcur;
8323 if (c2 == '$' || c2 == '@' || c2 == '{') {
8328 else if (c == '\\') {
8332 if (func & STR_FUNC_QWORDS) break;
8333 if (func & STR_FUNC_EXPAND) {
8334 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8345 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8349 if ((func & STR_FUNC_EXPAND) == 0) {
8353 tokadd_utf8(p, enc, term,
8354 func & STR_FUNC_SYMBOL,
8355 func & STR_FUNC_REGEXP);
8359 if (c == -1) return -1;
8361 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8364 if (func & STR_FUNC_REGEXP) {
8370 c = read_escape(p, 0);
8372 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8375 *t++ = nibble_char_upper(c >> 4);
8376 *t++ = nibble_char_upper(c);
8381 if (c == term && !simple_re_meta(c)) {
8386 if ((c = tokadd_escape(p)) < 0)
8388 if (*enc && *enc != *encp) {
8389 mixed_escape(p->lex.ptok+2, *enc, *encp);
8393 else if (func & STR_FUNC_EXPAND) {
8395 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8396 c = read_escape(p, 0);
8398 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8399 /* ignore backslashed spaces in %w */
8401 else if (c != term && !(paren && c == paren)) {
8408 else if (!parser_isascii(p)) {
8413 else if (*enc != *encp) {
8414 mixed_error(*enc, *encp);
8417 if (tokadd_mbchar(p, c) == -1) return -1;
8420 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8428 else if (*enc != *encp) {
8429 mixed_error(*enc, *encp);
8435 top_of_line = (c == '\n');
8439 if (*enc) *encp = *enc;
8443#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8447flush_string_content(struct parser_params *p, rb_encoding *enc)
8449 VALUE content = yylval.val;
8450 if (!ripper_is_node_yylval(p, content))
8451 content = ripper_new_yylval(p, 0, 0, content);
8452 if (has_delayed_token(p)) {
8453 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8455 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8457 dispatch_delayed_token(p, tSTRING_CONTENT);
8458 p->lex.ptok = p->lex.pcur;
8459 RNODE_RIPPER(content)->nd_rval = yylval.val;
8461 dispatch_scan_event(p, tSTRING_CONTENT);
8462 if (yylval.val != content)
8463 RNODE_RIPPER(content)->nd_rval = yylval.val;
8464 yylval.val = content;
8468flush_string_content(struct parser_params *p, rb_encoding *enc)
8470 if (has_delayed_token(p)) {
8471 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8473 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8474 p->delayed.end_line = p->ruby_sourceline;
8475 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8477 dispatch_delayed_token(p, tSTRING_CONTENT);
8478 p->lex.ptok = p->lex.pcur;
8480 dispatch_scan_event(p, tSTRING_CONTENT);
8484RUBY_FUNC_EXPORTED const uint_least32_t ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
8485/* this can be shared with ripper, since it's independent from struct
8488#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8489#define SPECIAL_PUNCT(idx) ( \
8490 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8491 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8492 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8493 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8494 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8496const uint_least32_t ruby_global_name_punct_bits[] = {
8505static enum yytokentype
8506parser_peek_variable_name(struct parser_params *p)
8509 const char *ptr = p->lex.pcur;
8511 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8515 if ((c = *ptr) == '-') {
8516 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8519 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8520 return tSTRING_DVAR;
8524 if ((c = *ptr) == '@') {
8525 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8531 p->command_start = TRUE;
8532 return tSTRING_DBEG;
8536 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8537 return tSTRING_DVAR;
8541#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8542#define IS_END() IS_lex_state(EXPR_END_ANY)
8543#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8544#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8545#define IS_LABEL_POSSIBLE() (\
8546 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8548#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8549#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8551static inline enum yytokentype
8552parser_string_term(struct parser_params *p, int func)
8554 xfree(p->lex.strterm);
8556 if (func & STR_FUNC_REGEXP) {
8557 set_yylval_num(regx_options(p));
8558 dispatch_scan_event(p, tREGEXP_END);
8559 SET_LEX_STATE(EXPR_END);
8562 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8564 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8567 SET_LEX_STATE(EXPR_END);
8571static enum yytokentype
8572parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8574 int func = quote->func;
8575 int term = quote->term;
8576 int paren = quote->paren;
8578 rb_encoding *enc = p->enc;
8579 rb_encoding *base_enc = 0;
8582 if (func & STR_FUNC_TERM) {
8583 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8584 SET_LEX_STATE(EXPR_END);
8585 xfree(p->lex.strterm);
8587 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8590 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8591 while (c != '\n' && ISSPACE(c = nextc(p)));
8594 if (func & STR_FUNC_LIST) {
8595 quote->func &= ~STR_FUNC_LIST;
8598 if (c == term && !quote->nest) {
8599 if (func & STR_FUNC_QWORDS) {
8600 quote->func |= STR_FUNC_TERM;
8601 pushback(p, c); /* dispatch the term at tSTRING_END */
8602 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8605 return parser_string_term(p, func);
8608 if (!ISSPACE(c)) pushback(p, c);
8609 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8613 if ((func & STR_FUNC_EXPAND) && c == '#') {
8614 enum yytokentype t = parser_peek_variable_name(p);
8620 if (tokadd_string(p, func, term, paren, "e->nest,
8621 &enc, &base_enc) == -1) {
8624# define unterminated_literal(mesg) yyerror0(mesg)
8626# define unterminated_literal(mesg) compile_error(p, mesg)
8628 literal_flush(p, p->lex.pcur);
8629 if (func & STR_FUNC_QWORDS) {
8630 /* no content to add, bailing out here */
8631 unterminated_literal("unterminated list meets end of file");
8632 xfree(p->lex.strterm);
8636 if (func & STR_FUNC_REGEXP) {
8637 unterminated_literal("unterminated regexp meets end of file");
8640 unterminated_literal("unterminated string meets end of file");
8642 quote->func |= STR_FUNC_TERM;
8647 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8648 set_yylval_str(lit);
8649 flush_string_content(p, enc);
8651 return tSTRING_CONTENT;
8654static enum yytokentype
8655heredoc_identifier(struct parser_params *p)
8658 * term_len is length of `<<"END"` except `END`,
8659 * in this case term_len is 4 (<, <, " and ").
8661 long len, offset = p->lex.pcur - p->lex.pbeg;
8662 int c = nextc(p), term, func = 0, quote = 0;
8663 enum yytokentype token = tSTRING_BEG;
8668 func = STR_FUNC_INDENT;
8671 else if (c == '~') {
8673 func = STR_FUNC_INDENT;
8679 func |= str_squote; goto quoted;
8681 func |= str_dquote; goto quoted;
8683 token = tXSTRING_BEG;
8684 func |= str_xquote; goto quoted;
8691 while ((c = nextc(p)) != term) {
8692 if (c == -1 || c == '\r' || c == '\n') {
8693 yyerror0("unterminated here document identifier");
8700 if (!parser_is_identchar(p)) {
8702 if (func & STR_FUNC_INDENT) {
8703 pushback(p, indent > 0 ? '~' : '-');
8709 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8710 if (n < 0) return 0;
8712 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8717 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8718 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8719 yyerror0("too long here document identifier");
8720 dispatch_scan_event(p, tHEREDOC_BEG);
8723 p->lex.strterm = new_heredoc(p);
8724 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8725 here->offset = offset;
8726 here->sourceline = p->ruby_sourceline;
8727 here->length = (unsigned)len;
8728 here->quote = quote;
8730 here->lastline = p->lex.lastline;
8731 rb_ast_add_mark_object(p->ast, p->lex.lastline);
8734 p->heredoc_indent = indent;
8735 p->heredoc_line_indent = 0;
8740heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8743 rb_strterm_t *term = p->lex.strterm;
8746 line = here->lastline;
8747 p->lex.lastline = line;
8748 p->lex.pbeg = RSTRING_PTR(line);
8749 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8750 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8751 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8752 p->heredoc_end = p->ruby_sourceline;
8753 p->ruby_sourceline = (int)here->sourceline;
8754 if (p->eofp) p->lex.nextline = Qnil;
8757 rb_ast_delete_mark_object(p->ast, line);
8761dedent_string(struct parser_params *p, VALUE string, int width)
8767 RSTRING_GETMEM(string, str, len);
8768 for (i = 0; i < len && col < width; i++) {
8769 if (str[i] == ' ') {
8772 else if (str[i] == '\t') {
8773 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8774 if (n > width) break;
8782 rb_str_modify(string);
8783 str = RSTRING_PTR(string);
8784 if (RSTRING_LEN(string) != len)
8785 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8786 MEMMOVE(str, str + i, char, len - i);
8787 rb_str_set_len(string, len - i);
8793heredoc_dedent(struct parser_params *p, NODE *root)
8795 NODE *node, *str_node, *prev_node;
8796 int indent = p->heredoc_indent;
8799 if (indent <= 0) return root;
8800 p->heredoc_indent = 0;
8801 if (!root) return root;
8803 prev_node = node = str_node = root;
8804 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8807 VALUE lit = RNODE_LIT(str_node)->nd_lit;
8808 if (nd_fl_newline(str_node)) {
8809 dedent_string(p, lit, indent);
8814 else if (!literal_concat0(p, prev_lit, lit)) {
8818 NODE *end = RNODE_LIST(node)->as.nd_end;
8819 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8821 if (nd_type_p(prev_node, NODE_DSTR))
8822 nd_set_type(prev_node, NODE_STR);
8825 RNODE_LIST(node)->as.nd_end = end;
8830 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
8832 if (!nd_type_p(node, NODE_LIST)) break;
8833 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
8834 enum node_type type = nd_type(str_node);
8835 if (type == NODE_STR || type == NODE_DSTR) break;
8845heredoc_dedent(struct parser_params *p, VALUE array)
8847 int indent = p->heredoc_indent;
8849 if (indent <= 0) return array;
8850 p->heredoc_indent = 0;
8851 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8857whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8859 const char *beg = p->lex.pbeg;
8860 const char *ptr = p->lex.pend;
8862 if (ptr - beg < len) return FALSE;
8863 if (ptr > beg && ptr[-1] == '\n') {
8864 if (--ptr > beg && ptr[-1] == '\r') --ptr;
8865 if (ptr - beg < len) return FALSE;
8867 if (strncmp(eos, ptr -= len, len)) return FALSE;
8869 while (beg < ptr && ISSPACE(*beg)) beg++;
8875word_match_p(struct parser_params *p, const char *word, long len)
8877 if (strncmp(p->lex.pcur, word, len)) return 0;
8878 if (lex_eol_n_p(p, len)) return 1;
8879 int c = (unsigned char)p->lex.pcur[len];
8880 if (ISSPACE(c)) return 1;
8882 case '\0': case '\004': case '\032': return 1;
8887#define NUM_SUFFIX_R (1<<0)
8888#define NUM_SUFFIX_I (1<<1)
8889#define NUM_SUFFIX_ALL 3
8892number_literal_suffix(struct parser_params *p, int mask)
8895 const char *lastp = p->lex.pcur;
8897 while ((c = nextc(p)) != -1) {
8898 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8899 result |= (mask & NUM_SUFFIX_I);
8900 mask &= ~NUM_SUFFIX_I;
8901 /* r after i, rational of complex is disallowed */
8902 mask &= ~NUM_SUFFIX_R;
8905 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8906 result |= (mask & NUM_SUFFIX_R);
8907 mask &= ~NUM_SUFFIX_R;
8910 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8911 p->lex.pcur = lastp;
8912 literal_flush(p, p->lex.pcur);
8921static enum yytokentype
8922set_number_literal(struct parser_params *p, VALUE v,
8923 enum yytokentype type, int suffix)
8925 if (suffix & NUM_SUFFIX_I) {
8926 v = rb_complex_raw(INT2FIX(0), v);
8929 set_yylval_literal(v);
8930 SET_LEX_STATE(EXPR_END);
8934static enum yytokentype
8935set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8937 enum yytokentype type = tINTEGER;
8938 if (suffix & NUM_SUFFIX_R) {
8939 v = rb_rational_raw1(v);
8942 return set_number_literal(p, v, type, suffix);
8947dispatch_heredoc_end(struct parser_params *p)
8950 if (has_delayed_token(p))
8951 dispatch_delayed_token(p, tSTRING_CONTENT);
8952 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8953 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8954 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8960#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8962parser_dispatch_heredoc_end(struct parser_params *p, int line)
8964 if (has_delayed_token(p))
8965 dispatch_delayed_token(p, tSTRING_CONTENT);
8967 if (p->keep_tokens) {
8968 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8969 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8970 parser_append_tokens(p, str, tHEREDOC_END, line);
8973 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8979static enum yytokentype
8980here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8982 int c, func, indent = 0;
8983 const char *eos, *ptr, *ptr_end;
8986 rb_encoding *enc = p->enc;
8987 rb_encoding *base_enc = 0;
8990 eos = RSTRING_PTR(here->lastline) + here->offset;
8992 indent = (func = here->func) & STR_FUNC_INDENT;
8994 if ((c = nextc(p)) == -1) {
8997 if (!has_delayed_token(p)) {
8998 dispatch_scan_event(p, tSTRING_CONTENT);
9001 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9002 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
9003 int cr = ENC_CODERANGE_UNKNOWN;
9004 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9005 if (cr != ENC_CODERANGE_7BIT &&
9006 rb_is_usascii_enc(p->enc) &&
9007 enc != rb_utf8_encoding()) {
9008 enc = rb_ascii8bit_encoding();
9011 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
9013 dispatch_delayed_token(p, tSTRING_CONTENT);
9017 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9018 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9021 SET_LEX_STATE(EXPR_END);
9026 /* not beginning of line, cannot be the terminator */
9028 else if (p->heredoc_line_indent == -1) {
9029 /* `heredoc_line_indent == -1` means
9030 * - "after an interpolation in the same line", or
9031 * - "in a continuing line"
9033 p->heredoc_line_indent = 0;
9035 else if (whole_match_p(p, eos, len, indent)) {
9036 dispatch_heredoc_end(p);
9038 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9040 SET_LEX_STATE(EXPR_END);
9044 if (!(func & STR_FUNC_EXPAND)) {
9046 ptr = RSTRING_PTR(p->lex.lastline);
9047 ptr_end = p->lex.pend;
9048 if (ptr_end > ptr) {
9049 switch (ptr_end[-1]) {
9051 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9060 if (p->heredoc_indent > 0) {
9062 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9064 p->heredoc_line_indent = 0;
9068 rb_str_cat(str, ptr, ptr_end - ptr);
9070 str = STR_NEW(ptr, ptr_end - ptr);
9071 if (!lex_eol_ptr_p(p, ptr_end)) rb_str_cat(str, "\n", 1);
9073 if (p->heredoc_indent > 0) {
9076 if (nextc(p) == -1) {
9082 } while (!whole_match_p(p, eos, len, indent));
9085 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9088 enum yytokentype t = parser_peek_variable_name(p);
9089 if (p->heredoc_line_indent != -1) {
9090 if (p->heredoc_indent > p->heredoc_line_indent) {
9091 p->heredoc_indent = p->heredoc_line_indent;
9093 p->heredoc_line_indent = -1;
9102 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9103 if (p->eofp) goto error;
9107 if (c == '\\') p->heredoc_line_indent = -1;
9109 str = STR_NEW3(tok(p), toklen(p), enc, func);
9111 set_yylval_str(str);
9113 if (bol) nd_set_fl_newline(yylval.node);
9115 flush_string_content(p, enc);
9116 return tSTRING_CONTENT;
9118 tokadd(p, nextc(p));
9119 if (p->heredoc_indent > 0) {
9123 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9124 if ((c = nextc(p)) == -1) goto error;
9125 } while (!whole_match_p(p, eos, len, indent));
9126 str = STR_NEW3(tok(p), toklen(p), enc, func);
9128 dispatch_heredoc_end(p);
9130 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
9133 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9135 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9136 set_yylval_str(str);
9138 if (bol) nd_set_fl_newline(yylval.node);
9140 return tSTRING_CONTENT;
9146arg_ambiguous(struct parser_params *p, char c)
9150 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
9153 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
9156 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9163formal_argument(struct parser_params *p, ID lhs)
9165formal_argument(struct parser_params *p, VALUE lhs)
9168 ID id = get_id(lhs);
9170 switch (id_type(id)) {
9174# define ERR(mesg) yyerror0(mesg)
9176# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
9179 ERR("formal argument cannot be a constant");
9182 ERR("formal argument cannot be an instance variable");
9185 ERR("formal argument cannot be a global variable");
9188 ERR("formal argument cannot be a class variable");
9191 ERR("formal argument must be local variable");
9195 shadowing_lvar(p, id);
9200lvar_defined(struct parser_params *p, ID id)
9202 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9205/* emacsen -*- hack */
9207parser_encode_length(struct parser_params *p, const char *name, long len)
9211 if (len > 5 && name[nlen = len - 5] == '-') {
9212 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9215 if (len > 4 && name[nlen = len - 4] == '-') {
9216 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9218 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9219 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9220 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9227parser_set_encode(struct parser_params *p, const char *name)
9229 int idx = rb_enc_find_index(name);
9234 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9236 excargs[0] = rb_eArgError;
9237 excargs[2] = rb_make_backtrace();
9238 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9239 rb_exc_raise(rb_make_exception(3, excargs));
9241 enc = rb_enc_from_index(idx);
9242 if (!rb_enc_asciicompat(enc)) {
9243 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9248 if (p->debug_lines) {
9249 VALUE lines = p->debug_lines;
9250 long i, n = RARRAY_LEN(lines);
9251 for (i = 0; i < n; ++i) {
9252 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
9259comment_at_top(struct parser_params *p)
9261 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
9262 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
9263 while (ptr < ptr_end) {
9264 if (!ISSPACE(*ptr)) return 0;
9270typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9271typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9273static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9276magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9278 if (!comment_at_top(p)) {
9281 parser_set_encode(p, val);
9285parser_get_bool(struct parser_params *p, const char *name, const char *val)
9289 if (STRCASECMP(val, "true") == 0) {
9294 if (STRCASECMP(val, "false") == 0) {
9299 return parser_invalid_pragma_value(p, name, val);
9303parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9305 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9310parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9312 int b = parser_get_bool(p, name, val);
9313 if (b >= 0) p->token_info_enabled = b;
9317parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9321 if (p->token_seen) {
9322 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
9326 b = parser_get_bool(p, name, val);
9329 p->frozen_string_literal = b;
9333parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9335 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9336 if (*s == ' ' || *s == '\t') continue;
9337 if (*s == '#') break;
9338 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
9344 if (STRCASECMP(val, "none") == 0) {
9345 p->ctxt.shareable_constant_value = shareable_none;
9350 if (STRCASECMP(val, "literal") == 0) {
9351 p->ctxt.shareable_constant_value = shareable_literal;
9356 if (STRCASECMP(val, "experimental_copy") == 0) {
9357 p->ctxt.shareable_constant_value = shareable_copy;
9360 if (STRCASECMP(val, "experimental_everything") == 0) {
9361 p->ctxt.shareable_constant_value = shareable_everything;
9366 parser_invalid_pragma_value(p, name, val);
9371parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9373 int b = parser_get_bool(p, name, val);
9374 if (b >= 0) p->past_scope_enabled = b;
9378struct magic_comment {
9380 rb_magic_comment_setter_t func;
9381 rb_magic_comment_length_t length;
9384static const struct magic_comment magic_comments[] = {
9385 {"coding", magic_comment_encoding, parser_encode_length},
9386 {"encoding", magic_comment_encoding, parser_encode_length},
9387 {"frozen_string_literal", parser_set_frozen_string_literal},
9388 {"shareable_constant_value", parser_set_shareable_constant_value},
9389 {"warn_indent", parser_set_token_info},
9391 {"warn_past_scope", parser_set_past_scope},
9396magic_comment_marker(const char *str, long len)
9403 if (str[i-1] == '*' && str[i-2] == '-') {
9409 if (i + 1 >= len) return 0;
9410 if (str[i+1] != '-') {
9413 else if (str[i-1] != '-') {
9429parser_magic_comment(struct parser_params *p, const char *str, long len)
9432 VALUE name = 0, val = 0;
9433 const char *beg, *end, *vbeg, *vend;
9434#define str_copy(_s, _p, _n) ((_s) \
9435 ? (void)(rb_str_resize((_s), (_n)), \
9436 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9437 : (void)((_s) = STR_NEW((_p), (_n))))
9439 if (len <= 7) return FALSE;
9440 if (!!(beg = magic_comment_marker(str, len))) {
9441 if (!(end = magic_comment_marker(beg, str + len - beg)))
9445 len = end - beg - 3;
9448 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9450 const struct magic_comment *mc = magic_comments;
9455 for (; len > 0 && *str; str++, --len) {
9457 case '\'': case '"': case ':': case ';':
9460 if (!ISSPACE(*str)) break;
9462 for (beg = str; len > 0; str++, --len) {
9464 case '\'': case '"': case ':': case ';':
9467 if (ISSPACE(*str)) break;
9472 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9475 if (!indicator) return FALSE;
9479 do str++; while (--len > 0 && ISSPACE(*str));
9482 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9495 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9499 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9502 while (len > 0 && (ISSPACE(*str))) --len, str++;
9503 if (len) return FALSE;
9507 str_copy(name, beg, n);
9508 s = RSTRING_PTR(name);
9509 for (i = 0; i < n; ++i) {
9510 if (s[i] == '-') s[i] = '_';
9513 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9516 n = (*mc->length)(p, vbeg, n);
9518 str_copy(val, vbeg, n);
9519 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9522 } while (++mc < magic_comments + numberof(magic_comments));
9524 str_copy(val, vbeg, vend - vbeg);
9525 dispatch2(magic_comment, name, val);
9533set_file_encoding(struct parser_params *p, const char *str, const char *send)
9536 const char *beg = str;
9540 if (send - str <= 6) return;
9542 case 'C': case 'c': str += 6; continue;
9543 case 'O': case 'o': str += 5; continue;
9544 case 'D': case 'd': str += 4; continue;
9545 case 'I': case 'i': str += 3; continue;
9546 case 'N': case 'n': str += 2; continue;
9547 case 'G': case 'g': str += 1; continue;
9554 if (ISSPACE(*str)) break;
9557 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9562 if (++str >= send) return;
9563 } while (ISSPACE(*str));
9565 if (*str != '=' && *str != ':') return;
9570 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9571 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9572 parser_set_encode(p, RSTRING_PTR(s));
9573 rb_str_resize(s, 0);
9577parser_prepare(struct parser_params *p)
9579 int c = nextc0(p, FALSE);
9580 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9583 if (peek(p, '!')) p->has_shebang = 1;
9585 case 0xef: /* UTF-8 BOM marker */
9586 if (!lex_eol_n_p(p, 2) &&
9587 (unsigned char)p->lex.pcur[0] == 0xbb &&
9588 (unsigned char)p->lex.pcur[1] == 0xbf) {
9589 p->enc = rb_utf8_encoding();
9592 if (p->debug_lines) {
9593 rb_enc_associate(p->lex.lastline, p->enc);
9596 p->lex.pbeg = p->lex.pcur;
9605 p->enc = rb_enc_get(p->lex.lastline);
9609#define ambiguous_operator(tok, op, syn) ( \
9610 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
9611 rb_warning0("even though it seems like "syn""))
9613#define ambiguous_operator(tok, op, syn) \
9614 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9616#define warn_balanced(tok, op, syn) ((void) \
9617 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9618 space_seen && !ISSPACE(c) && \
9619 (ambiguous_operator(tok, op, syn), 0)), \
9620 (enum yytokentype)(tok))
9623parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9626 char *point = &str[seen_point];
9627 size_t fraclen = len-seen_point-1;
9628 memmove(point, point+1, fraclen+1);
9629 v = rb_cstr_to_inum(str, 10, FALSE);
9630 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9633static enum yytokentype
9634no_digits(struct parser_params *p)
9636 yyerror0("numeric literal without digits");
9637 if (peek(p, '_')) nextc(p);
9638 /* dummy 0, for tUMINUS_NUM at numeric */
9639 return set_integer_literal(p, INT2FIX(0), 0);
9642static enum yytokentype
9643parse_numeric(struct parser_params *p, int c)
9645 int is_float, seen_point, seen_e, nondigit;
9648 is_float = seen_point = seen_e = nondigit = 0;
9649 SET_LEX_STATE(EXPR_END);
9651 if (c == '-' || c == '+') {
9656 int start = toklen(p);
9658 if (c == 'x' || c == 'X') {
9661 if (c != -1 && ISXDIGIT(c)) {
9664 if (nondigit) break;
9668 if (!ISXDIGIT(c)) break;
9671 } while ((c = nextc(p)) != -1);
9675 if (toklen(p) == start) {
9676 return no_digits(p);
9678 else if (nondigit) goto trailing_uc;
9679 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9680 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9682 if (c == 'b' || c == 'B') {
9685 if (c == '0' || c == '1') {
9688 if (nondigit) break;
9692 if (c != '0' && c != '1') break;
9695 } while ((c = nextc(p)) != -1);
9699 if (toklen(p) == start) {
9700 return no_digits(p);
9702 else if (nondigit) goto trailing_uc;
9703 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9704 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9706 if (c == 'd' || c == 'D') {
9709 if (c != -1 && ISDIGIT(c)) {
9712 if (nondigit) break;
9716 if (!ISDIGIT(c)) break;
9719 } while ((c = nextc(p)) != -1);
9723 if (toklen(p) == start) {
9724 return no_digits(p);
9726 else if (nondigit) goto trailing_uc;
9727 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9728 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9734 if (c == 'o' || c == 'O') {
9735 /* prefixed octal */
9737 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9738 return no_digits(p);
9741 if (c >= '0' && c <= '7') {
9746 if (nondigit) break;
9750 if (c < '0' || c > '9') break;
9751 if (c > '7') goto invalid_octal;
9754 } while ((c = nextc(p)) != -1);
9755 if (toklen(p) > start) {
9758 if (nondigit) goto trailing_uc;
9759 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9760 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9767 if (c > '7' && c <= '9') {
9769 yyerror0("Invalid octal digit");
9771 else if (c == '.' || c == 'e' || c == 'E') {
9776 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9777 return set_integer_literal(p, INT2FIX(0), suffix);
9783 case '0': case '1': case '2': case '3': case '4':
9784 case '5': case '6': case '7': case '8': case '9':
9790 if (nondigit) goto trailing_uc;
9791 if (seen_point || seen_e) {
9796 if (c0 == -1 || !ISDIGIT(c0)) {
9802 seen_point = toklen(p);
9821 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9827 tokadd(p, nondigit);
9831 nondigit = (c == '-' || c == '+') ? c : 0;
9834 case '_': /* `_' in number just ignored */
9835 if (nondigit) goto decode_num;
9849 literal_flush(p, p->lex.pcur - 1);
9850 YYLTYPE loc = RUBY_INIT_YYLLOC();
9851 compile_error(p, "trailing `%c' in number", nondigit);
9852 parser_show_error_line(p, &loc);
9856 enum yytokentype type = tFLOAT;
9859 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9860 if (suffix & NUM_SUFFIX_R) {
9862 v = parse_rational(p, tok(p), toklen(p), seen_point);
9865 double d = strtod(tok(p), 0);
9866 if (errno == ERANGE) {
9867 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9872 return set_number_literal(p, v, type, suffix);
9874 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9875 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9878static enum yytokentype
9879parse_qmark(struct parser_params *p, int space_seen)
9886 SET_LEX_STATE(EXPR_VALUE);
9891 compile_error(p, "incomplete character syntax");
9894 if (rb_enc_isspace(c, p->enc)) {
9896 int c2 = escaped_control_code(c);
9898 WARN_SPACE_CHAR(c2, "?");
9903 SET_LEX_STATE(EXPR_VALUE);
9908 if (!parser_isascii(p)) {
9909 if (tokadd_mbchar(p, c) == -1) return 0;
9911 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9912 !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
9914 const char *start = p->lex.pcur - 1, *ptr = start;
9916 int n = parser_precise_mbclen(p, ptr);
9917 if (n < 0) return -1;
9919 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
9920 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9921 " a conditional operator, put a space after `?'",
9922 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9926 else if (c == '\\') {
9929 enc = rb_utf8_encoding();
9930 tokadd_utf8(p, &enc, -1, 0, 0);
9932 else if (!ISASCII(c = peekc(p))) {
9934 if (tokadd_mbchar(p, c) == -1) return 0;
9937 c = read_escape(p, 0);
9945 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9946 set_yylval_str(lit);
9947 SET_LEX_STATE(EXPR_END);
9951static enum yytokentype
9952parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9955 const char *ptok = p->lex.pcur;
9963 if (c == -1) goto unterminated;
9966 if (!ISASCII(c)) goto unknown;
9971 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9974 c = parser_precise_mbclen(p, p->lex.pcur);
9975 if (c < 0) return 0;
9977 yyerror0("unknown type of %string");
9983 compile_error(p, "unterminated quoted string meets end of file");
9987 if (term == '(') term = ')';
9988 else if (term == '[') term = ']';
9989 else if (term == '{') term = '}';
9990 else if (term == '<') term = '>';
9993 p->lex.ptok = ptok-1;
9996 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10000 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10001 return tSTRING_BEG;
10004 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10008 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10009 return tQWORDS_BEG;
10012 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10013 return tSYMBOLS_BEG;
10016 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10017 return tQSYMBOLS_BEG;
10020 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10021 return tXSTRING_BEG;
10024 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10025 return tREGEXP_BEG;
10028 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10029 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10033 yyerror0("unknown type of %string");
10037 if ((c = nextc(p)) == '=') {
10038 set_yylval_id('%');
10039 SET_LEX_STATE(EXPR_BEG);
10042 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10045 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10047 return warn_balanced('%', "%%", "string literal");
10051tokadd_ident(struct parser_params *p, int c)
10054 if (tokadd_mbchar(p, c) == -1) return -1;
10056 } while (parser_is_identchar(p));
10062tokenize_ident(struct parser_params *p)
10064 ID ident = TOK_INTERN();
10066 set_yylval_name(ident);
10072parse_numvar(struct parser_params *p)
10076 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10077 const unsigned long nth_ref_max =
10078 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10079 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10080 * turned into a Fixnum, in compile.c */
10082 if (overflow || n > nth_ref_max) {
10083 /* compile_error()? */
10084 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10085 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10092static enum yytokentype
10093parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10095 const char *ptr = p->lex.pcur;
10098 SET_LEX_STATE(EXPR_END);
10099 p->lex.ptok = ptr - 1; /* from '$' */
10103 case '_': /* $_: last read line string */
10105 if (parser_is_identchar(p)) {
10113 case '~': /* $~: match-data */
10114 case '*': /* $*: argv */
10115 case '$': /* $$: pid */
10116 case '?': /* $?: last status */
10117 case '!': /* $!: error string */
10118 case '@': /* $@: error position */
10119 case '/': /* $/: input record separator */
10120 case '\\': /* $\: output record separator */
10121 case ';': /* $;: field separator */
10122 case ',': /* $,: output field separator */
10123 case '.': /* $.: last read line number */
10124 case '=': /* $=: ignorecase */
10125 case ':': /* $:: load path */
10126 case '<': /* $<: reading filename */
10127 case '>': /* $>: default output handle */
10128 case '\"': /* $": already loaded files */
10137 if (parser_is_identchar(p)) {
10138 if (tokadd_mbchar(p, c) == -1) return 0;
10146 set_yylval_name(TOK_INTERN());
10149 case '&': /* $&: last match */
10150 case '`': /* $`: string before last match */
10151 case '\'': /* $': string after last match */
10152 case '+': /* $+: string matches last paren. */
10153 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10158 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10161 case '1': case '2': case '3':
10162 case '4': case '5': case '6':
10163 case '7': case '8': case '9':
10168 } while (c != -1 && ISDIGIT(c));
10170 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10172 c = parse_numvar(p);
10173 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10177 if (!parser_is_identchar(p)) {
10178 YYLTYPE loc = RUBY_INIT_YYLLOC();
10179 if (c == -1 || ISSPACE(c)) {
10180 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
10184 compile_error(p, "`$%c' is not allowed as a global variable name", c);
10186 parser_show_error_line(p, &loc);
10187 set_yylval_noname();
10195 if (tokadd_ident(p, c)) return 0;
10196 SET_LEX_STATE(EXPR_END);
10197 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10201 compile_error(p, "`%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10202 set_yylval_noname();
10209parser_numbered_param(struct parser_params *p, int n)
10211 if (n < 0) return false;
10213 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10216 if (p->max_numparam == ORDINAL_PARAM) {
10217 compile_error(p, "ordinary parameter is defined");
10220 struct vtable *args = p->lvtbl->args;
10221 if (p->max_numparam < n) {
10222 p->max_numparam = n;
10224 while (n > args->pos) {
10225 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10231static enum yytokentype
10232parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10234 const char *ptr = p->lex.pcur;
10235 enum yytokentype result = tIVAR;
10236 register int c = nextc(p);
10239 p->lex.ptok = ptr - 1; /* from '@' */
10247 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10248 if (c == -1 || !parser_is_identchar(p)) {
10250 RUBY_SET_YYLLOC(loc);
10251 if (result == tIVAR) {
10252 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
10255 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
10257 parser_show_error_line(p, &loc);
10258 set_yylval_noname();
10259 SET_LEX_STATE(EXPR_END);
10262 else if (ISDIGIT(c)) {
10264 RUBY_SET_YYLLOC(loc);
10265 if (result == tIVAR) {
10266 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
10269 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
10271 parser_show_error_line(p, &loc);
10272 set_yylval_noname();
10273 SET_LEX_STATE(EXPR_END);
10277 if (tokadd_ident(p, c)) return 0;
10282static enum yytokentype
10283parse_ident(struct parser_params *p, int c, int cmd_state)
10285 enum yytokentype result;
10286 int mb = ENC_CODERANGE_7BIT;
10287 const enum lex_state_e last_state = p->lex.state;
10289 int enforce_keyword_end = 0;
10292 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
10293 if (tokadd_mbchar(p, c) == -1) return 0;
10295 } while (parser_is_identchar(p));
10296 if ((c == '!' || c == '?') && !peek(p, '=')) {
10300 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10301 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10302 result = tIDENTIFIER;
10306 result = tCONSTANT; /* assume provisionally */
10311 if (IS_LABEL_POSSIBLE()) {
10312 if (IS_LABEL_SUFFIX(0)) {
10313 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10315 set_yylval_name(TOK_INTERN());
10321 if (!NIL_P(peek_end_expect_token_locations(p))) {
10323 int lineno, column;
10324 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10326 end_loc = peek_end_expect_token_locations(p);
10327 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
10328 column = NUM2INT(rb_ary_entry(end_loc, 1));
10331 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10332 p->ruby_sourceline, beg_pos, lineno, column);
10335 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10336 const struct kwtable *kw;
10338 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10339 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10340 enforce_keyword_end = 1;
10346 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10347 const struct kwtable *kw;
10349 /* See if it is a reserved word. */
10350 kw = rb_reserved_word(tok(p), toklen(p));
10352 enum lex_state_e state = p->lex.state;
10353 if (IS_lex_state_for(state, EXPR_FNAME)) {
10354 SET_LEX_STATE(EXPR_ENDFN);
10355 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10358 SET_LEX_STATE(kw->state);
10359 if (IS_lex_state(EXPR_BEG)) {
10360 p->command_start = TRUE;
10362 if (kw->id[0] == keyword_do) {
10363 if (lambda_beginning_p()) {
10364 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10365 return keyword_do_LAMBDA;
10367 if (COND_P()) return keyword_do_cond;
10368 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10369 return keyword_do_block;
10372 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10375 if (kw->id[0] != kw->id[1])
10376 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10382 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10384 SET_LEX_STATE(EXPR_CMDARG);
10387 SET_LEX_STATE(EXPR_ARG);
10390 else if (p->lex.state == EXPR_FNAME) {
10391 SET_LEX_STATE(EXPR_ENDFN);
10394 SET_LEX_STATE(EXPR_END);
10397 ident = tokenize_ident(p);
10398 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10399 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10400 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10401 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10402 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10408warn_cr(struct parser_params *p)
10412 /* carried over with p->lex.nextline for nextc() */
10413 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10417static enum yytokentype
10418parser_yylex(struct parser_params *p)
10421 int space_seen = 0;
10424 enum lex_state_e last_state;
10425 int fallthru = FALSE;
10426 int token_seen = p->token_seen;
10428 if (p->lex.strterm) {
10429 if (strterm_is_heredoc(p->lex.strterm)) {
10431 return here_document(p, &p->lex.strterm->u.heredoc);
10435 return parse_string(p, &p->lex.strterm->u.literal);
10438 cmd_state = p->command_start;
10439 p->command_start = FALSE;
10440 p->token_seen = TRUE;
10445 last_state = p->lex.state;
10446 switch (c = nextc(p)) {
10447 case '\0': /* NUL */
10448 case '\004': /* ^D */
10449 case '\032': /* ^Z */
10450 case -1: /* end of script. */
10453 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
10454 pop_end_expect_token_locations(p);
10455 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10459 /* Set location for end-of-input because dispatch_scan_event is not called. */
10460 RUBY_SET_YYLLOC(*p->yylloc);
10461 return END_OF_INPUT;
10467 case ' ': case '\t': case '\f':
10468 case '\13': /* '\v' */
10470 while ((c = nextc(p))) {
10475 case ' ': case '\t': case '\f':
10476 case '\13': /* '\v' */
10484 dispatch_scan_event(p, tSP);
10490 case '#': /* it's a comment */
10491 p->token_seen = token_seen;
10492 /* no magic_comment in shebang line */
10493 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10494 if (comment_at_top(p)) {
10495 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10499 dispatch_scan_event(p, tCOMMENT);
10503 p->token_seen = token_seen;
10504 VALUE prevline = p->lex.lastline;
10505 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10506 !IS_lex_state(EXPR_LABELED));
10507 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10509 dispatch_scan_event(p, tIGNORED_NL);
10512 if (!c && p->ctxt.in_kwarg) {
10513 goto normal_newline;
10518 switch (c = nextc(p)) {
10519 case ' ': case '\t': case '\f': case '\r':
10520 case '\13': /* '\v' */
10526 dispatch_scan_event(p, tSP);
10532 dispatch_delayed_token(p, tIGNORED_NL);
10533 if (peek(p, '.') == (c == '&')) {
10535 dispatch_scan_event(p, tSP);
10540 p->ruby_sourceline--;
10541 p->lex.nextline = p->lex.lastline;
10542 set_lastline(p, prevline);
10543 case -1: /* EOF no decrement*/
10547 RUBY_SET_YYLLOC(*p->yylloc);
10549 goto normal_newline;
10553 p->command_start = TRUE;
10554 SET_LEX_STATE(EXPR_BEG);
10558 if ((c = nextc(p)) == '*') {
10559 if ((c = nextc(p)) == '=') {
10560 set_yylval_id(idPow);
10561 SET_LEX_STATE(EXPR_BEG);
10565 if (IS_SPCARG(c)) {
10566 rb_warning0("`**' interpreted as argument prefix");
10569 else if (IS_BEG()) {
10573 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10578 set_yylval_id('*');
10579 SET_LEX_STATE(EXPR_BEG);
10583 if (IS_SPCARG(c)) {
10584 rb_warning0("`*' interpreted as argument prefix");
10587 else if (IS_BEG()) {
10591 c = warn_balanced('*', "*", "argument prefix");
10594 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10599 if (IS_AFTER_OPERATOR()) {
10600 SET_LEX_STATE(EXPR_ARG);
10606 SET_LEX_STATE(EXPR_BEG);
10619 /* skip embedded rd document */
10620 if (word_match_p(p, "begin", 5)) {
10621 int first_p = TRUE;
10624 dispatch_scan_event(p, tEMBDOC_BEG);
10628 dispatch_scan_event(p, tEMBDOC);
10633 compile_error(p, "embedded document meets end of file");
10634 return END_OF_INPUT;
10636 if (c == '=' && word_match_p(p, "end", 3)) {
10642 dispatch_scan_event(p, tEMBDOC_END);
10647 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10648 if ((c = nextc(p)) == '=') {
10649 if ((c = nextc(p)) == '=') {
10658 else if (c == '>') {
10667 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10669 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10670 enum yytokentype token = heredoc_identifier(p);
10671 if (token) return token < 0 ? 0 : token;
10673 if (IS_AFTER_OPERATOR()) {
10674 SET_LEX_STATE(EXPR_ARG);
10677 if (IS_lex_state(EXPR_CLASS))
10678 p->command_start = TRUE;
10679 SET_LEX_STATE(EXPR_BEG);
10682 if ((c = nextc(p)) == '>') {
10689 if ((c = nextc(p)) == '=') {
10690 set_yylval_id(idLTLT);
10691 SET_LEX_STATE(EXPR_BEG);
10695 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10701 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10702 if ((c = nextc(p)) == '=') {
10706 if ((c = nextc(p)) == '=') {
10707 set_yylval_id(idGTGT);
10708 SET_LEX_STATE(EXPR_BEG);
10718 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10719 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10720 p->lex.ptok = p->lex.pcur-1;
10721 return tSTRING_BEG;
10724 if (IS_lex_state(EXPR_FNAME)) {
10725 SET_LEX_STATE(EXPR_ENDFN);
10728 if (IS_lex_state(EXPR_DOT)) {
10730 SET_LEX_STATE(EXPR_CMDARG);
10732 SET_LEX_STATE(EXPR_ARG);
10735 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10736 return tXSTRING_BEG;
10739 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10740 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10741 p->lex.ptok = p->lex.pcur-1;
10742 return tSTRING_BEG;
10745 return parse_qmark(p, space_seen);
10748 if ((c = nextc(p)) == '&') {
10749 SET_LEX_STATE(EXPR_BEG);
10750 if ((c = nextc(p)) == '=') {
10751 set_yylval_id(idANDOP);
10752 SET_LEX_STATE(EXPR_BEG);
10758 else if (c == '=') {
10759 set_yylval_id('&');
10760 SET_LEX_STATE(EXPR_BEG);
10763 else if (c == '.') {
10764 set_yylval_id(idANDDOT);
10765 SET_LEX_STATE(EXPR_DOT);
10769 if (IS_SPCARG(c)) {
10771 (c = peekc_n(p, 1)) == -1 ||
10772 !(c == '\'' || c == '"' ||
10773 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10774 rb_warning0("`&' interpreted as argument prefix");
10778 else if (IS_BEG()) {
10782 c = warn_balanced('&', "&", "argument prefix");
10784 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10788 if ((c = nextc(p)) == '|') {
10789 SET_LEX_STATE(EXPR_BEG);
10790 if ((c = nextc(p)) == '=') {
10791 set_yylval_id(idOROP);
10792 SET_LEX_STATE(EXPR_BEG);
10796 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10804 set_yylval_id('|');
10805 SET_LEX_STATE(EXPR_BEG);
10808 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10814 if (IS_AFTER_OPERATOR()) {
10815 SET_LEX_STATE(EXPR_ARG);
10823 set_yylval_id('+');
10824 SET_LEX_STATE(EXPR_BEG);
10827 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10828 SET_LEX_STATE(EXPR_BEG);
10830 if (c != -1 && ISDIGIT(c)) {
10831 return parse_numeric(p, '+');
10835 SET_LEX_STATE(EXPR_BEG);
10837 return warn_balanced('+', "+", "unary operator");
10841 if (IS_AFTER_OPERATOR()) {
10842 SET_LEX_STATE(EXPR_ARG);
10850 set_yylval_id('-');
10851 SET_LEX_STATE(EXPR_BEG);
10855 SET_LEX_STATE(EXPR_ENDFN);
10858 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10859 SET_LEX_STATE(EXPR_BEG);
10861 if (c != -1 && ISDIGIT(c)) {
10862 return tUMINUS_NUM;
10866 SET_LEX_STATE(EXPR_BEG);
10868 return warn_balanced('-', "-", "unary operator");
10871 int is_beg = IS_BEG();
10872 SET_LEX_STATE(EXPR_BEG);
10873 if ((c = nextc(p)) == '.') {
10874 if ((c = nextc(p)) == '.') {
10875 if (p->ctxt.in_argdef) {
10876 SET_LEX_STATE(EXPR_ENDARG);
10879 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10880 rb_warn0("... at EOL, should be parenthesized?");
10882 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10883 if (IS_lex_state_for(last_state, EXPR_LABEL))
10886 return is_beg ? tBDOT3 : tDOT3;
10889 return is_beg ? tBDOT2 : tDOT2;
10892 if (c != -1 && ISDIGIT(c)) {
10893 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10894 parse_numeric(p, '.');
10895 if (ISDIGIT(prev)) {
10896 yyerror0("unexpected fraction part after numeric literal");
10899 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10901 SET_LEX_STATE(EXPR_END);
10902 p->lex.ptok = p->lex.pcur;
10905 set_yylval_id('.');
10906 SET_LEX_STATE(EXPR_DOT);
10910 case '0': case '1': case '2': case '3': case '4':
10911 case '5': case '6': case '7': case '8': case '9':
10912 return parse_numeric(p, c);
10917 SET_LEX_STATE(EXPR_ENDFN);
10918 p->lex.paren_nest--;
10924 SET_LEX_STATE(EXPR_END);
10925 p->lex.paren_nest--;
10929 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10930 if (!p->lex.brace_nest--) return tSTRING_DEND;
10933 SET_LEX_STATE(EXPR_END);
10934 p->lex.paren_nest--;
10940 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10941 SET_LEX_STATE(EXPR_BEG);
10944 set_yylval_id(idCOLON2);
10945 SET_LEX_STATE(EXPR_DOT);
10948 if (IS_END() || ISSPACE(c) || c == '#') {
10950 c = warn_balanced(':', ":", "symbol literal");
10951 SET_LEX_STATE(EXPR_BEG);
10956 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10959 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10965 SET_LEX_STATE(EXPR_FNAME);
10970 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10971 return tREGEXP_BEG;
10973 if ((c = nextc(p)) == '=') {
10974 set_yylval_id('/');
10975 SET_LEX_STATE(EXPR_BEG);
10979 if (IS_SPCARG(c)) {
10980 arg_ambiguous(p, '/');
10981 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10982 return tREGEXP_BEG;
10984 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10985 return warn_balanced('/', "/", "regexp literal");
10988 if ((c = nextc(p)) == '=') {
10989 set_yylval_id('^');
10990 SET_LEX_STATE(EXPR_BEG);
10993 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10998 SET_LEX_STATE(EXPR_BEG);
10999 p->command_start = TRUE;
11003 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11007 if (IS_AFTER_OPERATOR()) {
11008 if ((c = nextc(p)) != '@') {
11011 SET_LEX_STATE(EXPR_ARG);
11014 SET_LEX_STATE(EXPR_BEG);
11022 else if (!space_seen) {
11023 /* foo( ... ) => method call, no ambiguity */
11025 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11028 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11029 rb_warning0("parentheses after method name is interpreted as "
11030 "an argument list, not a decomposed argument");
11032 p->lex.paren_nest++;
11035 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11039 p->lex.paren_nest++;
11040 if (IS_AFTER_OPERATOR()) {
11041 if ((c = nextc(p)) == ']') {
11042 p->lex.paren_nest--;
11043 SET_LEX_STATE(EXPR_ARG);
11044 if ((c = nextc(p)) == '=') {
11051 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11054 else if (IS_BEG()) {
11057 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11060 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11066 ++p->lex.brace_nest;
11067 if (lambda_beginning_p())
11069 else if (IS_lex_state(EXPR_LABELED))
11070 c = tLBRACE; /* hash */
11071 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11072 c = '{'; /* block (primary) */
11073 else if (IS_lex_state(EXPR_ENDARG))
11074 c = tLBRACE_ARG; /* block (expr) */
11076 c = tLBRACE; /* hash */
11077 if (c != tLBRACE) {
11078 p->command_start = TRUE;
11079 SET_LEX_STATE(EXPR_BEG);
11082 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11084 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11093 dispatch_scan_event(p, tSP);
11094 goto retry; /* skip \\n */
11096 if (c == ' ') return tSP;
11097 if (ISSPACE(c)) return c;
11102 return parse_percent(p, space_seen, last_state);
11105 return parse_gvar(p, last_state);
11108 return parse_atmark(p, last_state);
11111 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11112 p->ruby__end__seen = 1;
11116 dispatch_scan_event(p, k__END__);
11118 return END_OF_INPUT;
11124 if (!parser_is_identchar(p)) {
11125 compile_error(p, "Invalid char `\\x%02X' in expression", c);
11134 return parse_ident(p, c, cmd_state);
11137static enum yytokentype
11138yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11140 enum yytokentype t;
11143 lval->val = Qundef;
11144 p->yylloc = yylloc;
11146 t = parser_yylex(p);
11148 if (has_delayed_token(p))
11149 dispatch_delayed_token(p, t);
11150 else if (t != END_OF_INPUT)
11151 dispatch_scan_event(p, t);
11156#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11159node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11161 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11163 rb_node_init(n, type);
11168nd_set_loc(NODE *nd, const YYLTYPE *loc)
11171 nd_set_line(nd, loc->beg_pos.lineno);
11176node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11178 NODE *n = node_new_internal(p, type, size, alignment);
11180 nd_set_loc(n, loc);
11181 nd_set_node_id(n, parser_get_node_id(p));
11185#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11189static rb_node_scope_t *
11190rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11192 rb_ast_id_table_t *nd_tbl;
11193 nd_tbl = local_tbl(p);
11194 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11195 n->nd_tbl = nd_tbl;
11196 n->nd_body = nd_body;
11197 n->nd_args = nd_args;
11202static rb_node_scope_t *
11203rb_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)
11205 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11206 n->nd_tbl = nd_tbl;
11207 n->nd_body = nd_body;
11208 n->nd_args = nd_args;
11213static rb_node_defn_t *
11214rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11216 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11217 n->nd_mid = nd_mid;
11218 n->nd_defn = nd_defn;
11223static rb_node_defs_t *
11224rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11226 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11227 n->nd_recv = nd_recv;
11228 n->nd_mid = nd_mid;
11229 n->nd_defn = nd_defn;
11234static rb_node_block_t *
11235rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11237 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11238 n->nd_head = nd_head;
11245static rb_node_for_t *
11246rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11248 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11249 n->nd_body = nd_body;
11250 n->nd_iter = nd_iter;
11255static rb_node_for_masgn_t *
11256rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11258 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11259 n->nd_var = nd_var;
11264static rb_node_retry_t *
11265rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11267 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11272static rb_node_begin_t *
11273rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11275 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11276 n->nd_body = nd_body;
11281static rb_node_rescue_t *
11282rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11284 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11285 n->nd_head = nd_head;
11286 n->nd_resq = nd_resq;
11287 n->nd_else = nd_else;
11292static rb_node_resbody_t *
11293rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc)
11295 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11296 n->nd_head = nd_head;
11297 n->nd_body = nd_body;
11298 n->nd_args = nd_args;
11303static rb_node_ensure_t *
11304rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11306 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11307 n->nd_head = nd_head;
11309 n->nd_ensr = nd_ensr;
11314static rb_node_and_t *
11315rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11317 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11318 n->nd_1st = nd_1st;
11319 n->nd_2nd = nd_2nd;
11324static rb_node_or_t *
11325rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
11327 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11328 n->nd_1st = nd_1st;
11329 n->nd_2nd = nd_2nd;
11334static rb_node_return_t *
11335rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
11337 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11338 n->nd_stts = nd_stts;
11342static rb_node_yield_t *
11343rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11345 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11346 n->nd_head = nd_head;
11351static rb_node_if_t *
11352rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11354 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11355 n->nd_cond = nd_cond;
11356 n->nd_body = nd_body;
11357 n->nd_else = nd_else;
11362static rb_node_unless_t *
11363rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11365 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11366 n->nd_cond = nd_cond;
11367 n->nd_body = nd_body;
11368 n->nd_else = nd_else;
11373static rb_node_class_t *
11374rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11376 /* Keep the order of node creation */
11377 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11378 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11379 n->nd_cpath = nd_cpath;
11380 n->nd_body = scope;
11381 n->nd_super = nd_super;
11386static rb_node_sclass_t *
11387rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11389 /* Keep the order of node creation */
11390 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11391 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11392 n->nd_recv = nd_recv;
11393 n->nd_body = scope;
11398static rb_node_module_t *
11399rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11401 /* Keep the order of node creation */
11402 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11403 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11404 n->nd_cpath = nd_cpath;
11405 n->nd_body = scope;
11410static rb_node_iter_t *
11411rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11413 /* Keep the order of node creation */
11414 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11415 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11416 n->nd_body = scope;
11422static rb_node_lambda_t *
11423rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11425 /* Keep the order of node creation */
11426 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11427 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11428 n->nd_body = scope;
11433static rb_node_case_t *
11434rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11436 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11437 n->nd_head = nd_head;
11438 n->nd_body = nd_body;
11443static rb_node_case2_t *
11444rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11446 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11448 n->nd_body = nd_body;
11453static rb_node_case3_t *
11454rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
11456 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11457 n->nd_head = nd_head;
11458 n->nd_body = nd_body;
11463static rb_node_when_t *
11464rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11466 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11467 n->nd_head = nd_head;
11468 n->nd_body = nd_body;
11469 n->nd_next = nd_next;
11474static rb_node_in_t *
11475rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11477 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11478 n->nd_head = nd_head;
11479 n->nd_body = nd_body;
11480 n->nd_next = nd_next;
11485static rb_node_while_t *
11486rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11488 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11489 n->nd_cond = nd_cond;
11490 n->nd_body = nd_body;
11491 n->nd_state = nd_state;
11496static rb_node_until_t *
11497rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc)
11499 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11500 n->nd_cond = nd_cond;
11501 n->nd_body = nd_body;
11502 n->nd_state = nd_state;
11507static rb_node_colon2_t *
11508rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11510 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11511 n->nd_head = nd_head;
11512 n->nd_mid = nd_mid;
11517static rb_node_colon3_t *
11518rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11520 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11521 n->nd_mid = nd_mid;
11526static rb_node_dot2_t *
11527rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11529 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11530 n->nd_beg = nd_beg;
11531 n->nd_end = nd_end;
11536static rb_node_dot3_t *
11537rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11539 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11540 n->nd_beg = nd_beg;
11541 n->nd_end = nd_end;
11546static rb_node_self_t *
11547rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11549 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11555static rb_node_nil_t *
11556rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11558 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11563static rb_node_true_t *
11564rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11566 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11571static rb_node_false_t *
11572rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11574 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11579static rb_node_super_t *
11580rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11582 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11583 n->nd_args = nd_args;
11588static rb_node_zsuper_t *
11589rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11591 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11596static rb_node_match2_t *
11597rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11599 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11600 n->nd_recv = nd_recv;
11601 n->nd_value = nd_value;
11607static rb_node_match3_t *
11608rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11610 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11611 n->nd_recv = nd_recv;
11612 n->nd_value = nd_value;
11617/* TODO: Use union for NODE_LIST2 */
11618static rb_node_list_t *
11619rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11621 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11622 n->nd_head = nd_head;
11629static rb_node_list_t *
11630rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11632 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11633 n->nd_head = nd_head;
11634 n->as.nd_alen = nd_alen;
11635 n->nd_next = nd_next;
11640static rb_node_zlist_t *
11641rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11643 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11648static rb_node_hash_t *
11649rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11651 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11652 n->nd_head = nd_head;
11658static rb_node_masgn_t *
11659rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11661 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11662 n->nd_head = nd_head;
11664 n->nd_args = nd_args;
11669static rb_node_gasgn_t *
11670rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11672 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11673 n->nd_vid = nd_vid;
11674 n->nd_value = nd_value;
11679static rb_node_lasgn_t *
11680rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11682 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11683 n->nd_vid = nd_vid;
11684 n->nd_value = nd_value;
11689static rb_node_dasgn_t *
11690rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11692 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11693 n->nd_vid = nd_vid;
11694 n->nd_value = nd_value;
11699static rb_node_iasgn_t *
11700rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11702 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11703 n->nd_vid = nd_vid;
11704 n->nd_value = nd_value;
11709static rb_node_cvasgn_t *
11710rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11712 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11713 n->nd_vid = nd_vid;
11714 n->nd_value = nd_value;
11719static rb_node_op_asgn1_t *
11720rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc)
11722 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11723 n->nd_recv = nd_recv;
11724 n->nd_mid = nd_mid;
11725 n->nd_index = index;
11726 n->nd_rvalue = rvalue;
11731static rb_node_op_asgn2_t *
11732rb_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)
11734 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11735 n->nd_recv = nd_recv;
11736 n->nd_value = nd_value;
11737 n->nd_vid = nd_vid;
11738 n->nd_mid = nd_mid;
11739 n->nd_aid = nd_aid;
11744static rb_node_op_asgn_or_t *
11745rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11747 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11748 n->nd_head = nd_head;
11749 n->nd_value = nd_value;
11754static rb_node_op_asgn_and_t *
11755rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11757 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11758 n->nd_head = nd_head;
11759 n->nd_value = nd_value;
11764static rb_node_gvar_t *
11765rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11767 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11768 n->nd_vid = nd_vid;
11773static rb_node_lvar_t *
11774rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11776 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11777 n->nd_vid = nd_vid;
11782static rb_node_dvar_t *
11783rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11785 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11786 n->nd_vid = nd_vid;
11791static rb_node_ivar_t *
11792rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11794 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11795 n->nd_vid = nd_vid;
11800static rb_node_const_t *
11801rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11803 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11804 n->nd_vid = nd_vid;
11809static rb_node_cvar_t *
11810rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11812 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
11813 n->nd_vid = nd_vid;
11818static rb_node_nth_ref_t *
11819rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11821 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
11822 n->nd_nth = nd_nth;
11827static rb_node_back_ref_t *
11828rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
11830 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
11831 n->nd_nth = nd_nth;
11836static rb_node_lit_t *
11837rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11839 rb_node_lit_t *n = NODE_NEWNODE(NODE_LIT, rb_node_lit_t, loc);
11840 n->nd_lit = nd_lit;
11845static rb_node_str_t *
11846rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11848 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
11849 n->nd_lit = nd_lit;
11854/* TODO; Use union for NODE_DSTR2 */
11855static rb_node_dstr_t *
11856rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11858 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
11859 n->nd_lit = nd_lit;
11860 n->as.nd_alen = nd_alen;
11861 n->nd_next = (rb_node_list_t *)nd_next;
11866static rb_node_dstr_t *
11867rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11869 return rb_node_dstr_new0(p, nd_lit, 1, 0, loc);
11872static rb_node_xstr_t *
11873rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc)
11875 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
11876 n->nd_lit = nd_lit;
11881static rb_node_dxstr_t *
11882rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11884 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
11885 n->nd_lit = nd_lit;
11886 n->nd_alen = nd_alen;
11887 n->nd_next = (rb_node_list_t *)nd_next;
11892static rb_node_dsym_t *
11893rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11895 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
11896 n->nd_lit = nd_lit;
11897 n->nd_alen = nd_alen;
11898 n->nd_next = (rb_node_list_t *)nd_next;
11903static rb_node_evstr_t *
11904rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11906 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
11907 n->nd_body = nd_body;
11912static rb_node_call_t *
11913rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11915 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
11916 n->nd_recv = nd_recv;
11917 n->nd_mid = nd_mid;
11918 n->nd_args = nd_args;
11923static rb_node_opcall_t *
11924rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11926 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
11927 n->nd_recv = nd_recv;
11928 n->nd_mid = nd_mid;
11929 n->nd_args = nd_args;
11934static rb_node_fcall_t *
11935rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11937 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
11938 n->nd_mid = nd_mid;
11939 n->nd_args = nd_args;
11944static rb_node_qcall_t *
11945rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
11947 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
11948 n->nd_recv = nd_recv;
11949 n->nd_mid = nd_mid;
11950 n->nd_args = nd_args;
11955static rb_node_vcall_t *
11956rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11958 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
11959 n->nd_mid = nd_mid;
11964static rb_node_once_t *
11965rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11967 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
11968 n->nd_body = nd_body;
11973static rb_node_args_t *
11974rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
11976 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
11977 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
11982static rb_node_args_aux_t *
11983rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc)
11985 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
11986 n->nd_pid = nd_pid;
11987 n->nd_plen = nd_plen;
11993static rb_node_opt_arg_t *
11994rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11996 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
11997 n->nd_body = nd_body;
12003static rb_node_kw_arg_t *
12004rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12006 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12007 n->nd_body = nd_body;
12013static rb_node_postarg_t *
12014rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12016 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12017 n->nd_1st = nd_1st;
12018 n->nd_2nd = nd_2nd;
12023static rb_node_argscat_t *
12024rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12026 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12027 n->nd_head = nd_head;
12028 n->nd_body = nd_body;
12033static rb_node_argspush_t *
12034rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12036 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12037 n->nd_head = nd_head;
12038 n->nd_body = nd_body;
12043static rb_node_splat_t *
12044rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12046 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12047 n->nd_head = nd_head;
12052static rb_node_block_pass_t *
12053rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12055 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12057 n->nd_body = nd_body;
12062static rb_node_alias_t *
12063rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12065 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12066 n->nd_1st = nd_1st;
12067 n->nd_2nd = nd_2nd;
12072static rb_node_valias_t *
12073rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc)
12075 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12076 n->nd_alias = nd_alias;
12077 n->nd_orig = nd_orig;
12082static rb_node_undef_t *
12083rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12085 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12086 n->nd_undef = nd_undef;
12091static rb_node_errinfo_t *
12092rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12094 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12099static rb_node_defined_t *
12100rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12102 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12103 n->nd_head = nd_head;
12108static rb_node_postexe_t *
12109rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12111 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12112 n->nd_body = nd_body;
12117static rb_node_attrasgn_t *
12118rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12120 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12121 n->nd_recv = nd_recv;
12122 n->nd_mid = nd_mid;
12123 n->nd_args = nd_args;
12128static rb_node_aryptn_t *
12129rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12131 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12133 n->pre_args = pre_args;
12134 n->rest_arg = rest_arg;
12135 n->post_args = post_args;
12140static rb_node_hshptn_t *
12141rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12143 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12144 n->nd_pconst = nd_pconst;
12145 n->nd_pkwargs = nd_pkwargs;
12146 n->nd_pkwrestarg = nd_pkwrestarg;
12151static rb_node_fndptn_t *
12152rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12154 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12156 n->pre_rest_arg = pre_rest_arg;
12158 n->post_rest_arg = post_rest_arg;
12163static rb_node_cdecl_t *
12164rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc)
12166 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12167 n->nd_vid = nd_vid;
12168 n->nd_value = nd_value;
12169 n->nd_else = nd_else;
12174static rb_node_op_cdecl_t *
12175rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, const YYLTYPE *loc)
12177 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12178 n->nd_head = nd_head;
12179 n->nd_value = nd_value;
12180 n->nd_aid = nd_aid;
12185static rb_node_error_t *
12186rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12188 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12195static rb_node_ripper_t *
12196rb_node_ripper_new(struct parser_params *p, ID nd_vid, VALUE nd_rval, VALUE nd_cval, const YYLTYPE *loc)
12198 rb_node_ripper_t *n = NODE_NEWNODE(NODE_RIPPER, rb_node_ripper_t, loc);
12199 n->nd_vid = nd_vid;
12200 n->nd_rval = nd_rval;
12201 n->nd_cval = nd_cval;
12206static rb_node_ripper_values_t *
12207rb_node_ripper_values_new(struct parser_params *p, VALUE nd_val1, VALUE nd_val2, VALUE nd_val3, const YYLTYPE *loc)
12209 rb_node_ripper_values_t *n = NODE_NEWNODE(NODE_RIPPER_VALUES, rb_node_ripper_values_t, loc);
12210 n->nd_val1 = nd_val1;
12211 n->nd_val2 = nd_val2;
12212 n->nd_val3 = nd_val3;
12219static rb_node_break_t *
12220rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12222 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12223 n->nd_stts = nd_stts;
12229static rb_node_next_t *
12230rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc)
12232 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12233 n->nd_stts = nd_stts;
12239static rb_node_redo_t *
12240rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc)
12242 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12248static rb_node_def_temp_t *
12249rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12251 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12252 n->save.cur_arg = p->cur_arg;
12253 n->save.numparam_save = 0;
12254 n->save.max_numparam = 0;
12255 n->save.ctxt = p->ctxt;
12259 n->dot_or_colon = Qnil;
12268static rb_node_def_temp_t *
12269def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12271 n->save.numparam_save = numparam_push(p);
12272 n->save.max_numparam = p->max_numparam;
12277static enum node_type
12278nodetype(NODE *node) /* for debug */
12280 return (enum node_type)nd_type(node);
12284nodeline(NODE *node)
12286 return nd_line(node);
12290newline_node(NODE *node)
12293 node = remove_begin(node);
12294 nd_set_fl_newline(node);
12300fixpos(NODE *node, NODE *orig)
12304 nd_set_line(node, nd_line(orig));
12308parser_warning(struct parser_params *p, NODE *node, const char *mesg)
12310 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12314parser_warn(struct parser_params *p, NODE *node, const char *mesg)
12316 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
12320block_append(struct parser_params *p, NODE *head, NODE *tail)
12322 NODE *end, *h = head, *nd;
12324 if (tail == 0) return head;
12326 if (h == 0) return tail;
12327 switch (nd_type(h)) {
12329 h = end = NEW_BLOCK(head, &head->nd_loc);
12330 RNODE_BLOCK(end)->nd_end = end;
12334 end = RNODE_BLOCK(h)->nd_end;
12338 nd = RNODE_BLOCK(end)->nd_head;
12339 switch (nd_type(nd)) {
12345 if (RTEST(ruby_verbose)) {
12346 parser_warning(p, tail, "statement not reached");
12354 if (!nd_type_p(tail, NODE_BLOCK)) {
12355 tail = NEW_BLOCK(tail, &tail->nd_loc);
12356 RNODE_BLOCK(tail)->nd_end = tail;
12358 RNODE_BLOCK(end)->nd_next = tail;
12359 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12360 nd_set_last_loc(head, nd_last_loc(tail));
12364/* append item to the list */
12366list_append(struct parser_params *p, NODE *list, NODE *item)
12370 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12371 if (RNODE_LIST(list)->nd_next) {
12372 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12378 RNODE_LIST(list)->as.nd_alen += 1;
12379 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12380 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12382 nd_set_last_loc(list, nd_last_loc(item));
12387/* concat two lists */
12389list_concat(NODE *head, NODE *tail)
12393 if (RNODE_LIST(head)->nd_next) {
12394 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12400 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12401 RNODE_LIST(last)->nd_next = tail;
12402 if (RNODE_LIST(tail)->nd_next) {
12403 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12406 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12409 nd_set_last_loc(head, nd_last_loc(tail));
12415literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
12417 if (NIL_P(tail)) return 1;
12418 if (!rb_enc_compatible(head, tail)) {
12419 compile_error(p, "string literal encodings differ (%s / %s)",
12420 rb_enc_name(rb_enc_get(head)),
12421 rb_enc_name(rb_enc_get(tail)));
12422 rb_str_resize(head, 0);
12423 rb_str_resize(tail, 0);
12426 rb_str_buf_append(head, tail);
12431string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12433 if (htype != NODE_DSTR) return Qfalse;
12434 if (RNODE_DSTR(head)->nd_next) {
12435 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12436 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
12438 const VALUE lit = RNODE_DSTR(head)->nd_lit;
12439 ASSUME(lit != Qfalse);
12443/* concat two string literals */
12445literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12447 enum node_type htype;
12450 if (!head) return tail;
12451 if (!tail) return head;
12453 htype = nd_type(head);
12454 if (htype == NODE_EVSTR) {
12455 head = new_dstr(p, head, loc);
12458 if (p->heredoc_indent > 0) {
12461 head = str2dstr(p, head);
12463 return list_append(p, head, tail);
12468 switch (nd_type(tail)) {
12470 if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12474 lit = RNODE_DSTR(head)->nd_lit;
12476 if (htype == NODE_STR) {
12477 if (!literal_concat0(p, lit, RNODE_STR(tail)->nd_lit)) {
12479 rb_discard_node(p, head);
12480 rb_discard_node(p, tail);
12483 rb_discard_node(p, tail);
12486 list_append(p, head, tail);
12491 if (htype == NODE_STR) {
12492 if (!literal_concat0(p, RNODE_STR(head)->nd_lit, RNODE_DSTR(tail)->nd_lit))
12494 RNODE_DSTR(tail)->nd_lit = RNODE_STR(head)->nd_lit;
12495 rb_discard_node(p, head);
12498 else if (NIL_P(RNODE_DSTR(tail)->nd_lit)) {
12500 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12501 if (!RNODE_DSTR(head)->nd_next) {
12502 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12504 else if (RNODE_DSTR(tail)->nd_next) {
12505 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12506 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12508 rb_discard_node(p, tail);
12510 else if ((lit = string_literal_head(p, htype, head)) != Qfalse) {
12511 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->nd_lit))
12513 RNODE_DSTR(tail)->nd_lit = Qnil;
12517 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));
12522 if (htype == NODE_STR) {
12523 head = str2dstr(p, head);
12524 RNODE_DSTR(head)->as.nd_alen = 1;
12526 list_append(p, head, tail);
12533nd_copy_flag(NODE *new_node, NODE *old_node)
12535 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12536 nd_set_line(new_node, nd_line(old_node));
12537 new_node->nd_loc = old_node->nd_loc;
12538 new_node->node_id = old_node->node_id;
12542str2dstr(struct parser_params *p, NODE *node)
12544 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12545 nd_copy_flag(new_node, node);
12546 RNODE_DSTR(new_node)->nd_lit = RNODE_STR(node)->nd_lit;
12547 RNODE_DSTR(new_node)->as.nd_alen = 0;
12548 RNODE_DSTR(new_node)->nd_next = 0;
12549 RNODE_STR(node)->nd_lit = 0;
12555evstr2dstr(struct parser_params *p, NODE *node)
12557 if (nd_type_p(node, NODE_EVSTR)) {
12558 node = new_dstr(p, node, &node->nd_loc);
12564new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12569 switch (nd_type(node)) {
12571 return str2dstr(p, node);
12578 return NEW_EVSTR(head, loc);
12582new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12584 VALUE lit = STR_NEW0();
12585 NODE *dstr = NEW_DSTR(lit, loc);
12586 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12587 return list_append(p, dstr, node);
12591call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12592 const YYLTYPE *op_loc, const YYLTYPE *loc)
12597 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12598 nd_set_line(expr, op_loc->beg_pos.lineno);
12603call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12607 opcall = NEW_OPCALL(recv, id, 0, loc);
12608 nd_set_line(opcall, op_loc->beg_pos.lineno);
12613new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12615 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12616 nd_set_line(qcall, op_loc->beg_pos.lineno);
12621new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12624 if (block) block_dup_check(p, args, block);
12625 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12626 if (block) ret = method_add_block(p, ret, block, loc);
12631#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12634last_expr_once_body(NODE *node)
12636 if (!node) return 0;
12637 return nd_once_body(node);
12641match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12644 int line = op_loc->beg_pos.lineno;
12649 if ((n = last_expr_once_body(node1)) != 0) {
12650 switch (nd_type(n)) {
12653 NODE *match = NEW_MATCH2(node1, node2, loc);
12654 nd_set_line(match, line);
12659 if (RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) {
12660 const VALUE lit = RNODE_LIT(n)->nd_lit;
12661 NODE *match = NEW_MATCH2(node1, node2, loc);
12662 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc);
12663 nd_set_line(match, line);
12669 if ((n = last_expr_once_body(node2)) != 0) {
12672 switch (nd_type(n)) {
12674 if (!RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) break;
12677 match3 = NEW_MATCH3(node2, node1, loc);
12682 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12683 nd_set_line(n, line);
12687# if WARN_PAST_SCOPE
12689past_dvar_p(struct parser_params *p, ID id)
12691 struct vtable *past = p->lvtbl->past;
12693 if (vtable_included(past, id)) return 1;
12701numparam_nested_p(struct parser_params *p)
12703 struct local_vars *local = p->lvtbl;
12704 NODE *outer = local->numparam.outer;
12705 NODE *inner = local->numparam.inner;
12706 if (outer || inner) {
12707 NODE *used = outer ? outer : inner;
12708 compile_error(p, "numbered parameter is already used in\n"
12709 "%s:%d: %s block here",
12710 p->ruby_sourcefile, nd_line(used),
12711 outer ? "outer" : "inner");
12712 parser_show_error_line(p, &used->nd_loc);
12719gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
12725 return NEW_SELF(loc);
12727 return NEW_NIL(loc);
12729 return NEW_TRUE(loc);
12730 case keyword_false:
12731 return NEW_FALSE(loc);
12732 case keyword__FILE__:
12734 VALUE file = p->ruby_sourcefile_string;
12736 file = rb_str_new(0, 0);
12738 file = rb_str_dup(file);
12739 node = NEW_STR(file, loc);
12740 RB_OBJ_WRITTEN(p->ast, Qnil, file);
12743 case keyword__LINE__:
12744 return NEW_LIT(INT2FIX(loc->beg_pos.lineno), loc);
12745 case keyword__ENCODING__:
12746 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
12747 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12751 switch (id_type(id)) {
12753 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
12754 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
12755 if (id == p->cur_arg) {
12756 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12759 if (vidp) *vidp |= LVAR_USED;
12760 node = NEW_DVAR(id, loc);
12763 if (local_id_ref(p, id, &vidp)) {
12764 if (id == p->cur_arg) {
12765 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
12768 if (vidp) *vidp |= LVAR_USED;
12769 node = NEW_LVAR(id, loc);
12772 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
12773 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
12774 if (numparam_nested_p(p)) return 0;
12775 node = NEW_DVAR(id, loc);
12776 struct local_vars *local = p->lvtbl;
12777 if (!local->numparam.current) local->numparam.current = node;
12780# if WARN_PAST_SCOPE
12781 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
12782 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
12785 /* method call without arguments */
12786 if (dyna_in_block(p) && id == rb_intern("it")
12787 && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))
12788 && p->max_numparam != ORDINAL_PARAM) {
12789 rb_warn0("`it` calls without arguments will refer to the first block param in Ruby 3.4; use it() or self.it");
12791 return NEW_VCALL(id, loc);
12793 return NEW_GVAR(id, loc);
12795 return NEW_IVAR(id, loc);
12797 return NEW_CONST(id, loc);
12799 return NEW_CVAR(id, loc);
12801 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
12805static rb_node_opt_arg_t *
12806opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
12808 rb_node_opt_arg_t *opts = opt_list;
12809 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12811 while (opts->nd_next) {
12812 opts = opts->nd_next;
12813 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
12815 opts->nd_next = opt;
12820static rb_node_kw_arg_t *
12821kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
12824 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
12825 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
12831new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
12833 return NEW_DEFINED(remove_begin_all(expr), loc);
12837symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
12839 enum node_type type = nd_type(symbol);
12842 nd_set_type(symbol, NODE_DSYM);
12845 nd_set_type(symbol, NODE_LIT);
12846 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(symbol)->nd_lit = rb_str_intern(RNODE_LIT(symbol)->nd_lit));
12849 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
12851 return list_append(p, symbols, symbol);
12855new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
12857 struct RNode_LIST *list;
12862 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
12863 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit);
12866 switch (nd_type(node)) {
12869 VALUE src = RNODE_STR(node)->nd_lit;
12870 nd_set_type(node, NODE_LIT);
12871 nd_set_loc(node, loc);
12872 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit = reg_compile(p, src, options));
12877 node = NEW_DSTR0(lit, 1, NEW_LIST(node, loc), loc);
12878 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12881 nd_set_type(node, NODE_DREGX);
12882 nd_set_loc(node, loc);
12883 RNODE_DREGX(node)->nd_cflag = options & RE_OPTION_MASK;
12884 if (!NIL_P(RNODE_DREGX(node)->nd_lit)) reg_fragment_check(p, RNODE_DREGX(node)->nd_lit, options);
12885 for (list = RNODE_DREGX(prev = node)->nd_next; list; list = RNODE_LIST(list->nd_next)) {
12886 NODE *frag = list->nd_head;
12887 enum node_type type = nd_type(frag);
12888 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
12889 VALUE tail = RNODE_STR(frag)->nd_lit;
12890 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(RNODE_DREGX(prev)->nd_lit)) {
12891 VALUE lit = prev == node ? RNODE_DREGX(prev)->nd_lit : RNODE_LIT(RNODE_LIST(prev)->nd_head)->nd_lit;
12892 if (!literal_concat0(p, lit, tail)) {
12893 return NEW_NIL(loc); /* dummy node on error */
12895 rb_str_resize(tail, 0);
12896 RNODE_LIST(prev)->nd_next = list->nd_next;
12897 rb_discard_node(p, list->nd_head);
12898 rb_discard_node(p, (NODE *)list);
12899 list = RNODE_LIST(prev);
12902 prev = (NODE *)list;
12909 if (!RNODE_DREGX(node)->nd_next) {
12910 VALUE src = RNODE_DREGX(node)->nd_lit;
12911 VALUE re = reg_compile(p, src, options);
12912 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_DREGX(node)->nd_lit = re);
12914 if (options & RE_OPTION_ONCE) {
12915 node = NEW_ONCE(node, loc);
12922static rb_node_kw_arg_t *
12923new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
12926 return NEW_KW_ARG((k), loc);
12930new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12933 VALUE lit = STR_NEW0();
12934 NODE *xstr = NEW_XSTR(lit, loc);
12935 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
12938 switch (nd_type(node)) {
12940 nd_set_type(node, NODE_XSTR);
12941 nd_set_loc(node, loc);
12944 nd_set_type(node, NODE_DXSTR);
12945 nd_set_loc(node, loc);
12948 node = NEW_DXSTR(Qnil, 1, NEW_LIST(node, loc), loc);
12955check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
12959 if (!arg || !p->case_labels) return;
12961 lit = rb_node_case_when_optimizable_literal(arg);
12962 if (UNDEF_P(lit)) return;
12963 if (nd_type_p(arg, NODE_STR)) {
12964 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(arg)->nd_lit = lit);
12967 if (NIL_P(p->case_labels)) {
12968 p->case_labels = rb_obj_hide(rb_hash_new());
12971 VALUE line = rb_hash_lookup(p->case_labels, lit);
12972 if (!NIL_P(line)) {
12973 rb_warning1("duplicated `when' clause with line %d is ignored",
12978 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
12983id_is_var(struct parser_params *p, ID id)
12985 if (is_notop_id(id)) {
12986 switch (id & ID_SCOPE_MASK) {
12987 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
12990 if (dyna_in_block(p)) {
12991 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
12993 if (local_id(p, id)) return 1;
12994 /* method call without arguments */
12998 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13003new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
13005 VALUE src = 0, err = 0;
13007 if (ripper_is_node_yylval(p, re)) {
13008 src = RNODE_RIPPER(re)->nd_cval;
13009 re = RNODE_RIPPER(re)->nd_rval;
13011 if (ripper_is_node_yylval(p, opt)) {
13012 options = (int)RNODE_RIPPER(opt)->nd_vid;
13013 opt = RNODE_RIPPER(opt)->nd_rval;
13015 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
13016 compile_error(p, "%"PRIsVALUE, err);
13018 return dispatch2(regexp_literal, re, opt);
13020#endif /* !RIPPER */
13022static inline enum lex_state_e
13023parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13026 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13028 return p->lex.state = ls;
13032static const char rb_parser_lex_state_names[][8] = {
13033 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13034 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13035 "LABEL", "LABELED","FITEM",
13039append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13042 unsigned int mask = 1;
13043 static const char none[] = "NONE";
13045 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13046 if ((unsigned)state & mask) {
13048 rb_str_cat(buf, "|", 1);
13051 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13055 rb_str_cat(buf, none, sizeof(none)-1);
13061flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13063 VALUE mesg = p->debug_buffer;
13065 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13066 p->debug_buffer = Qnil;
13067 rb_io_puts(1, &mesg, out);
13069 if (!NIL_P(str) && RSTRING_LEN(str)) {
13070 rb_io_write(p->debug_output, str);
13075rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13076 enum lex_state_e to, int line)
13079 mesg = rb_str_new_cstr("lex_state: ");
13080 append_lex_state_name(p, from, mesg);
13081 rb_str_cat_cstr(mesg, " -> ");
13082 append_lex_state_name(p, to, mesg);
13083 rb_str_catf(mesg, " at line %d\n", line);
13084 flush_debug_buffer(p, p->debug_output, mesg);
13089rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13091 return rb_fstring(append_lex_state_name(p, state, rb_str_new(0, 0)));
13095append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13098 rb_str_cat_cstr(mesg, "0");
13101 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13102 for (; mask && !(stack & mask); mask >>= 1) continue;
13103 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13108rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13109 const char *name, int line)
13111 VALUE mesg = rb_sprintf("%s: ", name);
13112 append_bitstack_value(p, stack, mesg);
13113 rb_str_catf(mesg, " at line %d\n", line);
13114 flush_debug_buffer(p, p->debug_output, mesg);
13118rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13121 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13124 rb_str_vcatf(mesg, fmt, ap);
13126 yyerror0(RSTRING_PTR(mesg));
13129 mesg = rb_str_new(0, 0);
13130 append_lex_state_name(p, p->lex.state, mesg);
13131 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13132 rb_str_resize(mesg, 0);
13133 append_bitstack_value(p, p->cond_stack, mesg);
13134 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13135 rb_str_resize(mesg, 0);
13136 append_bitstack_value(p, p->cmdarg_stack, mesg);
13137 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13138 if (p->debug_output == rb_ractor_stdout())
13139 p->debug_output = rb_ractor_stderr();
13144rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13146 yylloc->beg_pos.lineno = sourceline;
13147 yylloc->beg_pos.column = beg_pos;
13148 yylloc->end_pos.lineno = sourceline;
13149 yylloc->end_pos.column = end_pos;
13154rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13156 int sourceline = here->sourceline;
13157 int beg_pos = (int)here->offset - here->quote
13158 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13159 int end_pos = (int)here->offset + here->length + here->quote;
13161 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13165rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13167 yylloc->beg_pos.lineno = p->delayed.beg_line;
13168 yylloc->beg_pos.column = p->delayed.beg_col;
13169 yylloc->end_pos.lineno = p->delayed.end_line;
13170 yylloc->end_pos.column = p->delayed.end_col;
13176rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13178 int sourceline = p->ruby_sourceline;
13179 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13180 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13181 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13185rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13187 yylloc->end_pos = yylloc->beg_pos;
13193rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13195 int sourceline = p->ruby_sourceline;
13196 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13197 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13198 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13202rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13204 int sourceline = p->ruby_sourceline;
13205 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13206 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13207 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13209#endif /* !RIPPER */
13212assignable0(struct parser_params *p, ID id, const char **err)
13214 if (!id) return -1;
13217 *err = "Can't change the value of self";
13220 *err = "Can't assign to nil";
13223 *err = "Can't assign to true";
13225 case keyword_false:
13226 *err = "Can't assign to false";
13228 case keyword__FILE__:
13229 *err = "Can't assign to __FILE__";
13231 case keyword__LINE__:
13232 *err = "Can't assign to __LINE__";
13234 case keyword__ENCODING__:
13235 *err = "Can't assign to __ENCODING__";
13238 switch (id_type(id)) {
13240 if (dyna_in_block(p)) {
13241 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13242 compile_error(p, "Can't assign to numbered parameter _%d",
13243 NUMPARAM_ID_TO_IDX(id));
13246 if (dvar_curr(p, id)) return NODE_DASGN;
13247 if (dvar_defined(p, id)) return NODE_DASGN;
13248 if (local_id(p, id)) return NODE_LASGN;
13253 if (!local_id(p, id)) local_var(p, id);
13257 case ID_GLOBAL: return NODE_GASGN;
13258 case ID_INSTANCE: return NODE_IASGN;
13260 if (!p->ctxt.in_def) return NODE_CDECL;
13261 *err = "dynamic constant assignment";
13263 case ID_CLASS: return NODE_CVASGN;
13265 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13272assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13274 const char *err = 0;
13275 int node_type = assignable0(p, id, &err);
13276 switch (node_type) {
13277 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13278 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13279 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13280 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13281 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
13282 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13284 if (err) yyerror1(loc, err);
13285 return NEW_BEGIN(0, loc);
13289assignable(struct parser_params *p, VALUE lhs)
13291 const char *err = 0;
13292 assignable0(p, get_id(lhs), &err);
13293 if (err) lhs = assign_error(p, err, lhs);
13299is_private_local_id(struct parser_params *p, ID name)
13302 if (name == idUScore) return 1;
13303 if (!is_local_id(name)) return 0;
13304 s = rb_id2str(name);
13306 return RSTRING_PTR(s)[0] == '_';
13310shadowing_lvar_0(struct parser_params *p, ID name)
13312 if (dyna_in_block(p)) {
13313 if (dvar_curr(p, name)) {
13314 if (is_private_local_id(p, name)) return 1;
13315 yyerror0("duplicated argument name");
13317 else if (dvar_defined(p, name) || local_id(p, name)) {
13318 vtable_add(p->lvtbl->vars, name);
13319 if (p->lvtbl->used) {
13320 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13326 if (local_id(p, name)) {
13327 if (is_private_local_id(p, name)) return 1;
13328 yyerror0("duplicated argument name");
13335shadowing_lvar(struct parser_params *p, ID name)
13337 shadowing_lvar_0(p, name);
13342new_bv(struct parser_params *p, ID name)
13345 if (!is_local_id(name)) {
13346 compile_error(p, "invalid local variable - %"PRIsVALUE,
13350 if (!shadowing_lvar_0(p, name)) return;
13356aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13358 return NEW_ATTRASGN(recv, tASET, idx, loc);
13362block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13364 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13365 compile_error(p, "both block arg and actual block given");
13370attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13372 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13373 return NEW_ATTRASGN(recv, id, 0, loc);
13377rb_backref_error(struct parser_params *p, NODE *node)
13379 switch (nd_type(node)) {
13381 compile_error(p, "Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13383 case NODE_BACK_REF:
13384 compile_error(p, "Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13390backref_error(struct parser_params *p, NODE *ref, VALUE expr)
13392 VALUE mesg = rb_str_new_cstr("Can't set variable ");
13393 rb_str_append(mesg, RNODE_RIPPER(ref)->nd_cval);
13394 return dispatch2(assign_error, mesg, expr);
13400arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13402 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13403 switch (nd_type(node1)) {
13405 return list_append(p, node1, node2);
13406 case NODE_BLOCK_PASS:
13407 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13408 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13410 case NODE_ARGSPUSH:
13411 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13412 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13413 nd_set_type(node1, NODE_ARGSCAT);
13416 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13417 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13418 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13421 return NEW_ARGSPUSH(node1, node2, loc);
13425arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13427 if (!node2) return node1;
13428 switch (nd_type(node1)) {
13429 case NODE_BLOCK_PASS:
13430 if (RNODE_BLOCK_PASS(node1)->nd_head)
13431 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13433 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13435 case NODE_ARGSPUSH:
13436 if (!nd_type_p(node2, NODE_LIST)) break;
13437 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13438 nd_set_type(node1, NODE_ARGSCAT);
13441 if (!nd_type_p(node2, NODE_LIST) ||
13442 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13443 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13446 return NEW_ARGSCAT(node1, node2, loc);
13450last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13453 if ((n1 = splat_array(args)) != 0) {
13454 return list_append(p, n1, last_arg);
13456 return arg_append(p, args, last_arg, loc);
13460rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13463 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13464 return list_concat(n1, rest_arg);
13466 return arg_concat(p, args, rest_arg, loc);
13470splat_array(NODE* node)
13472 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13473 if (nd_type_p(node, NODE_LIST)) return node;
13478mark_lvar_used(struct parser_params *p, NODE *rhs)
13482 switch (nd_type(rhs)) {
13484 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13485 if (vidp) *vidp |= LVAR_USED;
13489 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13490 if (vidp) *vidp |= LVAR_USED;
13495 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13496 mark_lvar_used(p, rhs->nd_head);
13504const_decl_path(struct parser_params *p, NODE **dest)
13507 if (!nd_type_p(n, NODE_CALL)) {
13508 const YYLTYPE *loc = &n->nd_loc;
13510 if (RNODE_CDECL(n)->nd_vid) {
13511 path = rb_id2str(RNODE_CDECL(n)->nd_vid);
13514 n = RNODE_CDECL(n)->nd_else;
13515 path = rb_ary_new();
13516 for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
13517 rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
13519 if (n && nd_type_p(n, NODE_CONST)) {
13521 rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid));
13523 else if (n && nd_type_p(n, NODE_COLON3)) {
13525 rb_ary_push(path, rb_str_new(0, 0));
13528 // expression::Name
13529 rb_ary_push(path, rb_str_new_cstr("..."));
13531 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
13532 path = rb_fstring(path);
13534 *dest = n = NEW_LIT(path, loc);
13535 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13541make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
13543 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13546 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
13547 NEW_LIST(value, loc), loc);
13550 return NEW_CALL(fcore, rb_intern("make_shareable"),
13551 NEW_LIST(value, loc), loc);
13556ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
13558 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
13559 NODE *args = NEW_LIST(value, loc);
13560 args = list_append(p, args, const_decl_path(p, dest));
13561 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
13564static int is_static_content(NODE *node);
13567shareable_literal_value(struct parser_params *p, NODE *node)
13569 if (!node) return Qnil;
13570 enum node_type type = nd_type(node);
13579 return RNODE_LIT(node)->nd_lit;
13585#ifndef SHAREABLE_BARE_EXPRESSION
13586#define SHAREABLE_BARE_EXPRESSION 1
13590shareable_literal_constant(struct parser_params *p, enum shareability shareable,
13591 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
13593# define shareable_literal_constant_next(n) \
13594 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
13597 if (!value) return 0;
13598 enum node_type type = nd_type(value);
13607 if (shareable == shareable_literal) {
13608 value = NEW_CALL(value, idUMinus, 0, loc);
13613 lit = rb_fstring(RNODE_STR(value)->nd_lit);
13614 nd_set_type(value, NODE_LIT);
13615 RB_OBJ_WRITE(p->ast, &RNODE_LIT(value)->nd_lit, lit);
13619 lit = rb_ary_new();
13620 OBJ_FREEZE_RAW(lit);
13621 NODE *n = NEW_LIT(lit, loc);
13622 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
13626 lit = rb_ary_new();
13627 for (NODE *n = value; n; n = RNODE_LIST(n)->nd_next) {
13628 NODE *elt = RNODE_LIST(n)->nd_head;
13630 elt = shareable_literal_constant_next(elt);
13632 RNODE_LIST(n)->nd_head = elt;
13634 else if (RTEST(lit)) {
13640 VALUE e = shareable_literal_value(p, elt);
13642 rb_ary_push(lit, e);
13646 lit = Qnil; /* make shareable at runtime */
13653 if (!RNODE_HASH(value)->nd_brace) return 0;
13654 lit = rb_hash_new();
13655 for (NODE *n = RNODE_HASH(value)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
13656 NODE *key = RNODE_LIST(n)->nd_head;
13657 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
13659 key = shareable_literal_constant_next(key);
13661 RNODE_LIST(n)->nd_head = key;
13663 else if (RTEST(lit)) {
13664 rb_hash_clear(lit);
13669 val = shareable_literal_constant_next(val);
13671 RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head = val;
13673 else if (RTEST(lit)) {
13674 rb_hash_clear(lit);
13679 VALUE k = shareable_literal_value(p, key);
13680 VALUE v = shareable_literal_value(p, val);
13681 if (!UNDEF_P(k) && !UNDEF_P(v)) {
13682 rb_hash_aset(lit, k, v);
13685 rb_hash_clear(lit);
13686 lit = Qnil; /* make shareable at runtime */
13693 if (shareable == shareable_literal &&
13694 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
13695 return ensure_shareable_node(p, dest, value, loc);
13700 /* Array or Hash */
13701 if (!lit) return 0;
13703 // if shareable_literal, all elements should have been ensured
13705 value = make_shareable_node(p, value, false, loc);
13708 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
13709 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(value)->nd_lit);
13713# undef shareable_literal_constant_next
13717shareable_constant_value(struct parser_params *p, enum shareability shareable,
13718 NODE *lhs, NODE *value, const YYLTYPE *loc)
13720 if (!value) return 0;
13721 switch (shareable) {
13722 case shareable_none:
13725 case shareable_literal:
13727 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13728 if (lit) return lit;
13733 case shareable_copy:
13734 case shareable_everything:
13736 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
13737 if (lit) return lit;
13738 return make_shareable_node(p, value, shareable == shareable_copy, loc);
13743 UNREACHABLE_RETURN(0);
13748node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13750 if (!lhs) return 0;
13752 switch (nd_type(lhs)) {
13754 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13763 set_nd_value(p, lhs, rhs);
13764 nd_set_loc(lhs, loc);
13767 case NODE_ATTRASGN:
13768 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13769 nd_set_loc(lhs, loc);
13773 /* should not happen */
13781value_expr_check(struct parser_params *p, NODE *node)
13783 NODE *void_node = 0, *vn;
13786 rb_warning0("empty expression");
13789 switch (nd_type(node)) {
13795 return void_node ? void_node : node;
13798 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13799 compile_error(p, "unexpected node");
13802 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13805 /* single line pattern matching with "=>" operator */
13806 return void_node ? void_node : node;
13809 while (RNODE_BLOCK(node)->nd_next) {
13810 node = RNODE_BLOCK(node)->nd_next;
13812 node = RNODE_BLOCK(node)->nd_head;
13816 node = RNODE_BEGIN(node)->nd_body;
13821 if (!RNODE_IF(node)->nd_body) {
13824 else if (!RNODE_IF(node)->nd_else) {
13827 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13828 if (!vn) return NULL;
13829 if (!void_node) void_node = vn;
13830 node = RNODE_IF(node)->nd_else;
13835 node = RNODE_AND(node)->nd_1st;
13841 mark_lvar_used(p, node);
13853value_expr_gen(struct parser_params *p, NODE *node)
13855 NODE *void_node = value_expr_check(p, node);
13857 yyerror1(&void_node->nd_loc, "void value expression");
13858 /* or "control never reach"? */
13865void_expr(struct parser_params *p, NODE *node)
13867 const char *useless = 0;
13869 if (!RTEST(ruby_verbose)) return;
13871 if (!node || !(node = nd_once_body(node))) return;
13872 switch (nd_type(node)) {
13874 switch (RNODE_OPCALL(node)->nd_mid) {
13893 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
13904 case NODE_BACK_REF:
13905 useless = "a variable";
13908 useless = "a constant";
13914 useless = "a literal";
13939 useless = "defined?";
13944 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
13949void_stmts(struct parser_params *p, NODE *node)
13951 NODE *const n = node;
13952 if (!RTEST(ruby_verbose)) return n;
13953 if (!node) return n;
13954 if (!nd_type_p(node, NODE_BLOCK)) return n;
13956 while (RNODE_BLOCK(node)->nd_next) {
13957 void_expr(p, RNODE_BLOCK(node)->nd_head);
13958 node = RNODE_BLOCK(node)->nd_next;
13964remove_begin(NODE *node)
13966 NODE **n = &node, *n1 = node;
13967 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
13968 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13974remove_begin_all(NODE *node)
13976 NODE **n = &node, *n1 = node;
13977 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
13978 *n = n1 = RNODE_BEGIN(n1)->nd_body;
13984reduce_nodes(struct parser_params *p, NODE **body)
13986 NODE *node = *body;
13989 *body = NEW_NIL(&NULL_LOC);
13992#define subnodes(type, n1, n2) \
13993 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
13994 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
13995 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
13998 int newline = (int)(nd_fl_newline(node));
13999 switch (nd_type(node)) {
14005 *body = node = RNODE_RETURN(node)->nd_stts;
14006 if (newline && node) nd_set_fl_newline(node);
14009 *body = node = RNODE_BEGIN(node)->nd_body;
14010 if (newline && node) nd_set_fl_newline(node);
14013 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14017 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14020 body = &RNODE_CASE(node)->nd_body;
14023 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14026 if (!subnodes(RNODE_ENSURE, nd_head, nd_resq)) goto end;
14029 newline = 0; // RESBODY should not be a NEWLINE
14030 if (RNODE_RESCUE(node)->nd_else) {
14031 body = &RNODE_RESCUE(node)->nd_resq;
14034 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14040 if (newline && node) nd_set_fl_newline(node);
14047is_static_content(NODE *node)
14049 if (!node) return 1;
14050 switch (nd_type(node)) {
14052 if (!(node = RNODE_HASH(node)->nd_head)) break;
14055 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14056 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14071assign_in_cond(struct parser_params *p, NODE *node)
14073 switch (nd_type(node)) {
14087 if (!get_nd_value(p, node)) return 1;
14088 if (is_static_content(get_nd_value(p, node))) {
14089 /* reports always */
14090 parser_warn(p, get_nd_value(p, node), "found `= literal' in conditional, should be ==");
14101#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14103 case COND_IN_OP: break; \
14104 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14105 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14109static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14112range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14114 enum node_type type;
14116 if (node == 0) return 0;
14118 type = nd_type(node);
14120 if (type == NODE_LIT && FIXNUM_P(RNODE_LIT(node)->nd_lit)) {
14121 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
14122 ID lineno = rb_intern("$.");
14123 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14125 return cond0(p, node, COND_IN_FF, loc, true);
14129cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14131 if (node == 0) return 0;
14132 if (!(node = nd_once_body(node))) return 0;
14133 assign_in_cond(p, node);
14135 switch (nd_type(node)) {
14137 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14143 SWITCH_BY_COND_TYPE(type, warn, "string ");
14147 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14149 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14152 RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head = cond0(p, RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head, type, loc, false);
14157 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14158 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14164 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14165 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14166 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
14167 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
14172 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14176 if (RB_TYPE_P(RNODE_LIT(node)->nd_lit, T_REGEXP)) {
14177 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14178 nd_set_type(node, NODE_MATCH);
14180 else if (RNODE_LIT(node)->nd_lit == Qtrue ||
14181 RNODE_LIT(node)->nd_lit == Qfalse) {
14182 /* booleans are OK, e.g., while true */
14184 else if (SYMBOL_P(RNODE_LIT(node)->nd_lit)) {
14188 SWITCH_BY_COND_TYPE(type, warning, "");
14197cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14199 if (node == 0) return 0;
14200 return cond0(p, node, COND_IN_COND, loc, true);
14204method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14206 if (node == 0) return 0;
14207 return cond0(p, node, COND_IN_OP, loc, true);
14211new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14213 YYLTYPE loc = {*pos, *pos};
14214 return NEW_NIL(&loc);
14218new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14220 if (!cc) return right;
14221 cc = cond0(p, cc, COND_IN_COND, loc, true);
14222 return newline_node(NEW_IF(cc, left, right, loc));
14226new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14228 if (!cc) return right;
14229 cc = cond0(p, cc, COND_IN_COND, loc, true);
14230 return newline_node(NEW_UNLESS(cc, left, right, loc));
14233#define NEW_AND_OR(type, f, s, loc) (type == NODE_AND ? NEW_AND(f,s,loc) : NEW_OR(f,s,loc))
14236logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14237 const YYLTYPE *op_loc, const YYLTYPE *loc)
14239 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14242 if (left && nd_type_p(left, type)) {
14243 NODE *node = left, *second;
14244 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14247 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc);
14248 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14249 left->nd_loc.end_pos = loc->end_pos;
14252 op = NEW_AND_OR(type, left, right, loc);
14253 nd_set_line(op, op_loc->beg_pos.lineno);
14260no_blockarg(struct parser_params *p, NODE *node)
14262 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14263 compile_error(p, "block argument should not be given");
14268ret_args(struct parser_params *p, NODE *node)
14271 no_blockarg(p, node);
14272 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14273 node = RNODE_LIST(node)->nd_head;
14280new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14282 if (node) no_blockarg(p, node);
14284 return NEW_YIELD(node, loc);
14288negate_lit(struct parser_params *p, VALUE lit)
14290 if (FIXNUM_P(lit)) {
14291 return LONG2FIX(-FIX2LONG(lit));
14293 if (SPECIAL_CONST_P(lit)) {
14295 if (FLONUM_P(lit)) {
14296 return DBL2NUM(-RFLOAT_VALUE(lit));
14301 switch (BUILTIN_TYPE(lit)) {
14303 bignum_negate(lit);
14304 lit = rb_big_norm(lit);
14307 rational_set_num(lit, negate_lit(p, rational_get_num(lit)));
14310 rcomplex_set_real(lit, negate_lit(p, rcomplex_get_real(lit)));
14311 rcomplex_set_imag(lit, negate_lit(p, rcomplex_get_imag(lit)));
14314 lit = DBL2NUM(-RFLOAT_VALUE(lit));
14318 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
14319 rb_builtin_class_name(lit));
14326arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14329 if (!node1) return (NODE *)node2;
14330 node2->nd_head = node1;
14331 nd_set_first_lineno(node2, nd_first_lineno(node1));
14332 nd_set_first_column(node2, nd_first_column(node1));
14333 return (NODE *)node2;
14339args_info_empty_p(struct rb_args_info *args)
14341 if (args->pre_args_num) return false;
14342 if (args->post_args_num) return false;
14343 if (args->rest_arg) return false;
14344 if (args->opt_args) return false;
14345 if (args->block_arg) return false;
14346 if (args->kw_args) return false;
14347 if (args->kw_rest_arg) return false;
14351static rb_node_args_t *
14352new_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)
14354 struct rb_args_info *args = &tail->nd_ainfo;
14356 if (args->forwarding) {
14358 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14361 rest_arg = idFWD_REST;
14364 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
14365 args->pre_init = pre_args ? pre_args->nd_next : 0;
14367 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
14368 args->post_init = post_args ? post_args->nd_next : 0;
14369 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14371 args->rest_arg = rest_arg;
14373 args->opt_args = opt_args;
14375#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14376 args->ruby2_keywords = args->forwarding;
14378 args->ruby2_keywords = 0;
14381 nd_set_loc(RNODE(tail), loc);
14386static rb_node_args_t *
14387new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14389 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14390 struct rb_args_info *args = &node->nd_ainfo;
14391 if (p->error_p) return node;
14393 args->block_arg = block;
14394 args->kw_args = kw_args;
14398 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14399 * variable order: k1, kr1, k2, &b, internal_id, krest
14401 * variable order: kr1, k1, k2, internal_id, krest, &b
14403 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14404 struct vtable *vtargs = p->lvtbl->args;
14405 rb_node_kw_arg_t *kwn = kw_args;
14407 if (block) block = vtargs->tbl[vtargs->pos-1];
14408 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14409 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14411 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14413 --required_kw_vars;
14414 kwn = kwn->nd_next;
14417 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14418 ID vid = get_nd_vid(p, kwn->nd_body);
14419 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14420 *required_kw_vars++ = vid;
14427 arg_var(p, kw_bits);
14428 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14429 if (block) arg_var(p, block);
14431 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14433 else if (kw_rest_arg == idNil) {
14434 args->no_kwarg = 1;
14436 else if (kw_rest_arg) {
14437 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14443static rb_node_args_t *
14444args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam)
14446 if (max_numparam > NO_PARAM) {
14448 YYLTYPE loc = RUBY_INIT_YYLLOC();
14449 args = new_args_tail(p, 0, 0, 0, 0);
14450 nd_set_loc(RNODE(args), &loc);
14452 args->nd_ainfo.pre_args_num = max_numparam;
14458new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14460 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14463 NODE *pre_args = NEW_LIST(pre_arg, loc);
14464 if (RNODE_ARYPTN(aryptn)->pre_args) {
14465 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14468 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14475new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14478 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14483 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14489new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14491 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14497new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14499 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14500 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14501 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14507new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14509 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14514new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14516 NODE *node, *kw_rest_arg_node;
14518 if (kw_rest_arg == idNil) {
14519 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14521 else if (kw_rest_arg) {
14522 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14525 kw_rest_arg_node = NULL;
14528 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14534dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14539 return NEW_LIT(ID2SYM(idNULL), loc);
14542 switch (nd_type(node)) {
14544 nd_set_type(node, NODE_DSYM);
14545 nd_set_loc(node, loc);
14548 lit = RNODE_STR(node)->nd_lit;
14549 RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit = ID2SYM(rb_intern_str(lit)));
14550 nd_set_type(node, NODE_LIT);
14551 nd_set_loc(node, loc);
14554 node = NEW_DSYM(Qnil, 1, NEW_LIST(node, loc), loc);
14561append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
14563 NODE *node = (NODE *)v;
14564 NODE **result = (NODE **)h;
14565 RNODE_LIST(node)->as.nd_alen = 2;
14566 RNODE_LIST(RNODE_LIST(node)->nd_next)->as.nd_end = RNODE_LIST(node)->nd_next;
14567 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next = 0;
14569 list_concat(*result, node);
14572 return ST_CONTINUE;
14576remove_duplicate_keys(struct parser_params *p, NODE *hash)
14578 struct st_hash_type literal_type = {
14583 st_table *literal_keys = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14585 NODE *last_expr = 0;
14586 rb_code_location_t loc = hash->nd_loc;
14587 while (hash && RNODE_LIST(hash)->nd_next) {
14588 NODE *head = RNODE_LIST(hash)->nd_head;
14589 NODE *value = RNODE_LIST(hash)->nd_next;
14590 NODE *next = RNODE_LIST(value)->nd_next;
14591 st_data_t key = (st_data_t)head;
14593 RNODE_LIST(value)->nd_next = 0;
14595 key = (st_data_t)value;
14597 else if (nd_type_p(head, NODE_LIT) &&
14598 st_delete(literal_keys, (key = (st_data_t)RNODE_LIT(head)->nd_lit, &key), &data)) {
14599 NODE *dup_value = (RNODE_LIST((NODE *)data))->nd_next;
14600 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
14601 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
14602 RNODE_LIT(head)->nd_lit, nd_line(head));
14603 if (dup_value == last_expr) {
14604 RNODE_LIST(value)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(value)->nd_head);
14607 RNODE_LIST(last_expr)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(last_expr)->nd_head);
14610 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
14611 last_expr = !head || nd_type_p(head, NODE_LIT) ? value : head;
14614 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
14615 st_free_table(literal_keys);
14617 if (!result) result = hash;
14618 else list_concat(result, hash);
14620 result->nd_loc = loc;
14625new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14627 if (hash) hash = remove_duplicate_keys(p, hash);
14628 return NEW_HASH(hash, loc);
14633error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14635 if (is_private_local_id(p, id)) {
14638 if (st_is_member(p->pvtbl, id)) {
14639 yyerror1(loc, "duplicated variable name");
14642 st_insert(p->pvtbl, (st_data_t)id, 0);
14647error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14650 p->pktbl = st_init_numtable();
14652 else if (st_is_member(p->pktbl, key)) {
14653 yyerror1(loc, "duplicated key name");
14656 st_insert(p->pktbl, (st_data_t)key, 0);
14661new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14663 return NEW_HASH(hash, loc);
14665#endif /* !RIPPER */
14669new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14674 ID vid = get_nd_vid(p, lhs);
14675 YYLTYPE lhs_loc = lhs->nd_loc;
14676 int shareable = ctxt.shareable_constant_value;
14678 switch (nd_type(lhs)) {
14689 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14690 set_nd_value(p, lhs, rhs);
14691 nd_set_loc(lhs, loc);
14692 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14694 else if (op == tANDOP) {
14696 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14698 set_nd_value(p, lhs, rhs);
14699 nd_set_loc(lhs, loc);
14700 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14704 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14706 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
14708 set_nd_value(p, asgn, rhs);
14709 nd_set_loc(asgn, loc);
14713 asgn = NEW_BEGIN(0, loc);
14719new_ary_op_assign(struct parser_params *p, NODE *ary,
14720 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
14724 args = make_list(args, args_loc);
14725 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc);
14731new_attr_op_assign(struct parser_params *p, NODE *lhs,
14732 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
14736 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
14742new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14747 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
14748 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
14751 asgn = NEW_BEGIN(0, loc);
14758const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14760 if (p->ctxt.in_def) {
14761 yyerror1(loc, "dynamic constant assignment");
14763 return NEW_CDECL(0, 0, (path), loc);
14767const_decl(struct parser_params *p, VALUE path)
14769 if (p->ctxt.in_def) {
14770 path = assign_error(p, "dynamic constant assignment", path);
14776assign_error(struct parser_params *p, const char *mesg, VALUE a)
14778 a = dispatch2(assign_error, ERR_MESG(), a);
14784var_field(struct parser_params *p, VALUE a)
14786 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
14792new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14794 NODE *result = head;
14796 NODE *tmp = rescue_else ? rescue_else : rescue;
14797 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14799 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14800 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14802 else if (rescue_else) {
14803 result = block_append(p, result, rescue_else);
14806 result = NEW_ENSURE(result, ensure, loc);
14808 fixpos(result, head);
14814warn_unused_var(struct parser_params *p, struct local_vars *local)
14818 if (!local->used) return;
14819 cnt = local->used->pos;
14820 if (cnt != local->vars->pos) {
14821 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14824 ID *v = local->vars->tbl;
14825 ID *u = local->used->tbl;
14826 for (int i = 0; i < cnt; ++i) {
14827 if (!v[i] || (u[i] & LVAR_USED)) continue;
14828 if (is_private_local_id(p, v[i])) continue;
14829 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14835local_push(struct parser_params *p, int toplevel_scope)
14837 struct local_vars *local;
14838 int inherits_dvars = toplevel_scope && compile_for_eval;
14839 int warn_unused_vars = RTEST(ruby_verbose);
14841 local = ALLOC(struct local_vars);
14842 local->prev = p->lvtbl;
14843 local->args = vtable_alloc(0);
14844 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14846 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14847 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14848 local->numparam.outer = 0;
14849 local->numparam.inner = 0;
14850 local->numparam.current = 0;
14852 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14854# if WARN_PAST_SCOPE
14863vtable_chain_free(struct parser_params *p, struct vtable *table)
14865 while (!DVARS_TERMINAL_P(table)) {
14866 struct vtable *cur_table = table;
14867 table = cur_table->prev;
14868 vtable_free(cur_table);
14873local_free(struct parser_params *p, struct local_vars *local)
14875 vtable_chain_free(p, local->used);
14877# if WARN_PAST_SCOPE
14878 vtable_chain_free(p, local->past);
14881 vtable_chain_free(p, local->args);
14882 vtable_chain_free(p, local->vars);
14884 ruby_sized_xfree(local, sizeof(struct local_vars));
14888local_pop(struct parser_params *p)
14890 struct local_vars *local = p->lvtbl->prev;
14891 if (p->lvtbl->used) {
14892 warn_unused_var(p, p->lvtbl);
14895 local_free(p, p->lvtbl);
14903static rb_ast_id_table_t *
14904local_tbl(struct parser_params *p)
14906 int cnt_args = vtable_size(p->lvtbl->args);
14907 int cnt_vars = vtable_size(p->lvtbl->vars);
14908 int cnt = cnt_args + cnt_vars;
14910 rb_ast_id_table_t *tbl;
14912 if (cnt <= 0) return 0;
14913 tbl = rb_ast_new_local_table(p->ast, cnt);
14914 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14915 /* remove IDs duplicated to warn shadowing */
14916 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
14917 ID id = p->lvtbl->vars->tbl[i];
14918 if (!vtable_included(p->lvtbl->args, id)) {
14919 tbl->ids[j++] = id;
14923 tbl = rb_ast_resize_latest_local_table(p->ast, j);
14932numparam_name(struct parser_params *p, ID id)
14934 if (!NUMPARAM_ID_P(id)) return;
14935 compile_error(p, "_%d is reserved for numbered parameter",
14936 NUMPARAM_ID_TO_IDX(id));
14940arg_var(struct parser_params *p, ID id)
14942 numparam_name(p, id);
14943 vtable_add(p->lvtbl->args, id);
14947local_var(struct parser_params *p, ID id)
14949 numparam_name(p, id);
14950 vtable_add(p->lvtbl->vars, id);
14951 if (p->lvtbl->used) {
14952 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
14957local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
14959 struct vtable *vars, *args, *used;
14961 vars = p->lvtbl->vars;
14962 args = p->lvtbl->args;
14963 used = p->lvtbl->used;
14965 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
14968 if (used) used = used->prev;
14971 if (vars && vars->prev == DVARS_INHERIT) {
14972 return rb_local_defined(id, p->parent_iseq);
14974 else if (vtable_included(args, id)) {
14978 int i = vtable_included(vars, id);
14979 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
14985local_id(struct parser_params *p, ID id)
14987 return local_id_ref(p, id, NULL);
14991check_forwarding_args(struct parser_params *p)
14993 if (local_id(p, idFWD_ALL)) return TRUE;
14994 compile_error(p, "unexpected ...");
14999add_forwarding_args(struct parser_params *p)
15001 arg_var(p, idFWD_REST);
15002#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15003 arg_var(p, idFWD_KWREST);
15005 arg_var(p, idFWD_BLOCK);
15006 arg_var(p, idFWD_ALL);
15010forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15012 bool conflict = false;
15014 struct vtable *vars, *args;
15016 vars = p->lvtbl->vars;
15017 args = p->lvtbl->args;
15019 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15022 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15025 bool found = false;
15026 if (vars && vars->prev == DVARS_INHERIT) {
15027 found = (rb_local_defined(arg, p->parent_iseq) &&
15028 !(all && rb_local_defined(all, p->parent_iseq)));
15031 found = (vtable_included(args, arg) &&
15032 !(all && vtable_included(args, all)));
15036 compile_error(p, "no anonymous %s parameter", var);
15038 else if (conflict) {
15039 compile_error(p, "anonymous %s parameter is also used within block", var);
15045new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15047 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15048#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15049 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15051 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
15052 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
15053#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15054 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
15056 return arg_blk_pass(args, block);
15061numparam_push(struct parser_params *p)
15064 struct local_vars *local = p->lvtbl;
15065 NODE *inner = local->numparam.inner;
15066 if (!local->numparam.outer) {
15067 local->numparam.outer = local->numparam.current;
15069 local->numparam.inner = 0;
15070 local->numparam.current = 0;
15078numparam_pop(struct parser_params *p, NODE *prev_inner)
15081 struct local_vars *local = p->lvtbl;
15083 /* prefer first one */
15084 local->numparam.inner = prev_inner;
15086 else if (local->numparam.current) {
15087 /* current and inner are exclusive */
15088 local->numparam.inner = local->numparam.current;
15090 if (p->max_numparam > NO_PARAM) {
15091 /* current and outer are exclusive */
15092 local->numparam.current = local->numparam.outer;
15093 local->numparam.outer = 0;
15096 /* no numbered parameter */
15097 local->numparam.current = 0;
15102static const struct vtable *
15103dyna_push(struct parser_params *p)
15105 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15106 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15107 if (p->lvtbl->used) {
15108 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15110 return p->lvtbl->args;
15114dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15116 struct vtable *tmp = *vtblp;
15117 *vtblp = tmp->prev;
15118# if WARN_PAST_SCOPE
15119 if (p->past_scope_enabled) {
15120 tmp->prev = p->lvtbl->past;
15121 p->lvtbl->past = tmp;
15129dyna_pop_1(struct parser_params *p)
15131 struct vtable *tmp;
15133 if ((tmp = p->lvtbl->used) != 0) {
15134 warn_unused_var(p, p->lvtbl);
15135 p->lvtbl->used = p->lvtbl->used->prev;
15138 dyna_pop_vtable(p, &p->lvtbl->args);
15139 dyna_pop_vtable(p, &p->lvtbl->vars);
15143dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15145 while (p->lvtbl->args != lvargs) {
15147 if (!p->lvtbl->args) {
15148 struct local_vars *local = p->lvtbl->prev;
15149 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15157dyna_in_block(struct parser_params *p)
15159 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15163dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15165 struct vtable *vars, *args, *used;
15168 args = p->lvtbl->args;
15169 vars = p->lvtbl->vars;
15170 used = p->lvtbl->used;
15172 while (!DVARS_TERMINAL_P(vars)) {
15173 if (vtable_included(args, id)) {
15176 if ((i = vtable_included(vars, id)) != 0) {
15177 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15182 if (!vidrefp) used = 0;
15183 if (used) used = used->prev;
15186 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15187 return rb_dvar_defined(id, p->parent_iseq);
15194dvar_defined(struct parser_params *p, ID id)
15196 return dvar_defined_ref(p, id, NULL);
15200dvar_curr(struct parser_params *p, ID id)
15202 return (vtable_included(p->lvtbl->args, id) ||
15203 vtable_included(p->lvtbl->vars, id));
15207reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
15210 "regexp encoding option '%c' differs from source encoding '%s'",
15211 c, rb_enc_name(rb_enc_get(str)));
15216rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15218 int c = RE_OPTION_ENCODING_IDX(options);
15222 rb_char_to_option_kcode(c, &opt, &idx);
15223 if (idx != ENCODING_GET(str) &&
15224 !is_ascii_string(str)) {
15227 ENCODING_SET(str, idx);
15229 else if (RE_OPTION_ENCODING_NONE(options)) {
15230 if (!ENCODING_IS_ASCII8BIT(str) &&
15231 !is_ascii_string(str)) {
15235 rb_enc_associate(str, rb_ascii8bit_encoding());
15237 else if (rb_is_usascii_enc(p->enc)) {
15238 if (!is_ascii_string(str)) {
15239 /* raise in re.c */
15240 rb_enc_associate(str, rb_usascii_encoding());
15243 rb_enc_associate(str, rb_ascii8bit_encoding());
15253reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
15255 int c = rb_reg_fragment_setenc(p, str, options);
15256 if (c) reg_fragment_enc_error(p, str, c);
15260reg_fragment_check(struct parser_params* p, VALUE str, int options)
15263 reg_fragment_setenc(p, str, options);
15264 err = rb_reg_check_preprocess(str);
15266 err = rb_obj_as_string(err);
15267 compile_error(p, "%"PRIsVALUE, err);
15273#ifndef UNIVERSAL_PARSER
15275 struct parser_params* parser;
15278 const YYLTYPE *loc;
15279} reg_named_capture_assign_t;
15282reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15283 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15285 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15286 struct parser_params* p = arg->parser;
15287 rb_encoding *enc = arg->enc;
15288 long len = name_end - name;
15289 const char *s = (const char *)name;
15291 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc);
15295reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
15297 reg_named_capture_assign_t arg;
15300 arg.enc = rb_enc_get(regexp);
15301 arg.succ_block = 0;
15303 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15305 if (!arg.succ_block) return 0;
15306 return RNODE_BLOCK(arg.succ_block)->nd_next;
15311rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15312 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc)
15317 if (!len) return ST_CONTINUE;
15318 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15319 return ST_CONTINUE;
15321 var = intern_cstr(s, len, enc);
15322 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15323 if (!lvar_defined(p, var)) return ST_CONTINUE;
15325 node = node_assign(p, assignable(p, var, 0, loc), NEW_LIT(ID2SYM(var), loc), NO_LEX_CTXT, loc);
15326 succ = *succ_block;
15327 if (!succ) succ = NEW_BEGIN(0, loc);
15328 succ = block_append(p, succ, node);
15329 *succ_block = succ;
15330 return ST_CONTINUE;
15334parser_reg_compile(struct parser_params* p, VALUE str, int options)
15336 reg_fragment_setenc(p, str, options);
15337 return rb_parser_reg_compile(p, str, options);
15341rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15343 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15347reg_compile(struct parser_params* p, VALUE str, int options)
15352 err = rb_errinfo();
15353 re = parser_reg_compile(p, str, options);
15355 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15356 rb_set_errinfo(err);
15357 compile_error(p, "%"PRIsVALUE, m);
15364parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
15366 VALUE err = rb_errinfo();
15368 str = ripper_is_node_yylval(p, str) ? RNODE_RIPPER(str)->nd_cval : str;
15369 int c = rb_reg_fragment_setenc(p, str, options);
15370 if (c) reg_fragment_enc_error(p, str, c);
15371 re = rb_parser_reg_compile(p, str, options);
15373 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
15374 rb_set_errinfo(err);
15382rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15384 p->do_print = print;
15386 p->do_chomp = chomp;
15387 p->do_split = split;
15391parser_append_options(struct parser_params *p, NODE *node)
15393 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15394 const YYLTYPE *const LOC = &default_location;
15397 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15398 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15400 node = block_append(p, node, print);
15404 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15407 ID ifs = rb_intern("$;");
15408 ID fields = rb_intern("$F");
15409 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15410 NODE *split = NEW_GASGN(fields,
15411 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15412 rb_intern("split"), args, LOC),
15414 node = block_append(p, split, node);
15417 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
15418 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15419 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15422 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
15431 /* just to suppress unused-function warnings */
15437internal_id(struct parser_params *p)
15439 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15441#endif /* !RIPPER */
15444parser_initialize(struct parser_params *p)
15446 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15447 p->command_start = TRUE;
15448 p->ruby_sourcefile_string = Qnil;
15449 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15451 p->delayed.token = Qnil;
15452 p->frozen_string_literal = -1; /* not specified */
15455 p->parsing_thread = Qnil;
15457 p->error_buffer = Qfalse;
15458 p->end_expect_token_locations = Qnil;
15462 p->debug_buffer = Qnil;
15463 p->debug_output = rb_ractor_stdout();
15464 p->enc = rb_utf8_encoding();
15469#define rb_ruby_parser_mark ripper_parser_mark
15470#define rb_ruby_parser_free ripper_parser_free
15471#define rb_ruby_parser_memsize ripper_parser_memsize
15475rb_ruby_parser_mark(void *ptr)
15477 struct parser_params *p = (struct parser_params*)ptr;
15479 rb_gc_mark(p->lex.input);
15480 rb_gc_mark(p->lex.lastline);
15481 rb_gc_mark(p->lex.nextline);
15482 rb_gc_mark(p->ruby_sourcefile_string);
15483 rb_gc_mark((VALUE)p->ast);
15484 rb_gc_mark(p->case_labels);
15485 rb_gc_mark(p->delayed.token);
15487 rb_gc_mark(p->debug_lines);
15488 rb_gc_mark(p->error_buffer);
15489 rb_gc_mark(p->end_expect_token_locations);
15490 rb_gc_mark(p->tokens);
15492 rb_gc_mark(p->value);
15493 rb_gc_mark(p->result);
15494 rb_gc_mark(p->parsing_thread);
15496 rb_gc_mark(p->debug_buffer);
15497 rb_gc_mark(p->debug_output);
15499 rb_gc_mark((VALUE)p->heap);
15504rb_ruby_parser_free(void *ptr)
15506 struct parser_params *p = (struct parser_params*)ptr;
15507 struct local_vars *local, *prev;
15508#ifdef UNIVERSAL_PARSER
15509 rb_parser_config_t *config = p->config;
15513 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15516 for (local = p->lvtbl; local; local = prev) {
15517 prev = local->prev;
15518 local_free(p, local);
15522 token_info *ptinfo;
15523 while ((ptinfo = p->token_info) != 0) {
15524 p->token_info = ptinfo->next;
15530#ifdef UNIVERSAL_PARSER
15532 if (config->counter <= 0) {
15533 rb_ruby_parser_config_free(config);
15539rb_ruby_parser_memsize(const void *ptr)
15541 struct parser_params *p = (struct parser_params*)ptr;
15542 struct local_vars *local;
15543 size_t size = sizeof(*p);
15546 for (local = p->lvtbl; local; local = local->prev) {
15547 size += sizeof(*local);
15548 if (local->vars) size += local->vars->capa * sizeof(ID);
15553#ifdef UNIVERSAL_PARSER
15554rb_parser_config_t *
15555rb_ruby_parser_config_new(void *(*malloc)(size_t size))
15557 return (rb_parser_config_t *)malloc(sizeof(rb_parser_config_t));
15561rb_ruby_parser_config_free(rb_parser_config_t *config)
15563 config->free(config);
15567#ifndef UNIVERSAL_PARSER
15569static const rb_data_type_t parser_data_type = {
15572 rb_ruby_parser_mark,
15573 rb_ruby_parser_free,
15574 rb_ruby_parser_memsize,
15576 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
15582#undef rb_reserved_word
15584const struct kwtable *
15585rb_reserved_word(const char *str, unsigned int len)
15587 return reserved_word(str, len);
15590#ifdef UNIVERSAL_PARSER
15592rb_ruby_parser_allocate(rb_parser_config_t *config)
15594 /* parser_initialize expects fields to be set to 0 */
15595 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15596 p->config = config;
15597 p->config->counter++;
15602rb_ruby_parser_new(rb_parser_config_t *config)
15604 /* parser_initialize expects fields to be set to 0 */
15605 rb_parser_t *p = rb_ruby_parser_allocate(config);
15606 parser_initialize(p);
15612rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15614 p->error_buffer = main ? Qfalse : Qnil;
15615 p->parent_iseq = base;
15620rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines)
15622 if (!RTEST(lines)) {
15625 else if (lines == Qtrue) {
15626 lines = rb_ary_new();
15629 Check_Type(lines, T_ARRAY);
15630 rb_ary_modify(lines);
15632 p->debug_lines = lines;
15636rb_ruby_parser_error_tolerant(rb_parser_t *p)
15638 p->error_tolerant = 1;
15640 p->end_expect_token_locations = rb_ary_new();
15644rb_ruby_parser_keep_tokens(rb_parser_t *p)
15646 p->keep_tokens = 1;
15648 p->tokens = rb_ary_new();
15651#ifndef UNIVERSAL_PARSER
15653rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
15655 struct parser_params *p;
15657 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15658 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15659 return rb_ruby_parser_compile_file_path(p, fname, file, start);
15663rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
15665 struct parser_params *p;
15667 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15668 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15669 return rb_ruby_parser_compile_generic(p, lex_gets, fname, input, start);
15673rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
15675 struct parser_params *p;
15677 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15678 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15679 return rb_ruby_parser_compile_string(p, f, s, line);
15683rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
15685 struct parser_params *p;
15687 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15688 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
15689 return rb_ruby_parser_compile_string_path(p, f, s, line);
15693rb_parser_encoding(VALUE vparser)
15695 struct parser_params *p;
15697 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15698 return rb_ruby_parser_encoding(p);
15702rb_parser_end_seen_p(VALUE vparser)
15704 struct parser_params *p;
15706 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15707 return RBOOL(rb_ruby_parser_end_seen_p(p));
15711rb_parser_error_tolerant(VALUE vparser)
15713 struct parser_params *p;
15715 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15716 rb_ruby_parser_error_tolerant(p);
15720rb_parser_set_script_lines(VALUE vparser, VALUE lines)
15722 struct parser_params *p;
15724 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15725 rb_ruby_parser_set_script_lines(p, lines);
15729rb_parser_keep_tokens(VALUE vparser)
15731 struct parser_params *p;
15733 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15734 rb_ruby_parser_keep_tokens(p);
15740 struct parser_params *p;
15741 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
15742 &parser_data_type, p);
15743 parser_initialize(p);
15748rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
15750 struct parser_params *p;
15752 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15753 rb_ruby_parser_set_context(p, base, main);
15758rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
15760 struct parser_params *p;
15762 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
15763 rb_ruby_parser_set_options(p, print, loop, chomp, split);
15767rb_parser_set_yydebug(VALUE self, VALUE flag)
15769 struct parser_params *p;
15771 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
15772 rb_ruby_parser_set_yydebug(p, RTEST(flag));
15775#endif /* !UNIVERSAL_PARSER */
15778rb_ruby_parser_encoding(rb_parser_t *p)
15780 return rb_enc_from_encoding(p->enc);
15784rb_ruby_parser_end_seen_p(rb_parser_t *p)
15786 return p->ruby__end__seen;
15790rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15795#endif /* !RIPPER */
15799rb_ruby_parser_get_yydebug(rb_parser_t *p)
15805rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15811rb_ruby_parser_error_p(rb_parser_t *p)
15817rb_ruby_parser_debug_output(rb_parser_t *p)
15819 return p->debug_output;
15823rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15825 p->debug_output = output;
15829rb_ruby_parser_parsing_thread(rb_parser_t *p)
15831 return p->parsing_thread;
15835rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15837 p->parsing_thread = parsing_thread;
15841rb_ruby_parser_ripper_initialize(rb_parser_t *p, VALUE (*gets)(struct parser_params*,VALUE), VALUE input, VALUE sourcefile_string, const char *sourcefile, int sourceline)
15843 p->lex.gets = gets;
15844 p->lex.input = input;
15846 p->ruby_sourcefile_string = sourcefile_string;
15847 p->ruby_sourcefile = sourcefile;
15848 p->ruby_sourceline = sourceline;
15852rb_ruby_parser_result(rb_parser_t *p)
15858rb_ruby_parser_enc(rb_parser_t *p)
15864rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15866 return p->ruby_sourcefile_string;
15870rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15872 return p->ruby_sourceline;
15876rb_ruby_parser_lex_state(rb_parser_t *p)
15878 return p->lex.state;
15882rb_ruby_ripper_parse0(rb_parser_t *p)
15885 p->ast = rb_ast_new();
15886 ripper_yyparse((void*)p);
15887 rb_ast_dispose(p->ast);
15892rb_ruby_ripper_dedent_string(rb_parser_t *p, VALUE string, int width)
15894 return dedent_string(p, string, width);
15898rb_ruby_ripper_lex_get_str(rb_parser_t *p, VALUE s)
15900 return lex_get_str(p, s);
15904rb_ruby_ripper_initialized_p(rb_parser_t *p)
15906 return p->lex.input != 0;
15910rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15912 parser_initialize(p);
15916rb_ruby_ripper_column(rb_parser_t *p)
15918 return p->lex.ptok - p->lex.pbeg;
15922rb_ruby_ripper_token_len(rb_parser_t *p)
15924 return p->lex.pcur - p->lex.ptok;
15928rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15930 return p->lex.lastline;
15934rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15936 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15939struct parser_params*
15940rb_ruby_ripper_parser_allocate(void)
15942 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15948#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
15949/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
15950 * potential memory leak */
15951#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
15952#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
15953 (new)->cnt = (cnt), (ptr))
15956rb_parser_malloc(struct parser_params *p, size_t size)
15958 size_t cnt = HEAPCNT(1, size);
15959 rb_imemo_tmpbuf_t *n = NEWHEAP();
15960 void *ptr = xmalloc(size);
15962 return ADD2HEAP(n, cnt, ptr);
15966rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
15968 size_t cnt = HEAPCNT(nelem, size);
15969 rb_imemo_tmpbuf_t *n = NEWHEAP();
15970 void *ptr = xcalloc(nelem, size);
15972 return ADD2HEAP(n, cnt, ptr);
15976rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
15978 rb_imemo_tmpbuf_t *n;
15979 size_t cnt = HEAPCNT(1, size);
15981 if (ptr && (n = p->heap) != NULL) {
15983 if (n->ptr == ptr) {
15984 n->ptr = ptr = xrealloc(ptr, size);
15985 if (n->cnt) n->cnt = cnt;
15988 } while ((n = n->next) != NULL);
15991 ptr = xrealloc(ptr, size);
15992 return ADD2HEAP(n, cnt, ptr);
15996rb_parser_free(struct parser_params *p, void *ptr)
15998 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
16000 while ((n = *prev) != NULL) {
16001 if (n->ptr == ptr) {
16011rb_parser_printf(struct parser_params *p, const char *fmt, ...)
16014 VALUE mesg = p->debug_buffer;
16016 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
16018 rb_str_vcatf(mesg, fmt, ap);
16020 if (end_with_newline_p(p, mesg)) {
16021 rb_io_write(p->debug_output, mesg);
16022 p->debug_buffer = Qnil;
16027parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
16030 int lineno, column;
16033 lineno = loc->end_pos.lineno;
16034 column = loc->end_pos.column;
16037 lineno = p->ruby_sourceline;
16038 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
16041 rb_io_flush(p->debug_output);
16045 rb_syntax_error_append(p->error_buffer,
16046 p->ruby_sourcefile_string,
16053count_char(const char *str, int c)
16056 while (str[n] == c) ++n;
16061 * strip enclosing double-quotes, same as the default yytnamerr except
16062 * for that single-quotes matching back-quotes do not stop stripping.
16064 * "\"`class' keyword\"" => "`class' keyword"
16066RUBY_FUNC_EXPORTED size_t
16067rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16069 if (*yystr == '"') {
16070 size_t yyn = 0, bquote = 0;
16071 const char *yyp = yystr;
16077 bquote = count_char(yyp+1, '`') + 1;
16078 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16086 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16087 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16093 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16094 if (yyres) memcpy(yyres + yyn, yyp, 3);
16099 goto do_not_strip_quotes;
16102 goto do_not_strip_quotes;
16105 if (*++yyp != '\\')
16106 goto do_not_strip_quotes;
16107 /* Fall through. */
16122 do_not_strip_quotes: ;
16125 if (!yyres) return strlen(yystr);
16127 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16135ripper_validate_object(VALUE self, VALUE x)
16137 if (x == Qfalse) return x;
16138 if (x == Qtrue) return x;
16139 if (NIL_P(x)) return x;
16141 rb_raise(rb_eArgError, "Qundef given");
16142 if (FIXNUM_P(x)) return x;
16143 if (SYMBOL_P(x)) return x;
16144 switch (BUILTIN_TYPE(x)) {
16154 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
16155 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
16157 x = ((NODE *)x)->nd_rval;
16160 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
16161 (void *)x, rb_obj_classname(x));
16163 if (!RBASIC_CLASS(x)) {
16164 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
16165 (void *)x, rb_builtin_type_name(TYPE(x)));
16171#define validate(x) ((x) = get_value(x))
16174ripper_dispatch0(struct parser_params *p, ID mid)
16176 return rb_funcall(p->value, mid, 0);
16180ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16183 return rb_funcall(p->value, mid, 1, a);
16187ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16191 return rb_funcall(p->value, mid, 2, a, b);
16195ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16200 return rb_funcall(p->value, mid, 3, a, b, c);
16204ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16210 return rb_funcall(p->value, mid, 4, a, b, c, d);
16214ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16221 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16225ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16234 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16238ripper_error(struct parser_params *p)
16244ripper_value(struct parser_params *p)
16246 (void)yystpcpy; /* may not used in newer bison */
16255 * c-file-style: "ruby"