Skip to content

Commit 92f7da7

Browse files
authored
Merge pull request #21 from dylanowen/tokenizer
Fixed a tokenizer edge case for strings
2 parents 0327873 + 3263b73 commit 92f7da7

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/tokenizer.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ impl<'a> TokenStream<'a> {
250250
)
251251
)
252252
} else {
253-
let mut prev_char = cur_char;
254253
let mut nchars = 1;
254+
let mut escaped = false;
255255
for (idx, cur_char) in iter {
256256
nchars += 1;
257257
match cur_char {
258-
'"' if prev_char == '\\' => {}
258+
'"' if escaped => {}
259259
'"' => {
260260
self.position.column += nchars;
261261
self.off += idx+1;
@@ -268,11 +268,14 @@ impl<'a> TokenStream<'a> {
268268
)
269269
);
270270
}
271+
271272
_ => {
272273

273274
}
274275
}
275-
prev_char = cur_char;
276+
277+
// if we aren't escaped and the current char is a \, we are now escaped
278+
escaped = !escaped && cur_char == '\\';
276279
}
277280
Err(
278281
Error::unexpected_message(
@@ -482,12 +485,17 @@ mod test {
482485
#[test] #[should_panic] fn letters_float2() { tok_str("0.bbc"); }
483486
#[test] #[should_panic] fn letters_float3() { tok_str("0.bbce0"); }
484487
#[test] #[should_panic] fn no_exp_sign_float() { tok_str("0e0"); }
488+
#[test] #[should_panic] fn unterminated_string() { tok_str(r#""hello\""#); }
489+
#[test] #[should_panic] fn extra_unterminated_string() { tok_str(r#""hello\\\""#); }
485490

486491
#[test]
487492
fn string() {
488493
assert_eq!(tok_str(r#""""#), [r#""""#]);
489494
assert_eq!(tok_typ(r#""""#), [StringValue]);
490495
assert_eq!(tok_str(r#""hello""#), [r#""hello""#]);
496+
assert_eq!(tok_str(r#""hello\\""#), [r#""hello\\""#]);
497+
assert_eq!(tok_str(r#""hello\\\\""#), [r#""hello\\\\""#]);
498+
assert_eq!(tok_str(r#""he\\llo""#), [r#""he\\llo""#]);
491499
assert_eq!(tok_typ(r#""hello""#), [StringValue]);
492500
assert_eq!(tok_str(r#""my\"quote""#), [r#""my\"quote""#]);
493501
assert_eq!(tok_typ(r#""my\"quote""#), [StringValue]);

0 commit comments

Comments
 (0)