Passing the context around.
This commit is contained in:
parent
a792acf4b0
commit
d277a033c9
@ -9,6 +9,7 @@ use nom::Parser;
|
|||||||
|
|
||||||
type Link<'r, T> = Option<&'r Node<'r, T>>;
|
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>>;
|
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> {
|
struct Node<'r, T> {
|
||||||
elem: T,
|
elem: T,
|
||||||
@ -44,3 +45,18 @@ pub enum ChainBehavior<'r> {
|
|||||||
AndParent(Option<&'r Matcher>),
|
AndParent(Option<&'r Matcher>),
|
||||||
IgnoreParent(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)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -26,9 +26,8 @@ use nom::multi::many_till;
|
|||||||
use nom::sequence::tuple;
|
use nom::sequence::tuple;
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
|
|
||||||
use super::nom_context::ChainBehavior;
|
|
||||||
use super::nom_context::ContextElement;
|
|
||||||
use super::nom_context::ContextTree;
|
use super::nom_context::ContextTree;
|
||||||
|
use super::nom_context::OrgModeContextTree;
|
||||||
use super::parser_with_context::parser_with_context;
|
use super::parser_with_context::parser_with_context;
|
||||||
use super::text_element_parser::paragraph;
|
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)>> {
|
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
|
||||||
let initial_context = ContextTree::new();
|
let initial_context = ContextTree::new();
|
||||||
let paragraph_context = initial_context.with_additional_node(ContextElement {
|
let paragraph_context = initial_context.with_additional_fail_matcher(¶graph_end);
|
||||||
fail_matcher: ChainBehavior::AndParent(Some(¶graph_end)),
|
let ret = many1(parser_with_context!(paragraph)(¶graph_context))(input);
|
||||||
});
|
|
||||||
// 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);
|
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ use std::rc::Rc;
|
|||||||
use crate::parser::parser_with_context::parser_with_context;
|
use crate::parser::parser_with_context::parser_with_context;
|
||||||
use crate::parser::text::paragraph_end;
|
use crate::parser::text::paragraph_end;
|
||||||
|
|
||||||
|
use super::nom_context::OrgModeContext;
|
||||||
use super::text::bold_end;
|
use super::text::bold_end;
|
||||||
use super::text::bold_start;
|
use super::text::bold_start;
|
||||||
use super::text::line_break;
|
use super::text::line_break;
|
||||||
@ -23,7 +24,10 @@ use nom::multi::many_till;
|
|||||||
use nom::sequence::tuple;
|
use nom::sequence::tuple;
|
||||||
use nom::IResult;
|
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)?;
|
// context.not_matching_fail(i)?;
|
||||||
|
|
||||||
alt((
|
alt((
|
||||||
@ -36,7 +40,7 @@ fn flat_text_element<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextEleme
|
|||||||
))(i)
|
))(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 fail_matcher = recognize(bold_end);
|
||||||
// let new_context = context.with_additional_fail_matcher(Rc::new(RefCell::new(paragraph_end)));
|
// let new_context = context.with_additional_fail_matcher(Rc::new(RefCell::new(paragraph_end)));
|
||||||
// let new_context =
|
// 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>(
|
pub fn paragraph<'s, 'r>(
|
||||||
i: &'s str,
|
i: &'s str,
|
||||||
context: u32,
|
context: &'r OrgModeContext<'r>,
|
||||||
) -> Res<&'s str, (Vec<TextElement<'s>>, &'s str)> {
|
) -> Res<&'s str, (Vec<TextElement<'s>>, &'s str)> {
|
||||||
let text_element_parser = parser_with_context!(flat_text_element)(context);
|
let text_element_parser = parser_with_context!(flat_text_element)(context);
|
||||||
many_till(text_element_parser, paragraph_end)(i)
|
many_till(text_element_parser, paragraph_end)(i)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user