From d780981baf4b01af6ffed7702dfe0b2952b70573 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 21 Apr 2023 16:10:56 -0400 Subject: [PATCH] Separate out the element parsers. --- src/parser/document.rs | 2 +- src/parser/drawer.rs | 2 +- src/parser/dynamic_block.rs | 2 +- src/parser/element.rs | 47 ------------------------------- src/parser/element_parser.rs | 47 +++++++++++++++++++++++++++++++ src/parser/footnote_definition.rs | 2 +- src/parser/greater_block.rs | 2 +- src/parser/mod.rs | 1 + src/parser/paragraph.rs | 2 +- src/parser/plain_list.rs | 2 +- 10 files changed, 55 insertions(+), 54 deletions(-) create mode 100644 src/parser/element_parser.rs diff --git a/src/parser/document.rs b/src/parser/document.rs index dc3591e..7887a9c 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -1,5 +1,5 @@ use crate::parser::comment::comment; -use crate::parser::element::element; +use crate::parser::element_parser::element; use crate::parser::exiting::ExitClass; use crate::parser::object_parser::standard_set_object; use crate::parser::parser_context::ContextElement; diff --git a/src/parser/drawer.rs b/src/parser/drawer.rs index e79cdeb..8cac28d 100644 --- a/src/parser/drawer.rs +++ b/src/parser/drawer.rs @@ -11,7 +11,7 @@ use nom::multi::many_till; use nom::sequence::tuple; use super::Context; -use crate::parser::element::element; +use crate::parser::element_parser::element; use crate::parser::error::CustomError; use crate::parser::error::MyError; use crate::parser::error::Res; diff --git a/src/parser/dynamic_block.rs b/src/parser/dynamic_block.rs index 2e3412a..728f49f 100644 --- a/src/parser/dynamic_block.rs +++ b/src/parser/dynamic_block.rs @@ -1,6 +1,6 @@ use super::error::Res; use super::Context; -use crate::parser::element::element; +use crate::parser::element_parser::element; use crate::parser::error::CustomError; use crate::parser::error::MyError; use crate::parser::exiting::ExitClass; diff --git a/src/parser/element.rs b/src/parser/element.rs index abfaf4b..d4e773a 100644 --- a/src/parser/element.rs +++ b/src/parser/element.rs @@ -1,9 +1,3 @@ -use super::comment::comment; -use super::drawer::drawer; -use super::dynamic_block::dynamic_block; -use super::error::Res; -use super::footnote_definition::footnote_definition; -use super::greater_block::greater_block; use super::greater_element::DynamicBlock; use super::greater_element::FootnoteDefinition; use super::greater_element::GreaterBlock; @@ -14,17 +8,9 @@ use super::greater_element::TableRow; use super::lesser_element::Comment; use super::lesser_element::Paragraph; use super::lesser_element::TableCell; -use super::paragraph::paragraph; -use super::plain_list::plain_list; use super::source::Source; -use super::Context; use super::Drawer; use super::PlainListItem; -use crate::parser::parser_with_context::parser_with_context; -use crate::parser::table; -use crate::parser::table::org_mode_table; -use nom::branch::alt; -use nom::combinator::map; #[derive(Debug)] pub enum Element<'s> { @@ -126,36 +112,3 @@ impl<'s> Source<'s> for TableCell<'s> { self.source } } - -#[tracing::instrument(ret, level = "debug")] -pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> { - let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context); - let paragraph_matcher = parser_with_context!(paragraph)(context); - - alt(( - non_paragraph_matcher, - map(paragraph_matcher, Element::Paragraph), - ))(input) -} - -pub fn non_paragraph_element<'r, 's>( - context: Context<'r, 's>, - input: &'s str, -) -> Res<&'s str, Element<'s>> { - let plain_list_matcher = parser_with_context!(plain_list)(context); - let greater_block_matcher = parser_with_context!(greater_block)(context); - let dynamic_block_matcher = parser_with_context!(dynamic_block)(context); - let footnote_definition_matcher = parser_with_context!(footnote_definition)(context); - let comment_matcher = parser_with_context!(comment)(context); - let drawer_matcher = parser_with_context!(drawer)(context); - let table_matcher = parser_with_context!(org_mode_table)(context); - alt(( - map(plain_list_matcher, Element::PlainList), - map(greater_block_matcher, Element::GreaterBlock), - map(dynamic_block_matcher, Element::DynamicBlock), - map(footnote_definition_matcher, Element::FootnoteDefinition), - map(comment_matcher, Element::Comment), - map(drawer_matcher, Element::Drawer), - map(table_matcher, Element::Table), - ))(input) -} diff --git a/src/parser/element_parser.rs b/src/parser/element_parser.rs new file mode 100644 index 0000000..7a6743a --- /dev/null +++ b/src/parser/element_parser.rs @@ -0,0 +1,47 @@ +use super::comment::comment; +use super::drawer::drawer; +use super::dynamic_block::dynamic_block; +use super::element::Element; +use super::error::Res; +use super::footnote_definition::footnote_definition; +use super::greater_block::greater_block; +use super::paragraph::paragraph; +use super::plain_list::plain_list; +use super::Context; +use crate::parser::parser_with_context::parser_with_context; +use crate::parser::table::org_mode_table; +use nom::branch::alt; +use nom::combinator::map; + +#[tracing::instrument(ret, level = "debug")] +pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> { + let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context); + let paragraph_matcher = parser_with_context!(paragraph)(context); + + alt(( + non_paragraph_matcher, + map(paragraph_matcher, Element::Paragraph), + ))(input) +} + +pub fn non_paragraph_element<'r, 's>( + context: Context<'r, 's>, + input: &'s str, +) -> Res<&'s str, Element<'s>> { + let plain_list_matcher = parser_with_context!(plain_list)(context); + let greater_block_matcher = parser_with_context!(greater_block)(context); + let dynamic_block_matcher = parser_with_context!(dynamic_block)(context); + let footnote_definition_matcher = parser_with_context!(footnote_definition)(context); + let comment_matcher = parser_with_context!(comment)(context); + let drawer_matcher = parser_with_context!(drawer)(context); + let table_matcher = parser_with_context!(org_mode_table)(context); + alt(( + map(plain_list_matcher, Element::PlainList), + map(greater_block_matcher, Element::GreaterBlock), + map(dynamic_block_matcher, Element::DynamicBlock), + map(footnote_definition_matcher, Element::FootnoteDefinition), + map(comment_matcher, Element::Comment), + map(drawer_matcher, Element::Drawer), + map(table_matcher, Element::Table), + ))(input) +} diff --git a/src/parser/footnote_definition.rs b/src/parser/footnote_definition.rs index ab07ab6..176aeb1 100644 --- a/src/parser/footnote_definition.rs +++ b/src/parser/footnote_definition.rs @@ -1,7 +1,7 @@ use super::error::Res; use super::util::WORD_CONSTITUENT_CHARACTERS; use super::Context; -use crate::parser::element::element; +use crate::parser::element_parser::element; use crate::parser::error::CustomError; use crate::parser::error::MyError; use crate::parser::exiting::ExitClass; diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index f13c644..73b1de4 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -1,6 +1,6 @@ use super::error::Res; use super::Context; -use crate::parser::element::element; +use crate::parser::element_parser::element; use crate::parser::error::CustomError; use crate::parser::error::MyError; use crate::parser::exiting::ExitClass; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index b57dbe8..234b0ab 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -3,6 +3,7 @@ mod document; mod drawer; mod dynamic_block; mod element; +mod element_parser; mod error; mod exiting; mod footnote_definition; diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index 9902cd5..8b18ce8 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -15,7 +15,7 @@ use crate::parser::util::exit_matcher_parser; use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting; use crate::parser::util::start_of_line; -use super::element::non_paragraph_element; +use super::element_parser::non_paragraph_element; use super::error::Res; use super::lesser_element::Paragraph; use super::util::blank_line; diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index a1825fa..afe48c5 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -7,7 +7,7 @@ use super::parser_with_context::parser_with_context; use super::util::maybe_consume_trailing_whitespace_if_not_exiting; use super::util::non_whitespace_character; use super::Context; -use crate::parser::element::element; +use crate::parser::element_parser::element; use crate::parser::exiting::ExitClass; use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ExitMatcherNode;