//! A single element of text. use std::cell::RefCell; use std::rc::Rc; use crate::parser::parser_with_context::parser_with_context; use crate::parser::text::paragraph_end; use super::nom_context::ChainBehavior; use super::nom_context::ContextElement; use super::nom_context::ContextTree; use super::nom_context::FailMatcherNode; use super::text::bold_end; use super::text::bold_start; use super::text::line_break; use super::text::link_end; use super::text::link_start; use super::text::space; use super::text::span; use super::text::symbol; use super::text::Bold; use super::text::Link; use super::text::Res; use super::text::TextElement; 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::error::ErrorKind; use nom::error::ParseError; use nom::error::VerboseError; use nom::multi::many1; use nom::multi::many_till; use nom::sequence::tuple; use nom::IResult; use nom::InputLength; use nom::Parser; use tracing::instrument; use tracing::trace; type UnboundMatcher<'r> = dyn for<'s> Fn(Context<'r>, &'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>; fn context_many_till<'r, I, O, E, F, M, T>( context: Context<'r>, mut many_matcher: M, mut till_matcher: T, ) -> impl FnMut(I) -> IResult, F), E> where I: Clone + InputLength, M: Parser, T: Parser, E: ParseError, { move |mut i: I| { let mut ret = Vec::new(); loop { let len = i.input_len(); match till_matcher.parse(i.clone()) { Ok((remaining, finish)) => return Ok((remaining, (ret, finish))), Err(nom::Err::Error(_)) => { match many_matcher.parse(i.clone()) { Err(nom::Err::Error(err)) => { return Err(nom::Err::Error(E::append(i, ErrorKind::ManyTill, err))) } Err(e) => return Err(e), Ok((remaining, many_elem)) => { // infinite loop check: the parser must always consume if remaining.input_len() == len { return Err(nom::Err::Error(E::from_error_kind( remaining, ErrorKind::ManyTill, ))); } ret.push(many_elem); i = remaining; } } } Err(e) => return Err(e), } } } } pub fn document(input: &str) -> Res<&str, Vec<(Vec, &str)>> { let initial_context: ContextTree<'_> = ContextTree::new(); let paragraph_parser = parser_with_context!(paragraph); let ret = many1(paragraph_parser(initial_context))(input); ret } pub fn paragraph<'s, 'r>( context: Context<'r>, i: &'s str, ) -> Res<&'s str, (Vec>, &'s str)> { // 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::FailMatcherNode(FailMatcherNode { fail_matcher: ChainBehavior::AndParent(Some(¶graph_end)), })); let ret = { let text_element_parser = parser_with_context!(flat_text_element)(paragraph_context); context_many_till(¶graph_context, text_element_parser, paragraph_end)(i) }; ret } fn flat_text_element<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, TextElement<'s>> { not(|i| context.check_fail_matcher(i))(i)?; let bold_matcher = parser_with_context!(flat_bold)(context.clone()); let link_matcher = parser_with_context!(flat_link)(context.clone()); alt(( map(bold_matcher, TextElement::Bold), map(link_matcher, TextElement::Link), map(span, TextElement::Span), map(symbol("*"), TextElement::Symbol), map(symbol("["), TextElement::Symbol), map(symbol("]"), TextElement::Symbol), map(space, TextElement::Space), map(line_break, TextElement::LineBreak), ))(i) } fn recognize_bold_end(input: &str) -> Res<&str, &str> { recognize(bold_end)(input) } fn flat_bold<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, Bold<'s>> { let nom_context = context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode { fail_matcher: ChainBehavior::AndParent(Some(&recognize_bold_end)), })); // let nom_context = context.with_additional_fail_matcher(&recognize_bold_end); let text_element_parser = parser_with_context!(flat_text_element)(nom_context); let (remaining, captured) = recognize(tuple(( bold_start, many_till(text_element_parser, bold_end), )))(i)?; let ret = Bold { contents: captured }; Ok((remaining, ret)) } fn recognize_link_end(input: &str) -> Res<&str, &str> { recognize(link_end)(input) } fn flat_link<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, Link<'s>> { let nom_context = context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode { fail_matcher: ChainBehavior::AndParent(Some(&recognize_link_end)), })); // let nom_context = context.with_additional_fail_matcher(&recognize_link_end); let text_element_parser = parser_with_context!(flat_text_element)(nom_context); let (remaining, captured) = recognize(tuple(( link_start, many_till(text_element_parser, link_end), )))(i)?; let ret = Link { contents: captured }; Ok((remaining, ret)) }