From cd69e08516712b727a5f49c1f20f4f321aebd679 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 3 Sep 2023 11:05:34 -0400 Subject: [PATCH] Fixing more errors. --- src/context/mod.rs | 4 +- src/parser/latex_environment.rs | 29 +++++----- src/parser/lesser_block.rs | 95 ++++++++++++++++++++------------- src/parser/paragraph.rs | 30 +++++++---- src/parser/plain_link.rs | 18 ++++--- 5 files changed, 106 insertions(+), 70 deletions(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 54e75e7..247b2cf 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -10,9 +10,9 @@ mod parser_with_context; pub type RefContext<'b, 'r, 's> = &'b Context<'r, 's>; pub trait ContextMatcher = for<'b, 'r, 's> Fn(RefContext<'b, 'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>>; -type DynContextMatcher<'c> = dyn ContextMatcher + 'c; +pub type DynContextMatcher<'c> = dyn ContextMatcher + 'c; pub trait Matcher = for<'s> Fn(OrgSource<'s>) -> Res, OrgSource<'s>>; -type DynMatcher<'c> = dyn Matcher + 'c; +pub type DynMatcher<'c> = dyn Matcher + 'c; pub use exiting::ExitClass; pub use global_settings::GlobalSettings; diff --git a/src/parser/latex_environment.rs b/src/parser/latex_environment.rs index 7a9320f..a001e69 100644 --- a/src/parser/latex_environment.rs +++ b/src/parser/latex_environment.rs @@ -13,10 +13,16 @@ use nom::sequence::tuple; use super::org_source::OrgSource; use super::util::get_consumed; +use crate::context::parser_with_context; +use crate::context::ContextElement; +use crate::context::ContextMatcher; +use crate::context::ExitClass; +use crate::context::ExitMatcherNode; +use crate::context::RefContext; use crate::error::Res; use crate::parser::util::exit_matcher_parser; use crate::parser::util::start_of_line; -use crate::parser::LatexEnvironment; +use crate::types::LatexEnvironment; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn latex_environment<'r, 's>( @@ -34,11 +40,11 @@ pub fn latex_environment<'r, 's>( ))(remaining)?; let latex_environment_end_specialized = latex_environment_end(name.into()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Beta, - exit_matcher: &latex_environment_end_specialized, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &latex_environment_end_specialized, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, _contents) = contents(&latex_environment_end_specialized, context, remaining)?; let (remaining, _end) = latex_environment_end_specialized(&parser_context, remaining)?; @@ -62,12 +68,13 @@ fn name<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { tracing::instrument(ret, level = "debug", skip(end_matcher)) )] pub fn contents< + 'b, 'r, 's, - F: Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>>, + F: Fn(RefContext<'b, 'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>>, >( end_matcher: F, - context: RefContext<'_, 'r, 's>, + context: RefContext<'b, 'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { let (remaining, source) = recognize(many_till( @@ -81,11 +88,9 @@ pub fn contents< Ok((remaining, source)) } -fn latex_environment_end( - current_name: &str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { +fn latex_environment_end(current_name: &str) -> impl ContextMatcher { let current_name_lower = current_name.to_lowercase(); - move |context: Context, input: OrgSource<'_>| { + move |context, input: OrgSource<'_>| { _latex_environment_end(context, input, current_name_lower.as_str()) } } diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index 8a78b6a..65d5181 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -12,20 +12,26 @@ use nom::multi::many_till; use nom::sequence::tuple; use super::org_source::OrgSource; +use crate::context::ContextMatcher; +use crate::context::parser_with_context; +use crate::context::ContextElement; +use crate::context::ExitClass; +use crate::context::ExitMatcherNode; +use crate::context::RefContext; use crate::error::Res; -use crate::parser::lesser_element::CommentBlock; -use crate::parser::lesser_element::ExampleBlock; -use crate::parser::lesser_element::ExportBlock; -use crate::parser::lesser_element::SrcBlock; -use crate::parser::lesser_element::VerseBlock; -use crate::parser::object::Object; -use crate::parser::object::PlainText; use crate::parser::object_parser::standard_set_object; use crate::parser::util::blank_line; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::start_of_line; use crate::parser::util::text_until_exit; +use crate::types::CommentBlock; +use crate::types::ExampleBlock; +use crate::types::ExportBlock; +use crate::types::Object; +use crate::types::PlainText; +use crate::types::SrcBlock; +use crate::types::VerseBlock; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn verse_block<'r, 's>( @@ -36,13 +42,18 @@ pub fn verse_block<'r, 's>( let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; let lesser_block_end_specialized = lesser_block_end("verse"); - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("lesser block")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ + ContextElement::ConsumeTrailingWhitespace(true), + ContextElement::Context("lesser block"), + ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: &lesser_block_end_specialized, - })); + }), + ]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), None => None, @@ -87,13 +98,18 @@ pub fn comment_block<'r, 's>( let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; let lesser_block_end_specialized = lesser_block_end("comment"); - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("lesser block")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ + ContextElement::ConsumeTrailingWhitespace(true), + ContextElement::Context("lesser block"), + ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: &lesser_block_end_specialized, - })); + }), + ]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), None => None, @@ -123,13 +139,14 @@ pub fn example_block<'r, 's>( let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; let lesser_block_end_specialized = lesser_block_end("example"); - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("lesser block")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ContextElement::ConsumeTrailingWhitespace(true), ContextElement::Context("lesser block"), ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: &lesser_block_end_specialized, - })); + })]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), None => None, @@ -160,13 +177,14 @@ pub fn export_block<'r, 's>( let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; let lesser_block_end_specialized = lesser_block_end("export"); - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("lesser block")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ContextElement::ConsumeTrailingWhitespace(true), ContextElement::Context("lesser block"), ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: &lesser_block_end_specialized, - })); + })]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), None => None, @@ -197,13 +215,14 @@ pub fn src_block<'r, 's>( let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; let lesser_block_end_specialized = lesser_block_end("src"); - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("lesser block")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ContextElement::ConsumeTrailingWhitespace(true), ContextElement::Context("lesser block"), ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &lesser_block_end_specialized, - })); + })]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), None => None, @@ -236,9 +255,9 @@ fn data<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { fn lesser_block_end( current_name: &str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { +) -> impl ContextMatcher { let current_name_lower = current_name.to_lowercase(); - move |context: Context, input: OrgSource<'_>| { + move |context, input: OrgSource<'_>| { _lesser_block_end(context, input, current_name_lower.as_str()) } } @@ -263,11 +282,11 @@ fn _lesser_block_end<'r, 's, 'x>( /// Parser for the beginning of a lesser block /// /// current_name MUST be lowercase. We do not do the conversion ourselves because it is not allowed in a const fn. -const fn lesser_block_begin( - current_name: &'static str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { +const fn lesser_block_begin<'c>( + current_name: &'c str, +) -> impl ContextMatcher + 'c { // TODO: Since this is a const fn, is there ANY way to "generate" functions at compile time? - move |context: Context, input: OrgSource<'_>| _lesser_block_begin(context, input, current_name) + move |context, input: OrgSource<'_>| _lesser_block_begin(context, input, current_name) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index c275f6d..643fb5c 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -7,26 +7,30 @@ use nom::multi::many_till; use nom::sequence::tuple; use super::element_parser::detect_element; -use super::lesser_element::Paragraph; use super::org_source::OrgSource; use super::util::blank_line; use super::util::get_consumed; -use super::Context; +use crate::context::parser_with_context; +use crate::context::ContextElement; +use crate::context::ExitClass; +use crate::context::ExitMatcherNode; +use crate::context::RefContext; use crate::error::Res; use crate::parser::object_parser::standard_set_object; use crate::parser::util::exit_matcher_parser; use crate::parser::util::start_of_line; +use crate::types::Paragraph; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn paragraph<'r, 's>( context: RefContext<'_, 'r, 's>, input: OrgSource<'s>, ) -> Res, Paragraph<'s>> { - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: ¶graph_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: ¶graph_end, + }); + let parser_context = context.with_additional_node(&parser_context); let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); @@ -63,16 +67,20 @@ fn paragraph_end<'r, 's>( #[cfg(test)] mod tests { + use crate::context::parser_with_context; + use crate::context::Context; + use crate::context::ContextElement; + use crate::context::GlobalSettings; + use crate::context::List; use crate::parser::element_parser::element; use crate::parser::org_source::OrgSource; - use crate::parser::parser_context::ContextTree; - use crate::parser::parser_with_context::parser_with_context; - use crate::parser::source::Source; #[test] fn two_paragraphs() { let input = OrgSource::new("foo bar baz\n\nlorem ipsum"); - let initial_context: ContextTree<'_, '_> = ContextTree::new(); + let global_settings = GlobalSettings::default(); + let initial_context = ContextElement::document_context(); + let initial_context = Context::new(&global_settings, List::new(&initial_context)); let paragraph_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); let (remaining, second_paragraph) = diff --git a/src/parser/plain_link.rs b/src/parser/plain_link.rs index 1cf2b36..2515aff 100644 --- a/src/parser/plain_link.rs +++ b/src/parser/plain_link.rs @@ -11,14 +11,18 @@ use nom::combinator::verify; use nom::multi::many_till; use super::org_source::OrgSource; -use super::Context; +use crate::context::parser_with_context; +use crate::context::ContextElement; +use crate::context::ExitClass; +use crate::context::ExitMatcherNode; +use crate::context::RefContext; use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; -use crate::parser::object::PlainLink; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; +use crate::types::PlainLink; // TODO: Make this a user-provided variable corresponding to elisp's org-link-parameters const ORG_LINK_PARAMETERS: [&'static str; 23] = [ @@ -118,11 +122,11 @@ fn path_plain<'r, 's>( input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { // TODO: "optionally containing parenthesis-wrapped non-whitespace non-bracket substrings up to a depth of two. The string must end with either a non-punctation non-whitespace character, a forwards slash, or a parenthesis-wrapped substring" - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &path_plain_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &path_plain_end, + }); + let parser_context = context.with_additional_node(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);