2022-07-16 03:26:49 +00:00
|
|
|
//! A single element of text.
|
2022-10-15 18:28:24 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-07-17 01:32:23 +00:00
|
|
|
use crate::parser::parser_with_context::parser_with_context;
|
|
|
|
use crate::parser::text::paragraph_end;
|
|
|
|
|
2022-12-04 01:53:14 +00:00
|
|
|
use super::new_context::ChainBehavior;
|
2022-12-04 02:11:39 +00:00
|
|
|
use super::new_context::ContextElement;
|
2022-12-04 01:53:14 +00:00
|
|
|
use super::new_context::ContextTree;
|
|
|
|
use super::new_context::FailMatcherNode;
|
2022-07-17 01:55:33 +00:00
|
|
|
use super::text::bold_end;
|
|
|
|
use super::text::bold_start;
|
2022-07-16 03:26:49 +00:00
|
|
|
use super::text::line_break;
|
2022-11-25 23:40:38 +00:00
|
|
|
use super::text::link_end;
|
|
|
|
use super::text::link_start;
|
2022-07-16 03:26:49 +00:00
|
|
|
use super::text::space;
|
|
|
|
use super::text::span;
|
|
|
|
use super::text::symbol;
|
2022-07-17 01:55:33 +00:00
|
|
|
use super::text::Bold;
|
2022-11-25 23:40:38 +00:00
|
|
|
use super::text::Link;
|
2022-07-17 00:42:56 +00:00
|
|
|
use super::text::Res;
|
2022-07-16 03:26:49 +00:00
|
|
|
use super::text::TextElement;
|
2022-12-04 02:13:42 +00:00
|
|
|
use super::Context;
|
2022-07-16 03:26:49 +00:00
|
|
|
use nom::branch::alt;
|
2022-11-26 23:25:53 +00:00
|
|
|
use nom::combinator::eof;
|
2022-07-16 03:26:49 +00:00
|
|
|
use nom::combinator::map;
|
|
|
|
use nom::combinator::not;
|
2022-07-17 01:55:33 +00:00
|
|
|
use nom::combinator::recognize;
|
2022-11-26 23:22:41 +00:00
|
|
|
use nom::error::ErrorKind;
|
|
|
|
use nom::error::ParseError;
|
2022-07-16 03:26:49 +00:00
|
|
|
use nom::error::VerboseError;
|
2022-11-27 00:14:19 +00:00
|
|
|
use nom::multi::many1;
|
2022-07-17 01:32:23 +00:00
|
|
|
use nom::multi::many_till;
|
2022-07-17 01:55:33 +00:00
|
|
|
use nom::sequence::tuple;
|
2022-07-16 03:26:49 +00:00
|
|
|
use nom::IResult;
|
2022-11-27 00:46:59 +00:00
|
|
|
use nom::InputLength;
|
|
|
|
use nom::Parser;
|
2022-11-25 23:23:51 +00:00
|
|
|
use tracing::instrument;
|
2022-11-26 23:22:41 +00:00
|
|
|
use tracing::trace;
|
2022-10-15 00:17:48 +00:00
|
|
|
|
2022-12-04 01:47:31 +00:00
|
|
|
fn context_many_till<'r, I, O, E, F, M, T>(
|
2022-12-04 02:13:42 +00:00
|
|
|
context: Context<'r>,
|
2022-11-27 00:41:33 +00:00
|
|
|
mut many_matcher: M,
|
|
|
|
mut till_matcher: T,
|
2022-11-27 00:46:59 +00:00
|
|
|
) -> impl FnMut(I) -> IResult<I, (Vec<O>, F), E>
|
|
|
|
where
|
|
|
|
I: Clone + InputLength,
|
|
|
|
M: Parser<I, O, E>,
|
|
|
|
T: Parser<I, F, E>,
|
|
|
|
E: ParseError<I>,
|
|
|
|
{
|
|
|
|
move |mut i: I| {
|
2022-11-27 00:54:46 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
2022-11-27 00:35:02 +00:00
|
|
|
}
|
2022-11-27 00:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 00:14:19 +00:00
|
|
|
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
|
2022-12-04 01:53:14 +00:00
|
|
|
let initial_context: ContextTree<'_> = ContextTree::new();
|
2022-11-27 00:35:02 +00:00
|
|
|
let paragraph_parser = parser_with_context!(paragraph);
|
2022-12-04 02:06:04 +00:00
|
|
|
let ret = many1(paragraph_parser(initial_context))(input);
|
2022-11-27 00:14:19 +00:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2022-12-04 01:53:14 +00:00
|
|
|
pub fn paragraph<'s, 'r>(
|
2022-12-04 02:13:42 +00:00
|
|
|
context: Context<'r>,
|
2022-11-27 00:26:48 +00:00
|
|
|
i: &'s str,
|
2022-12-04 01:53:14 +00:00
|
|
|
) -> Res<&'s str, (Vec<TextElement<'s>>, &'s str)> {
|
2022-11-27 00:14:19 +00:00
|
|
|
// Add a not(eof) check because many_till cannot match a zero-length string
|
|
|
|
not(eof)(i)?;
|
2022-12-04 02:11:39 +00:00
|
|
|
let paragraph_context =
|
|
|
|
context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode {
|
|
|
|
fail_matcher: ChainBehavior::AndParent(Some(¶graph_end)),
|
|
|
|
}));
|
2022-12-04 00:38:04 +00:00
|
|
|
let ret = {
|
2022-12-04 02:11:39 +00:00
|
|
|
let text_element_parser = parser_with_context!(flat_text_element)(paragraph_context);
|
2022-12-04 00:38:04 +00:00
|
|
|
many_till(text_element_parser, paragraph_end)(i)
|
|
|
|
};
|
2022-11-27 05:35:38 +00:00
|
|
|
// let paragraph_context = context.with_additional_fail_matcher(¶graph_end);
|
2022-12-04 00:38:04 +00:00
|
|
|
// let text_element_parser = parser_with_context!(flat_text_element)(¶graph_context);
|
|
|
|
// let ret = context_many_till(¶graph_context, text_element_parser, paragraph_end)(i);
|
2022-11-27 00:54:46 +00:00
|
|
|
// let ret = many_till(text_element_parser, paragraph_end)(i);
|
2022-11-27 00:14:19 +00:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2022-12-04 02:13:42 +00:00
|
|
|
fn flat_text_element<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, TextElement<'s>> {
|
2022-11-27 05:21:34 +00:00
|
|
|
not(|i| context.check_fail_matcher(i))(i)?;
|
2022-10-15 18:04:24 +00:00
|
|
|
|
2022-12-04 02:35:30 +00:00
|
|
|
let bold_matcher = parser_with_context!(flat_bold)(context.clone());
|
|
|
|
let link_matcher = parser_with_context!(flat_link)(context.clone());
|
2022-11-25 18:55:09 +00:00
|
|
|
|
2022-10-15 18:04:24 +00:00
|
|
|
alt((
|
2022-11-25 23:55:56 +00:00
|
|
|
map(bold_matcher, TextElement::Bold),
|
|
|
|
map(link_matcher, TextElement::Link),
|
2022-10-15 18:04:24 +00:00
|
|
|
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)
|
2022-10-15 00:17:48 +00:00
|
|
|
}
|
2022-10-15 18:16:52 +00:00
|
|
|
|
2022-11-25 18:55:09 +00:00
|
|
|
fn recognize_bold_end(input: &str) -> Res<&str, &str> {
|
|
|
|
recognize(bold_end)(input)
|
|
|
|
}
|
|
|
|
|
2022-12-04 02:13:42 +00:00
|
|
|
fn flat_bold<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, Bold<'s>> {
|
2022-12-04 02:11:39 +00:00
|
|
|
let new_context =
|
|
|
|
context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode {
|
|
|
|
fail_matcher: ChainBehavior::AndParent(Some(&recognize_bold_end)),
|
|
|
|
}));
|
2022-11-27 05:35:38 +00:00
|
|
|
// let new_context = context.with_additional_fail_matcher(&recognize_bold_end);
|
2022-12-04 02:11:39 +00:00
|
|
|
let text_element_parser = parser_with_context!(flat_text_element)(new_context);
|
2022-11-25 18:55:09 +00:00
|
|
|
let (remaining, captured) = recognize(tuple((
|
|
|
|
bold_start,
|
|
|
|
many_till(text_element_parser, bold_end),
|
|
|
|
)))(i)?;
|
2022-11-25 23:55:56 +00:00
|
|
|
let ret = Bold { contents: captured };
|
2022-11-25 18:55:09 +00:00
|
|
|
Ok((remaining, ret))
|
2022-10-15 18:28:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 23:40:38 +00:00
|
|
|
fn recognize_link_end(input: &str) -> Res<&str, &str> {
|
|
|
|
recognize(link_end)(input)
|
|
|
|
}
|
|
|
|
|
2022-12-04 02:13:42 +00:00
|
|
|
fn flat_link<'s, 'r, C>(context: Context<'r>, i: &'s str) -> Res<&'s str, Link<'s>> {
|
2022-12-04 02:11:39 +00:00
|
|
|
let new_context =
|
|
|
|
context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode {
|
|
|
|
fail_matcher: ChainBehavior::AndParent(Some(&recognize_link_end)),
|
|
|
|
}));
|
2022-11-27 05:35:38 +00:00
|
|
|
// let new_context = context.with_additional_fail_matcher(&recognize_link_end);
|
2022-12-04 02:11:39 +00:00
|
|
|
let text_element_parser = parser_with_context!(flat_text_element)(new_context);
|
2022-11-25 23:40:38 +00:00
|
|
|
let (remaining, captured) = recognize(tuple((
|
|
|
|
link_start,
|
|
|
|
many_till(text_element_parser, link_end),
|
|
|
|
)))(i)?;
|
2022-11-25 23:55:56 +00:00
|
|
|
let ret = Link { contents: captured };
|
2022-11-25 23:40:38 +00:00
|
|
|
Ok((remaining, ret))
|
|
|
|
}
|