Skip to main content

oak_cmd/lexer/
mod.rs

1#![doc = include_str!("readme.md")]
2pub mod token_type;
3
4pub use token_type::CmdTokenType;
5
6use crate::language::CmdLanguage;
7use oak_core::{Lexer, LexerCache, LexerState, OakError, lexer::LexOutput, source::Source};
8
9type State<'a, S> = LexerState<'a, S, CmdLanguage>;
10
11#[derive(Clone)]
12pub struct CmdLexer<'config> {
13    _config: &'config CmdLanguage,
14}
15
16impl<'config> Lexer<CmdLanguage> for CmdLexer<'config> {
17    fn lex<'a, S: Source + ?Sized>(&self, source: &S, _edits: &[oak_core::source::TextEdit], cache: &'a mut impl LexerCache<CmdLanguage>) -> LexOutput<CmdLanguage> {
18        let mut state = LexerState::new_with_cache(source, 0, cache);
19        let result = self.run(&mut state);
20        if result.is_ok() {
21            state.add_eof()
22        }
23        state.finish_with_cache(result, cache)
24    }
25}
26
27impl<'config> CmdLexer<'config> {
28    pub fn new(config: &'config CmdLanguage) -> Self {
29        Self { _config: config }
30    }
31
32    fn run<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> Result<(), OakError> {
33        while state.not_at_end() {
34            let start_pos = state.get_position();
35            if let Some(ch) = state.peek() {
36                state.advance(ch.len_utf8());
37                state.add_token(CmdTokenType::Text, start_pos, state.get_position())
38            }
39        }
40        Ok(())
41    }
42}