mirror of
https://github.com/joshuadavidthomas/django-template-ast.git
synced 2025-09-12 19:46:18 +00:00
move errors to dedicated file (#7)
This commit is contained in:
parent
aa7913b988
commit
deebe6d2f8
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 std::fmt::Debug;
|
||||||
|
|
||||||
|
use crate::error::LexerError;
|
||||||
use crate::scanner::{Scanner, ScannerState};
|
use crate::scanner::{Scanner, ScannerState};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -70,25 +70,6 @@ pub trait Tokenizer: Scanner {
|
||||||
fn add_token(&mut self, token_type: Self::TokenType);
|
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 {
|
pub struct Lexer {
|
||||||
source: String,
|
source: String,
|
||||||
tokens: Vec<Token>,
|
tokens: Vec<Token>,
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
|
mod error;
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod scanner;
|
mod scanner;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue