use crate::{
LinkNetwork, ParseConfiguration, docx_parser, language_catalog, lino_parser, pdf_parser,
structured_text_parser, tree_sitter_adapter,
};
pub trait LanguageParser {
fn parse_source(
&self,
text: &str,
language: &str,
configuration: ParseConfiguration,
) -> LinkNetwork;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct BuiltInLanguageParser;
impl LanguageParser for BuiltInLanguageParser {
fn parse_source(
&self,
text: &str,
language: &str,
configuration: ParseConfiguration,
) -> LinkNetwork {
if let Some(network) = parse_builtin_grammar(text, language, configuration) {
return network;
}
if language.eq_ignore_ascii_case("lean") || language.eq_ignore_ascii_case("lean4") {
return tree_sitter_adapter::parse(text, language, configuration)
.expect("the built-in Lean tree-sitter grammar must initialize");
}
if let Some(network) = tree_sitter_adapter::parse(text, language, configuration) {
return network;
}
LinkNetwork::parse_lossless_text(text, language, configuration)
}
}
fn parse_builtin_grammar(
text: &str,
language: &str,
configuration: ParseConfiguration,
) -> Option<LinkNetwork> {
let entry = language_catalog::language_entry(language)?;
match entry.name.as_str() {
"LiNo" => Some(lino_parser::parse(text, language, configuration)),
"txt" => Some(structured_text_parser::parse_plain(
text,
language,
configuration,
)),
"PDF" => Some(pdf_parser::parse(text, language, configuration)),
"DOCX" => Some(docx_parser::parse(text, language, configuration)),
_ if entry.family == "natural" => Some(structured_text_parser::parse_natural(
text,
language,
configuration,
)),
_ => None,
}
}