2023-03-25 16:53:57 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::combinator::eof;
|
|
|
|
use nom::combinator::recognize;
|
2023-03-27 22:50:25 +00:00
|
|
|
use nom::combinator::verify;
|
2023-03-25 16:53:57 +00:00
|
|
|
use nom::multi::many1;
|
2023-03-27 22:50:25 +00:00
|
|
|
use nom::multi::many_till;
|
2023-03-25 16:53:57 +00:00
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
|
|
|
use crate::parser::object::standard_set_object;
|
|
|
|
use crate::parser::parser_context::ChainBehavior;
|
|
|
|
use crate::parser::parser_context::ContextElement;
|
|
|
|
use crate::parser::parser_context::ExitMatcherNode;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
2023-03-27 22:50:25 +00:00
|
|
|
use crate::parser::util::exit_matcher_parser;
|
2023-04-10 15:50:43 +00:00
|
|
|
use crate::parser::util::maybe_consume_trailing_whitespace;
|
2023-04-03 20:29:47 +00:00
|
|
|
use crate::parser::util::start_of_line;
|
2023-03-25 16:53:57 +00:00
|
|
|
|
2023-03-27 17:06:41 +00:00
|
|
|
use super::element::non_paragraph_element;
|
2023-03-25 16:53:57 +00:00
|
|
|
use super::error::Res;
|
|
|
|
use super::lesser_element::Paragraph;
|
|
|
|
use super::util::blank_line;
|
|
|
|
use super::util::get_consumed;
|
|
|
|
use super::Context;
|
|
|
|
|
2023-03-27 22:08:17 +00:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
2023-03-25 16:53:57 +00:00
|
|
|
pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Paragraph<'s>> {
|
|
|
|
let parser_context =
|
|
|
|
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
|
|
|
exit_matcher: ChainBehavior::AndParent(Some(¶graph_end)),
|
|
|
|
}));
|
|
|
|
let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context);
|
2023-03-27 22:50:25 +00:00
|
|
|
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
2023-03-25 16:53:57 +00:00
|
|
|
|
2023-03-27 22:50:25 +00:00
|
|
|
let (remaining, (children, _exit_contents)) = verify(
|
2023-04-03 21:25:30 +00:00
|
|
|
many_till(standard_set_object_matcher, exit_matcher),
|
2023-03-27 22:50:25 +00:00
|
|
|
|(children, _exit_contents)| !children.is_empty(),
|
|
|
|
)(input)?;
|
2023-03-25 16:53:57 +00:00
|
|
|
|
2023-04-10 15:34:29 +00:00
|
|
|
// Not checking parent exit matcher because if there are any children matched then we have a valid paragraph.
|
2023-04-10 15:29:14 +00:00
|
|
|
|
2023-04-10 15:50:43 +00:00
|
|
|
let (remaining, _trailing_ws) = maybe_consume_trailing_whitespace(context, remaining)?;
|
2023-04-10 15:29:14 +00:00
|
|
|
|
2023-03-25 16:53:57 +00:00
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
|
|
|
|
Ok((remaining, Paragraph { source, children }))
|
|
|
|
}
|
|
|
|
|
2023-03-27 22:08:17 +00:00
|
|
|
#[tracing::instrument(ret, level = "debug")]
|
2023-03-25 16:53:57 +00:00
|
|
|
fn paragraph_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
2023-03-27 17:06:41 +00:00
|
|
|
let non_paragraph_element_matcher = parser_with_context!(non_paragraph_element)(context);
|
2023-04-03 20:29:47 +00:00
|
|
|
let start_of_line_matcher = parser_with_context!(start_of_line)(&context);
|
2023-03-27 17:06:41 +00:00
|
|
|
alt((
|
2023-04-03 20:29:47 +00:00
|
|
|
recognize(tuple((start_of_line_matcher, many1(blank_line)))),
|
2023-03-31 17:08:53 +00:00
|
|
|
recognize(non_paragraph_element_matcher),
|
2023-03-27 17:06:41 +00:00
|
|
|
eof,
|
|
|
|
))(input)
|
2023-03-25 16:53:57 +00:00
|
|
|
}
|
2023-04-10 15:24:04 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::parser::parser_context::ContextElement;
|
|
|
|
use crate::parser::parser_context::ContextTree;
|
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn two_paragraphs() {
|
|
|
|
let input = "foo bar baz\n\nlorem ipsum";
|
|
|
|
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
|
|
|
let document_context =
|
|
|
|
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
|
|
|
|
let paragraph_matcher = parser_with_context!(paragraph)(&document_context);
|
2023-04-10 15:34:29 +00:00
|
|
|
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
|
|
|
|
let (remaining, second_paragraph) =
|
|
|
|
paragraph_matcher(remaining).expect("Parse second paragraph.");
|
2023-04-10 15:24:04 +00:00
|
|
|
assert_eq!(remaining, "");
|
|
|
|
assert_eq!(first_paragraph.source, "foo bar baz\n\n");
|
|
|
|
assert_eq!(second_paragraph.source, "lorem ipsum");
|
|
|
|
}
|
|
|
|
}
|