ruby-changes:45533
From: nobu <ko1@a...>
Date: Sun, 12 Feb 2017 12:15:38 +0900 (JST)
Subject: [ruby-changes:45533] nobu:r57606 (trunk): ripper: fix %-op on_operator_ambiguous
nobu 2017-02-12 12:15:34 +0900 (Sun, 12 Feb 2017) New Revision: 57606 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57606 Log: ripper: fix %-op on_operator_ambiguous * parse.y (ambiguous_operator): separate token and string representation of operators, to fix %-operator argument. in a warning message, needs to be escaped by '%' but the symbol should not be. Modified files: trunk/parse.y trunk/test/ripper/test_parser_events.rb Index: test/ripper/test_parser_events.rb =================================================================== --- test/ripper/test_parser_events.rb (revision 57605) +++ test/ripper/test_parser_events.rb (revision 57606) @@ -173,8 +173,14 @@ class TestRipper::ParserEvents < Test::U https://github.com/ruby/ruby/blob/trunk/test/ripper/test_parser_events.rb#L173 def test_operator_ambiguous thru_operator_ambiguous = false - parse('a=1; a %[]', :on_operator_ambiguous) {thru_operator_ambiguous = true} + token = syntax = nil + parse('a=1; a %[]', :on_operator_ambiguous) {|*a| + thru_operator_ambiguous = true + _, token, syntax = *a + } assert_equal true, thru_operator_ambiguous + assert_equal :%, token + assert_equal "string literal", syntax end def test_array # array literal Index: parse.y =================================================================== --- parse.y (revision 57605) +++ parse.y (revision 57606) @@ -7221,16 +7221,18 @@ parser_prepare(struct parser_params *par https://github.com/ruby/ruby/blob/trunk/parse.y#L7221 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT) #ifndef RIPPER -#define ambiguous_operator(op, syn) ( \ +#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) \ +#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)), \ + (tok)) static VALUE parse_rational(struct parser_params *parser, char *str, int len, int seen_point) @@ -7662,8 +7664,7 @@ parse_percent(struct parser_params *pars https://github.com/ruby/ruby/blob/trunk/parse.y#L7664 } SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); pushback(c); - warn_balanced("%%", "string literal"); - return '%'; + return warn_balanced('%', "%%", "string literal"); } static int @@ -8108,8 +8109,7 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8109 c = tDSTAR; } else { - warn_balanced("**", "argument prefix"); - c = tPOW; + c = warn_balanced(tPOW, "**", "argument prefix"); } } else { @@ -8127,8 +8127,7 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8127 c = tSTAR; } else { - warn_balanced("*", "argument prefix"); - c = '*'; + c = warn_balanced('*', "*", "argument prefix"); } } SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); @@ -8234,8 +8233,7 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8233 return tOP_ASGN; } pushback(c); - warn_balanced("<<", "here document"); - return tLSHFT; + return warn_balanced(tLSHFT, "<<", "here document"); } pushback(c); return '<'; @@ -8314,8 +8312,7 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8312 c = tAMPER; } else { - warn_balanced("&", "argument prefix"); - c = '&'; + c = warn_balanced('&', "&", "argument prefix"); } SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); return c; @@ -8365,8 +8362,7 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8362 } SET_LEX_STATE(EXPR_BEG); pushback(c); - warn_balanced("+", "unary operator"); - return '+'; + return warn_balanced('+', "+", "unary operator"); case '-': c = nextc(); @@ -8398,8 +8394,7 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8394 } SET_LEX_STATE(EXPR_BEG); pushback(c); - warn_balanced("-", "unary operator"); - return '-'; + return warn_balanced('-', "-", "unary operator"); case '.': SET_LEX_STATE(EXPR_BEG); @@ -8448,9 +8443,9 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8443 } if (IS_END() || ISSPACE(c) || c == '#') { pushback(c); - warn_balanced(":", "symbol literal"); + c = warn_balanced(':', ":", "symbol literal"); SET_LEX_STATE(EXPR_BEG); - return ':'; + return c; } switch (c) { case '\'': @@ -8483,8 +8478,7 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8478 return tREGEXP_BEG; } SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG); - warn_balanced("/", "regexp literal"); - return '/'; + return warn_balanced('/', "/", "regexp literal"); case '^': if ((c = nextc()) == '=') { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/