From dc9f3eb2e6c6fb289e08dcbe9c980e4eb7d9e267 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 24 Mar 2023 17:00:27 -0400 Subject: [PATCH] Implement the section parser. --- src/parser/document.rs | 16 +++++++++++++++- src/parser/element.rs | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/parser/document.rs b/src/parser/document.rs index bec0cab0..32c408ff 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -9,6 +9,7 @@ use nom::multi::many1; use nom::multi::many1_count; use nom::sequence::tuple; +use crate::parser::element::element; use crate::parser::error::CustomError; use crate::parser::error::MyError; use crate::parser::object::standard_set_object; @@ -81,7 +82,10 @@ fn section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Sec })) .with_additional_node(ContextElement::Context("section")); not(|i| parser_context.check_exit_matcher(i))(input)?; - todo!() + let element_matcher = parser_with_context!(element)(&parser_context); + let (remaining, children) = many1(element_matcher)(input)?; + let source = get_consumed(input, remaining); + Ok((remaining, Section { source, children })) } fn section_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { @@ -160,6 +164,16 @@ fn is_slice_of(parent: &str, child: &str) -> bool { child_start >= parent_start && child_end <= parent_end } +/// Get a slice of the string that was consumed in a parser using the original input to the parser and the remaining input after the parser. +fn get_consumed<'s>(input: &'s str, remaining: &'s str) -> &'s str { + assert!(is_slice_of(input, remaining)); + let source = { + let offset = remaining.as_ptr() as usize - input.as_ptr() as usize; + &input[..offset] + }; + source +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/parser/element.rs b/src/parser/element.rs index eb7ed8c5..ecfa993e 100644 --- a/src/parser/element.rs +++ b/src/parser/element.rs @@ -1,6 +1,10 @@ +use nom::combinator::not; + +use super::error::Res; use super::greater_element::PlainList; use super::lesser_element::Paragraph; use super::source::Source; +use super::Context; #[derive(Debug)] pub enum Element<'s> { @@ -16,3 +20,8 @@ impl<'s> Source<'s> for Element<'s> { } } } + +pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> { + not(|i| context.check_exit_matcher(i))(input)?; + todo!() +}