Add paragraph parser.

This commit is contained in:
Tom Alexander 2022-10-15 14:16:52 -04:00
parent ba25f5b5ca
commit 9d534aa627
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 19 additions and 13 deletions

View File

@ -9,10 +9,10 @@ use nom::Parser;
type MatcherRef = Rc<RefCell<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>>; type MatcherRef = Rc<RefCell<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>>;
struct FailChecker<'a>(&'a NomContext<'a>); struct FailChecker<'r>(&'r NomContext<'r>);
impl<'a> FailChecker<'a> { impl<'r> FailChecker<'r> {
fn new(func: &'a NomContext<'a>) -> Self { fn new(func: &'r NomContext<'r>) -> Self {
Self(func) Self(func)
} }
} }
@ -22,15 +22,15 @@ enum ChainBehavior {
IgnoreParent(Option<MatcherRef>), IgnoreParent(Option<MatcherRef>),
} }
pub struct NomContext<'a> { pub struct NomContext<'r> {
parent: Option<&'a Self>, parent: Option<&'r Self>,
fail_matcher: ChainBehavior, fail_matcher: ChainBehavior,
/// You can't have nested bolds or links in org-mode /// You can't have nested bolds or links in org-mode
can_match_bold: bool, can_match_bold: bool,
can_match_link: bool, can_match_link: bool,
} }
impl<'a> NomContext<'a> { impl<'r> NomContext<'r> {
pub fn new(fail_matcher: MatcherRef) -> Self { pub fn new(fail_matcher: MatcherRef) -> Self {
NomContext { NomContext {
parent: None, parent: None,

View File

@ -1,6 +1,6 @@
macro_rules! parser_with_context { macro_rules! parser_with_context {
($target:ident) => { ($target:ident) => {
move |mut context| move |i| $target(i, &mut context) move |context| move |i| $target(i, context)
}; };
} }
pub(crate) use parser_with_context; pub(crate) use parser_with_context;

View File

@ -28,6 +28,7 @@ use nom::IResult;
use super::nom_context::NomContext; use super::nom_context::NomContext;
use super::parser_with_context::parser_with_context; use super::parser_with_context::parser_with_context;
use super::text_element_parser::paragraph;
pub type Res<T, U> = IResult<T, U, VerboseError<T>>; pub type Res<T, U> = IResult<T, U, VerboseError<T>>;
@ -137,6 +138,6 @@ 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 = NomContext::new(Rc::new(RefCell::new(paragraph_end))); let initial_context = NomContext::new(Rc::new(RefCell::new(paragraph_end)));
todo!() let ret = many1(parser_with_context!(paragraph)(&initial_context))(input);
// many1(parser_with_context!(paragraph)(initial_context))(input) ret
} }

View File

@ -21,10 +21,7 @@ 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>( fn flat_text_element<'s, 'r>(i: &'s str, context: &'r NomContext) -> Res<&'s str, TextElement<'s>> {
i: &'s str,
context: &'r mut NomContext,
) -> Res<&'s str, TextElement<'s>> {
context.not_matching_fail(i)?; context.not_matching_fail(i)?;
alt(( alt((
@ -36,3 +33,11 @@ fn flat_text_element<'s, 'r>(
map(line_break, TextElement::LineBreak), map(line_break, TextElement::LineBreak),
))(i) ))(i)
} }
pub fn paragraph<'s, 'r>(
i: &'s str,
context: &'r NomContext,
) -> 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)
}