From f2d16d302a3a16a2d421e9ec880ee2b608af2338 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 24 Mar 2023 17:34:56 -0400 Subject: [PATCH] Implement document parser. --- src/parser/document.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/parser/document.rs b/src/parser/document.rs index eeed9c53..a256f5ac 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -5,6 +5,7 @@ use nom::character::complete::space1; use nom::combinator::eof; use nom::combinator::map; use nom::combinator::not; +use nom::combinator::opt; use nom::combinator::recognize; use nom::combinator::verify; use nom::multi::many0; @@ -74,8 +75,19 @@ pub fn document(input: &str) -> Res<&str, Document> { let initial_context: ContextTree<'_, '_> = ContextTree::new(); let document_context = initial_context.with_additional_node(ContextElement::DocumentRoot(input)); - - todo!() + let section_matcher = parser_with_context!(section)(&document_context); + let heading_matcher = parser_with_context!(heading)(&document_context); + let (remaining, zeroth_section) = opt(section_matcher)(input)?; + let (remaining, children) = many0(heading_matcher)(remaining)?; + let source = get_consumed(input, remaining); + Ok(( + remaining, + Document { + source, + zeroth_section, + children, + }, + )) } fn section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Section<'s>> { @@ -114,7 +126,7 @@ fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Hea Ok(( remaining, Heading { - source: source, + source, stars: star_count, children, },