fix bug on error position

This commit is contained in:
Jihyun Yu 2018-09-29 15:39:58 +09:00
parent 22726c6752
commit ab670b4e9f
2 changed files with 30 additions and 7 deletions

View File

@ -455,10 +455,10 @@ impl<'a> SliceRead<'a> {
start = self.index;
}
_ => {
self.index += 1;
if validate {
return error(self, ErrorCode::ControlCharacterWhileParsingString);
}
self.index += 1;
}
}
}
@ -548,17 +548,20 @@ impl<'a> Read<'a> for SliceRead<'a> {
fn decode_hex_escape(&mut self) -> Result<u16> {
if self.index + 4 > self.slice.len() {
self.index = self.slice.len();
return error(self, ErrorCode::EofWhileParsingString);
}
let mut n = 0;
for _ in 0..4 {
match decode_hex_val(self.slice[self.index]) {
let ch = decode_hex_val(self.slice[self.index]);
self.index += 1;
match ch {
None => return error(self, ErrorCode::InvalidEscape),
Some(val) => {
n = (n << 4) + val;
}
}
self.index += 1;
}
Ok(n)
}

View File

@ -995,11 +995,11 @@ fn test_parse_string() {
),
(
"\"\n\"",
"control character (\\u0000-\\u001F) found while parsing a string at line 1 column 1",
"control character (\\u0000-\\u001F) found while parsing a string at line 2 column 0",
),
(
"\"\x1F\"",
"control character (\\u0000-\\u001F) found while parsing a string at line 1 column 1",
"control character (\\u0000-\\u001F) found while parsing a string at line 1 column 2",
),
]);
@ -1012,13 +1012,33 @@ fn test_parse_string() {
&[b'"', b'\\', b'n', 159, 146, 150, b'"'],
"invalid unicode code point at line 1 column 7",
),
(
&[b'"', b'\\', b'u', 48, 48, 51],
"EOF while parsing a string at line 1 column 6",
),
(
&[b'"', b'\\', b'u', 250, 48, 51, 48, b'"'],
"invalid escape at line 1 column 4",
),
(
&[b'"', b'\\', b'u', 48, 250, 51, 48, b'"'],
"invalid escape at line 1 column 5",
),
(
&[b'"', b'\\', b'u', 48, 48, 250, 48, b'"'],
"invalid escape at line 1 column 6",
),
(
&[b'"', b'\\', b'u', 48, 48, 51, 250, b'"'],
"invalid escape at line 1 column 7",
),
(
&[b'"', b'\n', b'"'],
"control character (\\u0000-\\u001F) found while parsing a string at line 1 column 1",
"control character (\\u0000-\\u001F) found while parsing a string at line 2 column 0",
),
(
&[b'"', b'\x1F', b'"'],
"control character (\\u0000-\\u001F) found while parsing a string at line 1 column 1",
"control character (\\u0000-\\u001F) found while parsing a string at line 1 column 2",
),
]);