diff --git a/src/parser/document.rs b/src/parser/document.rs index bec0cab..32c408f 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 eb7ed8c..ecfa993 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!() +}