mirror of
https://github.com/joshuadavidthomas/django-template-ast.git
synced 2025-08-04 00:48:16 +00:00
move errors to dedicated file
This commit is contained in:
parent
aa7913b988
commit
9c816917cb
3 changed files with 22 additions and 20 deletions
20
src/error.rs
Normal file
20
src/error.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LexerError {
|
||||
EmptyToken(usize),
|
||||
UnexpectedCharacter(char, usize),
|
||||
}
|
||||
|
||||
impl fmt::Display for LexerError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
LexerError::EmptyToken(line) => write!(f, "Empty token at line {}", line),
|
||||
LexerError::UnexpectedCharacter(c, line) => {
|
||||
write!(f, "Unexpected character '{}' at line {}", c, line)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LexerError {}
|
21
src/lexer.rs
21
src/lexer.rs
|
@ -1,6 +1,6 @@
|
|||
use std::fmt;
|
||||
use std::fmt::Debug;
|
||||
|
||||
use crate::error::LexerError;
|
||||
use crate::scanner::{Scanner, ScannerState};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
@ -70,25 +70,6 @@ pub trait Tokenizer: Scanner {
|
|||
fn add_token(&mut self, token_type: Self::TokenType);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LexerError {
|
||||
EmptyToken(usize),
|
||||
UnexpectedCharacter(char, usize),
|
||||
}
|
||||
|
||||
impl fmt::Display for LexerError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
LexerError::EmptyToken(line) => write!(f, "Empty token at line {}", line),
|
||||
LexerError::UnexpectedCharacter(c, line) => {
|
||||
write!(f, "Unexpected character '{}' at line {}", c, line)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LexerError {}
|
||||
|
||||
pub struct Lexer {
|
||||
source: String,
|
||||
tokens: Vec<Token>,
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
mod error;
|
||||
mod lexer;
|
||||
mod scanner;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue