Skip to main content

meta_language/
language_parser.rs

1use crate::{tree_sitter_adapter, LinkNetwork, ParseConfiguration};
2
3/// Parser boundary that produces lossless links networks for source text.
4pub trait LanguageParser {
5    /// Parses `text` using the requested language label.
6    fn parse_source(
7        &self,
8        text: &str,
9        language: &str,
10        configuration: ParseConfiguration,
11    ) -> LinkNetwork;
12}
13
14/// Built-in parser registry used by [`LinkNetwork::parse`].
15#[derive(Clone, Copy, Debug, Default)]
16pub struct BuiltInLanguageParser;
17
18impl LanguageParser for BuiltInLanguageParser {
19    fn parse_source(
20        &self,
21        text: &str,
22        language: &str,
23        configuration: ParseConfiguration,
24    ) -> LinkNetwork {
25        tree_sitter_adapter::parse(text, language, configuration)
26            .unwrap_or_else(|| LinkNetwork::parse_lossless_text(text, language, configuration))
27    }
28}