pub struct ParserRegistry { /* private fields */ }Expand description
A pluggable dispatch table mapping language keys to LanguageParsers.
The registry starts with the BuiltInLanguageParser as a fallback, so an
unmodified registry behaves exactly like LinkNetwork::parse: lino is
handled by the links-notation parser and every other key is routed through
the tree-sitter adapter with a lossless text fallback.
Users register parsers for new language keys or override an existing one. Following TXL’s grammar-override model and SWC’s plugin lesson, a user registration shadows the built-in dispatch for the same key rather than forking the pipeline: any key without an explicit registration still falls through to the built-in set.
Language keys are matched case-insensitively, mirroring the built-in
dispatch (LINO, Lino, and lino resolve to the same parser).
§Examples
use std::sync::Arc;
use meta_language::{
LanguageParser, LinkNetwork, ParseConfiguration, ParserRegistry,
};
#[derive(Debug)]
struct ShoutParser;
impl LanguageParser for ShoutParser {
fn parse_source(
&self,
text: &str,
language: &str,
configuration: ParseConfiguration,
) -> LinkNetwork {
LinkNetwork::parse_lossless_text(&text.to_uppercase(), language, configuration)
}
}
let registry = ParserRegistry::new().with_parser("shout", Arc::new(ShoutParser));
let network = registry.parse("hi", "shout", ParseConfiguration::default());
assert_eq!(network.reconstruct_text(), "HI");Implementations§
Source§impl ParserRegistry
impl ParserRegistry
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a registry backed by the BuiltInLanguageParser fallback and
no user registrations.
Sourcepub fn register(
&mut self,
language: impl Into<String>,
parser: Arc<dyn LanguageParser>,
) -> &mut Self
pub fn register( &mut self, language: impl Into<String>, parser: Arc<dyn LanguageParser>, ) -> &mut Self
Registers parser for language, shadowing any prior registration or
built-in dispatch for the same (case-insensitive) key.
Returns &mut Self so registrations can be chained.
Sourcepub fn with_parser(
self,
language: impl Into<String>,
parser: Arc<dyn LanguageParser>,
) -> Self
pub fn with_parser( self, language: impl Into<String>, parser: Arc<dyn LanguageParser>, ) -> Self
Builder-style variant of register that consumes and
returns the registry.
Sourcepub fn parser_for(&self, language: &str) -> Option<&Arc<dyn LanguageParser>>
pub fn parser_for(&self, language: &str) -> Option<&Arc<dyn LanguageParser>>
Returns the parser explicitly registered for language, if any.
Keys served by the built-in fallback report None; use
parse to dispatch including the fallback.
Sourcepub fn is_registered(&self, language: &str) -> bool
pub fn is_registered(&self, language: &str) -> bool
Whether language has an explicit (non-fallback) registration.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The number of explicit registrations, excluding the built-in fallback.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the registry holds no explicit registrations.
An empty registry still parses every key through the built-in fallback.
Sourcepub fn parse(
&self,
text: &str,
language: &str,
configuration: ParseConfiguration,
) -> LinkNetwork
pub fn parse( &self, text: &str, language: &str, configuration: ParseConfiguration, ) -> LinkNetwork
Parses text for language, dispatching to a registered parser when
one shadows the key and otherwise to the built-in fallback.
Trait Implementations§
Source§impl Clone for ParserRegistry
impl Clone for ParserRegistry
Source§fn clone(&self) -> ParserRegistry
fn clone(&self) -> ParserRegistry
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ParserRegistry
impl Debug for ParserRegistry
Auto Trait Implementations§
impl Freeze for ParserRegistry
impl !RefUnwindSafe for ParserRegistry
impl !Send for ParserRegistry
impl !Sync for ParserRegistry
impl Unpin for ParserRegistry
impl UnsafeUnpin for ParserRegistry
impl !UnwindSafe for ParserRegistry
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more