Really start from scratch.

This commit is contained in:
Tom Alexander 2022-11-24 14:42:05 -05:00
parent 6f416f6997
commit 1487f7d96b
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 39 additions and 36 deletions

View File

@ -7,29 +7,41 @@ use nom::error::VerboseError;
use nom::IResult;
use nom::Parser;
type ContextReference<'r, T> = Option<&'r ContextLayer<'r, T>>;
type MatcherRef = dyn for<'s> FnMut(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>;
// type ContextReference<'r, T> = Option<ContextLayer<'r, T>>;
// type MatcherRef = dyn for<'s> FnMut(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>;
pub struct ContextLayer<'r, T> {
data: T,
parent: ContextReference<'r, T>,
}
// pub struct ContextLayer<'r, T> {
// data: T,
// parent: ContextReference<'r, T>,
// }
pub struct TestContext<'r, T> {
head: ContextReference<'r, T>,
}
// pub struct TestContext<'r, T> {
// head: ContextReference<'r, T>,
// }
pub struct ContextData<'r> {
fail_matcher: ChainBehavior<'r>,
}
// pub struct ContextData<'r> {
// fail_matcher: ChainBehavior<'r>,
// }
impl<'r> TestContext<'r, ContextData<'r>> {
pub fn new() -> Self {
TestContext { head: None }
}
}
// impl<'r> TestContext<'r, ContextData<'r>> {
// pub fn new() -> Self {
// TestContext { head: None }
// }
enum ChainBehavior<'r> {
AndParent(Option<&'r MatcherRef>),
IgnoreParent(Option<&'r MatcherRef>),
}
// pub fn with_additional_fail_matcher<'subr>(
// &self,
// additional_fail_matcher: &'subr MatcherRef,
// ) -> TestContext<'subr, ContextData<'subr>> {
// // TestContext {
// // head: Some(
// // )
// // }
// todo!()
// }
// }
// enum ChainBehavior<'r> {
// AndParent(Option<&'r MatcherRef>),
// IgnoreParent(Option<&'r MatcherRef>),
// }

View File

@ -26,7 +26,6 @@ use nom::multi::many_till;
use nom::sequence::tuple;
use nom::IResult;
use super::nom_context::TestContext;
use super::parser_with_context::parser_with_context;
use super::text_element_parser::paragraph;
@ -137,11 +136,11 @@ pub fn paragraph_end(input: &str) -> Res<&str, &str> {
}
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
let initial_context = TestContext::new();
let paragraph_context = initial_context;
// 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)(&paragraph_context))(input);
let ret = many1(parser_with_context!(paragraph)(10))(input);
ret
}

View File

@ -5,8 +5,6 @@ use std::rc::Rc;
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::text::paragraph_end;
use super::nom_context::ContextData;
use super::nom_context::TestContext;
use super::text::bold_end;
use super::text::bold_start;
use super::text::line_break;
@ -25,10 +23,7 @@ use nom::multi::many_till;
use nom::sequence::tuple;
use nom::IResult;
fn flat_text_element<'s, 'r>(
i: &'s str,
context: &'r TestContext<ContextData>,
) -> Res<&'s str, TextElement<'s>> {
fn flat_text_element<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextElement<'s>> {
// context.not_matching_fail(i)?;
alt((
@ -41,10 +36,7 @@ fn flat_text_element<'s, 'r>(
))(i)
}
fn flat_bold<'s, 'r>(
i: &'s str,
context: &'r TestContext<ContextData>,
) -> Res<&'s str, TextElement<'s>> {
fn flat_bold<'s, 'r>(i: &'s str, context: u32) -> 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 =
@ -55,7 +47,7 @@ fn flat_bold<'s, 'r>(
pub fn paragraph<'s, 'r>(
i: &'s str,
context: &'r TestContext<ContextData>,
context: u32,
) -> 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)