63 lines
1.9 KiB
Rust
63 lines
1.9 KiB
Rust
|
use super::combinator::context_many_till;
|
||
|
use super::parser_context::ChainBehavior;
|
||
|
use super::parser_context::ContextElement;
|
||
|
use super::parser_context::ExitMatcherNode;
|
||
|
use super::text::blank_line;
|
||
|
use super::text::line_break;
|
||
|
use super::text::Paragraph;
|
||
|
use super::text::Res;
|
||
|
use super::text::TextElement;
|
||
|
use super::text_element_parser::flat_text_element;
|
||
|
use super::token::Token;
|
||
|
use super::Context;
|
||
|
use nom::branch::alt;
|
||
|
use nom::combinator::eof;
|
||
|
use nom::combinator::map;
|
||
|
use nom::combinator::not;
|
||
|
use nom::combinator::recognize;
|
||
|
use nom::multi::many1;
|
||
|
use nom::sequence::tuple;
|
||
|
|
||
|
pub fn paragraph<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Paragraph<'s>> {
|
||
|
// Add a not(eof) check because many_till cannot match a zero-length string
|
||
|
not(eof)(i)?;
|
||
|
let paragraph_context = context
|
||
|
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||
|
exit_matcher: ChainBehavior::AndParent(Some(&context_paragraph_end)),
|
||
|
}))
|
||
|
.with_additional_node(ContextElement::StartOfParagraph);
|
||
|
let (remaining, (many, till)) =
|
||
|
context_many_till(¶graph_context, flat_text_element, context_paragraph_end)(i)?;
|
||
|
let many = many
|
||
|
.into_iter()
|
||
|
.filter_map(|token| match token {
|
||
|
Token::TextElement(text_element) => Some(text_element),
|
||
|
Token::Paragraph(_) => panic!("There should only be text elements in paragraphs."),
|
||
|
})
|
||
|
.collect();
|
||
|
Ok((
|
||
|
remaining,
|
||
|
Paragraph {
|
||
|
contents: many,
|
||
|
paragraph_end: till,
|
||
|
},
|
||
|
))
|
||
|
}
|
||
|
|
||
|
fn context_paragraph_end<'s, 'r>(
|
||
|
context: Context<'r, 's>,
|
||
|
input: &'s str,
|
||
|
) -> Res<&'s str, &'s str> {
|
||
|
paragraph_end(input)
|
||
|
}
|
||
|
|
||
|
fn paragraph_end(input: &str) -> Res<&str, &str> {
|
||
|
alt((
|
||
|
recognize(tuple((
|
||
|
map(line_break, TextElement::LineBreak),
|
||
|
many1(blank_line),
|
||
|
))),
|
||
|
eof,
|
||
|
))(input)
|
||
|
}
|