diff --git a/crates/oxc_parser/src/config.rs b/crates/oxc_parser/src/config.rs index ac70ce9dc3bec..065916b470cac 100644 --- a/crates/oxc_parser/src/config.rs +++ b/crates/oxc_parser/src/config.rs @@ -114,6 +114,16 @@ pub trait LexerConfig: Default { /// Byte handlers table type. type ByteHandlers: Index>; + /// `true` if [`tokens`] method returns a static value. + /// + /// This const is useful in situations where code can be more efficient + /// when the return value of [`tokens`] method is known at compile time. + /// + /// Implementors of this trait MUST only set this const to `true` if [`tokens`] method returns a static value. + /// + /// [`tokens`]: LexerConfig::tokens + const TOKENS_METHOD_IS_STATIC: bool; + /// Returns `true` if tokens are enabled. fn tokens(&self) -> bool; @@ -128,6 +138,8 @@ pub struct NoTokensLexerConfig; impl LexerConfig for NoTokensLexerConfig { type ByteHandlers = ByteHandlers; + const TOKENS_METHOD_IS_STATIC: bool = true; + #[inline(always)] fn tokens(&self) -> bool { false @@ -146,6 +158,8 @@ pub struct TokensLexerConfig; impl LexerConfig for TokensLexerConfig { type ByteHandlers = ByteHandlers; + const TOKENS_METHOD_IS_STATIC: bool = true; + #[inline(always)] fn tokens(&self) -> bool { true @@ -174,6 +188,8 @@ impl RuntimeLexerConfig { impl LexerConfig for RuntimeLexerConfig { type ByteHandlers = ByteHandlers; + const TOKENS_METHOD_IS_STATIC: bool = false; + #[inline(always)] fn tokens(&self) -> bool { self.tokens