1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#![doc = include_str!("../README.md")]
use std::fmt;

use regex_automata::{dfa::Automaton, PatternID};

pub use lexi_matic_derive::Lexer;
#[doc(hidden)]
pub use regex_automata::dfa::dense::DFA;

#[derive(Debug)]
pub struct Error(pub usize);

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "lexical error at {}", self.0)
    }
}

impl std::error::Error for Error {}

pub trait Lexer<'a>: Sized {
    type Iterator: IntoIterator<Item = Result<(usize, Self, usize), Error>>;
    fn lex(input: &'a str) -> Self::Iterator;
}

#[doc(hidden)]
pub fn dfa_search_next(dfa: &DFA<&[u32]>, input: &str) -> Option<(PatternID, usize)> {
    let m = dfa
        .try_search_fwd(&regex_automata::Input::new(input).anchored(regex_automata::Anchored::Yes))
        .ok()??;
    Some((m.pattern(), m.offset()))
}