Separate out calls to build element parser.

This commit is contained in:
Tom Alexander 2023-04-22 01:08:45 -04:00
parent 4ac6d89311
commit ba74bb58bb
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
7 changed files with 28 additions and 14 deletions

View File

@ -124,7 +124,8 @@ fn zeroth_section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
let without_consuming_whitespace_context = let without_consuming_whitespace_context =
parser_context.with_additional_node(ContextElement::ConsumeTrailingWhitespace(false)); parser_context.with_additional_node(ContextElement::ConsumeTrailingWhitespace(false));
let element_matcher = parser_with_context!(element(true))(&parser_context); let element_matcher = element(true);
let element_matcher = parser_with_context!(element_matcher)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, comment_and_property_drawer_element) = opt(tuple(( let (remaining, comment_and_property_drawer_element) = opt(tuple((
@ -166,7 +167,8 @@ fn section<'r, 's>(context: Context<'r, 's>, mut input: &'s str) -> Res<&'s str,
class: ExitClass::Document, class: ExitClass::Document,
exit_matcher: &section_end, exit_matcher: &section_end,
})); }));
let element_matcher = parser_with_context!(element(true))(&parser_context); let element_matcher = element(true);
let element_matcher = parser_with_context!(element_matcher)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
// TODO: Match whatever a planning is. // TODO: Match whatever a planning is.
let (mut remaining, (planning_element, property_drawer_element)) = tuple(( let (mut remaining, (planning_element, property_drawer_element)) = tuple((

View File

@ -54,7 +54,8 @@ pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
exit_matcher: &drawer_end, exit_matcher: &drawer_end,
})); }));
let element_matcher = parser_with_context!(element(true))(&parser_context); let element_matcher = element(true);
let element_matcher = parser_with_context!(element_matcher)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) { let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) {
Ok((remaining, (whitespace, (_children, _exit_contents)))) => ( Ok((remaining, (whitespace, (_children, _exit_contents)))) => (

View File

@ -59,7 +59,8 @@ pub fn dynamic_block<'r, 's>(
Some((_ws, parameters)) => Some(parameters), Some((_ws, parameters)) => Some(parameters),
None => None, None => None,
}; };
let element_matcher = parser_with_context!(element(true))(&parser_context); let element_matcher = element(true);
let element_matcher = parser_with_context!(element_matcher)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) { let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) {
Ok((remaining, (whitespace, (_children, _exit_contents)))) => ( Ok((remaining, (whitespace, (_children, _exit_contents)))) => (

View File

@ -50,7 +50,8 @@ pub fn footnote_definition<'r, 's>(
exit_matcher: &footnote_definition_end, exit_matcher: &footnote_definition_end,
})); }));
// 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. // 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 element_matcher = element(true);
let element_matcher = parser_with_context!(element_matcher)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let (remaining, (children, _exit_contents)) = let (remaining, (children, _exit_contents)) =
many_till(element_matcher, exit_matcher)(remaining)?; many_till(element_matcher, exit_matcher)(remaining)?;
@ -124,7 +125,8 @@ line footnote.";
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let document_context =
initial_context.with_additional_node(ContextElement::DocumentRoot(input)); initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let footnote_definition_matcher = parser_with_context!(element(true))(&document_context); let element_matcher = element(true);
let footnote_definition_matcher = parser_with_context!(element_matcher)(&document_context);
let (remaining, first_footnote_definition) = let (remaining, first_footnote_definition) =
footnote_definition_matcher(input).expect("Parse first footnote_definition"); footnote_definition_matcher(input).expect("Parse first footnote_definition");
let (remaining, second_footnote_definition) = let (remaining, second_footnote_definition) =
@ -155,7 +157,8 @@ not in the footnote.";
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let document_context =
initial_context.with_additional_node(ContextElement::DocumentRoot(input)); initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let footnote_definition_matcher = parser_with_context!(element(true))(&document_context); let element_matcher = element(true);
let footnote_definition_matcher = parser_with_context!(element_matcher)(&document_context);
let (remaining, first_footnote_definition) = let (remaining, first_footnote_definition) =
footnote_definition_matcher(input).expect("Parse first footnote_definition"); footnote_definition_matcher(input).expect("Parse first footnote_definition");
assert_eq!(remaining, "not in the footnote."); assert_eq!(remaining, "not in the footnote.");

