ruby-changes:57884
From: Nobuyoshi <ko1@a...>
Date: Tue, 24 Sep 2019 21:58:12 +0900 (JST)
Subject: [ruby-changes:57884] ea68bb914a (master): Changed numbered parameter prefix
https://git.ruby-lang.org/ruby.git/commit/?id=ea68bb914a From ea68bb914a3c806a1c5188993b96791a76ab0849 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Wed, 4 Sep 2019 00:07:50 +0900 Subject: Changed numbered parameter prefix diff --git a/bootstraptest/test_syntax.rb b/bootstraptest/test_syntax.rb index d06ebc9..a111990 100644 --- a/bootstraptest/test_syntax.rb +++ b/bootstraptest/test_syntax.rb @@ -532,7 +532,7 @@ end https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_syntax.rb#L532 assert_syntax_error "unterminated string meets end of file", '().."', '[ruby-dev:29732]' assert_equal %q{[]}, %q{$&;[]}, '[ruby-dev:31068]' assert_syntax_error "syntax error, unexpected *, expecting '}'", %q{{*0}}, '[ruby-dev:31072]' -assert_syntax_error "leading zero is not allowed as a numbered parameter", %q{@0..0}, '[ruby-dev:31095]' +assert_syntax_error "`@0' is not allowed as an instance variable name", %q{@0..0}, '[ruby-dev:31095]' assert_syntax_error "identifier $00 is not valid to get", %q{$00..0}, '[ruby-dev:31100]' assert_syntax_error "identifier $00 is not valid to set", %q{0..$00=1} assert_equal %q{0}, %q{[*0];0}, '[ruby-dev:31102]' diff --git a/ext/ripper/eventids2.c b/ext/ripper/eventids2.c index ce4a285..9b18853 100644 --- a/ext/ripper/eventids2.c +++ b/ext/ripper/eventids2.c @@ -51,7 +51,6 @@ typedef struct { https://github.com/ruby/ruby/blob/trunk/ext/ripper/eventids2.c#L51 ID ripper_id_label_end; ID ripper_id_tlambda; ID ripper_id_tlambeg; - ID ripper_id_tnumparam; ID ripper_id_ignored_nl; ID ripper_id_comment; @@ -114,7 +113,6 @@ ripper_init_eventids2(void) https://github.com/ruby/ruby/blob/trunk/ext/ripper/eventids2.c#L113 set_id2(label_end); set_id2(tlambda); set_id2(tlambeg); - set_id2(tnumparam); set_id2(ignored_nl); set_id2(comment); @@ -278,7 +276,6 @@ static const struct token_assoc { https://github.com/ruby/ruby/blob/trunk/ext/ripper/eventids2.c#L276 {tLABEL_END, O(label_end)}, {tLAMBDA, O(tlambda)}, {tLAMBEG, O(tlambeg)}, - {tNUMPARAM, O(tnumparam)}, /* ripper specific tokens */ {tIGNORED_NL, O(ignored_nl)}, diff --git a/parse.y b/parse.y index 3732924..1a9b110 100644 --- a/parse.y +++ b/parse.y @@ -549,11 +549,8 @@ PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), https://github.com/ruby/ruby/blob/trunk/parse.y#L549 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_none(struct parser_params *p, YYLTYPE *yylloc); YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc); -ID rb_parser_numparam_id(struct parser_params *p, int num); RUBY_SYMBOL_EXPORT_END -#define numparam_id rb_parser_numparam_id - static void parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp); static ID formal_argument(struct parser_params*, ID); static ID shadowing_lvar(struct parser_params*,ID); @@ -1010,7 +1007,6 @@ static void token_info_warn(struct parser_params *p, const char *token, token_in https://github.com/ruby/ruby/blob/trunk/parse.y#L1007 %token <id> tCONSTANT "constant" %token <id> tCVAR "class variable" %token <id> tLABEL -%token <id> tNUMPARAM "numbered parameter" %token <node> tINTEGER "integer literal" %token <node> tFLOAT "float literal" %token <node> tRATIONAL "rational literal" @@ -4584,13 +4580,6 @@ string_dvar : tGVAR https://github.com/ruby/ruby/blob/trunk/parse.y#L4580 /*% %*/ /*% ripper: var_ref!($1) %*/ } - | tNUMPARAM - { - /*%%%*/ - $$ = NEW_DVAR($1, &@1); - /*% %*/ - /*% ripper: var_ref!($1) %*/ - } | backref ; @@ -4646,7 +4635,6 @@ user_variable : tIDENTIFIER https://github.com/ruby/ruby/blob/trunk/parse.y#L4635 | tGVAR | tCONSTANT | tCVAR - | tNUMPARAM ; keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);} @@ -8480,9 +8468,12 @@ parse_gvar(struct parser_params *p, const enum lex_state_e last_state) https://github.com/ruby/ruby/blob/trunk/parse.y#L8468 return tGVAR; } +#ifndef RIPPER static bool -parser_numbered_param(struct parser_params *p, unsigned long n) +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)) { compile_error(p, "implicit parameter outside block"); return false; @@ -8503,10 +8494,22 @@ parser_numbered_param(struct parser_params *p, unsigned long n) https://github.com/ruby/ruby/blob/trunk/parse.y#L8494 return false; } } - set_yylval_name(numparam_id(p, (int)n)); - SET_LEX_STATE(EXPR_ARG); + struct vtable *args = p->lvtbl->args; + if (n == 0) { + p->max_numparam = IMPLICIT_PARAM; + vtable_add(args, idNUMPARAM_0); + } + else { + 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; } +#endif static enum yytokentype parse_atmark(struct parser_params *p, const enum lex_state_e last_state) @@ -8540,31 +8543,17 @@ parse_atmark(struct parser_params *p, const enum lex_state_e last_state) https://github.com/ruby/ruby/blob/trunk/parse.y#L8543 return result; } else if (ISDIGIT(c)) { - const char *ptr = p->lex.pcur - 1; - size_t len = p->lex.pend - ptr; - int overflow; - unsigned long n = ruby_scan_digits(ptr, len, 10, &len, &overflow); - p->lex.pcur = ptr + len; + pushback(p, c); RUBY_SET_YYLLOC(loc); if (result == tIVAR) { - if (IS_lex_state_for(last_state, EXPR_FNAME)) { - compile_error(p, "`@%c' is not allowed as an instance variable name", c); - } - else if (ptr[0] == '0') { - compile_error(p, "leading zero is not allowed as a numbered parameter"); - } - else if (overflow || n > NUMPARAM_MAX) { - compile_error(p, "too large numbered parameter"); - } - else if (parser_numbered_param(p, n)) { - return tNUMPARAM; - } + 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; } @@ -9869,6 +9858,11 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) https://github.com/ruby/ruby/blob/trunk/parse.y#L9858 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))) { + node = NEW_DVAR(id, loc); + return node; + } # if WARN_PAST_SCOPE if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) { rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id)); @@ -10073,7 +10067,9 @@ id_is_var(struct parser_params *p, ID id) https://github.com/ruby/ruby/blob/trunk/parse.y#L10067 case ID_INTERNAL: return vtable_included(p->lvtbl->args, id); case ID_LOCAL: - if (dyna_in_block(p) && dvar_defined(p, id)) return 1; + 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; @@ -11254,25 +11250,6 @@ args_with_numbered(struct parser_params *p, NODE *args, int max_numparam) https://github.com/ruby/ruby/blob/trunk/parse.y#L11250 return args; } -ID -rb_parser_numparam_id(struct parser_params *p, int idx) -{ - struct vtable *args; - if (idx < 0) return (ID)0; - else if (idx == 0) { - p->max_numparam = IMPLICIT_PARAM; - idx = 1; - } - else if (p->max_numparam < idx) { - p->max_numparam = idx; - } - args = p->lvtbl->args; - while (idx > args->pos) { - vtable_add(args, internal_id(p)); - } - return args->tbl[idx-1]; -} - static NODE* new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc) { diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb index e1057d3..3ecbee8 100644 --- a/test/irb/test_color.rb +++ b/test/irb/test_color.rb @@ -67,8 +67,6 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L67 "4.5.6" => "#{MAGENTA}#{BOLD}4.5#{CLEAR}#{RED}#{REVERSE}.6#{CLEAR}", "\e[0m\n" => "#{RED}#{REVERSE}^[#{CLEAR}[#{BLUE}#{BOLD}0#{CLEAR}#{RED}#{REVERSE}m#{CLEAR}\n", "<<EOS\nhere\nEOS" => "#{RED}<<EOS#{CLEAR}\n#{RED}here#{CLEAR}\n#{RED}EOS#{CLEAR}", - ":@1" => "#{YELLOW}:#{CLEAR}#{RED}#{REVERSE}@1#{CLEAR}", - "@@1" => "#{RED}#{REVERSE}@@1#{CLEAR}", }) end diff --git a/test/ripper/test_lexer.rb b/test/ripper/test_lexer.rb index 060d5f2..1794773 100644 --- a/test/ripper/test_lexer.rb +++ b/test/ripper/test_lexer.rb @@ -102,15 +102,15 @@ class TestRipper::Lexer < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ripper/test_lexer.rb#L102 def test_state_after_ivar assert_equal [[1,0],:on_ivar,"@a",state(:EXPR_END)], Ripper.lex("@a").last assert_equal [[1,1],:on_ivar,"@a",state(:EXPR_ENDFN)], Ripper.lex(":@a").last - assert_equal [[1,0],:on_ivar,"@1",state(:EXPR_END)], Ripper.lex("@1").last - assert_equal [[1,1],:on_ivar,"@1",state(:EXPR_ENDFN)], Ripper.lex(":@1").last + assert_equal [[1,1],:on_int,"1",state(:EXPR_END)], Ripper.lex("@1").last + assert_equal [[1,2],:on_int,"1",state(:EXPR_END)], Ripper.lex(":@1").last end def test_state_after_cvar assert_equal [[1,0],:on_cvar,"@@a",state(:EXPR_END)], Ripper.lex("@@a").last assert_equal [[1,1],:on_cvar,"@@a",state(:EXPR_ENDFN)], Ripper.lex(":@@a").last - assert_equal [[1,0],:on_cvar,"@@1",state(:EXPR_END)], Ripper.lex("@@1").last - assert_equal [[1,1],:on_cvar,"@@1",state(:EXPR_ENDFN)], Ripper.lex(":@@1").last + assert_equal [[1,2],:on_int,"1",state(:EXPR_END)], Ripper.lex( (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/