Skip to main content

oak_valkyrie/lexer/
mod.rs

1pub use keywords::ValkyrieKeywords;
2pub use token_type::ValkyrieTokenType;
3
4use crate::ValkyrieLanguage;
5use oak_core::{
6    Lexer, LexerState, TextEdit,
7    lexer::{LexOutput, LexerCache},
8    source::Source,
9};
10
11pub(crate) type State<'a, S> = LexerState<'a, S, ValkyrieLanguage>;
12
13/// Valkyrie lexer implementation.
14#[derive(Clone)]
15pub struct ValkyrieLexer<'config> {
16    pub(crate) config: &'config ValkyrieLanguage,
17}
18
19impl<'config> Lexer<ValkyrieLanguage> for ValkyrieLexer<'config> {
20    fn lex<'a, S: Source + ?Sized>(&self, source: &'a S, _edits: &[TextEdit], cache: &'a mut impl LexerCache<ValkyrieLanguage>) -> LexOutput<ValkyrieLanguage> {
21        let mut state = State::new(source);
22        let result = self.run(&mut state);
23        if result.is_ok() {
24            state.add_eof();
25        }
26        state.finish_with_cache(result, cache)
27    }
28}
29
30impl<'config> ValkyrieLexer<'config> {
31    /// Create a new Valkyrie lexer.
32    pub fn new(config: &'config ValkyrieLanguage) -> Self {
33        Self { config }
34    }
35}
36
37/// Token type definitions for the Valkyrie lexer.
38pub mod token_type;
39
40/// Keywords for the Valkyrie language.
41pub mod keywords;
42
43/// Lexer implementation.
44mod lex;