lex_core/lex/lexing/
common.rs1use crate::lex::token::Token;
6use std::fmt;
7use std::ops::Range;
8
9#[derive(Debug, Clone)]
11pub enum LexerOutput {
12 Flat(Vec<(Token, Range<usize>)>),
14}
15
16#[derive(Debug, Clone)]
18pub enum LexError {
19 Error(String),
21 Transformation(String),
23}
24
25impl fmt::Display for LexError {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 match self {
28 LexError::Error(msg) => write!(f, "Lexing error: {msg}"),
29 LexError::Transformation(msg) => write!(f, "Transformation error: {msg}"),
30 }
31 }
32}
33
34impl std::error::Error for LexError {}
35
36impl From<LexError> for String {
37 fn from(err: LexError) -> Self {
38 err.to_string()
39 }
40}
41
42pub trait Lexer {
44 fn lex(&self, source: &str) -> Result<LexerOutput, LexError>;
46}