Move trailing whitespace parsing to a separate element.

I still need to parse the line break at the end of elements.
This commit is contained in:
Tom Alexander
2023-03-31 11:16:37 -04:00
parent 602cf4c374
commit 707eac5bf8
5 changed files with 42 additions and 5 deletions

View File

@@ -20,10 +20,12 @@ use crate::parser::parser_context::ChainBehavior;
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ContextTree;
use crate::parser::parser_context::ExitMatcherNode;
use crate::parser::util::element_trailing_whitespace;
use super::element::Element;
use super::error::Res;
use super::object::Object;
use super::parser_context;
use super::parser_with_context::parser_with_context;
use super::source::Source;
use super::util::exit_matcher_parser;
@@ -105,12 +107,36 @@ fn section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Sec
.with_additional_node(ContextElement::Context("section"));
let element_matcher = parser_with_context!(element)(&parser_context);
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
let trailing_matcher = parser_with_context!(element_trailing_whitespace)(&parser_context);
let (remaining, (children, _exit_contents)) = verify(
many_till(element_matcher, exit_matcher),
many_till(
tuple((
element_matcher,
opt(map(trailing_matcher, Element::TrailingWhitespace)),
)),
exit_matcher,
),
|(children, _exit_contents)| !children.is_empty(),
)(input)?;
let flattened_children: Vec<Element> = children
.into_iter()
.flat_map(|tpl| {
let mut flattened_children = Vec::with_capacity(2);
flattened_children.push(tpl.0);
if let Some(bar) = tpl.1 {
flattened_children.push(bar);
}
flattened_children.into_iter()
})
.collect();
let source = get_consumed(input, remaining);
Ok((remaining, Section { source, children }))
Ok((
remaining,
Section {
source,
children: flattened_children,
},
))
}
#[tracing::instrument(ret, level = "debug")]