Passing the context around.

This commit is contained in:
Tom Alexander 2022-11-24 15:40:07 -05:00
parent a792acf4b0
commit d277a033c9
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 26 additions and 14 deletions

View File

@ -9,6 +9,7 @@ use nom::Parser;
type Link<'r, T> = Option<&'r Node<'r, T>>;
type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>;
pub type OrgModeContext<'r> = ContextTree<'r, ContextElement<'r>>;
struct Node<'r, T> {
elem: T,
@ -44,3 +45,18 @@ pub enum ChainBehavior<'r> {
AndParent(Option<&'r Matcher>),
IgnoreParent(Option<&'r Matcher>),
}
pub trait OrgModeContextTree<'r> {
fn with_additional_fail_matcher(&'r self, fail_matcher: &'r Matcher) -> OrgModeContext<'r>;
}
impl<'r> OrgModeContextTree<'r> for OrgModeContext<'r> {
fn with_additional_fail_matcher(
&'r self,
fail_matcher: &'r Matcher,
) -> ContextTree<'r, ContextElement<'r>> {
self.with_additional_node(ContextElement {
fail_matcher: ChainBehavior::AndParent(Some(fail_matcher)),
})
}
}

View File

@ -26,9 +26,8 @@ use nom::multi::many_till;
use nom::sequence::tuple;
use nom::IResult;
use super::nom_context::ChainBehavior;
use super::nom_context::ContextElement;
use super::nom_context::ContextTree;
use super::nom_context::OrgModeContextTree;
use super::parser_with_context::parser_with_context;
use super::text_element_parser::paragraph;
@ -140,14 +139,7 @@ pub fn paragraph_end(input: &str) -> Res<&str, &str> {
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
let initial_context = ContextTree::new();
let paragraph_context = initial_context.with_additional_node(ContextElement {
fail_matcher: ChainBehavior::AndParent(Some(&paragraph_end)),
});
// let initial_context = TestContext::new();
// let paragraph_context = initial_context;
// let paragraph_context =
// initial_context.with_additional_fail_matcher(Rc::new(RefCell::new(paragraph_end)));
// let initial_context = NomContext::new(Rc::new(RefCell::new(paragraph_end)));
let ret = many1(parser_with_context!(paragraph)(10))(input);
let paragraph_context = initial_context.with_additional_fail_matcher(&paragraph_end);
let ret = many1(parser_with_context!(paragraph)(&paragraph_context))(input);
ret
}

View File

@ -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::OrgModeContext;
use super::text::bold_end;
use super::text::bold_start;
use super::text::line_break;
@ -23,7 +24,10 @@ use nom::multi::many_till;
use nom::sequence::tuple;
use nom::IResult;
fn flat_text_element<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextElement<'s>> {
fn flat_text_element<'s, 'r>(
i: &'s str,
context: &'r OrgModeContext<'r>,
) -> Res<&'s str, TextElement<'s>> {
// context.not_matching_fail(i)?;
alt((
@ -36,7 +40,7 @@ fn flat_text_element<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextEleme
))(i)
}
fn flat_bold<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextElement<'s>> {
fn flat_bold<'s, 'r>(i: &'s str, context: &'r OrgModeContext<'r>) -> Res<&'s str, TextElement<'s>> {
// let fail_matcher = recognize(bold_end);
// let new_context = context.with_additional_fail_matcher(Rc::new(RefCell::new(paragraph_end)));
// let new_context =
@ -47,7 +51,7 @@ fn flat_bold<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextElement<'s>>
pub fn paragraph<'s, 'r>(
i: &'s str,
context: u32,
context: &'r OrgModeContext<'r>,
) -> Res<&'s str, (Vec<TextElement<'s>>, &'s str)> {
let text_element_parser = parser_with_context!(flat_text_element)(context);
many_till(text_element_parser, paragraph_end)(i)