Skip to main content

oak_bat/lexer/
mod.rs

1#![doc = include_str!("readme.md")]
2/// Token type definitions for Windows Batch (BAT) lexical analysis.
3///
4/// This module provides [`BatTokenType`] which defines all token types
5/// recognized by the BAT lexer, including keywords, labels, and text.
6pub 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/// Lexer for Windows Batch (BAT) files.
16///
17/// This lexer tokenizes BAT source code into a sequence of tokens
18/// that can be processed by the parser.
19#[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    /// Creates a new `BatLexer` instance with the specified language configuration.
37    ///
38    /// # Arguments
39    ///
40    /// * `config` - A reference to the `BatLanguage` configuration.
41    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}