Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions crates/oxc_parser/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ use crate::lexer::{ByteHandler, ByteHandlers, byte_handler_tables};
///
/// You can also create your own config by implementing [`ParserConfig`] on a type.
pub trait ParserConfig: Default {
/// Type of [`LexerConfig`] this [`ParserConfig`] uses.
type LexerConfig: LexerConfig;

/// Get [`LexerConfig`] for this [`ParserConfig`].
fn lexer_config(&self) -> Self::LexerConfig;
}

Expand Down Expand Up @@ -67,7 +69,7 @@ impl ParserConfig for TokensParserConfig {
}
}

/// Parser config for parsing with/without tokens, decided at runtime.
/// Parser config for parsing with / without tokens, decided at runtime.
///
/// See [`ParserConfig`] for more details.
#[derive(Copy, Clone, Default)]
Expand All @@ -93,11 +95,29 @@ impl ParserConfig for RuntimeParserConfig {
}

/// Lexer config.
///
/// See [`ParserConfig`] for more details.
///
/// We have to define a different byte handler table for each config, as byte handler functions
/// are generic over the [`LexerConfig`].
///
/// Byte handler tables are defined within lexer (`lexer/byte_handlers.rs`).
/// We need them to be defined as `static` items, for performance.
/// 1. `static` ensures that only 1 copy of each byte handler table appears in the binary
/// (even if `Lexer::handle_byte` is inlined in multiple places).
/// 2. Indexing into byte handlers table is an extremely hot path, and even 1 extra indirection (pointer chasing)
/// has a sizeable impact on performance.
///
/// An associated type and `byte_handlers` method is the only way to make this work with Rust's type system
/// and borrow checker.
pub trait LexerConfig: Default {
/// Byte handlers table type.
type ByteHandlers: Index<usize, Output = ByteHandler<Self>>;

/// Returns `true` if tokens are enabled.
fn tokens(&self) -> bool;

/// Get byte handlers table to use in lexer with this config.
fn byte_handlers(&self) -> &Self::ByteHandlers;
}

Expand Down Expand Up @@ -137,7 +157,7 @@ impl LexerConfig for TokensLexerConfig {
}
}

/// Lexer config for lexing with/without tokens, decided at runtime.
/// Lexer config for lexing with / without tokens, decided at runtime.
#[derive(Copy, Clone, Default)]
#[repr(transparent)]
pub struct RuntimeLexerConfig {
Expand Down
Loading