From 992141878227439cd517d05b68f15e57d77452a5 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 29 Mar 2021 10:24:49 +0530 Subject: fix: handle unterminated strings gracefully --- src/lisp/error.rs | 4 ++-- src/lisp/lex.rs | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'src/lisp') diff --git a/src/lisp/error.rs b/src/lisp/error.rs index a360eb2..6d28c22 100644 --- a/src/lisp/error.rs +++ b/src/lisp/error.rs @@ -30,9 +30,9 @@ impl ParseError { pub fn new(span: Span, kind: ParseErrorKind) -> Self { Self { span, kind } } - pub fn fmt(&self, f: &mut fmt::Formatter<'_>, text: &str) -> fmt::Result { + pub fn display(&self, text: &str) -> String { let SpanDisplay { line, col, .. } = SpanDisplay::highlight_span(self.span, text); - write!(f, "line {}, col {}: {}", line, col, self.kind) + format!("line {}, col {}: {}", line, col, self.kind) } } diff --git a/src/lisp/lex.rs b/src/lisp/lex.rs index 1a34e53..e514d7f 100644 --- a/src/lisp/lex.rs +++ b/src/lisp/lex.rs @@ -210,6 +210,7 @@ fn parse_number<'a>(mut input: &'a str) -> Result<(usize, Token<'a>), ParseError fn parse_string<'a>(input: &'a str) -> Result<(usize, Token<'a>), ParseErrorKind> { // count opening quote let mut size = 1; + let mut closed = false; let mut chars = input.char_indices().skip(1); while let Some((ind, chr)) = chars.next() { match chr { @@ -218,12 +219,17 @@ fn parse_string<'a>(input: &'a str) -> Result<(usize, Token<'a>), ParseErrorKind } '"' => { size += ind; + closed = true; break; } _ => (), } } - return Ok((size, Token::String(&input[..size]))); + if !closed { + Err(ParseErrorKind::UnterminatedString) + } else { + Ok((size, Token::String(&input[..size]))) + } } fn is_ident(ch: char) -> bool { -- cgit v1.2.3