Skip to content

Commit

Permalink
add error handling for unterminated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser0 committed Jun 18, 2024
1 parent 59e50af commit 7f5b0e9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use thiserror::Error;

use crate::frontend::{ast::Expr, lexer::Token};

// Universal Errors used by functions like parser::produce_ast()
#[derive(Error, Debug)]
pub enum Error {
#[error("Lexer Error: {0}")]
Expand All @@ -17,6 +18,7 @@ pub enum Error {
// TODO: RuntimeError(RuntimeError),
}

// Lexer-specific Errors
#[derive(Error, Debug)]
pub enum LexerError {
#[error("Unrecognized character '{0}'")]
Expand All @@ -35,6 +37,7 @@ pub enum LexerError {
InternalError(String),
}

// Parser-specific Errors
#[derive(Error, Debug)]
pub enum ParserError {
#[error("Lexer Error: {0}")]
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ fn tokenize_string_literal(
match ch {
'"' => {
chars.next(); // Consume the closing quote
break;
tokens.push(Token::StringLiteral(literal));
return Ok(());
}
'\\' => {
chars.next(); // Consume the backslash
Expand All @@ -430,6 +431,5 @@ fn tokenize_string_literal(
}
}
}
tokens.push(Token::StringLiteral(literal));
Ok(())
Err(LexerError::UnterminatedStringLiteral)
}

0 comments on commit 7f5b0e9

Please sign in to comment.