Skip to content

Commit 45f0dde

Browse files
authored
Merge pull request #392 from dtolnay/whitespace
Stricter parsing of string_continue escapes in cooked string
2 parents 3e8d8ee + 10bbe3f commit 45f0dde

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/parse.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ fn cooked_string(input: Cursor) -> Result<Cursor, Reject> {
396396
return Err(Reject);
397397
}
398398
match chars.peek() {
399-
Some((_, ch)) if ch.is_whitespace() => {
399+
Some((_, ch @ ' ')) | Some((_, ch @ '\t')) | Some((_, ch @ '\n'))
400+
| Some((_, ch @ '\r')) => {
400401
last = *ch;
401402
chars.next();
402403
}
@@ -451,7 +452,10 @@ fn cooked_byte_string(mut input: Cursor) -> Result<Cursor, Reject> {
451452
return Err(Reject);
452453
}
453454
match chars.next() {
454-
Some((_, ch)) if ch.is_whitespace() => last = ch,
455+
Some((_, ch @ ' ')) | Some((_, ch @ '\t')) | Some((_, ch @ '\n'))
456+
| Some((_, ch @ '\r')) => {
457+
last = ch;
458+
}
455459
Some((offset, _)) => {
456460
input = rest.advance(offset);
457461
bytes = input.bytes().enumerate();

tests/test.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ fn literal_string() {
119119
Literal::string("a\00b\07c\08d\0e\0").to_string(),
120120
"\"a\\x000b\\x007c\\08d\\0e\\0\"",
121121
);
122+
123+
"\"\\\r\n x\"".parse::<TokenStream>().unwrap();
124+
"\"\\\r\n \rx\"".parse::<TokenStream>().unwrap_err();
122125
}
123126

124127
#[test]
@@ -156,6 +159,10 @@ fn literal_byte_string() {
156159
Literal::byte_string(b"a\00b\07c\08d\0e\0").to_string(),
157160
"b\"a\\x000b\\x007c\\08d\\0e\\0\"",
158161
);
162+
163+
"b\"\\\r\n x\"".parse::<TokenStream>().unwrap();
164+
"b\"\\\r\n \rx\"".parse::<TokenStream>().unwrap_err();
165+
"b\"\\\r\n \u{a0}x\"".parse::<TokenStream>().unwrap_err();
159166
}
160167

161168
#[test]
@@ -657,7 +664,6 @@ fn non_ascii_tokens() {
657664
check_spans("ábc// foo", &[(1, 0, 1, 3)]);
658665
check_spans("ábć// foo", &[(1, 0, 1, 3)]);
659666
check_spans("b\"a\\\n c\"", &[(1, 0, 2, 3)]);
660-
check_spans("b\"a\\\n\u{00a0}c\"", &[(1, 0, 2, 3)]);
661667
}
662668

663669
#[cfg(span_locations)]

0 commit comments

Comments
 (0)