#![expect(clippy::inline_always)]
use std::ops::Index;
use crate::lexer::{ByteHandler, ByteHandlers, byte_handler_tables};
pub trait ParserConfig: Default {
type LexerConfig: LexerConfig;
fn lexer_config(&self) -> Self::LexerConfig;
}
#[derive(Copy, Clone, Default)]
pub struct NoTokensParserConfig;
impl ParserConfig for NoTokensParserConfig {
type LexerConfig = NoTokensLexerConfig;
#[inline(always)]
fn lexer_config(&self) -> NoTokensLexerConfig {
NoTokensLexerConfig
}
}
#[derive(Copy, Clone, Default)]
pub struct TokensParserConfig;
impl ParserConfig for TokensParserConfig {
type LexerConfig = TokensLexerConfig;
#[inline(always)]
fn lexer_config(&self) -> TokensLexerConfig {
TokensLexerConfig
}
}
#[derive(Copy, Clone, Default)]
#[repr(transparent)]
pub struct RuntimeParserConfig {
lexer_config: RuntimeLexerConfig,
}
impl RuntimeParserConfig {
#[inline(always)]
pub fn new(tokens: bool) -> Self {
Self { lexer_config: RuntimeLexerConfig::new(tokens) }
}
}
impl ParserConfig for RuntimeParserConfig {
type LexerConfig = RuntimeLexerConfig;
#[inline(always)]
fn lexer_config(&self) -> RuntimeLexerConfig {
self.lexer_config
}
}
pub trait LexerConfig: Default {
type ByteHandlers: Index<usize, Output = ByteHandler<Self>>;
const TOKENS_METHOD_IS_STATIC: bool;
fn tokens(&self) -> bool;
fn byte_handlers(&self) -> &Self::ByteHandlers;
}
#[derive(Copy, Clone, Default)]
pub struct NoTokensLexerConfig;
impl LexerConfig for NoTokensLexerConfig {
type ByteHandlers = ByteHandlers<Self>;
const TOKENS_METHOD_IS_STATIC: bool = true;
#[inline(always)]
fn tokens(&self) -> bool {
false
}
#[inline(always)]
fn byte_handlers(&self) -> &Self::ByteHandlers {
&byte_handler_tables::NO_TOKENS
}
}
#[derive(Copy, Clone, Default)]
pub struct TokensLexerConfig;
impl LexerConfig for TokensLexerConfig {
type ByteHandlers = ByteHandlers<Self>;
const TOKENS_METHOD_IS_STATIC: bool = true;
#[inline(always)]
fn tokens(&self) -> bool {
true
}
#[inline(always)]
fn byte_handlers(&self) -> &Self::ByteHandlers {
&byte_handler_tables::WITH_TOKENS
}
}
#[derive(Copy, Clone, Default)]
#[repr(transparent)]
pub struct RuntimeLexerConfig {
tokens: bool,
}
impl RuntimeLexerConfig {
#[inline(always)]
pub fn new(tokens: bool) -> Self {
Self { tokens }
}
}
impl LexerConfig for RuntimeLexerConfig {
type ByteHandlers = ByteHandlers<Self>;
const TOKENS_METHOD_IS_STATIC: bool = false;
#[inline(always)]
fn tokens(&self) -> bool {
self.tokens
}
#[inline(always)]
fn byte_handlers(&self) -> &Self::ByteHandlers {
&byte_handler_tables::RUNTIME_TOKENS
}
}