use nom::branch::alt; use nom::combinator::eof; use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many1; use nom::multi::many_till; use nom::sequence::tuple; use crate::parser::error::CustomError; use crate::parser::error::MyError; use crate::parser::exiting::ExitClass; use crate::parser::object::standard_set_object; use crate::parser::parser_context::ContextElement; use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::exit_matcher_parser; 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 super::element::non_paragraph_element; use super::error::Res; use super::lesser_element::Paragraph; use super::util::blank_line; use super::util::get_consumed; use super::Context; #[tracing::instrument(ret, level = "debug")] pub fn paragraph<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Paragraph<'s>> { if immediate_in_section(context, "paragraph") { return Err(nom::Err::Error(CustomError::MyError(MyError( "Cannot nest objects of the same element", )))); } let parser_context = context .with_additional_node(ContextElement::Context("paragraph")) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Beta, exit_matcher: ¶graph_end, })); let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context); let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context); let (remaining, (children, _exit_contents)) = verify( many_till(standard_set_object_matcher, exit_matcher), |(children, _exit_contents)| !children.is_empty(), )(input)?; // Not checking parent exit matcher because if there are any children matched then we have a valid paragraph. let (remaining, _trailing_ws) = maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; let source = get_consumed(input, remaining); Ok((remaining, Paragraph { source, children })) } #[tracing::instrument(ret, level = "debug")] 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!(non_paragraph_element)(context); let start_of_line_matcher = parser_with_context!(start_of_line)(&context); alt(( recognize(tuple((start_of_line_matcher, many1(blank_line)))), recognize(non_paragraph_element_matcher), eof, ))(input) } #[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); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); let (remaining, second_paragraph) = paragraph_matcher(remaining).expect("Parse second paragraph."); assert_eq!(remaining, ""); assert_eq!(first_paragraph.source, "foo bar baz\n\n"); assert_eq!(second_paragraph.source, "lorem ipsum"); } }