Implement the section parser.

This commit is contained in:
Tom Alexander 2023-03-24 17:00:27 -04:00
parent 754c1922df
commit dc9f3eb2e6
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 24 additions and 1 deletions

View File

@ -9,6 +9,7 @@ use nom::multi::many1;
use nom::multi::many1_count; use nom::multi::many1_count;
use nom::sequence::tuple; use nom::sequence::tuple;
use crate::parser::element::element;
use crate::parser::error::CustomError; use crate::parser::error::CustomError;
use crate::parser::error::MyError; use crate::parser::error::MyError;
use crate::parser::object::standard_set_object; 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")); .with_additional_node(ContextElement::Context("section"));
not(|i| parser_context.check_exit_matcher(i))(input)?; 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> { 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 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -1,6 +1,10 @@
use nom::combinator::not;
use super::error::Res;
use super::greater_element::PlainList; use super::greater_element::PlainList;
use super::lesser_element::Paragraph; use super::lesser_element::Paragraph;
use super::source::Source; use super::source::Source;
use super::Context;
#[derive(Debug)] #[derive(Debug)]
pub enum Element<'s> { 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!()
}