From 02f37870e5552aed7505f9fa7ec1e46d176030e7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 22 Sep 2020 19:18:06 +0800 Subject: [PATCH] Better position of string literal errors. --- src/token.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/token.rs b/src/token.rs index c1968d23..ad0ef340 100644 --- a/src/token.rs +++ b/src/token.rs @@ -752,8 +752,10 @@ pub fn parse_string_literal( let mut result: StaticVec = Default::default(); let mut escape: StaticVec = Default::default(); + let start = *pos; + loop { - let next_char = stream.get_next().ok_or((LERR::UnterminatedString, *pos))?; + let next_char = stream.get_next().ok_or((LERR::UnterminatedString, start))?; pos.advance(); @@ -838,17 +840,19 @@ pub fn parse_string_literal( ch if enclosing_char == ch && escape.is_empty() => break, // Unknown escape sequence - _ if !escape.is_empty() => { + ch if !escape.is_empty() => { + escape.push(ch); + return Err(( LERR::MalformedEscapeSequence(escape.into_iter().collect()), *pos, - )) + )); } // Cannot have new-lines inside string literals '\n' => { pos.rewind(); - return Err((LERR::UnterminatedString, *pos)); + return Err((LERR::UnterminatedString, start)); } // All other characters