Skip to main content

meta_language/
language_parser.rs

1use crate::{
2    data_format_parser, docx_parser, lino_parser, pdf_parser, tree_sitter_adapter, LinkNetwork,
3    ParseConfiguration,
4};
5
6/// Parser boundary that produces lossless links networks for source text.
7pub trait LanguageParser {
8    /// Parses `text` using the requested language label.
9    fn parse_source(
10        &self,
11        text: &str,
12        language: &str,
13        configuration: ParseConfiguration,
14    ) -> LinkNetwork;
15}
16
17/// Built-in parser registry used by [`LinkNetwork::parse`].
18#[derive(Clone, Copy, Debug, Default)]
19pub struct BuiltInLanguageParser;
20
21impl LanguageParser for BuiltInLanguageParser {
22    fn parse_source(
23        &self,
24        text: &str,
25        language: &str,
26        configuration: ParseConfiguration,
27    ) -> LinkNetwork {
28        if language.eq_ignore_ascii_case("lino") {
29            return lino_parser::parse(text, language, configuration);
30        }
31
32        if language.eq_ignore_ascii_case("pdf") {
33            return pdf_parser::parse(text, language, configuration);
34        }
35
36        if language.eq_ignore_ascii_case("docx") {
37            return docx_parser::parse(text, language, configuration);
38        }
39
40        if let Some(network) = data_format_parser::parse(text, language, configuration) {
41            return network;
42        }
43
44        tree_sitter_adapter::parse(text, language, configuration)
45            .unwrap_or_else(|| LinkNetwork::parse_lossless_text(text, language, configuration))
46    }
47}