Expose a document parser and implement the paragraph parser so something is hitting our macro.

This commit is contained in:
Tom Alexander 2022-07-16 18:26:53 -04:00
parent 040671b98f
commit fdd5b532fc
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 14 additions and 8 deletions

View File

@ -1,6 +1,4 @@
use nom::multi::many1;
use crate::parser::paragraph;
use crate::parser::document;
mod parser;
@ -9,6 +7,6 @@ const TEST_DOC: &'static str = include_str!("../toy_language.txt");
fn main() -> Result<(), Box<dyn std::error::Error>> {
pretty_env_logger::init();
println!("{}\n\n\n", TEST_DOC);
println!("{:#?}", many1(paragraph)(TEST_DOC));
println!("{:#?}", document(TEST_DOC));
Ok(())
}

View File

@ -2,4 +2,4 @@ mod nom_context;
mod parser_with_context;
mod text;
mod text_element_parser;
pub use text::paragraph;
pub use text::document;

View File

@ -1,7 +1,7 @@
macro_rules! parser_with_context {
($name:ident,$typ:ty,$inp:ident,$context:ident,$fnbody:block) => {
pub fn $name<'c, I: Clone, O, E: nom::error::ParseError<I>>(
$context: &mut NomContext<'c, I, O, E>,
$context: NomContext<'c, I, O, E>,
) -> impl for<'a> FnMut(&'a str) -> IResult<&'a str, $typ, VerboseError<&'a str>> + 'c {
|$inp| $fnbody
}

View File

@ -18,10 +18,14 @@ use nom::character::complete::space1;
use nom::combinator::map;
use nom::combinator::recognize;
use nom::error::VerboseError;
use nom::multi::many1;
use nom::multi::many_till;
use nom::sequence::tuple;
use nom::IResult;
use super::nom_context::NomContext;
use super::text_element_parser::text_element;
pub type Res<T, U> = IResult<T, U, VerboseError<T>>;
#[derive(Debug)]
@ -125,10 +129,14 @@ pub fn link_end(input: &str) -> Res<&str, TextElement> {
}
pub fn paragraph(input: &str) -> Res<&str, (Vec<TextElement>, &str)> {
todo!()
// many_till(TextElementParser::new(paragraph_end), paragraph_end)(input)
let initial_context = NomContext::new(&paragraph_end);
many_till(text_element(initial_context), paragraph_end)(input)
}
fn paragraph_end(input: &str) -> Res<&str, &str> {
recognize(tuple((map(line_break, TextElement::LineBreak), blank_line)))(input)
}
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
many1(paragraph)(input)
}