Implement heading parser.

This commit is contained in:
Tom Alexander 2023-03-24 17:19:46 -04:00
parent dc9f3eb2e6
commit 8013f127df
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -3,8 +3,11 @@ use nom::bytes::complete::tag;
use nom::character::complete::line_ending; use nom::character::complete::line_ending;
use nom::character::complete::space1; use nom::character::complete::space1;
use nom::combinator::eof; use nom::combinator::eof;
use nom::combinator::map;
use nom::combinator::not; use nom::combinator::not;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many1; use nom::multi::many1;
use nom::multi::many1_count; use nom::multi::many1_count;
use nom::sequence::tuple; use nom::sequence::tuple;
@ -35,6 +38,7 @@ pub struct Document<'s> {
#[derive(Debug)] #[derive(Debug)]
pub struct Heading<'s> { pub struct Heading<'s> {
pub source: &'s str, pub source: &'s str,
pub stars: usize,
pub children: Vec<DocumentElement<'s>>, pub children: Vec<DocumentElement<'s>>,
} }
@ -96,7 +100,25 @@ fn section_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Heading<'s>> { fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Heading<'s>> {
not(|i| context.check_exit_matcher(i))(input)?; not(|i| context.check_exit_matcher(i))(input)?;
let (remaining, (star_count, _ws, title, _ws2)) = headline(context, input)?; let (remaining, (star_count, _ws, title, _ws2)) = headline(context, input)?;
todo!() let section_matcher = parser_with_context!(section)(context);
// TODO: This needs to only match headings below the current level
let heading_matcher = parser_with_context!(heading)(context);
let (remaining, children) = many0(alt((
map(
verify(heading_matcher, |h| h.stars > star_count),
DocumentElement::Heading,
),
map(section_matcher, DocumentElement::Section),
)))(remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
Heading {
source: source,
stars: star_count,
children,
},
))
} }
fn headline<'r, 's>( fn headline<'r, 's>(