From 77f02a21b924fdc24ac387b2b45dc51eccda4aaf Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 26 Nov 2022 19:14:19 -0500 Subject: [PATCH] Move the document parser inside text_element_parser. This is to put the context-sensitive parsers together during this early development stage. --- src/parser/mod.rs | 2 +- src/parser/text_element_parser.rs | 32 +++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 436b250..9de44a1 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -2,4 +2,4 @@ mod nom_context; mod parser_with_context; mod text; mod text_element_parser; -pub use text::document; +pub use text_element_parser::document; diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 403a3ac..416f20b 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -5,6 +5,7 @@ use std::rc::Rc; use crate::parser::parser_with_context::parser_with_context; use crate::parser::text::paragraph_end; +use super::nom_context::ContextTree; use super::nom_context::OrgModeContext; use super::nom_context::OrgModeContextTree; use super::text::bold_end; @@ -27,12 +28,31 @@ 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 tracing::instrument; use tracing::trace; +pub fn document(input: &str) -> Res<&str, Vec<(Vec, &str)>> { + let initial_context = ContextTree::new(); + let ret = many1(parser_with_context!(paragraph)(&initial_context))(input); + ret +} + +pub fn paragraph<'s, 'r>( + i: &'s str, + context: &'r OrgModeContext<'r>, +) -> 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_fail_matcher(¶graph_end); + let text_element_parser = parser_with_context!(flat_text_element)(¶graph_context); + let ret = many_till(text_element_parser, paragraph_end)(i); + ret +} + fn flat_text_element<'s, 'r>( i: &'s str, context: &'r OrgModeContext<'r>, @@ -83,15 +103,3 @@ fn flat_link<'s, 'r>(i: &'s str, context: &'r OrgModeContext<'r>) -> Res<&'s str let ret = Link { contents: captured }; Ok((remaining, ret)) } - -pub fn paragraph<'s, 'r>( - i: &'s str, - context: &'r OrgModeContext<'r>, -) -> 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_fail_matcher(¶graph_end); - let text_element_parser = parser_with_context!(flat_text_element)(¶graph_context); - let ret = many_till(text_element_parser, paragraph_end)(i); - ret -}