1#![doc = include_str!("readme.md")]
2pub mod token_type;
7
8pub use token_type::BatTokenType;
9
10use crate::language::BatLanguage;
11use oak_core::{Lexer, LexerCache, LexerState, OakError, lexer::LexOutput, source::Source};
12
13pub(crate) type State<'a, S> = LexerState<'a, S, BatLanguage>;
14
15#[derive(Clone)]
20pub struct BatLexer<'config> {
21 config: &'config BatLanguage,
22}
23
24impl<'config> Lexer<BatLanguage> for BatLexer<'config> {
25 fn lex<'a, S: Source + ?Sized>(&self, source: &S, _edits: &[oak_core::source::TextEdit], cache: &'a mut impl LexerCache<BatLanguage>) -> LexOutput<BatLanguage> {
26 let mut state = LexerState::new_with_cache(source, 0, cache);
27 let result = self.run(&mut state);
28 if result.is_ok() {
29 state.add_eof()
30 }
31 state.finish_with_cache(result, cache)
32 }
33}
34
35impl<'config> BatLexer<'config> {
36 pub fn new(config: &'config BatLanguage) -> Self {
42 Self { config }
43 }
44
45 fn run<'a, S: Source + ?Sized>(&self, state: &mut State<'a, S>) -> Result<(), OakError> {
46 while state.not_at_end() {
47 let start_pos = state.get_position();
48 if let Some(ch) = state.peek() {
49 state.advance(ch.len_utf8());
50 state.add_token(BatTokenType::Text, start_pos, state.get_position())
51 }
52 }
53 Ok(())
54 }
55}