diff --git a/src/parser/angle_link.rs b/src/parser/angle_link.rs index da25d49..88e9789 100644 --- a/src/parser/angle_link.rs +++ b/src/parser/angle_link.rs @@ -6,11 +6,16 @@ use nom::multi::many_till; use super::org_source::OrgSource; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; +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::plain_link::protocol; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; -use crate::parser::AngleLink; +use crate::types::AngleLink; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn angle_link<'r, 's>( @@ -40,11 +45,11 @@ fn path_angle<'r, 's>( context: RefContext<'_, 'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &path_angle_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &path_angle_end, + }); + let parser_context = context.with_additional_node(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); diff --git a/src/parser/citation.rs b/src/parser/citation.rs index babbae8..1b9f65c 100644 --- a/src/parser/citation.rs +++ b/src/parser/citation.rs @@ -14,15 +14,21 @@ use super::citation_reference::must_balance_bracket; use super::org_source::BracketDepth; use super::org_source::OrgSource; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; +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::CustomError; use crate::error::Res; use crate::parser::citation_reference::citation_reference; use crate::parser::citation_reference::citation_reference_key; -use crate::parser::object::Citation; use crate::parser::object_parser::standard_set_object; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; -use crate::parser::Object; +use crate::types::Citation; +use crate::types::Object; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn citation<'r, 's>( @@ -82,11 +88,11 @@ fn global_prefix<'r, 's>( input: OrgSource<'s>, ) -> Res, Vec>> { let exit_with_depth = global_prefix_end(input.get_bracket_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till( parser_with_context!(standard_set_object)(&parser_context), @@ -98,12 +104,8 @@ fn global_prefix<'r, 's>( Ok((remaining, children)) } -fn global_prefix_end( - starting_bracket_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { - _global_prefix_end(context, input, starting_bracket_depth) - } +fn global_prefix_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| _global_prefix_end(context, input, starting_bracket_depth) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -135,11 +137,11 @@ fn global_suffix<'r, 's>( input: OrgSource<'s>, ) -> Res, Vec>> { let exit_with_depth = global_suffix_end(input.get_bracket_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till( parser_with_context!(standard_set_object)(&parser_context), @@ -150,12 +152,8 @@ fn global_suffix<'r, 's>( Ok((remaining, children)) } -fn global_suffix_end( - starting_bracket_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { - _global_suffix_end(context, input, starting_bracket_depth) - } +fn global_suffix_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| _global_suffix_end(context, input, starting_bracket_depth) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -184,19 +182,23 @@ fn _global_suffix_end<'r, 's>( #[cfg(test)] mod tests { use super::*; + use crate::context::Context; + use crate::context::GlobalSettings; + use crate::context::List; use crate::parser::element_parser::element; - use crate::parser::parser_context::ContextTree; - use crate::parser::parser_with_context::parser_with_context; - use crate::parser::source::Source; + use crate::types::Element; + use crate::types::Source; #[test] fn citation_simple() { let input = OrgSource::new("[cite:@foo]"); - 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 first_paragraph = match first_paragraph { - crate::parser::Element::Paragraph(paragraph) => paragraph, + Element::Paragraph(paragraph) => paragraph, _ => panic!("Should be a paragraph!"), }; assert_eq!(Into::<&str>::into(remaining), ""); diff --git a/src/parser/citation_reference.rs b/src/parser/citation_reference.rs index 8b3c5b1..3a55adf 100644 --- a/src/parser/citation_reference.rs +++ b/src/parser/citation_reference.rs @@ -12,15 +12,21 @@ use nom::sequence::tuple; use super::org_source::BracketDepth; use super::org_source::OrgSource; +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::CustomError; use crate::error::MyError; use crate::error::Res; -use crate::parser::object::CitationReference; use crate::parser::object_parser::minimal_set_object; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; -use crate::parser::Object; +use crate::types::CitationReference; +use crate::types::Object; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn citation_reference<'r, 's>( @@ -68,11 +74,11 @@ fn key_prefix<'r, 's>( input: OrgSource<'s>, ) -> Res, Vec>> { let exit_with_depth = key_prefix_end(input.get_bracket_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till( parser_with_context!(minimal_set_object)(&parser_context), @@ -89,11 +95,11 @@ fn key_suffix<'r, 's>( input: OrgSource<'s>, ) -> Res, Vec>> { let exit_with_depth = key_suffix_end(input.get_bracket_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till( parser_with_context!(minimal_set_object)(&parser_context), @@ -104,12 +110,8 @@ fn key_suffix<'r, 's>( Ok((remaining, children)) } -fn key_prefix_end( - starting_bracket_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { - _key_prefix_end(context, input, starting_bracket_depth) - } +fn key_prefix_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| _key_prefix_end(context, input, starting_bracket_depth) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -135,12 +137,8 @@ fn _key_prefix_end<'r, 's>( ))(input) } -fn key_suffix_end( - starting_bracket_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { - _key_suffix_end(context, input, starting_bracket_depth) - } +fn key_suffix_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| _key_suffix_end(context, input, starting_bracket_depth) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] diff --git a/src/parser/comment.rs b/src/parser/comment.rs index 931ab97..545c6ec 100644 --- a/src/parser/comment.rs +++ b/src/parser/comment.rs @@ -13,13 +13,15 @@ use nom::sequence::tuple; use super::org_source::OrgSource; use super::util::get_consumed; +use crate::context::parser_with_context; +use crate::context::RefContext; use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; use crate::parser::util::exit_matcher_parser; use crate::parser::util::immediate_in_section; use crate::parser::util::start_of_line; -use crate::parser::Comment; +use crate::types::Comment; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn comment<'r, 's>( @@ -66,8 +68,10 @@ fn comment_line<'r, 's>( #[cfg(test)] mod tests { use super::*; - use crate::parser::parser_context::ContextTree; - use crate::parser::parser_with_context::parser_with_context; + use crate::context::Context; + use crate::context::ContextElement; + use crate::context::GlobalSettings; + use crate::context::List; #[test] fn require_space_after_hash() { @@ -76,7 +80,9 @@ mod tests { #not a comment # Comment again", ); - 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 comment_matcher = parser_with_context!(comment)(&initial_context); let (remaining, first_comment) = comment_matcher(input).expect("Parse first comment"); assert_eq!( diff --git a/src/parser/drawer.rs b/src/parser/drawer.rs index f794a3f..83246ac 100644 --- a/src/parser/drawer.rs +++ b/src/parser/drawer.rs @@ -11,6 +11,11 @@ use nom::multi::many_till; use nom::sequence::tuple; use super::org_source::OrgSource; +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; @@ -21,9 +26,10 @@ use crate::parser::util::get_consumed; use crate::parser::util::immediate_in_section; use crate::parser::util::start_of_line; use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; -use crate::parser::Drawer; -use crate::parser::Element; -use crate::parser::Paragraph; +use crate::types::Drawer; +use crate::types::Element; +use crate::types::Paragraph; +use crate::types::SetSource; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn drawer<'r, 's>( @@ -44,13 +50,18 @@ pub fn drawer<'r, 's>( recognize(tuple((space0, line_ending))), ))(remaining)?; - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("drawer")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ + ContextElement::ConsumeTrailingWhitespace(true), + ContextElement::Context("drawer"), + ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &drawer_end, - })); + }), + ]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); let element_matcher = parser_with_context!(element(true))(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); diff --git a/src/parser/dynamic_block.rs b/src/parser/dynamic_block.rs index 7625fde..46a5387 100644 --- a/src/parser/dynamic_block.rs +++ b/src/parser/dynamic_block.rs @@ -12,18 +12,24 @@ use nom::multi::many_till; use nom::sequence::tuple; use super::org_source::OrgSource; +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::element_parser::element; -use crate::parser::greater_element::DynamicBlock; -use crate::parser::lesser_element::Paragraph; use crate::parser::util::blank_line; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::immediate_in_section; use crate::parser::util::start_of_line; -use crate::parser::Element; +use crate::types::DynamicBlock; +use crate::types::Element; +use crate::types::Paragraph; +use crate::types::SetSource; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn dynamic_block<'r, 's>( @@ -44,13 +50,18 @@ pub fn dynamic_block<'r, 's>( opt(tuple((space1, parameters))), line_ending, ))(remaining)?; - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("dynamic block")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ + ContextElement::ConsumeTrailingWhitespace(true), + ContextElement::Context("dynamic block"), + ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &dynamic_block_end, - })); + }), + ]; + 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, diff --git a/src/parser/export_snippet.rs b/src/parser/export_snippet.rs index a294ab7..23d1251 100644 --- a/src/parser/export_snippet.rs +++ b/src/parser/export_snippet.rs @@ -9,10 +9,15 @@ use nom::sequence::tuple; use super::org_source::OrgSource; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; +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::util::exit_matcher_parser; use crate::parser::util::get_consumed; -use crate::parser::ExportSnippet; +use crate::types::ExportSnippet; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn export_snippet<'r, 's>( @@ -21,11 +26,11 @@ pub fn export_snippet<'r, 's>( ) -> Res, ExportSnippet<'s>> { let (remaining, _) = tag("@@")(input)?; let (remaining, backend_name) = backend(context, remaining)?; - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &export_snippet_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &export_snippet_end, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, backend_contents) = opt(tuple(( tag(":"), parser_with_context!(contents)(&parser_context), diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index 0bd1089..ec76833 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -12,17 +12,22 @@ use nom::sequence::tuple; use super::org_source::OrgSource; use super::util::WORD_CONSTITUENT_CHARACTERS; +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::element_parser::element; -use crate::parser::greater_element::FootnoteDefinition; use crate::parser::util::blank_line; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::immediate_in_section; use crate::parser::util::maybe_consume_trailing_whitespace; use crate::parser::util::start_of_line; +use crate::types::FootnoteDefinition; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn footnote_definition<'r, 's>( @@ -38,13 +43,18 @@ pub fn footnote_definition<'r, 's>( // Cannot be indented. let (remaining, (_lead_in, lbl, _lead_out, _ws)) = tuple((tag_no_case("[fn:"), label, tag("]"), space0))(input)?; - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("footnote definition")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ + ContextElement::ConsumeTrailingWhitespace(true), + ContextElement::Context("footnote definition"), + ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &footnote_definition_end, - })); + }), + ]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); // TODO: The problem is we are not accounting for trailing whitespace like we do in section. Maybe it would be easier if we passed down whether or not to parse trailing whitespace into the element matcher similar to how tag takes in parameters. let element_matcher = parser_with_context!(element(true))(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); @@ -100,9 +110,9 @@ fn detect_footnote_definition<'s>(input: OrgSource<'s>) -> Res, () #[cfg(test)] mod tests { use super::*; - use crate::parser::parser_context::ContextTree; - use crate::parser::parser_with_context::parser_with_context; - use crate::parser::Source; + use crate::context::Context; + use crate::context::GlobalSettings; + use crate::context::List; #[test] fn two_paragraphs() { @@ -113,7 +123,9 @@ mod tests { line footnote.", ); - 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 footnote_definition_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_footnote_definition) = footnote_definition_matcher(input).expect("Parse first footnote_definition"); @@ -144,7 +156,9 @@ line footnote. not in the footnote.", ); - 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 footnote_definition_matcher = parser_with_context!(element(true))(&initial_context); let (remaining, first_footnote_definition) = footnote_definition_matcher(input).expect("Parse first footnote_definition"); diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index 20462c3..bea3d94 100644 --- a/src/parser/footnote_reference.rs +++ b/src/parser/footnote_reference.rs @@ -7,6 +7,12 @@ use nom::multi::many_till; use super::org_source::BracketDepth; use super::org_source::OrgSource; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; +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::CustomError; use crate::error::MyError; use crate::error::Res; @@ -14,7 +20,7 @@ use crate::parser::footnote_definition::label; use crate::parser::object_parser::standard_set_object; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; -use crate::parser::FootnoteReference; +use crate::types::FootnoteReference; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn footnote_reference<'r, 's>( @@ -35,11 +41,11 @@ fn anonymous_footnote<'r, 's>( ) -> Res, FootnoteReference<'s>> { let (remaining, _) = tag_no_case("[fn::")(input)?; let exit_with_depth = footnote_definition_end(remaining.get_bracket_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Beta, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till( parser_with_context!(standard_set_object)(&parser_context), @@ -71,11 +77,11 @@ fn inline_footnote<'r, 's>( let (remaining, label_contents) = label(remaining)?; let (remaining, _) = tag(":")(remaining)?; let exit_with_depth = footnote_definition_end(remaining.get_bracket_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Beta, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till( parser_with_context!(standard_set_object)(&parser_context), @@ -119,10 +125,8 @@ fn footnote_reference_only<'r, 's>( )) } -fn footnote_definition_end( - starting_bracket_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { +fn footnote_definition_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| { _footnote_definition_end(context, input, starting_bracket_depth) } } diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index 22a5018..eda0d65 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -13,17 +13,24 @@ use nom::sequence::tuple; use super::org_source::OrgSource; use super::util::in_section; +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::CustomError; use crate::error::MyError; use crate::error::Res; use crate::parser::element_parser::element; -use crate::parser::greater_element::GreaterBlock; 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::Element; -use crate::parser::Paragraph; +use crate::types::Element; +use crate::types::GreaterBlock; +use crate::types::Paragraph; +use crate::types::SetSource; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn greater_block<'r, 's>( @@ -55,13 +62,18 @@ pub fn greater_block<'r, 's>( let exit_with_name = greater_block_end(name.into()); let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context(context_name.as_str())) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ + ContextElement::ConsumeTrailingWhitespace(true), + ContextElement::Context(context_name.as_str()), + ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &exit_with_name, - })); + }), + ]; + 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, @@ -114,19 +126,16 @@ fn parameters<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not("\r\n")(input) } -fn greater_block_end<'x>( - name: &'x str, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { +fn greater_block_end<'c>(name: &'c str) -> impl ContextMatcher + 'c { // TODO: Can this be done without making an owned copy? - let name = name.to_owned(); - move |context: Context, input: OrgSource<'_>| _greater_block_end(context, input, name.as_str()) + move |context, input: OrgSource<'_>| _greater_block_end(context, input, name) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn _greater_block_end<'r, 's, 'x>( +fn _greater_block_end<'r, 's, 'c>( _context: RefContext<'_, 'r, 's>, input: OrgSource<'s>, - name: &'x str, + name: &'c str, ) -> Res, OrgSource<'s>> { start_of_line(input)?; let (remaining, _leading_whitespace) = space0(input)?; diff --git a/src/parser/inline_babel_call.rs b/src/parser/inline_babel_call.rs index e059f50..74bf9ca 100644 --- a/src/parser/inline_babel_call.rs +++ b/src/parser/inline_babel_call.rs @@ -12,12 +12,18 @@ use nom::multi::many_till; use super::org_source::BracketDepth; use super::org_source::OrgSource; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; +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::CustomError; use crate::error::MyError; use crate::error::Res; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; -use crate::parser::InlineBabelCall; +use crate::types::InlineBabelCall; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn inline_babel_call<'r, 's>( @@ -45,11 +51,11 @@ fn name<'r, 's>( context: RefContext<'_, 'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &name_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &name_end, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, name) = recognize(many_till( verify(anychar, |c| !(c.is_whitespace() || "[]()".contains(*c))), parser_with_context!(exit_matcher_parser)(&parser_context), @@ -73,11 +79,11 @@ fn header<'r, 's>( let (remaining, _) = tag("[")(input)?; let exit_with_depth = header_end(remaining.get_bracket_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, name) = recognize(many_till( anychar, @@ -87,12 +93,8 @@ fn header<'r, 's>( Ok((remaining, name)) } -fn header_end( - starting_bracket_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { - _header_end(context, input, starting_bracket_depth) - } +fn header_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| _header_end(context, input, starting_bracket_depth) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -123,11 +125,11 @@ fn argument<'r, 's>( let (remaining, _) = tag("(")(input)?; let exit_with_depth = argument_end(remaining.get_parenthesis_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, name) = recognize(many_till( anychar, @@ -137,12 +139,8 @@ fn argument<'r, 's>( Ok((remaining, name)) } -fn argument_end( - starting_parenthesis_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { - _argument_end(context, input, starting_parenthesis_depth) - } +fn argument_end(starting_parenthesis_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| _argument_end(context, input, starting_parenthesis_depth) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] diff --git a/src/parser/inline_source_block.rs b/src/parser/inline_source_block.rs index 132163f..cd1cad9 100644 --- a/src/parser/inline_source_block.rs +++ b/src/parser/inline_source_block.rs @@ -14,12 +14,18 @@ use tracing::span; use super::org_source::BracketDepth; use super::org_source::OrgSource; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; +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::CustomError; use crate::error::MyError; use crate::error::Res; use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; -use crate::parser::InlineSourceBlock; +use crate::types::InlineSourceBlock; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn inline_source_block<'r, 's>( @@ -46,11 +52,11 @@ fn lang<'r, 's>( context: RefContext<'_, 'r, 's>, input: OrgSource<'s>, ) -> Res, OrgSource<'s>> { - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Beta, - exit_matcher: &lang_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &lang_end, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, lang) = recognize(many_till( verify(anychar, |c| !(c.is_whitespace() || "[{".contains(*c))), parser_with_context!(exit_matcher_parser)(&parser_context), @@ -74,11 +80,11 @@ fn header<'r, 's>( let (remaining, _) = tag("[")(input)?; let exit_with_depth = header_end(remaining.get_bracket_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Beta, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, header_contents) = recognize(many_till( anychar, @@ -88,12 +94,8 @@ fn header<'r, 's>( Ok((remaining, header_contents)) } -fn header_end( - starting_bracket_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { - _header_end(context, input, starting_bracket_depth) - } +fn header_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| _header_end(context, input, starting_bracket_depth) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] @@ -124,11 +126,11 @@ fn body<'r, 's>( let (remaining, _) = tag("{")(input)?; let exit_with_depth = body_end(remaining.get_brace_depth()); - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Beta, - exit_matcher: &exit_with_depth, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &exit_with_depth, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, body_contents) = recognize(many_till( anychar, @@ -148,10 +150,8 @@ fn body<'r, 's>( Ok((remaining, body_contents)) } -fn body_end( - starting_brace_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| _body_end(context, input, starting_brace_depth) +fn body_end(starting_brace_depth: BracketDepth) -> impl ContextMatcher { + move |context, input: OrgSource<'_>| _body_end(context, input, starting_brace_depth) } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] diff --git a/src/parser/latex_environment.rs b/src/parser/latex_environment.rs index a001e69..78299b0 100644 --- a/src/parser/latex_environment.rs +++ b/src/parser/latex_environment.rs @@ -46,7 +46,7 @@ pub fn latex_environment<'r, 's>( }); let parser_context = context.with_additional_node(&parser_context); - let (remaining, _contents) = contents(&latex_environment_end_specialized, context, remaining)?; + let (remaining, _contents) = contents(&latex_environment_end_specialized)(context, remaining)?; let (remaining, _end) = latex_environment_end_specialized(&parser_context, remaining)?; let source = get_consumed(input, remaining); @@ -63,16 +63,15 @@ fn name<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { take_while1(|c: char| c.is_alphanumeric() || c == '*')(input) } +fn contents(end_matcher: F) -> impl ContextMatcher { + move |context, input| _contents(&end_matcher, context, input) +} + #[cfg_attr( feature = "tracing", tracing::instrument(ret, level = "debug", skip(end_matcher)) )] -pub fn contents< - 'b, - 'r, - 's, - F: Fn(RefContext<'b, 'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>>, ->( +fn _contents<'b, 'r, 's, F: ContextMatcher>( end_matcher: F, context: RefContext<'b, 'r, 's>, input: OrgSource<'s>, @@ -81,7 +80,7 @@ pub fn contents< anychar, peek(alt(( parser_with_context!(exit_matcher_parser)(context), - parser_with_context!(end_matcher)(context), + parser_with_context!(&end_matcher)(context), ))), ))(input)?; diff --git a/src/parser/object_parser.rs b/src/parser/object_parser.rs index 084d526..1ae5189 100644 --- a/src/parser/object_parser.rs +++ b/src/parser/object_parser.rs @@ -4,7 +4,8 @@ use nom::combinator::map; use super::org_source::OrgSource; use super::plain_text::plain_text; use super::regular_link::regular_link; -use super::Context; +use crate::context::parser_with_context; +use crate::context::RefContext; use crate::error::Res; use crate::parser::angle_link::angle_link; use crate::parser::citation::citation; @@ -15,7 +16,6 @@ use crate::parser::inline_babel_call::inline_babel_call; use crate::parser::inline_source_block::inline_source_block; use crate::parser::latex_fragment::latex_fragment; use crate::parser::line_break::line_break; -use crate::parser::object::Object; use crate::parser::org_macro::org_macro; use crate::parser::plain_link::plain_link; use crate::parser::radio_link::radio_link; @@ -26,6 +26,7 @@ use crate::parser::subscript_and_superscript::superscript; use crate::parser::target::target; use crate::parser::text_markup::text_markup; use crate::parser::timestamp::timestamp; +use crate::types::Object; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn standard_set_object<'r, 's>(