diff --git a/src/parser/footnote_reference.rs b/src/parser/footnote_reference.rs index e023164..ff2b117 100644 --- a/src/parser/footnote_reference.rs +++ b/src/parser/footnote_reference.rs @@ -1,6 +1,7 @@ use nom::branch::alt; use nom::bytes::complete::tag; use nom::bytes::complete::tag_no_case; +use nom::character::complete::space0; use nom::combinator::verify; use nom::multi::many_till; @@ -9,8 +10,10 @@ use super::Context; use crate::error::CustomError; use crate::error::MyError; use crate::error::Res; +use crate::parser::exiting::ExitClass; use crate::parser::footnote_definition::label; use crate::parser::object_parser::standard_set_object; +use crate::parser::parser_context::ExitMatcherNode; use crate::parser::parser_context::FootnoteReferenceDefinition; use crate::parser::parser_with_context::parser_with_context; use crate::parser::util::exit_matcher_parser; @@ -23,7 +26,11 @@ pub fn footnote_reference<'r, 's>( context: Context<'r, 's>, input: &'s str, ) -> Res<&'s str, FootnoteReference<'s>> { - alt((parser_with_context!(anonymous_footnote)(context),))(input) + alt(( + parser_with_context!(anonymous_footnote)(context), + parser_with_context!(footnote_reference_only)(context), + parser_with_context!(inline_footnote)(context), + ))(input) } #[tracing::instrument(ret, level = "debug")] @@ -32,21 +39,28 @@ 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 parser_context = context + .with_additional_node(ContextElement::FootnoteReferenceDefinition( + FootnoteReferenceDefinition { + position: remaining, + depth: 0, + }, + )) + .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &footnote_definition_end, + })); // TODO: I could insert FootnoteReferenceDefinition entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. 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 == &"]", + |(children, _exit_contents)| !children.is_empty(), )(remaining)?; + let (remaining, _) = tag("]")(remaining)?; + let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, @@ -66,21 +80,28 @@ fn inline_footnote<'r, 's>( let (remaining, _) = tag_no_case("[fn:")(input)?; let (remaining, label_contents) = label(remaining)?; let (remaining, _) = tag(":")(remaining)?; - let parser_context = context.with_additional_node(ContextElement::FootnoteReferenceDefinition( - FootnoteReferenceDefinition { - position: remaining, - depth: 0, - }, - )); + let parser_context = context + .with_additional_node(ContextElement::FootnoteReferenceDefinition( + FootnoteReferenceDefinition { + position: remaining, + depth: 0, + }, + )) + .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { + class: ExitClass::Beta, + exit_matcher: &footnote_definition_end, + })); // TODO: I could insert FootnoteReferenceDefinition entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. 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 == &"]", + |(children, _exit_contents)| !children.is_empty(), )(remaining)?; + let (remaining, _) = tag("]")(remaining)?; + let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, @@ -100,6 +121,7 @@ fn footnote_reference_only<'r, 's>( let (remaining, _) = tag_no_case("[fn:")(input)?; let (remaining, label_contents) = label(remaining)?; let (remaining, _) = tag("]")(remaining)?; + let (remaining, _) = space0(remaining)?; let source = get_consumed(input, remaining); Ok(( remaining, diff --git a/toy_language.txt b/toy_language.txt index 5540dd1..ad282ae 100644 --- a/toy_language.txt +++ b/toy_language.txt @@ -1,3 +1 @@ -foo \Delta bar -foo \pibar -foo \pi{}bar +[fn:2:This is a footnote reference since it has the definition inside the brackets. This style is referred to as an "inline footnote".]