organic/src/parser/text_element_parser.rs

110 lines
3.4 KiB
Rust
Raw Normal View History

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;
use super::nom_context::ContextTree;
2022-11-27 00:22:14 +00:00
use super::nom_context::OrgModeContextNode;
use super::nom_context::OrgModeContextTree;
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;
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;
use super::text::Link;
use super::text::Res;
2022-07-16 03:26:49 +00:00
use super::text::TextElement;
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;
use nom::error::ErrorKind;
use nom::error::ParseError;
2022-07-16 03:26:49 +00:00
use nom::error::VerboseError;
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-25 23:23:51 +00:00
use tracing::instrument;
use tracing::trace;
2022-10-15 00:17:48 +00:00
2022-11-27 00:26:48 +00:00
fn context_many_till() {
todo!()
}
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
let initial_context = ContextTree::new();
let ret = many1(parser_with_context!(paragraph)(&initial_context))(input);
ret
}
pub fn paragraph<'s, 'r>(
2022-11-27 00:22:14 +00:00
context: &'r OrgModeContextNode<'r>,
2022-11-27 00:26:48 +00:00
i: &'s str,
) -> Res<&'s str, (Vec<TextElement<'s>>, &'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_fail_matcher(&paragraph_end);
let text_element_parser = parser_with_context!(flat_text_element)(&paragraph_context);
let ret = many_till(text_element_parser, paragraph_end)(i);
ret
}
2022-11-24 20:40:07 +00:00
fn flat_text_element<'s, 'r>(
2022-11-27 00:22:14 +00:00
context: &'r OrgModeContextNode<'r>,
2022-11-27 00:26:48 +00:00
i: &'s str,
2022-11-24 20:40:07 +00:00
) -> Res<&'s str, TextElement<'s>> {
not(|i| context.match_fail(i))(i)?;
let bold_matcher = parser_with_context!(flat_bold)(context);
let link_matcher = parser_with_context!(flat_link)(context);
alt((
2022-11-25 23:55:56 +00:00
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)
2022-10-15 00:17:48 +00:00
}
2022-10-15 18:16:52 +00:00
fn recognize_bold_end(input: &str) -> Res<&str, &str> {
recognize(bold_end)(input)
}
2022-11-27 00:26:48 +00:00
fn flat_bold<'s, 'r>(context: &'r OrgModeContextNode<'r>, i: &'s str) -> Res<&'s str, Bold<'s>> {
let new_context = context.with_additional_fail_matcher(&recognize_bold_end);
let text_element_parser = parser_with_context!(flat_text_element)(&new_context);
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 };
Ok((remaining, ret))
2022-10-15 18:28:24 +00:00
}
fn recognize_link_end(input: &str) -> Res<&str, &str> {
recognize(link_end)(input)
}
2022-11-27 00:26:48 +00:00
fn flat_link<'s, 'r>(context: &'r OrgModeContextNode<'r>, i: &'s str) -> Res<&'s str, Link<'s>> {
let new_context = context.with_additional_fail_matcher(&recognize_link_end);
let text_element_parser = parser_with_context!(flat_text_element)(&new_context);
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 };
Ok((remaining, ret))
}