Skip to main content

lex_core/lex/lexing/
common.rs

1//! Common lexer module
2//!
3//! This module contains shared interfaces and utilities for lexer implementations.
4
5use crate::lex::token::Token;
6use std::fmt;
7use std::ops::Range;
8
9/// Output from a lexer
10#[derive(Debug, Clone)]
11pub enum LexerOutput {
12    /// Flat sequence of tokens
13    Flat(Vec<(Token, Range<usize>)>),
14}
15
16/// Errors that can occur during lexing
17#[derive(Debug, Clone)]
18pub enum LexError {
19    /// Generic error message
20    Error(String),
21    /// Error during transformation phase
22    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
42/// Trait for lexer implementations
43pub trait Lexer {
44    /// Lex the source text
45    fn lex(&self, source: &str) -> Result<LexerOutput, LexError>;
46}