mirror of
https://github.com/ruby/ruby.git
synced 2025-08-28 07:26:00 +02:00
parse.y: refine error messages
* parse.y (parser_read_escape, parser_tok_hex): refine error messages. point from the backslash up to the invalid char. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59341 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a7452a8f4f
commit
70af8d3c9c
2 changed files with 15 additions and 6 deletions
7
parse.y
7
parse.y
|
@ -5636,6 +5636,7 @@ parser_tok_hex(struct parser_params *parser, size_t *numlen)
|
||||||
|
|
||||||
c = scan_hex(lex_p, 2, numlen);
|
c = scan_hex(lex_p, 2, numlen);
|
||||||
if (!*numlen) {
|
if (!*numlen) {
|
||||||
|
parser->tokp = lex_p;
|
||||||
yyerror("invalid hex escape");
|
yyerror("invalid hex escape");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -5780,7 +5781,6 @@ parser_read_escape(struct parser_params *parser, int flags,
|
||||||
case 'M':
|
case 'M':
|
||||||
if (flags & ESCAPE_META) goto eof;
|
if (flags & ESCAPE_META) goto eof;
|
||||||
if ((c = nextc()) != '-') {
|
if ((c = nextc()) != '-') {
|
||||||
pushback(c);
|
|
||||||
goto eof;
|
goto eof;
|
||||||
}
|
}
|
||||||
if ((c = nextc()) == '\\') {
|
if ((c = nextc()) == '\\') {
|
||||||
|
@ -5794,7 +5794,6 @@ parser_read_escape(struct parser_params *parser, int flags,
|
||||||
|
|
||||||
case 'C':
|
case 'C':
|
||||||
if ((c = nextc()) != '-') {
|
if ((c = nextc()) != '-') {
|
||||||
pushback(c);
|
|
||||||
goto eof;
|
goto eof;
|
||||||
}
|
}
|
||||||
case 'c':
|
case 'c':
|
||||||
|
@ -5811,6 +5810,7 @@ parser_read_escape(struct parser_params *parser, int flags,
|
||||||
eof:
|
eof:
|
||||||
case -1:
|
case -1:
|
||||||
yyerror("Invalid escape character syntax");
|
yyerror("Invalid escape character syntax");
|
||||||
|
pushback(c);
|
||||||
return '\0';
|
return '\0';
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -6045,6 +6045,9 @@ parser_tokadd_string(struct parser_params *parser,
|
||||||
}
|
}
|
||||||
else if (c == '\\') {
|
else if (c == '\\') {
|
||||||
const char *beg = lex_p - 1;
|
const char *beg = lex_p - 1;
|
||||||
|
#ifndef RIPPER
|
||||||
|
parser->tokp = beg;
|
||||||
|
#endif
|
||||||
c = nextc();
|
c = nextc();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '\n':
|
case '\n':
|
||||||
|
|
|
@ -484,21 +484,27 @@ class TestParse < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_string
|
def test_string
|
||||||
assert_raise(SyntaxError) do
|
mesg = 'from the backslash through the invalid char'
|
||||||
|
|
||||||
|
e = assert_raise_with_message(SyntaxError, /hex escape/) do
|
||||||
eval '"\xg1"'
|
eval '"\xg1"'
|
||||||
end
|
end
|
||||||
|
assert_equal(' ^', e.message.lines.last, mesg)
|
||||||
|
|
||||||
assert_raise(SyntaxError) do
|
e = assert_raise(SyntaxError) do
|
||||||
eval '"\u{1234"'
|
eval '"\u{1234"'
|
||||||
end
|
end
|
||||||
|
assert_match(' ^~~~~~~', e.message.lines.last, mesg)
|
||||||
|
|
||||||
assert_raise(SyntaxError) do
|
e = assert_raise_with_message(SyntaxError, /escape character syntax/) do
|
||||||
eval '"\M1"'
|
eval '"\M1"'
|
||||||
end
|
end
|
||||||
|
assert_equal(' ^~~', e.message.lines.last, mesg)
|
||||||
|
|
||||||
assert_raise(SyntaxError) do
|
e = assert_raise_with_message(SyntaxError, /escape character syntax/) do
|
||||||
eval '"\C1"'
|
eval '"\C1"'
|
||||||
end
|
end
|
||||||
|
assert_equal(' ^~~', e.message.lines.last, mesg)
|
||||||
|
|
||||||
assert_equal("\x81", eval('"\C-\M-a"'))
|
assert_equal("\x81", eval('"\C-\M-a"'))
|
||||||
assert_equal("\177", eval('"\c?"'))
|
assert_equal("\177", eval('"\c?"'))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue