aboutsummaryrefslogtreecommitdiff
path: root/src/lisp
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-03-29 05:54:49 +0100
committerAkshay <[email protected]>2021-03-29 05:54:49 +0100
commit992141878227439cd517d05b68f15e57d77452a5 (patch)
treed754797718cbe3b551e2abd737ccd1d0986d6b55 /src/lisp
parent9632fdd69442b10972cc1f71f7a2f11ecc1ca47a (diff)
fix: handle unterminated strings gracefully
Diffstat (limited to 'src/lisp')
-rw-r--r--src/lisp/error.rs4
-rw-r--r--src/lisp/lex.rs8
2 files changed, 9 insertions, 3 deletions
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 {
30 pub fn new(span: Span, kind: ParseErrorKind) -> Self { 30 pub fn new(span: Span, kind: ParseErrorKind) -> Self {
31 Self { span, kind } 31 Self { span, kind }
32 } 32 }
33 pub fn fmt(&self, f: &mut fmt::Formatter<'_>, text: &str) -> fmt::Result { 33 pub fn display(&self, text: &str) -> String {
34 let SpanDisplay { line, col, .. } = SpanDisplay::highlight_span(self.span, text); 34 let SpanDisplay { line, col, .. } = SpanDisplay::highlight_span(self.span, text);
35 write!(f, "line {}, col {}: {}", line, col, self.kind) 35 format!("line {}, col {}: {}", line, col, self.kind)
36 } 36 }
37} 37}
38 38
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
210fn parse_string<'a>(input: &'a str) -> Result<(usize, Token<'a>), ParseErrorKind> { 210fn parse_string<'a>(input: &'a str) -> Result<(usize, Token<'a>), ParseErrorKind> {
211 // count opening quote 211 // count opening quote
212 let mut size = 1; 212 let mut size = 1;
213 let mut closed = false;
213 let mut chars = input.char_indices().skip(1); 214 let mut chars = input.char_indices().skip(1);
214 while let Some((ind, chr)) = chars.next() { 215 while let Some((ind, chr)) = chars.next() {
215 match chr { 216 match chr {
@@ -218,12 +219,17 @@ fn parse_string<'a>(input: &'a str) -> Result<(usize, Token<'a>), ParseErrorKind
218 } 219 }
219 '"' => { 220 '"' => {
220 size += ind; 221 size += ind;
222 closed = true;
221 break; 223 break;
222 } 224 }
223 _ => (), 225 _ => (),
224 } 226 }
225 } 227 }
226 return Ok((size, Token::String(&input[..size]))); 228 if !closed {
229 Err(ParseErrorKind::UnterminatedString)
230 } else {
231 Ok((size, Token::String(&input[..size])))
232 }
227} 233}
228 234
229fn is_ident(ch: char) -> bool { 235fn is_ident(ch: char) -> bool {