View File

@ -69,7 +69,8 @@ pub fn greater_block<'r, 's>(
None => None, None => None,
}; };
let element_matcher = parser_with_context!(element(true))(&parser_context); let element_matcher = element(true);
let element_matcher = parser_with_context!(element_matcher)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
// Check for a completely empty block // Check for a completely empty block
let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) { let (remaining, children) = match consumed(many_till(blank_line, exit_matcher))(remaining) {

View File

@ -1,4 +1,5 @@
use crate::error::Res; use crate::error::Res;
use crate::parser::element_parser::element;
use nom::branch::alt; use nom::branch::alt;
use nom::combinator::eof; use nom::combinator::eof;
use nom::combinator::recognize; use nom::combinator::recognize;
@ -45,7 +46,8 @@ pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st
#[tracing::instrument(ret, level = "debug")] #[tracing::instrument(ret, level = "debug")]
fn paragraph_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn paragraph_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let non_paragraph_element_matcher = parser_with_context!(element(false))(context); let element_matcher = element(false);
let non_paragraph_element_matcher = parser_with_context!(element_matcher)(context);
let start_of_line_matcher = parser_with_context!(start_of_line)(&context); let start_of_line_matcher = parser_with_context!(start_of_line)(&context);
alt(( alt((
recognize(tuple((start_of_line_matcher, many1(blank_line)))), recognize(tuple((start_of_line_matcher, many1(blank_line)))),
@ -70,7 +72,8 @@ mod tests {
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let document_context =
initial_context.with_additional_node(ContextElement::DocumentRoot(input)); initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let paragraph_matcher = parser_with_context!(element(true))(&document_context); let element_matcher = element(true);
let paragraph_matcher = parser_with_context!(element_matcher)(&document_context);
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
let (remaining, second_paragraph) = let (remaining, second_paragraph) =
paragraph_matcher(remaining).expect("Parse second paragraph."); paragraph_matcher(remaining).expect("Parse second paragraph.");

View File

@ -161,8 +161,9 @@ pub fn plain_list_item<'r, 's>(
exit_matcher: &plain_list_item_end, exit_matcher: &plain_list_item_end,
})); }));
let with_consume_matcher = parser_with_context!(element(true))(&with_consume_context); let element_matcher = element(true);
let without_consume_matcher = parser_with_context!(element(true))(&without_consume_context); let with_consume_matcher = parser_with_context!(element_matcher)(&with_consume_context);
let without_consume_matcher = parser_with_context!(element_matcher)(&without_consume_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&with_consume_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&with_consume_context);
let (remaining, bull) = let (remaining, bull) =
verify(bullet, |bull: &str| bull != "*" || indent_level > 0)(remaining)?; verify(bullet, |bull: &str| bull != "*" || indent_level > 0)(remaining)?;
@ -370,7 +371,8 @@ mod tests {
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let document_context =
initial_context.with_additional_node(ContextElement::DocumentRoot(input)); initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let plain_list_matcher = parser_with_context!(element(true))(&document_context); let element_matcher = element(true);
let plain_list_matcher = parser_with_context!(element_matcher)(&document_context);
let (remaining, result) = let (remaining, result) =
plain_list_matcher(input).expect("Should parse the plain list successfully."); plain_list_matcher(input).expect("Should parse the plain list successfully.");
assert_eq!(remaining, " ipsum\n"); assert_eq!(remaining, " ipsum\n");
@ -396,7 +398,8 @@ baz"#;
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let document_context =
initial_context.with_additional_node(ContextElement::DocumentRoot(input)); initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let plain_list_matcher = parser_with_context!(element(true))(&document_context); let element_matcher = element(true);
let plain_list_matcher = parser_with_context!(element_matcher)(&document_context);
let (remaining, result) = let (remaining, result) =
plain_list_matcher(input).expect("Should parse the plain list successfully."); plain_list_matcher(input).expect("Should parse the plain list successfully.");
assert_eq!(remaining, "baz"); assert_eq!(remaining, "baz");