From 835783757132c05fedf76c1109e7a01b24b98deb Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 Jul 2022 21:32:23 -0400 Subject: [PATCH] Hmmm it seems to be building. --- src/parser/nom_context.rs | 57 ++++++++++++++++--------------- src/parser/parser_with_context.rs | 6 ++-- src/parser/text.rs | 2 +- src/parser/text_element_parser.rs | 10 ++++++ 4 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index bd17675..174866c 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -16,32 +16,35 @@ where pub can_match_link: bool, } -// impl<'a, I, O, E> NomContext<'a, I, O, E> { -// pub fn new(fail_matcher: &'a dyn FnMut(I) -> IResult) -> Self { -// NomContext { -// fail_matcher: fail_matcher, -// can_match_bold: true, -// can_match_link: true, -// } -// } +impl NomContext +where + F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>, +{ + pub fn new(fail_matcher: F) -> Self { + NomContext { + fail_matcher: fail_matcher, + can_match_bold: true, + can_match_link: true, + } + } -// pub fn with_no_bold(&self) -> NomContext { -// NomContext { -// fail_matcher: self.fail_matcher.clone(), -// can_match_bold: false, -// can_match_link: self.can_match_link, -// } -// } + // pub fn with_no_bold(&self) -> NomContext { + // NomContext { + // fail_matcher: self.fail_matcher.clone(), + // can_match_bold: false, + // can_match_link: self.can_match_link, + // } + // } -// // pub fn with_additional_fail_matcher(&self, additional_matcher: G) -> NomContext -// // where -// // G: for<'a> FnMut(I) -> IResult, -// // { -// // let new_fail_matcher = alt((self.fail_matcher, additional_matcher)); -// // NomContext { -// // fail_matcher: Rc::new(RefCell::new(new_fail_matcher)), -// // can_match_bold: self.can_match_bold, -// // can_match_link: self.can_match_link, -// // } -// // } -// } + // pub fn with_additional_fail_matcher(&self, additional_matcher: G) -> NomContext + // where + // G: for<'a> FnMut(I) -> IResult, + // { + // let new_fail_matcher = alt((self.fail_matcher, additional_matcher)); + // NomContext { + // fail_matcher: Rc::new(RefCell::new(new_fail_matcher)), + // can_match_bold: self.can_match_bold, + // can_match_link: self.can_match_link, + // } + // } +} diff --git a/src/parser/parser_with_context.rs b/src/parser/parser_with_context.rs index ddb12f0..c6b1412 100644 --- a/src/parser/parser_with_context.rs +++ b/src/parser/parser_with_context.rs @@ -16,10 +16,10 @@ macro_rules! parser_with_context { ($target:ident) => { - |context| { - |i| { + move |mut context| { + move |i| { // todo - $target(i, context) + $target(i, &mut context) } } }; diff --git a/src/parser/text.rs b/src/parser/text.rs index 2b7269a..ef3b968 100644 --- a/src/parser/text.rs +++ b/src/parser/text.rs @@ -133,7 +133,7 @@ pub fn paragraph(input: &str) -> Res<&str, (Vec, &str)> { todo!() } -fn paragraph_end(input: &str) -> Res<&str, &str> { +pub fn paragraph_end(input: &str) -> Res<&str, &str> { recognize(tuple((map(line_break, TextElement::LineBreak), blank_line)))(input) } diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 91c30ba..ce37405 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -1,4 +1,7 @@ //! A single element of text. +use crate::parser::parser_with_context::parser_with_context; +use crate::parser::text::paragraph_end; + use super::nom_context::NomContext; // use super::parser_with_context::parser_with_context; use super::text::line_break; @@ -11,6 +14,7 @@ use nom::branch::alt; use nom::combinator::map; use nom::combinator::not; use nom::error::VerboseError; +use nom::multi::many_till; use nom::IResult; // parser_with_context!(text_element, TextElement, i, context, { @@ -48,3 +52,9 @@ where // todo todo!() } + +pub fn paragraph(input: &str) -> Res<&str, (Vec, &str)> { + let initial_context = NomContext::new(¶graph_end); + let text_element_parser = parser_with_context!(flat_text_element)(initial_context); + many_till(text_element_parser, paragraph_end)(input) +}