From 6822069c2f1fd65104c3534da01cd6c3a0ea0545 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 19 Jul 2023 21:14:09 -0400 Subject: [PATCH] Implement the parser for anonymous footnotes. --- src/parser/footnote_reference.rs | 43 ++++++++++++++++++++++++++------ src/parser/object.rs | 2 +- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index 6d88df0..34c8428 100644 --- a/src/parser/footnote_reference.rs +++ b/src/parser/footnote_reference.rs @@ -1,14 +1,18 @@ use nom::branch::alt; use nom::bytes::complete::tag; use nom::bytes::complete::tag_no_case; +use nom::combinator::verify; +use nom::multi::many_till; use super::parser_context::ContextElement; use super::Context; use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; +use crate::parser::object_parser::standard_set_object; use crate::parser::parser_context::FootnoteReferenceDefinition; use crate::parser::parser_with_context::parser_with_context; +use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::not_yet_implemented; use crate::parser::FootnoteReference; @@ -28,9 +32,29 @@ pub fn anonymous_footnote<'r, 's>( input: &'s str, ) -> Res<&'s str, FootnoteReference<'s>> { let (remaining, _) = tag_no_case("[fn::")(input)?; + let parser_context = context.with_additional_node(ContextElement::FootnoteReferenceDefinition( + FootnoteReferenceDefinition { + position: remaining, + depth: 0, + }, + )); + let (remaining, (children, _exit_contents)) = verify( + many_till( + parser_with_context!(standard_set_object)(&parser_context), + parser_with_context!(exit_matcher_parser)(&parser_context), + ), + |(children, exit_contents)| !children.is_empty() && exit_contents == &"]", + )(remaining)?; - not_yet_implemented()?; - todo!() + let source = get_consumed(input, remaining); + Ok(( + remaining, + FootnoteReference { + source, + label: None, + definition: children, + }, + )) } #[tracing::instrument(ret, level = "debug")] @@ -43,24 +67,29 @@ fn footnote_definition_end<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, &'s str> { - let context_depth = get_bracket_depth(context).expect("This function should only be called from inside a footnote definition."); + let context_depth = get_bracket_depth(context) + .expect("This function should only be called from inside a footnote definition."); let text_since_context_entry = get_consumed(context_depth.position, input); let mut current_depth = context_depth.depth; for c in text_since_context_entry.chars() { match c { - '[' => {current_depth += 1;}, + '[' => { + current_depth += 1; + } ']' if current_depth == 0 => { panic!("Exceeded footnote reference definition bracket depth.") - }, + } ']' if current_depth > 0 => { current_depth -= 1; - }, + } _ => {} } } if current_depth > 0 { // Its impossible for the next character to end the footnote reference definition if we're any amount of brackets deep - return Err(nom::Err::Error(CustomError::MyError(MyError("NoFootnoteReferenceDefinitionEnd")))); + return Err(nom::Err::Error(CustomError::MyError(MyError( + "NoFootnoteReferenceDefinitionEnd", + )))); } tag("]")(input) } diff --git a/src/parser/object.rs b/src/parser/object.rs index 481408f..42e4401 100644 --- a/src/parser/object.rs +++ b/src/parser/object.rs @@ -121,7 +121,7 @@ pub struct ExportSnippet<'s> { #[derive(Debug, PartialEq)] pub struct FootnoteReference<'s> { pub source: &'s str, - pub label: &'s str, + pub label: Option<&'s str>, pub definition: Vec>, }