From b17efb6874345c2dc8dc41f76226463b9a785123 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 16 Jul 2017 23:45:48 +0000 Subject: [PATCH] parse.y: refine invalid Unicode escape message * parse.y (literal_flush): rename from numeric_literal_flush, as it is not just for numerics now. * parse.y (parser_tokadd_codepoint): show invalid character position, but not the start of Unicode escape. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59348 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- parse.y | 19 +++++++++---------- test/ruby/test_parse.rb | 5 ++++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/parse.y b/parse.y index ec0541d7ce..ceb8cc8d48 100644 --- a/parse.y +++ b/parse.y @@ -4885,12 +4885,12 @@ ripper_yylval_id(ID x) #endif #ifndef RIPPER -#define numeric_literal_flush(p) (parser->tokp = (p)) +#define literal_flush(p) (parser->tokp = (p)) #define dispatch_scan_event(t) ((void)0) #define dispatch_delayed_token(t) ((void)0) #define has_delayed_token() (0) #else -#define numeric_literal_flush(p) ((void)0) +#define literal_flush(p) ((void)0) #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val)) @@ -5652,6 +5652,7 @@ parser_tokadd_codepoint(struct parser_params *parser, rb_encoding **encp, { size_t numlen; int codepoint = scan_hex(lex_p, wide ? 6 : 4, &numlen); + literal_flush(lex_p); lex_p += numlen; if (wide ? (numlen == 0) : (numlen < 4)) { yyerror("invalid Unicode escape"); @@ -5721,7 +5722,7 @@ parser_tokadd_utf8(struct parser_params *parser, rb_encoding **encp, if (c != close_brace) { unterminated: - parser->tokp = lex_p; + literal_flush(lex_p); yyerror("unterminated Unicode escape"); return 0; } @@ -6056,9 +6057,7 @@ parser_tokadd_string(struct parser_params *parser, } } else if (c == '\\') { -#ifndef RIPPER - parser->tokp = lex_p - 1; -#endif + literal_flush(lex_p - 1); c = nextc(); switch (c) { case '\n': @@ -6532,7 +6531,7 @@ parser_number_literal_suffix(struct parser_params *parser, int mask) } if (!ISASCII(c) || ISALPHA(c) || c == '_') { lex_p = lastp; - numeric_literal_flush(lex_p); + literal_flush(lex_p); return 0; } pushback(c); @@ -6546,7 +6545,7 @@ parser_number_literal_suffix(struct parser_params *parser, int mask) } break; } - numeric_literal_flush(lex_p); + literal_flush(lex_p); return result; } @@ -7375,7 +7374,7 @@ parse_numeric(struct parser_params *parser, int c) if (nondigit) { char tmp[30]; trailing_uc: - numeric_literal_flush(lex_p - 1); + literal_flush(lex_p - 1); snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit); yyerror(tmp); } @@ -7397,7 +7396,7 @@ parse_numeric(struct parser_params *parser, int c) } v = DBL2NUM(d); } - numeric_literal_flush(lex_p); + literal_flush(lex_p); return set_number_literal(v, type, suffix); } suffix = number_literal_suffix(NUM_SUFFIX_ALL); diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index b3795a4ac4..7ce236679c 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -490,7 +490,10 @@ class TestParse < Test::Unit::TestCase assert_equal(' ^', e.message.lines.last, mesg) e = assert_syntax_error('"\u{1234"', 'Unicode escape') - assert_match(' ^~~~~~~', e.message.lines.last, mesg) + assert_match(' ^', e.message.lines.last, mesg) + + e = assert_syntax_error('"\u{xxxx}"', 'invalid Unicode escape') + assert_match(' ^', e.message.lines.last, mesg) e = assert_syntax_error('"\M1"', /escape character syntax/) assert_equal(' ^~~', e.message.lines.last, mesg)