diff --git a/src/parser/property_drawer.rs b/src/parser/property_drawer.rs index f9b380e..d7a2850 100644 --- a/src/parser/property_drawer.rs +++ b/src/parser/property_drawer.rs @@ -14,16 +14,21 @@ use nom::multi::many_till; use nom::sequence::tuple; use super::org_source::OrgSource; -use super::Context; +use super::util::exit_matcher_parser; +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::greater_element::NodeProperty; -use crate::parser::greater_element::PropertyDrawer; use crate::parser::util::get_consumed; use crate::parser::util::immediate_in_section; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; +use crate::types::NodeProperty; +use crate::types::PropertyDrawer; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn property_drawer<'r, 's>( @@ -46,13 +51,18 @@ pub fn property_drawer<'r, 's>( line_ending, ))(input)?; - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("property-drawer")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ + ContextElement::ConsumeTrailingWhitespace(true), + ContextElement::Context("property-drawer"), + ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &property_drawer_end, - })); + }), + ]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); let node_property_matcher = parser_with_context!(node_property)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); @@ -135,11 +145,11 @@ fn node_property_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::Beta, - exit_matcher: &node_property_name_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &node_property_name_end, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, name) = recognize(tuple(( verify( diff --git a/src/parser/radio_link.rs b/src/parser/radio_link.rs index 5a3a64e..977d8d9 100644 --- a/src/parser/radio_link.rs +++ b/src/parser/radio_link.rs @@ -5,16 +5,22 @@ use nom::character::complete::space0; use nom::combinator::verify; use nom::multi::many_till; +use super::object_parser::minimal_set_object; use super::org_source::OrgSource; +use super::util::exit_matcher_parser; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; -use super::Context; -use super::Object; +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::util::get_consumed; -use crate::parser::RadioLink; -use crate::parser::RadioTarget; +use crate::types::Object; +use crate::types::RadioLink; +use crate::types::RadioTarget; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn radio_link<'r, 's>( @@ -23,7 +29,7 @@ pub fn radio_link<'r, 's>( ) -> Res, RadioLink<'s>> { let radio_targets = context .iter() - .filter_map(|context_element| match context_element.get_data() { + .filter_map(|context_element| match context_element { ContextElement::RadioTarget(targets) => Some(targets), _ => None, }) @@ -84,11 +90,11 @@ pub fn radio_target<'r, 's>( input: OrgSource<'s>, ) -> Res, RadioTarget<'s>> { let (remaining, _opening) = tag("<<<")(input)?; - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Gamma, - exit_matcher: &radio_target_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Gamma, + exit_matcher: &radio_target_end, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till( @@ -130,25 +136,28 @@ pub trait RematchObject<'x> { #[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::ContextElement; - use crate::parser::parser_context::ContextTree; - use crate::parser::parser_with_context::parser_with_context; - use crate::parser::source::Source; - use crate::parser::Bold; - use crate::parser::PlainText; + use crate::types::Bold; + use crate::types::Element; + use crate::types::PlainText; + use crate::types::Source; #[test] fn plain_text_radio_target() { let input = OrgSource::new("foo bar baz"); let radio_target_match = vec![Object::PlainText(PlainText { source: "bar" })]; - let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = initial_context - .with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); + let global_settings = GlobalSettings::default(); + let initial_context = ContextElement::document_context(); + let initial_context = Context::new(&global_settings, List::new(&initial_context)); + let document_context = ContextElement::RadioTarget(vec![&radio_target_match]); + let document_context = initial_context.with_additional_node(&document_context); let paragraph_matcher = parser_with_context!(element(true))(&document_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), ""); @@ -173,14 +182,16 @@ mod tests { source: "*bar*", children: vec![Object::PlainText(PlainText { source: "bar" })], })]; - let initial_context: ContextTree<'_, '_> = ContextTree::new(); - let document_context = initial_context - .with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match])); + let global_settings = GlobalSettings::default(); + let initial_context = ContextElement::document_context(); + let initial_context = Context::new(&global_settings, List::new(&initial_context)); + let document_context = ContextElement::RadioTarget(vec![&radio_target_match]); + let document_context = initial_context.with_additional_node(&document_context); let paragraph_matcher = parser_with_context!(element(true))(&document_context); let (remaining, first_paragraph) = paragraph_matcher(input.into()).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/regular_link.rs b/src/parser/regular_link.rs index cf751bb..ce83bea 100644 --- a/src/parser/regular_link.rs +++ b/src/parser/regular_link.rs @@ -6,13 +6,19 @@ use nom::character::complete::one_of; use nom::combinator::verify; use nom::multi::many_till; +use super::object_parser::regular_link_description_object_set; use super::org_source::OrgSource; +use super::util::exit_matcher_parser; use super::util::get_consumed; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; -use super::Context; -use super::Object; -use super::RegularLink; +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::types::Object; +use crate::types::RegularLink; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn regular_link<'r, 's>( @@ -86,11 +92,11 @@ pub fn description<'r, 's>( context: RefContext<'_, 'r, 's>, input: OrgSource<'s>, ) -> Res, Vec>> { - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Beta, - exit_matcher: &description_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &description_end, + }); + let parser_context = context.with_additional_node(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till( parser_with_context!(regular_link_description_object_set)(&parser_context), diff --git a/src/parser/subscript_and_superscript.rs b/src/parser/subscript_and_superscript.rs index 8463a40..3fb3ee3 100644 --- a/src/parser/subscript_and_superscript.rs +++ b/src/parser/subscript_and_superscript.rs @@ -10,17 +10,23 @@ use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many_till; +use super::object_parser::standard_set_object; use super::org_source::BracketDepth; use super::org_source::OrgSource; +use super::util::exit_matcher_parser; use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting; -use super::Context; -use super::Object; +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::util::get_consumed; -use crate::parser::Subscript; -use crate::parser::Superscript; +use crate::types::Object; +use crate::types::Subscript; +use crate::types::Superscript; #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] pub fn subscript<'r, 's>( @@ -151,11 +157,11 @@ fn script_with_braces<'r, 's>( ) -> Res, Vec>> { let (remaining, _) = tag("{")(input)?; let exit_with_depth = script_with_braces_end(remaining.get_brace_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)) = many_till( parser_with_context!(standard_set_object)(&parser_context), @@ -168,8 +174,9 @@ fn script_with_braces<'r, 's>( fn script_with_braces_end( starting_brace_depth: BracketDepth, -) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { - move |context: Context, input: OrgSource<'_>| { +) -> impl for<'b, 'r, 's> Fn(RefContext<'b, 'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> +{ + move |context, input: OrgSource<'_>| { _script_with_braces_end(context, input, starting_brace_depth) } } diff --git a/src/parser/table.rs b/src/parser/table.rs index eeb3c1a..a471890 100644 --- a/src/parser/table.rs +++ b/src/parser/table.rs @@ -12,14 +12,20 @@ use nom::multi::many1; use nom::multi::many_till; use nom::sequence::tuple; +use super::object_parser::table_cell_set_object; use super::org_source::OrgSource; -use super::Context; +use super::util::exit_matcher_parser; +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::greater_element::TableRow; -use crate::parser::lesser_element::TableCell; use crate::parser::util::get_consumed; use crate::parser::util::start_of_line; -use crate::parser::Table; +use crate::types::Table; +use crate::types::TableCell; +use crate::types::TableRow; /// Parse an org-mode-style table /// @@ -32,13 +38,18 @@ pub fn org_mode_table<'r, 's>( start_of_line(input)?; peek(tuple((space0, tag("|"))))(input)?; - let parser_context = context - .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) - .with_additional_node(ContextElement::Context("table")) - .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + let contexts = [ + ContextElement::ConsumeTrailingWhitespace(true), + ContextElement::Context("table"), + ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, exit_matcher: &table_end, - })); + }), + ]; + let parser_context = context + .with_additional_node(&contexts[0]) + .with_additional_node(&contexts[1]) + .with_additional_node(&contexts[2]); let org_mode_table_row_matcher = parser_with_context!(org_mode_table_row)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); @@ -120,11 +131,11 @@ pub fn org_mode_table_cell<'r, 's>( context: RefContext<'_, 'r, 's>, input: OrgSource<'s>, ) -> Res, TableCell<'s>> { - let parser_context = - context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { - class: ExitClass::Beta, - exit_matcher: &org_mode_table_cell_end, - })); + let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &org_mode_table_cell_end, + }); + let parser_context = context.with_additional_node(&parser_context); let table_cell_set_object_matcher = parser_with_context!(table_cell_set_object)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); diff --git a/src/parser/util.rs b/src/parser/util.rs index 41539ca..f5cab9b 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -28,7 +28,7 @@ pub const WORD_CONSTITUENT_CHARACTERS: &str = #[allow(dead_code)] pub fn in_section<'r, 's, 'x>(context: RefContext<'_, 'r, 's>, section_name: &'x str) -> bool { for thing in context.iter() { - match thing.get_data() { + match thing { ContextElement::Context(name) if *name == section_name => return true, _ => {} } @@ -42,7 +42,7 @@ pub fn immediate_in_section<'r, 's, 'x>( section_name: &'x str, ) -> bool { for thing in context.iter() { - match thing.get_data() { + match thing { ContextElement::Context(name) if *name == section_name => return true, ContextElement::Context(name) if *name != section_name => return false, _ => {} diff --git a/src/types/mod.rs b/src/types/mod.rs index 9d2f200..8011bb2 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -17,6 +17,7 @@ pub use greater_element::PlainList; pub use greater_element::PlainListItem; pub use greater_element::PropertyDrawer; pub use greater_element::Table; +pub use greater_element::NodeProperty; pub use greater_element::TableRow; pub use lesser_element::Clock; pub use lesser_element::Comment;