diff options
Diffstat (limited to 'parse.y')
| -rw-r--r-- | parse.y | 9288 |
1 files changed, 4589 insertions, 4699 deletions
@@ -9,8 +9,6 @@ **********************************************************************/ -%require "3.0" - %{ #if !YYPURE @@ -19,8 +17,6 @@ #define YYDEBUG 1 #define YYERROR_VERBOSE 1 #define YYSTACK_USE_ALLOCA 0 -#define YYLTYPE rb_code_location_t -#define YYLTYPE_IS_DECLARED 1 /* For Ripper */ #ifdef RUBY_EXTCONF_H @@ -38,8 +34,6 @@ #include "universal_parser.c" #ifdef RIPPER -#undef T_NODE -#define T_NODE 0x1b #define STATIC_ID2SYM p->config->static_id2sym #define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable #endif @@ -53,7 +47,6 @@ #include "internal/encoding.h" #include "internal/error.h" #include "internal/hash.h" -#include "internal/imemo.h" #include "internal/io.h" #include "internal/numeric.h" #include "internal/parse.h" @@ -76,89 +69,151 @@ #include "symbol.h" #ifndef RIPPER -static void -bignum_negate(VALUE b) +static VALUE +syntax_error_new(void) { - BIGNUM_NEGATE(b); + return rb_class_new_instance(0, 0, rb_eSyntaxError); } +#endif -static void -rational_set_num(VALUE r, VALUE n) -{ - RATIONAL_SET_NUM(r, n); -} +static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable); -static VALUE -rational_get_num(VALUE obj) -{ - return RRATIONAL(obj)->num; -} +#define compile_callback rb_suppress_tracing +#endif /* !UNIVERSAL_PARSER */ -static void -rcomplex_set_real(VALUE cmp, VALUE r) +#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1) +#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS) + +static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2); + +#ifndef RIPPER +static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original); +#endif + +static int +node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2) { - RCOMPLEX_SET_REAL(cmp, r); + return (n1->minus != n2->minus || + n1->base != n2->base || + strcmp(n1->val, n2->val)); } -static VALUE -rcomplex_get_real(VALUE obj) +static int +node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2) { - return RCOMPLEX(obj)->real; + return (n1->minus != n2->minus || + strcmp(n1->val, n2->val)); } -static void -rcomplex_set_imag(VALUE cmp, VALUE i) +static int +node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2) { - RCOMPLEX_SET_IMAG(cmp, i); + return (n1->minus != n2->minus || + n1->base != n2->base || + n1->seen_point != n2->seen_point || + strcmp(n1->val, n2->val)); } -static VALUE -rcomplex_get_imag(VALUE obj) +static int +node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2) { - return RCOMPLEX(obj)->imag; + return (n1->minus != n2->minus || + n1->base != n2->base || + n1->seen_point != n2->seen_point || + n1->type != n2->type || + strcmp(n1->val, n2->val)); } -static bool -hash_literal_key_p(VALUE k) +static int +rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2) { - switch (OBJ_BUILTIN_TYPE(k)) { - case T_NODE: - return false; - default: - return true; - } + return (n1->options != n2->options || + rb_parser_string_hash_cmp(n1->string, n2->string)); } +static st_index_t rb_parser_str_hash(rb_parser_string_t *str); +static st_index_t rb_char_p_hash(const char *c); + static int -literal_cmp(VALUE val, VALUE lit) +literal_cmp(st_data_t val, st_data_t lit) { if (val == lit) return 0; - if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1; - return rb_iseq_cdhash_cmp(val, lit); + + NODE *node_val = RNODE(val); + NODE *node_lit = RNODE(lit); + enum node_type type_val = nd_type(node_val); + enum node_type type_lit = nd_type(node_lit); + + if (type_val != type_lit) { + return -1; + } + + switch (type_lit) { + case NODE_INTEGER: + return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit)); + case NODE_FLOAT: + return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit)); + case NODE_RATIONAL: + return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit)); + case NODE_IMAGINARY: + return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit)); + case NODE_STR: + return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string); + case NODE_SYM: + return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string); + case NODE_REGX: + return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit)); + case NODE_LINE: + return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno; + case NODE_FILE: + return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path); + case NODE_ENCODING: + return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc; + default: +#ifdef UNIVERSAL_PARSER + abort(); +#else + rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit)); +#endif + } } static st_index_t -literal_hash(VALUE a) +literal_hash(st_data_t a) { - if (!hash_literal_key_p(a)) return (st_index_t)a; - return rb_iseq_cdhash_hash(a); -} + NODE *node = (NODE *)a; + enum node_type type = nd_type(node); -static VALUE -syntax_error_new(void) -{ - return rb_class_new_instance(0, 0, rb_eSyntaxError); + switch (type) { + case NODE_INTEGER: + return rb_char_p_hash(RNODE_INTEGER(node)->val); + case NODE_FLOAT: + return rb_char_p_hash(RNODE_FLOAT(node)->val); + case NODE_RATIONAL: + return rb_char_p_hash(RNODE_RATIONAL(node)->val); + case NODE_IMAGINARY: + return rb_char_p_hash(RNODE_IMAGINARY(node)->val); + case NODE_STR: + return rb_parser_str_hash(RNODE_STR(node)->string); + case NODE_SYM: + return rb_parser_str_hash(RNODE_SYM(node)->string); + case NODE_REGX: + return rb_parser_str_hash(RNODE_REGX(node)->string); + case NODE_LINE: + return (st_index_t)node->nd_loc.beg_pos.lineno; + case NODE_FILE: + return rb_parser_str_hash(RNODE_FILE(node)->path); + case NODE_ENCODING: + return (st_index_t)RNODE_ENCODING(node)->enc; + default: +#ifdef UNIVERSAL_PARSER + abort(); +#else + rb_bug("unexpected node: %s", ruby_node_name(type)); +#endif + } } -static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc); -#endif /* !RIPPER */ - -#define compile_callback rb_suppress_tracing -VALUE rb_io_gets_internal(VALUE io); - -VALUE rb_node_case_when_optimizable_literal(const NODE *const node); -#endif /* !UNIVERSAL_PARSER */ - static inline int parse_isascii(int c) { @@ -219,7 +274,7 @@ parse_isdigit(int c) static inline int parse_isalnum(int c) { - return parse_isalpha(c) || parse_isdigit(c); + return ISALPHA(c) || ISDIGIT(c); } #undef ISALNUM @@ -228,7 +283,7 @@ parse_isalnum(int c) static inline int parse_isxdigit(int c) { - return parse_isdigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f'); + return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f'); } #undef ISXDIGIT @@ -246,13 +301,6 @@ parse_isxdigit(int c) #include "ripper_init.h" #endif -enum shareability { - shareable_none, - shareable_literal, - shareable_copy, - shareable_everything, -}; - enum rescue_context { before_rescue, after_rescue, @@ -266,12 +314,15 @@ struct lex_context { unsigned int in_argdef: 1; unsigned int in_def: 1; unsigned int in_class: 1; - BITFIELD(enum shareability, shareable_constant_value, 2); + unsigned int has_trailing_semicolon: 1; + BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2); BITFIELD(enum rescue_context, in_rescue, 2); + unsigned int cant_return: 1; + unsigned int in_alt_pattern: 1; + unsigned int capture_in_pattern: 1; }; typedef struct RNode_DEF_TEMP rb_node_def_temp_t; -typedef struct RNode_EXITS rb_node_exits_t; #if defined(__GNUC__) && !defined(__clang__) // Suppress "parameter passing for argument of type 'struct @@ -287,8 +338,6 @@ RBIMPL_WARNING_POP() #define NO_LEX_CTXT (struct lex_context){0} -#define AREF(ary, i) RARRAY_AREF(ary, i) - #ifndef WARN_PAST_SCOPE # define WARN_PAST_SCOPE 0 #endif @@ -297,10 +346,6 @@ RBIMPL_WARNING_POP() #define yydebug (p->debug) /* disable the global variable definition */ -#define YYMALLOC(size) rb_parser_malloc(p, (size)) -#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size)) -#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size)) -#define YYFREE(ptr) rb_parser_free(p, (ptr)) #define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__) #define YY_LOCATION_PRINT(File, loc, p) \ rb_parser_printf(p, "%d.%d-%d.%d", \ @@ -389,13 +434,18 @@ struct local_vars { struct vtable *past; # endif struct local_vars *prev; -# ifndef RIPPER struct { NODE *outer, *inner, *current; } numparam; -# endif + NODE *it; }; +typedef struct rb_locations_lambda_body_t { + NODE *node; + YYLTYPE opening_loc; + YYLTYPE closing_loc; +} rb_locations_lambda_body_t; + enum { ORDINAL_PARAM = -1, NO_PARAM = 0, @@ -414,6 +464,25 @@ typedef struct token_info { struct token_info *next; } token_info; +typedef struct end_expect_token_locations { + const rb_code_position_t *pos; + struct end_expect_token_locations *prev; +} end_expect_token_locations_t; + +typedef struct parser_string_buffer_elem { + struct parser_string_buffer_elem *next; + long len; /* Total length of allocated buf */ + long used; /* Current usage of buf */ + rb_parser_string_t *buf[FLEX_ARY_LEN]; +} parser_string_buffer_elem_t; + +typedef struct parser_string_buffer { + parser_string_buffer_elem_t *head; + parser_string_buffer_elem_t *last; +} parser_string_buffer_t; + +#define AFTER_HEREDOC_WITHOUT_TERMINATOR ((rb_parser_string_t *)1) + /* Structure of Lexer Buffer: @@ -424,25 +493,20 @@ typedef struct token_info { token */ struct parser_params { - rb_imemo_tmpbuf_t *heap; - YYSTYPE *lval; YYLTYPE *yylloc; struct { rb_strterm_t *strterm; - VALUE (*gets)(struct parser_params*,VALUE); - VALUE input; - VALUE lastline; - VALUE nextline; + rb_parser_lex_gets_func *gets; + rb_parser_input_data input; + parser_string_buffer_t string_buffer; + rb_parser_string_t *lastline; + rb_parser_string_t *nextline; const char *pbeg; const char *pcur; const char *pend; const char *ptok; - union { - long ptr; - VALUE (*call)(VALUE, int); - } gets_; enum lex_state_e state; /* track the nest level of any parens "()[]{}" */ int paren_nest; @@ -468,31 +532,36 @@ struct parser_params { VALUE ruby_sourcefile_string; rb_encoding *enc; token_info *token_info; - VALUE case_labels; + st_table *case_labels; rb_node_exits_t *exits; VALUE debug_buffer; VALUE debug_output; struct { - VALUE token; + rb_parser_string_t *token; int beg_line; int beg_col; int end_line; int end_col; } delayed; - ID cur_arg; - rb_ast_t *ast; int node_id; + st_table *warn_duplicate_keys_table; + int max_numparam; + ID it_id; struct lex_context ctxt; + NODE *eval_tree_begin; + NODE *eval_tree; + const struct rb_iseq_struct *parent_iseq; + #ifdef UNIVERSAL_PARSER - rb_parser_config_t *config; + const rb_parser_config_t *config; #endif /* compile_option */ signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */ @@ -520,23 +589,26 @@ struct parser_params { unsigned int error_tolerant: 1; unsigned int keep_tokens: 1; - NODE *eval_tree_begin; - NODE *eval_tree; VALUE error_buffer; - VALUE debug_lines; - const struct rb_iseq_struct *parent_iseq; - /* store specific keyword locations to generate dummy end token */ - VALUE end_expect_token_locations; + rb_parser_ary_t *debug_lines; + /* + * Store specific keyword locations to generate dummy end token. + * Refer to the tail of list element. + */ + end_expect_token_locations_t *end_expect_token_locations; /* id for terms */ int token_id; /* Array for term tokens */ - VALUE tokens; + rb_parser_ary_t *tokens; #else /* Ripper only */ VALUE value; VALUE result; VALUE parsing_thread; + VALUE s_value; /* Token VALUE */ + VALUE s_lvalue; /* VALUE generated by rule action (reduce) */ + VALUE s_value_stack; #endif }; @@ -552,9 +624,90 @@ numparam_id_p(struct parser_params *p, ID id) } static void numparam_name(struct parser_params *p, ID id); +#ifdef RIPPER +static void +after_shift(struct parser_params *p) +{ + if (p->debug) { + rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value); + } + rb_ary_push(p->s_value_stack, p->s_value); + p->s_value = Qnil; +} + +static void +before_reduce(int len, struct parser_params *p) +{ + // Initialize $$ with $1. + if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len); +} + +static void +after_reduce(int len, struct parser_params *p) +{ + for (int i = 0; i < len; i++) { + VALUE tos = rb_ary_pop(p->s_value_stack); + if (p->debug) { + rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos); + } + } + if (p->debug) { + rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue); + } + rb_ary_push(p->s_value_stack, p->s_lvalue); + p->s_lvalue = Qnil; +} + +static void +after_shift_error_token(struct parser_params *p) +{ + if (p->debug) { + rb_parser_printf(p, "after-shift-error-token:\n"); + } + rb_ary_push(p->s_value_stack, Qnil); +} + +static void +after_pop_stack(int len, struct parser_params *p) +{ + for (int i = 0; i < len; i++) { + VALUE tos = rb_ary_pop(p->s_value_stack); + if (p->debug) { + rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos); + } + } +} +#else +static void +after_shift(struct parser_params *p) +{ +} + +static void +before_reduce(int len, struct parser_params *p) +{ +} + +static void +after_reduce(int len, struct parser_params *p) +{ +} + +static void +after_shift_error_token(struct parser_params *p) +{ +} + +static void +after_pop_stack(int len, struct parser_params *p) +{ +} +#endif #define intern_cstr(n,l,en) rb_intern3(n,l,en) +#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc) + #define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc) #define STR_NEW0() rb_enc_str_new(0,0,p->enc) #define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc) @@ -562,6 +715,15 @@ static void numparam_name(struct parser_params *p, ID id); #define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc) #define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type)) +#ifndef RIPPER +static inline int +char_at_end(struct parser_params *p, VALUE str, int when_empty) +{ + long len = RSTRING_LEN(str); + return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty; +} +#endif + static void pop_pvtbl(struct parser_params *p, st_table *tbl) { @@ -576,6 +738,57 @@ pop_pktbl(struct parser_params *p, st_table *tbl) p->pktbl = tbl; } +#define STRING_BUF_DEFAULT_LEN 16 + +static void +string_buffer_init(struct parser_params *p) +{ + parser_string_buffer_t *buf = &p->lex.string_buffer; + const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN; + + buf->head = buf->last = xmalloc(size); + buf->head->len = STRING_BUF_DEFAULT_LEN; + buf->head->used = 0; + buf->head->next = NULL; +} + +static void +string_buffer_append(struct parser_params *p, rb_parser_string_t *str) +{ + parser_string_buffer_t *buf = &p->lex.string_buffer; + + if (buf->head->used >= buf->head->len) { + parser_string_buffer_elem_t *elem; + long n = buf->head->len * 2; + const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n; + + elem = xmalloc(size); + elem->len = n; + elem->used = 0; + elem->next = NULL; + buf->last->next = elem; + buf->last = elem; + } + buf->last->buf[buf->last->used++] = str; +} + +static void +string_buffer_free(struct parser_params *p) +{ + parser_string_buffer_elem_t *elem = p->lex.string_buffer.head; + + while (elem) { + parser_string_buffer_elem_t *next_elem = elem->next; + + for (long i = 0; i < elem->used; i++) { + rb_parser_string_free(p, elem->buf[i]); + } + + xfree(elem); + elem = next_elem; + } +} + #ifndef RIPPER static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str); @@ -583,8 +796,15 @@ static void debug_end_expect_token_locations(struct parser_params *p, const char *name) { if(p->debug) { - VALUE mesg = rb_sprintf("%s: ", name); - rb_str_catf(mesg, " %"PRIsVALUE"\n", p->end_expect_token_locations); + VALUE mesg = rb_sprintf("%s: [", name); + int i = 0; + for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) { + if (i > 0) + rb_str_cat_cstr(mesg, ", "); + rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column); + i++; + } + rb_str_cat_cstr(mesg, "]\n"); flush_debug_buffer(p, p->debug_output, mesg); } } @@ -592,197 +812,214 @@ debug_end_expect_token_locations(struct parser_params *p, const char *name) static void push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos) { - if(NIL_P(p->end_expect_token_locations)) return; - rb_ary_push(p->end_expect_token_locations, rb_ary_new_from_args(2, INT2NUM(pos->lineno), INT2NUM(pos->column))); + if(!p->error_tolerant) return; + + end_expect_token_locations_t *locations; + locations = ALLOC(end_expect_token_locations_t); + locations->pos = pos; + locations->prev = p->end_expect_token_locations; + p->end_expect_token_locations = locations; + debug_end_expect_token_locations(p, "push_end_expect_token_locations"); } static void pop_end_expect_token_locations(struct parser_params *p) { - if(NIL_P(p->end_expect_token_locations)) return; - rb_ary_pop(p->end_expect_token_locations); + if(!p->end_expect_token_locations) return; + + end_expect_token_locations_t *locations = p->end_expect_token_locations->prev; + ruby_xfree_sized(p->end_expect_token_locations, sizeof(end_expect_token_locations_t)); + p->end_expect_token_locations = locations; + debug_end_expect_token_locations(p, "pop_end_expect_token_locations"); } -static VALUE +static end_expect_token_locations_t * peek_end_expect_token_locations(struct parser_params *p) { - if(NIL_P(p->end_expect_token_locations)) return Qnil; - return rb_ary_last(0, 0, p->end_expect_token_locations); + return p->end_expect_token_locations; } -static ID -parser_token2id(struct parser_params *p, enum yytokentype tok) +static const char * +parser_token2char(struct parser_params *p, enum yytokentype tok) { switch ((int) tok) { -#define TOKEN2ID(tok) case tok: return rb_intern(#tok); -#define TOKEN2ID2(tok, name) case tok: return rb_intern(name); - TOKEN2ID2(' ', "words_sep") - TOKEN2ID2('!', "!") - TOKEN2ID2('%', "%"); - TOKEN2ID2('&', "&"); - TOKEN2ID2('*', "*"); - TOKEN2ID2('+', "+"); - TOKEN2ID2('-', "-"); - TOKEN2ID2('/', "/"); - TOKEN2ID2('<', "<"); - TOKEN2ID2('=', "="); - TOKEN2ID2('>', ">"); - TOKEN2ID2('?', "?"); - TOKEN2ID2('^', "^"); - TOKEN2ID2('|', "|"); - TOKEN2ID2('~', "~"); - TOKEN2ID2(':', ":"); - TOKEN2ID2(',', ","); - TOKEN2ID2('.', "."); - TOKEN2ID2(';', ";"); - TOKEN2ID2('`', "`"); - TOKEN2ID2('\n', "nl"); - TOKEN2ID2('{', "{"); - TOKEN2ID2('}', "}"); - TOKEN2ID2('[', "["); - TOKEN2ID2(']', "]"); - TOKEN2ID2('(', "("); - TOKEN2ID2(')', ")"); - TOKEN2ID2('\\', "backslash"); - TOKEN2ID(keyword_class); - TOKEN2ID(keyword_module); - TOKEN2ID(keyword_def); - TOKEN2ID(keyword_undef); - TOKEN2ID(keyword_begin); - TOKEN2ID(keyword_rescue); - TOKEN2ID(keyword_ensure); - TOKEN2ID(keyword_end); - TOKEN2ID(keyword_if); - TOKEN2ID(keyword_unless); - TOKEN2ID(keyword_then); - TOKEN2ID(keyword_elsif); - TOKEN2ID(keyword_else); - TOKEN2ID(keyword_case); - TOKEN2ID(keyword_when); - TOKEN2ID(keyword_while); - TOKEN2ID(keyword_until); - TOKEN2ID(keyword_for); - TOKEN2ID(keyword_break); - TOKEN2ID(keyword_next); - TOKEN2ID(keyword_redo); - TOKEN2ID(keyword_retry); - TOKEN2ID(keyword_in); - TOKEN2ID(keyword_do); - TOKEN2ID(keyword_do_cond); - TOKEN2ID(keyword_do_block); - TOKEN2ID(keyword_do_LAMBDA); - TOKEN2ID(keyword_return); - TOKEN2ID(keyword_yield); - TOKEN2ID(keyword_super); - TOKEN2ID(keyword_self); - TOKEN2ID(keyword_nil); - TOKEN2ID(keyword_true); - TOKEN2ID(keyword_false); - TOKEN2ID(keyword_and); - TOKEN2ID(keyword_or); - TOKEN2ID(keyword_not); - TOKEN2ID(modifier_if); - TOKEN2ID(modifier_unless); - TOKEN2ID(modifier_while); - TOKEN2ID(modifier_until); - TOKEN2ID(modifier_rescue); - TOKEN2ID(keyword_alias); - TOKEN2ID(keyword_defined); - TOKEN2ID(keyword_BEGIN); - TOKEN2ID(keyword_END); - TOKEN2ID(keyword__LINE__); - TOKEN2ID(keyword__FILE__); - TOKEN2ID(keyword__ENCODING__); - TOKEN2ID(tIDENTIFIER); - TOKEN2ID(tFID); - TOKEN2ID(tGVAR); - TOKEN2ID(tIVAR); - TOKEN2ID(tCONSTANT); - TOKEN2ID(tCVAR); - TOKEN2ID(tLABEL); - TOKEN2ID(tINTEGER); - TOKEN2ID(tFLOAT); - TOKEN2ID(tRATIONAL); - TOKEN2ID(tIMAGINARY); - TOKEN2ID(tCHAR); - TOKEN2ID(tNTH_REF); - TOKEN2ID(tBACK_REF); - TOKEN2ID(tSTRING_CONTENT); - TOKEN2ID(tREGEXP_END); - TOKEN2ID(tDUMNY_END); - TOKEN2ID(tSP); - TOKEN2ID(tUPLUS); - TOKEN2ID(tUMINUS); - TOKEN2ID(tPOW); - TOKEN2ID(tCMP); - TOKEN2ID(tEQ); - TOKEN2ID(tEQQ); - TOKEN2ID(tNEQ); - TOKEN2ID(tGEQ); - TOKEN2ID(tLEQ); - TOKEN2ID(tANDOP); - TOKEN2ID(tOROP); - TOKEN2ID(tMATCH); - TOKEN2ID(tNMATCH); - TOKEN2ID(tDOT2); - TOKEN2ID(tDOT3); - TOKEN2ID(tBDOT2); - TOKEN2ID(tBDOT3); - TOKEN2ID(tAREF); - TOKEN2ID(tASET); - TOKEN2ID(tLSHFT); - TOKEN2ID(tRSHFT); - TOKEN2ID(tANDDOT); - TOKEN2ID(tCOLON2); - TOKEN2ID(tCOLON3); - TOKEN2ID(tOP_ASGN); - TOKEN2ID(tASSOC); - TOKEN2ID(tLPAREN); - TOKEN2ID(tLPAREN_ARG); - TOKEN2ID(tRPAREN); - TOKEN2ID(tLBRACK); - TOKEN2ID(tLBRACE); - TOKEN2ID(tLBRACE_ARG); - TOKEN2ID(tSTAR); - TOKEN2ID(tDSTAR); - TOKEN2ID(tAMPER); - TOKEN2ID(tLAMBDA); - TOKEN2ID(tSYMBEG); - TOKEN2ID(tSTRING_BEG); - TOKEN2ID(tXSTRING_BEG); - TOKEN2ID(tREGEXP_BEG); - TOKEN2ID(tWORDS_BEG); - TOKEN2ID(tQWORDS_BEG); - TOKEN2ID(tSYMBOLS_BEG); - TOKEN2ID(tQSYMBOLS_BEG); - TOKEN2ID(tSTRING_END); - TOKEN2ID(tSTRING_DEND); - TOKEN2ID(tSTRING_DBEG); - TOKEN2ID(tSTRING_DVAR); - TOKEN2ID(tLAMBEG); - TOKEN2ID(tLABEL_END); - TOKEN2ID(tIGNORED_NL); - TOKEN2ID(tCOMMENT); - TOKEN2ID(tEMBDOC_BEG); - TOKEN2ID(tEMBDOC); - TOKEN2ID(tEMBDOC_END); - TOKEN2ID(tHEREDOC_BEG); - TOKEN2ID(tHEREDOC_END); - TOKEN2ID(k__END__); - TOKEN2ID(tLOWEST); - TOKEN2ID(tUMINUS_NUM); - TOKEN2ID(tLAST_TOKEN); -#undef TOKEN2ID -#undef TOKEN2ID2 +#define TOKEN2CHAR(tok) case tok: return (#tok); +#define TOKEN2CHAR2(tok, name) case tok: return (name); + TOKEN2CHAR2(' ', "word_sep"); + TOKEN2CHAR2('!', "!") + TOKEN2CHAR2('%', "%"); + TOKEN2CHAR2('&', "&"); + TOKEN2CHAR2('*', "*"); + TOKEN2CHAR2('+', "+"); + TOKEN2CHAR2('-', "-"); + TOKEN2CHAR2('/', "/"); + TOKEN2CHAR2('<', "<"); + TOKEN2CHAR2('=', "="); + TOKEN2CHAR2('>', ">"); + TOKEN2CHAR2('?', "?"); + TOKEN2CHAR2('^', "^"); + TOKEN2CHAR2('|', "|"); + TOKEN2CHAR2('~', "~"); + TOKEN2CHAR2(':', ":"); + TOKEN2CHAR2(',', ","); + TOKEN2CHAR2('.', "."); + TOKEN2CHAR2(';', ";"); + TOKEN2CHAR2('`', "`"); + TOKEN2CHAR2('\n', "nl"); + TOKEN2CHAR2('{', "\"{\""); + TOKEN2CHAR2('}', "\"}\""); + TOKEN2CHAR2('[', "\"[\""); + TOKEN2CHAR2(']', "\"]\""); + TOKEN2CHAR2('(', "\"(\""); + TOKEN2CHAR2(')', "\")\""); + TOKEN2CHAR2('\\', "backslash"); + TOKEN2CHAR(keyword_class); + TOKEN2CHAR(keyword_module); + TOKEN2CHAR(keyword_def); + TOKEN2CHAR(keyword_undef); + TOKEN2CHAR(keyword_begin); + TOKEN2CHAR(keyword_rescue); + TOKEN2CHAR(keyword_ensure); + TOKEN2CHAR(keyword_end); + TOKEN2CHAR(keyword_if); + TOKEN2CHAR(keyword_unless); + TOKEN2CHAR(keyword_then); + TOKEN2CHAR(keyword_elsif); + TOKEN2CHAR(keyword_else); + TOKEN2CHAR(keyword_case); + TOKEN2CHAR(keyword_when); + TOKEN2CHAR(keyword_while); + TOKEN2CHAR(keyword_until); + TOKEN2CHAR(keyword_for); + TOKEN2CHAR(keyword_break); + TOKEN2CHAR(keyword_next); + TOKEN2CHAR(keyword_redo); + TOKEN2CHAR(keyword_retry); + TOKEN2CHAR(keyword_in); + TOKEN2CHAR(keyword_do); + TOKEN2CHAR(keyword_do_cond); + TOKEN2CHAR(keyword_do_block); + TOKEN2CHAR(keyword_do_LAMBDA); + TOKEN2CHAR(keyword_return); + TOKEN2CHAR(keyword_yield); + TOKEN2CHAR(keyword_super); + TOKEN2CHAR(keyword_self); + TOKEN2CHAR(keyword_nil); + TOKEN2CHAR(keyword_true); + TOKEN2CHAR(keyword_false); + TOKEN2CHAR(keyword_and); + TOKEN2CHAR(keyword_or); + TOKEN2CHAR(keyword_not); + TOKEN2CHAR(modifier_if); + TOKEN2CHAR(modifier_unless); + TOKEN2CHAR(modifier_while); + TOKEN2CHAR(modifier_until); + TOKEN2CHAR(modifier_rescue); + TOKEN2CHAR(keyword_alias); + TOKEN2CHAR(keyword_defined); + TOKEN2CHAR(keyword_BEGIN); + TOKEN2CHAR(keyword_END); + TOKEN2CHAR(keyword__LINE__); + TOKEN2CHAR(keyword__FILE__); + TOKEN2CHAR(keyword__ENCODING__); + TOKEN2CHAR(tIDENTIFIER); + TOKEN2CHAR(tFID); + TOKEN2CHAR(tGVAR); + TOKEN2CHAR(tIVAR); + TOKEN2CHAR(tCONSTANT); + TOKEN2CHAR(tCVAR); + TOKEN2CHAR(tLABEL); + TOKEN2CHAR(tINTEGER); + TOKEN2CHAR(tFLOAT); + TOKEN2CHAR(tRATIONAL); + TOKEN2CHAR(tIMAGINARY); + TOKEN2CHAR(tCHAR); + TOKEN2CHAR(tNTH_REF); + TOKEN2CHAR(tBACK_REF); + TOKEN2CHAR(tSTRING_CONTENT); + TOKEN2CHAR(tREGEXP_END); + TOKEN2CHAR(tDUMNY_END); + TOKEN2CHAR(tSP); + TOKEN2CHAR(tUPLUS); + TOKEN2CHAR(tUMINUS); + TOKEN2CHAR(tPOW); + TOKEN2CHAR(tCMP); + TOKEN2CHAR(tEQ); + TOKEN2CHAR(tEQQ); + TOKEN2CHAR(tNEQ); + TOKEN2CHAR(tGEQ); + TOKEN2CHAR(tLEQ); + TOKEN2CHAR(tANDOP); + TOKEN2CHAR(tOROP); + TOKEN2CHAR(tMATCH); + TOKEN2CHAR(tNMATCH); + TOKEN2CHAR(tDOT2); + TOKEN2CHAR(tDOT3); + TOKEN2CHAR(tBDOT2); + TOKEN2CHAR(tBDOT3); + TOKEN2CHAR(tAREF); + TOKEN2CHAR(tASET); + TOKEN2CHAR(tLSHFT); + TOKEN2CHAR(tRSHFT); + TOKEN2CHAR(tANDDOT); + TOKEN2CHAR(tCOLON2); + TOKEN2CHAR(tCOLON3); + TOKEN2CHAR(tOP_ASGN); + TOKEN2CHAR(tASSOC); + TOKEN2CHAR(tLPAREN); + TOKEN2CHAR(tLPAREN_ARG); + TOKEN2CHAR(tLBRACK); + TOKEN2CHAR(tLBRACE); + TOKEN2CHAR(tLBRACE_ARG); + TOKEN2CHAR(tSTAR); + TOKEN2CHAR(tDSTAR); + TOKEN2CHAR(tAMPER); + TOKEN2CHAR(tLAMBDA); + TOKEN2CHAR(tSYMBEG); + TOKEN2CHAR(tSTRING_BEG); + TOKEN2CHAR(tXSTRING_BEG); + TOKEN2CHAR(tREGEXP_BEG); + TOKEN2CHAR(tWORDS_BEG); + TOKEN2CHAR(tQWORDS_BEG); + TOKEN2CHAR(tSYMBOLS_BEG); + TOKEN2CHAR(tQSYMBOLS_BEG); + TOKEN2CHAR(tSTRING_END); + TOKEN2CHAR(tSTRING_DEND); + TOKEN2CHAR(tSTRING_DBEG); + TOKEN2CHAR(tSTRING_DVAR); + TOKEN2CHAR(tLAMBEG); + TOKEN2CHAR(tLABEL_END); + TOKEN2CHAR(tIGNORED_NL); + TOKEN2CHAR(tCOMMENT); + TOKEN2CHAR(tEMBDOC_BEG); + TOKEN2CHAR(tEMBDOC); + TOKEN2CHAR(tEMBDOC_END); + TOKEN2CHAR(tHEREDOC_BEG); + TOKEN2CHAR(tHEREDOC_END); + TOKEN2CHAR(k__END__); + TOKEN2CHAR(tLOWEST); + TOKEN2CHAR(tUMINUS_NUM); + TOKEN2CHAR(tLAST_TOKEN); +#undef TOKEN2CHAR +#undef TOKEN2CHAR2 } rb_bug("parser_token2id: unknown token %d", tok); UNREACHABLE_RETURN(0); } +#else +static void +push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos) +{ +} +static void +pop_end_expect_token_locations(struct parser_params *p) +{ +} #endif RBIMPL_ATTR_NONNULL((1, 2, 3)) @@ -813,86 +1050,66 @@ static void token_info_drop(struct parser_params *p, const char *token, rb_code_ #define token_column ((int)(p->lex.ptok - p->lex.pbeg)) -#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT)) +#define CALL_Q_P(q) ((q) == tANDDOT) #define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc)) #define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest) static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*); -#ifndef RIPPER static inline void rb_discard_node(struct parser_params *p, NODE *n) { rb_ast_delete_node(p->ast, n); } -#endif - -#ifdef RIPPER -static inline VALUE -add_mark_object(struct parser_params *p, VALUE obj) -{ - if (!SPECIAL_CONST_P(obj) - && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */ - ) { - rb_ast_add_mark_object(p->ast, obj); - } - return obj; -} - -static rb_node_ripper_t *rb_node_ripper_new(struct parser_params *p, ID a, VALUE b, VALUE c, const YYLTYPE *loc); -static rb_node_ripper_values_t *rb_node_ripper_values_new(struct parser_params *p, VALUE a, VALUE b, VALUE c, const YYLTYPE *loc); -#define NEW_RIPPER(a,b,c,loc) (VALUE)rb_node_ripper_new(p,a,b,c,loc) -#define NEW_RIPPER_VALUES(a,b,c,loc) (VALUE)rb_node_ripper_values_new(p,a,b,c,loc) -#else -static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc); -static 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); +static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc); +static rb_node_scope_t *rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc); static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc); -static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc); -static rb_node_unless_t *rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc); -static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc); -static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); -static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc); -static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc); -static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc); -static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc); -static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc); +static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc); +static rb_node_unless_t *rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc); +static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc); +static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc); +static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc); +static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc); +static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc); +static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc); +static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc); static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc); -static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc); +static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *for_keyword_loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *do_keyword_loc, const YYLTYPE *end_keyword_loc); static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc); static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc); static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc); -static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc); +static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc); static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc); -static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc); -static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc); +static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc); +static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc); static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc); static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc); static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc); static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc); static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc); -static rb_node_cdecl_t *rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc); +static rb_node_cdecl_t *rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc); static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc); -static 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); -static 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); +static rb_node_op_asgn1_t *rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc); +static rb_node_op_asgn2_t *rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc); static 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); static 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); -static 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); +static rb_node_op_cdecl_t *rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc); static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc); static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc); static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc); static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc); static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc); -static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc); +static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc); static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc); static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc); static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc); static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc); static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc); -static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc); -static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc); +static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc); +static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc); static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc); static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc); static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc); @@ -903,97 +1120,104 @@ static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_n static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc); static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc); static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc); -static rb_node_lit_t *rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc); -static rb_node_str_t *rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc); -static rb_node_dstr_t *rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc); -static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc); -static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc); -static rb_node_dxstr_t *rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc); -static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); +static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc); +static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc); +static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc); +static rb_node_imaginary_t * rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type, const YYLTYPE *loc); +static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc); +static rb_node_dstr_t *rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc); +static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc); +static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc); +static rb_node_dxstr_t *rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc); +static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc); +static rb_node_regx_t *rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc); static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc); -static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc); +static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc); static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc); static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc); static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc); -static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc); -static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); +static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc); +static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc); static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc); static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc); -static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc); -static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc); +static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc); +static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc); static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc); -static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc); -static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc); -static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc); -static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc); -static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc); -static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc); -static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc); +static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc); +static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc); +static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *operator_loc, const YYLTYPE *end_keyword_loc); +static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc); +static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc); +static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc); +static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc); static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc); static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc); static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc); static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc); static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc); -static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc); -static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); -static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc); +static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc); +static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc); +static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc); +static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc); static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc); -static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc); +static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc); static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc); static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc); static 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); +static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc); +static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc); static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc); -#define NEW_SCOPE(a,b,loc) (NODE *)rb_node_scope_new(p,a,b,loc) -#define NEW_SCOPE2(t,a,b,loc) (NODE *)rb_node_scope_new2(p,t,a,b,loc) +#define NEW_SCOPE(a,b,c,loc) (NODE *)rb_node_scope_new(p,a,b,c,loc) +#define NEW_SCOPE2(t,a,b,c,loc) (NODE *)rb_node_scope_new2(p,t,a,b,c,loc) #define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc) -#define NEW_IF(c,t,e,loc) (NODE *)rb_node_if_new(p,c,t,e,loc) -#define NEW_UNLESS(c,t,e,loc) (NODE *)rb_node_unless_new(p,c,t,e,loc) -#define NEW_CASE(h,b,loc) (NODE *)rb_node_case_new(p,h,b,loc) -#define NEW_CASE2(b,loc) (NODE *)rb_node_case2_new(p,b,loc) -#define NEW_CASE3(h,b,loc) (NODE *)rb_node_case3_new(p,h,b,loc) -#define NEW_WHEN(c,t,e,loc) (NODE *)rb_node_when_new(p,c,t,e,loc) -#define NEW_IN(c,t,e,loc) (NODE *)rb_node_in_new(p,c,t,e,loc) -#define NEW_WHILE(c,b,n,loc) (NODE *)rb_node_while_new(p,c,b,n,loc) -#define NEW_UNTIL(c,b,n,loc) (NODE *)rb_node_until_new(p,c,b,n,loc) +#define NEW_IF(c,t,e,loc,ik_loc,tk_loc,ek_loc) (NODE *)rb_node_if_new(p,c,t,e,loc,ik_loc,tk_loc,ek_loc) +#define NEW_UNLESS(c,t,e,loc,k_loc,t_loc,e_loc) (NODE *)rb_node_unless_new(p,c,t,e,loc,k_loc,t_loc,e_loc) +#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc) +#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc) +#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc) +#define NEW_WHEN(c,t,e,loc,k_loc,t_loc) (NODE *)rb_node_when_new(p,c,t,e,loc,k_loc,t_loc) +#define NEW_IN(c,t,e,loc,ik_loc,tk_loc,o_loc) (NODE *)rb_node_in_new(p,c,t,e,loc,ik_loc,tk_loc,o_loc) +#define NEW_WHILE(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_while_new(p,c,b,n,loc,k_loc,c_loc) +#define NEW_UNTIL(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_until_new(p,c,b,n,loc,k_loc,c_loc) #define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc) -#define NEW_FOR(i,b,loc) (NODE *)rb_node_for_new(p,i,b,loc) +#define NEW_FOR(i,b,loc,f_loc,i_loc,d_loc,e_loc) (NODE *)rb_node_for_new(p,i,b,loc,f_loc,i_loc,d_loc,e_loc) #define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc) #define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc) #define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc) #define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc) -#define NEW_RESBODY(a,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,ex,n,loc) +#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc) #define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc) -#define NEW_AND(f,s,loc) (NODE *)rb_node_and_new(p,f,s,loc) -#define NEW_OR(f,s,loc) (NODE *)rb_node_or_new(p,f,s,loc) +#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc) +#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc) #define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc) #define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc) #define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc) #define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc) #define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc) -#define NEW_CDECL(v,val,path,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,loc) +#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc) #define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc) -#define NEW_OP_ASGN1(r,id,idx,rval,loc) (NODE *)rb_node_op_asgn1_new(p,r,id,idx,rval,loc) -#define NEW_OP_ASGN2(r,t,i,o,val,loc) (NODE *)rb_node_op_asgn2_new(p,r,val,i,o,t,loc) +#define NEW_OP_ASGN1(r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc) (NODE *)rb_node_op_asgn1_new(p,r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc) +#define NEW_OP_ASGN2(r,t,i,o,val,loc,c_op_loc,m_loc,b_op_loc) (NODE *)rb_node_op_asgn2_new(p,r,val,i,o,t,loc,c_op_loc,m_loc,b_op_loc) #define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc) #define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc) -#define NEW_OP_CDECL(v,op,val,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,loc) +#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc) #define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc) #define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc) #define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc) #define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc) #define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc) -#define NEW_SUPER(a,loc) (NODE *)rb_node_super_new(p,a,loc) +#define NEW_SUPER(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_super_new(p,a,loc,k_loc,l_loc,r_loc) #define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc) #define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc) #define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc) #define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc) #define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc) -#define NEW_RETURN(s,loc) (NODE *)rb_node_return_new(p,s,loc) -#define NEW_YIELD(a,loc) (NODE *)rb_node_yield_new(p,a,loc) +#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc) +#define NEW_YIELD(a,loc,k_loc,l_loc,r_loc) (NODE *)rb_node_yield_new(p,a,loc,k_loc,l_loc,r_loc) #define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc) #define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc) #define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc) @@ -1004,13 +1228,17 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE #define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc) #define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc) #define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc) -#define NEW_LIT(l,loc) (NODE *)rb_node_lit_new(p,l,loc) +#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc) +#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc) +#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc) +#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc) #define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc) #define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc) #define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc) #define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc) #define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc) -#define NEW_EVSTR(n,loc) (NODE *)rb_node_evstr_new(p,n,loc) +#define NEW_EVSTR(n,loc,o_loc,c_loc) (NODE *)rb_node_evstr_new(p,n,loc,o_loc,c_loc) +#define NEW_REGX(str,opts,loc,o_loc,ct_loc,c_loc) (NODE *)rb_node_regx_new(p,str,opts,loc,o_loc,ct_loc,c_loc) #define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc) #define NEW_ARGS(loc) rb_node_args_new(p,loc) #define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc) @@ -1019,37 +1247,39 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE #define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc) #define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc) #define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc) -#define NEW_SPLAT(a,loc) (NODE *)rb_node_splat_new(p,a,loc) -#define NEW_BLOCK_PASS(b,loc) rb_node_block_pass_new(p,b,loc) +#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc) +#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc) #define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc) #define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc) -#define NEW_ALIAS(n,o,loc) (NODE *)rb_node_alias_new(p,n,o,loc) -#define NEW_VALIAS(n,o,loc) (NODE *)rb_node_valias_new(p,n,o,loc) +#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc) +#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc) #define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc) -#define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc) -#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc) -#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc) -#define NEW_COLON2(c,i,loc) (NODE *)rb_node_colon2_new(p,c,i,loc) -#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc) -#define NEW_DOT2(b,e,loc) (NODE *)rb_node_dot2_new(p,b,e,loc) -#define NEW_DOT3(b,e,loc) (NODE *)rb_node_dot3_new(p,b,e,loc) +#define NEW_CLASS(n,b,s,loc,ck_loc,io_loc,ek_loc) (NODE *)rb_node_class_new(p,n,b,s,loc,ck_loc,io_loc,ek_loc) +#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc) +#define NEW_SCLASS(r,b,loc,ck_loc,op_loc,ek_loc) (NODE *)rb_node_sclass_new(p,r,b,loc,ck_loc,op_loc,ek_loc) +#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc) +#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc) +#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc) +#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc) #define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc) #define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc) #define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc) #define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc) #define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc) -#define NEW_DEFINED(e,loc) (NODE *)rb_node_defined_new(p,e,loc) -#define NEW_POSTEXE(b,loc) (NODE *)rb_node_postexe_new(p,b,loc) +#define NEW_DEFINED(e,loc,k_loc) (NODE *)rb_node_defined_new(p,e,loc, k_loc) +#define NEW_POSTEXE(b,loc,k_loc,o_loc,c_loc) (NODE *)rb_node_postexe_new(p,b,loc,k_loc,o_loc,c_loc) +#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc) #define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc) #define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc) -#define NEW_LAMBDA(a,b,loc) (NODE *)rb_node_lambda_new(p,a,b,loc) +#define NEW_LAMBDA(a,b,loc,op_loc,o_loc,c_loc) (NODE *)rb_node_lambda_new(p,a,b,loc,op_loc,o_loc,c_loc) #define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc) #define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc) #define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc) +#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc) +#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc) +#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc) #define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc) -#endif - enum internal_node_type { NODE_INTERNAL_ONLY = NODE_LAST, NODE_DEF_TEMP, @@ -1075,17 +1305,11 @@ struct RNode_DEF_TEMP { NODE node; /* for NODE_DEFN/NODE_DEFS */ -#ifndef RIPPER + struct RNode *nd_def; ID nd_mid; -#else - VALUE nd_recv; - VALUE nd_mid; - VALUE dot_or_colon; -#endif struct { - ID cur_arg; int max_numparam; NODE *numparam_save; struct lex_context ctxt; @@ -1094,14 +1318,15 @@ struct RNode_DEF_TEMP { #define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node)) -static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc); -static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc); -static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc); +static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc); +static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc); +static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc); static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc); +static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n); -#define NEW_BREAK(s,loc) (NODE *)rb_node_break_new(p,s,loc) -#define NEW_NEXT(s,loc) (NODE *)rb_node_next_new(p,s,loc) -#define NEW_REDO(loc) (NODE *)rb_node_redo_new(p,loc) +#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc) +#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc) +#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc) #define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc) /* Make a new internal node, which should not be appeared in the @@ -1123,11 +1348,10 @@ static void anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id) { if (id == tANDDOT) { - yyerror1(loc, "&. inside multiple assignment destination"); + yyerror1(loc, "&. inside multiple assignment destination"); } } -#ifndef RIPPER static inline void set_line_body(NODE *body, int line) { @@ -1149,30 +1373,37 @@ set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_l static NODE * last_expr_node(NODE *expr) { - if (nd_type_p(expr, NODE_BLOCK)) { - expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head; + while (expr) { + if (nd_type_p(expr, NODE_BLOCK)) { + expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head; + } + else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) { + expr = RNODE_BEGIN(expr)->nd_body; + } + else { + break; + } } return expr; } +#ifndef RIPPER #define yyparse ruby_yyparse +#endif static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc); static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc); -#define new_nil(loc) NEW_NIL(loc) static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos); -static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*); -static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*); +static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*); +static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*); static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*); static NODE *newline_node(NODE*); static void fixpos(NODE*,NODE*); -static int value_expr_gen(struct parser_params*,NODE*); +static int value_expr(struct parser_params*,NODE*); static void void_expr(struct parser_params*,NODE*); static NODE *remove_begin(NODE*); -static NODE *remove_begin_all(NODE*); -#define value_expr(node) value_expr_gen(p, (node)) static NODE *void_stmts(struct parser_params*,NODE*); static void reduce_nodes(struct parser_params*,NODE**); static void block_dup_check(struct parser_params*,NODE*,NODE*); @@ -1184,7 +1415,7 @@ static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*); static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc); static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc); static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*); -static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*); +static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*); static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*); static NODE *str2dstr(struct parser_params*,NODE*); static NODE *evstr2dstr(struct parser_params*,NODE*); @@ -1200,6 +1431,7 @@ static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YY static bool args_info_empty_p(struct rb_args_info *args); static 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*); static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*); +#define new_empty_args_tail(p, loc) new_args_tail(p, 0, 0, 0, loc) static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc); static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc); static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc); @@ -1208,12 +1440,12 @@ static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hsh static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc); static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc); -static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int); +static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID); -static VALUE negate_lit(struct parser_params*, VALUE); +static NODE* negate_lit(struct parser_params*, NODE*,const YYLTYPE*); +static void no_blockarg(struct parser_params*,NODE*); static NODE *ret_args(struct parser_params*,NODE*); static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*); -static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*); static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*); static NODE *gettable(struct parser_params*,ID,const YYLTYPE*); @@ -1222,12 +1454,12 @@ static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*); static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*); static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*); -static void rb_backref_error(struct parser_params*,NODE*); +static VALUE rb_backref_error(struct parser_params*,NODE*); static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*); static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc); -static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc); -static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc); +static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc); +static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc); static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc); static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc); @@ -1239,9 +1471,9 @@ static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*); static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc); static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc); -static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc); +static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc); -static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *); +static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *, const YYLTYPE *); #define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc)) @@ -1253,82 +1485,26 @@ static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYL static rb_ast_id_table_t *local_tbl(struct parser_params*); -static VALUE reg_compile(struct parser_params*, VALUE, int); -static void reg_fragment_setenc(struct parser_params*, VALUE, int); -static int reg_fragment_check(struct parser_params*, VALUE, int); +static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int); +static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int); -static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail); +static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail); static NODE *heredoc_dedent(struct parser_params*,NODE*); static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc); -#define get_id(id) (id) -#define get_value(val) (val) -#define get_num(num) (num) -#else /* RIPPER */ - -static inline int ripper_is_node_yylval(struct parser_params *p, VALUE n); - -static inline VALUE -ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c) -{ - if (ripper_is_node_yylval(p, c)) c = RNODE_RIPPER(c)->nd_cval; - add_mark_object(p, b); - add_mark_object(p, c); - return NEW_RIPPER(a, b, c, &NULL_LOC); -} +static rb_locations_lambda_body_t* new_locations_lambda_body(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc); -static inline VALUE -ripper_new_yylval2(struct parser_params *p, VALUE a, VALUE b, VALUE c) -{ - add_mark_object(p, a); - add_mark_object(p, b); - add_mark_object(p, c); - return NEW_RIPPER_VALUES(a, b, c, &NULL_LOC); -} - -static inline int -ripper_is_node_yylval(struct parser_params *p, VALUE n) -{ - return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER); -} - -#define value_expr(node) ((void)(node)) -#define remove_begin(node) (node) -#define void_stmts(p,x) (x) -#undef rb_dvar_defined -#define rb_dvar_defined(id, base) 0 -#undef rb_local_defined -#define rb_local_defined(id, base) 0 -#define get_id(id) ripper_get_id(id) -#define get_value(val) ripper_get_value(val) -#define get_num(num) (int)get_id(num) -static VALUE assignable(struct parser_params*,VALUE); -static int id_is_var(struct parser_params *p, ID id); - -#define method_cond(p,node,loc) (node) -#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1)) -#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc) -#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv)) -#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc) - -#define new_nil(loc) Qnil - -static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *); - -static VALUE const_decl(struct parser_params *p, VALUE path); - -static VALUE var_field(struct parser_params *p, VALUE a); +#ifdef RIPPER +#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx)) +#define set_value(val) (p->s_lvalue = val) static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a); - -static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *); - -static VALUE backref_error(struct parser_params*, NODE *, VALUE); -#endif /* !RIPPER */ +static int id_is_var(struct parser_params *p, ID id); +#endif RUBY_SYMBOL_EXPORT_BEGIN VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options); -int rb_reg_fragment_setenc(struct parser_params*, VALUE, int); +int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int); enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int); VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state); void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int); @@ -1339,15 +1515,13 @@ YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc); YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc); YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc); +void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str); RUBY_SYMBOL_EXPORT_END +static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back); static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc); static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc); -#ifndef RIPPER -static ID formal_argument(struct parser_params*, ID); -#else -static ID formal_argument(struct parser_params*, VALUE); -#endif +static VALUE formal_argument_error(struct parser_params*, ID); static ID shadowing_lvar(struct parser_params*,ID); static void new_bv(struct parser_params*,ID); @@ -1357,19 +1531,20 @@ static void local_var(struct parser_params*, ID); static void arg_var(struct parser_params*, ID); static int local_id(struct parser_params *p, ID id); static int local_id_ref(struct parser_params*, ID, ID **); -#ifndef RIPPER -static ID internal_id(struct parser_params*); +#define internal_id rb_parser_internal_id +ID internal_id(struct parser_params*); static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*); -#endif static int check_forwarding_args(struct parser_params*); static void add_forwarding_args(struct parser_params *p); +static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var); static const struct vtable *dyna_push(struct parser_params *); static void dyna_pop(struct parser_params*, const struct vtable *); static int dyna_in_block(struct parser_params*); #define dyna_var(p, id) local_var(p, id) static int dvar_defined(struct parser_params*, ID); -static int dvar_defined_ref(struct parser_params*, ID, ID**); +#define dvar_defined_ref rb_parser_dvar_defined_ref +int dvar_defined_ref(struct parser_params*, ID, ID**); static int dvar_curr(struct parser_params*,ID); static int lvar_defined(struct parser_params*, ID); @@ -1377,23 +1552,17 @@ static int lvar_defined(struct parser_params*, ID); static NODE *numparam_push(struct parser_params *p); static void numparam_pop(struct parser_params *p, NODE *prev_inner); -#ifdef RIPPER -# define METHOD_NOT idNOT -#else -# define METHOD_NOT '!' -#endif +#define METHOD_NOT '!' #define idFWD_REST '*' #define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */ #define idFWD_BLOCK '&' #define idFWD_ALL idDot3 -#ifdef RIPPER -#define arg_FWD_BLOCK Qnone -#else #define arg_FWD_BLOCK idFWD_BLOCK -#endif -#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS +#define RE_ONIG_OPTION_IGNORECASE 1 +#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1) +#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1) #define RE_OPTION_ONCE (1<<16) #define RE_OPTION_ENCODING_SHIFT 8 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT) @@ -1402,8 +1571,11 @@ static void numparam_pop(struct parser_params *p, NODE *prev_inner); #define RE_OPTION_MASK 0xff #define RE_OPTION_ARG_ENCODING_NONE 32 +#define CHECK_LITERAL_WHEN (st_table *)1 +#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN) + #define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr) -size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr); +RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr); #define TOKEN2ID(tok) ( \ tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \ @@ -1432,49 +1604,19 @@ static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,V static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE); void ripper_error(struct parser_params *p); -#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n)) -#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a)) -#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b)) -#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c)) -#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d)) -#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e)) -#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)) +#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n)) +#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a)) +#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b)) +#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c)) +#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d)) +#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e)) +#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g)) #define yyparse ripper_yyparse -#define ID2VAL(id) STATIC_ID2SYM(id) -#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t)) -#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0) - -#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \ - dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk)) - -static inline VALUE -new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc) -{ - struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(tail); - VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2, block = t->nd_val3; - return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, block); -} - -static inline VALUE -new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc) -{ - return ripper_new_yylval2(p, kw_args, kw_rest_arg, block); -} - -static inline VALUE -args_with_numbered(struct parser_params *p, VALUE args, int max_numparam) -{ - return args; -} - static VALUE -new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc) +aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args) { - struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(aryptn); - VALUE pre_args = t->nd_val1, rest_arg = t->nd_val2, post_args = t->nd_val3; - if (!NIL_P(pre_arg)) { if (!NIL_P(pre_args)) { rb_ary_unshift(pre_args, pre_arg); @@ -1483,73 +1625,21 @@ new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE pre_args = rb_ary_new_from_args(1, pre_arg); } } - return dispatch4(aryptn, constant, pre_args, rest_arg, post_args); -} - -static VALUE -new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc) -{ - return ripper_new_yylval2(p, pre_args, rest_arg, post_args); -} - -static VALUE -new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc) -{ - struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(fndptn); - VALUE pre_rest_arg = t->nd_val1, args = t->nd_val2, post_rest_arg = t->nd_val3; - - return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg); -} - -static VALUE -new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc) -{ - return ripper_new_yylval2(p, pre_rest_arg, args, post_rest_arg); -} - -#define new_hash(p,h,l) rb_ary_new_from_args(0) - -static VALUE -new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc) -{ - return ary; + return pre_args; } -static VALUE -new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc) -{ - struct RNode_RIPPER_VALUES *t = RNODE_RIPPER_VALUES(hshptn); - VALUE kw_args = t->nd_val1, kw_rest_arg = t->nd_val2; - return dispatch3(hshptn, constant, kw_args, kw_rest_arg); -} - -static VALUE -new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc) -{ - if (kw_rest_arg) { - kw_rest_arg = dispatch1(var_field, kw_rest_arg); - } - else { - kw_rest_arg = Qnil; - } - return ripper_new_yylval2(p, kw_args, kw_rest_arg, Qnil); -} - -#define new_defined(p,expr,loc) dispatch1(defined, (expr)) - -static VALUE heredoc_dedent(struct parser_params*,VALUE); +#define ID2VAL(id) STATIC_ID2SYM(id) +#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t)) +#endif /* RIPPER */ -#else -#define ID2VAL(id) (id) -#define TOKEN2VAL(t) ID2VAL(t) #define KWD2EID(t, v) keyword_##t static NODE * -new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc) +new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, NODE *parent, const YYLTYPE *loc) { body = remove_begin(body); reduce_nodes(p, &body); - NODE *n = NEW_SCOPE(args, body, loc); + NODE *n = NEW_SCOPE(args, body, parent, loc); nd_set_line(n, loc->end_pos.lineno); set_line_body(body, loc->beg_pos.lineno); return n; @@ -1560,13 +1650,11 @@ rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue, const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc) { YYLTYPE loc = code_loc_gen(mod_loc, res_loc); - rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc); + rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc); loc.beg_pos = arg_loc->beg_pos; return NEW_RESCUE(arg, rescue, 0, &loc); } -#endif /* RIPPER */ - static NODE *add_block_exit(struct parser_params *p, NODE *node); static rb_node_exits_t *init_block_exit(struct parser_params *p); static rb_node_exits_t *allow_block_exit(struct parser_params *p); @@ -1584,7 +1672,6 @@ restore_defun(struct parser_params *p, rb_node_def_temp_t *temp) { /* See: def_name action */ struct lex_context ctxt = temp->save.ctxt; - p->cur_arg = temp->save.cur_arg; p->ctxt.in_def = ctxt.in_def; p->ctxt.shareable_constant_value = ctxt.shareable_constant_value; p->ctxt.in_rescue = ctxt.in_rescue; @@ -1615,23 +1702,26 @@ endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc) #define begin_definition(k, loc_beg, loc_end) \ do { \ if (!(p->ctxt.in_class = (k)[0] != 0)) { \ + /* singleton class */ \ + p->ctxt.cant_return = !p->ctxt.in_def; \ p->ctxt.in_def = 0; \ } \ else if (p->ctxt.in_def) { \ YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \ yyerror1(&loc, k " definition in method body"); \ } \ + else { \ + p->ctxt.cant_return = 1; \ + } \ local_push(p, 0); \ } while (0) #ifndef RIPPER -# define Qnone 0 -# define Qnull 0 # define ifndef_ripper(x) (x) +# define ifdef_ripper(r,x) (x) #else -# define Qnone Qnil -# define Qnull Qundef # define ifndef_ripper(x) +# define ifdef_ripper(r,x) (r) #endif # define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1)) @@ -1661,9 +1751,7 @@ extern const ID id_warn, id_warning, id_gets, id_assoc; # define WARN_S(s) STR_NEW2(s) # define WARN_I(i) INT2NUM(i) # define WARN_ID(i) rb_id2str(i) -# define WARN_IVAL(i) i -# define PRIsWARN "s" -# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1)) +# define PRIsWARN PRIsVALUE # define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt) # define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n) # ifdef HAVE_VA_ARGS_MACRO @@ -1684,12 +1772,10 @@ extern const ID id_warn, id_warning, id_gets, id_assoc; # define WARN_S(s) s # define WARN_I(i) i # define WARN_ID(i) rb_id2name(i) -# define WARN_IVAL(i) NUM2INT(i) # define PRIsWARN PRIsVALUE # define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n) # define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt) # define WARN_CALL rb_compile_warn -# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1)) # define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n) # define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n) # define WARNING_CALL rb_compile_warning @@ -1697,13 +1783,6 @@ PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_cod # define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__) #endif -struct RNode_EXITS { - NODE node; - - NODE *nd_chain; /* Assume NODE_BREAK, NODE_NEXT, NODE_REDO have nd_chain here */ - NODE *nd_end; -}; - #define RNODE_EXITS(node) ((rb_node_exits_t*)(node)) static NODE * @@ -1716,14 +1795,14 @@ add_block_exit(struct parser_params *p, NODE *node) switch (nd_type(node)) { case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break; default: - compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node))); + compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node))); return node; } if (!p->ctxt.in_defined) { rb_node_exits_t *exits = p->exits; if (exits) { - RNODE_EXITS(exits->nd_end)->nd_chain = node; - exits->nd_end = node; + RNODE_EXITS(exits->nd_stts)->nd_chain = node; + exits->nd_stts = node; } } return node; @@ -1735,7 +1814,7 @@ init_block_exit(struct parser_params *p) rb_node_exits_t *old = p->exits; rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t); exits->nd_chain = 0; - exits->nd_end = RNODE(exits); + exits->nd_stts = RNODE(exits); p->exits = exits; return old; } @@ -1759,7 +1838,7 @@ clear_block_exit(struct parser_params *p, bool error) { rb_node_exits_t *exits = p->exits; if (!exits) return; - if (error && !compile_for_eval) { + if (error) { for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) { switch (nd_type(e)) { case NODE_BREAK: @@ -1778,17 +1857,16 @@ clear_block_exit(struct parser_params *p, bool error) } end_checks:; } - exits->nd_end = RNODE(exits); + exits->nd_stts = RNODE(exits); exits->nd_chain = 0; } #define WARN_EOL(tok) \ (looking_at_eol_p(p) ? \ - (void)rb_warning0("`" tok "' at the end of line without an expression") : \ + (void)rb_warning0("'" tok "' at the end of line without an expression") : \ (void)0) static int looking_at_eol_p(struct parser_params *p); -#ifndef RIPPER static NODE * get_nd_value(struct parser_params *p, NODE *node) { @@ -1808,7 +1886,7 @@ get_nd_value(struct parser_params *p, NODE *node) case NODE_CDECL: return RNODE_CDECL(node)->nd_value; default: - compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node))); + compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node))); return 0; } } @@ -1839,7 +1917,7 @@ set_nd_value(struct parser_params *p, NODE *node, NODE *rhs) RNODE_CVASGN(node)->nd_value = rhs; break; default: - compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node))); + compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node))); break; } } @@ -1861,7 +1939,7 @@ get_nd_vid(struct parser_params *p, NODE *node) case NODE_CVASGN: return RNODE_CVASGN(node)->nd_vid; default: - compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node))); + compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node))); return 0; } } @@ -1878,8 +1956,9 @@ get_nd_args(struct parser_params *p, NODE *node) return RNODE_FCALL(node)->nd_args; case NODE_QCALL: return RNODE_QCALL(node)->nd_args; - case NODE_VCALL: case NODE_SUPER: + return RNODE_SUPER(node)->nd_args; + case NODE_VCALL: case NODE_ZSUPER: case NODE_YIELD: case NODE_RETURN: @@ -1887,65 +1966,709 @@ get_nd_args(struct parser_params *p, NODE *node) case NODE_NEXT: return 0; default: - compile_error(p, "unexpected node: %s", parser_node_name(nd_type(node))); + compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node))); return 0; } } + +static st_index_t +djb2(const uint8_t *str, size_t len) +{ + st_index_t hash = 5381; + + for (size_t i = 0; i < len; i++) { + hash = ((hash << 5) + hash) + str[i]; + } + + return hash; +} + +static st_index_t +parser_memhash(const void *ptr, long len) +{ + return djb2(ptr, len); +} + +#define PARSER_STRING_PTR(str) (str->ptr) +#define PARSER_STRING_LEN(str) (str->len) +#define PARSER_STRING_END(str) (&str->ptr[str->len]) +#define STRING_SIZE(str) ((size_t)str->len + 1) +#define STRING_TERM_LEN(str) (1) +#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0') +#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\ + REALLOC_N(str->ptr, char, (size_t)total + termlen); \ + str->len = total; \ +} while (0) +#define STRING_SET_LEN(str, n) do { \ + (str)->len = (n); \ +} while (0) +#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \ + ((ptrvar) = str->ptr, \ + (lenvar) = str->len) + +static inline int +parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty) +{ + return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty; +} + +static rb_parser_string_t * +rb_parser_string_new(rb_parser_t *p, const char *ptr, long len) +{ + rb_parser_string_t *str; + + if (len < 0) { + rb_bug("negative string size (or size too big): %ld", len); + } + + str = xcalloc(1, sizeof(rb_parser_string_t)); + str->ptr = xcalloc(len + 1, sizeof(char)); + + if (ptr) { + memcpy(PARSER_STRING_PTR(str), ptr, len); + } + STRING_SET_LEN(str, len); + STRING_TERM_FILL(str); + return str; +} + +static rb_parser_string_t * +rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc) +{ + rb_parser_string_t *str = rb_parser_string_new(p, ptr, len); + str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN; + str->enc = enc; + return str; +} + +#ifndef RIPPER +rb_parser_string_t * +rb_str_to_parser_string(rb_parser_t *p, VALUE str) +{ + /* Type check */ + rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str)); + RB_GC_GUARD(str); + return ret; +} + +void +rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str) +{ + if (!str) return; + xfree(PARSER_STRING_PTR(str)); + xfree(str); +} #endif + +static st_index_t +rb_parser_str_hash(rb_parser_string_t *str) +{ + return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str)); +} + +static st_index_t +rb_char_p_hash(const char *c) +{ + return parser_memhash((const void *)c, strlen(c)); +} + +static size_t +rb_parser_str_capacity(rb_parser_string_t *str, const int termlen) +{ + return PARSER_STRING_LEN(str); +} + +#ifndef RIPPER +static char * +rb_parser_string_end(rb_parser_string_t *str) +{ + return &str->ptr[str->len]; +} +#endif + +static void +rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc) +{ + str->enc = enc; +} + +static rb_encoding * +rb_parser_str_get_encoding(rb_parser_string_t *str) +{ + return str->enc; +} + +#ifndef RIPPER +static bool +PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str) +{ + return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding(); +} +#endif + +static int +PARSER_ENC_CODERANGE(rb_parser_string_t *str) +{ + return str->coderange; +} + +static void +PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange) +{ + str->coderange = coderange; +} + +static void +PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr) +{ + rb_parser_string_set_encoding(str, enc); + PARSER_ENC_CODERANGE_SET(str, cr); +} + +static void +PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str) +{ + str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN; +} + +static bool +PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str) +{ + return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT; +} + +static bool +PARSER_ENC_CODERANGE_CLEAN_P(int cr) +{ + return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID; +} + +static const char * +rb_parser_search_nonascii(const char *p, const char *e) +{ + const char *s = p; + + for (; s < e; s++) { + if (*s & 0x80) return s; + } + + return NULL; +} + +static int +rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc) +{ + const char *e = ptr + len; + + if (enc == rb_ascii8bit_encoding()) { + /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */ + ptr = rb_parser_search_nonascii(ptr, e); + return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT; + } + + /* parser string encoding is always asciicompat */ + ptr = rb_parser_search_nonascii(ptr, e); + if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT; + for (;;) { + int ret = rb_enc_precise_mbclen(ptr, e, enc); + if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN; + ptr += MBCLEN_CHARFOUND_LEN(ret); + if (ptr == e) break; + ptr = rb_parser_search_nonascii(ptr, e); + if (!ptr) break; + } + + return RB_PARSER_ENC_CODERANGE_VALID; +} + +static int +rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc) +{ + return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc); +} + +static int +rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str) +{ + int cr = PARSER_ENC_CODERANGE(str); + + if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) { + cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str)); + PARSER_ENC_CODERANGE_SET(str, cr); + } + + return cr; +} + +static rb_parser_string_t * +rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc) +{ + if (rb_parser_str_get_encoding(str) == enc) + return str; + if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) { + PARSER_ENC_CODERANGE_CLEAR(str); + } + rb_parser_string_set_encoding(str, enc); + return str; +} + +static bool +rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str) +{ + return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT; +} + +static rb_encoding * +rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2) +{ + rb_encoding *enc1 = rb_parser_str_get_encoding(str1); + rb_encoding *enc2 = rb_parser_str_get_encoding(str2); + + if (enc1 == NULL || enc2 == NULL) + return 0; + + if (enc1 == enc2) { + return enc1; + } + + if (PARSER_STRING_LEN(str2) == 0) + return enc1; + if (PARSER_STRING_LEN(str1) == 0) + return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2; + + int cr1, cr2; + + cr1 = rb_parser_enc_str_coderange(p, str1); + cr2 = rb_parser_enc_str_coderange(p, str2); + + if (cr1 != cr2) { + if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2; + if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1; + } + + if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) { + return enc1; + } + + if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) { + return enc2; + } + + return 0; +} + +static void +rb_parser_str_modify(rb_parser_string_t *str) +{ + PARSER_ENC_CODERANGE_CLEAR(str); +} + +static void +rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len) +{ + long capa; + const int termlen = STRING_TERM_LEN(str); + + if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) { + rb_bug("probable buffer overflow: %ld for %ld", len, capa); + } + + int cr = PARSER_ENC_CODERANGE(str); + if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) { + /* Leave unknown. */ + } + else if (len > PARSER_STRING_LEN(str)) { + PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN); + } + else if (len < PARSER_STRING_LEN(str)) { + if (cr != RB_PARSER_ENC_CODERANGE_7BIT) { + /* ASCII-only string is keeping after truncated. Valid + * and broken may be invalid or valid, leave unknown. */ + PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN); + } + } + + STRING_SET_LEN(str, len); + STRING_TERM_FILL(str); +} + +static rb_parser_string_t * +rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len) +{ + rb_parser_str_modify(str); + if (len == 0) return 0; + + long total, olen, off = -1; + char *sptr; + const int termlen = STRING_TERM_LEN(str); + + PARSER_STRING_GETMEM(str, sptr, olen); + if (ptr >= sptr && ptr <= sptr + olen) { + off = ptr - sptr; + } + + if (olen > LONG_MAX - len) { + compile_error(p, "string sizes too big"); + return 0; + } + total = olen + len; + PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen); + sptr = PARSER_STRING_PTR(str); + if (off != -1) { + ptr = sptr + off; + } + memcpy(sptr + olen, ptr, len); + STRING_SET_LEN(str, total); + STRING_TERM_FILL(str); + + return str; +} + +#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len) +#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit)) + +static rb_parser_string_t * +rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len, + rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret) +{ + int str_cr, res_cr; + rb_encoding *str_enc, *res_enc; + + str_enc = rb_parser_str_get_encoding(str); + str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT; + + if (str_enc == ptr_enc) { + if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) { + ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc); + } + } + else { + /* parser string encoding is always asciicompat */ + if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) { + ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc); + } + if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) { + if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) { + str_cr = rb_parser_enc_str_coderange(p, str); + } + } + } + if (ptr_cr_ret) + *ptr_cr_ret = ptr_cr; + + if (str_enc != ptr_enc && + str_cr != RB_PARSER_ENC_CODERANGE_7BIT && + ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) { + goto incompatible; + } + + if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) { + res_enc = str_enc; + res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN; + } + else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) { + if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) { + res_enc = str_enc; + res_cr = RB_PARSER_ENC_CODERANGE_7BIT; + } + else { + res_enc = ptr_enc; + res_cr = ptr_cr; + } + } + else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) { + res_enc = str_enc; + if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr)) + res_cr = str_cr; + else + res_cr = ptr_cr; + } + else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */ + res_enc = str_enc; + res_cr = str_cr; + if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN; + } + + if (len < 0) { + compile_error(p, "negative string size (or size too big)"); + } + parser_str_cat(str, ptr, len); + PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr); + return str; + + incompatible: + compile_error(p, "incompatible character encodings: %s and %s", + rb_enc_name(str_enc), rb_enc_name(ptr_enc)); + UNREACHABLE_RETURN(0); + +} + +static rb_parser_string_t * +rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len, + rb_encoding *ptr_enc) +{ + return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL); +} + +static rb_parser_string_t * +rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2) +{ + int str2_cr = rb_parser_enc_str_coderange(p, str2); + + rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2), + rb_parser_str_get_encoding(str2), str2_cr, &str2_cr); + + PARSER_ENC_CODERANGE_SET(str2, str2_cr); + + return str; +} + +static rb_parser_string_t * +rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len) +{ + if (len < 0) { + rb_bug("negative string size (or size too big)"); + } + + long slen = PARSER_STRING_LEN(str); + + if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) { + PARSER_ENC_CODERANGE_CLEAR(str); + } + + { + long capa; + const int termlen = STRING_TERM_LEN(str); + + if ((capa = slen) < len) { + SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str)); + } + else if (len == slen) return str; + STRING_SET_LEN(str, len); + STRING_TERM_FILL(str); + } + return str; +} + +# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \ + ((ptrvar) = str->ptr, \ + (lenvar) = str->len, \ + (encvar) = str->enc) + +static int +rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2) +{ + long len1, len2; + const char *ptr1, *ptr2; + rb_encoding *enc1, *enc2; + + PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1); + PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2); + + return (len1 != len2 || + enc1 != enc2 || + memcmp(ptr1, ptr2, len1) != 0); +} + +static void +rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len) +{ + long i; + if (ary->capa < len) { + ary->capa = len; + ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len); + for (i = ary->len; i < len; i++) { + ary->data[i] = 0; + } + } +} + +/* + * Do not call this directly. + * Use rb_parser_ary_new_capa_for_XXX() instead. + */ +static rb_parser_ary_t * +parser_ary_new_capa(rb_parser_t *p, long len) +{ + if (len < 0) { + rb_bug("negative array size (or size too big): %ld", len); + } + rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t)); + ary->data_type = 0; + ary->len = 0; + ary->capa = len; + if (0 < len) { + ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data)); + } + else { + ary->data = NULL; + } + return ary; +} + +#ifndef RIPPER +static rb_parser_ary_t * +rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len) +{ + rb_parser_ary_t *ary = parser_ary_new_capa(p, len); + ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE; + return ary; +} + +static rb_parser_ary_t * +rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len) +{ + rb_parser_ary_t *ary = parser_ary_new_capa(p, len); + ary->data_type = PARSER_ARY_DATA_AST_TOKEN; + return ary; +} +#endif + +static rb_parser_ary_t * +rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len) +{ + rb_parser_ary_t *ary = parser_ary_new_capa(p, len); + ary->data_type = PARSER_ARY_DATA_NODE; + return ary; +} + +/* + * Do not call this directly. + * Use rb_parser_ary_push_XXX() instead. + */ +static rb_parser_ary_t * +parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val) +{ + if (ary->len == ary->capa) { + rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2); + } + ary->data[ary->len++] = val; + return ary; +} + +#ifndef RIPPER +static rb_parser_ary_t * +rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val) +{ + if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) { + rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type); + } + return parser_ary_push(p, ary, val); +} + +static rb_parser_ary_t * +rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val) +{ + if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) { + rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type); + } + return parser_ary_push(p, ary, val); +} +#endif + +static rb_parser_ary_t * +rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val) +{ + if (ary->data_type != PARSER_ARY_DATA_NODE) { + rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type); + } + return parser_ary_push(p, ary, val); +} + +#ifndef RIPPER +static void +rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token) +{ + if (!token) return; + rb_parser_string_free(p, token->str); + xfree(token); +} + +static void +rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary) +{ +# define foreach_ary(ptr) \ + for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \ + ptr < end_ary_data; ptr++) + switch (ary->data_type) { + case PARSER_ARY_DATA_AST_TOKEN: + foreach_ary(data) {rb_parser_ast_token_free(p, *data);} + break; + case PARSER_ARY_DATA_SCRIPT_LINE: + foreach_ary(data) {rb_parser_string_free(p, *data);} + break; + case PARSER_ARY_DATA_NODE: + /* Do nothing because nodes are freed when rb_ast_t is freed */ + break; + default: + rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type); + break; + } +# undef foreach_ary + xfree(ary->data); + xfree(ary); +} + +#endif /* !RIPPER */ %} %expect 0 %define api.pure %define parse.error verbose %printer { -#ifndef RIPPER if ((NODE *)$$ == (NODE *)-1) { rb_parser_printf(p, "NODE_SPECIAL"); } else if ($$) { rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$)))); } -#else -#endif -} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg> <node_kw_arg> <node_block_pass> +} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg> + <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits> %printer { -#ifndef RIPPER rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$)); -#else - rb_parser_printf(p, "%"PRIsVALUE, RNODE_RIPPER($$)->nd_rval); -#endif -} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN +} <id> %printer { -#ifndef RIPPER - rb_parser_printf(p, "%+"PRIsVALUE, RNODE_LIT($$)->nd_lit); -#else - rb_parser_printf(p, "%+"PRIsVALUE, get_value($$)); -#endif + switch (nd_type(RNODE($$))) { + case NODE_INTEGER: + rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$)); + break; + case NODE_FLOAT: + rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$)); + break; + case NODE_RATIONAL: + rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$)); + break; + case NODE_IMAGINARY: + rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$)); + break; + default: + break; + } } tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR %printer { -#ifndef RIPPER rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth); -#else - rb_parser_printf(p, "%"PRIsVALUE, $$); -#endif } tNTH_REF %printer { -#ifndef RIPPER rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth); -#else - rb_parser_printf(p, "%"PRIsVALUE, $$); -#endif } tBACK_REF +%destructor { + if (CASE_LABELS_ENABLED_P($$)) st_free_table($$); +} <labels> + %lex-param {struct parser_params *p} %parse-param {struct parser_params *p} %initial-action { RUBY_SET_YYLLOC_OF_NONE(@$); }; +%after-shift after_shift +%before-reduce before_reduce +%after-reduce after_reduce +%after-shift-error-token after_shift_error_token +%after-pop-stack after_pop_stack %union { - VALUE val; NODE *node; rb_node_fcall_t *node_fcall; rb_node_args_t *node_args; @@ -1956,64 +2679,67 @@ get_nd_args(struct parser_params *p, NODE *node) rb_node_masgn_t *node_masgn; rb_node_def_temp_t *node_def_temp; rb_node_exits_t *node_exits; + struct rb_locations_lambda_body_t *locations_lambda_body; ID id; int num; st_table *tbl; + st_table *labels; const struct vtable *vars; struct rb_strterm_struct *strterm; struct lex_context ctxt; + enum lex_state_e state; } %token <id> - keyword_class "`class'" - keyword_module "`module'" - keyword_def "`def'" - keyword_undef "`undef'" - keyword_begin "`begin'" - keyword_rescue "`rescue'" - keyword_ensure "`ensure'" - keyword_end "`end'" - keyword_if "`if'" - keyword_unless "`unless'" - keyword_then "`then'" - keyword_elsif "`elsif'" - keyword_else "`else'" - keyword_case "`case'" - keyword_when "`when'" - keyword_while "`while'" - keyword_until "`until'" - keyword_for "`for'" - keyword_break "`break'" - keyword_next "`next'" - keyword_redo "`redo'" - keyword_retry "`retry'" - keyword_in "`in'" - keyword_do "`do'" - keyword_do_cond "`do' for condition" - keyword_do_block "`do' for block" - keyword_do_LAMBDA "`do' for lambda" - keyword_return "`return'" - keyword_yield "`yield'" - keyword_super "`super'" - keyword_self "`self'" - keyword_nil "`nil'" - keyword_true "`true'" - keyword_false "`false'" - keyword_and "`and'" - keyword_or "`or'" - keyword_not "`not'" - modifier_if "`if' modifier" - modifier_unless "`unless' modifier" - modifier_while "`while' modifier" - modifier_until "`until' modifier" - modifier_rescue "`rescue' modifier" - keyword_alias "`alias'" - keyword_defined "`defined?'" - keyword_BEGIN "`BEGIN'" - keyword_END "`END'" - keyword__LINE__ "`__LINE__'" - keyword__FILE__ "`__FILE__'" - keyword__ENCODING__ "`__ENCODING__'" + keyword_class "'class'" + keyword_module "'module'" + keyword_def "'def'" + keyword_undef "'undef'" + keyword_begin "'begin'" + keyword_rescue "'rescue'" + keyword_ensure "'ensure'" + keyword_end "'end'" + keyword_if "'if'" + keyword_unless "'unless'" + keyword_then "'then'" + keyword_elsif "'elsif'" + keyword_else "'else'" + keyword_case "'case'" + keyword_when "'when'" + keyword_while "'while'" + keyword_until "'until'" + keyword_for "'for'" + keyword_break "'break'" + keyword_next "'next'" + keyword_redo "'redo'" + keyword_retry "'retry'" + keyword_in "'in'" + keyword_do "'do'" + keyword_do_cond "'do' for condition" + keyword_do_block "'do' for block" + keyword_do_LAMBDA "'do' for lambda" + keyword_return "'return'" + keyword_yield "'yield'" + keyword_super "'super'" + keyword_self "'self'" + keyword_nil "'nil'" + keyword_true "'true'" + keyword_false "'false'" + keyword_and "'and'" + keyword_or "'or'" + keyword_not "'not'" + modifier_if "'if' modifier" + modifier_unless "'unless' modifier" + modifier_while "'while' modifier" + modifier_until "'until' modifier" + modifier_rescue "'rescue' modifier" + keyword_alias "'alias'" + keyword_defined "'defined?'" + keyword_BEGIN "'BEGIN'" + keyword_END "'END'" + keyword__LINE__ "'__LINE__'" + keyword__FILE__ "'__FILE__'" + keyword__ENCODING__ "'__ENCODING__'" %token <id> tIDENTIFIER "local variable or method" %token <id> tFID "method" @@ -2033,56 +2759,55 @@ get_nd_args(struct parser_params *p, NODE *node) %token <num> tREGEXP_END %token <num> tDUMNY_END "dummy end" -%type <node> singleton strings string string1 xstring regexp +%type <node> singleton singleton_expr strings string string1 xstring regexp %type <node> string_contents xstring_contents regexp_contents string_content %type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word %type <node> literal numeric simple_numeric ssym dsym symbol cpath -/*ripper*/ %type <node_def_temp> defn_head defs_head k_def -/*ripper*/ %type <node_exits> block_open k_while k_until k_for allow_exits -%type <node> top_compstmt top_stmts top_stmt begin_block endless_arg endless_command -%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call +%type <node_def_temp> defn_head defs_head k_def +%type <node_exits> block_open k_while k_until k_for allow_exits +%type <node> top_stmts top_stmt begin_block endless_arg endless_command +%type <node> bodystmt stmts stmt_or_begin stmt expr arg ternary primary +%type <node> command command_call command_call_value method_call %type <node> expr_value expr_value_do arg_value primary_value rel_expr %type <node_fcall> fcall %type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure -%type <node> args call_args opt_call_args +%type <node> args arg_splat call_args opt_call_args %type <node> paren_args opt_paren_args -%type <node_args> args_tail opt_args_tail block_args_tail opt_block_args_tail +%type <node_args> args_tail block_args_tail %type <node> command_args aref_args %type <node_block_pass> opt_block_arg block_arg %type <node> var_ref var_lhs %type <node> command_rhs arg_rhs %type <node> command_asgn mrhs mrhs_arg superclass block_call block_command -%type <node_opt_arg> f_block_optarg f_block_opt -%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args +%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args f_empty_arg %type <node_args_aux> f_arg f_arg_item -%type <node_opt_arg> f_optarg -%type <node> f_marg f_marg_list f_rest_marg +%type <node> f_marg f_rest_marg %type <node_masgn> f_margs %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var -%type <node_args> block_param opt_block_param block_param_def -%type <node_opt_arg> f_opt -%type <node_kw_arg> f_kwarg f_kw f_block_kwarg f_block_kw -%type <node> bv_decls opt_bv_decl bvar -%type <node> lambda lambda_body brace_body do_body -%type <node_args> f_larglist +%type <node_args> block_param opt_block_param_def block_param_def opt_block_param +%type <id> do bv_decls opt_bv_decl bvar +%type <node> lambda brace_body do_body +%type <locations_lambda_body> lambda_body +%type <node_args> f_larglist f_largs largs_tail %type <node> brace_block cmd_brace_block do_block lhs none fitem -%type <node> mlhs_head mlhs_item mlhs_node mlhs_post +%type <node> mlhs_head mlhs_item mlhs_node %type <node_masgn> mlhs mlhs_basic mlhs_inner %type <node> p_case_body p_cases p_top_expr p_top_expr_body %type <node> p_expr p_as p_alt p_expr_basic p_find %type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest %type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const %type <node> p_kwargs p_kwarg p_kw -%type <id> keyword_variable user_variable sym operation operation2 operation3 -%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg +%type <id> keyword_variable user_variable sym operation2 operation3 +%type <id> cname fname op f_rest_arg f_block_arg opt_comma f_norm_arg f_bad_arg %type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon %type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label %type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name %type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue %type <ctxt> p_in_kwarg %type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl -/* ripper */ %type <num> max_numparam -/* ripper */ %type <node> numparam +%type <num> max_numparam +%type <node> numparam +%type <id> it_id %token END_OF_INPUT 0 "end-of-input" %token <id> '.' @@ -2121,14 +2846,13 @@ get_nd_args(struct parser_params *p, NODE *node) %token tASSOC "=>" %token tLPAREN "(" %token tLPAREN_ARG "( arg" -%token tRPAREN ")" %token tLBRACK "[" %token tLBRACE "{" %token tLBRACE_ARG "{ arg" %token tSTAR "*" %token tDSTAR "**arg" %token tAMPER "&" -%token tLAMBDA "->" +%token <num> tLAMBDA "->" %token tSYMBEG "symbol literal" %token tSTRING_BEG "string literal" %token tXSTRING_BEG "backtick literal" @@ -2139,7 +2863,8 @@ get_nd_args(struct parser_params *p, NODE *node) %token tQSYMBOLS_BEG "verbatim symbol list" %token tSTRING_END "terminator" %token tSTRING_DEND "'}'" -%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END +%token <state> tSTRING_DBEG "'#{'" +%token tSTRING_DVAR tLAMBEG tLABEL_END %token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END %token tHEREDOC_BEG tHEREDOC_END k__END__ @@ -2174,6 +2899,273 @@ get_nd_args(struct parser_params *p, NODE *node) %token tLAST_TOKEN +/* + * inlining rules + */ +%rule %inline ident_or_const + : tIDENTIFIER + | tCONSTANT + ; + +%rule %inline user_or_keyword_variable + : user_variable + | keyword_variable + ; + +/* + * parameterizing rules + */ +%rule asgn(rhs) <node> + : lhs '=' lex_ctxt rhs + { + $$ = node_assign(p, (NODE *)$lhs, $rhs, $lex_ctxt, &@$); + /*% ripper: assign!($:1, $:4) %*/ + } + ; + +%rule args_tail_basic(value, trailing) <node_args> + : f_kwarg(value) ',' f_kwrest opt_f_block_arg(trailing) + { + $$ = new_args_tail(p, $1, $3, $4, &@3); + /*% ripper: [$:1, $:3, $:4] %*/ + } + | f_kwarg(value) opt_f_block_arg(trailing) + { + $$ = new_args_tail(p, $1, 0, $2, &@1); + /*% ripper: [$:1, Qnil, $:2] %*/ + } + | f_any_kwrest opt_f_block_arg(trailing) + { + $$ = new_args_tail(p, 0, $1, $2, &@1); + /*% ripper: [Qnil, $:1, $:2] %*/ + } + | f_block_arg + { + $$ = new_args_tail(p, 0, 0, $1, &@1); + /*% ripper: [Qnil, Qnil, $:1] %*/ + } + ; + +%rule opt_f_block_arg(trailing) <id> + : ',' f_block_arg + { + $$ = $2; + /*% ripper: $:2 %*/ + } + | trailing + ; + +%rule def_endless_method(bodystmt) <node> + : defn_head[head] f_opt_paren_args[args] '=' bodystmt + { + endless_method_name(p, $head->nd_mid, &@head); + restore_defun(p, $head); + ($$ = $head->nd_def)->nd_loc = @$; + $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$); + RNODE_DEFN($$)->nd_defn = $bodystmt; + /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/ + /*% ripper: def!($:head, $:args, $:$) %*/ + local_pop(p); + } + | defs_head[head] f_opt_paren_args[args] '=' bodystmt + { + endless_method_name(p, $head->nd_mid, &@head); + restore_defun(p, $head); + ($$ = $head->nd_def)->nd_loc = @$; + $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$); + RNODE_DEFS($$)->nd_defn = $bodystmt; + /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/ + /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/ + local_pop(p); + } + ; + +%rule compstmt(stmts) <node> + : stmts terms? + { + void_stmts(p, $$ = $stmts); + } + ; + +%rule f_opt(value) <node_opt_arg> + : f_arg_asgn f_eq value + { + p->ctxt.in_argdef = 1; + $$ = NEW_OPT_ARG(assignable(p, $f_arg_asgn, $value, &@$), &@$); + /*% ripper: [$:$, $:3] %*/ + } + ; + +%rule f_opt_arg(value) <node_opt_arg> + : f_opt(value) + { + $$ = $f_opt; + /*% ripper: rb_ary_new3(1, $:1) %*/ + } + | f_opt_arg(value) ',' f_opt(value) + { + $$ = opt_arg_append($f_opt_arg, $f_opt); + /*% ripper: rb_ary_push($:1, $:3) %*/ + } + ; + +%rule f_kw(value) <node_kw_arg> + : f_label value + { + p->ctxt.in_argdef = 1; + $$ = new_kw_arg(p, assignable(p, $f_label, $value, &@$), &@$); + /*% ripper: [$:$, $:value] %*/ + } + | f_label + { + p->ctxt.in_argdef = 1; + $$ = new_kw_arg(p, assignable(p, $f_label, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$); + /*% ripper: [$:$, 0] %*/ + } + ; + +%rule f_kwarg(value) <node_kw_arg> + : f_kw(value) + { + $$ = $f_kw; + /*% ripper: rb_ary_new3(1, $:1) %*/ + } + | f_kwarg(value) ',' f_kw(value) + { + $$ = kwd_append($f_kwarg, $f_kw); + /*% ripper: rb_ary_push($:1, $:3) %*/ + } + ; + +%rule mlhs_items(item) <node> + : item + { + $$ = NEW_LIST($1, &@$); + /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/ + } + | mlhs_items(item) ',' item + { + $$ = list_append(p, $1, $3); + /*% ripper: mlhs_add!($:1, $:3) %*/ + } + ; + +%rule op_asgn(rhs) <node> + : var_lhs tOP_ASGN lex_ctxt rhs + { + $$ = new_op_assign(p, $var_lhs, $tOP_ASGN, $rhs, $lex_ctxt, &@$); + /*% ripper: opassign!($:var_lhs, $:tOP_ASGN, $:rhs) %*/ + } + | primary_value '['[lbracket] opt_call_args rbracket tOP_ASGN lex_ctxt rhs + { + $$ = new_ary_op_assign(p, $primary_value, $opt_call_args, $tOP_ASGN, $rhs, &@opt_call_args, &@$, &NULL_LOC, &@lbracket, &@rbracket, &@tOP_ASGN); + /*% ripper: opassign!(aref_field!($:primary_value, $:opt_call_args), $:tOP_ASGN, $:rhs) %*/ + } + | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt rhs + { + $$ = new_attr_op_assign(p, $primary_value, $call_op, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@call_op, &@tIDENTIFIER, &@tOP_ASGN); + /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/ + } + | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt rhs + { + $$ = new_attr_op_assign(p, $primary_value, $call_op, $tCONSTANT, $tOP_ASGN, $rhs, &@$, &@call_op, &@tCONSTANT, &@tOP_ASGN); + /*% ripper: opassign!(field!($:primary_value, $:call_op, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/ + } + | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt rhs + { + $$ = new_attr_op_assign(p, $primary_value, idCOLON2, $tIDENTIFIER, $tOP_ASGN, $rhs, &@$, &@tCOLON2, &@tIDENTIFIER, &@tOP_ASGN); + /*% ripper: opassign!(field!($:primary_value, $:tCOLON2, $:tIDENTIFIER), $:tOP_ASGN, $:rhs) %*/ + } + | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs + { + YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT); + $$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$); + /*% ripper: opassign!(const_path_field!($:primary_value, $:tCONSTANT), $:tOP_ASGN, $:rhs) %*/ + } + | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs + { + YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT); + $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$); + /*% ripper: opassign!(top_const_field!($:tCONSTANT), $:tOP_ASGN, $:rhs) %*/ + } + | backref tOP_ASGN lex_ctxt rhs + { + VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $backref); + $$ = NEW_ERROR(&@$); + /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:backref), $:tOP_ASGN, $:rhs)) %*/ + } + ; + +%rule opt_args_tail(tail, trailing) <node_args> + : ',' tail + { + $$ = $tail; + /*% ripper: $:tail %*/ + } + | trailing + { + $$ = new_empty_args_tail(p, &@$); + /*% ripper: [Qnil, Qnil, Qnil] %*/ + } + ; + +%rule range_expr(range) <node> + : range tDOT2 range + { + value_expr(p, $1); + value_expr(p, $3); + $$ = NEW_DOT2($1, $3, &@$, &@2); + /*% ripper: dot2!($:1, $:3) %*/ + } + | range tDOT3 range + { + value_expr(p, $1); + value_expr(p, $3); + $$ = NEW_DOT3($1, $3, &@$, &@2); + /*% ripper: dot3!($:1, $:3) %*/ + } + | range tDOT2 + { + value_expr(p, $1); + $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2); + /*% ripper: dot2!($:1, Qnil) %*/ + } + | range tDOT3 + { + value_expr(p, $1); + $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$, &@2); + /*% ripper: dot3!($:1, Qnil) %*/ + } + | tBDOT2 range + { + value_expr(p, $2); + $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1); + /*% ripper: dot2!(Qnil, $:2) %*/ + } + | tBDOT3 range + { + value_expr(p, $2); + $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1); + /*% ripper: dot3!(Qnil, $:2) %*/ + } + ; + +%rule value_expr(value) <node> + : value + { + value_expr(p, $1); + $$ = $1; + } + ; + +%rule words(begin, word_list) <node> + : begin ' '+ word_list tSTRING_END + { + $$ = make_list($word_list, &@$); + /*% ripper: array!($:word_list) %*/ + } + ; + %% program : { SET_LEX_STATE(EXPR_BEG); @@ -2181,9 +3173,8 @@ program : { /* jumps are possible in the top-level loop. */ if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p); } - top_compstmt + compstmt(top_stmts) { - /*%%%*/ if ($2 && !compile_for_eval) { NODE *node = $2; /* last expression should not be void */ @@ -2196,39 +3187,26 @@ program : { node = remove_begin(node); void_expr(p, node); } - p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$); - /*% %*/ - /*% ripper[final]: program!($2) %*/ + p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$); + /*% ripper[final]: program!($:2) %*/ local_pop(p); } ; -top_compstmt : top_stmts opt_terms - { - $$ = void_stmts(p, $1); - } - ; - top_stmts : none { - /*%%%*/ $$ = NEW_BEGIN(0, &@$); - /*% %*/ /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/ } | top_stmt { - /*%%%*/ $$ = newline_node($1); - /*% %*/ - /*% ripper: stmts_add!(stmts_new!, $1) %*/ + /*% ripper: stmts_add!(stmts_new!, $:1) %*/ } | top_stmts terms top_stmt { - /*%%%*/ $$ = block_append(p, $1, newline_node($3)); - /*% %*/ - /*% ripper: stmts_add!($1, $3) %*/ + /*% ripper: stmts_add!($:1, $:3) %*/ } ; @@ -2240,24 +3218,23 @@ top_stmt : stmt | keyword_BEGIN begin_block { $$ = $2; + /*% ripper: $:2 %*/ } ; block_open : '{' {$$ = init_block_exit(p);}; -begin_block : block_open top_compstmt '}' +begin_block : block_open compstmt(top_stmts) '}' { restore_block_exit(p, $block_open); - /*%%%*/ p->eval_tree_begin = block_append(p, p->eval_tree_begin, - NEW_BEGIN($2, &@$)); + NEW_BEGIN($compstmt, &@$)); $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper: BEGIN!($2) %*/ + /*% ripper: BEGIN!($:compstmt) %*/ } ; -bodystmt : compstmt[body] +bodystmt : compstmt(stmts)[body] lex_ctxt[ctxt] opt_rescue k_else @@ -2265,18 +3242,16 @@ bodystmt : compstmt[body] if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless"); next_rescue_context(&p->ctxt, &$ctxt, after_else); } - compstmt[elsebody] + compstmt(stmts)[elsebody] { next_rescue_context(&p->ctxt, &$ctxt, after_ensure); } opt_ensure { - /*%%%*/ $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$); - /*% %*/ - /*% ripper: bodystmt!($body, $opt_rescue, $elsebody, $opt_ensure) %*/ + /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/ } - | compstmt[body] + | compstmt(stmts)[body] lex_ctxt[ctxt] opt_rescue { @@ -2284,46 +3259,29 @@ bodystmt : compstmt[body] } opt_ensure { - /*%%%*/ $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$); - /*% %*/ - /*% ripper: bodystmt!($body, $opt_rescue, Qnil, $opt_ensure) %*/ - } - ; - -compstmt : stmts opt_terms - { - $$ = void_stmts(p, $1); + /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/ } ; stmts : none { - /*%%%*/ $$ = NEW_BEGIN(0, &@$); - /*% %*/ /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/ } | stmt_or_begin { - /*%%%*/ $$ = newline_node($1); - /*% %*/ - /*% ripper: stmts_add!(stmts_new!, $1) %*/ + /*% ripper: stmts_add!(stmts_new!, $:1) %*/ } | stmts terms stmt_or_begin { - /*%%%*/ $$ = block_append(p, $1, newline_node($3)); - /*% %*/ - /*% ripper: stmts_add!($1, $3) %*/ + /*% ripper: stmts_add!($:1, $:3) %*/ } ; stmt_or_begin : stmt - { - $$ = $1; - } | keyword_BEGIN { yyerror1(&@1, "BEGIN is permitted only at toplevel"); @@ -2338,304 +3296,188 @@ allow_exits : {$$ = allow_block_exit(p);}; k_END : keyword_END lex_ctxt { + if (p->ctxt.in_def) { + rb_warn0("END in method; use at_exit"); + } $$ = $2; p->ctxt.in_rescue = before_rescue; + /*% ripper: $:2 %*/ }; -stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem +stmt : keyword_alias[kw] fitem[new] {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem[old] { - /*%%%*/ - $$ = NEW_ALIAS($2, $4, &@$); - /*% %*/ - /*% ripper: alias!($2, $4) %*/ + $$ = NEW_ALIAS($new, $old, &@$, &@kw); + /*% ripper: alias!($:new, $:old) %*/ } - | keyword_alias tGVAR tGVAR + | keyword_alias[kw] tGVAR[new] tGVAR[old] { - /*%%%*/ - $$ = NEW_VALIAS($2, $3, &@$); - /*% %*/ - /*% ripper: var_alias!($2, $3) %*/ + $$ = NEW_VALIAS($new, $old, &@$, &@kw); + /*% ripper: var_alias!($:new, $:old) %*/ } - | keyword_alias tGVAR tBACK_REF + | keyword_alias[kw] tGVAR[new] tBACK_REF[old] { - /*%%%*/ char buf[2]; buf[0] = '$'; - buf[1] = (char)RNODE_BACK_REF($3)->nd_nth; - $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$); - /*% %*/ - /*% ripper: var_alias!($2, $3) %*/ + buf[1] = (char)RNODE_BACK_REF($old)->nd_nth; + $$ = NEW_VALIAS($new, rb_intern2(buf, 2), &@$, &@kw); + /*% ripper: var_alias!($:new, $:old) %*/ } - | keyword_alias tGVAR tNTH_REF + | keyword_alias tGVAR tNTH_REF[nth] { static const char mesg[] = "can't make alias for the number variables"; /*%%%*/ - yyerror1(&@3, mesg); - $$ = NEW_BEGIN(0, &@$); + yyerror1(&@nth, mesg); /*% %*/ - /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/ + $$ = NEW_ERROR(&@$); + /*% ripper[error]: alias_error!(ERR_MESG(), $:nth) %*/ } - | keyword_undef undef_list + | keyword_undef[kw] undef_list[list] { - /*%%%*/ - $$ = $2; - /*% %*/ - /*% ripper: undef!($2) %*/ + nd_set_first_loc($list, @kw.beg_pos); + RNODE_UNDEF($list)->keyword_loc = @kw; + $$ = $list; + /*% ripper: undef!($:list) %*/ } - | stmt modifier_if expr_value + | stmt[body] modifier_if[mod] expr_value[cond] { - /*%%%*/ - $$ = new_if(p, $3, remove_begin($1), 0, &@$); - fixpos($$, $3); - /*% %*/ - /*% ripper: if_mod!($3, $1) %*/ + $$ = new_if(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC); + fixpos($$, $cond); + /*% ripper: if_mod!($:cond, $:body) %*/ } - | stmt modifier_unless expr_value + | stmt[body] modifier_unless[mod] expr_value[cond] { - /*%%%*/ - $$ = new_unless(p, $3, remove_begin($1), 0, &@$); - fixpos($$, $3); - /*% %*/ - /*% ripper: unless_mod!($3, $1) %*/ + $$ = new_unless(p, $cond, remove_begin($body), 0, &@$, &@mod, &NULL_LOC, &NULL_LOC); + fixpos($$, $cond); + /*% ripper: unless_mod!($:cond, $:body) %*/ } - | stmt modifier_while expr_value + | stmt[body] modifier_while[mod] expr_value[cond_expr] { clear_block_exit(p, false); - /*%%%*/ - if ($1 && nd_type_p($1, NODE_BEGIN)) { - $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$); + if ($body && nd_type_p($body, NODE_BEGIN)) { + $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC); } else { - $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$); + $$ = NEW_WHILE(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC); } - /*% %*/ - /*% ripper: while_mod!($3, $1) %*/ + /*% ripper: while_mod!($:cond_expr, $:body) %*/ } - | stmt modifier_until expr_value + | stmt[body] modifier_until[mod] expr_value[cond_expr] { - clear_block_exit(p, false); - /*%%%*/ - if ($1 && nd_type_p($1, NODE_BEGIN)) { - $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$); + clear_block_exit(p, 0); + if ($body && nd_type_p($body, NODE_BEGIN)) { + $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), RNODE_BEGIN($body)->nd_body, 0, &@$, &@mod, &NULL_LOC); } else { - $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$); + $$ = NEW_UNTIL(cond(p, $cond_expr, &@cond_expr), $body, 1, &@$, &@mod, &NULL_LOC); } - /*% %*/ - /*% ripper: until_mod!($3, $1) %*/ + /*% ripper: until_mod!($:cond_expr, $:body) %*/ } - | stmt modifier_rescue after_rescue stmt + | stmt[body] modifier_rescue[mod] after_rescue[ctxt] stmt[resbody] { - p->ctxt.in_rescue = $3.in_rescue; - /*%%%*/ + p->ctxt.in_rescue = $ctxt.in_rescue; NODE *resq; - YYLTYPE loc = code_loc_gen(&@2, &@4); - resq = NEW_RESBODY(0, remove_begin($4), 0, &loc); - $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$); - /*% %*/ - /*% ripper: rescue_mod!($1, $4) %*/ + YYLTYPE loc = code_loc_gen(&@mod, &@resbody); + resq = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc); + $$ = NEW_RESCUE(remove_begin($body), resq, 0, &@$); + /*% ripper: rescue_mod!($:body, $:resbody) %*/ } - | k_END allow_exits '{' compstmt '}' + | k_END[k_end] block_open[lbrace] compstmt(stmts)[body] '}'[rbrace] { - if (p->ctxt.in_def) { - rb_warn0("END in method; use at_exit"); - } - restore_block_exit(p, $allow_exits); - p->ctxt = $k_END; - /*%%%*/ + clear_block_exit(p, true); + restore_block_exit(p, $block_open); + p->ctxt = $k_end; { - NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$); - $$ = NEW_POSTEXE(scope, &@$); + NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $body /* body */, NULL /* parent */, &@$); + $$ = NEW_POSTEXE(scope, &@$, &@k_end, &@lbrace, &@rbrace); + RNODE_SCOPE(scope)->nd_parent = $$; } - /*% %*/ - /*% ripper: END!($compstmt) %*/ + /*% ripper: END!($:body) %*/ } | command_asgn - | mlhs '=' lex_ctxt command_call + | mlhs[lhs] '=' lex_ctxt[ctxt] command_call_value[rhs] { - /*%%%*/ - value_expr($4); - $$ = node_assign(p, (NODE *)$1, $4, $3, &@$); - /*% %*/ - /*% ripper: massign!($1, $4) %*/ - } - | lhs '=' lex_ctxt mrhs - { - /*%%%*/ - $$ = node_assign(p, $1, $4, $3, &@$); - /*% %*/ - /*% ripper: assign!($1, $4) %*/ + $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$); + /*% ripper: massign!($:lhs, $:rhs) %*/ } - | mlhs '=' lex_ctxt mrhs_arg modifier_rescue - after_rescue stmt[resbody] + | asgn(mrhs) + | mlhs[lhs] '=' lex_ctxt[lex_ctxt] mrhs_arg[mrhs_arg] modifier_rescue[modifier_rescue] + after_rescue[after_rescue] stmt[resbody] { - p->ctxt.in_rescue = $3.in_rescue; - /*%%%*/ + p->ctxt.in_rescue = $after_rescue.in_rescue; YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody); - $resbody = NEW_RESBODY(0, remove_begin($resbody), 0, &loc); + $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc); loc.beg_pos = @mrhs_arg.beg_pos; $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc); - $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$); - /*% %*/ - /*% ripper: massign!($1, rescue_mod!($4, $7)) %*/ + $$ = node_assign(p, (NODE *)$lhs, $mrhs_arg, $lex_ctxt, &@$); + /*% ripper: massign!($:lhs, rescue_mod!($:mrhs_arg, $:resbody)) %*/ } - | mlhs '=' lex_ctxt mrhs_arg + | mlhs[lhs] '=' lex_ctxt[ctxt] mrhs_arg[rhs] { - /*%%%*/ - $$ = node_assign(p, (NODE *)$1, $4, $3, &@$); - /*% %*/ - /*% ripper: massign!($1, $4) %*/ + $$ = node_assign(p, (NODE *)$lhs, $rhs, $ctxt, &@$); + /*% ripper: massign!($:lhs, $:rhs) %*/ } | expr | error { (void)yynerrs; - /*%%%*/ $$ = NEW_ERROR(&@$); - /*% %*/ } ; -command_asgn : lhs '=' lex_ctxt command_rhs - { - /*%%%*/ - $$ = node_assign(p, $1, $4, $3, &@$); - /*% %*/ - /*% ripper: assign!($1, $4) %*/ - } - | var_lhs tOP_ASGN lex_ctxt command_rhs - { - /*%%%*/ - $$ = new_op_assign(p, $1, $2, $4, $3, &@$); - /*% %*/ - /*% ripper: opassign!($1, $2, $4) %*/ - } - | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs - { - /*%%%*/ - $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$); - /*% %*/ - /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/ - - } - | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs - { - /*%%%*/ - $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$); - /*% %*/ - /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ - } - | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs - { - /*%%%*/ - $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$); - /*% %*/ - /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ - } - | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs - { - /*%%%*/ - YYLTYPE loc = code_loc_gen(&@1, &@3); - $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$); - /*% %*/ - /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/ - } - | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs - { - /*%%%*/ - $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$); - /*% %*/ - /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ - } - | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt] - { - endless_method_name(p, get_id($head->nd_mid), &@head); - restore_defun(p, $head); - /*%%%*/ - $bodystmt = new_scope_body(p, $args, $bodystmt, &@$); - ($$ = $head->nd_def)->nd_loc = @$; - RNODE_DEFN($$)->nd_defn = $bodystmt; - /*% %*/ - /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/ - /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/ - local_pop(p); - } - | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt] - { - endless_method_name(p, get_id($head->nd_mid), &@head); - restore_defun(p, $head); - /*%%%*/ - $bodystmt = new_scope_body(p, $args, $bodystmt, &@$); - ($$ = $head->nd_def)->nd_loc = @$; - RNODE_DEFS($$)->nd_defn = $bodystmt; - /*% %*/ - /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/ - /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/ - local_pop(p); - } - | backref tOP_ASGN lex_ctxt command_rhs - { - /*%%%*/ - rb_backref_error(p, $1); - $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/ - } +command_asgn : asgn(command_rhs) + | op_asgn(command_rhs) + | def_endless_method(endless_command) ; endless_command : command | endless_command modifier_rescue after_rescue arg { p->ctxt.in_rescue = $3.in_rescue; - /*%%%*/ $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4); - /*% %*/ - /*% ripper: rescue_mod!($1, $4) %*/ + /*% ripper: rescue_mod!($:1, $:4) %*/ } - | keyword_not opt_nl endless_command + | keyword_not '\n'? endless_command { $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$); + /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/ } ; -command_rhs : command_call %prec tOP_ASGN - { - value_expr($1); - $$ = $1; - } - | command_call modifier_rescue after_rescue stmt +command_rhs : command_call_value %prec tOP_ASGN + | command_call_value modifier_rescue after_rescue stmt { p->ctxt.in_rescue = $3.in_rescue; - /*%%%*/ YYLTYPE loc = code_loc_gen(&@2, &@4); - value_expr($1); - $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($4), 0, &loc), 0, &@$); - /*% %*/ - /*% ripper: rescue_mod!($1, $4) %*/ + $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$); + /*% ripper: rescue_mod!($:1, $:4) %*/ } | command_asgn ; expr : command_call - | expr keyword_and expr + | expr[left] keyword_and[op] expr[right] { - $$ = logop(p, idAND, $1, $3, &@2, &@$); + $$ = logop(p, idAND, $left, $right, &@op, &@$); + /*% ripper: binary!($:left, ID2VAL(idAND), $:right) %*/ } - | expr keyword_or expr + | expr[left] keyword_or[op] expr[right] { - $$ = logop(p, idOR, $1, $3, &@2, &@$); + $$ = logop(p, idOR, $left, $right, &@op, &@$); + /*% ripper: binary!($:left, ID2VAL(idOR), $:right) %*/ } - | keyword_not opt_nl expr + | keyword_not[not] '\n'? expr[arg] { - $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$); + $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@not, &@$); + /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/ } - | '!' command_call + | '!'[not] command_call[arg] { - $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$); + $$ = call_uni_op(p, method_cond(p, $arg, &@arg), '!', &@not, &@$); + /*% ripper: unary!(ID2VAL('\'!\''), $:arg) %*/ } - | arg tASSOC + | arg tASSOC[assoc] { - value_expr($arg); + value_expr(p, $arg); } p_in_kwarg[ctxt] p_pvtbl p_pktbl p_top_expr_body[body] @@ -2643,14 +3485,14 @@ expr : command_call pop_pktbl(p, $p_pktbl); pop_pvtbl(p, $p_pvtbl); p->ctxt.in_kwarg = $ctxt.in_kwarg; - /*%%%*/ - $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$); - /*% %*/ - /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/ + p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern; + p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern; + $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@assoc), &@$, &NULL_LOC, &NULL_LOC); + /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/ } | arg keyword_in { - value_expr($arg); + value_expr(p, $arg); } p_in_kwarg[ctxt] p_pvtbl p_pktbl p_top_expr_body[body] @@ -2658,74 +3500,59 @@ expr : command_call pop_pktbl(p, $p_pktbl); pop_pvtbl(p, $p_pvtbl); p->ctxt.in_kwarg = $ctxt.in_kwarg; - /*%%%*/ - $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$); - /*% %*/ - /*% ripper: case!($arg, in!($body, Qnil, Qnil)) %*/ + p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern; + p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern; + $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC); + /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/ } | arg %prec tLBRACE_ARG ; def_name : fname { - ID fname = get_id($1); - numparam_name(p, fname); + numparam_name(p, $fname); local_push(p, 0); - p->cur_arg = 0; p->ctxt.in_def = 1; p->ctxt.in_rescue = before_rescue; - $$ = $1; + p->ctxt.cant_return = 0; + $$ = $fname; } ; defn_head : k_def def_name { - $$ = $k_def; + $$ = def_head_save(p, $k_def); $$->nd_mid = $def_name; - /*%%%*/ $$->nd_def = NEW_DEFN($def_name, 0, &@$); - /*% - add_mark_object(p, $def_name); - %*/ + /*% ripper: $:def_name %*/ } ; defs_head : k_def singleton dot_or_colon { SET_LEX_STATE(EXPR_FNAME); - p->ctxt.in_argdef = 1; } def_name { SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */ - $$ = $k_def; + $$ = def_head_save(p, $k_def); $$->nd_mid = $def_name; - /*%%%*/ $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$); - /*% - add_mark_object(p, $def_name); - $$->nd_recv = add_mark_object(p, $singleton); - $$->dot_or_colon = add_mark_object(p, $dot_or_colon); - %*/ + /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/ } ; -expr_value : expr - { - value_expr($1); - $$ = $1; - } +expr_value : value_expr(expr) | error { - /*%%%*/ $$ = NEW_ERROR(&@$); - /*% %*/ } ; expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();} { $$ = $2; + /*% ripper: $:2 %*/ } ; @@ -2733,398 +3560,270 @@ command_call : command | block_command ; +command_call_value : value_expr(command_call) + ; + block_command : block_call | block_call call_op2 operation2 command_args { - /*%%%*/ $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$); - /*% %*/ - /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/ + /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/ } ; cmd_brace_block : tLBRACE_ARG brace_body '}' { $$ = $2; - /*%%%*/ set_embraced_location($$, &@1, &@3); - /*% %*/ + /*% ripper: $:2 %*/ } ; fcall : operation { - /*%%%*/ $$ = NEW_FCALL($1, 0, &@$); - /*% %*/ - /*% ripper: $1 %*/ + /*% ripper: $:1 %*/ } ; command : fcall command_args %prec tLOWEST { - /*%%%*/ $1->nd_args = $2; nd_set_last_loc($1, @2.end_pos); $$ = (NODE *)$1; - /*% %*/ - /*% ripper: command!($1, $2) %*/ + /*% ripper: command!($:1, $:2) %*/ } | fcall command_args cmd_brace_block { - /*%%%*/ block_dup_check(p, $2, $3); $1->nd_args = $2; $$ = method_add_block(p, (NODE *)$1, $3, &@$); fixpos($$, RNODE($1)); nd_set_last_loc($1, @2.end_pos); - /*% %*/ - /*% ripper: method_add_block!(command!($1, $2), $3) %*/ + /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/ } | primary_value call_op operation2 command_args %prec tLOWEST { - /*%%%*/ - $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$); - /*% %*/ - /*% ripper: command_call!($1, $2, $3, $4) %*/ + $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$); + /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/ } | primary_value call_op operation2 command_args cmd_brace_block { - /*%%%*/ $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$); - /*% %*/ - /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/ + /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/ } | primary_value tCOLON2 operation2 command_args %prec tLOWEST { - /*%%%*/ - $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$); - /*% %*/ - /*% ripper: command_call!($1, $2, $3, $4) %*/ + $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$); + /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/ } | primary_value tCOLON2 operation2 command_args cmd_brace_block { - /*%%%*/ - $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$); - /*% %*/ - /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/ + $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$); + /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/ } | primary_value tCOLON2 tCONSTANT '{' brace_body '}' { - /*%%%*/ set_embraced_location($5, &@4, &@6); - $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, $5, &@3, &@$); - /*% %*/ - /*% ripper: method_add_block!(command_call!($1, $2, $3, Qnull), $5) %*/ + $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$); + /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/ } | keyword_super command_args { - /*%%%*/ - $$ = NEW_SUPER($2, &@$); + $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC); fixpos($$, $2); - /*% %*/ - /*% ripper: super!($2) %*/ + /*% ripper: super!($:2) %*/ } | k_yield command_args { - /*%%%*/ - $$ = new_yield(p, $2, &@$); + $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC); fixpos($$, $2); - /*% %*/ - /*% ripper: yield!($2) %*/ + /*% ripper: yield!($:2) %*/ } | k_return call_args { - /*%%%*/ - $$ = NEW_RETURN(ret_args(p, $2), &@$); - /*% %*/ - /*% ripper: return!($2) %*/ + $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1); + /*% ripper: return!($:2) %*/ } | keyword_break call_args { NODE *args = 0; - /*%%%*/ args = ret_args(p, $2); - /*% %*/ - $<node>$ = add_block_exit(p, NEW_BREAK(args, &@$)); - /*% ripper: break!($2) %*/ + $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1)); + /*% ripper: break!($:2) %*/ } | keyword_next call_args { NODE *args = 0; - /*%%%*/ args = ret_args(p, $2); - /*% %*/ - $<node>$ = add_block_exit(p, NEW_NEXT(args, &@$)); - /*% ripper: next!($2) %*/ + $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1)); + /*% ripper: next!($:2) %*/ } ; mlhs : mlhs_basic | tLPAREN mlhs_inner rparen { - /*%%%*/ $$ = $2; - /*% %*/ - /*% ripper: mlhs_paren!($2) %*/ + /*% ripper: mlhs_paren!($:2) %*/ } ; mlhs_inner : mlhs_basic | tLPAREN mlhs_inner rparen { - /*%%%*/ $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$); - /*% %*/ - /*% ripper: mlhs_paren!($2) %*/ + /*% ripper: mlhs_paren!($:2) %*/ } ; mlhs_basic : mlhs_head { - /*%%%*/ $$ = NEW_MASGN($1, 0, &@$); - /*% %*/ - /*% ripper: $1 %*/ + /*% ripper: $:1 %*/ } | mlhs_head mlhs_item { - /*%%%*/ $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$); - /*% %*/ - /*% ripper: mlhs_add!($1, $2) %*/ + /*% ripper: mlhs_add!($:1, $:2) %*/ } | mlhs_head tSTAR mlhs_node { - /*%%%*/ $$ = NEW_MASGN($1, $3, &@$); - /*% %*/ - /*% ripper: mlhs_add_star!($1, $3) %*/ + /*% ripper: mlhs_add_star!($:1, $:3) %*/ } - | mlhs_head tSTAR mlhs_node ',' mlhs_post + | mlhs_head tSTAR mlhs_node ',' mlhs_items(mlhs_item) { - /*%%%*/ $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$); - /*% %*/ - /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/ + /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/ } | mlhs_head tSTAR { - /*%%%*/ $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$); - /*% %*/ - /*% ripper: mlhs_add_star!($1, Qnil) %*/ + /*% ripper: mlhs_add_star!($:1, Qnil) %*/ } - | mlhs_head tSTAR ',' mlhs_post + | mlhs_head tSTAR ',' mlhs_items(mlhs_item) { - /*%%%*/ $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$); - /*% %*/ - /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/ + /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/ } | tSTAR mlhs_node { - /*%%%*/ $$ = NEW_MASGN(0, $2, &@$); - /*% %*/ - /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/ + /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/ } - | tSTAR mlhs_node ',' mlhs_post + | tSTAR mlhs_node ',' mlhs_items(mlhs_item) { - /*%%%*/ $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$); - /*% %*/ - /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/ + /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/ } | tSTAR { - /*%%%*/ $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$); - /*% %*/ /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/ } - | tSTAR ',' mlhs_post + | tSTAR ',' mlhs_items(mlhs_item) { - /*%%%*/ $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$); - /*% %*/ - /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/ + /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/ } ; mlhs_item : mlhs_node | tLPAREN mlhs_inner rparen { - /*%%%*/ $$ = (NODE *)$2; - /*% %*/ - /*% ripper: mlhs_paren!($2) %*/ + /*% ripper: mlhs_paren!($:2) %*/ } ; mlhs_head : mlhs_item ',' { - /*%%%*/ $$ = NEW_LIST($1, &@1); - /*% %*/ - /*% ripper: mlhs_add!(mlhs_new!, $1) %*/ + /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/ } | mlhs_head mlhs_item ',' { - /*%%%*/ $$ = list_append(p, $1, $2); - /*% %*/ - /*% ripper: mlhs_add!($1, $2) %*/ + /*% ripper: mlhs_add!($:1, $:2) %*/ } ; -mlhs_post : mlhs_item - { - /*%%%*/ - $$ = NEW_LIST($1, &@$); - /*% %*/ - /*% ripper: mlhs_add!(mlhs_new!, $1) %*/ - } - | mlhs_post ',' mlhs_item - { - /*%%%*/ - $$ = list_append(p, $1, $3); - /*% %*/ - /*% ripper: mlhs_add!($1, $3) %*/ - } - ; -mlhs_node : user_variable +mlhs_node : user_or_keyword_variable { - /*%%%*/ + /*% ripper: var_field!($:1) %*/ $$ = assignable(p, $1, 0, &@$); - /*% %*/ - /*% ripper: assignable(p, var_field(p, $1)) %*/ - } - | keyword_variable - { - /*%%%*/ - $$ = assignable(p, $1, 0, &@$); - /*% %*/ - /*% ripper: assignable(p, var_field(p, $1)) %*/ } | primary_value '[' opt_call_args rbracket { - /*%%%*/ $$ = aryset(p, $1, $3, &@$); - /*% %*/ - /*% ripper: aref_field!($1, $3) %*/ + /*% ripper: aref_field!($:1, $:3) %*/ } - | primary_value call_op tIDENTIFIER + | primary_value call_op ident_or_const { anddot_multiple_assignment_check(p, &@2, $2); - /*%%%*/ $$ = attrset(p, $1, $2, $3, &@$); - /*% %*/ - /*% ripper: field!($1, $2, $3) %*/ + /*% ripper: field!($:1, $:2, $:3) %*/ } | primary_value tCOLON2 tIDENTIFIER { - /*%%%*/ $$ = attrset(p, $1, idCOLON2, $3, &@$); - /*% %*/ - /*% ripper: const_path_field!($1, $3) %*/ - } - | primary_value call_op tCONSTANT - { - anddot_multiple_assignment_check(p, &@2, $2); - /*%%%*/ - $$ = attrset(p, $1, $2, $3, &@$); - /*% %*/ - /*% ripper: field!($1, $2, $3) %*/ + /*% ripper: const_path_field!($:1, $:3) %*/ } | primary_value tCOLON2 tCONSTANT { - /*%%%*/ - $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$); - /*% %*/ - /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/ + /*% ripper: const_path_field!($:1, $:3) %*/ + $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$); } | tCOLON3 tCONSTANT { - /*%%%*/ - $$ = const_decl(p, NEW_COLON3($2, &@$), &@$); - /*% %*/ - /*% ripper: const_decl(p, top_const_field!($2)) %*/ + /*% ripper: top_const_field!($:2) %*/ + $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$); } | backref { - /*%%%*/ - rb_backref_error(p, $1); - $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/ + VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1); + $$ = NEW_ERROR(&@$); + /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/ } ; -lhs : user_variable +lhs : user_or_keyword_variable { - /*%%%*/ + /*% ripper: var_field!($:1) %*/ $$ = assignable(p, $1, 0, &@$); - /*% %*/ - /*% ripper: assignable(p, var_field(p, $1)) %*/ - } - | keyword_variable - { - /*%%%*/ - $$ = assignable(p, $1, 0, &@$); - /*% %*/ - /*% ripper: assignable(p, var_field(p, $1)) %*/ } | primary_value '[' opt_call_args rbracket { - /*%%%*/ $$ = aryset(p, $1, $3, &@$); - /*% %*/ - /*% ripper: aref_field!($1, $3) %*/ + /*% ripper: aref_field!($:1, $:3) %*/ } - | primary_value call_op tIDENTIFIER + | primary_value call_op ident_or_const { - /*%%%*/ $$ = attrset(p, $1, $2, $3, &@$); - /*% %*/ - /*% ripper: field!($1, $2, $3) %*/ + /*% ripper: field!($:1, $:2, $:3) %*/ } | primary_value tCOLON2 tIDENTIFIER { - /*%%%*/ $$ = attrset(p, $1, idCOLON2, $3, &@$); - /*% %*/ - /*% ripper: field!($1, $2, $3) %*/ - } - | primary_value call_op tCONSTANT - { - /*%%%*/ - $$ = attrset(p, $1, $2, $3, &@$); - /*% %*/ - /*% ripper: field!($1, $2, $3) %*/ + /*% ripper: field!($:1, $:2, $:3) %*/ } | primary_value tCOLON2 tCONSTANT { - /*%%%*/ - $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$); - /*% %*/ - /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/ + /*% ripper: const_path_field!($:1, $:3) %*/ + $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$); } | tCOLON3 tCONSTANT { - /*%%%*/ - $$ = const_decl(p, NEW_COLON3($2, &@$), &@$); - /*% %*/ - /*% ripper: const_decl(p, top_const_field!($2)) %*/ + /*% ripper: top_const_field!($:2) %*/ + $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$); } | backref { - /*%%%*/ - rb_backref_error(p, $1); - $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/ + VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1); + $$ = NEW_ERROR(&@$); + /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/ } ; @@ -3134,37 +3833,29 @@ cname : tIDENTIFIER /*%%%*/ yyerror1(&@1, mesg); /*% %*/ - /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/ + /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/ } | tCONSTANT ; cpath : tCOLON3 cname { - /*%%%*/ - $$ = NEW_COLON3($2, &@$); - /*% %*/ - /*% ripper: top_const_ref!($2) %*/ + $$ = NEW_COLON3($2, &@$, &@1, &@2); + /*% ripper: top_const_ref!($:2) %*/ } | cname { - /*%%%*/ - $$ = NEW_COLON2(0, $1, &@$); - /*% %*/ - /*% ripper: const_ref!($1) %*/ + $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1); + /*% ripper: const_ref!($:1) %*/ } | primary_value tCOLON2 cname { - /*%%%*/ - $$ = NEW_COLON2($1, $3, &@$); - /*% %*/ - /*% ripper: const_path_ref!($1, $3) %*/ + $$ = NEW_COLON2($1, $3, &@$, &@2, &@3); + /*% ripper: const_path_ref!($:1, $:3) %*/ } ; -fname : tIDENTIFIER - | tCONSTANT - | tFID +fname : operation | op { SET_LEX_STATE(EXPR_ENDFN); @@ -3175,61 +3866,55 @@ fname : tIDENTIFIER fitem : fname { - /*%%%*/ - $$ = NEW_LIT(ID2SYM($1), &@$); - /*% %*/ - /*% ripper: symbol_literal!($1) %*/ + $$ = NEW_SYM(rb_id2str($1), &@$); + /*% ripper: symbol_literal!($:1) %*/ } | symbol ; undef_list : fitem { - /*%%%*/ $$ = NEW_UNDEF($1, &@$); - /*% %*/ - /*% ripper: rb_ary_new3(1, get_value($1)) %*/ + /*% ripper: rb_ary_new3(1, $:1) %*/ } | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem { - /*%%%*/ - NODE *undef = NEW_UNDEF($4, &@4); - $$ = block_append(p, $1, undef); - /*% %*/ - /*% ripper: rb_ary_push($1, get_value($4)) %*/ - } - ; - -op : '|' { ifndef_ripper($$ = '|'); } - | '^' { ifndef_ripper($$ = '^'); } - | '&' { ifndef_ripper($$ = '&'); } - | tCMP { ifndef_ripper($$ = tCMP); } - | tEQ { ifndef_ripper($$ = tEQ); } - | tEQQ { ifndef_ripper($$ = tEQQ); } - | tMATCH { ifndef_ripper($$ = tMATCH); } - | tNMATCH { ifndef_ripper($$ = tNMATCH); } - | '>' { ifndef_ripper($$ = '>'); } - | tGEQ { ifndef_ripper($$ = tGEQ); } - | '<' { ifndef_ripper($$ = '<'); } - | tLEQ { ifndef_ripper($$ = tLEQ); } - | tNEQ { ifndef_ripper($$ = tNEQ); } - | tLSHFT { ifndef_ripper($$ = tLSHFT); } - | tRSHFT { ifndef_ripper($$ = tRSHFT); } - | '+' { ifndef_ripper($$ = '+'); } - | '-' { ifndef_ripper($$ = '-'); } - | '*' { ifndef_ripper($$ = '*'); } - | tSTAR { ifndef_ripper($$ = '*'); } - | '/' { ifndef_ripper($$ = '/'); } - | '%' { ifndef_ripper($$ = '%'); } - | tPOW { ifndef_ripper($$ = tPOW); } - | tDSTAR { ifndef_ripper($$ = tDSTAR); } - | '!' { ifndef_ripper($$ = '!'); } - | '~' { ifndef_ripper($$ = '~'); } - | tUPLUS { ifndef_ripper($$ = tUPLUS); } - | tUMINUS { ifndef_ripper($$ = tUMINUS); } - | tAREF { ifndef_ripper($$ = tAREF); } - | tASET { ifndef_ripper($$ = tASET); } - | '`' { ifndef_ripper($$ = '`'); } + nd_set_last_loc($1, @4.end_pos); + rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4); + /*% ripper: rb_ary_push($:1, $:4) %*/ + } + ; + +op : '|' { $$ = '|'; } + | '^' { $$ = '^'; } + | '&' { $$ = '&'; } + | tCMP { $$ = tCMP; } + | tEQ { $$ = tEQ; } + | tEQQ { $$ = tEQQ; } + | tMATCH { $$ = tMATCH; } + | tNMATCH { $$ = tNMATCH; } + | '>' { $$ = '>'; } + | tGEQ { $$ = tGEQ; } + | '<' { $$ = '<'; } + | tLEQ { $$ = tLEQ; } + | tNEQ { $$ = tNEQ; } + | tLSHFT { $$ = tLSHFT; } + | tRSHFT { $$ = tRSHFT; } + | '+' { $$ = '+'; } + | '-' { $$ = '-'; } + | '*' { $$ = '*'; } + | tSTAR { $$ = '*'; } + | '/' { $$ = '/'; } + | '%' { $$ = '%'; } + | tPOW { $$ = tPOW; } + | tDSTAR { $$ = tDSTAR; } + | '!' { $$ = '!'; } + | '~' { $$ = '~'; } + | tUPLUS { $$ = tUPLUS; } + | tUMINUS { $$ = tUMINUS; } + | tAREF { $$ = tAREF; } + | tASET { $$ = tASET; } + | '`' { $$ = '`'; } ; reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__ @@ -3246,262 +3931,148 @@ reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__ | keyword_while | keyword_until ; -arg : lhs '=' lex_ctxt arg_rhs - { - /*%%%*/ - $$ = node_assign(p, $1, $4, $3, &@$); - /*% %*/ - /*% ripper: assign!($1, $4) %*/ - } - | var_lhs tOP_ASGN lex_ctxt arg_rhs - { - /*%%%*/ - $$ = new_op_assign(p, $1, $2, $4, $3, &@$); - /*% %*/ - /*% ripper: opassign!($1, $2, $4) %*/ - } - | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs - { - /*%%%*/ - $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$); - /*% %*/ - /*% ripper: opassign!(aref_field!($1, $3), $5, $7) %*/ - } - | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs - { - /*%%%*/ - $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$); - /*% %*/ - /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ - } - | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs - { - /*%%%*/ - $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$); - /*% %*/ - /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ - } - | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs - { - /*%%%*/ - $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$); - /*% %*/ - /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ - } - | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs - { - /*%%%*/ - YYLTYPE loc = code_loc_gen(&@1, &@3); - $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$); - /*% %*/ - /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/ - } - | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs - { - /*%%%*/ - YYLTYPE loc = code_loc_gen(&@1, &@2); - $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$); - /*% %*/ - /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/ - } - | backref tOP_ASGN lex_ctxt arg_rhs - { - /*%%%*/ - rb_backref_error(p, $1); - $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/ - } - | arg tDOT2 arg - { - /*%%%*/ - value_expr($1); - value_expr($3); - $$ = NEW_DOT2($1, $3, &@$); - /*% %*/ - /*% ripper: dot2!($1, $3) %*/ - } - | arg tDOT3 arg - { - /*%%%*/ - value_expr($1); - value_expr($3); - $$ = NEW_DOT3($1, $3, &@$); - /*% %*/ - /*% ripper: dot3!($1, $3) %*/ - } - | arg tDOT2 - { - /*%%%*/ - value_expr($1); - $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$); - /*% %*/ - /*% ripper: dot2!($1, Qnil) %*/ - } - | arg tDOT3 - { - /*%%%*/ - value_expr($1); - $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$); - /*% %*/ - /*% ripper: dot3!($1, Qnil) %*/ - } - | tBDOT2 arg - { - /*%%%*/ - value_expr($2); - $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$); - /*% %*/ - /*% ripper: dot2!(Qnil, $2) %*/ - } - | tBDOT3 arg - { - /*%%%*/ - value_expr($2); - $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$); - /*% %*/ - /*% ripper: dot3!(Qnil, $2) %*/ - } +arg : asgn(arg_rhs) + | op_asgn(arg_rhs) + | range_expr(arg) | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/ } | arg '-' arg { $$ = call_bin_op(p, $1, '-', $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/ } | arg '*' arg { $$ = call_bin_op(p, $1, '*', $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/ } | arg '/' arg { $$ = call_bin_op(p, $1, '/', $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/ } | arg '%' arg { $$ = call_bin_op(p, $1, '%', $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/ } | arg tPOW arg { $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/ } | tUMINUS_NUM simple_numeric tPOW arg { $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$); + /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/ } | tUPLUS arg { $$ = call_uni_op(p, $2, idUPlus, &@1, &@$); + /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/ } | tUMINUS arg { $$ = call_uni_op(p, $2, idUMinus, &@1, &@$); + /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/ } | arg '|' arg { $$ = call_bin_op(p, $1, '|', $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/ } | arg '^' arg { $$ = call_bin_op(p, $1, '^', $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/ } | arg '&' arg { $$ = call_bin_op(p, $1, '&', $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/ } | arg tCMP arg { $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/ } | rel_expr %prec tCMP | arg tEQ arg { $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/ } | arg tEQQ arg { $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/ } | arg tNEQ arg { $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/ } | arg tMATCH arg { $$ = match_op(p, $1, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/ } | arg tNMATCH arg { $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/ } | '!' arg { $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$); + /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/ } | '~' arg { $$ = call_uni_op(p, $2, '~', &@1, &@$); + /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/ } | arg tLSHFT arg { $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/ } | arg tRSHFT arg { $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/ } | arg tANDOP arg { $$ = logop(p, idANDOP, $1, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/ } | arg tOROP arg { $$ = logop(p, idOROP, $1, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/ } - | keyword_defined opt_nl begin_defined arg + | keyword_defined '\n'? begin_defined arg { p->ctxt.in_defined = $3.in_defined; - $$ = new_defined(p, $4, &@$); - } - | arg '?' arg opt_nl ':' arg - { - /*%%%*/ - value_expr($1); - $$ = new_if(p, $1, $3, $6, &@$); - fixpos($$, $1); - /*% %*/ - /*% ripper: ifop!($1, $3, $6) %*/ - } - | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt] - { - endless_method_name(p, get_id($head->nd_mid), &@head); - restore_defun(p, $head); - /*%%%*/ - $bodystmt = new_scope_body(p, $args, $bodystmt, &@$); - ($$ = $head->nd_def)->nd_loc = @$; - RNODE_DEFN($$)->nd_defn = $bodystmt; - /*% %*/ - /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/ - /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/ - local_pop(p); - } - | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt] - { - endless_method_name(p, get_id($head->nd_mid), &@head); - restore_defun(p, $head); - /*%%%*/ - $bodystmt = new_scope_body(p, $args, $bodystmt, &@$); - ($$ = $head->nd_def)->nd_loc = @$; - RNODE_DEFS($$)->nd_defn = $bodystmt; - /*% %*/ - /*% ripper[$bodystmt]: bodystmt!($bodystmt, Qnil, Qnil, Qnil) %*/ - /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/ - local_pop(p); + $$ = new_defined(p, $4, &@$, &@1); + p->ctxt.has_trailing_semicolon = $3.has_trailing_semicolon; + /*% ripper: defined!($:4) %*/ } + | def_endless_method(endless_arg) + | ternary | primary + ; + +ternary : arg '?' arg '\n'? ':' arg { - $$ = $1; + value_expr(p, $1); + $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC); + fixpos($$, $1); + /*% ripper: ifop!($:1, $:3, $:6) %*/ } ; @@ -3509,14 +4080,13 @@ endless_arg : arg %prec modifier_rescue | endless_arg modifier_rescue after_rescue arg { p->ctxt.in_rescue = $3.in_rescue; - /*%%%*/ $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4); - /*% %*/ - /*% ripper: rescue_mod!($1, $4) %*/ + /*% ripper: rescue_mod!($:1, $:4) %*/ } - | keyword_not opt_nl endless_arg + | keyword_not '\n'? endless_arg { $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$); + /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/ } ; @@ -3529,11 +4099,13 @@ relop : '>' {$$ = '>';} rel_expr : arg relop arg %prec '>' { $$ = call_bin_op(p, $1, $2, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/ } | rel_expr relop arg %prec '>' { rb_warning1("comparison '%s' after comparison", WARN_ID($2)); $$ = call_bin_op(p, $1, $2, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/ } ; @@ -3557,142 +4129,115 @@ after_rescue : lex_ctxt } ; -arg_value : arg - { - value_expr($1); - $$ = $1; - } +arg_value : value_expr(arg) ; aref_args : none | args trailer - { - $$ = $1; - } | args ',' assocs trailer { - /*%%%*/ $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1; - /*% %*/ - /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/ + /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/ } | assocs trailer { - /*%%%*/ $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0; - /*% %*/ - /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/ + /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/ } ; arg_rhs : arg %prec tOP_ASGN { - value_expr($1); + value_expr(p, $1); $$ = $1; } | arg modifier_rescue after_rescue arg { p->ctxt.in_rescue = $3.in_rescue; - /*%%%*/ - value_expr($1); + value_expr(p, $1); $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4); - /*% %*/ - /*% ripper: rescue_mod!($1, $4) %*/ + /*% ripper: rescue_mod!($:1, $:4) %*/ } ; paren_args : '(' opt_call_args rparen { - /*%%%*/ $$ = $2; - /*% %*/ - /*% ripper: arg_paren!($2) %*/ + /*% ripper: arg_paren!($:2) %*/ } | '(' args ',' args_forward rparen { if (!check_forwarding_args(p)) { - $$ = Qnone; + $$ = 0; } else { - /*%%%*/ $$ = new_args_forward_call(p, $2, &@4, &@$); - /*% %*/ - /*% ripper: arg_paren!(args_add!($2, $4)) %*/ + /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/ } } | '(' args_forward rparen { if (!check_forwarding_args(p)) { - $$ = Qnone; + $$ = 0; } else { - /*%%%*/ $$ = new_args_forward_call(p, 0, &@2, &@$); - /*% %*/ - /*% ripper: arg_paren!($2) %*/ + /*% ripper: arg_paren!($:2) %*/ } } ; opt_paren_args : none | paren_args + { + $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS; + } ; opt_call_args : none | call_args | args ',' - { - $$ = $1; - } | args ',' assocs ',' { - /*%%%*/ $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1; - /*% %*/ - /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/ + /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/ } | assocs ',' { - /*%%%*/ $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0; - /*% %*/ - /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/ + /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/ } ; -call_args : command +call_args : value_expr(command) { - /*%%%*/ - value_expr($1); $$ = NEW_LIST($1, &@$); - /*% %*/ - /*% ripper: args_add!(args_new!, $1) %*/ + /*% ripper: args_add!(args_new!, $:1) %*/ + } + | def_endless_method(endless_command) + { + $$ = NEW_LIST($1, &@$); + /*% ripper: args_add!(args_new!, $:1) %*/ } | args opt_block_arg { - /*%%%*/ $$ = arg_blk_pass($1, $2); - /*% %*/ - /*% ripper: args_add_block!($1, $2) %*/ + /*% ripper: args_add_block!($:1, $:2) %*/ } | assocs opt_block_arg { - /*%%%*/ $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0; $$ = arg_blk_pass($$, $2); - /*% %*/ - /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/ + /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/ } | args ',' assocs opt_block_arg { - /*%%%*/ $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1; $$ = arg_blk_pass($$, $4); - /*% %*/ - /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/ + /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/ } | block_arg - /*% ripper[brace]: args_add_block!(args_new!, $1) %*/ + /*% ripper: args_add_block!(args_new!, $:1) %*/ ; command_args : { @@ -3729,24 +4274,19 @@ command_args : { CMDARG_POP(); if (lookahead) CMDARG_PUSH(0); $$ = $2; + /*% ripper: $:2 %*/ } ; block_arg : tAMPER arg_value { - /*%%%*/ - $$ = NEW_BLOCK_PASS($2, &@$); - /*% %*/ - /*% ripper: $2 %*/ + $$ = NEW_BLOCK_PASS($2, &@$, &@1); + /*% ripper: $:2 %*/ } | tAMPER { - if (!local_id(p, idFWD_BLOCK)) { - compile_error(p, "no anonymous block parameter"); - } - /*%%%*/ - $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$); - /*% %*/ + forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block"); + $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1); /*% ripper: Qnil %*/ } ; @@ -3754,63 +4294,49 @@ block_arg : tAMPER arg_value opt_block_arg : ',' block_arg { $$ = $2; + /*% ripper: $:2 %*/ } | none { $$ = 0; + /*% ripper: Qfalse %*/ } ; /* value */ args : arg_value { - /*%%%*/ - $$ = NEW_LIST($1, &@$); - /*% %*/ - /*% ripper: args_add!(args_new!, $1) %*/ + $$ = NEW_LIST($arg_value, &@$); + /*% ripper: args_add!(args_new!, $:arg_value) %*/ } - | tSTAR arg_value + | arg_splat { - /*%%%*/ - $$ = NEW_SPLAT($2, &@$); - /*% %*/ - /*% ripper: args_add_star!(args_new!, $2) %*/ + $$ = $arg_splat; + /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/ } - | tSTAR + | args[non_last_args] ',' arg_value { - if (!local_id(p, idFWD_REST) || - local_id(p, idFWD_ALL)) { - compile_error(p, "no anonymous rest parameter"); - } - /*%%%*/ - $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@1), &@$); - /*% %*/ - /*% ripper: args_add_star!(args_new!, Qnil) %*/ + $$ = last_arg_append(p, $non_last_args, $arg_value, &@$); + /*% ripper: args_add!($:non_last_args, $:arg_value) %*/ } - | args ',' arg_value + | args[non_last_args] ',' arg_splat { - /*%%%*/ - $$ = last_arg_append(p, $1, $3, &@$); - /*% %*/ - /*% ripper: args_add!($1, $3) %*/ + $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$); + /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/ } - | args ',' tSTAR arg_value + ; + +/* value */ +arg_splat : tSTAR arg_value { - /*%%%*/ - $$ = rest_arg_append(p, $1, $4, &@$); - /*% %*/ - /*% ripper: args_add_star!($1, $4) %*/ + $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR); + /*% ripper: $:arg_value %*/ } - | args ',' tSTAR + | tSTAR /* none */ { - if (!local_id(p, idFWD_REST) || - local_id(p, idFWD_ALL)) { - compile_error(p, "no anonymous rest parameter"); - } - /*%%%*/ - $$ = rest_arg_append(p, $1, NEW_LVAR(idFWD_REST, &@3), &@$); - /*% %*/ - /*% ripper: args_add_star!($1, Qnil) %*/ + forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest"); + $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR); + /*% ripper: Qnil %*/ } ; @@ -3822,28 +4348,23 @@ mrhs_arg : mrhs /* value */ mrhs : args ',' arg_value { - /*%%%*/ - $$ = last_arg_append(p, $1, $3, &@$); - /*% %*/ - /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/ + $$ = last_arg_append(p, $args, $arg_value, &@$); + /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/ } | args ',' tSTAR arg_value { - /*%%%*/ - $$ = rest_arg_append(p, $1, $4, &@$); - /*% %*/ - /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/ + $$ = rest_arg_append(p, $args, $arg_value, &@$); + /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/ } | tSTAR arg_value { - /*%%%*/ - $$ = NEW_SPLAT($2, &@$); - /*% %*/ - /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/ + $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR); + /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/ } ; -primary : literal +%rule %inline inline_primary + : literal | strings | xstring | regexp @@ -3851,400 +4372,355 @@ primary : literal | qwords | symbols | qsymbols - | var_ref - | backref - | tFID - { - /*%%%*/ - $$ = (NODE *)NEW_FCALL($1, 0, &@$); - /*% %*/ - /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/ - } - | k_begin - { - CMDARG_PUSH(0); - } - bodystmt - k_end - { - CMDARG_POP(); - /*%%%*/ - set_line_body($3, @1.end_pos.lineno); - $$ = NEW_BEGIN($3, &@$); - nd_set_line($$, @1.end_pos.lineno); - /*% %*/ - /*% ripper: begin!($3) %*/ - } - | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')' - { - /*%%%*/ - if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0; - $$ = $2; - /*% %*/ - /*% ripper: paren!($2) %*/ - } - | tLPAREN compstmt ')' - { - /*%%%*/ - if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0; - $$ = $2; - /*% %*/ - /*% ripper: paren!($2) %*/ - } - | primary_value tCOLON2 tCONSTANT - { - /*%%%*/ - $$ = NEW_COLON2($1, $3, &@$); - /*% %*/ - /*% ripper: const_path_ref!($1, $3) %*/ - } - | tCOLON3 tCONSTANT - { - /*%%%*/ - $$ = NEW_COLON3($2, &@$); - /*% %*/ - /*% ripper: top_const_ref!($2) %*/ - } - | tLBRACK aref_args ']' - { - /*%%%*/ - $$ = make_list($2, &@$); - /*% %*/ - /*% ripper: array!($2) %*/ - } - | tLBRACE assoc_list '}' - { - /*%%%*/ - $$ = new_hash(p, $2, &@$); - RNODE_HASH($$)->nd_brace = TRUE; - /*% %*/ - /*% ripper: hash!($2) %*/ - } - | k_return - { - /*%%%*/ - $$ = NEW_RETURN(0, &@$); - /*% %*/ - /*% ripper: return0! %*/ - } - | k_yield '(' call_args rparen - { - /*%%%*/ - $$ = new_yield(p, $3, &@$); - /*% %*/ - /*% ripper: yield!(paren!($3)) %*/ - } - | k_yield '(' rparen - { - /*%%%*/ - $$ = NEW_YIELD(0, &@$); - /*% %*/ - /*% ripper: yield!(paren!(args_new!)) %*/ - } - | k_yield - { - /*%%%*/ - $$ = NEW_YIELD(0, &@$); - /*% %*/ - /*% ripper: yield0! %*/ - } - | keyword_defined opt_nl '(' begin_defined expr rparen - { - p->ctxt.in_defined = $4.in_defined; - $$ = new_defined(p, $5, &@$); - } - | keyword_not '(' expr rparen - { - $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$); - } - | keyword_not '(' rparen - { - $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$); - } - | fcall brace_block - { - /*%%%*/ - $$ = method_add_block(p, (NODE *)$1, $2, &@$); - /*% %*/ - /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/ - } - | method_call - | method_call brace_block - { - /*%%%*/ - block_dup_check(p, get_nd_args(p, $1), $2); - $$ = method_add_block(p, $1, $2, &@$); - /*% %*/ - /*% ripper: method_add_block!($1, $2) %*/ - } - | lambda - | k_if expr_value then - compstmt - if_tail - k_end - { - /*%%%*/ - $$ = new_if(p, $2, $4, $5, &@$); - fixpos($$, $2); - /*% %*/ - /*% ripper: if!($2, $4, $5) %*/ - } - | k_unless expr_value then - compstmt - opt_else - k_end - { - /*%%%*/ - $$ = new_unless(p, $2, $4, $5, &@$); - fixpos($$, $2); - /*% %*/ - /*% ripper: unless!($2, $4, $5) %*/ - } - | k_while expr_value_do - compstmt - k_end - { - restore_block_exit(p, $1); - /*%%%*/ - $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$); - fixpos($$, $2); - /*% %*/ - /*% ripper: while!($2, $3) %*/ - } - | k_until expr_value_do - compstmt - k_end - { - restore_block_exit(p, $1); - /*%%%*/ - $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$); - fixpos($$, $2); - /*% %*/ - /*% ripper: until!($2, $3) %*/ - } - | k_case expr_value opt_terms - { - $<val>$ = p->case_labels; - p->case_labels = Qnil; - } - case_body - k_end - { - if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels); - p->case_labels = $<val>4; - /*%%%*/ - $$ = NEW_CASE($2, $5, &@$); - fixpos($$, $2); - /*% %*/ - /*% ripper: case!($2, $5) %*/ - } - | k_case opt_terms - { - $<val>$ = p->case_labels; - p->case_labels = 0; - } - case_body - k_end - { - if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels); - p->case_labels = $<val>3; - /*%%%*/ - $$ = NEW_CASE2($4, &@$); - /*% %*/ - /*% ripper: case!(Qnil, $4) %*/ - } - | k_case expr_value opt_terms - p_case_body - k_end - { - /*%%%*/ - $$ = NEW_CASE3($2, $4, &@$); - /*% %*/ - /*% ripper: case!($2, $4) %*/ - } - | k_for for_var keyword_in expr_value_do - compstmt - k_end - { - restore_block_exit(p, $1); - /*%%%*/ - /* - * for a, b, c in e - * #=> - * e.each{|*x| a, b, c = x} - * - * for a in e - * #=> - * e.each{|x| a, = x} - */ - ID id = internal_id(p); - rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC); - rb_node_args_t *args; - NODE *scope, *internal_var = NEW_DVAR(id, &@2); - rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1); - tbl->ids[0] = id; /* internal id */ - - switch (nd_type($2)) { - case NODE_LASGN: - case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */ - set_nd_value(p, $2, internal_var); - id = 0; - m->nd_plen = 1; - m->nd_next = $2; - break; - case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */ - m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2); - break; - default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */ - m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2); - } - /* {|*internal_id| <m> = internal_id; ... } */ - args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2); - scope = NEW_SCOPE2(tbl, args, $5, &@$); - $$ = NEW_FOR($4, scope, &@$); - fixpos($$, $2); - /*% %*/ - /*% ripper: for!($2, $4, $5) %*/ - } - | k_class cpath superclass - { - begin_definition("class", &@k_class, &@cpath); - } - bodystmt - k_end - { - /*%%%*/ - $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$); - nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno); - set_line_body($bodystmt, @superclass.end_pos.lineno); - nd_set_line($$, @superclass.end_pos.lineno); - /*% %*/ - /*% ripper: class!($cpath, $superclass, $bodystmt) %*/ - local_pop(p); - p->ctxt.in_class = $k_class.in_class; - p->ctxt.shareable_constant_value = $k_class.shareable_constant_value; - } - | k_class tLSHFT expr - { - begin_definition("", &@k_class, &@tLSHFT); - } - term - bodystmt - k_end - { - /*%%%*/ - $$ = NEW_SCLASS($expr, $bodystmt, &@$); - nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno); - set_line_body($bodystmt, nd_line($expr)); - fixpos($$, $expr); - /*% %*/ - /*% ripper: sclass!($expr, $bodystmt) %*/ - local_pop(p); - p->ctxt.in_def = $k_class.in_def; - p->ctxt.in_class = $k_class.in_class; - p->ctxt.shareable_constant_value = $k_class.shareable_constant_value; - } - | k_module cpath - { - begin_definition("module", &@k_module, &@cpath); - } - bodystmt - k_end - { - /*%%%*/ - $$ = NEW_MODULE($cpath, $bodystmt, &@$); - nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno); - set_line_body($bodystmt, @cpath.end_pos.lineno); - nd_set_line($$, @cpath.end_pos.lineno); - /*% %*/ - /*% ripper: module!($cpath, $bodystmt) %*/ - local_pop(p); - p->ctxt.in_class = $k_module.in_class; - p->ctxt.shareable_constant_value = $k_module.shareable_constant_value; - } - | defn_head[head] - f_arglist[args] - { - /*%%%*/ - push_end_expect_token_locations(p, &@head.beg_pos); - /*% %*/ - } - bodystmt - k_end - { - restore_defun(p, $head); - /*%%%*/ - $bodystmt = new_scope_body(p, $args, $bodystmt, &@$); - ($$ = $head->nd_def)->nd_loc = @$; - RNODE_DEFN($$)->nd_defn = $bodystmt; - /*% %*/ - /*% ripper: def!($head->nd_mid, $args, $bodystmt) %*/ - local_pop(p); - } - | defs_head[head] - f_arglist[args] - { - /*%%%*/ - push_end_expect_token_locations(p, &@head.beg_pos); - /*% %*/ - } - bodystmt - k_end - { - restore_defun(p, $head); - /*%%%*/ - $bodystmt = new_scope_body(p, $args, $bodystmt, &@$); - ($$ = $head->nd_def)->nd_loc = @$; - RNODE_DEFS($$)->nd_defn = $bodystmt; - /*% %*/ - /*% ripper: defs!($head->nd_recv, $head->dot_or_colon, $head->nd_mid, $args, $bodystmt) %*/ - local_pop(p); - } - | keyword_break - { - $<node>$ = add_block_exit(p, NEW_BREAK(0, &@$)); - /*% ripper: break!(args_new!) %*/ - } - | keyword_next - { - $<node>$ = add_block_exit(p, NEW_NEXT(0, &@$)); - /*% ripper: next!(args_new!) %*/ - } - | keyword_redo - { - $<node>$ = add_block_exit(p, NEW_REDO(&@$)); - /*% ripper: redo! %*/ - } - | keyword_retry - { - if (!p->ctxt.in_defined) { - switch (p->ctxt.in_rescue) { - case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break; - case after_rescue: /* ok */ break; - case after_else: yyerror1(&@1, "Invalid retry after else"); break; - case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break; - } - } - /*%%%*/ - $$ = NEW_RETRY(&@$); - /*% %*/ - /*% ripper: retry! %*/ - } ; -primary_value : primary - { - value_expr($1); - $$ = $1; +primary : inline_primary + | var_ref + | backref + | tFID[fid] + { + $$ = (NODE *)NEW_FCALL($fid, 0, &@$); + /*% ripper: method_add_arg!(fcall!($:fid), args_new!) %*/ + } + | k_begin[kw] + { + CMDARG_PUSH(0); + } + bodystmt[body] + k_end[k_end] + { + CMDARG_POP(); + set_line_body($body, @kw.end_pos.lineno); + $$ = NEW_BEGIN($body, &@$); + nd_set_line($$, @kw.end_pos.lineno); + /*% ripper: begin!($:body) %*/ + } + | tLPAREN_ARG compstmt(stmts)[body] {SET_LEX_STATE(EXPR_ENDARG);} ')' + { + if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0; + $$ = $body; + /*% ripper: paren!($:body) %*/ + } + | tLPAREN compstmt(stmts)[body] ')' + { + if (nd_type_p($body, NODE_SELF)) RNODE_SELF($body)->nd_state = 0; + $$ = NEW_BLOCK($body, &@$); + /*% ripper: paren!($:body) %*/ + } + | primary_value[recv] tCOLON2[op] tCONSTANT[name] + { + $$ = NEW_COLON2($recv, $name, &@$, &@op, &@name); + /*% ripper: const_path_ref!($:recv, $:name) %*/ + } + | tCOLON3[top] tCONSTANT[name] + { + $$ = NEW_COLON3($name, &@$, &@top, &@name); + /*% ripper: top_const_ref!($:name) %*/ + } + | tLBRACK aref_args[args] ']' + { + $$ = make_list($args, &@$); + /*% ripper: array!($:args) %*/ + } + | tLBRACE assoc_list[list] '}' + { + $$ = new_hash(p, $list, &@$); + RNODE_HASH($$)->nd_brace = TRUE; + /*% ripper: hash!($:list) %*/ + } + | k_return[kw] + { + $$ = NEW_RETURN(0, &@$, &@kw); + /*% ripper: return0! %*/ + } + | k_yield[kw] '('[lpar] call_args[args] rparen[rpar] + { + $$ = NEW_YIELD($args, &@$, &@kw, &@lpar, &@rpar); + /*% ripper: yield!(paren!($:args)) %*/ + } + | k_yield[kw] '('[lpar] rparen[rpar] + { + $$ = NEW_YIELD(0, &@$, &@kw, &@lpar, &@rpar); + /*% ripper: yield!(paren!(args_new!)) %*/ + } + | k_yield[kw] + { + $$ = NEW_YIELD(0, &@$, &@kw, &NULL_LOC, &NULL_LOC); + /*% ripper: yield0! %*/ + } + | keyword_defined[kw] '\n'? '(' begin_defined[ctxt] expr[arg] rparen + { + p->ctxt.in_defined = $ctxt.in_defined; + $$ = new_defined(p, $arg, &@$, &@kw); + p->ctxt.has_trailing_semicolon = $ctxt.has_trailing_semicolon; + /*% ripper: defined!($:arg) %*/ + } + | keyword_not[kw] '(' expr[arg] rparen + { + $$ = call_uni_op(p, method_cond(p, $arg, &@arg), METHOD_NOT, &@kw, &@$); + /*% ripper: unary!(ID2VAL(idNOT), $:arg) %*/ + } + | keyword_not[kw] '('[lpar] rparen + { + $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@lpar), &@lpar), METHOD_NOT, &@kw, &@$); + /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/ + } + | fcall[call] brace_block[block] + { + $$ = method_add_block(p, (NODE *)$call, $block, &@$); + /*% ripper: method_add_block!(method_add_arg!(fcall!($:call), args_new!), $:block) %*/ + } + | method_call + | method_call[call] brace_block[block] + { + block_dup_check(p, get_nd_args(p, $call), $block); + $$ = method_add_block(p, $call, $block, &@$); + /*% ripper: method_add_block!($:call, $:block) %*/ + } + | lambda + | k_if[kw] expr_value[cond] then[then] + compstmt(stmts)[body] + if_tail[tail] + k_end[k_end] + { + if ($tail && nd_type_p($tail, NODE_IF)) + RNODE_IF($tail)->end_keyword_loc = @k_end; + + $$ = new_if(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end); + fixpos($$, $cond); + /*% ripper: if!($:cond, $:body, $:tail) %*/ + } + | k_unless[kw] expr_value[cond] then[then] + compstmt(stmts)[body] + opt_else[tail] + k_end[k_end] + { + $$ = new_unless(p, $cond, $body, $tail, &@$, &@kw, &@then, &@k_end); + fixpos($$, $cond); + /*% ripper: unless!($:cond, $:body, $:tail) %*/ + } + | k_while[kw] expr_value_do[cond] + compstmt(stmts)[body] + k_end[k_end] + { + restore_block_exit(p, $kw); + $$ = NEW_WHILE(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end); + fixpos($$, $cond); + /*% ripper: while!($:cond, $:body) %*/ + } + | k_until[kw] expr_value_do[cond] + compstmt(stmts)[body] + k_end[k_end] + { + restore_block_exit(p, $kw); + $$ = NEW_UNTIL(cond(p, $cond, &@cond), $body, 1, &@$, &@kw, &@k_end); + fixpos($$, $cond); + /*% ripper: until!($:cond, $:body) %*/ + } + | k_case[k_case] expr_value[expr] terms? + { + $$ = p->case_labels; + p->case_labels = CHECK_LITERAL_WHEN; + }[labels]<labels> + case_body[body] + k_end[k_end] + { + if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels); + p->case_labels = $labels; + $$ = NEW_CASE($expr, $body, &@$, &@k_case, &@k_end); + fixpos($$, $expr); + /*% ripper: case!($:expr, $:body) %*/ + } + | k_case[k_case] terms? + { + $$ = p->case_labels; + p->case_labels = 0; + }[labels]<labels> + case_body[body] + k_end[k_end] + { + if (p->case_labels) st_free_table(p->case_labels); + p->case_labels = $labels; + $$ = NEW_CASE2($body, &@$, &@k_case, &@k_end); + /*% ripper: case!(Qnil, $:body) %*/ + } + | k_case[k_case] expr_value[expr] terms? + p_case_body[body] + k_end[k_end] + { + $$ = NEW_CASE3($expr, $body, &@$, &@k_case, &@k_end); + /*% ripper: case!($:expr, $:body) %*/ + } + | k_for[k_for] for_var[for_var] keyword_in[keyword_in] + {COND_PUSH(1);} expr_value[expr_value] do[do] {COND_POP();} + compstmt(stmts)[compstmt] + k_end[k_end] + { + restore_block_exit(p, $k_for); + /* + * for a, b, c in e + * #=> + * e.each{|*x| a, b, c = x} + * + * for a in e + * #=> + * e.each{|x| a, = x} + */ + ID id = internal_id(p); + rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC); + rb_node_args_t *args; + NODE *scope, *internal_var = NEW_DVAR(id, &@for_var); + rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1); + tbl->ids[0] = id; /* internal id */ + + switch (nd_type($for_var)) { + case NODE_LASGN: + case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */ + set_nd_value(p, $for_var, internal_var); + id = 0; + m->nd_plen = 1; + m->nd_next = $for_var; + break; + case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */ + m->nd_next = node_assign(p, $for_var, NEW_FOR_MASGN(internal_var, &@for_var), NO_LEX_CTXT, &@for_var); + break; + default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */ + m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($for_var, &@for_var), 0, &@for_var), internal_var, NO_LEX_CTXT, &@for_var); + } + /* {|*internal_id| <m> = internal_id; ... } */ + args = new_args(p, m, 0, id, 0, new_empty_args_tail(p, &@for_var), &@for_var); + scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$); + YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC; + $$ = NEW_FOR($expr_value, scope, &@$, &@k_for, &@keyword_in, &do_keyword_loc, &@k_end); + RNODE_SCOPE(scope)->nd_parent = $$; + fixpos($$, $for_var); + /*% ripper: for!($:for_var, $:expr_value, $:compstmt) %*/ + } + | k_class cpath superclass + { + begin_definition("class", &@k_class, &@cpath); + } + bodystmt + k_end + { + YYLTYPE inheritance_operator_loc = NULL_LOC; + if ($superclass) { + inheritance_operator_loc = @superclass; + inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1; + } + $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end); + nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno); + set_line_body($bodystmt, @superclass.end_pos.lineno); + nd_set_line($$, @superclass.end_pos.lineno); + /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/ + local_pop(p); + p->ctxt.in_class = $k_class.in_class; + p->ctxt.cant_return = $k_class.cant_return; + p->ctxt.shareable_constant_value = $k_class.shareable_constant_value; + } + | k_class tLSHFT expr_value + { + begin_definition("", &@k_class, &@tLSHFT); + } + term + bodystmt + k_end + { + $$ = NEW_SCLASS($expr_value, $bodystmt, &@$, &@k_class, &@tLSHFT, &@k_end); + nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno); + set_line_body($bodystmt, nd_line($expr_value)); + fixpos($$, $expr_value); + /*% ripper: sclass!($:expr_value, $:bodystmt) %*/ + local_pop(p); + p->ctxt.in_def = $k_class.in_def; + p->ctxt.in_class = $k_class.in_class; + p->ctxt.cant_return = $k_class.cant_return; + p->ctxt.shareable_constant_value = $k_class.shareable_constant_value; + } + | k_module cpath + { + begin_definition("module", &@k_module, &@cpath); + } + bodystmt + k_end + { + $$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end); + nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno); + set_line_body($bodystmt, @cpath.end_pos.lineno); + nd_set_line($$, @cpath.end_pos.lineno); + /*% ripper: module!($:cpath, $:bodystmt) %*/ + local_pop(p); + p->ctxt.in_class = $k_module.in_class; + p->ctxt.cant_return = $k_module.cant_return; + p->ctxt.shareable_constant_value = $k_module.shareable_constant_value; + } + | defn_head[head] + f_arglist[args] + { + push_end_expect_token_locations(p, &@head.beg_pos); + } + bodystmt + k_end + { + restore_defun(p, $head); + ($$ = $head->nd_def)->nd_loc = @$; + $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$); + RNODE_DEFN($$)->nd_defn = $bodystmt; + /*% ripper: def!($:head, $:args, $:bodystmt) %*/ + local_pop(p); + } + | defs_head[head] + f_arglist[args] + { + push_end_expect_token_locations(p, &@head.beg_pos); + } + bodystmt + k_end + { + restore_defun(p, $head); + ($$ = $head->nd_def)->nd_loc = @$; + $bodystmt = new_scope_body(p, $args, $bodystmt, $$, &@$); + RNODE_DEFS($$)->nd_defn = $bodystmt; + /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/ + local_pop(p); + } + | keyword_break[kw] + { + $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@kw)); + /*% ripper: break!(args_new!) %*/ + } + | keyword_next[kw] + { + $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@kw)); + /*% ripper: next!(args_new!) %*/ + } + | keyword_redo[kw] + { + $$ = add_block_exit(p, NEW_REDO(&@$, &@kw)); + /*% ripper: redo! %*/ + } + | keyword_retry[kw] + { + if (!p->ctxt.in_defined) { + switch (p->ctxt.in_rescue) { + case before_rescue: yyerror1(&@kw, "Invalid retry without rescue"); break; + case after_rescue: /* ok */ break; + case after_else: yyerror1(&@kw, "Invalid retry after else"); break; + case after_ensure: yyerror1(&@kw, "Invalid retry after ensure"); break; + } } + $$ = NEW_RETRY(&@$); + /*% ripper: retry! %*/ + } + ; + +primary_value : value_expr(primary) ; k_begin : keyword_begin { token_info_push(p, "begin", &@$); - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } ; @@ -4262,57 +4738,45 @@ k_if : keyword_if p->token_info->nonspc = 0; } } - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } ; k_unless : keyword_unless { token_info_push(p, "unless", &@$); - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } ; -k_while : keyword_while allow_exits +k_while : keyword_while[kw] allow_exits { $$ = $allow_exits; token_info_push(p, "while", &@$); - /*%%%*/ - push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ + push_end_expect_token_locations(p, &@kw.beg_pos); } ; -k_until : keyword_until allow_exits +k_until : keyword_until[kw] allow_exits { $$ = $allow_exits; token_info_push(p, "until", &@$); - /*%%%*/ - push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ + push_end_expect_token_locations(p, &@kw.beg_pos); } ; k_case : keyword_case { token_info_push(p, "case", &@$); - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } ; -k_for : keyword_for allow_exits +k_for : keyword_for[kw] allow_exits { $$ = $allow_exits; token_info_push(p, "for", &@$); - /*%%%*/ - push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ + push_end_expect_token_locations(p, &@kw.beg_pos); } ; @@ -4321,9 +4785,7 @@ k_class : keyword_class token_info_push(p, "class", &@$); $$ = p->ctxt; p->ctxt.in_rescue = before_rescue; - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } ; @@ -4332,9 +4794,7 @@ k_module : keyword_module token_info_push(p, "module", &@$); $$ = p->ctxt; p->ctxt.in_rescue = before_rescue; - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } ; @@ -4349,18 +4809,14 @@ k_def : keyword_def k_do : keyword_do { token_info_push(p, "do", &@$); - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } ; k_do_block : keyword_do_block { token_info_push(p, "do", &@$); - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } ; @@ -4410,9 +4866,7 @@ k_elsif : keyword_elsif k_end : keyword_end { token_info_pop(p, "end", &@$); - /*%%%*/ pop_end_expect_token_locations(p); - /*% %*/ } | tDUMNY_END { @@ -4422,7 +4876,7 @@ k_end : keyword_end k_return : keyword_return { - if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p)) + if (p->ctxt.cant_return && !dyna_in_block(p)) yyerror1(&@1, "Invalid return in class/module body"); } ; @@ -4440,29 +4894,25 @@ then : term ; do : term - | keyword_do_cond + | keyword_do_cond { $$ = keyword_do_cond; } ; if_tail : opt_else | k_elsif expr_value then - compstmt + compstmt(stmts) if_tail { - /*%%%*/ - $$ = new_if(p, $2, $4, $5, &@$); + $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC); fixpos($$, $2); - /*% %*/ - /*% ripper: elsif!($2, $4, $5) %*/ + /*% ripper: elsif!($:2, $:4, $:5) %*/ } ; opt_else : none - | k_else compstmt + | k_else compstmt(stmts) { - /*%%%*/ $$ = $2; - /*% %*/ - /*% ripper: else!($2) %*/ + /*% ripper: else!($:2) %*/ } ; @@ -4472,257 +4922,141 @@ for_var : lhs f_marg : f_norm_arg { - /*%%%*/ $$ = assignable(p, $1, 0, &@$); mark_lvar_used(p, $$); - /*% %*/ - /*% ripper: assignable(p, $1) %*/ } | tLPAREN f_margs rparen { - /*%%%*/ $$ = (NODE *)$2; - /*% %*/ - /*% ripper: mlhs_paren!($2) %*/ + /*% ripper: mlhs_paren!($:2) %*/ } ; -f_marg_list : f_marg - { - /*%%%*/ - $$ = NEW_LIST($1, &@$); - /*% %*/ - /*% ripper: mlhs_add!(mlhs_new!, $1) %*/ - } - | f_marg_list ',' f_marg - { - /*%%%*/ - $$ = list_append(p, $1, $3); - /*% %*/ - /*% ripper: mlhs_add!($1, $3) %*/ - } - ; -f_margs : f_marg_list +f_margs : mlhs_items(f_marg) { - /*%%%*/ $$ = NEW_MASGN($1, 0, &@$); - /*% %*/ - /*% ripper: $1 %*/ + /*% ripper: $:1 %*/ } - | f_marg_list ',' f_rest_marg + | mlhs_items(f_marg) ',' f_rest_marg { - /*%%%*/ $$ = NEW_MASGN($1, $3, &@$); - /*% %*/ - /*% ripper: mlhs_add_star!($1, $3) %*/ + /*% ripper: mlhs_add_star!($:1, $:3) %*/ } - | f_marg_list ',' f_rest_marg ',' f_marg_list + | mlhs_items(f_marg) ',' f_rest_marg ',' mlhs_items(f_marg) { - /*%%%*/ $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$); - /*% %*/ - /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/ + /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/ } | f_rest_marg { - /*%%%*/ $$ = NEW_MASGN(0, $1, &@$); - /*% %*/ - /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/ + /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/ } - | f_rest_marg ',' f_marg_list + | f_rest_marg ',' mlhs_items(f_marg) { - /*%%%*/ $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$); - /*% %*/ - /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/ + /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/ } ; f_rest_marg : tSTAR f_norm_arg { - /*%%%*/ + /*% ripper: $:2 %*/ $$ = assignable(p, $2, 0, &@$); mark_lvar_used(p, $$); - /*% %*/ - /*% ripper: assignable(p, $2) %*/ } | tSTAR { - /*%%%*/ $$ = NODE_SPECIAL_NO_NAME_REST; - /*% %*/ /*% ripper: Qnil %*/ } ; f_any_kwrest : f_kwrest - | f_no_kwarg {$$ = ID2VAL(idNil);} - ; - -f_eq : {p->ctxt.in_argdef = 0;} '='; - -block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg - { - $$ = new_args_tail(p, $1, $3, $4, &@3); - } - | f_block_kwarg opt_f_block_arg + | f_no_kwarg { - $$ = new_args_tail(p, $1, Qnone, $2, &@1); - } - | f_any_kwrest opt_f_block_arg - { - $$ = new_args_tail(p, Qnone, $1, $2, &@1); - } - | f_block_arg - { - $$ = new_args_tail(p, Qnone, Qnone, $1, &@1); + $$ = idNil; + /*% ripper: ID2VAL(idNil) %*/ } ; -opt_block_args_tail : ',' block_args_tail - { - $$ = $2; - } - | /* none */ - { - $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0); - } +f_eq : {p->ctxt.in_argdef = 0;} '='; + +block_args_tail : args_tail_basic(primary_value, none) ; excessed_comma : ',' { /* magic number for rest_id in iseq_set_arguments() */ - /*%%%*/ $$ = NODE_SPECIAL_EXCESSIVE_COMMA; - /*% %*/ /*% ripper: excessed_comma! %*/ } ; -block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail - { - $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$); - } - | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail - { - $$ = new_args(p, $1, $3, $5, $7, $8, &@$); - } - | f_arg ',' f_block_optarg opt_block_args_tail - { - $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$); - } - | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail - { - $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$); - } - | f_arg ',' f_rest_arg opt_block_args_tail - { - $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$); - } - | f_arg excessed_comma - { - $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2); - $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$); - } - | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail - { - $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$); - } - | f_arg opt_block_args_tail - { - $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$); - } - | f_block_optarg ',' f_rest_arg opt_block_args_tail - { - $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$); - } - | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail - { - $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$); - } - | f_block_optarg opt_block_args_tail - { - $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$); - } - | f_block_optarg ',' f_arg opt_block_args_tail - { - $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$); - } - | f_rest_arg opt_block_args_tail +block_param : args-list(primary_value, opt_args_tail(block_args_tail, none)) + | f_arg[pre] excessed_comma { - $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$); + $$ = new_empty_args_tail(p, &@excessed_comma); + $$ = new_args(p, $pre, 0, $excessed_comma, 0, $$, &@$); + /*% ripper: params!($:pre, Qnil, $:excessed_comma, Qnil, Qnil, Qnil, Qnil) %*/ } - | f_rest_arg ',' f_arg opt_block_args_tail + | f_arg[pre] opt_args_tail(block_args_tail, none)[tail] { - $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$); - } - | block_args_tail - { - $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$); + $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$); + /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/ } + | tail-only-args(block_args_tail) ; -opt_block_param : none - | block_param_def +opt_block_param_def : none + | block_param_def + { + p->command_start = TRUE; + } + ; + +block_param_def : '|' opt_block_param opt_bv_decl '|' { - p->command_start = TRUE; + p->max_numparam = ORDINAL_PARAM; + p->ctxt.in_argdef = 0; + $$ = $2; + /*% ripper: block_var!($:2, $:3) %*/ } ; -block_param_def : '|' opt_bv_decl '|' +opt_block_param : /* none */ { - p->cur_arg = 0; - p->max_numparam = ORDINAL_PARAM; - p->ctxt.in_argdef = 0; - /*%%%*/ $$ = 0; - /*% %*/ /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/ - /*% ripper: block_var!($$, $2) %*/ - } - | '|' block_param opt_bv_decl '|' - { - p->cur_arg = 0; - p->max_numparam = ORDINAL_PARAM; - p->ctxt.in_argdef = 0; - /*%%%*/ - $$ = $2; - /*% %*/ - /*% ripper: block_var!($2, $3) %*/ } + | block_param ; - -opt_bv_decl : opt_nl +opt_bv_decl : '\n'? { $$ = 0; + /*% ripper: Qfalse %*/ } - | opt_nl ';' bv_decls opt_nl + | '\n'? ';' bv_decls '\n'? { - /*%%%*/ $$ = 0; - /*% %*/ - /*% ripper: $3 %*/ + /*% ripper: $:3 %*/ } ; bv_decls : bvar - /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/ + /*% ripper[brace]: rb_ary_new3(1, $:1) %*/ | bv_decls ',' bvar - /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/ + /*% ripper[brace]: rb_ary_push($:1, $:3) %*/ ; bvar : tIDENTIFIER { - new_bv(p, get_id($1)); - /*% ripper: get_value($1) %*/ + new_bv(p, $1); + /*% ripper: $:1 %*/ } | f_bad_arg - { - $$ = 0; - } ; max_numparam : { @@ -4736,14 +5070,18 @@ numparam : { } ; -lambda : tLAMBDA[dyna] +it_id : { + $$ = p->it_id; + p->it_id = 0; + } + ; + +lambda : tLAMBDA[lpar] { - token_info_push(p, "->", &@1); - $<vars>dyna = dyna_push(p); - $<num>$ = p->lex.lpar_beg; - p->lex.lpar_beg = p->lex.paren_nest; - }[lpar] - max_numparam numparam allow_exits + token_info_push(p, "->", &@lpar); + $$ = dyna_push(p); + }[dyna]<vars> + max_numparam numparam it_id allow_exits f_larglist[args] { CMDARG_PUSH(0); @@ -4751,75 +5089,70 @@ lambda : tLAMBDA[dyna] lambda_body[body] { int max_numparam = p->max_numparam; - p->lex.lpar_beg = $<num>lpar; + ID it_id = p->it_id; + p->lex.lpar_beg = $lpar; p->max_numparam = $max_numparam; + p->it_id = $it_id; restore_block_exit(p, $allow_exits); CMDARG_POP(); - $args = args_with_numbered(p, $args, max_numparam); - /*%%%*/ + $args = args_with_numbered(p, $args, max_numparam, it_id); { - YYLTYPE loc = code_loc_gen(&@args, &@body); - $$ = NEW_LAMBDA($args, $body, &loc); + YYLTYPE loc = code_loc_gen(&@lpar, &@body); + $$ = NEW_LAMBDA($args, $body->node, &loc, &@lpar, &$body->opening_loc, &$body->closing_loc); nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno); nd_set_line($$, @args.end_pos.lineno); - nd_set_first_loc($$, @1.beg_pos); + nd_set_first_loc($$, @lpar.beg_pos); + xfree($body); } - /*% %*/ - /*% ripper: lambda!($args, $body) %*/ + /*% ripper: lambda!($:args, $:body) %*/ numparam_pop(p, $numparam); - dyna_pop(p, $<vars>dyna); + dyna_pop(p, $dyna); } ; -f_larglist : '(' f_args opt_bv_decl ')' +f_larglist : '(' f_largs[args] opt_bv_decl ')' { p->ctxt.in_argdef = 0; - /*%%%*/ - $$ = $2; + $$ = $args; p->max_numparam = ORDINAL_PARAM; - /*% %*/ - /*% ripper: paren!($2) %*/ + /*% ripper: paren!($:args) %*/ } - | f_args + | f_largs[args] { p->ctxt.in_argdef = 0; - /*%%%*/ - if (!args_info_empty_p(&$1->nd_ainfo)) + if (!args_info_empty_p(&$args->nd_ainfo)) p->max_numparam = ORDINAL_PARAM; - /*% %*/ - $$ = $1; + $$ = $args; } ; -lambda_body : tLAMBEG compstmt '}' +lambda_body : tLAMBEG compstmt(stmts) '}' { token_info_pop(p, "}", &@3); - $$ = $2; + $$ = new_locations_lambda_body(p, $2, &@2, &@1, &@3); + /*% ripper: $:2 %*/ } | keyword_do_LAMBDA { - /*%%%*/ push_end_expect_token_locations(p, &@1.beg_pos); - /*% %*/ } bodystmt k_end { - $$ = $3; + $$ = new_locations_lambda_body(p, $3, &@3, &@1, &@4); + /*% ripper: $:3 %*/ } ; do_block : k_do_block do_body k_end { $$ = $2; - /*%%%*/ set_embraced_location($$, &@1, &@3); - /*% %*/ + /*% ripper: $:2 %*/ } ; block_call : command do_block { - /*%%%*/ if (nd_type_p($1, NODE_YIELD)) { compile_error(p, "block given to yield"); } @@ -4828,199 +5161,178 @@ block_call : command do_block } $$ = method_add_block(p, $1, $2, &@$); fixpos($$, $1); - /*% %*/ - /*% ripper: method_add_block!($1, $2) %*/ + /*% ripper: method_add_block!($:1, $:2) %*/ } | block_call call_op2 operation2 opt_paren_args { - /*%%%*/ + bool has_args = $4 != 0; + if (NODE_EMPTY_ARGS_P($4)) $4 = 0; $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$); - /*% %*/ - /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/ + /*% ripper: call!($:1, $:2, $:3) %*/ + if (has_args) { + /*% ripper: method_add_arg!($:$, $:4) %*/ + } } | block_call call_op2 operation2 opt_paren_args brace_block { - /*%%%*/ + if (NODE_EMPTY_ARGS_P($4)) $4 = 0; $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$); - /*% %*/ - /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/ + /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/ } | block_call call_op2 operation2 command_args do_block { - /*%%%*/ $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$); - /*% %*/ - /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/ + /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/ + } + | block_call call_op2 paren_args + { + $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$); + nd_set_line($$, @2.end_pos.lineno); + /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/ } ; method_call : fcall paren_args { - /*%%%*/ $1->nd_args = $2; $$ = (NODE *)$1; nd_set_last_loc($1, @2.end_pos); - /*% %*/ - /*% ripper: method_add_arg!(fcall!($1), $2) %*/ + /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/ } | primary_value call_op operation2 opt_paren_args { - /*%%%*/ + bool has_args = $4 != 0; + if (NODE_EMPTY_ARGS_P($4)) $4 = 0; $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$); nd_set_line($$, @3.end_pos.lineno); - /*% %*/ - /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/ + /*% ripper: call!($:1, $:2, $:3) %*/ + if (has_args) { + /*% ripper: method_add_arg!($:$, $:4) %*/ + } } | primary_value tCOLON2 operation2 paren_args { - /*%%%*/ - $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$); + $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$); nd_set_line($$, @3.end_pos.lineno); - /*% %*/ - /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/ + /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/ } | primary_value tCOLON2 operation3 { - /*%%%*/ - $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$); - /*% %*/ - /*% ripper: call!($1, $2, $3) %*/ + $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$); + /*% ripper: call!($:1, $:2, $:3) %*/ } - | primary_value call_op paren_args + | primary_value call_op2 paren_args { - /*%%%*/ - $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$); + $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$); nd_set_line($$, @2.end_pos.lineno); - /*% %*/ - /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/ - } - | primary_value tCOLON2 paren_args - { - /*%%%*/ - $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$); - nd_set_line($$, @2.end_pos.lineno); - /*% %*/ - /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/ + /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/ } | keyword_super paren_args { - /*%%%*/ - $$ = NEW_SUPER($2, &@$); - /*% %*/ - /*% ripper: super!($2) %*/ + rb_code_location_t lparen_loc = @2; + rb_code_location_t rparen_loc = @2; + lparen_loc.end_pos.column = lparen_loc.beg_pos.column + 1; + rparen_loc.beg_pos.column = rparen_loc.end_pos.column - 1; + + $$ = NEW_SUPER($2, &@$, &@1, &lparen_loc, &rparen_loc); + /*% ripper: super!($:2) %*/ } | keyword_super { - /*%%%*/ $$ = NEW_ZSUPER(&@$); - /*% %*/ /*% ripper: zsuper! %*/ } | primary_value '[' opt_call_args rbracket { - /*%%%*/ $$ = NEW_CALL($1, tAREF, $3, &@$); fixpos($$, $1); - /*% %*/ - /*% ripper: aref!($1, $3) %*/ + /*% ripper: aref!($:1, $:3) %*/ } ; brace_block : '{' brace_body '}' { $$ = $2; - /*%%%*/ set_embraced_location($$, &@1, &@3); - /*% %*/ + /*% ripper: $:2 %*/ } | k_do do_body k_end { $$ = $2; - /*%%%*/ set_embraced_location($$, &@1, &@3); - /*% %*/ + /*% ripper: $:2 %*/ } ; -brace_body : {$<vars>$ = dyna_push(p);}[dyna] - max_numparam numparam allow_exits - opt_block_param[args] compstmt +brace_body : {$$ = dyna_push(p);}[dyna]<vars> + max_numparam numparam it_id allow_exits + opt_block_param_def[args] compstmt(stmts) { int max_numparam = p->max_numparam; + ID it_id = p->it_id; p->max_numparam = $max_numparam; - $args = args_with_numbered(p, $args, max_numparam); - /*%%%*/ + p->it_id = $it_id; + $args = args_with_numbered(p, $args, max_numparam, it_id); $$ = NEW_ITER($args, $compstmt, &@$); - /*% %*/ - /*% ripper: brace_block!($args, $compstmt) %*/ + /*% ripper: brace_block!($:args, $:compstmt) %*/ restore_block_exit(p, $allow_exits); numparam_pop(p, $numparam); - dyna_pop(p, $<vars>dyna); + dyna_pop(p, $dyna); } ; do_body : { - $<vars>$ = dyna_push(p); + $$ = dyna_push(p); CMDARG_PUSH(0); - }[dyna] - max_numparam numparam allow_exits - opt_block_param[args] bodystmt + }[dyna]<vars> + max_numparam numparam it_id allow_exits + opt_block_param_def[args] bodystmt { int max_numparam = p->max_numparam; + ID it_id = p->it_id; p->max_numparam = $max_numparam; - $args = args_with_numbered(p, $args, max_numparam); - /*%%%*/ + p->it_id = $it_id; + $args = args_with_numbered(p, $args, max_numparam, it_id); $$ = NEW_ITER($args, $bodystmt, &@$); - /*% %*/ - /*% ripper: do_block!($args, $bodystmt) %*/ + /*% ripper: do_block!($:args, $:bodystmt) %*/ CMDARG_POP(); restore_block_exit(p, $allow_exits); numparam_pop(p, $numparam); - dyna_pop(p, $<vars>dyna); + dyna_pop(p, $dyna); } ; case_args : arg_value { - /*%%%*/ - check_literal_when(p, $1, &@1); - $$ = NEW_LIST($1, &@$); - /*% %*/ - /*% ripper: args_add!(args_new!, $1) %*/ + check_literal_when(p, $arg_value, &@arg_value); + $$ = NEW_LIST($arg_value, &@$); + /*% ripper: args_add!(args_new!, $:arg_value) %*/ } | tSTAR arg_value { - /*%%%*/ - $$ = NEW_SPLAT($2, &@$); - /*% %*/ - /*% ripper: args_add_star!(args_new!, $2) %*/ + $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR); + /*% ripper: args_add_star!(args_new!, $:arg_value) %*/ } - | case_args ',' arg_value + | case_args[non_last_args] ',' arg_value { - /*%%%*/ - check_literal_when(p, $3, &@3); - $$ = last_arg_append(p, $1, $3, &@$); - /*% %*/ - /*% ripper: args_add!($1, $3) %*/ + check_literal_when(p, $arg_value, &@arg_value); + $$ = last_arg_append(p, $non_last_args, $arg_value, &@$); + /*% ripper: args_add!($:non_last_args, $:arg_value) %*/ } - | case_args ',' tSTAR arg_value + | case_args[non_last_args] ',' tSTAR arg_value { - /*%%%*/ - $$ = rest_arg_append(p, $1, $4, &@$); - /*% %*/ - /*% ripper: args_add_star!($1, $4) %*/ + $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$); + /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/ } ; case_body : k_when case_args then - compstmt + compstmt(stmts) cases { - /*%%%*/ - $$ = NEW_WHEN($2, $4, $5, &@$); + $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3); fixpos($$, $2); - /*% %*/ - /*% ripper: when!($2, $4, $5) %*/ + /*% ripper: when!($:2, $:4, $:5) %*/ } ; @@ -5036,6 +5348,8 @@ p_in_kwarg : { SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); p->command_start = FALSE; p->ctxt.in_kwarg = 1; + p->ctxt.in_alt_pattern = 0; + p->ctxt.capture_in_pattern = 0; } ; @@ -5046,14 +5360,14 @@ p_case_body : keyword_in pop_pktbl(p, $p_pktbl); pop_pvtbl(p, $p_pvtbl); p->ctxt.in_kwarg = $ctxt.in_kwarg; + p->ctxt.in_alt_pattern = $ctxt.in_alt_pattern; + p->ctxt.capture_in_pattern = $ctxt.capture_in_pattern; } - compstmt + compstmt(stmts) p_cases[cases] { - /*%%%*/ - $$ = NEW_IN($expr, $compstmt, $cases, &@$); - /*% %*/ - /*% ripper: in!($expr, $compstmt, $cases) %*/ + $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC); + /*% ripper: in!($:expr, $:compstmt, $:cases) %*/ } ; @@ -5064,47 +5378,45 @@ p_cases : opt_else p_top_expr : p_top_expr_body | p_top_expr_body modifier_if expr_value { - /*%%%*/ - $$ = new_if(p, $3, $1, 0, &@$); + $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC); fixpos($$, $3); - /*% %*/ - /*% ripper: if_mod!($3, $1) %*/ + /*% ripper: if_mod!($:3, $:1) %*/ } | p_top_expr_body modifier_unless expr_value { - /*%%%*/ - $$ = new_unless(p, $3, $1, 0, &@$); + $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC); fixpos($$, $3); - /*% %*/ - /*% ripper: unless_mod!($3, $1) %*/ + /*% ripper: unless_mod!($:3, $:1) %*/ } ; p_top_expr_body : p_expr | p_expr ',' { - $$ = new_array_pattern_tail(p, Qnone, 1, Qnone, Qnone, &@$); - $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$); + $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$); + $$ = new_array_pattern(p, 0, $1, $$, &@$); + /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/ } | p_expr ',' p_args { - $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$); - /*%%%*/ + $$ = new_array_pattern(p, 0, $1, $3, &@$); nd_set_first_loc($$, @1.beg_pos); - /*% - %*/ + /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/ } | p_find { - $$ = new_find_pattern(p, Qnone, $1, &@$); + $$ = new_find_pattern(p, 0, $1, &@$); + /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/ } | p_args_tail { - $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$); + $$ = new_array_pattern(p, 0, 0, $1, &@$); + /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/ } | p_kwargs { - $$ = new_hash_pattern(p, Qnone, $1, &@$); + $$ = new_hash_pattern(p, 0, $1, &@$); + /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/ } ; @@ -5113,107 +5425,115 @@ p_expr : p_as p_as : p_expr tASSOC p_variable { - /*%%%*/ NODE *n = NEW_LIST($1, &@$); n = list_append(p, n, $3); $$ = new_hash(p, n, &@$); - /*% %*/ - /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/ + /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/ } | p_alt ; -p_alt : p_alt '|' p_expr_basic +p_alt : p_alt[left] '|'[alt] { - /*%%%*/ - $$ = NEW_OR($1, $3, &@$); - /*% %*/ - /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/ + p->ctxt.in_alt_pattern = 1; + } + p_expr_basic[right] + { + if (p->ctxt.capture_in_pattern) { + yyerror1(&@alt, "alternative pattern after variable capture"); + } + p->ctxt.in_alt_pattern = 0; + $$ = NEW_OR($left, $right, &@$, &@alt); + /*% ripper: binary!($:left, ID2VAL(idOr), $:right) %*/ } | p_expr_basic ; -p_lparen : '(' p_pktbl { $$ = $2;}; -p_lbracket : '[' p_pktbl { $$ = $2;}; +p_lparen : '(' p_pktbl + { + $$ = $2; + /*% ripper: $:2 %*/ + } + ; + +p_lbracket : '[' p_pktbl + { + $$ = $2; + /*% ripper: $:2 %*/ + } + ; p_expr_basic : p_value | p_variable | p_const p_lparen[p_pktbl] p_args rparen { pop_pktbl(p, $p_pktbl); - $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$); - /*%%%*/ + $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$); nd_set_first_loc($$, @p_const.beg_pos); - /*% - %*/ + /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/ } | p_const p_lparen[p_pktbl] p_find rparen { pop_pktbl(p, $p_pktbl); $$ = new_find_pattern(p, $p_const, $p_find, &@$); - /*%%%*/ nd_set_first_loc($$, @p_const.beg_pos); - /*% - %*/ + /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/ } | p_const p_lparen[p_pktbl] p_kwargs rparen { pop_pktbl(p, $p_pktbl); $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$); - /*%%%*/ nd_set_first_loc($$, @p_const.beg_pos); - /*% - %*/ + /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/ } | p_const '(' rparen { - $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$); - $$ = new_array_pattern(p, $p_const, Qnone, $$, &@$); + $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$); + $$ = new_array_pattern(p, $p_const, 0, $$, &@$); + /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/ } | p_const p_lbracket[p_pktbl] p_args rbracket { pop_pktbl(p, $p_pktbl); - $$ = new_array_pattern(p, $p_const, Qnone, $p_args, &@$); - /*%%%*/ + $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$); nd_set_first_loc($$, @p_const.beg_pos); - /*% - %*/ + /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/ } | p_const p_lbracket[p_pktbl] p_find rbracket { pop_pktbl(p, $p_pktbl); $$ = new_find_pattern(p, $p_const, $p_find, &@$); - /*%%%*/ nd_set_first_loc($$, @p_const.beg_pos); - /*% - %*/ + /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/ } | p_const p_lbracket[p_pktbl] p_kwargs rbracket { pop_pktbl(p, $p_pktbl); $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$); - /*%%%*/ nd_set_first_loc($$, @p_const.beg_pos); - /*% - %*/ + /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/ } | p_const '[' rbracket { - $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$); - $$ = new_array_pattern(p, $1, Qnone, $$, &@$); + $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$); + $$ = new_array_pattern(p, $p_const, 0, $$, &@$); + /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/ } | tLBRACK p_args rbracket { - $$ = new_array_pattern(p, Qnone, Qnone, $p_args, &@$); + $$ = new_array_pattern(p, 0, 0, $p_args, &@$); + /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/ } | tLBRACK p_find rbracket { - $$ = new_find_pattern(p, Qnone, $p_find, &@$); + $$ = new_find_pattern(p, 0, $p_find, &@$); + /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/ } | tLBRACK rbracket { - $$ = new_array_pattern_tail(p, Qnone, 0, Qnone, Qnone, &@$); - $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$); + $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$); + $$ = new_array_pattern(p, 0, 0, $$, &@$); + /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/ } | tLBRACE p_pktbl lex_ctxt[ctxt] { @@ -5223,167 +5543,154 @@ p_expr_basic : p_value { pop_pktbl(p, $p_pktbl); p->ctxt.in_kwarg = $ctxt.in_kwarg; - $$ = new_hash_pattern(p, Qnone, $p_kwargs, &@$); + $$ = new_hash_pattern(p, 0, $p_kwargs, &@$); + /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/ } | tLBRACE rbrace { - $$ = new_hash_pattern_tail(p, Qnone, 0, &@$); - $$ = new_hash_pattern(p, Qnone, $$, &@$); + $$ = new_hash_pattern_tail(p, 0, 0, &@$); + $$ = new_hash_pattern(p, 0, $$, &@$); + /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/ } | tLPAREN p_pktbl p_expr rparen { pop_pktbl(p, $p_pktbl); $$ = $p_expr; + /*% ripper: $:p_expr %*/ } ; p_args : p_expr { - /*%%%*/ NODE *pre_args = NEW_LIST($1, &@$); - $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$); - /*% - $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, Qnone, Qnone, &@$); - %*/ + $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$); + /*% ripper: [[$:1], Qnil, Qnil] %*/ } | p_args_head { - $$ = new_array_pattern_tail(p, $1, 1, Qnone, Qnone, &@$); + $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$); + /*% ripper: [$:1, Qnil, Qnil] %*/ } | p_args_head p_arg { - /*%%%*/ - $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, Qnone, Qnone, &@$); - /*% - VALUE pre_args = rb_ary_concat($1, get_value($2)); - $$ = new_array_pattern_tail(p, pre_args, 0, Qnone, Qnone, &@$); - %*/ + $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$); + /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/ } | p_args_head p_rest { - $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$); + $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$); + /*% ripper: [$:1, $:2, Qnil] %*/ } | p_args_head p_rest ',' p_args_post { $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$); + /*% ripper: [$:1, $:2, $:4] %*/ } | p_args_tail ; p_args_head : p_arg ',' - { - $$ = $1; - } | p_args_head p_arg ',' { - /*%%%*/ $$ = list_concat($1, $2); - /*% %*/ - /*% ripper: rb_ary_concat($1, get_value($2)) %*/ + /*% ripper: rb_ary_concat($:1, $:2) %*/ } ; p_args_tail : p_rest { - $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$); + $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$); + /*% ripper: [Qnil, $:1, Qnil] %*/ } | p_rest ',' p_args_post { - $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$); + $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$); + /*% ripper: [Qnil, $:1, $:3] %*/ } ; p_find : p_rest ',' p_args_post ',' p_rest { $$ = new_find_pattern_tail(p, $1, $3, $5, &@$); + /*% ripper: [$:1, $:3, $:5] %*/ } ; p_rest : tSTAR tIDENTIFIER { - /*%%%*/ error_duplicate_pattern_variable(p, $2, &@2); + /*% ripper: var_field!($:2) %*/ $$ = assignable(p, $2, 0, &@$); - /*% %*/ - /*% ripper: assignable(p, var_field(p, $2)) %*/ } | tSTAR { - /*%%%*/ $$ = 0; - /*% %*/ - /*% ripper: var_field(p, Qnil) %*/ + /*% ripper: var_field!(Qnil) %*/ } ; p_args_post : p_arg | p_args_post ',' p_arg { - /*%%%*/ $$ = list_concat($1, $3); - /*% %*/ - /*% ripper: rb_ary_concat($1, get_value($3)) %*/ + /*% ripper: rb_ary_concat($:1, $:3) %*/ } ; p_arg : p_expr { - /*%%%*/ $$ = NEW_LIST($1, &@$); - /*% %*/ - /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/ + /*% ripper: [$:1] %*/ } ; p_kwargs : p_kwarg ',' p_any_kwrest { $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$); + /*% ripper: [$:1, $:3] %*/ } | p_kwarg { $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$); + /*% ripper: [$:1, Qnil] %*/ } | p_kwarg ',' { $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$); + /*% ripper: [$:1, Qnil] %*/ } | p_any_kwrest { - $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$); + $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$); + /*% ripper: [[], $:1] %*/ } ; p_kwarg : p_kw - /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/ + /*% ripper[brace]: [$:1] %*/ | p_kwarg ',' p_kw { - /*%%%*/ $$ = list_concat($1, $3); - /*% %*/ - /*% ripper: rb_ary_push($1, $3) %*/ + /*% ripper: rb_ary_push($:1, $:3) %*/ } ; p_kw : p_kw_label p_expr { - error_duplicate_pattern_key(p, get_id($1), &@1); - /*%%%*/ - $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2); - /*% %*/ - /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/ + error_duplicate_pattern_key(p, $1, &@1); + $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2); + /*% ripper: [$:1, $:2] %*/ } | p_kw_label { - error_duplicate_pattern_key(p, get_id($1), &@1); - if ($1 && !is_local_id(get_id($1))) { + error_duplicate_pattern_key(p, $1, &@1); + if ($1 && !is_local_id($1)) { yyerror1(&@1, "key must be valid as local variables"); } - error_duplicate_pattern_variable(p, get_id($1), &@1); - /*%%%*/ - $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$)); - /*% %*/ - /*% ripper: rb_ary_new_from_args(2, get_value(assignable(p, $1)), Qnil) %*/ + error_duplicate_pattern_variable(p, $1, &@1); + $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$)); + /*% ripper: [$:1, Qnil] %*/ } ; @@ -5391,33 +5698,27 @@ p_kw_label : tLABEL | tSTRING_BEG string_contents tLABEL_END { YYLTYPE loc = code_loc_gen(&@1, &@3); - /*%%%*/ if (!$2 || nd_type_p($2, NODE_STR)) { NODE *node = dsym_node(p, $2, &loc); - $$ = SYM2ID(RNODE_LIT(node)->nd_lit); - } - /*% - if (ripper_is_node_yylval(p, $2) && RNODE_RIPPER($2)->nd_cval) { - VALUE label = RNODE_RIPPER($2)->nd_cval; - VALUE rval = RNODE_RIPPER($2)->nd_rval; - $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label); - RNODE($$)->nd_loc = loc; + $$ = rb_sym2id(rb_node_sym_string_val(node)); } - %*/ else { yyerror1(&loc, "symbol literal with interpolation is not allowed"); - $$ = 0; + $$ = rb_intern_str(STR_NEW0()); } + /*% ripper: $:2 %*/ } ; p_kwrest : kwrest_mark tIDENTIFIER { $$ = $2; + /*% ripper: var_field!($:2) %*/ } | kwrest_mark { $$ = 0; + /*% ripper: Qnil %*/ } ; @@ -5428,157 +5729,90 @@ p_kwnorest : kwrest_mark keyword_nil ; p_any_kwrest : p_kwrest - | p_kwnorest {$$ = ID2VAL(idNil);} + | p_kwnorest + { + $$ = idNil; + /*% ripper: var_field!(ID2VAL(idNil)) %*/ + } ; p_value : p_primitive - | p_primitive tDOT2 p_primitive - { - /*%%%*/ - value_expr($1); - value_expr($3); - $$ = NEW_DOT2($1, $3, &@$); - /*% %*/ - /*% ripper: dot2!($1, $3) %*/ - } - | p_primitive tDOT3 p_primitive - { - /*%%%*/ - value_expr($1); - value_expr($3); - $$ = NEW_DOT3($1, $3, &@$); - /*% %*/ - /*% ripper: dot3!($1, $3) %*/ - } - | p_primitive tDOT2 - { - /*%%%*/ - value_expr($1); - $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$); - /*% %*/ - /*% ripper: dot2!($1, Qnil) %*/ - } - | p_primitive tDOT3 - { - /*%%%*/ - value_expr($1); - $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$); - /*% %*/ - /*% ripper: dot3!($1, Qnil) %*/ - } + | range_expr(p_primitive) | p_var_ref | p_expr_ref | p_const - | tBDOT2 p_primitive - { - /*%%%*/ - value_expr($2); - $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$); - /*% %*/ - /*% ripper: dot2!(Qnil, $2) %*/ - } - | tBDOT3 p_primitive - { - /*%%%*/ - value_expr($2); - $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$); - /*% %*/ - /*% ripper: dot3!(Qnil, $2) %*/ - } ; -p_primitive : literal - | strings - | xstring - | regexp - | words - | qwords - | symbols - | qsymbols +p_primitive : inline_primary | keyword_variable { - /*%%%*/ - if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper: var_ref!($1) %*/ + if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$); + /*% ripper: var_ref!($:1) %*/ } | lambda ; p_variable : tIDENTIFIER { - /*%%%*/ error_duplicate_pattern_variable(p, $1, &@1); + /*% ripper: var_field!($:1) %*/ $$ = assignable(p, $1, 0, &@$); - /*% %*/ - /*% ripper: assignable(p, var_field(p, $1)) %*/ } ; p_var_ref : '^' tIDENTIFIER { - /*%%%*/ NODE *n = gettable(p, $2, &@$); - if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) { + if (!n) { + n = NEW_ERROR(&@$); + } + else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) { compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2)); } $$ = n; - /*% %*/ - /*% ripper: var_ref!($2) %*/ + /*% ripper: var_ref!($:2) %*/ } | '^' nonlocal_var { - /*%%%*/ - if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper: var_ref!($2) %*/ + if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$); + /*% ripper: var_ref!($:2) %*/ } ; p_expr_ref : '^' tLPAREN expr_value rparen { - /*%%%*/ - $$ = NEW_BEGIN($3, &@$); - /*% %*/ - /*% ripper: begin!($3) %*/ + $$ = NEW_BLOCK($3, &@$); + /*% ripper: begin!($:3) %*/ } ; p_const : tCOLON3 cname { - /*%%%*/ - $$ = NEW_COLON3($2, &@$); - /*% %*/ - /*% ripper: top_const_ref!($2) %*/ + $$ = NEW_COLON3($2, &@$, &@1, &@2); + /*% ripper: top_const_ref!($:2) %*/ } | p_const tCOLON2 cname { - /*%%%*/ - $$ = NEW_COLON2($1, $3, &@$); - /*% %*/ - /*% ripper: const_path_ref!($1, $3) %*/ + $$ = NEW_COLON2($1, $3, &@$, &@2, &@3); + /*% ripper: const_path_ref!($:1, $:3) %*/ } | tCONSTANT { - /*%%%*/ $$ = gettable(p, $1, &@$); - /*% %*/ - /*% ripper: var_ref!($1) %*/ + /*% ripper: var_ref!($:1) %*/ } ; opt_rescue : k_rescue exc_list exc_var then - compstmt + compstmt(stmts) opt_rescue { - /*%%%*/ - NODE *body = $5; + NODE *err = $3; if ($3) { - NODE *err = NEW_ERRINFO(&@3); + err = NEW_ERRINFO(&@3); err = node_assign(p, $3, err, NO_LEX_CTXT, &@3); - body = block_append(p, err, body); } - $$ = NEW_RESBODY($2, body, $6, &@$); + $$ = NEW_RESBODY($2, $3, $5, $6, &@$); if ($2) { fixpos($$, $2); } @@ -5588,25 +5822,19 @@ opt_rescue : k_rescue exc_list exc_var then else { fixpos($$, $5); } - /*% %*/ - /*% ripper: rescue!($2, $3, $5, $6) %*/ + /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/ } | none ; exc_list : arg_value { - /*%%%*/ $$ = NEW_LIST($1, &@$); - /*% %*/ - /*% ripper: rb_ary_new3(1, get_value($1)) %*/ + /*% ripper: rb_ary_new3(1, $:1) %*/ } | mrhs { - /*%%%*/ if (!($$ = splat_array($1))) $$ = $1; - /*% %*/ - /*% ripper: $1 %*/ } | none ; @@ -5614,17 +5842,17 @@ exc_list : arg_value exc_var : tASSOC lhs { $$ = $2; + /*% ripper: $:2 %*/ } | none ; -opt_ensure : k_ensure compstmt +opt_ensure : k_ensure stmts terms? { p->ctxt.in_rescue = $1.in_rescue; - /*%%%*/ $$ = $2; - /*% %*/ - /*% ripper: ensure!($2) %*/ + void_expr(p, void_stmts(p, $$)); + /*% ripper: ensure!($:2) %*/ } | none ; @@ -5635,18 +5863,13 @@ literal : numeric strings : string { - /*%%%*/ - NODE *node = $1; - if (!node) { - node = NEW_STR(STR_NEW0(), &@$); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit); + if (!$1) { + $$ = NEW_STR(STRING_NEW0(), &@$); } else { - node = evstr2dstr(p, node); + $$ = evstr2dstr(p, $1); } - $$ = node; - /*% %*/ - /*% ripper: $1 %*/ + /*% ripper: $:1 %*/ } ; @@ -5654,212 +5877,143 @@ string : tCHAR | string1 | string string1 { - /*%%%*/ $$ = literal_concat(p, $1, $2, &@$); - /*% %*/ - /*% ripper: string_concat!($1, $2) %*/ + /*% ripper: string_concat!($:1, $:2) %*/ } ; string1 : tSTRING_BEG string_contents tSTRING_END { - /*%%%*/ $$ = heredoc_dedent(p, $2); if ($$) nd_set_loc($$, &@$); - /*% %*/ - /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/ + /*% ripper: $:2 %*/ + if (p->heredoc_indent > 0) { + /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/ + p->heredoc_indent = 0; + } + /*% ripper: string_literal!($:$) %*/ } ; xstring : tXSTRING_BEG xstring_contents tSTRING_END { - /*%%%*/ $$ = new_xstring(p, heredoc_dedent(p, $2), &@$); - /*% %*/ - /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/ + /*% ripper: $:2 %*/ + if (p->heredoc_indent > 0) { + /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/ + p->heredoc_indent = 0; + } + /*% ripper: xstring_literal!($:$) %*/ } ; regexp : tREGEXP_BEG regexp_contents tREGEXP_END { - $$ = new_regexp(p, $2, $3, &@$); + $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3); + /*% ripper: regexp_literal!($:2, $:3) %*/ } ; -words_sep : ' ' {} - | words_sep ' ' - ; - -words : tWORDS_BEG words_sep word_list tSTRING_END - { - /*%%%*/ - $$ = make_list($3, &@$); - /*% %*/ - /*% ripper: array!($3) %*/ - } +words : words(tWORDS_BEG, word_list) ; word_list : /* none */ { - /*%%%*/ $$ = 0; - /*% %*/ /*% ripper: words_new! %*/ } - | word_list word words_sep + | word_list word ' '+ { - /*%%%*/ $$ = list_append(p, $1, evstr2dstr(p, $2)); - /*% %*/ - /*% ripper: words_add!($1, $2) %*/ + /*% ripper: words_add!($:1, $:2) %*/ } ; word : string_content - /*% ripper[brace]: word_add!(word_new!, $1) %*/ + /*% ripper[brace]: word_add!(word_new!, $:1) %*/ | word string_content { - /*%%%*/ $$ = literal_concat(p, $1, $2, &@$); - /*% %*/ - /*% ripper: word_add!($1, $2) %*/ + /*% ripper: word_add!($:1, $:2) %*/ } ; -symbols : tSYMBOLS_BEG words_sep symbol_list tSTRING_END - { - /*%%%*/ - $$ = make_list($3, &@$); - /*% %*/ - /*% ripper: array!($3) %*/ - } +symbols : words(tSYMBOLS_BEG, symbol_list) ; symbol_list : /* none */ { - /*%%%*/ $$ = 0; - /*% %*/ /*% ripper: symbols_new! %*/ } - | symbol_list word words_sep + | symbol_list word ' '+ { - /*%%%*/ $$ = symbol_append(p, $1, evstr2dstr(p, $2)); - /*% %*/ - /*% ripper: symbols_add!($1, $2) %*/ + /*% ripper: symbols_add!($:1, $:2) %*/ } ; -qwords : tQWORDS_BEG words_sep qword_list tSTRING_END - { - /*%%%*/ - $$ = make_list($3, &@$); - /*% %*/ - /*% ripper: array!($3) %*/ - } +qwords : words(tQWORDS_BEG, qword_list) ; -qsymbols : tQSYMBOLS_BEG words_sep qsym_list tSTRING_END - { - /*%%%*/ - $$ = make_list($3, &@$); - /*% %*/ - /*% ripper: array!($3) %*/ - } +qsymbols : words(tQSYMBOLS_BEG, qsym_list) ; qword_list : /* none */ { - /*%%%*/ $$ = 0; - /*% %*/ /*% ripper: qwords_new! %*/ } - | qword_list tSTRING_CONTENT words_sep + | qword_list tSTRING_CONTENT ' '+ { - /*%%%*/ $$ = list_append(p, $1, $2); - /*% %*/ - /*% ripper: qwords_add!($1, $2) %*/ + /*% ripper: qwords_add!($:1, $:2) %*/ } ; qsym_list : /* none */ { - /*%%%*/ $$ = 0; - /*% %*/ /*% ripper: qsymbols_new! %*/ } - | qsym_list tSTRING_CONTENT words_sep + | qsym_list tSTRING_CONTENT ' '+ { - /*%%%*/ $$ = symbol_append(p, $1, $2); - /*% %*/ - /*% ripper: qsymbols_add!($1, $2) %*/ + /*% ripper: qsymbols_add!($:1, $:2) %*/ } ; -string_contents : /* none */ +string_contents : /* none */ { - /*%%%*/ $$ = 0; - /*% %*/ /*% ripper: string_content! %*/ - /*%%%*/ - /*% - $$ = ripper_new_yylval(p, 0, $$, 0); - %*/ } | string_contents string_content { - /*%%%*/ $$ = literal_concat(p, $1, $2, &@$); - /*% %*/ - /*% ripper: string_add!($1, $2) %*/ - /*%%%*/ - /*% - if (ripper_is_node_yylval(p, $1) && ripper_is_node_yylval(p, $2) && - !RNODE_RIPPER($1)->nd_cval) { - RNODE_RIPPER($1)->nd_cval = RNODE_RIPPER($2)->nd_cval; - RNODE_RIPPER($1)->nd_rval = add_mark_object(p, $$); - $$ = $1; - } - %*/ + /*% ripper: string_add!($:1, $:2) %*/ } ; xstring_contents: /* none */ { - /*%%%*/ $$ = 0; - /*% %*/ /*% ripper: xstring_new! %*/ } | xstring_contents string_content { - /*%%%*/ $$ = literal_concat(p, $1, $2, &@$); - /*% %*/ - /*% ripper: xstring_add!($1, $2) %*/ + /*% ripper: xstring_add!($:1, $:2) %*/ } ; -regexp_contents: /* none */ +regexp_contents : /* none */ { - /*%%%*/ $$ = 0; - /*% %*/ /*% ripper: regexp_new! %*/ - /*%%%*/ - /*% - $$ = ripper_new_yylval(p, 0, $$, 0); - %*/ } | regexp_contents string_content { - /*%%%*/ NODE *head = $1, *tail = $2; if (!head) { $$ = tail; @@ -5875,79 +6029,60 @@ regexp_contents: /* none */ case NODE_DSTR: break; default: - head = list_append(p, NEW_DSTR(Qnil, &@$), head); + head = list_append(p, NEW_DSTR(0, &@$), head); break; } $$ = list_append(p, head, tail); } - /*% - VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2; - if (ripper_is_node_yylval(p, n1)) { - s1 = RNODE_RIPPER(n1)->nd_cval; - n1 = RNODE_RIPPER(n1)->nd_rval; - } - if (ripper_is_node_yylval(p, n2)) { - s2 = RNODE_RIPPER(n2)->nd_cval; - n2 = RNODE_RIPPER(n2)->nd_rval; - } - $$ = dispatch2(regexp_add, n1, n2); - if (!s1 && s2) { - $$ = ripper_new_yylval(p, 0, $$, s2); - } - %*/ + /*% ripper: regexp_add!($:1, $:2) %*/ } ; -string_content : tSTRING_CONTENT - /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/ - | tSTRING_DVAR +string_content : tSTRING_CONTENT[content] + /*% ripper[brace]: $:content %*/ + | tSTRING_DVAR[state] { /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */ - $<strterm>$ = p->lex.strterm; + $$ = p->lex.strterm; p->lex.strterm = 0; SET_LEX_STATE(EXPR_BEG); - } - string_dvar + }[strterm]<strterm> + string_dvar[dvar] { - p->lex.strterm = $<strterm>2; - /*%%%*/ - $$ = NEW_EVSTR($3, &@$); - nd_set_line($$, @3.end_pos.lineno); - /*% %*/ - /*% ripper: string_dvar!($3) %*/ + p->lex.strterm = $strterm; + $$ = NEW_EVSTR($dvar, &@$, &@state, &NULL_LOC); + nd_set_line($$, @dvar.end_pos.lineno); + /*% ripper: string_dvar!($:dvar) %*/ } - | tSTRING_DBEG[term] + | tSTRING_DBEG[state] { CMDARG_PUSH(0); COND_PUSH(0); /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */ - $<strterm>term = p->lex.strterm; + $$ = p->lex.strterm; p->lex.strterm = 0; - $<num>$ = p->lex.state; SET_LEX_STATE(EXPR_BEG); - }[state] + }[term]<strterm> { - $<num>$ = p->lex.brace_nest; + $$ = p->lex.brace_nest; p->lex.brace_nest = 0; - }[brace] + }[brace]<num> { - $<num>$ = p->heredoc_indent; + $$ = p->heredoc_indent; p->heredoc_indent = 0; - }[indent] - compstmt string_dend + }[indent]<num> + compstmt(stmts) string_dend { COND_POP(); CMDARG_POP(); - p->lex.strterm = $<strterm>term; - SET_LEX_STATE($<num>state); - p->lex.brace_nest = $<num>brace; - p->heredoc_indent = $<num>indent; + p->lex.strterm = $term; + SET_LEX_STATE($state); + p->lex.brace_nest = $brace; + p->heredoc_indent = $indent; p->heredoc_line_indent = -1; - /*%%%*/ if ($compstmt) nd_unset_fl_newline($compstmt); - $$ = new_evstr(p, $compstmt, &@$); - /*% %*/ - /*% ripper: string_embexpr!($compstmt) %*/ + $$ = new_evstr(p, $compstmt, &@$, &@state, &@string_dend); + /*% ripper: string_embexpr!($:compstmt) %*/ } ; @@ -5957,10 +6092,8 @@ string_dend : tSTRING_DEND string_dvar : nonlocal_var { - /*%%%*/ - if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper: var_ref!($1) %*/ + if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$); + /*% ripper: var_ref!($:1) %*/ } | backref ; @@ -5972,10 +6105,16 @@ symbol : ssym ssym : tSYMBEG sym { SET_LEX_STATE(EXPR_END); - /*%%%*/ - $$ = NEW_LIT(ID2SYM($2), &@$); - /*% %*/ - /*% ripper: symbol_literal!(symbol!($2)) %*/ + VALUE str = rb_id2str($2); + /* + * TODO: + * set_yylval_noname sets invalid id to yylval. + * This branch can be removed once yylval is changed to + * hold lexed string. + */ + if (!str) str = STR_NEW0(); + $$ = NEW_SYM(str, &@$); + /*% ripper: symbol_literal!(symbol!($:2)) %*/ } ; @@ -5986,21 +6125,17 @@ sym : fname dsym : tSYMBEG string_contents tSTRING_END { SET_LEX_STATE(EXPR_END); - /*%%%*/ $$ = dsym_node(p, $2, &@$); - /*% %*/ - /*% ripper: dyna_symbol!($2) %*/ + /*% ripper: dyna_symbol!($:2) %*/ } ; numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST { - /*%%%*/ $$ = $2; - RB_OBJ_WRITE(p->ast, &RNODE_LIT($$)->nd_lit, negate_lit(p, RNODE_LIT($$)->nd_lit)); - /*% %*/ - /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/ + negate_lit(p, $$, &@$); + /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/ } ; @@ -6010,13 +6145,12 @@ simple_numeric : tINTEGER | tIMAGINARY ; -nonlocal_var : tIVAR +nonlocal_var : tIVAR | tGVAR | tCVAR ; -user_variable : tIDENTIFIER - | tCONSTANT +user_variable : ident_or_const | nonlocal_var ; @@ -6031,39 +6165,25 @@ keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);} var_ref : user_variable { - /*%%%*/ - if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$); - /*% - if (id_is_var(p, get_id($1))) { - $$ = dispatch1(var_ref, $1); + if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$); + if (ifdef_ripper(id_is_var(p, $1), false)) { + /*% ripper: var_ref!($:1) %*/ } else { - $$ = dispatch1(vcall, $1); + /*% ripper: vcall!($:1) %*/ } - %*/ } | keyword_variable { - /*%%%*/ - if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$); - /*% %*/ - /*% ripper: var_ref!($1) %*/ + if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$); + /*% ripper: var_ref!($:1) %*/ } ; -var_lhs : user_variable +var_lhs : user_or_keyword_variable { - /*%%%*/ + /*% ripper: var_field!($:1) %*/ $$ = assignable(p, $1, 0, &@$); - /*% %*/ - /*% ripper: assignable(p, var_field(p, $1)) %*/ - } - | keyword_variable - { - /*%%%*/ - $$ = assignable(p, $1, 0, &@$); - /*% %*/ - /*% ripper: assignable(p, var_field(p, $1)) %*/ } ; @@ -6079,31 +6199,30 @@ superclass : '<' expr_value term { $$ = $3; + /*% ripper: $:3 %*/ } - | /* none */ - { - /*%%%*/ - $$ = 0; - /*% %*/ - /*% ripper: Qnil %*/ - } + | none ; f_opt_paren_args: f_paren_args - | none + | f_empty_arg { p->ctxt.in_argdef = 0; - $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0); - $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0); + } + ; + +f_empty_arg : /* none */ + { + $$ = new_empty_args_tail(p, &@$); + $$ = new_args(p, 0, 0, 0, 0, $$, &@$); + /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/ } ; f_paren_args : '(' f_args rparen { - /*%%%*/ $$ = $2; - /*% %*/ - /*% ripper: paren!($2) %*/ + /*% ripper: paren!($:2) %*/ SET_LEX_STATE(EXPR_BEG); p->command_start = TRUE; p->ctxt.in_argdef = 0; @@ -6112,129 +6231,133 @@ f_paren_args : '(' f_args rparen f_arglist : f_paren_args | { - $<ctxt>$ = p->ctxt; + $$ = p->ctxt; p->ctxt.in_kwarg = 1; p->ctxt.in_argdef = 1; SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */ - } + }<ctxt> f_args term { - p->ctxt.in_kwarg = $<ctxt>1.in_kwarg; + p->ctxt.in_kwarg = $1.in_kwarg; p->ctxt.in_argdef = 0; $$ = $2; SET_LEX_STATE(EXPR_BEG); p->command_start = TRUE; + /*% ripper: $:2 %*/ } ; -args_tail : f_kwarg ',' f_kwrest opt_f_block_arg - { - $$ = new_args_tail(p, $1, $3, $4, &@3); - } - | f_kwarg opt_f_block_arg - { - $$ = new_args_tail(p, $1, Qnone, $2, &@1); - } - | f_any_kwrest opt_f_block_arg - { - $$ = new_args_tail(p, Qnone, $1, $2, &@1); - } - | f_block_arg - { - $$ = new_args_tail(p, Qnone, Qnone, $1, &@1); - } +args_tail : args_tail_basic(arg_value, opt_comma) | args_forward { add_forwarding_args(p); - $$ = new_args_tail(p, Qnone, $1, arg_FWD_BLOCK, &@1); - /*%%%*/ + $$ = new_args_tail(p, 0, $args_forward, arg_FWD_BLOCK, &@args_forward); $$->nd_ainfo.forwarding = 1; - /*% %*/ + /*% ripper: [Qnil, $:args_forward, Qnil] %*/ } ; -opt_args_tail : ',' args_tail - { - $$ = $2; - } - | /* none */ +largs_tail : args_tail_basic(arg_value, none) + | args_forward { - $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0); + yyerror1(&@args_forward, "unexpected ... in lambda argument"); + $$ = new_args_tail(p, 0, 0, 0, &@args_forward); + $$->nd_ainfo.forwarding = 1; + /*% ripper: [Qnil, $:args_forward, Qnil] %*/ } ; -f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail - { - $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$); - } - | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail +%rule args-list(value, tail) <node_args> + : f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail { - $$ = new_args(p, $1, $3, $5, $7, $8, &@$); + $$ = new_args(p, $pre, $opt, $rest, 0, $tail, &@$); + /*% ripper: params!($:pre, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/ } - | f_arg ',' f_optarg opt_args_tail + | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail { - $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$); + $$ = new_args(p, $pre, $opt, $rest, $post, $tail, &@$); + /*% ripper: params!($:pre, $:opt, $:rest, $:post, *$:tail[0..2]) %*/ } - | f_arg ',' f_optarg ',' f_arg opt_args_tail + | f_arg[pre] ',' f_opt_arg(value)[opt] tail { - $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$); + $$ = new_args(p, $pre, $opt, 0, 0, $tail, &@$); + /*% ripper: params!($:pre, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/ } - | f_arg ',' f_rest_arg opt_args_tail + | f_arg[pre] ',' f_opt_arg(value)[opt] ',' f_arg[post] tail { - $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$); + $$ = new_args(p, $pre, $opt, 0, $post, $tail, &@$); + /*% ripper: params!($:pre, $:opt, Qnil, $:post, *$:tail[0..2]) %*/ } - | f_arg ',' f_rest_arg ',' f_arg opt_args_tail + | f_arg[pre] ',' f_rest_arg[rest] tail { - $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$); + $$ = new_args(p, $pre, 0, $rest, 0, $tail, &@$); + /*% ripper: params!($:pre, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/ } - | f_arg opt_args_tail + | f_arg[pre] ',' f_rest_arg[rest] ',' f_arg[post] tail { - $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$); + $$ = new_args(p, $pre, 0, $rest, $post, $tail, &@$); + /*% ripper: params!($:pre, Qnil, $:rest, $:post, *$:tail[0..2]) %*/ } - | f_optarg ',' f_rest_arg opt_args_tail + | f_opt_arg(value)[opt] ',' f_rest_arg[rest] tail { - $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$); + $$ = new_args(p, 0, $opt, $rest, 0, $tail, &@$); + /*% ripper: params!(Qnil, $:opt, $:rest, Qnil, *$:tail[0..2]) %*/ } - | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail + | f_opt_arg(value)[opt] ',' f_rest_arg[rest] ',' f_arg[post] tail { - $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$); + $$ = new_args(p, 0, $opt, $rest, $post, $tail, &@$); + /*% ripper: params!(Qnil, $:opt, $:rest, $:post, *$:tail[0..2]) %*/ } - | f_optarg opt_args_tail + | f_opt_arg(value)[opt] tail { - $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$); + $$ = new_args(p, 0, $opt, 0, 0, $tail, &@$); + /*% ripper: params!(Qnil, $:opt, Qnil, Qnil, *$:tail[0..2]) %*/ } - | f_optarg ',' f_arg opt_args_tail + | f_opt_arg(value)[opt] ',' f_arg[post] tail { - $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$); + $$ = new_args(p, 0, $opt, 0, $post, $tail, &@$); + /*% ripper: params!(Qnil, $:opt, Qnil, $:post, *$:tail[0..2]) %*/ } - | f_rest_arg opt_args_tail + | f_rest_arg[rest] tail { - $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$); + $$ = new_args(p, 0, 0, $rest, 0, $tail, &@$); + /*% ripper: params!(Qnil, Qnil, $:rest, Qnil, *$:tail[0..2]) %*/ } - | f_rest_arg ',' f_arg opt_args_tail + | f_rest_arg[rest] ',' f_arg[post] tail { - $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$); + $$ = new_args(p, 0, 0, $rest, $post, $tail, &@$); + /*% ripper: params!(Qnil, Qnil, $:rest, $:post, *$:tail[0..2]) %*/ } - | args_tail + ; + +%rule tail-only-args(tail) <node_args> + : tail { - $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$); + $$ = new_args(p, 0, 0, 0, 0, $tail, &@$); + /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/ } - | /* none */ + ; + +%rule f_args-list(tail, trailing) <node_args> + : args-list(arg_value, opt_args_tail(tail, trailing)) + | f_arg[pre] opt_args_tail(tail, trailing)[tail] { - $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0); - $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0); + $$ = new_args(p, $pre, 0, 0, 0, $tail, &@$); + /*% ripper: params!($:pre, Qnil, Qnil, Qnil, *$:tail[0..2]) %*/ } + | tail-only-args(tail) + | f_empty_arg + ; + +f_args : f_args-list(args_tail, opt_comma) + ; + +f_largs : f_args-list(largs_tail, none) ; args_forward : tBDOT3 { - /*%%%*/ -#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS - $$ = 0; -#else $$ = idFWD_KWREST; -#endif - /*% %*/ /*% ripper: args_forward! %*/ } ; @@ -6244,68 +6367,64 @@ f_bad_arg : tCONSTANT static const char mesg[] = "formal argument cannot be a constant"; /*%%%*/ yyerror1(&@1, mesg); - $$ = 0; /*% %*/ - /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/ + $$ = 0; + /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/ } | tIVAR { static const char mesg[] = "formal argument cannot be an instance variable"; /*%%%*/ yyerror1(&@1, mesg); - $$ = 0; /*% %*/ - /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/ + $$ = 0; + /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/ } | tGVAR { static const char mesg[] = "formal argument cannot be a global variable"; /*%%%*/ yyerror1(&@1, mesg); - $$ = 0; /*% %*/ - /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/ + $$ = 0; + /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/ } | tCVAR { static const char mesg[] = "formal argument cannot be a class variable"; /*%%%*/ yyerror1(&@1, mesg); - $$ = 0; /*% %*/ - /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/ + $$ = 0; + /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/ } ; f_norm_arg : f_bad_arg | tIDENTIFIER { - formal_argument(p, $1); + VALUE e = formal_argument_error(p, $$ = $1); + if (e) { + /*% ripper[error]: param_error!(?e, $:1) %*/ + } p->max_numparam = ORDINAL_PARAM; - $$ = $1; } ; f_arg_asgn : f_norm_arg { - ID id = get_id($1); - arg_var(p, id); - p->cur_arg = id; + arg_var(p, $1); $$ = $1; } ; f_arg_item : f_arg_asgn { - p->cur_arg = 0; - /*%%%*/ $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC); - /*% %*/ - /*% ripper: get_value($1) %*/ + /*% ripper: $:1 %*/ } | tLPAREN f_margs rparen { - /*%%%*/ ID tid = internal_id(p); YYLTYPE loc; loc.beg_pos = @2.beg_pos; @@ -6319,104 +6438,40 @@ f_arg_item : f_arg_asgn } $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC); $$->nd_next = (NODE *)$2; - /*% %*/ - /*% ripper: mlhs_paren!($2) %*/ + /*% ripper: mlhs_paren!($:2) %*/ } ; f_arg : f_arg_item - /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/ + /*% ripper[brace]: rb_ary_new3(1, $:1) %*/ | f_arg ',' f_arg_item { - /*%%%*/ $$ = $1; $$->nd_plen++; $$->nd_next = block_append(p, $$->nd_next, $3->nd_next); rb_discard_node(p, (NODE *)$3); - /*% %*/ - /*% ripper: rb_ary_push($1, get_value($3)) %*/ + /*% ripper: rb_ary_push($:1, $:3) %*/ } ; f_label : tLABEL { - arg_var(p, formal_argument(p, $1)); - p->cur_arg = get_id($1); + VALUE e = formal_argument_error(p, $$ = $1); + if (e) { + $$ = 0; + /*% ripper[error]: param_error!(?e, $:1) %*/ + } + /* + * Workaround for Prism::ParseTest#test_filepath for + * "unparser/corpus/literal/def.txt" + * + * See the discussion on https://github.com/ruby/ruby/pull/9923 + */ + arg_var(p, ifdef_ripper(0, $1)); + /*% ripper: $:1 %*/ p->max_numparam = ORDINAL_PARAM; p->ctxt.in_argdef = 0; - $$ = $1; - } - ; - -f_kw : f_label arg_value - { - p->cur_arg = 0; - p->ctxt.in_argdef = 1; - /*%%%*/ - $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$); - /*% %*/ - /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/ - } - | f_label - { - p->cur_arg = 0; - p->ctxt.in_argdef = 1; - /*%%%*/ - $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$); - /*% %*/ - /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/ - } - ; - -f_block_kw : f_label primary_value - { - p->ctxt.in_argdef = 1; - /*%%%*/ - $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$); - /*% %*/ - /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/ - } - | f_label - { - p->ctxt.in_argdef = 1; - /*%%%*/ - $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$); - /*% %*/ - /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/ - } - ; - -f_block_kwarg : f_block_kw - { - /*%%%*/ - $$ = $1; - /*% %*/ - /*% ripper: rb_ary_new3(1, get_value($1)) %*/ - } - | f_block_kwarg ',' f_block_kw - { - /*%%%*/ - $$ = kwd_append($1, $3); - /*% %*/ - /*% ripper: rb_ary_push($1, get_value($3)) %*/ - } - ; - - -f_kwarg : f_kw - { - /*%%%*/ - $$ = $1; - /*% %*/ - /*% ripper: rb_ary_new3(1, get_value($1)) %*/ - } - | f_kwarg ',' f_kw - { - /*%%%*/ - $$ = kwd_append($1, $3); - /*% %*/ - /*% ripper: rb_ary_push($1, get_value($3)) %*/ } ; @@ -6426,102 +6481,38 @@ kwrest_mark : tPOW f_no_kwarg : p_kwnorest { - /*%%%*/ - /*% %*/ /*% ripper: nokw_param!(Qnil) %*/ } ; f_kwrest : kwrest_mark tIDENTIFIER { - arg_var(p, shadowing_lvar(p, get_id($2))); - /*%%%*/ + arg_var(p, shadowing_lvar(p, $2)); $$ = $2; - /*% %*/ - /*% ripper: kwrest_param!($2) %*/ + /*% ripper: kwrest_param!($:2) %*/ } | kwrest_mark { arg_var(p, idFWD_KWREST); - /*%%%*/ $$ = idFWD_KWREST; - /*% %*/ /*% ripper: kwrest_param!(Qnil) %*/ } ; -f_opt : f_arg_asgn f_eq arg_value - { - p->cur_arg = 0; - p->ctxt.in_argdef = 1; - /*%%%*/ - $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$); - /*% %*/ - /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/ - } - ; - -f_block_opt : f_arg_asgn f_eq primary_value - { - p->cur_arg = 0; - p->ctxt.in_argdef = 1; - /*%%%*/ - $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$); - /*% %*/ - /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/ - } - ; - -f_block_optarg : f_block_opt - { - /*%%%*/ - $$ = $1; - /*% %*/ - /*% ripper: rb_ary_new3(1, get_value($1)) %*/ - } - | f_block_optarg ',' f_block_opt - { - /*%%%*/ - $$ = opt_arg_append($1, $3); - /*% %*/ - /*% ripper: rb_ary_push($1, get_value($3)) %*/ - } - ; - -f_optarg : f_opt - { - /*%%%*/ - $$ = $1; - /*% %*/ - /*% ripper: rb_ary_new3(1, get_value($1)) %*/ - } - | f_optarg ',' f_opt - { - /*%%%*/ - $$ = opt_arg_append($1, $3); - /*% %*/ - /*% ripper: rb_ary_push($1, get_value($3)) %*/ - } - ; - restarg_mark : '*' | tSTAR ; f_rest_arg : restarg_mark tIDENTIFIER { - arg_var(p, shadowing_lvar(p, get_id($2))); - /*%%%*/ + arg_var(p, shadowing_lvar(p, $2)); $$ = $2; - /*% %*/ - /*% ripper: rest_param!($2) %*/ + /*% ripper: rest_param!($:2) %*/ } | restarg_mark { arg_var(p, idFWD_REST); - /*%%%*/ $$ = idFWD_REST; - /*% %*/ /*% ripper: rest_param!(Qnil) %*/ } ; @@ -6532,168 +6523,150 @@ blkarg_mark : '&' f_block_arg : blkarg_mark tIDENTIFIER { - arg_var(p, shadowing_lvar(p, get_id($2))); - /*%%%*/ + arg_var(p, shadowing_lvar(p, $2)); $$ = $2; - /*% %*/ - /*% ripper: blockarg!($2) %*/ + /*% ripper: blockarg!($:2) %*/ + } + | blkarg_mark keyword_nil + { + $$ = idNil; + /*% ripper: blockarg!(ID2VAL(idNil)) %*/ } | blkarg_mark { arg_var(p, idFWD_BLOCK); - /*%%%*/ $$ = idFWD_BLOCK; - /*% %*/ /*% ripper: blockarg!(Qnil) %*/ } ; -opt_f_block_arg : ',' f_block_arg - { - $$ = $2; - } - | none +opt_comma : ','? { - $$ = Qnull; + $$ = 0; + /*% ripper: Qnil %*/ } ; -singleton : var_ref - { - value_expr($1); - $$ = $1; - } - | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen + +singleton : value_expr(singleton_expr) { - /*%%%*/ - NODE *expr = last_expr_node($3); + NODE *expr = last_expr_node($1); switch (nd_type(expr)) { case NODE_STR: case NODE_DSTR: case NODE_XSTR: case NODE_DXSTR: + case NODE_REGX: case NODE_DREGX: - case NODE_LIT: + case NODE_SYM: + case NODE_LINE: + case NODE_FILE: + case NODE_ENCODING: + case NODE_INTEGER: + case NODE_FLOAT: + case NODE_RATIONAL: + case NODE_IMAGINARY: case NODE_DSYM: case NODE_LIST: case NODE_ZLIST: yyerror1(&expr->nd_loc, "can't define singleton method for literals"); break; default: - value_expr($3); break; } + $$ = $1; + } + ; + +singleton_expr : var_ref + | '(' + { + SET_LEX_STATE(EXPR_BEG); + p->ctxt.in_argdef = 0; + } + expr rparen + { + p->ctxt.in_argdef = 1; $$ = $3; - /*% %*/ - /*% ripper: paren!($3) %*/ + /*% ripper: paren!($:3) %*/ } ; assoc_list : none | assocs trailer { - /*%%%*/ $$ = $1; - /*% %*/ - /*% ripper: assoclist_from_args!($1) %*/ + /*% ripper: assoclist_from_args!($:1) %*/ } ; assocs : assoc - /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/ + /*% ripper[brace]: rb_ary_new3(1, $:1) %*/ | assocs ',' assoc { - /*%%%*/ NODE *assocs = $1; NODE *tail = $3; if (!assocs) { assocs = tail; } else if (tail) { - if (RNODE_LIST(assocs)->nd_head && - !RNODE_LIST(tail)->nd_head && nd_type_p(RNODE_LIST(tail)->nd_next, NODE_LIST) && - nd_type_p(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head, NODE_HASH)) { - /* DSTAR */ - tail = RNODE_HASH(RNODE_LIST(RNODE_LIST(tail)->nd_next)->nd_head)->nd_head; + if (RNODE_LIST(assocs)->nd_head) { + NODE *n = RNODE_LIST(tail)->nd_next; + if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) && + nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) { + /* DSTAR */ + tail = RNODE_HASH(n)->nd_head; + } + } + if (tail) { + assocs = list_concat(assocs, tail); } - assocs = list_concat(assocs, tail); } $$ = assocs; - /*% %*/ - /*% ripper: rb_ary_push($1, get_value($3)) %*/ + /*% ripper: rb_ary_push($:1, $:3) %*/ } ; assoc : arg_value tASSOC arg_value { - /*%%%*/ - if (nd_type_p($1, NODE_STR)) { - nd_set_type($1, NODE_LIT); - RB_OBJ_WRITE(p->ast, &RNODE_LIT($1)->nd_lit, rb_fstring(RNODE_LIT($1)->nd_lit)); - } $$ = list_append(p, NEW_LIST($1, &@$), $3); - /*% %*/ - /*% ripper: assoc_new!($1, $3) %*/ + /*% ripper: assoc_new!($:1, $:3) %*/ } | tLABEL arg_value { - /*%%%*/ - $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2); - /*% %*/ - /*% ripper: assoc_new!($1, $2) %*/ + $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2); + /*% ripper: assoc_new!($:1, $:2) %*/ } | tLABEL { - /*%%%*/ NODE *val = gettable(p, $1, &@$); - if (!val) val = NEW_BEGIN(0, &@$); - $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val); - /*% %*/ - /*% ripper: assoc_new!($1, Qnil) %*/ + if (!val) val = NEW_ERROR(&@$); + $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val); + /*% ripper: assoc_new!($:1, Qnil) %*/ } | tSTRING_BEG string_contents tLABEL_END arg_value { - /*%%%*/ YYLTYPE loc = code_loc_gen(&@1, &@3); $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4); - /*% %*/ - /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/ + /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/ } | tDSTAR arg_value { - /*%%%*/ - if (nd_type_p($2, NODE_HASH) && - !(RNODE_HASH($2)->nd_head && RNODE_LIST(RNODE_HASH($2)->nd_head)->as.nd_alen)) { - static VALUE empty_hash; - if (!empty_hash) { - empty_hash = rb_obj_freeze(rb_hash_new()); - rb_gc_register_mark_object(empty_hash); - } - $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$)); - } - else - $$ = list_append(p, NEW_LIST(0, &@$), $2); - /*% %*/ - /*% ripper: assoc_splat!($2) %*/ + $$ = list_append(p, NEW_LIST(0, &@$), $2); + /*% ripper: assoc_splat!($:2) %*/ } | tDSTAR { - if (!local_id(p, idFWD_KWREST) || - local_id(p, idFWD_ALL)) { - compile_error(p, "no anonymous keyword rest parameter"); - } - /*%%%*/ + forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest"); $$ = list_append(p, NEW_LIST(0, &@$), NEW_LVAR(idFWD_KWREST, &@$)); - /*% %*/ /*% ripper: assoc_splat!(Qnil) %*/ } ; -operation : tIDENTIFIER - | tCONSTANT - | tFID - ; +%rule %inline operation : ident_or_const + | tFID + ; operation2 : operation | op @@ -6716,28 +6689,27 @@ call_op2 : call_op | tCOLON2 ; -opt_terms : /* none */ - | terms - ; - -opt_nl : /* none */ - | '\n' - ; - -rparen : opt_nl ')' +rparen : '\n'? ')' ; -rbracket : opt_nl ']' +rbracket : '\n'? ']' ; -rbrace : opt_nl '}' +rbrace : '\n'? '}' ; -trailer : opt_nl +trailer : '\n'? | ',' ; -term : ';' {yyerrok;token_flush(p);} +term : ';' + { + yyerrok; + token_flush(p); + if (p->ctxt.in_defined) { + p->ctxt.has_trailing_semicolon = 1; + } + } | '\n' { @$.end_pos = @$.beg_pos; @@ -6751,7 +6723,8 @@ terms : term none : /* none */ { - $$ = Qnull; + $$ = 0; + /*% ripper: Qnil %*/ } ; %% @@ -6766,44 +6739,32 @@ static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc); static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*); static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*); -#ifndef RIPPER +#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0)) + # define set_yylval_node(x) { \ YYLTYPE _cur_loc; \ rb_parser_set_location(p, &_cur_loc); \ yylval.node = (x); \ + set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \ } # define set_yylval_str(x) \ do { \ set_yylval_node(NEW_STR(x, &_cur_loc)); \ - RB_OBJ_WRITTEN(p->ast, Qnil, x); \ -} while(0) -# define set_yylval_literal(x) \ -do { \ - set_yylval_node(NEW_LIT(x, &_cur_loc)); \ - RB_OBJ_WRITTEN(p->ast, Qnil, x); \ + set_parser_s_value(rb_str_new_mutable_parser_string(x)); \ } while(0) -# define set_yylval_num(x) (yylval.num = (x)) -# define set_yylval_id(x) (yylval.id = (x)) -# define set_yylval_name(x) (yylval.id = (x)) +# define set_yylval_num(x) { \ + yylval.num = (x); \ + set_parser_s_value(x); \ +} +# define set_yylval_id(x) (yylval.id = (x)) +# define set_yylval_name(x) { \ + (yylval.id = (x)); \ + set_parser_s_value(ID2SYM(x)); \ +} # define yylval_id() (yylval.id) -#else -static inline VALUE -ripper_yylval_id(struct parser_params *p, ID x) -{ - return ripper_new_yylval(p, x, ID2SYM(x), 0); -} -# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x))) -# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0)) -# define set_yylval_id(x) (void)(x) -# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x)) -# define set_yylval_literal(x) add_mark_object(p, (x)) -# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok))) -# define yylval_id() yylval.id -# define _cur_loc NULL_LOC /* dummy */ -#endif #define set_yylval_noname() set_yylval_id(keyword_nil) -#define has_delayed_token(p) (!NIL_P(p->delayed.token)) +#define has_delayed_token(p) (p->delayed.token != NULL) #ifndef RIPPER #define literal_flush(p, ptr) ((p)->lex.ptok = (ptr)) @@ -6821,35 +6782,99 @@ parser_has_token(struct parser_params *p) return pcur > ptok; } -static VALUE -code_loc_to_ary(struct parser_params *p, const rb_code_location_t *loc) +static const char * +escaped_char(int c) { - VALUE ary = rb_ary_new_from_args(4, - INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column), - INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column)); - rb_obj_freeze(ary); - - return ary; + switch (c) { + case '"': return "\\\""; + case '\\': return "\\\\"; + case '\0': return "\\0"; + case '\n': return "\\n"; + case '\r': return "\\r"; + case '\t': return "\\t"; + case '\f': return "\\f"; + case '\013': return "\\v"; + case '\010': return "\\b"; + case '\007': return "\\a"; + case '\033': return "\\e"; + case '\x7f': return "\\c?"; + } + return NULL; } -static void -parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line) +static rb_parser_string_t * +rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str) { - VALUE ary; - int token_id; + rb_encoding *enc = p->enc; + const char *ptr = str->ptr; + const char *pend = ptr + str->len; + const char *prev = ptr; + char charbuf[5] = {'\\', 'x', 0, 0, 0}; + rb_parser_string_t * result = rb_parser_string_new(p, 0, 0); + + while (ptr < pend) { + unsigned int c; + const char *cc; + int n = rb_enc_precise_mbclen(ptr, pend, enc); + if (!MBCLEN_CHARFOUND_P(n)) { + if (ptr > prev) parser_str_cat(result, prev, ptr - prev); + n = rb_enc_mbminlen(enc); + if (pend < ptr + n) + n = (int)(pend - ptr); + while (n--) { + c = *ptr & 0xf0 >> 4; + charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10; + c = *ptr & 0x0f; + charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10; + parser_str_cat(result, charbuf, 4); + prev = ++ptr; + } + continue; + } + n = MBCLEN_CHARFOUND_LEN(n); + c = rb_enc_mbc_to_codepoint(ptr, pend, enc); + ptr += n; + cc = escaped_char(c); + if (cc) { + if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev); + parser_str_cat_cstr(result, cc); + prev = ptr; + } + else if (rb_enc_isascii(c, enc) && ISPRINT(c)) { + } + else { + if (ptr - n > prev) { + parser_str_cat(result, prev, ptr - n - prev); + prev = ptr - n; + } + parser_str_cat(result, prev, ptr - prev); + prev = ptr; + } + } + if (ptr > prev) parser_str_cat(result, prev, ptr - prev); - ary = rb_ary_new2(4); - token_id = p->token_id; - rb_ary_push(ary, INT2FIX(token_id)); - rb_ary_push(ary, ID2SYM(parser_token2id(p, t))); - rb_ary_push(ary, str); - rb_ary_push(ary, code_loc_to_ary(p, p->yylloc)); - rb_obj_freeze(ary); - rb_ary_push(p->tokens, ary); + return result; +} + +static void +parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line) +{ + rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t)); + token->id = p->token_id; + token->type_name = parser_token2char(p, t); + token->str = str; + token->loc.beg_pos = p->yylloc->beg_pos; + token->loc.end_pos = p->yylloc->end_pos; + rb_parser_ary_push_ast_token(p, p->tokens, token); p->token_id++; if (p->debug) { - rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary); + rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str); + rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n", + line, token->id, token->type_name, str_escaped->ptr, + token->loc.beg_pos.lineno, token->loc.beg_pos.column, + token->loc.end_pos.lineno, token->loc.end_pos.column); + rb_parser_string_free(p, str_escaped); } } @@ -6863,7 +6888,7 @@ parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line RUBY_SET_YYLLOC(*p->yylloc); if (p->keep_tokens) { - VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok); + rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc); parser_append_tokens(p, str, t, line); } @@ -6881,16 +6906,18 @@ parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int l RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc); if (p->keep_tokens) { + /* p->delayed.token is freed by rb_parser_tokens_free */ parser_append_tokens(p, p->delayed.token, t, line); } + else { + rb_parser_string_free(p, p->delayed.token); + } - p->delayed.token = Qnil; + p->delayed.token = NULL; } #else #define literal_flush(p, ptr) ((void)(ptr)) -#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &RNODE_RIPPER(yylval.node)->nd_rval : &yylval.val)) - static int ripper_has_scan_event(struct parser_params *p) { @@ -6912,7 +6939,8 @@ static void ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t) { if (!ripper_has_scan_event(p)) return; - add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t)); + + set_parser_s_value(ripper_scan_event_val(p, t)); } #define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t) @@ -6922,12 +6950,16 @@ ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t) /* save and adjust the location to delayed token for callbacks */ int saved_line = p->ruby_sourceline; const char *saved_tokp = p->lex.ptok; + VALUE s_value, str; if (!has_delayed_token(p)) return; p->ruby_sourceline = p->delayed.beg_line; p->lex.ptok = p->lex.pbeg + p->delayed.beg_col; - add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token)); - p->delayed.token = Qnil; + str = rb_str_new_mutable_parser_string(p->delayed.token); + rb_parser_string_free(p, p->delayed.token); + s_value = ripper_dispatch1(p, ripper_token2eventid(t), str); + set_parser_s_value(s_value); + p->delayed.token = NULL; p->ruby_sourceline = saved_line; p->lex.ptok = saved_tokp; } @@ -6940,6 +6972,19 @@ is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED( return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr); } +static inline bool +peek_word_at(struct parser_params *p, const char *str, size_t len, int at) +{ + const char *ptr = p->lex.pcur + at; + if (lex_eol_ptr_n_p(p, ptr, len-1)) return false; + if (memcmp(ptr, str, len)) return false; + if (lex_eol_ptr_n_p(p, ptr, len)) return true; + switch (ptr[len]) { + case '!': case '?': return false; + } + return !is_identchar(p, ptr+len, p->lex.pend, p->enc); +} + static inline int parser_is_identchar(struct parser_params *p) { @@ -6991,11 +7036,12 @@ token_info_pop(struct parser_params *p, const char *token, const rb_code_locatio token_info *ptinfo_beg = p->token_info; if (!ptinfo_beg) return; - p->token_info = ptinfo_beg->next; /* indentation check of matched keywords (begin..end, if..end, etc.) */ token_info_warn(p, token, ptinfo_beg, 1, loc); - ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg)); + + p->token_info = ptinfo_beg->next; + ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg)); } static void @@ -7015,7 +7061,7 @@ token_info_drop(struct parser_params *p, const char *token, rb_code_position_t b ptinfo_beg->token); } - ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg)); + ruby_xfree_sized(ptinfo_beg, sizeof(*ptinfo_beg)); } static void @@ -7046,12 +7092,10 @@ parser_precise_mbclen(struct parser_params *p, const char *ptr) } #ifndef RIPPER -static void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str); - static inline void parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc) { - VALUE str; + rb_parser_string_t *str; int lineno = p->ruby_sourceline; if (!yylloc) { return; @@ -7091,8 +7135,8 @@ parser_yyerror0(struct parser_params *p, const char *msg) return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg); } -static void -ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str) +void +ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str) { VALUE mesg; const int max_line_margin = 30; @@ -7100,13 +7144,13 @@ ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yyllo const char *pre = "", *post = "", *pend; const char *code = "", *caret = ""; const char *lim; - const char *const pbeg = RSTRING_PTR(str); + const char *const pbeg = PARSER_STRING_PTR(str); char *buf; long len; int i; if (!yylloc) return; - pend = RSTRING_END(str); + pend = rb_parser_string_end(str); if (pend > pbeg && pend[-1] == '\n') { if (--pend > pbeg && pend[-1] == '\r') --pend; } @@ -7127,11 +7171,11 @@ ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yyllo len = ptr_end - ptr; if (len > 4) { if (ptr > pbeg) { - ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str)); + ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str)); if (ptr > pbeg) pre = "..."; } if (ptr_end < pend) { - ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str)); + ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str)); if (ptr_end < pend) post = "..."; } } @@ -7146,11 +7190,11 @@ ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yyllo } if (RTEST(errbuf)) { mesg = rb_attr_get(errbuf, idMesg); - if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n') + if (char_at_end(p, mesg, '\n') != '\n') rb_str_cat_cstr(mesg, "\n"); } else { - mesg = rb_enc_str_new(0, 0, rb_enc_get(str)); + mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str)); } if (!errbuf && rb_stderr_tty_p()) { #define CSI_BEGIN "\033[" @@ -7196,6 +7240,7 @@ ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yyllo if (!errbuf) rb_write_error_str(mesg); } #else + static int parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg) { @@ -7229,7 +7274,6 @@ parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc) } #endif /* !RIPPER */ -#ifndef RIPPER static int vtable_size(const struct vtable *tbl) { @@ -7240,7 +7284,6 @@ vtable_size(const struct vtable *tbl) return 0; } } -#endif static struct vtable * vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev) @@ -7270,9 +7313,9 @@ vtable_free_gen(struct parser_params *p, int line, const char *name, #endif if (!DVARS_TERMINAL_P(tbl)) { if (tbl->tbl) { - ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID)); + ruby_xfree_sized(tbl->tbl, tbl->capa * sizeof(ID)); } - ruby_sized_xfree(tbl, sizeof(*tbl)); + ruby_xfree_sized(tbl, sizeof(*tbl)); } } #define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl) @@ -7299,7 +7342,6 @@ vtable_add_gen(struct parser_params *p, int line, const char *name, } #define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id) -#ifndef RIPPER static void vtable_pop_gen(struct parser_params *p, int line, const char *name, struct vtable *tbl, int n) @@ -7315,7 +7357,6 @@ vtable_pop_gen(struct parser_params *p, int line, const char *name, tbl->pos -= n; } #define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n) -#endif static int vtable_included(const struct vtable * tbl, ID id) @@ -7334,15 +7375,15 @@ vtable_included(const struct vtable * tbl, ID id) static void parser_prepare(struct parser_params *p); -#ifndef RIPPER -static NODE *parser_append_options(struct parser_params *p, NODE *node); - static int e_option_supplied(struct parser_params *p) { return strcmp(p->ruby_sourcefile, "-e") == 0; } +#ifndef RIPPER +static NODE *parser_append_options(struct parser_params *p, NODE *node); + static VALUE yycompile0(VALUE arg) { @@ -7351,22 +7392,12 @@ yycompile0(VALUE arg) struct parser_params *p = (struct parser_params *)arg; int cov = FALSE; - if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) { - if (p->debug_lines && p->ruby_sourceline > 0) { - VALUE str = rb_default_rs; - n = p->ruby_sourceline; - do { - rb_ary_push(p->debug_lines, str); - } while (--n); - } - - if (!e_option_supplied(p)) { - cov = TRUE; - } + if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) { + cov = TRUE; } if (p->debug_lines) { - RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines); + p->ast->body.script_lines = p->debug_lines; } parser_prepare(p); @@ -7377,8 +7408,10 @@ yycompile0(VALUE arg) RUBY_DTRACE_PARSE_HOOK(BEGIN); n = yyparse(p); RUBY_DTRACE_PARSE_HOOK(END); + p->debug_lines = 0; + xfree(p->lex.strterm); p->lex.strterm = 0; p->lex.pcur = p->lex.pbeg = p->lex.pend = 0; if (n || p->error_p) { @@ -7396,7 +7429,7 @@ yycompile0(VALUE arg) tree = NEW_NIL(&NULL_LOC); } else { - VALUE tokens = p->tokens; + rb_parser_ary_t *tokens = p->tokens; NODE *prelude; NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body); prelude = block_append(p, p->eval_tree_begin, body); @@ -7404,12 +7437,12 @@ yycompile0(VALUE arg) p->ast->body.frozen_string_literal = p->frozen_string_literal; p->ast->body.coverage_enabled = cov; if (p->keep_tokens) { - rb_obj_freeze(tokens); - rb_ast_set_tokens(p->ast, tokens); + p->ast->node_buffer->tokens = tokens; + p->tokens = NULL; } } p->ast->body.root = tree; - if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count); + p->ast->body.line_count = p->line_count; return TRUE; } @@ -7422,7 +7455,7 @@ yycompile(struct parser_params *p, VALUE fname, int line) p->ruby_sourcefile = "(none)"; } else { - p->ruby_sourcefile_string = rb_fstring(fname); + p->ruby_sourcefile_string = rb_str_to_interned_str(fname); p->ruby_sourcefile = StringValueCStr(fname); } p->ruby_sourceline = line - 1; @@ -7442,102 +7475,35 @@ yycompile(struct parser_params *p, VALUE fname, int line) #endif /* !RIPPER */ static rb_encoding * -must_be_ascii_compatible(struct parser_params *p, VALUE s) +must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s) { - rb_encoding *enc = rb_enc_get(s); + rb_encoding *enc = rb_parser_str_get_encoding(s); if (!rb_enc_asciicompat(enc)) { rb_raise(rb_eArgError, "invalid source encoding"); } return enc; } -static VALUE -lex_get_str(struct parser_params *p, VALUE s) -{ - char *beg, *end, *start; - long len; - - beg = RSTRING_PTR(s); - len = RSTRING_LEN(s); - start = beg; - if (p->lex.gets_.ptr) { - if (len == p->lex.gets_.ptr) return Qnil; - beg += p->lex.gets_.ptr; - len -= p->lex.gets_.ptr; - } - end = memchr(beg, '\n', len); - if (end) len = ++end - beg; - p->lex.gets_.ptr += len; - return rb_str_subseq(s, beg - start, len); -} - -static VALUE +static rb_parser_string_t * lex_getline(struct parser_params *p) { - VALUE line = (*p->lex.gets)(p, p->lex.input); - if (NIL_P(line)) return line; - must_be_ascii_compatible(p, line); - if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen + rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count); + if (!line) return 0; p->line_count++; + string_buffer_append(p, line); + must_be_ascii_compatible(p, line); return line; } #ifndef RIPPER -static rb_ast_t* -parser_compile_string(rb_parser_t *p, VALUE fname, VALUE s, int line) -{ - p->lex.gets = lex_get_str; - p->lex.gets_.ptr = 0; - p->lex.input = rb_str_new_frozen(s); - p->lex.pbeg = p->lex.pcur = p->lex.pend = 0; - - return yycompile(p, fname, line); -} - -rb_ast_t* -rb_ruby_parser_compile_string_path(rb_parser_t *p, VALUE f, VALUE s, int line) -{ - must_be_ascii_compatible(p, s); - return parser_compile_string(p, f, s, line); -} - -rb_ast_t* -rb_ruby_parser_compile_string(rb_parser_t *p, const char *f, VALUE s, int line) -{ - return rb_ruby_parser_compile_string_path(p, rb_filesystem_str_new_cstr(f), s, line); -} - -static VALUE -lex_io_gets(struct parser_params *p, VALUE io) -{ - return rb_io_gets_internal(io); -} - rb_ast_t* -rb_ruby_parser_compile_file_path(rb_parser_t *p, VALUE fname, VALUE file, int start) -{ - p->lex.gets = lex_io_gets; - p->lex.input = file; - p->lex.pbeg = p->lex.pcur = p->lex.pend = 0; - - return yycompile(p, fname, start); -} - -static VALUE -lex_generic_gets(struct parser_params *p, VALUE input) +rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line) { - return (*p->lex.gets_.call)(input, p->line_count); -} - -rb_ast_t* -rb_ruby_parser_compile_generic(rb_parser_t *p, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start) -{ - p->lex.gets = lex_generic_gets; - p->lex.gets_.call = lex_gets; + p->lex.gets = gets; p->lex.input = input; p->lex.pbeg = p->lex.pcur = p->lex.pend = 0; - return yycompile(p, fname, start); + return yycompile(p, fname, line); } #endif /* !RIPPER */ @@ -7563,27 +7529,30 @@ enum string_type { str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND) }; -static VALUE +static rb_parser_string_t * parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0) { - VALUE str; + rb_parser_string_t *pstr; - str = rb_enc_str_new(ptr, len, enc); - if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) { - if (is_ascii_string(str)) { + pstr = rb_parser_encoding_string_new(p, ptr, len, enc); + + if (!(func & STR_FUNC_REGEXP)) { + if (rb_parser_is_ascii_string(p, pstr)) { } else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) { - rb_enc_associate(str, rb_ascii8bit_encoding()); + /* everything is valid in ASCII-8BIT */ + enc = rb_ascii8bit_encoding(); + PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID); } } - return str; + return pstr; } static int strterm_is_heredoc(rb_strterm_t *strterm) { - return strterm->flags & STRTERM_HEREDOC; + return strterm->heredoc; } static rb_strterm_t * @@ -7600,7 +7569,7 @@ static rb_strterm_t * new_heredoc(struct parser_params *p) { rb_strterm_t *strterm = ZALLOC(rb_strterm_t); - strterm->flags |= STRTERM_HEREDOC; + strterm->heredoc = true; return strterm; } @@ -7609,21 +7578,28 @@ new_heredoc(struct parser_params *p) #define peekc(p) peekc_n(p, 0) #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n]) +#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__) static void -add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line) +parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line) { -#ifndef RIPPER debug_token_line(p, "add_delayed_token", line); -#endif if (tok < end) { + if (has_delayed_token(p)) { + bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n'; + int end_line = (next_line ? 1 : 0) + p->delayed.end_line; + int end_col = (next_line ? 0 : p->delayed.end_col); + if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) { + dispatch_delayed_token(p, tSTRING_CONTENT); + } + } if (!has_delayed_token(p)) { - p->delayed.token = rb_str_buf_new(end - tok); - rb_enc_associate(p->delayed.token, p->enc); + p->delayed.token = rb_parser_string_new(p, 0, 0); + rb_parser_enc_associate(p, p->delayed.token, p->enc); p->delayed.beg_line = p->ruby_sourceline; p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg); } - rb_str_buf_cat(p->delayed.token, tok, end - tok); + parser_str_cat(p->delayed.token, tok, end - tok); p->delayed.end_line = p->ruby_sourceline; p->delayed.end_col = rb_long2int(end - p->lex.pbeg); p->lex.ptok = end; @@ -7631,19 +7607,19 @@ add_delayed_token(struct parser_params *p, const char *tok, const char *end, int } static void -set_lastline(struct parser_params *p, VALUE v) +set_lastline(struct parser_params *p, rb_parser_string_t *str) { - p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v); - p->lex.pend = p->lex.pcur + RSTRING_LEN(v); - p->lex.lastline = v; + p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str); + p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str); + p->lex.lastline = str; } static int nextline(struct parser_params *p, int set_encoding) { - VALUE v = p->lex.nextline; + rb_parser_string_t *str = p->lex.nextline; p->lex.nextline = 0; - if (!v) { + if (!str) { if (p->eofp) return -1; @@ -7651,7 +7627,7 @@ nextline(struct parser_params *p, int set_encoding) goto end_of_input; } - if (!p->lex.input || NIL_P(v = lex_getline(p))) { + if (!p->lex.input || !(str = lex_getline(p))) { end_of_input: p->eofp = 1; lex_goto_eol(p); @@ -7659,23 +7635,24 @@ nextline(struct parser_params *p, int set_encoding) } #ifndef RIPPER if (p->debug_lines) { - if (set_encoding) rb_enc_associate(v, p->enc); - rb_ary_push(p->debug_lines, v); + if (set_encoding) rb_parser_enc_associate(p, str, p->enc); + rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str); + rb_parser_ary_push_script_line(p, p->debug_lines, copy); } #endif p->cr_seen = FALSE; } - else if (NIL_P(v)) { + else if (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) { /* after here-document without terminator */ goto end_of_input; } - add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__); + add_delayed_token(p, p->lex.ptok, p->lex.pend); if (p->heredoc_end > 0) { p->ruby_sourceline = p->heredoc_end; p->heredoc_end = 0; } p->ruby_sourceline++; - set_lastline(p, v); + set_lastline(p, str); token_flush(p); return 0; } @@ -7695,7 +7672,7 @@ nextc0(struct parser_params *p, int set_encoding) { int c; - if (UNLIKELY(lex_eol_p(p) || p->eofp || RTEST(p->lex.nextline))) { + if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINATOR)) { if (nextline(p, set_encoding)) return -1; } c = (unsigned char)*p->lex.pcur++; @@ -7782,8 +7759,9 @@ tok_hex(struct parser_params *p, size_t *numlen) c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen); if (!*numlen) { + flush_string_content(p, p->enc, rb_strlen_lit("\\x")); yyerror0("invalid hex escape"); - token_flush(p); + dispatch_scan_event(p, tSTRING_CONTENT); return 0; } p->lex.pcur += *numlen; @@ -7820,31 +7798,37 @@ escaped_control_code(int c) } #define WARN_SPACE_CHAR(c, prefix) \ - rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2)) + rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c)) static int tokadd_codepoint(struct parser_params *p, rb_encoding **encp, - int regexp_literal, int wide) + int regexp_literal, const char *begin) { + const int wide = !begin; size_t numlen; int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen); + p->lex.pcur += numlen; if (p->lex.strterm == NULL || strterm_is_heredoc(p->lex.strterm) || (p->lex.strterm->u.literal.func != str_regexp)) { + if (!begin) begin = p->lex.pcur; if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) { - literal_flush(p, p->lex.pcur); + flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin); yyerror0("invalid Unicode escape"); + dispatch_scan_event(p, tSTRING_CONTENT); return wide && numlen > 0; } if (codepoint > 0x10ffff) { - literal_flush(p, p->lex.pcur); + flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin); yyerror0("invalid Unicode codepoint (too large)"); + dispatch_scan_event(p, tSTRING_CONTENT); return wide; } if ((codepoint & 0xfffff800) == 0xd800) { - literal_flush(p, p->lex.pcur); + flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin); yyerror0("invalid Unicode codepoint"); + dispatch_scan_event(p, tSTRING_CONTENT); return wide; } } @@ -7870,6 +7854,16 @@ tokadd_codepoint(struct parser_params *p, rb_encoding **encp, static int tokadd_mbchar(struct parser_params *p, int c); +static int +tokskip_mbchar(struct parser_params *p) +{ + int len = parser_precise_mbclen(p, p->lex.pcur-1); + if (len > 0) { + p->lex.pcur += len - 1; + } + return len; +} + /* return value is for ?\u3042 */ static void tokadd_utf8(struct parser_params *p, rb_encoding **encp, @@ -7922,7 +7916,7 @@ tokadd_utf8(struct parser_params *p, rb_encoding **encp, if (second == multiple_codepoints) second = p->lex.pcur; if (regexp_literal) tokadd(p, last); - if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) { + if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) { break; } while (ISSPACE(c = peekc(p))) { @@ -7935,8 +7929,9 @@ tokadd_utf8(struct parser_params *p, rb_encoding **encp, if (c != close_brace) { unterminated: - token_flush(p); + flush_string_content(p, rb_utf8_encoding(), 0); yyerror0("unterminated Unicode escape"); + dispatch_scan_event(p, tSTRING_CONTENT); return; } if (second && second != multiple_codepoints) { @@ -7954,7 +7949,7 @@ tokadd_utf8(struct parser_params *p, rb_encoding **encp, } } else { /* handle \uxxxx form */ - if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) { + if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) { token_flush(p); return; } @@ -7965,7 +7960,7 @@ tokadd_utf8(struct parser_params *p, rb_encoding **encp, #define ESCAPE_META 2 static int -read_escape(struct parser_params *p, int flags) +read_escape(struct parser_params *p, int flags, const char *begin) { int c; size_t numlen; @@ -8024,9 +8019,13 @@ read_escape(struct parser_params *p, int flags) nextc(p); goto eof; } - return read_escape(p, flags|ESCAPE_META) | 0x80; + return read_escape(p, flags|ESCAPE_META, begin) | 0x80; + } + else if (c == -1) goto eof; + else if (!ISASCII(c)) { + tokskip_mbchar(p); + goto eof; } - else if (c == -1 || !ISASCII(c)) goto eof; else { int c2 = escaped_control_code(c); if (c2) { @@ -8053,11 +8052,15 @@ read_escape(struct parser_params *p, int flags) nextc(p); goto eof; } - c = read_escape(p, flags|ESCAPE_CONTROL); + c = read_escape(p, flags|ESCAPE_CONTROL, begin); } else if (c == '?') return 0177; - else if (c == -1 || !ISASCII(c)) goto eof; + else if (c == -1) goto eof; + else if (!ISASCII(c)) { + tokskip_mbchar(p); + goto eof; + } else { int c2 = escaped_control_code(c); if (c2) { @@ -8084,11 +8087,16 @@ read_escape(struct parser_params *p, int flags) eof: case -1: + flush_string_content(p, p->enc, p->lex.pcur - begin); yyerror0("Invalid escape character syntax"); - token_flush(p); + dispatch_scan_event(p, tSTRING_CONTENT); return '\0'; default: + if (!ISASCII(c)) { + tokskip_mbchar(p); + goto eof; + } return c; } } @@ -8105,6 +8113,7 @@ tokadd_escape(struct parser_params *p) { int c; size_t numlen; + const char *begin = p->lex.pcur; switch (c = nextc(p)) { case '\n': @@ -8130,6 +8139,7 @@ tokadd_escape(struct parser_params *p) eof: case -1: + flush_string_content(p, p->enc, p->lex.pcur - begin); yyerror0("Invalid escape character syntax"); token_flush(p); return -1; @@ -8142,6 +8152,61 @@ tokadd_escape(struct parser_params *p) } static int +char_to_option(int c) +{ + int val; + + switch (c) { + case 'i': + val = RE_ONIG_OPTION_IGNORECASE; + break; + case 'x': + val = RE_ONIG_OPTION_EXTEND; + break; + case 'm': + val = RE_ONIG_OPTION_MULTILINE; + break; + default: + val = 0; + break; + } + return val; +} + +#define ARG_ENCODING_FIXED 16 +#define ARG_ENCODING_NONE 32 +#define ENC_ASCII8BIT 1 +#define ENC_EUC_JP 2 +#define ENC_Windows_31J 3 +#define ENC_UTF8 4 + +static int +char_to_option_kcode(int c, int *option, int *kcode) +{ + *option = 0; + + switch (c) { + case 'n': + *kcode = ENC_ASCII8BIT; + return (*option = ARG_ENCODING_NONE); + case 'e': + *kcode = ENC_EUC_JP; + break; + case 's': + *kcode = ENC_Windows_31J; + break; + case 'u': + *kcode = ENC_UTF8; + break; + default: + *kcode = -1; + return (*option = char_to_option(c)); + } + *option = ARG_ENCODING_FIXED; + return 1; +} + +static int regx_options(struct parser_params *p) { int kcode = 0; @@ -8154,9 +8219,9 @@ regx_options(struct parser_params *p) if (c == 'o') { options |= RE_OPTION_ONCE; } - else if (rb_char_to_option_kcode(c, &opt, &kc)) { + else if (char_to_option_kcode(c, &opt, &kc)) { if (kc >= 0) { - if (kc != rb_ascii8bit_encindex()) kcode = c; + if (kc != ENC_ASCII8BIT) kcode = c; kopt = opt; } else { @@ -8225,6 +8290,10 @@ parser_update_heredoc_indent(struct parser_params *p, int c) } p->heredoc_line_indent = -1; } + else { + /* Whitespace only line has no indentation */ + p->heredoc_line_indent = 0; + } } return FALSE; } @@ -8341,7 +8410,7 @@ tokadd_string(struct parser_params *p, case 'C': case 'M': { pushback(p, c); - c = read_escape(p, 0); + c = read_escape(p, 0, p->lex.pcur - 1); char *t = tokspace(p, rb_strlen_lit("\\x00")); *t++ = '\\'; @@ -8367,7 +8436,7 @@ tokadd_string(struct parser_params *p, else if (func & STR_FUNC_EXPAND) { pushback(p, c); if (func & STR_FUNC_ESCAPE) tokadd(p, '\\'); - c = read_escape(p, 0); + c = read_escape(p, 0, p->lex.pcur - 1); } else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { /* ignore backslashed spaces in %w */ @@ -8416,35 +8485,14 @@ tokadd_string(struct parser_params *p, #define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren) -#ifdef RIPPER static void -flush_string_content(struct parser_params *p, rb_encoding *enc) +flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back) { - VALUE content = yylval.val; - if (!ripper_is_node_yylval(p, content)) - content = ripper_new_yylval(p, 0, 0, content); + p->lex.pcur -= back; if (has_delayed_token(p)) { ptrdiff_t len = p->lex.pcur - p->lex.ptok; if (len > 0) { - rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc); - } - dispatch_delayed_token(p, tSTRING_CONTENT); - p->lex.ptok = p->lex.pcur; - RNODE_RIPPER(content)->nd_rval = yylval.val; - } - dispatch_scan_event(p, tSTRING_CONTENT); - if (yylval.val != content) - RNODE_RIPPER(content)->nd_rval = yylval.val; - yylval.val = content; -} -#else -static void -flush_string_content(struct parser_params *p, rb_encoding *enc) -{ - if (has_delayed_token(p)) { - ptrdiff_t len = p->lex.pcur - p->lex.ptok; - if (len > 0) { - rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc); + rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc); p->delayed.end_line = p->ruby_sourceline; p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg); } @@ -8452,10 +8500,9 @@ flush_string_content(struct parser_params *p, rb_encoding *enc) p->lex.ptok = p->lex.pcur; } dispatch_scan_event(p, tSTRING_CONTENT); + p->lex.pcur += back; } -#endif -RUBY_FUNC_EXPORTED const uint_least32_t ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32]; /* this can be shared with ripper, since it's independent from struct * parser_params. */ #ifndef RIPPER @@ -8503,6 +8550,7 @@ parser_peek_variable_name(struct parser_params *p) case '{': p->lex.pcur = ptr; p->command_start = TRUE; + yylval.state = p->lex.state; return tSTRING_DBEG; default: return 0; @@ -8551,7 +8599,7 @@ parse_string(struct parser_params *p, rb_strterm_literal_t *quote) int c, space = 0; rb_encoding *enc = p->enc; rb_encoding *base_enc = 0; - VALUE lit; + rb_parser_string_t *lit; if (func & STR_FUNC_TERM) { if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */ @@ -8573,19 +8621,19 @@ parse_string(struct parser_params *p, rb_strterm_literal_t *quote) if (func & STR_FUNC_QWORDS) { quote->func |= STR_FUNC_TERM; pushback(p, c); /* dispatch the term at tSTRING_END */ - add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__); + add_delayed_token(p, p->lex.ptok, p->lex.pcur); return ' '; } return parser_string_term(p, func); } if (space) { if (!ISSPACE(c)) pushback(p, c); - add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__); + add_delayed_token(p, p->lex.ptok, p->lex.pcur); return ' '; } newtok(p); if ((func & STR_FUNC_EXPAND) && c == '#') { - int t = parser_peek_variable_name(p); + enum yytokentype t = parser_peek_variable_name(p); if (t) return t; tokadd(p, '#'); c = nextc(p); @@ -8597,7 +8645,7 @@ parse_string(struct parser_params *p, rb_strterm_literal_t *quote) #ifndef RIPPER # define unterminated_literal(mesg) yyerror0(mesg) #else -# define unterminated_literal(mesg) compile_error(p, mesg) +# define unterminated_literal(mesg) compile_error(p, mesg) #endif literal_flush(p, p->lex.pcur); if (func & STR_FUNC_QWORDS) { @@ -8620,7 +8668,7 @@ parse_string(struct parser_params *p, rb_strterm_literal_t *quote) tokfix(p); lit = STR_NEW3(tok(p), toklen(p), enc, func); set_yylval_str(lit); - flush_string_content(p, enc); + flush_string_content(p, enc, 0); return tSTRING_CONTENT; } @@ -8702,7 +8750,6 @@ heredoc_identifier(struct parser_params *p) here->quote = quote; here->func = func; here->lastline = p->lex.lastline; - rb_ast_add_mark_object(p->ast, p->lex.lastline); token_flush(p); p->heredoc_indent = indent; @@ -8713,32 +8760,28 @@ heredoc_identifier(struct parser_params *p) static void heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here) { - VALUE line; + rb_parser_string_t *line; rb_strterm_t *term = p->lex.strterm; p->lex.strterm = 0; line = here->lastline; p->lex.lastline = line; - p->lex.pbeg = RSTRING_PTR(line); - p->lex.pend = p->lex.pbeg + RSTRING_LEN(line); + p->lex.pbeg = PARSER_STRING_PTR(line); + p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line); p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote; p->lex.ptok = p->lex.pbeg + here->offset - here->quote; p->heredoc_end = p->ruby_sourceline; p->ruby_sourceline = (int)here->sourceline; - if (p->eofp) p->lex.nextline = Qnil; + if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINATOR; p->eofp = 0; xfree(term); - rb_ast_delete_mark_object(p->ast, line); } static int -dedent_string(struct parser_params *p, VALUE string, int width) +dedent_string_column(const char *str, long len, int width) { - char *str; - long len; int i, col = 0; - RSTRING_GETMEM(string, str, len); for (i = 0; i < len && col < width; i++) { if (str[i] == ' ') { col++; @@ -8752,33 +8795,47 @@ dedent_string(struct parser_params *p, VALUE string, int width) break; } } + + return i; +} + +static int +dedent_string(struct parser_params *p, rb_parser_string_t *string, int width) +{ + char *str; + long len; + int i; + + len = PARSER_STRING_LEN(string); + str = PARSER_STRING_PTR(string); + + i = dedent_string_column(str, len, width); if (!i) return 0; - rb_str_modify(string); - str = RSTRING_PTR(string); - if (RSTRING_LEN(string) != len) - rb_fatal("literal string changed: %+"PRIsVALUE, string); + + rb_parser_str_modify(string); + str = PARSER_STRING_PTR(string); + if (PARSER_STRING_LEN(string) != len) + rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string)); MEMMOVE(str, str + i, char, len - i); - rb_str_set_len(string, len - i); + rb_parser_str_set_len(p, string, len - i); return i; } -#ifndef RIPPER static NODE * heredoc_dedent(struct parser_params *p, NODE *root) { NODE *node, *str_node, *prev_node; int indent = p->heredoc_indent; - VALUE prev_lit = 0; + rb_parser_string_t *prev_lit = 0; if (indent <= 0) return root; - p->heredoc_indent = 0; if (!root) return root; prev_node = node = str_node = root; if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head; while (str_node) { - VALUE lit = RNODE_LIT(str_node)->nd_lit; + rb_parser_string_t *lit = RNODE_STR(str_node)->string; if (nd_fl_newline(str_node)) { dedent_string(p, lit, indent); } @@ -8814,18 +8871,6 @@ heredoc_dedent(struct parser_params *p, NODE *root) } return root; } -#else /* RIPPER */ -static VALUE -heredoc_dedent(struct parser_params *p, VALUE array) -{ - int indent = p->heredoc_indent; - - if (indent <= 0) return array; - p->heredoc_indent = 0; - dispatch2(heredoc_dedent, array, INT2NUM(indent)); - return array; -} -#endif static int whole_match_p(struct parser_params *p, const char *eos, long len, int indent) @@ -8883,7 +8928,6 @@ number_literal_suffix(struct parser_params *p, int mask) } if (!ISASCII(c) || ISALPHA(c) || c == '_') { p->lex.pcur = lastp; - literal_flush(p, p->lex.pcur); return 0; } pushback(p, c); @@ -8893,44 +8937,43 @@ number_literal_suffix(struct parser_params *p, int mask) } static enum yytokentype -set_number_literal(struct parser_params *p, VALUE v, - enum yytokentype type, int suffix) +set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point) { - if (suffix & NUM_SUFFIX_I) { - v = rb_complex_raw(INT2FIX(0), v); - type = tIMAGINARY; + enum rb_numeric_type numeric_type = integer_literal; + + if (type == tFLOAT) { + numeric_type = float_literal; } - set_yylval_literal(v); - SET_LEX_STATE(EXPR_END); - return type; -} -static enum yytokentype -set_integer_literal(struct parser_params *p, VALUE v, int suffix) -{ - enum yytokentype type = tINTEGER; if (suffix & NUM_SUFFIX_R) { - v = rb_rational_raw1(v); type = tRATIONAL; + numeric_type = rational_literal; + } + if (suffix & NUM_SUFFIX_I) { + type = tIMAGINARY; } - return set_number_literal(p, v, type, suffix); -} -#ifdef RIPPER -static void -dispatch_heredoc_end(struct parser_params *p) -{ - VALUE str; - if (has_delayed_token(p)) - dispatch_delayed_token(p, tSTRING_CONTENT); - str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok); - ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str); - RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc); - lex_goto_eol(p); - token_flush(p); + switch (type) { + case tINTEGER: + set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc)); + break; + case tFLOAT: + set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc)); + break; + case tRATIONAL: + set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc)); + break; + case tIMAGINARY: + set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc)); + (void)numeric_type; /* for ripper */ + break; + default: + rb_bug("unexpected token: %d", type); + } + SET_LEX_STATE(EXPR_END); + return type; } -#else #define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__) static void parser_dispatch_heredoc_end(struct parser_params *p, int line) @@ -8938,17 +8981,21 @@ parser_dispatch_heredoc_end(struct parser_params *p, int line) if (has_delayed_token(p)) dispatch_delayed_token(p, tSTRING_CONTENT); +#ifdef RIPPER + VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok); + ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str); +#else if (p->keep_tokens) { - VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok); + rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc); RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc); parser_append_tokens(p, str, tHEREDOC_END, line); } +#endif RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc); lex_goto_eol(p); token_flush(p); } -#endif static enum yytokentype here_document(struct parser_params *p, rb_strterm_heredoc_t *here) @@ -8956,12 +9003,15 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) int c, func, indent = 0; const char *eos, *ptr, *ptr_end; long len; - VALUE str = 0; + rb_parser_string_t *str = 0; rb_encoding *enc = p->enc; rb_encoding *base_enc = 0; int bol; +#ifdef RIPPER + VALUE s_value; +#endif - eos = RSTRING_PTR(here->lastline) + here->offset; + eos = PARSER_STRING_PTR(here->lastline) + here->offset; len = here->length; indent = (func = here->func) & STR_FUNC_INDENT; @@ -8971,9 +9021,9 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) if (!has_delayed_token(p)) { dispatch_scan_event(p, tSTRING_CONTENT); } - else { + else if (p->delayed.end_line + 1 == p->ruby_sourceline) { if ((len = p->lex.pcur - p->lex.ptok) > 0) { - if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) { + if (!(func & STR_FUNC_REGEXP)) { int cr = ENC_CODERANGE_UNKNOWN; rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr); if (cr != ENC_CODERANGE_7BIT && @@ -8982,10 +9032,14 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) enc = rb_ascii8bit_encoding(); } } - rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc); + rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc); } dispatch_delayed_token(p, tSTRING_CONTENT); } + else { + dispatch_delayed_token(p, tSTRING_CONTENT); + dispatch_scan_event(p, tSTRING_CONTENT); + } lex_goto_eol(p); #endif heredoc_restore(p, &p->lex.strterm->u.heredoc); @@ -9017,7 +9071,7 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) if (!(func & STR_FUNC_EXPAND)) { do { - ptr = RSTRING_PTR(p->lex.lastline); + ptr = PARSER_STRING_PTR(p->lex.lastline); ptr_end = p->lex.pend; if (ptr_end > ptr) { switch (ptr_end[-1]) { @@ -9039,16 +9093,17 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) } if (str) - rb_str_cat(str, ptr, ptr_end - ptr); + parser_str_cat(str, ptr, ptr_end - ptr); else - str = STR_NEW(ptr, ptr_end - ptr); - if (!lex_eol_ptr_p(p, ptr_end)) rb_str_cat(str, "\n", 1); + str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc); + if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n"); lex_goto_eol(p); if (p->heredoc_indent > 0) { goto flush_str; } if (nextc(p) == -1) { if (str) { + rb_parser_string_free(p, str); str = 0; } goto error; @@ -9059,7 +9114,7 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/ newtok(p); if (c == '#') { - int t = parser_peek_variable_name(p); + enum yytokentype t = parser_peek_variable_name(p); if (p->heredoc_line_indent != -1) { if (p->heredoc_indent > p->heredoc_line_indent) { p->heredoc_indent = p->heredoc_line_indent; @@ -9086,7 +9141,7 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) #ifndef RIPPER if (bol) nd_set_fl_newline(yylval.node); #endif - flush_string_content(p, enc); + flush_string_content(p, enc, 0); return tSTRING_CONTENT; } tokadd(p, nextc(p)); @@ -9100,14 +9155,18 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) str = STR_NEW3(tok(p), toklen(p), enc, func); } dispatch_heredoc_end(p); -#ifdef RIPPER - str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT), - yylval.val, str); -#endif heredoc_restore(p, &p->lex.strterm->u.heredoc); token_flush(p); p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0); +#ifdef RIPPER + /* Preserve s_value for set_yylval_str */ + s_value = p->s_value; +#endif set_yylval_str(str); +#ifdef RIPPER + set_parser_s_value(s_value); +#endif + #ifndef RIPPER if (bol) nd_set_fl_newline(yylval.node); #endif @@ -9121,10 +9180,10 @@ arg_ambiguous(struct parser_params *p, char c) { #ifndef RIPPER if (c == '/') { - rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c)); + rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c)); } else { - rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c)); + rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c)); } #else dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1)); @@ -9132,42 +9191,34 @@ arg_ambiguous(struct parser_params *p, char c) return TRUE; } -static ID -#ifndef RIPPER -formal_argument(struct parser_params *p, ID lhs) -#else -formal_argument(struct parser_params *p, VALUE lhs) -#endif +/* returns true value if formal argument error; + * Qtrue, or error message if ripper */ +static VALUE +formal_argument_error(struct parser_params *p, ID id) { - ID id = get_id(lhs); - switch (id_type(id)) { case ID_LOCAL: break; #ifndef RIPPER -# define ERR(mesg) yyerror0(mesg) +# define ERR(mesg) (yyerror0(mesg), Qtrue) #else -# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p)) +# define ERR(mesg) WARN_S(mesg) #endif case ID_CONST: - ERR("formal argument cannot be a constant"); - return 0; + return ERR("formal argument cannot be a constant"); case ID_INSTANCE: - ERR("formal argument cannot be an instance variable"); - return 0; + return ERR("formal argument cannot be an instance variable"); case ID_GLOBAL: - ERR("formal argument cannot be a global variable"); - return 0; + return ERR("formal argument cannot be a global variable"); case ID_CLASS: - ERR("formal argument cannot be a class variable"); - return 0; + return ERR("formal argument cannot be a class variable"); default: - ERR("formal argument must be local variable"); - return 0; + return ERR("formal argument must be local variable"); #undef ERR } shadowing_lvar(p, id); - return lhs; + + return Qfalse; } static int @@ -9200,17 +9251,33 @@ parser_encode_length(struct parser_params *p, const char *name, long len) static void parser_set_encode(struct parser_params *p, const char *name) { - int idx = rb_enc_find_index(name); rb_encoding *enc; VALUE excargs[3]; + int idx = 0; + const char *wrong = 0; + switch (*name) { + case 'e': case 'E': wrong = "external"; break; + case 'i': case 'I': wrong = "internal"; break; + case 'f': case 'F': wrong = "filesystem"; break; + case 'l': case 'L': wrong = "locale"; break; + } + if (wrong && STRCASECMP(name, wrong) == 0) goto unknown; + idx = rb_enc_find_index(name); if (idx < 0) { + unknown: excargs[1] = rb_sprintf("unknown encoding name: %s", name); error: excargs[0] = rb_eArgError; excargs[2] = rb_make_backtrace(); rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline)); - rb_exc_raise(rb_make_exception(3, excargs)); + VALUE exc = rb_make_exception(3, excargs); + ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline); + + rb_ast_free(p->ast); + p->ast = NULL; + + rb_exc_raise(exc); } enc = rb_enc_from_index(idx); if (!rb_enc_asciicompat(enc)) { @@ -9220,25 +9287,19 @@ parser_set_encode(struct parser_params *p, const char *name) p->enc = enc; #ifndef RIPPER if (p->debug_lines) { - VALUE lines = p->debug_lines; - long i, n = RARRAY_LEN(lines); - for (i = 0; i < n; ++i) { - rb_enc_associate_index(RARRAY_AREF(lines, i), idx); + long i; + for (i = 0; i < p->debug_lines->len; i++) { + rb_parser_enc_associate(p, p->debug_lines->data[i], enc); } } #endif } -static int +static bool comment_at_top(struct parser_params *p) { - const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1; - if (p->line_count != (p->has_shebang ? 2 : 1)) return 0; - while (ptr < ptr_end) { - if (!ISSPACE(*ptr)) return 0; - ptr++; - } - return 1; + if (p->token_seen) return false; + return (p->line_count == (p->has_shebang ? 2 : 1)); } typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len); @@ -9293,7 +9354,7 @@ parser_set_frozen_string_literal(struct parser_params *p, const char *name, cons int b; if (p->token_seen) { - rb_warning1("`%s' is ignored after any tokens", WARN_S(name)); + rb_warning1("'%s' is ignored after any tokens", WARN_S(name)); return; } @@ -9309,30 +9370,30 @@ parser_set_shareable_constant_value(struct parser_params *p, const char *name, c for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) { if (*s == ' ' || *s == '\t') continue; if (*s == '#') break; - rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name)); + rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name)); return; } switch (*val) { case 'n': case 'N': if (STRCASECMP(val, "none") == 0) { - p->ctxt.shareable_constant_value = shareable_none; + p->ctxt.shareable_constant_value = rb_parser_shareable_none; return; } break; case 'l': case 'L': if (STRCASECMP(val, "literal") == 0) { - p->ctxt.shareable_constant_value = shareable_literal; + p->ctxt.shareable_constant_value = rb_parser_shareable_literal; return; } break; case 'e': case 'E': if (STRCASECMP(val, "experimental_copy") == 0) { - p->ctxt.shareable_constant_value = shareable_copy; + p->ctxt.shareable_constant_value = rb_parser_shareable_copy; return; } if (STRCASECMP(val, "experimental_everything") == 0) { - p->ctxt.shareable_constant_value = shareable_everything; + p->ctxt.shareable_constant_value = rb_parser_shareable_everything; return; } break; @@ -9452,6 +9513,7 @@ parser_magic_comment(struct parser_params *p, const char *str, long len) do str++; while (--len > 0 && ISSPACE(*str)); if (!len) break; + const char *tok_beg = str; if (*str == '"') { for (vbeg = ++str; --len > 0 && *str != '"'; str++) { if (*str == '\\') { @@ -9469,6 +9531,7 @@ parser_magic_comment(struct parser_params *p, const char *str, long len) for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++); vend = str; } + const char *tok_end = str; if (indicator) { while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++; } @@ -9490,6 +9553,8 @@ parser_magic_comment(struct parser_params *p, const char *str, long len) n = (*mc->length)(p, vbeg, n); } str_copy(val, vbeg, n); + p->lex.ptok = tok_beg; + p->lex.pcur = tok_end; (*mc->func)(p, mc->name, RSTRING_PTR(val)); break; } @@ -9543,6 +9608,8 @@ set_file_encoding(struct parser_params *p, const char *str, const char *send) beg = str; while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send); s = rb_str_new(beg, parser_encode_length(p, beg, str - beg)); + p->lex.ptok = beg; + p->lex.pcur = str; parser_set_encode(p, RSTRING_PTR(s)); rb_str_resize(s, 0); } @@ -9564,7 +9631,7 @@ parser_prepare(struct parser_params *p) p->lex.pcur += 2; #ifndef RIPPER if (p->debug_lines) { - rb_enc_associate(p->lex.lastline, p->enc); + rb_parser_string_set_encoding(p->lex.lastline, p->enc); } #endif p->lex.pbeg = p->lex.pcur; @@ -9572,16 +9639,16 @@ parser_prepare(struct parser_params *p) return; } break; - case EOF: + case -1: /* end of script. */ return; } pushback(p, c); - p->enc = rb_enc_get(p->lex.lastline); + p->enc = rb_parser_str_get_encoding(p->lex.lastline); } #ifndef RIPPER #define ambiguous_operator(tok, op, syn) ( \ - rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \ + rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \ rb_warning0("even though it seems like "syn"")) #else #define ambiguous_operator(tok, op, syn) \ @@ -9593,24 +9660,13 @@ parser_prepare(struct parser_params *p) (ambiguous_operator(tok, op, syn), 0)), \ (enum yytokentype)(tok)) -static VALUE -parse_rational(struct parser_params *p, char *str, int len, int seen_point) -{ - VALUE v; - char *point = &str[seen_point]; - size_t fraclen = len-seen_point-1; - memmove(point, point+1, fraclen+1); - v = rb_cstr_to_inum(str, 10, FALSE); - return rb_rational_new(v, rb_int_positive_pow(10, fraclen)); -} - static enum yytokentype no_digits(struct parser_params *p) { yyerror0("numeric literal without digits"); if (peek(p, '_')) nextc(p); /* dummy 0, for tUMINUS_NUM at numeric */ - return set_integer_literal(p, INT2FIX(0), 0); + return set_number_literal(p, tINTEGER, 0, 10, 0); } static enum yytokentype @@ -9651,7 +9707,7 @@ parse_numeric(struct parser_params *p, int c) } else if (nondigit) goto trailing_uc; suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); - return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix); + return set_number_literal(p, tINTEGER, suffix, 16, 0); } if (c == 'b' || c == 'B') { /* binary */ @@ -9675,7 +9731,7 @@ parse_numeric(struct parser_params *p, int c) } else if (nondigit) goto trailing_uc; suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); - return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix); + return set_number_literal(p, tINTEGER, suffix, 2, 0); } if (c == 'd' || c == 'D') { /* decimal */ @@ -9699,7 +9755,7 @@ parse_numeric(struct parser_params *p, int c) } else if (nondigit) goto trailing_uc; suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); - return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix); + return set_number_literal(p, tINTEGER, suffix, 10, 0); } if (c == '_') { /* 0_0 */ @@ -9709,6 +9765,7 @@ parse_numeric(struct parser_params *p, int c) /* prefixed octal */ c = nextc(p); if (c == -1 || c == '_' || !ISDIGIT(c)) { + tokfix(p); return no_digits(p); } } @@ -9731,7 +9788,7 @@ parse_numeric(struct parser_params *p, int c) tokfix(p); if (nondigit) goto trailing_uc; suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); - return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix); + return set_number_literal(p, tINTEGER, suffix, 8, 0); } if (nondigit) { pushback(p, c); @@ -9747,8 +9804,9 @@ parse_numeric(struct parser_params *p, int c) } else { pushback(p, c); + tokfix(p); suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); - return set_integer_literal(p, INT2FIX(0), suffix); + return set_number_literal(p, tINTEGER, suffix, 10, 0); } } @@ -9822,31 +9880,28 @@ parse_numeric(struct parser_params *p, int c) trailing_uc: literal_flush(p, p->lex.pcur - 1); YYLTYPE loc = RUBY_INIT_YYLLOC(); - compile_error(p, "trailing `%c' in number", nondigit); + compile_error(p, "trailing '%c' in number", nondigit); parser_show_error_line(p, &loc); } tokfix(p); if (is_float) { enum yytokentype type = tFLOAT; - VALUE v; suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL); if (suffix & NUM_SUFFIX_R) { type = tRATIONAL; - v = parse_rational(p, tok(p), toklen(p), seen_point); } else { - double d = strtod(tok(p), 0); + strtod(tok(p), 0); if (errno == ERANGE) { rb_warning1("Float %s out of range", WARN_S(tok(p))); errno = 0; } - v = DBL2NUM(d); } - return set_number_literal(p, v, type, suffix); + return set_number_literal(p, type, suffix, 0, seen_point); } suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); - return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix); + return set_number_literal(p, tINTEGER, suffix, 10, 0); } static enum yytokentype @@ -9854,7 +9909,8 @@ parse_qmark(struct parser_params *p, int space_seen) { rb_encoding *enc; register int c; - VALUE lit; + rb_parser_string_t *lit; + const char *start = p->lex.pcur; if (IS_END()) { SET_LEX_STATE(EXPR_VALUE); @@ -9879,20 +9935,18 @@ parse_qmark(struct parser_params *p, int space_seen) } newtok(p); enc = p->enc; - if (!parser_isascii(p)) { - if (tokadd_mbchar(p, c) == -1) return 0; - } - else if ((rb_enc_isalnum(c, p->enc) || c == '_') && - !lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) { + int w = parser_precise_mbclen(p, start); + if (is_identchar(p, start, p->lex.pend, p->enc) && + !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) { if (space_seen) { - const char *start = p->lex.pcur - 1, *ptr = start; + const char *ptr = start; do { int n = parser_precise_mbclen(p, ptr); if (n < 0) return -1; ptr += n; } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc)); - rb_warn2("`?' just followed by `%.*s' is interpreted as" \ - " a conditional operator, put a space after `?'", + rb_warn2("'?' just followed by '%.*s' is interpreted as" \ + " a conditional operator, put a space after '?'", WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start))); } goto ternary; @@ -9903,17 +9957,17 @@ parse_qmark(struct parser_params *p, int space_seen) enc = rb_utf8_encoding(); tokadd_utf8(p, &enc, -1, 0, 0); } - else if (!ISASCII(c = peekc(p))) { + else if (!ISASCII(c = peekc(p)) && c != -1) { nextc(p); if (tokadd_mbchar(p, c) == -1) return 0; } else { - c = read_escape(p, 0); + c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\")); tokadd(p, c); } } else { - tokadd(p, c); + if (tokadd_mbchar(p, c) == -1) return 0; } tokfix(p); lit = STR_NEW3(tok(p), toklen(p), enc, 0); @@ -10055,7 +10109,7 @@ parse_numvar(struct parser_params *p) if (overflow || n > nth_ref_max) { /* compile_error()? */ - rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p))); + rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p))); return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */ } else { @@ -10097,7 +10151,7 @@ parse_gvar(struct parser_params *p, const enum lex_state_e last_state) case '.': /* $.: last read line number */ case '=': /* $=: ignorecase */ case ':': /* $:: load path */ - case '<': /* $<: reading filename */ + case '<': /* $<: default input handle */ case '>': /* $>: default output handle */ case '\"': /* $": already loaded files */ tokadd(p, '$'); @@ -10117,7 +10171,7 @@ parse_gvar(struct parser_params *p, const enum lex_state_e last_state) return '$'; } gvar: - set_yylval_name(TOK_INTERN()); + tokenize_ident(p); return tGVAR; case '&': /* $&: last match */ @@ -10151,11 +10205,11 @@ parse_gvar(struct parser_params *p, const enum lex_state_e last_state) if (!parser_is_identchar(p)) { YYLTYPE loc = RUBY_INIT_YYLLOC(); if (c == -1 || ISSPACE(c)) { - compile_error(p, "`$' without identifiers is not allowed as a global variable name"); + compile_error(p, "'$' without identifiers is not allowed as a global variable name"); } else { pushback(p, c); - compile_error(p, "`$%c' is not allowed as a global variable name", c); + compile_error(p, "'$%c' is not allowed as a global variable name", c); } parser_show_error_line(p, &loc); set_yylval_noname(); @@ -10172,13 +10226,12 @@ parse_gvar(struct parser_params *p, const enum lex_state_e last_state) tokenize_ident(p); } else { - compile_error(p, "`%.*s' is not allowed as a global variable name", toklen(p), tok(p)); + compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p)); set_yylval_noname(); } return tGVAR; } -#ifndef RIPPER static bool parser_numbered_param(struct parser_params *p, int n) { @@ -10200,7 +10253,6 @@ parser_numbered_param(struct parser_params *p, int n) } return true; } -#endif static enum yytokentype parse_atmark(struct parser_params *p, const enum lex_state_e last_state) @@ -10223,10 +10275,10 @@ parse_atmark(struct parser_params *p, const enum lex_state_e last_state) pushback(p, c); RUBY_SET_YYLLOC(loc); if (result == tIVAR) { - compile_error(p, "`@' without identifiers is not allowed as an instance variable name"); + compile_error(p, "'@' without identifiers is not allowed as an instance variable name"); } else { - compile_error(p, "`@@' without identifiers is not allowed as a class variable name"); + compile_error(p, "'@@' without identifiers is not allowed as a class variable name"); } parser_show_error_line(p, &loc); set_yylval_noname(); @@ -10237,10 +10289,10 @@ parse_atmark(struct parser_params *p, const enum lex_state_e last_state) pushback(p, c); RUBY_SET_YYLLOC(loc); if (result == tIVAR) { - compile_error(p, "`@%c' is not allowed as an instance variable name", c); + compile_error(p, "'@%c' is not allowed as an instance variable name", c); } else { - compile_error(p, "`@@%c' is not allowed as a class variable name", c); + compile_error(p, "'@@%c' is not allowed as a class variable name", c); } parser_show_error_line(p, &loc); set_yylval_noname(); @@ -10257,13 +10309,13 @@ static enum yytokentype parse_ident(struct parser_params *p, int c, int cmd_state) { enum yytokentype result; - int mb = ENC_CODERANGE_7BIT; + bool is_ascii = true; const enum lex_state_e last_state = p->lex.state; ID ident; int enforce_keyword_end = 0; do { - if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN; + if (!ISASCII(c)) is_ascii = false; if (tokadd_mbchar(p, c) == -1) return 0; c = nextc(p); } while (parser_is_identchar(p)); @@ -10286,20 +10338,20 @@ parse_ident(struct parser_params *p, int c, int cmd_state) if (IS_LABEL_SUFFIX(0)) { SET_LEX_STATE(EXPR_ARG|EXPR_LABELED); nextc(p); - set_yylval_name(TOK_INTERN()); + tokenize_ident(p); return tLABEL; } } #ifndef RIPPER - if (!NIL_P(peek_end_expect_token_locations(p))) { - VALUE end_loc; + if (peek_end_expect_token_locations(p)) { + const rb_code_position_t *end_pos; int lineno, column; int beg_pos = (int)(p->lex.ptok - p->lex.pbeg); - end_loc = peek_end_expect_token_locations(p); - lineno = NUM2INT(rb_ary_entry(end_loc, 0)); - column = NUM2INT(rb_ary_entry(end_loc, 1)); + end_pos = peek_end_expect_token_locations(p)->pos; + lineno = end_pos->lineno; + column = end_pos->column; if (p->debug) { rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n", @@ -10317,7 +10369,7 @@ parse_ident(struct parser_params *p, int c, int cmd_state) } #endif - if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) { + if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) { const struct kwtable *kw; /* See if it is a reserved word. */ @@ -10422,9 +10474,9 @@ parser_yylex(struct parser_params *p) case '\004': /* ^D */ case '\032': /* ^Z */ case -1: /* end of script. */ - p->eofp = 1; + p->eofp = 1; #ifndef RIPPER - if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) { + if (p->end_expect_token_locations) { pop_end_expect_token_locations(p); RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc); return tDUMNY_END; @@ -10463,19 +10515,21 @@ parser_yylex(struct parser_params *p) case '#': /* it's a comment */ p->token_seen = token_seen; + const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok; /* no magic_comment in shebang line */ if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) { if (comment_at_top(p)) { set_file_encoding(p, p->lex.pcur, p->lex.pend); } } + p->lex.pcur = pcur, p->lex.ptok = ptok; lex_goto_eol(p); dispatch_scan_event(p, tCOMMENT); fallthru = TRUE; /* fall through */ case '\n': p->token_seen = token_seen; - VALUE prevline = p->lex.lastline; + rb_parser_string_t *prevline = p->lex.lastline; c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) && !IS_lex_state(EXPR_LABELED)); if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) { @@ -10501,7 +10555,24 @@ parser_yylex(struct parser_params *p) token_flush(p); } goto retry; + case 'a': + if (peek_word_at(p, "nd", 2, 0)) goto leading_logical; + goto bol; + case 'o': + if (peek_word_at(p, "r", 1, 0)) goto leading_logical; + goto bol; + case '|': + if (peek(p, '|')) goto leading_logical; + goto bol; case '&': + if (peek(p, '&')) { + leading_logical: + pushback(p, c); + dispatch_delayed_token(p, tIGNORED_NL); + cmd_state = FALSE; + goto retry; + } + /* fall through */ case '.': { dispatch_delayed_token(p, tIGNORED_NL); if (peek(p, '.') == (c == '&')) { @@ -10510,11 +10581,15 @@ parser_yylex(struct parser_params *p) goto retry; } } + bol: default: p->ruby_sourceline--; p->lex.nextline = p->lex.lastline; set_lastline(p, prevline); case -1: /* EOF no decrement*/ + if (c == -1 && space_seen) { + dispatch_scan_event(p, tSP); + } lex_goto_eol(p); if (c != -1) { token_flush(p); @@ -10537,7 +10612,7 @@ parser_yylex(struct parser_params *p) } pushback(p, c); if (IS_SPCARG(c)) { - rb_warning0("`**' interpreted as argument prefix"); + rb_warning0("'**' interpreted as argument prefix"); c = tDSTAR; } else if (IS_BEG()) { @@ -10555,7 +10630,7 @@ parser_yylex(struct parser_params *p) } pushback(p, c); if (IS_SPCARG(c)) { - rb_warning0("`*' interpreted as argument prefix"); + rb_warning0("'*' interpreted as argument prefix"); c = tSTAR; } else if (IS_BEG()) { @@ -10641,7 +10716,7 @@ parser_yylex(struct parser_params *p) !IS_lex_state(EXPR_DOT | EXPR_CLASS) && !IS_END() && (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) { - int token = heredoc_identifier(p); + enum yytokentype token = heredoc_identifier(p); if (token) return token < 0 ? 0 : token; } if (IS_AFTER_OPERATOR()) { @@ -10745,7 +10820,7 @@ parser_yylex(struct parser_params *p) (c = peekc_n(p, 1)) == -1 || !(c == '\'' || c == '"' || is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) { - rb_warning0("`&' interpreted as argument prefix"); + rb_warning0("'&' interpreted as argument prefix"); } c = tAMPER; } @@ -10827,6 +10902,8 @@ parser_yylex(struct parser_params *p) } if (c == '>') { SET_LEX_STATE(EXPR_ENDFN); + yylval.num = p->lex.lpar_beg; + p->lex.lpar_beg = p->lex.paren_nest; return tLAMBDA; } if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) { @@ -10846,17 +10923,13 @@ parser_yylex(struct parser_params *p) SET_LEX_STATE(EXPR_BEG); if ((c = nextc(p)) == '.') { if ((c = nextc(p)) == '.') { - if (p->ctxt.in_argdef) { + if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) { SET_LEX_STATE(EXPR_ENDARG); return tBDOT3; } if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) { rb_warn0("... at EOL, should be parenthesized?"); } - else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) { - if (IS_lex_state_for(last_state, EXPR_LABEL)) - return tDOT3; - } return is_beg ? tBDOT3 : tDOT3; } pushback(p, c); @@ -11096,7 +11169,7 @@ parser_yylex(struct parser_params *p) default: if (!parser_is_identchar(p)) { - compile_error(p, "Invalid char `\\x%02X' in expression", c); + compile_error(p, "Invalid char '\\x%02X' in expression", c); token_flush(p); goto retry; } @@ -11114,7 +11187,7 @@ yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p) enum yytokentype t; p->lval = lval; - lval->val = Qundef; + lval->node = 0; p->yylloc = yylloc; t = parser_yylex(p); @@ -11158,27 +11231,27 @@ node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t a #define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc)) -#ifndef RIPPER - static rb_node_scope_t * -rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc) +rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc) { rb_ast_id_table_t *nd_tbl; nd_tbl = local_tbl(p); rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc); n->nd_tbl = nd_tbl; n->nd_body = nd_body; + n->nd_parent = nd_parent; n->nd_args = nd_args; return n; } static 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) +rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, NODE *nd_parent, const YYLTYPE *loc) { rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc); n->nd_tbl = nd_tbl; n->nd_body = nd_body; + n->nd_parent = nd_parent; n->nd_args = nd_args; return n; @@ -11210,18 +11283,22 @@ rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc) { rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc); n->nd_head = nd_head; - n->nd_end = 0; + n->nd_end = (NODE *)n; n->nd_next = 0; return n; } static rb_node_for_t * -rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc) +rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *for_keyword_loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *do_keyword_loc, const YYLTYPE *end_keyword_loc) { rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc); n->nd_body = nd_body; n->nd_iter = nd_iter; + n->for_keyword_loc = *for_keyword_loc; + n->in_keyword_loc = *in_keyword_loc; + n->do_keyword_loc = *do_keyword_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } @@ -11264,12 +11341,13 @@ rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE * } static rb_node_resbody_t * -rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_head, const YYLTYPE *loc) +rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc) { rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc); - n->nd_head = nd_head; - n->nd_body = nd_body; n->nd_args = nd_args; + n->nd_exc_var = nd_exc_var; + n->nd_body = nd_body; + n->nd_next = nd_next; return n; } @@ -11279,104 +11357,128 @@ rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const { rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc); n->nd_head = nd_head; - n->nd_resq = 0; n->nd_ensr = nd_ensr; return n; } static rb_node_and_t * -rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc) +rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc) { rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc); n->nd_1st = nd_1st; n->nd_2nd = nd_2nd; + n->operator_loc = *operator_loc; return n; } static rb_node_or_t * -rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc) +rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc) { rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc); n->nd_1st = nd_1st; n->nd_2nd = nd_2nd; + n->operator_loc = *operator_loc; return n; } static rb_node_return_t * -rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc) +rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc); n->nd_stts = nd_stts; + n->keyword_loc = *keyword_loc; return n; } static rb_node_yield_t * -rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc) +rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc) { + if (nd_head) no_blockarg(p, nd_head); + rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc); n->nd_head = nd_head; + n->keyword_loc = *keyword_loc; + n->lparen_loc = *lparen_loc; + n->rparen_loc = *rparen_loc; return n; } static rb_node_if_t * -rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc) +rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc) { rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc); n->nd_cond = nd_cond; n->nd_body = nd_body; n->nd_else = nd_else; + n->if_keyword_loc = *if_keyword_loc; + n->then_keyword_loc = *then_keyword_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } static rb_node_unless_t * -rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc) +rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc) { rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc); n->nd_cond = nd_cond; n->nd_body = nd_body; n->nd_else = nd_else; + n->keyword_loc = *keyword_loc; + n->then_keyword_loc = *then_keyword_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } static rb_node_class_t * -rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc) +rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc) { /* Keep the order of node creation */ - NODE *scope = NEW_SCOPE(0, nd_body, loc); + NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc); rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc); + RNODE_SCOPE(scope)->nd_parent = &n->node; n->nd_cpath = nd_cpath; n->nd_body = scope; n->nd_super = nd_super; + n->class_keyword_loc = *class_keyword_loc; + n->inheritance_operator_loc = *inheritance_operator_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } static rb_node_sclass_t * -rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc) +rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *class_keyword_loc, const YYLTYPE *operator_loc, const YYLTYPE *end_keyword_loc) { /* Keep the order of node creation */ - NODE *scope = NEW_SCOPE(0, nd_body, loc); + NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc); rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc); + RNODE_SCOPE(scope)->nd_parent = &n->node; n->nd_recv = nd_recv; n->nd_body = scope; + n->class_keyword_loc = *class_keyword_loc; + n->operator_loc = *operator_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } static rb_node_module_t * -rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc) +rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc) { /* Keep the order of node creation */ - NODE *scope = NEW_SCOPE(0, nd_body, loc); + NODE *scope = NEW_SCOPE(0, nd_body, NULL, loc); rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc); + RNODE_SCOPE(scope)->nd_parent = &n->node; n->nd_cpath = nd_cpath; n->nd_body = scope; + n->module_keyword_loc = *module_keyword_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } @@ -11385,8 +11487,9 @@ static rb_node_iter_t * rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc) { /* Keep the order of node creation */ - NODE *scope = NEW_SCOPE(nd_args, nd_body, loc); + NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc); rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc); + RNODE_SCOPE(scope)->nd_parent = &n->node; n->nd_body = scope; n->nd_iter = 0; @@ -11394,125 +11497,151 @@ rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body } static rb_node_lambda_t * -rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc) +rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc) { /* Keep the order of node creation */ - NODE *scope = NEW_SCOPE(nd_args, nd_body, loc); - rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc); + NODE *scope = NEW_SCOPE(nd_args, nd_body, NULL, loc); + YYLTYPE lambda_loc = code_loc_gen(operator_loc, closing_loc); + rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, &lambda_loc); + RNODE_SCOPE(scope)->nd_parent = &n->node; n->nd_body = scope; + n->operator_loc = *operator_loc; + n->opening_loc = *opening_loc; + n->closing_loc = *closing_loc; return n; } static rb_node_case_t * -rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc) +rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc) { rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc); n->nd_head = nd_head; n->nd_body = nd_body; + n->case_keyword_loc = *case_keyword_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } static rb_node_case2_t * -rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc) +rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc) { rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc); n->nd_head = 0; n->nd_body = nd_body; + n->case_keyword_loc = *case_keyword_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } static rb_node_case3_t * -rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc) +rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc) { rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc); n->nd_head = nd_head; n->nd_body = nd_body; + n->case_keyword_loc = *case_keyword_loc; + n->end_keyword_loc = *end_keyword_loc; return n; } static rb_node_when_t * -rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc) +rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc) { rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc); n->nd_head = nd_head; n->nd_body = nd_body; n->nd_next = nd_next; + n->keyword_loc = *keyword_loc; + n->then_keyword_loc = *then_keyword_loc; return n; } static rb_node_in_t * -rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc) +rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc) { rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc); n->nd_head = nd_head; n->nd_body = nd_body; n->nd_next = nd_next; + n->in_keyword_loc = *in_keyword_loc; + n->then_keyword_loc = *then_keyword_loc; + n->operator_loc = *operator_loc; return n; } static rb_node_while_t * -rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc) +rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc) { rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc); n->nd_cond = nd_cond; n->nd_body = nd_body; n->nd_state = nd_state; + n->keyword_loc = *keyword_loc; + n->closing_loc = *closing_loc; return n; } static rb_node_until_t * -rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc) +rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc) { rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc); n->nd_cond = nd_cond; n->nd_body = nd_body; n->nd_state = nd_state; + n->keyword_loc = *keyword_loc; + n->closing_loc = *closing_loc; return n; } static rb_node_colon2_t * -rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc) +rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc) { rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc); n->nd_head = nd_head; n->nd_mid = nd_mid; + n->delimiter_loc = *delimiter_loc; + n->name_loc = *name_loc; return n; } static rb_node_colon3_t * -rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc) +rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc) { rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc); n->nd_mid = nd_mid; + n->delimiter_loc = *delimiter_loc; + n->name_loc = *name_loc; return n; } static rb_node_dot2_t * -rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc) +rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc) { rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc); n->nd_beg = nd_beg; n->nd_end = nd_end; + n->operator_loc = *operator_loc; return n; } static rb_node_dot3_t * -rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc) +rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc) { rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc); n->nd_beg = nd_beg; n->nd_end = nd_end; + n->operator_loc = *operator_loc; return n; } @@ -11551,10 +11680,14 @@ rb_node_false_new(struct parser_params *p, const YYLTYPE *loc) } static rb_node_super_t * -rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc) +rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc, + const YYLTYPE *keyword_loc, const YYLTYPE *lparen_loc, const YYLTYPE *rparen_loc) { rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc); n->nd_args = nd_args; + n->keyword_loc = *keyword_loc; + n->lparen_loc = *lparen_loc; + n->rparen_loc = *rparen_loc; return n; } @@ -11588,16 +11721,10 @@ rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const return n; } -/* TODO: Use union for NODE_LIST2 */ static rb_node_list_t * rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc) { - rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc); - n->nd_head = nd_head; - n->as.nd_alen = 1; - n->nd_next = 0; - - return n; + return rb_node_list_new2(p, nd_head, 1, 0, loc); } static rb_node_list_t * @@ -11691,19 +11818,23 @@ rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYL } static 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) +rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc) { rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc); n->nd_recv = nd_recv; n->nd_mid = nd_mid; n->nd_index = index; n->nd_rvalue = rvalue; + n->call_operator_loc = *call_operator_loc; + n->opening_loc = *opening_loc; + n->closing_loc = *closing_loc; + n->binary_operator_loc = *binary_operator_loc; return n; } static 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) +rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc) { rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc); n->nd_recv = nd_recv; @@ -11711,6 +11842,9 @@ rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID n->nd_vid = nd_vid; n->nd_mid = nd_mid; n->nd_aid = nd_aid; + n->call_operator_loc = *call_operator_loc; + n->message_loc = *message_loc; + n->binary_operator_loc = *binary_operator_loc; return n; } @@ -11807,30 +11941,67 @@ rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc) return n; } -static rb_node_lit_t * -rb_node_lit_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc) +static rb_node_integer_t * +rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc) { - rb_node_lit_t *n = NODE_NEWNODE(NODE_LIT, rb_node_lit_t, loc); - n->nd_lit = nd_lit; + rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc); + n->val = val; + n->minus = FALSE; + n->base = base; + + return n; +} + +static rb_node_float_t * +rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc) +{ + rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc); + n->val = val; + n->minus = FALSE; + + return n; +} + +static rb_node_rational_t * +rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc) +{ + rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc); + n->val = val; + n->minus = FALSE; + n->base = base; + n->seen_point = seen_point; + + return n; +} + +static rb_node_imaginary_t * +rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc) +{ + rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc); + n->val = val; + n->minus = FALSE; + n->base = base; + n->seen_point = seen_point; + n->type = numeric_type; return n; } static rb_node_str_t * -rb_node_str_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc) +rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc) { rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc); - n->nd_lit = nd_lit; + n->string = string; return n; } /* TODO; Use union for NODE_DSTR2 */ static rb_node_dstr_t * -rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc) +rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc) { rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc); - n->nd_lit = nd_lit; + n->string = string; n->as.nd_alen = nd_alen; n->nd_next = (rb_node_list_t *)nd_next; @@ -11838,47 +12009,71 @@ rb_node_dstr_new0(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_ } static rb_node_dstr_t * -rb_node_dstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc) +rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc) { - return rb_node_dstr_new0(p, nd_lit, 1, 0, loc); + return rb_node_dstr_new0(p, string, 1, 0, loc); } static rb_node_xstr_t * -rb_node_xstr_new(struct parser_params *p, VALUE nd_lit, const YYLTYPE *loc) +rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc) { rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc); - n->nd_lit = nd_lit; + n->string = string; return n; } static rb_node_dxstr_t * -rb_node_dxstr_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc) +rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc) { rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc); - n->nd_lit = nd_lit; - n->nd_alen = nd_alen; + n->string = string; + n->as.nd_alen = nd_alen; n->nd_next = (rb_node_list_t *)nd_next; return n; } +static rb_node_sym_t * +rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc) +{ + rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc); + n->string = rb_str_to_parser_string(p, str); + + return n; +} + static rb_node_dsym_t * -rb_node_dsym_new(struct parser_params *p, VALUE nd_lit, long nd_alen, NODE *nd_next, const YYLTYPE *loc) +rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc) { rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc); - n->nd_lit = nd_lit; - n->nd_alen = nd_alen; + n->string = string; + n->as.nd_alen = nd_alen; n->nd_next = (rb_node_list_t *)nd_next; return n; } static rb_node_evstr_t * -rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc) +rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc) { rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc); n->nd_body = nd_body; + n->opening_loc = *opening_loc; + n->closing_loc = *closing_loc; + + return n; +} + +static rb_node_regx_t * +rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc) +{ + rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc); + n->string = string; + n->options = options & RE_OPTION_MASK; + n->opening_loc = *opening_loc; + n->content_loc = *content_loc; + n->closing_loc = *closing_loc; return n; } @@ -11954,7 +12149,7 @@ rb_node_args_new(struct parser_params *p, const YYLTYPE *loc) } static rb_node_args_aux_t * -rb_node_args_aux_new(struct parser_params *p, ID nd_pid, long nd_plen, const YYLTYPE *loc) +rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc) { rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc); n->nd_pid = nd_pid; @@ -12015,40 +12210,45 @@ rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, cons } static rb_node_splat_t * -rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc) +rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc) { rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc); n->nd_head = nd_head; + n->operator_loc = *operator_loc; return n; } static rb_node_block_pass_t * -rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc) +rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc) { rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc); + n->forwarding = 0; n->nd_head = 0; n->nd_body = nd_body; + n->operator_loc = *operator_loc; return n; } static rb_node_alias_t * -rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc) +rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc); n->nd_1st = nd_1st; n->nd_2nd = nd_2nd; + n->keyword_loc = *keyword_loc; return n; } static rb_node_valias_t * -rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc) +rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc); n->nd_alias = nd_alias; n->nd_orig = nd_orig; + n->keyword_loc = *keyword_loc; return n; } @@ -12057,7 +12257,9 @@ static rb_node_undef_t * rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc) { rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc); - n->nd_undef = nd_undef; + n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1); + n->keyword_loc = NULL_LOC; + rb_parser_ary_push_node(p, n->nd_undefs, nd_undef); return n; } @@ -12071,19 +12273,23 @@ rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc) } static rb_node_defined_t * -rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc) +rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc); n->nd_head = nd_head; + n->keyword_loc = *keyword_loc; return n; } static rb_node_postexe_t * -rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc) +rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc) { rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc); n->nd_body = nd_body; + n->keyword_loc = *keyword_loc; + n->opening_loc = *opening_loc; + n->closing_loc = *closing_loc; return n; } @@ -12134,24 +12340,52 @@ rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE return n; } +static rb_node_line_t * +rb_node_line_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc); + + return n; +} + +static rb_node_file_t * +rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc) +{ + rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc); + n->path = rb_str_to_parser_string(p, str); + + return n; +} + +static rb_node_encoding_t * +rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc); + n->enc = p->enc; + + return n; +} + static rb_node_cdecl_t * -rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, const YYLTYPE *loc) +rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc) { rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc); n->nd_vid = nd_vid; n->nd_value = nd_value; n->nd_else = nd_else; + n->shareability = shareability; return n; } static 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) +rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc) { rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc); n->nd_head = nd_head; n->nd_value = nd_value; n->nd_aid = nd_aid; + n->shareability = shareability; return n; } @@ -12164,57 +12398,34 @@ rb_node_error_new(struct parser_params *p, const YYLTYPE *loc) return n; } -#else - -static rb_node_ripper_t * -rb_node_ripper_new(struct parser_params *p, ID nd_vid, VALUE nd_rval, VALUE nd_cval, const YYLTYPE *loc) -{ - rb_node_ripper_t *n = NODE_NEWNODE(NODE_RIPPER, rb_node_ripper_t, loc); - n->nd_vid = nd_vid; - n->nd_rval = nd_rval; - n->nd_cval = nd_cval; - - return n; -} - -static rb_node_ripper_values_t * -rb_node_ripper_values_new(struct parser_params *p, VALUE nd_val1, VALUE nd_val2, VALUE nd_val3, const YYLTYPE *loc) -{ - rb_node_ripper_values_t *n = NODE_NEWNODE(NODE_RIPPER_VALUES, rb_node_ripper_values_t, loc); - n->nd_val1 = nd_val1; - n->nd_val2 = nd_val2; - n->nd_val3 = nd_val3; - - return n; -} - -#endif - static rb_node_break_t * -rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc) +rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc); n->nd_stts = nd_stts; n->nd_chain = 0; + n->keyword_loc = *keyword_loc; return n; } static rb_node_next_t * -rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc) +rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc); n->nd_stts = nd_stts; n->nd_chain = 0; + n->keyword_loc = *keyword_loc; return n; } static rb_node_redo_t * -rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc) +rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc); n->nd_chain = 0; + n->keyword_loc = *keyword_loc; return n; } @@ -12223,22 +12434,23 @@ static rb_node_def_temp_t * rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc) { rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc); - n->save.cur_arg = p->cur_arg; - n->save.numparam_save = numparam_push(p); - n->save.max_numparam = p->max_numparam; + n->save.numparam_save = 0; + n->save.max_numparam = 0; n->save.ctxt = p->ctxt; -#ifdef RIPPER - n->nd_recv = Qnil; - n->nd_mid = Qnil; - n->dot_or_colon = Qnil; -#else n->nd_def = 0; n->nd_mid = 0; -#endif return n; } +static rb_node_def_temp_t * +def_head_save(struct parser_params *p, rb_node_def_temp_t *n) +{ + n->save.numparam_save = numparam_push(p); + n->save.max_numparam = p->max_numparam; + return n; +} + #ifndef RIPPER static enum node_type nodetype(NODE *node) /* for debug */ @@ -12251,6 +12463,7 @@ nodeline(NODE *node) { return nd_line(node); } +#endif static NODE* newline_node(NODE *node) @@ -12270,18 +12483,6 @@ fixpos(NODE *node, NODE *orig) nd_set_line(node, nd_line(orig)); } -static void -parser_warning(struct parser_params *p, NODE *node, const char *mesg) -{ - rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg); -} - -static void -parser_warn(struct parser_params *p, NODE *node, const char *mesg) -{ - rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg); -} - static NODE* block_append(struct parser_params *p, NODE *head, NODE *tail) { @@ -12293,7 +12494,6 @@ block_append(struct parser_params *p, NODE *head, NODE *tail) switch (nd_type(h)) { default: h = end = NEW_BLOCK(head, &head->nd_loc); - RNODE_BLOCK(end)->nd_end = end; head = end; break; case NODE_BLOCK: @@ -12308,9 +12508,7 @@ block_append(struct parser_params *p, NODE *head, NODE *tail) case NODE_NEXT: case NODE_REDO: case NODE_RETRY: - if (RTEST(ruby_verbose)) { - parser_warning(p, tail, "statement not reached"); - } + rb_warning0L(nd_line(tail), "statement not reached"); break; default: @@ -12319,7 +12517,6 @@ block_append(struct parser_params *p, NODE *head, NODE *tail) if (!nd_type_p(tail, NODE_BLOCK)) { tail = NEW_BLOCK(tail, &tail->nd_loc); - RNODE_BLOCK(tail)->nd_end = tail; } RNODE_BLOCK(end)->nd_next = tail; RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end; @@ -12378,40 +12575,53 @@ list_concat(NODE *head, NODE *tail) } static int -literal_concat0(struct parser_params *p, VALUE head, VALUE tail) +literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail) { - if (NIL_P(tail)) return 1; - if (!rb_enc_compatible(head, tail)) { + if (!tail) return 1; + if (!rb_parser_enc_compatible(p, head, tail)) { compile_error(p, "string literal encodings differ (%s / %s)", - rb_enc_name(rb_enc_get(head)), - rb_enc_name(rb_enc_get(tail))); - rb_str_resize(head, 0); - rb_str_resize(tail, 0); + rb_enc_name(rb_parser_str_get_encoding(head)), + rb_enc_name(rb_parser_str_get_encoding(tail))); + rb_parser_str_resize(p, head, 0); + rb_parser_str_resize(p, tail, 0); return 0; } - rb_str_buf_append(head, tail); + rb_parser_str_buf_append(p, head, tail); return 1; } -static VALUE +static rb_parser_string_t * string_literal_head(struct parser_params *p, enum node_type htype, NODE *head) { - if (htype != NODE_DSTR) return Qfalse; + if (htype != NODE_DSTR) return NULL; if (RNODE_DSTR(head)->nd_next) { head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head; - if (!head || !nd_type_p(head, NODE_STR)) return Qfalse; + if (!head || !nd_type_p(head, NODE_STR)) return NULL; } - const VALUE lit = RNODE_DSTR(head)->nd_lit; - ASSUME(lit != Qfalse); + rb_parser_string_t *lit = RNODE_DSTR(head)->string; + ASSUME(lit); return lit; } +#ifndef RIPPER +static rb_parser_string_t * +rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig) +{ + rb_parser_string_t *copy; + if (!orig) return NULL; + copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig)); + copy->coderange = orig->coderange; + copy->enc = orig->enc; + return copy; +} +#endif + /* concat two string literals */ static NODE * literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc) { enum node_type htype; - VALUE lit; + rb_parser_string_t *lit; if (!head) return tail; if (!tail) return head; @@ -12433,14 +12643,14 @@ literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *l } switch (nd_type(tail)) { case NODE_STR: - if ((lit = string_literal_head(p, htype, head)) != Qfalse) { + if ((lit = string_literal_head(p, htype, head)) != false) { htype = NODE_STR; } else { - lit = RNODE_DSTR(head)->nd_lit; + lit = RNODE_DSTR(head)->string; } if (htype == NODE_STR) { - if (!literal_concat0(p, lit, RNODE_STR(tail)->nd_lit)) { + if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) { error: rb_discard_node(p, head); rb_discard_node(p, tail); @@ -12455,13 +12665,15 @@ literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *l case NODE_DSTR: if (htype == NODE_STR) { - if (!literal_concat0(p, RNODE_STR(head)->nd_lit, RNODE_DSTR(tail)->nd_lit)) + if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string)) goto error; - RNODE_DSTR(tail)->nd_lit = RNODE_STR(head)->nd_lit; + rb_parser_string_free(p, RNODE_DSTR(tail)->string); + RNODE_DSTR(tail)->string = RNODE_STR(head)->string; + RNODE_STR(head)->string = NULL; rb_discard_node(p, head); head = tail; } - else if (NIL_P(RNODE_DSTR(tail)->nd_lit)) { + else if (!RNODE_DSTR(tail)->string) { append: RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1; if (!RNODE_DSTR(head)->nd_next) { @@ -12473,14 +12685,16 @@ literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *l } rb_discard_node(p, tail); } - else if ((lit = string_literal_head(p, htype, head)) != Qfalse) { - if (!literal_concat0(p, lit, RNODE_DSTR(tail)->nd_lit)) + else if ((lit = string_literal_head(p, htype, head)) != false) { + if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string)) goto error; - RNODE_DSTR(tail)->nd_lit = Qnil; + rb_parser_string_free(p, RNODE_DSTR(tail)->string); + RNODE_DSTR(tail)->string = 0; goto append; } else { - 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)); + list_concat(head, NEW_LIST2(NEW_STR(RNODE_DSTR(tail)->string, loc), RNODE_DSTR(tail)->as.nd_alen, (NODE *)RNODE_DSTR(tail)->nd_next, loc)); + RNODE_DSTR(tail)->string = 0; } break; @@ -12509,10 +12723,26 @@ str2dstr(struct parser_params *p, NODE *node) { NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t); nd_copy_flag(new_node, node); - RNODE_DSTR(new_node)->nd_lit = RNODE_STR(node)->nd_lit; + RNODE_DSTR(new_node)->string = RNODE_STR(node)->string; RNODE_DSTR(new_node)->as.nd_alen = 0; RNODE_DSTR(new_node)->nd_next = 0; - RNODE_STR(node)->nd_lit = 0; + RNODE_STR(node)->string = 0; + + return new_node; +} + +static NODE * +str2regx(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc) +{ + NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t); + nd_copy_flag(new_node, node); + RNODE_REGX(new_node)->string = RNODE_STR(node)->string; + RNODE_REGX(new_node)->options = options; + nd_set_loc(new_node, loc); + RNODE_REGX(new_node)->opening_loc = *opening_loc; + RNODE_REGX(new_node)->content_loc = *content_loc; + RNODE_REGX(new_node)->closing_loc = *closing_loc; + RNODE_STR(node)->string = 0; return new_node; } @@ -12527,7 +12757,7 @@ evstr2dstr(struct parser_params *p, NODE *node) } static NODE * -new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc) +new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc) { NODE *head = node; @@ -12541,15 +12771,13 @@ new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc) return node; } } - return NEW_EVSTR(head, loc); + return NEW_EVSTR(head, loc, opening_loc, closing_loc); } static NODE * new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc) { - VALUE lit = STR_NEW0(); - NODE *dstr = NEW_DSTR(lit, loc); - RB_OBJ_WRITTEN(p->ast, Qnil, lit); + NODE *dstr = NEW_DSTR(STRING_NEW0(), loc); return list_append(p, dstr, node); } @@ -12558,8 +12786,8 @@ call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { NODE *expr; - value_expr(recv); - value_expr(arg1); + value_expr(p, recv); + value_expr(p, arg1); expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); nd_set_line(expr, op_loc->beg_pos.lineno); return expr; @@ -12569,7 +12797,7 @@ static NODE * call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc) { NODE *opcall; - value_expr(recv); + value_expr(p, recv); opcall = NEW_OPCALL(recv, id, 0, loc); nd_set_line(opcall, op_loc->beg_pos.lineno); return opcall; @@ -12594,14 +12822,22 @@ new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *a return ret; } +static rb_locations_lambda_body_t* +new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc) +{ + rb_locations_lambda_body_t *body = xcalloc(1, sizeof(rb_locations_lambda_body_t)); + body->node = node; + body->opening_loc = *opening_loc; + body->closing_loc = *closing_loc; + return body; +} + #define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node) static NODE* last_expr_once_body(NODE *node) { if (!node) return 0; - node = last_expr_node(node); - if (!node) return 0; return nd_once_body(node); } @@ -12611,8 +12847,8 @@ match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_lo NODE *n; int line = op_loc->beg_pos.lineno; - value_expr(node1); - value_expr(node2); + value_expr(p, node1); + value_expr(p, node2); if ((n = last_expr_once_body(node1)) != 0) { switch (nd_type(n)) { @@ -12623,13 +12859,15 @@ match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_lo return match; } - case NODE_LIT: - if (RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) { - const VALUE lit = RNODE_LIT(n)->nd_lit; - NODE *match = NEW_MATCH2(node1, node2, loc); - RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc); - nd_set_line(match, line); - return match; + case NODE_REGX: + { + const VALUE lit = rb_node_regx_string_val(n); + if (!NIL_P(lit)) { + NODE *match = NEW_MATCH2(node1, node2, loc); + RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable); + nd_set_line(match, line); + return match; + } } } } @@ -12638,9 +12876,6 @@ match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_lo NODE *match3; switch (nd_type(n)) { - case NODE_LIT: - if (!RB_TYPE_P(RNODE_LIT(n)->nd_lit, T_REGEXP)) break; - /* fallthru */ case NODE_DREGX: match3 = NEW_MATCH3(node2, node1, loc); return match3; @@ -12673,16 +12908,44 @@ numparam_nested_p(struct parser_params *p) NODE *inner = local->numparam.inner; if (outer || inner) { NODE *used = outer ? outer : inner; - compile_error(p, "numbered parameter is already used in\n" - "%s:%d: %s block here", - p->ruby_sourcefile, nd_line(used), - outer ? "outer" : "inner"); + compile_error(p, "numbered parameter is already used in %s block\n" + "%s:%d: numbered parameter is already used here", + outer ? "outer" : "inner", + p->ruby_sourcefile, nd_line(used)); parser_show_error_line(p, &used->nd_loc); return 1; } return 0; } +static int +numparam_used_p(struct parser_params *p) +{ + NODE *numparam = p->lvtbl->numparam.current; + if (numparam) { + compile_error(p, "'it' is not allowed when a numbered parameter is already used\n" + "%s:%d: numbered parameter is already used here", + p->ruby_sourcefile, nd_line(numparam)); + parser_show_error_line(p, &numparam->nd_loc); + return 1; + } + return 0; +} + +static int +it_used_p(struct parser_params *p) +{ + NODE *it = p->lvtbl->it; + if (it) { + compile_error(p, "numbered parameters are not allowed when 'it' is already used\n" + "%s:%d: 'it' is already used here", + p->ruby_sourcefile, nd_line(it)); + parser_show_error_line(p, &it->nd_loc); + return 1; + } + return 0; +} + static NODE* gettable(struct parser_params *p, ID id, const YYLTYPE *loc) { @@ -12702,44 +12965,31 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) VALUE file = p->ruby_sourcefile_string; if (NIL_P(file)) file = rb_str_new(0, 0); - else - file = rb_str_dup(file); - node = NEW_STR(file, loc); - RB_OBJ_WRITTEN(p->ast, Qnil, file); + node = NEW_FILE(file, loc); } return node; case keyword__LINE__: - return NEW_LIT(INT2FIX(loc->beg_pos.lineno), loc); + return NEW_LINE(loc); case keyword__ENCODING__: - node = NEW_LIT(rb_enc_from_encoding(p->enc), loc); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit); - return node; + return NEW_ENCODING(loc); } switch (id_type(id)) { case ID_LOCAL: if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) { - if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0; - if (id == p->cur_arg) { - compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id)); - return 0; - } + if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0; if (vidp) *vidp |= LVAR_USED; node = NEW_DVAR(id, loc); return node; } if (local_id_ref(p, id, &vidp)) { - if (id == p->cur_arg) { - compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id)); - return 0; - } if (vidp) *vidp |= LVAR_USED; node = NEW_LVAR(id, loc); return node; } if (dyna_in_block(p) && NUMPARAM_ID_P(id) && parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) { - if (numparam_nested_p(p)) return 0; + if (numparam_nested_p(p) || it_used_p(p)) return 0; node = NEW_DVAR(id, loc); struct local_vars *local = p->lvtbl; if (!local->numparam.current) local->numparam.current = node; @@ -12751,6 +13001,20 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) } # endif /* method call without arguments */ + if (dyna_in_block(p) && id == idIt && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) { + if (numparam_used_p(p)) return 0; + if (p->max_numparam == ORDINAL_PARAM) { + compile_error(p, "ordinary parameter is defined"); + return 0; + } + if (!p->it_id) { + p->it_id = idItImplicit; + vtable_add(p->lvtbl->args, p->it_id); + } + NODE *node = NEW_DVAR(p->it_id, loc); + if (!p->lvtbl->it) p->lvtbl->it = node; + return node; + } return NEW_VCALL(id, loc); case ID_GLOBAL: return NEW_GVAR(id, loc); @@ -12791,9 +13055,45 @@ kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw) } static NODE * -new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc) +new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_loc) { - return NEW_DEFINED(remove_begin_all(expr), loc); + int had_trailing_semicolon = p->ctxt.has_trailing_semicolon; + p->ctxt.has_trailing_semicolon = 0; + + NODE *n = expr; + while (n) { + if (nd_type_p(n, NODE_BEGIN)) { + n = RNODE_BEGIN(n)->nd_body; + } + else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) { + n = RNODE_BLOCK(n)->nd_head; + } + else { + break; + } + } + + if (had_trailing_semicolon && !nd_type_p(expr, NODE_BLOCK)) { + NODE *block = NEW_BLOCK(expr, loc); + return NEW_DEFINED(block, loc, keyword_loc); + } + + return NEW_DEFINED(n, loc, keyword_loc); +} + +static NODE* +str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc) +{ + VALUE lit; + rb_parser_string_t *str = RNODE_STR(node)->string; + if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) { + yyerror1(loc, "invalid symbol"); + lit = STR_NEW0(); + } + else { + lit = rb_str_new_parser_string(str); + } + return NEW_SYM(lit, loc); } static NODE* @@ -12805,8 +13105,7 @@ symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol) nd_set_type(symbol, NODE_DSYM); break; case NODE_STR: - nd_set_type(symbol, NODE_LIT); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(symbol)->nd_lit = rb_str_intern(RNODE_LIT(symbol)->nd_lit)); + symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc); break; default: compile_error(p, "unexpected node as symbol: %s", parser_node_name(type)); @@ -12814,65 +13113,51 @@ symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol) return list_append(p, symbols, symbol); } -static NODE * -new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc) +static void +dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options) { - struct RNode_LIST *list; - NODE *prev; - VALUE lit; + if (dreg->string) { + reg_fragment_setenc(p, dreg->string, options); + } + for (struct RNode_LIST *list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) { + NODE *frag = list->nd_head; + if (nd_type_p(frag, NODE_STR)) { + reg_fragment_setenc(p, RNODE_STR(frag)->string, options); + } + else if (nd_type_p(frag, NODE_DSTR)) { + dregex_fragment_setenc(p, RNODE_DSTR(frag), options); + } + } +} +static NODE * +new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *content_loc, const YYLTYPE *closing_loc) +{ if (!node) { - node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit); + /* Check string is valid regex */ + rb_parser_string_t *str = STRING_NEW0(); + reg_compile(p, str, options); + node = NEW_REGX(str, options, loc, opening_loc, content_loc, closing_loc); return node; } switch (nd_type(node)) { case NODE_STR: { - VALUE src = RNODE_STR(node)->nd_lit; - nd_set_type(node, NODE_LIT); - nd_set_loc(node, loc); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit = reg_compile(p, src, options)); + /* Check string is valid regex */ + reg_compile(p, RNODE_STR(node)->string, options); + node = str2regx(p, node, options, loc, opening_loc, content_loc, closing_loc); } break; default: - lit = STR_NEW0(); - node = NEW_DSTR0(lit, 1, NEW_LIST(node, loc), loc); - RB_OBJ_WRITTEN(p->ast, Qnil, lit); + node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc); /* fall through */ case NODE_DSTR: nd_set_type(node, NODE_DREGX); nd_set_loc(node, loc); - RNODE_DREGX(node)->nd_cflag = options & RE_OPTION_MASK; - if (!NIL_P(RNODE_DREGX(node)->nd_lit)) reg_fragment_check(p, RNODE_DREGX(node)->nd_lit, options); - for (list = RNODE_DREGX(prev = node)->nd_next; list; list = RNODE_LIST(list->nd_next)) { - NODE *frag = list->nd_head; - enum node_type type = nd_type(frag); - if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) { - VALUE tail = RNODE_STR(frag)->nd_lit; - if (reg_fragment_check(p, tail, options) && prev && !NIL_P(RNODE_DREGX(prev)->nd_lit)) { - VALUE lit = prev == node ? RNODE_DREGX(prev)->nd_lit : RNODE_LIT(RNODE_LIST(prev)->nd_head)->nd_lit; - if (!literal_concat0(p, lit, tail)) { - return NEW_NIL(loc); /* dummy node on error */ - } - rb_str_resize(tail, 0); - RNODE_LIST(prev)->nd_next = list->nd_next; - rb_discard_node(p, list->nd_head); - rb_discard_node(p, (NODE *)list); - list = RNODE_LIST(prev); - } - else { - prev = (NODE *)list; - } - } - else { - prev = 0; - } - } - if (!RNODE_DREGX(node)->nd_next) { - VALUE src = RNODE_DREGX(node)->nd_lit; - nd_set_type(node, NODE_LIT); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(node)->nd_lit = reg_compile(p, src, options)); + rb_node_dregx_t *const dreg = RNODE_DREGX(node); + dreg->as.nd_cflag = options & RE_OPTION_MASK; + if (dreg->nd_next) { + dregex_fragment_setenc(p, dreg, options); } if (options & RE_OPTION_ONCE) { node = NEW_ONCE(node, loc); @@ -12893,9 +13178,7 @@ static NODE * new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc) { if (!node) { - VALUE lit = STR_NEW0(); - NODE *xstr = NEW_XSTR(lit, loc); - RB_OBJ_WRITTEN(p->ast, Qnil, lit); + NODE *xstr = NEW_XSTR(STRING_NEW0(), loc); return xstr; } switch (nd_type(node)) { @@ -12908,40 +13191,42 @@ new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc) nd_set_loc(node, loc); break; default: - node = NEW_DXSTR(Qnil, 1, NEW_LIST(node, loc), loc); + node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc); break; } return node; } +static const +struct st_hash_type literal_type = { + literal_cmp, + literal_hash, +}; + +static int nd_type_st_key_enable_p(NODE *node); + static void check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc) { - VALUE lit; - + /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */ if (!arg || !p->case_labels) return; + if (!nd_type_st_key_enable_p(arg)) return; - lit = rb_node_case_when_optimizable_literal(arg); - if (UNDEF_P(lit)) return; - if (nd_type_p(arg, NODE_STR)) { - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(arg)->nd_lit = lit); - } - - if (NIL_P(p->case_labels)) { - p->case_labels = rb_obj_hide(rb_hash_new()); + if (p->case_labels == CHECK_LITERAL_WHEN) { + p->case_labels = st_init_table(&literal_type); } else { - VALUE line = rb_hash_lookup(p->case_labels, lit); - if (!NIL_P(line)) { - rb_warning1("duplicated `when' clause with line %d is ignored", - WARN_IVAL(line)); + st_data_t line; + if (st_lookup(p->case_labels, (st_data_t)arg, &line)) { + rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored", + WARN_I((int)nd_line(arg)), WARN_I((int)line)); return; } } - rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline)); + st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline); } -#else /* !RIPPER */ +#ifdef RIPPER static int id_is_var(struct parser_params *p, ID id) { @@ -12961,26 +13246,7 @@ id_is_var(struct parser_params *p, ID id) compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id)); return 0; } - -static VALUE -new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc) -{ - VALUE src = 0, err = 0; - int options = 0; - if (ripper_is_node_yylval(p, re)) { - src = RNODE_RIPPER(re)->nd_cval; - re = RNODE_RIPPER(re)->nd_rval; - } - if (ripper_is_node_yylval(p, opt)) { - options = (int)RNODE_RIPPER(opt)->nd_vid; - opt = RNODE_RIPPER(opt)->nd_rval; - } - if (src && NIL_P(parser_reg_compile(p, src, options, &err))) { - compile_error(p, "%"PRIsVALUE, err); - } - return dispatch2(regexp_literal, re, opt); -} -#endif /* !RIPPER */ +#endif static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line) @@ -12992,6 +13258,20 @@ parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line) } #ifndef RIPPER +static void +flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str) +{ + VALUE mesg = p->debug_buffer; + + if (!NIL_P(mesg) && RSTRING_LEN(mesg)) { + p->debug_buffer = Qnil; + rb_io_puts(1, &mesg, out); + } + if (!NIL_P(str) && RSTRING_LEN(str)) { + rb_io_write(p->debug_output, str); + } +} + static const char rb_parser_lex_state_names[][8] = { "BEG", "END", "ENDARG", "ENDFN", "ARG", "CMDARG", "MID", "FNAME", "DOT", "CLASS", @@ -13020,20 +13300,6 @@ append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf return buf; } -static void -flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str) -{ - VALUE mesg = p->debug_buffer; - - if (!NIL_P(mesg) && RSTRING_LEN(mesg)) { - p->debug_buffer = Qnil; - rb_io_puts(1, &mesg, out); - } - if (!NIL_P(str) && RSTRING_LEN(str)) { - rb_io_write(p->debug_output, str); - } -} - enum lex_state_e rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from, enum lex_state_e to, int line) @@ -13051,7 +13317,7 @@ rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from, VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state) { - return rb_fstring(append_lex_state_name(p, state, rb_str_new(0, 0))); + return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0))); } static void @@ -13230,7 +13496,6 @@ assignable0(struct parser_params *p, ID id, const char **err) return -1; } -#ifndef RIPPER static NODE* assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc) { @@ -13241,22 +13506,17 @@ assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc) case NODE_LASGN: return NEW_LASGN(id, val, loc); case NODE_GASGN: return NEW_GASGN(id, val, loc); case NODE_IASGN: return NEW_IASGN(id, val, loc); - case NODE_CDECL: return NEW_CDECL(id, val, 0, loc); + case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc); case NODE_CVASGN: return NEW_CVASGN(id, val, loc); } +/* TODO: FIXME */ +#ifndef RIPPER if (err) yyerror1(loc, err); - return NEW_BEGIN(0, loc); -} #else -static VALUE -assignable(struct parser_params *p, VALUE lhs) -{ - const char *err = 0; - assignable0(p, get_id(lhs), &err); - if (err) lhs = assign_error(p, err, lhs); - return lhs; -} + if (err) set_value(assign_error(p, err, p->s_lvalue)); #endif + return NEW_ERROR(loc); +} static int is_private_local_id(struct parser_params *p, ID name) @@ -13312,12 +13572,44 @@ new_bv(struct parser_params *p, ID name) } if (!shadowing_lvar_0(p, name)) return; dyna_var(p, name); + ID *vidp = 0; + if (dvar_defined_ref(p, name, &vidp)) { + if (vidp) *vidp |= LVAR_USED; + } +} + +static void +aryset_check(struct parser_params *p, NODE *args) +{ + NODE *block = 0, *kwds = 0; + if (args && nd_type_p(args, NODE_BLOCK_PASS)) { + block = RNODE_BLOCK_PASS(args)->nd_body; + args = RNODE_BLOCK_PASS(args)->nd_head; + } + if (args && nd_type_p(args, NODE_ARGSCAT)) { + args = RNODE_ARGSCAT(args)->nd_body; + } + if (args && nd_type_p(args, NODE_ARGSPUSH)) { + kwds = RNODE_ARGSPUSH(args)->nd_body; + } + else { + for (NODE *next = args; next && nd_type_p(next, NODE_LIST); + next = RNODE_LIST(next)->nd_next) { + kwds = RNODE_LIST(next)->nd_head; + } + } + if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) { + yyerror1(&kwds->nd_loc, "keyword arg given in index assignment"); + } + if (block) { + yyerror1(&block->nd_loc, "block arg given in index assignment"); + } } -#ifndef RIPPER static NODE * aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc) { + aryset_check(p, idx); return NEW_ATTRASGN(recv, tASET, idx, loc); } @@ -13336,29 +13628,24 @@ attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc return NEW_ATTRASGN(recv, id, 0, loc); } -static void +static VALUE rb_backref_error(struct parser_params *p, NODE *node) { +#ifndef RIPPER +# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue) +#else +# define ERR(...) rb_sprintf(__VA_ARGS__) +#endif switch (nd_type(node)) { case NODE_NTH_REF: - compile_error(p, "Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth); - break; + return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth); case NODE_BACK_REF: - compile_error(p, "Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth); - break; + return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth); } +#undef ERR + UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */ } -#else -static VALUE -backref_error(struct parser_params *p, NODE *ref, VALUE expr) -{ - VALUE mesg = rb_str_new_cstr("Can't set variable "); - rb_str_append(mesg, RNODE_RIPPER(ref)->nd_cval); - return dispatch2(assign_error, mesg, expr); -} -#endif -#ifndef RIPPER static NODE * arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc) { @@ -13463,250 +13750,8 @@ mark_lvar_used(struct parser_params *p, NODE *rhs) } } -static NODE * -const_decl_path(struct parser_params *p, NODE **dest) -{ - NODE *n = *dest; - if (!nd_type_p(n, NODE_CALL)) { - const YYLTYPE *loc = &n->nd_loc; - VALUE path; - if (RNODE_CDECL(n)->nd_vid) { - path = rb_id2str(RNODE_CDECL(n)->nd_vid); - } - else { - n = RNODE_CDECL(n)->nd_else; - path = rb_ary_new(); - for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) { - rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid)); - } - if (n && nd_type_p(n, NODE_CONST)) { - // Const::Name - rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid)); - } - else if (n && nd_type_p(n, NODE_COLON3)) { - // ::Const::Name - rb_ary_push(path, rb_str_new(0, 0)); - } - else { - // expression::Name - rb_ary_push(path, rb_str_new_cstr("...")); - } - path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::")); - path = rb_fstring(path); - } - *dest = n = NEW_LIT(path, loc); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit); - } - return n; -} - -static NODE * -make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc) -{ - NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc); - - if (copy) { - return NEW_CALL(fcore, rb_intern("make_shareable_copy"), - NEW_LIST(value, loc), loc); - } - else { - return NEW_CALL(fcore, rb_intern("make_shareable"), - NEW_LIST(value, loc), loc); - } -} - -static NODE * -ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc) -{ - NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc); - NODE *args = NEW_LIST(value, loc); - args = list_append(p, args, const_decl_path(p, dest)); - return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc); -} - static int is_static_content(NODE *node); -static VALUE -shareable_literal_value(struct parser_params *p, NODE *node) -{ - if (!node) return Qnil; - enum node_type type = nd_type(node); - switch (type) { - case NODE_TRUE: - return Qtrue; - case NODE_FALSE: - return Qfalse; - case NODE_NIL: - return Qnil; - case NODE_LIT: - return RNODE_LIT(node)->nd_lit; - default: - return Qundef; - } -} - -#ifndef SHAREABLE_BARE_EXPRESSION -#define SHAREABLE_BARE_EXPRESSION 1 -#endif - -static NODE * -shareable_literal_constant(struct parser_params *p, enum shareability shareable, - NODE **dest, NODE *value, const YYLTYPE *loc, size_t level) -{ -# define shareable_literal_constant_next(n) \ - shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1) - VALUE lit = Qnil; - - if (!value) return 0; - enum node_type type = nd_type(value); - switch (type) { - case NODE_TRUE: - case NODE_FALSE: - case NODE_NIL: - case NODE_LIT: - return value; - - case NODE_DSTR: - if (shareable == shareable_literal) { - value = NEW_CALL(value, idUMinus, 0, loc); - } - return value; - - case NODE_STR: - lit = rb_fstring(RNODE_STR(value)->nd_lit); - nd_set_type(value, NODE_LIT); - RB_OBJ_WRITE(p->ast, &RNODE_LIT(value)->nd_lit, lit); - return value; - - case NODE_ZLIST: - lit = rb_ary_new(); - OBJ_FREEZE_RAW(lit); - NODE *n = NEW_LIT(lit, loc); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit); - return n; - - case NODE_LIST: - lit = rb_ary_new(); - for (NODE *n = value; n; n = RNODE_LIST(n)->nd_next) { - NODE *elt = RNODE_LIST(n)->nd_head; - if (elt) { - elt = shareable_literal_constant_next(elt); - if (elt) { - RNODE_LIST(n)->nd_head = elt; - } - else if (RTEST(lit)) { - rb_ary_clear(lit); - lit = Qfalse; - } - } - if (RTEST(lit)) { - VALUE e = shareable_literal_value(p, elt); - if (!UNDEF_P(e)) { - rb_ary_push(lit, e); - } - else { - rb_ary_clear(lit); - lit = Qnil; /* make shareable at runtime */ - } - } - } - break; - - case NODE_HASH: - if (!RNODE_HASH(value)->nd_brace) return 0; - lit = rb_hash_new(); - for (NODE *n = RNODE_HASH(value)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) { - NODE *key = RNODE_LIST(n)->nd_head; - NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head; - if (key) { - key = shareable_literal_constant_next(key); - if (key) { - RNODE_LIST(n)->nd_head = key; - } - else if (RTEST(lit)) { - rb_hash_clear(lit); - lit = Qfalse; - } - } - if (val) { - val = shareable_literal_constant_next(val); - if (val) { - RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head = val; - } - else if (RTEST(lit)) { - rb_hash_clear(lit); - lit = Qfalse; - } - } - if (RTEST(lit)) { - VALUE k = shareable_literal_value(p, key); - VALUE v = shareable_literal_value(p, val); - if (!UNDEF_P(k) && !UNDEF_P(v)) { - rb_hash_aset(lit, k, v); - } - else { - rb_hash_clear(lit); - lit = Qnil; /* make shareable at runtime */ - } - } - } - break; - - default: - if (shareable == shareable_literal && - (SHAREABLE_BARE_EXPRESSION || level > 0)) { - return ensure_shareable_node(p, dest, value, loc); - } - return 0; - } - - /* Array or Hash */ - if (!lit) return 0; - if (NIL_P(lit)) { - // if shareable_literal, all elements should have been ensured - // as shareable - value = make_shareable_node(p, value, false, loc); - } - else { - value = NEW_LIT(rb_ractor_make_shareable(lit), loc); - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(value)->nd_lit); - } - - return value; -# undef shareable_literal_constant_next -} - -static NODE * -shareable_constant_value(struct parser_params *p, enum shareability shareable, - NODE *lhs, NODE *value, const YYLTYPE *loc) -{ - if (!value) return 0; - switch (shareable) { - case shareable_none: - return value; - - case shareable_literal: - { - NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0); - if (lit) return lit; - return value; - } - break; - - case shareable_copy: - case shareable_everything: - { - NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0); - if (lit) return lit; - return make_shareable_node(p, value, shareable == shareable_copy, loc); - } - break; - - default: - UNREACHABLE_RETURN(0); - } -} - static NODE * node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc) { @@ -13714,9 +13759,6 @@ node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ct switch (nd_type(lhs)) { case NODE_CDECL: - rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc); - /* fallthru */ - case NODE_GASGN: case NODE_IASGN: case NODE_LASGN: @@ -13750,26 +13792,80 @@ value_expr_check(struct parser_params *p, NODE *node) } while (node) { switch (nd_type(node)) { + case NODE_ENSURE: + vn = RNODE_ENSURE(node)->nd_head; + node = RNODE_ENSURE(node)->nd_ensr; + /* nd_ensr should not be NULL, check it out next */ + if (vn && (vn = value_expr_check(p, vn))) { + goto found; + } + break; + + case NODE_RESCUE: + /* void only if all children are void */ + vn = RNODE_RESCUE(node)->nd_head; + if (!vn || !(vn = value_expr_check(p, vn))) { + if (!RNODE_RESCUE(node)->nd_else) return NULL; + } + if (!void_node) void_node = vn; + for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) { + if (!nd_type_p(r, NODE_RESBODY)) { + compile_error(p, "unexpected node"); + return NULL; + } + if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) { + return NULL; + } + if (!void_node) void_node = vn; + } + node = RNODE_RESCUE(node)->nd_else; + if (!node) return void_node; + break; + case NODE_RETURN: case NODE_BREAK: case NODE_NEXT: case NODE_REDO: case NODE_RETRY: - return void_node ? void_node : node; + goto found; - case NODE_CASE3: - if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) { - compile_error(p, "unexpected node"); - return NULL; + case NODE_CASE: + case NODE_CASE2: + for (node = RNODE_CASE(node)->nd_body; + node && nd_type_p(node, NODE_WHEN); + node = RNODE_WHEN(node)->nd_next) { + if (!(vn = value_expr_check(p, RNODE_WHEN(node)->nd_body))) { + return NULL; + } + if (!void_node) void_node = vn; } - if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) { - return NULL; + break; + + case NODE_CASE3: + { + NODE *in = RNODE_CASE3(node)->nd_body; + if (!in || !nd_type_p(in, NODE_IN)) { + compile_error(p, "unexpected node"); + return NULL; + } + if (!RNODE_IN(in)->nd_body) { + /* single line pattern matching with "=>" operator */ + goto found; + } + do { + vn = value_expr_check(p, RNODE_IN(in)->nd_body); + if (!vn) return NULL; + if (!void_node) void_node = vn; + in = RNODE_IN(in)->nd_next; + } while (in && nd_type_p(in, NODE_IN)); + node = in; /* else */ } - /* single line pattern matching with "=>" operator */ - return void_node ? void_node : node; + break; case NODE_BLOCK: while (RNODE_BLOCK(node)->nd_next) { + vn = value_expr_check(p, RNODE_BLOCK(node)->nd_head); + if (vn) return vn; node = RNODE_BLOCK(node)->nd_next; } node = RNODE_BLOCK(node)->nd_head; @@ -13810,10 +13906,14 @@ value_expr_check(struct parser_params *p, NODE *node) } return NULL; + + found: + /* return the first found node */ + return void_node ? void_node : node; } static int -value_expr_gen(struct parser_params *p, NODE *node) +value_expr(struct parser_params *p, NODE *node) { NODE *void_node = value_expr_check(p, node); if (void_node) { @@ -13870,9 +13970,17 @@ void_expr(struct parser_params *p, NODE *node) case NODE_CONST: useless = "a constant"; break; - case NODE_LIT: + case NODE_SYM: + case NODE_LINE: + case NODE_FILE: + case NODE_ENCODING: + case NODE_INTEGER: + case NODE_FLOAT: + case NODE_RATIONAL: + case NODE_IMAGINARY: case NODE_STR: case NODE_DSTR: + case NODE_REGX: case NODE_DREGX: useless = "a literal"; break; @@ -13908,6 +14016,7 @@ void_expr(struct parser_params *p, NODE *node) } } +/* warns useless use of block and returns the last statement node */ static NODE * void_stmts(struct parser_params *p, NODE *node) { @@ -13920,7 +14029,7 @@ void_stmts(struct parser_params *p, NODE *node) void_expr(p, RNODE_BLOCK(node)->nd_head); node = RNODE_BLOCK(node)->nd_next; } - return n; + return RNODE_BLOCK(node)->nd_head; } static NODE * @@ -13933,16 +14042,6 @@ remove_begin(NODE *node) return node; } -static NODE * -remove_begin_all(NODE *node) -{ - NODE **n = &node, *n1 = node; - while (n1 && nd_type_p(n1, NODE_BEGIN)) { - *n = n1 = RNODE_BEGIN(n1)->nd_body; - } - return node; -} - static void reduce_nodes(struct parser_params *p, NODE **body) { @@ -13958,16 +14057,12 @@ reduce_nodes(struct parser_params *p, NODE **body) (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1)) while (node) { - int newline = (int)(nd_fl_newline(node)); + int newline = (int)nd_fl_newline(node); switch (nd_type(node)) { end: case NODE_NIL: *body = 0; return; - case NODE_RETURN: - *body = node = RNODE_RETURN(node)->nd_stts; - if (newline && node) nd_set_fl_newline(node); - continue; case NODE_BEGIN: *body = node = RNODE_BEGIN(node)->nd_body; if (newline && node) nd_set_fl_newline(node); @@ -13986,7 +14081,7 @@ reduce_nodes(struct parser_params *p, NODE **body) if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end; break; case NODE_ENSURE: - if (!subnodes(RNODE_ENSURE, nd_head, nd_resq)) goto end; + body = &RNODE_ENSURE(node)->nd_head; break; case NODE_RESCUE: newline = 0; // RESBODY should not be a NEWLINE @@ -14017,7 +14112,15 @@ is_static_content(NODE *node) do { if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0; } while ((node = RNODE_LIST(node)->nd_next) != 0); - case NODE_LIT: + case NODE_SYM: + case NODE_REGX: + case NODE_LINE: + case NODE_FILE: + case NODE_ENCODING: + case NODE_INTEGER: + case NODE_FLOAT: + case NODE_RATIONAL: + case NODE_IMAGINARY: case NODE_STR: case NODE_NIL: case NODE_TRUE: @@ -14050,7 +14153,7 @@ assign_in_cond(struct parser_params *p, NODE *node) if (!get_nd_value(p, node)) return 1; if (is_static_content(get_nd_value(p, node))) { /* reports always */ - parser_warn(p, get_nd_value(p, node), "found `= literal' in conditional, should be =="); + rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be =="); } return 1; } @@ -14069,7 +14172,7 @@ enum cond_type { } \ } while (0) -static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*); +static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool); static NODE* range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc) @@ -14079,72 +14182,92 @@ range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc) if (node == 0) return 0; type = nd_type(node); - value_expr(node); - if (type == NODE_LIT && FIXNUM_P(RNODE_LIT(node)->nd_lit)) { - if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop"); + value_expr(p, node); + if (type == NODE_INTEGER) { + if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop"); ID lineno = rb_intern("$."); return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc); } - return cond0(p, node, COND_IN_FF, loc); + return cond0(p, node, COND_IN_FF, loc, true); } static NODE* -cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc) +cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top) { if (node == 0) return 0; if (!(node = nd_once_body(node))) return 0; assign_in_cond(p, node); switch (nd_type(node)) { + case NODE_BEGIN: + RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top); + break; + case NODE_DSTR: case NODE_EVSTR: case NODE_STR: + case NODE_FILE: SWITCH_BY_COND_TYPE(type, warn, "string "); break; + case NODE_REGX: + if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex "); + nd_set_type(node, NODE_MATCH); + break; + case NODE_DREGX: if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex "); return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc); case NODE_BLOCK: - RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head = cond0(p, RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head, type, loc); + { + NODE *end = RNODE_BLOCK(node)->nd_end; + NODE **expr = &RNODE_BLOCK(end)->nd_head; + if (top) top = node == end; + *expr = cond0(p, *expr, type, loc, top); + } break; case NODE_AND: case NODE_OR: - RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc); - RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc); + RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true); + RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true); break; case NODE_DOT2: case NODE_DOT3: + if (!top) break; RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc); RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc); - if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2); - else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3); + switch (nd_type(node)) { + case NODE_DOT2: + nd_set_type(node,NODE_FLIP2); + rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */ + (void)flip2; + break; + case NODE_DOT3: + nd_set_type(node, NODE_FLIP3); + rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */ + (void)flip3; + break; + } break; + case NODE_SYM: case NODE_DSYM: - warn_symbol: SWITCH_BY_COND_TYPE(type, warning, "symbol "); break; - case NODE_LIT: - if (RB_TYPE_P(RNODE_LIT(node)->nd_lit, T_REGEXP)) { - if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex "); - nd_set_type(node, NODE_MATCH); - } - else if (RNODE_LIT(node)->nd_lit == Qtrue || - RNODE_LIT(node)->nd_lit == Qfalse) { - /* booleans are OK, e.g., while true */ - } - else if (SYMBOL_P(RNODE_LIT(node)->nd_lit)) { - goto warn_symbol; - } - else { - SWITCH_BY_COND_TYPE(type, warning, ""); - } + case NODE_LINE: + case NODE_ENCODING: + case NODE_INTEGER: + case NODE_FLOAT: + case NODE_RATIONAL: + case NODE_IMAGINARY: + SWITCH_BY_COND_TYPE(type, warning, ""); + break; + default: break; } @@ -14155,14 +14278,14 @@ static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc) { if (node == 0) return 0; - return cond0(p, node, COND_IN_COND, loc); + return cond0(p, node, COND_IN_COND, loc, true); } static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc) { if (node == 0) return 0; - return cond0(p, node, COND_IN_OP, loc); + return cond0(p, node, COND_IN_OP, loc, true); } static NODE* @@ -14173,22 +14296,22 @@ new_nil_at(struct parser_params *p, const rb_code_position_t *pos) } static NODE* -new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc) +new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc, const YYLTYPE* if_keyword_loc, const YYLTYPE* then_keyword_loc, const YYLTYPE* end_keyword_loc) { if (!cc) return right; - cc = cond0(p, cc, COND_IN_COND, loc); - return newline_node(NEW_IF(cc, left, right, loc)); + cc = cond0(p, cc, COND_IN_COND, loc, true); + return newline_node(NEW_IF(cc, left, right, loc, if_keyword_loc, then_keyword_loc, end_keyword_loc)); } static NODE* -new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc) +new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc) { if (!cc) return right; - cc = cond0(p, cc, COND_IN_COND, loc); - return newline_node(NEW_UNLESS(cc, left, right, loc)); + cc = cond0(p, cc, COND_IN_COND, loc, true); + return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc)); } -#define NEW_AND_OR(type, f, s, loc) (type == NODE_AND ? NEW_AND(f,s,loc) : NEW_OR(f,s,loc)) +#define NEW_AND_OR(type, f, s, loc, op_loc) (type == NODE_AND ? NEW_AND(f,s,loc,op_loc) : NEW_OR(f,s,loc,op_loc)) static NODE* logop(struct parser_params *p, ID id, NODE *left, NODE *right, @@ -14196,18 +14319,18 @@ logop(struct parser_params *p, ID id, NODE *left, NODE *right, { enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR; NODE *op; - value_expr(left); + value_expr(p, left); if (left && nd_type_p(left, type)) { NODE *node = left, *second; while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) { node = second; } - RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc); + RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc); nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno); left->nd_loc.end_pos = loc->end_pos; return left; } - op = NEW_AND_OR(type, left, right, loc); + op = NEW_AND_OR(type, left, right, loc, op_loc); nd_set_line(op, op_loc->beg_pos.lineno); return op; } @@ -14234,50 +14357,25 @@ ret_args(struct parser_params *p, NODE *node) return node; } -static NODE * -new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc) -{ - if (node) no_blockarg(p, node); - - return NEW_YIELD(node, loc); -} - -static VALUE -negate_lit(struct parser_params *p, VALUE lit) +static NODE* +negate_lit(struct parser_params *p, NODE* node, const YYLTYPE *loc) { - if (FIXNUM_P(lit)) { - return LONG2FIX(-FIX2LONG(lit)); - } - if (SPECIAL_CONST_P(lit)) { -#if USE_FLONUM - if (FLONUM_P(lit)) { - return DBL2NUM(-RFLOAT_VALUE(lit)); - } -#endif - goto unknown; - } - switch (BUILTIN_TYPE(lit)) { - case T_BIGNUM: - bignum_negate(lit); - lit = rb_big_norm(lit); - break; - case T_RATIONAL: - rational_set_num(lit, negate_lit(p, rational_get_num(lit))); + switch (nd_type(node)) { + case NODE_INTEGER: + RNODE_INTEGER(node)->minus = TRUE; break; - case T_COMPLEX: - rcomplex_set_real(lit, negate_lit(p, rcomplex_get_real(lit))); - rcomplex_set_imag(lit, negate_lit(p, rcomplex_get_imag(lit))); + case NODE_FLOAT: + RNODE_FLOAT(node)->minus = TRUE; break; - case T_FLOAT: - lit = DBL2NUM(-RFLOAT_VALUE(lit)); + case NODE_RATIONAL: + RNODE_RATIONAL(node)->minus = TRUE; break; - unknown: - default: - rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit", - rb_builtin_class_name(lit)); + case NODE_IMAGINARY: + RNODE_IMAGINARY(node)->minus = TRUE; break; } - return lit; + node->nd_loc = *loc; + return node; } static NODE * @@ -14319,10 +14417,10 @@ new_args(struct parser_params *p, rb_node_args_aux_t *pre_args, rb_node_opt_arg_ rest_arg = idFWD_REST; } - args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0; + args->pre_args_num = pre_args ? pre_args->nd_plen : 0; args->pre_init = pre_args ? pre_args->nd_next : 0; - args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0; + args->post_args_num = post_args ? post_args->nd_plen : 0; args->post_init = post_args ? post_args->nd_next : 0; args->first_post_arg = post_args ? post_args->nd_pid : 0; @@ -14330,12 +14428,6 @@ new_args(struct parser_params *p, rb_node_args_aux_t *pre_args, rb_node_opt_arg_ args->opt_args = opt_args; -#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS - args->ruby2_keywords = args->forwarding; -#else - args->ruby2_keywords = 0; -#endif - nd_set_loc(RNODE(tail), loc); return tail; @@ -14348,6 +14440,10 @@ new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg struct rb_args_info *args = &node->nd_ainfo; if (p->error_p) return node; + if (block == idNil) { + block = 0; + args->no_blockarg = TRUE; + } args->block_arg = block; args->kw_args = kw_args; @@ -14399,15 +14495,15 @@ new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg } static rb_node_args_t * -args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam) +args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id) { - if (max_numparam > NO_PARAM) { + if (max_numparam > NO_PARAM || it_id) { if (!args) { YYLTYPE loc = RUBY_INIT_YYLLOC(); - args = new_args_tail(p, 0, 0, 0, 0); + args = new_empty_args_tail(p, 0); nd_set_loc(RNODE(args), &loc); } - args->nd_ainfo.pre_args_num = max_numparam; + args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam; } return args; } @@ -14491,10 +14587,8 @@ new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, co static NODE* dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc) { - VALUE lit; - if (!node) { - return NEW_LIT(ID2SYM(idNULL), loc); + return NEW_SYM(STR_NEW0(), loc); } switch (nd_type(node)) { @@ -14503,89 +14597,104 @@ dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc) nd_set_loc(node, loc); break; case NODE_STR: - lit = RNODE_STR(node)->nd_lit; - RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_STR(node)->nd_lit = ID2SYM(rb_intern_str(lit))); - nd_set_type(node, NODE_LIT); - nd_set_loc(node, loc); + node = str_to_sym_node(p, node, loc); break; default: - node = NEW_DSYM(Qnil, 1, NEW_LIST(node, loc), loc); + node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc); break; } return node; } static int -append_literal_keys(st_data_t k, st_data_t v, st_data_t h) -{ - NODE *node = (NODE *)v; - NODE **result = (NODE **)h; - RNODE_LIST(node)->as.nd_alen = 2; - RNODE_LIST(RNODE_LIST(node)->nd_next)->as.nd_end = RNODE_LIST(node)->nd_next; - RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next = 0; - if (*result) - list_concat(*result, node); - else - *result = node; - return ST_CONTINUE; +nd_type_st_key_enable_p(NODE *node) +{ + switch (nd_type(node)) { + case NODE_INTEGER: + case NODE_FLOAT: + case NODE_RATIONAL: + case NODE_IMAGINARY: + case NODE_STR: + case NODE_SYM: + case NODE_REGX: + case NODE_LINE: + case NODE_FILE: + case NODE_ENCODING: + return true; + default: + return false; + } } -static NODE * -remove_duplicate_keys(struct parser_params *p, NODE *hash) +static VALUE +nd_value(struct parser_params *p, NODE *node) { - struct st_hash_type literal_type = { - literal_cmp, - literal_hash, - }; + switch (nd_type(node)) { + case NODE_STR: + return rb_node_str_string_val(node); + case NODE_INTEGER: + return rb_node_integer_literal_val(node); + case NODE_FLOAT: + return rb_node_float_literal_val(node); + case NODE_RATIONAL: + return rb_node_rational_literal_val(node); + case NODE_IMAGINARY: + return rb_node_imaginary_literal_val(node); + case NODE_SYM: + return rb_node_sym_string_val(node); + case NODE_REGX: + return rb_node_regx_string_val(node); + case NODE_LINE: + return rb_node_line_lineno_val(node); + case NODE_ENCODING: + return rb_node_encoding_val(node); + case NODE_FILE: + return rb_node_file_path_val(node); + default: + rb_bug("unexpected node: %s", ruby_node_name(nd_type(node))); + UNREACHABLE_RETURN(0); + } +} - st_table *literal_keys = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2); - NODE *result = 0; - NODE *last_expr = 0; - rb_code_location_t loc = hash->nd_loc; +static void +warn_duplicate_keys(struct parser_params *p, NODE *hash) +{ + /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */ + p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2); while (hash && RNODE_LIST(hash)->nd_next) { NODE *head = RNODE_LIST(hash)->nd_head; NODE *value = RNODE_LIST(hash)->nd_next; NODE *next = RNODE_LIST(value)->nd_next; - st_data_t key = (st_data_t)head; + st_data_t key; st_data_t data; - RNODE_LIST(value)->nd_next = 0; + + /* keyword splat, e.g. {k: 1, **z, k: 2} */ if (!head) { - key = (st_data_t)value; + head = value; } - else if (nd_type_p(head, NODE_LIT) && - st_delete(literal_keys, (key = (st_data_t)RNODE_LIT(head)->nd_lit, &key), &data)) { - NODE *dup_value = (RNODE_LIST((NODE *)data))->nd_next; - rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data), - "key %+"PRIsVALUE" is duplicated and overwritten on line %d", - RNODE_LIT(head)->nd_lit, nd_line(head)); - if (dup_value == last_expr) { - RNODE_LIST(value)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(value)->nd_head); - } - else { - RNODE_LIST(last_expr)->nd_head = block_append(p, RNODE_LIST(dup_value)->nd_head, RNODE_LIST(last_expr)->nd_head); + + if (nd_type_st_key_enable_p(head)) { + key = (st_data_t)head; + + if (st_delete(p->warn_duplicate_keys_table, &key, &data)) { + rb_warn2L(nd_line((NODE *)data), + "key %+"PRIsWARN" is duplicated and overwritten on line %d", + nd_value(p, head), WARN_I(nd_line(head))); } + st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash); } - st_insert(literal_keys, (st_data_t)key, (st_data_t)hash); - last_expr = !head || nd_type_p(head, NODE_LIT) ? value : head; hash = next; } - st_foreach(literal_keys, append_literal_keys, (st_data_t)&result); - st_free_table(literal_keys); - if (hash) { - if (!result) result = hash; - else list_concat(result, hash); - } - result->nd_loc = loc; - return result; + st_free_table(p->warn_duplicate_keys_table); + p->warn_duplicate_keys_table = NULL; } static NODE * new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc) { - if (hash) hash = remove_duplicate_keys(p, hash); + if (hash) warn_duplicate_keys(p, hash); return NEW_HASH(hash, loc); } -#endif static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc) @@ -14596,7 +14705,11 @@ error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE * if (st_is_member(p->pvtbl, id)) { yyerror1(loc, "duplicated variable name"); } + else if (p->ctxt.in_alt_pattern && id) { + yyerror1(loc, "variable capture in alternative pattern"); + } else { + p->ctxt.capture_in_pattern = 1; st_insert(p->pvtbl, (st_data_t)id, 0); } } @@ -14614,15 +14727,12 @@ error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *l st_insert(p->pktbl, (st_data_t)key, 0); } -#ifndef RIPPER static NODE * new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc) { return NEW_HASH(hash, loc); } -#endif /* !RIPPER */ -#ifndef RIPPER static NODE * new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc) { @@ -14631,28 +14741,12 @@ new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_c if (lhs) { ID vid = get_nd_vid(p, lhs); YYLTYPE lhs_loc = lhs->nd_loc; - int shareable = ctxt.shareable_constant_value; - if (shareable) { - switch (nd_type(lhs)) { - case NODE_CDECL: - case NODE_COLON2: - case NODE_COLON3: - break; - default: - shareable = 0; - break; - } - } if (op == tOROP) { - rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc); set_nd_value(p, lhs, rhs); nd_set_loc(lhs, loc); asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc); } else if (op == tANDOP) { - if (shareable) { - rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc); - } set_nd_value(p, lhs, rhs); nd_set_loc(lhs, loc); asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc); @@ -14660,38 +14754,38 @@ new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_c else { asgn = lhs; rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc); - if (shareable) { - rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc); - } set_nd_value(p, asgn, rhs); nd_set_loc(asgn, loc); } } else { - asgn = NEW_BEGIN(0, loc); + asgn = NEW_ERROR(loc); } return asgn; } static NODE * new_ary_op_assign(struct parser_params *p, NODE *ary, - NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc) + NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc, + const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc) { NODE *asgn; + aryset_check(p, args); args = make_list(args, args_loc); - asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc); + asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc); fixpos(asgn, ary); return asgn; } static NODE * new_attr_op_assign(struct parser_params *p, NODE *lhs, - ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc) + ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc, + const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc) { NODE *asgn; - asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc); + asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc); fixpos(asgn, lhs); return asgn; } @@ -14702,11 +14796,10 @@ new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct NODE *asgn; if (lhs) { - rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc); - asgn = NEW_OP_CDECL(lhs, op, rhs, loc); + asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc); } else { - asgn = NEW_BEGIN(0, loc); + asgn = NEW_ERROR(loc); } fixpos(asgn, lhs); return asgn; @@ -14716,20 +14809,16 @@ static NODE * const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc) { if (p->ctxt.in_def) { +#ifndef RIPPER yyerror1(loc, "dynamic constant assignment"); - } - return NEW_CDECL(0, 0, (path), loc); -} #else -static VALUE -const_decl(struct parser_params *p, VALUE path) -{ - if (p->ctxt.in_def) { - path = assign_error(p, "dynamic constant assignment", path); + set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue)); +#endif } - return path; + return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc); } +#ifdef RIPPER static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a) { @@ -14737,15 +14826,8 @@ assign_error(struct parser_params *p, const char *mesg, VALUE a) ripper_error(p); return a; } - -static VALUE -var_field(struct parser_params *p, VALUE a) -{ - return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0); -} #endif -#ifndef RIPPER static NODE * new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc) { @@ -14757,16 +14839,12 @@ new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_els result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc); nd_set_line(result, rescue->nd_loc.beg_pos.lineno); } - else if (rescue_else) { - result = block_append(p, result, rescue_else); - } if (ensure) { result = NEW_ENSURE(result, ensure, loc); } fixpos(result, head); return result; } -#endif static void warn_unused_var(struct parser_params *p, struct local_vars *local) @@ -14803,10 +14881,11 @@ local_push(struct parser_params *p, int toplevel_scope) #ifndef RIPPER if (toplevel_scope && compile_for_eval) warn_unused_vars = 0; if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0; +#endif local->numparam.outer = 0; local->numparam.inner = 0; local->numparam.current = 0; -#endif + local->it = 0; local->used = warn_unused_vars ? vtable_alloc(0) : 0; # if WARN_PAST_SCOPE @@ -14839,7 +14918,7 @@ local_free(struct parser_params *p, struct local_vars *local) vtable_chain_free(p, local->args); vtable_chain_free(p, local->vars); - ruby_sized_xfree(local, sizeof(struct local_vars)); + ruby_xfree_sized(local, sizeof(struct local_vars)); } static void @@ -14857,7 +14936,6 @@ local_pop(struct parser_params *p) COND_POP(); } -#ifndef RIPPER static rb_ast_id_table_t * local_tbl(struct parser_params *p) { @@ -14884,8 +14962,6 @@ local_tbl(struct parser_params *p) return tbl; } -#endif - static void numparam_name(struct parser_params *p, ID id) { @@ -14911,6 +14987,14 @@ local_var(struct parser_params *p, ID id) } } +#ifndef RIPPER +int +rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq) +{ + return rb_local_defined(id, iseq); +} +#endif + static int local_id_ref(struct parser_params *p, ID id, ID **vidrefp) { @@ -14927,7 +15011,7 @@ local_id_ref(struct parser_params *p, ID id, ID **vidrefp) } if (vars && vars->prev == DVARS_INHERIT) { - return rb_local_defined(id, p->parent_iseq); + return rb_parser_local_defined(p, id, p->parent_iseq); } else if (vtable_included(args, id)) { return 1; @@ -14957,34 +15041,60 @@ static void add_forwarding_args(struct parser_params *p) { arg_var(p, idFWD_REST); -#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS arg_var(p, idFWD_KWREST); -#endif arg_var(p, idFWD_BLOCK); arg_var(p, idFWD_ALL); } -#ifndef RIPPER +static void +forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var) +{ + bool conflict = false; + + struct vtable *vars, *args; + + vars = p->lvtbl->vars; + args = p->lvtbl->args; + + while (vars && !DVARS_TERMINAL_P(vars->prev)) { + conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all))); + vars = vars->prev; + args = args->prev; + } + + bool found = false; + if (vars && vars->prev == DVARS_INHERIT && !found) { + found = (rb_parser_local_defined(p, arg, p->parent_iseq) && + !(all && rb_parser_local_defined(p, all, p->parent_iseq))); + } + else { + found = (vtable_included(args, arg) && + !(all && vtable_included(args, all))); + } + + if (!found) { + compile_error(p, "no anonymous %s parameter", var); + } + else if (conflict) { + compile_error(p, "anonymous %s parameter is also used within block", var); + } +} + static NODE * new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc) { NODE *rest = NEW_LVAR(idFWD_REST, loc); -#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc)); -#endif - rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc); - NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc); -#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS - args = arg_append(p, args, new_hash(p, kwrest, loc), loc); -#endif + rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC); + NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC); + block->forwarding = TRUE; + args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc); return arg_blk_pass(args, block); } -#endif static NODE * numparam_push(struct parser_params *p) { -#ifndef RIPPER struct local_vars *local = p->lvtbl; NODE *inner = local->numparam.inner; if (!local->numparam.outer) { @@ -14992,16 +15102,13 @@ numparam_push(struct parser_params *p) } local->numparam.inner = 0; local->numparam.current = 0; + local->it = 0; return inner; -#else - return 0; -#endif } static void numparam_pop(struct parser_params *p, NODE *prev_inner) { -#ifndef RIPPER struct local_vars *local = p->lvtbl; if (prev_inner) { /* prefer first one */ @@ -15020,7 +15127,7 @@ numparam_pop(struct parser_params *p, NODE *prev_inner) /* no numbered parameter */ local->numparam.current = 0; } -#endif + local->it = 0; } static const struct vtable * @@ -15070,7 +15177,7 @@ dyna_pop(struct parser_params *p, const struct vtable *lvargs) dyna_pop_1(p); if (!p->lvtbl->args) { struct local_vars *local = p->lvtbl->prev; - ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl)); + ruby_xfree_sized(p->lvtbl, sizeof(*p->lvtbl)); p->lvtbl = local; } } @@ -15083,7 +15190,8 @@ dyna_in_block(struct parser_params *p) return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE; } -static int +#ifndef RIPPER +int dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp) { struct vtable *vars, *args, *used; @@ -15113,6 +15221,7 @@ dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp) return 0; } +#endif static int dvar_defined(struct parser_params *p, ID id) @@ -15128,78 +15237,100 @@ dvar_curr(struct parser_params *p, ID id) } static void -reg_fragment_enc_error(struct parser_params* p, VALUE str, int c) +reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c) { compile_error(p, "regexp encoding option '%c' differs from source encoding '%s'", - c, rb_enc_name(rb_enc_get(str))); + c, rb_enc_name(rb_parser_str_get_encoding(str))); } #ifndef RIPPER +static rb_encoding * +find_enc(struct parser_params* p, const char *name) +{ + int idx = rb_enc_find_index(name); + if (idx < 0) { + rb_bug("unknown encoding name: %s", name); + } + + return rb_enc_from_index(idx); +} + +static rb_encoding * +kcode_to_enc(struct parser_params* p, int kcode) +{ + rb_encoding *enc; + + switch (kcode) { + case ENC_ASCII8BIT: + enc = rb_ascii8bit_encoding(); + break; + case ENC_EUC_JP: + enc = find_enc(p, "EUC-JP"); + break; + case ENC_Windows_31J: + enc = find_enc(p, "Windows-31J"); + break; + case ENC_UTF8: + enc = rb_utf8_encoding(); + break; + default: + enc = NULL; + break; + } + + return enc; +} + int -rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options) +rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options) { int c = RE_OPTION_ENCODING_IDX(options); if (c) { int opt, idx; - rb_char_to_option_kcode(c, &opt, &idx); - if (idx != ENCODING_GET(str) && - !is_ascii_string(str)) { + rb_encoding *enc; + + char_to_option_kcode(c, &opt, &idx); + enc = kcode_to_enc(p, idx); + if (enc != rb_parser_str_get_encoding(str) && + !rb_parser_is_ascii_string(p, str)) { goto error; } - ENCODING_SET(str, idx); + rb_parser_string_set_encoding(str, enc); } else if (RE_OPTION_ENCODING_NONE(options)) { - if (!ENCODING_IS_ASCII8BIT(str) && - !is_ascii_string(str)) { + if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) && + !rb_parser_is_ascii_string(p, str)) { c = 'n'; goto error; } - rb_enc_associate(str, rb_ascii8bit_encoding()); + rb_parser_enc_associate(p, str, rb_ascii8bit_encoding()); } else if (rb_is_usascii_enc(p->enc)) { - if (!is_ascii_string(str)) { - /* raise in re.c */ - rb_enc_associate(str, rb_usascii_encoding()); - } - else { - rb_enc_associate(str, rb_ascii8bit_encoding()); - } + rb_parser_enc_associate(p, str, rb_ascii8bit_encoding()); } return 0; error: return c; } +#endif static void -reg_fragment_setenc(struct parser_params* p, VALUE str, int options) +reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options) { int c = rb_reg_fragment_setenc(p, str, options); if (c) reg_fragment_enc_error(p, str, c); } -static int -reg_fragment_check(struct parser_params* p, VALUE str, int options) -{ - VALUE err; - reg_fragment_setenc(p, str, options); - err = rb_reg_check_preprocess(str); - if (err != Qnil) { - err = rb_obj_as_string(err); - compile_error(p, "%"PRIsVALUE, err); - return 0; - } - return 1; -} - #ifndef UNIVERSAL_PARSER typedef struct { struct parser_params* parser; rb_encoding *enc; NODE *succ_block; const YYLTYPE *loc; + rb_parser_assignable_func assignable; } reg_named_capture_assign_t; static int @@ -15212,11 +15343,11 @@ reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end, long len = name_end - name; const char *s = (const char *)name; - return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc); + return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable); } static NODE * -reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc) +reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable) { reg_named_capture_assign_t arg; @@ -15224,6 +15355,7 @@ reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *l arg.enc = rb_enc_get(regexp); arg.succ_block = 0; arg.loc = loc; + arg.assignable = assignable; onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg); if (!arg.succ_block) return 0; @@ -15231,9 +15363,16 @@ reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *l } #endif +#ifndef RIPPER +NODE * +rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc) +{ + return assignable(p, id, val, loc); +} + int rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len, - rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc) + rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable) { ID var; NODE *node, *succ; @@ -15246,29 +15385,34 @@ rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, lo if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) { if (!lvar_defined(p, var)) return ST_CONTINUE; } - node = node_assign(p, assignable(p, var, 0, loc), NEW_LIT(ID2SYM(var), loc), NO_LEX_CTXT, loc); + node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc); succ = *succ_block; - if (!succ) succ = NEW_BEGIN(0, loc); + if (!succ) succ = NEW_ERROR(loc); succ = block_append(p, succ, node); *succ_block = succ; return ST_CONTINUE; } +#endif static VALUE -parser_reg_compile(struct parser_params* p, VALUE str, int options) +parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options) { + VALUE str2; reg_fragment_setenc(p, str, options); - return rb_parser_reg_compile(p, str, options); + str2 = rb_str_new_parser_string(str); + return rb_parser_reg_compile(p, str2, options); } +#ifndef RIPPER VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options) { return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline); } +#endif static VALUE -reg_compile(struct parser_params* p, VALUE str, int options) +reg_compile(struct parser_params* p, rb_parser_string_t *str, int options) { VALUE re; VALUE err; @@ -15283,23 +15427,6 @@ reg_compile(struct parser_params* p, VALUE str, int options) } return re; } -#else -static VALUE -parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg) -{ - VALUE err = rb_errinfo(); - VALUE re; - str = ripper_is_node_yylval(p, str) ? RNODE_RIPPER(str)->nd_cval : str; - int c = rb_reg_fragment_setenc(p, str, options); - if (c) reg_fragment_enc_error(p, str, c); - re = rb_parser_reg_compile(p, str, options); - if (NIL_P(re)) { - *errmsg = rb_attr_get(rb_errinfo(), idMesg); - rb_set_errinfo(err); - } - return re; -} -#endif #ifndef RIPPER void @@ -15338,12 +15465,12 @@ parser_append_options(struct parser_params *p, NODE *node) node = block_append(p, split, node); } if (p->do_chomp) { - NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC); + NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC); chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC)); irs = list_append(p, irs, NEW_HASH(chomp, LOC)); } - node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC); + node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC); } return node; @@ -15357,7 +15484,7 @@ rb_init_parse(void) (void)nodeline; } -static ID +ID internal_id(struct parser_params *p) { return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars)); @@ -15371,17 +15498,21 @@ parser_initialize(struct parser_params *p) p->command_start = TRUE; p->ruby_sourcefile_string = Qnil; p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */ + string_buffer_init(p); p->node_id = 0; - p->delayed.token = Qnil; + p->delayed.token = NULL; p->frozen_string_literal = -1; /* not specified */ -#ifdef RIPPER - p->result = Qnil; - p->parsing_thread = Qnil; -#else +#ifndef RIPPER p->error_buffer = Qfalse; - p->end_expect_token_locations = Qnil; + p->end_expect_token_locations = NULL; p->token_id = 0; - p->tokens = Qnil; + p->tokens = NULL; +#else + p->result = Qnil; + p->parsing_thread = Qnil; + p->s_value = Qnil; + p->s_lvalue = Qnil; + p->s_value_stack = rb_ary_new(); #endif p->debug_buffer = Qnil; p->debug_output = rb_ractor_stdout(); @@ -15400,28 +15531,19 @@ rb_ruby_parser_mark(void *ptr) { struct parser_params *p = (struct parser_params*)ptr; - rb_gc_mark(p->lex.input); - rb_gc_mark(p->lex.lastline); - rb_gc_mark(p->lex.nextline); rb_gc_mark(p->ruby_sourcefile_string); - rb_gc_mark((VALUE)p->ast); - rb_gc_mark(p->case_labels); - rb_gc_mark(p->delayed.token); #ifndef RIPPER - rb_gc_mark(p->debug_lines); rb_gc_mark(p->error_buffer); - rb_gc_mark(p->end_expect_token_locations); - rb_gc_mark(p->tokens); #else rb_gc_mark(p->value); rb_gc_mark(p->result); rb_gc_mark(p->parsing_thread); + rb_gc_mark(p->s_value); + rb_gc_mark(p->s_lvalue); + rb_gc_mark(p->s_value_stack); #endif rb_gc_mark(p->debug_buffer); rb_gc_mark(p->debug_output); -#ifdef YYMALLOC - rb_gc_mark((VALUE)p->heap); -#endif } void @@ -15429,12 +15551,23 @@ rb_ruby_parser_free(void *ptr) { struct parser_params *p = (struct parser_params*)ptr; struct local_vars *local, *prev; -#ifdef UNIVERSAL_PARSER - rb_parser_config_t *config = p->config; + + if (p->ast) { + rb_ast_free(p->ast); + } + + if (p->warn_duplicate_keys_table) { + st_free_table(p->warn_duplicate_keys_table); + } + +#ifndef RIPPER + if (p->tokens) { + rb_parser_ary_free(p, p->tokens); + } #endif if (p->tokenbuf) { - ruby_sized_xfree(p->tokenbuf, p->toksiz); + ruby_xfree_sized(p->tokenbuf, p->toksiz); } for (local = p->lvtbl; local; local = prev) { @@ -15449,14 +15582,20 @@ rb_ruby_parser_free(void *ptr) xfree(ptinfo); } } - xfree(ptr); + string_buffer_free(p); -#ifdef UNIVERSAL_PARSER - config->counter--; - if (config->counter <= 0) { - rb_ruby_parser_config_free(config); + if (p->pvtbl) { + st_free_table(p->pvtbl); } -#endif + + if (CASE_LABELS_ENABLED_P(p->case_labels)) { + st_free_table(p->case_labels); + } + + xfree(p->lex.strterm); + p->lex.strterm = 0; + + xfree(ptr); } size_t @@ -15474,34 +15613,6 @@ rb_ruby_parser_memsize(const void *ptr) return size; } -#ifdef UNIVERSAL_PARSER -rb_parser_config_t * -rb_ruby_parser_config_new(void *(*malloc)(size_t size)) -{ - return (rb_parser_config_t *)malloc(sizeof(rb_parser_config_t)); -} - -void -rb_ruby_parser_config_free(rb_parser_config_t *config) -{ - config->free(config); -} -#endif - -#ifndef UNIVERSAL_PARSER -#ifndef RIPPER -static const rb_data_type_t parser_data_type = { - "parser", - { - rb_ruby_parser_mark, - rb_ruby_parser_free, - rb_ruby_parser_memsize, - }, - 0, 0, RUBY_TYPED_FREE_IMMEDIATELY -}; -#endif -#endif - #ifndef RIPPER #undef rb_reserved_word @@ -15513,23 +15624,39 @@ rb_reserved_word(const char *str, unsigned int len) #ifdef UNIVERSAL_PARSER rb_parser_t * -rb_ruby_parser_allocate(rb_parser_config_t *config) +rb_ruby_parser_allocate(const rb_parser_config_t *config) { /* parser_initialize expects fields to be set to 0 */ rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t)); p->config = config; - p->config->counter++; return p; } rb_parser_t * -rb_ruby_parser_new(rb_parser_config_t *config) +rb_ruby_parser_new(const rb_parser_config_t *config) { /* parser_initialize expects fields to be set to 0 */ rb_parser_t *p = rb_ruby_parser_allocate(config); parser_initialize(p); return p; } +#else +rb_parser_t * +rb_ruby_parser_allocate(void) +{ + /* parser_initialize expects fields to be set to 0 */ + rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t)); + return p; +} + +rb_parser_t * +rb_ruby_parser_new(void) +{ + /* parser_initialize expects fields to be set to 0 */ + rb_parser_t *p = rb_ruby_parser_allocate(); + parser_initialize(p); + return p; +} #endif rb_parser_t * @@ -15541,167 +15668,28 @@ rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, in } void -rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines) +rb_ruby_parser_set_script_lines(rb_parser_t *p) { - if (!RTEST(lines)) { - lines = Qfalse; - } - else if (lines == Qtrue) { - lines = rb_ary_new(); - } - else { - Check_Type(lines, T_ARRAY); - rb_ary_modify(lines); - } - p->debug_lines = lines; + p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10); } void rb_ruby_parser_error_tolerant(rb_parser_t *p) { p->error_tolerant = 1; - // TODO - p->end_expect_token_locations = rb_ary_new(); } void rb_ruby_parser_keep_tokens(rb_parser_t *p) { p->keep_tokens = 1; - // TODO - p->tokens = rb_ary_new(); + p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10); } -#ifndef UNIVERSAL_PARSER -rb_ast_t* -rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - RB_GC_GUARD(vparser); /* prohibit tail call optimization */ - return rb_ruby_parser_compile_file_path(p, fname, file, start); -} - -rb_ast_t* -rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - RB_GC_GUARD(vparser); /* prohibit tail call optimization */ - return rb_ruby_parser_compile_generic(p, lex_gets, fname, input, start); -} - -rb_ast_t* -rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - RB_GC_GUARD(vparser); /* prohibit tail call optimization */ - return rb_ruby_parser_compile_string(p, f, s, line); -} - -rb_ast_t* -rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - RB_GC_GUARD(vparser); /* prohibit tail call optimization */ - return rb_ruby_parser_compile_string_path(p, f, s, line); -} - -VALUE -rb_parser_encoding(VALUE vparser) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - return rb_ruby_parser_encoding(p); -} - -VALUE -rb_parser_end_seen_p(VALUE vparser) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - return RBOOL(rb_ruby_parser_end_seen_p(p)); -} - -void -rb_parser_error_tolerant(VALUE vparser) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - rb_ruby_parser_error_tolerant(p); -} - -void -rb_parser_set_script_lines(VALUE vparser, VALUE lines) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - rb_ruby_parser_set_script_lines(p, lines); -} - -void -rb_parser_keep_tokens(VALUE vparser) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - rb_ruby_parser_keep_tokens(p); -} - -VALUE -rb_parser_new(void) -{ - struct parser_params *p; - VALUE parser = TypedData_Make_Struct(0, struct parser_params, - &parser_data_type, p); - parser_initialize(p); - return parser; -} - -VALUE -rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - rb_ruby_parser_set_context(p, base, main); - return vparser; -} - -void -rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split) -{ - struct parser_params *p; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - rb_ruby_parser_set_options(p, print, loop, chomp, split); -} - -VALUE -rb_parser_set_yydebug(VALUE self, VALUE flag) -{ - struct parser_params *p; - - TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p); - rb_ruby_parser_set_yydebug(p, RTEST(flag)); - return flag; -} -#endif /* !UNIVERSAL_PARSER */ - -VALUE +rb_encoding * rb_ruby_parser_encoding(rb_parser_t *p) { - return rb_enc_from_encoding(p->enc); + return p->enc; } int @@ -15762,7 +15750,7 @@ rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread) } void -rb_ruby_parser_ripper_initialize(rb_parser_t *p, VALUE (*gets)(struct parser_params*,VALUE), VALUE input, VALUE sourcefile_string, const char *sourcefile, int sourceline) +rb_ruby_parser_ripper_initialize(rb_parser_t *p, rb_parser_lex_gets_func *gets, rb_parser_input_data input, VALUE sourcefile_string, const char *sourcefile, int sourceline) { p->lex.gets = gets; p->lex.input = input; @@ -15808,22 +15796,18 @@ rb_ruby_ripper_parse0(rb_parser_t *p) parser_prepare(p); p->ast = rb_ast_new(); ripper_yyparse((void*)p); - rb_ast_dispose(p->ast); + rb_ast_free(p->ast); p->ast = 0; + p->eval_tree = 0; + p->eval_tree_begin = 0; } int -rb_ruby_ripper_dedent_string(rb_parser_t *p, VALUE string, int width) +rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width) { return dedent_string(p, string, width); } -VALUE -rb_ruby_ripper_lex_get_str(rb_parser_t *p, VALUE s) -{ - return lex_get_str(p, s); -} - int rb_ruby_ripper_initialized_p(rb_parser_t *p) { @@ -15848,7 +15832,7 @@ rb_ruby_ripper_token_len(rb_parser_t *p) return p->lex.pcur - p->lex.ptok; } -VALUE +rb_parser_string_t * rb_ruby_ripper_lex_lastline(rb_parser_t *p) { return p->lex.lastline; @@ -15860,6 +15844,16 @@ rb_ruby_ripper_lex_state_name(struct parser_params *p, int state) return rb_parser_lex_state_name(p, (enum lex_state_e)state); } +#ifdef UNIVERSAL_PARSER +rb_parser_t * +rb_ripper_parser_params_allocate(const rb_parser_config_t *config) +{ + rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t)); + p->config = config; + return p; +} +#endif + struct parser_params* rb_ruby_ripper_parser_allocate(void) { @@ -15868,69 +15862,6 @@ rb_ruby_ripper_parser_allocate(void) #endif /* RIPPER */ #ifndef RIPPER -#ifdef YYMALLOC -#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE)) -/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of - * potential memory leak */ -#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0) -#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \ - (new)->cnt = (cnt), (ptr)) - -void * -rb_parser_malloc(struct parser_params *p, size_t size) -{ - size_t cnt = HEAPCNT(1, size); - rb_imemo_tmpbuf_t *n = NEWHEAP(); - void *ptr = xmalloc(size); - - return ADD2HEAP(n, cnt, ptr); -} - -void * -rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size) -{ - size_t cnt = HEAPCNT(nelem, size); - rb_imemo_tmpbuf_t *n = NEWHEAP(); - void *ptr = xcalloc(nelem, size); - - return ADD2HEAP(n, cnt, ptr); -} - -void * -rb_parser_realloc(struct parser_params *p, void *ptr, size_t size) -{ - rb_imemo_tmpbuf_t *n; - size_t cnt = HEAPCNT(1, size); - - if (ptr && (n = p->heap) != NULL) { - do { - if (n->ptr == ptr) { - n->ptr = ptr = xrealloc(ptr, size); - if (n->cnt) n->cnt = cnt; - return ptr; - } - } while ((n = n->next) != NULL); - } - n = NEWHEAP(); - ptr = xrealloc(ptr, size); - return ADD2HEAP(n, cnt, ptr); -} - -void -rb_parser_free(struct parser_params *p, void *ptr) -{ - rb_imemo_tmpbuf_t **prev = &p->heap, *n; - - while ((n = *prev) != NULL) { - if (n->ptr == ptr) { - *prev = n->next; - break; - } - prev = &n->next; - } -} -#endif - void rb_parser_printf(struct parser_params *p, const char *fmt, ...) { @@ -15941,7 +15872,7 @@ rb_parser_printf(struct parser_params *p, const char *fmt, ...) va_start(ap, fmt); rb_str_vcatf(mesg, fmt, ap); va_end(ap); - if (RSTRING_END(mesg)[-1] == '\n') { + if (char_at_end(p, mesg, 0) == '\n') { rb_io_write(p->debug_output, mesg); p->debug_buffer = Qnil; } @@ -15987,7 +15918,7 @@ count_char(const char *str, int c) * * "\"`class' keyword\"" => "`class' keyword" */ -RUBY_FUNC_EXPORTED size_t +size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr) { if (*yystr == '"') { @@ -15996,31 +15927,30 @@ rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr) while (*++yyp) { switch (*yyp) { - case '`': + case '\'': if (!bquote) { - bquote = count_char(yyp+1, '`') + 1; + bquote = count_char(yyp+1, '\'') + 1; if (yyres) memcpy(&yyres[yyn], yyp, bquote); yyn += bquote; yyp += bquote - 1; break; } - goto default_char; - - case '\'': - if (bquote && count_char(yyp+1, '\'') + 1 == bquote) { - if (yyres) memcpy(yyres + yyn, yyp, bquote); - yyn += bquote; - yyp += bquote - 1; - bquote = 0; - break; - } - if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') { - if (yyres) memcpy(yyres + yyn, yyp, 3); - yyn += 3; - yyp += 2; - break; + else { + if (bquote && count_char(yyp+1, '\'') + 1 == bquote) { + if (yyres) memcpy(yyres + yyn, yyp, bquote); + yyn += bquote; + yyp += bquote - 1; + bquote = 0; + break; + } + if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') { + if (yyres) memcpy(yyres + yyn, yyp, 3); + yyn += 3; + yyp += 2; + break; + } + goto do_not_strip_quotes; } - goto do_not_strip_quotes; case ',': goto do_not_strip_quotes; @@ -16029,7 +15959,6 @@ rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr) if (*++yyp != '\\') goto do_not_strip_quotes; /* Fall through. */ - default_char: default: if (yyres) yyres[yyn] = *yyp; @@ -16053,46 +15982,7 @@ rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr) #endif #ifdef RIPPER -#ifdef RIPPER_DEBUG -/* :nodoc: */ -static VALUE -ripper_validate_object(VALUE self, VALUE x) -{ - if (x == Qfalse) return x; - if (x == Qtrue) return x; - if (NIL_P(x)) return x; - if (UNDEF_P(x)) - rb_raise(rb_eArgError, "Qundef given"); - if (FIXNUM_P(x)) return x; - if (SYMBOL_P(x)) return x; - switch (BUILTIN_TYPE(x)) { - case T_STRING: - case T_OBJECT: - case T_ARRAY: - case T_BIGNUM: - case T_FLOAT: - case T_COMPLEX: - case T_RATIONAL: - break; - case T_NODE: - if (!nd_type_p((NODE *)x, NODE_RIPPER)) { - rb_raise(rb_eArgError, "NODE given: %p", (void *)x); - } - x = ((NODE *)x)->nd_rval; - break; - default: - rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)", - (void *)x, rb_obj_classname(x)); - } - if (!RBASIC_CLASS(x)) { - rb_raise(rb_eArgError, "hidden ruby object: %p (%s)", - (void *)x, rb_builtin_type_name(TYPE(x))); - } - return x; -} -#endif - -#define validate(x) ((x) = get_value(x)) +#define validate(x) (void)(x) static VALUE ripper_dispatch0(struct parser_params *p, ID mid) |
