diff options
Diffstat (limited to 'parse.y')
| -rw-r--r-- | parse.y | 22942 |
1 files changed, 13829 insertions, 9113 deletions
@@ -14,103 +14,410 @@ #if !YYPURE # error needs pure parser #endif -#ifndef PARSER_DEBUG -#define PARSER_DEBUG 0 -#endif #define YYDEBUG 1 #define YYERROR_VERBOSE 1 #define YYSTACK_USE_ALLOCA 0 -#include "ruby/ruby.h" -#include "ruby/st.h" -#include "ruby/encoding.h" +/* For Ripper */ +#ifdef RUBY_EXTCONF_H +# include RUBY_EXTCONF_H +#endif + +#include "ruby/internal/config.h" + +#include <errno.h> + +#ifdef UNIVERSAL_PARSER + +#include "internal/ruby_parser.h" +#include "parser_node.h" +#include "universal_parser.c" + +#ifdef RIPPER +#define STATIC_ID2SYM p->config->static_id2sym +#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable +#endif + +#else + #include "internal.h" +#include "internal/compile.h" +#include "internal/compilers.h" +#include "internal/complex.h" +#include "internal/encoding.h" +#include "internal/error.h" +#include "internal/hash.h" +#include "internal/io.h" +#include "internal/numeric.h" +#include "internal/parse.h" +#include "internal/rational.h" +#include "internal/re.h" +#include "internal/ruby_parser.h" +#include "internal/symbol.h" +#include "internal/thread.h" +#include "internal/variable.h" #include "node.h" -#include "parse.h" -#include "symbol.h" -#include "regenc.h" -#include <stdio.h> -#include <errno.h> -#include <ctype.h> +#include "parser_node.h" #include "probes.h" +#include "regenc.h" +#include "ruby/encoding.h" +#include "ruby/regex.h" +#include "ruby/ruby.h" +#include "ruby/st.h" +#include "ruby/util.h" +#include "ruby/ractor.h" +#include "symbol.h" -#ifndef WARN_PAST_SCOPE -# define WARN_PAST_SCOPE 0 +#ifndef RIPPER +static VALUE +syntax_error_new(void) +{ + return rb_class_new_instance(0, 0, rb_eSyntaxError); +} +#endif + +static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable); + +#define compile_callback rb_suppress_tracing +#endif /* !UNIVERSAL_PARSER */ + +#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 -#define YYMALLOC(size) rb_parser_malloc(parser, (size)) -#define YYREALLOC(ptr, size) rb_parser_realloc(parser, (ptr), (size)) -#define YYCALLOC(nelem, size) rb_parser_calloc(parser, (nelem), (size)) -#define YYFREE(ptr) rb_parser_free(parser, (ptr)) -#undef malloc -#undef realloc -#undef calloc -#undef free -#define malloc YYMALLOC -#define realloc YYREALLOC -#define calloc YYCALLOC -#define free YYFREE - -enum lex_state_bits { - EXPR_BEG_bit, /* ignore newline, +/- is a sign. */ - EXPR_END_bit, /* newline significant, +/- is an operator. */ - EXPR_ENDARG_bit, /* ditto, and unbound braces. */ - EXPR_ENDFN_bit, /* ditto, and unbound braces. */ - EXPR_ARG_bit, /* newline significant, +/- is an operator. */ - EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */ - EXPR_MID_bit, /* newline significant, +/- is an operator. */ - EXPR_FNAME_bit, /* ignore newline, no reserved words. */ - EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */ - EXPR_CLASS_bit, /* immediate after `class', no here document. */ - EXPR_LABEL_bit, /* flag bit, label is allowed. */ - EXPR_LABELED_bit, /* flag bit, just after a label. */ - EXPR_MAX_STATE +static int +node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2) +{ + return (n1->minus != n2->minus || + n1->base != n2->base || + strcmp(n1->val, n2->val)); +} + +static int +node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2) +{ + return (n1->minus != n2->minus || + strcmp(n1->val, n2->val)); +} + +static int +node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2) +{ + return (n1->minus != n2->minus || + n1->base != n2->base || + n1->seen_point != n2->seen_point || + strcmp(n1->val, n2->val)); +} + +static int +node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2) +{ + 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 int +rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2) +{ + 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(st_data_t val, st_data_t lit) +{ + if (val == lit) return 0; + + 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(st_data_t a) +{ + NODE *node = (NODE *)a; + enum node_type type = nd_type(node); + + 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 inline int +parse_isascii(int c) +{ + return '\0' <= c && c <= '\x7f'; +} + +#undef ISASCII +#define ISASCII parse_isascii + +static inline int +parse_isspace(int c) +{ + return c == ' ' || ('\t' <= c && c <= '\r'); +} + +#undef ISSPACE +#define ISSPACE parse_isspace + +static inline int +parse_iscntrl(int c) +{ + return ('\0' <= c && c < ' ') || c == '\x7f'; +} + +#undef ISCNTRL +#define ISCNTRL(c) parse_iscntrl(c) + +static inline int +parse_isupper(int c) +{ + return 'A' <= c && c <= 'Z'; +} + +static inline int +parse_islower(int c) +{ + return 'a' <= c && c <= 'z'; +} + +static inline int +parse_isalpha(int c) +{ + return parse_isupper(c) || parse_islower(c); +} + +#undef ISALPHA +#define ISALPHA(c) parse_isalpha(c) + +static inline int +parse_isdigit(int c) +{ + return '0' <= c && c <= '9'; +} + +#undef ISDIGIT +#define ISDIGIT(c) parse_isdigit(c) + +static inline int +parse_isalnum(int c) +{ + return ISALPHA(c) || ISDIGIT(c); +} + +#undef ISALNUM +#define ISALNUM(c) parse_isalnum(c) + +static inline int +parse_isxdigit(int c) +{ + return ISDIGIT(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f'); +} + +#undef ISXDIGIT +#define ISXDIGIT(c) parse_isxdigit(c) + +#include "parser_st.h" + +#undef STRCASECMP +#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp + +#undef STRNCASECMP +#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp + +#ifdef RIPPER +#include "ripper_init.h" +#endif + +enum rescue_context { + before_rescue, + after_rescue, + after_else, + after_ensure, }; -/* examine combinations */ -enum lex_state_e { -#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit) - DEF_EXPR(BEG), - DEF_EXPR(END), - DEF_EXPR(ENDARG), - DEF_EXPR(ENDFN), - DEF_EXPR(ARG), - DEF_EXPR(CMDARG), - DEF_EXPR(MID), - DEF_EXPR(FNAME), - DEF_EXPR(DOT), - DEF_EXPR(CLASS), - DEF_EXPR(LABEL), - DEF_EXPR(LABELED), - EXPR_VALUE = EXPR_BEG, - EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS), - EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG), - EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN) + +struct lex_context { + unsigned int in_defined: 1; + unsigned int in_kwarg: 1; + unsigned int in_argdef: 1; + unsigned int in_def: 1; + unsigned int in_class: 1; + 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; + +#if defined(__GNUC__) && !defined(__clang__) +// Suppress "parameter passing for argument of type 'struct +// lex_context' changed" notes. `struct lex_context` is file scope, +// and has no ABI compatibility issue. +RBIMPL_WARNING_PUSH() +RBIMPL_WARNING_IGNORED(-Wpsabi) +RBIMPL_WARNING_POP() +// Not sure why effective even after popped. +#endif + +#include "parse.h" + +#define NO_LEX_CTXT (struct lex_context){0} + +#ifndef WARN_PAST_SCOPE +# define WARN_PAST_SCOPE 0 +#endif + +#define TAB_WIDTH 8 + +#define yydebug (p->debug) /* disable the global variable definition */ + +#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__) +#define YY_LOCATION_PRINT(File, loc, p) \ + rb_parser_printf(p, "%d.%d-%d.%d", \ + (loc).beg_pos.lineno, (loc).beg_pos.column,\ + (loc).end_pos.lineno, (loc).end_pos.column) +#define YYLLOC_DEFAULT(Current, Rhs, N) \ + do \ + if (N) \ + { \ + (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \ + (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \ + } \ + else \ + { \ + (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \ + (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \ + } \ + while (0) +#define YY_(Msgid) \ + (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \ + "nesting too deep" : (Msgid)) + +#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \ + rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current)) +#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \ + rb_parser_set_location_of_delayed_token(p, &(Current)) +#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \ + rb_parser_set_location_of_heredoc_end(p, &(Current)) +#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \ + rb_parser_set_location_of_dummy_end(p, &(Current)) +#define RUBY_SET_YYLLOC_OF_NONE(Current) \ + rb_parser_set_location_of_none(p, &(Current)) +#define RUBY_SET_YYLLOC(Current) \ + rb_parser_set_location(p, &(Current)) +#define RUBY_INIT_YYLLOC() \ + { \ + {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \ + {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \ + } + #define IS_lex_state_for(x, ls) ((x) & (ls)) #define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls)) -#define IS_lex_state(ls) IS_lex_state_for(lex_state, (ls)) -#define IS_lex_state_all(ls) IS_lex_state_all_for(lex_state, (ls)) +#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls)) +#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls)) -#if PARSER_DEBUG -static const char *lex_state_name(enum lex_state_e state); -#endif +# define SET_LEX_STATE(ls) \ + parser_set_lex_state(p, ls, __LINE__) +static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line); typedef VALUE stack_type; -# define BITSTACK_PUSH(stack, n) ((stack) = ((stack)<<1)|((n)&1)) -# define BITSTACK_POP(stack) ((stack) = (stack) >> 1) -# define BITSTACK_LEXPOP(stack) ((stack) = ((stack) >> 1) | ((stack) & 1)) -# define BITSTACK_SET_P(stack) ((stack)&1) +static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} }; + +# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0) +# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)")) +# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)")) +# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1) +# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)")) +/* A flag to identify keyword_do_cond, "do" keyword after condition expression. + Examples: `while ... do`, `until ... do`, and `for ... in ... do` */ #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n)) #define COND_POP() BITSTACK_POP(cond_stack) -#define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack) #define COND_P() BITSTACK_SET_P(cond_stack) +#define COND_SET(n) BITSTACK_SET(cond_stack, (n)) +/* A flag to identify keyword_do_block; "do" keyword after command_call. + Example: `foo 1, 2 do`. */ #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n)) #define CMDARG_POP() BITSTACK_POP(cmdarg_stack) -#define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack) #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack) +#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n)) struct vtable { ID *tbl; @@ -127,444 +434,1134 @@ struct local_vars { struct vtable *past; # endif struct local_vars *prev; - stack_type cmdargs; + struct { + NODE *outer, *inner, *current; + } numparam; + 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, + NUMPARAM_MAX = 9, }; #define DVARS_INHERIT ((void*)1) #define DVARS_TOPSCOPE NULL -#define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl)) -#define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3) - -static int -vtable_size(const struct vtable *tbl) -{ - if (POINTER_P(tbl)) { - return tbl->pos; - } - else { - return 0; - } -} - -#define VTBL_DEBUG 0 - -static struct vtable * -vtable_alloc(struct vtable *prev) -{ - struct vtable *tbl = ALLOC(struct vtable); - tbl->pos = 0; - tbl->capa = 8; - tbl->tbl = ALLOC_N(ID, tbl->capa); - tbl->prev = prev; - if (VTBL_DEBUG) printf("vtable_alloc: %p\n", (void *)tbl); - return tbl; -} - -static void -vtable_free(struct vtable *tbl) -{ - if (VTBL_DEBUG)printf("vtable_free: %p\n", (void *)tbl); - if (POINTER_P(tbl)) { - if (tbl->tbl) { - xfree(tbl->tbl); - } - xfree(tbl); - } -} - -static void -vtable_add(struct vtable *tbl, ID id) -{ - if (!POINTER_P(tbl)) { - rb_bug("vtable_add: vtable is not allocated (%p)", (void *)tbl); - } - if (VTBL_DEBUG) printf("vtable_add: %p, %"PRIsVALUE"\n", (void *)tbl, rb_id2str(id)); - - if (tbl->pos == tbl->capa) { - tbl->capa = tbl->capa * 2; - REALLOC_N(tbl->tbl, ID, tbl->capa); - } - tbl->tbl[tbl->pos++] = id; -} - -#ifndef RIPPER -static void -vtable_pop(struct vtable *tbl, int n) -{ - if (tbl->pos < n) rb_bug("vtable_pop: unreachable"); - tbl->pos -= n; -} -#endif - -static int -vtable_included(const struct vtable * tbl, ID id) -{ - int i; - - if (POINTER_P(tbl)) { - for (i = 0; i < tbl->pos; i++) { - if (tbl->tbl[i] == id) { - return i+1; - } - } - } - return 0; -} - +#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE) -#ifndef RIPPER typedef struct token_info { const char *token; - int linenum; - int column; + rb_code_position_t beg; + int indent; int nonspc; struct token_info *next; } token_info; -#endif + +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: - lex_pbeg tokp lex_p lex_pend - | | | | - |-----------+--------------+------------| - |<------------>| + lex.pbeg lex.ptok lex.pcur lex.pend + | | | | + |------------+------------+------------| + |<---------->| token */ struct parser_params { - NODE *heap; - YYSTYPE *lval; + YYLTYPE *yylloc; struct { - NODE *strterm; - VALUE (*gets)(struct parser_params*,VALUE); - VALUE input; - VALUE lastline; - VALUE nextline; - const char *pbeg; - const char *pcur; - const char *pend; - long gets_ptr; - enum lex_state_e state; - int paren_nest; - int lpar_beg; - int brace_nest; + rb_strterm_t *strterm; + 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; + enum lex_state_e state; + /* track the nest level of any parens "()[]{}" */ + int paren_nest; + /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */ + int lpar_beg; + /* track the nest level of only braces "{}" */ + int brace_nest; } lex; stack_type cond_stack; stack_type cmdarg_stack; int tokidx; int toksiz; - int tokline; int heredoc_end; + int heredoc_indent; + int heredoc_line_indent; char *tokenbuf; - NODE *deferred_nodes; struct local_vars *lvtbl; + st_table *pvtbl; + st_table *pktbl; int line_count; int ruby_sourceline; /* current line no. */ - char *ruby_sourcefile; /* current source file */ + const char *ruby_sourcefile; /* current source file */ VALUE ruby_sourcefile_string; rb_encoding *enc; + token_info *token_info; + st_table *case_labels; + rb_node_exits_t *exits; + + VALUE debug_buffer; + VALUE debug_output; + + struct { + rb_parser_string_t *token; + int beg_line; + int beg_col; + int end_line; + int end_col; + } delayed; + + rb_ast_t *ast; + int node_id; - ID cur_arg; + 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; - int last_cr_line; +#ifdef UNIVERSAL_PARSER + const rb_parser_config_t *config; +#endif + /* compile_option */ + signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */ unsigned int command_start:1; unsigned int eofp: 1; unsigned int ruby__end__seen: 1; - unsigned int yydebug: 1; + unsigned int debug: 1; unsigned int has_shebang: 1; - unsigned int in_defined: 1; - unsigned int compile_for_eval: 1; - unsigned int in_kwarg: 1; - unsigned int in_single: 1; - unsigned int in_def: 1; - -#ifndef RIPPER - /* Ruby core only */ + unsigned int token_seen: 1; unsigned int token_info_enabled: 1; # if WARN_PAST_SCOPE unsigned int past_scope_enabled: 1; # endif - unsigned int has_err: 1; - unsigned int token_seen: 1; + unsigned int error_p: 1; + unsigned int cr_seen: 1; - NODE *eval_tree_begin; - NODE *eval_tree; - VALUE debug_lines; - VALUE coverage; +#ifndef RIPPER + /* Ruby core only */ - token_info *token_info; + unsigned int do_print: 1; + unsigned int do_loop: 1; + unsigned int do_chomp: 1; + unsigned int do_split: 1; + unsigned int error_tolerant: 1; + unsigned int keep_tokens: 1; - VALUE compile_option; + VALUE error_buffer; + 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 */ + rb_parser_ary_t *tokens; #else /* Ripper only */ - unsigned int toplevel_p: 1; - unsigned int error_p: 1; - - const char *tokp; - VALUE delayed; - int delayed_line; - int delayed_col; 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 }; +#define NUMPARAM_ID_P(id) numparam_id_p(p, id) +#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1)) +#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx))) +static int +numparam_id_p(struct parser_params *p, ID id) +{ + if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0; + unsigned int idx = NUMPARAM_ID_TO_IDX(id); + return idx > 0 && idx <= NUMPARAM_MAX; +} +static void numparam_name(struct parser_params *p, ID id); + #ifdef RIPPER -#define intern_cstr(n,l,en) rb_intern3(n,l,en) +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) +#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc) +#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 -#define STR_NEW(p,n) rb_enc_str_new((p),(n),current_enc) -#define STR_NEW0() rb_enc_str_new(0,0,current_enc) -#define STR_NEW2(p) rb_enc_str_new((p),strlen(p),current_enc) -#define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),current_enc) -#define TOK_INTERN() intern_cstr(tok(), toklen(), current_enc) - -static int parser_yyerror(struct parser_params*, const char*); -#define yyerror(msg) parser_yyerror(parser, (msg)) - -#define lex_strterm (parser->lex.strterm) -#define lex_state (parser->lex.state) -#define cond_stack (parser->cond_stack) -#define cmdarg_stack (parser->cmdarg_stack) -#define paren_nest (parser->lex.paren_nest) -#define lpar_beg (parser->lex.lpar_beg) -#define brace_nest (parser->lex.brace_nest) -#define in_single (parser->in_single) -#define in_def (parser->in_def) -#define compile_for_eval (parser->compile_for_eval) -#define in_defined (parser->in_defined) -#define tokenbuf (parser->tokenbuf) -#define tokidx (parser->tokidx) -#define toksiz (parser->toksiz) -#define tokline (parser->tokline) -#define lex_input (parser->lex.input) -#define lex_lastline (parser->lex.lastline) -#define lex_nextline (parser->lex.nextline) -#define lex_pbeg (parser->lex.pbeg) -#define lex_p (parser->lex.pcur) -#define lex_pend (parser->lex.pend) -#define heredoc_end (parser->heredoc_end) -#define command_start (parser->command_start) -#define deferred_nodes (parser->deferred_nodes) -#define lex_gets_ptr (parser->lex.gets_ptr) -#define lex_gets (parser->lex.gets) -#define lvtbl (parser->lvtbl) -#define ruby__end__seen (parser->ruby__end__seen) -#define ruby_sourceline (parser->ruby_sourceline) -#define ruby_sourcefile (parser->ruby_sourcefile) -#define ruby_sourcefile_string (parser->ruby_sourcefile_string) -#define current_enc (parser->enc) -#define current_arg (parser->cur_arg) -#define yydebug (parser->yydebug) +static void +pop_pvtbl(struct parser_params *p, st_table *tbl) +{ + st_free_table(p->pvtbl); + p->pvtbl = tbl; +} + +static void +pop_pktbl(struct parser_params *p, st_table *tbl) +{ + if (p->pktbl) st_free_table(p->pktbl); + 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); + +static void +debug_end_expect_token_locations(struct parser_params *p, const char *name) +{ + if(p->debug) { + 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); + } +} + +static void +push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos) +{ + 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(!p->end_expect_token_locations) return; + + end_expect_token_locations_t *locations = p->end_expect_token_locations->prev; + ruby_sized_xfree(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 end_expect_token_locations_t * +peek_end_expect_token_locations(struct parser_params *p) +{ + return p->end_expect_token_locations; +} + +static const char * +parser_token2char(struct parser_params *p, enum yytokentype tok) +{ + switch ((int) tok) { +#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)) +static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*); +RBIMPL_ATTR_NONNULL((1, 2)) +static int parser_yyerror0(struct parser_params*, const char*); +#define yyerror0(msg) parser_yyerror0(p, (msg)) +#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg)) +#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg) +#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur) +#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend) +#define lex_eol_p(p) lex_eol_n_p(p, 0) +#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n) +#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0) +#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend) + +static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc); +static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc); +static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc); +static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc); +static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos); + #ifdef RIPPER +#define compile_for_eval (0) #else -#define ruby_eval_tree (parser->eval_tree) -#define ruby_eval_tree_begin (parser->eval_tree_begin) -#define ruby_debug_lines (parser->debug_lines) -#define ruby_coverage (parser->coverage) +#define compile_for_eval (p->parent_iseq != 0) #endif -#define NODE_CALL_Q(q) (((q) == tDOTQ) ? NODE_QCALL : NODE_CALL) -#define NEW_QCALL(q,r,m,a) NEW_NODE(NODE_CALL_Q(q),r,m,a) +#define token_column ((int)(p->lex.ptok - p->lex.pbeg)) + +#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*); + +static inline void +rb_discard_node(struct parser_params *p, NODE *n) +{ + rb_ast_delete_node(p->ast, n); +} + +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, 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, 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_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, 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, 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, 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, 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, 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, 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); +static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc); +static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc); +static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc); +static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc); +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_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, 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, 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, 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, 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, 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, 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,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,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,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,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,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,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,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,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,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,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) +#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc) +#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc) +#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc) +#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc) +#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_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,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) +#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc) +#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc) +#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,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,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,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,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,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) + +enum internal_node_type { + NODE_INTERNAL_ONLY = NODE_LAST, + NODE_DEF_TEMP, + NODE_EXITS, + NODE_INTERNAL_LAST +}; + +static const char * +parser_node_name(int node) +{ + switch (node) { + case NODE_DEF_TEMP: + return "NODE_DEF_TEMP"; + case NODE_EXITS: + return "NODE_EXITS"; + default: + return ruby_node_name(node); + } +} + +/* This node is parse.y internal */ +struct RNode_DEF_TEMP { + NODE node; + + /* for NODE_DEFN/NODE_DEFS */ + + struct RNode *nd_def; + ID nd_mid; + + struct { + int max_numparam; + NODE *numparam_save; + struct lex_context ctxt; + } save; +}; + +#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, 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,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 + * result AST and does not have node_id and location. */ +static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment); +#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type)) -static int yylex(YYSTYPE*, struct parser_params*); +static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc); + +static int +parser_get_node_id(struct parser_params *p) +{ + int node_id = p->node_id; + p->node_id++; + return node_id; +} + +static void +anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id) +{ + if (id == tANDDOT) { + yyerror1(loc, "&. inside multiple assignment destination"); + } +} + +static inline void +set_line_body(NODE *body, int line) +{ + if (!body) return; + switch (nd_type(body)) { + case NODE_RESCUE: + case NODE_ENSURE: + nd_set_line(body, line); + } +} + +static void +set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end) +{ + RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end); + nd_set_line(node, beg->end_pos.lineno); +} + +static NODE * +last_expr_node(NODE *expr) +{ + 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* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE); -#define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3)) - -static NODE *cond_gen(struct parser_params*,NODE*); -#define cond(node) cond_gen(parser, (node)) -static NODE *new_if_gen(struct parser_params*,NODE*,NODE*,NODE*); -#define new_if(cc,left,right) new_if_gen(parser, (cc), (left), (right)) -static NODE *logop_gen(struct parser_params*,enum node_type,NODE*,NODE*); -#define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2)) +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); +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*,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 void void_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(parser, (node) = remove_begin(node)) -#define void_expr0(node) void_expr_gen(parser, (node)) -#define void_expr(node) void_expr0((node) = remove_begin(node)) -static void void_stmts_gen(struct parser_params*,NODE*); -#define void_stmts(node) void_stmts_gen(parser, (node)) -static void reduce_nodes_gen(struct parser_params*,NODE**); -#define reduce_nodes(n) reduce_nodes_gen(parser,(n)) -static void block_dup_check_gen(struct parser_params*,NODE*,NODE*); -#define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2)) - -static NODE *block_append_gen(struct parser_params*,NODE*,NODE*); -#define block_append(h,t) block_append_gen(parser,(h),(t)) -static NODE *list_append_gen(struct parser_params*,NODE*,NODE*); -#define list_append(l,i) list_append_gen(parser,(l),(i)) +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*); + +static NODE *block_append(struct parser_params*,NODE*,NODE*); +static NODE *list_append(struct parser_params*,NODE*,NODE*); static NODE *list_concat(NODE*,NODE*); -static NODE *arg_append_gen(struct parser_params*,NODE*,NODE*); -#define arg_append(h,t) arg_append_gen(parser,(h),(t)) -static NODE *arg_concat_gen(struct parser_params*,NODE*,NODE*); -#define arg_concat(h,t) arg_concat_gen(parser,(h),(t)) -static NODE *literal_concat_gen(struct parser_params*,NODE*,NODE*); -#define literal_concat(h,t) literal_concat_gen(parser,(h),(t)) -static int literal_concat0(struct parser_params *, VALUE, VALUE); -static NODE *new_evstr_gen(struct parser_params*,NODE*); -#define new_evstr(n) new_evstr_gen(parser,(n)) -static NODE *evstr2dstr_gen(struct parser_params*,NODE*); -#define evstr2dstr(n) evstr2dstr_gen(parser,(n)) +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*,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*); static NODE *splat_array(NODE*); +static void mark_lvar_used(struct parser_params *p, NODE *rhs); -static NODE *call_bin_op_gen(struct parser_params*,NODE*,ID,NODE*); -#define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, (recv),(id),(arg1)) -static NODE *call_uni_op_gen(struct parser_params*,NODE*,ID); -#define call_uni_op(recv,id) call_uni_op_gen(parser, (recv),(id)) - -static NODE *new_args_gen(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*); -#define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t)) -static NODE *new_args_tail_gen(struct parser_params*,NODE*,ID,ID); -#define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b)) -#define new_kw_arg(k) ((k) ? NEW_KW_ARG(0, (k)) : 0) - -static VALUE negate_lit(VALUE); -static NODE *ret_args_gen(struct parser_params*,NODE*); -#define ret_args(node) ret_args_gen(parser, (node)) -static NODE *arg_blk_pass(NODE*,NODE*); -static NODE *new_yield_gen(struct parser_params*,NODE*); -#define new_yield(node) new_yield_gen(parser, (node)) -static NODE *dsym_node_gen(struct parser_params*,NODE*); -#define dsym_node(node) dsym_node_gen(parser, (node)) - -static NODE *gettable_gen(struct parser_params*,ID); -#define gettable(id) gettable_gen(parser,(id)) -static NODE *assignable_gen(struct parser_params*,ID,NODE*); -#define assignable(id,node) assignable_gen(parser, (id), (node)) - -static NODE *aryset_gen(struct parser_params*,NODE*,NODE*); -#define aryset(node1,node2) aryset_gen(parser, (node1), (node2)) -static NODE *attrset_gen(struct parser_params*,NODE*,ID,ID); -#define attrset(node,q,id) attrset_gen(parser, (node), (q), (id)) - -static void rb_backref_error_gen(struct parser_params*,NODE*); -#define rb_backref_error(n) rb_backref_error_gen(parser,(n)) -static NODE *node_assign_gen(struct parser_params*,NODE*,NODE*); -#define node_assign(node1, node2) node_assign_gen(parser, (node1), (node2)) - -static NODE *new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs); -static NODE *new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs); -#define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs)) -static NODE *new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs); -#define new_const_op_assign(lhs, op, rhs) new_const_op_assign_gen(parser, (lhs), (op), (rhs)) - -static NODE *new_hash_gen(struct parser_params *parser, NODE *hash); -#define new_hash(hash) new_hash_gen(parser, (hash)) - -#define new_defined(expr) NEW_DEFINED(remove_begin_all(expr)) - -static NODE *match_op_gen(struct parser_params*,NODE*,NODE*); -#define match_op(node1,node2) match_op_gen(parser, (node1), (node2)) - -static ID *local_tbl_gen(struct parser_params*); -#define local_tbl() local_tbl_gen(parser) - -static void fixup_nodes(NODE **); - -static VALUE reg_compile_gen(struct parser_params*, VALUE, int); -#define reg_compile(str,options) reg_compile_gen(parser, (str), (options)) -static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int); -#define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options)) -static int reg_fragment_check_gen(struct parser_params*, VALUE, int); -#define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options)) -static NODE *reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, NODE *match); -#define reg_named_capture_assign(regexp,match) reg_named_capture_assign_gen(parser,(regexp),(match)) - -#define get_id(id) (id) -#define get_value(val) (val) -#else -#define NODE_RIPPER NODE_CDECL +static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*); +static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*); +static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc); +static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc); +static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {RNODE_ITER(b)->nd_iter = m; b->nd_loc = *loc; return b;} -static inline VALUE -ripper_new_yylval(ID a, VALUE b, VALUE c) -{ - return (VALUE)NEW_CDECL(a, b, c); -} +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*); +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); +static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc); +static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc); +static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc); -static inline int -ripper_is_node_yylval(VALUE n) -{ - return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER; -} +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,ID); -#define value_expr(node) ((void)(node)) -#define remove_begin(node) (node) -#define rb_dvar_defined(id) 0 -#define rb_local_defined(id) 0 -static ID ripper_get_id(VALUE); -#define get_id(id) ripper_get_id(id) -static VALUE ripper_get_value(VALUE); -#define get_value(val) ripper_get_value(val) -static VALUE assignable_gen(struct parser_params*,VALUE); -#define assignable(lhs,node) assignable_gen(parser, (lhs)) -static int id_is_var_gen(struct parser_params *parser, ID id); -#define id_is_var(id) id_is_var_gen(parser, (id)) +static NODE* negate_lit(struct parser_params*, NODE*); +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 *dsym_node(struct parser_params*,NODE*,const YYLTYPE*); -#define node_assign(node1, node2) dispatch2(assign, (node1), (node2)) +static NODE *gettable(struct parser_params*,ID,const YYLTYPE*); +static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*); -static VALUE new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs); -static VALUE new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs); -#define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), ripper_id2sym(type), (attr), (op), (rhs)) +static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*); +static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*); -#endif /* !RIPPER */ +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, 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); + +static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc); + +static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*); +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, const YYLTYPE *keyword_loc); + +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)) + +static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc); -#define new_op_assign(lhs, op, rhs) new_op_assign_gen(parser, (lhs), (op), (rhs)) - -RUBY_FUNC_EXPORTED VALUE rb_parser_reg_compile(struct parser_params* parser, VALUE str, int options, VALUE *errmsg); - -static ID formal_argument_gen(struct parser_params*, ID); -#define formal_argument(id) formal_argument_gen(parser, (id)) -static ID shadowing_lvar_gen(struct parser_params*,ID); -#define shadowing_lvar(name) shadowing_lvar_gen(parser, (name)) -static void new_bv_gen(struct parser_params*,ID); -#define new_bv(id) new_bv_gen(parser, (id)) - -static void local_push_gen(struct parser_params*,int); -#define local_push(top) local_push_gen(parser,(top)) -static void local_pop_gen(struct parser_params*); -#define local_pop() local_pop_gen(parser) -static void local_var_gen(struct parser_params*, ID); -#define local_var(id) local_var_gen(parser, (id)) -static void arg_var_gen(struct parser_params*, ID); -#define arg_var(id) arg_var_gen(parser, (id)) -static int local_id_gen(struct parser_params*, ID); -#define local_id(id) local_id_gen(parser, (id)) -static ID internal_id_gen(struct parser_params*); -#define internal_id() internal_id_gen(parser) - -static const struct vtable *dyna_push_gen(struct parser_params *); -#define dyna_push() dyna_push_gen(parser) -static void dyna_pop_gen(struct parser_params*, const struct vtable *); -#define dyna_pop(node) dyna_pop_gen(parser, (node)) -static int dyna_in_block_gen(struct parser_params*); -#define dyna_in_block() dyna_in_block_gen(parser) -#define dyna_var(id) local_var(id) -static int dvar_defined_gen(struct parser_params*,ID,int); -#define dvar_defined(id) dvar_defined_gen(parser, (id), 0) -#define dvar_defined_get(id) dvar_defined_gen(parser, (id), 1) -static int dvar_curr_gen(struct parser_params*,ID); -#define dvar_curr(id) dvar_curr_gen(parser, (id)) - -static int lvar_defined_gen(struct parser_params*, ID); -#define lvar_defined(id) lvar_defined_gen(parser, (id)) +static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol); + +static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*); + +static rb_ast_id_table_t *local_tbl(struct parser_params*); + +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, 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); + +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); + +#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 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*, 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); +PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3); +YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc); +YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc); +YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc); +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); +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); + +static void local_push(struct parser_params*,int); +static void local_pop(struct parser_params*); +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 **); +#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*); +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); +#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); + +static NODE *numparam_push(struct parser_params *p); +static void numparam_pop(struct parser_params *p, NODE *prev_inner); + +#define METHOD_NOT '!' + +#define idFWD_REST '*' +#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */ +#define idFWD_BLOCK '&' +#define idFWD_ALL idDot3 +#define arg_FWD_BLOCK idFWD_BLOCK + +#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) @@ -573,27 +1570,29 @@ static int lvar_defined_gen(struct parser_params*, ID); #define RE_OPTION_MASK 0xff #define RE_OPTION_ARG_ENCODING_NONE 32 -#define NODE_STRTERM NODE_ZARRAY /* nothing to gc */ -#define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */ -#define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1)) -#define nd_func u1.id -#if SIZEOF_SHORT == 2 -#define nd_term(node) ((signed short)(node)->u2.id) -#else -#define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2) -#endif -#define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2) -#define nd_nest u3.cnt +#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) +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) : \ + tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \ + tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \ + tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \ + tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \ + tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \ + ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok))))) /****** Ripper *******/ #ifdef RIPPER -#define RIPPER_VERSION "0.1.0" -static inline VALUE intern_sym(const char *name); +#include "eventids1.h" +#include "eventids2.h" -#include "eventids1.c" -#include "eventids2.c" +extern const struct ripper_parser_ids ripper_parser_ids; static VALUE ripper_dispatch0(struct parser_params*,ID); static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE); @@ -602,84 +1601,126 @@ static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE); static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE); static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE); static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE); -static void ripper_error_gen(struct parser_params *parser); -#define ripper_error() ripper_error_gen(parser) +void ripper_error(struct parser_params *p); -#define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n)) -#define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a)) -#define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b)) -#define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c)) -#define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d)) -#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e)) -#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(parser, 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 ripper_intern(s) ID2SYM(rb_intern(s)) -static VALUE ripper_id2sym(ID); -#ifdef __GNUC__ -#define ripper_id2sym(id) (rb_ispunct((int)(id)) ? \ - ID2SYM(id) : ripper_id2sym(id)) -#endif - -#define arg_new() dispatch0(args_new) -#define arg_add(l,a) dispatch2(args_add, (l), (a)) -#define arg_add_star(l,a) dispatch2(args_add_star, (l), (a)) -#define arg_add_block(l,b) dispatch2(args_add_block, (l), (b)) -#define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b))) -#define bare_assoc(v) dispatch1(bare_assoc_hash, (v)) -#define arg_add_assocs(l,b) arg_add((l), bare_assoc(b)) - -#define args2mrhs(a) dispatch1(mrhs_new_from_args, (a)) -#define mrhs_new() dispatch0(mrhs_new) -#define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a)) -#define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a)) +static VALUE +aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args) +{ + if (!NIL_P(pre_arg)) { + if (!NIL_P(pre_args)) { + rb_ary_unshift(pre_args, pre_arg); + } + else { + pre_args = rb_ary_new_from_args(1, pre_arg); + } + } + return pre_args; +} -#define mlhs_new() dispatch0(mlhs_new) -#define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a)) -#define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a)) +#define ID2VAL(id) STATIC_ID2SYM(id) +#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t)) +#endif /* RIPPER */ -#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \ - dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk)) +#define KWD2EID(t, v) keyword_##t -#define blockvar_new(p,v) dispatch2(block_var, (p), (v)) -#define blockvar_add_star(l,a) dispatch2(block_var_add_star, (l), (a)) -#define blockvar_add_block(l,a) dispatch2(block_var_add_block, (l), (a)) +static NODE * +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, parent, loc); + nd_set_line(n, loc->end_pos.lineno); + set_line_body(body, loc->beg_pos.lineno); + return n; +} -#define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a))) -#define method_arg(m,a) dispatch2(method_add_arg,(m),(a)) -#define method_add_block(m,b) dispatch2(method_add_block, (m), (b)) +static NODE * +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, 0, remove_begin(rescue), 0, &loc); + loc.beg_pos = arg_loc->beg_pos; + return NEW_RESCUE(arg, rescue, 0, &loc); +} -#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x)) +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); +static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits); +static void clear_block_exit(struct parser_params *p, bool error); -static inline VALUE -new_args_gen(struct parser_params *parser, VALUE f, VALUE o, VALUE r, VALUE p, VALUE tail) +static void +next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def) { - NODE *t = (NODE *)tail; - VALUE k = t->u1.value, kr = t->u2.value, b = t->u3.value; - return params_new(f, o, r, p, k, kr, escape_Qundef(b)); + next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def; } -#define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t)) -static inline VALUE -new_args_tail_gen(struct parser_params *parser, VALUE k, VALUE kr, VALUE b) +static void +restore_defun(struct parser_params *p, rb_node_def_temp_t *temp) { - return (VALUE)MEMO_NEW(k, kr, b); + /* See: def_name action */ + struct lex_context ctxt = temp->save.ctxt; + p->ctxt.in_def = ctxt.in_def; + p->ctxt.shareable_constant_value = ctxt.shareable_constant_value; + p->ctxt.in_rescue = ctxt.in_rescue; + p->max_numparam = temp->save.max_numparam; + numparam_pop(p, temp->save.numparam_save); + clear_block_exit(p, true); } -#define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b)) -#define new_defined(expr) dispatch1(defined, (expr)) +static void +endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc) +{ + if (is_attrset_id(mid)) { + yyerror1(loc, "setter method cannot be defined in an endless method definition"); + } + token_info_drop(p, "def", loc->beg_pos); +} -#define FIXME 0 +#define debug_token_line(p, name, line) do { \ + if (p->debug) { \ + const char *const pcur = p->lex.pcur; \ + const char *const ptok = p->lex.ptok; \ + rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \ + line, p->ruby_sourceline, \ + ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \ + } \ + } while (0) -#endif /* RIPPER */ +#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 ifndef_ripper(x) (x) +# define ifdef_ripper(r,x) (x) #else -# define Qnone Qnil # define ifndef_ripper(x) +# define ifdef_ripper(r,x) (r) #endif # define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1)) @@ -703,156 +1744,1079 @@ new_args_tail_gen(struct parser_params *parser, VALUE k, VALUE kr, VALUE b) # define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c)) # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d)) #ifdef RIPPER -static ID id_warn, id_warning; +extern const ID id_warn, id_warning, id_gets, id_assoc; +# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */ +# define WARN_S_L(s,l) STR_NEW(s,l) # define WARN_S(s) STR_NEW2(s) # define WARN_I(i) INT2NUM(i) -# define PRIsWARN "s" -# define WARN_ARGS(fmt,n) parser->value, id_warn, n, rb_usascii_str_new_lit(fmt) +# define WARN_ID(i) rb_id2str(i) +# 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 +# define WARN_CALL(...) rb_funcall(__VA_ARGS__) +# else # define WARN_CALL rb_funcall -# define WARNING_ARGS(fmt,n) parser->value, id_warning, n, rb_usascii_str_new_lit(fmt) +# endif +# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt) # define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n) +# ifdef HAVE_VA_ARGS_MACRO +# define WARNING_CALL(...) rb_funcall(__VA_ARGS__) +# else # define WARNING_CALL rb_funcall -static void ripper_compile_error(struct parser_params*, const char *fmt, ...); -# define rb_compile_error ripper_compile_error +# endif # define compile_error ripper_compile_error -# define PARSER_ARG parser, #else +# define WARN_S_L(s,l) s # define WARN_S(s) s # define WARN_I(i) i +# define WARN_ID(i) rb_id2name(i) # define PRIsWARN PRIsVALUE -# define WARN_ARGS(fmt,n) ruby_sourcefile, ruby_sourceline, (fmt) -# define WARN_ARGS_L(l,fmt,n) ruby_sourcefile, (l), (fmt) +# 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 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 -# define rb_compile_error rb_compile_error_with_enc -# define compile_error (parser->has_err = 1),rb_compile_error_with_enc -# define PARSER_ARG ruby_sourcefile, ruby_sourceline, (void *)current_enc, +PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4); +# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__) +#endif + +#define RNODE_EXITS(node) ((rb_node_exits_t*)(node)) + +static NODE * +add_block_exit(struct parser_params *p, NODE *node) +{ + if (!node) { + compile_error(p, "unexpected null node"); + return 0; + } + switch (nd_type(node)) { + case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break; + default: + 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_stts)->nd_chain = node; + exits->nd_stts = node; + } + } + return node; +} + +static rb_node_exits_t * +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_stts = RNODE(exits); + p->exits = exits; + return old; +} + +static rb_node_exits_t * +allow_block_exit(struct parser_params *p) +{ + rb_node_exits_t *exits = p->exits; + p->exits = 0; + return exits; +} + +static void +restore_block_exit(struct parser_params *p, rb_node_exits_t *exits) +{ + p->exits = exits; +} + +static void +clear_block_exit(struct parser_params *p, bool error) +{ + rb_node_exits_t *exits = p->exits; + if (!exits) return; + if (error) { + for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) { + switch (nd_type(e)) { + case NODE_BREAK: + yyerror1(&e->nd_loc, "Invalid break"); + break; + case NODE_NEXT: + yyerror1(&e->nd_loc, "Invalid next"); + break; + case NODE_REDO: + yyerror1(&e->nd_loc, "Invalid redo"); + break; + default: + yyerror1(&e->nd_loc, "unexpected node"); + goto end_checks; /* no nd_chain */ + } + } + end_checks:; + } + 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)0) +static int looking_at_eol_p(struct parser_params *p); + +static NODE * +get_nd_value(struct parser_params *p, NODE *node) +{ + switch (nd_type(node)) { + case NODE_GASGN: + return RNODE_GASGN(node)->nd_value; + case NODE_IASGN: + return RNODE_IASGN(node)->nd_value; + case NODE_LASGN: + return RNODE_LASGN(node)->nd_value; + case NODE_DASGN: + return RNODE_DASGN(node)->nd_value; + case NODE_MASGN: + return RNODE_MASGN(node)->nd_value; + case NODE_CVASGN: + return RNODE_CVASGN(node)->nd_value; + case NODE_CDECL: + return RNODE_CDECL(node)->nd_value; + default: + compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node))); + return 0; + } +} + +static void +set_nd_value(struct parser_params *p, NODE *node, NODE *rhs) +{ + switch (nd_type(node)) { + case NODE_CDECL: + RNODE_CDECL(node)->nd_value = rhs; + break; + case NODE_GASGN: + RNODE_GASGN(node)->nd_value = rhs; + break; + case NODE_IASGN: + RNODE_IASGN(node)->nd_value = rhs; + break; + case NODE_LASGN: + RNODE_LASGN(node)->nd_value = rhs; + break; + case NODE_DASGN: + RNODE_DASGN(node)->nd_value = rhs; + break; + case NODE_MASGN: + RNODE_MASGN(node)->nd_value = rhs; + break; + case NODE_CVASGN: + RNODE_CVASGN(node)->nd_value = rhs; + break; + default: + compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node))); + break; + } +} + +static ID +get_nd_vid(struct parser_params *p, NODE *node) +{ + switch (nd_type(node)) { + case NODE_CDECL: + return RNODE_CDECL(node)->nd_vid; + case NODE_GASGN: + return RNODE_GASGN(node)->nd_vid; + case NODE_IASGN: + return RNODE_IASGN(node)->nd_vid; + case NODE_LASGN: + return RNODE_LASGN(node)->nd_vid; + case NODE_DASGN: + return RNODE_DASGN(node)->nd_vid; + case NODE_CVASGN: + return RNODE_CVASGN(node)->nd_vid; + default: + compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node))); + return 0; + } +} + +static NODE * +get_nd_args(struct parser_params *p, NODE *node) +{ + switch (nd_type(node)) { + case NODE_CALL: + return RNODE_CALL(node)->nd_args; + case NODE_OPCALL: + return RNODE_OPCALL(node)->nd_args; + case NODE_FCALL: + return RNODE_FCALL(node)->nd_args; + case NODE_QCALL: + return RNODE_QCALL(node)->nd_args; + case NODE_SUPER: + return RNODE_SUPER(node)->nd_args; + case NODE_VCALL: + case NODE_ZSUPER: + case NODE_YIELD: + case NODE_RETURN: + case NODE_BREAK: + case NODE_NEXT: + return 0; + default: + 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 {\ + SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \ + 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 -/* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150, - for instance). This is too low for Ruby to parse some files, such as - date/format.rb, therefore bump the value up to at least Bison's default. */ -#ifdef OLD_YACC -#ifndef YYMAXDEPTH -#define YYMAXDEPTH 10000 +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 void token_info_push(struct parser_params*, const char *token, size_t len); -static void token_info_pop(struct parser_params*, const char *token, size_t len); -#define token_info_push(token) (RTEST(ruby_verbose) ? token_info_push(parser, (token), rb_strlen_lit(token)) : (void)0) -#define token_info_pop(token) (RTEST(ruby_verbose) ? token_info_pop(parser, (token), rb_strlen_lit(token)) : (void)0) -#else -#define token_info_push(token) /* nothing */ -#define token_info_pop(token) /* nothing */ +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 */ %} -%pure-parser -%lex-param {struct parser_params *parser} -%parse-param {struct parser_params *parser} +%expect 0 +%define api.pure +%define parse.error verbose +%printer { + if ((NODE *)$$ == (NODE *)-1) { + rb_parser_printf(p, "NODE_SPECIAL"); + } + else if ($$) { + rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$)))); + } +} <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 { + rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$)); +} <id> +%printer { + 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 { + rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth); +} tNTH_REF +%printer { + rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth); +} 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; + rb_node_args_aux_t *node_args_aux; + rb_node_opt_arg_t *node_opt_arg; + rb_node_kw_arg_t *node_kw_arg; + rb_node_block_pass_t *node_block_pass; + 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; -} - -/*%%%*/ -%token -/*% -%token <val> -%*/ - keyword_class - keyword_module - keyword_def - keyword_undef - keyword_begin - keyword_rescue - keyword_ensure - keyword_end - keyword_if - keyword_unless - keyword_then - keyword_elsif - keyword_else - keyword_case - keyword_when - keyword_while - keyword_until - keyword_for - keyword_break - keyword_next - keyword_redo - keyword_retry - keyword_in - keyword_do - keyword_do_cond - keyword_do_block - keyword_do_LAMBDA - keyword_return - keyword_yield - keyword_super - keyword_self - keyword_nil - keyword_true - keyword_false - keyword_and - keyword_or - keyword_not - modifier_if - modifier_unless - modifier_while - modifier_until - modifier_rescue - keyword_alias - keyword_defined - keyword_BEGIN - keyword_END - keyword__LINE__ - keyword__FILE__ - keyword__ENCODING__ - -%token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL -%token <node> tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR -%token <node> tNTH_REF tBACK_REF + 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__'" + +%token <id> tIDENTIFIER "local variable or method" +%token <id> tFID "method" +%token <id> tGVAR "global variable" +%token <id> tIVAR "instance variable" +%token <id> tCONSTANT "constant" +%token <id> tCVAR "class variable" +%token <id> tLABEL "label" +%token <node> tINTEGER "integer literal" +%token <node> tFLOAT "float literal" +%token <node> tRATIONAL "rational literal" +%token <node> tIMAGINARY "imaginary literal" +%token <node> tCHAR "char literal" +%token <node> tNTH_REF "numbered reference" +%token <node> tBACK_REF "back reference" +%token <node> tSTRING_CONTENT "literal content" %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 dsym cpath -%type <node> top_compstmt top_stmts top_stmt -%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call -%type <node> expr_value arg_value primary_value fcall -%type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure -%type <node> args call_args opt_call_args -%type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail -%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs +%type <node> literal numeric simple_numeric ssym dsym symbol cpath +%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 arg_splat call_args opt_call_args +%type <node> paren_args opt_paren_args +%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> f_block_optarg f_block_opt -%type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs +%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args +%type <node_args_aux> f_arg f_arg_item +%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> block_param opt_block_param block_param_def f_opt -%type <node> f_kwarg f_kw f_block_kwarg f_block_kw -%type <node> bv_decls opt_bv_decl bvar -%type <node> lambda f_larglist lambda_body +%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 %type <node> brace_block cmd_brace_block do_block lhs none fitem -%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner -%type <id> fsym keyword_variable user_variable sym symbol operation operation2 operation3 +%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 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> f_kwrest f_label f_arg_asgn call_op call_op2 -/*%%%*/ -/*% -%type <val> program reswords then do dot_or_colon -%*/ +%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 +%type <num> max_numparam +%type <node> numparam +%type <id> it_id %token END_OF_INPUT 0 "end-of-input" +%token <id> '.' + +/* escaped chars, should be ignored otherwise */ +%token <id> '\\' "backslash" +%token tSP "escaped space" +%token <id> '\t' "escaped horizontal tab" +%token <id> '\f' "escaped form feed" +%token <id> '\r' "escaped carriage return" +%token <id> '\13' "escaped vertical tab" %token tUPLUS RUBY_TOKEN(UPLUS) "unary+" %token tUMINUS RUBY_TOKEN(UMINUS) "unary-" %token tPOW RUBY_TOKEN(POW) "**" @@ -868,27 +2832,41 @@ static void token_info_pop(struct parser_params*, const char *token, size_t len) %token tNMATCH RUBY_TOKEN(NMATCH) "!~" %token tDOT2 RUBY_TOKEN(DOT2) ".." %token tDOT3 RUBY_TOKEN(DOT3) "..." +%token tBDOT2 RUBY_TOKEN(BDOT2) "(.." +%token tBDOT3 RUBY_TOKEN(BDOT3) "(..." %token tAREF RUBY_TOKEN(AREF) "[]" %token tASET RUBY_TOKEN(ASET) "[]=" %token tLSHFT RUBY_TOKEN(LSHFT) "<<" %token tRSHFT RUBY_TOKEN(RSHFT) ">>" -%token tDOTQ RUBY_TOKEN(DOTQ) ".?" -%token tCOLON2 "::" +%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&." +%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::" %token tCOLON3 ":: at EXPR_BEG" -%token <id> tOP_ASGN /* +=, -= etc. */ +%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */ %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 tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG -%token tSTRING_DBEG tSTRING_DEND tSTRING_DVAR tSTRING_END tLAMBEG tLABEL_END +%token <num> tLAMBDA "->" +%token tSYMBEG "symbol literal" +%token tSTRING_BEG "string literal" +%token tXSTRING_BEG "backtick literal" +%token tREGEXP_BEG "regexp literal" +%token tWORDS_BEG "word list" +%token tQWORDS_BEG "verbatim word list" +%token tSYMBOLS_BEG "symbol list" +%token tQSYMBOLS_BEG "verbatim symbol list" +%token tSTRING_END "terminator" +%token tSTRING_DEND "'}'" +%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__ /* * precedence table @@ -897,14 +2875,14 @@ static void token_info_pop(struct parser_params*, const char *token, size_t len) %nonassoc tLOWEST %nonassoc tLBRACE_ARG -%nonassoc modifier_if modifier_unless modifier_while modifier_until +%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in %left keyword_or keyword_and %right keyword_not %nonassoc keyword_defined %right '=' tOP_ASGN %left modifier_rescue %right '?' ':' -%nonassoc tDOT2 tDOT3 +%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3 %left tOROP %left tANDOP %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH @@ -920,4768 +2898,4640 @@ static void token_info_pop(struct parser_params*, const char *token, size_t len) %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) <node_args> + : f_kwarg(value) ',' f_kwrest opt_f_block_arg + { + $$ = new_args_tail(p, $1, $3, $4, &@3); + /*% ripper: [$:1, $:3, $:4] %*/ + } + | f_kwarg(value) opt_f_block_arg + { + $$ = new_args_tail(p, $1, 0, $2, &@1); + /*% ripper: [$:1, Qnil, $:2] %*/ + } + | f_any_kwrest opt_f_block_arg + { + $$ = 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 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!($:1, $:2, $:4) %*/ + } + | 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!($:1, $:3), $:5, $:7) %*/ + } + | 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!($:1, $:2, $:3), $:4, $:6) %*/ + } + | 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!($:1, $:2, $:3), $:4, $:6) %*/ + } + | 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!($:1, $:2, $:3), $:4, $:6) %*/ + } + | 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!($:1, $:3), $:4, $:6) %*/ + } + | 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!($:2), $:3, $:5) %*/ + } + | 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!($:1), $:2, $:4)) %*/ + } + ; + +%rule opt_args_tail(tail) <node_args> + : ',' tail + { + $$ = $tail; + /*% ripper: $:2 %*/ + } + | /* none */ + { + $$ = new_args_tail(p, 0, 0, 0, &@0); + /*% 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!($:3) %*/ + } + ; + %% program : { - lex_state = EXPR_BEG; - /*%%%*/ - local_push(compile_for_eval || rb_parse_in_main()); - /*% - local_push(0); - %*/ - } - top_compstmt - { - /*%%%*/ - if ($2 && !compile_for_eval) { - /* last expression should not be void */ - if (nd_type($2) != NODE_BLOCK) void_expr($2); - else { - NODE *node = $2; - while (node->nd_next) { - node = node->nd_next; - } - void_expr(node->nd_head); - } - } - ruby_eval_tree = NEW_SCOPE(0, block_append(ruby_eval_tree, $2)); - /*% - $$ = $2; - parser->result = dispatch1(program, $$); - %*/ - local_pop(); - } - ; - -top_compstmt : top_stmts opt_terms - { - /*%%%*/ - void_stmts($1); - fixup_nodes(&deferred_nodes); - /*% - %*/ - $$ = $1; - } - ; + SET_LEX_STATE(EXPR_BEG); + local_push(p, ifndef_ripper(1)+0); + /* jumps are possible in the top-level loop. */ + if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p); + } + compstmt(top_stmts) + { + if ($2 && !compile_for_eval) { + NODE *node = $2; + /* last expression should not be void */ + if (nd_type_p(node, NODE_BLOCK)) { + while (RNODE_BLOCK(node)->nd_next) { + node = RNODE_BLOCK(node)->nd_next; + } + node = RNODE_BLOCK(node)->nd_head; + } + node = remove_begin(node); + void_expr(p, node); + } + p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), NULL, &@$); + /*% ripper[final]: program!($:2) %*/ + local_pop(p); + } + ; top_stmts : none { - /*%%%*/ - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch2(stmts_add, dispatch0(stmts_new), - dispatch0(void_stmt)); - %*/ - } - | top_stmt - { - /*%%%*/ - $$ = newline_node($1); - /*% - $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1); - %*/ - } - | top_stmts terms top_stmt - { - /*%%%*/ - $$ = block_append($1, newline_node($3)); - /*% - $$ = dispatch2(stmts_add, $1, $3); - %*/ - } - | error top_stmt - { - $$ = remove_begin($2); - } - ; + $$ = NEW_BEGIN(0, &@$); + /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/ + } + | top_stmt + { + $$ = newline_node($1); + /*% ripper: stmts_add!(stmts_new!, $:1) %*/ + } + | top_stmts terms top_stmt + { + $$ = block_append(p, $1, newline_node($3)); + /*% ripper: stmts_add!($:1, $:3) %*/ + } + ; top_stmt : stmt - | keyword_BEGIN - { - /*%%%*/ - /* local_push(0); */ - /*% - %*/ - } - '{' top_compstmt '}' - { - /*%%%*/ - ruby_eval_tree_begin = block_append(ruby_eval_tree_begin, - $4); - /* NEW_PREEXE($4)); */ - /* local_pop(); */ - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch1(BEGIN, $4); - %*/ - } - ; - -bodystmt : compstmt - opt_rescue - opt_else - opt_ensure - { - /*%%%*/ - $$ = $1; - if ($2) { - $$ = NEW_RESCUE($1, $2, $3); - } - else if ($3) { - rb_warn0("else without rescue is useless"); - $$ = block_append($$, $3); - } - if ($4) { - if ($$) { - $$ = NEW_ENSURE($$, $4); - } - else { - $$ = block_append($4, NEW_NIL()); - } - } - fixpos($$, $1); - /*% - $$ = dispatch4(bodystmt, - escape_Qundef($1), - escape_Qundef($2), - escape_Qundef($3), - escape_Qundef($4)); - %*/ - } - ; - -compstmt : stmts opt_terms - { - /*%%%*/ - void_stmts($1); - fixup_nodes(&deferred_nodes); - /*% - %*/ - $$ = $1; - } - ; + { + clear_block_exit(p, true); + $$ = $1; + } + | keyword_BEGIN begin_block + { + $$ = $2; + /*% ripper: $:2 %*/ + } + ; + +block_open : '{' {$$ = init_block_exit(p);}; + +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(0, &@$); + /*% ripper: BEGIN!($:2) %*/ + } + ; + +bodystmt : compstmt(stmts)[body] + lex_ctxt[ctxt] + opt_rescue + k_else + { + if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless"); + next_rescue_context(&p->ctxt, &$ctxt, after_else); + } + 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) %*/ + } + | compstmt(stmts)[body] + lex_ctxt[ctxt] + opt_rescue + { + next_rescue_context(&p->ctxt, &$ctxt, after_ensure); + } + opt_ensure + { + $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$); + /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/ + } + ; stmts : none { - /*%%%*/ - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch2(stmts_add, dispatch0(stmts_new), - dispatch0(void_stmt)); - %*/ - } - | stmt_or_begin - { - /*%%%*/ - $$ = newline_node($1); - /*% - $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1); - %*/ - } - | stmts terms stmt_or_begin - { - /*%%%*/ - $$ = block_append($1, newline_node($3)); - /*% - $$ = dispatch2(stmts_add, $1, $3); - %*/ - } - | error stmt - { - $$ = remove_begin($2); - } - ; + $$ = NEW_BEGIN(0, &@$); + /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/ + } + | stmt_or_begin + { + $$ = newline_node($1); + /*% ripper: stmts_add!(stmts_new!, $:1) %*/ + } + | stmts terms stmt_or_begin + { + $$ = block_append(p, $1, newline_node($3)); + /*% ripper: stmts_add!($:1, $:3) %*/ + } + ; stmt_or_begin : stmt - { - $$ = $1; - } | keyword_BEGIN - { - yyerror("BEGIN is permitted only at toplevel"); - /*%%%*/ - /* local_push(0); */ - /*% - %*/ - } - '{' top_compstmt '}' - { - /*%%%*/ - ruby_eval_tree_begin = block_append(ruby_eval_tree_begin, - $4); - /* NEW_PREEXE($4)); */ - /* local_pop(); */ - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch1(BEGIN, $4); - %*/ - } - -stmt : keyword_alias fitem {lex_state = EXPR_FNAME;} fitem - { - /*%%%*/ - $$ = NEW_ALIAS($2, $4); - /*% - $$ = dispatch2(alias, $2, $4); - %*/ - } - | keyword_alias tGVAR tGVAR - { - /*%%%*/ - $$ = NEW_VALIAS($2, $3); - /*% - $$ = dispatch2(var_alias, $2, $3); - %*/ - } - | keyword_alias tGVAR tBACK_REF - { - /*%%%*/ - char buf[2]; - buf[0] = '$'; - buf[1] = (char)$3->nd_nth; - $$ = NEW_VALIAS($2, rb_intern2(buf, 2)); - /*% - $$ = dispatch2(var_alias, $2, $3); - %*/ - } - | keyword_alias tGVAR tNTH_REF - { - /*%%%*/ - yyerror("can't make alias for the number variables"); - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch2(var_alias, $2, $3); - $$ = dispatch1(alias_error, $$); - ripper_error(); - %*/ - } - | keyword_undef undef_list - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(undef, $2); - %*/ - } - | stmt modifier_if expr_value - { - /*%%%*/ - $$ = new_if($3, remove_begin($1), 0); - fixpos($$, $3); - /*% - $$ = dispatch2(if_mod, $3, $1); - %*/ - } - | stmt modifier_unless expr_value - { - /*%%%*/ - $$ = NEW_UNLESS(cond($3), remove_begin($1), 0); - fixpos($$, $3); - /*% - $$ = dispatch2(unless_mod, $3, $1); - %*/ - } - | stmt modifier_while expr_value - { - /*%%%*/ - if ($1 && nd_type($1) == NODE_BEGIN) { - $$ = NEW_WHILE(cond($3), $1->nd_body, 0); - } - else { - $$ = NEW_WHILE(cond($3), $1, 1); - } - /*% - $$ = dispatch2(while_mod, $3, $1); - %*/ - } - | stmt modifier_until expr_value - { - /*%%%*/ - if ($1 && nd_type($1) == NODE_BEGIN) { - $$ = NEW_UNTIL(cond($3), $1->nd_body, 0); - } - else { - $$ = NEW_UNTIL(cond($3), $1, 1); - } - /*% - $$ = dispatch2(until_mod, $3, $1); - %*/ - } - | stmt modifier_rescue stmt - { - /*%%%*/ - NODE *resq = NEW_RESBODY(0, remove_begin($3), 0); - $$ = NEW_RESCUE(remove_begin($1), resq, 0); - /*% - $$ = dispatch2(rescue_mod, $1, $3); - %*/ - } - | keyword_END '{' compstmt '}' - { - if (in_def || in_single) { - rb_warn0("END in method; use at_exit"); - } - /*%%%*/ - $$ = NEW_POSTEXE(NEW_NODE( - NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */)); - /*% - $$ = dispatch1(END, $3); - %*/ - } - | command_asgn - | mlhs '=' command_call - { - /*%%%*/ - value_expr($3); - $1->nd_value = $3; - $$ = $1; - /*% - $$ = dispatch2(massign, $1, $3); - %*/ - } - | var_lhs tOP_ASGN command_call - { - value_expr($3); - $$ = new_op_assign($1, $2, $3); - } - | primary_value '[' opt_call_args rbracket tOP_ASGN command_call - { - /*%%%*/ - NODE *args; - - value_expr($6); - if (!$3) $3 = NEW_ZARRAY(); - args = arg_concat($3, $6); - if ($5 == tOROP) { - $5 = 0; - } - else if ($5 == tANDOP) { - $5 = 1; - } - $$ = NEW_OP_ASGN1($1, $5, args); - fixpos($$, $1); - /*% - $$ = dispatch2(aref_field, $1, escape_Qundef($3)); - $$ = dispatch3(opassign, $$, $5, $6); - %*/ - } - | primary_value call_op tIDENTIFIER tOP_ASGN command_call - { - value_expr($5); - $$ = new_attr_op_assign($1, $2, $3, $4, $5); - } - | primary_value call_op tCONSTANT tOP_ASGN command_call - { - value_expr($5); - $$ = new_attr_op_assign($1, $2, $3, $4, $5); - } - | primary_value tCOLON2 tCONSTANT tOP_ASGN command_call - { - /*%%%*/ - $$ = NEW_COLON2($1, $3); - $$ = new_const_op_assign($$, $4, $5); - /*% - $$ = dispatch2(const_path_field, $1, $3); - $$ = dispatch3(opassign, $$, $4, $5); - %*/ - } - | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call - { - value_expr($5); - $$ = new_attr_op_assign($1, idCOLON2, $3, $4, $5); - } - | backref tOP_ASGN command_call - { - /*%%%*/ - rb_backref_error($1); - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch2(assign, dispatch1(var_field, $1), $3); - $$ = dispatch1(assign_error, $$); - ripper_error(); - %*/ - } - | lhs '=' mrhs - { - /*%%%*/ - value_expr($3); - $$ = node_assign($1, $3); - /*% - $$ = dispatch2(assign, $1, $3); - %*/ - } - | mlhs '=' mrhs_arg - { - /*%%%*/ - $1->nd_value = $3; - $$ = $1; - /*% - $$ = dispatch2(massign, $1, $3); - %*/ - } - | expr - ; - -command_asgn : lhs '=' command_call - { - /*%%%*/ - value_expr($3); - $$ = node_assign($1, $3); - /*% - $$ = dispatch2(assign, $1, $3); - %*/ - } - | lhs '=' command_asgn - { - /*%%%*/ - value_expr($3); - $$ = node_assign($1, $3); - /*% - $$ = dispatch2(assign, $1, $3); - %*/ - } - ; + { + yyerror1(&@1, "BEGIN is permitted only at toplevel"); + } + begin_block + { + $$ = $3; + } + ; +allow_exits : {$$ = allow_block_exit(p);}; + +k_END : keyword_END lex_ctxt + { + $$ = $2; + p->ctxt.in_rescue = before_rescue; + /*% ripper: $:2 %*/ + }; + +stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem + { + $$ = NEW_ALIAS($2, $4, &@$, &@1); + /*% ripper: alias!($:2, $:4) %*/ + } + | keyword_alias tGVAR tGVAR + { + $$ = NEW_VALIAS($2, $3, &@$, &@1); + /*% ripper: var_alias!($:2, $:3) %*/ + } + | keyword_alias tGVAR tBACK_REF + { + char buf[2]; + buf[0] = '$'; + buf[1] = (char)RNODE_BACK_REF($3)->nd_nth; + $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1); + /*% ripper: var_alias!($:2, $:3) %*/ + } + | keyword_alias tGVAR tNTH_REF + { + static const char mesg[] = "can't make alias for the number variables"; + /*%%%*/ + yyerror1(&@3, mesg); + /*% %*/ + $$ = NEW_ERROR(&@$); + /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/ + } + | keyword_undef undef_list + { + nd_set_first_loc($2, @1.beg_pos); + RNODE_UNDEF($2)->keyword_loc = @1; + $$ = $2; + /*% ripper: undef!($:2) %*/ + } + | stmt modifier_if expr_value + { + $$ = new_if(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC); + fixpos($$, $3); + /*% ripper: if_mod!($:3, $:1) %*/ + } + | stmt modifier_unless expr_value + { + $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC); + fixpos($$, $3); + /*% ripper: unless_mod!($:3, $:1) %*/ + } + | stmt modifier_while expr_value + { + 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, &@$, &@2, &NULL_LOC); + } + else { + $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC); + } + /*% ripper: while_mod!($:3, $:1) %*/ + } + | stmt modifier_until expr_value + { + 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, &@$, &@2, &NULL_LOC); + } + else { + $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC); + } + /*% ripper: until_mod!($:3, $:1) %*/ + } + | stmt modifier_rescue after_rescue stmt + { + p->ctxt.in_rescue = $3.in_rescue; + NODE *resq; + YYLTYPE loc = code_loc_gen(&@2, &@4); + resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc); + $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$); + /*% ripper: rescue_mod!($:1, $:4) %*/ + } + | k_END allow_exits '{' compstmt(stmts) '}' + { + if (p->ctxt.in_def) { + rb_warn0("END in method; use at_exit"); + } + restore_block_exit(p, $allow_exits); + p->ctxt = $k_END; + { + NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, NULL /* parent */, &@$); + $$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5); + RNODE_SCOPE(scope)->nd_parent = $$; + } + /*% ripper: END!($:compstmt) %*/ + } + | command_asgn + | mlhs '=' lex_ctxt command_call_value + { + $$ = node_assign(p, (NODE *)$1, $4, $3, &@$); + /*% ripper: massign!($:1, $:4) %*/ + } + | asgn(mrhs) + | mlhs '=' lex_ctxt mrhs_arg modifier_rescue + after_rescue stmt[resbody] + { + p->ctxt.in_rescue = $3.in_rescue; + YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody); + $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)) %*/ + } + | mlhs '=' lex_ctxt mrhs_arg + { + $$ = node_assign(p, (NODE *)$1, $4, $3, &@$); + /*% ripper: massign!($:1, $:4) %*/ + } + | expr + | error + { + (void)yynerrs; + $$ = NEW_ERROR(&@$); + } + ; + +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) %*/ + } + | 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_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); + $$ = 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 - { - /*%%%*/ - $$ = logop(NODE_AND, $1, $3); - /*% - $$ = dispatch3(binary, $1, ripper_intern("and"), $3); - %*/ - } - | expr keyword_or expr - { - /*%%%*/ - $$ = logop(NODE_OR, $1, $3); - /*% - $$ = dispatch3(binary, $1, ripper_intern("or"), $3); - %*/ - } - | keyword_not opt_nl expr - { - /*%%%*/ - $$ = call_uni_op(cond($3), '!'); - /*% - $$ = dispatch2(unary, ripper_intern("not"), $3); - %*/ - } - | '!' command_call - { - /*%%%*/ - $$ = call_uni_op(cond($2), '!'); - /*% - $$ = dispatch2(unary, ripper_id2sym('!'), $2); - %*/ - } - | arg - ; - -expr_value : expr - { - /*%%%*/ - value_expr($1); - $$ = $1; - if (!$$) $$ = NEW_NIL(); - /*% - $$ = $1; - %*/ - } - ; + | expr keyword_and expr + { + $$ = logop(p, idAND, $1, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/ + } + | expr keyword_or expr + { + $$ = logop(p, idOR, $1, $3, &@2, &@$); + /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/ + } + | keyword_not '\n'? expr + { + $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$); + /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/ + } + | '!' command_call + { + $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$); + /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/ + } + | arg tASSOC + { + value_expr(p, $arg); + } + p_in_kwarg[ctxt] p_pvtbl p_pktbl + p_top_expr_body[body] + { + 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; + $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@2), &@$, &NULL_LOC, &NULL_LOC); + /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/ + } + | arg keyword_in + { + value_expr(p, $arg); + } + p_in_kwarg[ctxt] p_pvtbl p_pktbl + p_top_expr_body[body] + { + 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; + $$ = 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 + { + numparam_name(p, $fname); + local_push(p, 0); + p->ctxt.in_def = 1; + p->ctxt.in_rescue = before_rescue; + p->ctxt.cant_return = 0; + $$ = $fname; + } + ; + +defn_head : k_def def_name + { + $$ = def_head_save(p, $k_def); + $$->nd_mid = $def_name; + $$->nd_def = NEW_DEFN($def_name, 0, &@$); + /*% ripper: $:def_name %*/ + } + ; + +defs_head : k_def singleton dot_or_colon + { + SET_LEX_STATE(EXPR_FNAME); + } + def_name + { + SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */ + $$ = def_head_save(p, $k_def); + $$->nd_mid = $def_name; + $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$); + /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/ + } + ; + +expr_value : value_expr(expr) + | error + { + $$ = NEW_ERROR(&@$); + } + ; + +expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();} + { + $$ = $2; + /*% ripper: $:2 %*/ + } + ; command_call : command - | block_command - ; + | block_command + ; + +command_call_value : value_expr(command_call) + ; block_command : block_call - | block_call call_op2 operation2 command_args - { - /*%%%*/ - $$ = NEW_CALL($1, $3, $4); - /*% - $$ = dispatch3(call, $1, $2, $3); - $$ = method_arg($$, $4); - %*/ - } - ; - -cmd_brace_block : tLBRACE_ARG - { - $<vars>1 = dyna_push(); - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% - %*/ - } - opt_block_param - compstmt - '}' - { - /*%%%*/ - $$ = NEW_ITER($3,$4); - nd_set_line($$, $<num>2); - /*% - $$ = dispatch2(brace_block, escape_Qundef($3), $4); - %*/ - dyna_pop($<vars>1); - } - ; + | block_call call_op2 operation2 command_args + { + $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$); + /*% 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); - nd_set_line($$, tokline); - /*% - %*/ - } - ; + { + $$ = NEW_FCALL($1, 0, &@$); + /*% ripper: $:1 %*/ + } + ; command : fcall command_args %prec tLOWEST - { - /*%%%*/ - $$ = $1; - $$->nd_args = $2; - /*% - $$ = dispatch2(command, $1, $2); - %*/ - } - | fcall command_args cmd_brace_block - { - /*%%%*/ - block_dup_check($2,$3); - $1->nd_args = $2; - $3->nd_iter = $1; - $$ = $3; - fixpos($$, $1); - /*% - $$ = dispatch2(command, $1, $2); - $$ = method_add_block($$, $3); - %*/ - } - | primary_value call_op operation2 command_args %prec tLOWEST - { - /*%%%*/ - $$ = NEW_QCALL($2, $1, $3, $4); - fixpos($$, $1); - /*% - $$ = dispatch4(command_call, $1, ripper_id2sym($2), $3, $4); - %*/ - } - | primary_value call_op operation2 command_args cmd_brace_block - { - /*%%%*/ - block_dup_check($4,$5); - $5->nd_iter = NEW_QCALL($2, $1, $3, $4); - $$ = $5; - fixpos($$, $1); - /*% - $$ = dispatch4(command_call, $1, ripper_id2sym($2), $3, $4); - $$ = method_add_block($$, $5); - %*/ - } - | primary_value tCOLON2 operation2 command_args %prec tLOWEST - { - /*%%%*/ - $$ = NEW_CALL($1, $3, $4); - fixpos($$, $1); - /*% - $$ = dispatch4(command_call, $1, ID2SYM(idCOLON2), $3, $4); - %*/ - } - | primary_value tCOLON2 operation2 command_args cmd_brace_block - { - /*%%%*/ - block_dup_check($4,$5); - $5->nd_iter = NEW_CALL($1, $3, $4); - $$ = $5; - fixpos($$, $1); - /*% - $$ = dispatch4(command_call, $1, ID2SYM(idCOLON2), $3, $4); - $$ = method_add_block($$, $5); - %*/ - } - | keyword_super command_args - { - /*%%%*/ - $$ = NEW_SUPER($2); - fixpos($$, $2); - /*% - $$ = dispatch1(super, $2); - %*/ - } - | keyword_yield command_args - { - /*%%%*/ - $$ = new_yield($2); - fixpos($$, $2); - /*% - $$ = dispatch1(yield, $2); - %*/ - } - | keyword_return call_args - { - /*%%%*/ - $$ = NEW_RETURN(ret_args($2)); - /*% - $$ = dispatch1(return, $2); - %*/ - } - | keyword_break call_args - { - /*%%%*/ - $$ = NEW_BREAK(ret_args($2)); - /*% - $$ = dispatch1(break, $2); - %*/ - } - | keyword_next call_args - { - /*%%%*/ - $$ = NEW_NEXT(ret_args($2)); - /*% - $$ = dispatch1(next, $2); - %*/ - } - ; + { + $1->nd_args = $2; + nd_set_last_loc($1, @2.end_pos); + $$ = (NODE *)$1; + /*% 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) %*/ + } + | primary_value call_op operation2 command_args %prec tLOWEST + { + $$ = 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) %*/ + } + | primary_value tCOLON2 operation2 command_args %prec tLOWEST + { + $$ = 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, 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, idCOLON2, $1, $3, 0, $5, &@3, &@$); + /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/ + } + | keyword_super command_args + { + $$ = NEW_SUPER($2, &@$, &@1, &NULL_LOC, &NULL_LOC); + fixpos($$, $2); + /*% ripper: super!($:2) %*/ + } + | k_yield command_args + { + $$ = NEW_YIELD($2, &@$, &@1, &NULL_LOC, &NULL_LOC); + fixpos($$, $2); + /*% ripper: yield!($:2) %*/ + } + | k_return call_args + { + $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1); + /*% ripper: return!($:2) %*/ + } + | keyword_break call_args + { + NODE *args = 0; + args = ret_args(p, $2); + $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1)); + /*% ripper: break!($:2) %*/ + } + | keyword_next call_args + { + NODE *args = 0; + args = ret_args(p, $2); + $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1)); + /*% ripper: next!($:2) %*/ + } + ; mlhs : mlhs_basic - | tLPAREN mlhs_inner rparen - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(mlhs_paren, $2); - %*/ - } - ; + | tLPAREN mlhs_inner rparen + { + $$ = $2; + /*% ripper: mlhs_paren!($:2) %*/ + } + ; mlhs_inner : mlhs_basic - | tLPAREN mlhs_inner rparen - { - /*%%%*/ - $$ = NEW_MASGN(NEW_LIST($2), 0); - /*% - $$ = dispatch1(mlhs_paren, $2); - %*/ - } - ; + | tLPAREN mlhs_inner rparen + { + $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$); + /*% ripper: mlhs_paren!($:2) %*/ + } + ; mlhs_basic : mlhs_head - { - /*%%%*/ - $$ = NEW_MASGN($1, 0); - /*% - $$ = $1; - %*/ - } - | mlhs_head mlhs_item - { - /*%%%*/ - $$ = NEW_MASGN(list_append($1,$2), 0); - /*% - $$ = mlhs_add($1, $2); - %*/ - } - | mlhs_head tSTAR mlhs_node - { - /*%%%*/ - $$ = NEW_MASGN($1, $3); - /*% - $$ = mlhs_add_star($1, $3); - %*/ - } - | mlhs_head tSTAR mlhs_node ',' mlhs_post - { - /*%%%*/ - $$ = NEW_MASGN($1, NEW_POSTARG($3,$5)); - /*% - $1 = mlhs_add_star($1, $3); - $$ = mlhs_add($1, $5); - %*/ - } - | mlhs_head tSTAR - { - /*%%%*/ - $$ = NEW_MASGN($1, -1); - /*% - $$ = mlhs_add_star($1, Qnil); - %*/ - } - | mlhs_head tSTAR ',' mlhs_post - { - /*%%%*/ - $$ = NEW_MASGN($1, NEW_POSTARG(-1, $4)); - /*% - $1 = mlhs_add_star($1, Qnil); - $$ = mlhs_add($1, $4); - %*/ - } - | tSTAR mlhs_node - { - /*%%%*/ - $$ = NEW_MASGN(0, $2); - /*% - $$ = mlhs_add_star(mlhs_new(), $2); - %*/ - } - | tSTAR mlhs_node ',' mlhs_post - { - /*%%%*/ - $$ = NEW_MASGN(0, NEW_POSTARG($2,$4)); - /*% - $2 = mlhs_add_star(mlhs_new(), $2); - $$ = mlhs_add($2, $4); - %*/ - } - | tSTAR - { - /*%%%*/ - $$ = NEW_MASGN(0, -1); - /*% - $$ = mlhs_add_star(mlhs_new(), Qnil); - %*/ - } - | tSTAR ',' mlhs_post - { - /*%%%*/ - $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3)); - /*% - $$ = mlhs_add_star(mlhs_new(), Qnil); - $$ = mlhs_add($$, $3); - %*/ - } - ; + { + $$ = NEW_MASGN($1, 0, &@$); + /*% ripper: $:1 %*/ + } + | mlhs_head mlhs_item + { + $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$); + /*% ripper: mlhs_add!($:1, $:2) %*/ + } + | mlhs_head tSTAR mlhs_node + { + $$ = NEW_MASGN($1, $3, &@$); + /*% ripper: mlhs_add_star!($:1, $:3) %*/ + } + | 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) %*/ + } + | mlhs_head tSTAR + { + $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$); + /*% ripper: mlhs_add_star!($:1, Qnil) %*/ + } + | 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) %*/ + } + | tSTAR mlhs_node + { + $$ = NEW_MASGN(0, $2, &@$); + /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/ + } + | 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) %*/ + } + | tSTAR + { + $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$); + /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/ + } + | 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) %*/ + } + ; mlhs_item : mlhs_node - | tLPAREN mlhs_inner rparen - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(mlhs_paren, $2); - %*/ - } - ; + | tLPAREN mlhs_inner rparen + { + $$ = (NODE *)$2; + /*% ripper: mlhs_paren!($:2) %*/ + } + ; mlhs_head : mlhs_item ',' - { - /*%%%*/ - $$ = NEW_LIST($1); - /*% - $$ = mlhs_add(mlhs_new(), $1); - %*/ - } - | mlhs_head mlhs_item ',' - { - /*%%%*/ - $$ = list_append($1, $2); - /*% - $$ = mlhs_add($1, $2); - %*/ - } - ; - -mlhs_post : mlhs_item - { - /*%%%*/ - $$ = NEW_LIST($1); - /*% - $$ = mlhs_add(mlhs_new(), $1); - %*/ - } - | mlhs_post ',' mlhs_item - { - /*%%%*/ - $$ = list_append($1, $3); - /*% - $$ = mlhs_add($1, $3); - %*/ - } - ; - -mlhs_node : user_variable - { - $$ = assignable($1, 0); - } - | keyword_variable - { - $$ = assignable($1, 0); - } - | primary_value '[' opt_call_args rbracket - { - /*%%%*/ - $$ = aryset($1, $3); - /*% - $$ = dispatch2(aref_field, $1, escape_Qundef($3)); - %*/ - } - | primary_value call_op tIDENTIFIER - { - /*%%%*/ - $$ = attrset($1, $2, $3); - /*% - $$ = dispatch3(field, $1, ripper_id2sym($2), $3); - %*/ - } - | primary_value tCOLON2 tIDENTIFIER - { - /*%%%*/ - $$ = attrset($1, idCOLON2, $3); - /*% - $$ = dispatch2(const_path_field, $1, $3); - %*/ - } - | primary_value call_op tCONSTANT - { - /*%%%*/ - $$ = attrset($1, $2, $3); - /*% - $$ = dispatch3(field, $1, ripper_id2sym($2), $3); - %*/ - } - | primary_value tCOLON2 tCONSTANT - { - /*%%%*/ - if (in_def || in_single) - yyerror("dynamic constant assignment"); - $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3)); - /*% - $$ = dispatch2(const_path_field, $1, $3); - if (in_def || in_single) { - $$ = dispatch1(assign_error, $$); - ripper_error(); - } - %*/ - } - | tCOLON3 tCONSTANT - { - /*%%%*/ - if (in_def || in_single) - yyerror("dynamic constant assignment"); - $$ = NEW_CDECL(0, 0, NEW_COLON3($2)); - /*% - $$ = dispatch1(top_const_field, $2); - if (in_def || in_single) { - $$ = dispatch1(assign_error, $$); - ripper_error(); - } - %*/ - } - | backref - { - /*%%%*/ - rb_backref_error($1); - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch1(var_field, $1); - $$ = dispatch1(assign_error, $$); - ripper_error(); - %*/ - } - ; - -lhs : user_variable - { - $$ = assignable($1, 0); - /*%%%*/ - if (!$$) $$ = NEW_BEGIN(0); - /*% - $$ = dispatch1(var_field, $$); - %*/ - } - | keyword_variable - { - $$ = assignable($1, 0); - /*%%%*/ - if (!$$) $$ = NEW_BEGIN(0); - /*% - $$ = dispatch1(var_field, $$); - %*/ - } - | primary_value '[' opt_call_args rbracket - { - /*%%%*/ - $$ = aryset($1, $3); - /*% - $$ = dispatch2(aref_field, $1, escape_Qundef($3)); - %*/ - } - | primary_value call_op tIDENTIFIER - { - /*%%%*/ - $$ = attrset($1, $2, $3); - /*% - $$ = dispatch3(field, $1, ripper_id2sym($2), $3); - %*/ - } - | primary_value tCOLON2 tIDENTIFIER - { - /*%%%*/ - $$ = attrset($1, idCOLON2, $3); - /*% - $$ = dispatch3(field, $1, ID2SYM(idCOLON2), $3); - %*/ - } - | primary_value call_op tCONSTANT - { - /*%%%*/ - $$ = attrset($1, $2, $3); - /*% - $$ = dispatch3(field, $1, ripper_id2sym($2), $3); - %*/ - } - | primary_value tCOLON2 tCONSTANT - { - /*%%%*/ - if (in_def || in_single) - yyerror("dynamic constant assignment"); - $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3)); - /*% - $$ = dispatch2(const_path_field, $1, $3); - if (in_def || in_single) { - $$ = dispatch1(assign_error, $$); - ripper_error(); - } - %*/ - } - | tCOLON3 tCONSTANT - { - /*%%%*/ - if (in_def || in_single) - yyerror("dynamic constant assignment"); - $$ = NEW_CDECL(0, 0, NEW_COLON3($2)); - /*% - $$ = dispatch1(top_const_field, $2); - if (in_def || in_single) { - $$ = dispatch1(assign_error, $$); - ripper_error(); - } - %*/ - } - | backref - { - /*%%%*/ - rb_backref_error($1); - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch1(assign_error, $1); - ripper_error(); - %*/ - } - ; + { + $$ = NEW_LIST($1, &@1); + /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/ + } + | mlhs_head mlhs_item ',' + { + $$ = list_append(p, $1, $2); + /*% ripper: mlhs_add!($:1, $:2) %*/ + } + ; + + +mlhs_node : user_or_keyword_variable + { + /*% ripper: var_field!($:1) %*/ + $$ = assignable(p, $1, 0, &@$); + } + | primary_value '[' opt_call_args rbracket + { + $$ = aryset(p, $1, $3, &@$); + /*% ripper: aref_field!($:1, $:3) %*/ + } + | primary_value call_op ident_or_const + { + anddot_multiple_assignment_check(p, &@2, $2); + $$ = attrset(p, $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 tCOLON2 tCONSTANT + { + /*% ripper: const_path_field!($:1, $:3) %*/ + $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$); + } + | tCOLON3 tCONSTANT + { + /*% ripper: top_const_field!($:2) %*/ + $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$); + } + | backref + { + VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1); + $$ = NEW_ERROR(&@$); + /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/ + } + ; + +lhs : user_or_keyword_variable + { + /*% ripper: var_field!($:1) %*/ + $$ = assignable(p, $1, 0, &@$); + } + | primary_value '[' opt_call_args rbracket + { + $$ = aryset(p, $1, $3, &@$); + /*% ripper: aref_field!($:1, $:3) %*/ + } + | primary_value call_op ident_or_const + { + $$ = attrset(p, $1, $2, $3, &@$); + /*% ripper: field!($:1, $:2, $:3) %*/ + } + | primary_value tCOLON2 tIDENTIFIER + { + $$ = attrset(p, $1, idCOLON2, $3, &@$); + /*% ripper: field!($:1, $:2, $:3) %*/ + } + | primary_value tCOLON2 tCONSTANT + { + /*% ripper: const_path_field!($:1, $:3) %*/ + $$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$); + } + | tCOLON3 tCONSTANT + { + /*% ripper: top_const_field!($:2) %*/ + $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$); + } + | backref + { + VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1); + $$ = NEW_ERROR(&@$); + /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/ + } + ; cname : tIDENTIFIER - { - /*%%%*/ - yyerror("class/module name must be CONSTANT"); - /*% - $$ = dispatch1(class_name_error, $1); - ripper_error(); - %*/ - } - | tCONSTANT - ; + { + static const char mesg[] = "class/module name must be CONSTANT"; + /*%%%*/ + yyerror1(&@1, mesg); + /*% %*/ + /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/ + } + | tCONSTANT + ; cpath : tCOLON3 cname - { - /*%%%*/ - $$ = NEW_COLON3($2); - /*% - $$ = dispatch1(top_const_ref, $2); - %*/ - } - | cname - { - /*%%%*/ - $$ = NEW_COLON2(0, $$); - /*% - $$ = dispatch1(const_ref, $1); - %*/ - } - | primary_value tCOLON2 cname - { - /*%%%*/ - $$ = NEW_COLON2($1, $3); - /*% - $$ = dispatch2(const_path_ref, $1, $3); - %*/ - } - ; - -fname : tIDENTIFIER - | tCONSTANT - | tFID - | op - { - lex_state = EXPR_ENDFN; - $$ = $1; - } - | reswords - { - lex_state = EXPR_ENDFN; - /*%%%*/ - $$ = $<id>1; - /*% - $$ = $1; - %*/ - } - ; - -fsym : fname - | symbol - ; - -fitem : fsym - { - /*%%%*/ - $$ = NEW_LIT(ID2SYM($1)); - /*% - $$ = dispatch1(symbol_literal, $1); - %*/ - } - | dsym - ; + { + $$ = NEW_COLON3($2, &@$, &@1, &@2); + /*% ripper: top_const_ref!($:2) %*/ + } + | cname + { + $$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1); + /*% ripper: const_ref!($:1) %*/ + } + | primary_value tCOLON2 cname + { + $$ = NEW_COLON2($1, $3, &@$, &@2, &@3); + /*% ripper: const_path_ref!($:1, $:3) %*/ + } + ; + +fname : operation + | op + { + SET_LEX_STATE(EXPR_ENDFN); + $$ = $1; + } + | reswords + ; + +fitem : fname + { + $$ = NEW_SYM(rb_id2str($1), &@$); + /*% ripper: symbol_literal!($:1) %*/ + } + | symbol + ; undef_list : fitem - { - /*%%%*/ - $$ = NEW_UNDEF($1); - /*% - $$ = rb_ary_new3(1, $1); - %*/ - } - | undef_list ',' {lex_state = EXPR_FNAME;} fitem - { - /*%%%*/ - $$ = block_append($1, NEW_UNDEF($4)); - /*% - rb_ary_push($1, $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($$ = '`'); } - ; + { + $$ = NEW_UNDEF($1, &@$); + /*% ripper: rb_ary_new3(1, $:1) %*/ + } + | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem + { + 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__ - | keyword_BEGIN | keyword_END - | keyword_alias | keyword_and | keyword_begin - | keyword_break | keyword_case | keyword_class | keyword_def - | keyword_defined | keyword_do | keyword_else | keyword_elsif - | keyword_end | keyword_ensure | keyword_false - | keyword_for | keyword_in | keyword_module | keyword_next - | keyword_nil | keyword_not | keyword_or | keyword_redo - | keyword_rescue | keyword_retry | keyword_return | keyword_self - | keyword_super | keyword_then | keyword_true | keyword_undef - | keyword_when | keyword_yield | keyword_if | keyword_unless - | keyword_while | keyword_until - ; - -arg : lhs '=' arg - { - /*%%%*/ - value_expr($3); - $$ = node_assign($1, $3); - /*% - $$ = dispatch2(assign, $1, $3); - %*/ - } - | lhs '=' arg modifier_rescue arg - { - /*%%%*/ - value_expr($3); - $3 = NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0); - $$ = node_assign($1, $3); - /*% - $$ = dispatch2(assign, $1, dispatch2(rescue_mod, $3, $5)); - %*/ - } - | var_lhs tOP_ASGN arg - { - value_expr($3); - $$ = new_op_assign($1, $2, $3); - } - | var_lhs tOP_ASGN arg modifier_rescue arg - { - /*%%%*/ - value_expr($3); - $3 = NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0); - /*% - $3 = dispatch2(rescue_mod, $3, $5); - %*/ - $$ = new_op_assign($1, $2, $3); - } - | primary_value '[' opt_call_args rbracket tOP_ASGN arg - { - /*%%%*/ - NODE *args; - - value_expr($6); - if (!$3) $3 = NEW_ZARRAY(); - if (nd_type($3) == NODE_BLOCK_PASS) { - args = NEW_ARGSCAT($3, $6); - } - else { - args = arg_concat($3, $6); - } - if ($5 == tOROP) { - $5 = 0; - } - else if ($5 == tANDOP) { - $5 = 1; - } - $$ = NEW_OP_ASGN1($1, $5, args); - fixpos($$, $1); - /*% - $1 = dispatch2(aref_field, $1, escape_Qundef($3)); - $$ = dispatch3(opassign, $1, $5, $6); - %*/ - } - | primary_value call_op tIDENTIFIER tOP_ASGN arg - { - value_expr($5); - $$ = new_attr_op_assign($1, $2, $3, $4, $5); - } - | primary_value call_op tCONSTANT tOP_ASGN arg - { - value_expr($5); - $$ = new_attr_op_assign($1, $2, $3, $4, $5); - } - | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg - { - value_expr($5); - $$ = new_attr_op_assign($1, idCOLON2, $3, $4, $5); - } - | primary_value tCOLON2 tCONSTANT tOP_ASGN arg - { - /*%%%*/ - $$ = NEW_COLON2($1, $3); - $$ = new_const_op_assign($$, $4, $5); - /*% - $$ = dispatch2(const_path_field, $1, $3); - $$ = dispatch3(opassign, $$, $4, $5); - %*/ - } - | tCOLON3 tCONSTANT tOP_ASGN arg - { - /*%%%*/ - $$ = NEW_COLON3($2); - $$ = new_const_op_assign($$, $3, $4); - /*% - $$ = dispatch1(top_const_field, $2); - $$ = dispatch3(opassign, $$, $3, $4); - %*/ - } - | backref tOP_ASGN arg - { - /*%%%*/ - rb_backref_error($1); - $$ = NEW_BEGIN(0); - /*% - $$ = dispatch1(var_field, $1); - $$ = dispatch3(opassign, $$, $2, $3); - $$ = dispatch1(assign_error, $$); - ripper_error(); - %*/ - } - | arg tDOT2 arg - { - /*%%%*/ - value_expr($1); - value_expr($3); - $$ = NEW_DOT2($1, $3); - if ($1 && nd_type($1) == NODE_LIT && FIXNUM_P($1->nd_lit) && - $3 && nd_type($3) == NODE_LIT && FIXNUM_P($3->nd_lit)) { - deferred_nodes = list_append(deferred_nodes, $$); - } - /*% - $$ = dispatch2(dot2, $1, $3); - %*/ - } - | arg tDOT3 arg - { - /*%%%*/ - value_expr($1); - value_expr($3); - $$ = NEW_DOT3($1, $3); - if ($1 && nd_type($1) == NODE_LIT && FIXNUM_P($1->nd_lit) && - $3 && nd_type($3) == NODE_LIT && FIXNUM_P($3->nd_lit)) { - deferred_nodes = list_append(deferred_nodes, $$); - } - /*% - $$ = dispatch2(dot3, $1, $3); - %*/ - } - | arg '+' arg - { - /*%%%*/ - $$ = call_bin_op($1, '+', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('+'), $3); - %*/ - } - | arg '-' arg - { - /*%%%*/ - $$ = call_bin_op($1, '-', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('-'), $3); - %*/ - } - | arg '*' arg - { - /*%%%*/ - $$ = call_bin_op($1, '*', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('*'), $3); - %*/ - } - | arg '/' arg - { - /*%%%*/ - $$ = call_bin_op($1, '/', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('/'), $3); - %*/ - } - | arg '%' arg - { - /*%%%*/ - $$ = call_bin_op($1, '%', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('%'), $3); - %*/ - } - | arg tPOW arg - { - /*%%%*/ - $$ = call_bin_op($1, tPOW, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idPow), $3); - %*/ - } - | tUMINUS_NUM simple_numeric tPOW arg - { - /*%%%*/ - $$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0); - /*% - $$ = dispatch3(binary, $2, ID2SYM(idPow), $4); - $$ = dispatch2(unary, ID2SYM(idUMinus), $$); - %*/ - } - | tUPLUS arg - { - /*%%%*/ - $$ = call_uni_op($2, tUPLUS); - /*% - $$ = dispatch2(unary, ID2SYM(idUPlus), $2); - %*/ - } - | tUMINUS arg - { - /*%%%*/ - $$ = call_uni_op($2, tUMINUS); - /*% - $$ = dispatch2(unary, ID2SYM(idUMinus), $2); - %*/ - } - | arg '|' arg - { - /*%%%*/ - $$ = call_bin_op($1, '|', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('|'), $3); - %*/ - } - | arg '^' arg - { - /*%%%*/ - $$ = call_bin_op($1, '^', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('^'), $3); - %*/ - } - | arg '&' arg - { - /*%%%*/ - $$ = call_bin_op($1, '&', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('&'), $3); - %*/ - } - | arg tCMP arg - { - /*%%%*/ - $$ = call_bin_op($1, tCMP, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idCmp), $3); - %*/ - } - | arg '>' arg - { - /*%%%*/ - $$ = call_bin_op($1, '>', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('>'), $3); - %*/ - } - | arg tGEQ arg - { - /*%%%*/ - $$ = call_bin_op($1, tGEQ, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idGE), $3); - %*/ - } - | arg '<' arg - { - /*%%%*/ - $$ = call_bin_op($1, '<', $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM('<'), $3); - %*/ - } - | arg tLEQ arg - { - /*%%%*/ - $$ = call_bin_op($1, tLEQ, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idLE), $3); - %*/ - } - | arg tEQ arg - { - /*%%%*/ - $$ = call_bin_op($1, tEQ, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idEq), $3); - %*/ - } - | arg tEQQ arg - { - /*%%%*/ - $$ = call_bin_op($1, tEQQ, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idEqq), $3); - %*/ - } - | arg tNEQ arg - { - /*%%%*/ - $$ = call_bin_op($1, tNEQ, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idNeq), $3); - %*/ - } - | arg tMATCH arg - { - /*%%%*/ - $$ = match_op($1, $3); - if (nd_type($1) == NODE_LIT && RB_TYPE_P($1->nd_lit, T_REGEXP)) { - $$ = reg_named_capture_assign($1->nd_lit, $$); - } - /*% - $$ = dispatch3(binary, $1, ID2SYM(idEqTilde), $3); - %*/ - } - | arg tNMATCH arg - { - /*%%%*/ - $$ = call_bin_op($1, tNMATCH, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idNeqTilde), $3); - %*/ - } - | '!' arg - { - /*%%%*/ - $$ = call_uni_op(cond($2), '!'); - /*% - $$ = dispatch2(unary, ID2SYM('!'), $2); - %*/ - } - | '~' arg - { - /*%%%*/ - $$ = call_uni_op($2, '~'); - /*% - $$ = dispatch2(unary, ID2SYM('~'), $2); - %*/ - } - | arg tLSHFT arg - { - /*%%%*/ - $$ = call_bin_op($1, tLSHFT, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idLTLT), $3); - %*/ - } - | arg tRSHFT arg - { - /*%%%*/ - $$ = call_bin_op($1, tRSHFT, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idGTGT), $3); - %*/ - } - | arg tANDOP arg - { - /*%%%*/ - $$ = logop(NODE_AND, $1, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idANDOP), $3); - %*/ - } - | arg tOROP arg - { - /*%%%*/ - $$ = logop(NODE_OR, $1, $3); - /*% - $$ = dispatch3(binary, $1, ID2SYM(idOROP), $3); - %*/ - } - | keyword_defined opt_nl {in_defined = 1;} arg - { - in_defined = 0; - /*%%%*/ - $$ = new_defined($4); - /*% - $$ = dispatch1(defined, $4); - %*/ - } - | arg '?' arg opt_nl ':' arg - { - /*%%%*/ - value_expr($1); - $$ = new_if($1, $3, $6); - fixpos($$, $1); - /*% - $$ = dispatch3(ifop, $1, $3, $6); - %*/ - } - | primary - { - $$ = $1; - } - ; - -arg_value : arg - { - /*%%%*/ - value_expr($1); - $$ = $1; - if (!$$) $$ = NEW_NIL(); - /*% - $$ = $1; - %*/ - } - ; + | keyword_BEGIN | keyword_END + | keyword_alias | keyword_and | keyword_begin + | keyword_break | keyword_case | keyword_class | keyword_def + | keyword_defined | keyword_do | keyword_else | keyword_elsif + | keyword_end | keyword_ensure | keyword_false + | keyword_for | keyword_in | keyword_module | keyword_next + | keyword_nil | keyword_not | keyword_or | keyword_redo + | keyword_rescue | keyword_retry | keyword_return | keyword_self + | keyword_super | keyword_then | keyword_true | keyword_undef + | keyword_when | keyword_yield | keyword_if | keyword_unless + | keyword_while | keyword_until + ; + +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 '\n'? begin_defined arg + { + p->ctxt.in_defined = $3.in_defined; + $$ = 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 + { + value_expr(p, $1); + $$ = new_if(p, $1, $3, $6, &@$, &NULL_LOC, &@5, &NULL_LOC); + fixpos($$, $1); + /*% ripper: ifop!($:1, $:3, $:6) %*/ + } + ; + +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) %*/ + } + | keyword_not '\n'? endless_arg + { + $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$); + /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/ + } + ; + +relop : '>' {$$ = '>';} + | '<' {$$ = '<';} + | tGEQ {$$ = idGE;} + | tLEQ {$$ = idLE;} + ; + +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) %*/ + } + ; + +lex_ctxt : none + { + $$ = p->ctxt; + } + ; + +begin_defined : lex_ctxt + { + p->ctxt.in_defined = 1; + $$ = $1; + } + ; + +after_rescue : lex_ctxt + { + p->ctxt.in_rescue = after_rescue; + $$ = $1; + } + ; + +arg_value : value_expr(arg) + ; aref_args : none - | args trailer - { - $$ = $1; - } - | args ',' assocs trailer - { - /*%%%*/ - $$ = $3 ? arg_append($1, new_hash($3)) : $1; - /*% - $$ = arg_add_assocs($1, $3); - %*/ - } - | assocs trailer - { - /*%%%*/ - $$ = $1 ? NEW_LIST(new_hash($1)) : 0; - /*% - $$ = arg_add_assocs(arg_new(), $1); - %*/ - } - ; + | args trailer + | args ',' assocs trailer + { + $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1; + /*% 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)) %*/ + } + ; + +arg_rhs : arg %prec tOP_ASGN + { + value_expr(p, $1); + $$ = $1; + } + | arg modifier_rescue after_rescue arg + { + p->ctxt.in_rescue = $3.in_rescue; + value_expr(p, $1); + $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4); + /*% ripper: rescue_mod!($:1, $:4) %*/ + } + ; paren_args : '(' opt_call_args rparen - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(arg_paren, escape_Qundef($2)); - %*/ - } - ; + { + $$ = $2; + /*% ripper: arg_paren!($:2) %*/ + } + | '(' args ',' args_forward rparen + { + if (!check_forwarding_args(p)) { + $$ = 0; + } + else { + $$ = new_args_forward_call(p, $2, &@4, &@$); + /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/ + } + } + | '(' args_forward rparen + { + if (!check_forwarding_args(p)) { + $$ = 0; + } + else { + $$ = new_args_forward_call(p, 0, &@2, &@$); + /*% ripper: arg_paren!($:2) %*/ + } + } + ; opt_paren_args : none - | paren_args - ; + | paren_args + { + $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS; + } + ; opt_call_args : none - | call_args - | args ',' - { - $$ = $1; - } - | args ',' assocs ',' - { - /*%%%*/ - $$ = $3 ? arg_append($1, new_hash($3)) : $1; - /*% - $$ = arg_add_assocs($1, $3); - %*/ - } - | assocs ',' - { - /*%%%*/ - $$ = $1 ? NEW_LIST(new_hash($1)) : 0; - /*% - $$ = arg_add_assocs(arg_new(), $1); - %*/ - } - ; - -call_args : command - { - /*%%%*/ - value_expr($1); - $$ = NEW_LIST($1); - /*% - $$ = arg_add(arg_new(), $1); - %*/ - } - | args opt_block_arg - { - /*%%%*/ - $$ = arg_blk_pass($1, $2); - /*% - $$ = arg_add_optblock($1, $2); - %*/ - } - | assocs opt_block_arg - { - /*%%%*/ - $$ = NEW_LIST($1 ? new_hash($1) : 0); - $$ = arg_blk_pass($$, $2); - /*% - $$ = arg_add_assocs(arg_new(), $1); - $$ = arg_add_optblock($$, $2); - %*/ - } - | args ',' assocs opt_block_arg - { - /*%%%*/ - $$ = $3 ? arg_append($1, new_hash($3)) : $1; - $$ = arg_blk_pass($$, $4); - /*% - $$ = arg_add_optblock(arg_add_assocs($1, $3), $4); - %*/ - } - | block_arg - /*%c%*/ - /*%c - { - $$ = arg_add_block(arg_new(), $1); - } - %*/ - ; - -command_args : { - $<val>$ = cmdarg_stack; - CMDARG_PUSH(1); - } - call_args - { - /* CMDARG_POP() */ - cmdarg_stack = $<val>1; - $$ = $2; - } - ; + | call_args + | args ',' + | args ',' assocs ',' + { + $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1; + /*% 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)) %*/ + } + ; + +call_args : value_expr(command) + { + $$ = NEW_LIST($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) %*/ + } + | 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) %*/ + } + | 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) %*/ + } + | block_arg + /*% ripper: args_add_block!(args_new!, $:1) %*/ + ; + +command_args : { + /* If call_args starts with a open paren '(' or '[', + * look-ahead reading of the letters calls CMDARG_PUSH(0), + * but the push must be done after CMDARG_PUSH(1). + * So this code makes them consistent by first cancelling + * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1), + * and finally redoing CMDARG_PUSH(0). + */ + int lookahead = 0; + switch (yychar) { + case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK: + lookahead = 1; + } + if (lookahead) CMDARG_POP(); + CMDARG_PUSH(1); + if (lookahead) CMDARG_PUSH(0); + } + call_args + { + /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer) + * but the push must be done after CMDARG_POP() in the parser. + * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG, + * CMDARG_POP() to pop 1 pushed by command_args, + * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG. + */ + int lookahead = 0; + switch (yychar) { + case tLBRACE_ARG: + lookahead = 1; + } + if (lookahead) CMDARG_POP(); + CMDARG_POP(); + if (lookahead) CMDARG_PUSH(0); + $$ = $2; + /*% ripper: $:2 %*/ + } + ; block_arg : tAMPER arg_value - { - /*%%%*/ - $$ = NEW_BLOCK_PASS($2); - /*% - $$ = $2; - %*/ - } - ; + { + $$ = NEW_BLOCK_PASS($2, &@$, &@1); + /*% ripper: $:2 %*/ + } + | tAMPER + { + forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block"); + $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1); + /*% ripper: Qnil %*/ + } + ; opt_block_arg : ',' block_arg - { - $$ = $2; - } - | none - { - $$ = 0; - } - ; + { + $$ = $2; + /*% ripper: $:2 %*/ + } + | none + { + $$ = 0; + /*% ripper: Qfalse %*/ + } + ; +/* value */ args : arg_value - { - /*%%%*/ - $$ = NEW_LIST($1); - /*% - $$ = arg_add(arg_new(), $1); - %*/ - } - | tSTAR arg_value - { - /*%%%*/ - $$ = NEW_SPLAT($2); - /*% - $$ = arg_add_star(arg_new(), $2); - %*/ - } - | args ',' arg_value - { - /*%%%*/ - NODE *n1; - if ((n1 = splat_array($1)) != 0) { - $$ = list_append(n1, $3); - } - else { - $$ = arg_append($1, $3); - } - /*% - $$ = arg_add($1, $3); - %*/ - } - | args ',' tSTAR arg_value - { - /*%%%*/ - NODE *n1; - if ((nd_type($4) == NODE_ARRAY) && (n1 = splat_array($1)) != 0) { - $$ = list_concat(n1, $4); - } - else { - $$ = arg_concat($1, $4); - } - /*% - $$ = arg_add_star($1, $4); - %*/ - } - ; + { + $$ = NEW_LIST($arg_value, &@$); + /*% ripper: args_add!(args_new!, $:arg_value) %*/ + } + | arg_splat + { + $$ = $arg_splat; + /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/ + } + | args[non_last_args] ',' arg_value + { + $$ = last_arg_append(p, $non_last_args, $arg_value, &@$); + /*% ripper: args_add!($:non_last_args, $:arg_value) %*/ + } + | args[non_last_args] ',' arg_splat + { + $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$); + /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/ + } + ; + +/* value */ +arg_splat : tSTAR arg_value + { + $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR); + /*% ripper: $:arg_value %*/ + } + | tSTAR /* none */ + { + forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest"); + $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR); + /*% ripper: Qnil %*/ + } + ; +/* value */ mrhs_arg : mrhs - | arg_value - ; + | arg_value + ; +/* value */ mrhs : args ',' arg_value - { - /*%%%*/ - NODE *n1; - if ((n1 = splat_array($1)) != 0) { - $$ = list_append(n1, $3); - } - else { - $$ = arg_append($1, $3); - } - /*% - $$ = mrhs_add(args2mrhs($1), $3); - %*/ - } - | args ',' tSTAR arg_value - { - /*%%%*/ - NODE *n1; - if (nd_type($4) == NODE_ARRAY && - (n1 = splat_array($1)) != 0) { - $$ = list_concat(n1, $4); - } - else { - $$ = arg_concat($1, $4); - } - /*% - $$ = mrhs_add_star(args2mrhs($1), $4); - %*/ - } - | tSTAR arg_value - { - /*%%%*/ - $$ = NEW_SPLAT($2); - /*% - $$ = mrhs_add_star(mrhs_new(), $2); - %*/ - } - ; - -primary : literal - | strings - | xstring - | regexp - | words - | qwords - | symbols - | qsymbols - | var_ref - | backref - | tFID - { - /*%%%*/ - $$ = NEW_FCALL($1, 0); - /*% - $$ = method_arg(dispatch1(fcall, $1), arg_new()); - %*/ - } - | k_begin - { - $<val>1 = cmdarg_stack; - cmdarg_stack = 0; - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% - %*/ - } - bodystmt - k_end - { - cmdarg_stack = $<val>1; - /*%%%*/ - if ($3 == NULL) { - $$ = NEW_NIL(); - } - else { - if (nd_type($3) == NODE_RESCUE || - nd_type($3) == NODE_ENSURE) - nd_set_line($3, $<num>2); - $$ = NEW_BEGIN($3); - } - nd_set_line($$, $<num>2); - /*% - $$ = dispatch1(begin, $3); - %*/ - } - | tLPAREN_ARG {lex_state = EXPR_ENDARG;} rparen - { - /*%%%*/ - $$ = 0; - /*% - $$ = dispatch1(paren, 0); - %*/ - } - | tLPAREN_ARG - { - $<val>1 = cmdarg_stack; - cmdarg_stack = 0; - } - expr {lex_state = EXPR_ENDARG;} rparen - { - cmdarg_stack = $<val>1; - /*%%%*/ - $$ = $3; - /*% - $$ = dispatch1(paren, $3); - %*/ - } - | tLPAREN compstmt ')' - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(paren, $2); - %*/ - } - | primary_value tCOLON2 tCONSTANT - { - /*%%%*/ - $$ = NEW_COLON2($1, $3); - /*% - $$ = dispatch2(const_path_ref, $1, $3); - %*/ - } - | tCOLON3 tCONSTANT - { - /*%%%*/ - $$ = NEW_COLON3($2); - /*% - $$ = dispatch1(top_const_ref, $2); - %*/ - } - | tLBRACK aref_args ']' - { - /*%%%*/ - if ($2 == 0) { - $$ = NEW_ZARRAY(); /* zero length array*/ - } - else { - $$ = $2; - } - /*% - $$ = dispatch1(array, escape_Qundef($2)); - %*/ - } - | tLBRACE assoc_list '}' - { - /*%%%*/ - $$ = new_hash($2); - /*% - $$ = dispatch1(hash, escape_Qundef($2)); - %*/ - } - | keyword_return - { - /*%%%*/ - $$ = NEW_RETURN(0); - /*% - $$ = dispatch0(return0); - %*/ - } - | keyword_yield '(' call_args rparen - { - /*%%%*/ - $$ = new_yield($3); - /*% - $$ = dispatch1(yield, dispatch1(paren, $3)); - %*/ - } - | keyword_yield '(' rparen - { - /*%%%*/ - $$ = NEW_YIELD(0); - /*% - $$ = dispatch1(yield, dispatch1(paren, arg_new())); - %*/ - } - | keyword_yield - { - /*%%%*/ - $$ = NEW_YIELD(0); - /*% - $$ = dispatch0(yield0); - %*/ - } - | keyword_defined opt_nl '(' {in_defined = 1;} expr rparen - { - in_defined = 0; - /*%%%*/ - $$ = new_defined($5); - /*% - $$ = dispatch1(defined, $5); - %*/ - } - | keyword_not '(' expr rparen - { - /*%%%*/ - $$ = call_uni_op(cond($3), '!'); - /*% - $$ = dispatch2(unary, ripper_intern("not"), $3); - %*/ - } - | keyword_not '(' rparen - { - /*%%%*/ - $$ = call_uni_op(cond(NEW_NIL()), '!'); - /*% - $$ = dispatch2(unary, ripper_intern("not"), Qnil); - %*/ - } - | fcall brace_block - { - /*%%%*/ - $2->nd_iter = $1; - $$ = $2; - /*% - $$ = method_arg(dispatch1(fcall, $1), arg_new()); - $$ = method_add_block($$, $2); - %*/ - } - | method_call - | method_call brace_block - { - /*%%%*/ - block_dup_check($1->nd_args, $2); - $2->nd_iter = $1; - $$ = $2; - /*% - $$ = method_add_block($1, $2); - %*/ - } - | tLAMBDA lambda - { - $$ = $2; - } - | k_if expr_value then - compstmt - if_tail - k_end - { - /*%%%*/ - $$ = new_if($2, $4, $5); - fixpos($$, $2); - /*% - $$ = dispatch3(if, $2, $4, escape_Qundef($5)); - %*/ - } - | k_unless expr_value then - compstmt - opt_else - k_end - { - /*%%%*/ - $$ = NEW_UNLESS(cond($2), $4, $5); - fixpos($$, $2); - /*% - $$ = dispatch3(unless, $2, $4, escape_Qundef($5)); - %*/ - } - | k_while {COND_PUSH(1);} expr_value do {COND_POP();} - compstmt - k_end - { - /*%%%*/ - $$ = NEW_WHILE(cond($3), $6, 1); - fixpos($$, $3); - /*% - $$ = dispatch2(while, $3, $6); - %*/ - } - | k_until {COND_PUSH(1);} expr_value do {COND_POP();} - compstmt - k_end - { - /*%%%*/ - $$ = NEW_UNTIL(cond($3), $6, 1); - fixpos($$, $3); - /*% - $$ = dispatch2(until, $3, $6); - %*/ - } - | k_case expr_value opt_terms - case_body - k_end - { - /*%%%*/ - $$ = NEW_CASE($2, $4); - fixpos($$, $2); - /*% - $$ = dispatch2(case, $2, $4); - %*/ - } - | k_case opt_terms case_body k_end - { - /*%%%*/ - $$ = NEW_CASE(0, $3); - /*% - $$ = dispatch2(case, Qnil, $3); - %*/ - } - | k_for for_var keyword_in - {COND_PUSH(1);} - expr_value do - {COND_POP();} - compstmt - k_end - { - /*%%%*/ - /* - * 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(); - ID *tbl = ALLOC_N(ID, 2); - NODE *m = NEW_ARGS_AUX(0, 0); - NODE *args, *scope; - - switch (nd_type($2)) { - case NODE_MASGN: - m->nd_next = node_assign($2, NEW_FOR(NEW_DVAR(id), 0, 0)); - args = new_args(m, 0, id, 0, new_args_tail(0, 0, 0)); - break; - case NODE_LASGN: - case NODE_DASGN: - case NODE_DASGN_CURR: - $2->nd_value = NEW_DVAR(id); - m->nd_plen = 1; - m->nd_next = $2; - args = new_args(m, 0, 0, 0, new_args_tail(0, 0, 0)); - break; - default: - m->nd_next = node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id)); - args = new_args(m, 0, id, 0, new_args_tail(0, 0, 0)); - break; - } - scope = NEW_NODE(NODE_SCOPE, tbl, $8, args); - tbl[0] = 1; tbl[1] = id; - $$ = NEW_FOR(0, $5, scope); - fixpos($$, $2); - /*% - $$ = dispatch3(for, $2, $5, $8); - %*/ - } - | k_class cpath superclass - { - if (in_def || in_single) - yyerror("class definition in method body"); - local_push(0); - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% - %*/ - } - bodystmt - k_end - { - /*%%%*/ - $$ = NEW_CLASS($2, $5, $3); - nd_set_line($$, $<num>4); - /*% - $$ = dispatch3(class, $2, $3, $5); - %*/ - local_pop(); - } - | k_class tLSHFT expr - { - $<num>$ = (in_def << 1) | in_single; - in_def = 0; - in_single = 0; - local_push(0); - } - term - bodystmt - k_end - { - /*%%%*/ - $$ = NEW_SCLASS($3, $6); - fixpos($$, $3); - /*% - $$ = dispatch2(sclass, $3, $6); - %*/ - local_pop(); - in_def = ($<num>4 >> 1) & 1; - in_single = $<num>4 & 1; - } - | k_module cpath - { - if (in_def || in_single) - yyerror("module definition in method body"); - local_push(0); - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% - %*/ - } - bodystmt - k_end - { - /*%%%*/ - $$ = NEW_MODULE($2, $4); - nd_set_line($$, $<num>3); - /*% - $$ = dispatch2(module, $2, $4); - %*/ - local_pop(); - } - | k_def fname - { - local_push(0); - $<id>$ = current_arg; - current_arg = 0; - } - { - $<num>$ = in_def; - in_def = 1; - } - f_arglist - bodystmt - k_end - { - /*%%%*/ - NODE *body = remove_begin($6); - reduce_nodes(&body); - $$ = NEW_DEFN($2, $5, body, METHOD_VISI_PRIVATE); - nd_set_line($$, $<num>1); - /*% - $$ = dispatch3(def, $2, $5, $6); - %*/ - local_pop(); - in_def = $<num>4 & 1; - current_arg = $<id>3; - } - | k_def singleton dot_or_colon {lex_state = EXPR_FNAME;} fname - { - $<num>4 = in_single; - in_single = 1; - lex_state = EXPR_ENDFN|EXPR_LABEL; /* force for args */ - local_push(0); - $<id>$ = current_arg; - current_arg = 0; - } - f_arglist - bodystmt - k_end - { - /*%%%*/ - NODE *body = remove_begin($8); - reduce_nodes(&body); - $$ = NEW_DEFS($2, $5, $7, body); - nd_set_line($$, $<num>1); - /*% - $$ = dispatch5(defs, $2, $3, $5, $7, $8); - %*/ - local_pop(); - in_single = $<num>4 & 1; - current_arg = $<id>6; - } - | keyword_break - { - /*%%%*/ - $$ = NEW_BREAK(0); - /*% - $$ = dispatch1(break, arg_new()); - %*/ - } - | keyword_next - { - /*%%%*/ - $$ = NEW_NEXT(0); - /*% - $$ = dispatch1(next, arg_new()); - %*/ - } - | keyword_redo - { - /*%%%*/ - $$ = NEW_REDO(); - /*% - $$ = dispatch0(redo); - %*/ - } - | keyword_retry - { - /*%%%*/ - $$ = NEW_RETRY(); - /*% - $$ = dispatch0(retry); - %*/ - } - ; - -primary_value : primary - { - /*%%%*/ - value_expr($1); - $$ = $1; - if (!$$) $$ = NEW_NIL(); - /*% - $$ = $1; - %*/ - } - ; + { + $$ = 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, $args, $arg_value, &@$); + /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/ + } + | tSTAR arg_value + { + $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR); + /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/ + } + ; + +%rule %inline inline_primary + : literal + | strings + | xstring + | regexp + | words + | qwords + | symbols + | qsymbols + ; + +primary : inline_primary + | 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(stmts) {SET_LEX_STATE(EXPR_ENDARG);} ')' + { + if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0; + $$ = $2; + /*% ripper: paren!($:2) %*/ + } + | tLPAREN compstmt(stmts) ')' + { + if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0; + $$ = NEW_BLOCK($2, &@$); + /*% ripper: paren!($:2) %*/ + } + | primary_value tCOLON2 tCONSTANT + { + $$ = NEW_COLON2($1, $3, &@$, &@2, &@3); + /*% ripper: const_path_ref!($:1, $:3) %*/ + } + | tCOLON3 tCONSTANT + { + $$ = NEW_COLON3($2, &@$, &@1, &@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, &@$, &@1); + /*% ripper: return0! %*/ + } + | k_yield '(' call_args rparen + { + $$ = NEW_YIELD($3, &@$, &@1, &@2, &@4); + /*% ripper: yield!(paren!($:3)) %*/ + } + | k_yield '(' rparen + { + $$ = NEW_YIELD(0, &@$, &@1, &@2, &@3); + /*% ripper: yield!(paren!(args_new!)) %*/ + } + | k_yield + { + $$ = NEW_YIELD(0, &@$, &@1, &NULL_LOC, &NULL_LOC); + /*% ripper: yield0! %*/ + } + | keyword_defined '\n'? '(' begin_defined expr rparen + { + p->ctxt.in_defined = $4.in_defined; + $$ = new_defined(p, $5, &@$, &@1); + p->ctxt.has_trailing_semicolon = $4.has_trailing_semicolon; + /*% ripper: defined!($:5) %*/ + } + | keyword_not '(' expr rparen + { + $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$); + /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/ + } + | keyword_not '(' rparen + { + $$ = call_uni_op(p, method_cond(p, NEW_NIL(&@2), &@2), METHOD_NOT, &@1, &@$); + /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/ + } + | 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(stmts) + if_tail + k_end + { + if ($5 && nd_type_p($5, NODE_IF)) + RNODE_IF($5)->end_keyword_loc = @6; + + $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &@6); + fixpos($$, $2); + /*% ripper: if!($:2, $:4, $:5) %*/ + } + | k_unless expr_value then + compstmt(stmts) + opt_else + k_end + { + $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6); + fixpos($$, $2); + /*% ripper: unless!($:2, $:4, $:5) %*/ + } + | k_while expr_value_do + compstmt(stmts) + k_end + { + restore_block_exit(p, $1); + $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4); + fixpos($$, $2); + /*% ripper: while!($:2, $:3) %*/ + } + | k_until expr_value_do + compstmt(stmts) + k_end + { + restore_block_exit(p, $1); + $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4); + fixpos($$, $2); + /*% ripper: until!($:2, $:3) %*/ + } + | k_case expr_value terms? + { + $$ = p->case_labels; + p->case_labels = CHECK_LITERAL_WHEN; + }<labels> + case_body + k_end + { + if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels); + p->case_labels = $4; + $$ = NEW_CASE($2, $5, &@$, &@1, &@6); + fixpos($$, $2); + /*% ripper: case!($:2, $:5) %*/ + } + | k_case terms? + { + $$ = p->case_labels; + p->case_labels = 0; + }<labels> + case_body + k_end + { + if (p->case_labels) st_free_table(p->case_labels); + p->case_labels = $3; + $$ = NEW_CASE2($4, &@$, &@1, &@5); + /*% ripper: case!(Qnil, $:4) %*/ + } + | k_case expr_value terms? + p_case_body + k_end + { + $$ = NEW_CASE3($2, $4, &@$, &@1, &@5); + /*% ripper: case!($:2, $:4) %*/ + } + | k_for for_var keyword_in + {COND_PUSH(1);} expr_value do {COND_POP();} + compstmt(stmts) + 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_args_tail(p, 0, 0, 0, &@for_var), &@for_var); + scope = NEW_SCOPE2(tbl, args, $compstmt, NULL, &@$); + YYLTYPE do_keyword_loc = $do == keyword_do_cond ? @do : NULL_LOC; + $$ = NEW_FOR($5, 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 + { + $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1)); + /*% ripper: break!(args_new!) %*/ + } + | keyword_next + { + $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1)); + /*% ripper: next!(args_new!) %*/ + } + | keyword_redo + { + $$ = add_block_exit(p, NEW_REDO(&@$, &@1)); + /*% 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 : value_expr(primary) + ; k_begin : keyword_begin - { - token_info_push("begin"); - } - ; + { + token_info_push(p, "begin", &@$); + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; k_if : keyword_if - { - token_info_push("if"); - } - ; + { + WARN_EOL("if"); + token_info_push(p, "if", &@$); + if (p->token_info && p->token_info->nonspc && + p->token_info->next && !strcmp(p->token_info->next->token, "else")) { + const char *tok = p->lex.ptok - rb_strlen_lit("if"); + const char *beg = p->lex.pbeg + p->token_info->next->beg.column; + beg += rb_strlen_lit("else"); + while (beg < tok && ISSPACE(*beg)) beg++; + if (beg == tok) { + p->token_info->nonspc = 0; + } + } + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; k_unless : keyword_unless - { - token_info_push("unless"); - } - ; - -k_while : keyword_while - { - token_info_push("while"); - } - ; - -k_until : keyword_until - { - token_info_push("until"); - } - ; + { + token_info_push(p, "unless", &@$); + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; + +k_while : keyword_while allow_exits + { + $$ = $allow_exits; + token_info_push(p, "while", &@$); + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; + +k_until : keyword_until allow_exits + { + $$ = $allow_exits; + token_info_push(p, "until", &@$); + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; k_case : keyword_case - { - token_info_push("case"); - } - ; + { + token_info_push(p, "case", &@$); + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; -k_for : keyword_for - { - token_info_push("for"); - } - ; +k_for : keyword_for allow_exits + { + $$ = $allow_exits; + token_info_push(p, "for", &@$); + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; k_class : keyword_class - { - token_info_push("class"); - } - ; + { + token_info_push(p, "class", &@$); + $$ = p->ctxt; + p->ctxt.in_rescue = before_rescue; + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; k_module : keyword_module - { - token_info_push("module"); - } - ; + { + token_info_push(p, "module", &@$); + $$ = p->ctxt; + p->ctxt.in_rescue = before_rescue; + push_end_expect_token_locations(p, &@1.beg_pos); + } + ; k_def : keyword_def - { - token_info_push("def"); - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% - %*/ - } - ; + { + token_info_push(p, "def", &@$); + $$ = NEW_DEF_TEMP(&@$); + p->ctxt.in_argdef = 1; + } + ; + +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); + } + ; + +k_rescue : keyword_rescue + { + token_info_warn(p, "rescue", p->token_info, 1, &@$); + $$ = p->ctxt; + p->ctxt.in_rescue = after_rescue; + } + ; + +k_ensure : keyword_ensure + { + token_info_warn(p, "ensure", p->token_info, 1, &@$); + $$ = p->ctxt; + } + ; + +k_when : keyword_when + { + token_info_warn(p, "when", p->token_info, 0, &@$); + } + ; + +k_else : keyword_else + { + token_info *ptinfo_beg = p->token_info; + int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0; + token_info_warn(p, "else", p->token_info, same, &@$); + if (same) { + token_info e; + e.next = ptinfo_beg->next; + e.token = "else"; + token_info_setup(&e, p->lex.pbeg, &@$); + if (!e.nonspc) *ptinfo_beg = e; + } + } + ; + +k_elsif : keyword_elsif + { + WARN_EOL("elsif"); + token_info_warn(p, "elsif", p->token_info, 1, &@$); + } + ; k_end : keyword_end - { - token_info_pop("end"); - } - ; + { + token_info_pop(p, "end", &@$); + pop_end_expect_token_locations(p); + } + | tDUMNY_END + { + compile_error(p, "syntax error, unexpected end-of-input"); + } + ; + +k_return : keyword_return + { + if (p->ctxt.cant_return && !dyna_in_block(p)) + yyerror1(&@1, "Invalid return in class/module body"); + } + ; + +k_yield : keyword_yield + { + if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval) + yyerror1(&@1, "Invalid yield"); + } + ; then : term - /*%c%*/ - /*%c - { $$ = Qnil; } - %*/ - | keyword_then - | term keyword_then - /*%c%*/ - /*%c - { $$ = $2; } - %*/ - ; + | keyword_then + | term keyword_then + ; do : term - /*%c%*/ - /*%c - { $$ = Qnil; } - %*/ - | keyword_do_cond - ; + | keyword_do_cond { $$ = keyword_do_cond; } + ; if_tail : opt_else - | keyword_elsif expr_value then - compstmt - if_tail - { - /*%%%*/ - $$ = new_if($2, $4, $5); - fixpos($$, $2); - /*% - $$ = dispatch3(elsif, $2, $4, escape_Qundef($5)); - %*/ - } - ; + | k_elsif expr_value then + compstmt(stmts) + if_tail + { + $$ = new_if(p, $2, $4, $5, &@$, &@1, &@3, &NULL_LOC); + fixpos($$, $2); + /*% ripper: elsif!($:2, $:4, $:5) %*/ + } + ; opt_else : none - | keyword_else compstmt - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(else, $2); - %*/ - } - ; + | k_else compstmt(stmts) + { + $$ = $2; + /*% ripper: else!($:2) %*/ + } + ; for_var : lhs - | mlhs - ; + | mlhs + ; f_marg : f_norm_arg - { - $$ = assignable($1, 0); - /*%%%*/ - /*% - $$ = dispatch1(mlhs_paren, $$); - %*/ - } - | tLPAREN f_margs rparen - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(mlhs_paren, $2); - %*/ - } - ; - -f_marg_list : f_marg - { - /*%%%*/ - $$ = NEW_LIST($1); - /*% - $$ = mlhs_add(mlhs_new(), $1); - %*/ - } - | f_marg_list ',' f_marg - { - /*%%%*/ - $$ = list_append($1, $3); - /*% - $$ = mlhs_add($1, $3); - %*/ - } - ; - -f_margs : f_marg_list - { - /*%%%*/ - $$ = NEW_MASGN($1, 0); - /*% - $$ = $1; - %*/ - } - | f_marg_list ',' tSTAR f_norm_arg - { - $$ = assignable($4, 0); - /*%%%*/ - $$ = NEW_MASGN($1, $$); - /*% - $$ = mlhs_add_star($1, $$); - %*/ - } - | f_marg_list ',' tSTAR f_norm_arg ',' f_marg_list - { - $$ = assignable($4, 0); - /*%%%*/ - $$ = NEW_MASGN($1, NEW_POSTARG($$, $6)); - /*% - $$ = mlhs_add_star($1, $$); - %*/ - } - | f_marg_list ',' tSTAR - { - /*%%%*/ - $$ = NEW_MASGN($1, -1); - /*% - $$ = mlhs_add_star($1, Qnil); - %*/ - } - | f_marg_list ',' tSTAR ',' f_marg_list - { - /*%%%*/ - $$ = NEW_MASGN($1, NEW_POSTARG(-1, $5)); - /*% - $$ = mlhs_add_star($1, $5); - %*/ - } - | tSTAR f_norm_arg - { - $$ = assignable($2, 0); - /*%%%*/ - $$ = NEW_MASGN(0, $$); - /*% - $$ = mlhs_add_star(mlhs_new(), $$); - %*/ - } - | tSTAR f_norm_arg ',' f_marg_list - { - $$ = assignable($2, 0); - /*%%%*/ - $$ = NEW_MASGN(0, NEW_POSTARG($$, $4)); - /*% - #if 0 - TODO: Check me - #endif - $$ = mlhs_add_star($$, $4); - %*/ - } - | tSTAR - { - /*%%%*/ - $$ = NEW_MASGN(0, -1); - /*% - $$ = mlhs_add_star(mlhs_new(), Qnil); - %*/ - } - | tSTAR ',' f_marg_list - { - /*%%%*/ - $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3)); - /*% - $$ = mlhs_add_star(mlhs_new(), Qnil); - %*/ - } - ; - - -block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg - { - $$ = new_args_tail($1, $3, $4); - } - | f_block_kwarg opt_f_block_arg - { - $$ = new_args_tail($1, Qnone, $2); - } - | f_kwrest opt_f_block_arg - { - $$ = new_args_tail(Qnone, $1, $2); - } - | f_block_arg - { - $$ = new_args_tail(Qnone, Qnone, $1); - } - ; - -opt_block_args_tail : ',' block_args_tail - { - $$ = $2; - } - | /* none */ - { - $$ = new_args_tail(Qnone, Qnone, Qnone); - } - ; - -block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail - { - $$ = new_args($1, $3, $5, Qnone, $6); - } - | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail - { - $$ = new_args($1, $3, $5, $7, $8); - } - | f_arg ',' f_block_optarg opt_block_args_tail - { - $$ = new_args($1, $3, Qnone, Qnone, $4); - } - | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail - { - $$ = new_args($1, $3, Qnone, $5, $6); - } - | f_arg ',' f_rest_arg opt_block_args_tail - { - $$ = new_args($1, Qnone, $3, Qnone, $4); - } - | f_arg ',' - { - $$ = new_args($1, Qnone, 1, Qnone, new_args_tail(Qnone, Qnone, Qnone)); - /*%%%*/ - /*% - dispatch1(excessed_comma, $$); - %*/ - } - | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail - { - $$ = new_args($1, Qnone, $3, $5, $6); - } - | f_arg opt_block_args_tail - { - $$ = new_args($1, Qnone, Qnone, Qnone, $2); - } - | f_block_optarg ',' f_rest_arg opt_block_args_tail - { - $$ = new_args(Qnone, $1, $3, Qnone, $4); - } - | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail - { - $$ = new_args(Qnone, $1, $3, $5, $6); - } - | f_block_optarg opt_block_args_tail - { - $$ = new_args(Qnone, $1, Qnone, Qnone, $2); - } - | f_block_optarg ',' f_arg opt_block_args_tail - { - $$ = new_args(Qnone, $1, Qnone, $3, $4); - } - | f_rest_arg opt_block_args_tail - { - $$ = new_args(Qnone, Qnone, $1, Qnone, $2); - } - | f_rest_arg ',' f_arg opt_block_args_tail - { - $$ = new_args(Qnone, Qnone, $1, $3, $4); - } - | block_args_tail - { - $$ = new_args(Qnone, Qnone, Qnone, Qnone, $1); - } - ; - -opt_block_param : none - | block_param_def - { - command_start = TRUE; - } - ; - -block_param_def : '|' opt_bv_decl '|' - { - current_arg = 0; - /*%%%*/ - $$ = 0; - /*% - $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), - escape_Qundef($2)); - %*/ - } - | tOROP - { - /*%%%*/ - $$ = 0; - /*% - $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), - Qnil); - %*/ - } - | '|' block_param opt_bv_decl '|' - { - current_arg = 0; - /*%%%*/ - $$ = $2; - /*% - $$ = blockvar_new(escape_Qundef($2), escape_Qundef($3)); - %*/ - } - ; - - -opt_bv_decl : opt_nl - { - $$ = 0; - } - | opt_nl ';' bv_decls opt_nl - { - /*%%%*/ - $$ = 0; - /*% - $$ = $3; - %*/ - } - ; + { + $$ = assignable(p, $1, 0, &@$); + mark_lvar_used(p, $$); + } + | tLPAREN f_margs rparen + { + $$ = (NODE *)$2; + /*% ripper: mlhs_paren!($:2) %*/ + } + ; + + +f_margs : mlhs_items(f_marg) + { + $$ = NEW_MASGN($1, 0, &@$); + /*% ripper: $:1 %*/ + } + | mlhs_items(f_marg) ',' f_rest_marg + { + $$ = NEW_MASGN($1, $3, &@$); + /*% ripper: mlhs_add_star!($:1, $:3) %*/ + } + | 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) %*/ + } + | f_rest_marg + { + $$ = NEW_MASGN(0, $1, &@$); + /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/ + } + | 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) %*/ + } + ; + +f_rest_marg : tSTAR f_norm_arg + { + /*% ripper: $:2 %*/ + $$ = assignable(p, $2, 0, &@$); + mark_lvar_used(p, $$); + } + | tSTAR + { + $$ = NODE_SPECIAL_NO_NAME_REST; + /*% ripper: Qnil %*/ + } + ; + +f_any_kwrest : f_kwrest + | f_no_kwarg + { + $$ = idNil; + /*% ripper: ID2VAL(idNil) %*/ + } + ; + +f_eq : {p->ctxt.in_argdef = 0;} '='; + +block_args_tail : args_tail_basic(primary_value) + ; + +excessed_comma : ',' + { + /* magic number for rest_id in iseq_set_arguments() */ + $$ = NODE_SPECIAL_EXCESSIVE_COMMA; + /*% ripper: excessed_comma! %*/ + } + ; + +block_param : f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, $1, $3, $5, 0, $6, &@$); + /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/ + } + | f_arg ',' f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, $1, $3, $5, $7, $8, &@$); + /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/ + } + | f_arg ',' f_opt_arg(primary_value) opt_args_tail(block_args_tail) + { + $$ = new_args(p, $1, $3, 0, 0, $4, &@$); + /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/ + } + | f_arg ',' f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, $1, $3, 0, $5, $6, &@$); + /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/ + } + | f_arg ',' f_rest_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, $1, 0, $3, 0, $4, &@$); + /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/ + } + | f_arg excessed_comma + { + $$ = new_args_tail(p, 0, 0, 0, &@2); + $$ = new_args(p, $1, 0, $2, 0, $$, &@$); + /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/ + } + | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, $1, 0, $3, $5, $6, &@$); + /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/ + } + | f_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, $1, 0, 0, 0, $2, &@$); + /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/ + } + | f_opt_arg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, 0, $1, $3, 0, $4, &@$); + /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/ + } + | f_opt_arg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, 0, $1, $3, $5, $6, &@$); + /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/ + } + | f_opt_arg(primary_value) opt_args_tail(block_args_tail) + { + $$ = new_args(p, 0, $1, 0, 0, $2, &@$); + /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/ + } + | f_opt_arg(primary_value) ',' f_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, 0, $1, 0, $3, $4, &@$); + /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/ + } + | f_rest_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, 0, 0, $1, 0, $2, &@$); + /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/ + } + | f_rest_arg ',' f_arg opt_args_tail(block_args_tail) + { + $$ = new_args(p, 0, 0, $1, $3, $4, &@$); + /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/ + } + | block_args_tail + { + $$ = new_args(p, 0, 0, 0, 0, $1, &@$); + /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/ + } + ; + +opt_block_param_def : none + | block_param_def + { + p->command_start = TRUE; + } + ; + +block_param_def : '|' opt_block_param opt_bv_decl '|' + { + p->max_numparam = ORDINAL_PARAM; + p->ctxt.in_argdef = 0; + $$ = $2; + /*% ripper: block_var!($:2, $:3) %*/ + } + ; + +opt_block_param : /* none */ + { + $$ = 0; + /*% ripper: params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil) %*/ + } + | block_param + ; + +opt_bv_decl : '\n'? + { + $$ = 0; + /*% ripper: Qfalse %*/ + } + | '\n'? ';' bv_decls '\n'? + { + $$ = 0; + /*% ripper: $:3 %*/ + } + ; bv_decls : bvar - /*%c%*/ - /*%c - { - $$ = rb_ary_new3(1, $1); - } - %*/ - | bv_decls ',' bvar - /*%c%*/ - /*%c - { - rb_ary_push($1, $3); - } - %*/ - ; + /*% ripper[brace]: rb_ary_new3(1, $:1) %*/ + | bv_decls ',' bvar + /*% ripper[brace]: rb_ary_push($:1, $:3) %*/ + ; bvar : tIDENTIFIER - { - new_bv(get_id($1)); - /*%%%*/ - /*% - $$ = get_value($1); - %*/ - } - | f_bad_arg - { - $$ = 0; - } - ; - -lambda : { - $<vars>$ = dyna_push(); - } - { - $<num>$ = lpar_beg; - lpar_beg = ++paren_nest; - } - f_larglist - { - $<num>$ = ruby_sourceline; - } - { - $<val>$ = cmdarg_stack; - cmdarg_stack = 0; - } - lambda_body - { - lpar_beg = $<num>2; - cmdarg_stack = $<val>5; - CMDARG_LEXPOP(); - /*%%%*/ - $$ = NEW_LAMBDA($3, $6); - nd_set_line($$, $<num>4); - /*% - $$ = dispatch2(lambda, $3, $6); - %*/ - dyna_pop($<vars>1); - } - ; + { + new_bv(p, $1); + /*% ripper: $:1 %*/ + } + | f_bad_arg + ; + +max_numparam : { + $$ = p->max_numparam; + p->max_numparam = 0; + } + ; + +numparam : { + $$ = numparam_push(p); + } + ; + +it_id : { + $$ = p->it_id; + p->it_id = 0; + } + ; + +lambda : tLAMBDA[lpar] + { + token_info_push(p, "->", &@1); + $$ = dyna_push(p); + }[dyna]<vars> + max_numparam numparam it_id allow_exits + f_larglist[args] + { + CMDARG_PUSH(0); + } + lambda_body[body] + { + int max_numparam = p->max_numparam; + 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, it_id); + { + 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); + xfree($body); + } + /*% ripper: lambda!($:args, $:body) %*/ + numparam_pop(p, $numparam); + dyna_pop(p, $dyna); + } + ; f_larglist : '(' f_args opt_bv_decl ')' - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(paren, $2); - %*/ - } - | f_args - { - $$ = $1; - } - ; - -lambda_body : tLAMBEG compstmt '}' - { - $$ = $2; - } - | keyword_do_LAMBDA compstmt keyword_end - { - $$ = $2; - } - ; - -do_block : keyword_do_block - { - $<vars>1 = dyna_push(); - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% %*/ - } - opt_block_param - compstmt - keyword_end - { - /*%%%*/ - $$ = NEW_ITER($3,$4); - nd_set_line($$, $<num>2); - /*% - $$ = dispatch2(do_block, escape_Qundef($3), $4); - %*/ - dyna_pop($<vars>1); - } - ; + { + p->ctxt.in_argdef = 0; + $$ = $f_args; + p->max_numparam = ORDINAL_PARAM; + /*% ripper: paren!($:2) %*/ + } + | f_args + { + p->ctxt.in_argdef = 0; + if (!args_info_empty_p(&$1->nd_ainfo)) + p->max_numparam = ORDINAL_PARAM; + $$ = $f_args; + } + ; + +lambda_body : tLAMBEG compstmt(stmts) '}' + { + token_info_pop(p, "}", &@3); + $$ = 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 + { + $$ = 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($1) == NODE_YIELD) { - compile_error(PARSER_ARG "block given to yield"); - } - else { - block_dup_check($1->nd_args, $2); - } - $2->nd_iter = $1; - $$ = $2; - fixpos($$, $1); - /*% - $$ = method_add_block($1, $2); - %*/ - } - | block_call call_op2 operation2 opt_paren_args - { - /*%%%*/ - $$ = NEW_CALL($1, $3, $4); - /*% - $$ = dispatch3(call, $1, $2, $3); - $$ = method_optarg($$, $4); - %*/ - } - | block_call call_op2 operation2 opt_paren_args brace_block - { - /*%%%*/ - block_dup_check($4, $5); - $5->nd_iter = NEW_CALL($1, $3, $4); - $$ = $5; - fixpos($$, $1); - /*% - $$ = dispatch4(command_call, $1, $2, $3, $4); - $$ = method_add_block($$, $5); - %*/ - } - | block_call call_op2 operation2 command_args do_block - { - /*%%%*/ - block_dup_check($4, $5); - $5->nd_iter = NEW_CALL($1, $3, $4); - $$ = $5; - fixpos($$, $1); - /*% - $$ = dispatch4(command_call, $1, $2, $3, $4); - $$ = method_add_block($$, $5); - %*/ - } - ; + { + if (nd_type_p($1, NODE_YIELD)) { + compile_error(p, "block given to yield"); + } + else { + block_dup_check(p, get_nd_args(p, $1), $2); + } + $$ = method_add_block(p, $1, $2, &@$); + fixpos($$, $1); + /*% 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: 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: 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) %*/ + } + | 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; - /*% - $$ = method_arg(dispatch1(fcall, $1), $2); - %*/ - } - | primary_value call_op operation2 - { - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% %*/ - } - opt_paren_args - { - /*%%%*/ - $$ = NEW_QCALL($2, $1, $3, $5); - nd_set_line($$, $<num>4); - /*% - $$ = dispatch3(call, $1, ripper_id2sym($2), $3); - $$ = method_optarg($$, $5); - %*/ - } - | primary_value tCOLON2 operation2 - { - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% %*/ - } - paren_args - { - /*%%%*/ - $$ = NEW_CALL($1, $3, $5); - nd_set_line($$, $<num>4); - /*% - $$ = dispatch3(call, $1, ripper_id2sym(idCOLON2), $3); - $$ = method_optarg($$, $5); - %*/ - } - | primary_value tCOLON2 operation3 - { - /*%%%*/ - $$ = NEW_CALL($1, $3, 0); - /*% - $$ = dispatch3(call, $1, ID2SYM(idCOLON2), $3); - %*/ - } - | primary_value call_op - { - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% %*/ - } - paren_args - { - /*%%%*/ - $$ = NEW_QCALL($2, $1, idCall, $4); - nd_set_line($$, $<num>3); - /*% - $$ = dispatch3(call, $1, ripper_id2sym($2), - ID2SYM(idCall)); - $$ = method_optarg($$, $4); - %*/ - } - | primary_value tCOLON2 - { - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% %*/ - } - paren_args - { - /*%%%*/ - $$ = NEW_CALL($1, idCall, $4); - nd_set_line($$, $<num>3); - /*% - $$ = dispatch3(call, $1, ID2SYM(idCOLON2), - ID2SYM(idCall)); - $$ = method_optarg($$, $4); - %*/ - } - | keyword_super paren_args - { - /*%%%*/ - $$ = NEW_SUPER($2); - /*% - $$ = dispatch1(super, $2); - %*/ - } - | keyword_super - { - /*%%%*/ - $$ = NEW_ZSUPER(); - /*% - $$ = dispatch0(zsuper); - %*/ - } - | primary_value '[' opt_call_args rbracket - { - /*%%%*/ - if ($1 && nd_type($1) == NODE_SELF) - $$ = NEW_FCALL(tAREF, $3); - else - $$ = NEW_CALL($1, tAREF, $3); - fixpos($$, $1); - /*% - $$ = dispatch2(aref, $1, escape_Qundef($3)); - %*/ - } - ; - -brace_block : '{' - { - $<vars>1 = dyna_push(); - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% - %*/ - } - opt_block_param - compstmt '}' - { - /*%%%*/ - $$ = NEW_ITER($3,$4); - nd_set_line($$, $<num>2); - /*% - $$ = dispatch2(brace_block, escape_Qundef($3), $4); - %*/ - dyna_pop($<vars>1); - } - | keyword_do - { - $<vars>1 = dyna_push(); - /*%%%*/ - $<num>$ = ruby_sourceline; - /*% - %*/ - } - opt_block_param - compstmt keyword_end - { - /*%%%*/ - $$ = NEW_ITER($3,$4); - nd_set_line($$, $<num>2); - /*% - $$ = dispatch2(do_block, escape_Qundef($3), $4); - %*/ - dyna_pop($<vars>1); - } - ; - -case_body : keyword_when args then - compstmt - cases - { - /*%%%*/ - $$ = NEW_WHEN($2, $4, $5); - /*% - $$ = dispatch3(when, $2, $4, escape_Qundef($5)); - %*/ - } - ; + { + $1->nd_args = $2; + $$ = (NODE *)$1; + nd_set_last_loc($1, @2.end_pos); + /*% 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: call!($:1, $:2, $:3) %*/ + if (has_args) { + /*% ripper: method_add_arg!($:$, $:4) %*/ + } + } + | primary_value tCOLON2 operation2 paren_args + { + $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$); + nd_set_line($$, @3.end_pos.lineno); + /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/ + } + | primary_value tCOLON2 operation3 + { + $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$); + /*% ripper: call!($:1, $:2, $:3) %*/ + } + | primary_value 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) %*/ + } + | keyword_super paren_args + { + 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) %*/ + } + ; + +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 : {$$ = 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; + p->it_id = $it_id; + $args = args_with_numbered(p, $args, max_numparam, it_id); + $$ = NEW_ITER($args, $compstmt, &@$); + /*% ripper: brace_block!($:args, $:compstmt) %*/ + restore_block_exit(p, $allow_exits); + numparam_pop(p, $numparam); + dyna_pop(p, $dyna); + } + ; + +do_body : { + $$ = dyna_push(p); + CMDARG_PUSH(0); + }[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; + p->it_id = $it_id; + $args = args_with_numbered(p, $args, max_numparam, it_id); + $$ = NEW_ITER($args, $bodystmt, &@$); + /*% ripper: do_block!($:args, $:bodystmt) %*/ + CMDARG_POP(); + restore_block_exit(p, $allow_exits); + numparam_pop(p, $numparam); + dyna_pop(p, $dyna); + } + ; + +case_args : arg_value + { + check_literal_when(p, $arg_value, &@arg_value); + $$ = NEW_LIST($arg_value, &@$); + /*% ripper: args_add!(args_new!, $:arg_value) %*/ + } + | tSTAR arg_value + { + $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR); + /*% ripper: args_add_star!(args_new!, $:arg_value) %*/ + } + | case_args[non_last_args] ',' arg_value + { + 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[non_last_args] ',' tSTAR arg_value + { + $$ = 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(stmts) + cases + { + $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3); + fixpos($$, $2); + /*% ripper: when!($:2, $:4, $:5) %*/ + } + ; cases : opt_else - | case_body - ; - -opt_rescue : keyword_rescue exc_list exc_var then - compstmt - opt_rescue - { - /*%%%*/ - if ($3) { - $3 = node_assign($3, NEW_ERRINFO()); - $5 = block_append($3, $5); - } - $$ = NEW_RESBODY($2, $5, $6); - fixpos($$, $2?$2:$5); - /*% - $$ = dispatch4(rescue, - escape_Qundef($2), - escape_Qundef($3), - escape_Qundef($5), - escape_Qundef($6)); - %*/ - } - | none - ; + | case_body + ; + +p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();}; +p_pktbl : {$$ = p->pktbl; p->pktbl = 0;}; + +p_in_kwarg : { + $$ = p->ctxt; + 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; + } + ; + +p_case_body : keyword_in + p_in_kwarg[ctxt] p_pvtbl p_pktbl + p_top_expr[expr] then + { + 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(stmts) + p_cases[cases] + { + $$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC); + /*% ripper: in!($:expr, $:compstmt, $:cases) %*/ + } + ; + +p_cases : opt_else + | p_case_body + ; + +p_top_expr : p_top_expr_body + | p_top_expr_body modifier_if expr_value + { + $$ = new_if(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC); + fixpos($$, $3); + /*% ripper: if_mod!($:3, $:1) %*/ + } + | p_top_expr_body modifier_unless expr_value + { + $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC); + fixpos($$, $3); + /*% ripper: unless_mod!($:3, $:1) %*/ + } + ; + +p_top_expr_body : p_expr + | p_expr ',' + { + $$ = 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, 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, 0, $1, &@$); + /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/ + } + | p_args_tail + { + $$ = new_array_pattern(p, 0, 0, $1, &@$); + /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/ + } + | p_kwargs + { + $$ = new_hash_pattern(p, 0, $1, &@$); + /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/ + } + ; + +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, ID2VAL((id_assoc)), $:3) %*/ + } + | p_alt + ; + +p_alt : p_alt[left] '|'[alt] + { + 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; + /*% 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, 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, 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, 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, 0, 0, 0, 0, &@$); + $$ = new_array_pattern(p, $1, 0, $$, &@$); + /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/ + } + | tLBRACK p_args rbracket + { + $$ = new_array_pattern(p, 0, 0, $p_args, &@$); + /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/ + } + | tLBRACK p_find rbracket + { + $$ = new_find_pattern(p, 0, $p_find, &@$); + /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/ + } + | tLBRACK rbracket + { + $$ = 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] + { + p->ctxt.in_kwarg = 0; + } + p_kwargs rbrace + { + pop_pktbl(p, $p_pktbl); + p->ctxt.in_kwarg = $ctxt.in_kwarg; + $$ = new_hash_pattern(p, 0, $p_kwargs, &@$); + /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/ + } + | tLBRACE rbrace + { + $$ = 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, 0, 0, &@$); + /*% ripper: [[$:1], Qnil, Qnil] %*/ + } + | p_args_head + { + $$ = 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, 0, 0, &@$); + /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/ + } + | p_args_head p_rest + { + $$ = 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 ',' + | p_args_head p_arg ',' + { + $$ = list_concat($1, $2); + /*% ripper: rb_ary_concat($:1, $:2) %*/ + } + ; + +p_args_tail : p_rest + { + $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$); + /*% ripper: [Qnil, $:1, Qnil] %*/ + } + | p_rest ',' p_args_post + { + $$ = 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, &@$); + } + | tSTAR + { + $$ = 0; + /*% ripper: var_field!(Qnil) %*/ + } + ; + +p_args_post : p_arg + | p_args_post ',' p_arg + { + $$ = list_concat($1, $3); + /*% ripper: rb_ary_concat($:1, $:3) %*/ + } + ; + +p_arg : p_expr + { + $$ = NEW_LIST($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, 0, &@$), $1, &@$); + /*% ripper: [[], $:1] %*/ + } + ; + +p_kwarg : p_kw + /*% ripper[brace]: [$:1] %*/ + | p_kwarg ',' p_kw + { + $$ = list_concat($1, $3); + /*% ripper: rb_ary_push($:1, $:3) %*/ + } + ; + +p_kw : p_kw_label p_expr + { + 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, $1, &@1); + if ($1 && !is_local_id($1)) { + yyerror1(&@1, "key must be valid as local variables"); + } + error_duplicate_pattern_variable(p, $1, &@1); + $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$)); + /*% ripper: [$:1, Qnil] %*/ + } + ; + +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); + $$ = rb_sym2id(rb_node_sym_string_val(node)); + } + else { + yyerror1(&loc, "symbol literal with interpolation is not allowed"); + $$ = rb_intern_str(STR_NEW0()); + } + /*% ripper: $:2 %*/ + } + ; + +p_kwrest : kwrest_mark tIDENTIFIER + { + $$ = $2; + /*% ripper: var_field!($:2) %*/ + } + | kwrest_mark + { + $$ = 0; + /*% ripper: Qnil %*/ + } + ; + +p_kwnorest : kwrest_mark keyword_nil + { + $$ = 0; + } + ; + +p_any_kwrest : p_kwrest + | p_kwnorest + { + $$ = idNil; + /*% ripper: var_field!(ID2VAL(idNil)) %*/ + } + ; + +p_value : p_primitive + | range_expr(p_primitive) + | p_var_ref + | p_expr_ref + | p_const + ; + +p_primitive : inline_primary + | keyword_variable + { + 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, &@$); + } + ; + +p_var_ref : '^' tIDENTIFIER + { + NODE *n = gettable(p, $2, &@$); + 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) %*/ + } + | '^' nonlocal_var + { + if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$); + /*% ripper: var_ref!($:2) %*/ + } + ; + +p_expr_ref : '^' tLPAREN expr_value rparen + { + $$ = NEW_BLOCK($3, &@$); + /*% ripper: begin!($:3) %*/ + } + ; + +p_const : tCOLON3 cname + { + $$ = NEW_COLON3($2, &@$, &@1, &@2); + /*% ripper: top_const_ref!($:2) %*/ + } + | p_const tCOLON2 cname + { + $$ = NEW_COLON2($1, $3, &@$, &@2, &@3); + /*% ripper: const_path_ref!($:1, $:3) %*/ + } + | tCONSTANT + { + $$ = gettable(p, $1, &@$); + /*% ripper: var_ref!($:1) %*/ + } + ; + +opt_rescue : k_rescue exc_list exc_var then + compstmt(stmts) + opt_rescue + { + NODE *err = $3; + if ($3) { + err = NEW_ERRINFO(&@3); + err = node_assign(p, $3, err, NO_LEX_CTXT, &@3); + } + $$ = NEW_RESBODY($2, $3, $5, $6, &@$); + if ($2) { + fixpos($$, $2); + } + else if ($3) { + fixpos($$, $3); + } + else { + fixpos($$, $5); + } + /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/ + } + | none + ; exc_list : arg_value - { - /*%%%*/ - $$ = NEW_LIST($1); - /*% - $$ = rb_ary_new3(1, $1); - %*/ - } - | mrhs - { - /*%%%*/ - if (!($$ = splat_array($1))) $$ = $1; - /*% - $$ = $1; - %*/ - } - | none - ; + { + $$ = NEW_LIST($1, &@$); + /*% ripper: rb_ary_new3(1, $:1) %*/ + } + | mrhs + { + if (!($$ = splat_array($1))) $$ = $1; + } + | none + ; exc_var : tASSOC lhs - { - $$ = $2; - } - | none - ; - -opt_ensure : keyword_ensure compstmt - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(ensure, $2); - %*/ - } - | none - ; + { + $$ = $2; + /*% ripper: $:2 %*/ + } + | none + ; + +opt_ensure : k_ensure stmts terms? + { + p->ctxt.in_rescue = $1.in_rescue; + $$ = $2; + void_expr(p, void_stmts(p, $$)); + /*% ripper: ensure!($:2) %*/ + } + | none + ; literal : numeric - | symbol - { - /*%%%*/ - $$ = NEW_LIT(ID2SYM($1)); - /*% - $$ = dispatch1(symbol_literal, $1); - %*/ - } - | dsym - ; + | symbol + ; strings : string - { - /*%%%*/ - NODE *node = $1; - if (!node) { - node = NEW_STR(STR_NEW0()); - } - else { - node = evstr2dstr(node); - } - $$ = node; - /*% - $$ = $1; - %*/ - } - ; + { + if (!$1) { + $$ = NEW_STR(STRING_NEW0(), &@$); + } + else { + $$ = evstr2dstr(p, $1); + } + /*% ripper: $:1 %*/ + } + ; string : tCHAR - | string1 - | string string1 - { - /*%%%*/ - $$ = literal_concat($1, $2); - /*% - $$ = dispatch2(string_concat, $1, $2); - %*/ - } - ; + | string1 + | string string1 + { + $$ = literal_concat(p, $1, $2, &@$); + /*% ripper: string_concat!($:1, $:2) %*/ + } + ; string1 : tSTRING_BEG string_contents tSTRING_END - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(string_literal, $2); - %*/ - } - ; + { + $$ = heredoc_dedent(p, $2); + if ($$) nd_set_loc($$, &@$); + /*% 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 - { - /*%%%*/ - NODE *node = $2; - if (!node) { - node = NEW_XSTR(STR_NEW0()); - } - else { - switch (nd_type(node)) { - case NODE_STR: - nd_set_type(node, NODE_XSTR); - break; - case NODE_DSTR: - nd_set_type(node, NODE_DXSTR); - break; - default: - node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node)); - break; - } - } - $$ = node; - /*% - $$ = dispatch1(xstring_literal, $2); - %*/ - } - ; + { + $$ = new_xstring(p, 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 - { - /*%%%*/ - int options = $3; - NODE *node = $2; - NODE *list, *prev; - if (!node) { - node = NEW_LIT(reg_compile(STR_NEW0(), options)); - } - else switch (nd_type(node)) { - case NODE_STR: - { - VALUE src = node->nd_lit; - nd_set_type(node, NODE_LIT); - node->nd_lit = reg_compile(src, options); - } - break; - default: - node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node)); - case NODE_DSTR: - if (options & RE_OPTION_ONCE) { - nd_set_type(node, NODE_DREGX_ONCE); - } - else { - nd_set_type(node, NODE_DREGX); - } - node->nd_cflag = options & RE_OPTION_MASK; - if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options); - for (list = (prev = node)->nd_next; list; list = list->nd_next) { - if (nd_type(list->nd_head) == NODE_STR) { - VALUE tail = list->nd_head->nd_lit; - if (reg_fragment_check(tail, options) && prev && !NIL_P(prev->nd_lit)) { - VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit; - if (!literal_concat0(parser, lit, tail)) { - node = 0; - break; - } - rb_str_resize(tail, 0); - prev->nd_next = list->nd_next; - rb_gc_force_recycle((VALUE)list->nd_head); - rb_gc_force_recycle((VALUE)list); - list = prev; - } - else { - prev = list; - } - } - else { - prev = 0; - } - } - if (!node->nd_next) { - VALUE src = node->nd_lit; - nd_set_type(node, NODE_LIT); - node->nd_lit = reg_compile(src, options); - } - break; - } - $$ = node; - /*% - VALUE re = $2, opt = $3, src = 0, err; - int options = 0; - if (ripper_is_node_yylval(re)) { - $2 = RNODE(re)->nd_rval; - src = RNODE(re)->nd_cval; - } - if (ripper_is_node_yylval(opt)) { - $3 = RNODE(opt)->nd_rval; - options = (int)RNODE(opt)->nd_state; - } - if (src && NIL_P(rb_parser_reg_compile(parser, src, options, &err))) { - compile_error(PARSER_ARG "%"PRIsVALUE, err); - } - $$ = dispatch2(regexp_literal, $2, $3); - %*/ - } - ; - -words : tWORDS_BEG ' ' tSTRING_END - { - /*%%%*/ - $$ = NEW_ZARRAY(); - /*% - $$ = dispatch0(words_new); - $$ = dispatch1(array, $$); - %*/ - } - | tWORDS_BEG word_list tSTRING_END - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(array, $2); - %*/ - } - ; + { + $$ = new_regexp(p, $2, $3, &@$, &@1, &@2, &@3); + /*% ripper: regexp_literal!($:2, $:3) %*/ + } + ; + +words : words(tWORDS_BEG, word_list) + ; word_list : /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = dispatch0(words_new); - %*/ - } - | word_list word ' ' - { - /*%%%*/ - $$ = list_append($1, evstr2dstr($2)); - /*% - $$ = dispatch2(words_add, $1, $2); - %*/ - } - ; + { + $$ = 0; + /*% ripper: words_new! %*/ + } + | word_list word ' '+ + { + $$ = list_append(p, $1, evstr2dstr(p, $2)); + /*% ripper: words_add!($:1, $:2) %*/ + } + ; word : string_content - /*%c%*/ - /*%c - { - $$ = dispatch0(word_new); - $$ = dispatch2(word_add, $$, $1); - } - %*/ - | word string_content - { - /*%%%*/ - $$ = literal_concat($1, $2); - /*% - $$ = dispatch2(word_add, $1, $2); - %*/ - } - ; - -symbols : tSYMBOLS_BEG ' ' tSTRING_END - { - /*%%%*/ - $$ = NEW_ZARRAY(); - /*% - $$ = dispatch0(symbols_new); - $$ = dispatch1(array, $$); - %*/ - } - | tSYMBOLS_BEG symbol_list tSTRING_END - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(array, $2); - %*/ - } - ; + /*% ripper[brace]: word_add!(word_new!, $:1) %*/ + | word string_content + { + $$ = literal_concat(p, $1, $2, &@$); + /*% ripper: word_add!($:1, $:2) %*/ + } + ; + +symbols : words(tSYMBOLS_BEG, symbol_list) + ; symbol_list : /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = dispatch0(symbols_new); - %*/ - } - | symbol_list word ' ' - { - /*%%%*/ - $2 = evstr2dstr($2); - if (nd_type($2) == NODE_DSTR) { - nd_set_type($2, NODE_DSYM); - } - else { - nd_set_type($2, NODE_LIT); - $2->nd_lit = rb_str_intern($2->nd_lit); - } - $$ = list_append($1, $2); - /*% - $$ = dispatch2(symbols_add, $1, $2); - %*/ - } - ; - -qwords : tQWORDS_BEG ' ' tSTRING_END - { - /*%%%*/ - $$ = NEW_ZARRAY(); - /*% - $$ = dispatch0(qwords_new); - $$ = dispatch1(array, $$); - %*/ - } - | tQWORDS_BEG qword_list tSTRING_END - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(array, $2); - %*/ - } - ; - -qsymbols : tQSYMBOLS_BEG ' ' tSTRING_END - { - /*%%%*/ - $$ = NEW_ZARRAY(); - /*% - $$ = dispatch0(qsymbols_new); - $$ = dispatch1(array, $$); - %*/ - } - | tQSYMBOLS_BEG qsym_list tSTRING_END - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(array, $2); - %*/ - } - ; + { + $$ = 0; + /*% ripper: symbols_new! %*/ + } + | symbol_list word ' '+ + { + $$ = symbol_append(p, $1, evstr2dstr(p, $2)); + /*% ripper: symbols_add!($:1, $:2) %*/ + } + ; + +qwords : words(tQWORDS_BEG, qword_list) + ; + +qsymbols : words(tQSYMBOLS_BEG, qsym_list) + ; qword_list : /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = dispatch0(qwords_new); - %*/ - } - | qword_list tSTRING_CONTENT ' ' - { - /*%%%*/ - $$ = list_append($1, $2); - /*% - $$ = dispatch2(qwords_add, $1, $2); - %*/ - } - ; + { + $$ = 0; + /*% ripper: qwords_new! %*/ + } + | qword_list tSTRING_CONTENT ' '+ + { + $$ = list_append(p, $1, $2); + /*% ripper: qwords_add!($:1, $:2) %*/ + } + ; qsym_list : /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = dispatch0(qsymbols_new); - %*/ - } - | qsym_list tSTRING_CONTENT ' ' - { - /*%%%*/ - VALUE lit; - lit = $2->nd_lit; - $2->nd_lit = ID2SYM(rb_intern_str(lit)); - nd_set_type($2, NODE_LIT); - $$ = list_append($1, $2); - /*% - $$ = dispatch2(qsymbols_add, $1, $2); - %*/ - } - ; - -string_contents : /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = dispatch0(string_content); - %*/ - } - | string_contents string_content - { - /*%%%*/ - $$ = literal_concat($1, $2); - /*% - $$ = dispatch2(string_add, $1, $2); - %*/ - } - ; + { + $$ = 0; + /*% ripper: qsymbols_new! %*/ + } + | qsym_list tSTRING_CONTENT ' '+ + { + $$ = symbol_append(p, $1, $2); + /*% ripper: qsymbols_add!($:1, $:2) %*/ + } + ; + +string_contents : /* none */ + { + $$ = 0; + /*% ripper: string_content! %*/ + } + | string_contents string_content + { + $$ = literal_concat(p, $1, $2, &@$); + /*% ripper: string_add!($:1, $:2) %*/ + } + ; xstring_contents: /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = dispatch0(xstring_new); - %*/ - } - | xstring_contents string_content - { - /*%%%*/ - $$ = literal_concat($1, $2); - /*% - $$ = dispatch2(xstring_add, $1, $2); - %*/ - } - ; - -regexp_contents: /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = ripper_new_yylval(0, dispatch0(regexp_new), 0); - %*/ - } - | regexp_contents string_content - { - /*%%%*/ - NODE *head = $1, *tail = $2; - if (!head) { - $$ = tail; - } - else if (!tail) { - $$ = head; - } - else { - switch (nd_type(head)) { - case NODE_STR: - nd_set_type(head, NODE_DSTR); - break; - case NODE_DSTR: - break; - default: - head = list_append(NEW_DSTR(Qnil), head); - break; - } - $$ = list_append(head, tail); - } - /*% - VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2; - if (ripper_is_node_yylval(n1)) { - s1 = RNODE(n1)->nd_cval; - n1 = RNODE(n1)->nd_rval; - } - if (ripper_is_node_yylval(n2)) { - s2 = RNODE(n2)->nd_cval; - n2 = RNODE(n2)->nd_rval; - } - $$ = dispatch2(regexp_add, n1, n2); - if (!s1 && s2) { - $$ = ripper_new_yylval(0, $$, s2); - } - %*/ - } - ; + { + $$ = 0; + /*% ripper: xstring_new! %*/ + } + | xstring_contents string_content + { + $$ = literal_concat(p, $1, $2, &@$); + /*% ripper: xstring_add!($:1, $:2) %*/ + } + ; + +regexp_contents : /* none */ + { + $$ = 0; + /*% ripper: regexp_new! %*/ + } + | regexp_contents string_content + { + NODE *head = $1, *tail = $2; + if (!head) { + $$ = tail; + } + else if (!tail) { + $$ = head; + } + else { + switch (nd_type(head)) { + case NODE_STR: + head = str2dstr(p, head); + break; + case NODE_DSTR: + break; + default: + head = list_append(p, NEW_DSTR(0, &@$), head); + break; + } + $$ = list_append(p, head, tail); + } + /*% ripper: regexp_add!($:1, $:2) %*/ + } + ; string_content : tSTRING_CONTENT - | tSTRING_DVAR - { - $<node>$ = lex_strterm; - lex_strterm = 0; - lex_state = EXPR_BEG; - } - string_dvar - { - lex_strterm = $<node>2; - /*%%%*/ - $$ = NEW_EVSTR($3); - /*% - $$ = dispatch1(string_dvar, $3); - %*/ - } - | tSTRING_DBEG - { - $<val>1 = cond_stack; - $<val>$ = cmdarg_stack; - cond_stack = 0; - cmdarg_stack = 0; - } - { - $<node>$ = lex_strterm; - lex_strterm = 0; - } - { - $<num>$ = lex_state; - lex_state = EXPR_BEG; - } - { - $<num>$ = brace_nest; - brace_nest = 0; - } - compstmt tSTRING_DEND - { - cond_stack = $<val>1; - cmdarg_stack = $<val>2; - lex_strterm = $<node>3; - lex_state = $<num>4; - brace_nest = $<num>5; - /*%%%*/ - if ($6) $6->flags &= ~NODE_FL_NEWLINE; - $$ = new_evstr($6); - /*% - $$ = dispatch1(string_embexpr, $6); - %*/ - } - ; - -string_dvar : tGVAR - { - /*%%%*/ - $$ = NEW_GVAR($1); - /*% - $$ = dispatch1(var_ref, $1); - %*/ - } - | tIVAR - { - /*%%%*/ - $$ = NEW_IVAR($1); - /*% - $$ = dispatch1(var_ref, $1); - %*/ - } - | tCVAR - { - /*%%%*/ - $$ = NEW_CVAR($1); - /*% - $$ = dispatch1(var_ref, $1); - %*/ - } - | backref - ; - -symbol : tSYMBEG sym - { - lex_state = EXPR_END; - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(symbol, $2); - %*/ - } - ; + /*% ripper[brace]: $:1 %*/ + | tSTRING_DVAR + { + /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */ + $$ = p->lex.strterm; + p->lex.strterm = 0; + SET_LEX_STATE(EXPR_BEG); + }<strterm> + string_dvar + { + p->lex.strterm = $2; + $$ = NEW_EVSTR($3, &@$, &@1, &NULL_LOC); + nd_set_line($$, @3.end_pos.lineno); + /*% ripper: string_dvar!($:3) %*/ + } + | 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 */ + $$ = p->lex.strterm; + p->lex.strterm = 0; + SET_LEX_STATE(EXPR_BEG); + }[term]<strterm> + { + $$ = p->lex.brace_nest; + p->lex.brace_nest = 0; + }[brace]<num> + { + $$ = p->heredoc_indent; + p->heredoc_indent = 0; + }[indent]<num> + compstmt(stmts) string_dend + { + COND_POP(); + CMDARG_POP(); + 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, &@$, &@state, &@string_dend); + /*% ripper: string_embexpr!($:compstmt) %*/ + } + ; + +string_dend : tSTRING_DEND + | END_OF_INPUT + ; + +string_dvar : nonlocal_var + { + if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$); + /*% ripper: var_ref!($:1) %*/ + } + | backref + ; + +symbol : ssym + | dsym + ; + +ssym : tSYMBEG sym + { + SET_LEX_STATE(EXPR_END); + 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)) %*/ + } + ; sym : fname - | tIVAR - | tGVAR - | tCVAR - ; - -dsym : tSYMBEG xstring_contents tSTRING_END - { - lex_state = EXPR_END; - /*%%%*/ - $$ = dsym_node($2); - /*% - $$ = dispatch1(dyna_symbol, $2); - %*/ - } - ; + | nonlocal_var + ; + +dsym : tSYMBEG string_contents tSTRING_END + { + SET_LEX_STATE(EXPR_END); + $$ = dsym_node(p, $2, &@$); + /*% ripper: dyna_symbol!($:2) %*/ + } + ; numeric : simple_numeric - | tUMINUS_NUM simple_numeric %prec tLOWEST - { - /*%%%*/ - $$ = $2; - $$->nd_lit = negate_lit($$->nd_lit); - /*% - $$ = dispatch2(unary, ID2SYM(idUMinus), $2); - %*/ - } - ; + | tUMINUS_NUM simple_numeric %prec tLOWEST + { + $$ = $2; + negate_lit(p, $$); + /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/ + } + ; simple_numeric : tINTEGER - | tFLOAT - | tRATIONAL - | tIMAGINARY - ; - -user_variable : tIDENTIFIER - | tIVAR - | tGVAR - | tCONSTANT - | tCVAR - ; - -keyword_variable: keyword_nil {ifndef_ripper($$ = keyword_nil);} - | keyword_self {ifndef_ripper($$ = keyword_self);} - | keyword_true {ifndef_ripper($$ = keyword_true);} - | keyword_false {ifndef_ripper($$ = keyword_false);} - | keyword__FILE__ {ifndef_ripper($$ = keyword__FILE__);} - | keyword__LINE__ {ifndef_ripper($$ = keyword__LINE__);} - | keyword__ENCODING__ {ifndef_ripper($$ = keyword__ENCODING__);} - ; + | tFLOAT + | tRATIONAL + | tIMAGINARY + ; + +nonlocal_var : tIVAR + | tGVAR + | tCVAR + ; + +user_variable : ident_or_const + | nonlocal_var + ; + +keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);} + | keyword_self {$$ = KWD2EID(self, $1);} + | keyword_true {$$ = KWD2EID(true, $1);} + | keyword_false {$$ = KWD2EID(false, $1);} + | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);} + | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);} + | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);} + ; var_ref : user_variable - { - /*%%%*/ - if (!($$ = gettable($1))) $$ = NEW_BEGIN(0); - /*% - if (id_is_var(get_id($1))) { - $$ = dispatch1(var_ref, $1); - } - else { - $$ = dispatch1(vcall, $1); - } - %*/ - } - | keyword_variable - { - /*%%%*/ - if (!($$ = gettable($1))) $$ = NEW_BEGIN(0); - /*% - $$ = dispatch1(var_ref, $1); - %*/ - } - ; - -var_lhs : user_variable - { - $$ = assignable($1, 0); - /*%%%*/ - /*% - $$ = dispatch1(var_field, $$); - %*/ - } - | keyword_variable - { - $$ = assignable($1, 0); - /*%%%*/ - /*% - $$ = dispatch1(var_field, $$); - %*/ - } - ; + { + if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$); + if (ifdef_ripper(id_is_var(p, $1), false)) { + /*% ripper: var_ref!($:1) %*/ + } + else { + /*% ripper: vcall!($:1) %*/ + } + } + | keyword_variable + { + if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$); + /*% ripper: var_ref!($:1) %*/ + } + ; + +var_lhs : user_or_keyword_variable + { + /*% ripper: var_field!($:1) %*/ + $$ = assignable(p, $1, 0, &@$); + } + ; backref : tNTH_REF - | tBACK_REF - ; + | tBACK_REF + ; superclass : '<' - { - lex_state = EXPR_BEG; - command_start = TRUE; - } - expr_value term - { - $$ = $3; - } - | /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = Qnil; - %*/ - } - ; - -f_arglist : '(' f_args rparen - { - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(paren, $2); - %*/ - lex_state = EXPR_BEG; - command_start = TRUE; - } - | { - $<num>$ = parser->in_kwarg; - parser->in_kwarg = 1; - lex_state |= EXPR_LABEL; /* force for args */ - } - f_args term - { - parser->in_kwarg = !!$<num>1; - $$ = $2; - lex_state = EXPR_BEG; - command_start = TRUE; - } - ; - -args_tail : f_kwarg ',' f_kwrest opt_f_block_arg - { - $$ = new_args_tail($1, $3, $4); - } - | f_kwarg opt_f_block_arg - { - $$ = new_args_tail($1, Qnone, $2); - } - | f_kwrest opt_f_block_arg - { - $$ = new_args_tail(Qnone, $1, $2); - } - | f_block_arg - { - $$ = new_args_tail(Qnone, Qnone, $1); - } - ; - -opt_args_tail : ',' args_tail - { - $$ = $2; - } - | /* none */ - { - $$ = new_args_tail(Qnone, Qnone, Qnone); - } - ; - -f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail - { - $$ = new_args($1, $3, $5, Qnone, $6); - } - | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail - { - $$ = new_args($1, $3, $5, $7, $8); - } - | f_arg ',' f_optarg opt_args_tail - { - $$ = new_args($1, $3, Qnone, Qnone, $4); - } - | f_arg ',' f_optarg ',' f_arg opt_args_tail - { - $$ = new_args($1, $3, Qnone, $5, $6); - } - | f_arg ',' f_rest_arg opt_args_tail - { - $$ = new_args($1, Qnone, $3, Qnone, $4); - } - | f_arg ',' f_rest_arg ',' f_arg opt_args_tail - { - $$ = new_args($1, Qnone, $3, $5, $6); - } - | f_arg opt_args_tail - { - $$ = new_args($1, Qnone, Qnone, Qnone, $2); - } - | f_optarg ',' f_rest_arg opt_args_tail - { - $$ = new_args(Qnone, $1, $3, Qnone, $4); - } - | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail - { - $$ = new_args(Qnone, $1, $3, $5, $6); - } - | f_optarg opt_args_tail - { - $$ = new_args(Qnone, $1, Qnone, Qnone, $2); - } - | f_optarg ',' f_arg opt_args_tail - { - $$ = new_args(Qnone, $1, Qnone, $3, $4); - } - | f_rest_arg opt_args_tail - { - $$ = new_args(Qnone, Qnone, $1, Qnone, $2); - } - | f_rest_arg ',' f_arg opt_args_tail - { - $$ = new_args(Qnone, Qnone, $1, $3, $4); - } - | args_tail - { - $$ = new_args(Qnone, Qnone, Qnone, Qnone, $1); - } - | /* none */ - { - $$ = new_args_tail(Qnone, Qnone, Qnone); - $$ = new_args(Qnone, Qnone, Qnone, Qnone, $$); - } - ; + { + SET_LEX_STATE(EXPR_BEG); + p->command_start = TRUE; + } + expr_value term + { + $$ = $3; + /*% ripper: $:3 %*/ + } + | none + ; + +f_opt_paren_args: f_paren_args + | none + { + p->ctxt.in_argdef = 0; + $$ = new_args_tail(p, 0, 0, 0, &@0); + $$ = new_args(p, 0, 0, 0, 0, $$, &@0); + /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/ + } + ; + +f_paren_args : '(' f_args rparen + { + $$ = $2; + /*% ripper: paren!($:2) %*/ + SET_LEX_STATE(EXPR_BEG); + p->command_start = TRUE; + p->ctxt.in_argdef = 0; + } + ; + +f_arglist : f_paren_args + | { + $$ = 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 = $1.in_kwarg; + p->ctxt.in_argdef = 0; + $$ = $2; + SET_LEX_STATE(EXPR_BEG); + p->command_start = TRUE; + /*% ripper: $:2 %*/ + } + ; + +args_tail : args_tail_basic(arg_value) + | args_forward + { + ID fwd = $args_forward; + if (lambda_beginning_p() || + (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) { + yyerror0("unexpected ... in lambda argument"); + fwd = 0; + } + else { + add_forwarding_args(p); + } + $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1); + $$->nd_ainfo.forwarding = 1; + /*% ripper: [Qnil, $:1, Qnil] %*/ + } + ; + +f_args : f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail) + { + $$ = new_args(p, $1, $3, $5, 0, $6, &@$); + /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/ + } + | f_arg ',' f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail) + { + $$ = new_args(p, $1, $3, $5, $7, $8, &@$); + /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/ + } + | f_arg ',' f_opt_arg(arg_value) opt_args_tail(args_tail) + { + $$ = new_args(p, $1, $3, 0, 0, $4, &@$); + /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/ + } + | f_arg ',' f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail) + { + $$ = new_args(p, $1, $3, 0, $5, $6, &@$); + /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/ + } + | f_arg ',' f_rest_arg opt_args_tail(args_tail) + { + $$ = new_args(p, $1, 0, $3, 0, $4, &@$); + /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/ + } + | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail) + { + $$ = new_args(p, $1, 0, $3, $5, $6, &@$); + /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/ + } + | f_arg opt_args_tail(args_tail) + { + $$ = new_args(p, $1, 0, 0, 0, $2, &@$); + /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/ + } + | f_opt_arg(arg_value) ',' f_rest_arg opt_args_tail(args_tail) + { + $$ = new_args(p, 0, $1, $3, 0, $4, &@$); + /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/ + } + | f_opt_arg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail) + { + $$ = new_args(p, 0, $1, $3, $5, $6, &@$); + /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/ + } + | f_opt_arg(arg_value) opt_args_tail(args_tail) + { + $$ = new_args(p, 0, $1, 0, 0, $2, &@$); + /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/ + } + | f_opt_arg(arg_value) ',' f_arg opt_args_tail(args_tail) + { + $$ = new_args(p, 0, $1, 0, $3, $4, &@$); + /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/ + } + | f_rest_arg opt_args_tail(args_tail) + { + $$ = new_args(p, 0, 0, $1, 0, $2, &@$); + /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/ + } + | f_rest_arg ',' f_arg opt_args_tail(args_tail) + { + $$ = new_args(p, 0, 0, $1, $3, $4, &@$); + /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/ + } + | args_tail + { + $$ = new_args(p, 0, 0, 0, 0, $1, &@$); + /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/ + } + | /* none */ + { + $$ = new_args_tail(p, 0, 0, 0, &@0); + $$ = new_args(p, 0, 0, 0, 0, $$, &@0); + /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/ + } + ; + +args_forward : tBDOT3 + { + $$ = idFWD_KWREST; + /*% ripper: args_forward! %*/ + } + ; f_bad_arg : tCONSTANT - { - /*%%%*/ - yyerror("formal argument cannot be a constant"); - $$ = 0; - /*% - $$ = dispatch1(param_error, $1); - ripper_error(); - %*/ - } - | tIVAR - { - /*%%%*/ - yyerror("formal argument cannot be an instance variable"); - $$ = 0; - /*% - $$ = dispatch1(param_error, $1); - ripper_error(); - %*/ - } - | tGVAR - { - /*%%%*/ - yyerror("formal argument cannot be a global variable"); - $$ = 0; - /*% - $$ = dispatch1(param_error, $1); - ripper_error(); - %*/ - } - | tCVAR - { - /*%%%*/ - yyerror("formal argument cannot be a class variable"); - $$ = 0; - /*% - $$ = dispatch1(param_error, $1); - ripper_error(); - %*/ - } - ; + { + static const char mesg[] = "formal argument cannot be a constant"; + /*%%%*/ + yyerror1(&@1, mesg); + /*% %*/ + $$ = 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) %*/ + } + | tGVAR + { + static const char mesg[] = "formal argument cannot be a global variable"; + /*%%%*/ + yyerror1(&@1, mesg); + /*% %*/ + $$ = 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) %*/ + } + ; f_norm_arg : f_bad_arg - | tIDENTIFIER - { - formal_argument(get_id($1)); - $$ = $1; - } - ; + | tIDENTIFIER + { + VALUE e = formal_argument_error(p, $$ = $1); + if (e) { + /*% ripper[error]: param_error!(?e, $:1) %*/ + } + p->max_numparam = ORDINAL_PARAM; + } + ; f_arg_asgn : f_norm_arg - { - ID id = get_id($1); - arg_var(id); - current_arg = id; - $$ = $1; - } - ; + { + arg_var(p, $1); + $$ = $1; + } + ; f_arg_item : f_arg_asgn - { - current_arg = 0; - /*%%%*/ - $$ = NEW_ARGS_AUX($1, 1); - /*% - $$ = get_value($1); - %*/ - } - | tLPAREN f_margs rparen - { - ID tid = internal_id(); - arg_var(tid); - /*%%%*/ - if (dyna_in_block()) { - $2->nd_value = NEW_DVAR(tid); - } - else { - $2->nd_value = NEW_LVAR(tid); - } - $$ = NEW_ARGS_AUX(tid, 1); - $$->nd_next = $2; - /*% - $$ = dispatch1(mlhs_paren, $2); - %*/ - } - ; + { + $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC); + /*% ripper: $:1 %*/ + } + | tLPAREN f_margs rparen + { + ID tid = internal_id(p); + YYLTYPE loc; + loc.beg_pos = @2.beg_pos; + loc.end_pos = @2.beg_pos; + arg_var(p, tid); + if (dyna_in_block(p)) { + $2->nd_value = NEW_DVAR(tid, &loc); + } + else { + $2->nd_value = NEW_LVAR(tid, &loc); + } + $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC); + $$->nd_next = (NODE *)$2; + /*% ripper: mlhs_paren!($:2) %*/ + } + ; f_arg : f_arg_item - /*%c%*/ - /*%c - { - $$ = rb_ary_new3(1, $1); - } - c%*/ - | f_arg ',' f_arg_item - { - /*%%%*/ - $$ = $1; - $$->nd_plen++; - $$->nd_next = block_append($$->nd_next, $3->nd_next); - rb_gc_force_recycle((VALUE)$3); - /*% - $$ = rb_ary_push($1, $3); - %*/ - } - ; + /*% 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, $:3) %*/ + } + ; f_label : tLABEL - { - ID id = get_id($1); - arg_var(formal_argument(id)); - current_arg = id; - $$ = $1; - } - ; - -f_kw : f_label arg_value - { - current_arg = 0; - $$ = assignable($1, $2); - /*%%%*/ - $$ = new_kw_arg($$); - /*% - $$ = rb_assoc_new($$, $2); - %*/ - } - | f_label - { - current_arg = 0; - $$ = assignable($1, (NODE *)-1); - /*%%%*/ - $$ = new_kw_arg($$); - /*% - $$ = rb_assoc_new($$, 0); - %*/ - } - ; - -f_block_kw : f_label primary_value - { - $$ = assignable($1, $2); - /*%%%*/ - $$ = new_kw_arg($$); - /*% - $$ = rb_assoc_new($$, $2); - %*/ - } - | f_label - { - $$ = assignable($1, (NODE *)-1); - /*%%%*/ - $$ = new_kw_arg($$); - /*% - $$ = rb_assoc_new($$, 0); - %*/ - } - ; - -f_block_kwarg : f_block_kw - { - /*%%%*/ - $$ = $1; - /*% - $$ = rb_ary_new3(1, $1); - %*/ - } - | f_block_kwarg ',' f_block_kw - { - /*%%%*/ - NODE *kws = $1; - - while (kws->nd_next) { - kws = kws->nd_next; - } - kws->nd_next = $3; - $$ = $1; - /*% - $$ = rb_ary_push($1, $3); - %*/ - } - ; - - -f_kwarg : f_kw - { - /*%%%*/ - $$ = $1; - /*% - $$ = rb_ary_new3(1, $1); - %*/ - } - | f_kwarg ',' f_kw - { - /*%%%*/ - NODE *kws = $1; - - while (kws->nd_next) { - kws = kws->nd_next; - } - kws->nd_next = $3; - $$ = $1; - /*% - $$ = rb_ary_push($1, $3); - %*/ - } - ; + { + 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; + } + ; kwrest_mark : tPOW - | tDSTAR - ; + | tDSTAR + ; + +f_no_kwarg : p_kwnorest + { + /*% ripper: nokw_param!(Qnil) %*/ + } + ; f_kwrest : kwrest_mark tIDENTIFIER - { - shadowing_lvar(get_id($2)); - $$ = $2; - } - | kwrest_mark - { - $$ = internal_id(); - arg_var($$); - } - ; - -f_opt : f_arg_asgn '=' arg_value - { - current_arg = 0; - $$ = assignable($1, $3); - /*%%%*/ - $$ = NEW_OPT_ARG(0, $$); - /*% - $$ = rb_assoc_new($$, $3); - %*/ - } - ; - -f_block_opt : f_arg_asgn '=' primary_value - { - current_arg = 0; - $$ = assignable($1, $3); - /*%%%*/ - $$ = NEW_OPT_ARG(0, $$); - /*% - $$ = rb_assoc_new($$, $3); - %*/ - } - ; - -f_block_optarg : f_block_opt - { - /*%%%*/ - $$ = $1; - /*% - $$ = rb_ary_new3(1, $1); - %*/ - } - | f_block_optarg ',' f_block_opt - { - /*%%%*/ - NODE *opts = $1; - - while (opts->nd_next) { - opts = opts->nd_next; - } - opts->nd_next = $3; - $$ = $1; - /*% - $$ = rb_ary_push($1, $3); - %*/ - } - ; - -f_optarg : f_opt - { - /*%%%*/ - $$ = $1; - /*% - $$ = rb_ary_new3(1, $1); - %*/ - } - | f_optarg ',' f_opt - { - /*%%%*/ - NODE *opts = $1; - - while (opts->nd_next) { - opts = opts->nd_next; - } - opts->nd_next = $3; - $$ = $1; - /*% - $$ = rb_ary_push($1, $3); - %*/ - } - ; + { + arg_var(p, shadowing_lvar(p, $2)); + $$ = $2; + /*% ripper: kwrest_param!($:2) %*/ + } + | kwrest_mark + { + arg_var(p, idFWD_KWREST); + $$ = idFWD_KWREST; + /*% ripper: kwrest_param!(Qnil) %*/ + } + ; restarg_mark : '*' - | tSTAR - ; + | tSTAR + ; f_rest_arg : restarg_mark tIDENTIFIER - { - /*%%%*/ - if (!is_local_id($2)) - yyerror("rest argument must be local variable"); - /*% %*/ - arg_var(shadowing_lvar(get_id($2))); - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(rest_param, $2); - %*/ - } - | restarg_mark - { - /*%%%*/ - $$ = internal_id(); - arg_var($$); - /*% - $$ = dispatch1(rest_param, Qnil); - %*/ - } - ; + { + arg_var(p, shadowing_lvar(p, $2)); + $$ = $2; + /*% ripper: rest_param!($:2) %*/ + } + | restarg_mark + { + arg_var(p, idFWD_REST); + $$ = idFWD_REST; + /*% ripper: rest_param!(Qnil) %*/ + } + ; blkarg_mark : '&' - | tAMPER - ; + | tAMPER + ; f_block_arg : blkarg_mark tIDENTIFIER - { - /*%%%*/ - if (!is_local_id($2)) - yyerror("block argument must be local variable"); - else if (!dyna_in_block() && local_id($2)) - yyerror("duplicated block argument name"); - /*% %*/ - arg_var(shadowing_lvar(get_id($2))); - /*%%%*/ - $$ = $2; - /*% - $$ = dispatch1(blockarg, $2); - %*/ - } - ; + { + arg_var(p, shadowing_lvar(p, $2)); + $$ = $2; + /*% ripper: blockarg!($:2) %*/ + } + | blkarg_mark + { + arg_var(p, idFWD_BLOCK); + $$ = idFWD_BLOCK; + /*% ripper: blockarg!(Qnil) %*/ + } + ; opt_f_block_arg : ',' f_block_arg - { - $$ = $2; - } - | none - { - /*%%%*/ - $$ = 0; - /*% - $$ = Qundef; - %*/ - } - ; - -singleton : var_ref - { - /*%%%*/ - value_expr($1); - $$ = $1; - if (!$$) $$ = NEW_NIL(); - /*% - $$ = $1; - %*/ - } - | '(' {lex_state = EXPR_BEG;} expr rparen - { - /*%%%*/ - if ($3 == 0) { - yyerror("can't define singleton method for ()."); - } - else { - switch (nd_type($3)) { - case NODE_STR: - case NODE_DSTR: - case NODE_XSTR: - case NODE_DXSTR: - case NODE_DREGX: - case NODE_LIT: - case NODE_ARRAY: - case NODE_ZARRAY: - yyerror("can't define singleton method for literals"); - default: - value_expr($3); - break; - } - } - $$ = $3; - /*% - $$ = dispatch1(paren, $3); - %*/ - } - ; + { + $$ = $2; + /*% ripper: $:2 %*/ + } + | none + ; + + +singleton : value_expr(singleton_expr) + { + 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_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: + 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) %*/ + } + ; assoc_list : none - | assocs trailer - { - /*%%%*/ - $$ = $1; - /*% - $$ = dispatch1(assoclist_from_args, $1); - %*/ - } - ; + | assocs trailer + { + $$ = $1; + /*% ripper: assoclist_from_args!($:1) %*/ + } + ; assocs : assoc - /*%c%*/ - /*%c - { - $$ = rb_ary_new3(1, $1); - } - %*/ - | assocs ',' assoc - { - /*%%%*/ - NODE *assocs = $1; - NODE *tail = $3; - if (!assocs) { - assocs = tail; - } - else if (tail) { - if (assocs->nd_head && - !tail->nd_head && nd_type(tail->nd_next) == NODE_ARRAY && - nd_type(tail->nd_next->nd_head) == NODE_HASH) { - /* DSTAR */ - tail = tail->nd_next->nd_head->nd_head; - } - assocs = list_concat(assocs, tail); - } - $$ = assocs; - /*% - $$ = rb_ary_push($1, $3); - %*/ - } - ; + /*% 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) { + 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; + /*% ripper: rb_ary_push($:1, $:3) %*/ + } + ; assoc : arg_value tASSOC arg_value - { - /*%%%*/ - if (nd_type($1) == NODE_STR) { - nd_set_type($1, NODE_LIT); - $1->nd_lit = rb_fstring($1->nd_lit); - } - $$ = list_append(NEW_LIST($1), $3); - /*% - $$ = dispatch2(assoc_new, $1, $3); - %*/ - } - | tLABEL arg_value - { - /*%%%*/ - $$ = list_append(NEW_LIST(NEW_LIT(ID2SYM($1))), $2); - /*% - $$ = dispatch2(assoc_new, $1, $2); - %*/ - } - | tSTRING_BEG string_contents tLABEL_END arg_value - { - /*%%%*/ - $$ = list_append(NEW_LIST(dsym_node($2)), $4); - /*% - $$ = dispatch2(assoc_new, dispatch1(dyna_symbol, $2), $4); - %*/ - } - | tDSTAR arg_value - { - /*%%%*/ - if (nd_type($2) == NODE_HASH && - !($2->nd_head && $2->nd_head->nd_alen)) - $$ = 0; - else - $$ = list_append(NEW_LIST(0), $2); - /*% - $$ = dispatch1(assoc_splat, $2); - %*/ - } - ; - -operation : tIDENTIFIER - | tCONSTANT - | tFID - ; - -operation2 : tIDENTIFIER - | tCONSTANT - | tFID - | op - ; + { + $$ = list_append(p, NEW_LIST($1, &@$), $3); + /*% ripper: assoc_new!($:1, $:3) %*/ + } + | tLABEL arg_value + { + $$ = 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_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) %*/ + } + | tDSTAR arg_value + { + $$ = list_append(p, NEW_LIST(0, &@$), $2); + /*% ripper: assoc_splat!($:2) %*/ + } + | tDSTAR + { + forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest"); + $$ = list_append(p, NEW_LIST(0, &@$), + NEW_LVAR(idFWD_KWREST, &@$)); + /*% ripper: assoc_splat!(Qnil) %*/ + } + ; + +%rule %inline operation : ident_or_const + | tFID + ; + +operation2 : operation + | op + ; operation3 : tIDENTIFIER - | tFID - | op - ; + | tFID + | op + ; dot_or_colon : '.' - /*%c%*/ - /*%c - { $$ = $<val>1; } - %*/ - | tCOLON2 - /*%c%*/ - /*%c - { $$ = $<val>1; } - %*/ - ; - -call_op : '.' {$$ = '.';} - | tDOTQ {$$ = tDOTQ;} - ; + | tCOLON2 + ; -call_op2 : call_op - | tCOLON2 {$$ = tCOLON2;} - ; +call_op : '.' + | tANDDOT + ; -opt_terms : /* none */ - | terms - ; +call_op2 : call_op + | tCOLON2 + ; -opt_nl : /* none */ - | '\n' - ; +rparen : '\n'? ')' + ; -rparen : opt_nl ')' - ; +rbracket : '\n'? ']' + ; -rbracket : opt_nl ']' - ; +rbrace : '\n'? '}' + ; -trailer : /* none */ - | '\n' - | ',' - ; +trailer : '\n'? + | ',' + ; -term : ';' {yyerrok;} - | '\n' - ; +term : ';' + { + yyerrok; + token_flush(p); + if (p->ctxt.in_defined) { + p->ctxt.has_trailing_semicolon = 1; + } + } + | '\n' + { + @$.end_pos = @$.beg_pos; + token_flush(p); + } + ; terms : term - | terms ';' {yyerrok;} - ; + | terms ';' {yyerrok;} + ; none : /* none */ - { - /*%%%*/ - $$ = 0; - /*% - $$ = Qundef; - %*/ - } - ; + { + $$ = 0; + /*% ripper: Qnil %*/ + } + ; %% -# undef parser +# undef p # undef yylex # undef yylval -# define yylval (*parser->lval) - -static int parser_regx_options(struct parser_params*); -static int parser_tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**); -static void parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc); -static int parser_parse_string(struct parser_params*,NODE*); -static int parser_here_document(struct parser_params*,NODE*); - - -# define nextc() parser_nextc(parser) -# define pushback(c) parser_pushback(parser, (c)) -# define newtok() parser_newtok(parser) -# define tokspace(n) parser_tokspace(parser, (n)) -# define tokadd(c) parser_tokadd(parser, (c)) -# define tok_hex(numlen) parser_tok_hex(parser, (numlen)) -# define read_escape(flags,e) parser_read_escape(parser, (flags), (e)) -# define tokadd_escape(e) parser_tokadd_escape(parser, (e)) -# define regx_options() parser_regx_options(parser) -# define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,(f),(t),(p),(n),(e)) -# define parse_string(n) parser_parse_string(parser,(n)) -# define tokaddmbc(c, enc) parser_tokaddmbc(parser, (c), (enc)) -# define here_document(n) parser_here_document(parser,(n)) -# define heredoc_identifier() parser_heredoc_identifier(parser) -# define heredoc_restore(n) parser_heredoc_restore(parser,(n)) -# define whole_match_p(e,l,i) parser_whole_match_p(parser,(e),(l),(i)) -# define number_literal_suffix(f) parser_number_literal_suffix(parser, (f)) -# define set_number_literal(v, t, f) parser_set_number_literal(parser, (v), (t), (f)) -# define set_integer_literal(v, f) parser_set_integer_literal(parser, (v), (f)) - -#ifndef RIPPER -# define set_yylval_str(x) (yylval.node = NEW_STR(x)) -# 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_literal(x) (yylval.node = NEW_LIT(x)) -# define set_yylval_node(x) (yylval.node = (x)) +# define yylval (*p->lval) + +static int regx_options(struct parser_params*); +static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**); +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*); + +#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)); \ + set_parser_s_value(rb_str_new_mutable_parser_string(x)); \ +} while(0) +# 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(ID x) -{ - return ripper_new_yylval(x, ID2SYM(x), 0); -} -# define set_yylval_str(x) (yylval.val = (x)) -# define set_yylval_num(x) (yylval.val = ripper_new_yylval((x), 0, 0)) -# define set_yylval_id(x) (void)(x) -# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x)) -# define set_yylval_literal(x) (void)(x) -# define set_yylval_node(x) (void)(x) -# define yylval_id() yylval.id -#endif -#ifndef RIPPER -#define ripper_flush(p) (void)(p) -#else -#define ripper_flush(p) ((p)->tokp = (p)->lex.pcur) +#define set_yylval_noname() set_yylval_id(keyword_nil) +#define has_delayed_token(p) (p->delayed.token != NULL) -#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val)) +#ifndef RIPPER +#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr)) +#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__) -static inline VALUE -intern_sym(const char *name) +static bool +parser_has_token(struct parser_params *p) { - ID id = rb_intern_const(name); - return ID2SYM(id); + const char *const pcur = p->lex.pcur; + const char *const ptok = p->lex.ptok; + if (p->keep_tokens && (pcur < ptok)) { + rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"", + p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); + } + return pcur > ptok; } -static int -ripper_has_scan_event(struct parser_params *parser) +static const char * +escaped_char(int c) { + 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 rb_parser_string_t * +rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str) +{ + 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); - if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp"); - return lex_p > parser->tokp; + return result; } -static VALUE -ripper_scan_event_val(struct parser_params *parser, int t) +static void +parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line) { - VALUE str = STR_NEW(parser->tokp, lex_p - parser->tokp); - VALUE rval = ripper_dispatch1(parser, ripper_token2eventid(t), str); - ripper_flush(parser); - return rval; + 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_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); + } } static void -ripper_dispatch_scan_event(struct parser_params *parser, int t) +parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line) { - if (!ripper_has_scan_event(parser)) return; - yylval_rval = ripper_scan_event_val(parser, t); + debug_token_line(p, "parser_dispatch_scan_event", line); + + if (!parser_has_token(p)) return; + + RUBY_SET_YYLLOC(*p->yylloc); + + if (p->keep_tokens) { + 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); + } + + token_flush(p); } +#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__) static void -ripper_dispatch_ignored_scan_event(struct parser_params *parser, int t) +parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line) { - if (!ripper_has_scan_event(parser)) return; - (void)ripper_scan_event_val(parser, t); + debug_token_line(p, "parser_dispatch_delayed_token", line); + + if (!has_delayed_token(p)) return; + + 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 = NULL; +} +#else +#define literal_flush(p, ptr) ((void)(ptr)) + +static int +ripper_has_scan_event(struct parser_params *p) +{ + if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok"); + return p->lex.pcur > p->lex.ptok; +} + +static VALUE +ripper_scan_event_val(struct parser_params *p, enum yytokentype t) +{ + VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok); + VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str); + RUBY_SET_YYLLOC(*p->yylloc); + token_flush(p); + return rval; } static void -ripper_dispatch_delayed_token(struct parser_params *parser, int t) +ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t) { - int saved_line = ruby_sourceline; - const char *saved_tokp = parser->tokp; + if (!ripper_has_scan_event(p)) return; - ruby_sourceline = parser->delayed_line; - parser->tokp = lex_pbeg + parser->delayed_col; - yylval_rval = ripper_dispatch1(parser, ripper_token2eventid(t), parser->delayed); - parser->delayed = Qnil; - ruby_sourceline = saved_line; - parser->tokp = saved_tokp; + set_parser_s_value(ripper_scan_event_val(p, t)); } -#endif /* RIPPER */ +#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t) -#include "ruby/regex.h" -#include "ruby/util.h" +static void +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; + 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; +} +#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t) +#endif /* RIPPER */ -/* We remove any previous definition of `SIGN_EXTEND_CHAR', - since ours (we hope) works properly with all combinations of - machines, compilers, `char' and `unsigned char' argument types. - (Per Bothner suggested the basic approach.) */ -#undef SIGN_EXTEND_CHAR -#if __STDC__ -# define SIGN_EXTEND_CHAR(c) ((signed char)(c)) -#else /* not __STDC__ */ -/* As in Harbison and Steele. */ -# define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128) -#endif +static inline int +is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc) +{ + return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr); +} -#define parser_encoding_name() (current_enc->name) -#define parser_mbclen() mbclen((lex_p-1),lex_pend,current_enc) -#define parser_precise_mbclen() rb_enc_precise_mbclen((lex_p-1),lex_pend,current_enc) -#define is_identchar(p,e,enc) (rb_enc_isalnum((unsigned char)(*(p)),(enc)) || (*(p)) == '_' || !ISASCII(*(p))) -#define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,current_enc)) +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; + return !is_identchar(p, ptr+len, p->lex.pend, p->enc); +} -#define parser_isascii() ISASCII(*(lex_p-1)) +static inline int +parser_is_identchar(struct parser_params *p) +{ + return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc); +} -#ifndef RIPPER -static int -token_info_get_column(struct parser_params *parser, const char *pend) +static inline int +parser_isascii(struct parser_params *p) { - int column = 1; - const char *p; - for (p = lex_pbeg; p < pend; p++) { - if (*p == '\t') { - column = (((column - 1) / 8) + 1) * 8; - } - column++; - } - return column; + return ISASCII(*(p->lex.pcur-1)); } -static int -token_info_has_nonspaces(struct parser_params *parser, const char *pend) +static void +token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc) { - const char *p; - for (p = lex_pbeg; p < pend; p++) { - if (*p != ' ' && *p != '\t') { - return 1; - } + int column = 1, nonspc = 0, i; + for (i = 0; i < loc->beg_pos.column; i++, ptr++) { + if (*ptr == '\t') { + column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH; + } + column++; + if (*ptr != ' ' && *ptr != '\t') { + nonspc = 1; + } } - return 0; + + ptinfo->beg = loc->beg_pos; + ptinfo->indent = column; + ptinfo->nonspc = nonspc; } -#undef token_info_push static void -token_info_push(struct parser_params *parser, const char *token, size_t len) +token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc) { token_info *ptinfo; - const char *t = lex_p - len; - if (!parser->token_info_enabled) return; + if (!p->token_info_enabled) return; ptinfo = ALLOC(token_info); ptinfo->token = token; - ptinfo->linenum = ruby_sourceline; - ptinfo->column = token_info_get_column(parser, t); - ptinfo->nonspc = token_info_has_nonspaces(parser, t); - ptinfo->next = parser->token_info; + ptinfo->next = p->token_info; + token_info_setup(ptinfo, p->lex.pbeg, loc); - parser->token_info = ptinfo; + p->token_info = ptinfo; } -#undef token_info_pop static void -token_info_pop(struct parser_params *parser, const char *token, size_t len) +token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc) { - int linenum; - token_info *ptinfo = parser->token_info; - const char *t = lex_p - len; + token_info *ptinfo_beg = p->token_info; - if (!ptinfo) return; - parser->token_info = ptinfo->next; - linenum = ruby_sourceline; - if (parser->token_info_enabled && - linenum != ptinfo->linenum && !ptinfo->nonspc && - !token_info_has_nonspaces(parser, t) && - token_info_get_column(parser, t) != ptinfo->column) { - rb_compile_warn(ruby_sourcefile, linenum, - "mismatched indentations at '%s' with '%s' at %d", - token, ptinfo->token, ptinfo->linenum); - } + if (!ptinfo_beg) return; + + /* indentation check of matched keywords (begin..end, if..end, etc.) */ + token_info_warn(p, token, ptinfo_beg, 1, loc); - xfree(ptinfo); + p->token_info = ptinfo_beg->next; + ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg)); } -#endif /* RIPPER */ -static int -parser_yyerror(struct parser_params *parser, const char *msg) +static void +token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos) { -#ifndef RIPPER - const int max_line_margin = 30; - const char *p, *pe; - char *buf; - long len; - int i; + token_info *ptinfo_beg = p->token_info; - compile_error(PARSER_ARG "%s", msg); - p = lex_p; - while (lex_pbeg <= p) { - if (*p == '\n') break; - p--; - } - p++; + if (!ptinfo_beg) return; + p->token_info = ptinfo_beg->next; - pe = lex_p; - while (pe < lex_pend) { - if (*pe == '\n') break; - pe++; + if (ptinfo_beg->beg.lineno != beg_pos.lineno || + ptinfo_beg->beg.column != beg_pos.column || + strcmp(ptinfo_beg->token, token)) { + compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s", + beg_pos.lineno, beg_pos.column, token, + ptinfo_beg->beg.lineno, ptinfo_beg->beg.column, + ptinfo_beg->token); } - len = pe - p; - if (len > 4) { - char *p2; - const char *pre = "", *post = ""; - - if (len > max_line_margin * 2 + 10) { - if (lex_p - p > max_line_margin) { - p = rb_enc_prev_char(p, lex_p - max_line_margin, pe, rb_enc_get(lex_lastline)); - pre = "..."; - } - if (pe - lex_p > max_line_margin) { - pe = rb_enc_prev_char(lex_p, lex_p + max_line_margin, pe, rb_enc_get(lex_lastline)); - post = "..."; - } - len = pe - p; - } - buf = ALLOCA_N(char, len+2); - MEMCPY(buf, p, char, len); - buf[len] = '\0'; - rb_compile_error_with_enc(NULL, 0, (void *)current_enc, "%s%s%s", pre, buf, post); - - i = (int)(lex_p - p); - p2 = buf; pe = buf + len; - - while (p2 < pe) { - if (*p2 != '\t') *p2 = ' '; - p2++; - } - buf[i] = '^'; - buf[i+1] = '\0'; - rb_compile_error_append("%s%s", pre, buf); - } -#else - dispatch1(parse_error, STR_NEW2(msg)); - ripper_error(); -#endif /* !RIPPER */ - return 0; + ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg)); } -static void parser_prepare(struct parser_params *parser); +static void +token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc) +{ + token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body; + if (!p->token_info_enabled) return; + if (!ptinfo_beg) return; + token_info_setup(ptinfo_end, p->lex.pbeg, loc); + if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */ + if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */ + if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */ + if (!same && ptinfo_beg->indent < ptinfo_end->indent) return; + rb_warn3L(ptinfo_end->beg.lineno, + "mismatched indentations at '%s' with '%s' at %d", + WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno)); +} + +static int +parser_precise_mbclen(struct parser_params *p, const char *ptr) +{ + int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc); + if (!MBCLEN_CHARFOUND_P(len)) { + compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc)); + return -1; + } + return len; +} #ifndef RIPPER -static VALUE -debug_lines(VALUE fname) -{ - ID script_lines; - CONST_ID(script_lines, "SCRIPT_LINES__"); - if (rb_const_defined_at(rb_cObject, script_lines)) { - VALUE hash = rb_const_get_at(rb_cObject, script_lines); - if (RB_TYPE_P(hash, T_HASH)) { - VALUE lines = rb_ary_new(); - rb_hash_aset(hash, fname, lines); - return lines; - } +static inline void +parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc) +{ + rb_parser_string_t *str; + int lineno = p->ruby_sourceline; + if (!yylloc) { + return; } - return 0; + else if (yylloc->beg_pos.lineno == lineno) { + str = p->lex.lastline; + } + else { + return; + } + ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str); } -static VALUE -coverage(VALUE fname, int n) +static int +parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg) { - VALUE coverages = rb_get_coverages(); - if (RTEST(coverages) && RBASIC(coverages)->klass == 0) { - VALUE lines = rb_ary_tmp_new_fill(n); - rb_hash_aset(coverages, fname, lines); - return lines; +#if 0 + YYLTYPE current; + + if (!yylloc) { + yylloc = RUBY_SET_YYLLOC(current); + } + else if ((p->ruby_sourceline != yylloc->beg_pos.lineno && + p->ruby_sourceline != yylloc->end_pos.lineno)) { + yylloc = 0; } +#endif + parser_compile_error(p, yylloc, "%s", msg); + parser_show_error_line(p, yylloc); return 0; } static int -e_option_supplied(struct parser_params *parser) +parser_yyerror0(struct parser_params *p, const char *msg) { - return strcmp(ruby_sourcefile, "-e") == 0; + YYLTYPE current; + return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg); } -static VALUE -yycompile0(VALUE arg) +void +ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str) { - int n; - NODE *tree; - struct parser_params *parser = (struct parser_params *)arg; - - if (!compile_for_eval && rb_safe_level() == 0) { - ruby_debug_lines = debug_lines(ruby_sourcefile_string); - if (ruby_debug_lines && ruby_sourceline > 0) { - VALUE str = STR_NEW0(); - n = ruby_sourceline; - do { - rb_ary_push(ruby_debug_lines, str); - } while (--n); - } - - if (!e_option_supplied(parser)) { - ruby_coverage = coverage(ruby_sourcefile_string, ruby_sourceline); - } - } - parser->last_cr_line = ruby_sourceline - 1; - - parser_prepare(parser); - deferred_nodes = 0; -#ifndef RIPPER - parser->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose); -#endif -#ifndef RIPPER - if (RUBY_DTRACE_PARSE_BEGIN_ENABLED()) { - RUBY_DTRACE_PARSE_BEGIN(ruby_sourcefile, - ruby_sourceline); + VALUE mesg; + const int max_line_margin = 30; + const char *ptr, *ptr_end, *pt, *pb; + const char *pre = "", *post = "", *pend; + const char *code = "", *caret = ""; + const char *lim; + const char *const pbeg = PARSER_STRING_PTR(str); + char *buf; + long len; + int i; + + if (!yylloc) return; + pend = rb_parser_string_end(str); + if (pend > pbeg && pend[-1] == '\n') { + if (--pend > pbeg && pend[-1] == '\r') --pend; } -#endif - n = yyparse((void*)parser); -#ifndef RIPPER - if (RUBY_DTRACE_PARSE_END_ENABLED()) { - RUBY_DTRACE_PARSE_END(ruby_sourcefile, - ruby_sourceline); + + pt = pend; + if (lineno == yylloc->end_pos.lineno && + (pend - pbeg) > yylloc->end_pos.column) { + pt = pbeg + yylloc->end_pos.column; } -#endif - ruby_debug_lines = 0; - ruby_coverage = 0; - compile_for_eval = 0; - lex_strterm = 0; - lex_p = lex_pbeg = lex_pend = 0; - lex_lastline = lex_nextline = 0; - if (parser->has_err) { - return 0; + ptr = ptr_end = pt; + lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg; + while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--; + + lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend; + while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++; + + len = ptr_end - ptr; + if (len > 4) { + if (ptr > pbeg) { + 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_parser_str_get_encoding(str)); + if (ptr_end < pend) post = "..."; + } } - tree = ruby_eval_tree; - if (!tree) { - tree = NEW_NIL(); + pb = pbeg; + if (lineno == yylloc->beg_pos.lineno) { + pb += yylloc->beg_pos.column; + if (pb > pt) pb = pt; + } + if (pb < ptr) pb = ptr; + if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) { + return; + } + if (RTEST(errbuf)) { + mesg = rb_attr_get(errbuf, idMesg); + if (char_at_end(p, mesg, '\n') != '\n') + rb_str_cat_cstr(mesg, "\n"); } else { - tree->nd_body = NEW_PRELUDE(ruby_eval_tree_begin, tree->nd_body, parser->compile_option); + mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str)); + } + if (!errbuf && rb_stderr_tty_p()) { +#define CSI_BEGIN "\033[" +#define CSI_SGR "m" + rb_str_catf(mesg, + CSI_BEGIN""CSI_SGR"%s" /* pre */ + CSI_BEGIN"1"CSI_SGR"%.*s" + CSI_BEGIN"1;4"CSI_SGR"%.*s" + CSI_BEGIN";1"CSI_SGR"%.*s" + CSI_BEGIN""CSI_SGR"%s" /* post */ + "\n", + pre, + (int)(pb - ptr), ptr, + (int)(pt - pb), pb, + (int)(ptr_end - pt), pt, + post); + } + else { + char *p2; + + len = ptr_end - ptr; + lim = pt < pend ? pt : pend; + i = (int)(lim - ptr); + buf = ALLOCA_N(char, i+2); + code = ptr; + caret = p2 = buf; + if (ptr <= pb) { + while (ptr < pb) { + *p2++ = *ptr++ == '\t' ? '\t' : ' '; + } + *p2++ = '^'; + ptr++; + } + if (lim > ptr) { + memset(p2, '~', (lim - ptr)); + p2 += (lim - ptr); + } + *p2 = '\0'; + rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n", + pre, (int)len, code, post, + pre, caret); } - return (VALUE)tree; + if (!errbuf) rb_write_error_str(mesg); } +#else -static NODE* -yycompile(struct parser_params *parser, VALUE fname, int line) +static int +parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg) { - ruby_sourcefile_string = rb_str_new_frozen(fname); - ruby_sourcefile = RSTRING_PTR(fname); - ruby_sourceline = line - 1; - return (NODE *)rb_suppress_tracing(yycompile0, (VALUE)parser); + const char *pcur = 0, *ptok = 0; + if (p->ruby_sourceline == yylloc->beg_pos.lineno && + p->ruby_sourceline == yylloc->end_pos.lineno) { + pcur = p->lex.pcur; + ptok = p->lex.ptok; + p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column; + p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column; + } + parser_yyerror0(p, msg); + if (pcur) { + p->lex.ptok = ptok; + p->lex.pcur = pcur; + } + return 0; } -#endif /* !RIPPER */ -static rb_encoding * -must_be_ascii_compatible(VALUE s) +static int +parser_yyerror0(struct parser_params *p, const char *msg) { - rb_encoding *enc = rb_enc_get(s); - if (!rb_enc_asciicompat(enc)) { - rb_raise(rb_eArgError, "invalid source encoding"); - } - return enc; + dispatch1(parse_error, STR_NEW2(msg)); + ripper_error(p); + return 0; } -static VALUE -lex_get_str(struct parser_params *parser, VALUE s) +static inline void +parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc) { - char *beg, *end, *start; - long len; +} +#endif /* !RIPPER */ - beg = RSTRING_PTR(s); - len = RSTRING_LEN(s); - start = beg; - if (lex_gets_ptr) { - if (len == lex_gets_ptr) return Qnil; - beg += lex_gets_ptr; - len -= lex_gets_ptr; +static int +vtable_size(const struct vtable *tbl) +{ + if (!DVARS_TERMINAL_P(tbl)) { + return tbl->pos; + } + else { + return 0; } - end = memchr(beg, '\n', len); - if (end) len = ++end - beg; - lex_gets_ptr += len; - return rb_str_subseq(s, beg - start, len); } -static VALUE -lex_getline(struct parser_params *parser) +static struct vtable * +vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev) { - VALUE line = (*lex_gets)(parser, lex_input); - if (NIL_P(line)) return line; - must_be_ascii_compatible(line); + struct vtable *tbl = ALLOC(struct vtable); + tbl->pos = 0; + tbl->capa = 8; + tbl->tbl = ALLOC_N(ID, tbl->capa); + tbl->prev = prev; #ifndef RIPPER - if (ruby_debug_lines) { - rb_enc_associate(line, current_enc); - rb_ary_push(ruby_debug_lines, line); - } - if (ruby_coverage) { - rb_ary_push(ruby_coverage, Qnil); + if (p->debug) { + rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl); } #endif - return line; + return tbl; } +#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev) -static const rb_data_type_t parser_data_type; - -#ifndef RIPPER -static NODE* -parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line) +static void +vtable_free_gen(struct parser_params *p, int line, const char *name, + struct vtable *tbl) { - struct parser_params *parser; - NODE *node; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); - lex_gets = lex_get_str; - lex_gets_ptr = 0; - lex_input = rb_str_new_frozen(s); - lex_pbeg = lex_p = lex_pend = 0; - compile_for_eval = !!rb_parse_in_eval(); - - node = yycompile(parser, fname, line); - RB_GC_GUARD(vparser); /* prohibit tail call optimization */ - - return node; +#ifndef RIPPER + if (p->debug) { + rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl); + } +#endif + if (!DVARS_TERMINAL_P(tbl)) { + if (tbl->tbl) { + ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID)); + } + ruby_sized_xfree(tbl, sizeof(*tbl)); + } } +#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl) -NODE* -rb_compile_string(const char *f, VALUE s, int line) +static void +vtable_add_gen(struct parser_params *p, int line, const char *name, + struct vtable *tbl, ID id) { - must_be_ascii_compatible(s); - return parser_compile_string(rb_parser_new(), rb_filesystem_str_new_cstr(f), s, line); +#ifndef RIPPER + if (p->debug) { + rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n", + line, name, (void *)tbl, rb_id2name(id)); + } +#endif + if (DVARS_TERMINAL_P(tbl)) { + rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl); + return; + } + if (tbl->pos == tbl->capa) { + tbl->capa = tbl->capa * 2; + SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos); + } + tbl->tbl[tbl->pos++] = id; } +#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id) -NODE* -rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line) +static void +vtable_pop_gen(struct parser_params *p, int line, const char *name, + struct vtable *tbl, int n) { - return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line); + if (p->debug) { + rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n", + line, name, (void *)tbl, n); + } + if (tbl->pos < n) { + rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n); + return; + } + tbl->pos -= n; } +#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n) -NODE* -rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line) +static int +vtable_included(const struct vtable * tbl, ID id) { - must_be_ascii_compatible(s); - return parser_compile_string(vparser, f, s, line); -} + int i; -NODE* -rb_compile_cstr(const char *f, const char *s, int len, int line) -{ - VALUE str = rb_str_new(s, len); - return parser_compile_string(rb_parser_new(), rb_filesystem_str_new_cstr(f), str, line); + if (!DVARS_TERMINAL_P(tbl)) { + for (i = 0; i < tbl->pos; i++) { + if (tbl->tbl[i] == id) { + return i+1; + } + } + } + return 0; } -NODE* -rb_parser_compile_cstr(VALUE vparser, const char *f, const char *s, int len, int line) +static void parser_prepare(struct parser_params *p); + +static int +e_option_supplied(struct parser_params *p) { - VALUE str = rb_str_new(s, len); - return parser_compile_string(vparser, rb_filesystem_str_new_cstr(f), str, line); + return strcmp(p->ruby_sourcefile, "-e") == 0; } +#ifndef RIPPER +static NODE *parser_append_options(struct parser_params *p, NODE *node); + static VALUE -lex_io_gets(struct parser_params *parser, VALUE io) +yycompile0(VALUE arg) { - return rb_io_gets(io); + int n; + NODE *tree; + struct parser_params *p = (struct parser_params *)arg; + int cov = FALSE; + + if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) { + cov = TRUE; + } + + if (p->debug_lines) { + p->ast->body.script_lines = p->debug_lines; + } + + parser_prepare(p); +#define RUBY_DTRACE_PARSE_HOOK(name) \ + if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \ + RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \ + } + 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) { + VALUE mesg = p->error_buffer; + if (!mesg) { + mesg = syntax_error_new(); + } + if (!p->error_tolerant) { + rb_set_errinfo(mesg); + return FALSE; + } + } + tree = p->eval_tree; + if (!tree) { + tree = NEW_NIL(&NULL_LOC); + } + else { + 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); + RNODE_SCOPE(tree)->nd_body = prelude; + p->ast->body.frozen_string_literal = p->frozen_string_literal; + p->ast->body.coverage_enabled = cov; + if (p->keep_tokens) { + p->ast->node_buffer->tokens = tokens; + p->tokens = NULL; + } + } + p->ast->body.root = tree; + p->ast->body.line_count = p->line_count; + return TRUE; } -NODE* -rb_compile_file(const char *f, VALUE file, int start) +static rb_ast_t * +yycompile(struct parser_params *p, VALUE fname, int line) { - VALUE vparser = rb_parser_new(); + rb_ast_t *ast; + if (NIL_P(fname)) { + p->ruby_sourcefile_string = Qnil; + p->ruby_sourcefile = "(none)"; + } + else { + p->ruby_sourcefile_string = rb_str_to_interned_str(fname); + p->ruby_sourcefile = StringValueCStr(fname); + } + p->ruby_sourceline = line - 1; + + p->lvtbl = NULL; + + p->ast = ast = rb_ast_new(); + compile_callback(yycompile0, (VALUE)p); + p->ast = 0; + + while (p->lvtbl) { + local_pop(p); + } - return rb_parser_compile_file(vparser, f, file, start); + return ast; } +#endif /* !RIPPER */ -NODE* -rb_parser_compile_file(VALUE vparser, const char *f, VALUE file, int start) +static rb_encoding * +must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s) { - return rb_parser_compile_file_path(vparser, rb_filesystem_str_new_cstr(f), file, start); + rb_encoding *enc = rb_parser_str_get_encoding(s); + if (!rb_enc_asciicompat(enc)) { + rb_raise(rb_eArgError, "invalid source encoding"); + } + return enc; } -NODE* -rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start) +static rb_parser_string_t * +lex_getline(struct parser_params *p) { - struct parser_params *parser; - NODE *node; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); - lex_gets = lex_io_gets; - lex_input = file; - lex_pbeg = lex_p = lex_pend = 0; - compile_for_eval = !!rb_parse_in_eval(); + 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; +} - node = yycompile(parser, fname, start); - RB_GC_GUARD(vparser); /* prohibit tail call optimization */ +#ifndef RIPPER +rb_ast_t* +rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line) +{ + p->lex.gets = gets; + p->lex.input = input; + p->lex.pbeg = p->lex.pcur = p->lex.pend = 0; - return node; + return yycompile(p, fname, line); } #endif /* !RIPPER */ @@ -5692,6 +7542,8 @@ rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start) #define STR_FUNC_SYMBOL 0x10 #define STR_FUNC_INDENT 0x20 #define STR_FUNC_LABEL 0x40 +#define STR_FUNC_LIST 0x4000 +#define STR_FUNC_TERM 0x8000 enum string_type { str_label = STR_FUNC_LABEL, @@ -5699,475 +7551,738 @@ enum string_type { str_dquote = (STR_FUNC_EXPAND), str_xquote = (STR_FUNC_EXPAND), str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND), - str_sword = (STR_FUNC_QWORDS), - str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND), + str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST), + str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST), str_ssym = (STR_FUNC_SYMBOL), str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND) }; -static VALUE -parser_str_new(const char *p, long n, rb_encoding *enc, int func, rb_encoding *enc0) +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; + + pstr = rb_parser_encoding_string_new(p, ptr, len, enc); - str = rb_enc_str_new(p, n, enc); - if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) { - if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) { - } - else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) { - rb_enc_associate(str, rb_ascii8bit_encoding()); - } + 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()) { + /* 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->heredoc; +} + +static rb_strterm_t * +new_strterm(struct parser_params *p, int func, int term, int paren) +{ + rb_strterm_t *strterm = ZALLOC(rb_strterm_t); + strterm->u.literal.func = func; + strterm->u.literal.term = term; + strterm->u.literal.paren = paren; + return strterm; +} + +static rb_strterm_t * +new_heredoc(struct parser_params *p) +{ + rb_strterm_t *strterm = ZALLOC(rb_strterm_t); + strterm->heredoc = true; + return strterm; +} + +#define peek(p,c) peek_n(p, (c), 0) +#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n]) +#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 +parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line) +{ + debug_token_line(p, "add_delayed_token", line); + + 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_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); + } + 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; + } +} + +static void +set_lastline(struct parser_params *p, rb_parser_string_t *str) +{ + 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) +{ + rb_parser_string_t *str = p->lex.nextline; + p->lex.nextline = 0; + if (!str) { + if (p->eofp) + return -1; + + if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') { + goto end_of_input; + } + + if (!p->lex.input || !(str = lex_getline(p))) { + end_of_input: + p->eofp = 1; + lex_goto_eol(p); + return -1; + } +#ifndef RIPPER + if (p->debug_lines) { + 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 (str == AFTER_HEREDOC_WITHOUT_TERMINATOR) { + /* after here-document without terminator */ + goto end_of_input; + } + 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, str); + token_flush(p); + return 0; } -#define lex_goto_eol(parser) ((parser)->lex.pcur = (parser)->lex.pend) -#define lex_eol_p() (lex_p >= lex_pend) -#define peek(c) peek_n((c), 0) -#define peek_n(c,n) (lex_p+(n) < lex_pend && (c) == (unsigned char)lex_p[n]) -#define peekc() peekc_n(0) -#define peekc_n(n) (lex_p+(n) < lex_pend ? (unsigned char)lex_p[n] : -1) +static int +parser_cr(struct parser_params *p, int c) +{ + if (peek(p, '\n')) { + p->lex.pcur++; + c = '\n'; + } + return c; +} static inline int -parser_nextc(struct parser_params *parser) +nextc0(struct parser_params *p, int set_encoding) { int c; - if (lex_p == lex_pend) { - VALUE v = lex_nextline; - lex_nextline = 0; - if (!v) { - if (parser->eofp) - return -1; - - if (!lex_input || NIL_P(v = lex_getline(parser))) { - parser->eofp = 1; - lex_goto_eol(parser); - return -1; - } - } - { -#ifdef RIPPER - if (parser->tokp < lex_pend) { - if (NIL_P(parser->delayed)) { - parser->delayed = rb_str_buf_new(1024); - rb_enc_associate(parser->delayed, current_enc); - rb_str_buf_cat(parser->delayed, - parser->tokp, lex_pend - parser->tokp); - parser->delayed_line = ruby_sourceline; - parser->delayed_col = (int)(parser->tokp - lex_pbeg); - } - else { - rb_str_buf_cat(parser->delayed, - parser->tokp, lex_pend - parser->tokp); - } - } -#endif - if (heredoc_end > 0) { - ruby_sourceline = heredoc_end; - heredoc_end = 0; - } - ruby_sourceline++; - parser->line_count++; - lex_pbeg = lex_p = RSTRING_PTR(v); - lex_pend = lex_p + RSTRING_LEN(v); - ripper_flush(parser); - lex_lastline = v; - } - } - c = (unsigned char)*lex_p++; - if (c == '\r') { - if (peek('\n')) { - lex_p++; - c = '\n'; - } - else if (ruby_sourceline > parser->last_cr_line) { - parser->last_cr_line = ruby_sourceline; - rb_warn0("encountered \\r in middle of line, treated as a mere space"); - } + 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++; + if (UNLIKELY(c == '\r')) { + c = parser_cr(p, c); } return c; } +#define nextc(p) nextc0(p, TRUE) static void -parser_pushback(struct parser_params *parser, int c) +pushback(struct parser_params *p, int c) { if (c == -1) return; - lex_p--; - if (lex_p > lex_pbeg && lex_p[0] == '\n' && lex_p[-1] == '\r') { - lex_p--; + p->eofp = 0; + p->lex.pcur--; + if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') { + p->lex.pcur--; } } -#define was_bol() (lex_p == lex_pbeg + 1) +#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1) -#define tokfix() (tokenbuf[tokidx]='\0') -#define tok() tokenbuf -#define toklen() tokidx -#define toklast() (tokidx>0?tokenbuf[tokidx-1]:0) +#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0') +#define tok(p) (p)->tokenbuf +#define toklen(p) (p)->tokidx + +static int +looking_at_eol_p(struct parser_params *p) +{ + const char *ptr = p->lex.pcur; + while (!lex_eol_ptr_p(p, ptr)) { + int c = (unsigned char)*ptr++; + int eol = (c == '\n' || c == '#'); + if (eol || !ISSPACE(c)) { + return eol; + } + } + return TRUE; +} static char* -parser_newtok(struct parser_params *parser) +newtok(struct parser_params *p) { - tokidx = 0; - tokline = ruby_sourceline; - if (!tokenbuf) { - toksiz = 60; - tokenbuf = ALLOC_N(char, 60); + p->tokidx = 0; + if (!p->tokenbuf) { + p->toksiz = 60; + p->tokenbuf = ALLOC_N(char, 60); } - if (toksiz > 4096) { - toksiz = 60; - REALLOC_N(tokenbuf, char, 60); + if (p->toksiz > 4096) { + p->toksiz = 60; + REALLOC_N(p->tokenbuf, char, 60); } - return tokenbuf; + return p->tokenbuf; } static char * -parser_tokspace(struct parser_params *parser, int n) +tokspace(struct parser_params *p, int n) { - tokidx += n; + p->tokidx += n; - if (tokidx >= toksiz) { - do {toksiz *= 2;} while (toksiz < tokidx); - REALLOC_N(tokenbuf, char, toksiz); + if (p->tokidx >= p->toksiz) { + do {p->toksiz *= 2;} while (p->toksiz < p->tokidx); + REALLOC_N(p->tokenbuf, char, p->toksiz); } - return &tokenbuf[tokidx-n]; + return &p->tokenbuf[p->tokidx-n]; } static void -parser_tokadd(struct parser_params *parser, int c) +tokadd(struct parser_params *p, int c) { - tokenbuf[tokidx++] = (char)c; - if (tokidx >= toksiz) { - toksiz *= 2; - REALLOC_N(tokenbuf, char, toksiz); + p->tokenbuf[p->tokidx++] = (char)c; + if (p->tokidx >= p->toksiz) { + p->toksiz *= 2; + REALLOC_N(p->tokenbuf, char, p->toksiz); } } static int -parser_tok_hex(struct parser_params *parser, size_t *numlen) +tok_hex(struct parser_params *p, size_t *numlen) { int c; - c = scan_hex(lex_p, 2, numlen); + c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen); if (!*numlen) { - yyerror("invalid hex escape"); - return 0; + flush_string_content(p, p->enc, rb_strlen_lit("\\x")); + yyerror0("invalid hex escape"); + dispatch_scan_event(p, tSTRING_CONTENT); + return 0; } - lex_p += *numlen; + p->lex.pcur += *numlen; return c; } -#define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n)) +#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n)) -/* return value is for ?\u3042 */ static int -parser_tokadd_utf8(struct parser_params *parser, rb_encoding **encp, - int string_literal, int symbol_literal, int regexp_literal) +escaped_control_code(int c) +{ + int c2 = 0; + switch (c) { + case ' ': + c2 = 's'; + break; + case '\n': + c2 = 'n'; + break; + case '\t': + c2 = 't'; + break; + case '\v': + c2 = 'v'; + break; + case '\r': + c2 = 'r'; + break; + case '\f': + c2 = 'f'; + break; + } + return c2; +} + +#define WARN_SPACE_CHAR(c, prefix) \ + 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, 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)) { + 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) { + 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) { + flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin); + yyerror0("invalid Unicode codepoint"); + dispatch_scan_event(p, tSTRING_CONTENT); + return wide; + } + } + if (regexp_literal) { + tokcopy(p, (int)numlen); + } + else if (codepoint >= 0x80) { + rb_encoding *utf8 = rb_utf8_encoding(); + if (*encp && utf8 != *encp) { + YYLTYPE loc = RUBY_INIT_YYLLOC(); + compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp)); + parser_show_error_line(p, &loc); + return wide; + } + *encp = utf8; + tokaddmbc(p, codepoint, *encp); + } + else { + tokadd(p, codepoint); + } + return TRUE; +} + +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, + int term, int symbol_literal, int regexp_literal) { /* - * If string_literal is true, then we allow multiple codepoints - * in \u{}, and add the codepoints to the current token. - * Otherwise we're parsing a character literal and return a single - * codepoint without adding it + * If `term` is not -1, then we allow multiple codepoints in \u{} + * upto `term` byte, otherwise we're parsing a character literal. + * And then add the codepoints to the current token. */ + static const char multiple_codepoints[] = "Multiple codepoints at single character literal"; + + const int open_brace = '{', close_brace = '}'; + + if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); } + + if (peek(p, open_brace)) { /* handle \u{...} form */ + if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) { + /* + * Skip parsing validation code and copy bytes as-is until term or + * closing brace, in order to correctly handle extended regexps where + * invalid unicode escapes are allowed in comments. The regexp parser + * does its own validation and will catch any issues. + */ + tokadd(p, open_brace); + while (!lex_eol_ptr_p(p, ++p->lex.pcur)) { + int c = peekc(p); + if (c == close_brace) { + tokadd(p, c); + ++p->lex.pcur; + break; + } + else if (c == term) { + break; + } + if (c == '\\' && !lex_eol_n_p(p, 1)) { + tokadd(p, c); + c = *++p->lex.pcur; + } + tokadd_mbchar(p, c); + } + } + else { + const char *second = NULL; + int c, last = nextc(p); + if (lex_eol_p(p)) goto unterminated; + while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur)); + while (c != close_brace) { + if (c == term) goto unterminated; + if (second == multiple_codepoints) + second = p->lex.pcur; + if (regexp_literal) tokadd(p, last); + if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) { + break; + } + while (ISSPACE(c = peekc(p))) { + if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated; + last = c; + } + if (term == -1 && !second) + second = multiple_codepoints; + } - int codepoint; - size_t numlen; + if (c != close_brace) { + unterminated: + flush_string_content(p, rb_utf8_encoding(), 0); + yyerror0("unterminated Unicode escape"); + dispatch_scan_event(p, tSTRING_CONTENT); + return; + } + if (second && second != multiple_codepoints) { + const char *pcur = p->lex.pcur; + p->lex.pcur = second; + dispatch_scan_event(p, tSTRING_CONTENT); + token_flush(p); + p->lex.pcur = pcur; + yyerror0(multiple_codepoints); + token_flush(p); + } - if (regexp_literal) { tokadd('\\'); tokadd('u'); } - - if (peek('{')) { /* handle \u{...} form */ - do { - if (regexp_literal) { tokadd(*lex_p); } - nextc(); - codepoint = scan_hex(lex_p, 6, &numlen); - if (numlen == 0) { - yyerror("invalid Unicode escape"); - return 0; - } - if (codepoint > 0x10ffff) { - yyerror("invalid Unicode codepoint (too large)"); - return 0; - } - lex_p += numlen; - if (regexp_literal) { - tokcopy((int)numlen); - } - else if (codepoint >= 0x80) { - *encp = rb_utf8_encoding(); - if (string_literal) tokaddmbc(codepoint, *encp); - } - else if (string_literal) { - tokadd(codepoint); - } - } while (string_literal && (peek(' ') || peek('\t'))); - - if (!peek('}')) { - yyerror("unterminated Unicode escape"); - return 0; - } - - if (regexp_literal) { tokadd('}'); } - nextc(); + if (regexp_literal) tokadd(p, close_brace); + nextc(p); + } } else { /* handle \uxxxx form */ - codepoint = scan_hex(lex_p, 4, &numlen); - if (numlen < 4) { - yyerror("invalid Unicode escape"); - return 0; - } - lex_p += 4; - if (regexp_literal) { - tokcopy(4); + if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) { + token_flush(p); + return; } - else if (codepoint >= 0x80) { - *encp = rb_utf8_encoding(); - if (string_literal) tokaddmbc(codepoint, *encp); - } - else if (string_literal) { - tokadd(codepoint); - } } - - return codepoint; } #define ESCAPE_CONTROL 1 #define ESCAPE_META 2 static int -parser_read_escape(struct parser_params *parser, int flags, - rb_encoding **encp) +read_escape(struct parser_params *p, int flags, const char *begin) { int c; size_t numlen; - switch (c = nextc()) { + switch (c = nextc(p)) { case '\\': /* Backslash */ - return c; + return c; case 'n': /* newline */ - return '\n'; + return '\n'; case 't': /* horizontal tab */ - return '\t'; + return '\t'; case 'r': /* carriage-return */ - return '\r'; + return '\r'; case 'f': /* form-feed */ - return '\f'; + return '\f'; case 'v': /* vertical tab */ - return '\13'; + return '\13'; case 'a': /* alarm(bell) */ - return '\007'; + return '\007'; case 'e': /* escape */ - return 033; + return 033; case '0': case '1': case '2': case '3': /* octal constant */ case '4': case '5': case '6': case '7': - pushback(c); - c = scan_oct(lex_p, 3, &numlen); - lex_p += numlen; - return c; + pushback(p, c); + c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen); + p->lex.pcur += numlen; + return c; case 'x': /* hex constant */ - c = tok_hex(&numlen); - if (numlen == 0) return 0; - return c; + c = tok_hex(p, &numlen); + if (numlen == 0) return 0; + return c; case 'b': /* backspace */ - return '\010'; + return '\010'; case 's': /* space */ - return ' '; + return ' '; case 'M': - if (flags & ESCAPE_META) goto eof; - if ((c = nextc()) != '-') { - pushback(c); - goto eof; - } - if ((c = nextc()) == '\\') { - if (peek('u')) goto eof; - return read_escape(flags|ESCAPE_META, encp) | 0x80; - } - else if (c == -1 || !ISASCII(c)) goto eof; - else { - return ((c & 0xff) | 0x80); - } + if (flags & ESCAPE_META) goto eof; + if ((c = nextc(p)) != '-') { + goto eof; + } + if ((c = nextc(p)) == '\\') { + switch (peekc(p)) { + case 'u': case 'U': + nextc(p); + goto eof; + } + 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 { + int c2 = escaped_control_code(c); + if (c2) { + if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) { + WARN_SPACE_CHAR(c2, "\\M-"); + } + else { + WARN_SPACE_CHAR(c2, "\\C-\\M-"); + } + } + else if (ISCNTRL(c)) goto eof; + return ((c & 0xff) | 0x80); + } case 'C': - if ((c = nextc()) != '-') { - pushback(c); - goto eof; - } + if ((c = nextc(p)) != '-') { + goto eof; + } case 'c': - if (flags & ESCAPE_CONTROL) goto eof; - if ((c = nextc())== '\\') { - if (peek('u')) goto eof; - c = read_escape(flags|ESCAPE_CONTROL, encp); - } - else if (c == '?') - return 0177; - else if (c == -1 || !ISASCII(c)) goto eof; - return c & 0x9f; + if (flags & ESCAPE_CONTROL) goto eof; + if ((c = nextc(p))== '\\') { + switch (peekc(p)) { + case 'u': case 'U': + nextc(p); + goto eof; + } + c = read_escape(p, flags|ESCAPE_CONTROL, begin); + } + else if (c == '?') + return 0177; + else if (c == -1) goto eof; + else if (!ISASCII(c)) { + tokskip_mbchar(p); + goto eof; + } + else { + int c2 = escaped_control_code(c); + if (c2) { + if (ISCNTRL(c)) { + if (flags & ESCAPE_META) { + WARN_SPACE_CHAR(c2, "\\M-"); + } + else { + WARN_SPACE_CHAR(c2, ""); + } + } + else { + if (flags & ESCAPE_META) { + WARN_SPACE_CHAR(c2, "\\M-\\C-"); + } + else { + WARN_SPACE_CHAR(c2, "\\C-"); + } + } + } + else if (ISCNTRL(c)) goto eof; + } + return c & 0x9f; eof: case -1: - yyerror("Invalid escape character syntax"); - return '\0'; + flush_string_content(p, p->enc, p->lex.pcur - begin); + yyerror0("Invalid escape character syntax"); + dispatch_scan_event(p, tSTRING_CONTENT); + return '\0'; default: - return c; + if (!ISASCII(c)) { + tokskip_mbchar(p); + goto eof; + } + return c; } } static void -parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc) +tokaddmbc(struct parser_params *p, int c, rb_encoding *enc) { int len = rb_enc_codelen(c, enc); - rb_enc_mbcput(c, tokspace(len), enc); + rb_enc_mbcput(c, tokspace(p, len), enc); } static int -parser_tokadd_escape(struct parser_params *parser, rb_encoding **encp) +tokadd_escape(struct parser_params *p) { int c; - int flags = 0; size_t numlen; + const char *begin = p->lex.pcur; - first: - switch (c = nextc()) { + switch (c = nextc(p)) { case '\n': - return 0; /* just ignore */ + return 0; /* just ignore */ case '0': case '1': case '2': case '3': /* octal constant */ case '4': case '5': case '6': case '7': - { - ruby_scan_oct(--lex_p, 3, &numlen); - if (numlen == 0) goto eof; - lex_p += numlen; - tokcopy((int)numlen + 1); - } - return 0; + { + ruby_scan_oct(--p->lex.pcur, 3, &numlen); + if (numlen == 0) goto eof; + p->lex.pcur += numlen; + tokcopy(p, (int)numlen + 1); + } + return 0; case 'x': /* hex constant */ - { - tok_hex(&numlen); - if (numlen == 0) return -1; - tokcopy((int)numlen + 2); - } - return 0; - - case 'M': - if (flags & ESCAPE_META) goto eof; - if ((c = nextc()) != '-') { - pushback(c); - goto eof; - } - tokcopy(3); - flags |= ESCAPE_META; - goto escaped; - - case 'C': - if (flags & ESCAPE_CONTROL) goto eof; - if ((c = nextc()) != '-') { - pushback(c); - goto eof; - } - tokcopy(3); - goto escaped; - - case 'c': - if (flags & ESCAPE_CONTROL) goto eof; - tokcopy(2); - flags |= ESCAPE_CONTROL; - escaped: - if ((c = nextc()) == '\\') { - goto first; - } - else if (c == -1) goto eof; - tokadd(c); - return 0; + { + tok_hex(p, &numlen); + if (numlen == 0) return -1; + tokcopy(p, (int)numlen + 2); + } + return 0; eof: case -1: - yyerror("Invalid escape character syntax"); - return -1; + flush_string_content(p, p->enc, p->lex.pcur - begin); + yyerror0("Invalid escape character syntax"); + token_flush(p); + return -1; default: - tokadd('\\'); - tokadd(c); + tokadd(p, '\\'); + tokadd(p, c); } return 0; } static int -parser_regx_options(struct parser_params *parser) +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; int kopt = 0; int options = 0; int c, opt, kc; - newtok(); - while (c = nextc(), ISALPHA(c)) { + newtok(p); + while (c = nextc(p), ISALPHA(c)) { if (c == 'o') { options |= RE_OPTION_ONCE; } - else if (rb_char_to_option_kcode(c, &opt, &kc)) { - if (kc >= 0) { - if (kc != rb_ascii8bit_encindex()) kcode = c; - kopt = opt; - } - else { - options |= opt; - } + else if (char_to_option_kcode(c, &opt, &kc)) { + if (kc >= 0) { + if (kc != ENC_ASCII8BIT) kcode = c; + kopt = opt; + } + else { + options |= opt; + } } else { - tokadd(c); + tokadd(p, c); } } options |= kopt; - pushback(c); - if (toklen()) { - tokfix(); - compile_error(PARSER_ARG "unknown regexp option%s - %s", - toklen() > 1 ? "s" : "", tok()); + pushback(p, c); + if (toklen(p)) { + YYLTYPE loc = RUBY_INIT_YYLLOC(); + tokfix(p); + compile_error(p, "unknown regexp option%s - %*s", + toklen(p) > 1 ? "s" : "", toklen(p), tok(p)); + parser_show_error_line(p, &loc); } return options | RE_OPTION_ENCODING(kcode); } -static void -dispose_string(VALUE str) -{ - rb_str_free(str); - rb_gc_force_recycle(str); -} - static int -parser_tokadd_mbchar(struct parser_params *parser, int c) +tokadd_mbchar(struct parser_params *p, int c) { - int len = parser_precise_mbclen(); - if (!MBCLEN_CHARFOUND_P(len)) { - compile_error(PARSER_ARG "invalid multibyte char (%s)", parser_encoding_name()); - return -1; - } - tokadd(c); - lex_p += --len; - if (len > 0) tokcopy(len); + int len = parser_precise_mbclen(p, p->lex.pcur-1); + if (len < 0) return -1; + tokadd(p, c); + p->lex.pcur += --len; + if (len > 0) tokcopy(p, len); return c; } -#define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c)) - static inline int simple_re_meta(int c) { @@ -6175,189 +8290,259 @@ simple_re_meta(int c) case '$': case '*': case '+': case '.': case '?': case '^': case '|': case ')': case ']': case '}': case '>': - return TRUE; + return TRUE; default: - return FALSE; + return FALSE; } } static int -parser_tokadd_string(struct parser_params *parser, - int func, int term, int paren, long *nest, - rb_encoding **encp) +parser_update_heredoc_indent(struct parser_params *p, int c) +{ + if (p->heredoc_line_indent == -1) { + if (c == '\n') p->heredoc_line_indent = 0; + } + else { + if (c == ' ') { + p->heredoc_line_indent++; + return TRUE; + } + else if (c == '\t') { + int w = (p->heredoc_line_indent / TAB_WIDTH) + 1; + p->heredoc_line_indent = w * TAB_WIDTH; + return TRUE; + } + else if (c != '\n') { + if (p->heredoc_indent > p->heredoc_line_indent) { + p->heredoc_indent = p->heredoc_line_indent; + } + p->heredoc_line_indent = -1; + } + else { + /* Whitespace only line has no indentation */ + p->heredoc_line_indent = 0; + } + } + return FALSE; +} + +static void +parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2) +{ + YYLTYPE loc = RUBY_INIT_YYLLOC(); + const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2); + compile_error(p, "%s mixed within %s source", n1, n2); + parser_show_error_line(p, &loc); +} + +static void +parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2) +{ + const char *pos = p->lex.pcur; + p->lex.pcur = beg; + parser_mixed_error(p, enc1, enc2); + p->lex.pcur = pos; +} + +static inline char +nibble_char_upper(unsigned int c) +{ + c &= 0xf; + return c + (c < 10 ? '0' : 'A' - 10); +} + +static int +tokadd_string(struct parser_params *p, + int func, int term, int paren, long *nest, + rb_encoding **encp, rb_encoding **enc) { int c; - int has_nonascii = 0; - rb_encoding *enc = *encp; - char *errbuf = 0; - static const char mixed_msg[] = "%s mixed within %s source"; - -#define mixed_error(enc1, enc2) if (!errbuf) { \ - size_t len = sizeof(mixed_msg) - 4; \ - len += strlen(rb_enc_name(enc1)); \ - len += strlen(rb_enc_name(enc2)); \ - errbuf = ALLOCA_N(char, len); \ - snprintf(errbuf, len, mixed_msg, \ - rb_enc_name(enc1), \ - rb_enc_name(enc2)); \ - yyerror(errbuf); \ - } -#define mixed_escape(beg, enc1, enc2) do { \ - const char *pos = lex_p; \ - lex_p = (beg); \ - mixed_error((enc1), (enc2)); \ - lex_p = pos; \ - } while (0) + bool erred = false; +#ifdef RIPPER + const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0); + int top_of_line = FALSE; +#endif + +#define mixed_error(enc1, enc2) \ + (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true)) +#define mixed_escape(beg, enc1, enc2) \ + (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true)) + + while ((c = nextc(p)) != -1) { + if (p->heredoc_indent > 0) { + parser_update_heredoc_indent(p, c); + } +#ifdef RIPPER + if (top_of_line && heredoc_end == p->ruby_sourceline) { + pushback(p, c); + break; + } +#endif - while ((c = nextc()) != -1) { - if (paren && c == paren) { - ++*nest; - } - else if (c == term) { - if (!nest || !*nest) { - pushback(c); - break; - } - --*nest; - } - else if ((func & STR_FUNC_EXPAND) && c == '#' && lex_p < lex_pend) { - int c2 = *lex_p; - if (c2 == '$' || c2 == '@' || c2 == '{') { - pushback(c); - break; - } - } - else if (c == '\\') { - const char *beg = lex_p - 1; - c = nextc(); - switch (c) { - case '\n': - if (func & STR_FUNC_QWORDS) break; - if (func & STR_FUNC_EXPAND) continue; - tokadd('\\'); - break; - - case '\\': - if (func & STR_FUNC_ESCAPE) tokadd(c); - break; - - case 'u': - if ((func & STR_FUNC_EXPAND) == 0) { - tokadd('\\'); - break; - } - parser_tokadd_utf8(parser, &enc, 1, - func & STR_FUNC_SYMBOL, - func & STR_FUNC_REGEXP); - if (has_nonascii && enc != *encp) { - mixed_escape(beg, enc, *encp); - } - continue; - - default: - if (c == -1) return -1; - if (!ISASCII(c)) { - if ((func & STR_FUNC_EXPAND) == 0) tokadd('\\'); - goto non_ascii; - } - if (func & STR_FUNC_REGEXP) { - if (c == term && !simple_re_meta(c)) { - tokadd(c); - continue; - } - pushback(c); - if ((c = tokadd_escape(&enc)) < 0) - return -1; - if (has_nonascii && enc != *encp) { - mixed_escape(beg, enc, *encp); - } - continue; - } - else if (func & STR_FUNC_EXPAND) { - pushback(c); - if (func & STR_FUNC_ESCAPE) tokadd('\\'); - c = read_escape(0, &enc); - } - else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { - /* ignore backslashed spaces in %w */ - } - else if (c != term && !(paren && c == paren)) { - tokadd('\\'); - pushback(c); - continue; - } - } - } - else if (!parser_isascii()) { - non_ascii: - has_nonascii = 1; - if (enc != *encp) { - mixed_error(enc, *encp); - continue; - } - if (tokadd_mbchar(c) == -1) return -1; - continue; - } - else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { - pushback(c); - break; - } + if (paren && c == paren) { + ++*nest; + } + else if (c == term) { + if (!nest || !*nest) { + pushback(p, c); + break; + } + --*nest; + } + else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) { + unsigned char c2 = *p->lex.pcur; + if (c2 == '$' || c2 == '@' || c2 == '{') { + pushback(p, c); + break; + } + } + else if (c == '\\') { + c = nextc(p); + switch (c) { + case '\n': + if (func & STR_FUNC_QWORDS) break; + if (func & STR_FUNC_EXPAND) { + if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0)) + continue; + if (c == term) { + c = '\\'; + goto terminate; + } + } + tokadd(p, '\\'); + break; + + case '\\': + if (func & STR_FUNC_ESCAPE) tokadd(p, c); + break; + + case 'u': + if ((func & STR_FUNC_EXPAND) == 0) { + tokadd(p, '\\'); + break; + } + tokadd_utf8(p, enc, term, + func & STR_FUNC_SYMBOL, + func & STR_FUNC_REGEXP); + continue; + + default: + if (c == -1) return -1; + if (!ISASCII(c)) { + if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\'); + goto non_ascii; + } + if (func & STR_FUNC_REGEXP) { + switch (c) { + case 'c': + case 'C': + case 'M': { + pushback(p, c); + c = read_escape(p, 0, p->lex.pcur - 1); + + char *t = tokspace(p, rb_strlen_lit("\\x00")); + *t++ = '\\'; + *t++ = 'x'; + *t++ = nibble_char_upper(c >> 4); + *t++ = nibble_char_upper(c); + continue; + } + } + + if (c == term && !simple_re_meta(c)) { + tokadd(p, c); + continue; + } + pushback(p, c); + if ((c = tokadd_escape(p)) < 0) + return -1; + if (*enc && *enc != *encp) { + mixed_escape(p->lex.ptok+2, *enc, *encp); + } + continue; + } + else if (func & STR_FUNC_EXPAND) { + pushback(p, c); + if (func & STR_FUNC_ESCAPE) tokadd(p, '\\'); + c = read_escape(p, 0, p->lex.pcur - 1); + } + else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { + /* ignore backslashed spaces in %w */ + } + else if (c != term && !(paren && c == paren)) { + tokadd(p, '\\'); + pushback(p, c); + continue; + } + } + } + else if (!parser_isascii(p)) { + non_ascii: + if (!*enc) { + *enc = *encp; + } + else if (*enc != *encp) { + mixed_error(*enc, *encp); + continue; + } + if (tokadd_mbchar(p, c) == -1) return -1; + continue; + } + else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { + pushback(p, c); + break; + } if (c & 0x80) { - has_nonascii = 1; - if (enc != *encp) { - mixed_error(enc, *encp); - continue; - } + if (!*enc) { + *enc = *encp; + } + else if (*enc != *encp) { + mixed_error(*enc, *encp); + continue; + } } - tokadd(c); + tokadd(p, c); +#ifdef RIPPER + top_of_line = (c == '\n'); +#endif } - *encp = enc; + terminate: + if (*enc) *encp = *enc; return c; } -#define NEW_STRTERM(func, term, paren) \ - rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0) +#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren) -#ifdef RIPPER static void -ripper_flush_string_content(struct parser_params *parser, rb_encoding *enc) -{ - VALUE content = yylval.val; - if (!ripper_is_node_yylval(content)) - content = ripper_new_yylval(0, 0, content); - if (!NIL_P(parser->delayed)) { - ptrdiff_t len = lex_p - parser->tokp; - if (len > 0) { - rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc); - } - ripper_dispatch_delayed_token(parser, tSTRING_CONTENT); - parser->tokp = lex_p; - RNODE(content)->nd_rval = yylval.val; - } - ripper_dispatch_scan_event(parser, tSTRING_CONTENT); - if (yylval.val != content) - RNODE(content)->nd_rval = yylval.val; - yylval.val = content; -} - -#define flush_string_content(enc) ripper_flush_string_content(parser, (enc)) -#else -#define flush_string_content(enc) ((void)(enc)) -#endif +flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back) +{ + p->lex.pcur -= back; + if (has_delayed_token(p)) { + ptrdiff_t len = p->lex.pcur - p->lex.ptok; + if (len > 0) { + 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); + } + dispatch_delayed_token(p, tSTRING_CONTENT); + p->lex.ptok = p->lex.pcur; + } + dispatch_scan_event(p, tSTRING_CONTENT); + p->lex.pcur += back; +} -RUBY_FUNC_EXPORTED const unsigned int 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 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0) #define SPECIAL_PUNCT(idx) ( \ - BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \ - BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \ - BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \ - BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \ - BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \ - BIT('0', idx)) -const unsigned int ruby_global_name_punct_bits[] = { + BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \ + BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \ + BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \ + BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \ + BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \ + BIT('0', idx)) +const uint_least32_t ruby_global_name_punct_bits[] = { SPECIAL_PUNCT(0), SPECIAL_PUNCT(1), SPECIAL_PUNCT(2), @@ -6366,201 +8551,384 @@ const unsigned int ruby_global_name_punct_bits[] = { #undef SPECIAL_PUNCT #endif -static int -parser_peek_variable_name(struct parser_params *parser) +static enum yytokentype +parser_peek_variable_name(struct parser_params *p) { int c; - const char *p = lex_p; + const char *ptr = p->lex.pcur; - if (p + 1 >= lex_pend) return 0; - c = *p++; + if (lex_eol_ptr_n_p(p, ptr, 1)) return 0; + c = *ptr++; switch (c) { case '$': - if ((c = *p) == '-') { - if (++p >= lex_pend) return 0; - c = *p; - } - else if (is_global_name_punct(c) || ISDIGIT(c)) { - return tSTRING_DVAR; - } - break; + if ((c = *ptr) == '-') { + if (lex_eol_ptr_p(p, ++ptr)) return 0; + c = *ptr; + } + else if (is_global_name_punct(c) || ISDIGIT(c)) { + return tSTRING_DVAR; + } + break; case '@': - if ((c = *p) == '@') { - if (++p >= lex_pend) return 0; - c = *p; - } - break; + if ((c = *ptr) == '@') { + if (lex_eol_ptr_p(p, ++ptr)) return 0; + c = *ptr; + } + break; case '{': - lex_p = p; - command_start = TRUE; - return tSTRING_DBEG; + p->lex.pcur = ptr; + p->command_start = TRUE; + yylval.state = p->lex.state; + return tSTRING_DBEG; default: - return 0; + return 0; } if (!ISASCII(c) || c == '_' || ISALPHA(c)) - return tSTRING_DVAR; + return tSTRING_DVAR; return 0; } -static int -parser_parse_string(struct parser_params *parser, NODE *quote) +#define IS_ARG() IS_lex_state(EXPR_ARG_ANY) +#define IS_END() IS_lex_state(EXPR_END_ANY) +#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) +#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c)) +#define IS_LABEL_POSSIBLE() (\ + (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \ + IS_ARG()) +#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1)) +#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT) + +static inline enum yytokentype +parser_string_term(struct parser_params *p, int func) { - int func = (int)quote->nd_func; - int term = nd_term(quote); - int paren = nd_paren(quote); - int c, space = 0; - rb_encoding *enc = current_enc; + xfree(p->lex.strterm); + p->lex.strterm = 0; + if (func & STR_FUNC_REGEXP) { + set_yylval_num(regx_options(p)); + dispatch_scan_event(p, tREGEXP_END); + SET_LEX_STATE(EXPR_END); + return tREGEXP_END; + } + if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) { + nextc(p); + SET_LEX_STATE(EXPR_ARG|EXPR_LABELED); + return tLABEL_END; + } + SET_LEX_STATE(EXPR_END); + return tSTRING_END; +} - if (func == -1) return tSTRING_END; - c = nextc(); +static enum yytokentype +parse_string(struct parser_params *p, rb_strterm_literal_t *quote) +{ + int func = quote->func; + int term = quote->term; + int paren = quote->paren; + int c, space = 0; + rb_encoding *enc = p->enc; + rb_encoding *base_enc = 0; + rb_parser_string_t *lit; + + if (func & STR_FUNC_TERM) { + if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */ + SET_LEX_STATE(EXPR_END); + xfree(p->lex.strterm); + p->lex.strterm = 0; + return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END; + } + c = nextc(p); if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { - do {c = nextc();} while (ISSPACE(c)); - space = 1; - } - if (c == term && !quote->nd_nest) { - if (func & STR_FUNC_QWORDS) { - quote->nd_func = -1; - return ' '; - } - if (!(func & STR_FUNC_REGEXP)) return tSTRING_END; - set_yylval_num(regx_options()); -#ifdef RIPPER - ripper_dispatch_scan_event(parser, tREGEXP_END); -#endif - return tREGEXP_END; + while (c != '\n' && ISSPACE(c = nextc(p))); + space = 1; + } + if (func & STR_FUNC_LIST) { + quote->func &= ~STR_FUNC_LIST; + space = 1; + } + if (c == term && !quote->nest) { + 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); + return ' '; + } + return parser_string_term(p, func); } if (space) { - pushback(c); - return ' '; + if (!ISSPACE(c)) pushback(p, c); + add_delayed_token(p, p->lex.ptok, p->lex.pcur); + return ' '; } - newtok(); + newtok(p); if ((func & STR_FUNC_EXPAND) && c == '#') { - int t = parser_peek_variable_name(parser); - if (t) return t; - tokadd('#'); - c = nextc(); - } - pushback(c); - if (tokadd_string(func, term, paren, "e->nd_nest, - &enc) == -1) { - ruby_sourceline = nd_line(quote); - if (func & STR_FUNC_REGEXP) { - if (parser->eofp) - compile_error(PARSER_ARG "unterminated regexp meets end of file"); - return tREGEXP_END; - } - else { - if (parser->eofp) - compile_error(PARSER_ARG "unterminated string meets end of file"); - return tSTRING_END; - } - } - - tokfix(); - set_yylval_str(STR_NEW3(tok(), toklen(), enc, func)); - flush_string_content(enc); + enum yytokentype t = parser_peek_variable_name(p); + if (t) return t; + tokadd(p, '#'); + c = nextc(p); + } + pushback(p, c); + if (tokadd_string(p, func, term, paren, "e->nest, + &enc, &base_enc) == -1) { + if (p->eofp) { +#ifndef RIPPER +# define unterminated_literal(mesg) yyerror0(mesg) +#else +# define unterminated_literal(mesg) compile_error(p, mesg) +#endif + literal_flush(p, p->lex.pcur); + if (func & STR_FUNC_QWORDS) { + /* no content to add, bailing out here */ + unterminated_literal("unterminated list meets end of file"); + xfree(p->lex.strterm); + p->lex.strterm = 0; + return tSTRING_END; + } + if (func & STR_FUNC_REGEXP) { + unterminated_literal("unterminated regexp meets end of file"); + } + else { + unterminated_literal("unterminated string meets end of file"); + } + quote->func |= STR_FUNC_TERM; + } + } + + tokfix(p); + lit = STR_NEW3(tok(p), toklen(p), enc, func); + set_yylval_str(lit); + flush_string_content(p, enc, 0); return tSTRING_CONTENT; } -static int -parser_heredoc_identifier(struct parser_params *parser) +static enum yytokentype +heredoc_identifier(struct parser_params *p) { - int c = nextc(), term, func = 0; - long len; + /* + * term_len is length of `<<"END"` except `END`, + * in this case term_len is 4 (<, <, " and "). + */ + long len, offset = p->lex.pcur - p->lex.pbeg; + int c = nextc(p), term, func = 0, quote = 0; + enum yytokentype token = tSTRING_BEG; + int indent = 0; if (c == '-') { - c = nextc(); - func = STR_FUNC_INDENT; + c = nextc(p); + func = STR_FUNC_INDENT; + offset++; + } + else if (c == '~') { + c = nextc(p); + func = STR_FUNC_INDENT; + offset++; + indent = INT_MAX; } switch (c) { case '\'': - func |= str_squote; goto quoted; + func |= str_squote; goto quoted; case '"': - func |= str_dquote; goto quoted; + func |= str_dquote; goto quoted; case '`': - func |= str_xquote; + token = tXSTRING_BEG; + func |= str_xquote; goto quoted; + quoted: - newtok(); - tokadd(func); - term = c; - while ((c = nextc()) != -1 && c != term) { - if (tokadd_mbchar(c) == -1) return 0; - } - if (c == -1) { - compile_error(PARSER_ARG "unterminated here document identifier"); - return 0; - } - break; + quote++; + offset++; + term = c; + len = 0; + while ((c = nextc(p)) != term) { + if (c == -1 || c == '\r' || c == '\n') { + yyerror0("unterminated here document identifier"); + return -1; + } + } + break; default: - if (!parser_is_identchar()) { - pushback(c); - if (func & STR_FUNC_INDENT) { - pushback('-'); - } - return 0; - } - newtok(); - term = '"'; - tokadd(func |= str_dquote); - do { - if (tokadd_mbchar(c) == -1) return 0; - } while ((c = nextc()) != -1 && parser_is_identchar()); - pushback(c); - break; - } - - tokfix(); -#ifdef RIPPER - ripper_dispatch_scan_event(parser, tHEREDOC_BEG); -#endif - len = lex_p - lex_pbeg; - lex_goto_eol(parser); - lex_strterm = rb_node_newnode(NODE_HEREDOC, - STR_NEW(tok(), toklen()), /* nd_lit */ - len, /* nd_nth */ - lex_lastline); /* nd_orig */ - nd_set_line(lex_strterm, ruby_sourceline); - ripper_flush(parser); - return term == '`' ? tXSTRING_BEG : tSTRING_BEG; + if (!parser_is_identchar(p)) { + pushback(p, c); + if (func & STR_FUNC_INDENT) { + pushback(p, indent > 0 ? '~' : '-'); + } + return 0; + } + func |= str_dquote; + do { + int n = parser_precise_mbclen(p, p->lex.pcur-1); + if (n < 0) return 0; + p->lex.pcur += --n; + } while ((c = nextc(p)) != -1 && parser_is_identchar(p)); + pushback(p, c); + break; + } + + len = p->lex.pcur - (p->lex.pbeg + offset) - quote; + if ((unsigned long)len >= HERETERM_LENGTH_MAX) + yyerror0("too long here document identifier"); + dispatch_scan_event(p, tHEREDOC_BEG); + lex_goto_eol(p); + + p->lex.strterm = new_heredoc(p); + rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc; + here->offset = offset; + here->sourceline = p->ruby_sourceline; + here->length = (unsigned)len; + here->quote = quote; + here->func = func; + here->lastline = p->lex.lastline; + + token_flush(p); + p->heredoc_indent = indent; + p->heredoc_line_indent = 0; + return token; } static void -parser_heredoc_restore(struct parser_params *parser, NODE *here) +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; - lex_strterm = 0; - line = here->nd_orig; - lex_lastline = line; - lex_pbeg = RSTRING_PTR(line); - lex_pend = lex_pbeg + RSTRING_LEN(line); - lex_p = lex_pbeg + here->nd_nth; - heredoc_end = ruby_sourceline; - ruby_sourceline = nd_line(here); - dispose_string(here->nd_lit); - rb_gc_force_recycle((VALUE)here); - ripper_flush(parser); + p->lex.strterm = 0; + line = here->lastline; + p->lex.lastline = 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 = AFTER_HEREDOC_WITHOUT_TERMINATOR; + p->eofp = 0; + xfree(term); } static int -parser_whole_match_p(struct parser_params *parser, - const char *eos, long len, int indent) +dedent_string_column(const char *str, long len, int width) { - const char *p = lex_pbeg; - long n; + int i, col = 0; + + for (i = 0; i < len && col < width; i++) { + if (str[i] == ' ') { + col++; + } + else if (str[i] == '\t') { + int n = TAB_WIDTH * (col / TAB_WIDTH + 1); + if (n > width) break; + col = n; + } + else { + 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_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_parser_str_set_len(p, string, len - i); + return i; +} + +static NODE * +heredoc_dedent(struct parser_params *p, NODE *root) +{ + NODE *node, *str_node, *prev_node; + int indent = p->heredoc_indent; + rb_parser_string_t *prev_lit = 0; + + if (indent <= 0) return root; + 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) { + rb_parser_string_t *lit = RNODE_STR(str_node)->string; + if (nd_fl_newline(str_node)) { + dedent_string(p, lit, indent); + } + if (!prev_lit) { + prev_lit = lit; + } + else if (!literal_concat0(p, prev_lit, lit)) { + return 0; + } + else { + NODE *end = RNODE_LIST(node)->as.nd_end; + node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next; + if (!node) { + if (nd_type_p(prev_node, NODE_DSTR)) + nd_set_type(prev_node, NODE_STR); + break; + } + RNODE_LIST(node)->as.nd_end = end; + goto next_str; + } + + str_node = 0; + while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) { + next_str: + if (!nd_type_p(node, NODE_LIST)) break; + if ((str_node = RNODE_LIST(node)->nd_head) != 0) { + enum node_type type = nd_type(str_node); + if (type == NODE_STR || type == NODE_DSTR) break; + prev_lit = 0; + str_node = 0; + } + } + } + return root; +} + +static int +whole_match_p(struct parser_params *p, const char *eos, long len, int indent) +{ + const char *beg = p->lex.pbeg; + const char *ptr = p->lex.pend; + + if (ptr - beg < len) return FALSE; + if (ptr > beg && ptr[-1] == '\n') { + if (--ptr > beg && ptr[-1] == '\r') --ptr; + if (ptr - beg < len) return FALSE; + } + if (strncmp(eos, ptr -= len, len)) return FALSE; if (indent) { - while (*p && ISSPACE(*p)) p++; + while (beg < ptr && ISSPACE(*beg)) beg++; } - n = lex_pend - (p + len); - if (n < 0) return FALSE; - if (n > 0 && p[len] != '\n') { - if (p[len] != '\r') return FALSE; - if (n <= 1 || p[len+1] != '\n') return FALSE; + return beg == ptr; +} + +static int +word_match_p(struct parser_params *p, const char *word, long len) +{ + if (strncmp(p->lex.pcur, word, len)) return 0; + if (lex_eol_n_p(p, len)) return 1; + int c = (unsigned char)p->lex.pcur[len]; + if (ISSPACE(c)) return 1; + switch (c) { + case '\0': case '\004': case '\032': return 1; } - return strncmp(eos, p, len) == 0; + return 0; } #define NUM_SUFFIX_R (1<<0) @@ -6568,373 +8936,508 @@ parser_whole_match_p(struct parser_params *parser, #define NUM_SUFFIX_ALL 3 static int -parser_number_literal_suffix(struct parser_params *parser, int mask) +number_literal_suffix(struct parser_params *p, int mask) { int c, result = 0; - const char *lastp = lex_p; - - while ((c = nextc()) != -1) { - if ((mask & NUM_SUFFIX_I) && c == 'i') { - result |= (mask & NUM_SUFFIX_I); - mask &= ~NUM_SUFFIX_I; - /* r after i, rational of complex is disallowed */ - mask &= ~NUM_SUFFIX_R; - continue; - } - if ((mask & NUM_SUFFIX_R) && c == 'r') { - result |= (mask & NUM_SUFFIX_R); - mask &= ~NUM_SUFFIX_R; - continue; - } - if (!ISASCII(c) || ISALPHA(c) || c == '_') { - lex_p = lastp; - return 0; - } - pushback(c); - if (c == '.') { - c = peekc_n(1); - if (ISDIGIT(c)) { - yyerror("unexpected fraction part after numeric literal"); - lex_p += 2; - while (parser_is_identchar()) nextc(); - } - } - break; + const char *lastp = p->lex.pcur; + + while ((c = nextc(p)) != -1) { + if ((mask & NUM_SUFFIX_I) && c == 'i') { + result |= (mask & NUM_SUFFIX_I); + mask &= ~NUM_SUFFIX_I; + /* r after i, rational of complex is disallowed */ + mask &= ~NUM_SUFFIX_R; + continue; + } + if ((mask & NUM_SUFFIX_R) && c == 'r') { + result |= (mask & NUM_SUFFIX_R); + mask &= ~NUM_SUFFIX_R; + continue; + } + if (!ISASCII(c) || ISALPHA(c) || c == '_') { + p->lex.pcur = lastp; + literal_flush(p, p->lex.pcur); + return 0; + } + pushback(p, c); + break; } return result; } -static int -parser_set_number_literal(struct parser_params *parser, VALUE v, int type, int suffix) +static enum yytokentype +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); - return type; -} -static int -parser_set_integer_literal(struct parser_params *parser, VALUE v, int suffix) -{ - int type = tINTEGER; if (suffix & NUM_SUFFIX_R) { - v = rb_rational_raw1(v); - type = tRATIONAL; + type = tRATIONAL; + numeric_type = rational_literal; + } + if (suffix & NUM_SUFFIX_I) { + type = tIMAGINARY; + } + + 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); } - return set_number_literal(v, type, suffix); + SET_LEX_STATE(EXPR_END); + return type; } -#ifdef RIPPER +#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__) static void -ripper_dispatch_heredoc_end(struct parser_params *parser) +parser_dispatch_heredoc_end(struct parser_params *p, int line) { - if (!NIL_P(parser->delayed)) - ripper_dispatch_delayed_token(parser, tSTRING_CONTENT); - lex_goto_eol(parser); - ripper_dispatch_ignored_scan_event(parser, tHEREDOC_END); -} + if (has_delayed_token(p)) + dispatch_delayed_token(p, tSTRING_CONTENT); -#define dispatch_heredoc_end() ripper_dispatch_heredoc_end(parser) +#ifdef RIPPER + VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok); + ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str); #else -#define dispatch_heredoc_end() ((void)0) + if (p->keep_tokens) { + 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 -static int -parser_here_document(struct parser_params *parser, NODE *here) + RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc); + lex_goto_eol(p); + token_flush(p); +} + +static enum yytokentype +here_document(struct parser_params *p, rb_strterm_heredoc_t *here) { int c, func, indent = 0; - const char *eos, *p, *pend; + const char *eos, *ptr, *ptr_end; long len; - VALUE str = 0; - rb_encoding *enc = current_enc; + 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->nd_lit); - len = RSTRING_LEN(here->nd_lit) - 1; - indent = (func = *eos++) & STR_FUNC_INDENT; + eos = PARSER_STRING_PTR(here->lastline) + here->offset; + len = here->length; + indent = (func = here->func) & STR_FUNC_INDENT; - if ((c = nextc()) == -1) { + if ((c = nextc(p)) == -1) { error: - compile_error(PARSER_ARG "can't find string \"%s\" anywhere before EOF", eos); #ifdef RIPPER - if (NIL_P(parser->delayed)) { - ripper_dispatch_scan_event(parser, tSTRING_CONTENT); - } - else { - if (str) { - rb_str_append(parser->delayed, str); - } - else if ((len = lex_p - parser->tokp) > 0) { - if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) { - int cr = ENC_CODERANGE_UNKNOWN; - rb_str_coderange_scan_restartable(parser->tokp, lex_p, enc, &cr); - if (cr != ENC_CODERANGE_7BIT && - current_enc == rb_usascii_encoding() && - enc != rb_utf8_encoding()) { - enc = rb_ascii8bit_encoding(); - } - } - rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc); - } - ripper_dispatch_delayed_token(parser, tSTRING_CONTENT); - } - lex_goto_eol(parser); + if (!has_delayed_token(p)) { + dispatch_scan_event(p, tSTRING_CONTENT); + } + else if (p->delayed.end_line + 1 == p->ruby_sourceline) { + if ((len = p->lex.pcur - p->lex.ptok) > 0) { + 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 && + rb_is_usascii_enc(p->enc) && + enc != rb_utf8_encoding()) { + enc = rb_ascii8bit_encoding(); + } + } + 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); + compile_error(p, "can't find string \"%.*s\" anywhere before EOF", + (int)len, eos); + token_flush(p); + SET_LEX_STATE(EXPR_END); + return tSTRING_END; + } + bol = was_bol(p); + if (!bol) { + /* not beginning of line, cannot be the terminator */ + } + else if (p->heredoc_line_indent == -1) { + /* `heredoc_line_indent == -1` means + * - "after an interpolation in the same line", or + * - "in a continuing line" + */ + p->heredoc_line_indent = 0; + } + else if (whole_match_p(p, eos, len, indent)) { + dispatch_heredoc_end(p); restore: - heredoc_restore(lex_strterm); - return 0; - } - if (was_bol() && whole_match_p(eos, len, indent)) { - dispatch_heredoc_end(); - heredoc_restore(lex_strterm); - return tSTRING_END; + heredoc_restore(p, &p->lex.strterm->u.heredoc); + token_flush(p); + SET_LEX_STATE(EXPR_END); + return tSTRING_END; } if (!(func & STR_FUNC_EXPAND)) { - do { - p = RSTRING_PTR(lex_lastline); - pend = lex_pend; - if (pend > p) { - switch (pend[-1]) { - case '\n': - if (--pend == p || pend[-1] != '\r') { - pend++; - break; - } - case '\r': - --pend; - } - } - if (str) - rb_str_cat(str, p, pend - p); - else - str = STR_NEW(p, pend - p); - if (pend < lex_pend) rb_str_cat(str, "\n", 1); - lex_goto_eol(parser); - if (nextc() == -1) { - if (str) { - dispose_string(str); - str = 0; - } - goto error; - } - } while (!whole_match_p(eos, len, indent)); + do { + ptr = PARSER_STRING_PTR(p->lex.lastline); + ptr_end = p->lex.pend; + if (ptr_end > ptr) { + switch (ptr_end[-1]) { + case '\n': + if (--ptr_end == ptr || ptr_end[-1] != '\r') { + ptr_end++; + break; + } + case '\r': + --ptr_end; + } + } + + if (p->heredoc_indent > 0) { + long i = 0; + while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i])) + i++; + p->heredoc_line_indent = 0; + } + + if (str) + parser_str_cat(str, ptr, ptr_end - ptr); + else + 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; + } + } while (!whole_match_p(p, eos, len, indent)); } else { - /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/ - newtok(); - if (c == '#') { - int t = parser_peek_variable_name(parser); - if (t) return t; - tokadd('#'); - c = nextc(); - } - do { - pushback(c); - if ((c = tokadd_string(func, '\n', 0, NULL, &enc)) == -1) { - if (parser->eofp) goto error; - goto restore; - } - if (c != '\n') { - set_yylval_str(STR_NEW3(tok(), toklen(), enc, func)); - flush_string_content(enc); - return tSTRING_CONTENT; - } - tokadd(nextc()); - /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/ - if ((c = nextc()) == -1) goto error; - } while (!whole_match_p(eos, len, indent)); - str = STR_NEW3(tok(), toklen(), enc, func); - } - dispatch_heredoc_end(); - heredoc_restore(lex_strterm); - lex_strterm = NEW_STRTERM(-1, 0, 0); + /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/ + newtok(p); + if (c == '#') { + 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; + } + p->heredoc_line_indent = -1; + } + if (t) return t; + tokadd(p, '#'); + c = nextc(p); + } + do { + pushback(p, c); + enc = p->enc; + if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) { + if (p->eofp) goto error; + goto restore; + } + if (c != '\n') { + if (c == '\\') p->heredoc_line_indent = -1; + flush: + str = STR_NEW3(tok(p), toklen(p), enc, func); + flush_str: + set_yylval_str(str); +#ifndef RIPPER + if (bol) nd_set_fl_newline(yylval.node); +#endif + flush_string_content(p, enc, 0); + return tSTRING_CONTENT; + } + tokadd(p, nextc(p)); + if (p->heredoc_indent > 0) { + lex_goto_eol(p); + goto flush; + } + /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/ + if ((c = nextc(p)) == -1) goto error; + } while (!whole_match_p(p, eos, len, indent)); + str = STR_NEW3(tok(p), toklen(p), enc, func); + } + dispatch_heredoc_end(p); + 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 return tSTRING_CONTENT; } #include "lex.c" -static void -arg_ambiguous_gen(struct parser_params *parser, char c) +static int +arg_ambiguous(struct parser_params *p, char c) { #ifndef RIPPER - rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c)); + if (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)); + } #else dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1)); #endif + return TRUE; } -#define arg_ambiguous(c) (arg_ambiguous_gen(parser, (c)), 1) -static ID -formal_argument_gen(struct parser_params *parser, ID lhs) +/* returns true value if formal argument error; + * Qtrue, or error message if ripper */ +static VALUE +formal_argument_error(struct parser_params *p, ID id) { - switch (id_type(lhs)) { + switch (id_type(id)) { case ID_LOCAL: - break; + break; #ifndef RIPPER +# define ERR(mesg) (yyerror0(mesg), Qtrue) +#else +# define ERR(mesg) WARN_S(mesg) +#endif case ID_CONST: - yyerror("formal argument cannot be a constant"); - return 0; + return ERR("formal argument cannot be a constant"); case ID_INSTANCE: - yyerror("formal argument cannot be an instance variable"); - return 0; + return ERR("formal argument cannot be an instance variable"); case ID_GLOBAL: - yyerror("formal argument cannot be a global variable"); - return 0; + return ERR("formal argument cannot be a global variable"); case ID_CLASS: - yyerror("formal argument cannot be a class variable"); - return 0; -#else + return ERR("formal argument cannot be a class variable"); default: - lhs = dispatch1(param_error, lhs); - ripper_error(); - return 0; -#endif + return ERR("formal argument must be local variable"); +#undef ERR } - shadowing_lvar(lhs); - return lhs; + shadowing_lvar(p, id); + + return Qfalse; } static int -lvar_defined_gen(struct parser_params *parser, ID id) +lvar_defined(struct parser_params *p, ID id) { - return (dyna_in_block() && dvar_defined_get(id)) || local_id(id); + return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id); } /* emacsen -*- hack */ static long -parser_encode_length(struct parser_params *parser, const char *name, long len) +parser_encode_length(struct parser_params *p, const char *name, long len) { long nlen; if (len > 5 && name[nlen = len - 5] == '-') { - if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0) - return nlen; + if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0) + return nlen; } if (len > 4 && name[nlen = len - 4] == '-') { - if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0) - return nlen; - if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 && - !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0)) - /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */ - return nlen; + if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0) + return nlen; + if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 && + !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0)) + /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */ + return nlen; } return len; } static void -parser_set_encode(struct parser_params *parser, const char *name) +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) { - excargs[1] = rb_sprintf("unknown encoding name: %s", name); + 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("%s:%d", ruby_sourcefile, ruby_sourceline)); - rb_exc_raise(rb_make_exception(3, excargs)); + 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)); + 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)) { - excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc)); - goto error; + excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc)); + goto error; } - parser->enc = enc; + p->enc = enc; #ifndef RIPPER - if (ruby_debug_lines) { - VALUE lines = ruby_debug_lines; - long i, n = RARRAY_LEN(lines); - for (i = 0; i < n; ++i) { - rb_enc_associate_index(RARRAY_AREF(lines, i), idx); - } + if (p->debug_lines) { + 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 -comment_at_top(struct parser_params *parser) +static bool +comment_at_top(struct parser_params *p) { - const char *p = lex_pbeg, *pend = lex_p - 1; - if (parser->line_count != (parser->has_shebang ? 2 : 1)) return 0; - while (p < pend) { - if (!ISSPACE(*p)) return 0; - p++; - } - 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 *parser, const char *name, long len); -typedef void (*rb_magic_comment_setter_t)(struct parser_params *parser, const char *name, const char *val); +typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len); +typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val); + +static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val); static void -magic_comment_encoding(struct parser_params *parser, const char *name, const char *val) +magic_comment_encoding(struct parser_params *p, const char *name, const char *val) { - if (!comment_at_top(parser)) { - return; + if (!comment_at_top(p)) { + return; } - parser_set_encode(parser, val); + parser_set_encode(p, val); } -#ifndef RIPPER static int -parser_get_bool(struct parser_params *parser, const char *name, const char *val) +parser_get_bool(struct parser_params *p, const char *name, const char *val) { switch (*val) { case 't': case 'T': - if (strcasecmp(val, "true") == 0) { - return TRUE; - } - break; + if (STRCASECMP(val, "true") == 0) { + return TRUE; + } + break; case 'f': case 'F': - if (strcasecmp(val, "false") == 0) { - return FALSE; - } - break; + if (STRCASECMP(val, "false") == 0) { + return FALSE; + } + break; } - rb_compile_warning(ruby_sourcefile, ruby_sourceline, "invalid value for %s: %s", name, val); + return parser_invalid_pragma_value(p, name, val); +} + +static int +parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val) +{ + rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val)); return -1; } static void -parser_set_token_info(struct parser_params *parser, const char *name, const char *val) +parser_set_token_info(struct parser_params *p, const char *name, const char *val) { - int b = parser_get_bool(parser, name, val); - if (b >= 0) parser->token_info_enabled = b; + int b = parser_get_bool(p, name, val); + if (b >= 0) p->token_info_enabled = b; } static void -parser_set_compile_option_flag(struct parser_params *parser, const char *name, const char *val) +parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val) { int b; - if (parser->token_seen) { - rb_warning1("`%s' is ignored after any tokens", WARN_S(name)); - return; + if (p->token_seen) { + rb_warning1("'%s' is ignored after any tokens", WARN_S(name)); + return; } - b = parser_get_bool(parser, name, val); + b = parser_get_bool(p, name, val); if (b < 0) return; - if (!parser->compile_option) - parser->compile_option = rb_ident_hash_new(); - rb_hash_aset(parser->compile_option, ID2SYM(rb_intern(name)), - (b ? Qtrue : Qfalse)); + p->frozen_string_literal = b; +} + +static void +parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val) +{ + 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)); + return; + } + + switch (*val) { + case 'n': case 'N': + if (STRCASECMP(val, "none") == 0) { + 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 = rb_parser_shareable_literal; + return; + } + break; + case 'e': case 'E': + if (STRCASECMP(val, "experimental_copy") == 0) { + p->ctxt.shareable_constant_value = rb_parser_shareable_copy; + return; + } + if (STRCASECMP(val, "experimental_everything") == 0) { + p->ctxt.shareable_constant_value = rb_parser_shareable_everything; + return; + } + break; + } + parser_invalid_pragma_value(p, name, val); } # if WARN_PAST_SCOPE static void -parser_set_past_scope(struct parser_params *parser, const char *name, const char *val) +parser_set_past_scope(struct parser_params *p, const char *name, const char *val) { - int b = parser_get_bool(parser, name, val); - if (b >= 0) parser->past_scope_enabled = b; + int b = parser_get_bool(p, name, val); + if (b >= 0) p->past_scope_enabled = b; } # endif -#endif struct magic_comment { const char *name; @@ -6945,13 +9448,12 @@ struct magic_comment { static const struct magic_comment magic_comments[] = { {"coding", magic_comment_encoding, parser_encode_length}, {"encoding", magic_comment_encoding, parser_encode_length}, -#ifndef RIPPER - {"frozen_string_literal", parser_set_compile_option_flag}, + {"frozen_string_literal", parser_set_frozen_string_literal}, + {"shareable_constant_value", parser_set_shareable_constant_value}, {"warn_indent", parser_set_token_info}, # if WARN_PAST_SCOPE {"warn_past_scope", parser_set_past_scope}, # endif -#endif }; static const char * @@ -6960,131 +9462,135 @@ magic_comment_marker(const char *str, long len) long i = 2; while (i < len) { - switch (str[i]) { - case '-': - if (str[i-1] == '*' && str[i-2] == '-') { - return str + i + 1; - } - i += 2; - break; - case '*': - if (i + 1 >= len) return 0; - if (str[i+1] != '-') { - i += 4; - } - else if (str[i-1] != '-') { - i += 2; - } - else { - return str + i + 2; - } - break; - default: - i += 3; - break; - } + switch (str[i]) { + case '-': + if (str[i-1] == '*' && str[i-2] == '-') { + return str + i + 1; + } + i += 2; + break; + case '*': + if (i + 1 >= len) return 0; + if (str[i+1] != '-') { + i += 4; + } + else if (str[i-1] != '-') { + i += 2; + } + else { + return str + i + 2; + } + break; + default: + i += 3; + break; + } } return 0; } static int -parser_magic_comment(struct parser_params *parser, const char *str, long len) +parser_magic_comment(struct parser_params *p, const char *str, long len) { int indicator = 0; VALUE name = 0, val = 0; const char *beg, *end, *vbeg, *vend; #define str_copy(_s, _p, _n) ((_s) \ - ? (void)(rb_str_resize((_s), (_n)), \ - MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \ - : (void)((_s) = STR_NEW((_p), (_n)))) + ? (void)(rb_str_resize((_s), (_n)), \ + MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \ + : (void)((_s) = STR_NEW((_p), (_n)))) if (len <= 7) return FALSE; if (!!(beg = magic_comment_marker(str, len))) { - if (!(end = magic_comment_marker(beg, str + len - beg))) - return FALSE; - indicator = TRUE; - str = beg; - len = end - beg - 3; + if (!(end = magic_comment_marker(beg, str + len - beg))) + return FALSE; + indicator = TRUE; + str = beg; + len = end - beg - 3; } /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */ while (len > 0) { - const struct magic_comment *p = magic_comments; - char *s; - int i; - long n = 0; - - for (; len > 0 && *str; str++, --len) { - switch (*str) { - case '\'': case '"': case ':': case ';': - continue; - } - if (!ISSPACE(*str)) break; - } - for (beg = str; len > 0; str++, --len) { - switch (*str) { - case '\'': case '"': case ':': case ';': - break; - default: - if (ISSPACE(*str)) break; - continue; - } - break; - } - for (end = str; len > 0 && ISSPACE(*str); str++, --len); - if (!len) break; - if (*str != ':') { - if (!indicator) return FALSE; - continue; - } - - do str++; while (--len > 0 && ISSPACE(*str)); - if (!len) break; - if (*str == '"') { - for (vbeg = ++str; --len > 0 && *str != '"'; str++) { - if (*str == '\\') { - --len; - ++str; - } - } - vend = str; - if (len) { - --len; - ++str; - } - } - else { - for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++); - vend = str; - } - if (indicator) { - while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++; - } - else { - while (len > 0 && (ISSPACE(*str))) --len, str++; - if (len) return FALSE; - } - - n = end - beg; - str_copy(name, beg, n); - s = RSTRING_PTR(name); - for (i = 0; i < n; ++i) { - if (s[i] == '-') s[i] = '_'; - } - do { - if (STRNCASECMP(p->name, s, n) == 0) { - n = vend - vbeg; - if (p->length) { - n = (*p->length)(parser, vbeg, n); - } - str_copy(val, vbeg, n); - (*p->func)(parser, p->name, RSTRING_PTR(val)); - break; - } - } while (++p < magic_comments + numberof(magic_comments)); + const struct magic_comment *mc = magic_comments; + char *s; + int i; + long n = 0; + + for (; len > 0 && *str; str++, --len) { + switch (*str) { + case '\'': case '"': case ':': case ';': + continue; + } + if (!ISSPACE(*str)) break; + } + for (beg = str; len > 0; str++, --len) { + switch (*str) { + case '\'': case '"': case ':': case ';': + break; + default: + if (ISSPACE(*str)) break; + continue; + } + break; + } + for (end = str; len > 0 && ISSPACE(*str); str++, --len); + if (!len) break; + if (*str != ':') { + if (!indicator) return FALSE; + continue; + } + + 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 == '\\') { + --len; + ++str; + } + } + vend = str; + if (len) { + --len; + ++str; + } + } + else { + 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++; + } + else { + while (len > 0 && (ISSPACE(*str))) --len, str++; + if (len) return FALSE; + } + + n = end - beg; + str_copy(name, beg, n); + s = RSTRING_PTR(name); + for (i = 0; i < n; ++i) { + if (s[i] == '-') s[i] = '_'; + } + do { + if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) { + n = vend - vbeg; + if (mc->length) { + 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; + } + } while (++mc < magic_comments + numberof(magic_comments)); #ifdef RIPPER - str_copy(val, vbeg, vend - vbeg); - dispatch2(magic_comment, name, val); + str_copy(val, vbeg, vend - vbeg); + dispatch2(magic_comment, name, val); #endif } @@ -7092,1535 +9598,2895 @@ parser_magic_comment(struct parser_params *parser, const char *str, long len) } static void -set_file_encoding(struct parser_params *parser, const char *str, const char *send) +set_file_encoding(struct parser_params *p, const char *str, const char *send) { int sep = 0; const char *beg = str; VALUE s; for (;;) { - if (send - str <= 6) return; - switch (str[6]) { - case 'C': case 'c': str += 6; continue; - case 'O': case 'o': str += 5; continue; - case 'D': case 'd': str += 4; continue; - case 'I': case 'i': str += 3; continue; - case 'N': case 'n': str += 2; continue; - case 'G': case 'g': str += 1; continue; - case '=': case ':': - sep = 1; - str += 6; - break; - default: - str += 6; - if (ISSPACE(*str)) break; - continue; - } - if (STRNCASECMP(str-6, "coding", 6) == 0) break; + if (send - str <= 6) return; + switch (str[6]) { + case 'C': case 'c': str += 6; continue; + case 'O': case 'o': str += 5; continue; + case 'D': case 'd': str += 4; continue; + case 'I': case 'i': str += 3; continue; + case 'N': case 'n': str += 2; continue; + case 'G': case 'g': str += 1; continue; + case '=': case ':': + sep = 1; + str += 6; + break; + default: + str += 6; + if (ISSPACE(*str)) break; + continue; + } + if (STRNCASECMP(str-6, "coding", 6) == 0) break; + sep = 0; } for (;;) { - do { - if (++str >= send) return; - } while (ISSPACE(*str)); - if (sep) break; - if (*str != '=' && *str != ':') return; - sep = 1; - str++; + do { + if (++str >= send) return; + } while (ISSPACE(*str)); + if (sep) break; + if (*str != '=' && *str != ':') return; + sep = 1; + str++; } beg = str; while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send); - s = rb_str_new(beg, parser_encode_length(parser, beg, str - beg)); - parser_set_encode(parser, RSTRING_PTR(s)); + 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); } static void -parser_prepare(struct parser_params *parser) +parser_prepare(struct parser_params *p) { - int c = nextc(); + int c = nextc0(p, FALSE); + p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose); switch (c) { case '#': - if (peek('!')) parser->has_shebang = 1; - break; + if (peek(p, '!')) p->has_shebang = 1; + break; case 0xef: /* UTF-8 BOM marker */ - if (lex_pend - lex_p >= 2 && - (unsigned char)lex_p[0] == 0xbb && - (unsigned char)lex_p[1] == 0xbf) { - parser->enc = rb_utf8_encoding(); - lex_p += 2; - lex_pbeg = lex_p; - return; - } - break; - case EOF: - return; - } - pushback(c); - parser->enc = rb_enc_get(lex_lastline); + if (!lex_eol_n_p(p, 2) && + (unsigned char)p->lex.pcur[0] == 0xbb && + (unsigned char)p->lex.pcur[1] == 0xbf) { + p->enc = rb_utf8_encoding(); + p->lex.pcur += 2; +#ifndef RIPPER + if (p->debug_lines) { + rb_parser_string_set_encoding(p->lex.lastline, p->enc); + } +#endif + p->lex.pbeg = p->lex.pcur; + token_flush(p); + return; + } + break; + case -1: /* end of script. */ + return; + } + pushback(p, c); + p->enc = rb_parser_str_get_encoding(p->lex.lastline); } -#define IS_ARG() IS_lex_state(EXPR_ARG_ANY) -#define IS_END() IS_lex_state(EXPR_END_ANY) -#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) -#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c)) -#define IS_LABEL_POSSIBLE() (\ - (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \ - IS_ARG()) -#define IS_LABEL_SUFFIX(n) (peek_n(':',(n)) && !peek_n(':', (n)+1)) -#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT) - #ifndef RIPPER -#define ambiguous_operator(op, syn) ( \ - rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \ +#define ambiguous_operator(tok, op, syn) ( \ + 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(op, syn) dispatch2(operator_ambiguous, ripper_intern(op), rb_str_new_cstr(syn)) +#define ambiguous_operator(tok, op, syn) \ + dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn)) #endif -#define warn_balanced(op, syn) ((void) \ - (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN|EXPR_ENDARG) && \ +#define warn_balanced(tok, op, syn) ((void) \ + (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \ space_seen && !ISSPACE(c) && \ - (ambiguous_operator(op, syn), 0))) + (ambiguous_operator(tok, op, syn), 0)), \ + (enum yytokentype)(tok)) -static VALUE -parse_rational(struct parser_params *parser, char *str, int len, int seen_point) +static enum yytokentype +no_digits(struct parser_params *p) { - 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)); + yyerror0("numeric literal without digits"); + if (peek(p, '_')) nextc(p); + /* dummy 0, for tUMINUS_NUM at numeric */ + return set_number_literal(p, tINTEGER, 0, 10, 0); } -static int -parse_numeric(struct parser_params *parser, int c) +static enum yytokentype +parse_numeric(struct parser_params *p, int c) { int is_float, seen_point, seen_e, nondigit; int suffix; is_float = seen_point = seen_e = nondigit = 0; - lex_state = EXPR_END; - newtok(); + SET_LEX_STATE(EXPR_END); + newtok(p); if (c == '-' || c == '+') { - tokadd(c); - c = nextc(); + tokadd(p, c); + c = nextc(p); } if (c == '0') { -#define no_digits() do {yyerror("numeric literal without digits"); return 0;} while (0) - int start = toklen(); - c = nextc(); - if (c == 'x' || c == 'X') { - /* hexadecimal */ - c = nextc(); - if (c != -1 && ISXDIGIT(c)) { - do { - if (c == '_') { - if (nondigit) break; - nondigit = c; - continue; - } - if (!ISXDIGIT(c)) break; - nondigit = 0; - tokadd(c); - } while ((c = nextc()) != -1); - } - pushback(c); - tokfix(); - if (toklen() == start) { - no_digits(); - } - else if (nondigit) goto trailing_uc; - suffix = number_literal_suffix(NUM_SUFFIX_ALL); - return set_integer_literal(rb_cstr_to_inum(tok(), 16, FALSE), suffix); - } - if (c == 'b' || c == 'B') { - /* binary */ - c = nextc(); - if (c == '0' || c == '1') { - do { - if (c == '_') { - if (nondigit) break; - nondigit = c; - continue; - } - if (c != '0' && c != '1') break; - nondigit = 0; - tokadd(c); - } while ((c = nextc()) != -1); - } - pushback(c); - tokfix(); - if (toklen() == start) { - no_digits(); - } - else if (nondigit) goto trailing_uc; - suffix = number_literal_suffix(NUM_SUFFIX_ALL); - return set_integer_literal(rb_cstr_to_inum(tok(), 2, FALSE), suffix); - } - if (c == 'd' || c == 'D') { - /* decimal */ - c = nextc(); - if (c != -1 && ISDIGIT(c)) { - do { - if (c == '_') { - if (nondigit) break; - nondigit = c; - continue; - } - if (!ISDIGIT(c)) break; - nondigit = 0; - tokadd(c); - } while ((c = nextc()) != -1); - } - pushback(c); - tokfix(); - if (toklen() == start) { - no_digits(); - } - else if (nondigit) goto trailing_uc; - suffix = number_literal_suffix(NUM_SUFFIX_ALL); - return set_integer_literal(rb_cstr_to_inum(tok(), 10, FALSE), suffix); - } - if (c == '_') { - /* 0_0 */ - goto octal_number; - } - if (c == 'o' || c == 'O') { - /* prefixed octal */ - c = nextc(); - if (c == -1 || c == '_' || !ISDIGIT(c)) { - no_digits(); - } - } - if (c >= '0' && c <= '7') { - /* octal */ - octal_number: - do { - if (c == '_') { - if (nondigit) break; - nondigit = c; - continue; - } - if (c < '0' || c > '9') break; - if (c > '7') goto invalid_octal; - nondigit = 0; - tokadd(c); - } while ((c = nextc()) != -1); - if (toklen() > start) { - pushback(c); - tokfix(); - if (nondigit) goto trailing_uc; - suffix = number_literal_suffix(NUM_SUFFIX_ALL); - return set_integer_literal(rb_cstr_to_inum(tok(), 8, FALSE), suffix); - } - if (nondigit) { - pushback(c); - goto trailing_uc; - } - } - if (c > '7' && c <= '9') { - invalid_octal: - yyerror("Invalid octal digit"); - } - else if (c == '.' || c == 'e' || c == 'E') { - tokadd('0'); - } - else { - pushback(c); - suffix = number_literal_suffix(NUM_SUFFIX_ALL); - return set_integer_literal(INT2FIX(0), suffix); - } + int start = toklen(p); + c = nextc(p); + if (c == 'x' || c == 'X') { + /* hexadecimal */ + c = nextc(p); + if (c != -1 && ISXDIGIT(c)) { + do { + if (c == '_') { + if (nondigit) break; + nondigit = c; + continue; + } + if (!ISXDIGIT(c)) break; + nondigit = 0; + tokadd(p, c); + } while ((c = nextc(p)) != -1); + } + pushback(p, c); + tokfix(p); + if (toklen(p) == start) { + return no_digits(p); + } + else if (nondigit) goto trailing_uc; + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + return set_number_literal(p, tINTEGER, suffix, 16, 0); + } + if (c == 'b' || c == 'B') { + /* binary */ + c = nextc(p); + if (c == '0' || c == '1') { + do { + if (c == '_') { + if (nondigit) break; + nondigit = c; + continue; + } + if (c != '0' && c != '1') break; + nondigit = 0; + tokadd(p, c); + } while ((c = nextc(p)) != -1); + } + pushback(p, c); + tokfix(p); + if (toklen(p) == start) { + return no_digits(p); + } + else if (nondigit) goto trailing_uc; + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + return set_number_literal(p, tINTEGER, suffix, 2, 0); + } + if (c == 'd' || c == 'D') { + /* decimal */ + c = nextc(p); + if (c != -1 && ISDIGIT(c)) { + do { + if (c == '_') { + if (nondigit) break; + nondigit = c; + continue; + } + if (!ISDIGIT(c)) break; + nondigit = 0; + tokadd(p, c); + } while ((c = nextc(p)) != -1); + } + pushback(p, c); + tokfix(p); + if (toklen(p) == start) { + return no_digits(p); + } + else if (nondigit) goto trailing_uc; + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + return set_number_literal(p, tINTEGER, suffix, 10, 0); + } + if (c == '_') { + /* 0_0 */ + goto octal_number; + } + if (c == 'o' || c == 'O') { + /* prefixed octal */ + c = nextc(p); + if (c == -1 || c == '_' || !ISDIGIT(c)) { + tokfix(p); + return no_digits(p); + } + } + if (c >= '0' && c <= '7') { + /* octal */ + octal_number: + do { + if (c == '_') { + if (nondigit) break; + nondigit = c; + continue; + } + if (c < '0' || c > '9') break; + if (c > '7') goto invalid_octal; + nondigit = 0; + tokadd(p, c); + } while ((c = nextc(p)) != -1); + if (toklen(p) > start) { + pushback(p, c); + tokfix(p); + if (nondigit) goto trailing_uc; + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + return set_number_literal(p, tINTEGER, suffix, 8, 0); + } + if (nondigit) { + pushback(p, c); + goto trailing_uc; + } + } + if (c > '7' && c <= '9') { + invalid_octal: + yyerror0("Invalid octal digit"); + } + else if (c == '.' || c == 'e' || c == 'E') { + tokadd(p, '0'); + } + else { + pushback(p, c); + tokfix(p); + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + return set_number_literal(p, tINTEGER, suffix, 10, 0); + } } for (;;) { - switch (c) { - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - nondigit = 0; - tokadd(c); - break; - - case '.': - if (nondigit) goto trailing_uc; - if (seen_point || seen_e) { - goto decode_num; - } - else { - int c0 = nextc(); - if (c0 == -1 || !ISDIGIT(c0)) { - pushback(c0); - goto decode_num; - } - c = c0; - } - seen_point = toklen(); - tokadd('.'); - tokadd(c); - is_float++; - nondigit = 0; - break; - - case 'e': - case 'E': - if (nondigit) { - pushback(c); - c = nondigit; - goto decode_num; - } - if (seen_e) { - goto decode_num; - } - nondigit = c; - c = nextc(); - if (c != '-' && c != '+' && !ISDIGIT(c)) { - pushback(c); - nondigit = 0; - goto decode_num; - } - tokadd(nondigit); - seen_e++; - is_float++; - tokadd(c); - nondigit = (c == '-' || c == '+') ? c : 0; - break; - - case '_': /* `_' in number just ignored */ - if (nondigit) goto decode_num; - nondigit = c; - break; - - default: - goto decode_num; - } - c = nextc(); + switch (c) { + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + nondigit = 0; + tokadd(p, c); + break; + + case '.': + if (nondigit) goto trailing_uc; + if (seen_point || seen_e) { + goto decode_num; + } + else { + int c0 = nextc(p); + if (c0 == -1 || !ISDIGIT(c0)) { + pushback(p, c0); + goto decode_num; + } + c = c0; + } + seen_point = toklen(p); + tokadd(p, '.'); + tokadd(p, c); + is_float++; + nondigit = 0; + break; + + case 'e': + case 'E': + if (nondigit) { + pushback(p, c); + c = nondigit; + goto decode_num; + } + if (seen_e) { + goto decode_num; + } + nondigit = c; + c = nextc(p); + if (c != '-' && c != '+' && !ISDIGIT(c)) { + pushback(p, c); + c = nondigit; + nondigit = 0; + goto decode_num; + } + tokadd(p, nondigit); + seen_e++; + is_float++; + tokadd(p, c); + nondigit = (c == '-' || c == '+') ? c : 0; + break; + + case '_': /* `_' in number just ignored */ + if (nondigit) goto decode_num; + nondigit = c; + break; + + default: + goto decode_num; + } + c = nextc(p); } decode_num: - pushback(c); + pushback(p, c); if (nondigit) { - char tmp[30]; trailing_uc: - snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit); - yyerror(tmp); + literal_flush(p, p->lex.pcur - 1); + YYLTYPE loc = RUBY_INIT_YYLLOC(); + compile_error(p, "trailing '%c' in number", nondigit); + parser_show_error_line(p, &loc); } - tokfix(); + tokfix(p); if (is_float) { - int type = tFLOAT; - VALUE v; - - suffix = number_literal_suffix(seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL); - if (suffix & NUM_SUFFIX_R) { - type = tRATIONAL; - v = parse_rational(parser, tok(), toklen(), seen_point); - } - else { - double d = strtod(tok(), 0); - if (errno == ERANGE) { - rb_warning1("Float %s out of range", WARN_S(tok())); - errno = 0; - } - v = DBL2NUM(d); - } - return set_number_literal(v, type, suffix); - } - suffix = number_literal_suffix(NUM_SUFFIX_ALL); - return set_integer_literal(rb_cstr_to_inum(tok(), 10, FALSE), suffix); + enum yytokentype type = tFLOAT; + + suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL); + if (suffix & NUM_SUFFIX_R) { + type = tRATIONAL; + } + else { + strtod(tok(p), 0); + if (errno == ERANGE) { + rb_warning1("Float %s out of range", WARN_S(tok(p))); + errno = 0; + } + } + return set_number_literal(p, type, suffix, 0, seen_point); + } + suffix = number_literal_suffix(p, NUM_SUFFIX_ALL); + return set_number_literal(p, tINTEGER, suffix, 10, 0); } -static int -parse_qmark(struct parser_params *parser) +static enum yytokentype +parse_qmark(struct parser_params *p, int space_seen) { rb_encoding *enc; register int c; + rb_parser_string_t *lit; + const char *start = p->lex.pcur; if (IS_END()) { - lex_state = EXPR_VALUE; - return '?'; + SET_LEX_STATE(EXPR_VALUE); + return '?'; } - c = nextc(); + c = nextc(p); if (c == -1) { - compile_error(PARSER_ARG "incomplete character syntax"); - return 0; - } - if (rb_enc_isspace(c, current_enc)) { - if (!IS_ARG()) { - int c2 = 0; - switch (c) { - case ' ': - c2 = 's'; - break; - case '\n': - c2 = 'n'; - break; - case '\t': - c2 = 't'; - break; - case '\v': - c2 = 'v'; - break; - case '\r': - c2 = 'r'; - break; - case '\f': - c2 = 'f'; - break; - } - if (c2) { - rb_warn1("invalid character syntax; use ?\\%c", WARN_I(c2)); - } - } - ternary: - pushback(c); - lex_state = EXPR_VALUE; - return '?'; - } - newtok(); - enc = current_enc; - if (!parser_isascii()) { - if (tokadd_mbchar(c) == -1) return 0; + compile_error(p, "incomplete character syntax"); + return 0; } - else if ((rb_enc_isalnum(c, current_enc) || c == '_') && - lex_p < lex_pend && is_identchar(lex_p, lex_pend, current_enc)) { - goto ternary; + if (rb_enc_isspace(c, p->enc)) { + if (!IS_ARG()) { + int c2 = escaped_control_code(c); + if (c2) { + WARN_SPACE_CHAR(c2, "?"); + } + } + ternary: + pushback(p, c); + SET_LEX_STATE(EXPR_VALUE); + return '?'; + } + newtok(p); + enc = 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 *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 '?'", + WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start))); + } + goto ternary; } else if (c == '\\') { - if (peek('u')) { - nextc(); - c = parser_tokadd_utf8(parser, &enc, 0, 0, 0); - if (0x80 <= c) { - tokaddmbc(c, enc); - } - else { - tokadd(c); - } - } - else if (!lex_eol_p() && !(c = *lex_p, ISASCII(c))) { - nextc(); - if (tokadd_mbchar(c) == -1) return 0; - } - else { - c = read_escape(0, &enc); - tokadd(c); - } + if (peek(p, 'u')) { + nextc(p); + enc = rb_utf8_encoding(); + tokadd_utf8(p, &enc, -1, 0, 0); + } + else if (!ISASCII(c = peekc(p)) && c != -1) { + nextc(p); + if (tokadd_mbchar(p, c) == -1) return 0; + } + else { + c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\")); + tokadd(p, c); + } } else { - tokadd(c); + if (tokadd_mbchar(p, c) == -1) return 0; } - tokfix(); - set_yylval_str(STR_NEW3(tok(), toklen(), enc, 0)); - lex_state = EXPR_END; + tokfix(p); + lit = STR_NEW3(tok(p), toklen(p), enc, 0); + set_yylval_str(lit); + SET_LEX_STATE(EXPR_END); return tCHAR; } -static int -parse_percent(struct parser_params *parser, const int space_seen, const enum lex_state_e last_state) +static enum yytokentype +parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state) { register int c; + const char *ptok = p->lex.pcur; - if (IS_lex_state(EXPR_BEG_ANY)) { - int term; - int paren; + if (IS_BEG()) { + int term; + int paren; - c = nextc(); + c = nextc(p); quotation: - if (c == -1 || !ISALNUM(c)) { - term = c; - c = 'Q'; - } - else { - term = nextc(); - if (rb_enc_isalnum(term, current_enc) || !parser_isascii()) { - yyerror("unknown type of %string"); - return 0; - } - } - if (c == -1 || term == -1) { - compile_error(PARSER_ARG "unterminated quoted string meets end of file"); - return 0; - } - paren = term; - if (term == '(') term = ')'; - else if (term == '[') term = ']'; - else if (term == '{') term = '}'; - else if (term == '<') term = '>'; - else paren = 0; - - switch (c) { - case 'Q': - lex_strterm = NEW_STRTERM(str_dquote, term, paren); - return tSTRING_BEG; - - case 'q': - lex_strterm = NEW_STRTERM(str_squote, term, paren); - return tSTRING_BEG; - - case 'W': - lex_strterm = NEW_STRTERM(str_dword, term, paren); - do {c = nextc();} while (ISSPACE(c)); - pushback(c); - return tWORDS_BEG; - - case 'w': - lex_strterm = NEW_STRTERM(str_sword, term, paren); - do {c = nextc();} while (ISSPACE(c)); - pushback(c); - return tQWORDS_BEG; - - case 'I': - lex_strterm = NEW_STRTERM(str_dword, term, paren); - do {c = nextc();} while (ISSPACE(c)); - pushback(c); - return tSYMBOLS_BEG; - - case 'i': - lex_strterm = NEW_STRTERM(str_sword, term, paren); - do {c = nextc();} while (ISSPACE(c)); - pushback(c); - return tQSYMBOLS_BEG; - - case 'x': - lex_strterm = NEW_STRTERM(str_xquote, term, paren); - return tXSTRING_BEG; - - case 'r': - lex_strterm = NEW_STRTERM(str_regexp, term, paren); - return tREGEXP_BEG; - - case 's': - lex_strterm = NEW_STRTERM(str_ssym, term, paren); - lex_state = EXPR_FNAME; - return tSYMBEG; - - default: - yyerror("unknown type of %string"); - return 0; - } - } - if ((c = nextc()) == '=') { - set_yylval_id('%'); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - if (IS_SPCARG(c)) { - goto quotation; - } - lex_state = IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG; - pushback(c); - warn_balanced("%%", "string literal"); - return '%'; + if (c == -1) goto unterminated; + if (!ISALNUM(c)) { + term = c; + if (!ISASCII(c)) goto unknown; + c = 'Q'; + } + else { + term = nextc(p); + if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) { + unknown: + pushback(p, term); + c = parser_precise_mbclen(p, p->lex.pcur); + if (c < 0) return 0; + p->lex.pcur += c; + yyerror0("unknown type of %string"); + return 0; + } + } + if (term == -1) { + unterminated: + compile_error(p, "unterminated quoted string meets end of file"); + return 0; + } + paren = term; + if (term == '(') term = ')'; + else if (term == '[') term = ']'; + else if (term == '{') term = '}'; + else if (term == '<') term = '>'; + else paren = 0; + + p->lex.ptok = ptok-1; + switch (c) { + case 'Q': + p->lex.strterm = NEW_STRTERM(str_dquote, term, paren); + return tSTRING_BEG; + + case 'q': + p->lex.strterm = NEW_STRTERM(str_squote, term, paren); + return tSTRING_BEG; + + case 'W': + p->lex.strterm = NEW_STRTERM(str_dword, term, paren); + return tWORDS_BEG; + + case 'w': + p->lex.strterm = NEW_STRTERM(str_sword, term, paren); + return tQWORDS_BEG; + + case 'I': + p->lex.strterm = NEW_STRTERM(str_dword, term, paren); + return tSYMBOLS_BEG; + + case 'i': + p->lex.strterm = NEW_STRTERM(str_sword, term, paren); + return tQSYMBOLS_BEG; + + case 'x': + p->lex.strterm = NEW_STRTERM(str_xquote, term, paren); + return tXSTRING_BEG; + + case 'r': + p->lex.strterm = NEW_STRTERM(str_regexp, term, paren); + return tREGEXP_BEG; + + case 's': + p->lex.strterm = NEW_STRTERM(str_ssym, term, paren); + SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM); + return tSYMBEG; + + default: + yyerror0("unknown type of %string"); + return 0; + } + } + if ((c = nextc(p)) == '=') { + set_yylval_id('%'); + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) { + goto quotation; + } + SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); + pushback(p, c); + return warn_balanced('%', "%%", "string literal"); } static int -tokadd_ident(struct parser_params *parser, int c) +tokadd_ident(struct parser_params *p, int c) { do { - if (tokadd_mbchar(c) == -1) return -1; - c = nextc(); - } while (parser_is_identchar()); - pushback(c); + if (tokadd_mbchar(p, c) == -1) return -1; + c = nextc(p); + } while (parser_is_identchar(p)); + pushback(p, c); return 0; } -static void -tokenize_ident(struct parser_params *parser, const enum lex_state_e last_state) +static ID +tokenize_ident(struct parser_params *p) { ID ident = TOK_INTERN(); set_yylval_name(ident); - if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) && - is_local_id(ident) && lvar_defined(ident)) { - lex_state = EXPR_END; - } + + return ident; } static int -parse_numvar(struct parser_params *parser) +parse_numvar(struct parser_params *p) { size_t len; int overflow; - unsigned long n = ruby_scan_digits(tok()+1, toklen()-1, 10, &len, &overflow); + unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow); const unsigned long nth_ref_max = - (FIXNUM_MAX / 2 < INT_MAX) ? FIXNUM_MAX / 2 : INT_MAX; + ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1; /* NTH_REF is left-shifted to be ORed with back-ref flag and * turned into a Fixnum, in compile.c */ if (overflow || n > nth_ref_max) { - /* compile_error()? */ - rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok())); - return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */ + /* compile_error()? */ + 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 { - return (int)n; + return (int)n; } } -static int -parse_gvar(struct parser_params *parser, const enum lex_state_e last_state) +static enum yytokentype +parse_gvar(struct parser_params *p, const enum lex_state_e last_state) { + const char *ptr = p->lex.pcur; register int c; - lex_state = EXPR_END; - newtok(); - c = nextc(); + SET_LEX_STATE(EXPR_END); + p->lex.ptok = ptr - 1; /* from '$' */ + newtok(p); + c = nextc(p); switch (c) { case '_': /* $_: last read line string */ - c = nextc(); - if (parser_is_identchar()) { - tokadd('$'); - tokadd('_'); - break; - } - pushback(c); - c = '_'; - /* fall through */ - case '~': /* $~: match-data */ - case '*': /* $*: argv */ - case '$': /* $$: pid */ - case '?': /* $?: last status */ - case '!': /* $!: error string */ - case '@': /* $@: error position */ - case '/': /* $/: input record separator */ - case '\\': /* $\: output record separator */ - case ';': /* $;: field separator */ - case ',': /* $,: output field separator */ - case '.': /* $.: last read line number */ - case '=': /* $=: ignorecase */ - case ':': /* $:: load path */ - case '<': /* $<: reading filename */ - case '>': /* $>: default output handle */ - case '\"': /* $": already loaded files */ - tokadd('$'); - tokadd(c); - goto gvar; + c = nextc(p); + if (parser_is_identchar(p)) { + tokadd(p, '$'); + tokadd(p, '_'); + break; + } + pushback(p, c); + c = '_'; + /* fall through */ + case '~': /* $~: match-data */ + case '*': /* $*: argv */ + case '$': /* $$: pid */ + case '?': /* $?: last status */ + case '!': /* $!: error string */ + case '@': /* $@: error position */ + case '/': /* $/: input record separator */ + case '\\': /* $\: output record separator */ + case ';': /* $;: field separator */ + case ',': /* $,: output field separator */ + case '.': /* $.: last read line number */ + case '=': /* $=: ignorecase */ + case ':': /* $:: load path */ + case '<': /* $<: default input handle */ + case '>': /* $>: default output handle */ + case '\"': /* $": already loaded files */ + tokadd(p, '$'); + tokadd(p, c); + goto gvar; case '-': - tokadd('$'); - tokadd(c); - c = nextc(); - if (parser_is_identchar()) { - if (tokadd_mbchar(c) == -1) return 0; - } - else { - pushback(c); - pushback('-'); - return '$'; - } + tokadd(p, '$'); + tokadd(p, c); + c = nextc(p); + if (parser_is_identchar(p)) { + if (tokadd_mbchar(p, c) == -1) return 0; + } + else { + pushback(p, c); + pushback(p, '-'); + return '$'; + } gvar: - set_yylval_name(TOK_INTERN()); - return tGVAR; - - case '&': /* $&: last match */ - case '`': /* $`: string before last match */ - case '\'': /* $': string after last match */ - case '+': /* $+: string matches last paren. */ - if (IS_lex_state_for(last_state, EXPR_FNAME)) { - tokadd('$'); - tokadd(c); - goto gvar; - } - set_yylval_node(NEW_BACK_REF(c)); - return tBACK_REF; + tokenize_ident(p); + return tGVAR; + + case '&': /* $&: last match */ + case '`': /* $`: string before last match */ + case '\'': /* $': string after last match */ + case '+': /* $+: string matches last paren. */ + if (IS_lex_state_for(last_state, EXPR_FNAME)) { + tokadd(p, '$'); + tokadd(p, c); + goto gvar; + } + set_yylval_node(NEW_BACK_REF(c, &_cur_loc)); + return tBACK_REF; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - tokadd('$'); - do { - tokadd(c); - c = nextc(); - } while (c != -1 && ISDIGIT(c)); - pushback(c); - if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar; - tokfix(); - set_yylval_node(NEW_NTH_REF(parse_numvar(parser))); - return tNTH_REF; + tokadd(p, '$'); + do { + tokadd(p, c); + c = nextc(p); + } while (c != -1 && ISDIGIT(c)); + pushback(p, c); + if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar; + tokfix(p); + c = parse_numvar(p); + set_yylval_node(NEW_NTH_REF(c, &_cur_loc)); + return tNTH_REF; default: - if (!parser_is_identchar()) { - if (c == -1 || ISSPACE(c)) { - compile_error(PARSER_ARG "`$' without identifiers is not allowed as a global variable name"); - } - else { - pushback(c); - compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c); - } - return 0; - } + 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"); + } + else { + pushback(p, c); + compile_error(p, "'$%c' is not allowed as a global variable name", c); + } + parser_show_error_line(p, &loc); + set_yylval_noname(); + return tGVAR; + } + /* fall through */ case '0': - tokadd('$'); + tokadd(p, '$'); } - if (tokadd_ident(parser, c)) return 0; - lex_state = EXPR_END; - tokenize_ident(parser, last_state); + if (tokadd_ident(p, c)) return 0; + SET_LEX_STATE(EXPR_END); + if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) { + tokenize_ident(p); + } + else { + compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p)); + set_yylval_noname(); + } return tGVAR; } -static int -parse_atmark(struct parser_params *parser, const enum lex_state_e last_state) +static bool +parser_numbered_param(struct parser_params *p, int n) +{ + if (n < 0) return false; + + if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) { + return false; + } + if (p->max_numparam == ORDINAL_PARAM) { + compile_error(p, "ordinary parameter is defined"); + return false; + } + struct vtable *args = p->lvtbl->args; + if (p->max_numparam < n) { + p->max_numparam = n; + } + while (n > args->pos) { + vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1)); + } + return true; +} + +static enum yytokentype +parse_atmark(struct parser_params *p, const enum lex_state_e last_state) { - int result = tIVAR; - register int c = nextc(); + const char *ptr = p->lex.pcur; + enum yytokentype result = tIVAR; + register int c = nextc(p); + YYLTYPE loc; - newtok(); - tokadd('@'); + p->lex.ptok = ptr - 1; /* from '@' */ + newtok(p); + tokadd(p, '@'); if (c == '@') { - result = tCVAR; - tokadd('@'); - c = nextc(); - } - if (c == -1 || ISSPACE(c)) { - if (result == tIVAR) { - compile_error(PARSER_ARG "`@' without identifiers is not allowed as an instance variable name"); - } - else { - compile_error(PARSER_ARG "`@@' without identifiers is not allowed as a class variable name"); - } - return 0; - } - else if (ISDIGIT(c) || !parser_is_identchar()) { - pushback(c); - if (result == tIVAR) { - compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c); - } - else { - compile_error(PARSER_ARG "`@@%c' is not allowed as a class variable name", c); - } - return 0; - } - - if (tokadd_ident(parser, c)) return 0; - lex_state = EXPR_END; - tokenize_ident(parser, last_state); + result = tCVAR; + tokadd(p, '@'); + c = nextc(p); + } + SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END); + if (c == -1 || !parser_is_identchar(p)) { + pushback(p, c); + RUBY_SET_YYLLOC(loc); + if (result == tIVAR) { + 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"); + } + parser_show_error_line(p, &loc); + set_yylval_noname(); + SET_LEX_STATE(EXPR_END); + return result; + } + else if (ISDIGIT(c)) { + pushback(p, c); + RUBY_SET_YYLLOC(loc); + if (result == tIVAR) { + 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); + } + parser_show_error_line(p, &loc); + set_yylval_noname(); + SET_LEX_STATE(EXPR_END); + return result; + } + + if (tokadd_ident(p, c)) return 0; + tokenize_ident(p); return result; } -static int -parse_ident(struct parser_params *parser, int c, int cmd_state) +static enum yytokentype +parse_ident(struct parser_params *p, int c, int cmd_state) { - int result = 0; - int mb = ENC_CODERANGE_7BIT; - const enum lex_state_e last_state = lex_state; + enum yytokentype result; + 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 (tokadd_mbchar(c) == -1) return 0; - c = nextc(); - } while (parser_is_identchar()); - if ((c == '!' || c == '?') && !peek('=')) { - tokadd(c); + if (!ISASCII(c)) is_ascii = false; + if (tokadd_mbchar(p, c) == -1) return 0; + c = nextc(p); + } while (parser_is_identchar(p)); + if ((c == '!' || c == '?') && !peek(p, '=')) { + result = tFID; + tokadd(p, c); + } + else if (c == '=' && IS_lex_state(EXPR_FNAME) && + (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) { + result = tIDENTIFIER; + tokadd(p, c); } else { - pushback(c); + result = tCONSTANT; /* assume provisionally */ + pushback(p, c); } - tokfix(); + tokfix(p); - if (toklast() == '!' || toklast() == '?') { - result = tFID; + if (IS_LABEL_POSSIBLE()) { + if (IS_LABEL_SUFFIX(0)) { + SET_LEX_STATE(EXPR_ARG|EXPR_LABELED); + nextc(p); + tokenize_ident(p); + return tLABEL; + } } - else { - if (IS_lex_state(EXPR_FNAME)) { - register int c = nextc(); - if (c == '=' && !peek('~') && !peek('>') && - (!peek('=') || (peek_n('>', 1)))) { - result = tIDENTIFIER; - tokadd(c); - tokfix(); - } - else { - pushback(c); - } - } - if (result == 0 && ISUPPER(tok()[0])) { - result = tCONSTANT; - } - else { - result = tIDENTIFIER; - } + +#ifndef RIPPER + 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_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", + p->ruby_sourceline, beg_pos, lineno, column); + } + + if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) { + const struct kwtable *kw; + + if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) { + if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n"); + enforce_keyword_end = 1; + } + } } +#endif - if (IS_LABEL_POSSIBLE()) { - if (IS_LABEL_SUFFIX(0)) { - lex_state = EXPR_ARG|EXPR_LABELED; - nextc(); - set_yylval_name(TOK_INTERN()); - return tLABEL; - } - } - if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) { - const struct kwtable *kw; - - /* See if it is a reserved word. */ - kw = rb_reserved_word(tok(), toklen()); - if (kw) { - enum lex_state_e state = lex_state; - lex_state = kw->state; - if (IS_lex_state_for(state, EXPR_FNAME)) { - set_yylval_name(rb_intern2(tok(), toklen())); - return kw->id[0]; - } - if (IS_lex_state(EXPR_BEG)) { - command_start = TRUE; - } - if (kw->id[0] == keyword_do) { - if (lpar_beg && lpar_beg == paren_nest) { - lpar_beg = 0; - --paren_nest; - return keyword_do_LAMBDA; - } - if (COND_P()) return keyword_do_cond; - if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG)) - return keyword_do_block; - if (IS_lex_state_for(state, (EXPR_BEG | EXPR_ENDARG))) - return keyword_do_block; - return keyword_do; - } - if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED))) - return kw->id[0]; - else { - if (kw->id[0] != kw->id[1]) - lex_state = EXPR_BEG | EXPR_LABEL; - return kw->id[1]; - } - } + if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) { + const struct kwtable *kw; + + /* See if it is a reserved word. */ + kw = rb_reserved_word(tok(p), toklen(p)); + if (kw) { + enum lex_state_e state = p->lex.state; + if (IS_lex_state_for(state, EXPR_FNAME)) { + SET_LEX_STATE(EXPR_ENDFN); + set_yylval_name(rb_intern2(tok(p), toklen(p))); + return kw->id[0]; + } + SET_LEX_STATE(kw->state); + if (IS_lex_state(EXPR_BEG)) { + p->command_start = TRUE; + } + if (kw->id[0] == keyword_do) { + if (lambda_beginning_p()) { + p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */ + return keyword_do_LAMBDA; + } + if (COND_P()) return keyword_do_cond; + if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG)) + return keyword_do_block; + return keyword_do; + } + if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS))) + return kw->id[0]; + else { + if (kw->id[0] != kw->id[1]) + SET_LEX_STATE(EXPR_BEG | EXPR_LABEL); + return kw->id[1]; + } + } } if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) { - if (cmd_state) { - lex_state = EXPR_CMDARG; - } - else { - lex_state = EXPR_ARG; - } + if (cmd_state) { + SET_LEX_STATE(EXPR_CMDARG); + } + else { + SET_LEX_STATE(EXPR_ARG); + } } - else if (lex_state == EXPR_FNAME) { - lex_state = EXPR_ENDFN; + else if (p->lex.state == EXPR_FNAME) { + SET_LEX_STATE(EXPR_ENDFN); } else { - lex_state = EXPR_END; + SET_LEX_STATE(EXPR_END); } - tokenize_ident(parser, last_state); + ident = tokenize_ident(p); + if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER; + if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) && + (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */ + (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) { + SET_LEX_STATE(EXPR_END|EXPR_LABEL); + } return result; } -static int -parser_yylex(struct parser_params *parser) +static void +warn_cr(struct parser_params *p) +{ + if (!p->cr_seen) { + p->cr_seen = TRUE; + /* carried over with p->lex.nextline for nextc() */ + rb_warn0("encountered \\r in middle of line, treated as a mere space"); + } +} + +static enum yytokentype +parser_yylex(struct parser_params *p) { register int c; int space_seen = 0; int cmd_state; int label; enum lex_state_e last_state; -#ifdef RIPPER int fallthru = FALSE; -#else - int token_seen = parser->token_seen; -#endif + int token_seen = p->token_seen; - if (lex_strterm) { - int token; - if (nd_type(lex_strterm) == NODE_HEREDOC) { - token = here_document(lex_strterm); - if (token == tSTRING_END) { - lex_strterm = 0; - lex_state = EXPR_END; - } - } - else { - token = parse_string(lex_strterm); - if ((token == tSTRING_END) && (lex_strterm->nd_func & STR_FUNC_LABEL)) { - if (((IS_lex_state(EXPR_BEG | EXPR_ENDFN) && !COND_P()) || IS_ARG()) && - IS_LABEL_SUFFIX(0)) { - nextc(); - token = tLABEL_END; - } - } - if (token == tSTRING_END || token == tREGEXP_END || token == tLABEL_END) { - rb_gc_force_recycle((VALUE)lex_strterm); - lex_strterm = 0; - lex_state = token == tLABEL_END ? EXPR_BEG|EXPR_LABEL : EXPR_END; - } - } - return token; - } - cmd_state = command_start; - command_start = FALSE; + if (p->lex.strterm) { + if (strterm_is_heredoc(p->lex.strterm)) { + token_flush(p); + return here_document(p, &p->lex.strterm->u.heredoc); + } + else { + token_flush(p); + return parse_string(p, &p->lex.strterm->u.literal); + } + } + cmd_state = p->command_start; + p->command_start = FALSE; + p->token_seen = TRUE; #ifndef RIPPER - parser->token_seen = TRUE; + token_flush(p); #endif retry: - last_state = lex_state; - switch (c = nextc()) { + last_state = p->lex.state; + switch (c = nextc(p)) { case '\0': /* NUL */ case '\004': /* ^D */ case '\032': /* ^Z */ case -1: /* end of script. */ - return 0; - - /* white spaces */ - case ' ': case '\t': case '\f': case '\r': + p->eofp = 1; +#ifndef RIPPER + if (p->end_expect_token_locations) { + pop_end_expect_token_locations(p); + RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc); + return tDUMNY_END; + } +#endif + /* Set location for end-of-input because dispatch_scan_event is not called. */ + RUBY_SET_YYLLOC(*p->yylloc); + return END_OF_INPUT; + + /* white spaces */ + case '\r': + warn_cr(p); + /* fall through */ + case ' ': case '\t': case '\f': case '\13': /* '\v' */ - space_seen = 1; -#ifdef RIPPER - while ((c = nextc())) { - switch (c) { - case ' ': case '\t': case '\f': case '\r': - case '\13': /* '\v' */ - break; - default: - goto outofloop; - } - } + space_seen = 1; + while ((c = nextc(p))) { + switch (c) { + case '\r': + warn_cr(p); + /* fall through */ + case ' ': case '\t': case '\f': + case '\13': /* '\v' */ + break; + default: + goto outofloop; + } + } outofloop: - pushback(c); - ripper_dispatch_scan_event(parser, tSP); + pushback(p, c); + dispatch_scan_event(p, tSP); +#ifndef RIPPER + token_flush(p); #endif - goto retry; + goto retry; case '#': /* it's a comment */ -#ifndef RIPPER - parser->token_seen = token_seen; -#endif - /* no magic_comment in shebang line */ - if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) { - if (comment_at_top(parser)) { - set_file_encoding(parser, lex_p, lex_pend); - } - } - lex_p = lex_pend; -#ifdef RIPPER - ripper_dispatch_scan_event(parser, tCOMMENT); + 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; -#endif - /* fall through */ + /* fall through */ case '\n': -#ifndef RIPPER - parser->token_seen = token_seen; -#endif - 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)) { -#ifdef RIPPER + p->token_seen = token_seen; + 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)) { if (!fallthru) { - ripper_dispatch_scan_event(parser, tIGNORED_NL); + dispatch_scan_event(p, tIGNORED_NL); } fallthru = FALSE; -#endif - if (!c && parser->in_kwarg) { - goto normal_newline; - } - goto retry; - } - while ((c = nextc())) { - switch (c) { - case ' ': case '\t': case '\f': case '\r': - case '\13': /* '\v' */ - space_seen = 1; - break; - case '.': { -#ifdef RIPPER - ripper_dispatch_delayed_token(parser, tIGNORED_NL); -#endif - if ((c = nextc()) != '.') { - pushback(c); - pushback('.'); -#ifdef RIPPER - ripper_dispatch_scan_event(parser, tSP); -#endif - goto retry; - } - } - default: - --ruby_sourceline; - lex_nextline = lex_lastline; - case -1: /* EOF no decrement*/ - lex_goto_eol(parser); -#ifdef RIPPER - if (c != -1) { - parser->tokp = lex_p; - } -#endif - goto normal_newline; - } - } + if (!c && p->ctxt.in_kwarg) { + goto normal_newline; + } + goto retry; + } + while (1) { + switch (c = nextc(p)) { + case ' ': case '\t': case '\f': case '\r': + case '\13': /* '\v' */ + space_seen = 1; + break; + case '#': + pushback(p, c); + if (space_seen) { + dispatch_scan_event(p, tSP); + 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 == '&')) { + pushback(p, c); + dispatch_scan_event(p, tSP); + 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); + RUBY_SET_YYLLOC(*p->yylloc); + } + goto normal_newline; + } + } normal_newline: - command_start = TRUE; - lex_state = EXPR_BEG; - return '\n'; + p->command_start = TRUE; + SET_LEX_STATE(EXPR_BEG); + return '\n'; case '*': - if ((c = nextc()) == '*') { - if ((c = nextc()) == '=') { - set_yylval_id(tPOW); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - pushback(c); - if (IS_SPCARG(c)) { - rb_warning0("`**' interpreted as argument prefix"); - c = tDSTAR; - } - else if (IS_BEG()) { - c = tDSTAR; - } - else { - warn_balanced("**", "argument prefix"); - c = tPOW; - } - } - else { - if (c == '=') { + if ((c = nextc(p)) == '*') { + if ((c = nextc(p)) == '=') { + set_yylval_id(idPow); + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + pushback(p, c); + if (IS_SPCARG(c)) { + rb_warning0("'**' interpreted as argument prefix"); + c = tDSTAR; + } + else if (IS_BEG()) { + c = tDSTAR; + } + else { + c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix"); + } + } + else { + if (c == '=') { set_yylval_id('*'); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - pushback(c); - if (IS_SPCARG(c)) { - rb_warning0("`*' interpreted as argument prefix"); - c = tSTAR; - } - else if (IS_BEG()) { - c = tSTAR; - } - else { - warn_balanced("*", "argument prefix"); - c = '*'; - } - } - lex_state = IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG; - return c; + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + pushback(p, c); + if (IS_SPCARG(c)) { + rb_warning0("'*' interpreted as argument prefix"); + c = tSTAR; + } + else if (IS_BEG()) { + c = tSTAR; + } + else { + c = warn_balanced('*', "*", "argument prefix"); + } + } + SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); + return c; case '!': - c = nextc(); - if (IS_AFTER_OPERATOR()) { - lex_state = EXPR_ARG; - if (c == '@') { - return '!'; - } - } - else { - lex_state = EXPR_BEG; - } - if (c == '=') { - return tNEQ; - } - if (c == '~') { - return tNMATCH; - } - pushback(c); - return '!'; + c = nextc(p); + if (IS_AFTER_OPERATOR()) { + SET_LEX_STATE(EXPR_ARG); + if (c == '@') { + return '!'; + } + } + else { + SET_LEX_STATE(EXPR_BEG); + } + if (c == '=') { + return tNEQ; + } + if (c == '~') { + return tNMATCH; + } + pushback(p, c); + return '!'; case '=': - if (was_bol()) { - /* skip embedded rd document */ - if (strncmp(lex_p, "begin", 5) == 0 && ISSPACE(lex_p[5])) { -#ifdef RIPPER + if (was_bol(p)) { + /* skip embedded rd document */ + if (word_match_p(p, "begin", 5)) { int first_p = TRUE; - lex_goto_eol(parser); - ripper_dispatch_scan_event(parser, tEMBDOC_BEG); -#endif - for (;;) { - lex_goto_eol(parser); -#ifdef RIPPER + lex_goto_eol(p); + dispatch_scan_event(p, tEMBDOC_BEG); + for (;;) { + lex_goto_eol(p); if (!first_p) { - ripper_dispatch_scan_event(parser, tEMBDOC); + dispatch_scan_event(p, tEMBDOC); } first_p = FALSE; -#endif - c = nextc(); - if (c == -1) { - compile_error(PARSER_ARG "embedded document meets end of file"); - return 0; - } - if (c != '=') continue; - if (strncmp(lex_p, "end", 3) == 0 && - (lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) { - break; - } - } - lex_goto_eol(parser); -#ifdef RIPPER - ripper_dispatch_scan_event(parser, tEMBDOC_END); -#endif - goto retry; - } - } - - lex_state = IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG; - if ((c = nextc()) == '=') { - if ((c = nextc()) == '=') { - return tEQQ; - } - pushback(c); - return tEQ; - } - if (c == '~') { - return tMATCH; - } - else if (c == '>') { - return tASSOC; - } - pushback(c); - return '='; + c = nextc(p); + if (c == -1) { + compile_error(p, "embedded document meets end of file"); + return END_OF_INPUT; + } + if (c == '=' && word_match_p(p, "end", 3)) { + break; + } + pushback(p, c); + } + lex_goto_eol(p); + dispatch_scan_event(p, tEMBDOC_END); + goto retry; + } + } + + SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); + if ((c = nextc(p)) == '=') { + if ((c = nextc(p)) == '=') { + return tEQQ; + } + pushback(p, c); + return tEQ; + } + if (c == '~') { + return tMATCH; + } + else if (c == '>') { + return tASSOC; + } + pushback(p, c); + return '='; case '<': - last_state = lex_state; - c = nextc(); - if (c == '<' && - !IS_lex_state(EXPR_DOT | EXPR_CLASS) && - !IS_END() && - (!IS_ARG() || space_seen)) { - int token = heredoc_identifier(); - if (token) return token; - } - if (IS_AFTER_OPERATOR()) { - lex_state = EXPR_ARG; - } - else { - if (IS_lex_state(EXPR_CLASS)) - command_start = TRUE; - lex_state = EXPR_BEG; - } - if (c == '=') { - if ((c = nextc()) == '>') { - return tCMP; - } - pushback(c); - return tLEQ; - } - if (c == '<') { - if ((c = nextc()) == '=') { - set_yylval_id(tLSHFT); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - pushback(c); - warn_balanced("<<", "here document"); - return tLSHFT; - } - pushback(c); - return '<'; + c = nextc(p); + if (c == '<' && + !IS_lex_state(EXPR_DOT | EXPR_CLASS) && + !IS_END() && + (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) { + enum yytokentype token = heredoc_identifier(p); + if (token) return token < 0 ? 0 : token; + } + if (IS_AFTER_OPERATOR()) { + SET_LEX_STATE(EXPR_ARG); + } + else { + if (IS_lex_state(EXPR_CLASS)) + p->command_start = TRUE; + SET_LEX_STATE(EXPR_BEG); + } + if (c == '=') { + if ((c = nextc(p)) == '>') { + return tCMP; + } + pushback(p, c); + return tLEQ; + } + if (c == '<') { + if ((c = nextc(p)) == '=') { + set_yylval_id(idLTLT); + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + pushback(p, c); + return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document"); + } + pushback(p, c); + return '<'; case '>': - lex_state = IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG; - if ((c = nextc()) == '=') { - return tGEQ; - } - if (c == '>') { - if ((c = nextc()) == '=') { - set_yylval_id(tRSHFT); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - pushback(c); - return tRSHFT; - } - pushback(c); - return '>'; + SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); + if ((c = nextc(p)) == '=') { + return tGEQ; + } + if (c == '>') { + if ((c = nextc(p)) == '=') { + set_yylval_id(idGTGT); + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + pushback(p, c); + return tRSHFT; + } + pushback(p, c); + return '>'; case '"': - label = (IS_LABEL_POSSIBLE() ? str_label : 0); - lex_strterm = NEW_STRTERM(str_dquote | label, '"', 0); - return tSTRING_BEG; + label = (IS_LABEL_POSSIBLE() ? str_label : 0); + p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0); + p->lex.ptok = p->lex.pcur-1; + return tSTRING_BEG; case '`': - if (IS_lex_state(EXPR_FNAME)) { - lex_state = EXPR_ENDFN; - return c; - } - if (IS_lex_state(EXPR_DOT)) { - if (cmd_state) - lex_state = EXPR_CMDARG; - else - lex_state = EXPR_ARG; - return c; - } - lex_strterm = NEW_STRTERM(str_xquote, '`', 0); - return tXSTRING_BEG; + if (IS_lex_state(EXPR_FNAME)) { + SET_LEX_STATE(EXPR_ENDFN); + return c; + } + if (IS_lex_state(EXPR_DOT)) { + if (cmd_state) + SET_LEX_STATE(EXPR_CMDARG); + else + SET_LEX_STATE(EXPR_ARG); + return c; + } + p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0); + return tXSTRING_BEG; case '\'': - label = (IS_LABEL_POSSIBLE() ? str_label : 0); - lex_strterm = NEW_STRTERM(str_squote | label, '\'', 0); - return tSTRING_BEG; + label = (IS_LABEL_POSSIBLE() ? str_label : 0); + p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0); + p->lex.ptok = p->lex.pcur-1; + return tSTRING_BEG; case '?': - return parse_qmark(parser); + return parse_qmark(p, space_seen); case '&': - if ((c = nextc()) == '&') { - lex_state = EXPR_BEG; - if ((c = nextc()) == '=') { - set_yylval_id(tANDOP); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - pushback(c); - return tANDOP; - } - else if (c == '=') { + if ((c = nextc(p)) == '&') { + SET_LEX_STATE(EXPR_BEG); + if ((c = nextc(p)) == '=') { + set_yylval_id(idANDOP); + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + pushback(p, c); + return tANDOP; + } + else if (c == '=') { set_yylval_id('&'); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - pushback(c); - if (IS_SPCARG(c)) { - rb_warning0("`&' interpreted as argument prefix"); - c = tAMPER; - } - else if (IS_BEG()) { - c = tAMPER; - } - else { - warn_balanced("&", "argument prefix"); - c = '&'; - } - lex_state = IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG; - return c; + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + else if (c == '.') { + set_yylval_id(idANDDOT); + SET_LEX_STATE(EXPR_DOT); + return tANDDOT; + } + pushback(p, c); + if (IS_SPCARG(c)) { + if ((c != ':') || + (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"); + } + c = tAMPER; + } + else if (IS_BEG()) { + c = tAMPER; + } + else { + c = warn_balanced('&', "&", "argument prefix"); + } + SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); + return c; case '|': - if ((c = nextc()) == '|') { - lex_state = EXPR_BEG; - if ((c = nextc()) == '=') { - set_yylval_id(tOROP); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - pushback(c); - return tOROP; - } - if (c == '=') { + if ((c = nextc(p)) == '|') { + SET_LEX_STATE(EXPR_BEG); + if ((c = nextc(p)) == '=') { + set_yylval_id(idOROP); + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + pushback(p, c); + if (IS_lex_state_for(last_state, EXPR_BEG)) { + c = '|'; + pushback(p, '|'); + return c; + } + return tOROP; + } + if (c == '=') { set_yylval_id('|'); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - lex_state = IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL; - pushback(c); - return '|'; + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL); + pushback(p, c); + return '|'; case '+': - c = nextc(); - if (IS_AFTER_OPERATOR()) { - lex_state = EXPR_ARG; - if (c == '@') { - return tUPLUS; - } - pushback(c); - return '+'; - } - if (c == '=') { + c = nextc(p); + if (IS_AFTER_OPERATOR()) { + SET_LEX_STATE(EXPR_ARG); + if (c == '@') { + return tUPLUS; + } + pushback(p, c); + return '+'; + } + if (c == '=') { set_yylval_id('+'); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous('+'))) { - lex_state = EXPR_BEG; - pushback(c); - if (c != -1 && ISDIGIT(c)) { - return parse_numeric(parser, '+'); - } - return tUPLUS; - } - lex_state = EXPR_BEG; - pushback(c); - warn_balanced("+", "unary operator"); - return '+'; + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) { + SET_LEX_STATE(EXPR_BEG); + pushback(p, c); + if (c != -1 && ISDIGIT(c)) { + return parse_numeric(p, '+'); + } + return tUPLUS; + } + SET_LEX_STATE(EXPR_BEG); + pushback(p, c); + return warn_balanced('+', "+", "unary operator"); case '-': - c = nextc(); - if (IS_AFTER_OPERATOR()) { - lex_state = EXPR_ARG; - if (c == '@') { - return tUMINUS; - } - pushback(c); - return '-'; - } - if (c == '=') { + c = nextc(p); + if (IS_AFTER_OPERATOR()) { + SET_LEX_STATE(EXPR_ARG); + if (c == '@') { + return tUMINUS; + } + pushback(p, c); + return '-'; + } + if (c == '=') { set_yylval_id('-'); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - if (c == '>') { - lex_state = EXPR_ENDFN; - return tLAMBDA; - } - if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous('-'))) { - lex_state = EXPR_BEG; - pushback(c); - if (c != -1 && ISDIGIT(c)) { - return tUMINUS_NUM; - } - return tUMINUS; - } - lex_state = EXPR_BEG; - pushback(c); - warn_balanced("-", "unary operator"); - return '-'; - - case '.': - lex_state = EXPR_BEG; - if ((c = nextc()) == '.') { - if ((c = nextc()) == '.') { - return tDOT3; - } - pushback(c); - return tDOT2; - } - if (c == '?') { - lex_state = EXPR_DOT; - return tDOTQ; - } - pushback(c); - if (c != -1 && ISDIGIT(c)) { - yyerror("no .<digit> floating literal anymore; put 0 before dot"); - } - lex_state = EXPR_DOT; - return '.'; + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + 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, '-'))) { + SET_LEX_STATE(EXPR_BEG); + pushback(p, c); + if (c != -1 && ISDIGIT(c)) { + return tUMINUS_NUM; + } + return tUMINUS; + } + SET_LEX_STATE(EXPR_BEG); + pushback(p, c); + return warn_balanced('-', "-", "unary operator"); + + case '.': { + int is_beg = IS_BEG(); + SET_LEX_STATE(EXPR_BEG); + if ((c = nextc(p)) == '.') { + if ((c = nextc(p)) == '.') { + 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?"); + } + return is_beg ? tBDOT3 : tDOT3; + } + pushback(p, c); + return is_beg ? tBDOT2 : tDOT2; + } + pushback(p, c); + if (c != -1 && ISDIGIT(c)) { + char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0; + parse_numeric(p, '.'); + if (ISDIGIT(prev)) { + yyerror0("unexpected fraction part after numeric literal"); + } + else { + yyerror0("no .<digit> floating literal anymore; put 0 before dot"); + } + SET_LEX_STATE(EXPR_END); + p->lex.ptok = p->lex.pcur; + goto retry; + } + set_yylval_id('.'); + SET_LEX_STATE(EXPR_DOT); + return '.'; + } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - return parse_numeric(parser, c); + return parse_numeric(p, c); case ')': + COND_POP(); + CMDARG_POP(); + SET_LEX_STATE(EXPR_ENDFN); + p->lex.paren_nest--; + return c; + case ']': - paren_nest--; + COND_POP(); + CMDARG_POP(); + SET_LEX_STATE(EXPR_END); + p->lex.paren_nest--; + return c; + case '}': - COND_LEXPOP(); - CMDARG_LEXPOP(); - if (c == ')') - lex_state = EXPR_ENDFN; - else - lex_state = EXPR_ENDARG; - if (c == '}') { - if (!brace_nest--) c = tSTRING_DEND; - } - return c; + /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */ + if (!p->lex.brace_nest--) return tSTRING_DEND; + COND_POP(); + CMDARG_POP(); + SET_LEX_STATE(EXPR_END); + p->lex.paren_nest--; + return c; case ':': - c = nextc(); - if (c == ':') { - if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) { - lex_state = EXPR_BEG; - return tCOLON3; - } - lex_state = EXPR_DOT; - return tCOLON2; - } - if (IS_END() || ISSPACE(c)) { - pushback(c); - warn_balanced(":", "symbol literal"); - lex_state = EXPR_BEG; - return ':'; - } - switch (c) { - case '\'': - lex_strterm = NEW_STRTERM(str_ssym, c, 0); - break; - case '"': - lex_strterm = NEW_STRTERM(str_dsym, c, 0); - break; - default: - pushback(c); - break; - } - lex_state = EXPR_FNAME; - return tSYMBEG; + c = nextc(p); + if (c == ':') { + if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) { + SET_LEX_STATE(EXPR_BEG); + return tCOLON3; + } + set_yylval_id(idCOLON2); + SET_LEX_STATE(EXPR_DOT); + return tCOLON2; + } + if (IS_END() || ISSPACE(c) || c == '#') { + pushback(p, c); + c = warn_balanced(':', ":", "symbol literal"); + SET_LEX_STATE(EXPR_BEG); + return c; + } + switch (c) { + case '\'': + p->lex.strterm = NEW_STRTERM(str_ssym, c, 0); + break; + case '"': + p->lex.strterm = NEW_STRTERM(str_dsym, c, 0); + break; + default: + pushback(p, c); + break; + } + SET_LEX_STATE(EXPR_FNAME); + return tSYMBEG; case '/': - if (IS_BEG()) { - lex_strterm = NEW_STRTERM(str_regexp, '/', 0); - return tREGEXP_BEG; - } - if ((c = nextc()) == '=') { + if (IS_BEG()) { + p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0); + return tREGEXP_BEG; + } + if ((c = nextc(p)) == '=') { set_yylval_id('/'); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - pushback(c); - if (IS_SPCARG(c)) { - (void)arg_ambiguous('/'); - lex_strterm = NEW_STRTERM(str_regexp, '/', 0); - return tREGEXP_BEG; - } - lex_state = IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG; - warn_balanced("/", "regexp literal"); - return '/'; + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + pushback(p, c); + if (IS_SPCARG(c)) { + arg_ambiguous(p, '/'); + p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0); + return tREGEXP_BEG; + } + SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); + return warn_balanced('/', "/", "regexp literal"); case '^': - if ((c = nextc()) == '=') { + if ((c = nextc(p)) == '=') { set_yylval_id('^'); - lex_state = EXPR_BEG; - return tOP_ASGN; - } - lex_state = IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG; - pushback(c); - return '^'; + SET_LEX_STATE(EXPR_BEG); + return tOP_ASGN; + } + SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); + pushback(p, c); + return '^'; case ';': - lex_state = EXPR_BEG; - command_start = TRUE; - return ';'; + SET_LEX_STATE(EXPR_BEG); + p->command_start = TRUE; + return ';'; case ',': - lex_state = EXPR_BEG|EXPR_LABEL; - return ','; + SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); + return ','; case '~': - if (IS_AFTER_OPERATOR()) { - if ((c = nextc()) != '@') { - pushback(c); - } - lex_state = EXPR_ARG; - } - else { - lex_state = EXPR_BEG; - } - return '~'; + if (IS_AFTER_OPERATOR()) { + if ((c = nextc(p)) != '@') { + pushback(p, c); + } + SET_LEX_STATE(EXPR_ARG); + } + else { + SET_LEX_STATE(EXPR_BEG); + } + return '~'; case '(': - if (IS_BEG()) { - c = tLPAREN; - } - else if (IS_SPCARG(-1)) { - c = tLPAREN_ARG; - } - paren_nest++; - COND_PUSH(0); - CMDARG_PUSH(0); - lex_state = EXPR_BEG|EXPR_LABEL; - return c; + if (IS_BEG()) { + c = tLPAREN; + } + else if (!space_seen) { + /* foo( ... ) => method call, no ambiguity */ + } + else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) { + c = tLPAREN_ARG; + } + else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) { + rb_warning0("parentheses after method name is interpreted as " + "an argument list, not a decomposed argument"); + } + p->lex.paren_nest++; + COND_PUSH(0); + CMDARG_PUSH(0); + SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); + return c; case '[': - paren_nest++; - if (IS_AFTER_OPERATOR()) { - lex_state = EXPR_ARG; - if ((c = nextc()) == ']') { - if ((c = nextc()) == '=') { - return tASET; - } - pushback(c); - return tAREF; - } - pushback(c); - lex_state |= EXPR_LABEL; - return '['; - } - else if (IS_BEG()) { - c = tLBRACK; - } - else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) { - c = tLBRACK; - } - lex_state = EXPR_BEG|EXPR_LABEL; - COND_PUSH(0); - CMDARG_PUSH(0); - return c; + p->lex.paren_nest++; + if (IS_AFTER_OPERATOR()) { + if ((c = nextc(p)) == ']') { + p->lex.paren_nest--; + SET_LEX_STATE(EXPR_ARG); + if ((c = nextc(p)) == '=') { + return tASET; + } + pushback(p, c); + return tAREF; + } + pushback(p, c); + SET_LEX_STATE(EXPR_ARG|EXPR_LABEL); + return '['; + } + else if (IS_BEG()) { + c = tLBRACK; + } + else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) { + c = tLBRACK; + } + SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); + COND_PUSH(0); + CMDARG_PUSH(0); + return c; case '{': - ++brace_nest; - if (lpar_beg && lpar_beg == paren_nest) { - lex_state = EXPR_BEG; - lpar_beg = 0; - --paren_nest; - COND_PUSH(0); - CMDARG_PUSH(0); - return tLAMBEG; - } - if (IS_lex_state(EXPR_LABELED)) - c = tLBRACE; /* hash */ - else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN)) - c = '{'; /* block (primary) */ - else if (IS_lex_state(EXPR_ENDARG)) - c = tLBRACE_ARG; /* block (expr) */ - else - c = tLBRACE; /* hash */ - COND_PUSH(0); - CMDARG_PUSH(0); - lex_state = EXPR_BEG; - if (c != tLBRACE_ARG) lex_state |= EXPR_LABEL; - if (c != tLBRACE) command_start = TRUE; - return c; + ++p->lex.brace_nest; + if (lambda_beginning_p()) + c = tLAMBEG; + else if (IS_lex_state(EXPR_LABELED)) + c = tLBRACE; /* hash */ + else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN)) + c = '{'; /* block (primary) */ + else if (IS_lex_state(EXPR_ENDARG)) + c = tLBRACE_ARG; /* block (expr) */ + else + c = tLBRACE; /* hash */ + if (c != tLBRACE) { + p->command_start = TRUE; + SET_LEX_STATE(EXPR_BEG); + } + else { + SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); + } + ++p->lex.paren_nest; /* after lambda_beginning_p() */ + COND_PUSH(0); + CMDARG_PUSH(0); + return c; case '\\': - c = nextc(); - if (c == '\n') { - space_seen = 1; -#ifdef RIPPER - ripper_dispatch_scan_event(parser, tSP); -#endif - goto retry; /* skip \\n */ - } - pushback(c); - return '\\'; + c = nextc(p); + if (c == '\n') { + space_seen = 1; + dispatch_scan_event(p, tSP); + goto retry; /* skip \\n */ + } + if (c == ' ') return tSP; + if (ISSPACE(c)) return c; + pushback(p, c); + return '\\'; case '%': - return parse_percent(parser, space_seen, last_state); + return parse_percent(p, space_seen, last_state); case '$': - return parse_gvar(parser, last_state); + return parse_gvar(p, last_state); case '@': - return parse_atmark(parser, last_state); + return parse_atmark(p, last_state); case '_': - if (was_bol() && whole_match_p("__END__", 7, 0)) { - ruby__end__seen = 1; - parser->eofp = 1; -#ifndef RIPPER - return -1; -#else - lex_goto_eol(parser); - ripper_dispatch_scan_event(parser, k__END__); - return 0; + if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) { + p->ruby__end__seen = 1; + p->eofp = 1; +#ifdef RIPPER + lex_goto_eol(p); + dispatch_scan_event(p, k__END__); #endif - } - newtok(); - break; + return END_OF_INPUT; + } + newtok(p); + break; default: - if (!parser_is_identchar()) { - compile_error(PARSER_ARG "Invalid char `\\x%02X' in expression", c); - goto retry; - } + if (!parser_is_identchar(p)) { + compile_error(p, "Invalid char '\\x%02X' in expression", c); + token_flush(p); + goto retry; + } - newtok(); - break; + newtok(p); + break; } - return parse_ident(parser, c, cmd_state); + return parse_ident(p, c, cmd_state); } -static int -yylex(YYSTYPE *lval, struct parser_params *parser) +static enum yytokentype +yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p) { - int t; + enum yytokentype t; - parser->lval = lval; - lval->val = Qundef; - t = parser_yylex(parser); -#ifdef RIPPER - if (!NIL_P(parser->delayed)) { - ripper_dispatch_delayed_token(parser, t); - return t; - } - if (t != 0) - ripper_dispatch_scan_event(parser, t); -#endif + p->lval = lval; + lval->node = 0; + p->yylloc = yylloc; + + t = parser_yylex(p); + + if (has_delayed_token(p)) + dispatch_delayed_token(p, t); + else if (t != END_OF_INPUT) + dispatch_scan_event(p, t); return t; } -#ifndef RIPPER +#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1)) + +static NODE* +node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment) +{ + NODE *n = rb_ast_newnode(p->ast, type, size, alignment); + + rb_node_init(n, type); + return n; +} + +static NODE * +nd_set_loc(NODE *nd, const YYLTYPE *loc) +{ + nd->nd_loc = *loc; + nd_set_line(nd, loc->beg_pos.lineno); + return nd; +} + static NODE* -node_newnode(struct parser_params *parser, enum node_type type, VALUE a0, VALUE a1, VALUE a2) +node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc) +{ + NODE *n = node_new_internal(p, type, size, alignment); + + nd_set_loc(n, loc); + nd_set_node_id(n, parser_get_node_id(p)); + return n; +} + +#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), 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) +{ + 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, 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; +} + +static rb_node_defn_t * +rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc) +{ + rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc); + n->nd_mid = nd_mid; + n->nd_defn = nd_defn; + + return n; +} + +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) +{ + rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc); + n->nd_recv = nd_recv; + n->nd_mid = nd_mid; + n->nd_defn = nd_defn; + + return n; +} + +static rb_node_block_t * +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 = (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, 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; +} + +static rb_node_for_masgn_t * +rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc) +{ + rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc); + n->nd_var = nd_var; + + return n; +} + +static rb_node_retry_t * +rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc); + + return n; +} + +static rb_node_begin_t * +rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc) +{ + rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc); + n->nd_body = nd_body; + + return n; +} + +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) +{ + rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc); + n->nd_head = nd_head; + n->nd_resq = nd_resq; + n->nd_else = nd_else; + + return n; +} + +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) +{ + rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc); + n->nd_args = nd_args; + n->nd_exc_var = nd_exc_var; + n->nd_body = nd_body; + n->nd_next = nd_next; + + return n; +} + +static rb_node_ensure_t * +rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc) +{ + rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc); + n->nd_head = nd_head; + 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc) +{ + /* Keep the order of node creation */ + 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; +} + +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, 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; + + return n; +} + +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) +{ + /* Keep the order of node creation */ + 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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; +} + +static rb_node_self_t * +rb_node_self_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc); + n->nd_state = 1; + + return n; +} + +static rb_node_nil_t * +rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc); + + return n; +} + +static rb_node_true_t * +rb_node_true_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc); + + return n; +} + +static rb_node_false_t * +rb_node_false_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc); + + return n; +} + +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) +{ + 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; +} + +static rb_node_zsuper_t * +rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc); + + return n; +} + +static rb_node_match2_t * +rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc) +{ + rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc); + n->nd_recv = nd_recv; + n->nd_value = nd_value; + n->nd_args = 0; + + return n; +} + +static rb_node_match3_t * +rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc) +{ + rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc); + n->nd_recv = nd_recv; + n->nd_value = nd_value; + + 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; +} + +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) +{ + rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc); + n->nd_head = nd_head; + n->as.nd_alen = nd_alen; + n->nd_next = nd_next; + + return n; +} + +static rb_node_zlist_t * +rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc); + + return n; +} + +static rb_node_hash_t * +rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc) +{ + rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc); + n->nd_head = nd_head; + n->nd_brace = 0; + + return n; +} + +static rb_node_masgn_t * +rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc) +{ + rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc); + n->nd_head = nd_head; + n->nd_value = 0; + n->nd_args = nd_args; + + return n; +} + +static rb_node_gasgn_t * +rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc) +{ + rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc); + n->nd_vid = nd_vid; + n->nd_value = nd_value; + + return n; +} + +static rb_node_lasgn_t * +rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc) +{ + rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc); + n->nd_vid = nd_vid; + n->nd_value = nd_value; + + return n; +} + +static rb_node_dasgn_t * +rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc) +{ + rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc); + n->nd_vid = nd_vid; + n->nd_value = nd_value; + + return n; +} + +static rb_node_iasgn_t * +rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc) +{ + rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc); + n->nd_vid = nd_vid; + n->nd_value = nd_value; + + return n; +} + +static rb_node_cvasgn_t * +rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc) +{ + rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc); + n->nd_vid = nd_vid; + n->nd_value = nd_value; + + return n; +} + +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) +{ + 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, 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; + n->nd_value = nd_value; + 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; +} + +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) +{ + rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc); + n->nd_head = nd_head; + n->nd_value = nd_value; + + return n; +} + +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) +{ + rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc); + n->nd_head = nd_head; + n->nd_value = nd_value; + + return n; +} + +static rb_node_gvar_t * +rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc) +{ + rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc); + n->nd_vid = nd_vid; + + return n; +} + +static rb_node_lvar_t * +rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc) +{ + rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc); + n->nd_vid = nd_vid; + + return n; +} + +static rb_node_dvar_t * +rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc) +{ + rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc); + n->nd_vid = nd_vid; + + return n; +} + +static rb_node_ivar_t * +rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc) +{ + rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc); + n->nd_vid = nd_vid; + + return n; +} + +static rb_node_const_t * +rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc) +{ + rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc); + n->nd_vid = nd_vid; + + return n; +} + +static rb_node_cvar_t * +rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc) +{ + rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc); + n->nd_vid = nd_vid; + + return n; +} + +static rb_node_nth_ref_t * +rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc) +{ + rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc); + n->nd_nth = nd_nth; + + return n; +} + +static rb_node_back_ref_t * +rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc) +{ + rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc); + n->nd_nth = nd_nth; + + return n; +} + +static rb_node_integer_t * +rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc) +{ + 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, rb_parser_string_t *string, const YYLTYPE *loc) +{ + rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc); + n->string = string; + + return n; +} + +/* TODO; Use union for NODE_DSTR2 */ +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) +{ + rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc); + n->string = string; + n->as.nd_alen = nd_alen; + n->nd_next = (rb_node_list_t *)nd_next; + + return n; +} + +static rb_node_dstr_t * +rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc) +{ + return rb_node_dstr_new0(p, string, 1, 0, loc); +} + +static rb_node_xstr_t * +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->string = string; + + return n; +} + +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) +{ + rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc); + 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, 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->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, 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; +} + +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) +{ + rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc); + n->nd_recv = nd_recv; + n->nd_mid = nd_mid; + n->nd_args = nd_args; + + return n; +} + +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) +{ + rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc); + n->nd_recv = nd_recv; + n->nd_mid = nd_mid; + n->nd_args = nd_args; + + return n; +} + +static rb_node_fcall_t * +rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc) +{ + rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc); + n->nd_mid = nd_mid; + n->nd_args = nd_args; + + return n; +} + +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) +{ + rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc); + n->nd_recv = nd_recv; + n->nd_mid = nd_mid; + n->nd_args = nd_args; + + return n; +} + +static rb_node_vcall_t * +rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc) +{ + rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc); + n->nd_mid = nd_mid; + + return n; +} + +static rb_node_once_t * +rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc) +{ + rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc); + n->nd_body = nd_body; + + return n; +} + +static rb_node_args_t * +rb_node_args_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc); + MEMZERO(&n->nd_ainfo, struct rb_args_info, 1); + + return n; +} + +static rb_node_args_aux_t * +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; + n->nd_plen = nd_plen; + n->nd_next = 0; + + return n; +} + +static rb_node_opt_arg_t * +rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc) +{ + rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc); + n->nd_body = nd_body; + n->nd_next = 0; + + return n; +} + +static rb_node_kw_arg_t * +rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc) +{ + rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc); + n->nd_body = nd_body; + n->nd_next = 0; + + return n; +} + +static rb_node_postarg_t * +rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc) +{ + rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc); + n->nd_1st = nd_1st; + n->nd_2nd = nd_2nd; + + return n; +} + +static rb_node_argscat_t * +rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc) +{ + rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc); + n->nd_head = nd_head; + n->nd_body = nd_body; + + return n; +} + +static rb_node_argspush_t * +rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc) +{ + rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc); + n->nd_head = nd_head; + n->nd_body = nd_body; + + return n; +} + +static rb_node_splat_t * +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, 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, 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, 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; +} + +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_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; +} + +static rb_node_errinfo_t * +rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc); + + return n; +} + +static rb_node_defined_t * +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, 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; +} + +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) +{ + rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc); + n->nd_recv = nd_recv; + n->nd_mid = nd_mid; + n->nd_args = nd_args; + + return n; +} + +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) +{ + rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc); + n->nd_pconst = 0; + n->pre_args = pre_args; + n->rest_arg = rest_arg; + n->post_args = post_args; + + return n; +} + +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) +{ + rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc); + n->nd_pconst = nd_pconst; + n->nd_pkwargs = nd_pkwargs; + n->nd_pkwrestarg = nd_pkwrestarg; + + return n; +} + +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) +{ + rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc); + n->nd_pconst = 0; + n->pre_rest_arg = pre_rest_arg; + n->args = args; + n->post_rest_arg = post_rest_arg; + + 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, 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, 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; +} + +static rb_node_error_t * +rb_node_error_new(struct parser_params *p, const YYLTYPE *loc) +{ + rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc); + + return n; +} + +static rb_node_break_t * +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, 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, 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; +} + +static rb_node_def_temp_t * +rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc) { - NODE *n = (rb_node_newnode)(type, a0, a1, a2); - nd_set_line(n, ruby_sourceline); + rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc); + n->save.numparam_save = 0; + n->save.max_numparam = 0; + n->save.ctxt = p->ctxt; + n->nd_def = 0; + n->nd_mid = 0; + 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 */ { @@ -8632,13 +12498,14 @@ nodeline(NODE *node) { return nd_line(node); } +#endif static NODE* newline_node(NODE *node) { if (node) { - node = remove_begin(node); - node->flags |= NODE_FL_NEWLINE; + node = remove_begin(node); + nd_set_fl_newline(node); } return node; } @@ -8648,26 +12515,11 @@ fixpos(NODE *node, NODE *orig) { if (!node) return; if (!orig) return; - if (orig == (NODE*)1) return; nd_set_line(node, nd_line(orig)); } -static void -parser_warning(struct parser_params *parser, NODE *node, const char *mesg) -{ - rb_compile_warning(ruby_sourcefile, nd_line(node), "%s", mesg); -} -#define parser_warning(node, mesg) parser_warning(parser, (node), (mesg)) - -static void -parser_warn(struct parser_params *parser, NODE *node, const char *mesg) -{ - rb_compile_warn(ruby_sourcefile, nd_line(node), "%s", mesg); -} -#define parser_warn(node, mesg) parser_warn(parser, (node), (mesg)) - static NODE* -block_append_gen(struct parser_params *parser, NODE *head, NODE *tail) +block_append(struct parser_params *p, NODE *head, NODE *tail) { NODE *end, *h = head, *nd; @@ -8675,67 +12527,58 @@ block_append_gen(struct parser_params *parser, NODE *head, NODE *tail) if (h == 0) return tail; switch (nd_type(h)) { - case NODE_LIT: - case NODE_STR: - case NODE_SELF: - case NODE_TRUE: - case NODE_FALSE: - case NODE_NIL: - parser_warning(h, "unused literal ignored"); - return tail; default: - h = end = NEW_BLOCK(head); - end->nd_end = end; - fixpos(end, head); - head = end; - break; + h = end = NEW_BLOCK(head, &head->nd_loc); + head = end; + break; case NODE_BLOCK: - end = h->nd_end; - break; + end = RNODE_BLOCK(h)->nd_end; + break; } - nd = end->nd_head; + nd = RNODE_BLOCK(end)->nd_head; switch (nd_type(nd)) { case NODE_RETURN: case NODE_BREAK: case NODE_NEXT: case NODE_REDO: case NODE_RETRY: - if (RTEST(ruby_verbose)) { - parser_warning(tail, "statement not reached"); - } - break; + rb_warning0L(nd_line(tail), "statement not reached"); + break; default: - break; + break; } - if (nd_type(tail) != NODE_BLOCK) { - tail = NEW_BLOCK(tail); - tail->nd_end = tail; + if (!nd_type_p(tail, NODE_BLOCK)) { + tail = NEW_BLOCK(tail, &tail->nd_loc); } - end->nd_next = tail; - h->nd_end = tail->nd_end; + RNODE_BLOCK(end)->nd_next = tail; + RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end; + nd_set_last_loc(head, nd_last_loc(tail)); return head; } /* append item to the list */ static NODE* -list_append_gen(struct parser_params *parser, NODE *list, NODE *item) +list_append(struct parser_params *p, NODE *list, NODE *item) { NODE *last; - if (list == 0) return NEW_LIST(item); - if (list->nd_next) { - last = list->nd_next->nd_end; + if (list == 0) return NEW_LIST(item, &item->nd_loc); + if (RNODE_LIST(list)->nd_next) { + last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end; } else { - last = list; + last = list; } - list->nd_alen += 1; - last->nd_next = NEW_LIST(item); - list->nd_next->nd_end = last->nd_next; + RNODE_LIST(list)->as.nd_alen += 1; + RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc); + RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next; + + nd_set_last_loc(list, nd_last_loc(item)); + return list; } @@ -8745,380 +12588,973 @@ list_concat(NODE *head, NODE *tail) { NODE *last; - if (head->nd_next) { - last = head->nd_next->nd_end; + if (RNODE_LIST(head)->nd_next) { + last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end; } else { - last = head; + last = head; } - head->nd_alen += tail->nd_alen; - last->nd_next = tail; - if (tail->nd_next) { - head->nd_next->nd_end = tail->nd_next->nd_end; + RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen; + RNODE_LIST(last)->nd_next = tail; + if (RNODE_LIST(tail)->nd_next) { + RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end; } else { - head->nd_next->nd_end = tail; + RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail; } + nd_set_last_loc(head, nd_last_loc(tail)); + return head; } static int -literal_concat0(struct parser_params *parser, VALUE head, VALUE tail) -{ - if (NIL_P(tail)) return 1; - if (!rb_enc_compatible(head, tail)) { - compile_error(PARSER_ARG "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); - return 0; - } - rb_str_buf_append(head, tail); +literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *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_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_parser_str_buf_append(p, head, tail); return 1; } +static rb_parser_string_t * +string_literal_head(struct parser_params *p, enum node_type htype, NODE *head) +{ + 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 NULL; + } + 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_gen(struct parser_params *parser, NODE *head, NODE *tail) +literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc) { enum node_type htype; - NODE *headlast; - VALUE lit; + rb_parser_string_t *lit; if (!head) return tail; if (!tail) return head; htype = nd_type(head); if (htype == NODE_EVSTR) { - NODE *node = NEW_DSTR(STR_NEW0()); - head = list_append(node, head); - htype = NODE_DSTR; + head = new_dstr(p, head, loc); + htype = NODE_DSTR; + } + if (p->heredoc_indent > 0) { + switch (htype) { + case NODE_STR: + head = str2dstr(p, head); + case NODE_DSTR: + return list_append(p, head, tail); + default: + break; + } } switch (nd_type(tail)) { case NODE_STR: - if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) && - nd_type(headlast) == NODE_STR) { - htype = NODE_STR; - lit = headlast->nd_lit; - } - else { - lit = head->nd_lit; - } - if (htype == NODE_STR) { - if (!literal_concat0(parser, lit, tail->nd_lit)) { - error: - rb_gc_force_recycle((VALUE)head); - rb_gc_force_recycle((VALUE)tail); - return 0; - } - rb_gc_force_recycle((VALUE)tail); - } - else { - list_append(head, tail); - } - break; + if ((lit = string_literal_head(p, htype, head)) != false) { + htype = NODE_STR; + } + else { + lit = RNODE_DSTR(head)->string; + } + if (htype == NODE_STR) { + if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) { + error: + rb_discard_node(p, head); + rb_discard_node(p, tail); + return 0; + } + rb_discard_node(p, tail); + } + else { + list_append(p, head, tail); + } + break; case NODE_DSTR: - if (htype == NODE_STR) { - if (!literal_concat0(parser, head->nd_lit, tail->nd_lit)) - goto error; - tail->nd_lit = head->nd_lit; - rb_gc_force_recycle((VALUE)head); - head = tail; - } - else if (NIL_P(tail->nd_lit)) { - append: - head->nd_alen += tail->nd_alen - 1; - head->nd_next->nd_end->nd_next = tail->nd_next; - head->nd_next->nd_end = tail->nd_next->nd_end; - rb_gc_force_recycle((VALUE)tail); - } - else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) && - nd_type(headlast) == NODE_STR) { - lit = headlast->nd_lit; - if (!literal_concat0(parser, lit, tail->nd_lit)) - goto error; - tail->nd_lit = Qnil; - goto append; - } - else { - nd_set_type(tail, NODE_ARRAY); - tail->nd_head = NEW_STR(tail->nd_lit); - list_concat(head, tail); - } - break; + if (htype == NODE_STR) { + if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string)) + goto error; + 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 (!RNODE_DSTR(tail)->string) { + append: + RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1; + if (!RNODE_DSTR(head)->nd_next) { + RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next; + } + else if (RNODE_DSTR(tail)->nd_next) { + RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next; + RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end; + } + rb_discard_node(p, tail); + } + else if ((lit = string_literal_head(p, htype, head)) != false) { + if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string)) + goto error; + 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)->string, loc), RNODE_DSTR(tail)->as.nd_alen, (NODE *)RNODE_DSTR(tail)->nd_next, loc)); + RNODE_DSTR(tail)->string = 0; + } + break; case NODE_EVSTR: - if (htype == NODE_STR) { - nd_set_type(head, NODE_DSTR); - head->nd_alen = 1; - } - list_append(head, tail); - break; + if (htype == NODE_STR) { + head = str2dstr(p, head); + RNODE_DSTR(head)->as.nd_alen = 1; + } + list_append(p, head, tail); + break; } return head; } +static void +nd_copy_flag(NODE *new_node, NODE *old_node) +{ + if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node); + nd_set_line(new_node, nd_line(old_node)); + new_node->nd_loc = old_node->nd_loc; + new_node->node_id = old_node->node_id; +} + +static NODE * +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)->string = RNODE_STR(node)->string; + RNODE_DSTR(new_node)->as.nd_alen = 0; + RNODE_DSTR(new_node)->nd_next = 0; + RNODE_STR(node)->string = 0; + + return new_node; +} + static NODE * -evstr2dstr_gen(struct parser_params *parser, NODE *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) { - if (nd_type(node) == NODE_EVSTR) { - node = list_append(NEW_DSTR(STR_NEW0()), node); + 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; +} + +static NODE * +evstr2dstr(struct parser_params *p, NODE *node) +{ + if (nd_type_p(node, NODE_EVSTR)) { + node = new_dstr(p, node, &node->nd_loc); } return node; } static NODE * -new_evstr_gen(struct parser_params *parser, NODE *node) +new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc) { NODE *head = node; if (node) { - switch (nd_type(node)) { - case NODE_STR: case NODE_DSTR: case NODE_EVSTR: - return node; - } + switch (nd_type(node)) { + case NODE_STR: + return str2dstr(p, node); + case NODE_DSTR: + break; + case NODE_EVSTR: + return node; + } } - return NEW_EVSTR(head); + return NEW_EVSTR(head, loc, opening_loc, closing_loc); } static NODE * -call_bin_op_gen(struct parser_params *parser, NODE *recv, ID id, NODE *arg1) +new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc) { - value_expr(recv); - value_expr(arg1); - return NEW_CALL(recv, id, NEW_LIST(arg1)); + NODE *dstr = NEW_DSTR(STRING_NEW0(), loc); + return list_append(p, dstr, node); } static NODE * -call_uni_op_gen(struct parser_params *parser, NODE *recv, ID id) +call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, + const YYLTYPE *op_loc, const YYLTYPE *loc) { - value_expr(recv); - return NEW_CALL(recv, id, 0); + NODE *expr; + 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; +} + +static NODE * +call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc) +{ + NODE *opcall; + value_expr(p, recv); + opcall = NEW_OPCALL(recv, id, 0, loc); + nd_set_line(opcall, op_loc->beg_pos.lineno); + return opcall; +} + +static NODE * +new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc) +{ + NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc); + nd_set_line(qcall, op_loc->beg_pos.lineno); + return qcall; } static NODE* -match_op_gen(struct parser_params *parser, NODE *node1, NODE *node2) +new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc) { - value_expr(node1); - value_expr(node2); - if (node1) { - switch (nd_type(node1)) { - case NODE_DREGX: - case NODE_DREGX_ONCE: - return NEW_MATCH2(node1, node2); + NODE *ret; + if (block) block_dup_check(p, args, block); + ret = new_qcall(p, atype, recv, mid, args, op_loc, loc); + if (block) ret = method_add_block(p, ret, block, loc); + fixpos(ret, recv); + return ret; +} - case NODE_LIT: - if (RB_TYPE_P(node1->nd_lit, T_REGEXP)) { - return NEW_MATCH2(node1, node2); - } - } +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; + return nd_once_body(node); +} + +static NODE* +match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc) +{ + NODE *n; + int line = op_loc->beg_pos.lineno; + + value_expr(p, node1); + value_expr(p, node2); + + if ((n = last_expr_once_body(node1)) != 0) { + switch (nd_type(n)) { + case NODE_DREGX: + { + NODE *match = NEW_MATCH2(node1, node2, 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; + } + } + } } - if (node2) { - switch (nd_type(node2)) { - case NODE_DREGX: - case NODE_DREGX_ONCE: - return NEW_MATCH3(node2, node1); + if ((n = last_expr_once_body(node2)) != 0) { + NODE *match3; - case NODE_LIT: - if (RB_TYPE_P(node2->nd_lit, T_REGEXP)) { - return NEW_MATCH3(node2, node1); - } - } + switch (nd_type(n)) { + case NODE_DREGX: + match3 = NEW_MATCH3(node2, node1, loc); + return match3; + } } - return NEW_CALL(node1, tMATCH, NEW_LIST(node2)); + n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc); + nd_set_line(n, line); + return n; } # if WARN_PAST_SCOPE static int -past_dvar_p(struct parser_params *parser, ID id) +past_dvar_p(struct parser_params *p, ID id) { - struct vtable *past = lvtbl->past; + struct vtable *past = p->lvtbl->past; while (past) { - if (vtable_included(past, id)) return 1; - past = past->prev; + if (vtable_included(past, id)) return 1; + past = past->prev; } return 0; } # endif +static int +numparam_nested_p(struct parser_params *p) +{ + struct local_vars *local = p->lvtbl; + NODE *outer = local->numparam.outer; + NODE *inner = local->numparam.inner; + if (outer || inner) { + NODE *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_gen(struct parser_params *parser, ID id) +gettable(struct parser_params *p, ID id, const YYLTYPE *loc) { + ID *vidp = NULL; + NODE *node; switch (id) { case keyword_self: - return NEW_SELF(); + return NEW_SELF(loc); case keyword_nil: - return NEW_NIL(); + return NEW_NIL(loc); case keyword_true: - return NEW_TRUE(); + return NEW_TRUE(loc); case keyword_false: - return NEW_FALSE(); + return NEW_FALSE(loc); case keyword__FILE__: - return NEW_STR(rb_str_dup(ruby_sourcefile_string)); + { + VALUE file = p->ruby_sourcefile_string; + if (NIL_P(file)) + file = rb_str_new(0, 0); + node = NEW_FILE(file, loc); + } + return node; case keyword__LINE__: - return NEW_LIT(INT2FIX(tokline)); + return NEW_LINE(loc); case keyword__ENCODING__: - return NEW_LIT(rb_enc_from_encoding(current_enc)); + return NEW_ENCODING(loc); + } switch (id_type(id)) { case ID_LOCAL: - if (dyna_in_block() && dvar_defined(id)) { - if (id == current_arg) { - rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id)); - } - return NEW_DVAR(id); - } - if (local_id(id)) { - if (id == current_arg) { - rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id)); - } - return NEW_LVAR(id); - } + if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) { + 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 (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) || 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; + return node; + } # if WARN_PAST_SCOPE - if (!in_defined && RTEST(ruby_verbose) && past_dvar_p(parser, id)) { - rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id)); - } + if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) { + rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id)); + } # endif - /* method call without arguments */ - return NEW_VCALL(id); + /* 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); + return NEW_GVAR(id, loc); case ID_INSTANCE: - return NEW_IVAR(id); + return NEW_IVAR(id, loc); case ID_CONST: - return NEW_CONST(id); + return NEW_CONST(id, loc); case ID_CLASS: - return NEW_CVAR(id); + return NEW_CVAR(id, loc); } - compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id)); + compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id)); return 0; } -#else /* !RIPPER */ + +static rb_node_opt_arg_t * +opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt) +{ + rb_node_opt_arg_t *opts = opt_list; + RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos; + + while (opts->nd_next) { + opts = opts->nd_next; + RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos; + } + opts->nd_next = opt; + + return opt_list; +} + +static rb_node_kw_arg_t * +kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw) +{ + if (kwlist) { + /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */ + opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw)); + } + return kwlist; +} + +static NODE * +new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc, const YYLTYPE *keyword_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* +symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol) +{ + enum node_type type = nd_type(symbol); + switch (type) { + case NODE_DSTR: + nd_set_type(symbol, NODE_DSYM); + break; + case NODE_STR: + 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)); + } + return list_append(p, symbols, symbol); +} + +static void +dregex_fragment_setenc(struct parser_params *p, rb_node_dregx_t *const dreg, int options) +{ + 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) { + /* 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: + { + /* 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: + 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); + 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); + } + break; + } + return node; +} + +static rb_node_kw_arg_t * +new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc) +{ + if (!k) return 0; + return NEW_KW_ARG((k), loc); +} + +static NODE * +new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc) +{ + if (!node) { + NODE *xstr = NEW_XSTR(STRING_NEW0(), loc); + return xstr; + } + switch (nd_type(node)) { + case NODE_STR: + nd_set_type(node, NODE_XSTR); + nd_set_loc(node, loc); + break; + case NODE_DSTR: + nd_set_type(node, NODE_DXSTR); + nd_set_loc(node, loc); + break; + default: + 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) +{ + /* 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; + + if (p->case_labels == CHECK_LITERAL_WHEN) { + p->case_labels = st_init_table(&literal_type); + } + else { + 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; + } + } + st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline); +} + +#ifdef RIPPER static int -id_is_var_gen(struct parser_params *parser, ID id) +id_is_var(struct parser_params *p, ID id) { if (is_notop_id(id)) { - switch (id & ID_SCOPE_MASK) { - case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS: - return 1; - case ID_LOCAL: - if (dyna_in_block() && dvar_defined(id)) return 1; - if (local_id(id)) return 1; - /* method call without arguments */ - return 0; - } - } - compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2str(id)); + switch (id & ID_SCOPE_MASK) { + case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS: + return 1; + case ID_LOCAL: + if (dyna_in_block(p)) { + if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1; + } + if (local_id(p, id)) return 1; + /* method call without arguments */ + return 0; + } + } + compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id)); return 0; } -#endif /* !RIPPER */ +#endif -#if PARSER_DEBUG -static const char * -lex_state_name(enum lex_state_e state) +static inline enum lex_state_e +parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line) { - static const char names[][12] = { - "EXPR_BEG", "EXPR_END", "EXPR_ENDARG", "EXPR_ENDFN", "EXPR_ARG", - "EXPR_CMDARG", "EXPR_MID", "EXPR_FNAME", "EXPR_DOT", "EXPR_CLASS", - }; + if (p->debug) { + ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line); + } + return p->lex.state = ls; +} - if ((unsigned)state & ~(~0u << EXPR_MAX_STATE)) - return names[ffs(state)]; - return NULL; +#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); + } } -#endif -#ifdef RIPPER +static const char rb_parser_lex_state_names[][8] = { + "BEG", "END", "ENDARG", "ENDFN", "ARG", + "CMDARG", "MID", "FNAME", "DOT", "CLASS", + "LABEL", "LABELED","FITEM", +}; + static VALUE -assignable_gen(struct parser_params *parser, VALUE lhs) -#else -static NODE* -assignable_gen(struct parser_params *parser, ID id, NODE *val) -#endif +append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf) { -#ifdef RIPPER - ID id = get_id(lhs); -# define assignable_result(x) get_value(lhs) -# define parser_yyerror(parser, x) (dispatch1(assign_error, lhs), ripper_error()) -#else -# define assignable_result(x) (x) -#endif - if (!id) return assignable_result(0); + int i, sep = 0; + unsigned int mask = 1; + static const char none[] = "NONE"; + + for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) { + if ((unsigned)state & mask) { + if (sep) { + rb_str_cat(buf, "|", 1); + } + sep = 1; + rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]); + } + } + if (!sep) { + rb_str_cat(buf, none, sizeof(none)-1); + } + return buf; +} + +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) +{ + VALUE mesg; + mesg = rb_str_new_cstr("lex_state: "); + append_lex_state_name(p, from, mesg); + rb_str_cat_cstr(mesg, " -> "); + append_lex_state_name(p, to, mesg); + rb_str_catf(mesg, " at line %d\n", line); + flush_debug_buffer(p, p->debug_output, mesg); + return to; +} + +VALUE +rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state) +{ + return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0))); +} + +static void +append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg) +{ + if (stack == 0) { + rb_str_cat_cstr(mesg, "0"); + } + else { + stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1); + for (; mask && !(stack & mask); mask >>= 1) continue; + for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1); + } +} + +void +rb_parser_show_bitstack(struct parser_params *p, stack_type stack, + const char *name, int line) +{ + VALUE mesg = rb_sprintf("%s: ", name); + append_bitstack_value(p, stack, mesg); + rb_str_catf(mesg, " at line %d\n", line); + flush_debug_buffer(p, p->debug_output, mesg); +} + +void +rb_parser_fatal(struct parser_params *p, const char *fmt, ...) +{ + va_list ap; + VALUE mesg = rb_str_new_cstr("internal parser error: "); + + va_start(ap, fmt); + rb_str_vcatf(mesg, fmt, ap); + va_end(ap); + yyerror0(RSTRING_PTR(mesg)); + RB_GC_GUARD(mesg); + + mesg = rb_str_new(0, 0); + append_lex_state_name(p, p->lex.state, mesg); + compile_error(p, "lex.state: %"PRIsVALUE, mesg); + rb_str_resize(mesg, 0); + append_bitstack_value(p, p->cond_stack, mesg); + compile_error(p, "cond_stack: %"PRIsVALUE, mesg); + rb_str_resize(mesg, 0); + append_bitstack_value(p, p->cmdarg_stack, mesg); + compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg); + if (p->debug_output == rb_ractor_stdout()) + p->debug_output = rb_ractor_stderr(); + p->debug = TRUE; +} + +static YYLTYPE * +rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos) +{ + yylloc->beg_pos.lineno = sourceline; + yylloc->beg_pos.column = beg_pos; + yylloc->end_pos.lineno = sourceline; + yylloc->end_pos.column = end_pos; + return yylloc; +} + +YYLTYPE * +rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc) +{ + int sourceline = here->sourceline; + int beg_pos = (int)here->offset - here->quote + - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT)); + int end_pos = (int)here->offset + here->length + here->quote; + + return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos); +} + +YYLTYPE * +rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc) +{ + yylloc->beg_pos.lineno = p->delayed.beg_line; + yylloc->beg_pos.column = p->delayed.beg_col; + yylloc->end_pos.lineno = p->delayed.end_line; + yylloc->end_pos.column = p->delayed.end_col; + + return yylloc; +} + +YYLTYPE * +rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc) +{ + int sourceline = p->ruby_sourceline; + int beg_pos = (int)(p->lex.ptok - p->lex.pbeg); + int end_pos = (int)(p->lex.pend - p->lex.pbeg); + return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos); +} + +YYLTYPE * +rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc) +{ + yylloc->end_pos = yylloc->beg_pos; + + return yylloc; +} + +YYLTYPE * +rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc) +{ + int sourceline = p->ruby_sourceline; + int beg_pos = (int)(p->lex.ptok - p->lex.pbeg); + int end_pos = (int)(p->lex.ptok - p->lex.pbeg); + return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos); +} + +YYLTYPE * +rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc) +{ + int sourceline = p->ruby_sourceline; + int beg_pos = (int)(p->lex.ptok - p->lex.pbeg); + int end_pos = (int)(p->lex.pcur - p->lex.pbeg); + return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos); +} +#endif /* !RIPPER */ + +static int +assignable0(struct parser_params *p, ID id, const char **err) +{ + if (!id) return -1; switch (id) { case keyword_self: - yyerror("Can't change the value of self"); - goto error; + *err = "Can't change the value of self"; + return -1; case keyword_nil: - yyerror("Can't assign to nil"); - goto error; + *err = "Can't assign to nil"; + return -1; case keyword_true: - yyerror("Can't assign to true"); - goto error; + *err = "Can't assign to true"; + return -1; case keyword_false: - yyerror("Can't assign to false"); - goto error; + *err = "Can't assign to false"; + return -1; case keyword__FILE__: - yyerror("Can't assign to __FILE__"); - goto error; + *err = "Can't assign to __FILE__"; + return -1; case keyword__LINE__: - yyerror("Can't assign to __LINE__"); - goto error; + *err = "Can't assign to __LINE__"; + return -1; case keyword__ENCODING__: - yyerror("Can't assign to __ENCODING__"); - goto error; + *err = "Can't assign to __ENCODING__"; + return -1; } switch (id_type(id)) { case ID_LOCAL: - if (dyna_in_block()) { - if (dvar_curr(id)) { - return assignable_result(NEW_DASGN_CURR(id, val)); - } - else if (dvar_defined(id)) { - return assignable_result(NEW_DASGN(id, val)); - } - else if (local_id(id)) { - return assignable_result(NEW_LASGN(id, val)); - } - else { - dyna_var(id); - return assignable_result(NEW_DASGN_CURR(id, val)); - } - } - else { - if (!local_id(id)) { - local_var(id); - } - return assignable_result(NEW_LASGN(id, val)); - } - break; - case ID_GLOBAL: - return assignable_result(NEW_GASGN(id, val)); - case ID_INSTANCE: - return assignable_result(NEW_IASGN(id, val)); + if (dyna_in_block(p)) { + if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) { + compile_error(p, "Can't assign to numbered parameter _%d", + NUMPARAM_ID_TO_IDX(id)); + return -1; + } + if (dvar_curr(p, id)) return NODE_DASGN; + if (dvar_defined(p, id)) return NODE_DASGN; + if (local_id(p, id)) return NODE_LASGN; + dyna_var(p, id); + return NODE_DASGN; + } + else { + if (!local_id(p, id)) local_var(p, id); + return NODE_LASGN; + } + break; + case ID_GLOBAL: return NODE_GASGN; + case ID_INSTANCE: return NODE_IASGN; case ID_CONST: - if (!in_def && !in_single) - return assignable_result(NEW_CDECL(id, val, 0)); - yyerror("dynamic constant assignment"); - break; - case ID_CLASS: - return assignable_result(NEW_CVASGN(id, val)); + if (!p->ctxt.in_def) return NODE_CDECL; + *err = "dynamic constant assignment"; + return -1; + case ID_CLASS: return NODE_CVASGN; default: - compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id)); + compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id)); } - error: - return assignable_result(0); -#undef assignable_result -#undef parser_yyerror + return -1; +} + +static NODE* +assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc) +{ + const char *err = 0; + int node_type = assignable0(p, id, &err); + switch (node_type) { + case NODE_DASGN: return NEW_DASGN(id, val, 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, p->ctxt.shareable_constant_value, loc); + case NODE_CVASGN: return NEW_CVASGN(id, val, loc); + } +/* TODO: FIXME */ +#ifndef RIPPER + if (err) yyerror1(loc, err); +#else + if (err) set_value(assign_error(p, err, p->s_lvalue)); +#endif + return NEW_ERROR(loc); } static int -is_private_local_id(ID name) +is_private_local_id(struct parser_params *p, ID name) { VALUE s; if (name == idUScore) return 1; @@ -9128,260 +13564,409 @@ is_private_local_id(ID name) return RSTRING_PTR(s)[0] == '_'; } -#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1)) - static int -shadowing_lvar_0(struct parser_params *parser, ID name) -{ - if (is_private_local_id(name)) return 1; - if (dyna_in_block()) { - if (dvar_curr(name)) { - yyerror("duplicated argument name"); - } - else if (dvar_defined_get(name) || local_id(name)) { - rb_warning1("shadowing outer local variable - %"PRIsWARN, rb_id2str(name)); - vtable_add(lvtbl->vars, name); - if (lvtbl->used) { - vtable_add(lvtbl->used, (ID)ruby_sourceline | LVAR_USED); - } - return 0; - } +shadowing_lvar_0(struct parser_params *p, ID name) +{ + if (dyna_in_block(p)) { + if (dvar_curr(p, name)) { + if (is_private_local_id(p, name)) return 1; + yyerror0("duplicated argument name"); + } + else if (dvar_defined(p, name) || local_id(p, name)) { + vtable_add(p->lvtbl->vars, name); + if (p->lvtbl->used) { + vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED); + } + return 0; + } } else { - if (local_id(name)) { - yyerror("duplicated argument name"); - } + if (local_id(p, name)) { + if (is_private_local_id(p, name)) return 1; + yyerror0("duplicated argument name"); + } } return 1; } static ID -shadowing_lvar_gen(struct parser_params *parser, ID name) +shadowing_lvar(struct parser_params *p, ID name) { - shadowing_lvar_0(parser, name); + shadowing_lvar_0(p, name); return name; } static void -new_bv_gen(struct parser_params *parser, ID name) +new_bv(struct parser_params *p, ID name) { if (!name) return; if (!is_local_id(name)) { - compile_error(PARSER_ARG "invalid local variable - %"PRIsVALUE, - rb_id2str(name)); - return; + compile_error(p, "invalid local variable - %"PRIsVALUE, + rb_id2str(name)); + return; + } + 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"); } - if (!shadowing_lvar_0(parser, name)) return; - dyna_var(name); } -#ifndef RIPPER static NODE * -aryset_gen(struct parser_params *parser, NODE *recv, NODE *idx) +aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc) { - return NEW_ATTRASGN(recv, tASET, idx); + aryset_check(p, idx); + return NEW_ATTRASGN(recv, tASET, idx, loc); } static void -block_dup_check_gen(struct parser_params *parser, NODE *node1, NODE *node2) +block_dup_check(struct parser_params *p, NODE *node1, NODE *node2) { - if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) { - compile_error(PARSER_ARG "both block arg and actual block given"); + if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) { + compile_error(p, "both block arg and actual block given"); } } static NODE * -attrset_gen(struct parser_params *parser, NODE *recv, ID atype, ID id) +attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc) { - if (atype != tDOTQ) id = rb_id_attrset(id); - return NEW_ATTRASGN(recv, id, 0); + if (!CALL_Q_P(atype)) id = rb_id_attrset(id); + return NEW_ATTRASGN(recv, id, 0, loc); } -static void -rb_backref_error_gen(struct parser_params *parser, NODE *node) +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(PARSER_ARG "Can't set variable $%ld", node->nd_nth); - break; + return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth); case NODE_BACK_REF: - compile_error(PARSER_ARG "Can't set variable $%c", (int)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 */ } static NODE * -arg_concat_gen(struct parser_params *parser, NODE *node1, NODE *node2) +arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc) { - if (!node2) return node1; - switch (nd_type(node1)) { + if (!node1) return NEW_LIST(node2, &node2->nd_loc); + switch (nd_type(node1)) { + case NODE_LIST: + return list_append(p, node1, node2); case NODE_BLOCK_PASS: - if (node1->nd_head) - node1->nd_head = arg_concat(node1->nd_head, node2); - else - node1->nd_head = NEW_LIST(node2); - return node1; + RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc); + node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos; + return node1; case NODE_ARGSPUSH: - if (nd_type(node2) != NODE_ARRAY) break; - node1->nd_body = list_concat(NEW_LIST(node1->nd_body), node2); - nd_set_type(node1, NODE_ARGSCAT); - return node1; + RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2); + node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos; + nd_set_type(node1, NODE_ARGSCAT); + return node1; case NODE_ARGSCAT: - if (nd_type(node2) != NODE_ARRAY || - nd_type(node1->nd_body) != NODE_ARRAY) break; - node1->nd_body = list_concat(node1->nd_body, node2); - return node1; + if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break; + RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2); + node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos; + return node1; } - return NEW_ARGSCAT(node1, node2); + return NEW_ARGSPUSH(node1, node2, loc); } static NODE * -arg_append_gen(struct parser_params *parser, NODE *node1, NODE *node2) +arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc) { - if (!node1) return NEW_LIST(node2); - switch (nd_type(node1)) { - case NODE_ARRAY: - return list_append(node1, node2); + if (!node2) return node1; + switch (nd_type(node1)) { case NODE_BLOCK_PASS: - node1->nd_head = arg_append(node1->nd_head, node2); - return node1; + if (RNODE_BLOCK_PASS(node1)->nd_head) + RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc); + else + RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc); + return node1; case NODE_ARGSPUSH: - node1->nd_body = list_append(NEW_LIST(node1->nd_body), node2); - nd_set_type(node1, NODE_ARGSCAT); - return node1; + if (!nd_type_p(node2, NODE_LIST)) break; + RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2); + nd_set_type(node1, NODE_ARGSCAT); + return node1; + case NODE_ARGSCAT: + if (!nd_type_p(node2, NODE_LIST) || + !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break; + RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2); + return node1; } - return NEW_ARGSPUSH(node1, node2); + return NEW_ARGSCAT(node1, node2, loc); +} + +static NODE * +last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc) +{ + NODE *n1; + if ((n1 = splat_array(args)) != 0) { + return list_append(p, n1, last_arg); + } + return arg_append(p, args, last_arg, loc); +} + +static NODE * +rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc) +{ + NODE *n1; + if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) { + return list_concat(n1, rest_arg); + } + return arg_concat(p, args, rest_arg, loc); } static NODE * splat_array(NODE* node) { - if (nd_type(node) == NODE_SPLAT) node = node->nd_head; - if (nd_type(node) == NODE_ARRAY) return node; + if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head; + if (nd_type_p(node, NODE_LIST)) return node; return 0; } +static void +mark_lvar_used(struct parser_params *p, NODE *rhs) +{ + ID *vidp = NULL; + if (!rhs) return; + switch (nd_type(rhs)) { + case NODE_LASGN: + if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) { + if (vidp) *vidp |= LVAR_USED; + } + break; + case NODE_DASGN: + if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) { + if (vidp) *vidp |= LVAR_USED; + } + break; +#if 0 + case NODE_MASGN: + for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) { + mark_lvar_used(p, rhs->nd_head); + } + break; +#endif + } +} + +static int is_static_content(NODE *node); + static NODE * -node_assign_gen(struct parser_params *parser, NODE *lhs, NODE *rhs) +node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc) { if (!lhs) return 0; switch (nd_type(lhs)) { + case NODE_CDECL: case NODE_GASGN: case NODE_IASGN: - case NODE_IASGN2: case NODE_LASGN: case NODE_DASGN: - case NODE_DASGN_CURR: case NODE_MASGN: - case NODE_CDECL: case NODE_CVASGN: - lhs->nd_value = rhs; - break; + set_nd_value(p, lhs, rhs); + nd_set_loc(lhs, loc); + break; case NODE_ATTRASGN: - case NODE_CALL: - lhs->nd_args = arg_append(lhs->nd_args, rhs); - break; + RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc); + nd_set_loc(lhs, loc); + break; default: - /* should not happen */ - break; + /* should not happen */ + break; } return lhs; } -static int -value_expr_gen(struct parser_params *parser, NODE *node) +static NODE * +value_expr_check(struct parser_params *p, NODE *node) { - int cond = 0; + NODE *void_node = 0, *vn; if (!node) { - rb_warning0("empty expression"); + rb_warning0("empty expression"); } while (node) { - switch (nd_type(node)) { - case NODE_RETURN: - case NODE_BREAK: - case NODE_NEXT: - case NODE_REDO: - case NODE_RETRY: - if (!cond) yyerror("void value expression"); - /* or "control never reach"? */ - return FALSE; - - case NODE_BLOCK: - while (node->nd_next) { - node = node->nd_next; - } - node = node->nd_head; - break; - - case NODE_BEGIN: - node = node->nd_body; - break; - - case NODE_IF: - if (!node->nd_body) { - node = node->nd_else; - break; - } - else if (!node->nd_else) { - node = node->nd_body; - break; - } - if (!value_expr(node->nd_body)) return FALSE; - node = node->nd_else; - break; - - case NODE_AND: - case NODE_OR: - cond = 1; - node = node->nd_2nd; - break; - - default: - return TRUE; - } + 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))) 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))) { + void_node = 0; + break; + } + 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: + 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; + } + if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) { + return NULL; + } + /* single line pattern matching with "=>" operator */ + goto found; + + case NODE_BLOCK: + while (RNODE_BLOCK(node)->nd_next) { + node = RNODE_BLOCK(node)->nd_next; + } + node = RNODE_BLOCK(node)->nd_head; + break; + + case NODE_BEGIN: + node = RNODE_BEGIN(node)->nd_body; + break; + + case NODE_IF: + case NODE_UNLESS: + if (!RNODE_IF(node)->nd_body) { + return NULL; + } + else if (!RNODE_IF(node)->nd_else) { + return NULL; + } + vn = value_expr_check(p, RNODE_IF(node)->nd_body); + if (!vn) return NULL; + if (!void_node) void_node = vn; + node = RNODE_IF(node)->nd_else; + break; + + case NODE_AND: + case NODE_OR: + node = RNODE_AND(node)->nd_1st; + break; + + case NODE_LASGN: + case NODE_DASGN: + case NODE_MASGN: + mark_lvar_used(p, node); + return NULL; + + default: + return NULL; + } } + return NULL; + + found: + /* return the first found node */ + return void_node ? void_node : node; +} + +static int +value_expr(struct parser_params *p, NODE *node) +{ + NODE *void_node = value_expr_check(p, node); + if (void_node) { + yyerror1(&void_node->nd_loc, "void value expression"); + /* or "control never reach"? */ + return FALSE; + } return TRUE; } static void -void_expr_gen(struct parser_params *parser, NODE *node) +void_expr(struct parser_params *p, NODE *node) { const char *useless = 0; if (!RTEST(ruby_verbose)) return; - if (!node) return; + if (!node || !(node = nd_once_body(node))) return; switch (nd_type(node)) { - case NODE_CALL: - switch (node->nd_mid) { - case '+': - case '-': - case '*': - case '/': - case '%': - case tPOW: - case tUPLUS: - case tUMINUS: - case '|': - case '^': - case '&': - case tCMP: - case '>': - case tGEQ: - case '<': - case tLEQ: - case tEQ: - case tNEQ: - useless = rb_id2name(node->nd_mid); - break; - } - break; + case NODE_OPCALL: + switch (RNODE_OPCALL(node)->nd_mid) { + case '+': + case '-': + case '*': + case '/': + case '%': + case tPOW: + case tUPLUS: + case tUMINUS: + case '|': + case '^': + case '&': + case tCMP: + case '>': + case tGEQ: + case '<': + case tLEQ: + case tEQ: + case tNEQ: + useless = rb_id2name(RNODE_OPCALL(node)->nd_mid); + break; + } + break; case NODE_LVAR: case NODE_DVAR: @@ -9390,140 +13975,137 @@ void_expr_gen(struct parser_params *parser, NODE *node) case NODE_CVAR: case NODE_NTH_REF: case NODE_BACK_REF: - useless = "a variable"; - break; + useless = "a variable"; + break; case NODE_CONST: - useless = "a constant"; - break; - case NODE_LIT: + useless = "a constant"; + break; + 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: - case NODE_DREGX_ONCE: - useless = "a literal"; - break; + useless = "a literal"; + break; case NODE_COLON2: case NODE_COLON3: - useless = "::"; - break; + useless = "::"; + break; case NODE_DOT2: - useless = ".."; - break; + useless = ".."; + break; case NODE_DOT3: - useless = "..."; - break; + useless = "..."; + break; case NODE_SELF: - useless = "self"; - break; + useless = "self"; + break; case NODE_NIL: - useless = "nil"; - break; + useless = "nil"; + break; case NODE_TRUE: - useless = "true"; - break; + useless = "true"; + break; case NODE_FALSE: - useless = "false"; - break; + useless = "false"; + break; case NODE_DEFINED: - useless = "defined?"; - break; + useless = "defined?"; + break; } if (useless) { - rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless)); + rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless)); } } -static void -void_stmts_gen(struct parser_params *parser, NODE *node) +/* warns useless use of block and returns the last statement node */ +static NODE * +void_stmts(struct parser_params *p, NODE *node) { - if (!RTEST(ruby_verbose)) return; - if (!node) return; - if (nd_type(node) != NODE_BLOCK) return; + NODE *const n = node; + if (!RTEST(ruby_verbose)) return n; + if (!node) return n; + if (!nd_type_p(node, NODE_BLOCK)) return n; - for (;;) { - if (!node->nd_next) return; - void_expr0(node->nd_head); - node = node->nd_next; + while (RNODE_BLOCK(node)->nd_next) { + void_expr(p, RNODE_BLOCK(node)->nd_head); + node = RNODE_BLOCK(node)->nd_next; } + return RNODE_BLOCK(node)->nd_head; } static NODE * remove_begin(NODE *node) { NODE **n = &node, *n1 = node; - while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) { - *n = n1 = n1->nd_body; - } - return node; -} - -static NODE * -remove_begin_all(NODE *node) -{ - NODE **n = &node, *n1 = node; - while (n1 && nd_type(n1) == NODE_BEGIN) { - *n = n1 = n1->nd_body; + while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) { + *n = n1 = RNODE_BEGIN(n1)->nd_body; } return node; } static void -reduce_nodes_gen(struct parser_params *parser, NODE **body) +reduce_nodes(struct parser_params *p, NODE **body) { NODE *node = *body; if (!node) { - *body = NEW_NIL(); - return; + *body = NEW_NIL(&NULL_LOC); + return; } -#define subnodes(n1, n2) \ - ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \ - (!node->n2) ? (body = &node->n1, 1) : \ - (reduce_nodes(&node->n1), body = &node->n2, 1)) +#define subnodes(type, n1, n2) \ + ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \ + (!type(node)->n2) ? (body = &type(node)->n1, 1) : \ + (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1)) while (node) { - int newline = (int)(node->flags & NODE_FL_NEWLINE); - switch (nd_type(node)) { - end: - case NODE_NIL: - *body = 0; - return; - case NODE_RETURN: - *body = node = node->nd_stts; - if (newline && node) node->flags |= NODE_FL_NEWLINE; - continue; - case NODE_BEGIN: - *body = node = node->nd_body; - if (newline && node) node->flags |= NODE_FL_NEWLINE; - continue; - case NODE_BLOCK: - body = &node->nd_end->nd_head; - break; - case NODE_IF: - if (subnodes(nd_body, nd_else)) break; - return; - case NODE_CASE: - body = &node->nd_body; - break; - case NODE_WHEN: - if (!subnodes(nd_body, nd_next)) goto end; - break; - case NODE_ENSURE: - if (!subnodes(nd_head, nd_resq)) goto end; - break; - case NODE_RESCUE: - if (node->nd_else) { - body = &node->nd_resq; - break; - } - if (!subnodes(nd_head, nd_resq)) goto end; - break; - default: - return; - } - node = *body; - if (newline && node) node->flags |= NODE_FL_NEWLINE; + int newline = (int)nd_fl_newline(node); + switch (nd_type(node)) { + end: + case NODE_NIL: + *body = 0; + return; + case NODE_BEGIN: + *body = node = RNODE_BEGIN(node)->nd_body; + if (newline && node) nd_set_fl_newline(node); + continue; + case NODE_BLOCK: + body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head; + break; + case NODE_IF: + case NODE_UNLESS: + if (subnodes(RNODE_IF, nd_body, nd_else)) break; + return; + case NODE_CASE: + body = &RNODE_CASE(node)->nd_body; + break; + case NODE_WHEN: + if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end; + break; + case NODE_ENSURE: + body = &RNODE_ENSURE(node)->nd_head; + break; + case NODE_RESCUE: + newline = 0; // RESBODY should not be a NEWLINE + if (RNODE_RESCUE(node)->nd_else) { + body = &RNODE_RESCUE(node)->nd_resq; + break; + } + if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end; + break; + default: + return; + } + node = *body; + if (newline && node) nd_set_fl_newline(node); } #undef subnodes @@ -9535,864 +14117,1225 @@ is_static_content(NODE *node) if (!node) return 1; switch (nd_type(node)) { case NODE_HASH: - if (!(node = node->nd_head)) break; - case NODE_ARRAY: - do { - if (!is_static_content(node->nd_head)) return 0; - } while ((node = node->nd_next) != 0); - case NODE_LIT: + if (!(node = RNODE_HASH(node)->nd_head)) break; + case NODE_LIST: + do { + if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0; + } while ((node = RNODE_LIST(node)->nd_next) != 0); + 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: case NODE_FALSE: - case NODE_ZARRAY: - break; + case NODE_ZLIST: + break; default: - return 0; + return 0; } return 1; } static int -assign_in_cond(struct parser_params *parser, NODE *node) +assign_in_cond(struct parser_params *p, NODE *node) { switch (nd_type(node)) { case NODE_MASGN: - yyerror("multiple assignment in conditional"); - return 1; - case NODE_LASGN: case NODE_DASGN: - case NODE_DASGN_CURR: case NODE_GASGN: case NODE_IASGN: - break; + case NODE_CVASGN: + case NODE_CDECL: + break; default: - return 0; + return 0; } - if (!node->nd_value) return 1; - if (is_static_content(node->nd_value)) { - /* reports always */ - parser_warn(node->nd_value, "found = in conditional, should be =="); + if (!get_nd_value(p, node)) return 1; + if (is_static_content(get_nd_value(p, node))) { + /* reports always */ + rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be =="); } return 1; } -static void -warn_unless_e_option(struct parser_params *parser, NODE *node, const char *str) -{ - if (!e_option_supplied(parser)) parser_warn(node, str); -} - -static void -warning_unless_e_option(struct parser_params *parser, NODE *node, const char *str) -{ - if (!e_option_supplied(parser)) parser_warning(node, str); -} - -static void -fixup_nodes(NODE **rootnode) -{ - NODE *node, *next, *head; - - for (node = *rootnode; node; node = next) { - enum node_type type; - VALUE val; +enum cond_type { + COND_IN_OP, + COND_IN_COND, + COND_IN_FF +}; - next = node->nd_next; - head = node->nd_head; - rb_gc_force_recycle((VALUE)node); - *rootnode = next; - switch (type = nd_type(head)) { - case NODE_DOT2: - case NODE_DOT3: - val = rb_range_new(head->nd_beg->nd_lit, head->nd_end->nd_lit, - type == NODE_DOT3); - rb_gc_force_recycle((VALUE)head->nd_beg); - rb_gc_force_recycle((VALUE)head->nd_end); - nd_set_type(head, NODE_LIT); - head->nd_lit = val; - break; - default: - break; - } - } -} +#define SWITCH_BY_COND_TYPE(t, w, arg) do { \ + switch (t) { \ + case COND_IN_OP: break; \ + case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \ + case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \ + } \ +} while (0) -static NODE *cond0(struct parser_params*,NODE*); +static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool); static NODE* -range_op(struct parser_params *parser, NODE *node) +range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc) { enum node_type type; if (node == 0) return 0; type = nd_type(node); - value_expr(node); - if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) { - warn_unless_e_option(parser, node, "integer literal in conditional range"); - return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$.")))); - } - return cond0(parser, node); -} - -static int -literal_node(NODE *node) -{ - if (!node) return 1; /* same as NODE_NIL */ - switch (nd_type(node)) { - case NODE_LIT: - case NODE_STR: - case NODE_DSTR: - case NODE_EVSTR: - case NODE_DREGX: - case NODE_DREGX_ONCE: - case NODE_DSYM: - return 2; - case NODE_TRUE: - case NODE_FALSE: - case NODE_NIL: - return 1; + 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 0; + return cond0(p, node, COND_IN_FF, loc, true); } static NODE* -cond0(struct parser_params *parser, NODE *node) +cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top) { if (node == 0) return 0; - assign_in_cond(parser, node); + 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: - rb_warn0("string literal in condition"); - break; + 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: - case NODE_DREGX_ONCE: - warning_unless_e_option(parser, node, "regex literal in condition"); - return NEW_MATCH2(node, NEW_GVAR(rb_intern("$_"))); + if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex "); + + return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc); + + case NODE_BLOCK: + { + 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: - node->nd_1st = cond0(parser, node->nd_1st); - node->nd_2nd = cond0(parser, node->nd_2nd); - break; + 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: - node->nd_beg = range_op(parser, node->nd_beg); - node->nd_end = range_op(parser, node->nd_end); - if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2); - else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3); - if (!e_option_supplied(parser)) { - int b = literal_node(node->nd_beg); - int e = literal_node(node->nd_end); - if ((b == 1 && e == 1) || (b + e >= 2 && RTEST(ruby_verbose))) { - parser_warn(node, "range literal in condition"); - } - } - break; + 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); + 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: - parser_warning(node, "literal in condition"); - break; - - case NODE_LIT: - if (RB_TYPE_P(node->nd_lit, T_REGEXP)) { - warn_unless_e_option(parser, node, "regex literal in condition"); - nd_set_type(node, NODE_MATCH); - } - else { - parser_warning(node, "literal in condition"); - } + SWITCH_BY_COND_TYPE(type, warning, "symbol "); + break; + + 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; + break; } return node; } static NODE* -cond_gen(struct parser_params *parser, NODE *node) +cond(struct parser_params *p, NODE *node, const YYLTYPE *loc) { if (node == 0) return 0; - return cond0(parser, node); + 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, true); +} + +static NODE* +new_nil_at(struct parser_params *p, const rb_code_position_t *pos) +{ + YYLTYPE loc = {*pos, *pos}; + return NEW_NIL(&loc); } static NODE* -new_if_gen(struct parser_params *parser, NODE *cc, NODE *left, NODE *right) +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(parser, cc); - switch (nd_type(cc)) { - case NODE_NIL: - case NODE_FALSE: - return right; - case NODE_TRUE: - case NODE_LIT: - case NODE_STR: - return left; - } - return NEW_IF(cc, left, right); + 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, 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, 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, op_loc) (type == NODE_AND ? NEW_AND(f,s,loc,op_loc) : NEW_OR(f,s,loc,op_loc)) + static NODE* -logop_gen(struct parser_params *parser, enum node_type type, NODE *left, NODE *right) -{ - value_expr(left); - if (!left) { - if (!in_defined && type == NODE_AND) return 0; - /* make NODE_OR not to be "void value expression" */ - } - else if ((enum node_type)nd_type(left) == type) { - NODE *node = left, *second; - while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) { - node = second; - } - node->nd_2nd = NEW_NODE(type, second, right, 0); - return left; - } - else if (!in_defined) { - switch (nd_type(left)) { - case NODE_NIL: - case NODE_FALSE: - if (type == NODE_AND) return left; - break; - case NODE_TRUE: - case NODE_LIT: - case NODE_STR: - if (type != NODE_AND) return left; - nd_set_type(left, NODE_TRUE); - break; - } - } - return NEW_NODE(type, left, right, 0); +logop(struct parser_params *p, ID id, NODE *left, NODE *right, + const YYLTYPE *op_loc, const YYLTYPE *loc) +{ + enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR; + NODE *op; + 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, 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_loc); + nd_set_line(op, op_loc->beg_pos.lineno); + return op; } +#undef NEW_AND_OR + static void -no_blockarg(struct parser_params *parser, NODE *node) +no_blockarg(struct parser_params *p, NODE *node) { - if (node && nd_type(node) == NODE_BLOCK_PASS) { - compile_error(PARSER_ARG "block argument should not be given"); + if (nd_type_p(node, NODE_BLOCK_PASS)) { + compile_error(p, "block argument should not be given"); } } static NODE * -ret_args_gen(struct parser_params *parser, NODE *node) +ret_args(struct parser_params *p, NODE *node) { if (node) { - no_blockarg(parser, node); - if (nd_type(node) == NODE_ARRAY) { - if (node->nd_next == 0) { - node = node->nd_head; - } - else { - nd_set_type(node, NODE_VALUES); - } - } + no_blockarg(p, node); + if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) { + node = RNODE_LIST(node)->nd_head; + } + } + return node; +} + +static NODE* +negate_lit(struct parser_params *p, NODE* node) +{ + switch (nd_type(node)) { + case NODE_INTEGER: + RNODE_INTEGER(node)->minus = TRUE; + break; + case NODE_FLOAT: + RNODE_FLOAT(node)->minus = TRUE; + break; + case NODE_RATIONAL: + RNODE_RATIONAL(node)->minus = TRUE; + break; + case NODE_IMAGINARY: + RNODE_IMAGINARY(node)->minus = TRUE; + break; } return node; } static NODE * -new_yield_gen(struct parser_params *parser, NODE *node) +arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2) { - if (node) no_blockarg(parser, node); + if (node2) { + if (!node1) return (NODE *)node2; + node2->nd_head = node1; + nd_set_first_lineno(node2, nd_first_lineno(node1)); + nd_set_first_column(node2, nd_first_column(node1)); + return (NODE *)node2; + } + return node1; +} - return NEW_YIELD(node); +static bool +args_info_empty_p(struct rb_args_info *args) +{ + if (args->pre_args_num) return false; + if (args->post_args_num) return false; + if (args->rest_arg) return false; + if (args->opt_args) return false; + if (args->block_arg) return false; + if (args->kw_args) return false; + if (args->kw_rest_arg) return false; + return true; } -static VALUE -negate_lit(VALUE lit) +static rb_node_args_t * +new_args(struct parser_params *p, rb_node_args_aux_t *pre_args, rb_node_opt_arg_t *opt_args, ID rest_arg, rb_node_args_aux_t *post_args, rb_node_args_t *tail, const YYLTYPE *loc) { - int type = TYPE(lit); - switch (type) { - case T_FIXNUM: - lit = LONG2FIX(-FIX2LONG(lit)); - break; - case T_BIGNUM: - case T_RATIONAL: - case T_COMPLEX: - lit = rb_funcallv(lit, tUMINUS, 0, 0); - break; - case T_FLOAT: -#if USE_FLONUM - if (FLONUM_P(lit)) { - lit = DBL2NUM(-RFLOAT_VALUE(lit)); - break; - } -#endif - RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit); - break; - default: - rb_bug("unknown literal type (%d) passed to negate_lit", type); - break; + struct rb_args_info *args = &tail->nd_ainfo; + + if (args->forwarding) { + if (rest_arg) { + yyerror1(&RNODE(tail)->nd_loc, "... after rest argument"); + return tail; + } + rest_arg = idFWD_REST; } - return lit; + + 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 ? 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; + + args->rest_arg = rest_arg; + + args->opt_args = opt_args; + + nd_set_loc(RNODE(tail), loc); + + return tail; } -static NODE * -arg_blk_pass(NODE *node1, NODE *node2) +static rb_node_args_t * +new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc) +{ + rb_node_args_t *node = NEW_ARGS(&NULL_LOC); + struct rb_args_info *args = &node->nd_ainfo; + if (p->error_p) return node; + + args->block_arg = block; + args->kw_args = kw_args; + + if (kw_args) { + /* + * def foo(k1: 1, kr1:, k2: 2, **krest, &b) + * variable order: k1, kr1, k2, &b, internal_id, krest + * #=> <reorder> + * variable order: kr1, k1, k2, internal_id, krest, &b + */ + ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars; + struct vtable *vtargs = p->lvtbl->args; + rb_node_kw_arg_t *kwn = kw_args; + + if (block) block = vtargs->tbl[vtargs->pos-1]; + vtable_pop(vtargs, !!block + !!kw_rest_arg); + required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos]; + while (kwn) { + if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) + --kw_vars; + --required_kw_vars; + kwn = kwn->nd_next; + } + + for (kwn = kw_args; kwn; kwn = kwn->nd_next) { + ID vid = get_nd_vid(p, kwn->nd_body); + if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) { + *required_kw_vars++ = vid; + } + else { + *kw_vars++ = vid; + } + } + + arg_var(p, kw_bits); + if (kw_rest_arg) arg_var(p, kw_rest_arg); + if (block) arg_var(p, block); + + args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc); + } + else if (kw_rest_arg == idNil) { + args->no_kwarg = 1; + } + else if (kw_rest_arg) { + args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc); + } + + return node; +} + +static rb_node_args_t * +args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id) { - if (node2) { - node2->nd_head = node1; - return node2; + if (max_numparam > NO_PARAM || it_id) { + if (!args) { + YYLTYPE loc = RUBY_INIT_YYLLOC(); + args = new_args_tail(p, 0, 0, 0, 0); + nd_set_loc(RNODE(args), &loc); + } + args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam; } - return node1; + return args; } +static NODE* +new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc) +{ + RNODE_ARYPTN(aryptn)->nd_pconst = constant; + + if (pre_arg) { + NODE *pre_args = NEW_LIST(pre_arg, loc); + if (RNODE_ARYPTN(aryptn)->pre_args) { + RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args); + } + else { + RNODE_ARYPTN(aryptn)->pre_args = pre_args; + } + } + return aryptn; +} static NODE* -new_args_gen(struct parser_params *parser, NODE *m, NODE *o, ID r, NODE *p, NODE *tail) +new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc) { - int saved_line = ruby_sourceline; - struct rb_args_info *args = tail->nd_ainfo; + if (has_rest) { + rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST; + } + else { + rest_arg = NULL; + } + NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc); - args->pre_args_num = m ? rb_long2int(m->nd_plen) : 0; - args->pre_init = m ? m->nd_next : 0; + return node; +} - args->post_args_num = p ? rb_long2int(p->nd_plen) : 0; - args->post_init = p ? p->nd_next : 0; - args->first_post_arg = p ? p->nd_pid : 0; +static NODE* +new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc) +{ + RNODE_FNDPTN(fndptn)->nd_pconst = constant; - args->rest_arg = r; + return fndptn; +} - args->opt_args = o; +static NODE* +new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc) +{ + pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST; + post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST; + NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc); - ruby_sourceline = saved_line; + return node; +} - return tail; +static NODE* +new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc) +{ + RNODE_HSHPTN(hshptn)->nd_pconst = constant; + return hshptn; } static NODE* -new_args_tail_gen(struct parser_params *parser, NODE *k, ID kr, ID b) +new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc) { - int saved_line = ruby_sourceline; - struct rb_args_info *args; - NODE *node; + NODE *node, *kw_rest_arg_node; + + if (kw_rest_arg == idNil) { + kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD; + } + else if (kw_rest_arg) { + kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc); + } + else { + kw_rest_arg_node = NULL; + } + + node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc); - args = ZALLOC(struct rb_args_info); - node = NEW_NODE(NODE_ARGS, 0, 0, args); - - args->block_arg = b; - args->kw_args = k; - - if (k) { - /* - * def foo(k1: 1, kr1:, k2: 2, **krest, &b) - * variable order: k1, kr1, k2, &b, internal_id, krest - * #=> <reorder> - * variable order: kr1, k1, k2, internal_id, krest, &b - */ - ID kw_bits; - NODE *kwn = k; - struct vtable *required_kw_vars = vtable_alloc(NULL); - struct vtable *kw_vars = vtable_alloc(NULL); - int i; - - while (kwn) { - NODE *val_node = kwn->nd_body->nd_value; - ID vid = kwn->nd_body->nd_vid; - - if (val_node == (NODE *)-1) { - vtable_add(required_kw_vars, vid); - } - else { - vtable_add(kw_vars, vid); - } - - kwn = kwn->nd_next; - } - - kw_bits = internal_id(); - if (kr && is_junk_id(kr)) vtable_pop(lvtbl->args, 1); - vtable_pop(lvtbl->args, vtable_size(required_kw_vars) + vtable_size(kw_vars) + (b != 0)); - - for (i=0; i<vtable_size(required_kw_vars); i++) arg_var(required_kw_vars->tbl[i]); - for (i=0; i<vtable_size(kw_vars); i++) arg_var(kw_vars->tbl[i]); - vtable_free(required_kw_vars); - vtable_free(kw_vars); - - arg_var(kw_bits); - if (kr) arg_var(kr); - if (b) arg_var(b); - - args->kw_rest_arg = NEW_DVAR(kw_bits); - args->kw_rest_arg->nd_cflag = kr; - } - else if (kr) { - if (b) vtable_pop(lvtbl->args, 1); /* reorder */ - arg_var(kr); - if (b) arg_var(b); - args->kw_rest_arg = NEW_DVAR(kr); - } - - ruby_sourceline = saved_line; return node; } static NODE* -dsym_node_gen(struct parser_params *parser, NODE *node) +dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc) { - VALUE lit; - if (!node) { - return NEW_LIT(ID2SYM(idNULL)); + return NEW_SYM(STR_NEW0(), loc); } switch (nd_type(node)) { case NODE_DSTR: - nd_set_type(node, NODE_DSYM); - break; + nd_set_type(node, NODE_DSYM); + nd_set_loc(node, loc); + break; case NODE_STR: - lit = node->nd_lit; - node->nd_lit = ID2SYM(rb_intern_str(lit)); - nd_set_type(node, NODE_LIT); - break; + node = str_to_sym_node(p, node, loc); + break; default: - node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node)); - break; + 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; - node->nd_alen = 2; - node->nd_next->nd_end = node->nd_next; - 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 VALUE +nd_value(struct parser_params *p, NODE *node) +{ + 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); + } +} + +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 data; + + /* keyword splat, e.g. {k: 1, **z, k: 2} */ + if (!head) { + head = value; + } + + 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); + } + hash = next; + } + st_free_table(p->warn_duplicate_keys_table); + p->warn_duplicate_keys_table = NULL; } static NODE * -remove_duplicate_keys(struct parser_params *parser, NODE *hash) -{ - st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2); - NODE *result = 0; - while (hash && hash->nd_head && hash->nd_next) { - NODE *head = hash->nd_head; - NODE *value = hash->nd_next; - NODE *next = value->nd_next; - VALUE key = (VALUE)head; - st_data_t data; - if (nd_type(head) == NODE_LIT && - st_lookup(literal_keys, (key = head->nd_lit), &data)) { - rb_compile_warn(ruby_sourcefile, nd_line((NODE *)data), - "key %+"PRIsVALUE" is duplicated and overwritten on line %d", - head->nd_lit, nd_line(head)); - head = ((NODE *)data)->nd_next; - head->nd_head = block_append(head->nd_head, value->nd_head); - } - else { - st_insert(literal_keys, (st_data_t)key, (st_data_t)hash); - } - 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); +new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc) +{ + if (hash) warn_duplicate_keys(p, hash); + return NEW_HASH(hash, loc); +} + +static void +error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc) +{ + if (is_private_local_id(p, id)) { + return; + } + 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); } - return result; +} + +static void +error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc) +{ + if (!p->pktbl) { + p->pktbl = st_init_numtable(); + } + else if (st_is_member(p->pktbl, key)) { + yyerror1(loc, "duplicated key name"); + return; + } + st_insert(p->pktbl, (st_data_t)key, 0); } static NODE * -new_hash_gen(struct parser_params *parser, NODE *hash) +new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc) { - if (hash) hash = remove_duplicate_keys(parser, hash); - return NEW_HASH(hash); + return NEW_HASH(hash, loc); } -#endif /* !RIPPER */ -#ifndef RIPPER static NODE * -new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs) +new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc) { NODE *asgn; if (lhs) { - ID vid = lhs->nd_vid; - if (op == tOROP) { - lhs->nd_value = rhs; - asgn = NEW_OP_ASGN_OR(gettable(vid), lhs); - if (is_notop_id(vid)) { - switch (id_type(vid)) { - case ID_GLOBAL: - case ID_INSTANCE: - case ID_CLASS: - asgn->nd_aid = vid; - } - } - } - else if (op == tANDOP) { - lhs->nd_value = rhs; - asgn = NEW_OP_ASGN_AND(gettable(vid), lhs); - } - else { - asgn = lhs; - asgn->nd_value = NEW_CALL(gettable(vid), op, NEW_LIST(rhs)); - } + ID vid = get_nd_vid(p, lhs); + YYLTYPE lhs_loc = lhs->nd_loc; + if (op == tOROP) { + 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) { + set_nd_value(p, lhs, rhs); + nd_set_loc(lhs, loc); + asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc); + } + else { + asgn = lhs; + rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc); + set_nd_value(p, asgn, rhs); + nd_set_loc(asgn, loc); + } } else { - asgn = NEW_BEGIN(0); + asgn = NEW_ERROR(loc); } return asgn; } static NODE * -new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs, - ID atype, ID attr, ID op, NODE *rhs) +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) { NODE *asgn; - if (op == tOROP) { - op = 0; - } - else if (op == tANDOP) { - op = 1; - } - asgn = NEW_OP_ASGN2(lhs, (atype == tDOTQ), attr, op, rhs); + aryset_check(p, args); + args = make_list(args, args_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, + 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, call_operator_loc, message_loc, binary_operator_loc); fixpos(asgn, lhs); return asgn; } static NODE * -new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs) +new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc) { NODE *asgn; - if (op == tOROP) { - op = 0; - } - else if (op == tANDOP) { - op = 1; - } if (lhs) { - asgn = NEW_OP_CDECL(lhs, op, rhs); + asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc); } else { - asgn = NEW_BEGIN(0); + asgn = NEW_ERROR(loc); } fixpos(asgn, lhs); return asgn; } -#else -static VALUE -new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs) + +static NODE * +const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc) { - return dispatch3(opassign, lhs, op, rhs); + if (p->ctxt.in_def) { +#ifndef RIPPER + yyerror1(loc, "dynamic constant assignment"); +#else + set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue)); +#endif + } + return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc); } +#ifdef RIPPER static VALUE -new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs) +assign_error(struct parser_params *p, const char *mesg, VALUE a) { - VALUE recv = dispatch3(field, lhs, type, attr); - return dispatch3(opassign, recv, op, rhs); + a = dispatch2(assign_error, ERR_MESG(), a); + ripper_error(p); + return a; } #endif +static NODE * +new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc) +{ + NODE *result = head; + if (rescue) { + NODE *tmp = rescue_else ? rescue_else : rescue; + YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc); + + result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc); + nd_set_line(result, rescue->nd_loc.beg_pos.lineno); + } + if (ensure) { + result = NEW_ENSURE(result, ensure, loc); + } + fixpos(result, head); + return result; +} + static void -warn_unused_var(struct parser_params *parser, struct local_vars *local) +warn_unused_var(struct parser_params *p, struct local_vars *local) { - int i, cnt; - ID *v, *u; + int cnt; if (!local->used) return; - v = local->vars->tbl; - u = local->used->tbl; cnt = local->used->pos; if (cnt != local->vars->pos) { - rb_bug("local->used->pos != local->vars->pos"); + rb_parser_fatal(p, "local->used->pos != local->vars->pos"); } - for (i = 0; i < cnt; ++i) { - if (!v[i] || (u[i] & LVAR_USED)) continue; - if (is_private_local_id(v[i])) continue; - rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i])); +#ifndef RIPPER + ID *v = local->vars->tbl; + ID *u = local->used->tbl; + for (int i = 0; i < cnt; ++i) { + if (!v[i] || (u[i] & LVAR_USED)) continue; + if (is_private_local_id(p, v[i])) continue; + rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i])); } +#endif } static void -local_push_gen(struct parser_params *parser, int inherit_dvars) +local_push(struct parser_params *p, int toplevel_scope) { struct local_vars *local; + int inherits_dvars = toplevel_scope && compile_for_eval; + int warn_unused_vars = RTEST(ruby_verbose); local = ALLOC(struct local_vars); - local->prev = lvtbl; + local->prev = p->lvtbl; local->args = vtable_alloc(0); - local->vars = vtable_alloc(inherit_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE); - local->used = !(inherit_dvars && - (ifndef_ripper(compile_for_eval || e_option_supplied(parser))+0)) && - RTEST(ruby_verbose) ? vtable_alloc(0) : 0; + local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE); +#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; + local->it = 0; + local->used = warn_unused_vars ? vtable_alloc(0) : 0; + # if WARN_PAST_SCOPE local->past = 0; # endif - local->cmdargs = cmdarg_stack; - cmdarg_stack = 0; - lvtbl = local; + CMDARG_PUSH(0); + COND_PUSH(0); + p->lvtbl = local; } static void -local_pop_gen(struct parser_params *parser) +vtable_chain_free(struct parser_params *p, struct vtable *table) { - struct local_vars *local = lvtbl->prev; - if (lvtbl->used) { - warn_unused_var(parser, lvtbl); - vtable_free(lvtbl->used); + while (!DVARS_TERMINAL_P(table)) { + struct vtable *cur_table = table; + table = cur_table->prev; + vtable_free(cur_table); } +} + +static void +local_free(struct parser_params *p, struct local_vars *local) +{ + vtable_chain_free(p, local->used); + # if WARN_PAST_SCOPE - while (lvtbl->past) { - struct vtable *past = lvtbl->past; - lvtbl->past = past->prev; - vtable_free(past); - } + vtable_chain_free(p, local->past); # endif - vtable_free(lvtbl->args); - vtable_free(lvtbl->vars); - cmdarg_stack = lvtbl->cmdargs; - xfree(lvtbl); - lvtbl = local; + + vtable_chain_free(p, local->args); + vtable_chain_free(p, local->vars); + + ruby_sized_xfree(local, sizeof(struct local_vars)); } -#ifndef RIPPER -static ID* -local_tbl_gen(struct parser_params *parser) +static void +local_pop(struct parser_params *p) { - int cnt_args = vtable_size(lvtbl->args); - int cnt_vars = vtable_size(lvtbl->vars); + struct local_vars *local = p->lvtbl->prev; + if (p->lvtbl->used) { + warn_unused_var(p, p->lvtbl); + } + + local_free(p, p->lvtbl); + p->lvtbl = local; + + CMDARG_POP(); + COND_POP(); +} + +static rb_ast_id_table_t * +local_tbl(struct parser_params *p) +{ + int cnt_args = vtable_size(p->lvtbl->args); + int cnt_vars = vtable_size(p->lvtbl->vars); int cnt = cnt_args + cnt_vars; int i, j; - ID *buf; + rb_ast_id_table_t *tbl; if (cnt <= 0) return 0; - buf = ALLOC_N(ID, cnt + 1); - MEMCPY(buf+1, lvtbl->args->tbl, ID, cnt_args); + tbl = rb_ast_new_local_table(p->ast, cnt); + MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args); /* remove IDs duplicated to warn shadowing */ - for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) { - ID id = lvtbl->vars->tbl[i]; - if (!vtable_included(lvtbl->args, id)) { - buf[j++] = id; - } - } - if (--j < cnt) REALLOC_N(buf, ID, (cnt = j) + 1); - buf[0] = cnt; - return buf; + for (i = 0, j = cnt_args; i < cnt_vars; ++i) { + ID id = p->lvtbl->vars->tbl[i]; + if (!vtable_included(p->lvtbl->args, id)) { + tbl->ids[j++] = id; + } + } + if (j < cnt) { + tbl = rb_ast_resize_latest_local_table(p->ast, j); + } + + return tbl; } -#endif static void -arg_var_gen(struct parser_params *parser, ID id) +numparam_name(struct parser_params *p, ID id) { - vtable_add(lvtbl->args, id); + if (!NUMPARAM_ID_P(id)) return; + compile_error(p, "_%d is reserved for numbered parameter", + NUMPARAM_ID_TO_IDX(id)); } static void -local_var_gen(struct parser_params *parser, ID id) +arg_var(struct parser_params *p, ID id) { - vtable_add(lvtbl->vars, id); - if (lvtbl->used) { - vtable_add(lvtbl->used, (ID)ruby_sourceline); + numparam_name(p, id); + vtable_add(p->lvtbl->args, id); +} + +static void +local_var(struct parser_params *p, ID id) +{ + numparam_name(p, id); + vtable_add(p->lvtbl->vars, id); + if (p->lvtbl->used) { + vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline); } } +#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_gen(struct parser_params *parser, ID id) +local_id_ref(struct parser_params *p, ID id, ID **vidrefp) { struct vtable *vars, *args, *used; - vars = lvtbl->vars; - args = lvtbl->args; - used = lvtbl->used; + vars = p->lvtbl->vars; + args = p->lvtbl->args; + used = p->lvtbl->used; - while (vars && POINTER_P(vars->prev)) { - vars = vars->prev; - args = args->prev; - if (used) used = used->prev; + while (vars && !DVARS_TERMINAL_P(vars->prev)) { + vars = vars->prev; + args = args->prev; + if (used) used = used->prev; } if (vars && vars->prev == DVARS_INHERIT) { - return rb_local_defined(id); + return rb_parser_local_defined(p, id, p->parent_iseq); } else if (vtable_included(args, id)) { - return 1; + return 1; + } + else { + int i = vtable_included(vars, id); + if (i && used && vidrefp) *vidrefp = &used->tbl[i-1]; + return i != 0; + } +} + +static int +local_id(struct parser_params *p, ID id) +{ + return local_id_ref(p, id, NULL); +} + +static int +check_forwarding_args(struct parser_params *p) +{ + if (local_id(p, idFWD_ALL)) return TRUE; + compile_error(p, "unexpected ..."); + return FALSE; +} + +static void +add_forwarding_args(struct parser_params *p) +{ + arg_var(p, idFWD_REST); + arg_var(p, idFWD_KWREST); + arg_var(p, idFWD_BLOCK); + arg_var(p, idFWD_ALL); +} + +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 { - int i = vtable_included(vars, id); - if (i && used) used->tbl[i-1] |= LVAR_USED; - return i != 0; + 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); + NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc)); + 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); +} + +static NODE * +numparam_push(struct parser_params *p) +{ + struct local_vars *local = p->lvtbl; + NODE *inner = local->numparam.inner; + if (!local->numparam.outer) { + local->numparam.outer = local->numparam.current; + } + local->numparam.inner = 0; + local->numparam.current = 0; + local->it = 0; + return inner; +} + +static void +numparam_pop(struct parser_params *p, NODE *prev_inner) +{ + struct local_vars *local = p->lvtbl; + if (prev_inner) { + /* prefer first one */ + local->numparam.inner = prev_inner; + } + else if (local->numparam.current) { + /* current and inner are exclusive */ + local->numparam.inner = local->numparam.current; + } + if (p->max_numparam > NO_PARAM) { + /* current and outer are exclusive */ + local->numparam.current = local->numparam.outer; + local->numparam.outer = 0; + } + else { + /* no numbered parameter */ + local->numparam.current = 0; + } + local->it = 0; +} + static const struct vtable * -dyna_push_gen(struct parser_params *parser) +dyna_push(struct parser_params *p) { - lvtbl->args = vtable_alloc(lvtbl->args); - lvtbl->vars = vtable_alloc(lvtbl->vars); - if (lvtbl->used) { - lvtbl->used = vtable_alloc(lvtbl->used); + p->lvtbl->args = vtable_alloc(p->lvtbl->args); + p->lvtbl->vars = vtable_alloc(p->lvtbl->vars); + if (p->lvtbl->used) { + p->lvtbl->used = vtable_alloc(p->lvtbl->used); } - return lvtbl->args; + return p->lvtbl->args; } static void -dyna_pop_vtable(struct parser_params *parser, struct vtable **vtblp) +dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp) { struct vtable *tmp = *vtblp; *vtblp = tmp->prev; # if WARN_PAST_SCOPE - if (parser->past_scope_enabled) { - tmp->prev = lvtbl->past; - lvtbl->past = tmp; - return; + if (p->past_scope_enabled) { + tmp->prev = p->lvtbl->past; + p->lvtbl->past = tmp; + return; } # endif vtable_free(tmp); } static void -dyna_pop_1(struct parser_params *parser) +dyna_pop_1(struct parser_params *p) { struct vtable *tmp; - if ((tmp = lvtbl->used) != 0) { - warn_unused_var(parser, lvtbl); - lvtbl->used = lvtbl->used->prev; - vtable_free(tmp); + if ((tmp = p->lvtbl->used) != 0) { + warn_unused_var(p, p->lvtbl); + p->lvtbl->used = p->lvtbl->used->prev; + vtable_free(tmp); } - dyna_pop_vtable(parser, &lvtbl->args); - dyna_pop_vtable(parser, &lvtbl->vars); + dyna_pop_vtable(p, &p->lvtbl->args); + dyna_pop_vtable(p, &p->lvtbl->vars); } static void -dyna_pop_gen(struct parser_params *parser, const struct vtable *lvargs) -{ - while (lvtbl->args != lvargs) { - dyna_pop_1(parser); - if (!lvtbl->args) { - struct local_vars *local = lvtbl->prev; - xfree(lvtbl); - lvtbl = local; - } +dyna_pop(struct parser_params *p, const struct vtable *lvargs) +{ + while (p->lvtbl->args != lvargs) { + dyna_pop_1(p); + if (!p->lvtbl->args) { + struct local_vars *local = p->lvtbl->prev; + ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl)); + p->lvtbl = local; + } } - dyna_pop_1(parser); + dyna_pop_1(p); } static int -dyna_in_block_gen(struct parser_params *parser) +dyna_in_block(struct parser_params *p) { - return POINTER_P(lvtbl->vars) && lvtbl->vars->prev != DVARS_TOPSCOPE; + return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE; } -static int -dvar_defined_gen(struct parser_params *parser, ID id, int get) +#ifndef RIPPER +int +dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp) { struct vtable *vars, *args, *used; int i; - args = lvtbl->args; - vars = lvtbl->vars; - used = lvtbl->used; + args = p->lvtbl->args; + vars = p->lvtbl->vars; + used = p->lvtbl->used; - while (POINTER_P(vars)) { - if (vtable_included(args, id)) { - return 1; - } - if ((i = vtable_included(vars, id)) != 0) { - if (used) used->tbl[i-1] |= LVAR_USED; - return 1; - } - args = args->prev; - vars = vars->prev; - if (get) used = 0; - if (used) used = used->prev; + while (!DVARS_TERMINAL_P(vars)) { + if (vtable_included(args, id)) { + return 1; + } + if ((i = vtable_included(vars, id)) != 0) { + if (used && vidrefp) *vidrefp = &used->tbl[i-1]; + return 1; + } + args = args->prev; + vars = vars->prev; + if (!vidrefp) used = 0; + if (used) used = used->prev; } - if (vars == DVARS_INHERIT) { - return rb_dvar_defined(id); + if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) { + return rb_dvar_defined(id, p->parent_iseq); } return 0; } +#endif static int -dvar_curr_gen(struct parser_params *parser, ID id) +dvar_defined(struct parser_params *p, ID id) { - return (vtable_included(lvtbl->args, id) || - vtable_included(lvtbl->vars, id)); + return dvar_defined_ref(p, id, NULL); +} + +static int +dvar_curr(struct parser_params *p, ID id) +{ + return (vtable_included(p->lvtbl->args, id) || + vtable_included(p->lvtbl->vars, id)); } -#ifndef RIPPER static void -reg_fragment_setenc_gen(struct parser_params* parser, VALUE str, int options) +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_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, 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) && - rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) { + int opt, idx; + 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) && - rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) { + 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 (current_enc == rb_usascii_encoding()) { - if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) { - /* raise in re.c */ - rb_enc_associate(str, rb_usascii_encoding()); - } - else { - rb_enc_associate(str, rb_ascii8bit_encoding()); - } + else if (rb_is_usascii_enc(p->enc)) { + rb_parser_enc_associate(p, str, rb_ascii8bit_encoding()); } - return; + return 0; error: - compile_error(PARSER_ARG - "regexp encoding option '%c' differs from source encoding '%s'", - c, rb_enc_name(rb_enc_get(str))); + return c; } +#endif -static int -reg_fragment_check_gen(struct parser_params* parser, VALUE str, int options) +static void +reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options) { - VALUE err; - reg_fragment_setenc(str, options); - err = rb_reg_check_preprocess(str); - if (err != Qnil) { - err = rb_obj_as_string(err); - compile_error(PARSER_ARG "%"PRIsVALUE, err); - return 0; - } - return 1; + int c = rb_reg_fragment_setenc(p, str, options); + if (c) reg_fragment_enc_error(p, str, c); } +#ifndef UNIVERSAL_PARSER typedef struct { struct parser_params* parser; rb_encoding *enc; NODE *succ_block; - NODE *fail_block; - int num; + const YYLTYPE *loc; + rb_parser_assignable_func assignable; } reg_named_capture_assign_t; static int @@ -10400,185 +15343,142 @@ reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end, int back_num, int *back_refs, OnigRegex regex, void *arg0) { reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0; - struct parser_params* parser = arg->parser; + struct parser_params* p = arg->parser; rb_encoding *enc = arg->enc; long len = name_end - name; const char *s = (const char *)name; - ID var; - - arg->num++; - - if (arg->succ_block == 0) { - arg->succ_block = NEW_BEGIN(0); - arg->fail_block = NEW_BEGIN(0); - } - if (!len || (*name != '_' && ISASCII(*name) && !rb_enc_islower(*name, enc)) || - (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) || - !rb_enc_symname2_p(s, len, enc)) { - return ST_CONTINUE; - } - var = intern_cstr(s, len, enc); - if (dvar_defined(var) || local_id(var)) { - rb_warning1("named capture conflicts a local variable - %"PRIsWARN, - rb_id2str(var)); - } - arg->succ_block = block_append(arg->succ_block, - newline_node(node_assign(assignable(var,0), - NEW_CALL( - gettable(rb_intern("$~")), - idAREF, - NEW_LIST(NEW_LIT(ID2SYM(var)))) - ))); - arg->fail_block = block_append(arg->fail_block, - newline_node(node_assign(assignable(var,0), NEW_LIT(Qnil)))); - return ST_CONTINUE; + 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_gen(struct parser_params* parser, VALUE regexp, NODE *match) +reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable) { reg_named_capture_assign_t arg; - arg.parser = parser; + arg.parser = p; arg.enc = rb_enc_get(regexp); arg.succ_block = 0; - arg.fail_block = 0; - arg.num = 0; - onig_foreach_name(RREGEXP(regexp)->ptr, reg_named_capture_assign_iter, &arg); - - if (arg.num == 0) - return match; - - return - block_append( - newline_node(match), - NEW_IF(gettable(rb_intern("$~")), - block_append( - newline_node(arg.succ_block), - newline_node( - NEW_CALL( - gettable(rb_intern("$~")), - rb_intern("begin"), - NEW_LIST(NEW_LIT(INT2FIX(0)))))), - block_append( - newline_node(arg.fail_block), - newline_node( - NEW_LIT(Qnil))))); + arg.loc = loc; + arg.assignable = assignable; + onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg); + + if (!arg.succ_block) return 0; + return RNODE_BLOCK(arg.succ_block)->nd_next; } +#endif -static VALUE -parser_reg_compile(struct parser_params* parser, VALUE str, int options) +#ifndef RIPPER +NODE * +rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc) { - reg_fragment_setenc(str, options); - return rb_reg_compile(str, options & RE_OPTION_MASK, ruby_sourcefile, ruby_sourceline); + return assignable(p, id, val, loc); } -static VALUE -reg_compile_gen(struct parser_params* parser, VALUE str, int options) +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_parser_assignable_func assignable) { - VALUE re; - VALUE err; + ID var; + NODE *node, *succ; - err = rb_errinfo(); - re = parser_reg_compile(parser, str, options); - if (NIL_P(re)) { - VALUE m = rb_attr_get(rb_errinfo(), idMesg); - rb_set_errinfo(err); - if (!NIL_P(err)) { - rb_str_append(rb_str_cat(rb_attr_get(err, idMesg), "\n", 1), m); - } - else { - compile_error(PARSER_ARG "%"PRIsVALUE, m); - } - return Qnil; - } - return re; + if (!len) return ST_CONTINUE; + if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL)) + return ST_CONTINUE; + + var = intern_cstr(s, len, enc); + 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_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc); + succ = *succ_block; + if (!succ) succ = NEW_ERROR(loc); + succ = block_append(p, succ, node); + *succ_block = succ; + return ST_CONTINUE; } +#endif -VALUE -rb_parser_reg_compile(struct parser_params* parser, VALUE str, int options, VALUE *errmsg) +static VALUE +parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options) { - VALUE err = rb_errinfo(); - VALUE re = parser_reg_compile(parser, str, options); - if (NIL_P(re)) { - *errmsg = rb_attr_get(rb_errinfo(), idMesg); - rb_set_errinfo(err); - } - return re; + VALUE str2; + reg_fragment_setenc(p, str, options); + str2 = rb_str_new_parser_string(str); + return rb_parser_reg_compile(p, str2, options); } -NODE* -rb_parser_append_print(VALUE vparser, NODE *node) +#ifndef RIPPER +VALUE +rb_parser_reg_compile(struct parser_params* p, VALUE str, int options) { - NODE *prelude = 0; - NODE *scope = node; - struct parser_params *parser; - - if (!node) return node; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); - - node = node->nd_body; + return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline); +} +#endif - if (nd_type(node) == NODE_PRELUDE) { - prelude = node; - node = node->nd_body; - } +static VALUE +reg_compile(struct parser_params* p, rb_parser_string_t *str, int options) +{ + VALUE re; + VALUE err; - node = block_append(node, - NEW_FCALL(rb_intern("print"), - NEW_ARRAY(NEW_GVAR(rb_intern("$_"))))); - if (prelude) { - prelude->nd_body = node; - scope->nd_body = prelude; - } - else { - scope->nd_body = node; + err = rb_errinfo(); + re = parser_reg_compile(p, str, options); + if (NIL_P(re)) { + VALUE m = rb_attr_get(rb_errinfo(), idMesg); + rb_set_errinfo(err); + compile_error(p, "%"PRIsVALUE, m); + return Qnil; } - - return scope; + return re; } -NODE * -rb_parser_while_loop(VALUE vparser, NODE *node, int chop, int split) +#ifndef RIPPER +void +rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split) { - NODE *prelude = 0; - NODE *scope = node; - struct parser_params *parser; - - if (!node) return node; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); - - node = node->nd_body; - - if (nd_type(node) == NODE_PRELUDE) { - prelude = node; - node = node->nd_body; - } - if (split) { - node = block_append(NEW_GASGN(rb_intern("$F"), - NEW_CALL(NEW_GVAR(rb_intern("$_")), - rb_intern("split"), 0)), - node); - } - if (chop) { - node = block_append(NEW_CALL(NEW_GVAR(rb_intern("$_")), - rb_intern("chop!"), 0), node); - } + p->do_print = print; + p->do_loop = loop; + p->do_chomp = chomp; + p->do_split = split; +} - node = NEW_OPT_N(node); +static NODE * +parser_append_options(struct parser_params *p, NODE *node) +{ + static const YYLTYPE default_location = {{1, 0}, {1, 0}}; + const YYLTYPE *const LOC = &default_location; + + if (p->do_print) { + NODE *print = (NODE *)NEW_FCALL(rb_intern("print"), + NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC), + LOC); + node = block_append(p, node, print); + } + + if (p->do_loop) { + NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC); + + if (p->do_split) { + ID ifs = rb_intern("$;"); + ID fields = rb_intern("$F"); + NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC); + NODE *split = NEW_GASGN(fields, + NEW_CALL(NEW_GVAR(idLASTLINE, LOC), + rb_intern("split"), args, LOC), + LOC); + node = block_append(p, split, node); + } + if (p->do_chomp) { + 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)); + } - if (prelude) { - prelude->nd_body = node; - scope->nd_body = prelude; - } - else { - scope->nd_body = node; + node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC); } - return scope; + return node; } void @@ -10587,123 +15487,137 @@ rb_init_parse(void) /* just to suppress unused-function warnings */ (void)nodetype; (void)nodeline; -#if PARSER_DEBUG - (void)lex_state_name(-1); -#endif } -#endif /* !RIPPER */ -static ID -internal_id_gen(struct parser_params *parser) +ID +internal_id(struct parser_params *p) { - ID id = (ID)vtable_size(lvtbl->args) + (ID)vtable_size(lvtbl->vars); - id += ((tLAST_TOKEN - ID_INTERNAL) >> ID_SCOPE_SHIFT) + 1; - return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT); + return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars)); } +#endif /* !RIPPER */ static void -parser_initialize(struct parser_params *parser) +parser_initialize(struct parser_params *p) { /* note: we rely on TypedData_Make_Struct to set most fields to 0 */ - command_start = TRUE; - ruby_sourcefile_string = Qnil; -#ifdef RIPPER - parser->delayed = Qnil; - parser->result = Qnil; - parser->parsing_thread = Qnil; - parser->toplevel_p = TRUE; + 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 = NULL; + p->frozen_string_literal = -1; /* not specified */ +#ifndef RIPPER + p->error_buffer = Qfalse; + p->end_expect_token_locations = NULL; + p->token_id = 0; + 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 - parser->enc = rb_utf8_encoding(); + p->debug_buffer = Qnil; + p->debug_output = rb_ractor_stdout(); + p->enc = rb_utf8_encoding(); + p->exits = 0; } #ifdef RIPPER -#define parser_mark ripper_parser_mark -#define parser_free ripper_parser_free +#define rb_ruby_parser_mark ripper_parser_mark +#define rb_ruby_parser_free ripper_parser_free +#define rb_ruby_parser_memsize ripper_parser_memsize #endif -static void -parser_mark(void *ptr) +void +rb_ruby_parser_mark(void *ptr) { - struct parser_params *parser = (struct parser_params*)ptr; + struct parser_params *p = (struct parser_params*)ptr; - rb_gc_mark((VALUE)lex_strterm); - rb_gc_mark((VALUE)deferred_nodes); - rb_gc_mark(lex_input); - rb_gc_mark(lex_lastline); - rb_gc_mark(lex_nextline); - rb_gc_mark(ruby_sourcefile_string); + rb_gc_mark(p->ruby_sourcefile_string); #ifndef RIPPER - rb_gc_mark((VALUE)ruby_eval_tree_begin); - rb_gc_mark((VALUE)ruby_eval_tree); - rb_gc_mark(ruby_debug_lines); - rb_gc_mark(parser->compile_option); + rb_gc_mark(p->error_buffer); #else - rb_gc_mark(parser->delayed); - rb_gc_mark(parser->value); - rb_gc_mark(parser->result); - rb_gc_mark(parser->parsing_thread); -#endif -#ifdef YYMALLOC - rb_gc_mark((VALUE)parser->heap); + 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); } -static void -parser_free(void *ptr) +void +rb_ruby_parser_free(void *ptr) { - struct parser_params *parser = (struct parser_params*)ptr; + struct parser_params *p = (struct parser_params*)ptr; struct local_vars *local, *prev; - if (tokenbuf) { - xfree(tokenbuf); + if (p->ast) { + rb_ast_free(p->ast); } - for (local = lvtbl; local; local = prev) { - if (local->vars) xfree(local->vars); - prev = local->prev; - xfree(local); + + if (p->warn_duplicate_keys_table) { + st_free_table(p->warn_duplicate_keys_table); } + #ifndef RIPPER - { - token_info *ptinfo; - while ((ptinfo = parser->token_info) != 0) { - parser->token_info = ptinfo->next; - xfree(ptinfo); - } + if (p->tokens) { + rb_parser_ary_free(p, p->tokens); } #endif + + if (p->tokenbuf) { + ruby_sized_xfree(p->tokenbuf, p->toksiz); + } + + for (local = p->lvtbl; local; local = prev) { + prev = local->prev; + local_free(p, local); + } + + { + token_info *ptinfo; + while ((ptinfo = p->token_info) != 0) { + p->token_info = ptinfo->next; + xfree(ptinfo); + } + } + string_buffer_free(p); + + if (p->pvtbl) { + st_free_table(p->pvtbl); + } + + if (CASE_LABELS_ENABLED_P(p->case_labels)) { + st_free_table(p->case_labels); + } + + xfree(p->lex.strterm); + p->lex.strterm = 0; + xfree(ptr); } -static size_t -parser_memsize(const void *ptr) +size_t +rb_ruby_parser_memsize(const void *ptr) { - struct parser_params *parser = (struct parser_params*)ptr; + struct parser_params *p = (struct parser_params*)ptr; struct local_vars *local; - size_t size = sizeof(*parser); + size_t size = sizeof(*p); - if (!ptr) return 0; - size += toksiz; - for (local = lvtbl; local; local = local->prev) { - size += sizeof(*local); - if (local->vars) size += local->vars->capa * sizeof(ID); + size += p->toksiz; + for (local = p->lvtbl; local; local = local->prev) { + size += sizeof(*local); + if (local->vars) size += local->vars->capa * sizeof(ID); } return size; } -static const rb_data_type_t parser_data_type = { -#ifndef RIPPER - "parser", -#else - "ripper", -#endif - { - parser_mark, - parser_free, - parser_memsize, - }, - 0, 0, RUBY_TYPED_FREE_IMMEDIATELY -}; - #ifndef RIPPER #undef rb_reserved_word @@ -10713,648 +15627,450 @@ rb_reserved_word(const char *str, unsigned int len) return reserved_word(str, len); } -VALUE -rb_parser_new(void) +#ifdef UNIVERSAL_PARSER +rb_parser_t * +rb_ruby_parser_allocate(const rb_parser_config_t *config) { - struct parser_params *p; - VALUE parser = TypedData_Make_Struct(0, struct parser_params, - &parser_data_type, p); - parser_initialize(p); - return parser; + /* 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; + return p; } -#endif - -#ifdef RIPPER -#define rb_parser_end_seen_p ripper_parser_end_seen_p -#define rb_parser_encoding ripper_parser_encoding -#define rb_parser_get_yydebug ripper_parser_get_yydebug -#define rb_parser_set_yydebug ripper_parser_set_yydebug -static VALUE ripper_parser_end_seen_p(VALUE vparser); -static VALUE ripper_parser_encoding(VALUE vparser); -static VALUE ripper_parser_get_yydebug(VALUE self); -static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag); -/* - * call-seq: - * ripper#error? -> Boolean - * - * Return true if parsed source has errors. - */ -static VALUE -ripper_error_p(VALUE vparser) +rb_parser_t * +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) { - struct parser_params *parser; + /* 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; +} - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); - return parser->error_p ? Qtrue : Qfalse; +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 -/* - * call-seq: - * ripper#end_seen? -> Boolean - * - * Return true if parsed source ended by +\_\_END\_\_+. - */ -VALUE -rb_parser_end_seen_p(VALUE vparser) +rb_parser_t * +rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main) { - struct parser_params *parser; - - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); - return ruby__end__seen ? Qtrue : Qfalse; + p->error_buffer = main ? Qfalse : Qnil; + p->parent_iseq = base; + return p; } -/* - * call-seq: - * ripper#encoding -> encoding - * - * Return encoding of the source. - */ -VALUE -rb_parser_encoding(VALUE vparser) +void +rb_ruby_parser_set_script_lines(rb_parser_t *p) { - struct parser_params *parser; + p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10); +} - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); - return rb_enc_from_encoding(current_enc); +void +rb_ruby_parser_error_tolerant(rb_parser_t *p) +{ + p->error_tolerant = 1; } -/* - * call-seq: - * ripper.yydebug -> true or false - * - * Get yydebug. - */ -VALUE -rb_parser_get_yydebug(VALUE self) +void +rb_ruby_parser_keep_tokens(rb_parser_t *p) { - struct parser_params *parser; + p->keep_tokens = 1; + p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10); +} - TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); - return yydebug ? Qtrue : Qfalse; +rb_encoding * +rb_ruby_parser_encoding(rb_parser_t *p) +{ + return p->enc; } -/* - * call-seq: - * ripper.yydebug = flag - * - * Set yydebug. - */ -VALUE -rb_parser_set_yydebug(VALUE self, VALUE flag) +int +rb_ruby_parser_end_seen_p(rb_parser_t *p) { - struct parser_params *parser; + return p->ruby__end__seen; +} - TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); - yydebug = RTEST(flag); +int +rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag) +{ + p->debug = flag; return flag; } +#endif /* !RIPPER */ -#ifndef RIPPER -#ifdef YYMALLOC -#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE)) -#define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0) -#define ADD2HEAP(n, c, p) ((parser->heap = (n))->u1.node = (p), \ - (n)->u3.cnt = (c), (p)) - -void * -rb_parser_malloc(struct parser_params *parser, size_t size) +#ifdef RIPPER +int +rb_ruby_parser_get_yydebug(rb_parser_t *p) { - size_t cnt = HEAPCNT(1, size); - NODE *n = NEWHEAP(); - void *ptr = xmalloc(size); - - return ADD2HEAP(n, cnt, ptr); + return p->debug; } -void * -rb_parser_calloc(struct parser_params *parser, size_t nelem, size_t size) +void +rb_ruby_parser_set_value(rb_parser_t *p, VALUE value) { - size_t cnt = HEAPCNT(nelem, size); - NODE *n = NEWHEAP(); - void *ptr = xcalloc(nelem, size); - - return ADD2HEAP(n, cnt, ptr); + p->value = value; } -void * -rb_parser_realloc(struct parser_params *parser, void *ptr, size_t size) +int +rb_ruby_parser_error_p(rb_parser_t *p) { - NODE *n; - size_t cnt = HEAPCNT(1, size); + return p->error_p; +} - if (ptr && (n = parser->heap) != NULL) { - do { - if (n->u1.node == ptr) { - n->u1.node = ptr = xrealloc(ptr, size); - if (n->u3.cnt) n->u3.cnt = cnt; - return ptr; - } - } while ((n = n->u2.node) != NULL); - } - n = NEWHEAP(); - ptr = xrealloc(ptr, size); - return ADD2HEAP(n, cnt, ptr); +VALUE +rb_ruby_parser_debug_output(rb_parser_t *p) +{ + return p->debug_output; } void -rb_parser_free(struct parser_params *parser, void *ptr) +rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output) { - NODE **prev = &parser->heap, *n; - - while ((n = *prev) != NULL) { - if (n->u1.node == ptr) { - *prev = n->u2.node; - rb_gc_force_recycle((VALUE)n); - break; - } - prev = &n->u2.node; - } - xfree(ptr); + p->debug_output = output; } -#endif -#endif - -#ifdef RIPPER -#ifdef RIPPER_DEBUG -extern int rb_is_pointer_to_heap(VALUE); -/* :nodoc: */ -static VALUE -ripper_validate_object(VALUE self, VALUE x) -{ - if (x == Qfalse) return x; - if (x == Qtrue) return x; - if (x == Qnil) return x; - if (x == Qundef) - rb_raise(rb_eArgError, "Qundef given"); - if (FIXNUM_P(x)) return x; - if (SYMBOL_P(x)) return x; - if (!rb_is_pointer_to_heap(x)) - rb_raise(rb_eArgError, "invalid pointer: %p", 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: - return x; - case T_NODE: - if (nd_type(x) != NODE_RIPPER) { - rb_raise(rb_eArgError, "NODE given: %p", x); - } - return ((NODE *)x)->nd_rval; - default: - rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)", - x, rb_obj_classname(x)); - } - return x; +VALUE +rb_ruby_parser_parsing_thread(rb_parser_t *p) +{ + return p->parsing_thread; } -#endif -#define validate(x) ((x) = get_value(x)) - -static VALUE -ripper_dispatch0(struct parser_params *parser, ID mid) +void +rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread) { - return rb_funcall(parser->value, mid, 0); + p->parsing_thread = parsing_thread; } -static VALUE -ripper_dispatch1(struct parser_params *parser, ID mid, VALUE a) +void +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) { - validate(a); - return rb_funcall(parser->value, mid, 1, a); + p->lex.gets = gets; + p->lex.input = input; + p->eofp = 0; + p->ruby_sourcefile_string = sourcefile_string; + p->ruby_sourcefile = sourcefile; + p->ruby_sourceline = sourceline; } -static VALUE -ripper_dispatch2(struct parser_params *parser, ID mid, VALUE a, VALUE b) +VALUE +rb_ruby_parser_result(rb_parser_t *p) { - validate(a); - validate(b); - return rb_funcall(parser->value, mid, 2, a, b); + return p->result; } -static VALUE -ripper_dispatch3(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c) +rb_encoding * +rb_ruby_parser_enc(rb_parser_t *p) { - validate(a); - validate(b); - validate(c); - return rb_funcall(parser->value, mid, 3, a, b, c); + return p->enc; } -static VALUE -ripper_dispatch4(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d) +VALUE +rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p) { - validate(a); - validate(b); - validate(c); - validate(d); - return rb_funcall(parser->value, mid, 4, a, b, c, d); + return p->ruby_sourcefile_string; } -static VALUE -ripper_dispatch5(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e) +int +rb_ruby_parser_ruby_sourceline(rb_parser_t *p) { - validate(a); - validate(b); - validate(c); - validate(d); - validate(e); - return rb_funcall(parser->value, mid, 5, a, b, c, d, e); + return p->ruby_sourceline; } -static VALUE -ripper_dispatch7(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g) +int +rb_ruby_parser_lex_state(rb_parser_t *p) { - validate(a); - validate(b); - validate(c); - validate(d); - validate(e); - validate(f); - validate(g); - return rb_funcall(parser->value, mid, 7, a, b, c, d, e, f, g); + return p->lex.state; } -static const struct kw_assoc { - ID id; - const char *name; -} keyword_to_name[] = { - {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"}, - {keyword_do_block, "do"}, - {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_unless, "unless"}, - {modifier_while, "while"}, - {modifier_until, "until"}, - {modifier_rescue, "rescue"}, - {keyword_alias, "alias"}, - {keyword_defined, "defined?"}, - {keyword_BEGIN, "BEGIN"}, - {keyword_END, "END"}, - {keyword__LINE__, "__LINE__"}, - {keyword__FILE__, "__FILE__"}, - {keyword__ENCODING__, "__ENCODING__"}, - {0, NULL} -}; - -static const char* -keyword_id_to_str(ID id) +void +rb_ruby_ripper_parse0(rb_parser_t *p) { - const struct kw_assoc *a; - - for (a = keyword_to_name; a->id; a++) { - if (a->id == id) - return a->name; - } - return NULL; + parser_prepare(p); + p->ast = rb_ast_new(); + ripper_yyparse((void*)p); + rb_ast_free(p->ast); + p->ast = 0; + p->eval_tree = 0; + p->eval_tree_begin = 0; } -#undef ripper_id2sym -static VALUE -ripper_id2sym(ID id) +int +rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width) { - const char *name; - char buf[8]; - - if (ISASCII(id)) { - buf[0] = (char)id; - buf[1] = '\0'; - return ID2SYM(rb_intern2(buf, 1)); - } - if ((name = keyword_id_to_str(id))) { - return ID2SYM(rb_intern(name)); - } - if (!rb_id2str(id)) { - rb_bug("cannot convert ID to string: %ld", (unsigned long)id); - } - return ID2SYM(id); + return dedent_string(p, string, width); } -static ID -ripper_get_id(VALUE v) +int +rb_ruby_ripper_initialized_p(rb_parser_t *p) { - NODE *nd; - if (!RB_TYPE_P(v, T_NODE)) return 0; - nd = (NODE *)v; - if (nd_type(nd) != NODE_RIPPER) return 0; - return nd->nd_vid; + return p->lex.input != 0; } -static VALUE -ripper_get_value(VALUE v) +void +rb_ruby_ripper_parser_initialize(rb_parser_t *p) { - NODE *nd; - if (v == Qundef) return Qnil; - if (!RB_TYPE_P(v, T_NODE)) return v; - nd = (NODE *)v; - if (nd_type(nd) != NODE_RIPPER) return Qnil; - return nd->nd_rval; + parser_initialize(p); } -static void -ripper_error_gen(struct parser_params *parser) +long +rb_ruby_ripper_column(rb_parser_t *p) { - parser->error_p = TRUE; + return p->lex.ptok - p->lex.pbeg; } -static void -ripper_compile_error(struct parser_params *parser, const char *fmt, ...) +long +rb_ruby_ripper_token_len(rb_parser_t *p) { - VALUE str; - va_list args; + return p->lex.pcur - p->lex.ptok; +} - va_start(args, fmt); - str = rb_vsprintf(fmt, args); - va_end(args); - rb_funcall(parser->value, rb_intern("compile_error"), 1, str); - ripper_error_gen(parser); +rb_parser_string_t * +rb_ruby_ripper_lex_lastline(rb_parser_t *p) +{ + return p->lex.lastline; } -static VALUE -ripper_lex_get_generic(struct parser_params *parser, VALUE src) +VALUE +rb_ruby_ripper_lex_state_name(struct parser_params *p, int state) { - return rb_io_gets(src); + return rb_parser_lex_state_name(p, (enum lex_state_e)state); } -static VALUE -ripper_s_allocate(VALUE klass) +#ifdef UNIVERSAL_PARSER +rb_parser_t * +rb_ripper_parser_params_allocate(const rb_parser_config_t *config) { - struct parser_params *p; - VALUE self = TypedData_Make_Struct(klass, struct parser_params, - &parser_data_type, p); - p->value = self; - return self; + rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t)); + p->config = config; + return p; } +#endif -#define ripper_initialized_p(r) ((r)->lex.input != 0) +struct parser_params* +rb_ruby_ripper_parser_allocate(void) +{ + return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params)); +} +#endif /* RIPPER */ -/* - * call-seq: - * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper - * - * Create a new Ripper object. - * _src_ must be a String, an IO, or an Object which has #gets method. - * - * This method does not starts parsing. - * See also Ripper#parse and Ripper.parse. - */ -static VALUE -ripper_initialize(int argc, VALUE *argv, VALUE self) +#ifndef RIPPER +void +rb_parser_printf(struct parser_params *p, const char *fmt, ...) { - struct parser_params *parser; - VALUE src, fname, lineno; + va_list ap; + VALUE mesg = p->debug_buffer; - TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); - rb_scan_args(argc, argv, "12", &src, &fname, &lineno); - if (RB_TYPE_P(src, T_FILE)) { - lex_gets = ripper_lex_get_generic; + if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0); + va_start(ap, fmt); + rb_str_vcatf(mesg, fmt, ap); + va_end(ap); + if (char_at_end(p, mesg, 0) == '\n') { + rb_io_write(p->debug_output, mesg); + p->debug_buffer = Qnil; } - else { - StringValue(src); - lex_gets = lex_get_str; - } - lex_input = src; - parser->eofp = 0; - if (NIL_P(fname)) { - fname = STR_NEW2("(ripper)"); - OBJ_FREEZE(fname); +} + +static void +parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...) +{ + va_list ap; + int lineno, column; + + if (loc) { + lineno = loc->end_pos.lineno; + column = loc->end_pos.column; } else { - StringValue(fname); - fname = rb_str_new_frozen(fname); + lineno = p->ruby_sourceline; + column = rb_long2int(p->lex.pcur - p->lex.pbeg); } - parser_initialize(parser); - ruby_sourcefile_string = fname; - ruby_sourcefile = RSTRING_PTR(fname); - ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1; + rb_io_flush(p->debug_output); + p->error_p = 1; + va_start(ap, fmt); + p->error_buffer = + rb_syntax_error_append(p->error_buffer, + p->ruby_sourcefile_string, + lineno, column, + p->enc, fmt, ap); + va_end(ap); +} - return Qnil; +static size_t +count_char(const char *str, int c) +{ + int n = 0; + while (str[n] == c) ++n; + return n; } -struct ripper_args { - struct parser_params *parser; - int argc; - VALUE *argv; -}; +/* + * strip enclosing double-quotes, same as the default yytnamerr except + * for that single-quotes matching back-quotes do not stop stripping. + * + * "\"`class' keyword\"" => "`class' keyword" + */ +size_t +rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr) +{ + if (*yystr == '"') { + size_t yyn = 0, bquote = 0; + const char *yyp = yystr; + + while (*++yyp) { + switch (*yyp) { + case '\'': + if (!bquote) { + bquote = count_char(yyp+1, '\'') + 1; + if (yyres) memcpy(&yyres[yyn], yyp, bquote); + yyn += bquote; + yyp += bquote - 1; + 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; + } + + case ',': + goto do_not_strip_quotes; + + case '\\': + if (*++yyp != '\\') + goto do_not_strip_quotes; + /* Fall through. */ + default: + if (yyres) + yyres[yyn] = *yyp; + yyn++; + break; + + case '"': + case '\0': + if (yyres) + yyres[yyn] = '\0'; + return yyn; + } + } + do_not_strip_quotes: ; + } -static VALUE -ripper_parse0(VALUE parser_v) -{ - struct parser_params *parser; + if (!yyres) return strlen(yystr); - TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser); - parser_prepare(parser); - ripper_yyparse((void*)parser); - return parser->result; + return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres); } +#endif + +#ifdef RIPPER +#define validate(x) (void)(x) static VALUE -ripper_ensure(VALUE parser_v) +ripper_dispatch0(struct parser_params *p, ID mid) { - struct parser_params *parser; - - TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser); - parser->parsing_thread = Qnil; - return Qnil; + return rb_funcall(p->value, mid, 0); } -/* - * call-seq: - * ripper#parse - * - * Start parsing and returns the value of the root action. - */ static VALUE -ripper_parse(VALUE self) +ripper_dispatch1(struct parser_params *p, ID mid, VALUE a) { - struct parser_params *parser; - - TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); - if (!ripper_initialized_p(parser)) { - rb_raise(rb_eArgError, "method called for uninitialized object"); - } - if (!NIL_P(parser->parsing_thread)) { - if (parser->parsing_thread == rb_thread_current()) - rb_raise(rb_eArgError, "Ripper#parse is not reentrant"); - else - rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe"); - } - parser->parsing_thread = rb_thread_current(); - rb_ensure(ripper_parse0, self, ripper_ensure, self); - - return parser->result; + validate(a); + return rb_funcall(p->value, mid, 1, a); } -/* - * call-seq: - * ripper#column -> Integer - * - * Return column number of current parsing line. - * This number starts from 0. - */ static VALUE -ripper_column(VALUE self) +ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b) { - struct parser_params *parser; - long col; - - TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); - if (!ripper_initialized_p(parser)) { - rb_raise(rb_eArgError, "method called for uninitialized object"); - } - if (NIL_P(parser->parsing_thread)) return Qnil; - col = parser->tokp - lex_pbeg; - return LONG2NUM(col); + validate(a); + validate(b); + return rb_funcall(p->value, mid, 2, a, b); } -/* - * call-seq: - * ripper#filename -> String - * - * Return current parsing filename. - */ static VALUE -ripper_filename(VALUE self) +ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c) { - struct parser_params *parser; - - TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); - if (!ripper_initialized_p(parser)) { - rb_raise(rb_eArgError, "method called for uninitialized object"); - } - return ruby_sourcefile_string; + validate(a); + validate(b); + validate(c); + return rb_funcall(p->value, mid, 3, a, b, c); } -/* - * call-seq: - * ripper#lineno -> Integer - * - * Return line number of current parsing line. - * This number starts from 1. - */ static VALUE -ripper_lineno(VALUE self) +ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d) { - struct parser_params *parser; - - TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); - if (!ripper_initialized_p(parser)) { - rb_raise(rb_eArgError, "method called for uninitialized object"); - } - if (NIL_P(parser->parsing_thread)) return Qnil; - return INT2NUM(ruby_sourceline); + validate(a); + validate(b); + validate(c); + validate(d); + return rb_funcall(p->value, mid, 4, a, b, c, d); } -#ifdef RIPPER_DEBUG -/* :nodoc: */ static VALUE -ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg) +ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e) { - StringValue(msg); - if (obj == Qundef) { - rb_raise(rb_eArgError, "%"PRIsVALUE, msg); - } - return Qnil; + validate(a); + validate(b); + validate(c); + validate(d); + validate(e); + return rb_funcall(p->value, mid, 5, a, b, c, d, e); } -/* :nodoc: */ static VALUE -ripper_value(VALUE self, VALUE obj) +ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g) { - return ULONG2NUM(obj); + validate(a); + validate(b); + validate(c); + validate(d); + validate(e); + validate(f); + validate(g); + return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g); } -#endif - void -Init_ripper(void) +ripper_error(struct parser_params *p) { - ripper_init_eventids1(); - ripper_init_eventids2(); - id_warn = rb_intern_const("warn"); - id_warning = rb_intern_const("warning"); - - InitVM(ripper); + p->error_p = TRUE; } -void -InitVM_ripper(void) -{ - VALUE Ripper; - - Ripper = rb_define_class("Ripper", rb_cObject); - /* version of Ripper */ - rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION)); - rb_define_alloc_func(Ripper, ripper_s_allocate); - rb_define_method(Ripper, "initialize", ripper_initialize, -1); - rb_define_method(Ripper, "parse", ripper_parse, 0); - rb_define_method(Ripper, "column", ripper_column, 0); - rb_define_method(Ripper, "filename", ripper_filename, 0); - rb_define_method(Ripper, "lineno", ripper_lineno, 0); - rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0); - rb_define_method(Ripper, "encoding", rb_parser_encoding, 0); - rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0); - rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1); - rb_define_method(Ripper, "error?", ripper_error_p, 0); -#ifdef RIPPER_DEBUG - rb_define_method(rb_mKernel, "assert_Qundef", ripper_assert_Qundef, 2); - rb_define_method(rb_mKernel, "rawVALUE", ripper_value, 1); - rb_define_method(rb_mKernel, "validate_object", ripper_validate_object, 1); -#endif - - ripper_init_eventids1_table(Ripper); - ripper_init_eventids2_table(Ripper); - -# if 0 - /* Hack to let RDoc document SCRIPT_LINES__ */ - - /* - * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded - * after the assignment will be added as an Array of lines with the file - * name as the key. - */ - rb_define_global_const("SCRIPT_LINES__", Qnil); -#endif +VALUE +ripper_value(struct parser_params *p) +{ + (void)yystpcpy; /* may not used in newer bison */ + return p->value; } + #endif /* RIPPER */ +/* + * Local variables: + * mode: c + * c-file-style: "ruby" + * End: + */ |
