1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2017-07-16 23:45:48 +00:00
parent 26888a8ee7
commit b17efb6874
2 changed files with 13 additions and 11 deletions

19
parse.y
View file

@ -4885,12 +4885,12 @@ ripper_yylval_id(ID x)
#endif #endif
#ifndef RIPPER #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_scan_event(t) ((void)0)
#define dispatch_delayed_token(t) ((void)0) #define dispatch_delayed_token(t) ((void)0)
#define has_delayed_token() (0) #define has_delayed_token() (0)
#else #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)) #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; size_t numlen;
int codepoint = scan_hex(lex_p, wide ? 6 : 4, &numlen); int codepoint = scan_hex(lex_p, wide ? 6 : 4, &numlen);
literal_flush(lex_p);
lex_p += numlen; lex_p += numlen;
if (wide ? (numlen == 0) : (numlen < 4)) { if (wide ? (numlen == 0) : (numlen < 4)) {
yyerror("invalid Unicode escape"); yyerror("invalid Unicode escape");
@ -5721,7 +5722,7 @@ parser_tokadd_utf8(struct parser_params *parser, rb_encoding **encp,
if (c != close_brace) { if (c != close_brace) {
unterminated: unterminated:
parser->tokp = lex_p; literal_flush(lex_p);
yyerror("unterminated Unicode escape"); yyerror("unterminated Unicode escape");
return 0; return 0;
} }
@ -6056,9 +6057,7 @@ parser_tokadd_string(struct parser_params *parser,
} }
} }
else if (c == '\\') { else if (c == '\\') {
#ifndef RIPPER literal_flush(lex_p - 1);
parser->tokp = lex_p - 1;
#endif
c = nextc(); c = nextc();
switch (c) { switch (c) {
case '\n': case '\n':
@ -6532,7 +6531,7 @@ parser_number_literal_suffix(struct parser_params *parser, int mask)
} }
if (!ISASCII(c) || ISALPHA(c) || c == '_') { if (!ISASCII(c) || ISALPHA(c) || c == '_') {
lex_p = lastp; lex_p = lastp;
numeric_literal_flush(lex_p); literal_flush(lex_p);
return 0; return 0;
} }
pushback(c); pushback(c);
@ -6546,7 +6545,7 @@ parser_number_literal_suffix(struct parser_params *parser, int mask)
} }
break; break;
} }
numeric_literal_flush(lex_p); literal_flush(lex_p);
return result; return result;
} }
@ -7375,7 +7374,7 @@ parse_numeric(struct parser_params *parser, int c)
if (nondigit) { if (nondigit) {
char tmp[30]; char tmp[30];
trailing_uc: trailing_uc:
numeric_literal_flush(lex_p - 1); literal_flush(lex_p - 1);
snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit); snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit);
yyerror(tmp); yyerror(tmp);
} }
@ -7397,7 +7396,7 @@ parse_numeric(struct parser_params *parser, int c)
} }
v = DBL2NUM(d); v = DBL2NUM(d);
} }
numeric_literal_flush(lex_p); literal_flush(lex_p);
return set_number_literal(v, type, suffix); return set_number_literal(v, type, suffix);
} }
suffix = number_literal_suffix(NUM_SUFFIX_ALL); suffix = number_literal_suffix(NUM_SUFFIX_ALL);

View file

@ -490,7 +490,10 @@ class TestParse < Test::Unit::TestCase
assert_equal(' ^', e.message.lines.last, mesg) assert_equal(' ^', e.message.lines.last, mesg)
e = assert_syntax_error('"\u{1234"', 'Unicode escape') 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/) e = assert_syntax_error('"\M1"', /escape character syntax/)
assert_equal(' ^~~', e.message.lines.last, mesg) assert_equal(' ^~~', e.message.lines.last, mesg)